blob: 135293dd2d64458d0d1cdfb862aa741f40f34e0d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
Bram Moolenaar597a4222014-06-25 14:39:50 +020044 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
Bram Moolenaar597a4222014-06-25 14:39:50 +020055 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_str(
65 char_u *ptr,
66 int ts,
67 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 if (*ptr == TAB)
74 {
75 if (!list || lcs_tab1) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020078 /* In list mode, when tab is not set, count screen char width
79 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020080 count += ptr2cells(ptr);
81 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else if (*ptr == ' ')
83 ++count; /* count a space for one */
84 else
85 break;
86 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000088}
89
90/*
91 * Set the indent of the current line.
92 * Leaves the cursor on the first non-blank in the line.
93 * Caller must take care of undo.
94 * "flags":
95 * SIN_CHANGED: call changed_bytes() if the line was changed.
96 * SIN_INSERT: insert the indent in front of the line.
97 * SIN_UNDO: save line for undo before changing it.
98 * Returns TRUE if the line was changed.
99 */
100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100101set_indent(
102 int size, /* measured in spaces */
103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 char_u *p;
106 char_u *newline;
107 char_u *oldline;
108 char_u *s;
109 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 int line_len;
112 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000115 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000116 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119 /*
120 * First check if there is anything to do and compute the number of
121 * characters needed for the indent.
122 */
123 todo = size;
124 ind_len = 0;
125 p = oldline = ml_get_curline();
126
127 /* Calculate the buffer size for the new indent, and check to see if it
128 * isn't already set */
129
Bram Moolenaar5002c292007-07-24 13:26:15 +0000130 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
131 * 'preserveindent' are set count the number of characters at the
132 * beginning of the line to be copied */
133 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 /* If 'preserveindent' is set then reuse as much as possible of
136 * the existing indent structure for the new indent */
137 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
138 {
139 ind_done = 0;
140
141 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100142 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {
144 if (*p == TAB)
145 {
146 tab_pad = (int)curbuf->b_p_ts
147 - (ind_done % (int)curbuf->b_p_ts);
148 /* stop if this tab will overshoot the target */
149 if (todo < tab_pad)
150 break;
151 todo -= tab_pad;
152 ++ind_len;
153 ind_done += tab_pad;
154 }
155 else
156 {
157 --todo;
158 ++ind_len;
159 ++ind_done;
160 }
161 ++p;
162 }
163
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 /* Set initial number of whitespace chars to copy if we are
165 * preserving indent but expandtab is set */
166 if (curbuf->b_p_et)
167 orig_char_len = ind_len;
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* Fill to next tabstop with a tab, if possible */
170 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000171 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 doit = TRUE;
174 todo -= tab_pad;
175 ++ind_len;
176 /* ind_done += tab_pad; */
177 }
178 }
179
180 /* count tabs required for indent */
181 while (todo >= (int)curbuf->b_p_ts)
182 {
183 if (*p != TAB)
184 doit = TRUE;
185 else
186 ++p;
187 todo -= (int)curbuf->b_p_ts;
188 ++ind_len;
189 /* ind_done += (int)curbuf->b_p_ts; */
190 }
191 }
192 /* count spaces required for indent */
193 while (todo > 0)
194 {
195 if (*p != ' ')
196 doit = TRUE;
197 else
198 ++p;
199 --todo;
200 ++ind_len;
201 /* ++ind_done; */
202 }
203
204 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100205 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 return FALSE;
207
208 /* Allocate memory for the new line. */
209 if (flags & SIN_INSERT)
210 p = oldline;
211 else
212 p = skipwhite(p);
213 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214
215 /* If 'preserveindent' and 'expandtab' are both set keep the original
216 * characters and allocate accordingly. We will fill the rest with spaces
217 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 {
220 newline = alloc(orig_char_len + size - ind_done + line_len);
221 if (newline == NULL)
222 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000223 todo = size - ind_done;
224 ind_len = orig_char_len + todo; /* Set total length of indent in
225 * characters, which may have been
226 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 p = oldline;
228 s = newline;
229 while (orig_char_len > 0)
230 {
231 *s++ = *p++;
232 orig_char_len--;
233 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 /* Skip over any additional white space (useful when newindent is less
236 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100237 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000239
Bram Moolenaar5002c292007-07-24 13:26:15 +0000240 }
241 else
242 {
243 todo = size;
244 newline = alloc(ind_len + line_len);
245 if (newline == NULL)
246 return FALSE;
247 s = newline;
248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* if 'expandtab' isn't set: use TABs */
252 if (!curbuf->b_p_et)
253 {
254 /* If 'preserveindent' is set then reuse as much as possible of
255 * the existing indent structure for the new indent */
256 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
257 {
258 p = oldline;
259 ind_done = 0;
260
Bram Moolenaar1c465442017-03-12 20:10:05 +0100261 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 if (*p == TAB)
264 {
265 tab_pad = (int)curbuf->b_p_ts
266 - (ind_done % (int)curbuf->b_p_ts);
267 /* stop if this tab will overshoot the target */
268 if (todo < tab_pad)
269 break;
270 todo -= tab_pad;
271 ind_done += tab_pad;
272 }
273 else
274 {
275 --todo;
276 ++ind_done;
277 }
278 *s++ = *p++;
279 }
280
281 /* Fill to next tabstop with a tab, if possible */
282 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
283 if (todo >= tab_pad)
284 {
285 *s++ = TAB;
286 todo -= tab_pad;
287 }
288
289 p = skipwhite(p);
290 }
291
292 while (todo >= (int)curbuf->b_p_ts)
293 {
294 *s++ = TAB;
295 todo -= (int)curbuf->b_p_ts;
296 }
297 }
298 while (todo > 0)
299 {
300 *s++ = ' ';
301 --todo;
302 }
303 mch_memmove(s, p, (size_t)line_len);
304
305 /* Replace the line (unless undo fails). */
306 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
307 {
308 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
309 if (flags & SIN_CHANGED)
310 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200311 /* Correct saved cursor position if it is in this line. */
312 if (saved_cursor.lnum == curwin->w_cursor.lnum)
313 {
314 if (saved_cursor.col >= (colnr_T)(p - oldline))
315 /* cursor was after the indent, adjust for the number of
316 * bytes added/removed */
317 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
318 else if (saved_cursor.col >= (colnr_T)(s - newline))
319 /* cursor was in the indent, and is now after it, put it back
320 * at the start of the indent (replacing spaces with TAB) */
321 saved_cursor.col = (colnr_T)(s - newline);
322 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000323 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 else
326 vim_free(newline);
327
328 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000329 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Copy the indent from ptr to the current line (and fill to size)
334 * Leaves the cursor on the first non-blank in the line.
335 * Returns TRUE if the line was changed.
336 */
337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 char_u *p = NULL;
341 char_u *line = NULL;
342 char_u *s;
343 int todo;
344 int ind_len;
345 int line_len = 0;
346 int tab_pad;
347 int ind_done;
348 int round;
349
350 /* Round 1: compute the number of characters needed for the indent
351 * Round 2: copy the characters. */
352 for (round = 1; round <= 2; ++round)
353 {
354 todo = size;
355 ind_len = 0;
356 ind_done = 0;
357 s = src;
358
359 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100360 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
362 if (*s == TAB)
363 {
364 tab_pad = (int)curbuf->b_p_ts
365 - (ind_done % (int)curbuf->b_p_ts);
366 /* Stop if this tab will overshoot the target */
367 if (todo < tab_pad)
368 break;
369 todo -= tab_pad;
370 ind_done += tab_pad;
371 }
372 else
373 {
374 --todo;
375 ++ind_done;
376 }
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = *s;
380 ++s;
381 }
382
383 /* Fill to next tabstop with a tab, if possible */
384 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200385 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
387 todo -= tab_pad;
388 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000389 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 *p++ = TAB;
391 }
392
393 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200394 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 todo -= (int)curbuf->b_p_ts;
397 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 *p++ = TAB;
400 }
401
402 /* Count/add spaces required for indent */
403 while (todo > 0)
404 {
405 --todo;
406 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000407 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 *p++ = ' ';
409 }
410
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 /* Allocate memory for the result: the copied indent, new indent
414 * and the rest of the line. */
415 line_len = (int)STRLEN(ml_get_curline()) + 1;
416 line = alloc(ind_len + line_len);
417 if (line == NULL)
418 return FALSE;
419 p = line;
420 }
421 }
422
423 /* Append the original line */
424 mch_memmove(p, ml_get_curline(), (size_t)line_len);
425
426 /* Replace the line */
427 ml_replace(curwin->w_cursor.lnum, line, FALSE);
428
429 /* Put the cursor after the indent. */
430 curwin->w_cursor.col = ind_len;
431 return TRUE;
432}
433
434/*
435 * Return the indent of the current line after a number. Return -1 if no
436 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 colnr_T col;
443 pos_T pos;
444
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch_T regmatch;
446 int lead_len = 0; /* length of comment leader */
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (lnum > curbuf->b_ml.ml_line_count)
449 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000450 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
452#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
454 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
458 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 /* vim_regexec() expects a pointer to a line. This lets us
463 * start matching for the flp beyond any comment leader... */
464 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 pos.lnum = lnum;
467 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200472 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000474
475 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 getvcol(curwin, &pos, &col, NULL, NULL);
478 return (int)col;
479}
480
Bram Moolenaar597a4222014-06-25 14:39:50 +0200481#if defined(FEAT_LINEBREAK) || defined(PROTO)
482/*
483 * Return appropriate space number for breakindent, taking influencing
484 * parameters into account. Window must be specified, since it is not
485 * necessarily always the current one.
486 */
487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488get_breakindent_win(
489 win_T *wp,
490 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491{
492 static int prev_indent = 0; /* cached indent value */
493 static long prev_ts = 0L; /* cached tabstop value */
494 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100495 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496 int bri = 0;
497 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200498 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100505 || prev_tick != CHANGEDTICK(wp->w_buffer))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200511 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200513 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514
515 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 if (wp->w_p_brisbr)
517 bri -= vim_strsize(p_sbr);
518
519 /* Add offset for number column, if 'n' is in 'cpoptions' */
520 bri += win_col_off2(wp);
521
522 /* never indent past left window margin */
523 if (bri < 0)
524 bri = 0;
525 /* always leave at least bri_min characters on the left,
526 * if text width is sufficient */
527 else if (bri > eff_wwidth - wp->w_p_brimin)
528 bri = (eff_wwidth - wp->w_p_brimin < 0)
529 ? 0 : eff_wwidth - wp->w_p_brimin;
530
531 return bri;
532}
533#endif
534
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
537
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100538static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
541 * Return TRUE if the string "line" starts with a word from 'cinwords'.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *cinw;
547 char_u *cinw_buf;
548 int cinw_len;
549 int retval = FALSE;
550 int len;
551
552 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
553 cinw_buf = alloc((unsigned)cinw_len);
554 if (cinw_buf != NULL)
555 {
556 line = skipwhite(line);
557 for (cinw = curbuf->b_p_cinw; *cinw; )
558 {
559 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
560 if (STRNCMP(line, cinw_buf, len) == 0
561 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
562 {
563 retval = TRUE;
564 break;
565 }
566 }
567 vim_free(cinw_buf);
568 }
569 return retval;
570}
571#endif
572
573/*
574 * open_line: Add a new line below or above the current line.
575 *
576 * For VREPLACE mode, we only add a new line when we get to the end of the
577 * file, otherwise we just start replacing the next line.
578 *
579 * Caller must take care of undo. Since VREPLACE may affect any number of
580 * lines however, it may call u_save_cursor() again when starting to change a
581 * new line.
582 * "flags": OPENLINE_DELSPACES delete spaces after cursor
583 * OPENLINE_DO_COM format comments
584 * OPENLINE_KEEPTRAIL keep trailing spaces
585 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200586 * OPENLINE_COM_LIST format comments with list or 2nd line indent
587 *
588 * "second_line_indent": indent for after ^^D in Insert mode or if flag
589 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *
591 * Return TRUE for success, FALSE for failure
592 */
593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594open_line(
595 int dir, /* FORWARD or BACKWARD */
596 int flags,
597 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 char_u *saved_line; /* copy of the original line */
600 char_u *next_line = NULL; /* copy of the next line */
601 char_u *p_extra = NULL; /* what goes to next line */
602 int less_cols = 0; /* less columns for mark in new line */
603 int less_cols_off = 0; /* columns to skip for mark adjust */
604 pos_T old_cursor; /* old cursor position */
605 int newcol = 0; /* new cursor column */
606 int newindent = 0; /* auto-indent of the new line */
607 int n;
608 int trunc_line = FALSE; /* truncate current line afterwards */
609 int retval = FALSE; /* return value, default is FAIL */
610#ifdef FEAT_COMMENTS
611 int extra_len = 0; /* length of p_extra string */
612 int lead_len; /* length of comment leader */
613 char_u *lead_flags; /* position in 'comments' for comment leader */
614 char_u *leader = NULL; /* copy of comment leader */
615#endif
616 char_u *allocated = NULL; /* allocated memory */
617#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
618 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
619 char_u *p;
620#endif
621 int saved_char = NUL; /* init for GCC */
622#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
623 pos_T *pos;
624#endif
625#ifdef FEAT_SMARTINDENT
626 int do_si = (!p_paste && curbuf->b_p_si
627# ifdef FEAT_CINDENT
628 && !curbuf->b_p_cin
629# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200630# ifdef FEAT_EVAL
631 && *curbuf->b_p_inde == NUL
632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 );
634 int no_si = FALSE; /* reset did_si afterwards */
635 int first_char = NUL; /* init for GCC */
636#endif
637#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
638 int vreplace_mode;
639#endif
640 int did_append; /* appended a new line */
641 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
642
643 /*
644 * make a copy of the current line so we can mess with it
645 */
646 saved_line = vim_strsave(ml_get_curline());
647 if (saved_line == NULL) /* out of memory! */
648 return FALSE;
649
650#ifdef FEAT_VREPLACE
651 if (State & VREPLACE_FLAG)
652 {
653 /*
654 * With VREPLACE we make a copy of the next line, which we will be
655 * starting to replace. First make the new line empty and let vim play
656 * with the indenting and comment leader to its heart's content. Then
657 * we grab what it ended up putting on the new line, put back the
658 * original line, and call ins_char() to put each new character onto
659 * the line, replacing what was there before and pushing the right
660 * stuff onto the replace stack. -- webb.
661 */
662 if (curwin->w_cursor.lnum < orig_line_count)
663 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
664 else
665 next_line = vim_strsave((char_u *)"");
666 if (next_line == NULL) /* out of memory! */
667 goto theend;
668
669 /*
670 * In VREPLACE mode, a NL replaces the rest of the line, and starts
671 * replacing the next line, so push all of the characters left on the
672 * line onto the replace stack. We'll push any other characters that
673 * might be replaced at the start of the next line (due to autoindent
674 * etc) a bit later.
675 */
676 replace_push(NUL); /* Call twice because BS over NL expects it */
677 replace_push(NUL);
678 p = saved_line + curwin->w_cursor.col;
679 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000680 {
681#ifdef FEAT_MBYTE
682 if (has_mbyte)
683 p += replace_push_mb(p);
684 else
685#endif
686 replace_push(*p++);
687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 saved_line[curwin->w_cursor.col] = NUL;
689 }
690#endif
691
692 if ((State & INSERT)
693#ifdef FEAT_VREPLACE
694 && !(State & VREPLACE_FLAG)
695#endif
696 )
697 {
698 p_extra = saved_line + curwin->w_cursor.col;
699#ifdef FEAT_SMARTINDENT
700 if (do_si) /* need first char after new line break */
701 {
702 p = skipwhite(p_extra);
703 first_char = *p;
704 }
705#endif
706#ifdef FEAT_COMMENTS
707 extra_len = (int)STRLEN(p_extra);
708#endif
709 saved_char = *p_extra;
710 *p_extra = NUL;
711 }
712
713 u_clearline(); /* cannot do "U" command when adding lines */
714#ifdef FEAT_SMARTINDENT
715 did_si = FALSE;
716#endif
717 ai_col = 0;
718
719 /*
720 * If we just did an auto-indent, then we didn't type anything on
721 * the prior line, and it should be truncated. Do this even if 'ai' is not
722 * set because automatically inserting a comment leader also sets did_ai.
723 */
724 if (dir == FORWARD && did_ai)
725 trunc_line = TRUE;
726
727 /*
728 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
729 * indent to use for the new line.
730 */
731 if (curbuf->b_p_ai
732#ifdef FEAT_SMARTINDENT
733 || do_si
734#endif
735 )
736 {
737 /*
738 * count white space on current line
739 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200740 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200741 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
742 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744#ifdef FEAT_SMARTINDENT
745 /*
746 * Do smart indenting.
747 * In insert/replace mode (only when dir == FORWARD)
748 * we may move some text to the next line. If it starts with '{'
749 * don't add an indent. Fixes inserting a NL before '{' in line
750 * "if (condition) {"
751 */
752 if (!trunc_line && do_si && *saved_line != NUL
753 && (p_extra == NULL || first_char != '{'))
754 {
755 char_u *ptr;
756 char_u last_char;
757
758 old_cursor = curwin->w_cursor;
759 ptr = saved_line;
760# ifdef FEAT_COMMENTS
761 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200762 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 else
764 lead_len = 0;
765# endif
766 if (dir == FORWARD)
767 {
768 /*
769 * Skip preprocessor directives, unless they are
770 * recognised as comments.
771 */
772 if (
773# ifdef FEAT_COMMENTS
774 lead_len == 0 &&
775# endif
776 ptr[0] == '#')
777 {
778 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
779 ptr = ml_get(--curwin->w_cursor.lnum);
780 newindent = get_indent();
781 }
782# ifdef FEAT_COMMENTS
783 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200784 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 else
786 lead_len = 0;
787 if (lead_len > 0)
788 {
789 /*
790 * This case gets the following right:
791 * \*
792 * * A comment (read '\' as '/').
793 * *\
794 * #define IN_THE_WAY
795 * This should line up here;
796 */
797 p = skipwhite(ptr);
798 if (p[0] == '/' && p[1] == '*')
799 p++;
800 if (p[0] == '*')
801 {
802 for (p++; *p; p++)
803 {
804 if (p[0] == '/' && p[-1] == '*')
805 {
806 /*
807 * End of C comment, indent should line up
808 * with the line containing the start of
809 * the comment
810 */
811 curwin->w_cursor.col = (colnr_T)(p - ptr);
812 if ((pos = findmatch(NULL, NUL)) != NULL)
813 {
814 curwin->w_cursor.lnum = pos->lnum;
815 newindent = get_indent();
816 }
817 }
818 }
819 }
820 }
821 else /* Not a comment line */
822# endif
823 {
824 /* Find last non-blank in line */
825 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100826 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 --p;
828 last_char = *p;
829
830 /*
831 * find the character just before the '{' or ';'
832 */
833 if (last_char == '{' || last_char == ';')
834 {
835 if (p > ptr)
836 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100837 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 --p;
839 }
840 /*
841 * Try to catch lines that are split over multiple
842 * lines. eg:
843 * if (condition &&
844 * condition) {
845 * Should line up here!
846 * }
847 */
848 if (*p == ')')
849 {
850 curwin->w_cursor.col = (colnr_T)(p - ptr);
851 if ((pos = findmatch(NULL, '(')) != NULL)
852 {
853 curwin->w_cursor.lnum = pos->lnum;
854 newindent = get_indent();
855 ptr = ml_get_curline();
856 }
857 }
858 /*
859 * If last character is '{' do indent, without
860 * checking for "if" and the like.
861 */
862 if (last_char == '{')
863 {
864 did_si = TRUE; /* do indent */
865 no_si = TRUE; /* don't delete it when '{' typed */
866 }
867 /*
868 * Look for "if" and the like, use 'cinwords'.
869 * Don't do this if the previous line ended in ';' or
870 * '}'.
871 */
872 else if (last_char != ';' && last_char != '}'
873 && cin_is_cinword(ptr))
874 did_si = TRUE;
875 }
876 }
877 else /* dir == BACKWARD */
878 {
879 /*
880 * Skip preprocessor directives, unless they are
881 * recognised as comments.
882 */
883 if (
884# ifdef FEAT_COMMENTS
885 lead_len == 0 &&
886# endif
887 ptr[0] == '#')
888 {
889 int was_backslashed = FALSE;
890
891 while ((ptr[0] == '#' || was_backslashed) &&
892 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
893 {
894 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
895 was_backslashed = TRUE;
896 else
897 was_backslashed = FALSE;
898 ptr = ml_get(++curwin->w_cursor.lnum);
899 }
900 if (was_backslashed)
901 newindent = 0; /* Got to end of file */
902 else
903 newindent = get_indent();
904 }
905 p = skipwhite(ptr);
906 if (*p == '}') /* if line starts with '}': do indent */
907 did_si = TRUE;
908 else /* can delete indent when '{' typed */
909 can_si_back = TRUE;
910 }
911 curwin->w_cursor = old_cursor;
912 }
913 if (do_si)
914 can_si = TRUE;
915#endif /* FEAT_SMARTINDENT */
916
917 did_ai = TRUE;
918 }
919
920#ifdef FEAT_COMMENTS
921 /*
922 * Find out if the current line starts with a comment leader.
923 * This may then be inserted in front of the new line.
924 */
925 end_comment_pending = NUL;
926 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200927 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 lead_len = 0;
930 if (lead_len > 0)
931 {
932 char_u *lead_repl = NULL; /* replaces comment leader */
933 int lead_repl_len = 0; /* length of *lead_repl */
934 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
935 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
936 char_u *comment_end = NULL; /* where lead_end has been found */
937 int extra_space = FALSE; /* append extra space */
938 int current_flag;
939 int require_blank = FALSE; /* requires blank after middle */
940 char_u *p2;
941
942 /*
943 * If the comment leader has the start, middle or end flag, it may not
944 * be used or may be replaced with the middle leader.
945 */
946 for (p = lead_flags; *p && *p != ':'; ++p)
947 {
948 if (*p == COM_BLANK)
949 {
950 require_blank = TRUE;
951 continue;
952 }
953 if (*p == COM_START || *p == COM_MIDDLE)
954 {
955 current_flag = *p;
956 if (*p == COM_START)
957 {
958 /*
959 * Doing "O" on a start of comment does not insert leader.
960 */
961 if (dir == BACKWARD)
962 {
963 lead_len = 0;
964 break;
965 }
966
967 /* find start of middle part */
968 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
969 require_blank = FALSE;
970 }
971
972 /*
973 * Isolate the strings of the middle and end leader.
974 */
975 while (*p && p[-1] != ':') /* find end of middle flags */
976 {
977 if (*p == COM_BLANK)
978 require_blank = TRUE;
979 ++p;
980 }
981 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
982
983 while (*p && p[-1] != ':') /* find end of end flags */
984 {
985 /* Check whether we allow automatic ending of comments */
986 if (*p == COM_AUTO_END)
987 end_comment_pending = -1; /* means we want to set it */
988 ++p;
989 }
990 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
991
992 if (end_comment_pending == -1) /* we can set it now */
993 end_comment_pending = lead_end[n - 1];
994
995 /*
996 * If the end of the comment is in the same line, don't use
997 * the comment leader.
998 */
999 if (dir == FORWARD)
1000 {
1001 for (p = saved_line + lead_len; *p; ++p)
1002 if (STRNCMP(p, lead_end, n) == 0)
1003 {
1004 comment_end = p;
1005 lead_len = 0;
1006 break;
1007 }
1008 }
1009
1010 /*
1011 * Doing "o" on a start of comment inserts the middle leader.
1012 */
1013 if (lead_len > 0)
1014 {
1015 if (current_flag == COM_START)
1016 {
1017 lead_repl = lead_middle;
1018 lead_repl_len = (int)STRLEN(lead_middle);
1019 }
1020
1021 /*
1022 * If we have hit RETURN immediately after the start
1023 * comment leader, then put a space after the middle
1024 * comment leader on the next line.
1025 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001026 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 && ((p_extra != NULL
1028 && (int)curwin->w_cursor.col == lead_len)
1029 || (p_extra == NULL
1030 && saved_line[lead_len] == NUL)
1031 || require_blank))
1032 extra_space = TRUE;
1033 }
1034 break;
1035 }
1036 if (*p == COM_END)
1037 {
1038 /*
1039 * Doing "o" on the end of a comment does not insert leader.
1040 * Remember where the end is, might want to use it to find the
1041 * start (for C-comments).
1042 */
1043 if (dir == FORWARD)
1044 {
1045 comment_end = skipwhite(saved_line);
1046 lead_len = 0;
1047 break;
1048 }
1049
1050 /*
1051 * Doing "O" on the end of a comment inserts the middle leader.
1052 * Find the string for the middle leader, searching backwards.
1053 */
1054 while (p > curbuf->b_p_com && *p != ',')
1055 --p;
1056 for (lead_repl = p; lead_repl > curbuf->b_p_com
1057 && lead_repl[-1] != ':'; --lead_repl)
1058 ;
1059 lead_repl_len = (int)(p - lead_repl);
1060
1061 /* We can probably always add an extra space when doing "O" on
1062 * the comment-end */
1063 extra_space = TRUE;
1064
1065 /* Check whether we allow automatic ending of comments */
1066 for (p2 = p; *p2 && *p2 != ':'; p2++)
1067 {
1068 if (*p2 == COM_AUTO_END)
1069 end_comment_pending = -1; /* means we want to set it */
1070 }
1071 if (end_comment_pending == -1)
1072 {
1073 /* Find last character in end-comment string */
1074 while (*p2 && *p2 != ',')
1075 p2++;
1076 end_comment_pending = p2[-1];
1077 }
1078 break;
1079 }
1080 if (*p == COM_FIRST)
1081 {
1082 /*
1083 * Comment leader for first line only: Don't repeat leader
1084 * when using "O", blank out leader when using "o".
1085 */
1086 if (dir == BACKWARD)
1087 lead_len = 0;
1088 else
1089 {
1090 lead_repl = (char_u *)"";
1091 lead_repl_len = 0;
1092 }
1093 break;
1094 }
1095 }
1096 if (lead_len)
1097 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001098 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001099 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001100 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 allocated = leader; /* remember to free it later */
1102
1103 if (leader == NULL)
1104 lead_len = 0;
1105 else
1106 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001107 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 /*
1110 * Replace leader with lead_repl, right or left adjusted
1111 */
1112 if (lead_repl != NULL)
1113 {
1114 int c = 0;
1115 int off = 0;
1116
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else if (VIM_ISDIGIT(*p) || *p == '-')
1122 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001123 else
1124 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 if (c == COM_RIGHT) /* right adjusted leader */
1127 {
1128 /* find last non-white in the leader to line up with */
1129 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001130 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001133
1134#ifdef FEAT_MBYTE
1135 /* Compute the length of the replaced characters in
1136 * screen characters, not bytes. */
1137 {
1138 int repl_size = vim_strnsize(lead_repl,
1139 lead_repl_len);
1140 int old_size = 0;
1141 char_u *endp = p;
1142 int l;
1143
1144 while (old_size < repl_size && p > leader)
1145 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001146 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 old_size += ptr2cells(p);
1148 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001149 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001150 if (l != 0)
1151 mch_memmove(endp + l, endp,
1152 (size_t)((leader + lead_len) - endp));
1153 lead_len += l;
1154 }
1155#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (p < leader + lead_repl_len)
1157 p = leader;
1158 else
1159 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1162 if (p + lead_repl_len > leader + lead_len)
1163 p[lead_repl_len] = NUL;
1164
1165 /* blank-out any other chars from the old leader. */
1166 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001167 {
1168#ifdef FEAT_MBYTE
1169 int l = mb_head_off(leader, p);
1170
1171 if (l > 1)
1172 {
1173 p -= l;
1174 if (ptr2cells(p) > 1)
1175 {
1176 p[1] = ' ';
1177 --l;
1178 }
1179 mch_memmove(p + 1, p + l + 1,
1180 (size_t)((leader + lead_len) - (p + l + 1)));
1181 lead_len -= l;
1182 *p = ' ';
1183 }
1184 else
1185#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001186 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else /* left adjusted leader */
1191 {
1192 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001193#ifdef FEAT_MBYTE
1194 /* Compute the length of the replaced characters in
1195 * screen characters, not bytes. Move the part that is
1196 * not to be overwritten. */
1197 {
1198 int repl_size = vim_strnsize(lead_repl,
1199 lead_repl_len);
1200 int i;
1201 int l;
1202
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001203 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001204 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001205 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001206 if (vim_strnsize(p, i + l) > repl_size)
1207 break;
1208 }
1209 if (i != lead_repl_len)
1210 {
1211 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001212 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001213 lead_len += lead_repl_len - i;
1214 }
1215 }
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1218
1219 /* Replace any remaining non-white chars in the old
1220 * leader by spaces. Keep Tabs, the indent must
1221 * remain the same. */
1222 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001223 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
1225 /* Don't put a space before a TAB. */
1226 if (p + 1 < leader + lead_len && p[1] == TAB)
1227 {
1228 --lead_len;
1229 mch_memmove(p, p + 1,
1230 (leader + lead_len) - p);
1231 }
1232 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233 {
1234#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001235 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001236
1237 if (l > 1)
1238 {
1239 if (ptr2cells(p) > 1)
1240 {
1241 /* Replace a double-wide char with
1242 * two spaces */
1243 --l;
1244 *p++ = ' ';
1245 }
1246 mch_memmove(p + 1, p + l,
1247 (leader + lead_len) - p);
1248 lead_len -= l - 1;
1249 }
1250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 *p = NUL;
1255 }
1256
1257 /* Recompute the indent, it may have changed. */
1258 if (curbuf->b_p_ai
1259#ifdef FEAT_SMARTINDENT
1260 || do_si
1261#endif
1262 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001263 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 /* Add the indent offset */
1266 if (newindent + off < 0)
1267 {
1268 off = -newindent;
1269 newindent = 0;
1270 }
1271 else
1272 newindent += off;
1273
1274 /* Correct trailing spaces for the shift, so that
1275 * alignment remains equal. */
1276 while (off > 0 && lead_len > 0
1277 && leader[lead_len - 1] == ' ')
1278 {
1279 /* Don't do it when there is a tab before the space */
1280 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1281 break;
1282 --lead_len;
1283 --off;
1284 }
1285
1286 /* If the leader ends in white space, don't add an
1287 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001288 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 extra_space = FALSE;
1290 leader[lead_len] = NUL;
1291 }
1292
1293 if (extra_space)
1294 {
1295 leader[lead_len++] = ' ';
1296 leader[lead_len] = NUL;
1297 }
1298
1299 newcol = lead_len;
1300
1301 /*
1302 * if a new indent will be set below, remove the indent that
1303 * is in the comment leader
1304 */
1305 if (newindent
1306#ifdef FEAT_SMARTINDENT
1307 || did_si
1308#endif
1309 )
1310 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001311 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 --lead_len;
1314 --newcol;
1315 ++leader;
1316 }
1317 }
1318
1319 }
1320#ifdef FEAT_SMARTINDENT
1321 did_si = can_si = FALSE;
1322#endif
1323 }
1324 else if (comment_end != NULL)
1325 {
1326 /*
1327 * We have finished a comment, so we don't use the leader.
1328 * If this was a C-comment and 'ai' or 'si' is set do a normal
1329 * indent to align with the line containing the start of the
1330 * comment.
1331 */
1332 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1333 (curbuf->b_p_ai
1334#ifdef FEAT_SMARTINDENT
1335 || do_si
1336#endif
1337 ))
1338 {
1339 old_cursor = curwin->w_cursor;
1340 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1341 if ((pos = findmatch(NULL, NUL)) != NULL)
1342 {
1343 curwin->w_cursor.lnum = pos->lnum;
1344 newindent = get_indent();
1345 }
1346 curwin->w_cursor = old_cursor;
1347 }
1348 }
1349 }
1350#endif
1351
1352 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1353 if (p_extra != NULL)
1354 {
1355 *p_extra = saved_char; /* restore char that NUL replaced */
1356
1357 /*
1358 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1359 * non-blank.
1360 *
1361 * When in REPLACE mode, put the deleted blanks on the replace stack,
1362 * preceded by a NUL, so they can be put back when a BS is entered.
1363 */
1364 if (REPLACE_NORMAL(State))
1365 replace_push(NUL); /* end of extra blanks */
1366 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1367 {
1368 while ((*p_extra == ' ' || *p_extra == '\t')
1369#ifdef FEAT_MBYTE
1370 && (!enc_utf8
1371 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1372#endif
1373 )
1374 {
1375 if (REPLACE_NORMAL(State))
1376 replace_push(*p_extra);
1377 ++p_extra;
1378 ++less_cols_off;
1379 }
1380 }
1381 if (*p_extra != NUL)
1382 did_ai = FALSE; /* append some text, don't truncate now */
1383
1384 /* columns for marks adjusted for removed columns */
1385 less_cols = (int)(p_extra - saved_line);
1386 }
1387
1388 if (p_extra == NULL)
1389 p_extra = (char_u *)""; /* append empty line */
1390
1391#ifdef FEAT_COMMENTS
1392 /* concatenate leader and p_extra, if there is a leader */
1393 if (lead_len)
1394 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001395 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1396 {
1397 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001398 int padding = second_line_indent
1399 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001400
1401 /* Here whitespace is inserted after the comment char.
1402 * Below, set_indent(newindent, SIN_INSERT) will insert the
1403 * whitespace needed before the comment char. */
1404 for (i = 0; i < padding; i++)
1405 {
1406 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001407 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001408 newcol++;
1409 }
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 STRCAT(leader, p_extra);
1412 p_extra = leader;
1413 did_ai = TRUE; /* So truncating blanks works with comments */
1414 less_cols -= lead_len;
1415 }
1416 else
1417 end_comment_pending = NUL; /* turns out there was no leader */
1418#endif
1419
1420 old_cursor = curwin->w_cursor;
1421 if (dir == BACKWARD)
1422 --curwin->w_cursor.lnum;
1423#ifdef FEAT_VREPLACE
1424 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1425#endif
1426 {
1427 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1428 == FAIL)
1429 goto theend;
1430 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001431 * with markers.
1432 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001433 * be marks there. But still needed in diff mode. */
1434 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1435#ifdef FEAT_DIFF
1436 || curwin->w_p_diff
1437#endif
1438 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001439 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 did_append = TRUE;
1441 }
1442#ifdef FEAT_VREPLACE
1443 else
1444 {
1445 /*
1446 * In VREPLACE mode we are starting to replace the next line.
1447 */
1448 curwin->w_cursor.lnum++;
1449 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1450 {
1451 /* In case we NL to a new line, BS to the previous one, and NL
1452 * again, we don't want to save the new line for undo twice.
1453 */
1454 (void)u_save_cursor(); /* errors are ignored! */
1455 vr_lines_changed++;
1456 }
1457 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1458 changed_bytes(curwin->w_cursor.lnum, 0);
1459 curwin->w_cursor.lnum--;
1460 did_append = FALSE;
1461 }
1462#endif
1463
1464 if (newindent
1465#ifdef FEAT_SMARTINDENT
1466 || did_si
1467#endif
1468 )
1469 {
1470 ++curwin->w_cursor.lnum;
1471#ifdef FEAT_SMARTINDENT
1472 if (did_si)
1473 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001474 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001477 newindent -= newindent % sw;
1478 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001481 /* Copy the indent */
1482 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 (void)copy_indent(newindent, saved_line);
1485
1486 /*
1487 * Set the 'preserveindent' option so that any further screwing
1488 * with the line doesn't entirely destroy our efforts to preserve
1489 * it. It gets restored at the function end.
1490 */
1491 curbuf->b_p_pi = TRUE;
1492 }
1493 else
1494 (void)set_indent(newindent, SIN_INSERT);
1495 less_cols -= curwin->w_cursor.col;
1496
1497 ai_col = curwin->w_cursor.col;
1498
1499 /*
1500 * In REPLACE mode, for each character in the new indent, there must
1501 * be a NUL on the replace stack, for when it is deleted with BS
1502 */
1503 if (REPLACE_NORMAL(State))
1504 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1505 replace_push(NUL);
1506 newcol += curwin->w_cursor.col;
1507#ifdef FEAT_SMARTINDENT
1508 if (no_si)
1509 did_si = FALSE;
1510#endif
1511 }
1512
1513#ifdef FEAT_COMMENTS
1514 /*
1515 * In REPLACE mode, for each character in the extra leader, there must be
1516 * a NUL on the replace stack, for when it is deleted with BS.
1517 */
1518 if (REPLACE_NORMAL(State))
1519 while (lead_len-- > 0)
1520 replace_push(NUL);
1521#endif
1522
1523 curwin->w_cursor = old_cursor;
1524
1525 if (dir == FORWARD)
1526 {
1527 if (trunc_line || (State & INSERT))
1528 {
1529 /* truncate current line at cursor */
1530 saved_line[curwin->w_cursor.col] = NUL;
1531 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1532 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1533 truncate_spaces(saved_line);
1534 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1535 saved_line = NULL;
1536 if (did_append)
1537 {
1538 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1539 curwin->w_cursor.lnum + 1, 1L);
1540 did_append = FALSE;
1541
1542 /* Move marks after the line break to the new line. */
1543 if (flags & OPENLINE_MARKFIX)
1544 mark_col_adjust(curwin->w_cursor.lnum,
1545 curwin->w_cursor.col + less_cols_off,
1546 1L, (long)-less_cols);
1547 }
1548 else
1549 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1550 }
1551
1552 /*
1553 * Put the cursor on the new line. Careful: the scrollup() above may
1554 * have moved w_cursor, we must use old_cursor.
1555 */
1556 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1557 }
1558 if (did_append)
1559 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1560
1561 curwin->w_cursor.col = newcol;
1562#ifdef FEAT_VIRTUALEDIT
1563 curwin->w_cursor.coladd = 0;
1564#endif
1565
1566#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1567 /*
1568 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1569 * fixthisline() from doing it (via change_indent()) by telling it we're in
1570 * normal INSERT mode.
1571 */
1572 if (State & VREPLACE_FLAG)
1573 {
1574 vreplace_mode = State; /* So we know to put things right later */
1575 State = INSERT;
1576 }
1577 else
1578 vreplace_mode = 0;
1579#endif
1580#ifdef FEAT_LISP
1581 /*
1582 * May do lisp indenting.
1583 */
1584 if (!p_paste
1585# ifdef FEAT_COMMENTS
1586 && leader == NULL
1587# endif
1588 && curbuf->b_p_lisp
1589 && curbuf->b_p_ai)
1590 {
1591 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001592 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594#endif
1595#ifdef FEAT_CINDENT
1596 /*
1597 * May do indenting after opening a new line.
1598 */
1599 if (!p_paste
1600 && (curbuf->b_p_cin
1601# ifdef FEAT_EVAL
1602 || *curbuf->b_p_inde != NUL
1603# endif
1604 )
1605 && in_cinkeys(dir == FORWARD
1606 ? KEY_OPEN_FORW
1607 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1608 {
1609 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001610 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612#endif
1613#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1614 if (vreplace_mode != 0)
1615 State = vreplace_mode;
1616#endif
1617
1618#ifdef FEAT_VREPLACE
1619 /*
1620 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1621 * original line, and inserts the new stuff char by char, pushing old stuff
1622 * onto the replace stack (via ins_char()).
1623 */
1624 if (State & VREPLACE_FLAG)
1625 {
1626 /* Put new line in p_extra */
1627 p_extra = vim_strsave(ml_get_curline());
1628 if (p_extra == NULL)
1629 goto theend;
1630
1631 /* Put back original line */
1632 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1633
1634 /* Insert new stuff into line again */
1635 curwin->w_cursor.col = 0;
1636#ifdef FEAT_VIRTUALEDIT
1637 curwin->w_cursor.coladd = 0;
1638#endif
1639 ins_bytes(p_extra); /* will call changed_bytes() */
1640 vim_free(p_extra);
1641 next_line = NULL;
1642 }
1643#endif
1644
1645 retval = TRUE; /* success! */
1646theend:
1647 curbuf->b_p_pi = saved_pi;
1648 vim_free(saved_line);
1649 vim_free(next_line);
1650 vim_free(allocated);
1651 return retval;
1652}
1653
1654#if defined(FEAT_COMMENTS) || defined(PROTO)
1655/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001656 * get_leader_len() returns the length in bytes of the prefix of the given
1657 * string which introduces a comment. If this string is not a comment then
1658 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * When "flags" is not NULL, it is set to point to the flags of the recognized
1660 * comment leader.
1661 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001662 * If "include_space" is set, include trailing whitespace while calculating the
1663 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 */
1665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001666get_leader_len(
1667 char_u *line,
1668 char_u **flags,
1669 int backward,
1670 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001673 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 int got_com = FALSE;
1675 int found_one;
1676 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1677 char_u *string; /* pointer to comment string */
1678 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001679 int middle_match_len = 0;
1680 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001681 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaar81340392012-06-06 16:12:59 +02001683 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001684 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 ++i;
1686
1687 /*
1688 * Repeat to match several nested comment strings.
1689 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001690 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 /*
1693 * scan through the 'comments' option for a match
1694 */
1695 found_one = FALSE;
1696 for (list = curbuf->b_p_com; *list; )
1697 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001698 /* Get one option part into part_buf[]. Advance "list" to next
1699 * one. Put "string" at start of string. */
1700 if (!got_com && flags != NULL)
1701 *flags = list; /* remember where flags started */
1702 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1704 string = vim_strchr(part_buf, ':');
1705 if (string == NULL) /* missing ':', ignore this part */
1706 continue;
1707 *string++ = NUL; /* isolate flags from string */
1708
Bram Moolenaara4271d52011-05-10 13:38:27 +02001709 /* If we found a middle match previously, use that match when this
1710 * is not a middle or end. */
1711 if (middle_match_len != 0
1712 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1713 && vim_strchr(part_buf, COM_END) == NULL)
1714 break;
1715
1716 /* When we already found a nested comment, only accept further
1717 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1719 continue;
1720
Bram Moolenaara4271d52011-05-10 13:38:27 +02001721 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1723 continue;
1724
Bram Moolenaara4271d52011-05-10 13:38:27 +02001725 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 * When string starts with white space, must have some white space
1727 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001728 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001729 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001731 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001732 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001733 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 ++string;
1735 }
1736 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1737 ;
1738 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001739 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaara4271d52011-05-10 13:38:27 +02001741 /* When 'b' flag used, there must be white space or an
1742 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001744 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 continue;
1746
Bram Moolenaara4271d52011-05-10 13:38:27 +02001747 /* We have found a match, stop searching unless this is a middle
1748 * comment. The middle comment can be a substring of the end
1749 * comment in which case it's better to return the length of the
1750 * end comment and its flags. Thus we keep searching with middle
1751 * and end matches and use an end match if it matches better. */
1752 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1753 {
1754 if (middle_match_len == 0)
1755 {
1756 middle_match_len = j;
1757 saved_flags = prev_list;
1758 }
1759 continue;
1760 }
1761 if (middle_match_len != 0 && j > middle_match_len)
1762 /* Use this match instead of the middle match, since it's a
1763 * longer thus better match. */
1764 middle_match_len = 0;
1765
1766 if (middle_match_len == 0)
1767 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 found_one = TRUE;
1769 break;
1770 }
1771
Bram Moolenaara4271d52011-05-10 13:38:27 +02001772 if (middle_match_len != 0)
1773 {
1774 /* Use the previously found middle match after failing to find a
1775 * match with an end. */
1776 if (!got_com && flags != NULL)
1777 *flags = saved_flags;
1778 i += middle_match_len;
1779 found_one = TRUE;
1780 }
1781
1782 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (!found_one)
1784 break;
1785
Bram Moolenaar81340392012-06-06 16:12:59 +02001786 result = i;
1787
Bram Moolenaara4271d52011-05-10 13:38:27 +02001788 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001789 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 ++i;
1791
Bram Moolenaar81340392012-06-06 16:12:59 +02001792 if (include_space)
1793 result = i;
1794
Bram Moolenaara4271d52011-05-10 13:38:27 +02001795 /* If this comment doesn't nest, stop here. */
1796 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (vim_strchr(part_buf, COM_NEST) == NULL)
1798 break;
1799 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001800 return result;
1801}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001802
Bram Moolenaar81340392012-06-06 16:12:59 +02001803/*
1804 * Return the offset at which the last comment in line starts. If there is no
1805 * comment in the whole line, -1 is returned.
1806 *
1807 * When "flags" is not null, it is set to point to the flags describing the
1808 * recognized comment leader.
1809 */
1810 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001811get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001812{
1813 int result = -1;
1814 int i, j;
1815 int lower_check_bound = 0;
1816 char_u *string;
1817 char_u *com_leader;
1818 char_u *com_flags;
1819 char_u *list;
1820 int found_one;
1821 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1822
1823 /*
1824 * Repeat to match several nested comment strings.
1825 */
1826 i = (int)STRLEN(line);
1827 while (--i >= lower_check_bound)
1828 {
1829 /*
1830 * scan through the 'comments' option for a match
1831 */
1832 found_one = FALSE;
1833 for (list = curbuf->b_p_com; *list; )
1834 {
1835 char_u *flags_save = list;
1836
1837 /*
1838 * Get one option part into part_buf[]. Advance list to next one.
1839 * put string at start of string.
1840 */
1841 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1842 string = vim_strchr(part_buf, ':');
1843 if (string == NULL) /* If everything is fine, this cannot actually
1844 * happen. */
1845 {
1846 continue;
1847 }
1848 *string++ = NUL; /* Isolate flags from string. */
1849 com_leader = string;
1850
1851 /*
1852 * Line contents and string must match.
1853 * When string starts with white space, must have some white space
1854 * (but the amount does not need to match, there might be a mix of
1855 * TABs and spaces).
1856 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001857 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001858 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001859 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001860 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001861 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 ++string;
1863 }
1864 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1865 /* do nothing */;
1866 if (string[j] != NUL)
1867 continue;
1868
1869 /*
1870 * When 'b' flag used, there must be white space or an
1871 * end-of-line after the string in the line.
1872 */
1873 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001874 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02001875 {
1876 continue;
1877 }
1878
1879 /*
1880 * We have found a match, stop searching.
1881 */
1882 found_one = TRUE;
1883
1884 if (flags)
1885 *flags = flags_save;
1886 com_flags = flags_save;
1887
1888 break;
1889 }
1890
1891 if (found_one)
1892 {
1893 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1894 int len1, len2, off;
1895
1896 result = i;
1897 /*
1898 * If this comment nests, continue searching.
1899 */
1900 if (vim_strchr(part_buf, COM_NEST) != NULL)
1901 continue;
1902
1903 lower_check_bound = i;
1904
1905 /* Let's verify whether the comment leader found is a substring
1906 * of other comment leaders. If it is, let's adjust the
1907 * lower_check_bound so that we make sure that we have determined
1908 * the comment leader correctly.
1909 */
1910
Bram Moolenaar1c465442017-03-12 20:10:05 +01001911 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02001912 ++com_leader;
1913 len1 = (int)STRLEN(com_leader);
1914
1915 for (list = curbuf->b_p_com; *list; )
1916 {
1917 char_u *flags_save = list;
1918
1919 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1920 if (flags_save == com_flags)
1921 continue;
1922 string = vim_strchr(part_buf2, ':');
1923 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001924 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 ++string;
1926 len2 = (int)STRLEN(string);
1927 if (len2 == 0)
1928 continue;
1929
1930 /* Now we have to verify whether string ends with a substring
1931 * beginning the com_leader. */
1932 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1933 {
1934 --off;
1935 if (!STRNCMP(string + off, com_leader, len2 - off))
1936 {
1937 if (i - off < lower_check_bound)
1938 lower_check_bound = i - off;
1939 }
1940 }
1941 }
1942 }
1943 }
1944 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945}
1946#endif
1947
1948/*
1949 * Return the number of window lines occupied by buffer line "lnum".
1950 */
1951 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001952plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 return plines_win(curwin, lnum, TRUE);
1955}
1956
1957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001958plines_win(
1959 win_T *wp,
1960 linenr_T lnum,
1961 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963#if defined(FEAT_DIFF) || defined(PROTO)
1964 /* Check for filler lines above this buffer line. When folded the result
1965 * is one line anyway. */
1966 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1967}
1968
1969 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001970plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 return plines_win_nofill(curwin, lnum, TRUE);
1973}
1974
1975 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976plines_win_nofill(
1977 win_T *wp,
1978 linenr_T lnum,
1979 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981#endif
1982 int lines;
1983
1984 if (!wp->w_p_wrap)
1985 return 1;
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (wp->w_width == 0)
1988 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990#ifdef FEAT_FOLDING
1991 /* A folded lines is handled just like an empty line. */
1992 /* NOTE: Caller must handle lines that are MAYBE folded. */
1993 if (lineFolded(wp, lnum) == TRUE)
1994 return 1;
1995#endif
1996
1997 lines = plines_win_nofold(wp, lnum);
1998 if (winheight > 0 && lines > wp->w_height)
1999 return (int)wp->w_height;
2000 return lines;
2001}
2002
2003/*
2004 * Return number of window lines physical line "lnum" will occupy in window
2005 * "wp". Does not care about folding, 'wrap' or 'diff'.
2006 */
2007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 char_u *s;
2011 long col;
2012 int width;
2013
2014 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2015 if (*s == NUL) /* empty line */
2016 return 1;
2017 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2018
2019 /*
2020 * If list mode is on, then the '$' at the end of the line may take up one
2021 * extra column.
2022 */
2023 if (wp->w_p_list && lcs_eol != NUL)
2024 col += 1;
2025
2026 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002027 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002029 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (width <= 0)
2031 return 32000;
2032 if (col <= width)
2033 return 1;
2034 col -= width;
2035 width += win_col_off2(wp);
2036 return (col + (width - 1)) / width + 1;
2037}
2038
2039/*
2040 * Like plines_win(), but only reports the number of physical screen lines
2041 * used from the start of the line to the given column number.
2042 */
2043 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 long col;
2047 char_u *s;
2048 int lines = 0;
2049 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002050 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052#ifdef FEAT_DIFF
2053 /* Check for filler lines above this buffer line. When folded the result
2054 * is one line anyway. */
2055 lines = diff_check_fill(wp, lnum);
2056#endif
2057
2058 if (!wp->w_p_wrap)
2059 return lines + 1;
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (wp->w_width == 0)
2062 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaar597a4222014-06-25 14:39:50 +02002064 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 col = 0;
2067 while (*s != NUL && --column >= 0)
2068 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002069 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002070 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072
2073 /*
2074 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2075 * INSERT mode, then col must be adjusted so that it represents the last
2076 * screen position of the TAB. This only fixes an error when the TAB wraps
2077 * from one screen line to the next (when 'columns' is not a multiple of
2078 * 'ts') -- webb.
2079 */
2080 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002081 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002084 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002086 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002087 if (width <= 0)
2088 return 9999;
2089
2090 lines += 1;
2091 if (col > width)
2092 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2093 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094}
2095
2096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002097plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 int count = 0;
2100
2101 while (first <= last)
2102 {
2103#ifdef FEAT_FOLDING
2104 int x;
2105
2106 /* Check if there are any really folded lines, but also included lines
2107 * that are maybe folded. */
2108 x = foldedCount(wp, first, NULL);
2109 if (x > 0)
2110 {
2111 ++count; /* count 1 for "+-- folded" line */
2112 first += x;
2113 }
2114 else
2115#endif
2116 {
2117#ifdef FEAT_DIFF
2118 if (first == wp->w_topline)
2119 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2120 else
2121#endif
2122 count += plines_win(wp, first, TRUE);
2123 ++first;
2124 }
2125 }
2126 return (count);
2127}
2128
2129#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2130/*
2131 * Insert string "p" at the cursor position. Stops at a NUL byte.
2132 * Handles Replace mode and multi-byte characters.
2133 */
2134 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002135ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
2137 ins_bytes_len(p, (int)STRLEN(p));
2138}
2139#endif
2140
2141#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2142 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2143/*
2144 * Insert string "p" with length "len" at the cursor position.
2145 * Handles Replace mode and multi-byte characters.
2146 */
2147 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002148ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 int i;
2151# ifdef FEAT_MBYTE
2152 int n;
2153
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002154 if (has_mbyte)
2155 for (i = 0; i < len; i += n)
2156 {
2157 if (enc_utf8)
2158 /* avoid reading past p[len] */
2159 n = utfc_ptr2len_len(p + i, len - i);
2160 else
2161 n = (*mb_ptr2len)(p + i);
2162 ins_char_bytes(p + i, n);
2163 }
2164 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002166 for (i = 0; i < len; ++i)
2167 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168}
2169#endif
2170
2171/*
2172 * Insert or replace a single character at the cursor position.
2173 * When in REPLACE or VREPLACE mode, replace any existing character.
2174 * Caller must have prepared for undo.
2175 * For multi-byte characters we get the whole character, the caller must
2176 * convert bytes to a character.
2177 */
2178 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002181 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002182 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002184#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 n = (*mb_char2bytes)(c, buf);
2186
2187 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2188 * Happens for CTRL-Vu9900. */
2189 if (buf[0] == 0)
2190 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002191#else
2192 buf[0] = c;
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 ins_char_bytes(buf, n);
2196}
2197
2198 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002199ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 int newlen; /* nr of bytes inserted */
2203 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2204 char_u *p;
2205 char_u *newp;
2206 char_u *oldp;
2207 int linelen; /* length of old line including NUL */
2208 colnr_T col;
2209 linenr_T lnum = curwin->w_cursor.lnum;
2210 int i;
2211
2212#ifdef FEAT_VIRTUALEDIT
2213 /* Break tabs if needed. */
2214 if (virtual_active() && curwin->w_cursor.coladd > 0)
2215 coladvance_force(getviscol());
2216#endif
2217
2218 col = curwin->w_cursor.col;
2219 oldp = ml_get(lnum);
2220 linelen = (int)STRLEN(oldp) + 1;
2221
2222 /* The lengths default to the values for when not replacing. */
2223 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 if (State & REPLACE_FLAG)
2227 {
2228#ifdef FEAT_VREPLACE
2229 if (State & VREPLACE_FLAG)
2230 {
2231 colnr_T new_vcol = 0; /* init for GCC */
2232 colnr_T vcol;
2233 int old_list;
2234#ifndef FEAT_MBYTE
2235 char_u buf[2];
2236#endif
2237
2238 /*
2239 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2240 * Returns the old value of list, so when finished,
2241 * curwin->w_p_list should be set back to this.
2242 */
2243 old_list = curwin->w_p_list;
2244 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2245 curwin->w_p_list = FALSE;
2246
2247 /*
2248 * In virtual replace mode each character may replace one or more
2249 * characters (zero if it's a TAB). Count the number of bytes to
2250 * be deleted to make room for the new character, counting screen
2251 * cells. May result in adding spaces to fill a gap.
2252 */
2253 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2254#ifndef FEAT_MBYTE
2255 buf[0] = c;
2256 buf[1] = NUL;
2257#endif
2258 new_vcol = vcol + chartabsize(buf, vcol);
2259 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2260 {
2261 vcol += chartabsize(oldp + col + oldlen, vcol);
2262 /* Don't need to remove a TAB that takes us to the right
2263 * position. */
2264 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2265 break;
2266#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002267 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#else
2269 ++oldlen;
2270#endif
2271 /* Deleted a bit too much, insert spaces. */
2272 if (vcol > new_vcol)
2273 newlen += vcol - new_vcol;
2274 }
2275 curwin->w_p_list = old_list;
2276 }
2277 else
2278#endif
2279 if (oldp[col] != NUL)
2280 {
2281 /* normal replace */
2282#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002283 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#else
2285 oldlen = 1;
2286#endif
2287 }
2288
2289
2290 /* Push the replaced bytes onto the replace stack, so that they can be
2291 * put back when BS is used. The bytes of a multi-byte character are
2292 * done the other way around, so that the first byte is popped off
2293 * first (it tells the byte length of the character). */
2294 replace_push(NUL);
2295 for (i = 0; i < oldlen; ++i)
2296 {
2297#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002298 if (has_mbyte)
2299 i += replace_push_mb(oldp + col + i) - 1;
2300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002302 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 }
2305
2306 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2307 if (newp == NULL)
2308 return;
2309
2310 /* Copy bytes before the cursor. */
2311 if (col > 0)
2312 mch_memmove(newp, oldp, (size_t)col);
2313
2314 /* Copy bytes after the changed character(s). */
2315 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002316 if (linelen > col + oldlen)
2317 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 (size_t)(linelen - col - oldlen));
2319
2320 /* Insert or overwrite the new character. */
2321#ifdef FEAT_MBYTE
2322 mch_memmove(p, buf, charlen);
2323 i = charlen;
2324#else
2325 *p = c;
2326 i = 1;
2327#endif
2328
2329 /* Fill with spaces when necessary. */
2330 while (i < newlen)
2331 p[i++] = ' ';
2332
2333 /* Replace the line in the buffer. */
2334 ml_replace(lnum, newp, FALSE);
2335
2336 /* mark the buffer as changed and prepare for displaying */
2337 changed_bytes(lnum, col);
2338
2339 /*
2340 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2341 * show the match for right parens and braces.
2342 */
2343 if (p_sm && (State & INSERT)
2344 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002345#ifdef FEAT_INS_EXPAND
2346 && !ins_compl_active()
2347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002349 {
2350#ifdef FEAT_MBYTE
2351 if (has_mbyte)
2352 showmatch(mb_ptr2char(buf));
2353 else
2354#endif
2355 showmatch(c);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_RIGHTLEFT
2359 if (!p_ri || (State & REPLACE_FLAG))
2360#endif
2361 {
2362 /* Normal insert: move cursor right */
2363#ifdef FEAT_MBYTE
2364 curwin->w_cursor.col += charlen;
2365#else
2366 ++curwin->w_cursor.col;
2367#endif
2368 }
2369 /*
2370 * TODO: should try to update w_row here, to avoid recomputing it later.
2371 */
2372}
2373
2374/*
2375 * Insert a string at the cursor position.
2376 * Note: Does NOT handle Replace mode.
2377 * Caller must have prepared for undo.
2378 */
2379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002380ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char_u *oldp, *newp;
2383 int newlen = (int)STRLEN(s);
2384 int oldlen;
2385 colnr_T col;
2386 linenr_T lnum = curwin->w_cursor.lnum;
2387
2388#ifdef FEAT_VIRTUALEDIT
2389 if (virtual_active() && curwin->w_cursor.coladd > 0)
2390 coladvance_force(getviscol());
2391#endif
2392
2393 col = curwin->w_cursor.col;
2394 oldp = ml_get(lnum);
2395 oldlen = (int)STRLEN(oldp);
2396
2397 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2398 if (newp == NULL)
2399 return;
2400 if (col > 0)
2401 mch_memmove(newp, oldp, (size_t)col);
2402 mch_memmove(newp + col, s, (size_t)newlen);
2403 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2404 ml_replace(lnum, newp, FALSE);
2405 changed_bytes(lnum, col);
2406 curwin->w_cursor.col += newlen;
2407}
2408
2409/*
2410 * Delete one character under the cursor.
2411 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2412 * Caller must have prepared for undo.
2413 *
2414 * return FAIL for failure, OK otherwise
2415 */
2416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419#ifdef FEAT_MBYTE
2420 if (has_mbyte)
2421 {
2422 /* Make sure the cursor is at the start of a character. */
2423 mb_adjust_cursor();
2424 if (*ml_get_cursor() == NUL)
2425 return FAIL;
2426 return del_chars(1L, fixpos);
2427 }
2428#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002429 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
2432#if defined(FEAT_MBYTE) || defined(PROTO)
2433/*
2434 * Like del_bytes(), but delete characters instead of bytes.
2435 */
2436 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002437del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 long bytes = 0;
2440 long i;
2441 char_u *p;
2442 int l;
2443
2444 p = ml_get_cursor();
2445 for (i = 0; i < count && *p != NUL; ++i)
2446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 bytes += l;
2449 p += l;
2450 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002451 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
2453#endif
2454
2455/*
2456 * Delete "count" bytes under the cursor.
2457 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2458 * Caller must have prepared for undo.
2459 *
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 Moolenaard857f0e2005-06-21 22:37:39 +00003597
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003598 i = get_number(TRUE, mouse_used);
3599 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003600 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003601 /* don't call wait_return() now */
3602 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 cmdline_row = msg_row - 1;
3604 need_wait_return = FALSE;
3605 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003606 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003607 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003608 else
3609 cmdline_row = save_cmdline_row;
3610 State = save_State;
3611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 return i;
3613}
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003616msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 long pn;
3619
3620 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3622 return;
3623
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003624 /* We don't want to overwrite another important message, but do overwrite
3625 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3626 * then "put" reports the last action. */
3627 if (keep_msg != NULL && !keep_msg_more)
3628 return;
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (n > 0)
3631 pn = n;
3632 else
3633 pn = -n;
3634
3635 if (pn > p_report)
3636 {
3637 if (pn == 1)
3638 {
3639 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003640 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3641 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003643 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3644 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 else
3647 {
3648 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003649 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3650 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003652 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3653 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
3655 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003656 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (msg(msg_buf))
3658 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003659 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003660 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662 }
3663}
3664
3665/*
3666 * flush map and typeahead buffers and give a warning for an error
3667 */
3668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003669beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
3671 if (emsg_silent == 0)
3672 {
3673 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003674 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676}
3677
3678/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003679 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 */
3681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003682vim_beep(
3683 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003685#ifdef FEAT_EVAL
3686 called_vim_beep = TRUE;
3687#endif
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (emsg_silent == 0)
3690 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003691 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3692 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003693#ifdef ELAPSED_FUNC
3694 static int did_init = FALSE;
3695 static ELAPSED_TYPE start_tv;
3696
3697 /* Only beep once per half a second, otherwise a sequence of beeps
3698 * would freeze Vim. */
3699 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3700 {
3701 did_init = TRUE;
3702 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003704 if (p_vb
3705#ifdef FEAT_GUI
3706 /* While the GUI is starting up the termcap is set for
3707 * the GUI but the output still goes to a terminal. */
3708 && !(gui.in_use && gui.starting)
3709#endif
3710 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003711 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003712 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003713#ifdef FEAT_VTP
3714 /* No restore color information, refresh the screen. */
3715 if (has_vtp_working() != 0
3716# ifdef FEAT_TERMGUICOLORS
3717 && p_tgc
3718# endif
3719 )
3720 {
3721 redraw_later(CLEAR);
3722 update_screen(0);
3723 redrawcmd();
3724 }
3725#endif
3726 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003727 else
3728 out_char(BELL);
3729#ifdef ELAPSED_FUNC
3730 }
3731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003733
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003734 /* When 'debug' contains "beep" produce a message. If we are sourcing
3735 * a script or executing a function give the user a hint where the beep
3736 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003737 if (vim_strchr(p_debug, 'e') != NULL)
3738 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003739 msg_source(HL_ATTR(HLF_W));
3740 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743}
3744
3745/*
3746 * To get the "real" home directory:
3747 * - get value of $HOME
3748 * For Unix:
3749 * - go to that directory
3750 * - do mch_dirname() to get the real name of that directory.
3751 * This also works with mounts and links.
3752 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3753 */
3754static char_u *homedir = NULL;
3755
3756 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003757init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 char_u *var;
3760
Bram Moolenaar05159a02005-02-26 23:04:13 +00003761 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003762 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764#ifdef VMS
3765 var = mch_getenv((char_u *)"SYS$LOGIN");
3766#else
3767 var = mch_getenv((char_u *)"HOME");
3768#endif
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770#ifdef WIN3264
3771 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003772 * Typically, $HOME is not defined on Windows, unless the user has
3773 * specifically defined it for Vim's sake. However, on Windows NT
3774 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3775 * each user. Try constructing $HOME from these.
3776 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003777 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003778 {
3779 char_u *homedrive, *homepath;
3780
3781 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3782 homepath = mch_getenv((char_u *)"HOMEPATH");
3783 if (homepath == NULL || *homepath == NUL)
3784 homepath = (char_u *)"\\";
3785 if (homedrive != NULL
3786 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3787 {
3788 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3789 if (NameBuff[0] != NUL)
3790 var = NameBuff;
3791 }
3792 }
3793
3794 if (var == NULL)
3795 var = mch_getenv((char_u *)"USERPROFILE");
3796
3797 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 * Weird but true: $HOME may contain an indirect reference to another
3799 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3800 * when $HOME is being set.
3801 */
3802 if (var != NULL && *var == '%')
3803 {
3804 char_u *p;
3805 char_u *exp;
3806
3807 p = vim_strchr(var + 1, '%');
3808 if (p != NULL)
3809 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003810 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 exp = mch_getenv(NameBuff);
3812 if (exp != NULL && *exp != NUL
3813 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3814 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003815 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818 }
3819 }
3820
Bram Moolenaar48340b62017-08-29 22:08:53 +02003821 if (var != NULL && *var == NUL) /* empty is same as not set */
3822 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Bram Moolenaar48340b62017-08-29 22:08:53 +02003824# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003825 if (enc_utf8 && var != NULL)
3826 {
3827 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003828 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003829
3830 /* Convert from active codepage to UTF-8. Other conversions are
3831 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003832 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003833 if (pp != NULL)
3834 {
3835 homedir = pp;
3836 return;
3837 }
3838 }
3839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 /*
3842 * Default home dir is C:/
3843 * Best assumption we can make in such a situation.
3844 */
3845 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003846 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (var != NULL)
3850 {
3851#ifdef UNIX
3852 /*
3853 * Change to the directory and get the actual path. This resolves
3854 * links. Don't do it when we can't return.
3855 */
3856 if (mch_dirname(NameBuff, MAXPATHL) == OK
3857 && mch_chdir((char *)NameBuff) == 0)
3858 {
3859 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3860 var = IObuff;
3861 if (mch_chdir((char *)NameBuff) != 0)
3862 EMSG(_(e_prev_dir));
3863 }
3864#endif
3865 homedir = vim_strsave(var);
3866 }
3867}
3868
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003869#if defined(EXITFREE) || defined(PROTO)
3870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003871free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003872{
3873 vim_free(homedir);
3874}
Bram Moolenaar24305862012-08-15 14:05:05 +02003875
3876# ifdef FEAT_CMDL_COMPL
3877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003878free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003879{
3880 ga_clear_strings(&ga_users);
3881}
3882# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003883#endif
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003886 * Call expand_env() and store the result in an allocated string.
3887 * This is not very memory efficient, this expects the result to be freed
3888 * again soon.
3889 */
3890 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003891expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003892{
3893 return expand_env_save_opt(src, FALSE);
3894}
3895
3896/*
3897 * Idem, but when "one" is TRUE handle the string as one file name, only
3898 * expand "~" at the start.
3899 */
3900 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003901expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003902{
3903 char_u *p;
3904
3905 p = alloc(MAXPATHL);
3906 if (p != NULL)
3907 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3908 return p;
3909}
3910
3911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 * Expand environment variable with path name.
3913 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003914 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 * If anything fails no expansion is done and dst equals src.
3916 */
3917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003918expand_env(
3919 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3920 char_u *dst, /* where to put the result */
3921 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003923 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924}
3925
3926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003927expand_env_esc(
3928 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3929 char_u *dst, /* where to put the result */
3930 int dstlen, /* maximum length of the result */
3931 int esc, /* escape spaces in expanded variables */
3932 int one, /* "srcp" is one file name */
3933 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003935 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 char_u *tail;
3937 int c;
3938 char_u *var;
3939 int copy_char;
3940 int mustfree; /* var was allocated, need to free it later */
3941 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003942 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003944 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003946
3947 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 --dstlen; /* leave one char space for "\," */
3949 while (*src && dstlen > 0)
3950 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003951#ifdef FEAT_EVAL
3952 /* Skip over `=expr`. */
3953 if (src[0] == '`' && src[1] == '=')
3954 {
3955 size_t len;
3956
3957 var = src;
3958 src += 2;
3959 (void)skip_expr(&src);
3960 if (*src == '`')
3961 ++src;
3962 len = src - var;
3963 if (len > (size_t)dstlen)
3964 len = dstlen;
3965 vim_strncpy(dst, var, len);
3966 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003967 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003968 continue;
3969 }
3970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003972 if ((*src == '$'
3973#ifdef VMS
3974 && at_start
3975#endif
3976 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003977#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 || *src == '%'
3979#endif
3980 || (*src == '~' && at_start))
3981 {
3982 mustfree = FALSE;
3983
3984 /*
3985 * The variable name is copied into dst temporarily, because it may
3986 * be a string in read-only memory and a NUL needs to be appended.
3987 */
3988 if (*src != '~') /* environment var */
3989 {
3990 tail = src + 1;
3991 var = dst;
3992 c = dstlen - 1;
3993
3994#ifdef UNIX
3995 /* Unix has ${var-name} type environment vars */
3996 if (*tail == '{' && !vim_isIDc('{'))
3997 {
3998 tail++; /* ignore '{' */
3999 while (c-- > 0 && *tail && *tail != '}')
4000 *var++ = *tail++;
4001 }
4002 else
4003#endif
4004 {
4005 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004006#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 || (*src == '%' && *tail != '%')
4008#endif
4009 ))
4010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 }
4014
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004015#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016# ifdef UNIX
4017 if (src[1] == '{' && *tail != '}')
4018# else
4019 if (*src == '%' && *tail != '%')
4020# endif
4021 var = NULL;
4022 else
4023 {
4024# ifdef UNIX
4025 if (src[1] == '{')
4026# else
4027 if (*src == '%')
4028#endif
4029 ++tail;
4030#endif
4031 *var = NUL;
4032 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004033#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035#endif
4036 }
4037 /* home directory */
4038 else if ( src[1] == NUL
4039 || vim_ispathsep(src[1])
4040 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4041 {
4042 var = homedir;
4043 tail = src + 1;
4044 }
4045 else /* user directory */
4046 {
4047#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4048 /*
4049 * Copy ~user to dst[], so we can put a NUL after it.
4050 */
4051 tail = src;
4052 var = dst;
4053 c = dstlen - 1;
4054 while ( c-- > 0
4055 && *tail
4056 && vim_isfilec(*tail)
4057 && !vim_ispathsep(*tail))
4058 *var++ = *tail++;
4059 *var = NUL;
4060# ifdef UNIX
4061 /*
4062 * If the system supports getpwnam(), use it.
4063 * Otherwise, or if getpwnam() fails, the shell is used to
4064 * expand ~user. This is slower and may fail if the shell
4065 * does not support ~user (old versions of /bin/sh).
4066 */
4067# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4068 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004069 /* Note: memory allocated by getpwnam() is never freed.
4070 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004071 struct passwd *pw = (*dst == NUL)
4072 ? NULL : getpwnam((char *)dst + 1);
4073
4074 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 if (var == NULL)
4077# endif
4078 {
4079 expand_T xpc;
4080
4081 ExpandInit(&xpc);
4082 xpc.xp_context = EXPAND_FILES;
4083 var = ExpandOne(&xpc, dst, NULL,
4084 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 mustfree = TRUE;
4086 }
4087
4088# else /* !UNIX, thus VMS */
4089 /*
4090 * USER_HOME is a comma-separated list of
4091 * directories to search for the user account in.
4092 */
4093 {
4094 char_u test[MAXPATHL], paths[MAXPATHL];
4095 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004096 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 STRCPY(paths, USER_HOME);
4099 next_path = paths;
4100 while (*next_path)
4101 {
4102 for (path = next_path; *next_path && *next_path != ',';
4103 next_path++);
4104 if (*next_path)
4105 *next_path++ = NUL;
4106 STRCPY(test, path);
4107 STRCAT(test, "/");
4108 STRCAT(test, dst + 1);
4109 if (mch_stat(test, &st) == 0)
4110 {
4111 var = alloc(STRLEN(test) + 1);
4112 STRCPY(var, test);
4113 mustfree = TRUE;
4114 break;
4115 }
4116 }
4117 }
4118# endif /* UNIX */
4119#else
4120 /* cannot expand user's home directory, so don't try */
4121 var = NULL;
4122 tail = (char_u *)""; /* for gcc */
4123#endif /* UNIX || VMS */
4124 }
4125
4126#ifdef BACKSLASH_IN_FILENAME
4127 /* If 'shellslash' is set change backslashes to forward slashes.
4128 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4129 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4130 {
4131 char_u *p = vim_strsave(var);
4132
4133 if (p != NULL)
4134 {
4135 if (mustfree)
4136 vim_free(var);
4137 var = p;
4138 mustfree = TRUE;
4139 forward_slash(var);
4140 }
4141 }
4142#endif
4143
4144 /* If "var" contains white space, escape it with a backslash.
4145 * Required for ":e ~/tt" when $HOME includes a space. */
4146 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4147 {
4148 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4149
4150 if (p != NULL)
4151 {
4152 if (mustfree)
4153 vim_free(var);
4154 var = p;
4155 mustfree = TRUE;
4156 }
4157 }
4158
4159 if (var != NULL && *var != NUL
4160 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4161 {
4162 STRCPY(dst, var);
4163 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004164 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 /* if var[] ends in a path separator and tail[] starts
4166 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004167 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4169 && dst[-1] != ':'
4170#endif
4171 && vim_ispathsep(*tail))
4172 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004173 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 src = tail;
4175 copy_char = FALSE;
4176 }
4177 if (mustfree)
4178 vim_free(var);
4179 }
4180
4181 if (copy_char) /* copy at least one char */
4182 {
4183 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004184 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004185 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4186 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 */
4188 at_start = FALSE;
4189 if (src[0] == '\\' && src[1] != NUL)
4190 {
4191 *dst++ = *src++;
4192 --dstlen;
4193 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004194 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004196 if (dstlen > 0)
4197 {
4198 *dst++ = *src++;
4199 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004200
Bram Moolenaar1c864092017-08-06 18:15:45 +02004201 if (startstr != NULL && src - startstr_len >= srcp
4202 && STRNCMP(src - startstr_len, startstr,
4203 startstr_len) == 0)
4204 at_start = TRUE;
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209 *dst = NUL;
4210}
4211
4212/*
4213 * Vim's version of getenv().
4214 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004215 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004216 * "mustfree" is set to TRUE when returned is allocated, it must be
4217 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 */
4219 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004220vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 char_u *p;
4223 char_u *pend;
4224 int vimruntime;
4225
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004226#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 /* use "C:/" when $HOME is not set */
4228 if (STRCMP(name, "HOME") == 0)
4229 return homedir;
4230#endif
4231
4232 p = mch_getenv(name);
4233 if (p != NULL && *p == NUL) /* empty is the same as not set */
4234 p = NULL;
4235
4236 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004237 {
4238#if defined(FEAT_MBYTE) && defined(WIN3264)
4239 if (enc_utf8)
4240 {
4241 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004242 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004243
4244 /* Convert from active codepage to UTF-8. Other conversions are
4245 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004246 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004247 if (pp != NULL)
4248 {
4249 p = pp;
4250 *mustfree = TRUE;
4251 }
4252 }
4253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4258 if (!vimruntime && STRCMP(name, "VIM") != 0)
4259 return NULL;
4260
4261 /*
4262 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4263 * Don't do this when default_vimruntime_dir is non-empty.
4264 */
4265 if (vimruntime
4266#ifdef HAVE_PATHDEF
4267 && *default_vimruntime_dir == NUL
4268#endif
4269 )
4270 {
4271 p = mch_getenv((char_u *)"VIM");
4272 if (p != NULL && *p == NUL) /* empty is the same as not set */
4273 p = NULL;
4274 if (p != NULL)
4275 {
4276 p = vim_version_dir(p);
4277 if (p != NULL)
4278 *mustfree = TRUE;
4279 else
4280 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281
4282#if defined(FEAT_MBYTE) && defined(WIN3264)
4283 if (enc_utf8)
4284 {
4285 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004286 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004287
4288 /* Convert from active codepage to UTF-8. Other conversions
4289 * are not done, because they would fail for non-ASCII
4290 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004291 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004292 if (pp != NULL)
4293 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004294 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004295 vim_free(p);
4296 p = pp;
4297 *mustfree = TRUE;
4298 }
4299 }
4300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303
4304 /*
4305 * When expanding $VIM or $VIMRUNTIME fails, try using:
4306 * - the directory name from 'helpfile' (unless it contains '$')
4307 * - the executable name from argv[0]
4308 */
4309 if (p == NULL)
4310 {
4311 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4312 p = p_hf;
4313#ifdef USE_EXE_NAME
4314 /*
4315 * Use the name of the executable, obtained from argv[0].
4316 */
4317 else
4318 p = exe_name;
4319#endif
4320 if (p != NULL)
4321 {
4322 /* remove the file name */
4323 pend = gettail(p);
4324
4325 /* remove "doc/" from 'helpfile', if present */
4326 if (p == p_hf)
4327 pend = remove_tail(p, pend, (char_u *)"doc");
4328
4329#ifdef USE_EXE_NAME
4330# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004331 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 if (p == exe_name)
4333 {
4334 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004335 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004337 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4338 if (pend1 != pend)
4339 {
4340 pnew = alloc((unsigned)(pend1 - p) + 15);
4341 if (pnew != NULL)
4342 {
4343 STRNCPY(pnew, p, (pend1 - p));
4344 STRCPY(pnew + (pend1 - p), "Resources/vim");
4345 p = pnew;
4346 pend = p + STRLEN(p);
4347 }
4348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350# endif
4351 /* remove "src/" from exe_name, if present */
4352 if (p == exe_name)
4353 pend = remove_tail(p, pend, (char_u *)"src");
4354#endif
4355
4356 /* for $VIM, remove "runtime/" or "vim54/", if present */
4357 if (!vimruntime)
4358 {
4359 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4360 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4361 }
4362
4363 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004364 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004367#ifdef MACOS_X
4368 if (p == exe_name || p == p_hf)
4369#endif
4370 /* check that the result is a directory name */
4371 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004374 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 else
4376 {
4377#ifdef USE_EXE_NAME
4378 /* may add "/vim54" or "/runtime" if it exists */
4379 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4380 {
4381 vim_free(p);
4382 p = pend;
4383 }
4384#endif
4385 *mustfree = TRUE;
4386 }
4387 }
4388 }
4389
4390#ifdef HAVE_PATHDEF
4391 /* When there is a pathdef.c file we can use default_vim_dir and
4392 * default_vimruntime_dir */
4393 if (p == NULL)
4394 {
4395 /* Only use default_vimruntime_dir when it is not empty */
4396 if (vimruntime && *default_vimruntime_dir != NUL)
4397 {
4398 p = default_vimruntime_dir;
4399 *mustfree = FALSE;
4400 }
4401 else if (*default_vim_dir != NUL)
4402 {
4403 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4404 *mustfree = TRUE;
4405 else
4406 {
4407 p = default_vim_dir;
4408 *mustfree = FALSE;
4409 }
4410 }
4411 }
4412#endif
4413
4414 /*
4415 * Set the environment variable, so that the new value can be found fast
4416 * next time, and others can also use it (e.g. Perl).
4417 */
4418 if (p != NULL)
4419 {
4420 if (vimruntime)
4421 {
4422 vim_setenv((char_u *)"VIMRUNTIME", p);
4423 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 else
4426 {
4427 vim_setenv((char_u *)"VIM", p);
4428 didset_vim = TRUE;
4429 }
4430 }
4431 return p;
4432}
4433
4434/*
4435 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4436 * Return NULL if not, return its name in allocated memory otherwise.
4437 */
4438 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004439vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 char_u *p;
4442
4443 if (vimdir == NULL || *vimdir == NUL)
4444 return NULL;
4445 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4446 if (p != NULL && mch_isdir(p))
4447 return p;
4448 vim_free(p);
4449 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4450 if (p != NULL && mch_isdir(p))
4451 return p;
4452 vim_free(p);
4453 return NULL;
4454}
4455
4456/*
4457 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4458 * the length of "name/". Otherwise return "pend".
4459 */
4460 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004461remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 int len = (int)STRLEN(name) + 1;
4464 char_u *newend = pend - len;
4465
4466 if (newend >= p
4467 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004468 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return newend;
4470 return pend;
4471}
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 * Our portable version of setenv.
4475 */
4476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004477vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
4479#ifdef HAVE_SETENV
4480 mch_setenv((char *)name, (char *)val, 1);
4481#else
4482 char_u *envbuf;
4483
4484 /*
4485 * Putenv does not copy the string, it has to remain
4486 * valid. The allocated memory will never be freed.
4487 */
4488 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4489 if (envbuf != NULL)
4490 {
4491 sprintf((char *)envbuf, "%s=%s", name, val);
4492 putenv((char *)envbuf);
4493 }
4494#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004495#ifdef FEAT_GETTEXT
4496 /*
4497 * When setting $VIMRUNTIME adjust the directory to find message
4498 * translations to $VIMRUNTIME/lang.
4499 */
4500 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4501 {
4502 char_u *buf = concat_str(val, (char_u *)"/lang");
4503
4504 if (buf != NULL)
4505 {
4506 bindtextdomain(VIMPACKAGE, (char *)buf);
4507 vim_free(buf);
4508 }
4509 }
4510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511}
4512
4513#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4514/*
4515 * Function given to ExpandGeneric() to obtain an environment variable name.
4516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004518get_env_name(
4519 expand_T *xp UNUSED,
4520 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521{
Bram Moolenaard0573012017-10-28 21:11:06 +02004522# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004524 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
4526 return NULL;
4527# else
4528# ifndef __WIN32__
4529 /* Borland C++ 5.2 has this in a header file. */
4530 extern char **environ;
4531# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004532# define ENVNAMELEN 100
4533 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 char_u *str;
4535 int n;
4536
4537 str = (char_u *)environ[idx];
4538 if (str == NULL)
4539 return NULL;
4540
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004541 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
4543 if (str[n] == '=' || str[n] == NUL)
4544 break;
4545 name[n] = str[n];
4546 }
4547 name[n] = NUL;
4548 return name;
4549# endif
4550}
Bram Moolenaar24305862012-08-15 14:05:05 +02004551
4552/*
4553 * Find all user names for user completion.
4554 * Done only once and then cached.
4555 */
4556 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004557init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004558{
Bram Moolenaar24305862012-08-15 14:05:05 +02004559 static int lazy_init_done = FALSE;
4560
4561 if (lazy_init_done)
4562 return;
4563
4564 lazy_init_done = TRUE;
4565 ga_init2(&ga_users, sizeof(char_u *), 20);
4566
4567# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4568 {
4569 char_u* user;
4570 struct passwd* pw;
4571
4572 setpwent();
4573 while ((pw = getpwent()) != NULL)
4574 /* pw->pw_name shouldn't be NULL but just in case... */
4575 if (pw->pw_name != NULL)
4576 {
4577 if (ga_grow(&ga_users, 1) == FAIL)
4578 break;
4579 user = vim_strsave((char_u*)pw->pw_name);
4580 if (user == NULL)
4581 break;
4582 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4583 }
4584 endpwent();
4585 }
4586# endif
4587}
4588
4589/*
4590 * Function given to ExpandGeneric() to obtain an user names.
4591 */
4592 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004593get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004594{
4595 init_users();
4596 if (idx < ga_users.ga_len)
4597 return ((char_u **)ga_users.ga_data)[idx];
4598 return NULL;
4599}
4600
4601/*
4602 * Check whether name matches a user name. Return:
4603 * 0 if name does not match any user name.
4604 * 1 if name partially matches the beginning of a user name.
4605 * 2 is name fully matches a user name.
4606 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004607int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004608{
4609 int i;
4610 int n = (int)STRLEN(name);
4611 int result = 0;
4612
4613 init_users();
4614 for (i = 0; i < ga_users.ga_len; i++)
4615 {
4616 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4617 return 2; /* full match */
4618 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4619 result = 1; /* partial match */
4620 }
4621 return result;
4622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623#endif
4624
4625/*
4626 * Replace home directory by "~" in each space or comma separated file name in
4627 * 'src'.
4628 * If anything fails (except when out of space) dst equals src.
4629 */
4630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004631home_replace(
4632 buf_T *buf, /* when not NULL, check for help files */
4633 char_u *src, /* input file name */
4634 char_u *dst, /* where to put the result */
4635 int dstlen, /* maximum length of the result */
4636 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 spaces and commas in the file name. */
4638{
4639 size_t dirlen = 0, envlen = 0;
4640 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004641 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 char_u *p;
4643
4644 if (src == NULL)
4645 {
4646 *dst = NUL;
4647 return;
4648 }
4649
4650 /*
4651 * If the file is a help file, remove the path completely.
4652 */
4653 if (buf != NULL && buf->b_help)
4654 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004655 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 return;
4657 }
4658
4659 /*
4660 * We check both the value of the $HOME environment variable and the
4661 * "real" home directory.
4662 */
4663 if (homedir != NULL)
4664 dirlen = STRLEN(homedir);
4665
4666#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004667 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004669 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4670#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004671#ifdef WIN3264
4672 if (homedir_env == NULL)
4673 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4674#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004675 /* Empty is the same as not set. */
4676 if (homedir_env != NULL && *homedir_env == NUL)
4677 homedir_env = NULL;
4678
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004679#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004680 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004681 {
4682 int usedlen = 0;
4683 int flen;
4684 char_u *fbuf = NULL;
4685
4686 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004687 (void)modify_fname((char_u *)":p", &usedlen,
4688 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004689 flen = (int)STRLEN(homedir_env);
4690 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4691 /* Remove the trailing / that is added to a directory. */
4692 homedir_env[flen - 1] = NUL;
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#endif
4695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (homedir_env != NULL)
4697 envlen = STRLEN(homedir_env);
4698
4699 if (!one)
4700 src = skipwhite(src);
4701 while (*src && dstlen > 0)
4702 {
4703 /*
4704 * Here we are at the beginning of a file name.
4705 * First, check to see if the beginning of the file name matches
4706 * $HOME or the "real" home directory. Check that there is a '/'
4707 * after the match (so that if e.g. the file is "/home/pieter/bla",
4708 * and the home directory is "/home/piet", the file does not end up
4709 * as "~er/bla" (which would seem to indicate the file "bla" in user
4710 * er's home directory)).
4711 */
4712 p = homedir;
4713 len = dirlen;
4714 for (;;)
4715 {
4716 if ( len
4717 && fnamencmp(src, p, len) == 0
4718 && (vim_ispathsep(src[len])
4719 || (!one && (src[len] == ',' || src[len] == ' '))
4720 || src[len] == NUL))
4721 {
4722 src += len;
4723 if (--dstlen > 0)
4724 *dst++ = '~';
4725
4726 /*
4727 * If it's just the home directory, add "/".
4728 */
4729 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4730 *dst++ = '/';
4731 break;
4732 }
4733 if (p == homedir_env)
4734 break;
4735 p = homedir_env;
4736 len = envlen;
4737 }
4738
4739 /* if (!one) skip to separator: space or comma */
4740 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4741 *dst++ = *src++;
4742 /* skip separator */
4743 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4744 *dst++ = *src++;
4745 }
4746 /* if (dstlen == 0) out of space, what to do??? */
4747
4748 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004749
4750 if (homedir_env != homedir_env_orig)
4751 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752}
4753
4754/*
4755 * Like home_replace, store the replaced string in allocated memory.
4756 * When something fails, NULL is returned.
4757 */
4758 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004759home_replace_save(
4760 buf_T *buf, /* when not NULL, check for help files */
4761 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 char_u *dst;
4764 unsigned len;
4765
4766 len = 3; /* space for "~/" and trailing NUL */
4767 if (src != NULL) /* just in case */
4768 len += (unsigned)STRLEN(src);
4769 dst = alloc(len);
4770 if (dst != NULL)
4771 home_replace(buf, src, dst, len, TRUE);
4772 return dst;
4773}
4774
4775/*
4776 * Compare two file names and return:
4777 * FPC_SAME if they both exist and are the same file.
4778 * FPC_SAMEX if they both don't exist and have the same file name.
4779 * FPC_DIFF if they both exist and are different files.
4780 * FPC_NOTX if they both don't exist.
4781 * FPC_DIFFX if one of them doesn't exist.
4782 * For the first name environment variables are expanded
4783 */
4784 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004785fullpathcmp(
4786 char_u *s1,
4787 char_u *s2,
4788 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790#ifdef UNIX
4791 char_u exp1[MAXPATHL];
4792 char_u full1[MAXPATHL];
4793 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004794 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 int r1, r2;
4796
4797 expand_env(s1, exp1, MAXPATHL);
4798 r1 = mch_stat((char *)exp1, &st1);
4799 r2 = mch_stat((char *)s2, &st2);
4800 if (r1 != 0 && r2 != 0)
4801 {
4802 /* if mch_stat() doesn't work, may compare the names */
4803 if (checkname)
4804 {
4805 if (fnamecmp(exp1, s2) == 0)
4806 return FPC_SAMEX;
4807 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4808 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4809 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4810 return FPC_SAMEX;
4811 }
4812 return FPC_NOTX;
4813 }
4814 if (r1 != 0 || r2 != 0)
4815 return FPC_DIFFX;
4816 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4817 return FPC_SAME;
4818 return FPC_DIFF;
4819#else
4820 char_u *exp1; /* expanded s1 */
4821 char_u *full1; /* full path of s1 */
4822 char_u *full2; /* full path of s2 */
4823 int retval = FPC_DIFF;
4824 int r1, r2;
4825
4826 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4827 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4828 {
4829 full1 = exp1 + MAXPATHL;
4830 full2 = full1 + MAXPATHL;
4831
4832 expand_env(s1, exp1, MAXPATHL);
4833 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4834 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4835
4836 /* If vim_FullName() fails, the file probably doesn't exist. */
4837 if (r1 != OK && r2 != OK)
4838 {
4839 if (checkname && fnamecmp(exp1, s2) == 0)
4840 retval = FPC_SAMEX;
4841 else
4842 retval = FPC_NOTX;
4843 }
4844 else if (r1 != OK || r2 != OK)
4845 retval = FPC_DIFFX;
4846 else if (fnamecmp(full1, full2))
4847 retval = FPC_DIFF;
4848 else
4849 retval = FPC_SAME;
4850 vim_free(exp1);
4851 }
4852 return retval;
4853#endif
4854}
4855
4856/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004857 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004858 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004859 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 */
4861 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004862gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 char_u *p1, *p2;
4865
4866 if (fname == NULL)
4867 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004868 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004870 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004872 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 return p1;
4875}
4876
Bram Moolenaar31710262010-08-13 13:36:15 +02004877#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004878static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004879
4880/*
4881 * Return the end of the directory name, on the first path
4882 * separator:
4883 * "/path/file", "/path/dir/", "/path//dir", "/file"
4884 * ^ ^ ^ ^
4885 */
4886 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004887gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004888{
4889 char_u *dir_end = fname;
4890 char_u *next_dir_end = fname;
4891 int look_for_sep = TRUE;
4892 char_u *p;
4893
4894 for (p = fname; *p != NUL; )
4895 {
4896 if (vim_ispathsep(*p))
4897 {
4898 if (look_for_sep)
4899 {
4900 next_dir_end = p;
4901 look_for_sep = FALSE;
4902 }
4903 }
4904 else
4905 {
4906 if (!look_for_sep)
4907 dir_end = next_dir_end;
4908 look_for_sep = TRUE;
4909 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004910 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004911 }
4912 return dir_end;
4913}
4914#endif
4915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004917 * Get pointer to tail of "fname", including path separators. Putting a NUL
4918 * here leaves the directory name. Takes care of "c:/" and "//".
4919 * Always returns a valid pointer.
4920 */
4921 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004922gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004923{
4924 char_u *p;
4925 char_u *t;
4926
4927 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4928 t = gettail(fname);
4929 while (t > p && after_pathsep(fname, t))
4930 --t;
4931#ifdef VMS
4932 /* path separator is part of the path */
4933 ++t;
4934#endif
4935 return t;
4936}
4937
4938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 * get the next path component (just after the next path separator).
4940 */
4941 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004942getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
4944 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004945 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (*fname)
4947 ++fname;
4948 return fname;
4949}
4950
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951/*
4952 * Get a pointer to one character past the head of a path name.
4953 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4954 * If there is no head, path is returned.
4955 */
4956 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004957get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 char_u *retval;
4960
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004961#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 /* may skip "c:" */
4963 if (isalpha(path[0]) && path[1] == ':')
4964 retval = path + 2;
4965 else
4966 retval = path;
4967#else
4968# if defined(AMIGA)
4969 /* may skip "label:" */
4970 retval = vim_strchr(path, ':');
4971 if (retval == NULL)
4972 retval = path;
4973# else /* Unix */
4974 retval = path;
4975# endif
4976#endif
4977
4978 while (vim_ispathsep(*retval))
4979 ++retval;
4980
4981 return retval;
4982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
4984/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004985 * Return TRUE if 'c' is a path separator.
4986 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 */
4988 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004989vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004991#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004993#else
4994# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004996# else
4997# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4999 return (c == ':' || c == '[' || c == ']' || c == '/'
5000 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005001# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005003# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006}
5007
Bram Moolenaar69c35002013-11-04 02:54:12 +01005008/*
5009 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5010 */
5011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005012vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005013{
5014 return vim_ispathsep(c)
5015#ifdef BACKSLASH_IN_FILENAME
5016 && c != ':'
5017#endif
5018 ;
5019}
5020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5022/*
5023 * return TRUE if 'c' is a path list separator.
5024 */
5025 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005026vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028#ifdef UNIX
5029 return (c == ':');
5030#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005031 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032#endif
5033}
5034#endif
5035
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005036/*
5037 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5038 * It's done in-place.
5039 */
5040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005041shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005042{
5043 char_u *tail, *s, *d;
5044 int skip = FALSE;
5045
5046 tail = gettail(str);
5047 d = str;
5048 for (s = str; ; ++s)
5049 {
5050 if (s >= tail) /* copy the whole tail */
5051 {
5052 *d++ = *s;
5053 if (*s == NUL)
5054 break;
5055 }
5056 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5057 {
5058 *d++ = *s;
5059 skip = FALSE;
5060 }
5061 else if (!skip)
5062 {
5063 *d++ = *s; /* copy next char */
5064 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5065 skip = TRUE;
5066# ifdef FEAT_MBYTE
5067 if (has_mbyte)
5068 {
5069 int l = mb_ptr2len(s);
5070
5071 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005072 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005073 }
5074# endif
5075 }
5076 }
5077}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005078
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005079/*
5080 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5081 * Also returns TRUE if there is no directory name.
5082 * "fname" must be writable!.
5083 */
5084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005085dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005086{
5087 char_u *p;
5088 int c;
5089 int retval;
5090
5091 p = gettail_sep(fname);
5092 if (p == fname)
5093 return TRUE;
5094 c = *p;
5095 *p = NUL;
5096 retval = mch_isdir(fname);
5097 *p = c;
5098 return retval;
5099}
5100
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005102 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5103 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 */
5105 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005106vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005108#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005110#else
5111 if (p_fic)
5112 return MB_STRICMP(x, y);
5113 return STRCMP(x, y);
5114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115}
5116
5117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005118vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005120#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005121 char_u *px = x;
5122 char_u *py = y;
5123 int cx = NUL;
5124 int cy = NUL;
5125
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005126 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005128 cx = PTR2CHAR(px);
5129 cy = PTR2CHAR(py);
5130 if (cx == NUL || cy == NUL
5131 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5132 && !(cx == '/' && cy == '\\')
5133 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005135 len -= MB_PTR2LEN(px);
5136 px += MB_PTR2LEN(px);
5137 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 if (len == 0)
5140 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005141 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005142#else
5143 if (p_fic)
5144 return MB_STRNICMP(x, y, len);
5145 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149/*
5150 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005151 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 */
5153 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005154concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
5156 char_u *dest;
5157
5158 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5159 if (dest != NULL)
5160 {
5161 STRCPY(dest, fname1);
5162 if (sep)
5163 add_pathsep(dest);
5164 STRCAT(dest, fname2);
5165 }
5166 return dest;
5167}
5168
Bram Moolenaard6754642005-01-17 22:18:45 +00005169/*
5170 * Concatenate two strings and return the result in allocated memory.
5171 * Returns NULL when out of memory.
5172 */
5173 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005174concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005175{
5176 char_u *dest;
5177 size_t l = STRLEN(str1);
5178
5179 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5180 if (dest != NULL)
5181 {
5182 STRCPY(dest, str1);
5183 STRCPY(dest + l, str2);
5184 }
5185 return dest;
5186}
Bram Moolenaard6754642005-01-17 22:18:45 +00005187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188/*
5189 * Add a path separator to a file name, unless it already ends in a path
5190 * separator.
5191 */
5192 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005193add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005195 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 STRCAT(p, PATHSEPSTR);
5197}
5198
5199/*
5200 * FullName_save - Make an allocated copy of a full file name.
5201 * Returns NULL when out of memory.
5202 */
5203 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204FullName_save(
5205 char_u *fname,
5206 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005207 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208{
5209 char_u *buf;
5210 char_u *new_fname = NULL;
5211
5212 if (fname == NULL)
5213 return NULL;
5214
5215 buf = alloc((unsigned)MAXPATHL);
5216 if (buf != NULL)
5217 {
5218 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5219 new_fname = vim_strsave(buf);
5220 else
5221 new_fname = vim_strsave(fname);
5222 vim_free(buf);
5223 }
5224 return new_fname;
5225}
5226
5227#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5228
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005229static char_u *skip_string(char_u *p);
5230static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005231static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005232static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234/*
5235 * Find the start of a comment, not knowing if we are in a comment right now.
5236 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005237 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005239 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005241{
5242 return find_start_comment(curbuf->b_ind_maxcomment);
5243}
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005246find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247{
5248 pos_T *pos;
5249 char_u *line;
5250 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005251 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005253 for (;;)
5254 {
5255 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5256 if (pos == NULL)
5257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005259 /*
5260 * Check if the comment start we found is inside a string.
5261 * If it is then restrict the search to below this line and try again.
5262 */
5263 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005264 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005265 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005266 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005267 break;
5268 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5269 if (cur_maxcomment <= 0)
5270 {
5271 pos = NULL;
5272 break;
5273 }
5274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 return pos;
5276}
5277
5278/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005279 * Find the start of a comment or raw string, not knowing if we are in a
5280 * comment or raw string right now.
5281 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005282 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005283 * Return NULL when not inside a comment or raw string.
5284 * "CORS" -> Comment Or Raw String
5285 */
5286 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005287ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005288{
Bram Moolenaar089af182015-10-07 11:41:49 +02005289 static pos_T comment_pos_copy;
5290 pos_T *comment_pos;
5291 pos_T *rs_pos;
5292
5293 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5294 if (comment_pos != NULL)
5295 {
5296 /* Need to make a copy of the static pos in findmatchlimit(),
5297 * calling find_start_rawstring() may change it. */
5298 comment_pos_copy = *comment_pos;
5299 comment_pos = &comment_pos_copy;
5300 }
5301 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005302
5303 /* If comment_pos is before rs_pos the raw string is inside the comment.
5304 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005305 if (comment_pos == NULL || (rs_pos != NULL
5306 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005307 {
5308 if (is_raw != NULL && rs_pos != NULL)
5309 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005310 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005311 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005312 return comment_pos;
5313}
5314
5315/*
5316 * Find the start of a raw string, not knowing if we are in one right now.
5317 * Search starts at w_cursor.lnum and goes backwards.
5318 * Return NULL when not inside a raw string.
5319 */
5320 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005321find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005322{
5323 pos_T *pos;
5324 char_u *line;
5325 char_u *p;
5326 int cur_maxcomment = ind_maxcomment;
5327
5328 for (;;)
5329 {
5330 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5331 if (pos == NULL)
5332 break;
5333
5334 /*
5335 * Check if the raw string start we found is inside a string.
5336 * If it is then restrict the search to below this line and try again.
5337 */
5338 line = ml_get(pos->lnum);
5339 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5340 p = skip_string(p);
5341 if ((colnr_T)(p - line) <= pos->col)
5342 break;
5343 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5344 if (cur_maxcomment <= 0)
5345 {
5346 pos = NULL;
5347 break;
5348 }
5349 }
5350 return pos;
5351}
5352
5353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 * Skip to the end of a "string" and a 'c' character.
5355 * If there is no string or character, return argument unmodified.
5356 */
5357 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005358skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
5360 int i;
5361
5362 /*
5363 * We loop, because strings may be concatenated: "date""time".
5364 */
5365 for ( ; ; ++p)
5366 {
5367 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5368 {
5369 if (!p[1]) /* ' at end of line */
5370 break;
5371 i = 2;
5372 if (p[1] == '\\') /* '\n' or '\000' */
5373 {
5374 ++i;
5375 while (vim_isdigit(p[i - 1])) /* '\000' */
5376 ++i;
5377 }
5378 if (p[i] == '\'') /* check for trailing ' */
5379 {
5380 p += i;
5381 continue;
5382 }
5383 }
5384 else if (p[0] == '"') /* start of string */
5385 {
5386 for (++p; p[0]; ++p)
5387 {
5388 if (p[0] == '\\' && p[1] != NUL)
5389 ++p;
5390 else if (p[0] == '"') /* end of string */
5391 break;
5392 }
5393 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005394 continue; /* continue for another string */
5395 }
5396 else if (p[0] == 'R' && p[1] == '"')
5397 {
5398 /* Raw string: R"[delim](...)[delim]" */
5399 char_u *delim = p + 2;
5400 char_u *paren = vim_strchr(delim, '(');
5401
5402 if (paren != NULL)
5403 {
5404 size_t delim_len = paren - delim;
5405
5406 for (p += 3; *p; ++p)
5407 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5408 && p[delim_len + 1] == '"')
5409 {
5410 p += delim_len + 1;
5411 break;
5412 }
5413 if (p[0] == '"')
5414 continue; /* continue for another string */
5415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
5417 break; /* no string found */
5418 }
5419 if (!*p)
5420 --p; /* backup from NUL */
5421 return p;
5422}
5423#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5424
5425#if defined(FEAT_CINDENT) || defined(PROTO)
5426
5427/*
5428 * Do C or expression indenting on the current line.
5429 */
5430 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005431do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433# ifdef FEAT_EVAL
5434 if (*curbuf->b_p_inde != NUL)
5435 fixthisline(get_expr_indent);
5436 else
5437# endif
5438 fixthisline(get_c_indent);
5439}
5440
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005441/* Find result cache for cpp_baseclass */
5442typedef struct {
5443 int found;
5444 lpos_T lpos;
5445} cpp_baseclass_cache_T;
5446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447/*
5448 * Functions for C-indenting.
5449 * Most of this originally comes from Eric Fischer.
5450 */
5451/*
5452 * Below "XXX" means that this function may unlock the current line.
5453 */
5454
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005455static char_u *cin_skipcomment(char_u *);
5456static int cin_nocode(char_u *);
5457static pos_T *find_line_comment(void);
5458static int cin_has_js_key(char_u *text);
5459static int cin_islabel_skip(char_u **);
5460static int cin_isdefault(char_u *);
5461static char_u *after_label(char_u *l);
5462static int get_indent_nolabel(linenr_T lnum);
5463static int skip_label(linenr_T, char_u **pp);
5464static int cin_first_id_amount(void);
5465static int cin_get_equal_amount(linenr_T lnum);
5466static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005467static int cin_iscomment(char_u *);
5468static int cin_islinecomment(char_u *);
5469static int cin_isterminated(char_u *, int, int);
5470static int cin_isinit(void);
5471static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5472static int cin_isif(char_u *);
5473static int cin_iselse(char_u *);
5474static int cin_isdo(char_u *);
5475static int cin_iswhileofdo(char_u *, linenr_T);
5476static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5477static int cin_iswhileofdo_end(int terminated);
5478static int cin_isbreak(char_u *);
5479static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5480static int get_baseclass_amount(int col);
5481static int cin_ends_in(char_u *, char_u *, char_u *);
5482static int cin_starts_with(char_u *s, char *word);
5483static int cin_skip2pos(pos_T *trypos);
5484static pos_T *find_start_brace(void);
5485static pos_T *find_match_paren(int);
5486static pos_T *find_match_char(int c, int ind_maxparen);
5487static int corr_ind_maxparen(pos_T *startpos);
5488static int find_last_paren(char_u *l, int start, int end);
5489static int find_match(int lookfor, linenr_T ourscope);
5490static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492/*
5493 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005494 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 */
5496 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005497cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499 while (*s)
5500 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005501 char_u *prev_s = s;
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005504
5505 /* Perl/shell # comment comment continues until eol. Require a space
5506 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005507 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005508 {
5509 s += STRLEN(s);
5510 break;
5511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if (*s != '/')
5513 break;
5514 ++s;
5515 if (*s == '/') /* slash-slash comment continues till eol */
5516 {
5517 s += STRLEN(s);
5518 break;
5519 }
5520 if (*s != '*')
5521 break;
5522 for (++s; *s; ++s) /* skip slash-star comment */
5523 if (s[0] == '*' && s[1] == '/')
5524 {
5525 s += 2;
5526 break;
5527 }
5528 }
5529 return s;
5530}
5531
5532/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005533 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 * not considered code.
5535 */
5536 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005537cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 return *cin_skipcomment(s) == NUL;
5540}
5541
5542/*
5543 * Check previous lines for a "//" line comment, skipping over blank lines.
5544 */
5545 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005546find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 static pos_T pos;
5549 char_u *line;
5550 char_u *p;
5551
5552 pos = curwin->w_cursor;
5553 while (--pos.lnum > 0)
5554 {
5555 line = ml_get(pos.lnum);
5556 p = skipwhite(line);
5557 if (cin_islinecomment(p))
5558 {
5559 pos.col = (int)(p - line);
5560 return &pos;
5561 }
5562 if (*p != NUL)
5563 break;
5564 }
5565 return NULL;
5566}
5567
5568/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005569 * Return TRUE if "text" starts with "key:".
5570 */
5571 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005572cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005573{
5574 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005575 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005576
5577 if (*s == '\'' || *s == '"')
5578 {
5579 /* can be 'key': or "key": */
5580 quote = *s;
5581 ++s;
5582 }
5583 if (!vim_isIDc(*s)) /* need at least one ID character */
5584 return FALSE;
5585
5586 while (vim_isIDc(*s))
5587 ++s;
5588 if (*s == quote)
5589 ++s;
5590
5591 s = cin_skipcomment(s);
5592
5593 /* "::" is not a label, it's C++ */
5594 return (*s == ':' && s[1] != ':');
5595}
5596
5597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005599 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 */
5601 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005602cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 if (!vim_isIDc(**s)) /* need at least one ID character */
5605 return FALSE;
5606
5607 while (vim_isIDc(**s))
5608 (*s)++;
5609
5610 *s = cin_skipcomment(*s);
5611
5612 /* "::" is not a label, it's C++ */
5613 return (**s == ':' && *++*s != ':');
5614}
5615
5616/*
5617 * Recognize a label: "label:".
5618 * Note: curwin->w_cursor must be where we are looking for the label.
5619 */
5620 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005621cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622{
5623 char_u *s;
5624
5625 s = cin_skipcomment(ml_get_curline());
5626
5627 /*
5628 * Exclude "default" from labels, since it should be indented
5629 * like a switch label. Same for C++ scope declarations.
5630 */
5631 if (cin_isdefault(s))
5632 return FALSE;
5633 if (cin_isscopedecl(s))
5634 return FALSE;
5635
5636 if (cin_islabel_skip(&s))
5637 {
5638 /*
5639 * Only accept a label if the previous line is terminated or is a case
5640 * label.
5641 */
5642 pos_T cursor_save;
5643 pos_T *trypos;
5644 char_u *line;
5645
5646 cursor_save = curwin->w_cursor;
5647 while (curwin->w_cursor.lnum > 1)
5648 {
5649 --curwin->w_cursor.lnum;
5650
5651 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005652 * If we're in a comment or raw string now, skip to the start of
5653 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 */
5655 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005656 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 curwin->w_cursor = *trypos;
5658
5659 line = ml_get_curline();
5660 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5661 continue;
5662 if (*(line = cin_skipcomment(line)) == NUL)
5663 continue;
5664
5665 curwin->w_cursor = cursor_save;
5666 if (cin_isterminated(line, TRUE, FALSE)
5667 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005668 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 || (cin_islabel_skip(&line) && cin_nocode(line)))
5670 return TRUE;
5671 return FALSE;
5672 }
5673 curwin->w_cursor = cursor_save;
5674 return TRUE; /* label at start of file??? */
5675 }
5676 return FALSE;
5677}
5678
5679/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005680 * Recognize structure initialization and enumerations:
5681 * "[typedef] [static|public|protected|private] enum"
5682 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 */
5684 static int
5685cin_isinit(void)
5686{
5687 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005688 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 s = cin_skipcomment(ml_get_curline());
5691
Bram Moolenaar75342212013-03-07 13:13:52 +01005692 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 s = cin_skipcomment(s + 7);
5694
Bram Moolenaar75342212013-03-07 13:13:52 +01005695 for (;;)
5696 {
5697 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005698
Bram Moolenaar75342212013-03-07 13:13:52 +01005699 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5700 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005701 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005702 if (cin_starts_with(s, skip[i]))
5703 {
5704 s = cin_skipcomment(s + l);
5705 l = 0;
5706 break;
5707 }
5708 }
5709 if (l != 0)
5710 break;
5711 }
5712
5713 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 return TRUE;
5715
5716 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5717 return TRUE;
5718
5719 return FALSE;
5720}
5721
5722/*
5723 * Recognize a switch label: "case .*:" or "default:".
5724 */
5725 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005726cin_iscase(
5727 char_u *s,
5728 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005731 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
5733 for (s += 4; *s; ++s)
5734 {
5735 s = cin_skipcomment(s);
5736 if (*s == ':')
5737 {
5738 if (s[1] == ':') /* skip over "::" for C++ */
5739 ++s;
5740 else
5741 return TRUE;
5742 }
5743 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005744 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5746 return FALSE; /* stop at comment */
5747 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005748 {
5749 /* JS etc. */
5750 if (strict)
5751 return FALSE; /* stop at string */
5752 else
5753 return TRUE;
5754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
5756 return FALSE;
5757 }
5758
5759 if (cin_isdefault(s))
5760 return TRUE;
5761 return FALSE;
5762}
5763
5764/*
5765 * Recognize a "default" switch label.
5766 */
5767 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005768cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 return (STRNCMP(s, "default", 7) == 0
5771 && *(s = cin_skipcomment(s + 7)) == ':'
5772 && s[1] != ':');
5773}
5774
5775/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005776 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 */
5778 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005779cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 int i;
5782
5783 s = cin_skipcomment(s);
5784 if (STRNCMP(s, "public", 6) == 0)
5785 i = 6;
5786 else if (STRNCMP(s, "protected", 9) == 0)
5787 i = 9;
5788 else if (STRNCMP(s, "private", 7) == 0)
5789 i = 7;
5790 else
5791 return FALSE;
5792 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5793}
5794
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005795/* Maximum number of lines to search back for a "namespace" line. */
5796#define FIND_NAMESPACE_LIM 20
5797
5798/*
5799 * Recognize a "namespace" scope declaration.
5800 */
5801 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005802cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005803{
5804 char_u *p;
5805 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005806 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005807
5808 s = cin_skipcomment(s);
5809 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5810 {
5811 p = cin_skipcomment(skipwhite(s + 9));
5812 while (*p != NUL)
5813 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005814 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005815 {
5816 has_name = TRUE; /* found end of a name */
5817 p = cin_skipcomment(skipwhite(p));
5818 }
5819 else if (*p == '{')
5820 {
5821 break;
5822 }
5823 else if (vim_iswordc(*p))
5824 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005825 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005826 if (has_name)
5827 return FALSE; /* word character after skipping past name */
5828 ++p;
5829 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005830 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5831 {
5832 if (!has_name_start || has_name)
5833 return FALSE;
5834 /* C++ 17 nested namespace */
5835 p += 3;
5836 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005837 else
5838 {
5839 return FALSE;
5840 }
5841 }
5842 return TRUE;
5843 }
5844 return FALSE;
5845}
5846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005848 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5849 */
5850 static int
5851cin_is_cpp_extern_c(char_u *s)
5852{
5853 char_u *p;
5854 int has_string_literal = FALSE;
5855
5856 s = cin_skipcomment(s);
5857 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5858 {
5859 p = cin_skipcomment(skipwhite(s + 6));
5860 while (*p != NUL)
5861 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005862 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005863 {
5864 p = cin_skipcomment(skipwhite(p));
5865 }
5866 else if (*p == '{')
5867 {
5868 break;
5869 }
5870 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5871 {
5872 if (has_string_literal)
5873 return FALSE;
5874 has_string_literal = TRUE;
5875 p += 3;
5876 }
5877 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5878 && p[4] == '"')
5879 {
5880 if (has_string_literal)
5881 return FALSE;
5882 has_string_literal = TRUE;
5883 p += 5;
5884 }
5885 else
5886 {
5887 return FALSE;
5888 }
5889 }
5890 return has_string_literal ? TRUE : FALSE;
5891 }
5892 return FALSE;
5893}
5894
5895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * Return a pointer to the first non-empty non-comment character after a ':'.
5897 * Return NULL if not found.
5898 * case 234: a = b;
5899 * ^
5900 */
5901 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005902after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
5904 for ( ; *l; ++l)
5905 {
5906 if (*l == ':')
5907 {
5908 if (l[1] == ':') /* skip over "::" for C++ */
5909 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005910 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 break;
5912 }
5913 else if (*l == '\'' && l[1] && l[2] == '\'')
5914 l += 2; /* skip over 'x' */
5915 }
5916 if (*l == NUL)
5917 return NULL;
5918 l = cin_skipcomment(l + 1);
5919 if (*l == NUL)
5920 return NULL;
5921 return l;
5922}
5923
5924/*
5925 * Get indent of line "lnum", skipping a label.
5926 * Return 0 if there is nothing after the label.
5927 */
5928 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005929get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 char_u *l;
5932 pos_T fp;
5933 colnr_T col;
5934 char_u *p;
5935
5936 l = ml_get(lnum);
5937 p = after_label(l);
5938 if (p == NULL)
5939 return 0;
5940
5941 fp.col = (colnr_T)(p - l);
5942 fp.lnum = lnum;
5943 getvcol(curwin, &fp, &col, NULL, NULL);
5944 return (int)col;
5945}
5946
5947/*
5948 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005949 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 * label: if (asdf && asdfasdf)
5951 * ^
5952 */
5953 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005954skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 char_u *l;
5957 int amount;
5958 pos_T cursor_save;
5959
5960 cursor_save = curwin->w_cursor;
5961 curwin->w_cursor.lnum = lnum;
5962 l = ml_get_curline();
5963 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005964 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {
5966 amount = get_indent_nolabel(lnum);
5967 l = after_label(ml_get_curline());
5968 if (l == NULL) /* just in case */
5969 l = ml_get_curline();
5970 }
5971 else
5972 {
5973 amount = get_indent();
5974 l = ml_get_curline();
5975 }
5976 *pp = l;
5977
5978 curwin->w_cursor = cursor_save;
5979 return amount;
5980}
5981
5982/*
5983 * Return the indent of the first variable name after a type in a declaration.
5984 * int a, indent of "a"
5985 * static struct foo b, indent of "b"
5986 * enum bla c, indent of "c"
5987 * Returns zero when it doesn't look like a declaration.
5988 */
5989 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005990cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 char_u *line, *p, *s;
5993 int len;
5994 pos_T fp;
5995 colnr_T col;
5996
5997 line = ml_get_curline();
5998 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005999 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6001 {
6002 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006003 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 }
6005 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6006 p = skipwhite(p + 6);
6007 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6008 p = skipwhite(p + 4);
6009 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6010 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6011 {
6012 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006013 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6014 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6015 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6016 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 p = s;
6018 }
6019 for (len = 0; vim_isIDc(p[len]); ++len)
6020 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006021 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 return 0;
6023
6024 p = skipwhite(p + len);
6025 fp.lnum = curwin->w_cursor.lnum;
6026 fp.col = (colnr_T)(p - line);
6027 getvcol(curwin, &fp, &col, NULL, NULL);
6028 return (int)col;
6029}
6030
6031/*
6032 * Return the indent of the first non-blank after an equal sign.
6033 * char *foo = "here";
6034 * Return zero if no (useful) equal sign found.
6035 * Return -1 if the line above "lnum" ends in a backslash.
6036 * foo = "asdf\
6037 * asdf\
6038 * here";
6039 */
6040 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006041cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
6043 char_u *line;
6044 char_u *s;
6045 colnr_T col;
6046 pos_T fp;
6047
6048 if (lnum > 1)
6049 {
6050 line = ml_get(lnum - 1);
6051 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6052 return -1;
6053 }
6054
6055 line = s = ml_get(lnum);
6056 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6057 {
6058 if (cin_iscomment(s)) /* ignore comments */
6059 s = cin_skipcomment(s);
6060 else
6061 ++s;
6062 }
6063 if (*s != '=')
6064 return 0;
6065
6066 s = skipwhite(s + 1);
6067 if (cin_nocode(s))
6068 return 0;
6069
6070 if (*s == '"') /* nice alignment for continued strings */
6071 ++s;
6072
6073 fp.lnum = lnum;
6074 fp.col = (colnr_T)(s - line);
6075 getvcol(curwin, &fp, &col, NULL, NULL);
6076 return (int)col;
6077}
6078
6079/*
6080 * Recognize a preprocessor statement: Any line that starts with '#'.
6081 */
6082 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006083cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006085 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 return TRUE;
6087 return FALSE;
6088}
6089
6090/*
6091 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6092 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6093 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006094 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 */
6096 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006097cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
6099 char_u *line = *pp;
6100 linenr_T lnum = *lnump;
6101 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006102 int candidate_amount = *amount;
6103
6104 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6105 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006107 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 {
6109 if (cin_ispreproc(line))
6110 {
6111 retval = TRUE;
6112 *lnump = lnum;
6113 break;
6114 }
6115 if (lnum == 1)
6116 break;
6117 line = ml_get(--lnum);
6118 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6119 break;
6120 }
6121
6122 if (lnum != *lnump)
6123 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006124 if (retval)
6125 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 return retval;
6127}
6128
6129/*
6130 * Recognize the start of a C or C++ comment.
6131 */
6132 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006133cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
6135 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6136}
6137
6138/*
6139 * Recognize the start of a "//" comment.
6140 */
6141 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006142cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 return (p[0] == '/' && p[1] == '/');
6145}
6146
6147/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006148 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6149 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006151 * If a line begins with an "else", only consider it terminated if no unmatched
6152 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 * Return the character terminating the line (ending char's have precedence if
6154 * both apply in order to determine initializations).
6155 */
6156 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006157cin_isterminated(
6158 char_u *s,
6159 int incl_open, /* include '{' at the end as terminator */
6160 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006162 char_u found_start = 0;
6163 unsigned n_open = 0;
6164 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166 s = cin_skipcomment(s);
6167
6168 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6169 found_start = *s;
6170
Bram Moolenaar496f9512011-05-19 16:35:09 +02006171 if (!found_start)
6172 is_else = cin_iselse(s);
6173
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 while (*s)
6175 {
6176 /* skip over comments, "" strings and 'c'haracters */
6177 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006178 if (*s == '}' && n_open > 0)
6179 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006180 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006181 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 && cin_nocode(s + 1))
6183 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006184 else if (*s == '{')
6185 {
6186 if (incl_open && cin_nocode(s + 1))
6187 return *s;
6188 else
6189 ++n_open;
6190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191
6192 if (*s)
6193 s++;
6194 }
6195 return found_start;
6196}
6197
6198/*
6199 * Recognize the basic picture of a function declaration -- it needs to
6200 * have an open paren somewhere and a close paren at the end of the line and
6201 * no semicolons anywhere.
6202 * When a line ends in a comma we continue looking in the next line.
6203 * "sp" points to a string with the line. When looking at other lines it must
6204 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006205 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006206 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 */
6208 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006209cin_isfuncdecl(
6210 char_u **sp,
6211 linenr_T first_lnum,
6212 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 char_u *s;
6215 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006216 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006218 pos_T *trypos;
6219 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221 if (sp == NULL)
6222 s = ml_get(lnum);
6223 else
6224 s = *sp;
6225
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006226 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006227 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006228 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006229 {
6230 lnum = trypos->lnum;
6231 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006232 {
6233 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006234 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006235 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006236
6237 s = ml_get(lnum);
6238 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006239 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006240
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006241 /* Ignore line starting with #. */
6242 if (cin_ispreproc(s))
6243 return FALSE;
6244
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6246 {
6247 if (cin_iscomment(s)) /* ignore comments */
6248 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006249 else if (*s == ':')
6250 {
6251 if (*(s + 1) == ':')
6252 s += 2;
6253 else
6254 /* To avoid a mistake in the following situation:
6255 * A::A(int a, int b)
6256 * : a(0) // <--not a function decl
6257 * , b(0)
6258 * {...
6259 */
6260 return FALSE;
6261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 else
6263 ++s;
6264 }
6265 if (*s != '(')
6266 return FALSE; /* ';', ' or " before any () or no '(' */
6267
6268 while (*s && *s != ';' && *s != '\'' && *s != '"')
6269 {
6270 if (*s == ')' && cin_nocode(s + 1))
6271 {
6272 /* ')' at the end: may have found a match
6273 * Check for he previous line not to end in a backslash:
6274 * #if defined(x) && \
6275 * defined(y)
6276 */
6277 lnum = first_lnum - 1;
6278 s = ml_get(lnum);
6279 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6280 retval = TRUE;
6281 goto done;
6282 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006283 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006285 int comma = (*s == ',');
6286
6287 /* ',' at the end: continue looking in the next line.
6288 * At the end: check for ',' in the next line, for this style:
6289 * func(arg1
6290 * , arg2) */
6291 for (;;)
6292 {
6293 if (lnum >= curbuf->b_ml.ml_line_count)
6294 break;
6295 s = ml_get(++lnum);
6296 if (!cin_ispreproc(s))
6297 break;
6298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (lnum >= curbuf->b_ml.ml_line_count)
6300 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006301 /* Require a comma at end of the line or a comma or ')' at the
6302 * start of next line. */
6303 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006304 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006305 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006306 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
6308 else if (cin_iscomment(s)) /* ignore comments */
6309 s = cin_skipcomment(s);
6310 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006313 just_started = FALSE;
6314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316
6317done:
6318 if (lnum != first_lnum && sp != NULL)
6319 *sp = ml_get(first_lnum);
6320
6321 return retval;
6322}
6323
6324 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006325cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006327 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328}
6329
6330 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006331cin_iselse(
6332 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 if (*p == '}') /* accept "} else" */
6335 p = cin_skipcomment(p + 1);
6336 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6337}
6338
6339 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006340cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6343}
6344
6345/*
6346 * Check if this is a "while" that should have a matching "do".
6347 * We only accept a "while (condition) ;", with only white space between the
6348 * ')' and ';'. The condition may be spread over several lines.
6349 */
6350 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006351cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 pos_T cursor_save;
6354 pos_T *trypos;
6355 int retval = FALSE;
6356
6357 p = cin_skipcomment(p);
6358 if (*p == '}') /* accept "} while (cond);" */
6359 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006360 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
6362 cursor_save = curwin->w_cursor;
6363 curwin->w_cursor.lnum = lnum;
6364 curwin->w_cursor.col = 0;
6365 p = ml_get_curline();
6366 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6367 {
6368 ++p;
6369 ++curwin->w_cursor.col;
6370 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006371 if ((trypos = findmatchlimit(NULL, 0, 0,
6372 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6374 retval = TRUE;
6375 curwin->w_cursor = cursor_save;
6376 }
6377 return retval;
6378}
6379
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006380/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006381 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006382 * Return 0 if there is none.
6383 * Otherwise return !0 and update "*poffset" to point to the place where the
6384 * string was found.
6385 */
6386 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006387cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006388{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006389 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006390
6391 if (offset-- < 2)
6392 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006393 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006394 --offset;
6395
6396 offset -= 1;
6397 if (!STRNCMP(line + offset, "if", 2))
6398 goto probablyFound;
6399
6400 if (offset >= 1)
6401 {
6402 offset -= 1;
6403 if (!STRNCMP(line + offset, "for", 3))
6404 goto probablyFound;
6405
6406 if (offset >= 2)
6407 {
6408 offset -= 2;
6409 if (!STRNCMP(line + offset, "while", 5))
6410 goto probablyFound;
6411 }
6412 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006413 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006414
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006415probablyFound:
6416 if (!offset || !vim_isIDc(line[offset - 1]))
6417 {
6418 *poffset = offset;
6419 return 1;
6420 }
6421 return 0;
6422}
6423
6424/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006425 * Return TRUE if we are at the end of a do-while.
6426 * do
6427 * nothing;
6428 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006429 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006430 * Adjust the cursor to the line with "while".
6431 */
6432 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006433cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006434{
6435 char_u *line;
6436 char_u *p;
6437 char_u *s;
6438 pos_T *trypos;
6439 int i;
6440
6441 if (terminated != ';') /* there must be a ';' at the end */
6442 return FALSE;
6443
6444 p = line = ml_get_curline();
6445 while (*p != NUL)
6446 {
6447 p = cin_skipcomment(p);
6448 if (*p == ')')
6449 {
6450 s = skipwhite(p + 1);
6451 if (*s == ';' && cin_nocode(s + 1))
6452 {
6453 /* Found ");" at end of the line, now check there is "while"
6454 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006455 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006456 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006457 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006458 if (trypos != NULL)
6459 {
6460 s = cin_skipcomment(ml_get(trypos->lnum));
6461 if (*s == '}') /* accept "} while (cond);" */
6462 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006463 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006464 {
6465 curwin->w_cursor.lnum = trypos->lnum;
6466 return TRUE;
6467 }
6468 }
6469
6470 /* Searching may have made "line" invalid, get it again. */
6471 line = ml_get_curline();
6472 p = line + i;
6473 }
6474 }
6475 if (*p != NUL)
6476 ++p;
6477 }
6478 return FALSE;
6479}
6480
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006482cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6485}
6486
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006487/*
6488 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 * constructor-initialization. eg:
6490 *
6491 * class MyClass :
6492 * baseClass <-- here
6493 * class MyClass : public baseClass,
6494 * anotherBaseClass <-- here (should probably lineup ??)
6495 * MyClass::MyClass(...) :
6496 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006497 *
6498 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
6500 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006501cin_is_cpp_baseclass(
6502 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006504 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 char_u *s;
6506 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006507 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006508 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006510 if (pos->lnum <= lnum)
6511 return cached->found; /* Use the cached result */
6512
6513 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006515 s = skipwhite(line);
6516 if (*s == '#') /* skip #define FOO x ? (x) : x */
6517 return FALSE;
6518 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 if (*s == NUL)
6520 return FALSE;
6521
6522 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6523
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006524 /* Search for a line starting with '#', empty, ending in ';' or containing
6525 * '{' or '}' and start below it. This handles the following situations:
6526 * a = cond ?
6527 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006528 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006529 * func::foo()
6530 * : something
6531 * {}
6532 * Foo::Foo (int one, int two)
6533 * : something(4),
6534 * somethingelse(3)
6535 * {}
6536 */
6537 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006539 line = ml_get(lnum - 1);
6540 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006541 if (*s == '#' || *s == NUL)
6542 break;
6543 while (*s != NUL)
6544 {
6545 s = cin_skipcomment(s);
6546 if (*s == '{' || *s == '}'
6547 || (*s == ';' && cin_nocode(s + 1)))
6548 break;
6549 if (*s != NUL)
6550 ++s;
6551 }
6552 if (*s != NUL)
6553 break;
6554 --lnum;
6555 }
6556
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006557 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006558 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006559 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006560 for (;;)
6561 {
6562 if (*s == NUL)
6563 {
6564 if (lnum == curwin->w_cursor.lnum)
6565 break;
6566 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006567 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006568 s = line;
6569 }
6570 if (s == line)
6571 {
6572 /* don't recognize "case (foo):" as a baseclass */
6573 if (cin_iscase(s, FALSE))
6574 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006575 s = cin_skipcomment(line);
6576 if (*s == NUL)
6577 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006578 }
6579
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006580 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006581 s = skip_string(s) + 1;
6582 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 {
6584 if (s[1] == ':')
6585 {
6586 /* skip double colon. It can't be a constructor
6587 * initialization any more */
6588 lookfor_ctor_init = FALSE;
6589 s = cin_skipcomment(s + 2);
6590 }
6591 else if (lookfor_ctor_init || class_or_struct)
6592 {
6593 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006594 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 cpp_base_class = TRUE;
6596 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006597 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 s = cin_skipcomment(s + 1);
6599 }
6600 else
6601 s = cin_skipcomment(s + 1);
6602 }
6603 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6604 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6605 {
6606 class_or_struct = TRUE;
6607 lookfor_ctor_init = FALSE;
6608
6609 if (*s == 'c')
6610 s = cin_skipcomment(s + 5);
6611 else
6612 s = cin_skipcomment(s + 6);
6613 }
6614 else
6615 {
6616 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6617 {
6618 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6619 }
6620 else if (s[0] == ')')
6621 {
6622 /* Constructor-initialization is assumed if we come across
6623 * something like "):" */
6624 class_or_struct = FALSE;
6625 lookfor_ctor_init = TRUE;
6626 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006627 else if (s[0] == '?')
6628 {
6629 /* Avoid seeing '() :' after '?' as constructor init. */
6630 return FALSE;
6631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 else if (!vim_isIDc(s[0]))
6633 {
6634 /* if it is not an identifier, we are wrong */
6635 class_or_struct = FALSE;
6636 lookfor_ctor_init = FALSE;
6637 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006638 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
6640 /* it can't be a constructor-initialization any more */
6641 lookfor_ctor_init = FALSE;
6642
6643 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006644 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006645 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 }
6647
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006648 /* When the line ends in a comma don't align with it. */
6649 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006650 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006651
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 s = cin_skipcomment(s + 1);
6653 }
6654 }
6655
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006656 cached->found = cpp_base_class;
6657 if (cpp_base_class)
6658 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 return cpp_base_class;
6660}
6661
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006662 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006663get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006664{
6665 int amount;
6666 colnr_T vcol;
6667 pos_T *trypos;
6668
6669 if (col == 0)
6670 {
6671 amount = get_indent();
6672 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006673 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006674 amount = get_indent_lnum(trypos->lnum); /* XXX */
6675 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006676 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006677 }
6678 else
6679 {
6680 curwin->w_cursor.col = col;
6681 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6682 amount = (int)vcol;
6683 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006684 if (amount < curbuf->b_ind_cpp_baseclass)
6685 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006686 return amount;
6687}
6688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689/*
6690 * Return TRUE if string "s" ends with the string "find", possibly followed by
6691 * white space and comments. Skip strings and comments.
6692 * Ignore "ignore" after "find" if it's not NULL.
6693 */
6694 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006695cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 char_u *p = s;
6698 char_u *r;
6699 int len = (int)STRLEN(find);
6700
6701 while (*p != NUL)
6702 {
6703 p = cin_skipcomment(p);
6704 if (STRNCMP(p, find, len) == 0)
6705 {
6706 r = skipwhite(p + len);
6707 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6708 r = skipwhite(r + STRLEN(ignore));
6709 if (cin_nocode(r))
6710 return TRUE;
6711 }
6712 if (*p != NUL)
6713 ++p;
6714 }
6715 return FALSE;
6716}
6717
6718/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006719 * Return TRUE when "s" starts with "word" and then a non-ID character.
6720 */
6721 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006722cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006723{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006724 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006725
6726 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6727}
6728
6729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 * Skip strings, chars and comments until at or past "trypos".
6731 * Return the column found.
6732 */
6733 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006734cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 char_u *line;
6737 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006738 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
6740 p = line = ml_get(trypos->lnum);
6741 while (*p && (colnr_T)(p - line) < trypos->col)
6742 {
6743 if (cin_iscomment(p))
6744 p = cin_skipcomment(p);
6745 else
6746 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006747 new_p = skip_string(p);
6748 if (new_p == p)
6749 ++p;
6750 else
6751 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753 }
6754 return (int)(p - line);
6755}
6756
6757/*
6758 * Find the '{' at the start of the block we are in.
6759 * Return NULL if no match found.
6760 * Ignore a '{' that is in a comment, makes indenting the next three lines
6761 * work. */
6762/* foo() */
6763/* { */
6764/* } */
6765
6766 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006767find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 pos_T cursor_save;
6770 pos_T *trypos;
6771 pos_T *pos;
6772 static pos_T pos_copy;
6773
6774 cursor_save = curwin->w_cursor;
6775 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6776 {
6777 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6778 trypos = &pos_copy;
6779 curwin->w_cursor = *trypos;
6780 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006781 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006783 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 break;
6785 if (pos != NULL)
6786 curwin->w_cursor.lnum = pos->lnum;
6787 }
6788 curwin->w_cursor = cursor_save;
6789 return trypos;
6790}
6791
6792/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006793 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006794 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 */
6796 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006797find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006799 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006800}
6801
6802 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006803find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006804{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 pos_T cursor_save;
6806 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006807 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006808 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006811 ind_maxp_wk = ind_maxparen;
6812retry:
6813 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
6815 /* check if the ( is in a // comment */
6816 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006817 {
6818 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6819 if (ind_maxp_wk > 0)
6820 {
6821 curwin->w_cursor = *trypos;
6822 curwin->w_cursor.col = 0; /* XXX */
6823 goto retry;
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 else
6828 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006829 pos_T *trypos_wk;
6830
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6832 trypos = &pos_copy;
6833 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006834 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006835 {
6836 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6837 - trypos_wk->lnum);
6838 if (ind_maxp_wk > 0)
6839 {
6840 curwin->w_cursor = *trypos_wk;
6841 goto retry;
6842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846 }
6847 curwin->w_cursor = cursor_save;
6848 return trypos;
6849}
6850
6851/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006852 * Find the matching '(', ignoring it if it is in a comment or before an
6853 * unmatched {.
6854 * Return NULL if no match found.
6855 */
6856 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006857find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006858{
6859 pos_T *trypos = find_match_paren(ind_maxparen);
6860
6861 if (trypos != NULL)
6862 {
6863 pos_T *tryposBrace = find_start_brace();
6864
6865 /* If both an unmatched '(' and '{' is found. Ignore the '('
6866 * position if the '{' is further down. */
6867 if (tryposBrace != NULL
6868 && (trypos->lnum != tryposBrace->lnum
6869 ? trypos->lnum < tryposBrace->lnum
6870 : trypos->col < tryposBrace->col))
6871 trypos = NULL;
6872 }
6873 return trypos;
6874}
6875
6876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 * Return ind_maxparen corrected for the difference in line number between the
6878 * cursor position and "startpos". This makes sure that searching for a
6879 * matching paren above the cursor line doesn't find a match because of
6880 * looking a few lines further.
6881 */
6882 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006883corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6886
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006887 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6888 return curbuf->b_ind_maxparen - (int)n;
6889 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890}
6891
6892/*
6893 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006894 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 */
6896 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006897find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898{
6899 int i;
6900 int retval = FALSE;
6901 int open_count = 0;
6902
6903 curwin->w_cursor.col = 0; /* default is start of line */
6904
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006905 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
6907 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6908 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6909 if (l[i] == start)
6910 ++open_count;
6911 else if (l[i] == end)
6912 {
6913 if (open_count > 0)
6914 --open_count;
6915 else
6916 {
6917 curwin->w_cursor.col = i;
6918 retval = TRUE;
6919 }
6920 }
6921 }
6922 return retval;
6923}
6924
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006925/*
6926 * Parse 'cinoptions' and set the values in "curbuf".
6927 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6928 */
6929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006930parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006931{
6932 char_u *p;
6933 char_u *l;
6934 char_u *digits;
6935 int n;
6936 int divider;
6937 int fraction = 0;
6938 int sw = (int)get_sw_value(buf);
6939
6940 /*
6941 * Set the default values.
6942 */
6943 /* Spaces from a block's opening brace the prevailing indent for that
6944 * block should be. */
6945 buf->b_ind_level = sw;
6946
6947 /* Spaces from the edge of the line an open brace that's at the end of a
6948 * line is imagined to be. */
6949 buf->b_ind_open_imag = 0;
6950
6951 /* Spaces from the prevailing indent for a line that is not preceded by
6952 * an opening brace. */
6953 buf->b_ind_no_brace = 0;
6954
6955 /* Column where the first { of a function should be located }. */
6956 buf->b_ind_first_open = 0;
6957
6958 /* Spaces from the prevailing indent a leftmost open brace should be
6959 * located. */
6960 buf->b_ind_open_extra = 0;
6961
6962 /* Spaces from the matching open brace (real location for one at the left
6963 * edge; imaginary location from one that ends a line) the matching close
6964 * brace should be located. */
6965 buf->b_ind_close_extra = 0;
6966
6967 /* Spaces from the edge of the line an open brace sitting in the leftmost
6968 * column is imagined to be. */
6969 buf->b_ind_open_left_imag = 0;
6970
6971 /* Spaces jump labels should be shifted to the left if N is non-negative,
6972 * otherwise the jump label will be put to column 1. */
6973 buf->b_ind_jump_label = -1;
6974
6975 /* Spaces from the switch() indent a "case xx" label should be located. */
6976 buf->b_ind_case = sw;
6977
6978 /* Spaces from the "case xx:" code after a switch() should be located. */
6979 buf->b_ind_case_code = sw;
6980
6981 /* Lineup break at end of case in switch() with case label. */
6982 buf->b_ind_case_break = 0;
6983
6984 /* Spaces from the class declaration indent a scope declaration label
6985 * should be located. */
6986 buf->b_ind_scopedecl = sw;
6987
6988 /* Spaces from the scope declaration label code should be located. */
6989 buf->b_ind_scopedecl_code = sw;
6990
6991 /* Amount K&R-style parameters should be indented. */
6992 buf->b_ind_param = sw;
6993
6994 /* Amount a function type spec should be indented. */
6995 buf->b_ind_func_type = sw;
6996
6997 /* Amount a cpp base class declaration or constructor initialization
6998 * should be indented. */
6999 buf->b_ind_cpp_baseclass = sw;
7000
7001 /* additional spaces beyond the prevailing indent a continuation line
7002 * should be located. */
7003 buf->b_ind_continuation = sw;
7004
7005 /* Spaces from the indent of the line with an unclosed parentheses. */
7006 buf->b_ind_unclosed = sw * 2;
7007
7008 /* Spaces from the indent of the line with an unclosed parentheses, which
7009 * itself is also unclosed. */
7010 buf->b_ind_unclosed2 = sw;
7011
7012 /* Suppress ignoring spaces from the indent of a line starting with an
7013 * unclosed parentheses. */
7014 buf->b_ind_unclosed_noignore = 0;
7015
7016 /* If the opening paren is the last nonwhite character on the line, and
7017 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7018 * context (for very long lines). */
7019 buf->b_ind_unclosed_wrapped = 0;
7020
7021 /* Suppress ignoring white space when lining up with the character after
7022 * an unclosed parentheses. */
7023 buf->b_ind_unclosed_whiteok = 0;
7024
7025 /* Indent a closing parentheses under the line start of the matching
7026 * opening parentheses. */
7027 buf->b_ind_matching_paren = 0;
7028
7029 /* Indent a closing parentheses under the previous line. */
7030 buf->b_ind_paren_prev = 0;
7031
7032 /* Extra indent for comments. */
7033 buf->b_ind_comment = 0;
7034
7035 /* Spaces from the comment opener when there is nothing after it. */
7036 buf->b_ind_in_comment = 3;
7037
7038 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7039 * after the comment opener. */
7040 buf->b_ind_in_comment2 = 0;
7041
7042 /* Max lines to search for an open paren. */
7043 buf->b_ind_maxparen = 20;
7044
7045 /* Max lines to search for an open comment. */
7046 buf->b_ind_maxcomment = 70;
7047
7048 /* Handle braces for java code. */
7049 buf->b_ind_java = 0;
7050
7051 /* Not to confuse JS object properties with labels. */
7052 buf->b_ind_js = 0;
7053
7054 /* Handle blocked cases correctly. */
7055 buf->b_ind_keep_case_label = 0;
7056
7057 /* Handle C++ namespace. */
7058 buf->b_ind_cpp_namespace = 0;
7059
7060 /* Handle continuation lines containing conditions of if(), for() and
7061 * while(). */
7062 buf->b_ind_if_for_while = 0;
7063
Bram Moolenaar6b643942017-03-05 19:44:06 +01007064 /* indentation for # comments */
7065 buf->b_ind_hash_comment = 0;
7066
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007067 /* Handle C++ extern "C" or "C++" */
7068 buf->b_ind_cpp_extern_c = 0;
7069
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007070 for (p = buf->b_p_cino; *p; )
7071 {
7072 l = p++;
7073 if (*p == '-')
7074 ++p;
7075 digits = p; /* remember where the digits start */
7076 n = getdigits(&p);
7077 divider = 0;
7078 if (*p == '.') /* ".5s" means a fraction */
7079 {
7080 fraction = atol((char *)++p);
7081 while (VIM_ISDIGIT(*p))
7082 {
7083 ++p;
7084 if (divider)
7085 divider *= 10;
7086 else
7087 divider = 10;
7088 }
7089 }
7090 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7091 {
7092 if (p == digits)
7093 n = sw; /* just "s" is one 'shiftwidth' */
7094 else
7095 {
7096 n *= sw;
7097 if (divider)
7098 n += (sw * fraction + divider / 2) / divider;
7099 }
7100 ++p;
7101 }
7102 if (l[1] == '-')
7103 n = -n;
7104
7105 /* When adding an entry here, also update the default 'cinoptions' in
7106 * doc/indent.txt, and add explanation for it! */
7107 switch (*l)
7108 {
7109 case '>': buf->b_ind_level = n; break;
7110 case 'e': buf->b_ind_open_imag = n; break;
7111 case 'n': buf->b_ind_no_brace = n; break;
7112 case 'f': buf->b_ind_first_open = n; break;
7113 case '{': buf->b_ind_open_extra = n; break;
7114 case '}': buf->b_ind_close_extra = n; break;
7115 case '^': buf->b_ind_open_left_imag = n; break;
7116 case 'L': buf->b_ind_jump_label = n; break;
7117 case ':': buf->b_ind_case = n; break;
7118 case '=': buf->b_ind_case_code = n; break;
7119 case 'b': buf->b_ind_case_break = n; break;
7120 case 'p': buf->b_ind_param = n; break;
7121 case 't': buf->b_ind_func_type = n; break;
7122 case '/': buf->b_ind_comment = n; break;
7123 case 'c': buf->b_ind_in_comment = n; break;
7124 case 'C': buf->b_ind_in_comment2 = n; break;
7125 case 'i': buf->b_ind_cpp_baseclass = n; break;
7126 case '+': buf->b_ind_continuation = n; break;
7127 case '(': buf->b_ind_unclosed = n; break;
7128 case 'u': buf->b_ind_unclosed2 = n; break;
7129 case 'U': buf->b_ind_unclosed_noignore = n; break;
7130 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7131 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7132 case 'm': buf->b_ind_matching_paren = n; break;
7133 case 'M': buf->b_ind_paren_prev = n; break;
7134 case ')': buf->b_ind_maxparen = n; break;
7135 case '*': buf->b_ind_maxcomment = n; break;
7136 case 'g': buf->b_ind_scopedecl = n; break;
7137 case 'h': buf->b_ind_scopedecl_code = n; break;
7138 case 'j': buf->b_ind_java = n; break;
7139 case 'J': buf->b_ind_js = n; break;
7140 case 'l': buf->b_ind_keep_case_label = n; break;
7141 case '#': buf->b_ind_hash_comment = n; break;
7142 case 'N': buf->b_ind_cpp_namespace = n; break;
7143 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007144 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007145 }
7146 if (*p == ',')
7147 ++p;
7148 }
7149}
7150
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007151/*
7152 * Return the desired indent for C code.
7153 * Return -1 if the indent should be left alone (inside a raw string).
7154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007156get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 pos_T cur_curpos;
7159 int amount;
7160 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007161 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 colnr_T col;
7163 char_u *theline;
7164 char_u *linecopy;
7165 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007166 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007168 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 pos_T our_paren_pos;
7170 char_u *start;
7171 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007172#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173#define BRACE_AT_START 2 /* '{' is at start of line */
7174#define BRACE_AT_END 3 /* '{' is at end of line */
7175 linenr_T ourscope;
7176 char_u *l;
7177 char_u *look;
7178 char_u terminated;
7179 int lookfor;
7180#define LOOKFOR_INITIAL 0
7181#define LOOKFOR_IF 1
7182#define LOOKFOR_DO 2
7183#define LOOKFOR_CASE 3
7184#define LOOKFOR_ANY 4
7185#define LOOKFOR_TERM 5
7186#define LOOKFOR_UNTERM 6
7187#define LOOKFOR_SCOPEDECL 7
7188#define LOOKFOR_NOBREAK 8
7189#define LOOKFOR_CPP_BASECLASS 9
7190#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007191#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007192#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
7194 int whilelevel;
7195 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 int n;
7197 int iscase;
7198 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007199 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007201 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007202 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007203 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007204 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007205 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007207 /* make a copy, value is changed below */
7208 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
7210 /* remember where the cursor was when we started */
7211 cur_curpos = curwin->w_cursor;
7212
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007213 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007214 if (cur_curpos.lnum == 1)
7215 return 0;
7216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 /* Get a copy of the current contents of the line.
7218 * This is required, because only the most recent line obtained with
7219 * ml_get is valid! */
7220 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7221 if (linecopy == NULL)
7222 return 0;
7223
7224 /*
7225 * In insert mode and the cursor is on a ')' truncate the line at the
7226 * cursor position. We don't want to line up with the matching '(' when
7227 * inserting new stuff.
7228 * For unknown reasons the cursor might be past the end of the line, thus
7229 * check for that.
7230 */
7231 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007232 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 && linecopy[curwin->w_cursor.col] == ')')
7234 linecopy[curwin->w_cursor.col] = NUL;
7235
7236 theline = skipwhite(linecopy);
7237
7238 /* move the cursor to the start of the line */
7239
7240 curwin->w_cursor.col = 0;
7241
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007242 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007245 * If we are inside a raw string don't change the indent.
7246 * Ignore a raw string inside a comment.
7247 */
7248 comment_pos = ind_find_start_comment();
7249 if (comment_pos != NULL)
7250 {
7251 /* findmatchlimit() static pos is overwritten, make a copy */
7252 tryposCopy = *comment_pos;
7253 comment_pos = &tryposCopy;
7254 }
7255 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007256 if (trypos != NULL && (comment_pos == NULL
7257 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007258 {
7259 amount = -1;
7260 goto laterend;
7261 }
7262
7263 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 * #defines and so on always go at the left when included in 'cinkeys'.
7265 */
7266 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007267 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007268 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007269 goto theend;
7270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
7272 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007273 * Is it a non-case label? Then that goes at the left margin too unless:
7274 * - JS flag is set.
7275 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007277 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007278 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {
7280 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007281 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 }
7283
7284 /*
7285 * If we're inside a "//" comment and there is a "//" comment in a
7286 * previous line, lineup with that one.
7287 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007288 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 && (trypos = find_line_comment()) != NULL) /* XXX */
7290 {
7291 /* find how indented the line beginning the comment is */
7292 getvcol(curwin, trypos, &col, NULL, NULL);
7293 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007294 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 }
7296
7297 /*
7298 * If we're inside a comment and not looking at the start of the
7299 * comment, try using the 'comments' option.
7300 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007301 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 int lead_start_len = 2;
7304 int lead_middle_len = 1;
7305 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7306 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7307 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7308 char_u *p;
7309 int start_align = 0;
7310 int start_off = 0;
7311 int done = FALSE;
7312
7313 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007314 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007316 *lead_start = NUL;
7317 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
7319 p = curbuf->b_p_com;
7320 while (*p != NUL)
7321 {
7322 int align = 0;
7323 int off = 0;
7324 int what = 0;
7325
7326 while (*p != NUL && *p != ':')
7327 {
7328 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7329 what = *p++;
7330 else if (*p == COM_LEFT || *p == COM_RIGHT)
7331 align = *p++;
7332 else if (VIM_ISDIGIT(*p) || *p == '-')
7333 off = getdigits(&p);
7334 else
7335 ++p;
7336 }
7337
7338 if (*p == ':')
7339 ++p;
7340 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7341 if (what == COM_START)
7342 {
7343 STRCPY(lead_start, lead_end);
7344 lead_start_len = (int)STRLEN(lead_start);
7345 start_off = off;
7346 start_align = align;
7347 }
7348 else if (what == COM_MIDDLE)
7349 {
7350 STRCPY(lead_middle, lead_end);
7351 lead_middle_len = (int)STRLEN(lead_middle);
7352 }
7353 else if (what == COM_END)
7354 {
7355 /* If our line starts with the middle comment string, line it
7356 * up with the comment opener per the 'comments' option. */
7357 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7358 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7359 {
7360 done = TRUE;
7361 if (curwin->w_cursor.lnum > 1)
7362 {
7363 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007364 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 * the middle comment string matches in the previous
7366 * line, use the indent of that line. XXX */
7367 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7368 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7369 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7370 else if (STRNCMP(look, lead_middle,
7371 lead_middle_len) == 0)
7372 {
7373 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7374 break;
7375 }
7376 /* If the start comment string doesn't match with the
7377 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007378 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 lead_start, lead_start_len) != 0)
7380 continue;
7381 }
7382 if (start_off != 0)
7383 amount += start_off;
7384 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007385 amount += vim_strsize(lead_start)
7386 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 break;
7388 }
7389
7390 /* If our line starts with the end comment string, line it up
7391 * with the middle comment */
7392 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7393 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7394 {
7395 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7396 /* XXX */
7397 if (off != 0)
7398 amount += off;
7399 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007400 amount += vim_strsize(lead_start)
7401 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 done = TRUE;
7403 break;
7404 }
7405 }
7406 }
7407
7408 /* If our line starts with an asterisk, line up with the
7409 * asterisk in the comment opener; otherwise, line up
7410 * with the first character of the comment text.
7411 */
7412 if (done)
7413 ;
7414 else if (theline[0] == '*')
7415 amount += 1;
7416 else
7417 {
7418 /*
7419 * If we are more than one line away from the comment opener, take
7420 * the indent of the previous non-empty line. If 'cino' has "CO"
7421 * and we are just below the comment opener and there are any
7422 * white characters after it line up with the text after it;
7423 * otherwise, add the amount specified by "c" in 'cino'
7424 */
7425 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007426 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 {
7428 if (linewhite(lnum)) /* skip blank lines */
7429 continue;
7430 amount = get_indent_lnum(lnum); /* XXX */
7431 break;
7432 }
7433 if (amount == -1) /* use the comment opener */
7434 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007435 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007437 start = ml_get(comment_pos->lnum);
7438 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007440 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007442 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007444 if (curbuf->b_ind_in_comment2 || *look == NUL)
7445 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 }
7447 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007448 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
7450
7451 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007452 * Are we looking at a ']' that has a match?
7453 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007454 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007455 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7456 {
7457 /* align with the line containing the '['. */
7458 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007459 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007460 }
7461
7462 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 * Are we inside parentheses or braces?
7464 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007465 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007466 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007467 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 || trypos != NULL)
7469 {
7470 if (trypos != NULL && tryposBrace != NULL)
7471 {
7472 /* Both an unmatched '(' and '{' is found. Use the one which is
7473 * closer to the current cursor position, set the other to NULL. */
7474 if (trypos->lnum != tryposBrace->lnum
7475 ? trypos->lnum < tryposBrace->lnum
7476 : trypos->col < tryposBrace->col)
7477 trypos = NULL;
7478 else
7479 tryposBrace = NULL;
7480 }
7481
7482 if (trypos != NULL)
7483 {
7484 /*
7485 * If the matching paren is more than one line away, use the indent of
7486 * a previous non-empty line that matches the same paren.
7487 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007488 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007490 /* Line up with the start of the matching paren line. */
7491 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7492 }
7493 else
7494 {
7495 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007496 our_paren_pos = *trypos;
7497 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007499 l = skipwhite(ml_get(lnum));
7500 if (cin_nocode(l)) /* skip comment lines */
7501 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007502 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007503 continue; /* ignore #define, #if, etc. */
7504 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007506 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007507 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007508 {
7509 lnum = trypos->lnum + 1;
7510 continue;
7511 }
7512
7513 /* XXX */
7514 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007515 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007516 && trypos->lnum == our_paren_pos.lnum
7517 && trypos->col == our_paren_pos.col)
7518 {
7519 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007521 if (theline[0] == ')')
7522 {
7523 if (our_paren_pos.lnum != lnum
7524 && cur_amount > amount)
7525 cur_amount = amount;
7526 amount = -1;
7527 }
7528 break;
7529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 }
7531 }
7532
7533 /*
7534 * Line up with line where the matching paren is. XXX
7535 * If the line starts with a '(' or the indent for unclosed
7536 * parentheses is zero, line up with the unclosed parentheses.
7537 */
7538 if (amount == -1)
7539 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007540 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007541 int is_if_for_while = 0;
7542
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007543 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007544 {
7545 /* Look for the outermost opening parenthesis on this line
7546 * and check whether it belongs to an "if", "for" or "while". */
7547
7548 pos_T cursor_save = curwin->w_cursor;
7549 pos_T outermost;
7550 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007551
7552 trypos = &our_paren_pos;
7553 do {
7554 outermost = *trypos;
7555 curwin->w_cursor.lnum = outermost.lnum;
7556 curwin->w_cursor.col = outermost.col;
7557
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007558 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007559 } while (trypos && trypos->lnum == outermost.lnum);
7560
7561 curwin->w_cursor = cursor_save;
7562
7563 line = ml_get(outermost.lnum);
7564
7565 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007566 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007567 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007568
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007569 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007570 look = skipwhite(look);
7571 if (*look == '(')
7572 {
7573 linenr_T save_lnum = curwin->w_cursor.lnum;
7574 char_u *line;
7575 int look_col;
7576
7577 /* Ignore a '(' in front of the line that has a match before
7578 * our matching '('. */
7579 curwin->w_cursor.lnum = our_paren_pos.lnum;
7580 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007581 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007582 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007583 if ((trypos = findmatchlimit(NULL, ')', 0,
7584 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007585 != NULL
7586 && trypos->lnum == our_paren_pos.lnum
7587 && trypos->col < our_paren_pos.col)
7588 ignore_paren_col = trypos->col + 1;
7589
7590 curwin->w_cursor.lnum = save_lnum;
7591 look = ml_get(our_paren_pos.lnum) + look_col;
7592 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007593 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7594 && is_if_for_while == 0)
7595 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007596 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
7598 /*
7599 * If we're looking at a close paren, line up right there;
7600 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007601 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 * the last nonwhite character of the line, use either the
7603 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007604 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 * lines).
7606 */
7607 if (theline[0] != ')')
7608 {
7609 cur_amount = MAXCOL;
7610 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007611 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 && cin_ends_in(l, (char_u *)"(", NULL))
7613 {
7614 /* look for opening unmatched paren, indent one level
7615 * for each additional level */
7616 n = 1;
7617 for (col = 0; col < our_paren_pos.col; ++col)
7618 {
7619 switch (l[col])
7620 {
7621 case '(':
7622 case '{': ++n;
7623 break;
7624
7625 case ')':
7626 case '}': if (n > 1)
7627 --n;
7628 break;
7629 }
7630 }
7631
7632 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007633 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007635 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 our_paren_pos.col++;
7637 else
7638 {
7639 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007640 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 col++;
7642 if (l[col] != NUL) /* In case of trailing space */
7643 our_paren_pos.col = col;
7644 else
7645 our_paren_pos.col++;
7646 }
7647 }
7648
7649 /*
7650 * Find how indented the paren is, or the character after it
7651 * if we did the above "if".
7652 */
7653 if (our_paren_pos.col > 0)
7654 {
7655 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7656 if (cur_amount > (int)col)
7657 cur_amount = col;
7658 }
7659 }
7660
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007661 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
7663 /* Line up with the start of the matching paren line. */
7664 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007665 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7666 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007667 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {
7669 if (cur_amount != MAXCOL)
7670 amount = cur_amount;
7671 }
7672 else
7673 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007674 /* Add b_ind_unclosed2 for each '(' before our matching one,
7675 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007677 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {
7679 --our_paren_pos.col;
7680 switch (*ml_get_pos(&our_paren_pos))
7681 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007682 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 col = our_paren_pos.col;
7684 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007685 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 col = MAXCOL;
7687 break;
7688 }
7689 }
7690
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007691 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 * braces */
7693 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007694 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 else
7696 {
7697 curwin->w_cursor.lnum = our_paren_pos.lnum;
7698 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007699 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7700 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007701 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007703 {
7704 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007705 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007706 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007707 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710 /*
7711 * For a line starting with ')' use the minimum of the two
7712 * positions, to avoid giving it more indent than the previous
7713 * lines:
7714 * func_long_name( if (x
7715 * arg && yy
7716 * ) ^ not here ) ^ not here
7717 */
7718 if (cur_amount < amount)
7719 amount = cur_amount;
7720 }
7721 }
7722
7723 /* add extra indent for a comment */
7724 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007725 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 else
7728 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007729 /*
7730 * We are inside braces, there is a { before this line at the position
7731 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007732 * Make a copy of tryposBrace, it may point to pos_copy inside
7733 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007734 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007735 tryposCopy = *tryposBrace;
7736 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 ourscope = trypos->lnum;
7739 start = ml_get(ourscope);
7740
7741 /*
7742 * Now figure out how indented the line is in general.
7743 * If the brace was at the start of the line, we use that;
7744 * otherwise, check out the indentation of the line as
7745 * a whole and then add the "imaginary indent" to that.
7746 */
7747 look = skipwhite(start);
7748 if (*look == '{')
7749 {
7750 getvcol(curwin, trypos, &col, NULL, NULL);
7751 amount = col;
7752 if (*start == '{')
7753 start_brace = BRACE_IN_COL0;
7754 else
7755 start_brace = BRACE_AT_START;
7756 }
7757 else
7758 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007759 /* That opening brace might have been on a continuation
7760 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 curwin->w_cursor.lnum = ourscope;
7762
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007763 /* Position the cursor over the rightmost paren, so that
7764 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 lnum = ourscope;
7766 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007767 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7768 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 lnum = trypos->lnum;
7770
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007771 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 * case 1: if (asdf &&
7773 * ldfd) {
7774 * }
7775 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007776 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7777 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007779 else if (curbuf->b_ind_js)
7780 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007782 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783
7784 start_brace = BRACE_AT_END;
7785 }
7786
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007787 /* For Javascript check if the line starts with "key:". */
7788 if (curbuf->b_ind_js)
7789 js_cur_has_key = cin_has_js_key(theline);
7790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007792 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 * we want to be. otherwise, add the amount of room
7794 * that an indent is supposed to be.
7795 */
7796 if (theline[0] == '}')
7797 {
7798 /*
7799 * they may want closing braces to line up with something
7800 * other than the open brace. indulge them, if so.
7801 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007802 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804 else
7805 {
7806 /*
7807 * If we're looking at an "else", try to find an "if"
7808 * to match it with.
7809 * If we're looking at a "while", try to find a "do"
7810 * to match it with.
7811 */
7812 lookfor = LOOKFOR_INITIAL;
7813 if (cin_iselse(theline))
7814 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007815 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 lookfor = LOOKFOR_DO;
7817 if (lookfor != LOOKFOR_INITIAL)
7818 {
7819 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007820 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
7822 amount = get_indent(); /* XXX */
7823 goto theend;
7824 }
7825 }
7826
7827 /*
7828 * We get here if we are not on an "while-of-do" or "else" (or
7829 * failed to find a matching "if").
7830 * Search backwards for something to line up with.
7831 * First set amount for when we don't find anything.
7832 */
7833
7834 /*
7835 * if the '{' is _really_ at the left margin, use the imaginary
7836 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007837 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 */
7839
7840 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7841 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007842 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007843 lookfor_cpp_namespace = TRUE;
7844 }
7845 else if (start_brace == BRACE_AT_START &&
7846 lookfor_cpp_namespace) /* '{' is at start */
7847 {
7848
7849 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 }
7851 else
7852 {
7853 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007854 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007855 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007856
7857 l = skipwhite(ml_get_curline());
7858 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007859 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007860 else if (cin_is_cpp_extern_c(l))
7861 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 else
7864 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007865 /* Compensate for adding b_ind_open_extra later. */
7866 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 if (amount < 0)
7868 amount = 0;
7869 }
7870 }
7871
7872 lookfor_break = FALSE;
7873
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007874 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {
7876 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007877 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 }
7879 else if (cin_isscopedecl(theline)) /* private:, ... */
7880 {
7881 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007882 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 }
7884 else
7885 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007886 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7887 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 lookfor_break = TRUE;
7889
7890 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007891 /* b_ind_level from start of block */
7892 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894 scope_amount = amount;
7895 whilelevel = 0;
7896
7897 /*
7898 * Search backwards. If we find something we recognize, line up
7899 * with that.
7900 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007901 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 * the usual amount relative to the conditional
7903 * that opens the block.
7904 */
7905 curwin->w_cursor = cur_curpos;
7906 for (;;)
7907 {
7908 curwin->w_cursor.lnum--;
7909 curwin->w_cursor.col = 0;
7910
7911 /*
7912 * If we went all the way back to the start of our scope, line
7913 * up with it.
7914 */
7915 if (curwin->w_cursor.lnum <= ourscope)
7916 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007917 /* We reached end of scope:
7918 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007920 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 * don't add ind_continuation, otherwise it is a variable
7922 * declaration:
7923 * int x,
7924 * here; <-- add ind_continuation
7925 */
7926 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7927 {
7928 if (curwin->w_cursor.lnum == 0
7929 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007930 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007932 /* nothing found (abuse curbuf->b_ind_maxparen as
7933 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 * initialization) */
7935 if (cont_amount > 0)
7936 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007937 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 amount += ind_continuation;
7939 break;
7940 }
7941
7942 l = ml_get_curline();
7943
7944 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007945 * If we're in a comment or raw string now, skip to
7946 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007948 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 if (trypos != NULL)
7950 {
7951 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007952 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 continue;
7954 }
7955
7956 /*
7957 * Skip preprocessor directives and blank lines.
7958 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007959 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7960 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 continue;
7962
7963 if (cin_nocode(l))
7964 continue;
7965
7966 terminated = cin_isterminated(l, FALSE, TRUE);
7967
7968 /*
7969 * If we are at top level and the line looks like a
7970 * function declaration, we are done
7971 * (it's a variable declaration).
7972 */
7973 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007974 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
7976 /* if the line is terminated with another ','
7977 * it is a continued variable initialization.
7978 * don't add extra indent.
7979 * TODO: does not work, if a function
7980 * declaration is split over multiple lines:
7981 * cin_isfuncdecl returns FALSE then.
7982 */
7983 if (terminated == ',')
7984 break;
7985
7986 /* if it es a enum declaration or an assignment,
7987 * we are done.
7988 */
7989 if (terminated != ';' && cin_isinit())
7990 break;
7991
7992 /* nothing useful found */
7993 if (terminated == 0 || terminated == '{')
7994 continue;
7995 }
7996
7997 if (terminated != ';')
7998 {
7999 /* Skip parens and braces. Position the cursor
8000 * over the rightmost paren, so that matching it
8001 * will take us back to the start of the line.
8002 */ /* XXX */
8003 trypos = NULL;
8004 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008005 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008006 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007
8008 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008009 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
8011 if (trypos != NULL)
8012 {
8013 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008014 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 continue;
8016 }
8017 }
8018
8019 /* it's a variable declaration, add indentation
8020 * like in
8021 * int a,
8022 * b;
8023 */
8024 if (cont_amount > 0)
8025 amount = cont_amount;
8026 else
8027 amount += ind_continuation;
8028 }
8029 else if (lookfor == LOOKFOR_UNTERM)
8030 {
8031 if (cont_amount > 0)
8032 amount = cont_amount;
8033 else
8034 amount += ind_continuation;
8035 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008036 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008037 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008038 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008039 && lookfor != LOOKFOR_CPP_BASECLASS
8040 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008041 {
8042 amount = scope_amount;
8043 if (theline[0] == '{')
8044 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008045 amount += curbuf->b_ind_open_extra;
8046 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008047 }
8048 }
8049
8050 if (lookfor_cpp_namespace)
8051 {
8052 /*
8053 * Looking for C++ namespace, need to look further
8054 * back.
8055 */
8056 if (curwin->w_cursor.lnum == ourscope)
8057 continue;
8058
8059 if (curwin->w_cursor.lnum == 0
8060 || curwin->w_cursor.lnum
8061 < ourscope - FIND_NAMESPACE_LIM)
8062 break;
8063
8064 l = ml_get_curline();
8065
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008066 /* If we're in a comment or raw string now, skip
8067 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008068 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008069 if (trypos != NULL)
8070 {
8071 curwin->w_cursor.lnum = trypos->lnum + 1;
8072 curwin->w_cursor.col = 0;
8073 continue;
8074 }
8075
8076 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008077 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8078 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008079 continue;
8080
8081 /* Finally the actual check for "namespace". */
8082 if (cin_is_cpp_namespace(l))
8083 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008084 amount += curbuf->b_ind_cpp_namespace
8085 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008086 break;
8087 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008088 else if (cin_is_cpp_extern_c(l))
8089 {
8090 amount += curbuf->b_ind_cpp_extern_c
8091 - added_to_amount;
8092 break;
8093 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008094
8095 if (cin_nocode(l))
8096 continue;
8097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 }
8099 break;
8100 }
8101
8102 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008103 * If we're in a comment or raw string now, skip to the start
8104 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008106 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
8108 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008109 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 continue;
8111 }
8112
8113 l = ml_get_curline();
8114
8115 /*
8116 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008117 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008119 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 if (iscase || cin_isscopedecl(l))
8121 {
8122 /* we are only looking for cpp base class
8123 * declaration/initialization any longer */
8124 if (lookfor == LOOKFOR_CPP_BASECLASS)
8125 break;
8126
8127 /* When looking for a "do" we are not interested in
8128 * labels. */
8129 if (whilelevel > 0)
8130 continue;
8131
8132 /*
8133 * case xx:
8134 * c = 99 + <- this indent plus continuation
8135 *-> here;
8136 */
8137 if (lookfor == LOOKFOR_UNTERM
8138 || lookfor == LOOKFOR_ENUM_OR_INIT)
8139 {
8140 if (cont_amount > 0)
8141 amount = cont_amount;
8142 else
8143 amount += ind_continuation;
8144 break;
8145 }
8146
8147 /*
8148 * case xx: <- line up with this case
8149 * x = 333;
8150 * case yy:
8151 */
8152 if ( (iscase && lookfor == LOOKFOR_CASE)
8153 || (iscase && lookfor_break)
8154 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8155 {
8156 /*
8157 * Check that this case label is not for another
8158 * switch()
8159 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008160 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008161 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
8163 amount = get_indent(); /* XXX */
8164 break;
8165 }
8166 continue;
8167 }
8168
8169 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8170
8171 /*
8172 * case xx: if (cond) <- line up with this if
8173 * y = y + 1;
8174 * -> s = 99;
8175 *
8176 * case xx:
8177 * if (cond) <- line up with this line
8178 * y = y + 1;
8179 * -> s = 99;
8180 */
8181 if (lookfor == LOOKFOR_TERM)
8182 {
8183 if (n)
8184 amount = n;
8185
8186 if (!lookfor_break)
8187 break;
8188 }
8189
8190 /*
8191 * case xx: x = x + 1; <- line up with this x
8192 * -> y = y + 1;
8193 *
8194 * case xx: if (cond) <- line up with this if
8195 * -> y = y + 1;
8196 */
8197 if (n)
8198 {
8199 amount = n;
8200 l = after_label(ml_get_curline());
8201 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008202 {
8203 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008204 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008205 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008206 amount += curbuf->b_ind_level
8207 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 break;
8210 }
8211
8212 /*
8213 * Try to get the indent of a statement before the switch
8214 * label. If nothing is found, line up relative to the
8215 * switch label.
8216 * break; <- may line up with this line
8217 * case xx:
8218 * -> y = 1;
8219 */
8220 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008221 ? curbuf->b_ind_case_code
8222 : curbuf->b_ind_scopedecl_code);
8223 lookfor = curbuf->b_ind_case_break
8224 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 continue;
8226 }
8227
8228 /*
8229 * Looking for a switch() label or C++ scope declaration,
8230 * ignore other lines, skip {}-blocks.
8231 */
8232 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8233 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008234 if (find_last_paren(l, '{', '}')
8235 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008238 curwin->w_cursor.col = 0;
8239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 continue;
8241 }
8242
8243 /*
8244 * Ignore jump labels with nothing after them.
8245 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008246 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
8248 l = after_label(ml_get_curline());
8249 if (l == NULL || cin_nocode(l))
8250 continue;
8251 }
8252
8253 /*
8254 * Ignore #defines, #if, etc.
8255 * Ignore comment and empty lines.
8256 * (need to get the line again, cin_islabel() may have
8257 * unlocked it)
8258 */
8259 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008260 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 || cin_nocode(l))
8262 continue;
8263
8264 /*
8265 * Are we at the start of a cpp base class declaration or
8266 * constructor initialization?
8267 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008268 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008269 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008270 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008271 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008272 l = ml_get_curline();
8273 }
8274 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
8276 if (lookfor == LOOKFOR_UNTERM)
8277 {
8278 if (cont_amount > 0)
8279 amount = cont_amount;
8280 else
8281 amount += ind_continuation;
8282 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008283 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008285 /* Need to find start of the declaration. */
8286 lookfor = LOOKFOR_UNTERM;
8287 ind_continuation = 0;
8288 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 }
8290 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008291 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008292 amount = get_baseclass_amount(
8293 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 break;
8295 }
8296 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8297 {
8298 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008299 * declaration or initialization before the opening brace.
8300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 if (cin_isterminated(l, TRUE, FALSE))
8302 break;
8303 else
8304 continue;
8305 }
8306
8307 /*
8308 * What happens next depends on the line being terminated.
8309 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008310 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 * 123,
8312 * sizeof
8313 * here
8314 * Otherwise check whether it is a enumeration or structure
8315 * initialisation (not indented) or a variable declaration
8316 * (indented).
8317 */
8318 terminated = cin_isterminated(l, FALSE, TRUE);
8319
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008320 if (js_cur_has_key)
8321 {
8322 js_cur_has_key = 0; /* only check the first line */
8323 if (curbuf->b_ind_js && terminated == ',')
8324 {
8325 /* For Javascript we might be inside an object:
8326 * key: something, <- align with this
8327 * key: something
8328 * or:
8329 * key: something + <- align with this
8330 * something,
8331 * key: something
8332 */
8333 lookfor = LOOKFOR_JS_KEY;
8334 }
8335 }
8336 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8337 {
8338 amount = get_indent();
8339 break;
8340 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008341 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008342 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008343 if (tryposBrace != NULL && tryposBrace->lnum
8344 >= curwin->w_cursor.lnum)
8345 break;
8346 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008347 /* line below current line is the one that starts a
8348 * (possibly broken) line ending in a comma. */
8349 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008350 else
8351 {
8352 amount = get_indent();
8353 if (curwin->w_cursor.lnum - 1 == ourscope)
8354 /* line above is start of the scope, thus current
8355 * line is the one that stars a (possibly broken)
8356 * line ending in a comma. */
8357 break;
8358 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008359 }
8360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8362 && terminated == ','))
8363 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008364 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8365 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008366 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 /*
8368 * if we're in the middle of a paren thing,
8369 * go back to the line that starts it so
8370 * we can get the right prevailing indent
8371 * if ( foo &&
8372 * bar )
8373 */
8374 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008375 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008377 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 */
8379 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008380 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008381 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8382 || (trypos->lnum == tryposBrace->lnum
8383 && trypos->col < tryposBrace->col)))
8384 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385
8386 /*
8387 * If we are looking for ',', we also look for matching
8388 * braces.
8389 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008390 if (trypos == NULL && terminated == ','
8391 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008392 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
8394 if (trypos != NULL)
8395 {
8396 /*
8397 * Check if we are on a case label now. This is
8398 * handled above.
8399 * case xx: if ( asdf &&
8400 * asdf)
8401 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008402 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008404 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {
8406 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008407 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 continue;
8409 }
8410 }
8411
8412 /*
8413 * Skip over continuation lines to find the one to get the
8414 * indent from
8415 * char *usethis = "bla\
8416 * bla",
8417 * here;
8418 */
8419 if (terminated == ',')
8420 {
8421 while (curwin->w_cursor.lnum > 1)
8422 {
8423 l = ml_get(curwin->w_cursor.lnum - 1);
8424 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8425 break;
8426 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008427 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429 }
8430
8431 /*
8432 * Get indent and pointer to text for current line,
8433 * ignoring any jump label. XXX
8434 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008435 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008436 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008437 else
8438 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 /*
8440 * If this is just above the line we are indenting, and it
8441 * starts with a '{', line it up with this line.
8442 * while (not)
8443 * -> {
8444 * }
8445 */
8446 if (terminated != ',' && lookfor != LOOKFOR_TERM
8447 && theline[0] == '{')
8448 {
8449 amount = cur_amount;
8450 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008451 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 * doesn't start with a '{', which must have a match
8453 * in the same line (scope is the same). Probably:
8454 * { 1, 2 },
8455 * -> { 3, 4 }
8456 */
8457 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008458 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008460 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {
8462 /* have to look back, whether it is a cpp base
8463 * class declaration or initialization */
8464 lookfor = LOOKFOR_CPP_BASECLASS;
8465 continue;
8466 }
8467 break;
8468 }
8469
8470 /*
8471 * Check if we are after an "if", "while", etc.
8472 * Also allow " } else".
8473 */
8474 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8475 {
8476 /*
8477 * Found an unterminated line after an if (), line up
8478 * with the last one.
8479 * if (cond)
8480 * 100 +
8481 * -> here;
8482 */
8483 if (lookfor == LOOKFOR_UNTERM
8484 || lookfor == LOOKFOR_ENUM_OR_INIT)
8485 {
8486 if (cont_amount > 0)
8487 amount = cont_amount;
8488 else
8489 amount += ind_continuation;
8490 break;
8491 }
8492
8493 /*
8494 * If this is just above the line we are indenting, we
8495 * are finished.
8496 * while (not)
8497 * -> here;
8498 * Otherwise this indent can be used when the line
8499 * before this is terminated.
8500 * yyy;
8501 * if (stat)
8502 * while (not)
8503 * xxx;
8504 * -> here;
8505 */
8506 amount = cur_amount;
8507 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008508 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 if (lookfor != LOOKFOR_TERM)
8510 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008511 amount += curbuf->b_ind_level
8512 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 break;
8514 }
8515
8516 /*
8517 * Special trick: when expecting the while () after a
8518 * do, line up with the while()
8519 * do
8520 * x = 1;
8521 * -> here
8522 */
8523 l = skipwhite(ml_get_curline());
8524 if (cin_isdo(l))
8525 {
8526 if (whilelevel == 0)
8527 break;
8528 --whilelevel;
8529 }
8530
8531 /*
8532 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008533 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 * Need to use the scope of this "else". XXX
8535 * If whilelevel != 0 continue looking for a "do {".
8536 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008537 if (cin_iselse(l) && whilelevel == 0)
8538 {
8539 /* If we're looking at "} else", let's make sure we
8540 * find the opening brace of the enclosing scope,
8541 * not the one from "if () {". */
8542 if (*l == '}')
8543 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008544 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008545
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008546 if ((trypos = find_start_brace()) == NULL
8547 || find_match(LOOKFOR_IF, trypos->lnum)
8548 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008549 break;
8550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
8552
8553 /*
8554 * If we're below an unterminated line that is not an
8555 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008556 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 * the line before this one.
8558 */
8559 else
8560 {
8561 /*
8562 * Found two unterminated lines on a row, line up with
8563 * the last one.
8564 * c = 99 +
8565 * 100 +
8566 * -> here;
8567 */
8568 if (lookfor == LOOKFOR_UNTERM)
8569 {
8570 /* When line ends in a comma add extra indent */
8571 if (terminated == ',')
8572 amount += ind_continuation;
8573 break;
8574 }
8575
8576 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8577 {
8578 /* Found two lines ending in ',', lineup with the
8579 * lowest one, but check for cpp base class
8580 * declaration/initialization, if it is an
8581 * opening brace or we are looking just for
8582 * enumerations/initializations. */
8583 if (terminated == ',')
8584 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008585 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 break;
8587
8588 lookfor = LOOKFOR_CPP_BASECLASS;
8589 continue;
8590 }
8591
8592 /* Ignore unterminated lines in between, but
8593 * reduce indent. */
8594 if (amount > cur_amount)
8595 amount = cur_amount;
8596 }
8597 else
8598 {
8599 /*
8600 * Found first unterminated line on a row, may
8601 * line up with this line, remember its indent
8602 * 100 +
8603 * -> here;
8604 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008605 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008607
8608 n = (int)STRLEN(l);
8609 if (terminated == ',' && (*skipwhite(l) == ']'
8610 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
8613 /*
8614 * If previous line ends in ',', check whether we
8615 * are in an initialization or enum
8616 * struct xxx =
8617 * {
8618 * sizeof a,
8619 * 124 };
8620 * or a normal possible continuation line.
8621 * but only, of no other statement has been found
8622 * yet.
8623 */
8624 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8625 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008626 if (curbuf->b_ind_js)
8627 {
8628 /* Search for a line ending in a comma
8629 * and line up with the line below it
8630 * (could be the current line).
8631 * some = [
8632 * 1, <- line up here
8633 * 2,
8634 * some = [
8635 * 3 + <- line up here
8636 * 4 *
8637 * 5,
8638 * 6,
8639 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008640 if (cin_iscomment(skipwhite(l)))
8641 break;
8642 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008643 trypos = find_match_char('[',
8644 curbuf->b_ind_maxparen);
8645 if (trypos != NULL)
8646 {
8647 if (trypos->lnum
8648 == curwin->w_cursor.lnum - 1)
8649 {
8650 /* Current line is first inside
8651 * [], line up with it. */
8652 break;
8653 }
8654 ourscope = trypos->lnum;
8655 }
8656 }
8657 else
8658 {
8659 lookfor = LOOKFOR_ENUM_OR_INIT;
8660 cont_amount = cin_first_id_amount();
8661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 }
8663 else
8664 {
8665 if (lookfor == LOOKFOR_INITIAL
8666 && *l != NUL
8667 && l[STRLEN(l) - 1] == '\\')
8668 /* XXX */
8669 cont_amount = cin_get_equal_amount(
8670 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008671 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008672 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008673 && lookfor != LOOKFOR_COMMA
8674 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 lookfor = LOOKFOR_UNTERM;
8676 }
8677 }
8678 }
8679 }
8680
8681 /*
8682 * Check if we are after a while (cond);
8683 * If so: Ignore until the matching "do".
8684 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008685 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {
8687 /*
8688 * Found an unterminated line after a while ();, line up
8689 * with the last one.
8690 * while (cond);
8691 * 100 + <- line up with this one
8692 * -> here;
8693 */
8694 if (lookfor == LOOKFOR_UNTERM
8695 || lookfor == LOOKFOR_ENUM_OR_INIT)
8696 {
8697 if (cont_amount > 0)
8698 amount = cont_amount;
8699 else
8700 amount += ind_continuation;
8701 break;
8702 }
8703
8704 if (whilelevel == 0)
8705 {
8706 lookfor = LOOKFOR_TERM;
8707 amount = get_indent(); /* XXX */
8708 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008709 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 }
8711 ++whilelevel;
8712 }
8713
8714 /*
8715 * We are after a "normal" statement.
8716 * If we had another statement we can stop now and use the
8717 * indent of that other statement.
8718 * Otherwise the indent of the current statement may be used,
8719 * search backwards for the next "normal" statement.
8720 */
8721 else
8722 {
8723 /*
8724 * Skip single break line, if before a switch label. It
8725 * may be lined up with the case label.
8726 */
8727 if (lookfor == LOOKFOR_NOBREAK
8728 && cin_isbreak(skipwhite(ml_get_curline())))
8729 {
8730 lookfor = LOOKFOR_ANY;
8731 continue;
8732 }
8733
8734 /*
8735 * Handle "do {" line.
8736 */
8737 if (whilelevel > 0)
8738 {
8739 l = cin_skipcomment(ml_get_curline());
8740 if (cin_isdo(l))
8741 {
8742 amount = get_indent(); /* XXX */
8743 --whilelevel;
8744 continue;
8745 }
8746 }
8747
8748 /*
8749 * Found a terminated line above an unterminated line. Add
8750 * the amount for a continuation line.
8751 * x = 1;
8752 * y = foo +
8753 * -> here;
8754 * or
8755 * int x = 1;
8756 * int foo,
8757 * -> here;
8758 */
8759 if (lookfor == LOOKFOR_UNTERM
8760 || lookfor == LOOKFOR_ENUM_OR_INIT)
8761 {
8762 if (cont_amount > 0)
8763 amount = cont_amount;
8764 else
8765 amount += ind_continuation;
8766 break;
8767 }
8768
8769 /*
8770 * Found a terminated line above a terminated line or "if"
8771 * etc. line. Use the amount of the line below us.
8772 * x = 1; x = 1;
8773 * if (asdf) y = 2;
8774 * while (asdf) ->here;
8775 * here;
8776 * ->foo;
8777 */
8778 if (lookfor == LOOKFOR_TERM)
8779 {
8780 if (!lookfor_break && whilelevel == 0)
8781 break;
8782 }
8783
8784 /*
8785 * First line above the one we're indenting is terminated.
8786 * To know what needs to be done look further backward for
8787 * a terminated line.
8788 */
8789 else
8790 {
8791 /*
8792 * position the cursor over the rightmost paren, so
8793 * that matching it will take us back to the start of
8794 * the line. Helps for:
8795 * func(asdr,
8796 * asdfasdf);
8797 * here;
8798 */
8799term_again:
8800 l = ml_get_curline();
8801 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008802 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008803 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {
8805 /*
8806 * Check if we are on a case label now. This is
8807 * handled above.
8808 * case xx: if ( asdf &&
8809 * asdf)
8810 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008811 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008813 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {
8815 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008816 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 continue;
8818 }
8819 }
8820
8821 /* When aligning with the case statement, don't align
8822 * with a statement after it.
8823 * case 1: { <-- don't use this { position
8824 * stat;
8825 * }
8826 * case 2:
8827 * stat;
8828 * }
8829 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008830 iscase = (curbuf->b_ind_keep_case_label
8831 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 /*
8834 * Get indent and pointer to text for current line,
8835 * ignoring any jump label.
8836 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008837 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
8839 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008840 amount += curbuf->b_ind_open_extra;
8841 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008842 l = skipwhite(l);
8843 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008844 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8846
8847 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008848 * When a terminated line starts with "else" skip to
8849 * the matching "if":
8850 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008851 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008852 * Need to use the scope of this "else". XXX
8853 * If whilelevel != 0 continue looking for a "do {".
8854 */
8855 if (lookfor == LOOKFOR_TERM
8856 && *l != '}'
8857 && cin_iselse(l)
8858 && whilelevel == 0)
8859 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008860 if ((trypos = find_start_brace()) == NULL
8861 || find_match(LOOKFOR_IF, trypos->lnum)
8862 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008863 break;
8864 continue;
8865 }
8866
8867 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 * If we're at the end of a block, skip to the start of
8869 * that block.
8870 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008871 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008872 if (find_last_paren(l, '{', '}') /* XXX */
8873 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008875 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 /* if not "else {" check for terminated again */
8877 /* but skip block for "} else {" */
8878 l = cin_skipcomment(ml_get_curline());
8879 if (*l == '}' || !cin_iselse(l))
8880 goto term_again;
8881 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008882 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 }
8884 }
8885 }
8886 }
8887 }
8888 }
8889
8890 /* add extra indent for a comment */
8891 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008892 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008893
8894 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008895 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8896 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008897
8898 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008900
8901 /*
8902 * ok -- we're not inside any sort of structure at all!
8903 *
8904 * This means we're at the top level, and everything should
8905 * basically just match where the previous line is, except
8906 * for the lines immediately following a function declaration,
8907 * which are K&R-style parameters and need to be indented.
8908 *
8909 * if our line starts with an open brace, forget about any
8910 * prevailing indent and make sure it looks like the start
8911 * of a function
8912 */
8913
8914 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008916 amount = curbuf->b_ind_first_open;
8917 goto theend;
8918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008920 /*
8921 * If the NEXT line is a function declaration, the current
8922 * line needs to be indented as a function type spec.
8923 * Don't do this if the current line looks like a comment or if the
8924 * current line is terminated, ie. ends in ';', or if the current line
8925 * contains { or }: "void f() {\n if (1)"
8926 */
8927 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8928 && !cin_nocode(theline)
8929 && vim_strchr(theline, '{') == NULL
8930 && vim_strchr(theline, '}') == NULL
8931 && !cin_ends_in(theline, (char_u *)":", NULL)
8932 && !cin_ends_in(theline, (char_u *)",", NULL)
8933 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8934 cur_curpos.lnum + 1)
8935 && !cin_isterminated(theline, FALSE, TRUE))
8936 {
8937 amount = curbuf->b_ind_func_type;
8938 goto theend;
8939 }
8940
8941 /* search backwards until we find something we recognize */
8942 amount = 0;
8943 curwin->w_cursor = cur_curpos;
8944 while (curwin->w_cursor.lnum > 1)
8945 {
8946 curwin->w_cursor.lnum--;
8947 curwin->w_cursor.col = 0;
8948
8949 l = ml_get_curline();
8950
8951 /*
8952 * If we're in a comment or raw string now, skip to the start
8953 * of it.
8954 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008955 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008957 curwin->w_cursor.lnum = trypos->lnum + 1;
8958 curwin->w_cursor.col = 0;
8959 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961
8962 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008963 * Are we at the start of a cpp base class declaration or
8964 * constructor initialization?
8965 */ /* XXX */
8966 n = FALSE;
8967 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008969 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8970 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008972 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008974 /* XXX */
8975 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8976 break;
8977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008979 /*
8980 * Skip preprocessor directives and blank lines.
8981 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008982 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008983 continue;
8984
8985 if (cin_nocode(l))
8986 continue;
8987
8988 /*
8989 * If the previous line ends in ',', use one level of
8990 * indentation:
8991 * int foo,
8992 * bar;
8993 * do this before checking for '}' in case of eg.
8994 * enum foobar
8995 * {
8996 * ...
8997 * } foo,
8998 * bar;
8999 */
9000 n = 0;
9001 if (cin_ends_in(l, (char_u *)",", NULL)
9002 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9003 {
9004 /* take us back to opening paren */
9005 if (find_last_paren(l, '(', ')')
9006 && (trypos = find_match_paren(
9007 curbuf->b_ind_maxparen)) != NULL)
9008 curwin->w_cursor = *trypos;
9009
9010 /* For a line ending in ',' that is a continuation line go
9011 * back to the first line with a backslash:
9012 * char *foo = "bla\
9013 * bla",
9014 * here;
9015 */
9016 while (n == 0 && curwin->w_cursor.lnum > 1)
9017 {
9018 l = ml_get(curwin->w_cursor.lnum - 1);
9019 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9020 break;
9021 --curwin->w_cursor.lnum;
9022 curwin->w_cursor.col = 0;
9023 }
9024
9025 amount = get_indent(); /* XXX */
9026
9027 if (amount == 0)
9028 amount = cin_first_id_amount();
9029 if (amount == 0)
9030 amount = ind_continuation;
9031 break;
9032 }
9033
9034 /*
9035 * If the line looks like a function declaration, and we're
9036 * not in a comment, put it the left margin.
9037 */
9038 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9039 break;
9040 l = ml_get_curline();
9041
9042 /*
9043 * Finding the closing '}' of a previous function. Put
9044 * current line at the left margin. For when 'cino' has "fs".
9045 */
9046 if (*skipwhite(l) == '}')
9047 break;
9048
9049 /* (matching {)
9050 * If the previous line ends on '};' (maybe followed by
9051 * comments) align at column 0. For example:
9052 * char *string_array[] = { "foo",
9053 * / * x * / "b};ar" }; / * foobar * /
9054 */
9055 if (cin_ends_in(l, (char_u *)"};", NULL))
9056 break;
9057
9058 /*
9059 * If the previous line ends on '[' we are probably in an
9060 * array constant:
9061 * something = [
9062 * 234, <- extra indent
9063 */
9064 if (cin_ends_in(l, (char_u *)"[", NULL))
9065 {
9066 amount = get_indent() + ind_continuation;
9067 break;
9068 }
9069
9070 /*
9071 * Find a line only has a semicolon that belongs to a previous
9072 * line ending in '}', e.g. before an #endif. Don't increase
9073 * indent then.
9074 */
9075 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9076 {
9077 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
9079 while (curwin->w_cursor.lnum > 1)
9080 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009081 look = ml_get(--curwin->w_cursor.lnum);
9082 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009083 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009085 }
9086 if (curwin->w_cursor.lnum > 0
9087 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009090 curwin->w_cursor = curpos_save;
9091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009093 /*
9094 * If the PREVIOUS line is a function declaration, the current
9095 * line (and the ones that follow) needs to be indented as
9096 * parameters.
9097 */
9098 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9099 {
9100 amount = curbuf->b_ind_param;
9101 break;
9102 }
9103
9104 /*
9105 * If the previous line ends in ';' and the line before the
9106 * previous line ends in ',' or '\', ident to column zero:
9107 * int foo,
9108 * bar;
9109 * indent_to_0 here;
9110 */
9111 if (cin_ends_in(l, (char_u *)";", NULL))
9112 {
9113 l = ml_get(curwin->w_cursor.lnum - 1);
9114 if (cin_ends_in(l, (char_u *)",", NULL)
9115 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9116 break;
9117 l = ml_get_curline();
9118 }
9119
9120 /*
9121 * Doesn't look like anything interesting -- so just
9122 * use the indent of this line.
9123 *
9124 * Position the cursor over the rightmost paren, so that
9125 * matching it will take us back to the start of the line.
9126 */
9127 find_last_paren(l, '(', ')');
9128
9129 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9130 curwin->w_cursor = *trypos;
9131 amount = get_indent(); /* XXX */
9132 break;
9133 }
9134
9135 /* add extra indent for a comment */
9136 if (cin_iscomment(theline))
9137 amount += curbuf->b_ind_comment;
9138
9139 /* add extra indent if the previous line ended in a backslash:
9140 * "asdfasdf\
9141 * here";
9142 * char *foo = "asdf\
9143 * here";
9144 */
9145 if (cur_curpos.lnum > 1)
9146 {
9147 l = ml_get(cur_curpos.lnum - 1);
9148 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9149 {
9150 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9151 if (cur_amount > 0)
9152 amount = cur_amount;
9153 else if (cur_amount == 0)
9154 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 }
9156 }
9157
9158theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009159 if (amount < 0)
9160 amount = 0;
9161
9162laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 /* put the cursor back where it belongs */
9164 curwin->w_cursor = cur_curpos;
9165
9166 vim_free(linecopy);
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 return amount;
9169}
9170
9171 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009172find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
9174 char_u *look;
9175 pos_T *theirscope;
9176 char_u *mightbeif;
9177 int elselevel;
9178 int whilelevel;
9179
9180 if (lookfor == LOOKFOR_IF)
9181 {
9182 elselevel = 1;
9183 whilelevel = 0;
9184 }
9185 else
9186 {
9187 elselevel = 0;
9188 whilelevel = 1;
9189 }
9190
9191 curwin->w_cursor.col = 0;
9192
9193 while (curwin->w_cursor.lnum > ourscope + 1)
9194 {
9195 curwin->w_cursor.lnum--;
9196 curwin->w_cursor.col = 0;
9197
9198 look = cin_skipcomment(ml_get_curline());
9199 if (cin_iselse(look)
9200 || cin_isif(look)
9201 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009202 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 {
9204 /*
9205 * if we've gone outside the braces entirely,
9206 * we must be out of scope...
9207 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009208 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 if (theirscope == NULL)
9210 break;
9211
9212 /*
9213 * and if the brace enclosing this is further
9214 * back than the one enclosing the else, we're
9215 * out of luck too.
9216 */
9217 if (theirscope->lnum < ourscope)
9218 break;
9219
9220 /*
9221 * and if they're enclosed in a *deeper* brace,
9222 * then we can ignore it because it's in a
9223 * different scope...
9224 */
9225 if (theirscope->lnum > ourscope)
9226 continue;
9227
9228 /*
9229 * if it was an "else" (that's not an "else if")
9230 * then we need to go back to another if, so
9231 * increment elselevel
9232 */
9233 look = cin_skipcomment(ml_get_curline());
9234 if (cin_iselse(look))
9235 {
9236 mightbeif = cin_skipcomment(look + 4);
9237 if (!cin_isif(mightbeif))
9238 ++elselevel;
9239 continue;
9240 }
9241
9242 /*
9243 * if it was a "while" then we need to go back to
9244 * another "do", so increment whilelevel. XXX
9245 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009246 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 {
9248 ++whilelevel;
9249 continue;
9250 }
9251
9252 /* If it's an "if" decrement elselevel */
9253 look = cin_skipcomment(ml_get_curline());
9254 if (cin_isif(look))
9255 {
9256 elselevel--;
9257 /*
9258 * When looking for an "if" ignore "while"s that
9259 * get in the way.
9260 */
9261 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9262 whilelevel = 0;
9263 }
9264
9265 /* If it's a "do" decrement whilelevel */
9266 if (cin_isdo(look))
9267 whilelevel--;
9268
9269 /*
9270 * if we've used up all the elses, then
9271 * this must be the if that we want!
9272 * match the indent level of that if.
9273 */
9274 if (elselevel <= 0 && whilelevel <= 0)
9275 {
9276 return OK;
9277 }
9278 }
9279 }
9280 return FAIL;
9281}
9282
9283# if defined(FEAT_EVAL) || defined(PROTO)
9284/*
9285 * Get indent level from 'indentexpr'.
9286 */
9287 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009288get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009290 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009291 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009292 pos_T save_pos;
9293 colnr_T save_curswant;
9294 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009296 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9297 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009299 /* Save and restore cursor position and curswant, in case it was changed
9300 * via :normal commands */
9301 save_pos = curwin->w_cursor;
9302 save_curswant = curwin->w_curswant;
9303 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009305 if (use_sandbox)
9306 ++sandbox;
9307 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009308
9309 /* Need to make a copy, the 'indentexpr' option could be changed while
9310 * evaluating it. */
9311 inde_copy = vim_strsave(curbuf->b_p_inde);
9312 if (inde_copy != NULL)
9313 {
9314 indent = (int)eval_to_number(inde_copy);
9315 vim_free(inde_copy);
9316 }
9317
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009318 if (use_sandbox)
9319 --sandbox;
9320 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321
9322 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9323 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9324 * command. */
9325 save_State = State;
9326 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009327 curwin->w_cursor = save_pos;
9328 curwin->w_curswant = save_curswant;
9329 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 check_cursor();
9331 State = save_State;
9332
9333 /* If there is an error, just keep the current indent. */
9334 if (indent < 0)
9335 indent = get_indent();
9336
9337 return indent;
9338}
9339# endif
9340
9341#endif /* FEAT_CINDENT */
9342
9343#if defined(FEAT_LISP) || defined(PROTO)
9344
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009345static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
9347 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009348lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
9350 char_u buf[LSIZE];
9351 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009352 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353
9354 while (*word != NUL)
9355 {
9356 (void)copy_option_part(&word, buf, LSIZE, ",");
9357 len = (int)STRLEN(buf);
9358 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9359 return TRUE;
9360 }
9361 return FALSE;
9362}
9363
9364/*
9365 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9366 * The incompatible newer method is quite a bit better at indenting
9367 * code in lisp-like languages than the traditional one; it's still
9368 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9369 *
9370 * TODO:
9371 * Findmatch() should be adapted for lisp, also to make showmatch
9372 * work correctly: now (v5.3) it seems all C/C++ oriented:
9373 * - it does not recognize the #\( and #\) notations as character literals
9374 * - it doesn't know about comments starting with a semicolon
9375 * - it incorrectly interprets '(' as a character literal
9376 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009377 * Update from Sergey Khorev:
9378 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 */
9380 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009381get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009383 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 int amount;
9385 char_u *that;
9386 colnr_T col;
9387 colnr_T firsttry;
9388 int parencount, quotecount;
9389 int vi_lisp;
9390
9391 /* Set vi_lisp to use the vi-compatible method */
9392 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9393
9394 realpos = curwin->w_cursor;
9395 curwin->w_cursor.col = 0;
9396
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009397 if ((pos = findmatch(NULL, '(')) == NULL)
9398 pos = findmatch(NULL, '[');
9399 else
9400 {
9401 paren = *pos;
9402 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009403 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009404 pos = &paren;
9405 }
9406 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
9408 /* Extra trick: Take the indent of the first previous non-white
9409 * line that is at the same () level. */
9410 amount = -1;
9411 parencount = 0;
9412
9413 while (--curwin->w_cursor.lnum >= pos->lnum)
9414 {
9415 if (linewhite(curwin->w_cursor.lnum))
9416 continue;
9417 for (that = ml_get_curline(); *that != NUL; ++that)
9418 {
9419 if (*that == ';')
9420 {
9421 while (*(that + 1) != NUL)
9422 ++that;
9423 continue;
9424 }
9425 if (*that == '\\')
9426 {
9427 if (*(that + 1) != NUL)
9428 ++that;
9429 continue;
9430 }
9431 if (*that == '"' && *(that + 1) != NUL)
9432 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009433 while (*++that && *that != '"')
9434 {
9435 /* skipping escaped characters in the string */
9436 if (*that == '\\')
9437 {
9438 if (*++that == NUL)
9439 break;
9440 if (that[1] == NUL)
9441 {
9442 ++that;
9443 break;
9444 }
9445 }
9446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009448 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009450 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 --parencount;
9452 }
9453 if (parencount == 0)
9454 {
9455 amount = get_indent();
9456 break;
9457 }
9458 }
9459
9460 if (amount == -1)
9461 {
9462 curwin->w_cursor.lnum = pos->lnum;
9463 curwin->w_cursor.col = pos->col;
9464 col = pos->col;
9465
9466 that = ml_get_curline();
9467
9468 if (vi_lisp && get_indent() == 0)
9469 amount = 2;
9470 else
9471 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009472 char_u *line = that;
9473
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 amount = 0;
9475 while (*that && col)
9476 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009477 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 col--;
9479 }
9480
9481 /*
9482 * Some keywords require "body" indenting rules (the
9483 * non-standard-lisp ones are Scheme special forms):
9484 *
9485 * (let ((a 1)) instead (let ((a 1))
9486 * (...)) of (...))
9487 */
9488
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009489 if (!vi_lisp && (*that == '(' || *that == '[')
9490 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 amount += 2;
9492 else
9493 {
9494 that++;
9495 amount++;
9496 firsttry = amount;
9497
Bram Moolenaar1c465442017-03-12 20:10:05 +01009498 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009500 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 ++that;
9502 }
9503
9504 if (*that && *that != ';') /* not a comment line */
9505 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009506 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009508 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 firsttry++;
9510
9511 parencount = 0;
9512 quotecount = 0;
9513
9514 if (vi_lisp
9515 || (*that != '"'
9516 && *that != '\''
9517 && *that != '#'
9518 && (*that < '0' || *that > '9')))
9519 {
9520 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009521 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 || quotecount
9523 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009524 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 && !quotecount
9526 && !parencount
9527 && vi_lisp)))
9528 {
9529 if (*that == '"')
9530 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009531 if ((*that == '(' || *that == '[')
9532 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009534 if ((*that == ')' || *that == ']')
9535 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 --parencount;
9537 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009538 amount += lbr_chartabsize_adv(
9539 line, &that, (colnr_T)amount);
9540 amount += lbr_chartabsize_adv(
9541 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 }
9543 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009544 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009546 amount += lbr_chartabsize(
9547 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 that++;
9549 }
9550 if (!*that || *that == ';')
9551 amount = firsttry;
9552 }
9553 }
9554 }
9555 }
9556 }
9557 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009558 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
9560 curwin->w_cursor = realpos;
9561
9562 return amount;
9563}
9564#endif /* FEAT_LISP */
9565
9566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009567prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009569#if defined(SIGHUP) && defined(SIG_IGN)
9570 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9571 * makes Vim exit and then handling SIGHUP causes various reentrance
9572 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009573 signal(SIGHUP, SIG_IGN);
9574#endif
9575
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576#ifdef FEAT_GUI
9577 if (gui.in_use)
9578 {
9579 gui.dying = TRUE;
9580 out_trash(); /* trash any pending output */
9581 }
9582 else
9583#endif
9584 {
9585 windgoto((int)Rows - 1, 0);
9586
9587 /*
9588 * Switch terminal mode back now, so messages end up on the "normal"
9589 * screen (if there are two screens).
9590 */
9591 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009592 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 out_flush();
9594 }
9595}
9596
9597/*
9598 * Preserve files and exit.
9599 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009600 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9601 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 */
9603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009604preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 buf_T *buf;
9607
9608 prepare_to_exit();
9609
Bram Moolenaar4770d092006-01-12 23:22:24 +00009610 /* Setting this will prevent free() calls. That avoids calling free()
9611 * recursively when free() was invoked with a bad pointer. */
9612 really_exiting = TRUE;
9613
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 out_str(IObuff);
9615 screen_start(); /* don't know where cursor is now */
9616 out_flush();
9617
9618 ml_close_notmod(); /* close all not-modified buffers */
9619
Bram Moolenaar29323592016-07-24 22:04:11 +02009620 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 {
9622 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9623 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009624 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 screen_start(); /* don't know where cursor is now */
9626 out_flush();
9627 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9628 break;
9629 }
9630 }
9631
9632 ml_close_all(FALSE); /* close all memfiles, without deleting */
9633
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009634 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635
9636 getout(1);
9637}
9638
9639/*
9640 * return TRUE if "fname" exists.
9641 */
9642 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009643vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009645 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646
9647 if (mch_stat((char *)fname, &st))
9648 return FALSE;
9649 return TRUE;
9650}
9651
9652/*
9653 * Check for CTRL-C pressed, but only once in a while.
9654 * Should be used instead of ui_breakcheck() for functions that check for
9655 * each line in the file. Calling ui_breakcheck() each time takes too much
9656 * time, because it can be a system call.
9657 */
9658
9659#ifndef BREAKCHECK_SKIP
9660# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9661# define BREAKCHECK_SKIP 200
9662# else
9663# define BREAKCHECK_SKIP 32
9664# endif
9665#endif
9666
9667static int breakcheck_count = 0;
9668
9669 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009670line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
9672 if (++breakcheck_count >= BREAKCHECK_SKIP)
9673 {
9674 breakcheck_count = 0;
9675 ui_breakcheck();
9676 }
9677}
9678
9679/*
9680 * Like line_breakcheck() but check 10 times less often.
9681 */
9682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009683fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9686 {
9687 breakcheck_count = 0;
9688 ui_breakcheck();
9689 }
9690}
9691
9692/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009693 * Invoke expand_wildcards() for one pattern.
9694 * Expand items like "%:h" before the expansion.
9695 * Returns OK or FAIL.
9696 */
9697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009698expand_wildcards_eval(
9699 char_u **pat, /* pointer to input pattern */
9700 int *num_file, /* resulting number of files */
9701 char_u ***file, /* array of resulting files */
9702 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009703{
9704 int ret = FAIL;
9705 char_u *eval_pat = NULL;
9706 char_u *exp_pat = *pat;
9707 char_u *ignored_msg;
9708 int usedlen;
9709
9710 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9711 {
9712 ++emsg_off;
9713 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9714 NULL, &ignored_msg, NULL);
9715 --emsg_off;
9716 if (eval_pat != NULL)
9717 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9718 }
9719
9720 if (exp_pat != NULL)
9721 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9722
9723 if (eval_pat != NULL)
9724 {
9725 vim_free(exp_pat);
9726 vim_free(eval_pat);
9727 }
9728
9729 return ret;
9730}
9731
9732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9734 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009735 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 */
9737 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009738expand_wildcards(
9739 int num_pat, /* number of input patterns */
9740 char_u **pat, /* array of input patterns */
9741 int *num_files, /* resulting number of files */
9742 char_u ***files, /* array of resulting files */
9743 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744{
9745 int retval;
9746 int i, j;
9747 char_u *p;
9748 int non_suf_match; /* number without matching suffix */
9749
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009750 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751
9752 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009753 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 return retval;
9755
9756#ifdef FEAT_WILDIGN
9757 /*
9758 * Remove names that match 'wildignore'.
9759 */
9760 if (*p_wig)
9761 {
9762 char_u *ffname;
9763
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009764 /* check all files in (*files)[] */
9765 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009767 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 if (ffname == NULL) /* out of memory */
9769 break;
9770# ifdef VMS
9771 vms_remove_version(ffname);
9772# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009773 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009775 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009776 vim_free((*files)[i]);
9777 for (j = i; j + 1 < *num_files; ++j)
9778 (*files)[j] = (*files)[j + 1];
9779 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 --i;
9781 }
9782 vim_free(ffname);
9783 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009784
9785 /* If the number of matches is now zero, we fail. */
9786 if (*num_files == 0)
9787 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009788 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009789 return FAIL;
9790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 }
9792#endif
9793
9794 /*
9795 * Move the names where 'suffixes' match to the end.
9796 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009797 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 {
9799 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009800 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009802 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 {
9804 /*
9805 * Move the name without matching suffix to the front
9806 * of the list.
9807 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009808 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009810 (*files)[j] = (*files)[j - 1];
9811 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 }
9813 }
9814 }
9815
9816 return retval;
9817}
9818
9819/*
9820 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9821 */
9822 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009823match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824{
9825 int fnamelen, setsuflen;
9826 char_u *setsuf;
9827#define MAXSUFLEN 30 /* maximum length of a file suffix */
9828 char_u suf_buf[MAXSUFLEN];
9829
9830 fnamelen = (int)STRLEN(fname);
9831 setsuflen = 0;
9832 for (setsuf = p_su; *setsuf; )
9833 {
9834 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009835 if (setsuflen == 0)
9836 {
9837 char_u *tail = gettail(fname);
9838
9839 /* empty entry: match name without a '.' */
9840 if (vim_strchr(tail, '.') == NULL)
9841 {
9842 setsuflen = 1;
9843 break;
9844 }
9845 }
9846 else
9847 {
9848 if (fnamelen >= setsuflen
9849 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9850 (size_t)setsuflen) == 0)
9851 break;
9852 setsuflen = 0;
9853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 }
9855 return (setsuflen != 0);
9856}
9857
9858#if !defined(NO_EXPANDPATH) || defined(PROTO)
9859
9860# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009861static int vim_backtick(char_u *p);
9862static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863# endif
9864
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009865# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866/*
9867 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9868 * it's shared between these systems.
9869 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009870# if defined(PROTO)
9871# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872# else
9873# ifdef __BORLANDC__
9874# define _cdecl _RTLENTRYF
9875# endif
9876# endif
9877
9878/*
9879 * comparison function for qsort in dos_expandpath()
9880 */
9881 static int _cdecl
9882pstrcmp(const void *a, const void *b)
9883{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009884 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885}
9886
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009888 * Recursively expand one path component into all matching files and/or
9889 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 * Return the number of matches found.
9891 * "path" has backslashes before chars that are not to be expanded, starting
9892 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009893 * Return the number of matches found.
9894 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 */
9896 static int
9897dos_expandpath(
9898 garray_T *gap,
9899 char_u *path,
9900 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009901 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009902 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009904 char_u *buf;
9905 char_u *path_end;
9906 char_u *p, *s, *e;
9907 int start_len = gap->ga_len;
9908 char_u *pat;
9909 regmatch_T regmatch;
9910 int starts_with_dot;
9911 int matches;
9912 int len;
9913 int starstar = FALSE;
9914 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 WIN32_FIND_DATA fb;
9916 HANDLE hFind = (HANDLE)0;
9917# ifdef FEAT_MBYTE
9918 WIN32_FIND_DATAW wfb;
9919 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009922 int ok;
9923
9924 /* Expanding "**" may take a long time, check for CTRL-C. */
9925 if (stardepth > 0)
9926 {
9927 ui_breakcheck();
9928 if (got_int)
9929 return 0;
9930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009932 /* Make room for file name. When doing encoding conversion the actual
9933 * length may be quite a bit longer, thus use the maximum possible length. */
9934 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 if (buf == NULL)
9936 return 0;
9937
9938 /*
9939 * Find the first part in the path name that contains a wildcard or a ~1.
9940 * Copy it into buf, including the preceding characters.
9941 */
9942 p = buf;
9943 s = buf;
9944 e = NULL;
9945 path_end = path;
9946 while (*path_end != NUL)
9947 {
9948 /* May ignore a wildcard that has a backslash before it; it will
9949 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9950 if (path_end >= path + wildoff && rem_backslash(path_end))
9951 *p++ = *path_end++;
9952 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9953 {
9954 if (e != NULL)
9955 break;
9956 s = p + 1;
9957 }
9958 else if (path_end >= path + wildoff
9959 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9960 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009961# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 if (has_mbyte)
9963 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009964 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 STRNCPY(p, path_end, len);
9966 p += len;
9967 path_end += len;
9968 }
9969 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 *p++ = *path_end++;
9972 }
9973 e = p;
9974 *e = NUL;
9975
9976 /* now we have one wildcard component between s and e */
9977 /* Remove backslashes between "wildoff" and the start of the wildcard
9978 * component. */
9979 for (p = buf + wildoff; p < s; ++p)
9980 if (rem_backslash(p))
9981 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009982 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 --e;
9984 --s;
9985 }
9986
Bram Moolenaar231334e2005-07-25 20:46:57 +00009987 /* Check for "**" between "s" and "e". */
9988 for (p = s; p < e; ++p)
9989 if (p[0] == '*' && p[1] == '*')
9990 starstar = TRUE;
9991
Bram Moolenaard82103e2016-01-17 17:04:05 +01009992 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9994 if (pat == NULL)
9995 {
9996 vim_free(buf);
9997 return 0;
9998 }
9999
10000 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010001 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010002 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 regmatch.rm_ic = TRUE; /* Always ignore case */
10004 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010005 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010006 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 vim_free(pat);
10008
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010009 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 {
10011 vim_free(buf);
10012 return 0;
10013 }
10014
10015 /* remember the pattern or file name being looked for */
10016 matchname = vim_strsave(s);
10017
Bram Moolenaar231334e2005-07-25 20:46:57 +000010018 /* If "**" is by itself, this is the first time we encounter it and more
10019 * is following then find matches without any directory. */
10020 if (!didstar && stardepth < 100 && starstar && e - s == 2
10021 && *path_end == '/')
10022 {
10023 STRCPY(s, path_end + 1);
10024 ++stardepth;
10025 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10026 --stardepth;
10027 }
10028
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 /* Scan all files in the directory with "dir/ *.*" */
10030 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031# ifdef FEAT_MBYTE
10032 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10033 {
10034 /* The active codepage differs from 'encoding'. Attempt using the
10035 * wide function. If it fails because it is not implemented fall back
10036 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010037 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 if (wn != NULL)
10039 {
10040 hFind = FindFirstFileW(wn, &wfb);
10041 if (hFind == INVALID_HANDLE_VALUE
10042 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010043 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 }
10045 }
10046
10047 if (wn == NULL)
10048# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010049 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
10052 while (ok)
10053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054# ifdef FEAT_MBYTE
10055 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010056 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 else
10058# endif
10059 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 /* Ignore entries starting with a dot, unless when asked for. Accept
10061 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010062 if ((p[0] != '.' || starts_with_dot
10063 || ((flags & EW_DODOT)
10064 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010066 || (regmatch.regprog != NULL
10067 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010068 || ((flags & EW_NOTWILD)
10069 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010073
10074 if (starstar && stardepth < 100)
10075 {
10076 /* For "**" in the pattern first go deeper in the tree to
10077 * find matches. */
10078 STRCPY(buf + len, "/**");
10079 STRCPY(buf + len + 3, path_end);
10080 ++stardepth;
10081 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10082 --stardepth;
10083 }
10084
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 STRCPY(buf + len, path_end);
10086 if (mch_has_exp_wildcard(path_end))
10087 {
10088 /* need to expand another component of the path */
10089 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010090 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 }
10092 else
10093 {
10094 /* no more wildcards, check if there is a match */
10095 /* remove backslashes for the remaining components only */
10096 if (*path_end != 0)
10097 backslash_halve(buf + len + 1);
10098 if (mch_getperm(buf) >= 0) /* add existing file */
10099 addfile(gap, buf, flags);
10100 }
10101 }
10102
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103# ifdef FEAT_MBYTE
10104 if (wn != NULL)
10105 {
10106 vim_free(p);
10107 ok = FindNextFileW(hFind, &wfb);
10108 }
10109 else
10110# endif
10111 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112
10113 /* If no more matches and no match was used, try expanding the name
10114 * itself. Finds the long name of a short filename. */
10115 if (!ok && matchname != NULL && gap->ga_len == start_len)
10116 {
10117 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 FindClose(hFind);
10119# ifdef FEAT_MBYTE
10120 if (wn != NULL)
10121 {
10122 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010123 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 if (wn != NULL)
10125 hFind = FindFirstFileW(wn, &wfb);
10126 }
10127 if (wn == NULL)
10128# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010129 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010131 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133 }
10134
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 FindClose(hFind);
10136# ifdef FEAT_MBYTE
10137 vim_free(wn);
10138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010140 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 vim_free(matchname);
10142
10143 matches = gap->ga_len - start_len;
10144 if (matches > 0)
10145 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10146 sizeof(char_u *), pstrcmp);
10147 return matches;
10148}
10149
10150 int
10151mch_expandpath(
10152 garray_T *gap,
10153 char_u *path,
10154 int flags) /* EW_* flags */
10155{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010156 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010158# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159
Bram Moolenaar231334e2005-07-25 20:46:57 +000010160#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10161 || defined(PROTO)
10162/*
10163 * Unix style wildcard expansion code.
10164 * It's here because it's used both for Unix and Mac.
10165 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010166static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010167
10168 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010169pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010170{
10171 return (pathcmp(*(char **)a, *(char **)b, -1));
10172}
10173
10174/*
10175 * Recursively expand one path component into all matching files and/or
10176 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10177 * "path" has backslashes before chars that are not to be expanded, starting
10178 * at "path + wildoff".
10179 * Return the number of matches found.
10180 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10181 */
10182 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010183unix_expandpath(
10184 garray_T *gap,
10185 char_u *path,
10186 int wildoff,
10187 int flags, /* EW_* flags */
10188 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010189{
10190 char_u *buf;
10191 char_u *path_end;
10192 char_u *p, *s, *e;
10193 int start_len = gap->ga_len;
10194 char_u *pat;
10195 regmatch_T regmatch;
10196 int starts_with_dot;
10197 int matches;
10198 int len;
10199 int starstar = FALSE;
10200 static int stardepth = 0; /* depth for "**" expansion */
10201
10202 DIR *dirp;
10203 struct dirent *dp;
10204
10205 /* Expanding "**" may take a long time, check for CTRL-C. */
10206 if (stardepth > 0)
10207 {
10208 ui_breakcheck();
10209 if (got_int)
10210 return 0;
10211 }
10212
10213 /* make room for file name */
10214 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10215 if (buf == NULL)
10216 return 0;
10217
10218 /*
10219 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010220 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010221 * Copy it into "buf", including the preceding characters.
10222 */
10223 p = buf;
10224 s = buf;
10225 e = NULL;
10226 path_end = path;
10227 while (*path_end != NUL)
10228 {
10229 /* May ignore a wildcard that has a backslash before it; it will
10230 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10231 if (path_end >= path + wildoff && rem_backslash(path_end))
10232 *p++ = *path_end++;
10233 else if (*path_end == '/')
10234 {
10235 if (e != NULL)
10236 break;
10237 s = p + 1;
10238 }
10239 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010240 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010241 || (!p_fic && (flags & EW_ICASE)
10242 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010243 e = p;
10244#ifdef FEAT_MBYTE
10245 if (has_mbyte)
10246 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010247 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010248 STRNCPY(p, path_end, len);
10249 p += len;
10250 path_end += len;
10251 }
10252 else
10253#endif
10254 *p++ = *path_end++;
10255 }
10256 e = p;
10257 *e = NUL;
10258
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010259 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010260 /* Remove backslashes between "wildoff" and the start of the wildcard
10261 * component. */
10262 for (p = buf + wildoff; p < s; ++p)
10263 if (rem_backslash(p))
10264 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010265 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010266 --e;
10267 --s;
10268 }
10269
10270 /* Check for "**" between "s" and "e". */
10271 for (p = s; p < e; ++p)
10272 if (p[0] == '*' && p[1] == '*')
10273 starstar = TRUE;
10274
10275 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010276 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010277 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10278 if (pat == NULL)
10279 {
10280 vim_free(buf);
10281 return 0;
10282 }
10283
10284 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010285 if (flags & EW_ICASE)
10286 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10287 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010288 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010289 if (flags & (EW_NOERROR | EW_NOTWILD))
10290 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010291 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010292 if (flags & (EW_NOERROR | EW_NOTWILD))
10293 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010294 vim_free(pat);
10295
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010296 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010297 {
10298 vim_free(buf);
10299 return 0;
10300 }
10301
10302 /* If "**" is by itself, this is the first time we encounter it and more
10303 * is following then find matches without any directory. */
10304 if (!didstar && stardepth < 100 && starstar && e - s == 2
10305 && *path_end == '/')
10306 {
10307 STRCPY(s, path_end + 1);
10308 ++stardepth;
10309 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10310 --stardepth;
10311 }
10312
10313 /* open the directory for scanning */
10314 *s = NUL;
10315 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10316
10317 /* Find all matching entries */
10318 if (dirp != NULL)
10319 {
10320 for (;;)
10321 {
10322 dp = readdir(dirp);
10323 if (dp == NULL)
10324 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010325 if ((dp->d_name[0] != '.' || starts_with_dot
10326 || ((flags & EW_DODOT)
10327 && dp->d_name[1] != NUL
10328 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010329 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10330 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010331 || ((flags & EW_NOTWILD)
10332 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010333 {
10334 STRCPY(s, dp->d_name);
10335 len = STRLEN(buf);
10336
10337 if (starstar && stardepth < 100)
10338 {
10339 /* For "**" in the pattern first go deeper in the tree to
10340 * find matches. */
10341 STRCPY(buf + len, "/**");
10342 STRCPY(buf + len + 3, path_end);
10343 ++stardepth;
10344 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10345 --stardepth;
10346 }
10347
10348 STRCPY(buf + len, path_end);
10349 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10350 {
10351 /* need to expand another component of the path */
10352 /* remove backslashes for the remaining components only */
10353 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10354 }
10355 else
10356 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010357 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010358
Bram Moolenaar231334e2005-07-25 20:46:57 +000010359 /* no more wildcards, check if there is a match */
10360 /* remove backslashes for the remaining components only */
10361 if (*path_end != NUL)
10362 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010363 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010364 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010365 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010366 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010367#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010368 size_t precomp_len = STRLEN(buf)+1;
10369 char_u *precomp_buf =
10370 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010371
Bram Moolenaar231334e2005-07-25 20:46:57 +000010372 if (precomp_buf)
10373 {
10374 mch_memmove(buf, precomp_buf, precomp_len);
10375 vim_free(precomp_buf);
10376 }
10377#endif
10378 addfile(gap, buf, flags);
10379 }
10380 }
10381 }
10382 }
10383
10384 closedir(dirp);
10385 }
10386
10387 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010388 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010389
10390 matches = gap->ga_len - start_len;
10391 if (matches > 0)
10392 qsort(((char_u **)gap->ga_data) + start_len, matches,
10393 sizeof(char_u *), pstrcmp);
10394 return matches;
10395}
10396#endif
10397
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010398#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010399static int find_previous_pathsep(char_u *path, char_u **psep);
10400static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10401static void expand_path_option(char_u *curdir, garray_T *gap);
10402static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10403static void uniquefy_paths(garray_T *gap, char_u *pattern);
10404static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010405
10406/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010407 * Moves "*psep" back to the previous path separator in "path".
10408 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010409 */
10410 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010411find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010412{
10413 /* skip the current separator */
10414 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010415 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010416
10417 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010418 while (*psep > path)
10419 {
10420 if (vim_ispathsep(**psep))
10421 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010422 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010423 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010424
10425 return FAIL;
10426}
10427
10428/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010429 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10430 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010431 */
10432 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010433is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010434{
10435 int j;
10436 int candidate_len;
10437 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010438 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010439 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010440
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010441 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010442 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010443 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010444 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010445
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010446 candidate_len = (int)STRLEN(maybe_unique);
10447 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010448 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010449 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010450
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010451 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010452 if (fnamecmp(maybe_unique, rival) == 0
10453 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010454 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010455 }
10456
Bram Moolenaar162bd912010-07-28 22:29:10 +020010457 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010458}
10459
10460/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010461 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010462 * paths are expanded to their equivalent fullpath. This includes the "."
10463 * (relative to current buffer directory) and empty path (relative to current
10464 * directory) notations.
10465 *
10466 * TODO: handle upward search (;) and path limiter (**N) notations by
10467 * expanding each into their equivalent path(s).
10468 */
10469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010470expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010471{
10472 char_u *path_option = *curbuf->b_p_path == NUL
10473 ? p_path : curbuf->b_p_path;
10474 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010475 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010476 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010477
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010478 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010479 return;
10480
10481 while (*path_option != NUL)
10482 {
10483 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10484
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010485 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010486 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010487 /* Relative to current buffer:
10488 * "/path/file" + "." -> "/path/"
10489 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010490 if (curbuf->b_ffname == NULL)
10491 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010492 p = gettail(curbuf->b_ffname);
10493 len = (int)(p - curbuf->b_ffname);
10494 if (len + (int)STRLEN(buf) >= MAXPATHL)
10495 continue;
10496 if (buf[1] == NUL)
10497 buf[len] = NUL;
10498 else
10499 STRMOVE(buf + len, buf + 2);
10500 mch_memmove(buf, curbuf->b_ffname, len);
10501 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010502 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010503 else if (buf[0] == NUL)
10504 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010505 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010506 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010507 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010508 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010509 else if (!mch_isFullName(buf))
10510 {
10511 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010512 len = (int)STRLEN(curdir);
10513 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010514 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010515 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010516 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010517 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010518 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010519 }
10520
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010521 if (ga_grow(gap, 1) == FAIL)
10522 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010523
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010524# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010525 /* Avoid the path ending in a backslash, it fails when a comma is
10526 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010527 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010528 if (buf[len - 1] == '\\')
10529 buf[len - 1] = '/';
10530# endif
10531
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010532 p = vim_strsave(buf);
10533 if (p == NULL)
10534 break;
10535 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010536 }
10537
10538 vim_free(buf);
10539}
10540
10541/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010542 * Returns a pointer to the file or directory name in "fname" that matches the
10543 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010544 *
10545 * path: /foo/bar/baz
10546 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010547 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010548 */
10549 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010550get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010551{
10552 int i;
10553 int maxlen = 0;
10554 char_u **path_part = (char_u **)gap->ga_data;
10555 char_u *cutoff = NULL;
10556
10557 for (i = 0; i < gap->ga_len; i++)
10558 {
10559 int j = 0;
10560
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010561 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010562# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010563 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10564#endif
10565 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010566 j++;
10567 if (j > maxlen)
10568 {
10569 maxlen = j;
10570 cutoff = &fname[j];
10571 }
10572 }
10573
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010574 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010575 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010576 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010577 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010578
10579 return cutoff;
10580}
10581
10582/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010583 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10584 * that they are unique with respect to each other while conserving the part
10585 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010586 */
10587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010588uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010589{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010590 int i;
10591 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010592 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010593 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010594 char_u *pat;
10595 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010596 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010597 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010598 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010599 char_u **in_curdir = NULL;
10600 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010601
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010602 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010603 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010604
10605 /*
10606 * We need to prepend a '*' at the beginning of file_pattern so that the
10607 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010608 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010609 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010610 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010611 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010612 if (file_pattern == NULL)
10613 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010614 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010615 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010616 STRCAT(file_pattern, pattern);
10617 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10618 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010619 if (pat == NULL)
10620 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010621
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010622 regmatch.rm_ic = TRUE; /* always ignore case */
10623 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10624 vim_free(pat);
10625 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010626 return;
10627
Bram Moolenaar162bd912010-07-28 22:29:10 +020010628 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010629 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010630 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010631 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010632
10633 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010634 if (in_curdir == NULL)
10635 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010636
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010637 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010638 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639 char_u *path = fnames[i];
10640 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010641 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010642 char_u *pathsep_p;
10643 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010644
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010645 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010646 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010647 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010648 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010649 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010650
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010651 /* Shorten the filename while maintaining its uniqueness */
10652 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010653
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010654 /* Don't assume all files can be reached without path when search
10655 * pattern starts with star star slash, so only remove path_cutoff
10656 * when possible. */
10657 if (pattern[0] == '*' && pattern[1] == '*'
10658 && vim_ispathsep_nocolon(pattern[2])
10659 && path_cutoff != NULL
10660 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10661 && is_unique(path_cutoff, gap, i))
10662 {
10663 sort_again = TRUE;
10664 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10665 }
10666 else
10667 {
10668 /* Here all files can be reached without path, so get shortest
10669 * unique path. We start at the end of the path. */
10670 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010671
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010672 while (find_previous_pathsep(path, &pathsep_p))
10673 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10674 && is_unique(pathsep_p + 1, gap, i)
10675 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10676 {
10677 sort_again = TRUE;
10678 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10679 break;
10680 }
10681 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010682
10683 if (mch_isFullName(path))
10684 {
10685 /*
10686 * Last resort: shorten relative to curdir if possible.
10687 * 'possible' means:
10688 * 1. It is under the current directory.
10689 * 2. The result is actually shorter than the original.
10690 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010691 * Before curdir After
10692 * /foo/bar/file.txt /foo/bar ./file.txt
10693 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10694 * /file.txt / /file.txt
10695 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010696 */
10697 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010698 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010699#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010700 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010701 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010702 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010703 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010704 && !vim_ispathsep(*short_name)
10705#endif
10706 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010707 {
10708 STRCPY(path, ".");
10709 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010710 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010711 }
10712 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010713 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010714 }
10715
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010716 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010717 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010718 {
10719 char_u *rel_path;
10720 char_u *path = in_curdir[i];
10721
10722 if (path == NULL)
10723 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010724
10725 /* If the {filename} is not unique, change it to ./{filename}.
10726 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010727 short_name = shorten_fname(path, curdir);
10728 if (short_name == NULL)
10729 short_name = path;
10730 if (is_unique(short_name, gap, i))
10731 {
10732 STRCPY(fnames[i], short_name);
10733 continue;
10734 }
10735
10736 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10737 if (rel_path == NULL)
10738 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010739 STRCPY(rel_path, ".");
10740 add_pathsep(rel_path);
10741 STRCAT(rel_path, short_name);
10742
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010743 vim_free(fnames[i]);
10744 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010745 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010746 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010747 }
10748
Bram Moolenaar162bd912010-07-28 22:29:10 +020010749theend:
10750 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010751 if (in_curdir != NULL)
10752 {
10753 for (i = 0; i < gap->ga_len; i++)
10754 vim_free(in_curdir[i]);
10755 vim_free(in_curdir);
10756 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010757 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010758 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010759
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010760 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010761 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010762}
10763
10764/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010765 * Calls globpath() with 'path' values for the given pattern and stores the
10766 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010767 * Returns the total number of matches.
10768 */
10769 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010770expand_in_path(
10771 garray_T *gap,
10772 char_u *pattern,
10773 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010774{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010775 char_u *curdir;
10776 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010777 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010778 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010779
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010780 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010781 return 0;
10782 mch_dirname(curdir, MAXPATHL);
10783
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010784 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010785 expand_path_option(curdir, &path_ga);
10786 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010787 if (path_ga.ga_len == 0)
10788 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010789
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010790 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010791 ga_clear_strings(&path_ga);
10792 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010793 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010794
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010795 if (flags & EW_ICASE)
10796 glob_flags |= WILD_ICASE;
10797 if (flags & EW_ADDSLASH)
10798 glob_flags |= WILD_ADD_SLASH;
10799 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010800 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010801
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010802 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010803}
10804#endif
10805
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010806#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10807/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010808 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10809 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010810 */
10811 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010812remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010813{
10814 int i;
10815 int j;
10816 char_u **fnames = (char_u **)gap->ga_data;
10817
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010818 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010819 for (i = gap->ga_len - 1; i > 0; --i)
10820 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10821 {
10822 vim_free(fnames[i]);
10823 for (j = i + 1; j < gap->ga_len; ++j)
10824 fnames[j - 1] = fnames[j];
10825 --gap->ga_len;
10826 }
10827}
10828#endif
10829
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010830static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010831
10832/*
10833 * Return TRUE if "p" contains what looks like an environment variable.
10834 * Allowing for escaping.
10835 */
10836 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010837has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010838{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010839 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010840 {
10841 if (*p == '\\' && p[1] != NUL)
10842 ++p;
10843 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010844#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010845 "$%"
10846#else
10847 "$"
10848#endif
10849 , *p) != NULL)
10850 return TRUE;
10851 }
10852 return FALSE;
10853}
10854
10855#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010856static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010857
10858/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010859 * Return TRUE if "p" contains a special wildcard character, one that Vim
10860 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010861 */
10862 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010863has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010864{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010865 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010866 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010867 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010868 if (*p == '\\' && p[1] != NUL)
10869 ++p;
10870 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10871 return TRUE;
10872 }
10873 return FALSE;
10874}
10875#endif
10876
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877/*
10878 * Generic wildcard expansion code.
10879 *
10880 * Characters in "pat" that should not be expanded must be preceded with a
10881 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10882 *
10883 * Return FAIL when no single file was found. In this case "num_file" is not
10884 * set, and "file" may contain an error message.
10885 * Return OK when some files found. "num_file" is set to the number of
10886 * matches, "file" to the array of matches. Call FreeWild() later.
10887 */
10888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010889gen_expand_wildcards(
10890 int num_pat, /* number of input patterns */
10891 char_u **pat, /* array of input patterns */
10892 int *num_file, /* resulting number of files */
10893 char_u ***file, /* array of resulting files */
10894 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895{
10896 int i;
10897 garray_T ga;
10898 char_u *p;
10899 static int recursive = FALSE;
10900 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010901 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010902#if defined(FEAT_SEARCHPATH)
10903 int did_expand_in_path = FALSE;
10904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
10906 /*
10907 * expand_env() is called to expand things like "~user". If this fails,
10908 * it calls ExpandOne(), which brings us back here. In this case, always
10909 * call the machine specific expansion function, if possible. Otherwise,
10910 * return FAIL.
10911 */
10912 if (recursive)
10913#ifdef SPECIAL_WILDCHAR
10914 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10915#else
10916 return FAIL;
10917#endif
10918
10919#ifdef SPECIAL_WILDCHAR
10920 /*
10921 * If there are any special wildcard characters which we cannot handle
10922 * here, call machine specific function for all the expansion. This
10923 * avoids starting the shell for each argument separately.
10924 * For `=expr` do use the internal function.
10925 */
10926 for (i = 0; i < num_pat; i++)
10927 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010928 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929# ifdef VIM_BACKTICK
10930 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10931# endif
10932 )
10933 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10934 }
10935#endif
10936
10937 recursive = TRUE;
10938
10939 /*
10940 * The matching file names are stored in a growarray. Init it empty.
10941 */
10942 ga_init2(&ga, (int)sizeof(char_u *), 30);
10943
10944 for (i = 0; i < num_pat; ++i)
10945 {
10946 add_pat = -1;
10947 p = pat[i];
10948
10949#ifdef VIM_BACKTICK
10950 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010953 if (add_pat == -1)
10954 retval = FAIL;
10955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 else
10957#endif
10958 {
10959 /*
10960 * First expand environment variables, "~/" and "~user/".
10961 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010962 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010964 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 if (p == NULL)
10966 p = pat[i];
10967#ifdef UNIX
10968 /*
10969 * On Unix, if expand_env() can't expand an environment
10970 * variable, use the shell to do that. Discard previously
10971 * found file names and start all over again.
10972 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010973 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 {
10975 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010976 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010978 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 recursive = FALSE;
10980 return i;
10981 }
10982#endif
10983 }
10984
10985 /*
10986 * If there are wildcards: Expand file names and add each match to
10987 * the list. If there is no match, and EW_NOTFOUND is given, add
10988 * the pattern.
10989 * If there are no wildcards: Add the file name if it exists or
10990 * when EW_NOTFOUND is given.
10991 */
10992 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010993 {
10994#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010995 if ((flags & EW_PATH)
10996 && !mch_isFullName(p)
10997 && !(p[0] == '.'
10998 && (vim_ispathsep(p[1])
10999 || (p[1] == '.' && vim_ispathsep(p[2]))))
11000 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011001 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011002 /* :find completion where 'path' is used.
11003 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011004 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011005 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011006 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011007 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011008 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011009 else
11010#endif
11011 add_pat = mch_expandpath(&ga, p, flags);
11012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 }
11014
11015 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11016 {
11017 char_u *t = backslash_halve_save(p);
11018
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11020 * "vim c:/" work. */
11021 if (flags & EW_NOTFOUND)
11022 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 addfile(&ga, t, flags);
11025 vim_free(t);
11026 }
11027
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011028#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011029 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011030 uniquefy_paths(&ga, p);
11031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 if (p != pat[i])
11033 vim_free(p);
11034 }
11035
11036 *num_file = ga.ga_len;
11037 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11038
11039 recursive = FALSE;
11040
Bram Moolenaar336bd622016-01-17 18:23:58 +010011041 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042}
11043
11044# ifdef VIM_BACKTICK
11045
11046/*
11047 * Return TRUE if we can expand this backtick thing here.
11048 */
11049 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011050vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051{
11052 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11053}
11054
11055/*
11056 * Expand an item in `backticks` by executing it as a command.
11057 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011058 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 */
11060 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011061expand_backtick(
11062 garray_T *gap,
11063 char_u *pat,
11064 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065{
11066 char_u *p;
11067 char_u *cmd;
11068 char_u *buffer;
11069 int cnt = 0;
11070 int i;
11071
11072 /* Create the command: lop off the backticks. */
11073 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11074 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011075 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076
11077#ifdef FEAT_EVAL
11078 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011079 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 else
11081#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011082 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011083 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 vim_free(cmd);
11085 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011086 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087
11088 cmd = buffer;
11089 while (*cmd != NUL)
11090 {
11091 cmd = skipwhite(cmd); /* skip over white space */
11092 p = cmd;
11093 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11094 ++p;
11095 /* add an entry if it is not empty */
11096 if (p > cmd)
11097 {
11098 i = *p;
11099 *p = NUL;
11100 addfile(gap, cmd, flags);
11101 *p = i;
11102 ++cnt;
11103 }
11104 cmd = p;
11105 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11106 ++cmd;
11107 }
11108
11109 vim_free(buffer);
11110 return cnt;
11111}
11112# endif /* VIM_BACKTICK */
11113
11114/*
11115 * Add a file to a file list. Accepted flags:
11116 * EW_DIR add directories
11117 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011118 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 * EW_NOTFOUND add even when it doesn't exist
11120 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011121 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 */
11123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011124addfile(
11125 garray_T *gap,
11126 char_u *f, /* filename */
11127 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128{
11129 char_u *p;
11130 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011131 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011133 /* if the file/dir/link doesn't exist, may not add it */
11134 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011135 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 return;
11137
11138#ifdef FNAME_ILLEGAL
11139 /* if the file/dir contains illegal characters, don't add it */
11140 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11141 return;
11142#endif
11143
11144 isdir = mch_isdir(f);
11145 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11146 return;
11147
Bram Moolenaarb5971142015-03-21 17:32:19 +010011148 /* If the file isn't executable, may not add it. Do accept directories.
11149 * When invoked from expand_shellcmd() do not use $PATH. */
11150 if (!isdir && (flags & EW_EXEC)
11151 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011152 return;
11153
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 /* Make room for another item in the file list. */
11155 if (ga_grow(gap, 1) == FAIL)
11156 return;
11157
11158 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11159 if (p == NULL)
11160 return;
11161
11162 STRCPY(p, f);
11163#ifdef BACKSLASH_IN_FILENAME
11164 slash_adjust(p);
11165#endif
11166 /*
11167 * Append a slash or backslash after directory names if none is present.
11168 */
11169#ifndef DONT_ADD_PATHSEP_TO_DIR
11170 if (isdir && (flags & EW_ADDSLASH))
11171 add_pathsep(p);
11172#endif
11173 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174}
11175#endif /* !NO_EXPANDPATH */
11176
11177#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11178
11179#ifndef SEEK_SET
11180# define SEEK_SET 0
11181#endif
11182#ifndef SEEK_END
11183# define SEEK_END 2
11184#endif
11185
11186/*
11187 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011188 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11189 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 * Returns an allocated string, or NULL for error.
11191 */
11192 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011193get_cmd_output(
11194 char_u *cmd,
11195 char_u *infile, /* optional input file name */
11196 int flags, /* can be SHELL_SILENT */
11197 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198{
11199 char_u *tempname;
11200 char_u *command;
11201 char_u *buffer = NULL;
11202 int len;
11203 int i = 0;
11204 FILE *fd;
11205
11206 if (check_restricted() || check_secure())
11207 return NULL;
11208
11209 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011210 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 {
11212 EMSG(_(e_notmp));
11213 return NULL;
11214 }
11215
11216 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011217 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 if (command == NULL)
11219 goto done;
11220
11221 /*
11222 * Call the shell to execute the command (errors are ignored).
11223 * Don't check timestamps here.
11224 */
11225 ++no_check_timestamps;
11226 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11227 --no_check_timestamps;
11228
11229 vim_free(command);
11230
11231 /*
11232 * read the names from the file into memory
11233 */
11234# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011235 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 fd = mch_fopen((char *)tempname, "r");
11237# else
11238 fd = mch_fopen((char *)tempname, READBIN);
11239# endif
11240
11241 if (fd == NULL)
11242 {
11243 EMSG2(_(e_notopen), tempname);
11244 goto done;
11245 }
11246
11247 fseek(fd, 0L, SEEK_END);
11248 len = ftell(fd); /* get size of temp file */
11249 fseek(fd, 0L, SEEK_SET);
11250
11251 buffer = alloc(len + 1);
11252 if (buffer != NULL)
11253 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11254 fclose(fd);
11255 mch_remove(tempname);
11256 if (buffer == NULL)
11257 goto done;
11258#ifdef VMS
11259 len = i; /* VMS doesn't give us what we asked for... */
11260#endif
11261 if (i != len)
11262 {
11263 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011264 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011266 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011267 {
11268 /* Change NUL into SOH, otherwise the string is truncated. */
11269 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011270 if (buffer[i] == NUL)
11271 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011272
Bram Moolenaar162bd912010-07-28 22:29:10 +020011273 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011274 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011275 else
11276 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
11278done:
11279 vim_free(tempname);
11280 return buffer;
11281}
11282#endif
11283
11284/*
11285 * Free the list of files returned by expand_wildcards() or other expansion
11286 * functions.
11287 */
11288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011289FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011291 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 while (count--)
11294 vim_free(files[count]);
11295 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296}
11297
11298/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011299 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 * Don't do this when still processing a command or a mapping.
11301 * Don't do this when inside a ":normal" command.
11302 */
11303 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011304goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305{
11306 return (p_im && stuff_empty() && typebuf_typed());
11307}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011308
11309/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011310 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011311 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11312 * - Remove any argument. E.g., "csh -f" -> "csh".
11313 * But don't allow a space in the path, so that this works:
11314 * "/usr/bin/csh --rcfile ~/.cshrc"
11315 * But don't do that for Windows, it's common to have a space in the path.
11316 */
11317 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011318get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011319{
11320 char_u *p;
11321
11322#ifdef WIN3264
11323 p = gettail(p_sh);
11324 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11325#else
11326 p = skiptowhite(p_sh);
11327 if (*p == NUL)
11328 {
11329 /* No white space, use the tail. */
11330 p = vim_strsave(gettail(p_sh));
11331 }
11332 else
11333 {
11334 char_u *p1, *p2;
11335
11336 /* Find the last path separator before the space. */
11337 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011338 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011339 if (vim_ispathsep(*p2))
11340 p1 = p2 + 1;
11341 p = vim_strnsave(p1, (int)(p - p1));
11342 }
11343#endif
11344 return p;
11345}