blob: 00a549d66ca412c65f29458390bfac0434ec0064 [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 Moolenaar828c3d72018-06-19 18:58:07 +020017#if defined(FEAT_CMDL_COMPL) && defined(WIN3264)
18# include <lm.h>
19#endif
20
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static char_u *vim_version_dir(char_u *vimdir);
22static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
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 Moolenaar04958cb2018-06-23 19:23:02 +020035#ifdef FEAT_VARTABS
36 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
37 curbuf->b_p_vts_array, FALSE);
38#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020039 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041}
42
43/*
44 * Count the size (in window cells) of the indent in line "lnum".
45 */
46 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010047get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020049#ifdef FEAT_VARTABS
50 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
51 curbuf->b_p_vts_array, FALSE);
52#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020053 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000055}
56
57#if defined(FEAT_FOLDING) || defined(PROTO)
58/*
59 * Count the size (in window cells) of the indent in line "lnum" of buffer
60 * "buf".
61 */
62 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010063get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000064{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020065#ifdef FEAT_VARTABS
66 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
67 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
68#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020069 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000071}
72#endif
73
74/*
75 * count the size (in window cells) of the indent in line "ptr", with
76 * 'tabstop' at "ts"
77 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000078 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010079get_indent_str(
80 char_u *ptr,
81 int ts,
82 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 int count = 0;
85
86 for ( ; *ptr; ++ptr)
87 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020088 if (*ptr == TAB)
89 {
90 if (!list || lcs_tab1) /* count a tab for what it is worth */
91 count += ts - (count % ts);
92 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020093 /* In list mode, when tab is not set, count screen char width
94 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020095 count += ptr2cells(ptr);
96 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 else if (*ptr == ' ')
98 ++count; /* count a space for one */
99 else
100 break;
101 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000102 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103}
104
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200105#ifdef FEAT_VARTABS
106/*
107 * Count the size (in window cells) of the indent in line "ptr", using
108 * variable tabstops.
109 * if "list" is TRUE, count only screen size for tabs.
110 */
111 int
112get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
113{
114 int count = 0;
115
116 for ( ; *ptr; ++ptr)
117 {
118 if (*ptr == TAB) /* count a tab for what it is worth */
119 {
120 if (!list || lcs_tab1)
121 count += tabstop_padding(count, ts, vts);
122 else
123 /* In list mode, when tab is not set, count screen char width
124 * for Tab, displays: ^I */
125 count += ptr2cells(ptr);
126 }
127 else if (*ptr == ' ')
128 ++count; /* count a space for one */
129 else
130 break;
131 }
132 return count;
133}
134#endif
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Set the indent of the current line.
138 * Leaves the cursor on the first non-blank in the line.
139 * Caller must take care of undo.
140 * "flags":
141 * SIN_CHANGED: call changed_bytes() if the line was changed.
142 * SIN_INSERT: insert the indent in front of the line.
143 * SIN_UNDO: save line for undo before changing it.
144 * Returns TRUE if the line was changed.
145 */
146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100147set_indent(
148 int size, /* measured in spaces */
149 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150{
151 char_u *p;
152 char_u *newline;
153 char_u *oldline;
154 char_u *s;
155 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000156 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 int line_len;
158 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000159 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200160#ifdef FEAT_VARTABS
161 int ind_col = 0;
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000164 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000165 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000166 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168 /*
169 * First check if there is anything to do and compute the number of
170 * characters needed for the indent.
171 */
172 todo = size;
173 ind_len = 0;
174 p = oldline = ml_get_curline();
175
176 /* Calculate the buffer size for the new indent, and check to see if it
177 * isn't already set */
178
Bram Moolenaar5002c292007-07-24 13:26:15 +0000179 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
180 * 'preserveindent' are set count the number of characters at the
181 * beginning of the line to be copied */
182 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
184 /* If 'preserveindent' is set then reuse as much as possible of
185 * the existing indent structure for the new indent */
186 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
187 {
188 ind_done = 0;
189
190 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100191 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 if (*p == TAB)
194 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200195#ifdef FEAT_VARTABS
196 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
197 curbuf->b_p_vts_array);
198#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 tab_pad = (int)curbuf->b_p_ts
200 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 /* stop if this tab will overshoot the target */
203 if (todo < tab_pad)
204 break;
205 todo -= tab_pad;
206 ++ind_len;
207 ind_done += tab_pad;
208 }
209 else
210 {
211 --todo;
212 ++ind_len;
213 ++ind_done;
214 }
215 ++p;
216 }
217
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200218#ifdef FEAT_VARTABS
219 /* These diverge from this point. */
220 ind_col = ind_done;
221#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000222 /* Set initial number of whitespace chars to copy if we are
223 * preserving indent but expandtab is set */
224 if (curbuf->b_p_et)
225 orig_char_len = ind_len;
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200228#ifdef FEAT_VARTABS
229 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
230 curbuf->b_p_vts_array);
231#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200233#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000234 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {
236 doit = TRUE;
237 todo -= tab_pad;
238 ++ind_len;
239 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200240#ifdef FEAT_VARTABS
241 ind_col += tab_pad;
242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 }
244 }
245
246 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200247#ifdef FEAT_VARTABS
248 for (;;)
249 {
250 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
251 curbuf->b_p_vts_array);
252 if (todo < tab_pad)
253 break;
254 if (*p != TAB)
255 doit = TRUE;
256 else
257 ++p;
258 todo -= tab_pad;
259 ++ind_len;
260 ind_col += tab_pad;
261 }
262#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 while (todo >= (int)curbuf->b_p_ts)
264 {
265 if (*p != TAB)
266 doit = TRUE;
267 else
268 ++p;
269 todo -= (int)curbuf->b_p_ts;
270 ++ind_len;
271 /* ind_done += (int)curbuf->b_p_ts; */
272 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275 /* count spaces required for indent */
276 while (todo > 0)
277 {
278 if (*p != ' ')
279 doit = TRUE;
280 else
281 ++p;
282 --todo;
283 ++ind_len;
284 /* ++ind_done; */
285 }
286
287 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100288 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 return FALSE;
290
291 /* Allocate memory for the new line. */
292 if (flags & SIN_INSERT)
293 p = oldline;
294 else
295 p = skipwhite(p);
296 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000297
298 /* If 'preserveindent' and 'expandtab' are both set keep the original
299 * characters and allocate accordingly. We will fill the rest with spaces
300 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000301 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000302 {
303 newline = alloc(orig_char_len + size - ind_done + line_len);
304 if (newline == NULL)
305 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000306 todo = size - ind_done;
307 ind_len = orig_char_len + todo; /* Set total length of indent in
308 * characters, which may have been
309 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000310 p = oldline;
311 s = newline;
312 while (orig_char_len > 0)
313 {
314 *s++ = *p++;
315 orig_char_len--;
316 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000317
Bram Moolenaar5002c292007-07-24 13:26:15 +0000318 /* Skip over any additional white space (useful when newindent is less
319 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100320 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000321 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000322
Bram Moolenaar5002c292007-07-24 13:26:15 +0000323 }
324 else
325 {
326 todo = size;
327 newline = alloc(ind_len + line_len);
328 if (newline == NULL)
329 return FALSE;
330 s = newline;
331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
333 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 /* if 'expandtab' isn't set: use TABs */
335 if (!curbuf->b_p_et)
336 {
337 /* If 'preserveindent' is set then reuse as much as possible of
338 * the existing indent structure for the new indent */
339 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
340 {
341 p = oldline;
342 ind_done = 0;
343
Bram Moolenaar1c465442017-03-12 20:10:05 +0100344 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
346 if (*p == TAB)
347 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348#ifdef FEAT_VARTABS
349 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
350 curbuf->b_p_vts_array);
351#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 tab_pad = (int)curbuf->b_p_ts
353 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 /* stop if this tab will overshoot the target */
356 if (todo < tab_pad)
357 break;
358 todo -= tab_pad;
359 ind_done += tab_pad;
360 }
361 else
362 {
363 --todo;
364 ++ind_done;
365 }
366 *s++ = *p++;
367 }
368
369 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200370#ifdef FEAT_VARTABS
371 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
372 curbuf->b_p_vts_array);
373#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (todo >= tab_pad)
377 {
378 *s++ = TAB;
379 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200380#ifdef FEAT_VARTABS
381 ind_done += tab_pad;
382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384
385 p = skipwhite(p);
386 }
387
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200388#ifdef FEAT_VARTABS
389 for (;;)
390 {
391 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
392 curbuf->b_p_vts_array);
393 if (todo < tab_pad)
394 break;
395 *s++ = TAB;
396 todo -= tab_pad;
397 ind_done += tab_pad;
398 }
399#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 while (todo >= (int)curbuf->b_p_ts)
401 {
402 *s++ = TAB;
403 todo -= (int)curbuf->b_p_ts;
404 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 while (todo > 0)
408 {
409 *s++ = ' ';
410 --todo;
411 }
412 mch_memmove(s, p, (size_t)line_len);
413
414 /* Replace the line (unless undo fails). */
415 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
416 {
417 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
418 if (flags & SIN_CHANGED)
419 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200420 /* Correct saved cursor position if it is in this line. */
421 if (saved_cursor.lnum == curwin->w_cursor.lnum)
422 {
423 if (saved_cursor.col >= (colnr_T)(p - oldline))
424 /* cursor was after the indent, adjust for the number of
425 * bytes added/removed */
426 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
427 else if (saved_cursor.col >= (colnr_T)(s - newline))
428 /* cursor was in the indent, and is now after it, put it back
429 * at the start of the indent (replacing spaces with TAB) */
430 saved_cursor.col = (colnr_T)(s - newline);
431 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000432 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 }
434 else
435 vim_free(newline);
436
437 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000438 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439}
440
441/*
442 * Copy the indent from ptr to the current line (and fill to size)
443 * Leaves the cursor on the first non-blank in the line.
444 * Returns TRUE if the line was changed.
445 */
446 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100447copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 char_u *p = NULL;
450 char_u *line = NULL;
451 char_u *s;
452 int todo;
453 int ind_len;
454 int line_len = 0;
455 int tab_pad;
456 int ind_done;
457 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200458#ifdef FEAT_VARTABS
459 int ind_col;
460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
462 /* Round 1: compute the number of characters needed for the indent
463 * Round 2: copy the characters. */
464 for (round = 1; round <= 2; ++round)
465 {
466 todo = size;
467 ind_len = 0;
468 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200469#ifdef FEAT_VARTABS
470 ind_col = 0;
471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 s = src;
473
474 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100475 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {
477 if (*s == TAB)
478 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200479#ifdef FEAT_VARTABS
480 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
481 curbuf->b_p_vts_array);
482#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 tab_pad = (int)curbuf->b_p_ts
484 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 /* Stop if this tab will overshoot the target */
487 if (todo < tab_pad)
488 break;
489 todo -= tab_pad;
490 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200491#ifdef FEAT_VARTABS
492 ind_col += tab_pad;
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
495 else
496 {
497 --todo;
498 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200499#ifdef FEAT_VARTABS
500 ++ind_col;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000504 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 *p++ = *s;
506 ++s;
507 }
508
509 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200510#ifdef FEAT_VARTABS
511 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
512 curbuf->b_p_vts_array);
513#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200515#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200516 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 todo -= tab_pad;
519 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200520#ifdef FEAT_VARTABS
521 ind_col += tab_pad;
522#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000523 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 *p++ = TAB;
525 }
526
527 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200528 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200530#ifdef FEAT_VARTABS
531 for (;;)
532 {
533 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
534 curbuf->b_p_vts_array);
535 if (todo < tab_pad)
536 break;
537 todo -= tab_pad;
538 ++ind_len;
539 ind_col += tab_pad;
540 if (p != NULL)
541 *p++ = TAB;
542 }
543#else
544 while (todo >= (int)curbuf->b_p_ts)
545 {
546 todo -= (int)curbuf->b_p_ts;
547 ++ind_len;
548 if (p != NULL)
549 *p++ = TAB;
550 }
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553
554 /* Count/add spaces required for indent */
555 while (todo > 0)
556 {
557 --todo;
558 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000559 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 *p++ = ' ';
561 }
562
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
565 /* Allocate memory for the result: the copied indent, new indent
566 * and the rest of the line. */
567 line_len = (int)STRLEN(ml_get_curline()) + 1;
568 line = alloc(ind_len + line_len);
569 if (line == NULL)
570 return FALSE;
571 p = line;
572 }
573 }
574
575 /* Append the original line */
576 mch_memmove(p, ml_get_curline(), (size_t)line_len);
577
578 /* Replace the line */
579 ml_replace(curwin->w_cursor.lnum, line, FALSE);
580
581 /* Put the cursor after the indent. */
582 curwin->w_cursor.col = ind_len;
583 return TRUE;
584}
585
586/*
587 * Return the indent of the current line after a number. Return -1 if no
588 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000589 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 */
591 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100592get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 colnr_T col;
595 pos_T pos;
596
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200597 regmatch_T regmatch;
598 int lead_len = 0; /* length of comment leader */
599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (lnum > curbuf->b_ml.ml_line_count)
601 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000602 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200603
604#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200605 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
606 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200607 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000608#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200609 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
610 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200611 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200612 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200613
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200614 /* vim_regexec() expects a pointer to a line. This lets us
615 * start matching for the flp beyond any comment leader... */
616 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200617 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200618 pos.lnum = lnum;
619 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200620#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200621 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200622#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200623 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200624 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200625 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000626
627 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 getvcol(curwin, &pos, &col, NULL, NULL);
630 return (int)col;
631}
632
Bram Moolenaar597a4222014-06-25 14:39:50 +0200633#if defined(FEAT_LINEBREAK) || defined(PROTO)
634/*
635 * Return appropriate space number for breakindent, taking influencing
636 * parameters into account. Window must be specified, since it is not
637 * necessarily always the current one.
638 */
639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640get_breakindent_win(
641 win_T *wp,
642 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200643{
644 static int prev_indent = 0; /* cached indent value */
645 static long prev_ts = 0L; /* cached tabstop value */
646 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100647 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200648#ifdef FEAT_VARTABS
649 static int *prev_vts = NULL; /* cached vartabs values */
650#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200651 int bri = 0;
652 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200653 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200654 - ((wp->w_p_nu || wp->w_p_rnu)
655 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
656 ? number_width(wp) + 1 : 0);
657
658 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200659 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200660 || prev_tick != CHANGEDTICK(wp->w_buffer)
661#ifdef FEAT_VARTABS
662 || prev_vts != wp->w_buffer->b_p_vts_array
663#endif
664 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200665 {
666 prev_line = line;
667 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100668 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200669#ifdef FEAT_VARTABS
670 prev_vts = wp->w_buffer->b_p_vts_array;
671 prev_indent = get_indent_str_vtab(line,
672 (int)wp->w_buffer->b_p_ts,
673 wp->w_buffer->b_p_vts_array, wp->w_p_list);
674#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200675 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200676 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200677#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200678 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200679 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200680
681 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200682 if (wp->w_p_brisbr)
683 bri -= vim_strsize(p_sbr);
684
685 /* Add offset for number column, if 'n' is in 'cpoptions' */
686 bri += win_col_off2(wp);
687
688 /* never indent past left window margin */
689 if (bri < 0)
690 bri = 0;
691 /* always leave at least bri_min characters on the left,
692 * if text width is sufficient */
693 else if (bri > eff_wwidth - wp->w_p_brimin)
694 bri = (eff_wwidth - wp->w_p_brimin < 0)
695 ? 0 : eff_wwidth - wp->w_p_brimin;
696
697 return bri;
698}
699#endif
700
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704/*
705 * Return TRUE if the string "line" starts with a word from 'cinwords'.
706 */
707 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100708cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 char_u *cinw;
711 char_u *cinw_buf;
712 int cinw_len;
713 int retval = FALSE;
714 int len;
715
716 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
717 cinw_buf = alloc((unsigned)cinw_len);
718 if (cinw_buf != NULL)
719 {
720 line = skipwhite(line);
721 for (cinw = curbuf->b_p_cinw; *cinw; )
722 {
723 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
724 if (STRNCMP(line, cinw_buf, len) == 0
725 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
726 {
727 retval = TRUE;
728 break;
729 }
730 }
731 vim_free(cinw_buf);
732 }
733 return retval;
734}
735#endif
736
737/*
738 * open_line: Add a new line below or above the current line.
739 *
740 * For VREPLACE mode, we only add a new line when we get to the end of the
741 * file, otherwise we just start replacing the next line.
742 *
743 * Caller must take care of undo. Since VREPLACE may affect any number of
744 * lines however, it may call u_save_cursor() again when starting to change a
745 * new line.
746 * "flags": OPENLINE_DELSPACES delete spaces after cursor
747 * OPENLINE_DO_COM format comments
748 * OPENLINE_KEEPTRAIL keep trailing spaces
749 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200750 * OPENLINE_COM_LIST format comments with list or 2nd line indent
751 *
752 * "second_line_indent": indent for after ^^D in Insert mode or if flag
753 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200755 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100758open_line(
759 int dir, /* FORWARD or BACKWARD */
760 int flags,
761 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 char_u *saved_line; /* copy of the original line */
764 char_u *next_line = NULL; /* copy of the next line */
765 char_u *p_extra = NULL; /* what goes to next line */
766 int less_cols = 0; /* less columns for mark in new line */
767 int less_cols_off = 0; /* columns to skip for mark adjust */
768 pos_T old_cursor; /* old cursor position */
769 int newcol = 0; /* new cursor column */
770 int newindent = 0; /* auto-indent of the new line */
771 int n;
772 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200773 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#ifdef FEAT_COMMENTS
775 int extra_len = 0; /* length of p_extra string */
776 int lead_len; /* length of comment leader */
777 char_u *lead_flags; /* position in 'comments' for comment leader */
778 char_u *leader = NULL; /* copy of comment leader */
779#endif
780 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 int saved_char = NUL; /* init for GCC */
783#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
784 pos_T *pos;
785#endif
786#ifdef FEAT_SMARTINDENT
787 int do_si = (!p_paste && curbuf->b_p_si
788# ifdef FEAT_CINDENT
789 && !curbuf->b_p_cin
790# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200791# ifdef FEAT_EVAL
792 && *curbuf->b_p_inde == NUL
793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 );
795 int no_si = FALSE; /* reset did_si afterwards */
796 int first_char = NUL; /* init for GCC */
797#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200798#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 int vreplace_mode;
800#endif
801 int did_append; /* appended a new line */
802 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
803
804 /*
805 * make a copy of the current line so we can mess with it
806 */
807 saved_line = vim_strsave(ml_get_curline());
808 if (saved_line == NULL) /* out of memory! */
809 return FALSE;
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (State & VREPLACE_FLAG)
812 {
813 /*
814 * With VREPLACE we make a copy of the next line, which we will be
815 * starting to replace. First make the new line empty and let vim play
816 * with the indenting and comment leader to its heart's content. Then
817 * we grab what it ended up putting on the new line, put back the
818 * original line, and call ins_char() to put each new character onto
819 * the line, replacing what was there before and pushing the right
820 * stuff onto the replace stack. -- webb.
821 */
822 if (curwin->w_cursor.lnum < orig_line_count)
823 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
824 else
825 next_line = vim_strsave((char_u *)"");
826 if (next_line == NULL) /* out of memory! */
827 goto theend;
828
829 /*
830 * In VREPLACE mode, a NL replaces the rest of the line, and starts
831 * replacing the next line, so push all of the characters left on the
832 * line onto the replace stack. We'll push any other characters that
833 * might be replaced at the start of the next line (due to autoindent
834 * etc) a bit later.
835 */
836 replace_push(NUL); /* Call twice because BS over NL expects it */
837 replace_push(NUL);
838 p = saved_line + curwin->w_cursor.col;
839 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000840 {
841#ifdef FEAT_MBYTE
842 if (has_mbyte)
843 p += replace_push_mb(p);
844 else
845#endif
846 replace_push(*p++);
847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 saved_line[curwin->w_cursor.col] = NUL;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200851 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
853 p_extra = saved_line + curwin->w_cursor.col;
854#ifdef FEAT_SMARTINDENT
855 if (do_si) /* need first char after new line break */
856 {
857 p = skipwhite(p_extra);
858 first_char = *p;
859 }
860#endif
861#ifdef FEAT_COMMENTS
862 extra_len = (int)STRLEN(p_extra);
863#endif
864 saved_char = *p_extra;
865 *p_extra = NUL;
866 }
867
868 u_clearline(); /* cannot do "U" command when adding lines */
869#ifdef FEAT_SMARTINDENT
870 did_si = FALSE;
871#endif
872 ai_col = 0;
873
874 /*
875 * If we just did an auto-indent, then we didn't type anything on
876 * the prior line, and it should be truncated. Do this even if 'ai' is not
877 * set because automatically inserting a comment leader also sets did_ai.
878 */
879 if (dir == FORWARD && did_ai)
880 trunc_line = TRUE;
881
882 /*
883 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
884 * indent to use for the new line.
885 */
886 if (curbuf->b_p_ai
887#ifdef FEAT_SMARTINDENT
888 || do_si
889#endif
890 )
891 {
892 /*
893 * count white space on current line
894 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200895#ifdef FEAT_VARTABS
896 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
897 curbuf->b_p_vts_array, FALSE);
898#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200899 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200900#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200901 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
902 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904#ifdef FEAT_SMARTINDENT
905 /*
906 * Do smart indenting.
907 * In insert/replace mode (only when dir == FORWARD)
908 * we may move some text to the next line. If it starts with '{'
909 * don't add an indent. Fixes inserting a NL before '{' in line
910 * "if (condition) {"
911 */
912 if (!trunc_line && do_si && *saved_line != NUL
913 && (p_extra == NULL || first_char != '{'))
914 {
915 char_u *ptr;
916 char_u last_char;
917
918 old_cursor = curwin->w_cursor;
919 ptr = saved_line;
920# ifdef FEAT_COMMENTS
921 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200922 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 else
924 lead_len = 0;
925# endif
926 if (dir == FORWARD)
927 {
928 /*
929 * Skip preprocessor directives, unless they are
930 * recognised as comments.
931 */
932 if (
933# ifdef FEAT_COMMENTS
934 lead_len == 0 &&
935# endif
936 ptr[0] == '#')
937 {
938 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
939 ptr = ml_get(--curwin->w_cursor.lnum);
940 newindent = get_indent();
941 }
942# ifdef FEAT_COMMENTS
943 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200944 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 else
946 lead_len = 0;
947 if (lead_len > 0)
948 {
949 /*
950 * This case gets the following right:
951 * \*
952 * * A comment (read '\' as '/').
953 * *\
954 * #define IN_THE_WAY
955 * This should line up here;
956 */
957 p = skipwhite(ptr);
958 if (p[0] == '/' && p[1] == '*')
959 p++;
960 if (p[0] == '*')
961 {
962 for (p++; *p; p++)
963 {
964 if (p[0] == '/' && p[-1] == '*')
965 {
966 /*
967 * End of C comment, indent should line up
968 * with the line containing the start of
969 * the comment
970 */
971 curwin->w_cursor.col = (colnr_T)(p - ptr);
972 if ((pos = findmatch(NULL, NUL)) != NULL)
973 {
974 curwin->w_cursor.lnum = pos->lnum;
975 newindent = get_indent();
976 }
977 }
978 }
979 }
980 }
981 else /* Not a comment line */
982# endif
983 {
984 /* Find last non-blank in line */
985 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100986 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 --p;
988 last_char = *p;
989
990 /*
991 * find the character just before the '{' or ';'
992 */
993 if (last_char == '{' || last_char == ';')
994 {
995 if (p > ptr)
996 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100997 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 --p;
999 }
1000 /*
1001 * Try to catch lines that are split over multiple
1002 * lines. eg:
1003 * if (condition &&
1004 * condition) {
1005 * Should line up here!
1006 * }
1007 */
1008 if (*p == ')')
1009 {
1010 curwin->w_cursor.col = (colnr_T)(p - ptr);
1011 if ((pos = findmatch(NULL, '(')) != NULL)
1012 {
1013 curwin->w_cursor.lnum = pos->lnum;
1014 newindent = get_indent();
1015 ptr = ml_get_curline();
1016 }
1017 }
1018 /*
1019 * If last character is '{' do indent, without
1020 * checking for "if" and the like.
1021 */
1022 if (last_char == '{')
1023 {
1024 did_si = TRUE; /* do indent */
1025 no_si = TRUE; /* don't delete it when '{' typed */
1026 }
1027 /*
1028 * Look for "if" and the like, use 'cinwords'.
1029 * Don't do this if the previous line ended in ';' or
1030 * '}'.
1031 */
1032 else if (last_char != ';' && last_char != '}'
1033 && cin_is_cinword(ptr))
1034 did_si = TRUE;
1035 }
1036 }
1037 else /* dir == BACKWARD */
1038 {
1039 /*
1040 * Skip preprocessor directives, unless they are
1041 * recognised as comments.
1042 */
1043 if (
1044# ifdef FEAT_COMMENTS
1045 lead_len == 0 &&
1046# endif
1047 ptr[0] == '#')
1048 {
1049 int was_backslashed = FALSE;
1050
1051 while ((ptr[0] == '#' || was_backslashed) &&
1052 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1053 {
1054 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1055 was_backslashed = TRUE;
1056 else
1057 was_backslashed = FALSE;
1058 ptr = ml_get(++curwin->w_cursor.lnum);
1059 }
1060 if (was_backslashed)
1061 newindent = 0; /* Got to end of file */
1062 else
1063 newindent = get_indent();
1064 }
1065 p = skipwhite(ptr);
1066 if (*p == '}') /* if line starts with '}': do indent */
1067 did_si = TRUE;
1068 else /* can delete indent when '{' typed */
1069 can_si_back = TRUE;
1070 }
1071 curwin->w_cursor = old_cursor;
1072 }
1073 if (do_si)
1074 can_si = TRUE;
1075#endif /* FEAT_SMARTINDENT */
1076
1077 did_ai = TRUE;
1078 }
1079
1080#ifdef FEAT_COMMENTS
1081 /*
1082 * Find out if the current line starts with a comment leader.
1083 * This may then be inserted in front of the new line.
1084 */
1085 end_comment_pending = NUL;
1086 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001087 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 else
1089 lead_len = 0;
1090 if (lead_len > 0)
1091 {
1092 char_u *lead_repl = NULL; /* replaces comment leader */
1093 int lead_repl_len = 0; /* length of *lead_repl */
1094 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1095 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1096 char_u *comment_end = NULL; /* where lead_end has been found */
1097 int extra_space = FALSE; /* append extra space */
1098 int current_flag;
1099 int require_blank = FALSE; /* requires blank after middle */
1100 char_u *p2;
1101
1102 /*
1103 * If the comment leader has the start, middle or end flag, it may not
1104 * be used or may be replaced with the middle leader.
1105 */
1106 for (p = lead_flags; *p && *p != ':'; ++p)
1107 {
1108 if (*p == COM_BLANK)
1109 {
1110 require_blank = TRUE;
1111 continue;
1112 }
1113 if (*p == COM_START || *p == COM_MIDDLE)
1114 {
1115 current_flag = *p;
1116 if (*p == COM_START)
1117 {
1118 /*
1119 * Doing "O" on a start of comment does not insert leader.
1120 */
1121 if (dir == BACKWARD)
1122 {
1123 lead_len = 0;
1124 break;
1125 }
1126
1127 /* find start of middle part */
1128 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1129 require_blank = FALSE;
1130 }
1131
1132 /*
1133 * Isolate the strings of the middle and end leader.
1134 */
1135 while (*p && p[-1] != ':') /* find end of middle flags */
1136 {
1137 if (*p == COM_BLANK)
1138 require_blank = TRUE;
1139 ++p;
1140 }
1141 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1142
1143 while (*p && p[-1] != ':') /* find end of end flags */
1144 {
1145 /* Check whether we allow automatic ending of comments */
1146 if (*p == COM_AUTO_END)
1147 end_comment_pending = -1; /* means we want to set it */
1148 ++p;
1149 }
1150 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1151
1152 if (end_comment_pending == -1) /* we can set it now */
1153 end_comment_pending = lead_end[n - 1];
1154
1155 /*
1156 * If the end of the comment is in the same line, don't use
1157 * the comment leader.
1158 */
1159 if (dir == FORWARD)
1160 {
1161 for (p = saved_line + lead_len; *p; ++p)
1162 if (STRNCMP(p, lead_end, n) == 0)
1163 {
1164 comment_end = p;
1165 lead_len = 0;
1166 break;
1167 }
1168 }
1169
1170 /*
1171 * Doing "o" on a start of comment inserts the middle leader.
1172 */
1173 if (lead_len > 0)
1174 {
1175 if (current_flag == COM_START)
1176 {
1177 lead_repl = lead_middle;
1178 lead_repl_len = (int)STRLEN(lead_middle);
1179 }
1180
1181 /*
1182 * If we have hit RETURN immediately after the start
1183 * comment leader, then put a space after the middle
1184 * comment leader on the next line.
1185 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001186 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && ((p_extra != NULL
1188 && (int)curwin->w_cursor.col == lead_len)
1189 || (p_extra == NULL
1190 && saved_line[lead_len] == NUL)
1191 || require_blank))
1192 extra_space = TRUE;
1193 }
1194 break;
1195 }
1196 if (*p == COM_END)
1197 {
1198 /*
1199 * Doing "o" on the end of a comment does not insert leader.
1200 * Remember where the end is, might want to use it to find the
1201 * start (for C-comments).
1202 */
1203 if (dir == FORWARD)
1204 {
1205 comment_end = skipwhite(saved_line);
1206 lead_len = 0;
1207 break;
1208 }
1209
1210 /*
1211 * Doing "O" on the end of a comment inserts the middle leader.
1212 * Find the string for the middle leader, searching backwards.
1213 */
1214 while (p > curbuf->b_p_com && *p != ',')
1215 --p;
1216 for (lead_repl = p; lead_repl > curbuf->b_p_com
1217 && lead_repl[-1] != ':'; --lead_repl)
1218 ;
1219 lead_repl_len = (int)(p - lead_repl);
1220
1221 /* We can probably always add an extra space when doing "O" on
1222 * the comment-end */
1223 extra_space = TRUE;
1224
1225 /* Check whether we allow automatic ending of comments */
1226 for (p2 = p; *p2 && *p2 != ':'; p2++)
1227 {
1228 if (*p2 == COM_AUTO_END)
1229 end_comment_pending = -1; /* means we want to set it */
1230 }
1231 if (end_comment_pending == -1)
1232 {
1233 /* Find last character in end-comment string */
1234 while (*p2 && *p2 != ',')
1235 p2++;
1236 end_comment_pending = p2[-1];
1237 }
1238 break;
1239 }
1240 if (*p == COM_FIRST)
1241 {
1242 /*
1243 * Comment leader for first line only: Don't repeat leader
1244 * when using "O", blank out leader when using "o".
1245 */
1246 if (dir == BACKWARD)
1247 lead_len = 0;
1248 else
1249 {
1250 lead_repl = (char_u *)"";
1251 lead_repl_len = 0;
1252 }
1253 break;
1254 }
1255 }
1256 if (lead_len)
1257 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001258 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001259 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001260 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 allocated = leader; /* remember to free it later */
1262
1263 if (leader == NULL)
1264 lead_len = 0;
1265 else
1266 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001267 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 /*
1270 * Replace leader with lead_repl, right or left adjusted
1271 */
1272 if (lead_repl != NULL)
1273 {
1274 int c = 0;
1275 int off = 0;
1276
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001277 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001280 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 else if (VIM_ISDIGIT(*p) || *p == '-')
1282 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001283 else
1284 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 if (c == COM_RIGHT) /* right adjusted leader */
1287 {
1288 /* find last non-white in the leader to line up with */
1289 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001290 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001293
1294#ifdef FEAT_MBYTE
1295 /* Compute the length of the replaced characters in
1296 * screen characters, not bytes. */
1297 {
1298 int repl_size = vim_strnsize(lead_repl,
1299 lead_repl_len);
1300 int old_size = 0;
1301 char_u *endp = p;
1302 int l;
1303
1304 while (old_size < repl_size && p > leader)
1305 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001306 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001307 old_size += ptr2cells(p);
1308 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001309 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001310 if (l != 0)
1311 mch_memmove(endp + l, endp,
1312 (size_t)((leader + lead_len) - endp));
1313 lead_len += l;
1314 }
1315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (p < leader + lead_repl_len)
1317 p = leader;
1318 else
1319 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1322 if (p + lead_repl_len > leader + lead_len)
1323 p[lead_repl_len] = NUL;
1324
1325 /* blank-out any other chars from the old leader. */
1326 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001327 {
1328#ifdef FEAT_MBYTE
1329 int l = mb_head_off(leader, p);
1330
1331 if (l > 1)
1332 {
1333 p -= l;
1334 if (ptr2cells(p) > 1)
1335 {
1336 p[1] = ' ';
1337 --l;
1338 }
1339 mch_memmove(p + 1, p + l + 1,
1340 (size_t)((leader + lead_len) - (p + l + 1)));
1341 lead_len -= l;
1342 *p = ' ';
1343 }
1344 else
1345#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001346 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 else /* left adjusted leader */
1351 {
1352 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001353#ifdef FEAT_MBYTE
1354 /* Compute the length of the replaced characters in
1355 * screen characters, not bytes. Move the part that is
1356 * not to be overwritten. */
1357 {
1358 int repl_size = vim_strnsize(lead_repl,
1359 lead_repl_len);
1360 int i;
1361 int l;
1362
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001363 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001365 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001366 if (vim_strnsize(p, i + l) > repl_size)
1367 break;
1368 }
1369 if (i != lead_repl_len)
1370 {
1371 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001372 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001373 lead_len += lead_repl_len - i;
1374 }
1375 }
1376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1378
1379 /* Replace any remaining non-white chars in the old
1380 * leader by spaces. Keep Tabs, the indent must
1381 * remain the same. */
1382 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001383 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
1385 /* Don't put a space before a TAB. */
1386 if (p + 1 < leader + lead_len && p[1] == TAB)
1387 {
1388 --lead_len;
1389 mch_memmove(p, p + 1,
1390 (leader + lead_len) - p);
1391 }
1392 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001393 {
1394#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001395 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001396
1397 if (l > 1)
1398 {
1399 if (ptr2cells(p) > 1)
1400 {
1401 /* Replace a double-wide char with
1402 * two spaces */
1403 --l;
1404 *p++ = ' ';
1405 }
1406 mch_memmove(p + 1, p + l,
1407 (leader + lead_len) - p);
1408 lead_len -= l - 1;
1409 }
1410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414 *p = NUL;
1415 }
1416
1417 /* Recompute the indent, it may have changed. */
1418 if (curbuf->b_p_ai
1419#ifdef FEAT_SMARTINDENT
1420 || do_si
1421#endif
1422 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001423#ifdef FEAT_VARTABS
1424 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1425 curbuf->b_p_vts_array, FALSE);
1426#else
1427 newindent = get_indent_str(leader,
1428 (int)curbuf->b_p_ts, FALSE);
1429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 /* Add the indent offset */
1432 if (newindent + off < 0)
1433 {
1434 off = -newindent;
1435 newindent = 0;
1436 }
1437 else
1438 newindent += off;
1439
1440 /* Correct trailing spaces for the shift, so that
1441 * alignment remains equal. */
1442 while (off > 0 && lead_len > 0
1443 && leader[lead_len - 1] == ' ')
1444 {
1445 /* Don't do it when there is a tab before the space */
1446 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1447 break;
1448 --lead_len;
1449 --off;
1450 }
1451
1452 /* If the leader ends in white space, don't add an
1453 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001454 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 extra_space = FALSE;
1456 leader[lead_len] = NUL;
1457 }
1458
1459 if (extra_space)
1460 {
1461 leader[lead_len++] = ' ';
1462 leader[lead_len] = NUL;
1463 }
1464
1465 newcol = lead_len;
1466
1467 /*
1468 * if a new indent will be set below, remove the indent that
1469 * is in the comment leader
1470 */
1471 if (newindent
1472#ifdef FEAT_SMARTINDENT
1473 || did_si
1474#endif
1475 )
1476 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001477 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 --lead_len;
1480 --newcol;
1481 ++leader;
1482 }
1483 }
1484
1485 }
1486#ifdef FEAT_SMARTINDENT
1487 did_si = can_si = FALSE;
1488#endif
1489 }
1490 else if (comment_end != NULL)
1491 {
1492 /*
1493 * We have finished a comment, so we don't use the leader.
1494 * If this was a C-comment and 'ai' or 'si' is set do a normal
1495 * indent to align with the line containing the start of the
1496 * comment.
1497 */
1498 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1499 (curbuf->b_p_ai
1500#ifdef FEAT_SMARTINDENT
1501 || do_si
1502#endif
1503 ))
1504 {
1505 old_cursor = curwin->w_cursor;
1506 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1507 if ((pos = findmatch(NULL, NUL)) != NULL)
1508 {
1509 curwin->w_cursor.lnum = pos->lnum;
1510 newindent = get_indent();
1511 }
1512 curwin->w_cursor = old_cursor;
1513 }
1514 }
1515 }
1516#endif
1517
1518 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1519 if (p_extra != NULL)
1520 {
1521 *p_extra = saved_char; /* restore char that NUL replaced */
1522
1523 /*
1524 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1525 * non-blank.
1526 *
1527 * When in REPLACE mode, put the deleted blanks on the replace stack,
1528 * preceded by a NUL, so they can be put back when a BS is entered.
1529 */
1530 if (REPLACE_NORMAL(State))
1531 replace_push(NUL); /* end of extra blanks */
1532 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1533 {
1534 while ((*p_extra == ' ' || *p_extra == '\t')
1535#ifdef FEAT_MBYTE
1536 && (!enc_utf8
1537 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1538#endif
1539 )
1540 {
1541 if (REPLACE_NORMAL(State))
1542 replace_push(*p_extra);
1543 ++p_extra;
1544 ++less_cols_off;
1545 }
1546 }
1547 if (*p_extra != NUL)
1548 did_ai = FALSE; /* append some text, don't truncate now */
1549
1550 /* columns for marks adjusted for removed columns */
1551 less_cols = (int)(p_extra - saved_line);
1552 }
1553
1554 if (p_extra == NULL)
1555 p_extra = (char_u *)""; /* append empty line */
1556
1557#ifdef FEAT_COMMENTS
1558 /* concatenate leader and p_extra, if there is a leader */
1559 if (lead_len)
1560 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001561 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1562 {
1563 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001564 int padding = second_line_indent
1565 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001566
1567 /* Here whitespace is inserted after the comment char.
1568 * Below, set_indent(newindent, SIN_INSERT) will insert the
1569 * whitespace needed before the comment char. */
1570 for (i = 0; i < padding; i++)
1571 {
1572 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001573 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001574 newcol++;
1575 }
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 STRCAT(leader, p_extra);
1578 p_extra = leader;
1579 did_ai = TRUE; /* So truncating blanks works with comments */
1580 less_cols -= lead_len;
1581 }
1582 else
1583 end_comment_pending = NUL; /* turns out there was no leader */
1584#endif
1585
1586 old_cursor = curwin->w_cursor;
1587 if (dir == BACKWARD)
1588 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1592 == FAIL)
1593 goto theend;
1594 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001595 * with markers.
1596 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001597 * be marks there. But still needed in diff mode. */
1598 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1599#ifdef FEAT_DIFF
1600 || curwin->w_p_diff
1601#endif
1602 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001603 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 did_append = TRUE;
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 else
1607 {
1608 /*
1609 * In VREPLACE mode we are starting to replace the next line.
1610 */
1611 curwin->w_cursor.lnum++;
1612 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1613 {
1614 /* In case we NL to a new line, BS to the previous one, and NL
1615 * again, we don't want to save the new line for undo twice.
1616 */
1617 (void)u_save_cursor(); /* errors are ignored! */
1618 vr_lines_changed++;
1619 }
1620 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1621 changed_bytes(curwin->w_cursor.lnum, 0);
1622 curwin->w_cursor.lnum--;
1623 did_append = FALSE;
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 if (newindent
1627#ifdef FEAT_SMARTINDENT
1628 || did_si
1629#endif
1630 )
1631 {
1632 ++curwin->w_cursor.lnum;
1633#ifdef FEAT_SMARTINDENT
1634 if (did_si)
1635 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001636 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001639 newindent -= newindent % sw;
1640 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001643 /* Copy the indent */
1644 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 (void)copy_indent(newindent, saved_line);
1647
1648 /*
1649 * Set the 'preserveindent' option so that any further screwing
1650 * with the line doesn't entirely destroy our efforts to preserve
1651 * it. It gets restored at the function end.
1652 */
1653 curbuf->b_p_pi = TRUE;
1654 }
1655 else
1656 (void)set_indent(newindent, SIN_INSERT);
1657 less_cols -= curwin->w_cursor.col;
1658
1659 ai_col = curwin->w_cursor.col;
1660
1661 /*
1662 * In REPLACE mode, for each character in the new indent, there must
1663 * be a NUL on the replace stack, for when it is deleted with BS
1664 */
1665 if (REPLACE_NORMAL(State))
1666 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1667 replace_push(NUL);
1668 newcol += curwin->w_cursor.col;
1669#ifdef FEAT_SMARTINDENT
1670 if (no_si)
1671 did_si = FALSE;
1672#endif
1673 }
1674
1675#ifdef FEAT_COMMENTS
1676 /*
1677 * In REPLACE mode, for each character in the extra leader, there must be
1678 * a NUL on the replace stack, for when it is deleted with BS.
1679 */
1680 if (REPLACE_NORMAL(State))
1681 while (lead_len-- > 0)
1682 replace_push(NUL);
1683#endif
1684
1685 curwin->w_cursor = old_cursor;
1686
1687 if (dir == FORWARD)
1688 {
1689 if (trunc_line || (State & INSERT))
1690 {
1691 /* truncate current line at cursor */
1692 saved_line[curwin->w_cursor.col] = NUL;
1693 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1694 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1695 truncate_spaces(saved_line);
1696 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1697 saved_line = NULL;
1698 if (did_append)
1699 {
1700 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1701 curwin->w_cursor.lnum + 1, 1L);
1702 did_append = FALSE;
1703
1704 /* Move marks after the line break to the new line. */
1705 if (flags & OPENLINE_MARKFIX)
1706 mark_col_adjust(curwin->w_cursor.lnum,
1707 curwin->w_cursor.col + less_cols_off,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001708 1L, (long)-less_cols, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 else
1711 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1712 }
1713
1714 /*
1715 * Put the cursor on the new line. Careful: the scrollup() above may
1716 * have moved w_cursor, we must use old_cursor.
1717 */
1718 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1719 }
1720 if (did_append)
1721 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1722
1723 curwin->w_cursor.col = newcol;
1724#ifdef FEAT_VIRTUALEDIT
1725 curwin->w_cursor.coladd = 0;
1726#endif
1727
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001728#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /*
1730 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1731 * fixthisline() from doing it (via change_indent()) by telling it we're in
1732 * normal INSERT mode.
1733 */
1734 if (State & VREPLACE_FLAG)
1735 {
1736 vreplace_mode = State; /* So we know to put things right later */
1737 State = INSERT;
1738 }
1739 else
1740 vreplace_mode = 0;
1741#endif
1742#ifdef FEAT_LISP
1743 /*
1744 * May do lisp indenting.
1745 */
1746 if (!p_paste
1747# ifdef FEAT_COMMENTS
1748 && leader == NULL
1749# endif
1750 && curbuf->b_p_lisp
1751 && curbuf->b_p_ai)
1752 {
1753 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001754 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756#endif
1757#ifdef FEAT_CINDENT
1758 /*
1759 * May do indenting after opening a new line.
1760 */
1761 if (!p_paste
1762 && (curbuf->b_p_cin
1763# ifdef FEAT_EVAL
1764 || *curbuf->b_p_inde != NUL
1765# endif
1766 )
1767 && in_cinkeys(dir == FORWARD
1768 ? KEY_OPEN_FORW
1769 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1770 {
1771 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001772 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
1774#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001775#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (vreplace_mode != 0)
1777 State = vreplace_mode;
1778#endif
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 /*
1781 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1782 * original line, and inserts the new stuff char by char, pushing old stuff
1783 * onto the replace stack (via ins_char()).
1784 */
1785 if (State & VREPLACE_FLAG)
1786 {
1787 /* Put new line in p_extra */
1788 p_extra = vim_strsave(ml_get_curline());
1789 if (p_extra == NULL)
1790 goto theend;
1791
1792 /* Put back original line */
1793 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1794
1795 /* Insert new stuff into line again */
1796 curwin->w_cursor.col = 0;
1797#ifdef FEAT_VIRTUALEDIT
1798 curwin->w_cursor.coladd = 0;
1799#endif
1800 ins_bytes(p_extra); /* will call changed_bytes() */
1801 vim_free(p_extra);
1802 next_line = NULL;
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001805 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806theend:
1807 curbuf->b_p_pi = saved_pi;
1808 vim_free(saved_line);
1809 vim_free(next_line);
1810 vim_free(allocated);
1811 return retval;
1812}
1813
1814#if defined(FEAT_COMMENTS) || defined(PROTO)
1815/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001816 * get_leader_len() returns the length in bytes of the prefix of the given
1817 * string which introduces a comment. If this string is not a comment then
1818 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 * When "flags" is not NULL, it is set to point to the flags of the recognized
1820 * comment leader.
1821 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001822 * If "include_space" is set, include trailing whitespace while calculating the
1823 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826get_leader_len(
1827 char_u *line,
1828 char_u **flags,
1829 int backward,
1830 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001833 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int got_com = FALSE;
1835 int found_one;
1836 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1837 char_u *string; /* pointer to comment string */
1838 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001839 int middle_match_len = 0;
1840 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001841 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar81340392012-06-06 16:12:59 +02001843 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001844 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 ++i;
1846
1847 /*
1848 * Repeat to match several nested comment strings.
1849 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001850 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 /*
1853 * scan through the 'comments' option for a match
1854 */
1855 found_one = FALSE;
1856 for (list = curbuf->b_p_com; *list; )
1857 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001858 /* Get one option part into part_buf[]. Advance "list" to next
1859 * one. Put "string" at start of string. */
1860 if (!got_com && flags != NULL)
1861 *flags = list; /* remember where flags started */
1862 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1864 string = vim_strchr(part_buf, ':');
1865 if (string == NULL) /* missing ':', ignore this part */
1866 continue;
1867 *string++ = NUL; /* isolate flags from string */
1868
Bram Moolenaara4271d52011-05-10 13:38:27 +02001869 /* If we found a middle match previously, use that match when this
1870 * is not a middle or end. */
1871 if (middle_match_len != 0
1872 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1873 && vim_strchr(part_buf, COM_END) == NULL)
1874 break;
1875
1876 /* When we already found a nested comment, only accept further
1877 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1879 continue;
1880
Bram Moolenaara4271d52011-05-10 13:38:27 +02001881 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1883 continue;
1884
Bram Moolenaara4271d52011-05-10 13:38:27 +02001885 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * When string starts with white space, must have some white space
1887 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001888 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001889 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001891 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001892 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001893 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 ++string;
1895 }
1896 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1897 ;
1898 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001899 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaara4271d52011-05-10 13:38:27 +02001901 /* When 'b' flag used, there must be white space or an
1902 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001904 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 continue;
1906
Bram Moolenaara4271d52011-05-10 13:38:27 +02001907 /* We have found a match, stop searching unless this is a middle
1908 * comment. The middle comment can be a substring of the end
1909 * comment in which case it's better to return the length of the
1910 * end comment and its flags. Thus we keep searching with middle
1911 * and end matches and use an end match if it matches better. */
1912 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1913 {
1914 if (middle_match_len == 0)
1915 {
1916 middle_match_len = j;
1917 saved_flags = prev_list;
1918 }
1919 continue;
1920 }
1921 if (middle_match_len != 0 && j > middle_match_len)
1922 /* Use this match instead of the middle match, since it's a
1923 * longer thus better match. */
1924 middle_match_len = 0;
1925
1926 if (middle_match_len == 0)
1927 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 found_one = TRUE;
1929 break;
1930 }
1931
Bram Moolenaara4271d52011-05-10 13:38:27 +02001932 if (middle_match_len != 0)
1933 {
1934 /* Use the previously found middle match after failing to find a
1935 * match with an end. */
1936 if (!got_com && flags != NULL)
1937 *flags = saved_flags;
1938 i += middle_match_len;
1939 found_one = TRUE;
1940 }
1941
1942 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (!found_one)
1944 break;
1945
Bram Moolenaar81340392012-06-06 16:12:59 +02001946 result = i;
1947
Bram Moolenaara4271d52011-05-10 13:38:27 +02001948 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001949 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 ++i;
1951
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 if (include_space)
1953 result = i;
1954
Bram Moolenaara4271d52011-05-10 13:38:27 +02001955 /* If this comment doesn't nest, stop here. */
1956 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (vim_strchr(part_buf, COM_NEST) == NULL)
1958 break;
1959 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001960 return result;
1961}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001962
Bram Moolenaar81340392012-06-06 16:12:59 +02001963/*
1964 * Return the offset at which the last comment in line starts. If there is no
1965 * comment in the whole line, -1 is returned.
1966 *
1967 * When "flags" is not null, it is set to point to the flags describing the
1968 * recognized comment leader.
1969 */
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001972{
1973 int result = -1;
1974 int i, j;
1975 int lower_check_bound = 0;
1976 char_u *string;
1977 char_u *com_leader;
1978 char_u *com_flags;
1979 char_u *list;
1980 int found_one;
1981 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1982
1983 /*
1984 * Repeat to match several nested comment strings.
1985 */
1986 i = (int)STRLEN(line);
1987 while (--i >= lower_check_bound)
1988 {
1989 /*
1990 * scan through the 'comments' option for a match
1991 */
1992 found_one = FALSE;
1993 for (list = curbuf->b_p_com; *list; )
1994 {
1995 char_u *flags_save = list;
1996
1997 /*
1998 * Get one option part into part_buf[]. Advance list to next one.
1999 * put string at start of string.
2000 */
2001 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2002 string = vim_strchr(part_buf, ':');
2003 if (string == NULL) /* If everything is fine, this cannot actually
2004 * happen. */
2005 {
2006 continue;
2007 }
2008 *string++ = NUL; /* Isolate flags from string. */
2009 com_leader = string;
2010
2011 /*
2012 * Line contents and string must match.
2013 * When string starts with white space, must have some white space
2014 * (but the amount does not need to match, there might be a mix of
2015 * TABs and spaces).
2016 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002017 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002018 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002019 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002020 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002021 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002022 ++string;
2023 }
2024 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2025 /* do nothing */;
2026 if (string[j] != NUL)
2027 continue;
2028
2029 /*
2030 * When 'b' flag used, there must be white space or an
2031 * end-of-line after the string in the line.
2032 */
2033 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002034 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002035 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002036
Bram Moolenaar4af72592018-12-09 15:00:52 +01002037 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +01002038 {
Bram Moolenaar4af72592018-12-09 15:00:52 +01002039 // For a middlepart comment, only consider it to match if
2040 // everything before the current position in the line is
2041 // whitespace. Otherwise we would think we are inside a
2042 // comment if the middle part appears somewhere in the middle
2043 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +01002044 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
2045 ;
2046 if (j < i)
2047 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02002048 }
2049
2050 /*
2051 * We have found a match, stop searching.
2052 */
2053 found_one = TRUE;
2054
2055 if (flags)
2056 *flags = flags_save;
2057 com_flags = flags_save;
2058
2059 break;
2060 }
2061
2062 if (found_one)
2063 {
2064 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2065 int len1, len2, off;
2066
2067 result = i;
2068 /*
2069 * If this comment nests, continue searching.
2070 */
2071 if (vim_strchr(part_buf, COM_NEST) != NULL)
2072 continue;
2073
2074 lower_check_bound = i;
2075
2076 /* Let's verify whether the comment leader found is a substring
2077 * of other comment leaders. If it is, let's adjust the
2078 * lower_check_bound so that we make sure that we have determined
2079 * the comment leader correctly.
2080 */
2081
Bram Moolenaar1c465442017-03-12 20:10:05 +01002082 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002083 ++com_leader;
2084 len1 = (int)STRLEN(com_leader);
2085
2086 for (list = curbuf->b_p_com; *list; )
2087 {
2088 char_u *flags_save = list;
2089
2090 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2091 if (flags_save == com_flags)
2092 continue;
2093 string = vim_strchr(part_buf2, ':');
2094 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002095 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002096 ++string;
2097 len2 = (int)STRLEN(string);
2098 if (len2 == 0)
2099 continue;
2100
2101 /* Now we have to verify whether string ends with a substring
2102 * beginning the com_leader. */
2103 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2104 {
2105 --off;
2106 if (!STRNCMP(string + off, com_leader, len2 - off))
2107 {
2108 if (i - off < lower_check_bound)
2109 lower_check_bound = i - off;
2110 }
2111 }
2112 }
2113 }
2114 }
2115 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116}
2117#endif
2118
2119/*
2120 * Return the number of window lines occupied by buffer line "lnum".
2121 */
2122 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 return plines_win(curwin, lnum, TRUE);
2126}
2127
2128 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002129plines_win(
2130 win_T *wp,
2131 linenr_T lnum,
2132 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134#if defined(FEAT_DIFF) || defined(PROTO)
2135 /* Check for filler lines above this buffer line. When folded the result
2136 * is one line anyway. */
2137 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2138}
2139
2140 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002141plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 return plines_win_nofill(curwin, lnum, TRUE);
2144}
2145
2146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147plines_win_nofill(
2148 win_T *wp,
2149 linenr_T lnum,
2150 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
2152#endif
2153 int lines;
2154
2155 if (!wp->w_p_wrap)
2156 return 1;
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (wp->w_width == 0)
2159 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161#ifdef FEAT_FOLDING
2162 /* A folded lines is handled just like an empty line. */
2163 /* NOTE: Caller must handle lines that are MAYBE folded. */
2164 if (lineFolded(wp, lnum) == TRUE)
2165 return 1;
2166#endif
2167
2168 lines = plines_win_nofold(wp, lnum);
2169 if (winheight > 0 && lines > wp->w_height)
2170 return (int)wp->w_height;
2171 return lines;
2172}
2173
2174/*
2175 * Return number of window lines physical line "lnum" will occupy in window
2176 * "wp". Does not care about folding, 'wrap' or 'diff'.
2177 */
2178 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
2181 char_u *s;
2182 long col;
2183 int width;
2184
2185 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2186 if (*s == NUL) /* empty line */
2187 return 1;
2188 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2189
2190 /*
2191 * If list mode is on, then the '$' at the end of the line may take up one
2192 * extra column.
2193 */
2194 if (wp->w_p_list && lcs_eol != NUL)
2195 col += 1;
2196
2197 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002198 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002200 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (width <= 0)
2202 return 32000;
2203 if (col <= width)
2204 return 1;
2205 col -= width;
2206 width += win_col_off2(wp);
2207 return (col + (width - 1)) / width + 1;
2208}
2209
2210/*
2211 * Like plines_win(), but only reports the number of physical screen lines
2212 * used from the start of the line to the given column number.
2213 */
2214 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002215plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
2217 long col;
2218 char_u *s;
2219 int lines = 0;
2220 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002221 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223#ifdef FEAT_DIFF
2224 /* Check for filler lines above this buffer line. When folded the result
2225 * is one line anyway. */
2226 lines = diff_check_fill(wp, lnum);
2227#endif
2228
2229 if (!wp->w_p_wrap)
2230 return lines + 1;
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (wp->w_width == 0)
2233 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
Bram Moolenaar597a4222014-06-25 14:39:50 +02002235 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 col = 0;
2238 while (*s != NUL && --column >= 0)
2239 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002240 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002241 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243
2244 /*
2245 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2246 * INSERT mode, then col must be adjusted so that it represents the last
2247 * screen position of the TAB. This only fixes an error when the TAB wraps
2248 * from one screen line to the next (when 'columns' is not a multiple of
2249 * 'ts') -- webb.
2250 */
2251 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002252 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002255 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002257 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002258 if (width <= 0)
2259 return 9999;
2260
2261 lines += 1;
2262 if (col > width)
2263 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2264 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265}
2266
2267 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002268plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270 int count = 0;
2271
2272 while (first <= last)
2273 {
2274#ifdef FEAT_FOLDING
2275 int x;
2276
2277 /* Check if there are any really folded lines, but also included lines
2278 * that are maybe folded. */
2279 x = foldedCount(wp, first, NULL);
2280 if (x > 0)
2281 {
2282 ++count; /* count 1 for "+-- folded" line */
2283 first += x;
2284 }
2285 else
2286#endif
2287 {
2288#ifdef FEAT_DIFF
2289 if (first == wp->w_topline)
2290 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2291 else
2292#endif
2293 count += plines_win(wp, first, TRUE);
2294 ++first;
2295 }
2296 }
2297 return (count);
2298}
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300/*
2301 * Insert string "p" at the cursor position. Stops at a NUL byte.
2302 * Handles Replace mode and multi-byte characters.
2303 */
2304 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002305ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 ins_bytes_len(p, (int)STRLEN(p));
2308}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310/*
2311 * Insert string "p" with length "len" at the cursor position.
2312 * Handles Replace mode and multi-byte characters.
2313 */
2314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002315ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002318#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 int n;
2320
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002321 if (has_mbyte)
2322 for (i = 0; i < len; i += n)
2323 {
2324 if (enc_utf8)
2325 /* avoid reading past p[len] */
2326 n = utfc_ptr2len_len(p + i, len - i);
2327 else
2328 n = (*mb_ptr2len)(p + i);
2329 ins_char_bytes(p + i, n);
2330 }
2331 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002332#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002333 for (i = 0; i < len; ++i)
2334 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337/*
2338 * Insert or replace a single character at the cursor position.
2339 * When in REPLACE or VREPLACE mode, replace any existing character.
2340 * Caller must have prepared for undo.
2341 * For multi-byte characters we get the whole character, the caller must
2342 * convert bytes to a character.
2343 */
2344 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002345ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002347 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002348 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002350#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 n = (*mb_char2bytes)(c, buf);
2352
2353 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2354 * Happens for CTRL-Vu9900. */
2355 if (buf[0] == 0)
2356 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002357#else
2358 buf[0] = c;
2359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 ins_char_bytes(buf, n);
2362}
2363
2364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002365ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 int newlen; /* nr of bytes inserted */
2369 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2370 char_u *p;
2371 char_u *newp;
2372 char_u *oldp;
2373 int linelen; /* length of old line including NUL */
2374 colnr_T col;
2375 linenr_T lnum = curwin->w_cursor.lnum;
2376 int i;
2377
2378#ifdef FEAT_VIRTUALEDIT
2379 /* Break tabs if needed. */
2380 if (virtual_active() && curwin->w_cursor.coladd > 0)
2381 coladvance_force(getviscol());
2382#endif
2383
2384 col = curwin->w_cursor.col;
2385 oldp = ml_get(lnum);
2386 linelen = (int)STRLEN(oldp) + 1;
2387
2388 /* The lengths default to the values for when not replacing. */
2389 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 if (State & REPLACE_FLAG)
2393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (State & VREPLACE_FLAG)
2395 {
2396 colnr_T new_vcol = 0; /* init for GCC */
2397 colnr_T vcol;
2398 int old_list;
2399#ifndef FEAT_MBYTE
2400 char_u buf[2];
2401#endif
2402
2403 /*
2404 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2405 * Returns the old value of list, so when finished,
2406 * curwin->w_p_list should be set back to this.
2407 */
2408 old_list = curwin->w_p_list;
2409 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2410 curwin->w_p_list = FALSE;
2411
2412 /*
2413 * In virtual replace mode each character may replace one or more
2414 * characters (zero if it's a TAB). Count the number of bytes to
2415 * be deleted to make room for the new character, counting screen
2416 * cells. May result in adding spaces to fill a gap.
2417 */
2418 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2419#ifndef FEAT_MBYTE
2420 buf[0] = c;
2421 buf[1] = NUL;
2422#endif
2423 new_vcol = vcol + chartabsize(buf, vcol);
2424 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2425 {
2426 vcol += chartabsize(oldp + col + oldlen, vcol);
2427 /* Don't need to remove a TAB that takes us to the right
2428 * position. */
2429 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2430 break;
2431#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002432 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#else
2434 ++oldlen;
2435#endif
2436 /* Deleted a bit too much, insert spaces. */
2437 if (vcol > new_vcol)
2438 newlen += vcol - new_vcol;
2439 }
2440 curwin->w_p_list = old_list;
2441 }
2442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (oldp[col] != NUL)
2444 {
2445 /* normal replace */
2446#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448#else
2449 oldlen = 1;
2450#endif
2451 }
2452
2453
2454 /* Push the replaced bytes onto the replace stack, so that they can be
2455 * put back when BS is used. The bytes of a multi-byte character are
2456 * done the other way around, so that the first byte is popped off
2457 * first (it tells the byte length of the character). */
2458 replace_push(NUL);
2459 for (i = 0; i < oldlen; ++i)
2460 {
2461#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002462 if (has_mbyte)
2463 i += replace_push_mb(oldp + col + i) - 1;
2464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002466 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468 }
2469
2470 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2471 if (newp == NULL)
2472 return;
2473
2474 /* Copy bytes before the cursor. */
2475 if (col > 0)
2476 mch_memmove(newp, oldp, (size_t)col);
2477
2478 /* Copy bytes after the changed character(s). */
2479 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002480 if (linelen > col + oldlen)
2481 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 (size_t)(linelen - col - oldlen));
2483
2484 /* Insert or overwrite the new character. */
2485#ifdef FEAT_MBYTE
2486 mch_memmove(p, buf, charlen);
2487 i = charlen;
2488#else
2489 *p = c;
2490 i = 1;
2491#endif
2492
2493 /* Fill with spaces when necessary. */
2494 while (i < newlen)
2495 p[i++] = ' ';
2496
2497 /* Replace the line in the buffer. */
2498 ml_replace(lnum, newp, FALSE);
2499
2500 /* mark the buffer as changed and prepare for displaying */
2501 changed_bytes(lnum, col);
2502
2503 /*
2504 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2505 * show the match for right parens and braces.
2506 */
2507 if (p_sm && (State & INSERT)
2508 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002509#ifdef FEAT_INS_EXPAND
2510 && !ins_compl_active()
2511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002513 {
2514#ifdef FEAT_MBYTE
2515 if (has_mbyte)
2516 showmatch(mb_ptr2char(buf));
2517 else
2518#endif
2519 showmatch(c);
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
2522#ifdef FEAT_RIGHTLEFT
2523 if (!p_ri || (State & REPLACE_FLAG))
2524#endif
2525 {
2526 /* Normal insert: move cursor right */
2527#ifdef FEAT_MBYTE
2528 curwin->w_cursor.col += charlen;
2529#else
2530 ++curwin->w_cursor.col;
2531#endif
2532 }
2533 /*
2534 * TODO: should try to update w_row here, to avoid recomputing it later.
2535 */
2536}
2537
2538/*
2539 * Insert a string at the cursor position.
2540 * Note: Does NOT handle Replace mode.
2541 * Caller must have prepared for undo.
2542 */
2543 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002544ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 char_u *oldp, *newp;
2547 int newlen = (int)STRLEN(s);
2548 int oldlen;
2549 colnr_T col;
2550 linenr_T lnum = curwin->w_cursor.lnum;
2551
2552#ifdef FEAT_VIRTUALEDIT
2553 if (virtual_active() && curwin->w_cursor.coladd > 0)
2554 coladvance_force(getviscol());
2555#endif
2556
2557 col = curwin->w_cursor.col;
2558 oldp = ml_get(lnum);
2559 oldlen = (int)STRLEN(oldp);
2560
2561 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2562 if (newp == NULL)
2563 return;
2564 if (col > 0)
2565 mch_memmove(newp, oldp, (size_t)col);
2566 mch_memmove(newp + col, s, (size_t)newlen);
2567 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2568 ml_replace(lnum, newp, FALSE);
2569 changed_bytes(lnum, col);
2570 curwin->w_cursor.col += newlen;
2571}
2572
2573/*
2574 * Delete one character under the cursor.
2575 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2576 * Caller must have prepared for undo.
2577 *
2578 * return FAIL for failure, OK otherwise
2579 */
2580 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002581del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583#ifdef FEAT_MBYTE
2584 if (has_mbyte)
2585 {
2586 /* Make sure the cursor is at the start of a character. */
2587 mb_adjust_cursor();
2588 if (*ml_get_cursor() == NUL)
2589 return FAIL;
2590 return del_chars(1L, fixpos);
2591 }
2592#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002593 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594}
2595
2596#if defined(FEAT_MBYTE) || defined(PROTO)
2597/*
2598 * Like del_bytes(), but delete characters instead of bytes.
2599 */
2600 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002601del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 long bytes = 0;
2604 long i;
2605 char_u *p;
2606 int l;
2607
2608 p = ml_get_cursor();
2609 for (i = 0; i < count && *p != NUL; ++i)
2610 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002611 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 bytes += l;
2613 p += l;
2614 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002615 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616}
2617#endif
2618
2619/*
2620 * Delete "count" bytes under the cursor.
2621 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2622 * Caller must have prepared for undo.
2623 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002624 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 */
2626 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002627del_bytes(
2628 long count,
2629 int fixpos_arg,
2630 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 char_u *oldp, *newp;
2633 colnr_T oldlen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002634 colnr_T newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 linenr_T lnum = curwin->w_cursor.lnum;
2636 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002637 int alloc_newp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002639 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 oldp = ml_get(lnum);
2642 oldlen = (int)STRLEN(oldp);
2643
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002644 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (col >= oldlen)
2646 return FAIL;
2647
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002648 /* If "count" is zero there is nothing to do. */
2649 if (count == 0)
2650 return OK;
2651
2652 /* If "count" is negative the caller must be doing something wrong. */
2653 if (count < 1)
2654 {
2655 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2656 return FAIL;
2657 }
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#ifdef FEAT_MBYTE
2660 /* If 'delcombine' is set and deleting (less than) one character, only
2661 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002662 if (p_deco && use_delcombine && enc_utf8
2663 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002665 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 int n;
2667
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002668 (void)utfc_ptr2char(oldp + col, cc);
2669 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
2671 /* Find the last composing char, there can be several. */
2672 n = col;
2673 do
2674 {
2675 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002676 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 n += count;
2678 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2679 fixpos = 0;
2680 }
2681 }
2682#endif
2683
2684 /*
2685 * When count is too big, reduce it.
2686 */
2687 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2688 if (movelen <= 1)
2689 {
2690 /*
2691 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002692 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2693 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002695 if (col > 0 && fixpos && restart_edit == 0
2696#ifdef FEAT_VIRTUALEDIT
2697 && (ve_flags & VE_ONEMORE) == 0
2698#endif
2699 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 --curwin->w_cursor.col;
2702#ifdef FEAT_VIRTUALEDIT
2703 curwin->w_cursor.coladd = 0;
2704#endif
2705#ifdef FEAT_MBYTE
2706 if (has_mbyte)
2707 curwin->w_cursor.col -=
2708 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2709#endif
2710 }
2711 count = oldlen - col;
2712 movelen = 1;
2713 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002714 newlen = oldlen - count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 /*
2717 * If the old line has been allocated the deletion can be done in the
2718 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002719 * Can't do this when using Netbeans, because we would need to invoke
2720 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002721 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002724 if (netbeans_active())
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002725 alloc_newp = TRUE;
Bram Moolenaare21877a2008-02-13 09:58:14 +00002726 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002728 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
2729 if (!alloc_newp)
2730 newp = oldp; // use same allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 else
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002732 { // need to allocate a new line
2733 newp = alloc((unsigned)(newlen + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (newp == NULL)
2735 return FAIL;
2736 mch_memmove(newp, oldp, (size_t)col);
2737 }
2738 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002739 if (alloc_newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 ml_replace(lnum, newp, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002741#ifdef FEAT_TEXT_PROP
2742 else
2743 {
2744 // Also move any following text properties.
2745 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
2746 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
2747 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
2748 curbuf->b_ml.ml_line_len -= count;
2749 }
2750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002752 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 changed_bytes(lnum, curwin->w_cursor.col);
2754
2755 return OK;
2756}
2757
2758/*
2759 * Delete from cursor to end of line.
2760 * Caller must have prepared for undo.
2761 *
2762 * return FAIL for failure, OK otherwise
2763 */
2764 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002765truncate_line(
2766 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
2768 char_u *newp;
2769 linenr_T lnum = curwin->w_cursor.lnum;
2770 colnr_T col = curwin->w_cursor.col;
2771
2772 if (col == 0)
2773 newp = vim_strsave((char_u *)"");
2774 else
2775 newp = vim_strnsave(ml_get(lnum), col);
2776
2777 if (newp == NULL)
2778 return FAIL;
2779
2780 ml_replace(lnum, newp, FALSE);
2781
2782 /* mark the buffer as changed and prepare for displaying */
2783 changed_bytes(lnum, curwin->w_cursor.col);
2784
2785 /*
2786 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2787 */
2788 if (fixpos && curwin->w_cursor.col > 0)
2789 --curwin->w_cursor.col;
2790
2791 return OK;
2792}
2793
2794/*
2795 * Delete "nlines" lines at the cursor.
2796 * Saves the lines for undo first if "undo" is TRUE.
2797 */
2798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002799del_lines(
2800 long nlines, /* number of lines to delete */
2801 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
2803 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002804 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806 if (nlines <= 0)
2807 return;
2808
2809 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002810 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 return;
2812
2813 for (n = 0; n < nlines; )
2814 {
2815 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2816 break;
2817
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002818 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 ++n;
2820
2821 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002822 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 break;
2824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002826 /* Correct the cursor position before calling deleted_lines_mark(), it may
2827 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 curwin->w_cursor.col = 0;
2829 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002830
2831 /* adjust marks, mark the buffer as changed and prepare for displaying */
2832 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833}
2834
2835 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002836gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002838 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002840 /* When searching columns is sometimes put at the end of a line. */
2841 if (pos->col == MAXCOL)
2842 return NUL;
2843 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844#ifdef FEAT_MBYTE
2845 if (has_mbyte)
2846 return (*mb_ptr2char)(ptr);
2847#endif
2848 return (int)*ptr;
2849}
2850
2851 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002852gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854#ifdef FEAT_MBYTE
2855 if (has_mbyte)
2856 return (*mb_ptr2char)(ml_get_cursor());
2857#endif
2858 return (int)*ml_get_cursor();
2859}
2860
2861/*
2862 * Write a character at the current cursor position.
2863 * It is directly written into the block.
2864 */
2865 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002866pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2869 + curwin->w_cursor.col) = c;
2870}
2871
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872/*
2873 * When extra == 0: Return TRUE if the cursor is before or on the first
2874 * non-blank in the line.
2875 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2876 * the line.
2877 */
2878 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002879inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 char_u *ptr;
2882 colnr_T col;
2883
Bram Moolenaar1c465442017-03-12 20:10:05 +01002884 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 ++ptr;
2886 if (col >= curwin->w_cursor.col + extra)
2887 return TRUE;
2888 else
2889 return FALSE;
2890}
2891
2892/*
2893 * Skip to next part of an option argument: Skip space and comma.
2894 */
2895 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002896skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
2898 if (*p == ',')
2899 ++p;
2900 while (*p == ' ')
2901 ++p;
2902 return p;
2903}
2904
2905/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002906 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 *
2908 * Most often called through changed_bytes() and changed_lines(), which also
2909 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002910 *
2911 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 */
2913 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002914changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
2916#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002917 if (p_imst == IM_ON_THE_SPOT)
2918 {
2919 /* The text of the preediting area is inserted, but this doesn't
2920 * mean a change of the buffer yet. That is delayed until the
2921 * text is committed. (this means preedit becomes empty) */
2922 if (im_is_preediting() && !xim_changed_while_preediting)
2923 return;
2924 xim_changed_while_preediting = FALSE;
2925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#endif
2927
2928 if (!curbuf->b_changed)
2929 {
2930 int save_msg_scroll = msg_scroll;
2931
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002932 /* Give a warning about changing a read-only file. This may also
2933 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002935
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 /* Create a swap file if that is wanted.
2937 * Don't do this for "nofile" and "nowrite" buffer types. */
2938 if (curbuf->b_may_swap
2939#ifdef FEAT_QUICKFIX
2940 && !bt_dontwrite(curbuf)
2941#endif
2942 )
2943 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002944 int save_need_wait_return = need_wait_return;
2945
2946 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 ml_open_file(curbuf);
2948
2949 /* The ml_open_file() can cause an ATTENTION message.
2950 * Wait two seconds, to make sure the user reads this unexpected
2951 * message. Since we could be anywhere, call wait_return() now,
2952 * and don't let the emsg() set msg_scroll. */
2953 if (need_wait_return && emsg_silent == 0)
2954 {
2955 out_flush();
2956 ui_delay(2000L, TRUE);
2957 wait_return(TRUE);
2958 msg_scroll = save_msg_scroll;
2959 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002960 else
2961 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002963 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002965 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966}
2967
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002968/*
2969 * Internal part of changed(), no user interaction.
2970 */
2971 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002972changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002973{
2974 curbuf->b_changed = TRUE;
2975 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002976 check_status(curbuf);
2977 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002978#ifdef FEAT_TITLE
2979 need_maketitle = TRUE; /* set window title later */
2980#endif
2981}
2982
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002983static void changedOneline(buf_T *buf, linenr_T lnum);
2984static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2985static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987/*
2988 * Changed bytes within a single line for the current buffer.
2989 * - marks the windows on this buffer to be redisplayed
2990 * - marks the buffer changed by calling changed()
2991 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002992 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 */
2994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002995changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002997 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002999
3000#ifdef FEAT_DIFF
3001 /* Diff highlighting in other diff windows may need to be updated too. */
3002 if (curwin->w_p_diff)
3003 {
3004 win_T *wp;
3005 linenr_T wlnum;
3006
Bram Moolenaar29323592016-07-24 22:04:11 +02003007 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003008 if (wp->w_p_diff && wp != curwin)
3009 {
3010 redraw_win_later(wp, VALID);
3011 wlnum = diff_lnum_win(lnum, wp);
3012 if (wlnum > 0)
3013 changedOneline(wp->w_buffer, wlnum);
3014 }
3015 }
3016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017}
3018
3019 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003020changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003022 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 {
3024 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003025 if (lnum < buf->b_mod_top)
3026 buf->b_mod_top = lnum;
3027 else if (lnum >= buf->b_mod_bot)
3028 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030 else
3031 {
3032 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003033 buf->b_mod_set = TRUE;
3034 buf->b_mod_top = lnum;
3035 buf->b_mod_bot = lnum + 1;
3036 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
3038}
3039
3040/*
3041 * Appended "count" lines below line "lnum" in the current buffer.
3042 * Must be called AFTER the change and after mark_adjust().
3043 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3044 */
3045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003046appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 changed_lines(lnum + 1, 0, lnum + 1, count);
3049}
3050
3051/*
3052 * Like appended_lines(), but adjust marks first.
3053 */
3054 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003055appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003057 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003058 * be marks there. But it's still needed in diff mode. */
3059 if (lnum + count < curbuf->b_ml.ml_line_count
3060#ifdef FEAT_DIFF
3061 || curwin->w_p_diff
3062#endif
3063 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003064 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 changed_lines(lnum + 1, 0, lnum + 1, count);
3066}
3067
3068/*
3069 * Deleted "count" lines at line "lnum" in the current buffer.
3070 * Must be called AFTER the change and after mark_adjust().
3071 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3072 */
3073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003074deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 changed_lines(lnum, 0, lnum + count, -count);
3077}
3078
3079/*
3080 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003081 * Make sure the cursor is on a valid line before calling, a GUI callback may
3082 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 */
3084 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003085deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3088 changed_lines(lnum, 0, lnum + count, -count);
3089}
3090
3091/*
3092 * Changed lines for the current buffer.
3093 * Must be called AFTER the change and after mark_adjust().
3094 * - mark the buffer changed by calling changed()
3095 * - mark the windows on this buffer to be redisplayed
3096 * - invalidate cached values
3097 * "lnum" is the first line that needs displaying, "lnume" the first line
3098 * below the changed lines (BEFORE the change).
3099 * When only inserting lines, "lnum" and "lnume" are equal.
3100 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003101 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 */
3103 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003104changed_lines(
3105 linenr_T lnum, /* first line with change */
3106 colnr_T col, /* column in first line with change */
3107 linenr_T lnume, /* line below last changed line */
3108 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003110 changed_lines_buf(curbuf, lnum, lnume, xtra);
3111
3112#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003113 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003114 {
3115 /* When the number of lines doesn't change then mark_adjust() isn't
3116 * called and other diff buffers still need to be marked for
3117 * displaying. */
3118 win_T *wp;
3119 linenr_T wlnum;
3120
Bram Moolenaar29323592016-07-24 22:04:11 +02003121 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003122 if (wp->w_p_diff && wp != curwin)
3123 {
3124 redraw_win_later(wp, VALID);
3125 wlnum = diff_lnum_win(lnum, wp);
3126 if (wlnum > 0)
3127 changed_lines_buf(wp->w_buffer, wlnum,
3128 lnume - lnum + wlnum, 0L);
3129 }
3130 }
3131#endif
3132
3133 changed_common(lnum, col, lnume, xtra);
3134}
3135
3136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003137changed_lines_buf(
3138 buf_T *buf,
3139 linenr_T lnum, /* first line with change */
3140 linenr_T lnume, /* line below last changed line */
3141 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003142{
3143 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003146 if (lnum < buf->b_mod_top)
3147 buf->b_mod_top = lnum;
3148 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
3150 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003151 buf->b_mod_bot += xtra;
3152 if (buf->b_mod_bot < lnum)
3153 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003155 if (lnume + xtra > buf->b_mod_bot)
3156 buf->b_mod_bot = lnume + xtra;
3157 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159 else
3160 {
3161 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003162 buf->b_mod_set = TRUE;
3163 buf->b_mod_top = lnum;
3164 buf->b_mod_bot = lnume + xtra;
3165 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167}
3168
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003169/*
3170 * Common code for when a change is was made.
3171 * See changed_lines() for the arguments.
3172 * Careful: may trigger autocommands that reload the buffer.
3173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003175changed_common(
3176 linenr_T lnum,
3177 colnr_T col,
3178 linenr_T lnume,
3179 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003182 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 int i;
3184#ifdef FEAT_JUMPLIST
3185 int cols;
3186 pos_T *p;
3187 int add;
3188#endif
3189
3190 /* mark the buffer as modified */
3191 changed();
3192
Bram Moolenaare3521d92018-09-16 14:10:31 +02003193#ifdef FEAT_DIFF
3194 if (curwin->w_p_diff && diff_internal())
3195 curtab->tp_diff_update = TRUE;
3196#endif
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 /* set the '. mark */
3199 if (!cmdmod.keepjumps)
3200 {
3201 curbuf->b_last_change.lnum = lnum;
3202 curbuf->b_last_change.col = col;
3203
3204#ifdef FEAT_JUMPLIST
3205 /* Create a new entry if a new undo-able change was started or we
3206 * don't have an entry yet. */
3207 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3208 {
3209 if (curbuf->b_changelistlen == 0)
3210 add = TRUE;
3211 else
3212 {
3213 /* Don't create a new entry when the line number is the same
3214 * as the last one and the column is not too far away. Avoids
3215 * creating many entries for typing "xxxxx". */
3216 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3217 if (p->lnum != lnum)
3218 add = TRUE;
3219 else
3220 {
3221 cols = comp_textwidth(FALSE);
3222 if (cols == 0)
3223 cols = 79;
3224 add = (p->col + cols < col || col + cols < p->col);
3225 }
3226 }
3227 if (add)
3228 {
3229 /* This is the first of a new sequence of undo-able changes
3230 * and it's at some distance of the last change. Use a new
3231 * position in the changelist. */
3232 curbuf->b_new_change = FALSE;
3233
3234 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3235 {
3236 /* changelist is full: remove oldest entry */
3237 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3238 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3239 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003240 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
3242 /* Correct position in changelist for other windows on
3243 * this buffer. */
3244 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3245 --wp->w_changelistidx;
3246 }
3247 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003248 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 /* For other windows, if the position in the changelist is
3251 * at the end it stays at the end. */
3252 if (wp->w_buffer == curbuf
3253 && wp->w_changelistidx == curbuf->b_changelistlen)
3254 ++wp->w_changelistidx;
3255 }
3256 ++curbuf->b_changelistlen;
3257 }
3258 }
3259 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3260 curbuf->b_last_change;
3261 /* The current window is always after the last change, so that "g,"
3262 * takes you back to it. */
3263 curwin->w_changelistidx = curbuf->b_changelistlen;
3264#endif
3265 }
3266
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003267 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 if (wp->w_buffer == curbuf)
3270 {
3271 /* Mark this window to be redrawn later. */
3272 if (wp->w_redr_type < VALID)
3273 wp->w_redr_type = VALID;
3274
3275 /* Check if a change in the buffer has invalidated the cached
3276 * values for the cursor. */
3277#ifdef FEAT_FOLDING
3278 /*
3279 * Update the folds for this window. Can't postpone this, because
3280 * a following operator might work on the whole fold: ">>dd".
3281 */
3282 foldUpdate(wp, lnum, lnume + xtra - 1);
3283
3284 /* The change may cause lines above or below the change to become
3285 * included in a fold. Set lnum/lnume to the first/last line that
3286 * might be displayed differently.
3287 * Set w_cline_folded here as an efficient way to update it when
3288 * inserting lines just above a closed fold. */
3289 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3290 if (wp->w_cursor.lnum == lnum)
3291 wp->w_cline_folded = i;
3292 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3293 if (wp->w_cursor.lnum == lnume)
3294 wp->w_cline_folded = i;
3295
3296 /* If the changed line is in a range of previously folded lines,
3297 * compare with the first line in that range. */
3298 if (wp->w_cursor.lnum <= lnum)
3299 {
3300 i = find_wl_entry(wp, lnum);
3301 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3302 changed_line_abv_curs_win(wp);
3303 }
3304#endif
3305
3306 if (wp->w_cursor.lnum > lnum)
3307 changed_line_abv_curs_win(wp);
3308 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3309 changed_cline_bef_curs_win(wp);
3310 if (wp->w_botline >= lnum)
3311 {
3312 /* Assume that botline doesn't change (inserted lines make
3313 * other lines scroll down below botline). */
3314 approximate_botline_win(wp);
3315 }
3316
3317 /* Check if any w_lines[] entries have become invalid.
3318 * For entries below the change: Correct the lnums for
3319 * inserted/deleted lines. Makes it possible to stop displaying
3320 * after the change. */
3321 for (i = 0; i < wp->w_lines_valid; ++i)
3322 if (wp->w_lines[i].wl_valid)
3323 {
3324 if (wp->w_lines[i].wl_lnum >= lnum)
3325 {
3326 if (wp->w_lines[i].wl_lnum < lnume)
3327 {
3328 /* line included in change */
3329 wp->w_lines[i].wl_valid = FALSE;
3330 }
3331 else if (xtra != 0)
3332 {
3333 /* line below change */
3334 wp->w_lines[i].wl_lnum += xtra;
3335#ifdef FEAT_FOLDING
3336 wp->w_lines[i].wl_lastlnum += xtra;
3337#endif
3338 }
3339 }
3340#ifdef FEAT_FOLDING
3341 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3342 {
3343 /* change somewhere inside this range of folded lines,
3344 * may need to be redrawn */
3345 wp->w_lines[i].wl_valid = FALSE;
3346 }
3347#endif
3348 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003349
3350#ifdef FEAT_FOLDING
3351 /* Take care of side effects for setting w_topline when folds have
3352 * changed. Esp. when the buffer was changed in another window. */
3353 if (hasAnyFolding(wp))
3354 set_topline(wp, wp->w_topline);
3355#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003356 /* relative numbering may require updating more */
3357 if (wp->w_p_rnu)
3358 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 }
3361
3362 /* Call update_screen() later, which checks out what needs to be redrawn,
3363 * since it notices b_mod_set and then uses b_mod_*. */
3364 if (must_redraw < VALID)
3365 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003366
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003367 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003368 if (lnum <= curwin->w_cursor.lnum
3369 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003370 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373/*
3374 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3375 */
3376 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003377unchanged(
3378 buf_T *buf,
3379 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003381 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003384 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (ff)
3386 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003388 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#ifdef FEAT_TITLE
3390 need_maketitle = TRUE; /* set window title later */
3391#endif
3392 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003393 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#ifdef FEAT_NETBEANS_INTG
3395 netbeans_unmodified(buf);
3396#endif
3397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * check_status: called when the status bars for the buffer 'buf'
3401 * need to be updated
3402 */
3403 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003404check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
3406 win_T *wp;
3407
Bram Moolenaar29323592016-07-24 22:04:11 +02003408 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (wp->w_buffer == buf && wp->w_status_height)
3410 {
3411 wp->w_redr_status = TRUE;
3412 if (must_redraw < VALID)
3413 must_redraw = VALID;
3414 }
3415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417/*
3418 * If the file is readonly, give a warning message with the first change.
3419 * Don't do this for autocommands.
3420 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003421 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003423 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 */
3425 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003426change_warning(
3427 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 mode and 'showmode' is on */
3429{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003430 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (curbuf->b_did_warn == FALSE
3433 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 && curbuf->b_p_ro)
3436 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003437 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003439 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (!curbuf->b_p_ro)
3441 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 /*
3443 * Do what msg() does, but with a column offset if the warning should
3444 * be after the mode message.
3445 */
3446 msg_start();
3447 if (msg_row == Rows - 1)
3448 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003449 msg_source(HL_ATTR(HLF_W));
3450 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003451#ifdef FEAT_EVAL
3452 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 msg_clr_eos();
3455 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003456 if (msg_silent == 0 && !silent_mode
3457#ifdef FEAT_EVAL
3458 && time_for_testing != 1
3459#endif
3460 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 out_flush();
3463 ui_delay(1000L, TRUE); /* give the user time to think about it */
3464 }
3465 curbuf->b_did_warn = TRUE;
3466 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3467 if (msg_row < Rows - 1)
3468 showmode();
3469 }
3470}
3471
3472/*
3473 * Ask for a reply from the user, a 'y' or a 'n'.
3474 * No other characters are accepted, the message is repeated until a valid
3475 * reply is entered or CTRL-C is hit.
3476 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3477 * from any buffers but directly from the user.
3478 *
3479 * return the 'y' or 'n'
3480 */
3481 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003482ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 int r = ' ';
3485 int save_State = State;
3486
3487 if (exiting) /* put terminal in raw mode for this question */
3488 settmode(TMODE_RAW);
3489 ++no_wait_return;
3490#ifdef USE_ON_FLY_SCROLL
3491 dont_scroll = TRUE; /* disallow scrolling here */
3492#endif
3493 State = CONFIRM; /* mouse behaves like with :confirm */
3494#ifdef FEAT_MOUSE
3495 setmouse(); /* disables mouse for xterm */
3496#endif
3497 ++no_mapping;
3498 ++allow_keys; /* no mapping here, but recognize keys */
3499
3500 while (r != 'y' && r != 'n')
3501 {
3502 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003503 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (direct)
3505 r = get_keystroke();
3506 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003507 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (r == Ctrl_C || r == ESC)
3509 r = 'n';
3510 msg_putchar(r); /* show what you typed */
3511 out_flush();
3512 }
3513 --no_wait_return;
3514 State = save_State;
3515#ifdef FEAT_MOUSE
3516 setmouse();
3517#endif
3518 --no_mapping;
3519 --allow_keys;
3520
3521 return r;
3522}
3523
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003524#if defined(FEAT_MOUSE) || defined(PROTO)
3525/*
3526 * Return TRUE if "c" is a mouse key.
3527 */
3528 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003529is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003530{
3531 return c == K_LEFTMOUSE
3532 || c == K_LEFTMOUSE_NM
3533 || c == K_LEFTDRAG
3534 || c == K_LEFTRELEASE
3535 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003536 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003537 || c == K_MIDDLEMOUSE
3538 || c == K_MIDDLEDRAG
3539 || c == K_MIDDLERELEASE
3540 || c == K_RIGHTMOUSE
3541 || c == K_RIGHTDRAG
3542 || c == K_RIGHTRELEASE
3543 || c == K_MOUSEDOWN
3544 || c == K_MOUSEUP
3545 || c == K_MOUSELEFT
3546 || c == K_MOUSERIGHT
3547 || c == K_X1MOUSE
3548 || c == K_X1DRAG
3549 || c == K_X1RELEASE
3550 || c == K_X2MOUSE
3551 || c == K_X2DRAG
3552 || c == K_X2RELEASE;
3553}
3554#endif
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556/*
3557 * Get a key stroke directly from the user.
3558 * Ignores mouse clicks and scrollbar events, except a click for the left
3559 * button (used at the more prompt).
3560 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3561 * Disadvantage: typeahead is ignored.
3562 * Translates the interrupt character for unix to ESC.
3563 */
3564 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003565get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003567 char_u *buf = NULL;
3568 int buflen = 150;
3569 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 int len = 0;
3571 int n;
3572 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003573 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 mapped_ctrl_c = FALSE; /* mappings are not used here */
3576 for (;;)
3577 {
3578 cursor_on();
3579 out_flush();
3580
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003581 /* Leave some room for check_termcode() to insert a key code into (max
3582 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3583 * bytes. */
3584 maxlen = (buflen - 6 - len) / 3;
3585 if (buf == NULL)
3586 buf = alloc(buflen);
3587 else if (maxlen < 10)
3588 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003589 char_u *t_buf = buf;
3590
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003591 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003592 * escape sequence. */
3593 buflen += 100;
3594 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003595 if (buf == NULL)
3596 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003597 maxlen = (buflen - 6 - len) / 3;
3598 }
3599 if (buf == NULL)
3600 {
3601 do_outofmem_msg((long_u)buflen);
3602 return ESC; /* panic! */
3603 }
3604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003606 * terminal code to complete. */
3607 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 if (n > 0)
3609 {
3610 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003611 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003613 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003615 else if (len > 0)
3616 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
Bram Moolenaar4395a712006-09-05 18:57:57 +00003618 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003619 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003620 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003622
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003623 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003624 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003625 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003626 {
3627 /* Redrawing was postponed, do it now. */
3628 update_screen(0);
3629 setcursor(); /* put cursor back where it belongs */
3630 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003631 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003632 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003633 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003635 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 continue;
3637
3638 /* Handle modifier and/or special key code. */
3639 n = buf[0];
3640 if (n == K_SPECIAL)
3641 {
3642 n = TO_SPECIAL(buf[1], buf[2]);
3643 if (buf[1] == KS_MODIFIER
3644 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003645#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003646 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003647#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003648#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 || n == K_VER_SCROLLBAR
3650 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651#endif
3652 )
3653 {
3654 if (buf[1] == KS_MODIFIER)
3655 mod_mask = buf[2];
3656 len -= 3;
3657 if (len > 0)
3658 mch_memmove(buf, buf + 3, (size_t)len);
3659 continue;
3660 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003661 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663#ifdef FEAT_MBYTE
3664 if (has_mbyte)
3665 {
3666 if (MB_BYTE2LEN(n) > len)
3667 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003668 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 n = (*mb_ptr2char)(buf);
3670 }
3671#endif
3672#ifdef UNIX
3673 if (n == intr_char)
3674 n = ESC;
3675#endif
3676 break;
3677 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003678 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 mapped_ctrl_c = save_mapped_ctrl_c;
3681 return n;
3682}
3683
3684/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003685 * Get a number from the user.
3686 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 */
3688 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003689get_number(
3690 int colon, /* allow colon to abort */
3691 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692{
3693 int n = 0;
3694 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003695 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003697 if (mouse_used != NULL)
3698 *mouse_used = FALSE;
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 /* When not printing messages, the user won't know what to type, return a
3701 * zero (as if CR was hit). */
3702 if (msg_silent != 0)
3703 return 0;
3704
3705#ifdef USE_ON_FLY_SCROLL
3706 dont_scroll = TRUE; /* disallow scrolling here */
3707#endif
3708 ++no_mapping;
3709 ++allow_keys; /* no mapping here, but recognize keys */
3710 for (;;)
3711 {
3712 windgoto(msg_row, msg_col);
3713 c = safe_vgetc();
3714 if (VIM_ISDIGIT(c))
3715 {
3716 n = n * 10 + c - '0';
3717 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003718 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3721 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003722 if (typed > 0)
3723 {
3724 MSG_PUTS("\b \b");
3725 --typed;
3726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003729#ifdef FEAT_MOUSE
3730 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3731 {
3732 *mouse_used = TRUE;
3733 n = mouse_row + 1;
3734 break;
3735 }
3736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 else if (n == 0 && c == ':' && colon)
3738 {
3739 stuffcharReadbuff(':');
3740 if (!exmode_active)
3741 cmdline_row = msg_row;
3742 skip_redraw = TRUE; /* skip redraw once */
3743 do_redraw = FALSE;
3744 break;
3745 }
3746 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3747 break;
3748 }
3749 --no_mapping;
3750 --allow_keys;
3751 return n;
3752}
3753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754/*
3755 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003756 * When "mouse_used" is not NULL allow using the mouse and in that case return
3757 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 */
3759 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003760prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761{
3762 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003763 int save_cmdline_row;
3764 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765
3766 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003767 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003768 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003769 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003770 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003771
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003772 // Set the state such that text can be selected/copied/pasted and we still
3773 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3774 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003775 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003776 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003777 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003778 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003779#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003780 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003781 setmouse();
3782#endif
3783
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003784 i = get_number(TRUE, mouse_used);
3785 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003787 /* don't call wait_return() now */
3788 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 cmdline_row = msg_row - 1;
3790 need_wait_return = FALSE;
3791 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003792 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003793 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003794 else
3795 cmdline_row = save_cmdline_row;
3796 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003797#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003798 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003799 setmouse();
3800#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003802 return i;
3803}
3804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003806msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 long pn;
3809
3810 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3812 return;
3813
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003814 /* We don't want to overwrite another important message, but do overwrite
3815 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3816 * then "put" reports the last action. */
3817 if (keep_msg != NULL && !keep_msg_more)
3818 return;
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 if (n > 0)
3821 pn = n;
3822 else
3823 pn = -n;
3824
3825 if (pn > p_report)
3826 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003827 if (n > 0)
3828 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3829 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003831 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3832 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003834 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (msg(msg_buf))
3836 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003837 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003838 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
3841}
3842
3843/*
3844 * flush map and typeahead buffers and give a warning for an error
3845 */
3846 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003847beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
3849 if (emsg_silent == 0)
3850 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003851 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003852 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854}
3855
3856/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003857 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 */
3859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003860vim_beep(
3861 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003863#ifdef FEAT_EVAL
3864 called_vim_beep = TRUE;
3865#endif
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (emsg_silent == 0)
3868 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003869 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3870 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003871#ifdef ELAPSED_FUNC
3872 static int did_init = FALSE;
3873 static ELAPSED_TYPE start_tv;
3874
3875 /* Only beep once per half a second, otherwise a sequence of beeps
3876 * would freeze Vim. */
3877 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3878 {
3879 did_init = TRUE;
3880 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003882 if (p_vb
3883#ifdef FEAT_GUI
3884 /* While the GUI is starting up the termcap is set for
3885 * the GUI but the output still goes to a terminal. */
3886 && !(gui.in_use && gui.starting)
3887#endif
3888 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003889 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003890 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003891#ifdef FEAT_VTP
3892 /* No restore color information, refresh the screen. */
3893 if (has_vtp_working() != 0
3894# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003895 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003896# endif
3897 )
3898 {
3899 redraw_later(CLEAR);
3900 update_screen(0);
3901 redrawcmd();
3902 }
3903#endif
3904 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003905 else
3906 out_char(BELL);
3907#ifdef ELAPSED_FUNC
3908 }
3909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003911
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003912 /* When 'debug' contains "beep" produce a message. If we are sourcing
3913 * a script or executing a function give the user a hint where the beep
3914 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003915 if (vim_strchr(p_debug, 'e') != NULL)
3916 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003917 msg_source(HL_ATTR(HLF_W));
3918 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921}
3922
3923/*
3924 * To get the "real" home directory:
3925 * - get value of $HOME
3926 * For Unix:
3927 * - go to that directory
3928 * - do mch_dirname() to get the real name of that directory.
3929 * This also works with mounts and links.
3930 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003931 * For Windows:
3932 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 */
3934static char_u *homedir = NULL;
3935
3936 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003937init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 char_u *var;
3940
Bram Moolenaar05159a02005-02-26 23:04:13 +00003941 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003942 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#ifdef VMS
3945 var = mch_getenv((char_u *)"SYS$LOGIN");
3946#else
3947 var = mch_getenv((char_u *)"HOME");
3948#endif
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#ifdef WIN3264
3951 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003952 * Typically, $HOME is not defined on Windows, unless the user has
3953 * specifically defined it for Vim's sake. However, on Windows NT
3954 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3955 * each user. Try constructing $HOME from these.
3956 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003957 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003958 {
3959 char_u *homedrive, *homepath;
3960
3961 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3962 homepath = mch_getenv((char_u *)"HOMEPATH");
3963 if (homepath == NULL || *homepath == NUL)
3964 homepath = (char_u *)"\\";
3965 if (homedrive != NULL
3966 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3967 {
3968 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3969 if (NameBuff[0] != NUL)
3970 var = NameBuff;
3971 }
3972 }
3973
3974 if (var == NULL)
3975 var = mch_getenv((char_u *)"USERPROFILE");
3976
3977 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 * Weird but true: $HOME may contain an indirect reference to another
3979 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3980 * when $HOME is being set.
3981 */
3982 if (var != NULL && *var == '%')
3983 {
3984 char_u *p;
3985 char_u *exp;
3986
3987 p = vim_strchr(var + 1, '%');
3988 if (p != NULL)
3989 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003990 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 exp = mch_getenv(NameBuff);
3992 if (exp != NULL && *exp != NUL
3993 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3994 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003995 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998 }
3999 }
4000
Bram Moolenaar48340b62017-08-29 22:08:53 +02004001 if (var != NULL && *var == NUL) /* empty is same as not set */
4002 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
Bram Moolenaar48340b62017-08-29 22:08:53 +02004004# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00004005 if (enc_utf8 && var != NULL)
4006 {
4007 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004008 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004009
4010 /* Convert from active codepage to UTF-8. Other conversions are
4011 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004012 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004013 if (pp != NULL)
4014 {
4015 homedir = pp;
4016 return;
4017 }
4018 }
4019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 /*
4022 * Default home dir is C:/
4023 * Best assumption we can make in such a situation.
4024 */
4025 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004026 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if (var != NULL)
4030 {
4031#ifdef UNIX
4032 /*
4033 * Change to the directory and get the actual path. This resolves
4034 * links. Don't do it when we can't return.
4035 */
4036 if (mch_dirname(NameBuff, MAXPATHL) == OK
4037 && mch_chdir((char *)NameBuff) == 0)
4038 {
4039 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4040 var = IObuff;
4041 if (mch_chdir((char *)NameBuff) != 0)
4042 EMSG(_(e_prev_dir));
4043 }
4044#endif
4045 homedir = vim_strsave(var);
4046 }
4047}
4048
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004049#if defined(EXITFREE) || defined(PROTO)
4050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004051free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004052{
4053 vim_free(homedir);
4054}
Bram Moolenaar24305862012-08-15 14:05:05 +02004055
4056# ifdef FEAT_CMDL_COMPL
4057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004058free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004059{
4060 ga_clear_strings(&ga_users);
4061}
4062# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004063#endif
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004066 * Call expand_env() and store the result in an allocated string.
4067 * This is not very memory efficient, this expects the result to be freed
4068 * again soon.
4069 */
4070 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004071expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004072{
4073 return expand_env_save_opt(src, FALSE);
4074}
4075
4076/*
4077 * Idem, but when "one" is TRUE handle the string as one file name, only
4078 * expand "~" at the start.
4079 */
4080 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004081expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004082{
4083 char_u *p;
4084
4085 p = alloc(MAXPATHL);
4086 if (p != NULL)
4087 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4088 return p;
4089}
4090
4091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * Expand environment variable with path name.
4093 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004094 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 * If anything fails no expansion is done and dst equals src.
4096 */
4097 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004098expand_env(
4099 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4100 char_u *dst, /* where to put the result */
4101 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004103 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104}
4105
4106 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004107expand_env_esc(
4108 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4109 char_u *dst, /* where to put the result */
4110 int dstlen, /* maximum length of the result */
4111 int esc, /* escape spaces in expanded variables */
4112 int one, /* "srcp" is one file name */
4113 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004115 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 char_u *tail;
4117 int c;
4118 char_u *var;
4119 int copy_char;
4120 int mustfree; /* var was allocated, need to free it later */
4121 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004122 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004124 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004125 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004126
4127 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 --dstlen; /* leave one char space for "\," */
4129 while (*src && dstlen > 0)
4130 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004131#ifdef FEAT_EVAL
4132 /* Skip over `=expr`. */
4133 if (src[0] == '`' && src[1] == '=')
4134 {
4135 size_t len;
4136
4137 var = src;
4138 src += 2;
4139 (void)skip_expr(&src);
4140 if (*src == '`')
4141 ++src;
4142 len = src - var;
4143 if (len > (size_t)dstlen)
4144 len = dstlen;
4145 vim_strncpy(dst, var, len);
4146 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004147 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004148 continue;
4149 }
4150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004152 if ((*src == '$'
4153#ifdef VMS
4154 && at_start
4155#endif
4156 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004157#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 || *src == '%'
4159#endif
4160 || (*src == '~' && at_start))
4161 {
4162 mustfree = FALSE;
4163
4164 /*
4165 * The variable name is copied into dst temporarily, because it may
4166 * be a string in read-only memory and a NUL needs to be appended.
4167 */
4168 if (*src != '~') /* environment var */
4169 {
4170 tail = src + 1;
4171 var = dst;
4172 c = dstlen - 1;
4173
4174#ifdef UNIX
4175 /* Unix has ${var-name} type environment vars */
4176 if (*tail == '{' && !vim_isIDc('{'))
4177 {
4178 tail++; /* ignore '{' */
4179 while (c-- > 0 && *tail && *tail != '}')
4180 *var++ = *tail++;
4181 }
4182 else
4183#endif
4184 {
4185 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004186#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 || (*src == '%' && *tail != '%')
4188#endif
4189 ))
4190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 }
4194
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004195#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196# ifdef UNIX
4197 if (src[1] == '{' && *tail != '}')
4198# else
4199 if (*src == '%' && *tail != '%')
4200# endif
4201 var = NULL;
4202 else
4203 {
4204# ifdef UNIX
4205 if (src[1] == '{')
4206# else
4207 if (*src == '%')
4208#endif
4209 ++tail;
4210#endif
4211 *var = NUL;
4212 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004213#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215#endif
4216 }
4217 /* home directory */
4218 else if ( src[1] == NUL
4219 || vim_ispathsep(src[1])
4220 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4221 {
4222 var = homedir;
4223 tail = src + 1;
4224 }
4225 else /* user directory */
4226 {
4227#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4228 /*
4229 * Copy ~user to dst[], so we can put a NUL after it.
4230 */
4231 tail = src;
4232 var = dst;
4233 c = dstlen - 1;
4234 while ( c-- > 0
4235 && *tail
4236 && vim_isfilec(*tail)
4237 && !vim_ispathsep(*tail))
4238 *var++ = *tail++;
4239 *var = NUL;
4240# ifdef UNIX
4241 /*
4242 * If the system supports getpwnam(), use it.
4243 * Otherwise, or if getpwnam() fails, the shell is used to
4244 * expand ~user. This is slower and may fail if the shell
4245 * does not support ~user (old versions of /bin/sh).
4246 */
4247# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4248 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004249 /* Note: memory allocated by getpwnam() is never freed.
4250 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004251 struct passwd *pw = (*dst == NUL)
4252 ? NULL : getpwnam((char *)dst + 1);
4253
4254 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 if (var == NULL)
4257# endif
4258 {
4259 expand_T xpc;
4260
4261 ExpandInit(&xpc);
4262 xpc.xp_context = EXPAND_FILES;
4263 var = ExpandOne(&xpc, dst, NULL,
4264 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 mustfree = TRUE;
4266 }
4267
4268# else /* !UNIX, thus VMS */
4269 /*
4270 * USER_HOME is a comma-separated list of
4271 * directories to search for the user account in.
4272 */
4273 {
4274 char_u test[MAXPATHL], paths[MAXPATHL];
4275 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004276 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 STRCPY(paths, USER_HOME);
4279 next_path = paths;
4280 while (*next_path)
4281 {
4282 for (path = next_path; *next_path && *next_path != ',';
4283 next_path++);
4284 if (*next_path)
4285 *next_path++ = NUL;
4286 STRCPY(test, path);
4287 STRCAT(test, "/");
4288 STRCAT(test, dst + 1);
4289 if (mch_stat(test, &st) == 0)
4290 {
4291 var = alloc(STRLEN(test) + 1);
4292 STRCPY(var, test);
4293 mustfree = TRUE;
4294 break;
4295 }
4296 }
4297 }
4298# endif /* UNIX */
4299#else
4300 /* cannot expand user's home directory, so don't try */
4301 var = NULL;
4302 tail = (char_u *)""; /* for gcc */
4303#endif /* UNIX || VMS */
4304 }
4305
4306#ifdef BACKSLASH_IN_FILENAME
4307 /* If 'shellslash' is set change backslashes to forward slashes.
4308 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4309 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4310 {
4311 char_u *p = vim_strsave(var);
4312
4313 if (p != NULL)
4314 {
4315 if (mustfree)
4316 vim_free(var);
4317 var = p;
4318 mustfree = TRUE;
4319 forward_slash(var);
4320 }
4321 }
4322#endif
4323
4324 /* If "var" contains white space, escape it with a backslash.
4325 * Required for ":e ~/tt" when $HOME includes a space. */
4326 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4327 {
4328 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4329
4330 if (p != NULL)
4331 {
4332 if (mustfree)
4333 vim_free(var);
4334 var = p;
4335 mustfree = TRUE;
4336 }
4337 }
4338
4339 if (var != NULL && *var != NUL
4340 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4341 {
4342 STRCPY(dst, var);
4343 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004344 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 /* if var[] ends in a path separator and tail[] starts
4346 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004347 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4349 && dst[-1] != ':'
4350#endif
4351 && vim_ispathsep(*tail))
4352 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004353 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 src = tail;
4355 copy_char = FALSE;
4356 }
4357 if (mustfree)
4358 vim_free(var);
4359 }
4360
4361 if (copy_char) /* copy at least one char */
4362 {
4363 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004364 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004365 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4366 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
4368 at_start = FALSE;
4369 if (src[0] == '\\' && src[1] != NUL)
4370 {
4371 *dst++ = *src++;
4372 --dstlen;
4373 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004374 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004376 if (dstlen > 0)
4377 {
4378 *dst++ = *src++;
4379 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004380
Bram Moolenaar1c864092017-08-06 18:15:45 +02004381 if (startstr != NULL && src - startstr_len >= srcp
4382 && STRNCMP(src - startstr_len, startstr,
4383 startstr_len) == 0)
4384 at_start = TRUE;
4385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004387
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 *dst = NUL;
4390}
4391
4392/*
4393 * Vim's version of getenv().
4394 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004395 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004396 * "mustfree" is set to TRUE when returned is allocated, it must be
4397 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 */
4399 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004400vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
4402 char_u *p;
4403 char_u *pend;
4404 int vimruntime;
4405
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004406#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 /* use "C:/" when $HOME is not set */
4408 if (STRCMP(name, "HOME") == 0)
4409 return homedir;
4410#endif
4411
4412 p = mch_getenv(name);
4413 if (p != NULL && *p == NUL) /* empty is the same as not set */
4414 p = NULL;
4415
4416 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004417 {
4418#if defined(FEAT_MBYTE) && defined(WIN3264)
4419 if (enc_utf8)
4420 {
4421 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004422 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004423
4424 /* Convert from active codepage to UTF-8. Other conversions are
4425 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004426 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004427 if (pp != NULL)
4428 {
4429 p = pp;
4430 *mustfree = TRUE;
4431 }
4432 }
4433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4438 if (!vimruntime && STRCMP(name, "VIM") != 0)
4439 return NULL;
4440
4441 /*
4442 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4443 * Don't do this when default_vimruntime_dir is non-empty.
4444 */
4445 if (vimruntime
4446#ifdef HAVE_PATHDEF
4447 && *default_vimruntime_dir == NUL
4448#endif
4449 )
4450 {
4451 p = mch_getenv((char_u *)"VIM");
4452 if (p != NULL && *p == NUL) /* empty is the same as not set */
4453 p = NULL;
4454 if (p != NULL)
4455 {
4456 p = vim_version_dir(p);
4457 if (p != NULL)
4458 *mustfree = TRUE;
4459 else
4460 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004461
4462#if defined(FEAT_MBYTE) && defined(WIN3264)
4463 if (enc_utf8)
4464 {
4465 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004466 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004467
4468 /* Convert from active codepage to UTF-8. Other conversions
4469 * are not done, because they would fail for non-ASCII
4470 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004471 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004472 if (pp != NULL)
4473 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004474 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004475 vim_free(p);
4476 p = pp;
4477 *mustfree = TRUE;
4478 }
4479 }
4480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482 }
4483
4484 /*
4485 * When expanding $VIM or $VIMRUNTIME fails, try using:
4486 * - the directory name from 'helpfile' (unless it contains '$')
4487 * - the executable name from argv[0]
4488 */
4489 if (p == NULL)
4490 {
4491 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4492 p = p_hf;
4493#ifdef USE_EXE_NAME
4494 /*
4495 * Use the name of the executable, obtained from argv[0].
4496 */
4497 else
4498 p = exe_name;
4499#endif
4500 if (p != NULL)
4501 {
4502 /* remove the file name */
4503 pend = gettail(p);
4504
4505 /* remove "doc/" from 'helpfile', if present */
4506 if (p == p_hf)
4507 pend = remove_tail(p, pend, (char_u *)"doc");
4508
4509#ifdef USE_EXE_NAME
4510# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004511 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (p == exe_name)
4513 {
4514 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004515 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004517 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4518 if (pend1 != pend)
4519 {
4520 pnew = alloc((unsigned)(pend1 - p) + 15);
4521 if (pnew != NULL)
4522 {
4523 STRNCPY(pnew, p, (pend1 - p));
4524 STRCPY(pnew + (pend1 - p), "Resources/vim");
4525 p = pnew;
4526 pend = p + STRLEN(p);
4527 }
4528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530# endif
4531 /* remove "src/" from exe_name, if present */
4532 if (p == exe_name)
4533 pend = remove_tail(p, pend, (char_u *)"src");
4534#endif
4535
4536 /* for $VIM, remove "runtime/" or "vim54/", if present */
4537 if (!vimruntime)
4538 {
4539 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4540 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4541 }
4542
4543 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004544 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004547#ifdef MACOS_X
4548 if (p == exe_name || p == p_hf)
4549#endif
4550 /* check that the result is a directory name */
4551 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004554 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 else
4556 {
4557#ifdef USE_EXE_NAME
4558 /* may add "/vim54" or "/runtime" if it exists */
4559 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4560 {
4561 vim_free(p);
4562 p = pend;
4563 }
4564#endif
4565 *mustfree = TRUE;
4566 }
4567 }
4568 }
4569
4570#ifdef HAVE_PATHDEF
4571 /* When there is a pathdef.c file we can use default_vim_dir and
4572 * default_vimruntime_dir */
4573 if (p == NULL)
4574 {
4575 /* Only use default_vimruntime_dir when it is not empty */
4576 if (vimruntime && *default_vimruntime_dir != NUL)
4577 {
4578 p = default_vimruntime_dir;
4579 *mustfree = FALSE;
4580 }
4581 else if (*default_vim_dir != NUL)
4582 {
4583 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4584 *mustfree = TRUE;
4585 else
4586 {
4587 p = default_vim_dir;
4588 *mustfree = FALSE;
4589 }
4590 }
4591 }
4592#endif
4593
4594 /*
4595 * Set the environment variable, so that the new value can be found fast
4596 * next time, and others can also use it (e.g. Perl).
4597 */
4598 if (p != NULL)
4599 {
4600 if (vimruntime)
4601 {
4602 vim_setenv((char_u *)"VIMRUNTIME", p);
4603 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 else
4606 {
4607 vim_setenv((char_u *)"VIM", p);
4608 didset_vim = TRUE;
4609 }
4610 }
4611 return p;
4612}
4613
4614/*
4615 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4616 * Return NULL if not, return its name in allocated memory otherwise.
4617 */
4618 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004619vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620{
4621 char_u *p;
4622
4623 if (vimdir == NULL || *vimdir == NUL)
4624 return NULL;
4625 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4626 if (p != NULL && mch_isdir(p))
4627 return p;
4628 vim_free(p);
4629 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4630 if (p != NULL && mch_isdir(p))
4631 return p;
4632 vim_free(p);
4633 return NULL;
4634}
4635
4636/*
4637 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4638 * the length of "name/". Otherwise return "pend".
4639 */
4640 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004641remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 int len = (int)STRLEN(name) + 1;
4644 char_u *newend = pend - len;
4645
4646 if (newend >= p
4647 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004648 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return newend;
4650 return pend;
4651}
4652
Bram Moolenaar137374f2018-05-13 15:59:50 +02004653 void
4654vim_unsetenv(char_u *var)
4655{
4656#ifdef HAVE_UNSETENV
4657 unsetenv((char *)var);
4658#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004659 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004660#endif
4661}
4662
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 * Our portable version of setenv.
4666 */
4667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004668vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
4670#ifdef HAVE_SETENV
4671 mch_setenv((char *)name, (char *)val, 1);
4672#else
4673 char_u *envbuf;
4674
4675 /*
4676 * Putenv does not copy the string, it has to remain
4677 * valid. The allocated memory will never be freed.
4678 */
4679 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4680 if (envbuf != NULL)
4681 {
4682 sprintf((char *)envbuf, "%s=%s", name, val);
4683 putenv((char *)envbuf);
4684 }
4685#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004686#ifdef FEAT_GETTEXT
4687 /*
4688 * When setting $VIMRUNTIME adjust the directory to find message
4689 * translations to $VIMRUNTIME/lang.
4690 */
4691 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4692 {
4693 char_u *buf = concat_str(val, (char_u *)"/lang");
4694
4695 if (buf != NULL)
4696 {
4697 bindtextdomain(VIMPACKAGE, (char *)buf);
4698 vim_free(buf);
4699 }
4700 }
4701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702}
4703
4704#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4705/*
4706 * Function given to ExpandGeneric() to obtain an environment variable name.
4707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004709get_env_name(
4710 expand_T *xp UNUSED,
4711 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
Bram Moolenaard0573012017-10-28 21:11:06 +02004713# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004715 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 */
4717 return NULL;
4718# else
4719# ifndef __WIN32__
4720 /* Borland C++ 5.2 has this in a header file. */
4721 extern char **environ;
4722# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004723# define ENVNAMELEN 100
4724 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 char_u *str;
4726 int n;
4727
4728 str = (char_u *)environ[idx];
4729 if (str == NULL)
4730 return NULL;
4731
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004732 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
4734 if (str[n] == '=' || str[n] == NUL)
4735 break;
4736 name[n] = str[n];
4737 }
4738 name[n] = NUL;
4739 return name;
4740# endif
4741}
Bram Moolenaar24305862012-08-15 14:05:05 +02004742
4743/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004744 * Add a user name to the list of users in ga_users.
4745 * Do nothing if user name is NULL or empty.
4746 */
4747 static void
4748add_user(char_u *user, int need_copy)
4749{
4750 char_u *user_copy = (user != NULL && need_copy)
4751 ? vim_strsave(user) : user;
4752
4753 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4754 {
4755 if (need_copy)
4756 vim_free(user);
4757 return;
4758 }
4759 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4760}
4761
4762/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004763 * Find all user names for user completion.
4764 * Done only once and then cached.
4765 */
4766 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004767init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004768{
Bram Moolenaar24305862012-08-15 14:05:05 +02004769 static int lazy_init_done = FALSE;
4770
4771 if (lazy_init_done)
4772 return;
4773
4774 lazy_init_done = TRUE;
4775 ga_init2(&ga_users, sizeof(char_u *), 20);
4776
4777# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4778 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004779 struct passwd* pw;
4780
4781 setpwent();
4782 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004783 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004784 endpwent();
4785 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004786# elif defined(WIN3264)
4787 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004788 DWORD nusers = 0, ntotal = 0, i;
4789 PUSER_INFO_0 uinfo;
4790
4791 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4792 &nusers, &ntotal, NULL) == NERR_Success)
4793 {
4794 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004795 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004796
4797 NetApiBufferFree(uinfo);
4798 }
4799 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004800# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004801# if defined(HAVE_GETPWNAM)
4802 {
4803 char_u *user_env = mch_getenv((char_u *)"USER");
4804
4805 // The $USER environment variable may be a valid remote user name (NIS,
4806 // LDAP) not already listed by getpwent(), as getpwent() only lists
4807 // local user names. If $USER is not already listed, check whether it
4808 // is a valid remote user name using getpwnam() and if it is, add it to
4809 // the list of user names.
4810
4811 if (user_env != NULL && *user_env != NUL)
4812 {
4813 int i;
4814
4815 for (i = 0; i < ga_users.ga_len; i++)
4816 {
4817 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4818
4819 if (STRCMP(local_user, user_env) == 0)
4820 break;
4821 }
4822
4823 if (i == ga_users.ga_len)
4824 {
4825 struct passwd *pw = getpwnam((char *)user_env);
4826
4827 if (pw != NULL)
4828 add_user((char_u *)pw->pw_name, TRUE);
4829 }
4830 }
4831 }
4832# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004833}
4834
4835/*
4836 * Function given to ExpandGeneric() to obtain an user names.
4837 */
4838 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004839get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004840{
4841 init_users();
4842 if (idx < ga_users.ga_len)
4843 return ((char_u **)ga_users.ga_data)[idx];
4844 return NULL;
4845}
4846
4847/*
4848 * Check whether name matches a user name. Return:
4849 * 0 if name does not match any user name.
4850 * 1 if name partially matches the beginning of a user name.
4851 * 2 is name fully matches a user name.
4852 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004853 int
4854match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004855{
4856 int i;
4857 int n = (int)STRLEN(name);
4858 int result = 0;
4859
4860 init_users();
4861 for (i = 0; i < ga_users.ga_len; i++)
4862 {
4863 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4864 return 2; /* full match */
4865 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4866 result = 1; /* partial match */
4867 }
4868 return result;
4869}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#endif
4871
4872/*
4873 * Replace home directory by "~" in each space or comma separated file name in
4874 * 'src'.
4875 * If anything fails (except when out of space) dst equals src.
4876 */
4877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004878home_replace(
4879 buf_T *buf, /* when not NULL, check for help files */
4880 char_u *src, /* input file name */
4881 char_u *dst, /* where to put the result */
4882 int dstlen, /* maximum length of the result */
4883 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 spaces and commas in the file name. */
4885{
4886 size_t dirlen = 0, envlen = 0;
4887 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004888 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 char_u *p;
4890
4891 if (src == NULL)
4892 {
4893 *dst = NUL;
4894 return;
4895 }
4896
4897 /*
4898 * If the file is a help file, remove the path completely.
4899 */
4900 if (buf != NULL && buf->b_help)
4901 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004902 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 return;
4904 }
4905
4906 /*
4907 * We check both the value of the $HOME environment variable and the
4908 * "real" home directory.
4909 */
4910 if (homedir != NULL)
4911 dirlen = STRLEN(homedir);
4912
4913#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004914 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004916 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4917#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004918#ifdef WIN3264
4919 if (homedir_env == NULL)
4920 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4921#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004922 /* Empty is the same as not set. */
4923 if (homedir_env != NULL && *homedir_env == NUL)
4924 homedir_env = NULL;
4925
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004926#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004927 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004928 {
4929 int usedlen = 0;
4930 int flen;
4931 char_u *fbuf = NULL;
4932
4933 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004934 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004935 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004936 flen = (int)STRLEN(homedir_env);
4937 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4938 /* Remove the trailing / that is added to a directory. */
4939 homedir_env[flen - 1] = NUL;
4940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#endif
4942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (homedir_env != NULL)
4944 envlen = STRLEN(homedir_env);
4945
4946 if (!one)
4947 src = skipwhite(src);
4948 while (*src && dstlen > 0)
4949 {
4950 /*
4951 * Here we are at the beginning of a file name.
4952 * First, check to see if the beginning of the file name matches
4953 * $HOME or the "real" home directory. Check that there is a '/'
4954 * after the match (so that if e.g. the file is "/home/pieter/bla",
4955 * and the home directory is "/home/piet", the file does not end up
4956 * as "~er/bla" (which would seem to indicate the file "bla" in user
4957 * er's home directory)).
4958 */
4959 p = homedir;
4960 len = dirlen;
4961 for (;;)
4962 {
4963 if ( len
4964 && fnamencmp(src, p, len) == 0
4965 && (vim_ispathsep(src[len])
4966 || (!one && (src[len] == ',' || src[len] == ' '))
4967 || src[len] == NUL))
4968 {
4969 src += len;
4970 if (--dstlen > 0)
4971 *dst++ = '~';
4972
4973 /*
4974 * If it's just the home directory, add "/".
4975 */
4976 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4977 *dst++ = '/';
4978 break;
4979 }
4980 if (p == homedir_env)
4981 break;
4982 p = homedir_env;
4983 len = envlen;
4984 }
4985
4986 /* if (!one) skip to separator: space or comma */
4987 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4988 *dst++ = *src++;
4989 /* skip separator */
4990 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4991 *dst++ = *src++;
4992 }
4993 /* if (dstlen == 0) out of space, what to do??? */
4994
4995 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004996
4997 if (homedir_env != homedir_env_orig)
4998 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999}
5000
5001/*
5002 * Like home_replace, store the replaced string in allocated memory.
5003 * When something fails, NULL is returned.
5004 */
5005 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005006home_replace_save(
5007 buf_T *buf, /* when not NULL, check for help files */
5008 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
5010 char_u *dst;
5011 unsigned len;
5012
5013 len = 3; /* space for "~/" and trailing NUL */
5014 if (src != NULL) /* just in case */
5015 len += (unsigned)STRLEN(src);
5016 dst = alloc(len);
5017 if (dst != NULL)
5018 home_replace(buf, src, dst, len, TRUE);
5019 return dst;
5020}
5021
5022/*
5023 * Compare two file names and return:
5024 * FPC_SAME if they both exist and are the same file.
5025 * FPC_SAMEX if they both don't exist and have the same file name.
5026 * FPC_DIFF if they both exist and are different files.
5027 * FPC_NOTX if they both don't exist.
5028 * FPC_DIFFX if one of them doesn't exist.
5029 * For the first name environment variables are expanded
5030 */
5031 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032fullpathcmp(
5033 char_u *s1,
5034 char_u *s2,
5035 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037#ifdef UNIX
5038 char_u exp1[MAXPATHL];
5039 char_u full1[MAXPATHL];
5040 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005041 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 int r1, r2;
5043
5044 expand_env(s1, exp1, MAXPATHL);
5045 r1 = mch_stat((char *)exp1, &st1);
5046 r2 = mch_stat((char *)s2, &st2);
5047 if (r1 != 0 && r2 != 0)
5048 {
5049 /* if mch_stat() doesn't work, may compare the names */
5050 if (checkname)
5051 {
5052 if (fnamecmp(exp1, s2) == 0)
5053 return FPC_SAMEX;
5054 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5055 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5056 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5057 return FPC_SAMEX;
5058 }
5059 return FPC_NOTX;
5060 }
5061 if (r1 != 0 || r2 != 0)
5062 return FPC_DIFFX;
5063 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5064 return FPC_SAME;
5065 return FPC_DIFF;
5066#else
5067 char_u *exp1; /* expanded s1 */
5068 char_u *full1; /* full path of s1 */
5069 char_u *full2; /* full path of s2 */
5070 int retval = FPC_DIFF;
5071 int r1, r2;
5072
5073 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5074 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5075 {
5076 full1 = exp1 + MAXPATHL;
5077 full2 = full1 + MAXPATHL;
5078
5079 expand_env(s1, exp1, MAXPATHL);
5080 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5081 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5082
5083 /* If vim_FullName() fails, the file probably doesn't exist. */
5084 if (r1 != OK && r2 != OK)
5085 {
5086 if (checkname && fnamecmp(exp1, s2) == 0)
5087 retval = FPC_SAMEX;
5088 else
5089 retval = FPC_NOTX;
5090 }
5091 else if (r1 != OK || r2 != OK)
5092 retval = FPC_DIFFX;
5093 else if (fnamecmp(full1, full2))
5094 retval = FPC_DIFF;
5095 else
5096 retval = FPC_SAME;
5097 vim_free(exp1);
5098 }
5099 return retval;
5100#endif
5101}
5102
5103/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005104 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005105 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005106 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 */
5108 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005109gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
5111 char_u *p1, *p2;
5112
5113 if (fname == NULL)
5114 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005115 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005117 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005119 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 return p1;
5122}
5123
Bram Moolenaar31710262010-08-13 13:36:15 +02005124#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005125/*
5126 * Return the end of the directory name, on the first path
5127 * separator:
5128 * "/path/file", "/path/dir/", "/path//dir", "/file"
5129 * ^ ^ ^ ^
5130 */
5131 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005132gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005133{
5134 char_u *dir_end = fname;
5135 char_u *next_dir_end = fname;
5136 int look_for_sep = TRUE;
5137 char_u *p;
5138
5139 for (p = fname; *p != NUL; )
5140 {
5141 if (vim_ispathsep(*p))
5142 {
5143 if (look_for_sep)
5144 {
5145 next_dir_end = p;
5146 look_for_sep = FALSE;
5147 }
5148 }
5149 else
5150 {
5151 if (!look_for_sep)
5152 dir_end = next_dir_end;
5153 look_for_sep = TRUE;
5154 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005155 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005156 }
5157 return dir_end;
5158}
5159#endif
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005162 * Get pointer to tail of "fname", including path separators. Putting a NUL
5163 * here leaves the directory name. Takes care of "c:/" and "//".
5164 * Always returns a valid pointer.
5165 */
5166 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005167gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005168{
5169 char_u *p;
5170 char_u *t;
5171
5172 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5173 t = gettail(fname);
5174 while (t > p && after_pathsep(fname, t))
5175 --t;
5176#ifdef VMS
5177 /* path separator is part of the path */
5178 ++t;
5179#endif
5180 return t;
5181}
5182
5183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 * get the next path component (just after the next path separator).
5185 */
5186 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005187getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
5189 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005190 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (*fname)
5192 ++fname;
5193 return fname;
5194}
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196/*
5197 * Get a pointer to one character past the head of a path name.
5198 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5199 * If there is no head, path is returned.
5200 */
5201 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
5204 char_u *retval;
5205
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005206#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 /* may skip "c:" */
5208 if (isalpha(path[0]) && path[1] == ':')
5209 retval = path + 2;
5210 else
5211 retval = path;
5212#else
5213# if defined(AMIGA)
5214 /* may skip "label:" */
5215 retval = vim_strchr(path, ':');
5216 if (retval == NULL)
5217 retval = path;
5218# else /* Unix */
5219 retval = path;
5220# endif
5221#endif
5222
5223 while (vim_ispathsep(*retval))
5224 ++retval;
5225
5226 return retval;
5227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
5229/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005230 * Return TRUE if 'c' is a path separator.
5231 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 */
5233 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005234vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005236#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005238#else
5239# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005241# else
5242# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5244 return (c == ':' || c == '[' || c == ']' || c == '/'
5245 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005246# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005248# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251}
5252
Bram Moolenaar69c35002013-11-04 02:54:12 +01005253/*
5254 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5255 */
5256 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005257vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005258{
5259 return vim_ispathsep(c)
5260#ifdef BACKSLASH_IN_FILENAME
5261 && c != ':'
5262#endif
5263 ;
5264}
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5267/*
5268 * return TRUE if 'c' is a path list separator.
5269 */
5270 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005271vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273#ifdef UNIX
5274 return (c == ':');
5275#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005276 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#endif
5278}
5279#endif
5280
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005281/*
5282 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5283 * It's done in-place.
5284 */
5285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005287{
5288 char_u *tail, *s, *d;
5289 int skip = FALSE;
5290
5291 tail = gettail(str);
5292 d = str;
5293 for (s = str; ; ++s)
5294 {
5295 if (s >= tail) /* copy the whole tail */
5296 {
5297 *d++ = *s;
5298 if (*s == NUL)
5299 break;
5300 }
5301 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5302 {
5303 *d++ = *s;
5304 skip = FALSE;
5305 }
5306 else if (!skip)
5307 {
5308 *d++ = *s; /* copy next char */
5309 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5310 skip = TRUE;
5311# ifdef FEAT_MBYTE
5312 if (has_mbyte)
5313 {
5314 int l = mb_ptr2len(s);
5315
5316 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005317 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005318 }
5319# endif
5320 }
5321 }
5322}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005323
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005324/*
5325 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5326 * Also returns TRUE if there is no directory name.
5327 * "fname" must be writable!.
5328 */
5329 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005330dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005331{
5332 char_u *p;
5333 int c;
5334 int retval;
5335
5336 p = gettail_sep(fname);
5337 if (p == fname)
5338 return TRUE;
5339 c = *p;
5340 *p = NUL;
5341 retval = mch_isdir(fname);
5342 *p = c;
5343 return retval;
5344}
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005347 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5348 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 */
5350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005353#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005355#else
5356 if (p_fic)
5357 return MB_STRICMP(x, y);
5358 return STRCMP(x, y);
5359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360}
5361
5362 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005363vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005365#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005366 char_u *px = x;
5367 char_u *py = y;
5368 int cx = NUL;
5369 int cy = NUL;
5370
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005371 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005373 cx = PTR2CHAR(px);
5374 cy = PTR2CHAR(py);
5375 if (cx == NUL || cy == NUL
5376 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5377 && !(cx == '/' && cy == '\\')
5378 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005380 len -= MB_PTR2LEN(px);
5381 px += MB_PTR2LEN(px);
5382 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384 if (len == 0)
5385 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005386 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005387#else
5388 if (p_fic)
5389 return MB_STRNICMP(x, y, len);
5390 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
5394/*
5395 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005396 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 */
5398 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005399concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 char_u *dest;
5402
5403 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5404 if (dest != NULL)
5405 {
5406 STRCPY(dest, fname1);
5407 if (sep)
5408 add_pathsep(dest);
5409 STRCAT(dest, fname2);
5410 }
5411 return dest;
5412}
5413
Bram Moolenaard6754642005-01-17 22:18:45 +00005414/*
5415 * Concatenate two strings and return the result in allocated memory.
5416 * Returns NULL when out of memory.
5417 */
5418 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005419concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005420{
5421 char_u *dest;
5422 size_t l = STRLEN(str1);
5423
5424 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5425 if (dest != NULL)
5426 {
5427 STRCPY(dest, str1);
5428 STRCPY(dest + l, str2);
5429 }
5430 return dest;
5431}
Bram Moolenaard6754642005-01-17 22:18:45 +00005432
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433/*
5434 * Add a path separator to a file name, unless it already ends in a path
5435 * separator.
5436 */
5437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005438add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005440 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 STRCAT(p, PATHSEPSTR);
5442}
5443
5444/*
5445 * FullName_save - Make an allocated copy of a full file name.
5446 * Returns NULL when out of memory.
5447 */
5448 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005449FullName_save(
5450 char_u *fname,
5451 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005452 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454 char_u *buf;
5455 char_u *new_fname = NULL;
5456
5457 if (fname == NULL)
5458 return NULL;
5459
5460 buf = alloc((unsigned)MAXPATHL);
5461 if (buf != NULL)
5462 {
5463 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5464 new_fname = vim_strsave(buf);
5465 else
5466 new_fname = vim_strsave(fname);
5467 vim_free(buf);
5468 }
5469 return new_fname;
5470}
5471
5472#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5473
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005474static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005475static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477/*
5478 * Find the start of a comment, not knowing if we are in a comment right now.
5479 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005480 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005482 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005483ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005484{
5485 return find_start_comment(curbuf->b_ind_maxcomment);
5486}
5487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005489find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 pos_T *pos;
5492 char_u *line;
5493 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005494 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005496 for (;;)
5497 {
5498 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5499 if (pos == NULL)
5500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005502 /*
5503 * Check if the comment start we found is inside a string.
5504 * If it is then restrict the search to below this line and try again.
5505 */
5506 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005507 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005508 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005509 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005510 break;
5511 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5512 if (cur_maxcomment <= 0)
5513 {
5514 pos = NULL;
5515 break;
5516 }
5517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 return pos;
5519}
5520
5521/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005522 * Find the start of a comment or raw string, not knowing if we are in a
5523 * comment or raw string right now.
5524 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005525 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005526 * Return NULL when not inside a comment or raw string.
5527 * "CORS" -> Comment Or Raw String
5528 */
5529 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005530ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005531{
Bram Moolenaar089af182015-10-07 11:41:49 +02005532 static pos_T comment_pos_copy;
5533 pos_T *comment_pos;
5534 pos_T *rs_pos;
5535
5536 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5537 if (comment_pos != NULL)
5538 {
5539 /* Need to make a copy of the static pos in findmatchlimit(),
5540 * calling find_start_rawstring() may change it. */
5541 comment_pos_copy = *comment_pos;
5542 comment_pos = &comment_pos_copy;
5543 }
5544 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005545
5546 /* If comment_pos is before rs_pos the raw string is inside the comment.
5547 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005548 if (comment_pos == NULL || (rs_pos != NULL
5549 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005550 {
5551 if (is_raw != NULL && rs_pos != NULL)
5552 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005553 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005554 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005555 return comment_pos;
5556}
5557
5558/*
5559 * Find the start of a raw string, not knowing if we are in one right now.
5560 * Search starts at w_cursor.lnum and goes backwards.
5561 * Return NULL when not inside a raw string.
5562 */
5563 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005564find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005565{
5566 pos_T *pos;
5567 char_u *line;
5568 char_u *p;
5569 int cur_maxcomment = ind_maxcomment;
5570
5571 for (;;)
5572 {
5573 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5574 if (pos == NULL)
5575 break;
5576
5577 /*
5578 * Check if the raw string start we found is inside a string.
5579 * If it is then restrict the search to below this line and try again.
5580 */
5581 line = ml_get(pos->lnum);
5582 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5583 p = skip_string(p);
5584 if ((colnr_T)(p - line) <= pos->col)
5585 break;
5586 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5587 if (cur_maxcomment <= 0)
5588 {
5589 pos = NULL;
5590 break;
5591 }
5592 }
5593 return pos;
5594}
5595
5596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * Skip to the end of a "string" and a 'c' character.
5598 * If there is no string or character, return argument unmodified.
5599 */
5600 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005601skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 int i;
5604
5605 /*
5606 * We loop, because strings may be concatenated: "date""time".
5607 */
5608 for ( ; ; ++p)
5609 {
5610 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5611 {
5612 if (!p[1]) /* ' at end of line */
5613 break;
5614 i = 2;
5615 if (p[1] == '\\') /* '\n' or '\000' */
5616 {
5617 ++i;
5618 while (vim_isdigit(p[i - 1])) /* '\000' */
5619 ++i;
5620 }
5621 if (p[i] == '\'') /* check for trailing ' */
5622 {
5623 p += i;
5624 continue;
5625 }
5626 }
5627 else if (p[0] == '"') /* start of string */
5628 {
5629 for (++p; p[0]; ++p)
5630 {
5631 if (p[0] == '\\' && p[1] != NUL)
5632 ++p;
5633 else if (p[0] == '"') /* end of string */
5634 break;
5635 }
5636 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005637 continue; /* continue for another string */
5638 }
5639 else if (p[0] == 'R' && p[1] == '"')
5640 {
5641 /* Raw string: R"[delim](...)[delim]" */
5642 char_u *delim = p + 2;
5643 char_u *paren = vim_strchr(delim, '(');
5644
5645 if (paren != NULL)
5646 {
5647 size_t delim_len = paren - delim;
5648
5649 for (p += 3; *p; ++p)
5650 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5651 && p[delim_len + 1] == '"')
5652 {
5653 p += delim_len + 1;
5654 break;
5655 }
5656 if (p[0] == '"')
5657 continue; /* continue for another string */
5658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 break; /* no string found */
5661 }
5662 if (!*p)
5663 --p; /* backup from NUL */
5664 return p;
5665}
5666#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5667
5668#if defined(FEAT_CINDENT) || defined(PROTO)
5669
5670/*
5671 * Do C or expression indenting on the current line.
5672 */
5673 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005674do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676# ifdef FEAT_EVAL
5677 if (*curbuf->b_p_inde != NUL)
5678 fixthisline(get_expr_indent);
5679 else
5680# endif
5681 fixthisline(get_c_indent);
5682}
5683
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005684/* Find result cache for cpp_baseclass */
5685typedef struct {
5686 int found;
5687 lpos_T lpos;
5688} cpp_baseclass_cache_T;
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690/*
5691 * Functions for C-indenting.
5692 * Most of this originally comes from Eric Fischer.
5693 */
5694/*
5695 * Below "XXX" means that this function may unlock the current line.
5696 */
5697
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005698static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005699static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005700static int cin_iscomment(char_u *);
5701static int cin_islinecomment(char_u *);
5702static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005703static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005704static int cin_ends_in(char_u *, char_u *, char_u *);
5705static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005706static pos_T *find_match_paren(int);
5707static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005708static int find_last_paren(char_u *l, int start, int end);
5709static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
5711/*
5712 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005713 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 */
5715 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005716cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 while (*s)
5719 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005720 char_u *prev_s = s;
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005723
5724 /* Perl/shell # comment comment continues until eol. Require a space
5725 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005726 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005727 {
5728 s += STRLEN(s);
5729 break;
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (*s != '/')
5732 break;
5733 ++s;
5734 if (*s == '/') /* slash-slash comment continues till eol */
5735 {
5736 s += STRLEN(s);
5737 break;
5738 }
5739 if (*s != '*')
5740 break;
5741 for (++s; *s; ++s) /* skip slash-star comment */
5742 if (s[0] == '*' && s[1] == '/')
5743 {
5744 s += 2;
5745 break;
5746 }
5747 }
5748 return s;
5749}
5750
5751/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005752 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 * not considered code.
5754 */
5755 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005756cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758 return *cin_skipcomment(s) == NUL;
5759}
5760
5761/*
5762 * Check previous lines for a "//" line comment, skipping over blank lines.
5763 */
5764 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005765find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 static pos_T pos;
5768 char_u *line;
5769 char_u *p;
5770
5771 pos = curwin->w_cursor;
5772 while (--pos.lnum > 0)
5773 {
5774 line = ml_get(pos.lnum);
5775 p = skipwhite(line);
5776 if (cin_islinecomment(p))
5777 {
5778 pos.col = (int)(p - line);
5779 return &pos;
5780 }
5781 if (*p != NUL)
5782 break;
5783 }
5784 return NULL;
5785}
5786
5787/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005788 * Return TRUE if "text" starts with "key:".
5789 */
5790 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005791cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005792{
5793 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005794 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005795
5796 if (*s == '\'' || *s == '"')
5797 {
5798 /* can be 'key': or "key": */
5799 quote = *s;
5800 ++s;
5801 }
5802 if (!vim_isIDc(*s)) /* need at least one ID character */
5803 return FALSE;
5804
5805 while (vim_isIDc(*s))
5806 ++s;
5807 if (*s == quote)
5808 ++s;
5809
5810 s = cin_skipcomment(s);
5811
5812 /* "::" is not a label, it's C++ */
5813 return (*s == ':' && s[1] != ':');
5814}
5815
5816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005818 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 */
5820 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005821cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
5823 if (!vim_isIDc(**s)) /* need at least one ID character */
5824 return FALSE;
5825
5826 while (vim_isIDc(**s))
5827 (*s)++;
5828
5829 *s = cin_skipcomment(*s);
5830
5831 /* "::" is not a label, it's C++ */
5832 return (**s == ':' && *++*s != ':');
5833}
5834
5835/*
5836 * Recognize a label: "label:".
5837 * Note: curwin->w_cursor must be where we are looking for the label.
5838 */
5839 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005840cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 char_u *s;
5843
5844 s = cin_skipcomment(ml_get_curline());
5845
5846 /*
5847 * Exclude "default" from labels, since it should be indented
5848 * like a switch label. Same for C++ scope declarations.
5849 */
5850 if (cin_isdefault(s))
5851 return FALSE;
5852 if (cin_isscopedecl(s))
5853 return FALSE;
5854
5855 if (cin_islabel_skip(&s))
5856 {
5857 /*
5858 * Only accept a label if the previous line is terminated or is a case
5859 * label.
5860 */
5861 pos_T cursor_save;
5862 pos_T *trypos;
5863 char_u *line;
5864
5865 cursor_save = curwin->w_cursor;
5866 while (curwin->w_cursor.lnum > 1)
5867 {
5868 --curwin->w_cursor.lnum;
5869
5870 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005871 * If we're in a comment or raw string now, skip to the start of
5872 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 */
5874 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005875 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 curwin->w_cursor = *trypos;
5877
5878 line = ml_get_curline();
5879 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5880 continue;
5881 if (*(line = cin_skipcomment(line)) == NUL)
5882 continue;
5883
5884 curwin->w_cursor = cursor_save;
5885 if (cin_isterminated(line, TRUE, FALSE)
5886 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005887 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 || (cin_islabel_skip(&line) && cin_nocode(line)))
5889 return TRUE;
5890 return FALSE;
5891 }
5892 curwin->w_cursor = cursor_save;
5893 return TRUE; /* label at start of file??? */
5894 }
5895 return FALSE;
5896}
5897
5898/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005899 * Recognize structure initialization and enumerations:
5900 * "[typedef] [static|public|protected|private] enum"
5901 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 */
5903 static int
5904cin_isinit(void)
5905{
5906 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005907 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
5909 s = cin_skipcomment(ml_get_curline());
5910
Bram Moolenaar75342212013-03-07 13:13:52 +01005911 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 s = cin_skipcomment(s + 7);
5913
Bram Moolenaar75342212013-03-07 13:13:52 +01005914 for (;;)
5915 {
5916 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005917
Bram Moolenaar75342212013-03-07 13:13:52 +01005918 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5919 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005920 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005921 if (cin_starts_with(s, skip[i]))
5922 {
5923 s = cin_skipcomment(s + l);
5924 l = 0;
5925 break;
5926 }
5927 }
5928 if (l != 0)
5929 break;
5930 }
5931
5932 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 return TRUE;
5934
5935 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5936 return TRUE;
5937
5938 return FALSE;
5939}
5940
5941/*
5942 * Recognize a switch label: "case .*:" or "default:".
5943 */
5944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005945cin_iscase(
5946 char_u *s,
5947 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005950 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
5952 for (s += 4; *s; ++s)
5953 {
5954 s = cin_skipcomment(s);
5955 if (*s == ':')
5956 {
5957 if (s[1] == ':') /* skip over "::" for C++ */
5958 ++s;
5959 else
5960 return TRUE;
5961 }
5962 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005963 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5965 return FALSE; /* stop at comment */
5966 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005967 {
5968 /* JS etc. */
5969 if (strict)
5970 return FALSE; /* stop at string */
5971 else
5972 return TRUE;
5973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
5975 return FALSE;
5976 }
5977
5978 if (cin_isdefault(s))
5979 return TRUE;
5980 return FALSE;
5981}
5982
5983/*
5984 * Recognize a "default" switch label.
5985 */
5986 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005987cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 return (STRNCMP(s, "default", 7) == 0
5990 && *(s = cin_skipcomment(s + 7)) == ':'
5991 && s[1] != ':');
5992}
5993
5994/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005995 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 */
5997 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005998cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 int i;
6001
6002 s = cin_skipcomment(s);
6003 if (STRNCMP(s, "public", 6) == 0)
6004 i = 6;
6005 else if (STRNCMP(s, "protected", 9) == 0)
6006 i = 9;
6007 else if (STRNCMP(s, "private", 7) == 0)
6008 i = 7;
6009 else
6010 return FALSE;
6011 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6012}
6013
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006014/* Maximum number of lines to search back for a "namespace" line. */
6015#define FIND_NAMESPACE_LIM 20
6016
6017/*
6018 * Recognize a "namespace" scope declaration.
6019 */
6020 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006021cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006022{
6023 char_u *p;
6024 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006025 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006026
6027 s = cin_skipcomment(s);
6028 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6029 {
6030 p = cin_skipcomment(skipwhite(s + 9));
6031 while (*p != NUL)
6032 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006033 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006034 {
6035 has_name = TRUE; /* found end of a name */
6036 p = cin_skipcomment(skipwhite(p));
6037 }
6038 else if (*p == '{')
6039 {
6040 break;
6041 }
6042 else if (vim_iswordc(*p))
6043 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006044 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006045 if (has_name)
6046 return FALSE; /* word character after skipping past name */
6047 ++p;
6048 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006049 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6050 {
6051 if (!has_name_start || has_name)
6052 return FALSE;
6053 /* C++ 17 nested namespace */
6054 p += 3;
6055 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006056 else
6057 {
6058 return FALSE;
6059 }
6060 }
6061 return TRUE;
6062 }
6063 return FALSE;
6064}
6065
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006067 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6068 */
6069 static int
6070cin_is_cpp_extern_c(char_u *s)
6071{
6072 char_u *p;
6073 int has_string_literal = FALSE;
6074
6075 s = cin_skipcomment(s);
6076 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6077 {
6078 p = cin_skipcomment(skipwhite(s + 6));
6079 while (*p != NUL)
6080 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006081 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006082 {
6083 p = cin_skipcomment(skipwhite(p));
6084 }
6085 else if (*p == '{')
6086 {
6087 break;
6088 }
6089 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6090 {
6091 if (has_string_literal)
6092 return FALSE;
6093 has_string_literal = TRUE;
6094 p += 3;
6095 }
6096 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6097 && p[4] == '"')
6098 {
6099 if (has_string_literal)
6100 return FALSE;
6101 has_string_literal = TRUE;
6102 p += 5;
6103 }
6104 else
6105 {
6106 return FALSE;
6107 }
6108 }
6109 return has_string_literal ? TRUE : FALSE;
6110 }
6111 return FALSE;
6112}
6113
6114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 * Return a pointer to the first non-empty non-comment character after a ':'.
6116 * Return NULL if not found.
6117 * case 234: a = b;
6118 * ^
6119 */
6120 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006121after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 for ( ; *l; ++l)
6124 {
6125 if (*l == ':')
6126 {
6127 if (l[1] == ':') /* skip over "::" for C++ */
6128 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006129 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 break;
6131 }
6132 else if (*l == '\'' && l[1] && l[2] == '\'')
6133 l += 2; /* skip over 'x' */
6134 }
6135 if (*l == NUL)
6136 return NULL;
6137 l = cin_skipcomment(l + 1);
6138 if (*l == NUL)
6139 return NULL;
6140 return l;
6141}
6142
6143/*
6144 * Get indent of line "lnum", skipping a label.
6145 * Return 0 if there is nothing after the label.
6146 */
6147 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006148get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
6150 char_u *l;
6151 pos_T fp;
6152 colnr_T col;
6153 char_u *p;
6154
6155 l = ml_get(lnum);
6156 p = after_label(l);
6157 if (p == NULL)
6158 return 0;
6159
6160 fp.col = (colnr_T)(p - l);
6161 fp.lnum = lnum;
6162 getvcol(curwin, &fp, &col, NULL, NULL);
6163 return (int)col;
6164}
6165
6166/*
6167 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006168 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 * label: if (asdf && asdfasdf)
6170 * ^
6171 */
6172 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006173skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
6175 char_u *l;
6176 int amount;
6177 pos_T cursor_save;
6178
6179 cursor_save = curwin->w_cursor;
6180 curwin->w_cursor.lnum = lnum;
6181 l = ml_get_curline();
6182 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006183 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
6185 amount = get_indent_nolabel(lnum);
6186 l = after_label(ml_get_curline());
6187 if (l == NULL) /* just in case */
6188 l = ml_get_curline();
6189 }
6190 else
6191 {
6192 amount = get_indent();
6193 l = ml_get_curline();
6194 }
6195 *pp = l;
6196
6197 curwin->w_cursor = cursor_save;
6198 return amount;
6199}
6200
6201/*
6202 * Return the indent of the first variable name after a type in a declaration.
6203 * int a, indent of "a"
6204 * static struct foo b, indent of "b"
6205 * enum bla c, indent of "c"
6206 * Returns zero when it doesn't look like a declaration.
6207 */
6208 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006209cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211 char_u *line, *p, *s;
6212 int len;
6213 pos_T fp;
6214 colnr_T col;
6215
6216 line = ml_get_curline();
6217 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006218 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6220 {
6221 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006222 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6225 p = skipwhite(p + 6);
6226 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6227 p = skipwhite(p + 4);
6228 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6229 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6230 {
6231 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006232 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6233 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6234 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6235 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 p = s;
6237 }
6238 for (len = 0; vim_isIDc(p[len]); ++len)
6239 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006240 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 return 0;
6242
6243 p = skipwhite(p + len);
6244 fp.lnum = curwin->w_cursor.lnum;
6245 fp.col = (colnr_T)(p - line);
6246 getvcol(curwin, &fp, &col, NULL, NULL);
6247 return (int)col;
6248}
6249
6250/*
6251 * Return the indent of the first non-blank after an equal sign.
6252 * char *foo = "here";
6253 * Return zero if no (useful) equal sign found.
6254 * Return -1 if the line above "lnum" ends in a backslash.
6255 * foo = "asdf\
6256 * asdf\
6257 * here";
6258 */
6259 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006260cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 char_u *line;
6263 char_u *s;
6264 colnr_T col;
6265 pos_T fp;
6266
6267 if (lnum > 1)
6268 {
6269 line = ml_get(lnum - 1);
6270 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6271 return -1;
6272 }
6273
6274 line = s = ml_get(lnum);
6275 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6276 {
6277 if (cin_iscomment(s)) /* ignore comments */
6278 s = cin_skipcomment(s);
6279 else
6280 ++s;
6281 }
6282 if (*s != '=')
6283 return 0;
6284
6285 s = skipwhite(s + 1);
6286 if (cin_nocode(s))
6287 return 0;
6288
6289 if (*s == '"') /* nice alignment for continued strings */
6290 ++s;
6291
6292 fp.lnum = lnum;
6293 fp.col = (colnr_T)(s - line);
6294 getvcol(curwin, &fp, &col, NULL, NULL);
6295 return (int)col;
6296}
6297
6298/*
6299 * Recognize a preprocessor statement: Any line that starts with '#'.
6300 */
6301 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006302cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006304 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 return TRUE;
6306 return FALSE;
6307}
6308
6309/*
6310 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6311 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6312 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006313 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 */
6315 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006316cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 char_u *line = *pp;
6319 linenr_T lnum = *lnump;
6320 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006321 int candidate_amount = *amount;
6322
6323 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6324 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006326 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
6328 if (cin_ispreproc(line))
6329 {
6330 retval = TRUE;
6331 *lnump = lnum;
6332 break;
6333 }
6334 if (lnum == 1)
6335 break;
6336 line = ml_get(--lnum);
6337 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6338 break;
6339 }
6340
6341 if (lnum != *lnump)
6342 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006343 if (retval)
6344 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 return retval;
6346}
6347
6348/*
6349 * Recognize the start of a C or C++ comment.
6350 */
6351 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006352cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6355}
6356
6357/*
6358 * Recognize the start of a "//" comment.
6359 */
6360 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006361cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 return (p[0] == '/' && p[1] == '/');
6364}
6365
6366/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006367 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6368 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006370 * If a line begins with an "else", only consider it terminated if no unmatched
6371 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 * Return the character terminating the line (ending char's have precedence if
6373 * both apply in order to determine initializations).
6374 */
6375 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006376cin_isterminated(
6377 char_u *s,
6378 int incl_open, /* include '{' at the end as terminator */
6379 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006381 char_u found_start = 0;
6382 unsigned n_open = 0;
6383 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
6385 s = cin_skipcomment(s);
6386
6387 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6388 found_start = *s;
6389
Bram Moolenaar496f9512011-05-19 16:35:09 +02006390 if (!found_start)
6391 is_else = cin_iselse(s);
6392
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 while (*s)
6394 {
6395 /* skip over comments, "" strings and 'c'haracters */
6396 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006397 if (*s == '}' && n_open > 0)
6398 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006399 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006400 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 && cin_nocode(s + 1))
6402 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006403 else if (*s == '{')
6404 {
6405 if (incl_open && cin_nocode(s + 1))
6406 return *s;
6407 else
6408 ++n_open;
6409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411 if (*s)
6412 s++;
6413 }
6414 return found_start;
6415}
6416
6417/*
6418 * Recognize the basic picture of a function declaration -- it needs to
6419 * have an open paren somewhere and a close paren at the end of the line and
6420 * no semicolons anywhere.
6421 * When a line ends in a comma we continue looking in the next line.
6422 * "sp" points to a string with the line. When looking at other lines it must
6423 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006424 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006425 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 */
6427 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006428cin_isfuncdecl(
6429 char_u **sp,
6430 linenr_T first_lnum,
6431 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 char_u *s;
6434 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006435 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006437 pos_T *trypos;
6438 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
6440 if (sp == NULL)
6441 s = ml_get(lnum);
6442 else
6443 s = *sp;
6444
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006445 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006446 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006447 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006448 {
6449 lnum = trypos->lnum;
6450 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006451 {
6452 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006453 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006454 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006455
6456 s = ml_get(lnum);
6457 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006458 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006459
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006460 /* Ignore line starting with #. */
6461 if (cin_ispreproc(s))
6462 return FALSE;
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6465 {
6466 if (cin_iscomment(s)) /* ignore comments */
6467 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006468 else if (*s == ':')
6469 {
6470 if (*(s + 1) == ':')
6471 s += 2;
6472 else
6473 /* To avoid a mistake in the following situation:
6474 * A::A(int a, int b)
6475 * : a(0) // <--not a function decl
6476 * , b(0)
6477 * {...
6478 */
6479 return FALSE;
6480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 else
6482 ++s;
6483 }
6484 if (*s != '(')
6485 return FALSE; /* ';', ' or " before any () or no '(' */
6486
6487 while (*s && *s != ';' && *s != '\'' && *s != '"')
6488 {
6489 if (*s == ')' && cin_nocode(s + 1))
6490 {
6491 /* ')' at the end: may have found a match
6492 * Check for he previous line not to end in a backslash:
6493 * #if defined(x) && \
6494 * defined(y)
6495 */
6496 lnum = first_lnum - 1;
6497 s = ml_get(lnum);
6498 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6499 retval = TRUE;
6500 goto done;
6501 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006502 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006504 int comma = (*s == ',');
6505
6506 /* ',' at the end: continue looking in the next line.
6507 * At the end: check for ',' in the next line, for this style:
6508 * func(arg1
6509 * , arg2) */
6510 for (;;)
6511 {
6512 if (lnum >= curbuf->b_ml.ml_line_count)
6513 break;
6514 s = ml_get(++lnum);
6515 if (!cin_ispreproc(s))
6516 break;
6517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (lnum >= curbuf->b_ml.ml_line_count)
6519 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006520 /* Require a comma at end of the line or a comma or ')' at the
6521 * start of next line. */
6522 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006523 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006524 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006525 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527 else if (cin_iscomment(s)) /* ignore comments */
6528 s = cin_skipcomment(s);
6529 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006532 just_started = FALSE;
6533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535
6536done:
6537 if (lnum != first_lnum && sp != NULL)
6538 *sp = ml_get(first_lnum);
6539
6540 return retval;
6541}
6542
6543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006544cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006546 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547}
6548
6549 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006550cin_iselse(
6551 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
6553 if (*p == '}') /* accept "} else" */
6554 p = cin_skipcomment(p + 1);
6555 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6556}
6557
6558 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006559cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6562}
6563
6564/*
6565 * Check if this is a "while" that should have a matching "do".
6566 * We only accept a "while (condition) ;", with only white space between the
6567 * ')' and ';'. The condition may be spread over several lines.
6568 */
6569 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006570cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 pos_T cursor_save;
6573 pos_T *trypos;
6574 int retval = FALSE;
6575
6576 p = cin_skipcomment(p);
6577 if (*p == '}') /* accept "} while (cond);" */
6578 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006579 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
6581 cursor_save = curwin->w_cursor;
6582 curwin->w_cursor.lnum = lnum;
6583 curwin->w_cursor.col = 0;
6584 p = ml_get_curline();
6585 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6586 {
6587 ++p;
6588 ++curwin->w_cursor.col;
6589 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006590 if ((trypos = findmatchlimit(NULL, 0, 0,
6591 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6593 retval = TRUE;
6594 curwin->w_cursor = cursor_save;
6595 }
6596 return retval;
6597}
6598
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006599/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006600 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006601 * Return 0 if there is none.
6602 * Otherwise return !0 and update "*poffset" to point to the place where the
6603 * string was found.
6604 */
6605 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006606cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006607{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006608 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006609
6610 if (offset-- < 2)
6611 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006612 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006613 --offset;
6614
6615 offset -= 1;
6616 if (!STRNCMP(line + offset, "if", 2))
6617 goto probablyFound;
6618
6619 if (offset >= 1)
6620 {
6621 offset -= 1;
6622 if (!STRNCMP(line + offset, "for", 3))
6623 goto probablyFound;
6624
6625 if (offset >= 2)
6626 {
6627 offset -= 2;
6628 if (!STRNCMP(line + offset, "while", 5))
6629 goto probablyFound;
6630 }
6631 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006632 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006633
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006634probablyFound:
6635 if (!offset || !vim_isIDc(line[offset - 1]))
6636 {
6637 *poffset = offset;
6638 return 1;
6639 }
6640 return 0;
6641}
6642
6643/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006644 * Return TRUE if we are at the end of a do-while.
6645 * do
6646 * nothing;
6647 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006648 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006649 * Adjust the cursor to the line with "while".
6650 */
6651 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006652cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006653{
6654 char_u *line;
6655 char_u *p;
6656 char_u *s;
6657 pos_T *trypos;
6658 int i;
6659
6660 if (terminated != ';') /* there must be a ';' at the end */
6661 return FALSE;
6662
6663 p = line = ml_get_curline();
6664 while (*p != NUL)
6665 {
6666 p = cin_skipcomment(p);
6667 if (*p == ')')
6668 {
6669 s = skipwhite(p + 1);
6670 if (*s == ';' && cin_nocode(s + 1))
6671 {
6672 /* Found ");" at end of the line, now check there is "while"
6673 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006674 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006675 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006676 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006677 if (trypos != NULL)
6678 {
6679 s = cin_skipcomment(ml_get(trypos->lnum));
6680 if (*s == '}') /* accept "} while (cond);" */
6681 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006682 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006683 {
6684 curwin->w_cursor.lnum = trypos->lnum;
6685 return TRUE;
6686 }
6687 }
6688
6689 /* Searching may have made "line" invalid, get it again. */
6690 line = ml_get_curline();
6691 p = line + i;
6692 }
6693 }
6694 if (*p != NUL)
6695 ++p;
6696 }
6697 return FALSE;
6698}
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6704}
6705
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006706/*
6707 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 * constructor-initialization. eg:
6709 *
6710 * class MyClass :
6711 * baseClass <-- here
6712 * class MyClass : public baseClass,
6713 * anotherBaseClass <-- here (should probably lineup ??)
6714 * MyClass::MyClass(...) :
6715 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006716 *
6717 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 */
6719 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006720cin_is_cpp_baseclass(
6721 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006723 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 char_u *s;
6725 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006726 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006727 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006729 if (pos->lnum <= lnum)
6730 return cached->found; /* Use the cached result */
6731
6732 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006734 s = skipwhite(line);
6735 if (*s == '#') /* skip #define FOO x ? (x) : x */
6736 return FALSE;
6737 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (*s == NUL)
6739 return FALSE;
6740
6741 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6742
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006743 /* Search for a line starting with '#', empty, ending in ';' or containing
6744 * '{' or '}' and start below it. This handles the following situations:
6745 * a = cond ?
6746 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006747 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006748 * func::foo()
6749 * : something
6750 * {}
6751 * Foo::Foo (int one, int two)
6752 * : something(4),
6753 * somethingelse(3)
6754 * {}
6755 */
6756 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006758 line = ml_get(lnum - 1);
6759 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006760 if (*s == '#' || *s == NUL)
6761 break;
6762 while (*s != NUL)
6763 {
6764 s = cin_skipcomment(s);
6765 if (*s == '{' || *s == '}'
6766 || (*s == ';' && cin_nocode(s + 1)))
6767 break;
6768 if (*s != NUL)
6769 ++s;
6770 }
6771 if (*s != NUL)
6772 break;
6773 --lnum;
6774 }
6775
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006776 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006777 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006778 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006779 for (;;)
6780 {
6781 if (*s == NUL)
6782 {
6783 if (lnum == curwin->w_cursor.lnum)
6784 break;
6785 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006786 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006787 s = line;
6788 }
6789 if (s == line)
6790 {
6791 /* don't recognize "case (foo):" as a baseclass */
6792 if (cin_iscase(s, FALSE))
6793 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006794 s = cin_skipcomment(line);
6795 if (*s == NUL)
6796 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006797 }
6798
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006799 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006800 s = skip_string(s) + 1;
6801 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
6803 if (s[1] == ':')
6804 {
6805 /* skip double colon. It can't be a constructor
6806 * initialization any more */
6807 lookfor_ctor_init = FALSE;
6808 s = cin_skipcomment(s + 2);
6809 }
6810 else if (lookfor_ctor_init || class_or_struct)
6811 {
6812 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006813 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 cpp_base_class = TRUE;
6815 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006816 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 s = cin_skipcomment(s + 1);
6818 }
6819 else
6820 s = cin_skipcomment(s + 1);
6821 }
6822 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6823 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6824 {
6825 class_or_struct = TRUE;
6826 lookfor_ctor_init = FALSE;
6827
6828 if (*s == 'c')
6829 s = cin_skipcomment(s + 5);
6830 else
6831 s = cin_skipcomment(s + 6);
6832 }
6833 else
6834 {
6835 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6836 {
6837 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6838 }
6839 else if (s[0] == ')')
6840 {
6841 /* Constructor-initialization is assumed if we come across
6842 * something like "):" */
6843 class_or_struct = FALSE;
6844 lookfor_ctor_init = TRUE;
6845 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006846 else if (s[0] == '?')
6847 {
6848 /* Avoid seeing '() :' after '?' as constructor init. */
6849 return FALSE;
6850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 else if (!vim_isIDc(s[0]))
6852 {
6853 /* if it is not an identifier, we are wrong */
6854 class_or_struct = FALSE;
6855 lookfor_ctor_init = FALSE;
6856 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006857 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
6859 /* it can't be a constructor-initialization any more */
6860 lookfor_ctor_init = FALSE;
6861
6862 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006863 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006864 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006867 /* When the line ends in a comma don't align with it. */
6868 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006869 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006870
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 s = cin_skipcomment(s + 1);
6872 }
6873 }
6874
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006875 cached->found = cpp_base_class;
6876 if (cpp_base_class)
6877 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 return cpp_base_class;
6879}
6880
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006881 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006882get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006883{
6884 int amount;
6885 colnr_T vcol;
6886 pos_T *trypos;
6887
6888 if (col == 0)
6889 {
6890 amount = get_indent();
6891 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006892 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006893 amount = get_indent_lnum(trypos->lnum); /* XXX */
6894 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006895 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006896 }
6897 else
6898 {
6899 curwin->w_cursor.col = col;
6900 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6901 amount = (int)vcol;
6902 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006903 if (amount < curbuf->b_ind_cpp_baseclass)
6904 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006905 return amount;
6906}
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908/*
6909 * Return TRUE if string "s" ends with the string "find", possibly followed by
6910 * white space and comments. Skip strings and comments.
6911 * Ignore "ignore" after "find" if it's not NULL.
6912 */
6913 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006914cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 char_u *p = s;
6917 char_u *r;
6918 int len = (int)STRLEN(find);
6919
6920 while (*p != NUL)
6921 {
6922 p = cin_skipcomment(p);
6923 if (STRNCMP(p, find, len) == 0)
6924 {
6925 r = skipwhite(p + len);
6926 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6927 r = skipwhite(r + STRLEN(ignore));
6928 if (cin_nocode(r))
6929 return TRUE;
6930 }
6931 if (*p != NUL)
6932 ++p;
6933 }
6934 return FALSE;
6935}
6936
6937/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006938 * Return TRUE when "s" starts with "word" and then a non-ID character.
6939 */
6940 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006941cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006942{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006943 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006944
6945 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6946}
6947
6948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 * Skip strings, chars and comments until at or past "trypos".
6950 * Return the column found.
6951 */
6952 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006953cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
6955 char_u *line;
6956 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006957 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
6959 p = line = ml_get(trypos->lnum);
6960 while (*p && (colnr_T)(p - line) < trypos->col)
6961 {
6962 if (cin_iscomment(p))
6963 p = cin_skipcomment(p);
6964 else
6965 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006966 new_p = skip_string(p);
6967 if (new_p == p)
6968 ++p;
6969 else
6970 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 }
6972 }
6973 return (int)(p - line);
6974}
6975
6976/*
6977 * Find the '{' at the start of the block we are in.
6978 * Return NULL if no match found.
6979 * Ignore a '{' that is in a comment, makes indenting the next three lines
6980 * work. */
6981/* foo() */
6982/* { */
6983/* } */
6984
6985 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006986find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 pos_T cursor_save;
6989 pos_T *trypos;
6990 pos_T *pos;
6991 static pos_T pos_copy;
6992
6993 cursor_save = curwin->w_cursor;
6994 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6995 {
6996 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6997 trypos = &pos_copy;
6998 curwin->w_cursor = *trypos;
6999 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007000 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007002 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 break;
7004 if (pos != NULL)
7005 curwin->w_cursor.lnum = pos->lnum;
7006 }
7007 curwin->w_cursor = cursor_save;
7008 return trypos;
7009}
7010
7011/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007012 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007013 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 */
7015 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007016find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007018 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007019}
7020
7021 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007022find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007023{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 pos_T cursor_save;
7025 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007026 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007027 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
7029 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007030 ind_maxp_wk = ind_maxparen;
7031retry:
7032 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
7034 /* check if the ( is in a // comment */
7035 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007036 {
7037 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7038 if (ind_maxp_wk > 0)
7039 {
7040 curwin->w_cursor = *trypos;
7041 curwin->w_cursor.col = 0; /* XXX */
7042 goto retry;
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 else
7047 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007048 pos_T *trypos_wk;
7049
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7051 trypos = &pos_copy;
7052 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007053 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007054 {
7055 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7056 - trypos_wk->lnum);
7057 if (ind_maxp_wk > 0)
7058 {
7059 curwin->w_cursor = *trypos_wk;
7060 goto retry;
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065 }
7066 curwin->w_cursor = cursor_save;
7067 return trypos;
7068}
7069
7070/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007071 * Find the matching '(', ignoring it if it is in a comment or before an
7072 * unmatched {.
7073 * Return NULL if no match found.
7074 */
7075 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007076find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007077{
7078 pos_T *trypos = find_match_paren(ind_maxparen);
7079
7080 if (trypos != NULL)
7081 {
7082 pos_T *tryposBrace = find_start_brace();
7083
7084 /* If both an unmatched '(' and '{' is found. Ignore the '('
7085 * position if the '{' is further down. */
7086 if (tryposBrace != NULL
7087 && (trypos->lnum != tryposBrace->lnum
7088 ? trypos->lnum < tryposBrace->lnum
7089 : trypos->col < tryposBrace->col))
7090 trypos = NULL;
7091 }
7092 return trypos;
7093}
7094
7095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 * Return ind_maxparen corrected for the difference in line number between the
7097 * cursor position and "startpos". This makes sure that searching for a
7098 * matching paren above the cursor line doesn't find a match because of
7099 * looking a few lines further.
7100 */
7101 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007102corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7105
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007106 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7107 return curbuf->b_ind_maxparen - (int)n;
7108 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109}
7110
7111/*
7112 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007113 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 */
7115 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007116find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 int i;
7119 int retval = FALSE;
7120 int open_count = 0;
7121
7122 curwin->w_cursor.col = 0; /* default is start of line */
7123
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007124 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
7126 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7127 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7128 if (l[i] == start)
7129 ++open_count;
7130 else if (l[i] == end)
7131 {
7132 if (open_count > 0)
7133 --open_count;
7134 else
7135 {
7136 curwin->w_cursor.col = i;
7137 retval = TRUE;
7138 }
7139 }
7140 }
7141 return retval;
7142}
7143
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007144/*
7145 * Parse 'cinoptions' and set the values in "curbuf".
7146 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7147 */
7148 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007149parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007150{
7151 char_u *p;
7152 char_u *l;
7153 char_u *digits;
7154 int n;
7155 int divider;
7156 int fraction = 0;
7157 int sw = (int)get_sw_value(buf);
7158
7159 /*
7160 * Set the default values.
7161 */
7162 /* Spaces from a block's opening brace the prevailing indent for that
7163 * block should be. */
7164 buf->b_ind_level = sw;
7165
7166 /* Spaces from the edge of the line an open brace that's at the end of a
7167 * line is imagined to be. */
7168 buf->b_ind_open_imag = 0;
7169
7170 /* Spaces from the prevailing indent for a line that is not preceded by
7171 * an opening brace. */
7172 buf->b_ind_no_brace = 0;
7173
7174 /* Column where the first { of a function should be located }. */
7175 buf->b_ind_first_open = 0;
7176
7177 /* Spaces from the prevailing indent a leftmost open brace should be
7178 * located. */
7179 buf->b_ind_open_extra = 0;
7180
7181 /* Spaces from the matching open brace (real location for one at the left
7182 * edge; imaginary location from one that ends a line) the matching close
7183 * brace should be located. */
7184 buf->b_ind_close_extra = 0;
7185
7186 /* Spaces from the edge of the line an open brace sitting in the leftmost
7187 * column is imagined to be. */
7188 buf->b_ind_open_left_imag = 0;
7189
7190 /* Spaces jump labels should be shifted to the left if N is non-negative,
7191 * otherwise the jump label will be put to column 1. */
7192 buf->b_ind_jump_label = -1;
7193
7194 /* Spaces from the switch() indent a "case xx" label should be located. */
7195 buf->b_ind_case = sw;
7196
7197 /* Spaces from the "case xx:" code after a switch() should be located. */
7198 buf->b_ind_case_code = sw;
7199
7200 /* Lineup break at end of case in switch() with case label. */
7201 buf->b_ind_case_break = 0;
7202
7203 /* Spaces from the class declaration indent a scope declaration label
7204 * should be located. */
7205 buf->b_ind_scopedecl = sw;
7206
7207 /* Spaces from the scope declaration label code should be located. */
7208 buf->b_ind_scopedecl_code = sw;
7209
7210 /* Amount K&R-style parameters should be indented. */
7211 buf->b_ind_param = sw;
7212
7213 /* Amount a function type spec should be indented. */
7214 buf->b_ind_func_type = sw;
7215
7216 /* Amount a cpp base class declaration or constructor initialization
7217 * should be indented. */
7218 buf->b_ind_cpp_baseclass = sw;
7219
7220 /* additional spaces beyond the prevailing indent a continuation line
7221 * should be located. */
7222 buf->b_ind_continuation = sw;
7223
7224 /* Spaces from the indent of the line with an unclosed parentheses. */
7225 buf->b_ind_unclosed = sw * 2;
7226
7227 /* Spaces from the indent of the line with an unclosed parentheses, which
7228 * itself is also unclosed. */
7229 buf->b_ind_unclosed2 = sw;
7230
7231 /* Suppress ignoring spaces from the indent of a line starting with an
7232 * unclosed parentheses. */
7233 buf->b_ind_unclosed_noignore = 0;
7234
7235 /* If the opening paren is the last nonwhite character on the line, and
7236 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7237 * context (for very long lines). */
7238 buf->b_ind_unclosed_wrapped = 0;
7239
7240 /* Suppress ignoring white space when lining up with the character after
7241 * an unclosed parentheses. */
7242 buf->b_ind_unclosed_whiteok = 0;
7243
7244 /* Indent a closing parentheses under the line start of the matching
7245 * opening parentheses. */
7246 buf->b_ind_matching_paren = 0;
7247
7248 /* Indent a closing parentheses under the previous line. */
7249 buf->b_ind_paren_prev = 0;
7250
7251 /* Extra indent for comments. */
7252 buf->b_ind_comment = 0;
7253
7254 /* Spaces from the comment opener when there is nothing after it. */
7255 buf->b_ind_in_comment = 3;
7256
7257 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7258 * after the comment opener. */
7259 buf->b_ind_in_comment2 = 0;
7260
7261 /* Max lines to search for an open paren. */
7262 buf->b_ind_maxparen = 20;
7263
7264 /* Max lines to search for an open comment. */
7265 buf->b_ind_maxcomment = 70;
7266
7267 /* Handle braces for java code. */
7268 buf->b_ind_java = 0;
7269
7270 /* Not to confuse JS object properties with labels. */
7271 buf->b_ind_js = 0;
7272
7273 /* Handle blocked cases correctly. */
7274 buf->b_ind_keep_case_label = 0;
7275
7276 /* Handle C++ namespace. */
7277 buf->b_ind_cpp_namespace = 0;
7278
7279 /* Handle continuation lines containing conditions of if(), for() and
7280 * while(). */
7281 buf->b_ind_if_for_while = 0;
7282
Bram Moolenaar6b643942017-03-05 19:44:06 +01007283 /* indentation for # comments */
7284 buf->b_ind_hash_comment = 0;
7285
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007286 /* Handle C++ extern "C" or "C++" */
7287 buf->b_ind_cpp_extern_c = 0;
7288
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007289 for (p = buf->b_p_cino; *p; )
7290 {
7291 l = p++;
7292 if (*p == '-')
7293 ++p;
7294 digits = p; /* remember where the digits start */
7295 n = getdigits(&p);
7296 divider = 0;
7297 if (*p == '.') /* ".5s" means a fraction */
7298 {
7299 fraction = atol((char *)++p);
7300 while (VIM_ISDIGIT(*p))
7301 {
7302 ++p;
7303 if (divider)
7304 divider *= 10;
7305 else
7306 divider = 10;
7307 }
7308 }
7309 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7310 {
7311 if (p == digits)
7312 n = sw; /* just "s" is one 'shiftwidth' */
7313 else
7314 {
7315 n *= sw;
7316 if (divider)
7317 n += (sw * fraction + divider / 2) / divider;
7318 }
7319 ++p;
7320 }
7321 if (l[1] == '-')
7322 n = -n;
7323
7324 /* When adding an entry here, also update the default 'cinoptions' in
7325 * doc/indent.txt, and add explanation for it! */
7326 switch (*l)
7327 {
7328 case '>': buf->b_ind_level = n; break;
7329 case 'e': buf->b_ind_open_imag = n; break;
7330 case 'n': buf->b_ind_no_brace = n; break;
7331 case 'f': buf->b_ind_first_open = n; break;
7332 case '{': buf->b_ind_open_extra = n; break;
7333 case '}': buf->b_ind_close_extra = n; break;
7334 case '^': buf->b_ind_open_left_imag = n; break;
7335 case 'L': buf->b_ind_jump_label = n; break;
7336 case ':': buf->b_ind_case = n; break;
7337 case '=': buf->b_ind_case_code = n; break;
7338 case 'b': buf->b_ind_case_break = n; break;
7339 case 'p': buf->b_ind_param = n; break;
7340 case 't': buf->b_ind_func_type = n; break;
7341 case '/': buf->b_ind_comment = n; break;
7342 case 'c': buf->b_ind_in_comment = n; break;
7343 case 'C': buf->b_ind_in_comment2 = n; break;
7344 case 'i': buf->b_ind_cpp_baseclass = n; break;
7345 case '+': buf->b_ind_continuation = n; break;
7346 case '(': buf->b_ind_unclosed = n; break;
7347 case 'u': buf->b_ind_unclosed2 = n; break;
7348 case 'U': buf->b_ind_unclosed_noignore = n; break;
7349 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7350 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7351 case 'm': buf->b_ind_matching_paren = n; break;
7352 case 'M': buf->b_ind_paren_prev = n; break;
7353 case ')': buf->b_ind_maxparen = n; break;
7354 case '*': buf->b_ind_maxcomment = n; break;
7355 case 'g': buf->b_ind_scopedecl = n; break;
7356 case 'h': buf->b_ind_scopedecl_code = n; break;
7357 case 'j': buf->b_ind_java = n; break;
7358 case 'J': buf->b_ind_js = n; break;
7359 case 'l': buf->b_ind_keep_case_label = n; break;
7360 case '#': buf->b_ind_hash_comment = n; break;
7361 case 'N': buf->b_ind_cpp_namespace = n; break;
7362 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007363 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007364 }
7365 if (*p == ',')
7366 ++p;
7367 }
7368}
7369
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007370/*
7371 * Return the desired indent for C code.
7372 * Return -1 if the indent should be left alone (inside a raw string).
7373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007375get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 pos_T cur_curpos;
7378 int amount;
7379 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007380 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 colnr_T col;
7382 char_u *theline;
7383 char_u *linecopy;
7384 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007385 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007387 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 pos_T our_paren_pos;
7389 char_u *start;
7390 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007391#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392#define BRACE_AT_START 2 /* '{' is at start of line */
7393#define BRACE_AT_END 3 /* '{' is at end of line */
7394 linenr_T ourscope;
7395 char_u *l;
7396 char_u *look;
7397 char_u terminated;
7398 int lookfor;
7399#define LOOKFOR_INITIAL 0
7400#define LOOKFOR_IF 1
7401#define LOOKFOR_DO 2
7402#define LOOKFOR_CASE 3
7403#define LOOKFOR_ANY 4
7404#define LOOKFOR_TERM 5
7405#define LOOKFOR_UNTERM 6
7406#define LOOKFOR_SCOPEDECL 7
7407#define LOOKFOR_NOBREAK 8
7408#define LOOKFOR_CPP_BASECLASS 9
7409#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007410#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007411#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
7413 int whilelevel;
7414 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 int n;
7416 int iscase;
7417 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007418 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007420 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007421 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007422 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007423 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007424 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007426 /* make a copy, value is changed below */
7427 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429 /* remember where the cursor was when we started */
7430 cur_curpos = curwin->w_cursor;
7431
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007432 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007433 if (cur_curpos.lnum == 1)
7434 return 0;
7435
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 /* Get a copy of the current contents of the line.
7437 * This is required, because only the most recent line obtained with
7438 * ml_get is valid! */
7439 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7440 if (linecopy == NULL)
7441 return 0;
7442
7443 /*
7444 * In insert mode and the cursor is on a ')' truncate the line at the
7445 * cursor position. We don't want to line up with the matching '(' when
7446 * inserting new stuff.
7447 * For unknown reasons the cursor might be past the end of the line, thus
7448 * check for that.
7449 */
7450 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007451 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 && linecopy[curwin->w_cursor.col] == ')')
7453 linecopy[curwin->w_cursor.col] = NUL;
7454
7455 theline = skipwhite(linecopy);
7456
7457 /* move the cursor to the start of the line */
7458
7459 curwin->w_cursor.col = 0;
7460
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007461 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007464 * If we are inside a raw string don't change the indent.
7465 * Ignore a raw string inside a comment.
7466 */
7467 comment_pos = ind_find_start_comment();
7468 if (comment_pos != NULL)
7469 {
7470 /* findmatchlimit() static pos is overwritten, make a copy */
7471 tryposCopy = *comment_pos;
7472 comment_pos = &tryposCopy;
7473 }
7474 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007475 if (trypos != NULL && (comment_pos == NULL
7476 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007477 {
7478 amount = -1;
7479 goto laterend;
7480 }
7481
7482 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 * #defines and so on always go at the left when included in 'cinkeys'.
7484 */
7485 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007486 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007487 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007488 goto theend;
7489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
7491 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007492 * Is it a non-case label? Then that goes at the left margin too unless:
7493 * - JS flag is set.
7494 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007496 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007497 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {
7499 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007500 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 }
7502
7503 /*
7504 * If we're inside a "//" comment and there is a "//" comment in a
7505 * previous line, lineup with that one.
7506 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007507 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 && (trypos = find_line_comment()) != NULL) /* XXX */
7509 {
7510 /* find how indented the line beginning the comment is */
7511 getvcol(curwin, trypos, &col, NULL, NULL);
7512 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007513 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
7515
7516 /*
7517 * If we're inside a comment and not looking at the start of the
7518 * comment, try using the 'comments' option.
7519 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007520 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 {
7522 int lead_start_len = 2;
7523 int lead_middle_len = 1;
7524 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7525 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7526 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7527 char_u *p;
7528 int start_align = 0;
7529 int start_off = 0;
7530 int done = FALSE;
7531
7532 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007533 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007535 *lead_start = NUL;
7536 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537
7538 p = curbuf->b_p_com;
7539 while (*p != NUL)
7540 {
7541 int align = 0;
7542 int off = 0;
7543 int what = 0;
7544
7545 while (*p != NUL && *p != ':')
7546 {
7547 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7548 what = *p++;
7549 else if (*p == COM_LEFT || *p == COM_RIGHT)
7550 align = *p++;
7551 else if (VIM_ISDIGIT(*p) || *p == '-')
7552 off = getdigits(&p);
7553 else
7554 ++p;
7555 }
7556
7557 if (*p == ':')
7558 ++p;
7559 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7560 if (what == COM_START)
7561 {
7562 STRCPY(lead_start, lead_end);
7563 lead_start_len = (int)STRLEN(lead_start);
7564 start_off = off;
7565 start_align = align;
7566 }
7567 else if (what == COM_MIDDLE)
7568 {
7569 STRCPY(lead_middle, lead_end);
7570 lead_middle_len = (int)STRLEN(lead_middle);
7571 }
7572 else if (what == COM_END)
7573 {
7574 /* If our line starts with the middle comment string, line it
7575 * up with the comment opener per the 'comments' option. */
7576 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7577 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7578 {
7579 done = TRUE;
7580 if (curwin->w_cursor.lnum > 1)
7581 {
7582 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007583 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 * the middle comment string matches in the previous
7585 * line, use the indent of that line. XXX */
7586 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7587 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7588 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7589 else if (STRNCMP(look, lead_middle,
7590 lead_middle_len) == 0)
7591 {
7592 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7593 break;
7594 }
7595 /* If the start comment string doesn't match with the
7596 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007597 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 lead_start, lead_start_len) != 0)
7599 continue;
7600 }
7601 if (start_off != 0)
7602 amount += start_off;
7603 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007604 amount += vim_strsize(lead_start)
7605 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 break;
7607 }
7608
7609 /* If our line starts with the end comment string, line it up
7610 * with the middle comment */
7611 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7612 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7613 {
7614 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7615 /* XXX */
7616 if (off != 0)
7617 amount += off;
7618 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007619 amount += vim_strsize(lead_start)
7620 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 done = TRUE;
7622 break;
7623 }
7624 }
7625 }
7626
7627 /* If our line starts with an asterisk, line up with the
7628 * asterisk in the comment opener; otherwise, line up
7629 * with the first character of the comment text.
7630 */
7631 if (done)
7632 ;
7633 else if (theline[0] == '*')
7634 amount += 1;
7635 else
7636 {
7637 /*
7638 * If we are more than one line away from the comment opener, take
7639 * the indent of the previous non-empty line. If 'cino' has "CO"
7640 * and we are just below the comment opener and there are any
7641 * white characters after it line up with the text after it;
7642 * otherwise, add the amount specified by "c" in 'cino'
7643 */
7644 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007645 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
7647 if (linewhite(lnum)) /* skip blank lines */
7648 continue;
7649 amount = get_indent_lnum(lnum); /* XXX */
7650 break;
7651 }
7652 if (amount == -1) /* use the comment opener */
7653 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007654 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007656 start = ml_get(comment_pos->lnum);
7657 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007659 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007661 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007663 if (curbuf->b_ind_in_comment2 || *look == NUL)
7664 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
7666 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007667 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
7669
7670 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007671 * Are we looking at a ']' that has a match?
7672 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007673 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007674 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7675 {
7676 /* align with the line containing the '['. */
7677 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007678 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007679 }
7680
7681 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 * Are we inside parentheses or braces?
7683 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007684 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007685 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007686 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 || trypos != NULL)
7688 {
7689 if (trypos != NULL && tryposBrace != NULL)
7690 {
7691 /* Both an unmatched '(' and '{' is found. Use the one which is
7692 * closer to the current cursor position, set the other to NULL. */
7693 if (trypos->lnum != tryposBrace->lnum
7694 ? trypos->lnum < tryposBrace->lnum
7695 : trypos->col < tryposBrace->col)
7696 trypos = NULL;
7697 else
7698 tryposBrace = NULL;
7699 }
7700
7701 if (trypos != NULL)
7702 {
7703 /*
7704 * If the matching paren is more than one line away, use the indent of
7705 * a previous non-empty line that matches the same paren.
7706 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007707 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007709 /* Line up with the start of the matching paren line. */
7710 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7711 }
7712 else
7713 {
7714 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007715 our_paren_pos = *trypos;
7716 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007718 l = skipwhite(ml_get(lnum));
7719 if (cin_nocode(l)) /* skip comment lines */
7720 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007721 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007722 continue; /* ignore #define, #if, etc. */
7723 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007725 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007726 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007727 {
7728 lnum = trypos->lnum + 1;
7729 continue;
7730 }
7731
7732 /* XXX */
7733 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007734 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007735 && trypos->lnum == our_paren_pos.lnum
7736 && trypos->col == our_paren_pos.col)
7737 {
7738 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007740 if (theline[0] == ')')
7741 {
7742 if (our_paren_pos.lnum != lnum
7743 && cur_amount > amount)
7744 cur_amount = amount;
7745 amount = -1;
7746 }
7747 break;
7748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 }
7750 }
7751
7752 /*
7753 * Line up with line where the matching paren is. XXX
7754 * If the line starts with a '(' or the indent for unclosed
7755 * parentheses is zero, line up with the unclosed parentheses.
7756 */
7757 if (amount == -1)
7758 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007759 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007760 int is_if_for_while = 0;
7761
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007762 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007763 {
7764 /* Look for the outermost opening parenthesis on this line
7765 * and check whether it belongs to an "if", "for" or "while". */
7766
7767 pos_T cursor_save = curwin->w_cursor;
7768 pos_T outermost;
7769 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007770
7771 trypos = &our_paren_pos;
7772 do {
7773 outermost = *trypos;
7774 curwin->w_cursor.lnum = outermost.lnum;
7775 curwin->w_cursor.col = outermost.col;
7776
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007777 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007778 } while (trypos && trypos->lnum == outermost.lnum);
7779
7780 curwin->w_cursor = cursor_save;
7781
7782 line = ml_get(outermost.lnum);
7783
7784 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007785 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007786 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007787
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007788 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007789 look = skipwhite(look);
7790 if (*look == '(')
7791 {
7792 linenr_T save_lnum = curwin->w_cursor.lnum;
7793 char_u *line;
7794 int look_col;
7795
7796 /* Ignore a '(' in front of the line that has a match before
7797 * our matching '('. */
7798 curwin->w_cursor.lnum = our_paren_pos.lnum;
7799 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007800 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007801 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007802 if ((trypos = findmatchlimit(NULL, ')', 0,
7803 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007804 != NULL
7805 && trypos->lnum == our_paren_pos.lnum
7806 && trypos->col < our_paren_pos.col)
7807 ignore_paren_col = trypos->col + 1;
7808
7809 curwin->w_cursor.lnum = save_lnum;
7810 look = ml_get(our_paren_pos.lnum) + look_col;
7811 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007812 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7813 && is_if_for_while == 0)
7814 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007815 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 /*
7818 * If we're looking at a close paren, line up right there;
7819 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007820 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 * the last nonwhite character of the line, use either the
7822 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007823 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 * lines).
7825 */
7826 if (theline[0] != ')')
7827 {
7828 cur_amount = MAXCOL;
7829 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007830 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 && cin_ends_in(l, (char_u *)"(", NULL))
7832 {
7833 /* look for opening unmatched paren, indent one level
7834 * for each additional level */
7835 n = 1;
7836 for (col = 0; col < our_paren_pos.col; ++col)
7837 {
7838 switch (l[col])
7839 {
7840 case '(':
7841 case '{': ++n;
7842 break;
7843
7844 case ')':
7845 case '}': if (n > 1)
7846 --n;
7847 break;
7848 }
7849 }
7850
7851 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007852 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007854 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 our_paren_pos.col++;
7856 else
7857 {
7858 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007859 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 col++;
7861 if (l[col] != NUL) /* In case of trailing space */
7862 our_paren_pos.col = col;
7863 else
7864 our_paren_pos.col++;
7865 }
7866 }
7867
7868 /*
7869 * Find how indented the paren is, or the character after it
7870 * if we did the above "if".
7871 */
7872 if (our_paren_pos.col > 0)
7873 {
7874 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7875 if (cur_amount > (int)col)
7876 cur_amount = col;
7877 }
7878 }
7879
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007880 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
7882 /* Line up with the start of the matching paren line. */
7883 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007884 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7885 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007886 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
7888 if (cur_amount != MAXCOL)
7889 amount = cur_amount;
7890 }
7891 else
7892 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007893 /* Add b_ind_unclosed2 for each '(' before our matching one,
7894 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007896 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {
7898 --our_paren_pos.col;
7899 switch (*ml_get_pos(&our_paren_pos))
7900 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007901 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 col = our_paren_pos.col;
7903 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007904 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 col = MAXCOL;
7906 break;
7907 }
7908 }
7909
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007910 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 * braces */
7912 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007913 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 else
7915 {
7916 curwin->w_cursor.lnum = our_paren_pos.lnum;
7917 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007918 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7919 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007920 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007922 {
7923 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007924 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007925 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007926 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929 /*
7930 * For a line starting with ')' use the minimum of the two
7931 * positions, to avoid giving it more indent than the previous
7932 * lines:
7933 * func_long_name( if (x
7934 * arg && yy
7935 * ) ^ not here ) ^ not here
7936 */
7937 if (cur_amount < amount)
7938 amount = cur_amount;
7939 }
7940 }
7941
7942 /* add extra indent for a comment */
7943 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007944 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 else
7947 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007948 /*
7949 * We are inside braces, there is a { before this line at the position
7950 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007951 * Make a copy of tryposBrace, it may point to pos_copy inside
7952 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007953 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007954 tryposCopy = *tryposBrace;
7955 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 ourscope = trypos->lnum;
7958 start = ml_get(ourscope);
7959
7960 /*
7961 * Now figure out how indented the line is in general.
7962 * If the brace was at the start of the line, we use that;
7963 * otherwise, check out the indentation of the line as
7964 * a whole and then add the "imaginary indent" to that.
7965 */
7966 look = skipwhite(start);
7967 if (*look == '{')
7968 {
7969 getvcol(curwin, trypos, &col, NULL, NULL);
7970 amount = col;
7971 if (*start == '{')
7972 start_brace = BRACE_IN_COL0;
7973 else
7974 start_brace = BRACE_AT_START;
7975 }
7976 else
7977 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007978 /* That opening brace might have been on a continuation
7979 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 curwin->w_cursor.lnum = ourscope;
7981
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007982 /* Position the cursor over the rightmost paren, so that
7983 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 lnum = ourscope;
7985 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007986 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7987 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 lnum = trypos->lnum;
7989
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007990 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 * case 1: if (asdf &&
7992 * ldfd) {
7993 * }
7994 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007995 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7996 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007998 else if (curbuf->b_ind_js)
7999 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008001 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
8003 start_brace = BRACE_AT_END;
8004 }
8005
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008006 /* For Javascript check if the line starts with "key:". */
8007 if (curbuf->b_ind_js)
8008 js_cur_has_key = cin_has_js_key(theline);
8009
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008011 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 * we want to be. otherwise, add the amount of room
8013 * that an indent is supposed to be.
8014 */
8015 if (theline[0] == '}')
8016 {
8017 /*
8018 * they may want closing braces to line up with something
8019 * other than the open brace. indulge them, if so.
8020 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008021 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
8023 else
8024 {
8025 /*
8026 * If we're looking at an "else", try to find an "if"
8027 * to match it with.
8028 * If we're looking at a "while", try to find a "do"
8029 * to match it with.
8030 */
8031 lookfor = LOOKFOR_INITIAL;
8032 if (cin_iselse(theline))
8033 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008034 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 lookfor = LOOKFOR_DO;
8036 if (lookfor != LOOKFOR_INITIAL)
8037 {
8038 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008039 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {
8041 amount = get_indent(); /* XXX */
8042 goto theend;
8043 }
8044 }
8045
8046 /*
8047 * We get here if we are not on an "while-of-do" or "else" (or
8048 * failed to find a matching "if").
8049 * Search backwards for something to line up with.
8050 * First set amount for when we don't find anything.
8051 */
8052
8053 /*
8054 * if the '{' is _really_ at the left margin, use the imaginary
8055 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008056 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 */
8058
8059 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8060 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008061 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008062 lookfor_cpp_namespace = TRUE;
8063 }
8064 else if (start_brace == BRACE_AT_START &&
8065 lookfor_cpp_namespace) /* '{' is at start */
8066 {
8067
8068 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 }
8070 else
8071 {
8072 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008073 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008074 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008075
8076 l = skipwhite(ml_get_curline());
8077 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008078 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008079 else if (cin_is_cpp_extern_c(l))
8080 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 else
8083 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008084 /* Compensate for adding b_ind_open_extra later. */
8085 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 if (amount < 0)
8087 amount = 0;
8088 }
8089 }
8090
8091 lookfor_break = FALSE;
8092
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008093 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
8095 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008096 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098 else if (cin_isscopedecl(theline)) /* private:, ... */
8099 {
8100 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008101 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103 else
8104 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008105 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8106 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 lookfor_break = TRUE;
8108
8109 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008110 /* b_ind_level from start of block */
8111 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
8113 scope_amount = amount;
8114 whilelevel = 0;
8115
8116 /*
8117 * Search backwards. If we find something we recognize, line up
8118 * with that.
8119 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008120 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 * the usual amount relative to the conditional
8122 * that opens the block.
8123 */
8124 curwin->w_cursor = cur_curpos;
8125 for (;;)
8126 {
8127 curwin->w_cursor.lnum--;
8128 curwin->w_cursor.col = 0;
8129
8130 /*
8131 * If we went all the way back to the start of our scope, line
8132 * up with it.
8133 */
8134 if (curwin->w_cursor.lnum <= ourscope)
8135 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008136 /* We reached end of scope:
8137 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008139 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 * don't add ind_continuation, otherwise it is a variable
8141 * declaration:
8142 * int x,
8143 * here; <-- add ind_continuation
8144 */
8145 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8146 {
8147 if (curwin->w_cursor.lnum == 0
8148 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008149 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008151 /* nothing found (abuse curbuf->b_ind_maxparen as
8152 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 * initialization) */
8154 if (cont_amount > 0)
8155 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008156 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 amount += ind_continuation;
8158 break;
8159 }
8160
8161 l = ml_get_curline();
8162
8163 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008164 * If we're in a comment or raw string now, skip to
8165 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008167 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (trypos != NULL)
8169 {
8170 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008171 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 continue;
8173 }
8174
8175 /*
8176 * Skip preprocessor directives and blank lines.
8177 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008178 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8179 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 continue;
8181
8182 if (cin_nocode(l))
8183 continue;
8184
8185 terminated = cin_isterminated(l, FALSE, TRUE);
8186
8187 /*
8188 * If we are at top level and the line looks like a
8189 * function declaration, we are done
8190 * (it's a variable declaration).
8191 */
8192 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008193 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
8195 /* if the line is terminated with another ','
8196 * it is a continued variable initialization.
8197 * don't add extra indent.
8198 * TODO: does not work, if a function
8199 * declaration is split over multiple lines:
8200 * cin_isfuncdecl returns FALSE then.
8201 */
8202 if (terminated == ',')
8203 break;
8204
8205 /* if it es a enum declaration or an assignment,
8206 * we are done.
8207 */
8208 if (terminated != ';' && cin_isinit())
8209 break;
8210
8211 /* nothing useful found */
8212 if (terminated == 0 || terminated == '{')
8213 continue;
8214 }
8215
8216 if (terminated != ';')
8217 {
8218 /* Skip parens and braces. Position the cursor
8219 * over the rightmost paren, so that matching it
8220 * will take us back to the start of the line.
8221 */ /* XXX */
8222 trypos = NULL;
8223 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008224 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008225 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226
8227 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008228 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
8230 if (trypos != NULL)
8231 {
8232 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008233 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 continue;
8235 }
8236 }
8237
8238 /* it's a variable declaration, add indentation
8239 * like in
8240 * int a,
8241 * b;
8242 */
8243 if (cont_amount > 0)
8244 amount = cont_amount;
8245 else
8246 amount += ind_continuation;
8247 }
8248 else if (lookfor == LOOKFOR_UNTERM)
8249 {
8250 if (cont_amount > 0)
8251 amount = cont_amount;
8252 else
8253 amount += ind_continuation;
8254 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008255 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008256 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008257 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008258 && lookfor != LOOKFOR_CPP_BASECLASS
8259 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008260 {
8261 amount = scope_amount;
8262 if (theline[0] == '{')
8263 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008264 amount += curbuf->b_ind_open_extra;
8265 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008266 }
8267 }
8268
8269 if (lookfor_cpp_namespace)
8270 {
8271 /*
8272 * Looking for C++ namespace, need to look further
8273 * back.
8274 */
8275 if (curwin->w_cursor.lnum == ourscope)
8276 continue;
8277
8278 if (curwin->w_cursor.lnum == 0
8279 || curwin->w_cursor.lnum
8280 < ourscope - FIND_NAMESPACE_LIM)
8281 break;
8282
8283 l = ml_get_curline();
8284
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008285 /* If we're in a comment or raw string now, skip
8286 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008287 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008288 if (trypos != NULL)
8289 {
8290 curwin->w_cursor.lnum = trypos->lnum + 1;
8291 curwin->w_cursor.col = 0;
8292 continue;
8293 }
8294
8295 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008296 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8297 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008298 continue;
8299
8300 /* Finally the actual check for "namespace". */
8301 if (cin_is_cpp_namespace(l))
8302 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008303 amount += curbuf->b_ind_cpp_namespace
8304 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008305 break;
8306 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008307 else if (cin_is_cpp_extern_c(l))
8308 {
8309 amount += curbuf->b_ind_cpp_extern_c
8310 - added_to_amount;
8311 break;
8312 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008313
8314 if (cin_nocode(l))
8315 continue;
8316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 }
8318 break;
8319 }
8320
8321 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008322 * If we're in a comment or raw string now, skip to the start
8323 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008325 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
8327 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008328 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 continue;
8330 }
8331
8332 l = ml_get_curline();
8333
8334 /*
8335 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008336 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008338 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 if (iscase || cin_isscopedecl(l))
8340 {
8341 /* we are only looking for cpp base class
8342 * declaration/initialization any longer */
8343 if (lookfor == LOOKFOR_CPP_BASECLASS)
8344 break;
8345
8346 /* When looking for a "do" we are not interested in
8347 * labels. */
8348 if (whilelevel > 0)
8349 continue;
8350
8351 /*
8352 * case xx:
8353 * c = 99 + <- this indent plus continuation
8354 *-> here;
8355 */
8356 if (lookfor == LOOKFOR_UNTERM
8357 || lookfor == LOOKFOR_ENUM_OR_INIT)
8358 {
8359 if (cont_amount > 0)
8360 amount = cont_amount;
8361 else
8362 amount += ind_continuation;
8363 break;
8364 }
8365
8366 /*
8367 * case xx: <- line up with this case
8368 * x = 333;
8369 * case yy:
8370 */
8371 if ( (iscase && lookfor == LOOKFOR_CASE)
8372 || (iscase && lookfor_break)
8373 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8374 {
8375 /*
8376 * Check that this case label is not for another
8377 * switch()
8378 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008379 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008380 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {
8382 amount = get_indent(); /* XXX */
8383 break;
8384 }
8385 continue;
8386 }
8387
8388 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8389
8390 /*
8391 * case xx: if (cond) <- line up with this if
8392 * y = y + 1;
8393 * -> s = 99;
8394 *
8395 * case xx:
8396 * if (cond) <- line up with this line
8397 * y = y + 1;
8398 * -> s = 99;
8399 */
8400 if (lookfor == LOOKFOR_TERM)
8401 {
8402 if (n)
8403 amount = n;
8404
8405 if (!lookfor_break)
8406 break;
8407 }
8408
8409 /*
8410 * case xx: x = x + 1; <- line up with this x
8411 * -> y = y + 1;
8412 *
8413 * case xx: if (cond) <- line up with this if
8414 * -> y = y + 1;
8415 */
8416 if (n)
8417 {
8418 amount = n;
8419 l = after_label(ml_get_curline());
8420 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008421 {
8422 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008423 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008424 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008425 amount += curbuf->b_ind_level
8426 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 break;
8429 }
8430
8431 /*
8432 * Try to get the indent of a statement before the switch
8433 * label. If nothing is found, line up relative to the
8434 * switch label.
8435 * break; <- may line up with this line
8436 * case xx:
8437 * -> y = 1;
8438 */
8439 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008440 ? curbuf->b_ind_case_code
8441 : curbuf->b_ind_scopedecl_code);
8442 lookfor = curbuf->b_ind_case_break
8443 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 continue;
8445 }
8446
8447 /*
8448 * Looking for a switch() label or C++ scope declaration,
8449 * ignore other lines, skip {}-blocks.
8450 */
8451 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8452 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008453 if (find_last_paren(l, '{', '}')
8454 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008457 curwin->w_cursor.col = 0;
8458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 continue;
8460 }
8461
8462 /*
8463 * Ignore jump labels with nothing after them.
8464 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008465 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {
8467 l = after_label(ml_get_curline());
8468 if (l == NULL || cin_nocode(l))
8469 continue;
8470 }
8471
8472 /*
8473 * Ignore #defines, #if, etc.
8474 * Ignore comment and empty lines.
8475 * (need to get the line again, cin_islabel() may have
8476 * unlocked it)
8477 */
8478 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008479 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 || cin_nocode(l))
8481 continue;
8482
8483 /*
8484 * Are we at the start of a cpp base class declaration or
8485 * constructor initialization?
8486 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008487 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008488 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008489 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008490 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008491 l = ml_get_curline();
8492 }
8493 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {
8495 if (lookfor == LOOKFOR_UNTERM)
8496 {
8497 if (cont_amount > 0)
8498 amount = cont_amount;
8499 else
8500 amount += ind_continuation;
8501 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008502 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008504 /* Need to find start of the declaration. */
8505 lookfor = LOOKFOR_UNTERM;
8506 ind_continuation = 0;
8507 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008510 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008511 amount = get_baseclass_amount(
8512 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 break;
8514 }
8515 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8516 {
8517 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008518 * declaration or initialization before the opening brace.
8519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (cin_isterminated(l, TRUE, FALSE))
8521 break;
8522 else
8523 continue;
8524 }
8525
8526 /*
8527 * What happens next depends on the line being terminated.
8528 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008529 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 * 123,
8531 * sizeof
8532 * here
8533 * Otherwise check whether it is a enumeration or structure
8534 * initialisation (not indented) or a variable declaration
8535 * (indented).
8536 */
8537 terminated = cin_isterminated(l, FALSE, TRUE);
8538
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008539 if (js_cur_has_key)
8540 {
8541 js_cur_has_key = 0; /* only check the first line */
8542 if (curbuf->b_ind_js && terminated == ',')
8543 {
8544 /* For Javascript we might be inside an object:
8545 * key: something, <- align with this
8546 * key: something
8547 * or:
8548 * key: something + <- align with this
8549 * something,
8550 * key: something
8551 */
8552 lookfor = LOOKFOR_JS_KEY;
8553 }
8554 }
8555 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8556 {
8557 amount = get_indent();
8558 break;
8559 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008560 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008561 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008562 if (tryposBrace != NULL && tryposBrace->lnum
8563 >= curwin->w_cursor.lnum)
8564 break;
8565 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008566 /* line below current line is the one that starts a
8567 * (possibly broken) line ending in a comma. */
8568 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008569 else
8570 {
8571 amount = get_indent();
8572 if (curwin->w_cursor.lnum - 1 == ourscope)
8573 /* line above is start of the scope, thus current
8574 * line is the one that stars a (possibly broken)
8575 * line ending in a comma. */
8576 break;
8577 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008578 }
8579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8581 && terminated == ','))
8582 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008583 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8584 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008585 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 /*
8587 * if we're in the middle of a paren thing,
8588 * go back to the line that starts it so
8589 * we can get the right prevailing indent
8590 * if ( foo &&
8591 * bar )
8592 */
8593 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008594 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008596 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 */
8598 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008599 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008600 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8601 || (trypos->lnum == tryposBrace->lnum
8602 && trypos->col < tryposBrace->col)))
8603 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
8605 /*
8606 * If we are looking for ',', we also look for matching
8607 * braces.
8608 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008609 if (trypos == NULL && terminated == ','
8610 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008611 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
8613 if (trypos != NULL)
8614 {
8615 /*
8616 * Check if we are on a case label now. This is
8617 * handled above.
8618 * case xx: if ( asdf &&
8619 * asdf)
8620 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008621 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008623 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {
8625 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008626 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 continue;
8628 }
8629 }
8630
8631 /*
8632 * Skip over continuation lines to find the one to get the
8633 * indent from
8634 * char *usethis = "bla\
8635 * bla",
8636 * here;
8637 */
8638 if (terminated == ',')
8639 {
8640 while (curwin->w_cursor.lnum > 1)
8641 {
8642 l = ml_get(curwin->w_cursor.lnum - 1);
8643 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8644 break;
8645 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008646 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 }
8648 }
8649
8650 /*
8651 * Get indent and pointer to text for current line,
8652 * ignoring any jump label. XXX
8653 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008654 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008655 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008656 else
8657 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 /*
8659 * If this is just above the line we are indenting, and it
8660 * starts with a '{', line it up with this line.
8661 * while (not)
8662 * -> {
8663 * }
8664 */
8665 if (terminated != ',' && lookfor != LOOKFOR_TERM
8666 && theline[0] == '{')
8667 {
8668 amount = cur_amount;
8669 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008670 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 * doesn't start with a '{', which must have a match
8672 * in the same line (scope is the same). Probably:
8673 * { 1, 2 },
8674 * -> { 3, 4 }
8675 */
8676 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008677 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008679 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {
8681 /* have to look back, whether it is a cpp base
8682 * class declaration or initialization */
8683 lookfor = LOOKFOR_CPP_BASECLASS;
8684 continue;
8685 }
8686 break;
8687 }
8688
8689 /*
8690 * Check if we are after an "if", "while", etc.
8691 * Also allow " } else".
8692 */
8693 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8694 {
8695 /*
8696 * Found an unterminated line after an if (), line up
8697 * with the last one.
8698 * if (cond)
8699 * 100 +
8700 * -> here;
8701 */
8702 if (lookfor == LOOKFOR_UNTERM
8703 || lookfor == LOOKFOR_ENUM_OR_INIT)
8704 {
8705 if (cont_amount > 0)
8706 amount = cont_amount;
8707 else
8708 amount += ind_continuation;
8709 break;
8710 }
8711
8712 /*
8713 * If this is just above the line we are indenting, we
8714 * are finished.
8715 * while (not)
8716 * -> here;
8717 * Otherwise this indent can be used when the line
8718 * before this is terminated.
8719 * yyy;
8720 * if (stat)
8721 * while (not)
8722 * xxx;
8723 * -> here;
8724 */
8725 amount = cur_amount;
8726 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008727 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 if (lookfor != LOOKFOR_TERM)
8729 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008730 amount += curbuf->b_ind_level
8731 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 break;
8733 }
8734
8735 /*
8736 * Special trick: when expecting the while () after a
8737 * do, line up with the while()
8738 * do
8739 * x = 1;
8740 * -> here
8741 */
8742 l = skipwhite(ml_get_curline());
8743 if (cin_isdo(l))
8744 {
8745 if (whilelevel == 0)
8746 break;
8747 --whilelevel;
8748 }
8749
8750 /*
8751 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008752 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 * Need to use the scope of this "else". XXX
8754 * If whilelevel != 0 continue looking for a "do {".
8755 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008756 if (cin_iselse(l) && whilelevel == 0)
8757 {
8758 /* If we're looking at "} else", let's make sure we
8759 * find the opening brace of the enclosing scope,
8760 * not the one from "if () {". */
8761 if (*l == '}')
8762 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008763 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008764
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008765 if ((trypos = find_start_brace()) == NULL
8766 || find_match(LOOKFOR_IF, trypos->lnum)
8767 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008768 break;
8769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
8771
8772 /*
8773 * If we're below an unterminated line that is not an
8774 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008775 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 * the line before this one.
8777 */
8778 else
8779 {
8780 /*
8781 * Found two unterminated lines on a row, line up with
8782 * the last one.
8783 * c = 99 +
8784 * 100 +
8785 * -> here;
8786 */
8787 if (lookfor == LOOKFOR_UNTERM)
8788 {
8789 /* When line ends in a comma add extra indent */
8790 if (terminated == ',')
8791 amount += ind_continuation;
8792 break;
8793 }
8794
8795 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8796 {
8797 /* Found two lines ending in ',', lineup with the
8798 * lowest one, but check for cpp base class
8799 * declaration/initialization, if it is an
8800 * opening brace or we are looking just for
8801 * enumerations/initializations. */
8802 if (terminated == ',')
8803 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008804 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 break;
8806
8807 lookfor = LOOKFOR_CPP_BASECLASS;
8808 continue;
8809 }
8810
8811 /* Ignore unterminated lines in between, but
8812 * reduce indent. */
8813 if (amount > cur_amount)
8814 amount = cur_amount;
8815 }
8816 else
8817 {
8818 /*
8819 * Found first unterminated line on a row, may
8820 * line up with this line, remember its indent
8821 * 100 +
8822 * -> here;
8823 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008824 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008826
8827 n = (int)STRLEN(l);
8828 if (terminated == ',' && (*skipwhite(l) == ']'
8829 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008830 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831
8832 /*
8833 * If previous line ends in ',', check whether we
8834 * are in an initialization or enum
8835 * struct xxx =
8836 * {
8837 * sizeof a,
8838 * 124 };
8839 * or a normal possible continuation line.
8840 * but only, of no other statement has been found
8841 * yet.
8842 */
8843 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8844 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008845 if (curbuf->b_ind_js)
8846 {
8847 /* Search for a line ending in a comma
8848 * and line up with the line below it
8849 * (could be the current line).
8850 * some = [
8851 * 1, <- line up here
8852 * 2,
8853 * some = [
8854 * 3 + <- line up here
8855 * 4 *
8856 * 5,
8857 * 6,
8858 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008859 if (cin_iscomment(skipwhite(l)))
8860 break;
8861 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008862 trypos = find_match_char('[',
8863 curbuf->b_ind_maxparen);
8864 if (trypos != NULL)
8865 {
8866 if (trypos->lnum
8867 == curwin->w_cursor.lnum - 1)
8868 {
8869 /* Current line is first inside
8870 * [], line up with it. */
8871 break;
8872 }
8873 ourscope = trypos->lnum;
8874 }
8875 }
8876 else
8877 {
8878 lookfor = LOOKFOR_ENUM_OR_INIT;
8879 cont_amount = cin_first_id_amount();
8880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 }
8882 else
8883 {
8884 if (lookfor == LOOKFOR_INITIAL
8885 && *l != NUL
8886 && l[STRLEN(l) - 1] == '\\')
8887 /* XXX */
8888 cont_amount = cin_get_equal_amount(
8889 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008890 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008891 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008892 && lookfor != LOOKFOR_COMMA
8893 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 lookfor = LOOKFOR_UNTERM;
8895 }
8896 }
8897 }
8898 }
8899
8900 /*
8901 * Check if we are after a while (cond);
8902 * If so: Ignore until the matching "do".
8903 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008904 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 {
8906 /*
8907 * Found an unterminated line after a while ();, line up
8908 * with the last one.
8909 * while (cond);
8910 * 100 + <- line up with this one
8911 * -> here;
8912 */
8913 if (lookfor == LOOKFOR_UNTERM
8914 || lookfor == LOOKFOR_ENUM_OR_INIT)
8915 {
8916 if (cont_amount > 0)
8917 amount = cont_amount;
8918 else
8919 amount += ind_continuation;
8920 break;
8921 }
8922
8923 if (whilelevel == 0)
8924 {
8925 lookfor = LOOKFOR_TERM;
8926 amount = get_indent(); /* XXX */
8927 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008928 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 }
8930 ++whilelevel;
8931 }
8932
8933 /*
8934 * We are after a "normal" statement.
8935 * If we had another statement we can stop now and use the
8936 * indent of that other statement.
8937 * Otherwise the indent of the current statement may be used,
8938 * search backwards for the next "normal" statement.
8939 */
8940 else
8941 {
8942 /*
8943 * Skip single break line, if before a switch label. It
8944 * may be lined up with the case label.
8945 */
8946 if (lookfor == LOOKFOR_NOBREAK
8947 && cin_isbreak(skipwhite(ml_get_curline())))
8948 {
8949 lookfor = LOOKFOR_ANY;
8950 continue;
8951 }
8952
8953 /*
8954 * Handle "do {" line.
8955 */
8956 if (whilelevel > 0)
8957 {
8958 l = cin_skipcomment(ml_get_curline());
8959 if (cin_isdo(l))
8960 {
8961 amount = get_indent(); /* XXX */
8962 --whilelevel;
8963 continue;
8964 }
8965 }
8966
8967 /*
8968 * Found a terminated line above an unterminated line. Add
8969 * the amount for a continuation line.
8970 * x = 1;
8971 * y = foo +
8972 * -> here;
8973 * or
8974 * int x = 1;
8975 * int foo,
8976 * -> here;
8977 */
8978 if (lookfor == LOOKFOR_UNTERM
8979 || lookfor == LOOKFOR_ENUM_OR_INIT)
8980 {
8981 if (cont_amount > 0)
8982 amount = cont_amount;
8983 else
8984 amount += ind_continuation;
8985 break;
8986 }
8987
8988 /*
8989 * Found a terminated line above a terminated line or "if"
8990 * etc. line. Use the amount of the line below us.
8991 * x = 1; x = 1;
8992 * if (asdf) y = 2;
8993 * while (asdf) ->here;
8994 * here;
8995 * ->foo;
8996 */
8997 if (lookfor == LOOKFOR_TERM)
8998 {
8999 if (!lookfor_break && whilelevel == 0)
9000 break;
9001 }
9002
9003 /*
9004 * First line above the one we're indenting is terminated.
9005 * To know what needs to be done look further backward for
9006 * a terminated line.
9007 */
9008 else
9009 {
9010 /*
9011 * position the cursor over the rightmost paren, so
9012 * that matching it will take us back to the start of
9013 * the line. Helps for:
9014 * func(asdr,
9015 * asdfasdf);
9016 * here;
9017 */
9018term_again:
9019 l = ml_get_curline();
9020 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009021 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009022 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 {
9024 /*
9025 * Check if we are on a case label now. This is
9026 * handled above.
9027 * case xx: if ( asdf &&
9028 * asdf)
9029 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009030 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009032 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 {
9034 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009035 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 continue;
9037 }
9038 }
9039
9040 /* When aligning with the case statement, don't align
9041 * with a statement after it.
9042 * case 1: { <-- don't use this { position
9043 * stat;
9044 * }
9045 * case 2:
9046 * stat;
9047 * }
9048 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009049 iscase = (curbuf->b_ind_keep_case_label
9050 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052 /*
9053 * Get indent and pointer to text for current line,
9054 * ignoring any jump label.
9055 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009056 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057
9058 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009059 amount += curbuf->b_ind_open_extra;
9060 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009061 l = skipwhite(l);
9062 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009063 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9065
9066 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009067 * When a terminated line starts with "else" skip to
9068 * the matching "if":
9069 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009070 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009071 * Need to use the scope of this "else". XXX
9072 * If whilelevel != 0 continue looking for a "do {".
9073 */
9074 if (lookfor == LOOKFOR_TERM
9075 && *l != '}'
9076 && cin_iselse(l)
9077 && whilelevel == 0)
9078 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009079 if ((trypos = find_start_brace()) == NULL
9080 || find_match(LOOKFOR_IF, trypos->lnum)
9081 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009082 break;
9083 continue;
9084 }
9085
9086 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 * If we're at the end of a block, skip to the start of
9088 * that block.
9089 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009090 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009091 if (find_last_paren(l, '{', '}') /* XXX */
9092 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009094 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 /* if not "else {" check for terminated again */
9096 /* but skip block for "} else {" */
9097 l = cin_skipcomment(ml_get_curline());
9098 if (*l == '}' || !cin_iselse(l))
9099 goto term_again;
9100 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009101 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 }
9103 }
9104 }
9105 }
9106 }
9107 }
9108
9109 /* add extra indent for a comment */
9110 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009111 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009112
9113 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009114 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9115 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009116
9117 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009119
9120 /*
9121 * ok -- we're not inside any sort of structure at all!
9122 *
9123 * This means we're at the top level, and everything should
9124 * basically just match where the previous line is, except
9125 * for the lines immediately following a function declaration,
9126 * which are K&R-style parameters and need to be indented.
9127 *
9128 * if our line starts with an open brace, forget about any
9129 * prevailing indent and make sure it looks like the start
9130 * of a function
9131 */
9132
9133 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009135 amount = curbuf->b_ind_first_open;
9136 goto theend;
9137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009139 /*
9140 * If the NEXT line is a function declaration, the current
9141 * line needs to be indented as a function type spec.
9142 * Don't do this if the current line looks like a comment or if the
9143 * current line is terminated, ie. ends in ';', or if the current line
9144 * contains { or }: "void f() {\n if (1)"
9145 */
9146 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9147 && !cin_nocode(theline)
9148 && vim_strchr(theline, '{') == NULL
9149 && vim_strchr(theline, '}') == NULL
9150 && !cin_ends_in(theline, (char_u *)":", NULL)
9151 && !cin_ends_in(theline, (char_u *)",", NULL)
9152 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9153 cur_curpos.lnum + 1)
9154 && !cin_isterminated(theline, FALSE, TRUE))
9155 {
9156 amount = curbuf->b_ind_func_type;
9157 goto theend;
9158 }
9159
9160 /* search backwards until we find something we recognize */
9161 amount = 0;
9162 curwin->w_cursor = cur_curpos;
9163 while (curwin->w_cursor.lnum > 1)
9164 {
9165 curwin->w_cursor.lnum--;
9166 curwin->w_cursor.col = 0;
9167
9168 l = ml_get_curline();
9169
9170 /*
9171 * If we're in a comment or raw string now, skip to the start
9172 * of it.
9173 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009174 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009176 curwin->w_cursor.lnum = trypos->lnum + 1;
9177 curwin->w_cursor.col = 0;
9178 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
9180
9181 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009182 * Are we at the start of a cpp base class declaration or
9183 * constructor initialization?
9184 */ /* XXX */
9185 n = FALSE;
9186 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009188 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9189 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009191 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009193 /* XXX */
9194 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9195 break;
9196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009198 /*
9199 * Skip preprocessor directives and blank lines.
9200 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009201 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009202 continue;
9203
9204 if (cin_nocode(l))
9205 continue;
9206
9207 /*
9208 * If the previous line ends in ',', use one level of
9209 * indentation:
9210 * int foo,
9211 * bar;
9212 * do this before checking for '}' in case of eg.
9213 * enum foobar
9214 * {
9215 * ...
9216 * } foo,
9217 * bar;
9218 */
9219 n = 0;
9220 if (cin_ends_in(l, (char_u *)",", NULL)
9221 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9222 {
9223 /* take us back to opening paren */
9224 if (find_last_paren(l, '(', ')')
9225 && (trypos = find_match_paren(
9226 curbuf->b_ind_maxparen)) != NULL)
9227 curwin->w_cursor = *trypos;
9228
9229 /* For a line ending in ',' that is a continuation line go
9230 * back to the first line with a backslash:
9231 * char *foo = "bla\
9232 * bla",
9233 * here;
9234 */
9235 while (n == 0 && curwin->w_cursor.lnum > 1)
9236 {
9237 l = ml_get(curwin->w_cursor.lnum - 1);
9238 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9239 break;
9240 --curwin->w_cursor.lnum;
9241 curwin->w_cursor.col = 0;
9242 }
9243
9244 amount = get_indent(); /* XXX */
9245
9246 if (amount == 0)
9247 amount = cin_first_id_amount();
9248 if (amount == 0)
9249 amount = ind_continuation;
9250 break;
9251 }
9252
9253 /*
9254 * If the line looks like a function declaration, and we're
9255 * not in a comment, put it the left margin.
9256 */
9257 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9258 break;
9259 l = ml_get_curline();
9260
9261 /*
9262 * Finding the closing '}' of a previous function. Put
9263 * current line at the left margin. For when 'cino' has "fs".
9264 */
9265 if (*skipwhite(l) == '}')
9266 break;
9267
9268 /* (matching {)
9269 * If the previous line ends on '};' (maybe followed by
9270 * comments) align at column 0. For example:
9271 * char *string_array[] = { "foo",
9272 * / * x * / "b};ar" }; / * foobar * /
9273 */
9274 if (cin_ends_in(l, (char_u *)"};", NULL))
9275 break;
9276
9277 /*
9278 * If the previous line ends on '[' we are probably in an
9279 * array constant:
9280 * something = [
9281 * 234, <- extra indent
9282 */
9283 if (cin_ends_in(l, (char_u *)"[", NULL))
9284 {
9285 amount = get_indent() + ind_continuation;
9286 break;
9287 }
9288
9289 /*
9290 * Find a line only has a semicolon that belongs to a previous
9291 * line ending in '}', e.g. before an #endif. Don't increase
9292 * indent then.
9293 */
9294 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9295 {
9296 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297
9298 while (curwin->w_cursor.lnum > 1)
9299 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009300 look = ml_get(--curwin->w_cursor.lnum);
9301 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009302 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009304 }
9305 if (curwin->w_cursor.lnum > 0
9306 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009309 curwin->w_cursor = curpos_save;
9310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009312 /*
9313 * If the PREVIOUS line is a function declaration, the current
9314 * line (and the ones that follow) needs to be indented as
9315 * parameters.
9316 */
9317 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9318 {
9319 amount = curbuf->b_ind_param;
9320 break;
9321 }
9322
9323 /*
9324 * If the previous line ends in ';' and the line before the
9325 * previous line ends in ',' or '\', ident to column zero:
9326 * int foo,
9327 * bar;
9328 * indent_to_0 here;
9329 */
9330 if (cin_ends_in(l, (char_u *)";", NULL))
9331 {
9332 l = ml_get(curwin->w_cursor.lnum - 1);
9333 if (cin_ends_in(l, (char_u *)",", NULL)
9334 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9335 break;
9336 l = ml_get_curline();
9337 }
9338
9339 /*
9340 * Doesn't look like anything interesting -- so just
9341 * use the indent of this line.
9342 *
9343 * Position the cursor over the rightmost paren, so that
9344 * matching it will take us back to the start of the line.
9345 */
9346 find_last_paren(l, '(', ')');
9347
9348 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9349 curwin->w_cursor = *trypos;
9350 amount = get_indent(); /* XXX */
9351 break;
9352 }
9353
9354 /* add extra indent for a comment */
9355 if (cin_iscomment(theline))
9356 amount += curbuf->b_ind_comment;
9357
9358 /* add extra indent if the previous line ended in a backslash:
9359 * "asdfasdf\
9360 * here";
9361 * char *foo = "asdf\
9362 * here";
9363 */
9364 if (cur_curpos.lnum > 1)
9365 {
9366 l = ml_get(cur_curpos.lnum - 1);
9367 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9368 {
9369 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9370 if (cur_amount > 0)
9371 amount = cur_amount;
9372 else if (cur_amount == 0)
9373 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 }
9375 }
9376
9377theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009378 if (amount < 0)
9379 amount = 0;
9380
9381laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 /* put the cursor back where it belongs */
9383 curwin->w_cursor = cur_curpos;
9384
9385 vim_free(linecopy);
9386
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 return amount;
9388}
9389
9390 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009391find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392{
9393 char_u *look;
9394 pos_T *theirscope;
9395 char_u *mightbeif;
9396 int elselevel;
9397 int whilelevel;
9398
9399 if (lookfor == LOOKFOR_IF)
9400 {
9401 elselevel = 1;
9402 whilelevel = 0;
9403 }
9404 else
9405 {
9406 elselevel = 0;
9407 whilelevel = 1;
9408 }
9409
9410 curwin->w_cursor.col = 0;
9411
9412 while (curwin->w_cursor.lnum > ourscope + 1)
9413 {
9414 curwin->w_cursor.lnum--;
9415 curwin->w_cursor.col = 0;
9416
9417 look = cin_skipcomment(ml_get_curline());
9418 if (cin_iselse(look)
9419 || cin_isif(look)
9420 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009421 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 {
9423 /*
9424 * if we've gone outside the braces entirely,
9425 * we must be out of scope...
9426 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009427 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 if (theirscope == NULL)
9429 break;
9430
9431 /*
9432 * and if the brace enclosing this is further
9433 * back than the one enclosing the else, we're
9434 * out of luck too.
9435 */
9436 if (theirscope->lnum < ourscope)
9437 break;
9438
9439 /*
9440 * and if they're enclosed in a *deeper* brace,
9441 * then we can ignore it because it's in a
9442 * different scope...
9443 */
9444 if (theirscope->lnum > ourscope)
9445 continue;
9446
9447 /*
9448 * if it was an "else" (that's not an "else if")
9449 * then we need to go back to another if, so
9450 * increment elselevel
9451 */
9452 look = cin_skipcomment(ml_get_curline());
9453 if (cin_iselse(look))
9454 {
9455 mightbeif = cin_skipcomment(look + 4);
9456 if (!cin_isif(mightbeif))
9457 ++elselevel;
9458 continue;
9459 }
9460
9461 /*
9462 * if it was a "while" then we need to go back to
9463 * another "do", so increment whilelevel. XXX
9464 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009465 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 ++whilelevel;
9468 continue;
9469 }
9470
9471 /* If it's an "if" decrement elselevel */
9472 look = cin_skipcomment(ml_get_curline());
9473 if (cin_isif(look))
9474 {
9475 elselevel--;
9476 /*
9477 * When looking for an "if" ignore "while"s that
9478 * get in the way.
9479 */
9480 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9481 whilelevel = 0;
9482 }
9483
9484 /* If it's a "do" decrement whilelevel */
9485 if (cin_isdo(look))
9486 whilelevel--;
9487
9488 /*
9489 * if we've used up all the elses, then
9490 * this must be the if that we want!
9491 * match the indent level of that if.
9492 */
9493 if (elselevel <= 0 && whilelevel <= 0)
9494 {
9495 return OK;
9496 }
9497 }
9498 }
9499 return FAIL;
9500}
9501
9502# if defined(FEAT_EVAL) || defined(PROTO)
9503/*
9504 * Get indent level from 'indentexpr'.
9505 */
9506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009507get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009509 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009510 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009511 pos_T save_pos;
9512 colnr_T save_curswant;
9513 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009515 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9516 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009518 /* Save and restore cursor position and curswant, in case it was changed
9519 * via :normal commands */
9520 save_pos = curwin->w_cursor;
9521 save_curswant = curwin->w_curswant;
9522 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009524 if (use_sandbox)
9525 ++sandbox;
9526 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009527
9528 /* Need to make a copy, the 'indentexpr' option could be changed while
9529 * evaluating it. */
9530 inde_copy = vim_strsave(curbuf->b_p_inde);
9531 if (inde_copy != NULL)
9532 {
9533 indent = (int)eval_to_number(inde_copy);
9534 vim_free(inde_copy);
9535 }
9536
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009537 if (use_sandbox)
9538 --sandbox;
9539 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9542 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9543 * command. */
9544 save_State = State;
9545 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009546 curwin->w_cursor = save_pos;
9547 curwin->w_curswant = save_curswant;
9548 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 check_cursor();
9550 State = save_State;
9551
9552 /* If there is an error, just keep the current indent. */
9553 if (indent < 0)
9554 indent = get_indent();
9555
9556 return indent;
9557}
9558# endif
9559
9560#endif /* FEAT_CINDENT */
9561
9562#if defined(FEAT_LISP) || defined(PROTO)
9563
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009565lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 char_u buf[LSIZE];
9568 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009569 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570
9571 while (*word != NUL)
9572 {
9573 (void)copy_option_part(&word, buf, LSIZE, ",");
9574 len = (int)STRLEN(buf);
9575 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9576 return TRUE;
9577 }
9578 return FALSE;
9579}
9580
9581/*
9582 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9583 * The incompatible newer method is quite a bit better at indenting
9584 * code in lisp-like languages than the traditional one; it's still
9585 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9586 *
9587 * TODO:
9588 * Findmatch() should be adapted for lisp, also to make showmatch
9589 * work correctly: now (v5.3) it seems all C/C++ oriented:
9590 * - it does not recognize the #\( and #\) notations as character literals
9591 * - it doesn't know about comments starting with a semicolon
9592 * - it incorrectly interprets '(' as a character literal
9593 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009594 * Update from Sergey Khorev:
9595 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 */
9597 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009598get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009600 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 int amount;
9602 char_u *that;
9603 colnr_T col;
9604 colnr_T firsttry;
9605 int parencount, quotecount;
9606 int vi_lisp;
9607
9608 /* Set vi_lisp to use the vi-compatible method */
9609 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9610
9611 realpos = curwin->w_cursor;
9612 curwin->w_cursor.col = 0;
9613
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009614 if ((pos = findmatch(NULL, '(')) == NULL)
9615 pos = findmatch(NULL, '[');
9616 else
9617 {
9618 paren = *pos;
9619 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009620 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009621 pos = &paren;
9622 }
9623 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
9625 /* Extra trick: Take the indent of the first previous non-white
9626 * line that is at the same () level. */
9627 amount = -1;
9628 parencount = 0;
9629
9630 while (--curwin->w_cursor.lnum >= pos->lnum)
9631 {
9632 if (linewhite(curwin->w_cursor.lnum))
9633 continue;
9634 for (that = ml_get_curline(); *that != NUL; ++that)
9635 {
9636 if (*that == ';')
9637 {
9638 while (*(that + 1) != NUL)
9639 ++that;
9640 continue;
9641 }
9642 if (*that == '\\')
9643 {
9644 if (*(that + 1) != NUL)
9645 ++that;
9646 continue;
9647 }
9648 if (*that == '"' && *(that + 1) != NUL)
9649 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009650 while (*++that && *that != '"')
9651 {
9652 /* skipping escaped characters in the string */
9653 if (*that == '\\')
9654 {
9655 if (*++that == NUL)
9656 break;
9657 if (that[1] == NUL)
9658 {
9659 ++that;
9660 break;
9661 }
9662 }
9663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009665 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009667 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 --parencount;
9669 }
9670 if (parencount == 0)
9671 {
9672 amount = get_indent();
9673 break;
9674 }
9675 }
9676
9677 if (amount == -1)
9678 {
9679 curwin->w_cursor.lnum = pos->lnum;
9680 curwin->w_cursor.col = pos->col;
9681 col = pos->col;
9682
9683 that = ml_get_curline();
9684
9685 if (vi_lisp && get_indent() == 0)
9686 amount = 2;
9687 else
9688 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009689 char_u *line = that;
9690
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 amount = 0;
9692 while (*that && col)
9693 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009694 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 col--;
9696 }
9697
9698 /*
9699 * Some keywords require "body" indenting rules (the
9700 * non-standard-lisp ones are Scheme special forms):
9701 *
9702 * (let ((a 1)) instead (let ((a 1))
9703 * (...)) of (...))
9704 */
9705
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009706 if (!vi_lisp && (*that == '(' || *that == '[')
9707 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 amount += 2;
9709 else
9710 {
9711 that++;
9712 amount++;
9713 firsttry = amount;
9714
Bram Moolenaar1c465442017-03-12 20:10:05 +01009715 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009717 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 ++that;
9719 }
9720
9721 if (*that && *that != ';') /* not a comment line */
9722 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009723 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009725 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 firsttry++;
9727
9728 parencount = 0;
9729 quotecount = 0;
9730
9731 if (vi_lisp
9732 || (*that != '"'
9733 && *that != '\''
9734 && *that != '#'
9735 && (*that < '0' || *that > '9')))
9736 {
9737 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009738 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 || quotecount
9740 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009741 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 && !quotecount
9743 && !parencount
9744 && vi_lisp)))
9745 {
9746 if (*that == '"')
9747 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009748 if ((*that == '(' || *that == '[')
9749 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009751 if ((*that == ')' || *that == ']')
9752 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 --parencount;
9754 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009755 amount += lbr_chartabsize_adv(
9756 line, &that, (colnr_T)amount);
9757 amount += lbr_chartabsize_adv(
9758 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 }
9760 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009761 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009763 amount += lbr_chartabsize(
9764 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 that++;
9766 }
9767 if (!*that || *that == ';')
9768 amount = firsttry;
9769 }
9770 }
9771 }
9772 }
9773 }
9774 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009775 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776
9777 curwin->w_cursor = realpos;
9778
9779 return amount;
9780}
9781#endif /* FEAT_LISP */
9782
9783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009784prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009786#if defined(SIGHUP) && defined(SIG_IGN)
9787 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9788 * makes Vim exit and then handling SIGHUP causes various reentrance
9789 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009790 signal(SIGHUP, SIG_IGN);
9791#endif
9792
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793#ifdef FEAT_GUI
9794 if (gui.in_use)
9795 {
9796 gui.dying = TRUE;
9797 out_trash(); /* trash any pending output */
9798 }
9799 else
9800#endif
9801 {
9802 windgoto((int)Rows - 1, 0);
9803
9804 /*
9805 * Switch terminal mode back now, so messages end up on the "normal"
9806 * screen (if there are two screens).
9807 */
9808 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009809 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 out_flush();
9811 }
9812}
9813
9814/*
9815 * Preserve files and exit.
9816 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009817 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9818 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 */
9820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009821preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822{
9823 buf_T *buf;
9824
9825 prepare_to_exit();
9826
Bram Moolenaar4770d092006-01-12 23:22:24 +00009827 /* Setting this will prevent free() calls. That avoids calling free()
9828 * recursively when free() was invoked with a bad pointer. */
9829 really_exiting = TRUE;
9830
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 out_str(IObuff);
9832 screen_start(); /* don't know where cursor is now */
9833 out_flush();
9834
9835 ml_close_notmod(); /* close all not-modified buffers */
9836
Bram Moolenaar29323592016-07-24 22:04:11 +02009837 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 {
9839 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9840 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009841 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 screen_start(); /* don't know where cursor is now */
9843 out_flush();
9844 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9845 break;
9846 }
9847 }
9848
9849 ml_close_all(FALSE); /* close all memfiles, without deleting */
9850
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009851 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852
9853 getout(1);
9854}
9855
9856/*
9857 * return TRUE if "fname" exists.
9858 */
9859 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009860vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009862 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
9864 if (mch_stat((char *)fname, &st))
9865 return FALSE;
9866 return TRUE;
9867}
9868
9869/*
9870 * Check for CTRL-C pressed, but only once in a while.
9871 * Should be used instead of ui_breakcheck() for functions that check for
9872 * each line in the file. Calling ui_breakcheck() each time takes too much
9873 * time, because it can be a system call.
9874 */
9875
9876#ifndef BREAKCHECK_SKIP
9877# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9878# define BREAKCHECK_SKIP 200
9879# else
9880# define BREAKCHECK_SKIP 32
9881# endif
9882#endif
9883
9884static int breakcheck_count = 0;
9885
9886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009887line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889 if (++breakcheck_count >= BREAKCHECK_SKIP)
9890 {
9891 breakcheck_count = 0;
9892 ui_breakcheck();
9893 }
9894}
9895
9896/*
9897 * Like line_breakcheck() but check 10 times less often.
9898 */
9899 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009900fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901{
9902 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9903 {
9904 breakcheck_count = 0;
9905 ui_breakcheck();
9906 }
9907}
9908
9909/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009910 * Invoke expand_wildcards() for one pattern.
9911 * Expand items like "%:h" before the expansion.
9912 * Returns OK or FAIL.
9913 */
9914 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009915expand_wildcards_eval(
9916 char_u **pat, /* pointer to input pattern */
9917 int *num_file, /* resulting number of files */
9918 char_u ***file, /* array of resulting files */
9919 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009920{
9921 int ret = FAIL;
9922 char_u *eval_pat = NULL;
9923 char_u *exp_pat = *pat;
9924 char_u *ignored_msg;
9925 int usedlen;
9926
9927 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9928 {
9929 ++emsg_off;
9930 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9931 NULL, &ignored_msg, NULL);
9932 --emsg_off;
9933 if (eval_pat != NULL)
9934 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9935 }
9936
9937 if (exp_pat != NULL)
9938 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9939
9940 if (eval_pat != NULL)
9941 {
9942 vim_free(exp_pat);
9943 vim_free(eval_pat);
9944 }
9945
9946 return ret;
9947}
9948
9949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9951 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009952 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 */
9954 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009955expand_wildcards(
9956 int num_pat, /* number of input patterns */
9957 char_u **pat, /* array of input patterns */
9958 int *num_files, /* resulting number of files */
9959 char_u ***files, /* array of resulting files */
9960 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961{
9962 int retval;
9963 int i, j;
9964 char_u *p;
9965 int non_suf_match; /* number without matching suffix */
9966
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009967 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
9969 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009970 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 return retval;
9972
9973#ifdef FEAT_WILDIGN
9974 /*
9975 * Remove names that match 'wildignore'.
9976 */
9977 if (*p_wig)
9978 {
9979 char_u *ffname;
9980
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009981 /* check all files in (*files)[] */
9982 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009984 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 if (ffname == NULL) /* out of memory */
9986 break;
9987# ifdef VMS
9988 vms_remove_version(ffname);
9989# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009990 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009992 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009993 vim_free((*files)[i]);
9994 for (j = i; j + 1 < *num_files; ++j)
9995 (*files)[j] = (*files)[j + 1];
9996 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 --i;
9998 }
9999 vim_free(ffname);
10000 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010001
10002 /* If the number of matches is now zero, we fail. */
10003 if (*num_files == 0)
10004 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010005 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010006 return FAIL;
10007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 }
10009#endif
10010
10011 /*
10012 * Move the names where 'suffixes' match to the end.
10013 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010014 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 {
10016 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010017 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010019 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 {
10021 /*
10022 * Move the name without matching suffix to the front
10023 * of the list.
10024 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010025 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010027 (*files)[j] = (*files)[j - 1];
10028 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 }
10030 }
10031 }
10032
10033 return retval;
10034}
10035
10036/*
10037 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10038 */
10039 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010040match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
10042 int fnamelen, setsuflen;
10043 char_u *setsuf;
10044#define MAXSUFLEN 30 /* maximum length of a file suffix */
10045 char_u suf_buf[MAXSUFLEN];
10046
10047 fnamelen = (int)STRLEN(fname);
10048 setsuflen = 0;
10049 for (setsuf = p_su; *setsuf; )
10050 {
10051 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010052 if (setsuflen == 0)
10053 {
10054 char_u *tail = gettail(fname);
10055
10056 /* empty entry: match name without a '.' */
10057 if (vim_strchr(tail, '.') == NULL)
10058 {
10059 setsuflen = 1;
10060 break;
10061 }
10062 }
10063 else
10064 {
10065 if (fnamelen >= setsuflen
10066 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10067 (size_t)setsuflen) == 0)
10068 break;
10069 setsuflen = 0;
10070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 }
10072 return (setsuflen != 0);
10073}
10074
10075#if !defined(NO_EXPANDPATH) || defined(PROTO)
10076
10077# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010078static int vim_backtick(char_u *p);
10079static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080# endif
10081
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010082# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083/*
10084 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10085 * it's shared between these systems.
10086 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010087# if defined(PROTO)
10088# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089# else
10090# ifdef __BORLANDC__
10091# define _cdecl _RTLENTRYF
10092# endif
10093# endif
10094
10095/*
10096 * comparison function for qsort in dos_expandpath()
10097 */
10098 static int _cdecl
10099pstrcmp(const void *a, const void *b)
10100{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010101 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102}
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010105 * Recursively expand one path component into all matching files and/or
10106 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 * Return the number of matches found.
10108 * "path" has backslashes before chars that are not to be expanded, starting
10109 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010110 * Return the number of matches found.
10111 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 */
10113 static int
10114dos_expandpath(
10115 garray_T *gap,
10116 char_u *path,
10117 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010118 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010119 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010121 char_u *buf;
10122 char_u *path_end;
10123 char_u *p, *s, *e;
10124 int start_len = gap->ga_len;
10125 char_u *pat;
10126 regmatch_T regmatch;
10127 int starts_with_dot;
10128 int matches;
10129 int len;
10130 int starstar = FALSE;
10131 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 WIN32_FIND_DATA fb;
10133 HANDLE hFind = (HANDLE)0;
10134# ifdef FEAT_MBYTE
10135 WIN32_FIND_DATAW wfb;
10136 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010139 int ok;
10140
10141 /* Expanding "**" may take a long time, check for CTRL-C. */
10142 if (stardepth > 0)
10143 {
10144 ui_breakcheck();
10145 if (got_int)
10146 return 0;
10147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010149 /* Make room for file name. When doing encoding conversion the actual
10150 * length may be quite a bit longer, thus use the maximum possible length. */
10151 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 if (buf == NULL)
10153 return 0;
10154
10155 /*
10156 * Find the first part in the path name that contains a wildcard or a ~1.
10157 * Copy it into buf, including the preceding characters.
10158 */
10159 p = buf;
10160 s = buf;
10161 e = NULL;
10162 path_end = path;
10163 while (*path_end != NUL)
10164 {
10165 /* May ignore a wildcard that has a backslash before it; it will
10166 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10167 if (path_end >= path + wildoff && rem_backslash(path_end))
10168 *p++ = *path_end++;
10169 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10170 {
10171 if (e != NULL)
10172 break;
10173 s = p + 1;
10174 }
10175 else if (path_end >= path + wildoff
10176 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10177 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010178# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (has_mbyte)
10180 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010181 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 STRNCPY(p, path_end, len);
10183 p += len;
10184 path_end += len;
10185 }
10186 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 *p++ = *path_end++;
10189 }
10190 e = p;
10191 *e = NUL;
10192
10193 /* now we have one wildcard component between s and e */
10194 /* Remove backslashes between "wildoff" and the start of the wildcard
10195 * component. */
10196 for (p = buf + wildoff; p < s; ++p)
10197 if (rem_backslash(p))
10198 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010199 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 --e;
10201 --s;
10202 }
10203
Bram Moolenaar231334e2005-07-25 20:46:57 +000010204 /* Check for "**" between "s" and "e". */
10205 for (p = s; p < e; ++p)
10206 if (p[0] == '*' && p[1] == '*')
10207 starstar = TRUE;
10208
Bram Moolenaard82103e2016-01-17 17:04:05 +010010209 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10211 if (pat == NULL)
10212 {
10213 vim_free(buf);
10214 return 0;
10215 }
10216
10217 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010218 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010219 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 regmatch.rm_ic = TRUE; /* Always ignore case */
10221 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010222 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010223 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 vim_free(pat);
10225
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010226 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 {
10228 vim_free(buf);
10229 return 0;
10230 }
10231
10232 /* remember the pattern or file name being looked for */
10233 matchname = vim_strsave(s);
10234
Bram Moolenaar231334e2005-07-25 20:46:57 +000010235 /* If "**" is by itself, this is the first time we encounter it and more
10236 * is following then find matches without any directory. */
10237 if (!didstar && stardepth < 100 && starstar && e - s == 2
10238 && *path_end == '/')
10239 {
10240 STRCPY(s, path_end + 1);
10241 ++stardepth;
10242 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10243 --stardepth;
10244 }
10245
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 /* Scan all files in the directory with "dir/ *.*" */
10247 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248# ifdef FEAT_MBYTE
10249 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10250 {
10251 /* The active codepage differs from 'encoding'. Attempt using the
10252 * wide function. If it fails because it is not implemented fall back
10253 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010254 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 if (wn != NULL)
10256 {
10257 hFind = FindFirstFileW(wn, &wfb);
10258 if (hFind == INVALID_HANDLE_VALUE
10259 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010260 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 }
10262 }
10263
10264 if (wn == NULL)
10265# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010266 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268
10269 while (ok)
10270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271# ifdef FEAT_MBYTE
10272 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010273 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 else
10275# endif
10276 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 /* Ignore entries starting with a dot, unless when asked for. Accept
10278 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010279 if ((p[0] != '.' || starts_with_dot
10280 || ((flags & EW_DODOT)
10281 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010283 || (regmatch.regprog != NULL
10284 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010285 || ((flags & EW_NOTWILD)
10286 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010290
10291 if (starstar && stardepth < 100)
10292 {
10293 /* For "**" in the pattern first go deeper in the tree to
10294 * find matches. */
10295 STRCPY(buf + len, "/**");
10296 STRCPY(buf + len + 3, path_end);
10297 ++stardepth;
10298 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10299 --stardepth;
10300 }
10301
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 STRCPY(buf + len, path_end);
10303 if (mch_has_exp_wildcard(path_end))
10304 {
10305 /* need to expand another component of the path */
10306 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010307 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 }
10309 else
10310 {
10311 /* no more wildcards, check if there is a match */
10312 /* remove backslashes for the remaining components only */
10313 if (*path_end != 0)
10314 backslash_halve(buf + len + 1);
10315 if (mch_getperm(buf) >= 0) /* add existing file */
10316 addfile(gap, buf, flags);
10317 }
10318 }
10319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320# ifdef FEAT_MBYTE
10321 if (wn != NULL)
10322 {
10323 vim_free(p);
10324 ok = FindNextFileW(hFind, &wfb);
10325 }
10326 else
10327# endif
10328 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329
10330 /* If no more matches and no match was used, try expanding the name
10331 * itself. Finds the long name of a short filename. */
10332 if (!ok && matchname != NULL && gap->ga_len == start_len)
10333 {
10334 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 FindClose(hFind);
10336# ifdef FEAT_MBYTE
10337 if (wn != NULL)
10338 {
10339 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010340 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 if (wn != NULL)
10342 hFind = FindFirstFileW(wn, &wfb);
10343 }
10344 if (wn == NULL)
10345# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010346 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010348 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 }
10350 }
10351
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 FindClose(hFind);
10353# ifdef FEAT_MBYTE
10354 vim_free(wn);
10355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010357 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 vim_free(matchname);
10359
10360 matches = gap->ga_len - start_len;
10361 if (matches > 0)
10362 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10363 sizeof(char_u *), pstrcmp);
10364 return matches;
10365}
10366
10367 int
10368mch_expandpath(
10369 garray_T *gap,
10370 char_u *path,
10371 int flags) /* EW_* flags */
10372{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010373 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010375# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376
Bram Moolenaar231334e2005-07-25 20:46:57 +000010377#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10378 || defined(PROTO)
10379/*
10380 * Unix style wildcard expansion code.
10381 * It's here because it's used both for Unix and Mac.
10382 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010383 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010384pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010385{
10386 return (pathcmp(*(char **)a, *(char **)b, -1));
10387}
10388
10389/*
10390 * Recursively expand one path component into all matching files and/or
10391 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10392 * "path" has backslashes before chars that are not to be expanded, starting
10393 * at "path + wildoff".
10394 * Return the number of matches found.
10395 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10396 */
10397 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010398unix_expandpath(
10399 garray_T *gap,
10400 char_u *path,
10401 int wildoff,
10402 int flags, /* EW_* flags */
10403 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010404{
10405 char_u *buf;
10406 char_u *path_end;
10407 char_u *p, *s, *e;
10408 int start_len = gap->ga_len;
10409 char_u *pat;
10410 regmatch_T regmatch;
10411 int starts_with_dot;
10412 int matches;
10413 int len;
10414 int starstar = FALSE;
10415 static int stardepth = 0; /* depth for "**" expansion */
10416
10417 DIR *dirp;
10418 struct dirent *dp;
10419
10420 /* Expanding "**" may take a long time, check for CTRL-C. */
10421 if (stardepth > 0)
10422 {
10423 ui_breakcheck();
10424 if (got_int)
10425 return 0;
10426 }
10427
10428 /* make room for file name */
10429 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10430 if (buf == NULL)
10431 return 0;
10432
10433 /*
10434 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010435 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010436 * Copy it into "buf", including the preceding characters.
10437 */
10438 p = buf;
10439 s = buf;
10440 e = NULL;
10441 path_end = path;
10442 while (*path_end != NUL)
10443 {
10444 /* May ignore a wildcard that has a backslash before it; it will
10445 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10446 if (path_end >= path + wildoff && rem_backslash(path_end))
10447 *p++ = *path_end++;
10448 else if (*path_end == '/')
10449 {
10450 if (e != NULL)
10451 break;
10452 s = p + 1;
10453 }
10454 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010455 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010456 || (!p_fic && (flags & EW_ICASE)
10457 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010458 e = p;
10459#ifdef FEAT_MBYTE
10460 if (has_mbyte)
10461 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010462 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010463 STRNCPY(p, path_end, len);
10464 p += len;
10465 path_end += len;
10466 }
10467 else
10468#endif
10469 *p++ = *path_end++;
10470 }
10471 e = p;
10472 *e = NUL;
10473
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010474 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010475 /* Remove backslashes between "wildoff" and the start of the wildcard
10476 * component. */
10477 for (p = buf + wildoff; p < s; ++p)
10478 if (rem_backslash(p))
10479 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010480 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010481 --e;
10482 --s;
10483 }
10484
10485 /* Check for "**" between "s" and "e". */
10486 for (p = s; p < e; ++p)
10487 if (p[0] == '*' && p[1] == '*')
10488 starstar = TRUE;
10489
10490 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010491 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010492 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10493 if (pat == NULL)
10494 {
10495 vim_free(buf);
10496 return 0;
10497 }
10498
10499 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010500 if (flags & EW_ICASE)
10501 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10502 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010503 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010504 if (flags & (EW_NOERROR | EW_NOTWILD))
10505 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010506 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010507 if (flags & (EW_NOERROR | EW_NOTWILD))
10508 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010509 vim_free(pat);
10510
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010511 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010512 {
10513 vim_free(buf);
10514 return 0;
10515 }
10516
10517 /* If "**" is by itself, this is the first time we encounter it and more
10518 * is following then find matches without any directory. */
10519 if (!didstar && stardepth < 100 && starstar && e - s == 2
10520 && *path_end == '/')
10521 {
10522 STRCPY(s, path_end + 1);
10523 ++stardepth;
10524 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10525 --stardepth;
10526 }
10527
10528 /* open the directory for scanning */
10529 *s = NUL;
10530 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10531
10532 /* Find all matching entries */
10533 if (dirp != NULL)
10534 {
10535 for (;;)
10536 {
10537 dp = readdir(dirp);
10538 if (dp == NULL)
10539 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010540 if ((dp->d_name[0] != '.' || starts_with_dot
10541 || ((flags & EW_DODOT)
10542 && dp->d_name[1] != NUL
10543 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010544 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10545 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010546 || ((flags & EW_NOTWILD)
10547 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010548 {
10549 STRCPY(s, dp->d_name);
10550 len = STRLEN(buf);
10551
10552 if (starstar && stardepth < 100)
10553 {
10554 /* For "**" in the pattern first go deeper in the tree to
10555 * find matches. */
10556 STRCPY(buf + len, "/**");
10557 STRCPY(buf + len + 3, path_end);
10558 ++stardepth;
10559 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10560 --stardepth;
10561 }
10562
10563 STRCPY(buf + len, path_end);
10564 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10565 {
10566 /* need to expand another component of the path */
10567 /* remove backslashes for the remaining components only */
10568 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10569 }
10570 else
10571 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010572 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010573
Bram Moolenaar231334e2005-07-25 20:46:57 +000010574 /* no more wildcards, check if there is a match */
10575 /* remove backslashes for the remaining components only */
10576 if (*path_end != NUL)
10577 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010578 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010579 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010580 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010581 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010582#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010583 size_t precomp_len = STRLEN(buf)+1;
10584 char_u *precomp_buf =
10585 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010586
Bram Moolenaar231334e2005-07-25 20:46:57 +000010587 if (precomp_buf)
10588 {
10589 mch_memmove(buf, precomp_buf, precomp_len);
10590 vim_free(precomp_buf);
10591 }
10592#endif
10593 addfile(gap, buf, flags);
10594 }
10595 }
10596 }
10597 }
10598
10599 closedir(dirp);
10600 }
10601
10602 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010603 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010604
10605 matches = gap->ga_len - start_len;
10606 if (matches > 0)
10607 qsort(((char_u **)gap->ga_data) + start_len, matches,
10608 sizeof(char_u *), pstrcmp);
10609 return matches;
10610}
10611#endif
10612
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010613#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010614/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010615 * Moves "*psep" back to the previous path separator in "path".
10616 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010617 */
10618 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010619find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010620{
10621 /* skip the current separator */
10622 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010623 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010624
10625 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010626 while (*psep > path)
10627 {
10628 if (vim_ispathsep(**psep))
10629 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010630 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010631 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010632
10633 return FAIL;
10634}
10635
10636/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010637 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10638 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010639 */
10640 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010641is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010642{
10643 int j;
10644 int candidate_len;
10645 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010646 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010647 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010648
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010649 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010650 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010651 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010652 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010653
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010654 candidate_len = (int)STRLEN(maybe_unique);
10655 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010656 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010657 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010658
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010659 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010660 if (fnamecmp(maybe_unique, rival) == 0
10661 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010662 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010663 }
10664
Bram Moolenaar162bd912010-07-28 22:29:10 +020010665 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010666}
10667
10668/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010669 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010670 * paths are expanded to their equivalent fullpath. This includes the "."
10671 * (relative to current buffer directory) and empty path (relative to current
10672 * directory) notations.
10673 *
10674 * TODO: handle upward search (;) and path limiter (**N) notations by
10675 * expanding each into their equivalent path(s).
10676 */
10677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010678expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010679{
10680 char_u *path_option = *curbuf->b_p_path == NUL
10681 ? p_path : curbuf->b_p_path;
10682 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010683 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010684 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010685
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010686 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010687 return;
10688
10689 while (*path_option != NUL)
10690 {
10691 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10692
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010693 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010694 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010695 /* Relative to current buffer:
10696 * "/path/file" + "." -> "/path/"
10697 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010698 if (curbuf->b_ffname == NULL)
10699 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010700 p = gettail(curbuf->b_ffname);
10701 len = (int)(p - curbuf->b_ffname);
10702 if (len + (int)STRLEN(buf) >= MAXPATHL)
10703 continue;
10704 if (buf[1] == NUL)
10705 buf[len] = NUL;
10706 else
10707 STRMOVE(buf + len, buf + 2);
10708 mch_memmove(buf, curbuf->b_ffname, len);
10709 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010710 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010711 else if (buf[0] == NUL)
10712 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010713 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010714 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010715 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010716 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010717 else if (!mch_isFullName(buf))
10718 {
10719 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010720 len = (int)STRLEN(curdir);
10721 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010722 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010723 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010724 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010725 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010726 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010727 }
10728
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010729 if (ga_grow(gap, 1) == FAIL)
10730 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010731
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010732# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010733 /* Avoid the path ending in a backslash, it fails when a comma is
10734 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010735 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010736 if (buf[len - 1] == '\\')
10737 buf[len - 1] = '/';
10738# endif
10739
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010740 p = vim_strsave(buf);
10741 if (p == NULL)
10742 break;
10743 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010744 }
10745
10746 vim_free(buf);
10747}
10748
10749/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010750 * Returns a pointer to the file or directory name in "fname" that matches the
10751 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010752 *
10753 * path: /foo/bar/baz
10754 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010755 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010756 */
10757 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010758get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010759{
10760 int i;
10761 int maxlen = 0;
10762 char_u **path_part = (char_u **)gap->ga_data;
10763 char_u *cutoff = NULL;
10764
10765 for (i = 0; i < gap->ga_len; i++)
10766 {
10767 int j = 0;
10768
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010769 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010770# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010771 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10772#endif
10773 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010774 j++;
10775 if (j > maxlen)
10776 {
10777 maxlen = j;
10778 cutoff = &fname[j];
10779 }
10780 }
10781
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010782 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010783 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010784 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010785 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010786
10787 return cutoff;
10788}
10789
10790/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010791 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10792 * that they are unique with respect to each other while conserving the part
10793 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010794 */
10795 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010796uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010797{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010798 int i;
10799 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010800 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010801 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010802 char_u *pat;
10803 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010804 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010805 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010806 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010807 char_u **in_curdir = NULL;
10808 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010809
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010810 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010811 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010812
10813 /*
10814 * We need to prepend a '*' at the beginning of file_pattern so that the
10815 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010816 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010817 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010818 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010819 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010820 if (file_pattern == NULL)
10821 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010822 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010823 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010824 STRCAT(file_pattern, pattern);
10825 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10826 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010827 if (pat == NULL)
10828 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010829
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010830 regmatch.rm_ic = TRUE; /* always ignore case */
10831 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10832 vim_free(pat);
10833 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010834 return;
10835
Bram Moolenaar162bd912010-07-28 22:29:10 +020010836 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010837 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010838 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010839 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010840
10841 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010842 if (in_curdir == NULL)
10843 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010844
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010845 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010846 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010847 char_u *path = fnames[i];
10848 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010849 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010850 char_u *pathsep_p;
10851 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010852
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010853 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010854 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010855 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010856 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010857 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010858
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010859 /* Shorten the filename while maintaining its uniqueness */
10860 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010861
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010862 /* Don't assume all files can be reached without path when search
10863 * pattern starts with star star slash, so only remove path_cutoff
10864 * when possible. */
10865 if (pattern[0] == '*' && pattern[1] == '*'
10866 && vim_ispathsep_nocolon(pattern[2])
10867 && path_cutoff != NULL
10868 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10869 && is_unique(path_cutoff, gap, i))
10870 {
10871 sort_again = TRUE;
10872 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10873 }
10874 else
10875 {
10876 /* Here all files can be reached without path, so get shortest
10877 * unique path. We start at the end of the path. */
10878 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010879
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010880 while (find_previous_pathsep(path, &pathsep_p))
10881 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10882 && is_unique(pathsep_p + 1, gap, i)
10883 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10884 {
10885 sort_again = TRUE;
10886 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10887 break;
10888 }
10889 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010890
10891 if (mch_isFullName(path))
10892 {
10893 /*
10894 * Last resort: shorten relative to curdir if possible.
10895 * 'possible' means:
10896 * 1. It is under the current directory.
10897 * 2. The result is actually shorter than the original.
10898 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010899 * Before curdir After
10900 * /foo/bar/file.txt /foo/bar ./file.txt
10901 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10902 * /file.txt / /file.txt
10903 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010904 */
10905 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010906 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010907#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010908 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010909 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010910 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010911 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010912 && !vim_ispathsep(*short_name)
10913#endif
10914 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010915 {
10916 STRCPY(path, ".");
10917 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010918 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010919 }
10920 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010921 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010922 }
10923
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010924 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010925 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010926 {
10927 char_u *rel_path;
10928 char_u *path = in_curdir[i];
10929
10930 if (path == NULL)
10931 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010932
10933 /* If the {filename} is not unique, change it to ./{filename}.
10934 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010935 short_name = shorten_fname(path, curdir);
10936 if (short_name == NULL)
10937 short_name = path;
10938 if (is_unique(short_name, gap, i))
10939 {
10940 STRCPY(fnames[i], short_name);
10941 continue;
10942 }
10943
10944 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10945 if (rel_path == NULL)
10946 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010947 STRCPY(rel_path, ".");
10948 add_pathsep(rel_path);
10949 STRCAT(rel_path, short_name);
10950
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010951 vim_free(fnames[i]);
10952 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010953 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010954 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010955 }
10956
Bram Moolenaar162bd912010-07-28 22:29:10 +020010957theend:
10958 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010959 if (in_curdir != NULL)
10960 {
10961 for (i = 0; i < gap->ga_len; i++)
10962 vim_free(in_curdir[i]);
10963 vim_free(in_curdir);
10964 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010965 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010966 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010967
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010968 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010969 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010970}
10971
10972/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010973 * Calls globpath() with 'path' values for the given pattern and stores the
10974 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010975 * Returns the total number of matches.
10976 */
10977 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010978expand_in_path(
10979 garray_T *gap,
10980 char_u *pattern,
10981 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010982{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010983 char_u *curdir;
10984 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010985 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010986 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010987
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010988 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010989 return 0;
10990 mch_dirname(curdir, MAXPATHL);
10991
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010992 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010993 expand_path_option(curdir, &path_ga);
10994 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010995 if (path_ga.ga_len == 0)
10996 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010997
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010998 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010999 ga_clear_strings(&path_ga);
11000 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011001 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011002
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011003 if (flags & EW_ICASE)
11004 glob_flags |= WILD_ICASE;
11005 if (flags & EW_ADDSLASH)
11006 glob_flags |= WILD_ADD_SLASH;
11007 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011008 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011009
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011010 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011011}
11012#endif
11013
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011014#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11015/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011016 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11017 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011018 */
11019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011020remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011021{
11022 int i;
11023 int j;
11024 char_u **fnames = (char_u **)gap->ga_data;
11025
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011026 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011027 for (i = gap->ga_len - 1; i > 0; --i)
11028 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11029 {
11030 vim_free(fnames[i]);
11031 for (j = i + 1; j < gap->ga_len; ++j)
11032 fnames[j - 1] = fnames[j];
11033 --gap->ga_len;
11034 }
11035}
11036#endif
11037
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011038/*
11039 * Return TRUE if "p" contains what looks like an environment variable.
11040 * Allowing for escaping.
11041 */
11042 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011043has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011044{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011045 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011046 {
11047 if (*p == '\\' && p[1] != NUL)
11048 ++p;
11049 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011050#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011051 "$%"
11052#else
11053 "$"
11054#endif
11055 , *p) != NULL)
11056 return TRUE;
11057 }
11058 return FALSE;
11059}
11060
11061#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011062/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011063 * Return TRUE if "p" contains a special wildcard character, one that Vim
11064 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011065 */
11066 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011067has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011068{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011069 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011070 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011071 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011072 if (*p == '\\' && p[1] != NUL)
11073 ++p;
11074 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11075 return TRUE;
11076 }
11077 return FALSE;
11078}
11079#endif
11080
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081/*
11082 * Generic wildcard expansion code.
11083 *
11084 * Characters in "pat" that should not be expanded must be preceded with a
11085 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11086 *
11087 * Return FAIL when no single file was found. In this case "num_file" is not
11088 * set, and "file" may contain an error message.
11089 * Return OK when some files found. "num_file" is set to the number of
11090 * matches, "file" to the array of matches. Call FreeWild() later.
11091 */
11092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011093gen_expand_wildcards(
11094 int num_pat, /* number of input patterns */
11095 char_u **pat, /* array of input patterns */
11096 int *num_file, /* resulting number of files */
11097 char_u ***file, /* array of resulting files */
11098 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099{
11100 int i;
11101 garray_T ga;
11102 char_u *p;
11103 static int recursive = FALSE;
11104 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011105 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011106#if defined(FEAT_SEARCHPATH)
11107 int did_expand_in_path = FALSE;
11108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
11110 /*
11111 * expand_env() is called to expand things like "~user". If this fails,
11112 * it calls ExpandOne(), which brings us back here. In this case, always
11113 * call the machine specific expansion function, if possible. Otherwise,
11114 * return FAIL.
11115 */
11116 if (recursive)
11117#ifdef SPECIAL_WILDCHAR
11118 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11119#else
11120 return FAIL;
11121#endif
11122
11123#ifdef SPECIAL_WILDCHAR
11124 /*
11125 * If there are any special wildcard characters which we cannot handle
11126 * here, call machine specific function for all the expansion. This
11127 * avoids starting the shell for each argument separately.
11128 * For `=expr` do use the internal function.
11129 */
11130 for (i = 0; i < num_pat; i++)
11131 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011132 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133# ifdef VIM_BACKTICK
11134 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11135# endif
11136 )
11137 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11138 }
11139#endif
11140
11141 recursive = TRUE;
11142
11143 /*
11144 * The matching file names are stored in a growarray. Init it empty.
11145 */
11146 ga_init2(&ga, (int)sizeof(char_u *), 30);
11147
11148 for (i = 0; i < num_pat; ++i)
11149 {
11150 add_pat = -1;
11151 p = pat[i];
11152
11153#ifdef VIM_BACKTICK
11154 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011157 if (add_pat == -1)
11158 retval = FAIL;
11159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 else
11161#endif
11162 {
11163 /*
11164 * First expand environment variables, "~/" and "~user/".
11165 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011166 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011168 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 if (p == NULL)
11170 p = pat[i];
11171#ifdef UNIX
11172 /*
11173 * On Unix, if expand_env() can't expand an environment
11174 * variable, use the shell to do that. Discard previously
11175 * found file names and start all over again.
11176 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011177 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 {
11179 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011180 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011182 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 recursive = FALSE;
11184 return i;
11185 }
11186#endif
11187 }
11188
11189 /*
11190 * If there are wildcards: Expand file names and add each match to
11191 * the list. If there is no match, and EW_NOTFOUND is given, add
11192 * the pattern.
11193 * If there are no wildcards: Add the file name if it exists or
11194 * when EW_NOTFOUND is given.
11195 */
11196 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011197 {
11198#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011199 if ((flags & EW_PATH)
11200 && !mch_isFullName(p)
11201 && !(p[0] == '.'
11202 && (vim_ispathsep(p[1])
11203 || (p[1] == '.' && vim_ispathsep(p[2]))))
11204 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011205 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011206 /* :find completion where 'path' is used.
11207 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011208 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011209 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011210 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011211 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011212 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011213 else
11214#endif
11215 add_pat = mch_expandpath(&ga, p, flags);
11216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 }
11218
11219 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11220 {
11221 char_u *t = backslash_halve_save(p);
11222
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11224 * "vim c:/" work. */
11225 if (flags & EW_NOTFOUND)
11226 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 addfile(&ga, t, flags);
11229 vim_free(t);
11230 }
11231
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011232#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011233 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011234 uniquefy_paths(&ga, p);
11235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 if (p != pat[i])
11237 vim_free(p);
11238 }
11239
11240 *num_file = ga.ga_len;
11241 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11242
11243 recursive = FALSE;
11244
Bram Moolenaar336bd622016-01-17 18:23:58 +010011245 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246}
11247
11248# ifdef VIM_BACKTICK
11249
11250/*
11251 * Return TRUE if we can expand this backtick thing here.
11252 */
11253 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011254vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255{
11256 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11257}
11258
11259/*
11260 * Expand an item in `backticks` by executing it as a command.
11261 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011262 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 */
11264 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011265expand_backtick(
11266 garray_T *gap,
11267 char_u *pat,
11268 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
11270 char_u *p;
11271 char_u *cmd;
11272 char_u *buffer;
11273 int cnt = 0;
11274 int i;
11275
11276 /* Create the command: lop off the backticks. */
11277 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11278 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011279 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280
11281#ifdef FEAT_EVAL
11282 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011283 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 else
11285#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011286 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011287 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 vim_free(cmd);
11289 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011290 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
11292 cmd = buffer;
11293 while (*cmd != NUL)
11294 {
11295 cmd = skipwhite(cmd); /* skip over white space */
11296 p = cmd;
11297 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11298 ++p;
11299 /* add an entry if it is not empty */
11300 if (p > cmd)
11301 {
11302 i = *p;
11303 *p = NUL;
11304 addfile(gap, cmd, flags);
11305 *p = i;
11306 ++cnt;
11307 }
11308 cmd = p;
11309 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11310 ++cmd;
11311 }
11312
11313 vim_free(buffer);
11314 return cnt;
11315}
11316# endif /* VIM_BACKTICK */
11317
11318/*
11319 * Add a file to a file list. Accepted flags:
11320 * EW_DIR add directories
11321 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011322 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 * EW_NOTFOUND add even when it doesn't exist
11324 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011325 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 */
11327 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011328addfile(
11329 garray_T *gap,
11330 char_u *f, /* filename */
11331 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332{
11333 char_u *p;
11334 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011335 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011337 /* if the file/dir/link doesn't exist, may not add it */
11338 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011339 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 return;
11341
11342#ifdef FNAME_ILLEGAL
11343 /* if the file/dir contains illegal characters, don't add it */
11344 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11345 return;
11346#endif
11347
11348 isdir = mch_isdir(f);
11349 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11350 return;
11351
Bram Moolenaarb5971142015-03-21 17:32:19 +010011352 /* If the file isn't executable, may not add it. Do accept directories.
11353 * When invoked from expand_shellcmd() do not use $PATH. */
11354 if (!isdir && (flags & EW_EXEC)
11355 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011356 return;
11357
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 /* Make room for another item in the file list. */
11359 if (ga_grow(gap, 1) == FAIL)
11360 return;
11361
11362 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11363 if (p == NULL)
11364 return;
11365
11366 STRCPY(p, f);
11367#ifdef BACKSLASH_IN_FILENAME
11368 slash_adjust(p);
11369#endif
11370 /*
11371 * Append a slash or backslash after directory names if none is present.
11372 */
11373#ifndef DONT_ADD_PATHSEP_TO_DIR
11374 if (isdir && (flags & EW_ADDSLASH))
11375 add_pathsep(p);
11376#endif
11377 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378}
11379#endif /* !NO_EXPANDPATH */
11380
11381#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11382
11383#ifndef SEEK_SET
11384# define SEEK_SET 0
11385#endif
11386#ifndef SEEK_END
11387# define SEEK_END 2
11388#endif
11389
11390/*
11391 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011392 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11393 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 * Returns an allocated string, or NULL for error.
11395 */
11396 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011397get_cmd_output(
11398 char_u *cmd,
11399 char_u *infile, /* optional input file name */
11400 int flags, /* can be SHELL_SILENT */
11401 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402{
11403 char_u *tempname;
11404 char_u *command;
11405 char_u *buffer = NULL;
11406 int len;
11407 int i = 0;
11408 FILE *fd;
11409
11410 if (check_restricted() || check_secure())
11411 return NULL;
11412
11413 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011414 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 {
11416 EMSG(_(e_notmp));
11417 return NULL;
11418 }
11419
11420 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011421 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 if (command == NULL)
11423 goto done;
11424
11425 /*
11426 * Call the shell to execute the command (errors are ignored).
11427 * Don't check timestamps here.
11428 */
11429 ++no_check_timestamps;
11430 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11431 --no_check_timestamps;
11432
11433 vim_free(command);
11434
11435 /*
11436 * read the names from the file into memory
11437 */
11438# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011439 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 fd = mch_fopen((char *)tempname, "r");
11441# else
11442 fd = mch_fopen((char *)tempname, READBIN);
11443# endif
11444
11445 if (fd == NULL)
11446 {
11447 EMSG2(_(e_notopen), tempname);
11448 goto done;
11449 }
11450
11451 fseek(fd, 0L, SEEK_END);
11452 len = ftell(fd); /* get size of temp file */
11453 fseek(fd, 0L, SEEK_SET);
11454
11455 buffer = alloc(len + 1);
11456 if (buffer != NULL)
11457 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11458 fclose(fd);
11459 mch_remove(tempname);
11460 if (buffer == NULL)
11461 goto done;
11462#ifdef VMS
11463 len = i; /* VMS doesn't give us what we asked for... */
11464#endif
11465 if (i != len)
11466 {
11467 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011468 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011470 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011471 {
11472 /* Change NUL into SOH, otherwise the string is truncated. */
11473 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011474 if (buffer[i] == NUL)
11475 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011476
Bram Moolenaar162bd912010-07-28 22:29:10 +020011477 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011478 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011479 else
11480 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481
11482done:
11483 vim_free(tempname);
11484 return buffer;
11485}
11486#endif
11487
11488/*
11489 * Free the list of files returned by expand_wildcards() or other expansion
11490 * functions.
11491 */
11492 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011493FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011495 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 while (count--)
11498 vim_free(files[count]);
11499 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500}
11501
11502/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011503 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 * Don't do this when still processing a command or a mapping.
11505 * Don't do this when inside a ":normal" command.
11506 */
11507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011508goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509{
11510 return (p_im && stuff_empty() && typebuf_typed());
11511}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011512
11513/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011514 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011515 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11516 * - Remove any argument. E.g., "csh -f" -> "csh".
11517 * But don't allow a space in the path, so that this works:
11518 * "/usr/bin/csh --rcfile ~/.cshrc"
11519 * But don't do that for Windows, it's common to have a space in the path.
11520 */
11521 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011522get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011523{
11524 char_u *p;
11525
11526#ifdef WIN3264
11527 p = gettail(p_sh);
11528 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11529#else
11530 p = skiptowhite(p_sh);
11531 if (*p == NUL)
11532 {
11533 /* No white space, use the tail. */
11534 p = vim_strsave(gettail(p_sh));
11535 }
11536 else
11537 {
11538 char_u *p1, *p2;
11539
11540 /* Find the last path separator before the space. */
11541 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011542 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011543 if (vim_ispathsep(*p2))
11544 p1 = p2 + 1;
11545 p = vim_strnsave(p1, (int)(p - p1));
11546 }
11547#endif
11548 return p;
11549}