blob: f1f0074787a840b448bf220557604090c5a1ef3b [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,
1708 1L, (long)-less_cols);
1709 }
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;
2634 linenr_T lnum = curwin->w_cursor.lnum;
2635 colnr_T col = curwin->w_cursor.col;
2636 int was_alloced;
2637 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002638 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640 oldp = ml_get(lnum);
2641 oldlen = (int)STRLEN(oldp);
2642
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002643 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (col >= oldlen)
2645 return FAIL;
2646
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002647 /* If "count" is zero there is nothing to do. */
2648 if (count == 0)
2649 return OK;
2650
2651 /* If "count" is negative the caller must be doing something wrong. */
2652 if (count < 1)
2653 {
2654 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2655 return FAIL;
2656 }
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#ifdef FEAT_MBYTE
2659 /* If 'delcombine' is set and deleting (less than) one character, only
2660 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002661 if (p_deco && use_delcombine && enc_utf8
2662 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002664 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 int n;
2666
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002667 (void)utfc_ptr2char(oldp + col, cc);
2668 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 /* Find the last composing char, there can be several. */
2671 n = col;
2672 do
2673 {
2674 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002675 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 n += count;
2677 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2678 fixpos = 0;
2679 }
2680 }
2681#endif
2682
2683 /*
2684 * When count is too big, reduce it.
2685 */
2686 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2687 if (movelen <= 1)
2688 {
2689 /*
2690 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002691 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2692 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002694 if (col > 0 && fixpos && restart_edit == 0
2695#ifdef FEAT_VIRTUALEDIT
2696 && (ve_flags & VE_ONEMORE) == 0
2697#endif
2698 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 --curwin->w_cursor.col;
2701#ifdef FEAT_VIRTUALEDIT
2702 curwin->w_cursor.coladd = 0;
2703#endif
2704#ifdef FEAT_MBYTE
2705 if (has_mbyte)
2706 curwin->w_cursor.col -=
2707 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2708#endif
2709 }
2710 count = oldlen - col;
2711 movelen = 1;
2712 }
2713
2714 /*
2715 * If the old line has been allocated the deletion can be done in the
2716 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002717 * Can't do this when using Netbeans, because we would need to invoke
2718 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002719 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002722 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002723 was_alloced = FALSE;
2724 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002726 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (was_alloced)
2728 newp = oldp; /* use same allocated memory */
2729 else
2730 { /* need to allocate a new line */
2731 newp = alloc((unsigned)(oldlen + 1 - count));
2732 if (newp == NULL)
2733 return FAIL;
2734 mch_memmove(newp, oldp, (size_t)col);
2735 }
2736 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2737 if (!was_alloced)
2738 ml_replace(lnum, newp, FALSE);
2739
2740 /* mark the buffer as changed and prepare for displaying */
2741 changed_bytes(lnum, curwin->w_cursor.col);
2742
2743 return OK;
2744}
2745
2746/*
2747 * Delete from cursor to end of line.
2748 * Caller must have prepared for undo.
2749 *
2750 * return FAIL for failure, OK otherwise
2751 */
2752 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002753truncate_line(
2754 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 char_u *newp;
2757 linenr_T lnum = curwin->w_cursor.lnum;
2758 colnr_T col = curwin->w_cursor.col;
2759
2760 if (col == 0)
2761 newp = vim_strsave((char_u *)"");
2762 else
2763 newp = vim_strnsave(ml_get(lnum), col);
2764
2765 if (newp == NULL)
2766 return FAIL;
2767
2768 ml_replace(lnum, newp, FALSE);
2769
2770 /* mark the buffer as changed and prepare for displaying */
2771 changed_bytes(lnum, curwin->w_cursor.col);
2772
2773 /*
2774 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2775 */
2776 if (fixpos && curwin->w_cursor.col > 0)
2777 --curwin->w_cursor.col;
2778
2779 return OK;
2780}
2781
2782/*
2783 * Delete "nlines" lines at the cursor.
2784 * Saves the lines for undo first if "undo" is TRUE.
2785 */
2786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002787del_lines(
2788 long nlines, /* number of lines to delete */
2789 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002792 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 if (nlines <= 0)
2795 return;
2796
2797 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002798 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return;
2800
2801 for (n = 0; n < nlines; )
2802 {
2803 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2804 break;
2805
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002806 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 ++n;
2808
2809 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002810 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 break;
2812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002814 /* Correct the cursor position before calling deleted_lines_mark(), it may
2815 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 curwin->w_cursor.col = 0;
2817 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002818
2819 /* adjust marks, mark the buffer as changed and prepare for displaying */
2820 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821}
2822
2823 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002824gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002826 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002828 /* When searching columns is sometimes put at the end of a line. */
2829 if (pos->col == MAXCOL)
2830 return NUL;
2831 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832#ifdef FEAT_MBYTE
2833 if (has_mbyte)
2834 return (*mb_ptr2char)(ptr);
2835#endif
2836 return (int)*ptr;
2837}
2838
2839 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002840gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
2842#ifdef FEAT_MBYTE
2843 if (has_mbyte)
2844 return (*mb_ptr2char)(ml_get_cursor());
2845#endif
2846 return (int)*ml_get_cursor();
2847}
2848
2849/*
2850 * Write a character at the current cursor position.
2851 * It is directly written into the block.
2852 */
2853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002854pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2857 + curwin->w_cursor.col) = c;
2858}
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860/*
2861 * When extra == 0: Return TRUE if the cursor is before or on the first
2862 * non-blank in the line.
2863 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2864 * the line.
2865 */
2866 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002867inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
2869 char_u *ptr;
2870 colnr_T col;
2871
Bram Moolenaar1c465442017-03-12 20:10:05 +01002872 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 ++ptr;
2874 if (col >= curwin->w_cursor.col + extra)
2875 return TRUE;
2876 else
2877 return FALSE;
2878}
2879
2880/*
2881 * Skip to next part of an option argument: Skip space and comma.
2882 */
2883 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002884skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 if (*p == ',')
2887 ++p;
2888 while (*p == ' ')
2889 ++p;
2890 return p;
2891}
2892
2893/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002894 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 *
2896 * Most often called through changed_bytes() and changed_lines(), which also
2897 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002898 *
2899 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 */
2901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002902changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002905 if (p_imst == IM_ON_THE_SPOT)
2906 {
2907 /* The text of the preediting area is inserted, but this doesn't
2908 * mean a change of the buffer yet. That is delayed until the
2909 * text is committed. (this means preedit becomes empty) */
2910 if (im_is_preediting() && !xim_changed_while_preediting)
2911 return;
2912 xim_changed_while_preediting = FALSE;
2913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#endif
2915
2916 if (!curbuf->b_changed)
2917 {
2918 int save_msg_scroll = msg_scroll;
2919
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002920 /* Give a warning about changing a read-only file. This may also
2921 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 /* Create a swap file if that is wanted.
2925 * Don't do this for "nofile" and "nowrite" buffer types. */
2926 if (curbuf->b_may_swap
2927#ifdef FEAT_QUICKFIX
2928 && !bt_dontwrite(curbuf)
2929#endif
2930 )
2931 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002932 int save_need_wait_return = need_wait_return;
2933
2934 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 ml_open_file(curbuf);
2936
2937 /* The ml_open_file() can cause an ATTENTION message.
2938 * Wait two seconds, to make sure the user reads this unexpected
2939 * message. Since we could be anywhere, call wait_return() now,
2940 * and don't let the emsg() set msg_scroll. */
2941 if (need_wait_return && emsg_silent == 0)
2942 {
2943 out_flush();
2944 ui_delay(2000L, TRUE);
2945 wait_return(TRUE);
2946 msg_scroll = save_msg_scroll;
2947 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002948 else
2949 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002951 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002953 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954}
2955
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002956/*
2957 * Internal part of changed(), no user interaction.
2958 */
2959 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002960changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002961{
2962 curbuf->b_changed = TRUE;
2963 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002964 check_status(curbuf);
2965 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002966#ifdef FEAT_TITLE
2967 need_maketitle = TRUE; /* set window title later */
2968#endif
2969}
2970
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002971static void changedOneline(buf_T *buf, linenr_T lnum);
2972static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2973static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975/*
2976 * Changed bytes within a single line for the current buffer.
2977 * - marks the windows on this buffer to be redisplayed
2978 * - marks the buffer changed by calling changed()
2979 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002980 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
2982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002983changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002985 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002987
2988#ifdef FEAT_DIFF
2989 /* Diff highlighting in other diff windows may need to be updated too. */
2990 if (curwin->w_p_diff)
2991 {
2992 win_T *wp;
2993 linenr_T wlnum;
2994
Bram Moolenaar29323592016-07-24 22:04:11 +02002995 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002996 if (wp->w_p_diff && wp != curwin)
2997 {
2998 redraw_win_later(wp, VALID);
2999 wlnum = diff_lnum_win(lnum, wp);
3000 if (wlnum > 0)
3001 changedOneline(wp->w_buffer, wlnum);
3002 }
3003 }
3004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005}
3006
3007 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003008changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003010 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 {
3012 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003013 if (lnum < buf->b_mod_top)
3014 buf->b_mod_top = lnum;
3015 else if (lnum >= buf->b_mod_bot)
3016 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018 else
3019 {
3020 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003021 buf->b_mod_set = TRUE;
3022 buf->b_mod_top = lnum;
3023 buf->b_mod_bot = lnum + 1;
3024 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026}
3027
3028/*
3029 * Appended "count" lines below line "lnum" in the current buffer.
3030 * Must be called AFTER the change and after mark_adjust().
3031 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3032 */
3033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003034appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 changed_lines(lnum + 1, 0, lnum + 1, count);
3037}
3038
3039/*
3040 * Like appended_lines(), but adjust marks first.
3041 */
3042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003043appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003045 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003046 * be marks there. But it's still needed in diff mode. */
3047 if (lnum + count < curbuf->b_ml.ml_line_count
3048#ifdef FEAT_DIFF
3049 || curwin->w_p_diff
3050#endif
3051 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003052 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 changed_lines(lnum + 1, 0, lnum + 1, count);
3054}
3055
3056/*
3057 * Deleted "count" lines at line "lnum" in the current buffer.
3058 * Must be called AFTER the change and after mark_adjust().
3059 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3060 */
3061 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003062deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 changed_lines(lnum, 0, lnum + count, -count);
3065}
3066
3067/*
3068 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003069 * Make sure the cursor is on a valid line before calling, a GUI callback may
3070 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 */
3072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003073deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
3075 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3076 changed_lines(lnum, 0, lnum + count, -count);
3077}
3078
3079/*
3080 * Changed lines for the current buffer.
3081 * Must be called AFTER the change and after mark_adjust().
3082 * - mark the buffer changed by calling changed()
3083 * - mark the windows on this buffer to be redisplayed
3084 * - invalidate cached values
3085 * "lnum" is the first line that needs displaying, "lnume" the first line
3086 * below the changed lines (BEFORE the change).
3087 * When only inserting lines, "lnum" and "lnume" are equal.
3088 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003089 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 */
3091 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003092changed_lines(
3093 linenr_T lnum, /* first line with change */
3094 colnr_T col, /* column in first line with change */
3095 linenr_T lnume, /* line below last changed line */
3096 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003098 changed_lines_buf(curbuf, lnum, lnume, xtra);
3099
3100#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003101 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003102 {
3103 /* When the number of lines doesn't change then mark_adjust() isn't
3104 * called and other diff buffers still need to be marked for
3105 * displaying. */
3106 win_T *wp;
3107 linenr_T wlnum;
3108
Bram Moolenaar29323592016-07-24 22:04:11 +02003109 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003110 if (wp->w_p_diff && wp != curwin)
3111 {
3112 redraw_win_later(wp, VALID);
3113 wlnum = diff_lnum_win(lnum, wp);
3114 if (wlnum > 0)
3115 changed_lines_buf(wp->w_buffer, wlnum,
3116 lnume - lnum + wlnum, 0L);
3117 }
3118 }
3119#endif
3120
3121 changed_common(lnum, col, lnume, xtra);
3122}
3123
3124 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003125changed_lines_buf(
3126 buf_T *buf,
3127 linenr_T lnum, /* first line with change */
3128 linenr_T lnume, /* line below last changed line */
3129 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003130{
3131 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003134 if (lnum < buf->b_mod_top)
3135 buf->b_mod_top = lnum;
3136 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
3138 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003139 buf->b_mod_bot += xtra;
3140 if (buf->b_mod_bot < lnum)
3141 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003143 if (lnume + xtra > buf->b_mod_bot)
3144 buf->b_mod_bot = lnume + xtra;
3145 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
3147 else
3148 {
3149 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003150 buf->b_mod_set = TRUE;
3151 buf->b_mod_top = lnum;
3152 buf->b_mod_bot = lnume + xtra;
3153 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155}
3156
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003157/*
3158 * Common code for when a change is was made.
3159 * See changed_lines() for the arguments.
3160 * Careful: may trigger autocommands that reload the buffer.
3161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003163changed_common(
3164 linenr_T lnum,
3165 colnr_T col,
3166 linenr_T lnume,
3167 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003170 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 int i;
3172#ifdef FEAT_JUMPLIST
3173 int cols;
3174 pos_T *p;
3175 int add;
3176#endif
3177
3178 /* mark the buffer as modified */
3179 changed();
3180
Bram Moolenaare3521d92018-09-16 14:10:31 +02003181#ifdef FEAT_DIFF
3182 if (curwin->w_p_diff && diff_internal())
3183 curtab->tp_diff_update = TRUE;
3184#endif
3185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 /* set the '. mark */
3187 if (!cmdmod.keepjumps)
3188 {
3189 curbuf->b_last_change.lnum = lnum;
3190 curbuf->b_last_change.col = col;
3191
3192#ifdef FEAT_JUMPLIST
3193 /* Create a new entry if a new undo-able change was started or we
3194 * don't have an entry yet. */
3195 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3196 {
3197 if (curbuf->b_changelistlen == 0)
3198 add = TRUE;
3199 else
3200 {
3201 /* Don't create a new entry when the line number is the same
3202 * as the last one and the column is not too far away. Avoids
3203 * creating many entries for typing "xxxxx". */
3204 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3205 if (p->lnum != lnum)
3206 add = TRUE;
3207 else
3208 {
3209 cols = comp_textwidth(FALSE);
3210 if (cols == 0)
3211 cols = 79;
3212 add = (p->col + cols < col || col + cols < p->col);
3213 }
3214 }
3215 if (add)
3216 {
3217 /* This is the first of a new sequence of undo-able changes
3218 * and it's at some distance of the last change. Use a new
3219 * position in the changelist. */
3220 curbuf->b_new_change = FALSE;
3221
3222 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3223 {
3224 /* changelist is full: remove oldest entry */
3225 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3226 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3227 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003228 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
3230 /* Correct position in changelist for other windows on
3231 * this buffer. */
3232 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3233 --wp->w_changelistidx;
3234 }
3235 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003236 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 /* For other windows, if the position in the changelist is
3239 * at the end it stays at the end. */
3240 if (wp->w_buffer == curbuf
3241 && wp->w_changelistidx == curbuf->b_changelistlen)
3242 ++wp->w_changelistidx;
3243 }
3244 ++curbuf->b_changelistlen;
3245 }
3246 }
3247 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3248 curbuf->b_last_change;
3249 /* The current window is always after the last change, so that "g,"
3250 * takes you back to it. */
3251 curwin->w_changelistidx = curbuf->b_changelistlen;
3252#endif
3253 }
3254
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003255 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
3257 if (wp->w_buffer == curbuf)
3258 {
3259 /* Mark this window to be redrawn later. */
3260 if (wp->w_redr_type < VALID)
3261 wp->w_redr_type = VALID;
3262
3263 /* Check if a change in the buffer has invalidated the cached
3264 * values for the cursor. */
3265#ifdef FEAT_FOLDING
3266 /*
3267 * Update the folds for this window. Can't postpone this, because
3268 * a following operator might work on the whole fold: ">>dd".
3269 */
3270 foldUpdate(wp, lnum, lnume + xtra - 1);
3271
3272 /* The change may cause lines above or below the change to become
3273 * included in a fold. Set lnum/lnume to the first/last line that
3274 * might be displayed differently.
3275 * Set w_cline_folded here as an efficient way to update it when
3276 * inserting lines just above a closed fold. */
3277 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3278 if (wp->w_cursor.lnum == lnum)
3279 wp->w_cline_folded = i;
3280 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3281 if (wp->w_cursor.lnum == lnume)
3282 wp->w_cline_folded = i;
3283
3284 /* If the changed line is in a range of previously folded lines,
3285 * compare with the first line in that range. */
3286 if (wp->w_cursor.lnum <= lnum)
3287 {
3288 i = find_wl_entry(wp, lnum);
3289 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3290 changed_line_abv_curs_win(wp);
3291 }
3292#endif
3293
3294 if (wp->w_cursor.lnum > lnum)
3295 changed_line_abv_curs_win(wp);
3296 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3297 changed_cline_bef_curs_win(wp);
3298 if (wp->w_botline >= lnum)
3299 {
3300 /* Assume that botline doesn't change (inserted lines make
3301 * other lines scroll down below botline). */
3302 approximate_botline_win(wp);
3303 }
3304
3305 /* Check if any w_lines[] entries have become invalid.
3306 * For entries below the change: Correct the lnums for
3307 * inserted/deleted lines. Makes it possible to stop displaying
3308 * after the change. */
3309 for (i = 0; i < wp->w_lines_valid; ++i)
3310 if (wp->w_lines[i].wl_valid)
3311 {
3312 if (wp->w_lines[i].wl_lnum >= lnum)
3313 {
3314 if (wp->w_lines[i].wl_lnum < lnume)
3315 {
3316 /* line included in change */
3317 wp->w_lines[i].wl_valid = FALSE;
3318 }
3319 else if (xtra != 0)
3320 {
3321 /* line below change */
3322 wp->w_lines[i].wl_lnum += xtra;
3323#ifdef FEAT_FOLDING
3324 wp->w_lines[i].wl_lastlnum += xtra;
3325#endif
3326 }
3327 }
3328#ifdef FEAT_FOLDING
3329 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3330 {
3331 /* change somewhere inside this range of folded lines,
3332 * may need to be redrawn */
3333 wp->w_lines[i].wl_valid = FALSE;
3334 }
3335#endif
3336 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003337
3338#ifdef FEAT_FOLDING
3339 /* Take care of side effects for setting w_topline when folds have
3340 * changed. Esp. when the buffer was changed in another window. */
3341 if (hasAnyFolding(wp))
3342 set_topline(wp, wp->w_topline);
3343#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003344 /* relative numbering may require updating more */
3345 if (wp->w_p_rnu)
3346 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348 }
3349
3350 /* Call update_screen() later, which checks out what needs to be redrawn,
3351 * since it notices b_mod_set and then uses b_mod_*. */
3352 if (must_redraw < VALID)
3353 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003354
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003355 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003356 if (lnum <= curwin->w_cursor.lnum
3357 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003358 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359}
3360
3361/*
3362 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3363 */
3364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003365unchanged(
3366 buf_T *buf,
3367 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003369 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
3371 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003372 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (ff)
3374 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003376 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#ifdef FEAT_TITLE
3378 need_maketitle = TRUE; /* set window title later */
3379#endif
3380 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003381 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382#ifdef FEAT_NETBEANS_INTG
3383 netbeans_unmodified(buf);
3384#endif
3385}
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387/*
3388 * check_status: called when the status bars for the buffer 'buf'
3389 * need to be updated
3390 */
3391 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003392check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 win_T *wp;
3395
Bram Moolenaar29323592016-07-24 22:04:11 +02003396 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (wp->w_buffer == buf && wp->w_status_height)
3398 {
3399 wp->w_redr_status = TRUE;
3400 if (must_redraw < VALID)
3401 must_redraw = VALID;
3402 }
3403}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
3405/*
3406 * If the file is readonly, give a warning message with the first change.
3407 * Don't do this for autocommands.
3408 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003409 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003411 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 */
3413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003414change_warning(
3415 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 mode and 'showmode' is on */
3417{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003418 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (curbuf->b_did_warn == FALSE
3421 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 && curbuf->b_p_ro)
3424 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003425 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003427 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (!curbuf->b_p_ro)
3429 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 /*
3431 * Do what msg() does, but with a column offset if the warning should
3432 * be after the mode message.
3433 */
3434 msg_start();
3435 if (msg_row == Rows - 1)
3436 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003437 msg_source(HL_ATTR(HLF_W));
3438 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003439#ifdef FEAT_EVAL
3440 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 msg_clr_eos();
3443 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003444 if (msg_silent == 0 && !silent_mode
3445#ifdef FEAT_EVAL
3446 && time_for_testing != 1
3447#endif
3448 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 out_flush();
3451 ui_delay(1000L, TRUE); /* give the user time to think about it */
3452 }
3453 curbuf->b_did_warn = TRUE;
3454 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3455 if (msg_row < Rows - 1)
3456 showmode();
3457 }
3458}
3459
3460/*
3461 * Ask for a reply from the user, a 'y' or a 'n'.
3462 * No other characters are accepted, the message is repeated until a valid
3463 * reply is entered or CTRL-C is hit.
3464 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3465 * from any buffers but directly from the user.
3466 *
3467 * return the 'y' or 'n'
3468 */
3469 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003470ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
3472 int r = ' ';
3473 int save_State = State;
3474
3475 if (exiting) /* put terminal in raw mode for this question */
3476 settmode(TMODE_RAW);
3477 ++no_wait_return;
3478#ifdef USE_ON_FLY_SCROLL
3479 dont_scroll = TRUE; /* disallow scrolling here */
3480#endif
3481 State = CONFIRM; /* mouse behaves like with :confirm */
3482#ifdef FEAT_MOUSE
3483 setmouse(); /* disables mouse for xterm */
3484#endif
3485 ++no_mapping;
3486 ++allow_keys; /* no mapping here, but recognize keys */
3487
3488 while (r != 'y' && r != 'n')
3489 {
3490 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003491 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (direct)
3493 r = get_keystroke();
3494 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003495 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (r == Ctrl_C || r == ESC)
3497 r = 'n';
3498 msg_putchar(r); /* show what you typed */
3499 out_flush();
3500 }
3501 --no_wait_return;
3502 State = save_State;
3503#ifdef FEAT_MOUSE
3504 setmouse();
3505#endif
3506 --no_mapping;
3507 --allow_keys;
3508
3509 return r;
3510}
3511
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003512#if defined(FEAT_MOUSE) || defined(PROTO)
3513/*
3514 * Return TRUE if "c" is a mouse key.
3515 */
3516 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003517is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003518{
3519 return c == K_LEFTMOUSE
3520 || c == K_LEFTMOUSE_NM
3521 || c == K_LEFTDRAG
3522 || c == K_LEFTRELEASE
3523 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003524 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003525 || c == K_MIDDLEMOUSE
3526 || c == K_MIDDLEDRAG
3527 || c == K_MIDDLERELEASE
3528 || c == K_RIGHTMOUSE
3529 || c == K_RIGHTDRAG
3530 || c == K_RIGHTRELEASE
3531 || c == K_MOUSEDOWN
3532 || c == K_MOUSEUP
3533 || c == K_MOUSELEFT
3534 || c == K_MOUSERIGHT
3535 || c == K_X1MOUSE
3536 || c == K_X1DRAG
3537 || c == K_X1RELEASE
3538 || c == K_X2MOUSE
3539 || c == K_X2DRAG
3540 || c == K_X2RELEASE;
3541}
3542#endif
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544/*
3545 * Get a key stroke directly from the user.
3546 * Ignores mouse clicks and scrollbar events, except a click for the left
3547 * button (used at the more prompt).
3548 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3549 * Disadvantage: typeahead is ignored.
3550 * Translates the interrupt character for unix to ESC.
3551 */
3552 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003553get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003555 char_u *buf = NULL;
3556 int buflen = 150;
3557 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 int len = 0;
3559 int n;
3560 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003561 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 mapped_ctrl_c = FALSE; /* mappings are not used here */
3564 for (;;)
3565 {
3566 cursor_on();
3567 out_flush();
3568
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003569 /* Leave some room for check_termcode() to insert a key code into (max
3570 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3571 * bytes. */
3572 maxlen = (buflen - 6 - len) / 3;
3573 if (buf == NULL)
3574 buf = alloc(buflen);
3575 else if (maxlen < 10)
3576 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003577 char_u *t_buf = buf;
3578
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003579 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003580 * escape sequence. */
3581 buflen += 100;
3582 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003583 if (buf == NULL)
3584 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003585 maxlen = (buflen - 6 - len) / 3;
3586 }
3587 if (buf == NULL)
3588 {
3589 do_outofmem_msg((long_u)buflen);
3590 return ESC; /* panic! */
3591 }
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003594 * terminal code to complete. */
3595 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (n > 0)
3597 {
3598 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003599 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003601 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003603 else if (len > 0)
3604 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
Bram Moolenaar4395a712006-09-05 18:57:57 +00003606 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003607 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003608 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003610
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003611 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003612 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003613 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003614 {
3615 /* Redrawing was postponed, do it now. */
3616 update_screen(0);
3617 setcursor(); /* put cursor back where it belongs */
3618 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003619 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003620 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003621 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003623 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 continue;
3625
3626 /* Handle modifier and/or special key code. */
3627 n = buf[0];
3628 if (n == K_SPECIAL)
3629 {
3630 n = TO_SPECIAL(buf[1], buf[2]);
3631 if (buf[1] == KS_MODIFIER
3632 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003633#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003634 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003635#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003636#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 || n == K_VER_SCROLLBAR
3638 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#endif
3640 )
3641 {
3642 if (buf[1] == KS_MODIFIER)
3643 mod_mask = buf[2];
3644 len -= 3;
3645 if (len > 0)
3646 mch_memmove(buf, buf + 3, (size_t)len);
3647 continue;
3648 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003649 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651#ifdef FEAT_MBYTE
3652 if (has_mbyte)
3653 {
3654 if (MB_BYTE2LEN(n) > len)
3655 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003656 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 n = (*mb_ptr2char)(buf);
3658 }
3659#endif
3660#ifdef UNIX
3661 if (n == intr_char)
3662 n = ESC;
3663#endif
3664 break;
3665 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003666 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 mapped_ctrl_c = save_mapped_ctrl_c;
3669 return n;
3670}
3671
3672/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003673 * Get a number from the user.
3674 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 */
3676 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003677get_number(
3678 int colon, /* allow colon to abort */
3679 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 int n = 0;
3682 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003683 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003685 if (mouse_used != NULL)
3686 *mouse_used = FALSE;
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 /* When not printing messages, the user won't know what to type, return a
3689 * zero (as if CR was hit). */
3690 if (msg_silent != 0)
3691 return 0;
3692
3693#ifdef USE_ON_FLY_SCROLL
3694 dont_scroll = TRUE; /* disallow scrolling here */
3695#endif
3696 ++no_mapping;
3697 ++allow_keys; /* no mapping here, but recognize keys */
3698 for (;;)
3699 {
3700 windgoto(msg_row, msg_col);
3701 c = safe_vgetc();
3702 if (VIM_ISDIGIT(c))
3703 {
3704 n = n * 10 + c - '0';
3705 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003706 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3709 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003710 if (typed > 0)
3711 {
3712 MSG_PUTS("\b \b");
3713 --typed;
3714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003717#ifdef FEAT_MOUSE
3718 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3719 {
3720 *mouse_used = TRUE;
3721 n = mouse_row + 1;
3722 break;
3723 }
3724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 else if (n == 0 && c == ':' && colon)
3726 {
3727 stuffcharReadbuff(':');
3728 if (!exmode_active)
3729 cmdline_row = msg_row;
3730 skip_redraw = TRUE; /* skip redraw once */
3731 do_redraw = FALSE;
3732 break;
3733 }
3734 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3735 break;
3736 }
3737 --no_mapping;
3738 --allow_keys;
3739 return n;
3740}
3741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003742/*
3743 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003744 * When "mouse_used" is not NULL allow using the mouse and in that case return
3745 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003746 */
3747 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003748prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003749{
3750 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003751 int save_cmdline_row;
3752 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753
3754 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003755 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003756 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003757 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003758 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003759
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003760 // Set the state such that text can be selected/copied/pasted and we still
3761 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3762 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003763 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003764 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003765 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003766 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003767#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003768 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003769 setmouse();
3770#endif
3771
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003772 i = get_number(TRUE, mouse_used);
3773 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003775 /* don't call wait_return() now */
3776 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 cmdline_row = msg_row - 1;
3778 need_wait_return = FALSE;
3779 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003780 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003782 else
3783 cmdline_row = save_cmdline_row;
3784 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003785#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003786 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003787 setmouse();
3788#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003790 return i;
3791}
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003794msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 long pn;
3797
3798 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3800 return;
3801
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003802 /* We don't want to overwrite another important message, but do overwrite
3803 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3804 * then "put" reports the last action. */
3805 if (keep_msg != NULL && !keep_msg_more)
3806 return;
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (n > 0)
3809 pn = n;
3810 else
3811 pn = -n;
3812
3813 if (pn > p_report)
3814 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003815 if (n > 0)
3816 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3817 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003819 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3820 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003822 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (msg(msg_buf))
3824 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003825 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003826 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829}
3830
3831/*
3832 * flush map and typeahead buffers and give a warning for an error
3833 */
3834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003835beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
3837 if (emsg_silent == 0)
3838 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003839 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003840 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842}
3843
3844/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003845 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 */
3847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003848vim_beep(
3849 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003851#ifdef FEAT_EVAL
3852 called_vim_beep = TRUE;
3853#endif
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (emsg_silent == 0)
3856 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003857 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3858 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003859#ifdef ELAPSED_FUNC
3860 static int did_init = FALSE;
3861 static ELAPSED_TYPE start_tv;
3862
3863 /* Only beep once per half a second, otherwise a sequence of beeps
3864 * would freeze Vim. */
3865 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3866 {
3867 did_init = TRUE;
3868 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003870 if (p_vb
3871#ifdef FEAT_GUI
3872 /* While the GUI is starting up the termcap is set for
3873 * the GUI but the output still goes to a terminal. */
3874 && !(gui.in_use && gui.starting)
3875#endif
3876 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003877 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003878 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003879#ifdef FEAT_VTP
3880 /* No restore color information, refresh the screen. */
3881 if (has_vtp_working() != 0
3882# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003883 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003884# endif
3885 )
3886 {
3887 redraw_later(CLEAR);
3888 update_screen(0);
3889 redrawcmd();
3890 }
3891#endif
3892 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003893 else
3894 out_char(BELL);
3895#ifdef ELAPSED_FUNC
3896 }
3897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003899
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003900 /* When 'debug' contains "beep" produce a message. If we are sourcing
3901 * a script or executing a function give the user a hint where the beep
3902 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003903 if (vim_strchr(p_debug, 'e') != NULL)
3904 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003905 msg_source(HL_ATTR(HLF_W));
3906 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909}
3910
3911/*
3912 * To get the "real" home directory:
3913 * - get value of $HOME
3914 * For Unix:
3915 * - go to that directory
3916 * - do mch_dirname() to get the real name of that directory.
3917 * This also works with mounts and links.
3918 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003919 * For Windows:
3920 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 */
3922static char_u *homedir = NULL;
3923
3924 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003925init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927 char_u *var;
3928
Bram Moolenaar05159a02005-02-26 23:04:13 +00003929 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003930 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#ifdef VMS
3933 var = mch_getenv((char_u *)"SYS$LOGIN");
3934#else
3935 var = mch_getenv((char_u *)"HOME");
3936#endif
3937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#ifdef WIN3264
3939 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003940 * Typically, $HOME is not defined on Windows, unless the user has
3941 * specifically defined it for Vim's sake. However, on Windows NT
3942 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3943 * each user. Try constructing $HOME from these.
3944 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003945 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003946 {
3947 char_u *homedrive, *homepath;
3948
3949 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3950 homepath = mch_getenv((char_u *)"HOMEPATH");
3951 if (homepath == NULL || *homepath == NUL)
3952 homepath = (char_u *)"\\";
3953 if (homedrive != NULL
3954 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3955 {
3956 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3957 if (NameBuff[0] != NUL)
3958 var = NameBuff;
3959 }
3960 }
3961
3962 if (var == NULL)
3963 var = mch_getenv((char_u *)"USERPROFILE");
3964
3965 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 * Weird but true: $HOME may contain an indirect reference to another
3967 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3968 * when $HOME is being set.
3969 */
3970 if (var != NULL && *var == '%')
3971 {
3972 char_u *p;
3973 char_u *exp;
3974
3975 p = vim_strchr(var + 1, '%');
3976 if (p != NULL)
3977 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003978 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 exp = mch_getenv(NameBuff);
3980 if (exp != NULL && *exp != NUL
3981 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3982 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003983 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986 }
3987 }
3988
Bram Moolenaar48340b62017-08-29 22:08:53 +02003989 if (var != NULL && *var == NUL) /* empty is same as not set */
3990 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar48340b62017-08-29 22:08:53 +02003992# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003993 if (enc_utf8 && var != NULL)
3994 {
3995 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003996 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003997
3998 /* Convert from active codepage to UTF-8. Other conversions are
3999 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004000 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004001 if (pp != NULL)
4002 {
4003 homedir = pp;
4004 return;
4005 }
4006 }
4007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 /*
4010 * Default home dir is C:/
4011 * Best assumption we can make in such a situation.
4012 */
4013 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004014 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (var != NULL)
4018 {
4019#ifdef UNIX
4020 /*
4021 * Change to the directory and get the actual path. This resolves
4022 * links. Don't do it when we can't return.
4023 */
4024 if (mch_dirname(NameBuff, MAXPATHL) == OK
4025 && mch_chdir((char *)NameBuff) == 0)
4026 {
4027 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4028 var = IObuff;
4029 if (mch_chdir((char *)NameBuff) != 0)
4030 EMSG(_(e_prev_dir));
4031 }
4032#endif
4033 homedir = vim_strsave(var);
4034 }
4035}
4036
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004037#if defined(EXITFREE) || defined(PROTO)
4038 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004039free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004040{
4041 vim_free(homedir);
4042}
Bram Moolenaar24305862012-08-15 14:05:05 +02004043
4044# ifdef FEAT_CMDL_COMPL
4045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004046free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004047{
4048 ga_clear_strings(&ga_users);
4049}
4050# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004051#endif
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004054 * Call expand_env() and store the result in an allocated string.
4055 * This is not very memory efficient, this expects the result to be freed
4056 * again soon.
4057 */
4058 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004059expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004060{
4061 return expand_env_save_opt(src, FALSE);
4062}
4063
4064/*
4065 * Idem, but when "one" is TRUE handle the string as one file name, only
4066 * expand "~" at the start.
4067 */
4068 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004069expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004070{
4071 char_u *p;
4072
4073 p = alloc(MAXPATHL);
4074 if (p != NULL)
4075 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4076 return p;
4077}
4078
4079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 * Expand environment variable with path name.
4081 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004082 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * If anything fails no expansion is done and dst equals src.
4084 */
4085 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004086expand_env(
4087 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4088 char_u *dst, /* where to put the result */
4089 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004091 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092}
4093
4094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004095expand_env_esc(
4096 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4097 char_u *dst, /* where to put the result */
4098 int dstlen, /* maximum length of the result */
4099 int esc, /* escape spaces in expanded variables */
4100 int one, /* "srcp" is one file name */
4101 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004103 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 char_u *tail;
4105 int c;
4106 char_u *var;
4107 int copy_char;
4108 int mustfree; /* var was allocated, need to free it later */
4109 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004110 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004112 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004113 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004114
4115 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 --dstlen; /* leave one char space for "\," */
4117 while (*src && dstlen > 0)
4118 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004119#ifdef FEAT_EVAL
4120 /* Skip over `=expr`. */
4121 if (src[0] == '`' && src[1] == '=')
4122 {
4123 size_t len;
4124
4125 var = src;
4126 src += 2;
4127 (void)skip_expr(&src);
4128 if (*src == '`')
4129 ++src;
4130 len = src - var;
4131 if (len > (size_t)dstlen)
4132 len = dstlen;
4133 vim_strncpy(dst, var, len);
4134 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004135 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004136 continue;
4137 }
4138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004140 if ((*src == '$'
4141#ifdef VMS
4142 && at_start
4143#endif
4144 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004145#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 || *src == '%'
4147#endif
4148 || (*src == '~' && at_start))
4149 {
4150 mustfree = FALSE;
4151
4152 /*
4153 * The variable name is copied into dst temporarily, because it may
4154 * be a string in read-only memory and a NUL needs to be appended.
4155 */
4156 if (*src != '~') /* environment var */
4157 {
4158 tail = src + 1;
4159 var = dst;
4160 c = dstlen - 1;
4161
4162#ifdef UNIX
4163 /* Unix has ${var-name} type environment vars */
4164 if (*tail == '{' && !vim_isIDc('{'))
4165 {
4166 tail++; /* ignore '{' */
4167 while (c-- > 0 && *tail && *tail != '}')
4168 *var++ = *tail++;
4169 }
4170 else
4171#endif
4172 {
4173 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004174#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 || (*src == '%' && *tail != '%')
4176#endif
4177 ))
4178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 }
4182
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004183#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184# ifdef UNIX
4185 if (src[1] == '{' && *tail != '}')
4186# else
4187 if (*src == '%' && *tail != '%')
4188# endif
4189 var = NULL;
4190 else
4191 {
4192# ifdef UNIX
4193 if (src[1] == '{')
4194# else
4195 if (*src == '%')
4196#endif
4197 ++tail;
4198#endif
4199 *var = NUL;
4200 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004201#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203#endif
4204 }
4205 /* home directory */
4206 else if ( src[1] == NUL
4207 || vim_ispathsep(src[1])
4208 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4209 {
4210 var = homedir;
4211 tail = src + 1;
4212 }
4213 else /* user directory */
4214 {
4215#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4216 /*
4217 * Copy ~user to dst[], so we can put a NUL after it.
4218 */
4219 tail = src;
4220 var = dst;
4221 c = dstlen - 1;
4222 while ( c-- > 0
4223 && *tail
4224 && vim_isfilec(*tail)
4225 && !vim_ispathsep(*tail))
4226 *var++ = *tail++;
4227 *var = NUL;
4228# ifdef UNIX
4229 /*
4230 * If the system supports getpwnam(), use it.
4231 * Otherwise, or if getpwnam() fails, the shell is used to
4232 * expand ~user. This is slower and may fail if the shell
4233 * does not support ~user (old versions of /bin/sh).
4234 */
4235# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4236 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004237 /* Note: memory allocated by getpwnam() is never freed.
4238 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004239 struct passwd *pw = (*dst == NUL)
4240 ? NULL : getpwnam((char *)dst + 1);
4241
4242 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244 if (var == NULL)
4245# endif
4246 {
4247 expand_T xpc;
4248
4249 ExpandInit(&xpc);
4250 xpc.xp_context = EXPAND_FILES;
4251 var = ExpandOne(&xpc, dst, NULL,
4252 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 mustfree = TRUE;
4254 }
4255
4256# else /* !UNIX, thus VMS */
4257 /*
4258 * USER_HOME is a comma-separated list of
4259 * directories to search for the user account in.
4260 */
4261 {
4262 char_u test[MAXPATHL], paths[MAXPATHL];
4263 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004264 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 STRCPY(paths, USER_HOME);
4267 next_path = paths;
4268 while (*next_path)
4269 {
4270 for (path = next_path; *next_path && *next_path != ',';
4271 next_path++);
4272 if (*next_path)
4273 *next_path++ = NUL;
4274 STRCPY(test, path);
4275 STRCAT(test, "/");
4276 STRCAT(test, dst + 1);
4277 if (mch_stat(test, &st) == 0)
4278 {
4279 var = alloc(STRLEN(test) + 1);
4280 STRCPY(var, test);
4281 mustfree = TRUE;
4282 break;
4283 }
4284 }
4285 }
4286# endif /* UNIX */
4287#else
4288 /* cannot expand user's home directory, so don't try */
4289 var = NULL;
4290 tail = (char_u *)""; /* for gcc */
4291#endif /* UNIX || VMS */
4292 }
4293
4294#ifdef BACKSLASH_IN_FILENAME
4295 /* If 'shellslash' is set change backslashes to forward slashes.
4296 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4297 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4298 {
4299 char_u *p = vim_strsave(var);
4300
4301 if (p != NULL)
4302 {
4303 if (mustfree)
4304 vim_free(var);
4305 var = p;
4306 mustfree = TRUE;
4307 forward_slash(var);
4308 }
4309 }
4310#endif
4311
4312 /* If "var" contains white space, escape it with a backslash.
4313 * Required for ":e ~/tt" when $HOME includes a space. */
4314 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4315 {
4316 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4317
4318 if (p != NULL)
4319 {
4320 if (mustfree)
4321 vim_free(var);
4322 var = p;
4323 mustfree = TRUE;
4324 }
4325 }
4326
4327 if (var != NULL && *var != NUL
4328 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4329 {
4330 STRCPY(dst, var);
4331 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004332 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 /* if var[] ends in a path separator and tail[] starts
4334 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004335 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4337 && dst[-1] != ':'
4338#endif
4339 && vim_ispathsep(*tail))
4340 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004341 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 src = tail;
4343 copy_char = FALSE;
4344 }
4345 if (mustfree)
4346 vim_free(var);
4347 }
4348
4349 if (copy_char) /* copy at least one char */
4350 {
4351 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004352 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004353 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4354 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 */
4356 at_start = FALSE;
4357 if (src[0] == '\\' && src[1] != NUL)
4358 {
4359 *dst++ = *src++;
4360 --dstlen;
4361 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004362 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004364 if (dstlen > 0)
4365 {
4366 *dst++ = *src++;
4367 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004368
Bram Moolenaar1c864092017-08-06 18:15:45 +02004369 if (startstr != NULL && src - startstr_len >= srcp
4370 && STRNCMP(src - startstr_len, startstr,
4371 startstr_len) == 0)
4372 at_start = TRUE;
4373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377 *dst = NUL;
4378}
4379
4380/*
4381 * Vim's version of getenv().
4382 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004383 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004384 * "mustfree" is set to TRUE when returned is allocated, it must be
4385 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
4387 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004388vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 char_u *p;
4391 char_u *pend;
4392 int vimruntime;
4393
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004394#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 /* use "C:/" when $HOME is not set */
4396 if (STRCMP(name, "HOME") == 0)
4397 return homedir;
4398#endif
4399
4400 p = mch_getenv(name);
4401 if (p != NULL && *p == NUL) /* empty is the same as not set */
4402 p = NULL;
4403
4404 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004405 {
4406#if defined(FEAT_MBYTE) && defined(WIN3264)
4407 if (enc_utf8)
4408 {
4409 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004410 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004411
4412 /* Convert from active codepage to UTF-8. Other conversions are
4413 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004414 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004415 if (pp != NULL)
4416 {
4417 p = pp;
4418 *mustfree = TRUE;
4419 }
4420 }
4421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4426 if (!vimruntime && STRCMP(name, "VIM") != 0)
4427 return NULL;
4428
4429 /*
4430 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4431 * Don't do this when default_vimruntime_dir is non-empty.
4432 */
4433 if (vimruntime
4434#ifdef HAVE_PATHDEF
4435 && *default_vimruntime_dir == NUL
4436#endif
4437 )
4438 {
4439 p = mch_getenv((char_u *)"VIM");
4440 if (p != NULL && *p == NUL) /* empty is the same as not set */
4441 p = NULL;
4442 if (p != NULL)
4443 {
4444 p = vim_version_dir(p);
4445 if (p != NULL)
4446 *mustfree = TRUE;
4447 else
4448 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004449
4450#if defined(FEAT_MBYTE) && defined(WIN3264)
4451 if (enc_utf8)
4452 {
4453 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004454 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004455
4456 /* Convert from active codepage to UTF-8. Other conversions
4457 * are not done, because they would fail for non-ASCII
4458 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004459 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004460 if (pp != NULL)
4461 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004462 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004463 vim_free(p);
4464 p = pp;
4465 *mustfree = TRUE;
4466 }
4467 }
4468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 }
4471
4472 /*
4473 * When expanding $VIM or $VIMRUNTIME fails, try using:
4474 * - the directory name from 'helpfile' (unless it contains '$')
4475 * - the executable name from argv[0]
4476 */
4477 if (p == NULL)
4478 {
4479 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4480 p = p_hf;
4481#ifdef USE_EXE_NAME
4482 /*
4483 * Use the name of the executable, obtained from argv[0].
4484 */
4485 else
4486 p = exe_name;
4487#endif
4488 if (p != NULL)
4489 {
4490 /* remove the file name */
4491 pend = gettail(p);
4492
4493 /* remove "doc/" from 'helpfile', if present */
4494 if (p == p_hf)
4495 pend = remove_tail(p, pend, (char_u *)"doc");
4496
4497#ifdef USE_EXE_NAME
4498# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004499 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (p == exe_name)
4501 {
4502 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004503 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004505 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4506 if (pend1 != pend)
4507 {
4508 pnew = alloc((unsigned)(pend1 - p) + 15);
4509 if (pnew != NULL)
4510 {
4511 STRNCPY(pnew, p, (pend1 - p));
4512 STRCPY(pnew + (pend1 - p), "Resources/vim");
4513 p = pnew;
4514 pend = p + STRLEN(p);
4515 }
4516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518# endif
4519 /* remove "src/" from exe_name, if present */
4520 if (p == exe_name)
4521 pend = remove_tail(p, pend, (char_u *)"src");
4522#endif
4523
4524 /* for $VIM, remove "runtime/" or "vim54/", if present */
4525 if (!vimruntime)
4526 {
4527 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4528 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4529 }
4530
4531 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004532 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004535#ifdef MACOS_X
4536 if (p == exe_name || p == p_hf)
4537#endif
4538 /* check that the result is a directory name */
4539 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
4541 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004542 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else
4544 {
4545#ifdef USE_EXE_NAME
4546 /* may add "/vim54" or "/runtime" if it exists */
4547 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4548 {
4549 vim_free(p);
4550 p = pend;
4551 }
4552#endif
4553 *mustfree = TRUE;
4554 }
4555 }
4556 }
4557
4558#ifdef HAVE_PATHDEF
4559 /* When there is a pathdef.c file we can use default_vim_dir and
4560 * default_vimruntime_dir */
4561 if (p == NULL)
4562 {
4563 /* Only use default_vimruntime_dir when it is not empty */
4564 if (vimruntime && *default_vimruntime_dir != NUL)
4565 {
4566 p = default_vimruntime_dir;
4567 *mustfree = FALSE;
4568 }
4569 else if (*default_vim_dir != NUL)
4570 {
4571 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4572 *mustfree = TRUE;
4573 else
4574 {
4575 p = default_vim_dir;
4576 *mustfree = FALSE;
4577 }
4578 }
4579 }
4580#endif
4581
4582 /*
4583 * Set the environment variable, so that the new value can be found fast
4584 * next time, and others can also use it (e.g. Perl).
4585 */
4586 if (p != NULL)
4587 {
4588 if (vimruntime)
4589 {
4590 vim_setenv((char_u *)"VIMRUNTIME", p);
4591 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 else
4594 {
4595 vim_setenv((char_u *)"VIM", p);
4596 didset_vim = TRUE;
4597 }
4598 }
4599 return p;
4600}
4601
4602/*
4603 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4604 * Return NULL if not, return its name in allocated memory otherwise.
4605 */
4606 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004607vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
4609 char_u *p;
4610
4611 if (vimdir == NULL || *vimdir == NUL)
4612 return NULL;
4613 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4614 if (p != NULL && mch_isdir(p))
4615 return p;
4616 vim_free(p);
4617 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4618 if (p != NULL && mch_isdir(p))
4619 return p;
4620 vim_free(p);
4621 return NULL;
4622}
4623
4624/*
4625 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4626 * the length of "name/". Otherwise return "pend".
4627 */
4628 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004629remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 int len = (int)STRLEN(name) + 1;
4632 char_u *newend = pend - len;
4633
4634 if (newend >= p
4635 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004636 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 return newend;
4638 return pend;
4639}
4640
Bram Moolenaar137374f2018-05-13 15:59:50 +02004641 void
4642vim_unsetenv(char_u *var)
4643{
4644#ifdef HAVE_UNSETENV
4645 unsetenv((char *)var);
4646#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004647 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004648#endif
4649}
4650
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 * Our portable version of setenv.
4654 */
4655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004656vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658#ifdef HAVE_SETENV
4659 mch_setenv((char *)name, (char *)val, 1);
4660#else
4661 char_u *envbuf;
4662
4663 /*
4664 * Putenv does not copy the string, it has to remain
4665 * valid. The allocated memory will never be freed.
4666 */
4667 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4668 if (envbuf != NULL)
4669 {
4670 sprintf((char *)envbuf, "%s=%s", name, val);
4671 putenv((char *)envbuf);
4672 }
4673#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004674#ifdef FEAT_GETTEXT
4675 /*
4676 * When setting $VIMRUNTIME adjust the directory to find message
4677 * translations to $VIMRUNTIME/lang.
4678 */
4679 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4680 {
4681 char_u *buf = concat_str(val, (char_u *)"/lang");
4682
4683 if (buf != NULL)
4684 {
4685 bindtextdomain(VIMPACKAGE, (char *)buf);
4686 vim_free(buf);
4687 }
4688 }
4689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690}
4691
4692#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4693/*
4694 * Function given to ExpandGeneric() to obtain an environment variable name.
4695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004697get_env_name(
4698 expand_T *xp UNUSED,
4699 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
Bram Moolenaard0573012017-10-28 21:11:06 +02004701# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004703 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 */
4705 return NULL;
4706# else
4707# ifndef __WIN32__
4708 /* Borland C++ 5.2 has this in a header file. */
4709 extern char **environ;
4710# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004711# define ENVNAMELEN 100
4712 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 char_u *str;
4714 int n;
4715
4716 str = (char_u *)environ[idx];
4717 if (str == NULL)
4718 return NULL;
4719
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004720 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 if (str[n] == '=' || str[n] == NUL)
4723 break;
4724 name[n] = str[n];
4725 }
4726 name[n] = NUL;
4727 return name;
4728# endif
4729}
Bram Moolenaar24305862012-08-15 14:05:05 +02004730
4731/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004732 * Add a user name to the list of users in ga_users.
4733 * Do nothing if user name is NULL or empty.
4734 */
4735 static void
4736add_user(char_u *user, int need_copy)
4737{
4738 char_u *user_copy = (user != NULL && need_copy)
4739 ? vim_strsave(user) : user;
4740
4741 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4742 {
4743 if (need_copy)
4744 vim_free(user);
4745 return;
4746 }
4747 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4748}
4749
4750/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004751 * Find all user names for user completion.
4752 * Done only once and then cached.
4753 */
4754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004755init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004756{
Bram Moolenaar24305862012-08-15 14:05:05 +02004757 static int lazy_init_done = FALSE;
4758
4759 if (lazy_init_done)
4760 return;
4761
4762 lazy_init_done = TRUE;
4763 ga_init2(&ga_users, sizeof(char_u *), 20);
4764
4765# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4766 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004767 struct passwd* pw;
4768
4769 setpwent();
4770 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004771 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004772 endpwent();
4773 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004774# elif defined(WIN3264)
4775 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004776 DWORD nusers = 0, ntotal = 0, i;
4777 PUSER_INFO_0 uinfo;
4778
4779 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4780 &nusers, &ntotal, NULL) == NERR_Success)
4781 {
4782 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004783 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004784
4785 NetApiBufferFree(uinfo);
4786 }
4787 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004788# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004789# if defined(HAVE_GETPWNAM)
4790 {
4791 char_u *user_env = mch_getenv((char_u *)"USER");
4792
4793 // The $USER environment variable may be a valid remote user name (NIS,
4794 // LDAP) not already listed by getpwent(), as getpwent() only lists
4795 // local user names. If $USER is not already listed, check whether it
4796 // is a valid remote user name using getpwnam() and if it is, add it to
4797 // the list of user names.
4798
4799 if (user_env != NULL && *user_env != NUL)
4800 {
4801 int i;
4802
4803 for (i = 0; i < ga_users.ga_len; i++)
4804 {
4805 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4806
4807 if (STRCMP(local_user, user_env) == 0)
4808 break;
4809 }
4810
4811 if (i == ga_users.ga_len)
4812 {
4813 struct passwd *pw = getpwnam((char *)user_env);
4814
4815 if (pw != NULL)
4816 add_user((char_u *)pw->pw_name, TRUE);
4817 }
4818 }
4819 }
4820# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004821}
4822
4823/*
4824 * Function given to ExpandGeneric() to obtain an user names.
4825 */
4826 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004827get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004828{
4829 init_users();
4830 if (idx < ga_users.ga_len)
4831 return ((char_u **)ga_users.ga_data)[idx];
4832 return NULL;
4833}
4834
4835/*
4836 * Check whether name matches a user name. Return:
4837 * 0 if name does not match any user name.
4838 * 1 if name partially matches the beginning of a user name.
4839 * 2 is name fully matches a user name.
4840 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004841 int
4842match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004843{
4844 int i;
4845 int n = (int)STRLEN(name);
4846 int result = 0;
4847
4848 init_users();
4849 for (i = 0; i < ga_users.ga_len; i++)
4850 {
4851 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4852 return 2; /* full match */
4853 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4854 result = 1; /* partial match */
4855 }
4856 return result;
4857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858#endif
4859
4860/*
4861 * Replace home directory by "~" in each space or comma separated file name in
4862 * 'src'.
4863 * If anything fails (except when out of space) dst equals src.
4864 */
4865 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004866home_replace(
4867 buf_T *buf, /* when not NULL, check for help files */
4868 char_u *src, /* input file name */
4869 char_u *dst, /* where to put the result */
4870 int dstlen, /* maximum length of the result */
4871 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 spaces and commas in the file name. */
4873{
4874 size_t dirlen = 0, envlen = 0;
4875 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004876 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 char_u *p;
4878
4879 if (src == NULL)
4880 {
4881 *dst = NUL;
4882 return;
4883 }
4884
4885 /*
4886 * If the file is a help file, remove the path completely.
4887 */
4888 if (buf != NULL && buf->b_help)
4889 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004890 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return;
4892 }
4893
4894 /*
4895 * We check both the value of the $HOME environment variable and the
4896 * "real" home directory.
4897 */
4898 if (homedir != NULL)
4899 dirlen = STRLEN(homedir);
4900
4901#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004902 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004904 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4905#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004906#ifdef WIN3264
4907 if (homedir_env == NULL)
4908 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4909#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004910 /* Empty is the same as not set. */
4911 if (homedir_env != NULL && *homedir_env == NUL)
4912 homedir_env = NULL;
4913
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004914#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004915 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004916 {
4917 int usedlen = 0;
4918 int flen;
4919 char_u *fbuf = NULL;
4920
4921 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004922 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004923 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004924 flen = (int)STRLEN(homedir_env);
4925 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4926 /* Remove the trailing / that is added to a directory. */
4927 homedir_env[flen - 1] = NUL;
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (homedir_env != NULL)
4932 envlen = STRLEN(homedir_env);
4933
4934 if (!one)
4935 src = skipwhite(src);
4936 while (*src && dstlen > 0)
4937 {
4938 /*
4939 * Here we are at the beginning of a file name.
4940 * First, check to see if the beginning of the file name matches
4941 * $HOME or the "real" home directory. Check that there is a '/'
4942 * after the match (so that if e.g. the file is "/home/pieter/bla",
4943 * and the home directory is "/home/piet", the file does not end up
4944 * as "~er/bla" (which would seem to indicate the file "bla" in user
4945 * er's home directory)).
4946 */
4947 p = homedir;
4948 len = dirlen;
4949 for (;;)
4950 {
4951 if ( len
4952 && fnamencmp(src, p, len) == 0
4953 && (vim_ispathsep(src[len])
4954 || (!one && (src[len] == ',' || src[len] == ' '))
4955 || src[len] == NUL))
4956 {
4957 src += len;
4958 if (--dstlen > 0)
4959 *dst++ = '~';
4960
4961 /*
4962 * If it's just the home directory, add "/".
4963 */
4964 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4965 *dst++ = '/';
4966 break;
4967 }
4968 if (p == homedir_env)
4969 break;
4970 p = homedir_env;
4971 len = envlen;
4972 }
4973
4974 /* if (!one) skip to separator: space or comma */
4975 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4976 *dst++ = *src++;
4977 /* skip separator */
4978 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4979 *dst++ = *src++;
4980 }
4981 /* if (dstlen == 0) out of space, what to do??? */
4982
4983 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004984
4985 if (homedir_env != homedir_env_orig)
4986 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987}
4988
4989/*
4990 * Like home_replace, store the replaced string in allocated memory.
4991 * When something fails, NULL is returned.
4992 */
4993 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004994home_replace_save(
4995 buf_T *buf, /* when not NULL, check for help files */
4996 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
4998 char_u *dst;
4999 unsigned len;
5000
5001 len = 3; /* space for "~/" and trailing NUL */
5002 if (src != NULL) /* just in case */
5003 len += (unsigned)STRLEN(src);
5004 dst = alloc(len);
5005 if (dst != NULL)
5006 home_replace(buf, src, dst, len, TRUE);
5007 return dst;
5008}
5009
5010/*
5011 * Compare two file names and return:
5012 * FPC_SAME if they both exist and are the same file.
5013 * FPC_SAMEX if they both don't exist and have the same file name.
5014 * FPC_DIFF if they both exist and are different files.
5015 * FPC_NOTX if they both don't exist.
5016 * FPC_DIFFX if one of them doesn't exist.
5017 * For the first name environment variables are expanded
5018 */
5019 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005020fullpathcmp(
5021 char_u *s1,
5022 char_u *s2,
5023 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025#ifdef UNIX
5026 char_u exp1[MAXPATHL];
5027 char_u full1[MAXPATHL];
5028 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005029 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 int r1, r2;
5031
5032 expand_env(s1, exp1, MAXPATHL);
5033 r1 = mch_stat((char *)exp1, &st1);
5034 r2 = mch_stat((char *)s2, &st2);
5035 if (r1 != 0 && r2 != 0)
5036 {
5037 /* if mch_stat() doesn't work, may compare the names */
5038 if (checkname)
5039 {
5040 if (fnamecmp(exp1, s2) == 0)
5041 return FPC_SAMEX;
5042 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5043 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5044 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5045 return FPC_SAMEX;
5046 }
5047 return FPC_NOTX;
5048 }
5049 if (r1 != 0 || r2 != 0)
5050 return FPC_DIFFX;
5051 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5052 return FPC_SAME;
5053 return FPC_DIFF;
5054#else
5055 char_u *exp1; /* expanded s1 */
5056 char_u *full1; /* full path of s1 */
5057 char_u *full2; /* full path of s2 */
5058 int retval = FPC_DIFF;
5059 int r1, r2;
5060
5061 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5062 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5063 {
5064 full1 = exp1 + MAXPATHL;
5065 full2 = full1 + MAXPATHL;
5066
5067 expand_env(s1, exp1, MAXPATHL);
5068 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5069 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5070
5071 /* If vim_FullName() fails, the file probably doesn't exist. */
5072 if (r1 != OK && r2 != OK)
5073 {
5074 if (checkname && fnamecmp(exp1, s2) == 0)
5075 retval = FPC_SAMEX;
5076 else
5077 retval = FPC_NOTX;
5078 }
5079 else if (r1 != OK || r2 != OK)
5080 retval = FPC_DIFFX;
5081 else if (fnamecmp(full1, full2))
5082 retval = FPC_DIFF;
5083 else
5084 retval = FPC_SAME;
5085 vim_free(exp1);
5086 }
5087 return retval;
5088#endif
5089}
5090
5091/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005092 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005093 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005094 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 */
5096 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005097gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
5099 char_u *p1, *p2;
5100
5101 if (fname == NULL)
5102 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005103 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005105 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005107 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 return p1;
5110}
5111
Bram Moolenaar31710262010-08-13 13:36:15 +02005112#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005113/*
5114 * Return the end of the directory name, on the first path
5115 * separator:
5116 * "/path/file", "/path/dir/", "/path//dir", "/file"
5117 * ^ ^ ^ ^
5118 */
5119 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005120gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005121{
5122 char_u *dir_end = fname;
5123 char_u *next_dir_end = fname;
5124 int look_for_sep = TRUE;
5125 char_u *p;
5126
5127 for (p = fname; *p != NUL; )
5128 {
5129 if (vim_ispathsep(*p))
5130 {
5131 if (look_for_sep)
5132 {
5133 next_dir_end = p;
5134 look_for_sep = FALSE;
5135 }
5136 }
5137 else
5138 {
5139 if (!look_for_sep)
5140 dir_end = next_dir_end;
5141 look_for_sep = TRUE;
5142 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005143 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005144 }
5145 return dir_end;
5146}
5147#endif
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005150 * Get pointer to tail of "fname", including path separators. Putting a NUL
5151 * here leaves the directory name. Takes care of "c:/" and "//".
5152 * Always returns a valid pointer.
5153 */
5154 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005155gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005156{
5157 char_u *p;
5158 char_u *t;
5159
5160 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5161 t = gettail(fname);
5162 while (t > p && after_pathsep(fname, t))
5163 --t;
5164#ifdef VMS
5165 /* path separator is part of the path */
5166 ++t;
5167#endif
5168 return t;
5169}
5170
5171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 * get the next path component (just after the next path separator).
5173 */
5174 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005175getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
5177 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005178 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (*fname)
5180 ++fname;
5181 return fname;
5182}
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184/*
5185 * Get a pointer to one character past the head of a path name.
5186 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5187 * If there is no head, path is returned.
5188 */
5189 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005190get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
5192 char_u *retval;
5193
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005194#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 /* may skip "c:" */
5196 if (isalpha(path[0]) && path[1] == ':')
5197 retval = path + 2;
5198 else
5199 retval = path;
5200#else
5201# if defined(AMIGA)
5202 /* may skip "label:" */
5203 retval = vim_strchr(path, ':');
5204 if (retval == NULL)
5205 retval = path;
5206# else /* Unix */
5207 retval = path;
5208# endif
5209#endif
5210
5211 while (vim_ispathsep(*retval))
5212 ++retval;
5213
5214 return retval;
5215}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
5217/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005218 * Return TRUE if 'c' is a path separator.
5219 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 */
5221 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005224#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005226#else
5227# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005229# else
5230# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5232 return (c == ':' || c == '[' || c == ']' || c == '/'
5233 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005234# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005236# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239}
5240
Bram Moolenaar69c35002013-11-04 02:54:12 +01005241/*
5242 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5243 */
5244 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005245vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005246{
5247 return vim_ispathsep(c)
5248#ifdef BACKSLASH_IN_FILENAME
5249 && c != ':'
5250#endif
5251 ;
5252}
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5255/*
5256 * return TRUE if 'c' is a path list separator.
5257 */
5258 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005259vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260{
5261#ifdef UNIX
5262 return (c == ':');
5263#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005264 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#endif
5266}
5267#endif
5268
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005269/*
5270 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5271 * It's done in-place.
5272 */
5273 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005274shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005275{
5276 char_u *tail, *s, *d;
5277 int skip = FALSE;
5278
5279 tail = gettail(str);
5280 d = str;
5281 for (s = str; ; ++s)
5282 {
5283 if (s >= tail) /* copy the whole tail */
5284 {
5285 *d++ = *s;
5286 if (*s == NUL)
5287 break;
5288 }
5289 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5290 {
5291 *d++ = *s;
5292 skip = FALSE;
5293 }
5294 else if (!skip)
5295 {
5296 *d++ = *s; /* copy next char */
5297 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5298 skip = TRUE;
5299# ifdef FEAT_MBYTE
5300 if (has_mbyte)
5301 {
5302 int l = mb_ptr2len(s);
5303
5304 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005305 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005306 }
5307# endif
5308 }
5309 }
5310}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005311
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005312/*
5313 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5314 * Also returns TRUE if there is no directory name.
5315 * "fname" must be writable!.
5316 */
5317 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005318dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005319{
5320 char_u *p;
5321 int c;
5322 int retval;
5323
5324 p = gettail_sep(fname);
5325 if (p == fname)
5326 return TRUE;
5327 c = *p;
5328 *p = NUL;
5329 retval = mch_isdir(fname);
5330 *p = c;
5331 return retval;
5332}
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005335 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5336 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
5338 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005339vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005341#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005343#else
5344 if (p_fic)
5345 return MB_STRICMP(x, y);
5346 return STRCMP(x, y);
5347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348}
5349
5350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005353#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005354 char_u *px = x;
5355 char_u *py = y;
5356 int cx = NUL;
5357 int cy = NUL;
5358
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005359 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005361 cx = PTR2CHAR(px);
5362 cy = PTR2CHAR(py);
5363 if (cx == NUL || cy == NUL
5364 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5365 && !(cx == '/' && cy == '\\')
5366 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005368 len -= MB_PTR2LEN(px);
5369 px += MB_PTR2LEN(px);
5370 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372 if (len == 0)
5373 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005374 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005375#else
5376 if (p_fic)
5377 return MB_STRNICMP(x, y, len);
5378 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005380}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
5382/*
5383 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005384 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 */
5386 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 char_u *dest;
5390
5391 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5392 if (dest != NULL)
5393 {
5394 STRCPY(dest, fname1);
5395 if (sep)
5396 add_pathsep(dest);
5397 STRCAT(dest, fname2);
5398 }
5399 return dest;
5400}
5401
Bram Moolenaard6754642005-01-17 22:18:45 +00005402/*
5403 * Concatenate two strings and return the result in allocated memory.
5404 * Returns NULL when out of memory.
5405 */
5406 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005407concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005408{
5409 char_u *dest;
5410 size_t l = STRLEN(str1);
5411
5412 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5413 if (dest != NULL)
5414 {
5415 STRCPY(dest, str1);
5416 STRCPY(dest + l, str2);
5417 }
5418 return dest;
5419}
Bram Moolenaard6754642005-01-17 22:18:45 +00005420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421/*
5422 * Add a path separator to a file name, unless it already ends in a path
5423 * separator.
5424 */
5425 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005426add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005428 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 STRCAT(p, PATHSEPSTR);
5430}
5431
5432/*
5433 * FullName_save - Make an allocated copy of a full file name.
5434 * Returns NULL when out of memory.
5435 */
5436 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005437FullName_save(
5438 char_u *fname,
5439 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005440 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 char_u *buf;
5443 char_u *new_fname = NULL;
5444
5445 if (fname == NULL)
5446 return NULL;
5447
5448 buf = alloc((unsigned)MAXPATHL);
5449 if (buf != NULL)
5450 {
5451 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5452 new_fname = vim_strsave(buf);
5453 else
5454 new_fname = vim_strsave(fname);
5455 vim_free(buf);
5456 }
5457 return new_fname;
5458}
5459
5460#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5461
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005462static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005463static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465/*
5466 * Find the start of a comment, not knowing if we are in a comment right now.
5467 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005468 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005470 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005471ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005472{
5473 return find_start_comment(curbuf->b_ind_maxcomment);
5474}
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005477find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479 pos_T *pos;
5480 char_u *line;
5481 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005482 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005484 for (;;)
5485 {
5486 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5487 if (pos == NULL)
5488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005490 /*
5491 * Check if the comment start we found is inside a string.
5492 * If it is then restrict the search to below this line and try again.
5493 */
5494 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005495 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005496 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005497 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005498 break;
5499 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5500 if (cur_maxcomment <= 0)
5501 {
5502 pos = NULL;
5503 break;
5504 }
5505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 return pos;
5507}
5508
5509/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005510 * Find the start of a comment or raw string, not knowing if we are in a
5511 * comment or raw string right now.
5512 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005513 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005514 * Return NULL when not inside a comment or raw string.
5515 * "CORS" -> Comment Or Raw String
5516 */
5517 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005518ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005519{
Bram Moolenaar089af182015-10-07 11:41:49 +02005520 static pos_T comment_pos_copy;
5521 pos_T *comment_pos;
5522 pos_T *rs_pos;
5523
5524 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5525 if (comment_pos != NULL)
5526 {
5527 /* Need to make a copy of the static pos in findmatchlimit(),
5528 * calling find_start_rawstring() may change it. */
5529 comment_pos_copy = *comment_pos;
5530 comment_pos = &comment_pos_copy;
5531 }
5532 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005533
5534 /* If comment_pos is before rs_pos the raw string is inside the comment.
5535 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005536 if (comment_pos == NULL || (rs_pos != NULL
5537 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005538 {
5539 if (is_raw != NULL && rs_pos != NULL)
5540 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005541 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005542 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005543 return comment_pos;
5544}
5545
5546/*
5547 * Find the start of a raw string, not knowing if we are in one right now.
5548 * Search starts at w_cursor.lnum and goes backwards.
5549 * Return NULL when not inside a raw string.
5550 */
5551 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005552find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005553{
5554 pos_T *pos;
5555 char_u *line;
5556 char_u *p;
5557 int cur_maxcomment = ind_maxcomment;
5558
5559 for (;;)
5560 {
5561 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5562 if (pos == NULL)
5563 break;
5564
5565 /*
5566 * Check if the raw string start we found is inside a string.
5567 * If it is then restrict the search to below this line and try again.
5568 */
5569 line = ml_get(pos->lnum);
5570 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5571 p = skip_string(p);
5572 if ((colnr_T)(p - line) <= pos->col)
5573 break;
5574 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5575 if (cur_maxcomment <= 0)
5576 {
5577 pos = NULL;
5578 break;
5579 }
5580 }
5581 return pos;
5582}
5583
5584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 * Skip to the end of a "string" and a 'c' character.
5586 * If there is no string or character, return argument unmodified.
5587 */
5588 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005589skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 int i;
5592
5593 /*
5594 * We loop, because strings may be concatenated: "date""time".
5595 */
5596 for ( ; ; ++p)
5597 {
5598 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5599 {
5600 if (!p[1]) /* ' at end of line */
5601 break;
5602 i = 2;
5603 if (p[1] == '\\') /* '\n' or '\000' */
5604 {
5605 ++i;
5606 while (vim_isdigit(p[i - 1])) /* '\000' */
5607 ++i;
5608 }
5609 if (p[i] == '\'') /* check for trailing ' */
5610 {
5611 p += i;
5612 continue;
5613 }
5614 }
5615 else if (p[0] == '"') /* start of string */
5616 {
5617 for (++p; p[0]; ++p)
5618 {
5619 if (p[0] == '\\' && p[1] != NUL)
5620 ++p;
5621 else if (p[0] == '"') /* end of string */
5622 break;
5623 }
5624 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005625 continue; /* continue for another string */
5626 }
5627 else if (p[0] == 'R' && p[1] == '"')
5628 {
5629 /* Raw string: R"[delim](...)[delim]" */
5630 char_u *delim = p + 2;
5631 char_u *paren = vim_strchr(delim, '(');
5632
5633 if (paren != NULL)
5634 {
5635 size_t delim_len = paren - delim;
5636
5637 for (p += 3; *p; ++p)
5638 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5639 && p[delim_len + 1] == '"')
5640 {
5641 p += delim_len + 1;
5642 break;
5643 }
5644 if (p[0] == '"')
5645 continue; /* continue for another string */
5646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648 break; /* no string found */
5649 }
5650 if (!*p)
5651 --p; /* backup from NUL */
5652 return p;
5653}
5654#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5655
5656#if defined(FEAT_CINDENT) || defined(PROTO)
5657
5658/*
5659 * Do C or expression indenting on the current line.
5660 */
5661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005662do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664# ifdef FEAT_EVAL
5665 if (*curbuf->b_p_inde != NUL)
5666 fixthisline(get_expr_indent);
5667 else
5668# endif
5669 fixthisline(get_c_indent);
5670}
5671
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005672/* Find result cache for cpp_baseclass */
5673typedef struct {
5674 int found;
5675 lpos_T lpos;
5676} cpp_baseclass_cache_T;
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678/*
5679 * Functions for C-indenting.
5680 * Most of this originally comes from Eric Fischer.
5681 */
5682/*
5683 * Below "XXX" means that this function may unlock the current line.
5684 */
5685
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005686static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005687static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005688static int cin_iscomment(char_u *);
5689static int cin_islinecomment(char_u *);
5690static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005691static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005692static int cin_ends_in(char_u *, char_u *, char_u *);
5693static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005694static pos_T *find_match_paren(int);
5695static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005696static int find_last_paren(char_u *l, int start, int end);
5697static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699/*
5700 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005701 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 */
5703 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005704cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
5706 while (*s)
5707 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005708 char_u *prev_s = s;
5709
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005711
5712 /* Perl/shell # comment comment continues until eol. Require a space
5713 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005714 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005715 {
5716 s += STRLEN(s);
5717 break;
5718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (*s != '/')
5720 break;
5721 ++s;
5722 if (*s == '/') /* slash-slash comment continues till eol */
5723 {
5724 s += STRLEN(s);
5725 break;
5726 }
5727 if (*s != '*')
5728 break;
5729 for (++s; *s; ++s) /* skip slash-star comment */
5730 if (s[0] == '*' && s[1] == '/')
5731 {
5732 s += 2;
5733 break;
5734 }
5735 }
5736 return s;
5737}
5738
5739/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005740 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * not considered code.
5742 */
5743 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005744cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
5746 return *cin_skipcomment(s) == NUL;
5747}
5748
5749/*
5750 * Check previous lines for a "//" line comment, skipping over blank lines.
5751 */
5752 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005753find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 static pos_T pos;
5756 char_u *line;
5757 char_u *p;
5758
5759 pos = curwin->w_cursor;
5760 while (--pos.lnum > 0)
5761 {
5762 line = ml_get(pos.lnum);
5763 p = skipwhite(line);
5764 if (cin_islinecomment(p))
5765 {
5766 pos.col = (int)(p - line);
5767 return &pos;
5768 }
5769 if (*p != NUL)
5770 break;
5771 }
5772 return NULL;
5773}
5774
5775/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005776 * Return TRUE if "text" starts with "key:".
5777 */
5778 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005779cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005780{
5781 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005782 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005783
5784 if (*s == '\'' || *s == '"')
5785 {
5786 /* can be 'key': or "key": */
5787 quote = *s;
5788 ++s;
5789 }
5790 if (!vim_isIDc(*s)) /* need at least one ID character */
5791 return FALSE;
5792
5793 while (vim_isIDc(*s))
5794 ++s;
5795 if (*s == quote)
5796 ++s;
5797
5798 s = cin_skipcomment(s);
5799
5800 /* "::" is not a label, it's C++ */
5801 return (*s == ':' && s[1] != ':');
5802}
5803
5804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005806 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 */
5808 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005809cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 if (!vim_isIDc(**s)) /* need at least one ID character */
5812 return FALSE;
5813
5814 while (vim_isIDc(**s))
5815 (*s)++;
5816
5817 *s = cin_skipcomment(*s);
5818
5819 /* "::" is not a label, it's C++ */
5820 return (**s == ':' && *++*s != ':');
5821}
5822
5823/*
5824 * Recognize a label: "label:".
5825 * Note: curwin->w_cursor must be where we are looking for the label.
5826 */
5827 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005828cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 char_u *s;
5831
5832 s = cin_skipcomment(ml_get_curline());
5833
5834 /*
5835 * Exclude "default" from labels, since it should be indented
5836 * like a switch label. Same for C++ scope declarations.
5837 */
5838 if (cin_isdefault(s))
5839 return FALSE;
5840 if (cin_isscopedecl(s))
5841 return FALSE;
5842
5843 if (cin_islabel_skip(&s))
5844 {
5845 /*
5846 * Only accept a label if the previous line is terminated or is a case
5847 * label.
5848 */
5849 pos_T cursor_save;
5850 pos_T *trypos;
5851 char_u *line;
5852
5853 cursor_save = curwin->w_cursor;
5854 while (curwin->w_cursor.lnum > 1)
5855 {
5856 --curwin->w_cursor.lnum;
5857
5858 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005859 * If we're in a comment or raw string now, skip to the start of
5860 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 */
5862 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005863 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 curwin->w_cursor = *trypos;
5865
5866 line = ml_get_curline();
5867 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5868 continue;
5869 if (*(line = cin_skipcomment(line)) == NUL)
5870 continue;
5871
5872 curwin->w_cursor = cursor_save;
5873 if (cin_isterminated(line, TRUE, FALSE)
5874 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005875 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 || (cin_islabel_skip(&line) && cin_nocode(line)))
5877 return TRUE;
5878 return FALSE;
5879 }
5880 curwin->w_cursor = cursor_save;
5881 return TRUE; /* label at start of file??? */
5882 }
5883 return FALSE;
5884}
5885
5886/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005887 * Recognize structure initialization and enumerations:
5888 * "[typedef] [static|public|protected|private] enum"
5889 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 */
5891 static int
5892cin_isinit(void)
5893{
5894 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005895 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 s = cin_skipcomment(ml_get_curline());
5898
Bram Moolenaar75342212013-03-07 13:13:52 +01005899 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 s = cin_skipcomment(s + 7);
5901
Bram Moolenaar75342212013-03-07 13:13:52 +01005902 for (;;)
5903 {
5904 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005905
Bram Moolenaar75342212013-03-07 13:13:52 +01005906 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5907 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005908 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005909 if (cin_starts_with(s, skip[i]))
5910 {
5911 s = cin_skipcomment(s + l);
5912 l = 0;
5913 break;
5914 }
5915 }
5916 if (l != 0)
5917 break;
5918 }
5919
5920 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 return TRUE;
5922
5923 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5924 return TRUE;
5925
5926 return FALSE;
5927}
5928
5929/*
5930 * Recognize a switch label: "case .*:" or "default:".
5931 */
5932 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005933cin_iscase(
5934 char_u *s,
5935 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005938 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
5940 for (s += 4; *s; ++s)
5941 {
5942 s = cin_skipcomment(s);
5943 if (*s == ':')
5944 {
5945 if (s[1] == ':') /* skip over "::" for C++ */
5946 ++s;
5947 else
5948 return TRUE;
5949 }
5950 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005951 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5953 return FALSE; /* stop at comment */
5954 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005955 {
5956 /* JS etc. */
5957 if (strict)
5958 return FALSE; /* stop at string */
5959 else
5960 return TRUE;
5961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 }
5963 return FALSE;
5964 }
5965
5966 if (cin_isdefault(s))
5967 return TRUE;
5968 return FALSE;
5969}
5970
5971/*
5972 * Recognize a "default" switch label.
5973 */
5974 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005975cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 return (STRNCMP(s, "default", 7) == 0
5978 && *(s = cin_skipcomment(s + 7)) == ':'
5979 && s[1] != ':');
5980}
5981
5982/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005983 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 */
5985 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005986cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
5988 int i;
5989
5990 s = cin_skipcomment(s);
5991 if (STRNCMP(s, "public", 6) == 0)
5992 i = 6;
5993 else if (STRNCMP(s, "protected", 9) == 0)
5994 i = 9;
5995 else if (STRNCMP(s, "private", 7) == 0)
5996 i = 7;
5997 else
5998 return FALSE;
5999 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6000}
6001
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006002/* Maximum number of lines to search back for a "namespace" line. */
6003#define FIND_NAMESPACE_LIM 20
6004
6005/*
6006 * Recognize a "namespace" scope declaration.
6007 */
6008 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006009cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006010{
6011 char_u *p;
6012 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006013 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006014
6015 s = cin_skipcomment(s);
6016 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6017 {
6018 p = cin_skipcomment(skipwhite(s + 9));
6019 while (*p != NUL)
6020 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006021 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006022 {
6023 has_name = TRUE; /* found end of a name */
6024 p = cin_skipcomment(skipwhite(p));
6025 }
6026 else if (*p == '{')
6027 {
6028 break;
6029 }
6030 else if (vim_iswordc(*p))
6031 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006032 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006033 if (has_name)
6034 return FALSE; /* word character after skipping past name */
6035 ++p;
6036 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006037 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6038 {
6039 if (!has_name_start || has_name)
6040 return FALSE;
6041 /* C++ 17 nested namespace */
6042 p += 3;
6043 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006044 else
6045 {
6046 return FALSE;
6047 }
6048 }
6049 return TRUE;
6050 }
6051 return FALSE;
6052}
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006055 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6056 */
6057 static int
6058cin_is_cpp_extern_c(char_u *s)
6059{
6060 char_u *p;
6061 int has_string_literal = FALSE;
6062
6063 s = cin_skipcomment(s);
6064 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6065 {
6066 p = cin_skipcomment(skipwhite(s + 6));
6067 while (*p != NUL)
6068 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006069 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006070 {
6071 p = cin_skipcomment(skipwhite(p));
6072 }
6073 else if (*p == '{')
6074 {
6075 break;
6076 }
6077 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6078 {
6079 if (has_string_literal)
6080 return FALSE;
6081 has_string_literal = TRUE;
6082 p += 3;
6083 }
6084 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6085 && p[4] == '"')
6086 {
6087 if (has_string_literal)
6088 return FALSE;
6089 has_string_literal = TRUE;
6090 p += 5;
6091 }
6092 else
6093 {
6094 return FALSE;
6095 }
6096 }
6097 return has_string_literal ? TRUE : FALSE;
6098 }
6099 return FALSE;
6100}
6101
6102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 * Return a pointer to the first non-empty non-comment character after a ':'.
6104 * Return NULL if not found.
6105 * case 234: a = b;
6106 * ^
6107 */
6108 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006109after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110{
6111 for ( ; *l; ++l)
6112 {
6113 if (*l == ':')
6114 {
6115 if (l[1] == ':') /* skip over "::" for C++ */
6116 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006117 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 break;
6119 }
6120 else if (*l == '\'' && l[1] && l[2] == '\'')
6121 l += 2; /* skip over 'x' */
6122 }
6123 if (*l == NUL)
6124 return NULL;
6125 l = cin_skipcomment(l + 1);
6126 if (*l == NUL)
6127 return NULL;
6128 return l;
6129}
6130
6131/*
6132 * Get indent of line "lnum", skipping a label.
6133 * Return 0 if there is nothing after the label.
6134 */
6135 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006136get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 char_u *l;
6139 pos_T fp;
6140 colnr_T col;
6141 char_u *p;
6142
6143 l = ml_get(lnum);
6144 p = after_label(l);
6145 if (p == NULL)
6146 return 0;
6147
6148 fp.col = (colnr_T)(p - l);
6149 fp.lnum = lnum;
6150 getvcol(curwin, &fp, &col, NULL, NULL);
6151 return (int)col;
6152}
6153
6154/*
6155 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006156 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 * label: if (asdf && asdfasdf)
6158 * ^
6159 */
6160 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006161skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 char_u *l;
6164 int amount;
6165 pos_T cursor_save;
6166
6167 cursor_save = curwin->w_cursor;
6168 curwin->w_cursor.lnum = lnum;
6169 l = ml_get_curline();
6170 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006171 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
6173 amount = get_indent_nolabel(lnum);
6174 l = after_label(ml_get_curline());
6175 if (l == NULL) /* just in case */
6176 l = ml_get_curline();
6177 }
6178 else
6179 {
6180 amount = get_indent();
6181 l = ml_get_curline();
6182 }
6183 *pp = l;
6184
6185 curwin->w_cursor = cursor_save;
6186 return amount;
6187}
6188
6189/*
6190 * Return the indent of the first variable name after a type in a declaration.
6191 * int a, indent of "a"
6192 * static struct foo b, indent of "b"
6193 * enum bla c, indent of "c"
6194 * Returns zero when it doesn't look like a declaration.
6195 */
6196 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006197cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 char_u *line, *p, *s;
6200 int len;
6201 pos_T fp;
6202 colnr_T col;
6203
6204 line = ml_get_curline();
6205 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006206 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6208 {
6209 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006210 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
6212 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6213 p = skipwhite(p + 6);
6214 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6215 p = skipwhite(p + 4);
6216 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6217 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6218 {
6219 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006220 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6221 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6222 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6223 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 p = s;
6225 }
6226 for (len = 0; vim_isIDc(p[len]); ++len)
6227 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006228 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 return 0;
6230
6231 p = skipwhite(p + len);
6232 fp.lnum = curwin->w_cursor.lnum;
6233 fp.col = (colnr_T)(p - line);
6234 getvcol(curwin, &fp, &col, NULL, NULL);
6235 return (int)col;
6236}
6237
6238/*
6239 * Return the indent of the first non-blank after an equal sign.
6240 * char *foo = "here";
6241 * Return zero if no (useful) equal sign found.
6242 * Return -1 if the line above "lnum" ends in a backslash.
6243 * foo = "asdf\
6244 * asdf\
6245 * here";
6246 */
6247 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006248cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 char_u *line;
6251 char_u *s;
6252 colnr_T col;
6253 pos_T fp;
6254
6255 if (lnum > 1)
6256 {
6257 line = ml_get(lnum - 1);
6258 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6259 return -1;
6260 }
6261
6262 line = s = ml_get(lnum);
6263 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6264 {
6265 if (cin_iscomment(s)) /* ignore comments */
6266 s = cin_skipcomment(s);
6267 else
6268 ++s;
6269 }
6270 if (*s != '=')
6271 return 0;
6272
6273 s = skipwhite(s + 1);
6274 if (cin_nocode(s))
6275 return 0;
6276
6277 if (*s == '"') /* nice alignment for continued strings */
6278 ++s;
6279
6280 fp.lnum = lnum;
6281 fp.col = (colnr_T)(s - line);
6282 getvcol(curwin, &fp, &col, NULL, NULL);
6283 return (int)col;
6284}
6285
6286/*
6287 * Recognize a preprocessor statement: Any line that starts with '#'.
6288 */
6289 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006290cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006292 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 return TRUE;
6294 return FALSE;
6295}
6296
6297/*
6298 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6299 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6300 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006301 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 */
6303 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006304cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 char_u *line = *pp;
6307 linenr_T lnum = *lnump;
6308 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006309 int candidate_amount = *amount;
6310
6311 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6312 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006314 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
6316 if (cin_ispreproc(line))
6317 {
6318 retval = TRUE;
6319 *lnump = lnum;
6320 break;
6321 }
6322 if (lnum == 1)
6323 break;
6324 line = ml_get(--lnum);
6325 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6326 break;
6327 }
6328
6329 if (lnum != *lnump)
6330 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006331 if (retval)
6332 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 return retval;
6334}
6335
6336/*
6337 * Recognize the start of a C or C++ comment.
6338 */
6339 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006340cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6343}
6344
6345/*
6346 * Recognize the start of a "//" comment.
6347 */
6348 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006349cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 return (p[0] == '/' && p[1] == '/');
6352}
6353
6354/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006355 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6356 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006358 * If a line begins with an "else", only consider it terminated if no unmatched
6359 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 * Return the character terminating the line (ending char's have precedence if
6361 * both apply in order to determine initializations).
6362 */
6363 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006364cin_isterminated(
6365 char_u *s,
6366 int incl_open, /* include '{' at the end as terminator */
6367 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006369 char_u found_start = 0;
6370 unsigned n_open = 0;
6371 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373 s = cin_skipcomment(s);
6374
6375 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6376 found_start = *s;
6377
Bram Moolenaar496f9512011-05-19 16:35:09 +02006378 if (!found_start)
6379 is_else = cin_iselse(s);
6380
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 while (*s)
6382 {
6383 /* skip over comments, "" strings and 'c'haracters */
6384 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006385 if (*s == '}' && n_open > 0)
6386 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006387 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006388 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 && cin_nocode(s + 1))
6390 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006391 else if (*s == '{')
6392 {
6393 if (incl_open && cin_nocode(s + 1))
6394 return *s;
6395 else
6396 ++n_open;
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
6399 if (*s)
6400 s++;
6401 }
6402 return found_start;
6403}
6404
6405/*
6406 * Recognize the basic picture of a function declaration -- it needs to
6407 * have an open paren somewhere and a close paren at the end of the line and
6408 * no semicolons anywhere.
6409 * When a line ends in a comma we continue looking in the next line.
6410 * "sp" points to a string with the line. When looking at other lines it must
6411 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006412 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006413 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 */
6415 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006416cin_isfuncdecl(
6417 char_u **sp,
6418 linenr_T first_lnum,
6419 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 char_u *s;
6422 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006423 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006425 pos_T *trypos;
6426 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
6428 if (sp == NULL)
6429 s = ml_get(lnum);
6430 else
6431 s = *sp;
6432
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006433 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006434 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006435 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006436 {
6437 lnum = trypos->lnum;
6438 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006439 {
6440 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006441 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006442 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006443
6444 s = ml_get(lnum);
6445 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006446 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006447
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006448 /* Ignore line starting with #. */
6449 if (cin_ispreproc(s))
6450 return FALSE;
6451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6453 {
6454 if (cin_iscomment(s)) /* ignore comments */
6455 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006456 else if (*s == ':')
6457 {
6458 if (*(s + 1) == ':')
6459 s += 2;
6460 else
6461 /* To avoid a mistake in the following situation:
6462 * A::A(int a, int b)
6463 * : a(0) // <--not a function decl
6464 * , b(0)
6465 * {...
6466 */
6467 return FALSE;
6468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 else
6470 ++s;
6471 }
6472 if (*s != '(')
6473 return FALSE; /* ';', ' or " before any () or no '(' */
6474
6475 while (*s && *s != ';' && *s != '\'' && *s != '"')
6476 {
6477 if (*s == ')' && cin_nocode(s + 1))
6478 {
6479 /* ')' at the end: may have found a match
6480 * Check for he previous line not to end in a backslash:
6481 * #if defined(x) && \
6482 * defined(y)
6483 */
6484 lnum = first_lnum - 1;
6485 s = ml_get(lnum);
6486 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6487 retval = TRUE;
6488 goto done;
6489 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006490 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006492 int comma = (*s == ',');
6493
6494 /* ',' at the end: continue looking in the next line.
6495 * At the end: check for ',' in the next line, for this style:
6496 * func(arg1
6497 * , arg2) */
6498 for (;;)
6499 {
6500 if (lnum >= curbuf->b_ml.ml_line_count)
6501 break;
6502 s = ml_get(++lnum);
6503 if (!cin_ispreproc(s))
6504 break;
6505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if (lnum >= curbuf->b_ml.ml_line_count)
6507 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006508 /* Require a comma at end of the line or a comma or ')' at the
6509 * start of next line. */
6510 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006511 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006512 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006513 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515 else if (cin_iscomment(s)) /* ignore comments */
6516 s = cin_skipcomment(s);
6517 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006520 just_started = FALSE;
6521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523
6524done:
6525 if (lnum != first_lnum && sp != NULL)
6526 *sp = ml_get(first_lnum);
6527
6528 return retval;
6529}
6530
6531 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006532cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006534 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535}
6536
6537 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006538cin_iselse(
6539 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 if (*p == '}') /* accept "} else" */
6542 p = cin_skipcomment(p + 1);
6543 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6544}
6545
6546 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006547cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6550}
6551
6552/*
6553 * Check if this is a "while" that should have a matching "do".
6554 * We only accept a "while (condition) ;", with only white space between the
6555 * ')' and ';'. The condition may be spread over several lines.
6556 */
6557 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006558cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 pos_T cursor_save;
6561 pos_T *trypos;
6562 int retval = FALSE;
6563
6564 p = cin_skipcomment(p);
6565 if (*p == '}') /* accept "} while (cond);" */
6566 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006567 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
6569 cursor_save = curwin->w_cursor;
6570 curwin->w_cursor.lnum = lnum;
6571 curwin->w_cursor.col = 0;
6572 p = ml_get_curline();
6573 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6574 {
6575 ++p;
6576 ++curwin->w_cursor.col;
6577 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006578 if ((trypos = findmatchlimit(NULL, 0, 0,
6579 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6581 retval = TRUE;
6582 curwin->w_cursor = cursor_save;
6583 }
6584 return retval;
6585}
6586
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006587/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006588 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006589 * Return 0 if there is none.
6590 * Otherwise return !0 and update "*poffset" to point to the place where the
6591 * string was found.
6592 */
6593 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006594cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006595{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006596 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006597
6598 if (offset-- < 2)
6599 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006600 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006601 --offset;
6602
6603 offset -= 1;
6604 if (!STRNCMP(line + offset, "if", 2))
6605 goto probablyFound;
6606
6607 if (offset >= 1)
6608 {
6609 offset -= 1;
6610 if (!STRNCMP(line + offset, "for", 3))
6611 goto probablyFound;
6612
6613 if (offset >= 2)
6614 {
6615 offset -= 2;
6616 if (!STRNCMP(line + offset, "while", 5))
6617 goto probablyFound;
6618 }
6619 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006620 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006621
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006622probablyFound:
6623 if (!offset || !vim_isIDc(line[offset - 1]))
6624 {
6625 *poffset = offset;
6626 return 1;
6627 }
6628 return 0;
6629}
6630
6631/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006632 * Return TRUE if we are at the end of a do-while.
6633 * do
6634 * nothing;
6635 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006636 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006637 * Adjust the cursor to the line with "while".
6638 */
6639 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006640cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006641{
6642 char_u *line;
6643 char_u *p;
6644 char_u *s;
6645 pos_T *trypos;
6646 int i;
6647
6648 if (terminated != ';') /* there must be a ';' at the end */
6649 return FALSE;
6650
6651 p = line = ml_get_curline();
6652 while (*p != NUL)
6653 {
6654 p = cin_skipcomment(p);
6655 if (*p == ')')
6656 {
6657 s = skipwhite(p + 1);
6658 if (*s == ';' && cin_nocode(s + 1))
6659 {
6660 /* Found ");" at end of the line, now check there is "while"
6661 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006662 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006663 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006664 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006665 if (trypos != NULL)
6666 {
6667 s = cin_skipcomment(ml_get(trypos->lnum));
6668 if (*s == '}') /* accept "} while (cond);" */
6669 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006670 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006671 {
6672 curwin->w_cursor.lnum = trypos->lnum;
6673 return TRUE;
6674 }
6675 }
6676
6677 /* Searching may have made "line" invalid, get it again. */
6678 line = ml_get_curline();
6679 p = line + i;
6680 }
6681 }
6682 if (*p != NUL)
6683 ++p;
6684 }
6685 return FALSE;
6686}
6687
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006689cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6692}
6693
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006694/*
6695 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 * constructor-initialization. eg:
6697 *
6698 * class MyClass :
6699 * baseClass <-- here
6700 * class MyClass : public baseClass,
6701 * anotherBaseClass <-- here (should probably lineup ??)
6702 * MyClass::MyClass(...) :
6703 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006704 *
6705 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 */
6707 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006708cin_is_cpp_baseclass(
6709 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006711 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 char_u *s;
6713 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006714 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006715 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006717 if (pos->lnum <= lnum)
6718 return cached->found; /* Use the cached result */
6719
6720 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006722 s = skipwhite(line);
6723 if (*s == '#') /* skip #define FOO x ? (x) : x */
6724 return FALSE;
6725 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (*s == NUL)
6727 return FALSE;
6728
6729 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6730
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006731 /* Search for a line starting with '#', empty, ending in ';' or containing
6732 * '{' or '}' and start below it. This handles the following situations:
6733 * a = cond ?
6734 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006735 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006736 * func::foo()
6737 * : something
6738 * {}
6739 * Foo::Foo (int one, int two)
6740 * : something(4),
6741 * somethingelse(3)
6742 * {}
6743 */
6744 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006746 line = ml_get(lnum - 1);
6747 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006748 if (*s == '#' || *s == NUL)
6749 break;
6750 while (*s != NUL)
6751 {
6752 s = cin_skipcomment(s);
6753 if (*s == '{' || *s == '}'
6754 || (*s == ';' && cin_nocode(s + 1)))
6755 break;
6756 if (*s != NUL)
6757 ++s;
6758 }
6759 if (*s != NUL)
6760 break;
6761 --lnum;
6762 }
6763
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006764 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006765 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006766 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006767 for (;;)
6768 {
6769 if (*s == NUL)
6770 {
6771 if (lnum == curwin->w_cursor.lnum)
6772 break;
6773 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006774 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006775 s = line;
6776 }
6777 if (s == line)
6778 {
6779 /* don't recognize "case (foo):" as a baseclass */
6780 if (cin_iscase(s, FALSE))
6781 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006782 s = cin_skipcomment(line);
6783 if (*s == NUL)
6784 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006785 }
6786
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006787 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006788 s = skip_string(s) + 1;
6789 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 if (s[1] == ':')
6792 {
6793 /* skip double colon. It can't be a constructor
6794 * initialization any more */
6795 lookfor_ctor_init = FALSE;
6796 s = cin_skipcomment(s + 2);
6797 }
6798 else if (lookfor_ctor_init || class_or_struct)
6799 {
6800 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006801 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 cpp_base_class = TRUE;
6803 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006804 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 s = cin_skipcomment(s + 1);
6806 }
6807 else
6808 s = cin_skipcomment(s + 1);
6809 }
6810 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6811 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6812 {
6813 class_or_struct = TRUE;
6814 lookfor_ctor_init = FALSE;
6815
6816 if (*s == 'c')
6817 s = cin_skipcomment(s + 5);
6818 else
6819 s = cin_skipcomment(s + 6);
6820 }
6821 else
6822 {
6823 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6824 {
6825 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6826 }
6827 else if (s[0] == ')')
6828 {
6829 /* Constructor-initialization is assumed if we come across
6830 * something like "):" */
6831 class_or_struct = FALSE;
6832 lookfor_ctor_init = TRUE;
6833 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006834 else if (s[0] == '?')
6835 {
6836 /* Avoid seeing '() :' after '?' as constructor init. */
6837 return FALSE;
6838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 else if (!vim_isIDc(s[0]))
6840 {
6841 /* if it is not an identifier, we are wrong */
6842 class_or_struct = FALSE;
6843 lookfor_ctor_init = FALSE;
6844 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006845 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
6847 /* it can't be a constructor-initialization any more */
6848 lookfor_ctor_init = FALSE;
6849
6850 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006851 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006852 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 }
6854
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006855 /* When the line ends in a comma don't align with it. */
6856 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006857 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006858
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 s = cin_skipcomment(s + 1);
6860 }
6861 }
6862
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006863 cached->found = cpp_base_class;
6864 if (cpp_base_class)
6865 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 return cpp_base_class;
6867}
6868
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006869 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006870get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006871{
6872 int amount;
6873 colnr_T vcol;
6874 pos_T *trypos;
6875
6876 if (col == 0)
6877 {
6878 amount = get_indent();
6879 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006880 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006881 amount = get_indent_lnum(trypos->lnum); /* XXX */
6882 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006883 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006884 }
6885 else
6886 {
6887 curwin->w_cursor.col = col;
6888 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6889 amount = (int)vcol;
6890 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006891 if (amount < curbuf->b_ind_cpp_baseclass)
6892 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006893 return amount;
6894}
6895
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896/*
6897 * Return TRUE if string "s" ends with the string "find", possibly followed by
6898 * white space and comments. Skip strings and comments.
6899 * Ignore "ignore" after "find" if it's not NULL.
6900 */
6901 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006902cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
6904 char_u *p = s;
6905 char_u *r;
6906 int len = (int)STRLEN(find);
6907
6908 while (*p != NUL)
6909 {
6910 p = cin_skipcomment(p);
6911 if (STRNCMP(p, find, len) == 0)
6912 {
6913 r = skipwhite(p + len);
6914 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6915 r = skipwhite(r + STRLEN(ignore));
6916 if (cin_nocode(r))
6917 return TRUE;
6918 }
6919 if (*p != NUL)
6920 ++p;
6921 }
6922 return FALSE;
6923}
6924
6925/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006926 * Return TRUE when "s" starts with "word" and then a non-ID character.
6927 */
6928 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006929cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006930{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006931 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006932
6933 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6934}
6935
6936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 * Skip strings, chars and comments until at or past "trypos".
6938 * Return the column found.
6939 */
6940 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006941cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 char_u *line;
6944 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006945 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947 p = line = ml_get(trypos->lnum);
6948 while (*p && (colnr_T)(p - line) < trypos->col)
6949 {
6950 if (cin_iscomment(p))
6951 p = cin_skipcomment(p);
6952 else
6953 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006954 new_p = skip_string(p);
6955 if (new_p == p)
6956 ++p;
6957 else
6958 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960 }
6961 return (int)(p - line);
6962}
6963
6964/*
6965 * Find the '{' at the start of the block we are in.
6966 * Return NULL if no match found.
6967 * Ignore a '{' that is in a comment, makes indenting the next three lines
6968 * work. */
6969/* foo() */
6970/* { */
6971/* } */
6972
6973 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006974find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 pos_T cursor_save;
6977 pos_T *trypos;
6978 pos_T *pos;
6979 static pos_T pos_copy;
6980
6981 cursor_save = curwin->w_cursor;
6982 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6983 {
6984 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6985 trypos = &pos_copy;
6986 curwin->w_cursor = *trypos;
6987 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006988 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006990 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 break;
6992 if (pos != NULL)
6993 curwin->w_cursor.lnum = pos->lnum;
6994 }
6995 curwin->w_cursor = cursor_save;
6996 return trypos;
6997}
6998
6999/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007000 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007001 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 */
7003 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007004find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007006 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007007}
7008
7009 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007010find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007011{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 pos_T cursor_save;
7013 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007014 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007015 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
7017 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007018 ind_maxp_wk = ind_maxparen;
7019retry:
7020 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {
7022 /* check if the ( is in a // comment */
7023 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007024 {
7025 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7026 if (ind_maxp_wk > 0)
7027 {
7028 curwin->w_cursor = *trypos;
7029 curwin->w_cursor.col = 0; /* XXX */
7030 goto retry;
7031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 else
7035 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007036 pos_T *trypos_wk;
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7039 trypos = &pos_copy;
7040 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007041 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007042 {
7043 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7044 - trypos_wk->lnum);
7045 if (ind_maxp_wk > 0)
7046 {
7047 curwin->w_cursor = *trypos_wk;
7048 goto retry;
7049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
7053 }
7054 curwin->w_cursor = cursor_save;
7055 return trypos;
7056}
7057
7058/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007059 * Find the matching '(', ignoring it if it is in a comment or before an
7060 * unmatched {.
7061 * Return NULL if no match found.
7062 */
7063 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007064find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007065{
7066 pos_T *trypos = find_match_paren(ind_maxparen);
7067
7068 if (trypos != NULL)
7069 {
7070 pos_T *tryposBrace = find_start_brace();
7071
7072 /* If both an unmatched '(' and '{' is found. Ignore the '('
7073 * position if the '{' is further down. */
7074 if (tryposBrace != NULL
7075 && (trypos->lnum != tryposBrace->lnum
7076 ? trypos->lnum < tryposBrace->lnum
7077 : trypos->col < tryposBrace->col))
7078 trypos = NULL;
7079 }
7080 return trypos;
7081}
7082
7083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 * Return ind_maxparen corrected for the difference in line number between the
7085 * cursor position and "startpos". This makes sure that searching for a
7086 * matching paren above the cursor line doesn't find a match because of
7087 * looking a few lines further.
7088 */
7089 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007090corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7093
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007094 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7095 return curbuf->b_ind_maxparen - (int)n;
7096 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097}
7098
7099/*
7100 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007101 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 */
7103 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007104find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 int i;
7107 int retval = FALSE;
7108 int open_count = 0;
7109
7110 curwin->w_cursor.col = 0; /* default is start of line */
7111
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007112 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {
7114 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7115 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7116 if (l[i] == start)
7117 ++open_count;
7118 else if (l[i] == end)
7119 {
7120 if (open_count > 0)
7121 --open_count;
7122 else
7123 {
7124 curwin->w_cursor.col = i;
7125 retval = TRUE;
7126 }
7127 }
7128 }
7129 return retval;
7130}
7131
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007132/*
7133 * Parse 'cinoptions' and set the values in "curbuf".
7134 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7135 */
7136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007137parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007138{
7139 char_u *p;
7140 char_u *l;
7141 char_u *digits;
7142 int n;
7143 int divider;
7144 int fraction = 0;
7145 int sw = (int)get_sw_value(buf);
7146
7147 /*
7148 * Set the default values.
7149 */
7150 /* Spaces from a block's opening brace the prevailing indent for that
7151 * block should be. */
7152 buf->b_ind_level = sw;
7153
7154 /* Spaces from the edge of the line an open brace that's at the end of a
7155 * line is imagined to be. */
7156 buf->b_ind_open_imag = 0;
7157
7158 /* Spaces from the prevailing indent for a line that is not preceded by
7159 * an opening brace. */
7160 buf->b_ind_no_brace = 0;
7161
7162 /* Column where the first { of a function should be located }. */
7163 buf->b_ind_first_open = 0;
7164
7165 /* Spaces from the prevailing indent a leftmost open brace should be
7166 * located. */
7167 buf->b_ind_open_extra = 0;
7168
7169 /* Spaces from the matching open brace (real location for one at the left
7170 * edge; imaginary location from one that ends a line) the matching close
7171 * brace should be located. */
7172 buf->b_ind_close_extra = 0;
7173
7174 /* Spaces from the edge of the line an open brace sitting in the leftmost
7175 * column is imagined to be. */
7176 buf->b_ind_open_left_imag = 0;
7177
7178 /* Spaces jump labels should be shifted to the left if N is non-negative,
7179 * otherwise the jump label will be put to column 1. */
7180 buf->b_ind_jump_label = -1;
7181
7182 /* Spaces from the switch() indent a "case xx" label should be located. */
7183 buf->b_ind_case = sw;
7184
7185 /* Spaces from the "case xx:" code after a switch() should be located. */
7186 buf->b_ind_case_code = sw;
7187
7188 /* Lineup break at end of case in switch() with case label. */
7189 buf->b_ind_case_break = 0;
7190
7191 /* Spaces from the class declaration indent a scope declaration label
7192 * should be located. */
7193 buf->b_ind_scopedecl = sw;
7194
7195 /* Spaces from the scope declaration label code should be located. */
7196 buf->b_ind_scopedecl_code = sw;
7197
7198 /* Amount K&R-style parameters should be indented. */
7199 buf->b_ind_param = sw;
7200
7201 /* Amount a function type spec should be indented. */
7202 buf->b_ind_func_type = sw;
7203
7204 /* Amount a cpp base class declaration or constructor initialization
7205 * should be indented. */
7206 buf->b_ind_cpp_baseclass = sw;
7207
7208 /* additional spaces beyond the prevailing indent a continuation line
7209 * should be located. */
7210 buf->b_ind_continuation = sw;
7211
7212 /* Spaces from the indent of the line with an unclosed parentheses. */
7213 buf->b_ind_unclosed = sw * 2;
7214
7215 /* Spaces from the indent of the line with an unclosed parentheses, which
7216 * itself is also unclosed. */
7217 buf->b_ind_unclosed2 = sw;
7218
7219 /* Suppress ignoring spaces from the indent of a line starting with an
7220 * unclosed parentheses. */
7221 buf->b_ind_unclosed_noignore = 0;
7222
7223 /* If the opening paren is the last nonwhite character on the line, and
7224 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7225 * context (for very long lines). */
7226 buf->b_ind_unclosed_wrapped = 0;
7227
7228 /* Suppress ignoring white space when lining up with the character after
7229 * an unclosed parentheses. */
7230 buf->b_ind_unclosed_whiteok = 0;
7231
7232 /* Indent a closing parentheses under the line start of the matching
7233 * opening parentheses. */
7234 buf->b_ind_matching_paren = 0;
7235
7236 /* Indent a closing parentheses under the previous line. */
7237 buf->b_ind_paren_prev = 0;
7238
7239 /* Extra indent for comments. */
7240 buf->b_ind_comment = 0;
7241
7242 /* Spaces from the comment opener when there is nothing after it. */
7243 buf->b_ind_in_comment = 3;
7244
7245 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7246 * after the comment opener. */
7247 buf->b_ind_in_comment2 = 0;
7248
7249 /* Max lines to search for an open paren. */
7250 buf->b_ind_maxparen = 20;
7251
7252 /* Max lines to search for an open comment. */
7253 buf->b_ind_maxcomment = 70;
7254
7255 /* Handle braces for java code. */
7256 buf->b_ind_java = 0;
7257
7258 /* Not to confuse JS object properties with labels. */
7259 buf->b_ind_js = 0;
7260
7261 /* Handle blocked cases correctly. */
7262 buf->b_ind_keep_case_label = 0;
7263
7264 /* Handle C++ namespace. */
7265 buf->b_ind_cpp_namespace = 0;
7266
7267 /* Handle continuation lines containing conditions of if(), for() and
7268 * while(). */
7269 buf->b_ind_if_for_while = 0;
7270
Bram Moolenaar6b643942017-03-05 19:44:06 +01007271 /* indentation for # comments */
7272 buf->b_ind_hash_comment = 0;
7273
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007274 /* Handle C++ extern "C" or "C++" */
7275 buf->b_ind_cpp_extern_c = 0;
7276
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007277 for (p = buf->b_p_cino; *p; )
7278 {
7279 l = p++;
7280 if (*p == '-')
7281 ++p;
7282 digits = p; /* remember where the digits start */
7283 n = getdigits(&p);
7284 divider = 0;
7285 if (*p == '.') /* ".5s" means a fraction */
7286 {
7287 fraction = atol((char *)++p);
7288 while (VIM_ISDIGIT(*p))
7289 {
7290 ++p;
7291 if (divider)
7292 divider *= 10;
7293 else
7294 divider = 10;
7295 }
7296 }
7297 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7298 {
7299 if (p == digits)
7300 n = sw; /* just "s" is one 'shiftwidth' */
7301 else
7302 {
7303 n *= sw;
7304 if (divider)
7305 n += (sw * fraction + divider / 2) / divider;
7306 }
7307 ++p;
7308 }
7309 if (l[1] == '-')
7310 n = -n;
7311
7312 /* When adding an entry here, also update the default 'cinoptions' in
7313 * doc/indent.txt, and add explanation for it! */
7314 switch (*l)
7315 {
7316 case '>': buf->b_ind_level = n; break;
7317 case 'e': buf->b_ind_open_imag = n; break;
7318 case 'n': buf->b_ind_no_brace = n; break;
7319 case 'f': buf->b_ind_first_open = n; break;
7320 case '{': buf->b_ind_open_extra = n; break;
7321 case '}': buf->b_ind_close_extra = n; break;
7322 case '^': buf->b_ind_open_left_imag = n; break;
7323 case 'L': buf->b_ind_jump_label = n; break;
7324 case ':': buf->b_ind_case = n; break;
7325 case '=': buf->b_ind_case_code = n; break;
7326 case 'b': buf->b_ind_case_break = n; break;
7327 case 'p': buf->b_ind_param = n; break;
7328 case 't': buf->b_ind_func_type = n; break;
7329 case '/': buf->b_ind_comment = n; break;
7330 case 'c': buf->b_ind_in_comment = n; break;
7331 case 'C': buf->b_ind_in_comment2 = n; break;
7332 case 'i': buf->b_ind_cpp_baseclass = n; break;
7333 case '+': buf->b_ind_continuation = n; break;
7334 case '(': buf->b_ind_unclosed = n; break;
7335 case 'u': buf->b_ind_unclosed2 = n; break;
7336 case 'U': buf->b_ind_unclosed_noignore = n; break;
7337 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7338 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7339 case 'm': buf->b_ind_matching_paren = n; break;
7340 case 'M': buf->b_ind_paren_prev = n; break;
7341 case ')': buf->b_ind_maxparen = n; break;
7342 case '*': buf->b_ind_maxcomment = n; break;
7343 case 'g': buf->b_ind_scopedecl = n; break;
7344 case 'h': buf->b_ind_scopedecl_code = n; break;
7345 case 'j': buf->b_ind_java = n; break;
7346 case 'J': buf->b_ind_js = n; break;
7347 case 'l': buf->b_ind_keep_case_label = n; break;
7348 case '#': buf->b_ind_hash_comment = n; break;
7349 case 'N': buf->b_ind_cpp_namespace = n; break;
7350 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007351 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007352 }
7353 if (*p == ',')
7354 ++p;
7355 }
7356}
7357
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007358/*
7359 * Return the desired indent for C code.
7360 * Return -1 if the indent should be left alone (inside a raw string).
7361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007363get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 pos_T cur_curpos;
7366 int amount;
7367 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007368 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 colnr_T col;
7370 char_u *theline;
7371 char_u *linecopy;
7372 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007373 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007375 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 pos_T our_paren_pos;
7377 char_u *start;
7378 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007379#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380#define BRACE_AT_START 2 /* '{' is at start of line */
7381#define BRACE_AT_END 3 /* '{' is at end of line */
7382 linenr_T ourscope;
7383 char_u *l;
7384 char_u *look;
7385 char_u terminated;
7386 int lookfor;
7387#define LOOKFOR_INITIAL 0
7388#define LOOKFOR_IF 1
7389#define LOOKFOR_DO 2
7390#define LOOKFOR_CASE 3
7391#define LOOKFOR_ANY 4
7392#define LOOKFOR_TERM 5
7393#define LOOKFOR_UNTERM 6
7394#define LOOKFOR_SCOPEDECL 7
7395#define LOOKFOR_NOBREAK 8
7396#define LOOKFOR_CPP_BASECLASS 9
7397#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007398#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007399#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
7401 int whilelevel;
7402 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 int n;
7404 int iscase;
7405 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007406 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007408 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007409 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007410 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007411 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007412 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007414 /* make a copy, value is changed below */
7415 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
7417 /* remember where the cursor was when we started */
7418 cur_curpos = curwin->w_cursor;
7419
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007420 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007421 if (cur_curpos.lnum == 1)
7422 return 0;
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 /* Get a copy of the current contents of the line.
7425 * This is required, because only the most recent line obtained with
7426 * ml_get is valid! */
7427 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7428 if (linecopy == NULL)
7429 return 0;
7430
7431 /*
7432 * In insert mode and the cursor is on a ')' truncate the line at the
7433 * cursor position. We don't want to line up with the matching '(' when
7434 * inserting new stuff.
7435 * For unknown reasons the cursor might be past the end of the line, thus
7436 * check for that.
7437 */
7438 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007439 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 && linecopy[curwin->w_cursor.col] == ')')
7441 linecopy[curwin->w_cursor.col] = NUL;
7442
7443 theline = skipwhite(linecopy);
7444
7445 /* move the cursor to the start of the line */
7446
7447 curwin->w_cursor.col = 0;
7448
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007449 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007450
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007452 * If we are inside a raw string don't change the indent.
7453 * Ignore a raw string inside a comment.
7454 */
7455 comment_pos = ind_find_start_comment();
7456 if (comment_pos != NULL)
7457 {
7458 /* findmatchlimit() static pos is overwritten, make a copy */
7459 tryposCopy = *comment_pos;
7460 comment_pos = &tryposCopy;
7461 }
7462 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007463 if (trypos != NULL && (comment_pos == NULL
7464 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007465 {
7466 amount = -1;
7467 goto laterend;
7468 }
7469
7470 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 * #defines and so on always go at the left when included in 'cinkeys'.
7472 */
7473 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007474 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007475 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007476 goto theend;
7477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
7479 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007480 * Is it a non-case label? Then that goes at the left margin too unless:
7481 * - JS flag is set.
7482 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007484 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007485 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {
7487 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007488 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
7490
7491 /*
7492 * If we're inside a "//" comment and there is a "//" comment in a
7493 * previous line, lineup with that one.
7494 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007495 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 && (trypos = find_line_comment()) != NULL) /* XXX */
7497 {
7498 /* find how indented the line beginning the comment is */
7499 getvcol(curwin, trypos, &col, NULL, NULL);
7500 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007501 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503
7504 /*
7505 * If we're inside a comment and not looking at the start of the
7506 * comment, try using the 'comments' option.
7507 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007508 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
7510 int lead_start_len = 2;
7511 int lead_middle_len = 1;
7512 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7513 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7514 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7515 char_u *p;
7516 int start_align = 0;
7517 int start_off = 0;
7518 int done = FALSE;
7519
7520 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007521 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007523 *lead_start = NUL;
7524 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
7526 p = curbuf->b_p_com;
7527 while (*p != NUL)
7528 {
7529 int align = 0;
7530 int off = 0;
7531 int what = 0;
7532
7533 while (*p != NUL && *p != ':')
7534 {
7535 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7536 what = *p++;
7537 else if (*p == COM_LEFT || *p == COM_RIGHT)
7538 align = *p++;
7539 else if (VIM_ISDIGIT(*p) || *p == '-')
7540 off = getdigits(&p);
7541 else
7542 ++p;
7543 }
7544
7545 if (*p == ':')
7546 ++p;
7547 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7548 if (what == COM_START)
7549 {
7550 STRCPY(lead_start, lead_end);
7551 lead_start_len = (int)STRLEN(lead_start);
7552 start_off = off;
7553 start_align = align;
7554 }
7555 else if (what == COM_MIDDLE)
7556 {
7557 STRCPY(lead_middle, lead_end);
7558 lead_middle_len = (int)STRLEN(lead_middle);
7559 }
7560 else if (what == COM_END)
7561 {
7562 /* If our line starts with the middle comment string, line it
7563 * up with the comment opener per the 'comments' option. */
7564 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7565 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7566 {
7567 done = TRUE;
7568 if (curwin->w_cursor.lnum > 1)
7569 {
7570 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007571 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 * the middle comment string matches in the previous
7573 * line, use the indent of that line. XXX */
7574 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7575 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7576 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7577 else if (STRNCMP(look, lead_middle,
7578 lead_middle_len) == 0)
7579 {
7580 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7581 break;
7582 }
7583 /* If the start comment string doesn't match with the
7584 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007585 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 lead_start, lead_start_len) != 0)
7587 continue;
7588 }
7589 if (start_off != 0)
7590 amount += start_off;
7591 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007592 amount += vim_strsize(lead_start)
7593 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 break;
7595 }
7596
7597 /* If our line starts with the end comment string, line it up
7598 * with the middle comment */
7599 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7600 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7601 {
7602 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7603 /* XXX */
7604 if (off != 0)
7605 amount += off;
7606 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007607 amount += vim_strsize(lead_start)
7608 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 done = TRUE;
7610 break;
7611 }
7612 }
7613 }
7614
7615 /* If our line starts with an asterisk, line up with the
7616 * asterisk in the comment opener; otherwise, line up
7617 * with the first character of the comment text.
7618 */
7619 if (done)
7620 ;
7621 else if (theline[0] == '*')
7622 amount += 1;
7623 else
7624 {
7625 /*
7626 * If we are more than one line away from the comment opener, take
7627 * the indent of the previous non-empty line. If 'cino' has "CO"
7628 * and we are just below the comment opener and there are any
7629 * white characters after it line up with the text after it;
7630 * otherwise, add the amount specified by "c" in 'cino'
7631 */
7632 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007633 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {
7635 if (linewhite(lnum)) /* skip blank lines */
7636 continue;
7637 amount = get_indent_lnum(lnum); /* XXX */
7638 break;
7639 }
7640 if (amount == -1) /* use the comment opener */
7641 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007642 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007644 start = ml_get(comment_pos->lnum);
7645 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007647 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007649 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007651 if (curbuf->b_ind_in_comment2 || *look == NUL)
7652 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 }
7654 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007655 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657
7658 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007659 * Are we looking at a ']' that has a match?
7660 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007661 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007662 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7663 {
7664 /* align with the line containing the '['. */
7665 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007666 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007667 }
7668
7669 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 * Are we inside parentheses or braces?
7671 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007672 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007673 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007674 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 || trypos != NULL)
7676 {
7677 if (trypos != NULL && tryposBrace != NULL)
7678 {
7679 /* Both an unmatched '(' and '{' is found. Use the one which is
7680 * closer to the current cursor position, set the other to NULL. */
7681 if (trypos->lnum != tryposBrace->lnum
7682 ? trypos->lnum < tryposBrace->lnum
7683 : trypos->col < tryposBrace->col)
7684 trypos = NULL;
7685 else
7686 tryposBrace = NULL;
7687 }
7688
7689 if (trypos != NULL)
7690 {
7691 /*
7692 * If the matching paren is more than one line away, use the indent of
7693 * a previous non-empty line that matches the same paren.
7694 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007695 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007697 /* Line up with the start of the matching paren line. */
7698 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7699 }
7700 else
7701 {
7702 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007703 our_paren_pos = *trypos;
7704 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007706 l = skipwhite(ml_get(lnum));
7707 if (cin_nocode(l)) /* skip comment lines */
7708 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007709 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007710 continue; /* ignore #define, #if, etc. */
7711 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007713 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007714 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007715 {
7716 lnum = trypos->lnum + 1;
7717 continue;
7718 }
7719
7720 /* XXX */
7721 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007722 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007723 && trypos->lnum == our_paren_pos.lnum
7724 && trypos->col == our_paren_pos.col)
7725 {
7726 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007728 if (theline[0] == ')')
7729 {
7730 if (our_paren_pos.lnum != lnum
7731 && cur_amount > amount)
7732 cur_amount = amount;
7733 amount = -1;
7734 }
7735 break;
7736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
7738 }
7739
7740 /*
7741 * Line up with line where the matching paren is. XXX
7742 * If the line starts with a '(' or the indent for unclosed
7743 * parentheses is zero, line up with the unclosed parentheses.
7744 */
7745 if (amount == -1)
7746 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007747 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007748 int is_if_for_while = 0;
7749
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007750 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007751 {
7752 /* Look for the outermost opening parenthesis on this line
7753 * and check whether it belongs to an "if", "for" or "while". */
7754
7755 pos_T cursor_save = curwin->w_cursor;
7756 pos_T outermost;
7757 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007758
7759 trypos = &our_paren_pos;
7760 do {
7761 outermost = *trypos;
7762 curwin->w_cursor.lnum = outermost.lnum;
7763 curwin->w_cursor.col = outermost.col;
7764
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007765 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007766 } while (trypos && trypos->lnum == outermost.lnum);
7767
7768 curwin->w_cursor = cursor_save;
7769
7770 line = ml_get(outermost.lnum);
7771
7772 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007773 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007774 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007775
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007776 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007777 look = skipwhite(look);
7778 if (*look == '(')
7779 {
7780 linenr_T save_lnum = curwin->w_cursor.lnum;
7781 char_u *line;
7782 int look_col;
7783
7784 /* Ignore a '(' in front of the line that has a match before
7785 * our matching '('. */
7786 curwin->w_cursor.lnum = our_paren_pos.lnum;
7787 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007788 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007789 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007790 if ((trypos = findmatchlimit(NULL, ')', 0,
7791 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007792 != NULL
7793 && trypos->lnum == our_paren_pos.lnum
7794 && trypos->col < our_paren_pos.col)
7795 ignore_paren_col = trypos->col + 1;
7796
7797 curwin->w_cursor.lnum = save_lnum;
7798 look = ml_get(our_paren_pos.lnum) + look_col;
7799 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007800 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7801 && is_if_for_while == 0)
7802 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007803 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {
7805 /*
7806 * If we're looking at a close paren, line up right there;
7807 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007808 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 * the last nonwhite character of the line, use either the
7810 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007811 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 * lines).
7813 */
7814 if (theline[0] != ')')
7815 {
7816 cur_amount = MAXCOL;
7817 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007818 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 && cin_ends_in(l, (char_u *)"(", NULL))
7820 {
7821 /* look for opening unmatched paren, indent one level
7822 * for each additional level */
7823 n = 1;
7824 for (col = 0; col < our_paren_pos.col; ++col)
7825 {
7826 switch (l[col])
7827 {
7828 case '(':
7829 case '{': ++n;
7830 break;
7831
7832 case ')':
7833 case '}': if (n > 1)
7834 --n;
7835 break;
7836 }
7837 }
7838
7839 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007840 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007842 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 our_paren_pos.col++;
7844 else
7845 {
7846 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007847 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 col++;
7849 if (l[col] != NUL) /* In case of trailing space */
7850 our_paren_pos.col = col;
7851 else
7852 our_paren_pos.col++;
7853 }
7854 }
7855
7856 /*
7857 * Find how indented the paren is, or the character after it
7858 * if we did the above "if".
7859 */
7860 if (our_paren_pos.col > 0)
7861 {
7862 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7863 if (cur_amount > (int)col)
7864 cur_amount = col;
7865 }
7866 }
7867
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007868 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {
7870 /* Line up with the start of the matching paren line. */
7871 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007872 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7873 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007874 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {
7876 if (cur_amount != MAXCOL)
7877 amount = cur_amount;
7878 }
7879 else
7880 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007881 /* Add b_ind_unclosed2 for each '(' before our matching one,
7882 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007884 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {
7886 --our_paren_pos.col;
7887 switch (*ml_get_pos(&our_paren_pos))
7888 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007889 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 col = our_paren_pos.col;
7891 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007892 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 col = MAXCOL;
7894 break;
7895 }
7896 }
7897
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007898 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 * braces */
7900 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007901 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 else
7903 {
7904 curwin->w_cursor.lnum = our_paren_pos.lnum;
7905 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007906 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7907 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007908 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007910 {
7911 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007912 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007913 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007914 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 }
7917 /*
7918 * For a line starting with ')' use the minimum of the two
7919 * positions, to avoid giving it more indent than the previous
7920 * lines:
7921 * func_long_name( if (x
7922 * arg && yy
7923 * ) ^ not here ) ^ not here
7924 */
7925 if (cur_amount < amount)
7926 amount = cur_amount;
7927 }
7928 }
7929
7930 /* add extra indent for a comment */
7931 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007932 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 else
7935 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007936 /*
7937 * We are inside braces, there is a { before this line at the position
7938 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007939 * Make a copy of tryposBrace, it may point to pos_copy inside
7940 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007941 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007942 tryposCopy = *tryposBrace;
7943 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 ourscope = trypos->lnum;
7946 start = ml_get(ourscope);
7947
7948 /*
7949 * Now figure out how indented the line is in general.
7950 * If the brace was at the start of the line, we use that;
7951 * otherwise, check out the indentation of the line as
7952 * a whole and then add the "imaginary indent" to that.
7953 */
7954 look = skipwhite(start);
7955 if (*look == '{')
7956 {
7957 getvcol(curwin, trypos, &col, NULL, NULL);
7958 amount = col;
7959 if (*start == '{')
7960 start_brace = BRACE_IN_COL0;
7961 else
7962 start_brace = BRACE_AT_START;
7963 }
7964 else
7965 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007966 /* That opening brace might have been on a continuation
7967 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 curwin->w_cursor.lnum = ourscope;
7969
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007970 /* Position the cursor over the rightmost paren, so that
7971 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 lnum = ourscope;
7973 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007974 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7975 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 lnum = trypos->lnum;
7977
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007978 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 * case 1: if (asdf &&
7980 * ldfd) {
7981 * }
7982 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007983 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7984 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007986 else if (curbuf->b_ind_js)
7987 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007989 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990
7991 start_brace = BRACE_AT_END;
7992 }
7993
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007994 /* For Javascript check if the line starts with "key:". */
7995 if (curbuf->b_ind_js)
7996 js_cur_has_key = cin_has_js_key(theline);
7997
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007999 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 * we want to be. otherwise, add the amount of room
8001 * that an indent is supposed to be.
8002 */
8003 if (theline[0] == '}')
8004 {
8005 /*
8006 * they may want closing braces to line up with something
8007 * other than the open brace. indulge them, if so.
8008 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008009 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 }
8011 else
8012 {
8013 /*
8014 * If we're looking at an "else", try to find an "if"
8015 * to match it with.
8016 * If we're looking at a "while", try to find a "do"
8017 * to match it with.
8018 */
8019 lookfor = LOOKFOR_INITIAL;
8020 if (cin_iselse(theline))
8021 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008022 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 lookfor = LOOKFOR_DO;
8024 if (lookfor != LOOKFOR_INITIAL)
8025 {
8026 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008027 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {
8029 amount = get_indent(); /* XXX */
8030 goto theend;
8031 }
8032 }
8033
8034 /*
8035 * We get here if we are not on an "while-of-do" or "else" (or
8036 * failed to find a matching "if").
8037 * Search backwards for something to line up with.
8038 * First set amount for when we don't find anything.
8039 */
8040
8041 /*
8042 * if the '{' is _really_ at the left margin, use the imaginary
8043 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008044 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 */
8046
8047 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8048 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008049 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008050 lookfor_cpp_namespace = TRUE;
8051 }
8052 else if (start_brace == BRACE_AT_START &&
8053 lookfor_cpp_namespace) /* '{' is at start */
8054 {
8055
8056 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 }
8058 else
8059 {
8060 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008061 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008062 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008063
8064 l = skipwhite(ml_get_curline());
8065 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008066 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008067 else if (cin_is_cpp_extern_c(l))
8068 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 else
8071 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008072 /* Compensate for adding b_ind_open_extra later. */
8073 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 if (amount < 0)
8075 amount = 0;
8076 }
8077 }
8078
8079 lookfor_break = FALSE;
8080
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008081 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {
8083 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008084 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 }
8086 else if (cin_isscopedecl(theline)) /* private:, ... */
8087 {
8088 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008089 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 }
8091 else
8092 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008093 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8094 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 lookfor_break = TRUE;
8096
8097 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008098 /* b_ind_level from start of block */
8099 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
8101 scope_amount = amount;
8102 whilelevel = 0;
8103
8104 /*
8105 * Search backwards. If we find something we recognize, line up
8106 * with that.
8107 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008108 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 * the usual amount relative to the conditional
8110 * that opens the block.
8111 */
8112 curwin->w_cursor = cur_curpos;
8113 for (;;)
8114 {
8115 curwin->w_cursor.lnum--;
8116 curwin->w_cursor.col = 0;
8117
8118 /*
8119 * If we went all the way back to the start of our scope, line
8120 * up with it.
8121 */
8122 if (curwin->w_cursor.lnum <= ourscope)
8123 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008124 /* We reached end of scope:
8125 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008127 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 * don't add ind_continuation, otherwise it is a variable
8129 * declaration:
8130 * int x,
8131 * here; <-- add ind_continuation
8132 */
8133 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8134 {
8135 if (curwin->w_cursor.lnum == 0
8136 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008137 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008139 /* nothing found (abuse curbuf->b_ind_maxparen as
8140 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 * initialization) */
8142 if (cont_amount > 0)
8143 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008144 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 amount += ind_continuation;
8146 break;
8147 }
8148
8149 l = ml_get_curline();
8150
8151 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008152 * If we're in a comment or raw string now, skip to
8153 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008155 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 if (trypos != NULL)
8157 {
8158 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008159 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 continue;
8161 }
8162
8163 /*
8164 * Skip preprocessor directives and blank lines.
8165 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008166 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8167 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 continue;
8169
8170 if (cin_nocode(l))
8171 continue;
8172
8173 terminated = cin_isterminated(l, FALSE, TRUE);
8174
8175 /*
8176 * If we are at top level and the line looks like a
8177 * function declaration, we are done
8178 * (it's a variable declaration).
8179 */
8180 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008181 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {
8183 /* if the line is terminated with another ','
8184 * it is a continued variable initialization.
8185 * don't add extra indent.
8186 * TODO: does not work, if a function
8187 * declaration is split over multiple lines:
8188 * cin_isfuncdecl returns FALSE then.
8189 */
8190 if (terminated == ',')
8191 break;
8192
8193 /* if it es a enum declaration or an assignment,
8194 * we are done.
8195 */
8196 if (terminated != ';' && cin_isinit())
8197 break;
8198
8199 /* nothing useful found */
8200 if (terminated == 0 || terminated == '{')
8201 continue;
8202 }
8203
8204 if (terminated != ';')
8205 {
8206 /* Skip parens and braces. Position the cursor
8207 * over the rightmost paren, so that matching it
8208 * will take us back to the start of the line.
8209 */ /* XXX */
8210 trypos = NULL;
8211 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008212 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008213 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214
8215 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008216 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217
8218 if (trypos != NULL)
8219 {
8220 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008221 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 continue;
8223 }
8224 }
8225
8226 /* it's a variable declaration, add indentation
8227 * like in
8228 * int a,
8229 * b;
8230 */
8231 if (cont_amount > 0)
8232 amount = cont_amount;
8233 else
8234 amount += ind_continuation;
8235 }
8236 else if (lookfor == LOOKFOR_UNTERM)
8237 {
8238 if (cont_amount > 0)
8239 amount = cont_amount;
8240 else
8241 amount += ind_continuation;
8242 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008243 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008244 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008245 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008246 && lookfor != LOOKFOR_CPP_BASECLASS
8247 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008248 {
8249 amount = scope_amount;
8250 if (theline[0] == '{')
8251 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008252 amount += curbuf->b_ind_open_extra;
8253 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008254 }
8255 }
8256
8257 if (lookfor_cpp_namespace)
8258 {
8259 /*
8260 * Looking for C++ namespace, need to look further
8261 * back.
8262 */
8263 if (curwin->w_cursor.lnum == ourscope)
8264 continue;
8265
8266 if (curwin->w_cursor.lnum == 0
8267 || curwin->w_cursor.lnum
8268 < ourscope - FIND_NAMESPACE_LIM)
8269 break;
8270
8271 l = ml_get_curline();
8272
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008273 /* If we're in a comment or raw string now, skip
8274 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008275 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008276 if (trypos != NULL)
8277 {
8278 curwin->w_cursor.lnum = trypos->lnum + 1;
8279 curwin->w_cursor.col = 0;
8280 continue;
8281 }
8282
8283 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008284 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8285 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008286 continue;
8287
8288 /* Finally the actual check for "namespace". */
8289 if (cin_is_cpp_namespace(l))
8290 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008291 amount += curbuf->b_ind_cpp_namespace
8292 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008293 break;
8294 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008295 else if (cin_is_cpp_extern_c(l))
8296 {
8297 amount += curbuf->b_ind_cpp_extern_c
8298 - added_to_amount;
8299 break;
8300 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008301
8302 if (cin_nocode(l))
8303 continue;
8304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 }
8306 break;
8307 }
8308
8309 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008310 * If we're in a comment or raw string now, skip to the start
8311 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008313 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
8315 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008316 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 continue;
8318 }
8319
8320 l = ml_get_curline();
8321
8322 /*
8323 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008324 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008326 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 if (iscase || cin_isscopedecl(l))
8328 {
8329 /* we are only looking for cpp base class
8330 * declaration/initialization any longer */
8331 if (lookfor == LOOKFOR_CPP_BASECLASS)
8332 break;
8333
8334 /* When looking for a "do" we are not interested in
8335 * labels. */
8336 if (whilelevel > 0)
8337 continue;
8338
8339 /*
8340 * case xx:
8341 * c = 99 + <- this indent plus continuation
8342 *-> here;
8343 */
8344 if (lookfor == LOOKFOR_UNTERM
8345 || lookfor == LOOKFOR_ENUM_OR_INIT)
8346 {
8347 if (cont_amount > 0)
8348 amount = cont_amount;
8349 else
8350 amount += ind_continuation;
8351 break;
8352 }
8353
8354 /*
8355 * case xx: <- line up with this case
8356 * x = 333;
8357 * case yy:
8358 */
8359 if ( (iscase && lookfor == LOOKFOR_CASE)
8360 || (iscase && lookfor_break)
8361 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8362 {
8363 /*
8364 * Check that this case label is not for another
8365 * switch()
8366 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008367 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008368 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {
8370 amount = get_indent(); /* XXX */
8371 break;
8372 }
8373 continue;
8374 }
8375
8376 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8377
8378 /*
8379 * case xx: if (cond) <- line up with this if
8380 * y = y + 1;
8381 * -> s = 99;
8382 *
8383 * case xx:
8384 * if (cond) <- line up with this line
8385 * y = y + 1;
8386 * -> s = 99;
8387 */
8388 if (lookfor == LOOKFOR_TERM)
8389 {
8390 if (n)
8391 amount = n;
8392
8393 if (!lookfor_break)
8394 break;
8395 }
8396
8397 /*
8398 * case xx: x = x + 1; <- line up with this x
8399 * -> y = y + 1;
8400 *
8401 * case xx: if (cond) <- line up with this if
8402 * -> y = y + 1;
8403 */
8404 if (n)
8405 {
8406 amount = n;
8407 l = after_label(ml_get_curline());
8408 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008409 {
8410 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008411 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008412 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008413 amount += curbuf->b_ind_level
8414 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 break;
8417 }
8418
8419 /*
8420 * Try to get the indent of a statement before the switch
8421 * label. If nothing is found, line up relative to the
8422 * switch label.
8423 * break; <- may line up with this line
8424 * case xx:
8425 * -> y = 1;
8426 */
8427 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008428 ? curbuf->b_ind_case_code
8429 : curbuf->b_ind_scopedecl_code);
8430 lookfor = curbuf->b_ind_case_break
8431 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 continue;
8433 }
8434
8435 /*
8436 * Looking for a switch() label or C++ scope declaration,
8437 * ignore other lines, skip {}-blocks.
8438 */
8439 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8440 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008441 if (find_last_paren(l, '{', '}')
8442 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008445 curwin->w_cursor.col = 0;
8446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 continue;
8448 }
8449
8450 /*
8451 * Ignore jump labels with nothing after them.
8452 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008453 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {
8455 l = after_label(ml_get_curline());
8456 if (l == NULL || cin_nocode(l))
8457 continue;
8458 }
8459
8460 /*
8461 * Ignore #defines, #if, etc.
8462 * Ignore comment and empty lines.
8463 * (need to get the line again, cin_islabel() may have
8464 * unlocked it)
8465 */
8466 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008467 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 || cin_nocode(l))
8469 continue;
8470
8471 /*
8472 * Are we at the start of a cpp base class declaration or
8473 * constructor initialization?
8474 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008475 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008476 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008477 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008478 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008479 l = ml_get_curline();
8480 }
8481 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {
8483 if (lookfor == LOOKFOR_UNTERM)
8484 {
8485 if (cont_amount > 0)
8486 amount = cont_amount;
8487 else
8488 amount += ind_continuation;
8489 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008490 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008492 /* Need to find start of the declaration. */
8493 lookfor = LOOKFOR_UNTERM;
8494 ind_continuation = 0;
8495 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 }
8497 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008498 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008499 amount = get_baseclass_amount(
8500 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 break;
8502 }
8503 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8504 {
8505 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008506 * declaration or initialization before the opening brace.
8507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 if (cin_isterminated(l, TRUE, FALSE))
8509 break;
8510 else
8511 continue;
8512 }
8513
8514 /*
8515 * What happens next depends on the line being terminated.
8516 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008517 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 * 123,
8519 * sizeof
8520 * here
8521 * Otherwise check whether it is a enumeration or structure
8522 * initialisation (not indented) or a variable declaration
8523 * (indented).
8524 */
8525 terminated = cin_isterminated(l, FALSE, TRUE);
8526
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008527 if (js_cur_has_key)
8528 {
8529 js_cur_has_key = 0; /* only check the first line */
8530 if (curbuf->b_ind_js && terminated == ',')
8531 {
8532 /* For Javascript we might be inside an object:
8533 * key: something, <- align with this
8534 * key: something
8535 * or:
8536 * key: something + <- align with this
8537 * something,
8538 * key: something
8539 */
8540 lookfor = LOOKFOR_JS_KEY;
8541 }
8542 }
8543 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8544 {
8545 amount = get_indent();
8546 break;
8547 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008548 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008549 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008550 if (tryposBrace != NULL && tryposBrace->lnum
8551 >= curwin->w_cursor.lnum)
8552 break;
8553 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008554 /* line below current line is the one that starts a
8555 * (possibly broken) line ending in a comma. */
8556 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008557 else
8558 {
8559 amount = get_indent();
8560 if (curwin->w_cursor.lnum - 1 == ourscope)
8561 /* line above is start of the scope, thus current
8562 * line is the one that stars a (possibly broken)
8563 * line ending in a comma. */
8564 break;
8565 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008566 }
8567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8569 && terminated == ','))
8570 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008571 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8572 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008573 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 /*
8575 * if we're in the middle of a paren thing,
8576 * go back to the line that starts it so
8577 * we can get the right prevailing indent
8578 * if ( foo &&
8579 * bar )
8580 */
8581 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008582 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008584 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 */
8586 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008587 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008588 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8589 || (trypos->lnum == tryposBrace->lnum
8590 && trypos->col < tryposBrace->col)))
8591 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 /*
8594 * If we are looking for ',', we also look for matching
8595 * braces.
8596 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008597 if (trypos == NULL && terminated == ','
8598 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008599 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
8601 if (trypos != NULL)
8602 {
8603 /*
8604 * Check if we are on a case label now. This is
8605 * handled above.
8606 * case xx: if ( asdf &&
8607 * asdf)
8608 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008609 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008611 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 {
8613 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008614 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 continue;
8616 }
8617 }
8618
8619 /*
8620 * Skip over continuation lines to find the one to get the
8621 * indent from
8622 * char *usethis = "bla\
8623 * bla",
8624 * here;
8625 */
8626 if (terminated == ',')
8627 {
8628 while (curwin->w_cursor.lnum > 1)
8629 {
8630 l = ml_get(curwin->w_cursor.lnum - 1);
8631 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8632 break;
8633 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008634 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636 }
8637
8638 /*
8639 * Get indent and pointer to text for current line,
8640 * ignoring any jump label. XXX
8641 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008642 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008643 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008644 else
8645 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 /*
8647 * If this is just above the line we are indenting, and it
8648 * starts with a '{', line it up with this line.
8649 * while (not)
8650 * -> {
8651 * }
8652 */
8653 if (terminated != ',' && lookfor != LOOKFOR_TERM
8654 && theline[0] == '{')
8655 {
8656 amount = cur_amount;
8657 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008658 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 * doesn't start with a '{', which must have a match
8660 * in the same line (scope is the same). Probably:
8661 * { 1, 2 },
8662 * -> { 3, 4 }
8663 */
8664 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008665 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008667 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
8669 /* have to look back, whether it is a cpp base
8670 * class declaration or initialization */
8671 lookfor = LOOKFOR_CPP_BASECLASS;
8672 continue;
8673 }
8674 break;
8675 }
8676
8677 /*
8678 * Check if we are after an "if", "while", etc.
8679 * Also allow " } else".
8680 */
8681 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8682 {
8683 /*
8684 * Found an unterminated line after an if (), line up
8685 * with the last one.
8686 * if (cond)
8687 * 100 +
8688 * -> here;
8689 */
8690 if (lookfor == LOOKFOR_UNTERM
8691 || lookfor == LOOKFOR_ENUM_OR_INIT)
8692 {
8693 if (cont_amount > 0)
8694 amount = cont_amount;
8695 else
8696 amount += ind_continuation;
8697 break;
8698 }
8699
8700 /*
8701 * If this is just above the line we are indenting, we
8702 * are finished.
8703 * while (not)
8704 * -> here;
8705 * Otherwise this indent can be used when the line
8706 * before this is terminated.
8707 * yyy;
8708 * if (stat)
8709 * while (not)
8710 * xxx;
8711 * -> here;
8712 */
8713 amount = cur_amount;
8714 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008715 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 if (lookfor != LOOKFOR_TERM)
8717 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008718 amount += curbuf->b_ind_level
8719 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 break;
8721 }
8722
8723 /*
8724 * Special trick: when expecting the while () after a
8725 * do, line up with the while()
8726 * do
8727 * x = 1;
8728 * -> here
8729 */
8730 l = skipwhite(ml_get_curline());
8731 if (cin_isdo(l))
8732 {
8733 if (whilelevel == 0)
8734 break;
8735 --whilelevel;
8736 }
8737
8738 /*
8739 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008740 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 * Need to use the scope of this "else". XXX
8742 * If whilelevel != 0 continue looking for a "do {".
8743 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008744 if (cin_iselse(l) && whilelevel == 0)
8745 {
8746 /* If we're looking at "} else", let's make sure we
8747 * find the opening brace of the enclosing scope,
8748 * not the one from "if () {". */
8749 if (*l == '}')
8750 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008751 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008752
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008753 if ((trypos = find_start_brace()) == NULL
8754 || find_match(LOOKFOR_IF, trypos->lnum)
8755 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008756 break;
8757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
8759
8760 /*
8761 * If we're below an unterminated line that is not an
8762 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008763 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 * the line before this one.
8765 */
8766 else
8767 {
8768 /*
8769 * Found two unterminated lines on a row, line up with
8770 * the last one.
8771 * c = 99 +
8772 * 100 +
8773 * -> here;
8774 */
8775 if (lookfor == LOOKFOR_UNTERM)
8776 {
8777 /* When line ends in a comma add extra indent */
8778 if (terminated == ',')
8779 amount += ind_continuation;
8780 break;
8781 }
8782
8783 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8784 {
8785 /* Found two lines ending in ',', lineup with the
8786 * lowest one, but check for cpp base class
8787 * declaration/initialization, if it is an
8788 * opening brace or we are looking just for
8789 * enumerations/initializations. */
8790 if (terminated == ',')
8791 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008792 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 break;
8794
8795 lookfor = LOOKFOR_CPP_BASECLASS;
8796 continue;
8797 }
8798
8799 /* Ignore unterminated lines in between, but
8800 * reduce indent. */
8801 if (amount > cur_amount)
8802 amount = cur_amount;
8803 }
8804 else
8805 {
8806 /*
8807 * Found first unterminated line on a row, may
8808 * line up with this line, remember its indent
8809 * 100 +
8810 * -> here;
8811 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008812 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008814
8815 n = (int)STRLEN(l);
8816 if (terminated == ',' && (*skipwhite(l) == ']'
8817 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819
8820 /*
8821 * If previous line ends in ',', check whether we
8822 * are in an initialization or enum
8823 * struct xxx =
8824 * {
8825 * sizeof a,
8826 * 124 };
8827 * or a normal possible continuation line.
8828 * but only, of no other statement has been found
8829 * yet.
8830 */
8831 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8832 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008833 if (curbuf->b_ind_js)
8834 {
8835 /* Search for a line ending in a comma
8836 * and line up with the line below it
8837 * (could be the current line).
8838 * some = [
8839 * 1, <- line up here
8840 * 2,
8841 * some = [
8842 * 3 + <- line up here
8843 * 4 *
8844 * 5,
8845 * 6,
8846 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008847 if (cin_iscomment(skipwhite(l)))
8848 break;
8849 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008850 trypos = find_match_char('[',
8851 curbuf->b_ind_maxparen);
8852 if (trypos != NULL)
8853 {
8854 if (trypos->lnum
8855 == curwin->w_cursor.lnum - 1)
8856 {
8857 /* Current line is first inside
8858 * [], line up with it. */
8859 break;
8860 }
8861 ourscope = trypos->lnum;
8862 }
8863 }
8864 else
8865 {
8866 lookfor = LOOKFOR_ENUM_OR_INIT;
8867 cont_amount = cin_first_id_amount();
8868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 }
8870 else
8871 {
8872 if (lookfor == LOOKFOR_INITIAL
8873 && *l != NUL
8874 && l[STRLEN(l) - 1] == '\\')
8875 /* XXX */
8876 cont_amount = cin_get_equal_amount(
8877 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008878 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008879 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008880 && lookfor != LOOKFOR_COMMA
8881 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 lookfor = LOOKFOR_UNTERM;
8883 }
8884 }
8885 }
8886 }
8887
8888 /*
8889 * Check if we are after a while (cond);
8890 * If so: Ignore until the matching "do".
8891 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008892 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 {
8894 /*
8895 * Found an unterminated line after a while ();, line up
8896 * with the last one.
8897 * while (cond);
8898 * 100 + <- line up with this one
8899 * -> here;
8900 */
8901 if (lookfor == LOOKFOR_UNTERM
8902 || lookfor == LOOKFOR_ENUM_OR_INIT)
8903 {
8904 if (cont_amount > 0)
8905 amount = cont_amount;
8906 else
8907 amount += ind_continuation;
8908 break;
8909 }
8910
8911 if (whilelevel == 0)
8912 {
8913 lookfor = LOOKFOR_TERM;
8914 amount = get_indent(); /* XXX */
8915 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008916 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 }
8918 ++whilelevel;
8919 }
8920
8921 /*
8922 * We are after a "normal" statement.
8923 * If we had another statement we can stop now and use the
8924 * indent of that other statement.
8925 * Otherwise the indent of the current statement may be used,
8926 * search backwards for the next "normal" statement.
8927 */
8928 else
8929 {
8930 /*
8931 * Skip single break line, if before a switch label. It
8932 * may be lined up with the case label.
8933 */
8934 if (lookfor == LOOKFOR_NOBREAK
8935 && cin_isbreak(skipwhite(ml_get_curline())))
8936 {
8937 lookfor = LOOKFOR_ANY;
8938 continue;
8939 }
8940
8941 /*
8942 * Handle "do {" line.
8943 */
8944 if (whilelevel > 0)
8945 {
8946 l = cin_skipcomment(ml_get_curline());
8947 if (cin_isdo(l))
8948 {
8949 amount = get_indent(); /* XXX */
8950 --whilelevel;
8951 continue;
8952 }
8953 }
8954
8955 /*
8956 * Found a terminated line above an unterminated line. Add
8957 * the amount for a continuation line.
8958 * x = 1;
8959 * y = foo +
8960 * -> here;
8961 * or
8962 * int x = 1;
8963 * int foo,
8964 * -> here;
8965 */
8966 if (lookfor == LOOKFOR_UNTERM
8967 || lookfor == LOOKFOR_ENUM_OR_INIT)
8968 {
8969 if (cont_amount > 0)
8970 amount = cont_amount;
8971 else
8972 amount += ind_continuation;
8973 break;
8974 }
8975
8976 /*
8977 * Found a terminated line above a terminated line or "if"
8978 * etc. line. Use the amount of the line below us.
8979 * x = 1; x = 1;
8980 * if (asdf) y = 2;
8981 * while (asdf) ->here;
8982 * here;
8983 * ->foo;
8984 */
8985 if (lookfor == LOOKFOR_TERM)
8986 {
8987 if (!lookfor_break && whilelevel == 0)
8988 break;
8989 }
8990
8991 /*
8992 * First line above the one we're indenting is terminated.
8993 * To know what needs to be done look further backward for
8994 * a terminated line.
8995 */
8996 else
8997 {
8998 /*
8999 * position the cursor over the rightmost paren, so
9000 * that matching it will take us back to the start of
9001 * the line. Helps for:
9002 * func(asdr,
9003 * asdfasdf);
9004 * here;
9005 */
9006term_again:
9007 l = ml_get_curline();
9008 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009009 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009010 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 {
9012 /*
9013 * Check if we are on a case label now. This is
9014 * handled above.
9015 * case xx: if ( asdf &&
9016 * asdf)
9017 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009018 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009020 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 {
9022 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009023 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 continue;
9025 }
9026 }
9027
9028 /* When aligning with the case statement, don't align
9029 * with a statement after it.
9030 * case 1: { <-- don't use this { position
9031 * stat;
9032 * }
9033 * case 2:
9034 * stat;
9035 * }
9036 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009037 iscase = (curbuf->b_ind_keep_case_label
9038 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039
9040 /*
9041 * Get indent and pointer to text for current line,
9042 * ignoring any jump label.
9043 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009044 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045
9046 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009047 amount += curbuf->b_ind_open_extra;
9048 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009049 l = skipwhite(l);
9050 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009051 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9053
9054 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009055 * When a terminated line starts with "else" skip to
9056 * the matching "if":
9057 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009058 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009059 * Need to use the scope of this "else". XXX
9060 * If whilelevel != 0 continue looking for a "do {".
9061 */
9062 if (lookfor == LOOKFOR_TERM
9063 && *l != '}'
9064 && cin_iselse(l)
9065 && whilelevel == 0)
9066 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009067 if ((trypos = find_start_brace()) == NULL
9068 || find_match(LOOKFOR_IF, trypos->lnum)
9069 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009070 break;
9071 continue;
9072 }
9073
9074 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 * If we're at the end of a block, skip to the start of
9076 * that block.
9077 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009078 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009079 if (find_last_paren(l, '{', '}') /* XXX */
9080 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009082 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 /* if not "else {" check for terminated again */
9084 /* but skip block for "} else {" */
9085 l = cin_skipcomment(ml_get_curline());
9086 if (*l == '}' || !cin_iselse(l))
9087 goto term_again;
9088 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009089 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091 }
9092 }
9093 }
9094 }
9095 }
9096
9097 /* add extra indent for a comment */
9098 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009099 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009100
9101 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009102 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9103 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009104
9105 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009107
9108 /*
9109 * ok -- we're not inside any sort of structure at all!
9110 *
9111 * This means we're at the top level, and everything should
9112 * basically just match where the previous line is, except
9113 * for the lines immediately following a function declaration,
9114 * which are K&R-style parameters and need to be indented.
9115 *
9116 * if our line starts with an open brace, forget about any
9117 * prevailing indent and make sure it looks like the start
9118 * of a function
9119 */
9120
9121 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009123 amount = curbuf->b_ind_first_open;
9124 goto theend;
9125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009127 /*
9128 * If the NEXT line is a function declaration, the current
9129 * line needs to be indented as a function type spec.
9130 * Don't do this if the current line looks like a comment or if the
9131 * current line is terminated, ie. ends in ';', or if the current line
9132 * contains { or }: "void f() {\n if (1)"
9133 */
9134 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9135 && !cin_nocode(theline)
9136 && vim_strchr(theline, '{') == NULL
9137 && vim_strchr(theline, '}') == NULL
9138 && !cin_ends_in(theline, (char_u *)":", NULL)
9139 && !cin_ends_in(theline, (char_u *)",", NULL)
9140 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9141 cur_curpos.lnum + 1)
9142 && !cin_isterminated(theline, FALSE, TRUE))
9143 {
9144 amount = curbuf->b_ind_func_type;
9145 goto theend;
9146 }
9147
9148 /* search backwards until we find something we recognize */
9149 amount = 0;
9150 curwin->w_cursor = cur_curpos;
9151 while (curwin->w_cursor.lnum > 1)
9152 {
9153 curwin->w_cursor.lnum--;
9154 curwin->w_cursor.col = 0;
9155
9156 l = ml_get_curline();
9157
9158 /*
9159 * If we're in a comment or raw string now, skip to the start
9160 * of it.
9161 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009162 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009164 curwin->w_cursor.lnum = trypos->lnum + 1;
9165 curwin->w_cursor.col = 0;
9166 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 }
9168
9169 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009170 * Are we at the start of a cpp base class declaration or
9171 * constructor initialization?
9172 */ /* XXX */
9173 n = FALSE;
9174 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009176 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9177 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009179 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009181 /* XXX */
9182 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9183 break;
9184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009186 /*
9187 * Skip preprocessor directives and blank lines.
9188 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009189 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009190 continue;
9191
9192 if (cin_nocode(l))
9193 continue;
9194
9195 /*
9196 * If the previous line ends in ',', use one level of
9197 * indentation:
9198 * int foo,
9199 * bar;
9200 * do this before checking for '}' in case of eg.
9201 * enum foobar
9202 * {
9203 * ...
9204 * } foo,
9205 * bar;
9206 */
9207 n = 0;
9208 if (cin_ends_in(l, (char_u *)",", NULL)
9209 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9210 {
9211 /* take us back to opening paren */
9212 if (find_last_paren(l, '(', ')')
9213 && (trypos = find_match_paren(
9214 curbuf->b_ind_maxparen)) != NULL)
9215 curwin->w_cursor = *trypos;
9216
9217 /* For a line ending in ',' that is a continuation line go
9218 * back to the first line with a backslash:
9219 * char *foo = "bla\
9220 * bla",
9221 * here;
9222 */
9223 while (n == 0 && curwin->w_cursor.lnum > 1)
9224 {
9225 l = ml_get(curwin->w_cursor.lnum - 1);
9226 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9227 break;
9228 --curwin->w_cursor.lnum;
9229 curwin->w_cursor.col = 0;
9230 }
9231
9232 amount = get_indent(); /* XXX */
9233
9234 if (amount == 0)
9235 amount = cin_first_id_amount();
9236 if (amount == 0)
9237 amount = ind_continuation;
9238 break;
9239 }
9240
9241 /*
9242 * If the line looks like a function declaration, and we're
9243 * not in a comment, put it the left margin.
9244 */
9245 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9246 break;
9247 l = ml_get_curline();
9248
9249 /*
9250 * Finding the closing '}' of a previous function. Put
9251 * current line at the left margin. For when 'cino' has "fs".
9252 */
9253 if (*skipwhite(l) == '}')
9254 break;
9255
9256 /* (matching {)
9257 * If the previous line ends on '};' (maybe followed by
9258 * comments) align at column 0. For example:
9259 * char *string_array[] = { "foo",
9260 * / * x * / "b};ar" }; / * foobar * /
9261 */
9262 if (cin_ends_in(l, (char_u *)"};", NULL))
9263 break;
9264
9265 /*
9266 * If the previous line ends on '[' we are probably in an
9267 * array constant:
9268 * something = [
9269 * 234, <- extra indent
9270 */
9271 if (cin_ends_in(l, (char_u *)"[", NULL))
9272 {
9273 amount = get_indent() + ind_continuation;
9274 break;
9275 }
9276
9277 /*
9278 * Find a line only has a semicolon that belongs to a previous
9279 * line ending in '}', e.g. before an #endif. Don't increase
9280 * indent then.
9281 */
9282 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9283 {
9284 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285
9286 while (curwin->w_cursor.lnum > 1)
9287 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009288 look = ml_get(--curwin->w_cursor.lnum);
9289 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009290 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009292 }
9293 if (curwin->w_cursor.lnum > 0
9294 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009297 curwin->w_cursor = curpos_save;
9298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009300 /*
9301 * If the PREVIOUS line is a function declaration, the current
9302 * line (and the ones that follow) needs to be indented as
9303 * parameters.
9304 */
9305 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9306 {
9307 amount = curbuf->b_ind_param;
9308 break;
9309 }
9310
9311 /*
9312 * If the previous line ends in ';' and the line before the
9313 * previous line ends in ',' or '\', ident to column zero:
9314 * int foo,
9315 * bar;
9316 * indent_to_0 here;
9317 */
9318 if (cin_ends_in(l, (char_u *)";", NULL))
9319 {
9320 l = ml_get(curwin->w_cursor.lnum - 1);
9321 if (cin_ends_in(l, (char_u *)",", NULL)
9322 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9323 break;
9324 l = ml_get_curline();
9325 }
9326
9327 /*
9328 * Doesn't look like anything interesting -- so just
9329 * use the indent of this line.
9330 *
9331 * Position the cursor over the rightmost paren, so that
9332 * matching it will take us back to the start of the line.
9333 */
9334 find_last_paren(l, '(', ')');
9335
9336 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9337 curwin->w_cursor = *trypos;
9338 amount = get_indent(); /* XXX */
9339 break;
9340 }
9341
9342 /* add extra indent for a comment */
9343 if (cin_iscomment(theline))
9344 amount += curbuf->b_ind_comment;
9345
9346 /* add extra indent if the previous line ended in a backslash:
9347 * "asdfasdf\
9348 * here";
9349 * char *foo = "asdf\
9350 * here";
9351 */
9352 if (cur_curpos.lnum > 1)
9353 {
9354 l = ml_get(cur_curpos.lnum - 1);
9355 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9356 {
9357 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9358 if (cur_amount > 0)
9359 amount = cur_amount;
9360 else if (cur_amount == 0)
9361 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 }
9363 }
9364
9365theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009366 if (amount < 0)
9367 amount = 0;
9368
9369laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 /* put the cursor back where it belongs */
9371 curwin->w_cursor = cur_curpos;
9372
9373 vim_free(linecopy);
9374
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 return amount;
9376}
9377
9378 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009379find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381 char_u *look;
9382 pos_T *theirscope;
9383 char_u *mightbeif;
9384 int elselevel;
9385 int whilelevel;
9386
9387 if (lookfor == LOOKFOR_IF)
9388 {
9389 elselevel = 1;
9390 whilelevel = 0;
9391 }
9392 else
9393 {
9394 elselevel = 0;
9395 whilelevel = 1;
9396 }
9397
9398 curwin->w_cursor.col = 0;
9399
9400 while (curwin->w_cursor.lnum > ourscope + 1)
9401 {
9402 curwin->w_cursor.lnum--;
9403 curwin->w_cursor.col = 0;
9404
9405 look = cin_skipcomment(ml_get_curline());
9406 if (cin_iselse(look)
9407 || cin_isif(look)
9408 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009409 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 {
9411 /*
9412 * if we've gone outside the braces entirely,
9413 * we must be out of scope...
9414 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009415 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 if (theirscope == NULL)
9417 break;
9418
9419 /*
9420 * and if the brace enclosing this is further
9421 * back than the one enclosing the else, we're
9422 * out of luck too.
9423 */
9424 if (theirscope->lnum < ourscope)
9425 break;
9426
9427 /*
9428 * and if they're enclosed in a *deeper* brace,
9429 * then we can ignore it because it's in a
9430 * different scope...
9431 */
9432 if (theirscope->lnum > ourscope)
9433 continue;
9434
9435 /*
9436 * if it was an "else" (that's not an "else if")
9437 * then we need to go back to another if, so
9438 * increment elselevel
9439 */
9440 look = cin_skipcomment(ml_get_curline());
9441 if (cin_iselse(look))
9442 {
9443 mightbeif = cin_skipcomment(look + 4);
9444 if (!cin_isif(mightbeif))
9445 ++elselevel;
9446 continue;
9447 }
9448
9449 /*
9450 * if it was a "while" then we need to go back to
9451 * another "do", so increment whilelevel. XXX
9452 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009453 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 {
9455 ++whilelevel;
9456 continue;
9457 }
9458
9459 /* If it's an "if" decrement elselevel */
9460 look = cin_skipcomment(ml_get_curline());
9461 if (cin_isif(look))
9462 {
9463 elselevel--;
9464 /*
9465 * When looking for an "if" ignore "while"s that
9466 * get in the way.
9467 */
9468 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9469 whilelevel = 0;
9470 }
9471
9472 /* If it's a "do" decrement whilelevel */
9473 if (cin_isdo(look))
9474 whilelevel--;
9475
9476 /*
9477 * if we've used up all the elses, then
9478 * this must be the if that we want!
9479 * match the indent level of that if.
9480 */
9481 if (elselevel <= 0 && whilelevel <= 0)
9482 {
9483 return OK;
9484 }
9485 }
9486 }
9487 return FAIL;
9488}
9489
9490# if defined(FEAT_EVAL) || defined(PROTO)
9491/*
9492 * Get indent level from 'indentexpr'.
9493 */
9494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009495get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009497 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009498 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009499 pos_T save_pos;
9500 colnr_T save_curswant;
9501 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009503 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9504 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009506 /* Save and restore cursor position and curswant, in case it was changed
9507 * via :normal commands */
9508 save_pos = curwin->w_cursor;
9509 save_curswant = curwin->w_curswant;
9510 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009512 if (use_sandbox)
9513 ++sandbox;
9514 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009515
9516 /* Need to make a copy, the 'indentexpr' option could be changed while
9517 * evaluating it. */
9518 inde_copy = vim_strsave(curbuf->b_p_inde);
9519 if (inde_copy != NULL)
9520 {
9521 indent = (int)eval_to_number(inde_copy);
9522 vim_free(inde_copy);
9523 }
9524
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009525 if (use_sandbox)
9526 --sandbox;
9527 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
9529 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9530 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9531 * command. */
9532 save_State = State;
9533 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009534 curwin->w_cursor = save_pos;
9535 curwin->w_curswant = save_curswant;
9536 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 check_cursor();
9538 State = save_State;
9539
9540 /* If there is an error, just keep the current indent. */
9541 if (indent < 0)
9542 indent = get_indent();
9543
9544 return indent;
9545}
9546# endif
9547
9548#endif /* FEAT_CINDENT */
9549
9550#if defined(FEAT_LISP) || defined(PROTO)
9551
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009553lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 char_u buf[LSIZE];
9556 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009557 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
9559 while (*word != NUL)
9560 {
9561 (void)copy_option_part(&word, buf, LSIZE, ",");
9562 len = (int)STRLEN(buf);
9563 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9564 return TRUE;
9565 }
9566 return FALSE;
9567}
9568
9569/*
9570 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9571 * The incompatible newer method is quite a bit better at indenting
9572 * code in lisp-like languages than the traditional one; it's still
9573 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9574 *
9575 * TODO:
9576 * Findmatch() should be adapted for lisp, also to make showmatch
9577 * work correctly: now (v5.3) it seems all C/C++ oriented:
9578 * - it does not recognize the #\( and #\) notations as character literals
9579 * - it doesn't know about comments starting with a semicolon
9580 * - it incorrectly interprets '(' as a character literal
9581 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009582 * Update from Sergey Khorev:
9583 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 */
9585 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009586get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009588 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 int amount;
9590 char_u *that;
9591 colnr_T col;
9592 colnr_T firsttry;
9593 int parencount, quotecount;
9594 int vi_lisp;
9595
9596 /* Set vi_lisp to use the vi-compatible method */
9597 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9598
9599 realpos = curwin->w_cursor;
9600 curwin->w_cursor.col = 0;
9601
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009602 if ((pos = findmatch(NULL, '(')) == NULL)
9603 pos = findmatch(NULL, '[');
9604 else
9605 {
9606 paren = *pos;
9607 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009608 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009609 pos = &paren;
9610 }
9611 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 {
9613 /* Extra trick: Take the indent of the first previous non-white
9614 * line that is at the same () level. */
9615 amount = -1;
9616 parencount = 0;
9617
9618 while (--curwin->w_cursor.lnum >= pos->lnum)
9619 {
9620 if (linewhite(curwin->w_cursor.lnum))
9621 continue;
9622 for (that = ml_get_curline(); *that != NUL; ++that)
9623 {
9624 if (*that == ';')
9625 {
9626 while (*(that + 1) != NUL)
9627 ++that;
9628 continue;
9629 }
9630 if (*that == '\\')
9631 {
9632 if (*(that + 1) != NUL)
9633 ++that;
9634 continue;
9635 }
9636 if (*that == '"' && *(that + 1) != NUL)
9637 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009638 while (*++that && *that != '"')
9639 {
9640 /* skipping escaped characters in the string */
9641 if (*that == '\\')
9642 {
9643 if (*++that == NUL)
9644 break;
9645 if (that[1] == NUL)
9646 {
9647 ++that;
9648 break;
9649 }
9650 }
9651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009653 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009655 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 --parencount;
9657 }
9658 if (parencount == 0)
9659 {
9660 amount = get_indent();
9661 break;
9662 }
9663 }
9664
9665 if (amount == -1)
9666 {
9667 curwin->w_cursor.lnum = pos->lnum;
9668 curwin->w_cursor.col = pos->col;
9669 col = pos->col;
9670
9671 that = ml_get_curline();
9672
9673 if (vi_lisp && get_indent() == 0)
9674 amount = 2;
9675 else
9676 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009677 char_u *line = that;
9678
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 amount = 0;
9680 while (*that && col)
9681 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009682 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 col--;
9684 }
9685
9686 /*
9687 * Some keywords require "body" indenting rules (the
9688 * non-standard-lisp ones are Scheme special forms):
9689 *
9690 * (let ((a 1)) instead (let ((a 1))
9691 * (...)) of (...))
9692 */
9693
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009694 if (!vi_lisp && (*that == '(' || *that == '[')
9695 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 amount += 2;
9697 else
9698 {
9699 that++;
9700 amount++;
9701 firsttry = amount;
9702
Bram Moolenaar1c465442017-03-12 20:10:05 +01009703 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009705 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 ++that;
9707 }
9708
9709 if (*that && *that != ';') /* not a comment line */
9710 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009711 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009713 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 firsttry++;
9715
9716 parencount = 0;
9717 quotecount = 0;
9718
9719 if (vi_lisp
9720 || (*that != '"'
9721 && *that != '\''
9722 && *that != '#'
9723 && (*that < '0' || *that > '9')))
9724 {
9725 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009726 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 || quotecount
9728 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009729 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 && !quotecount
9731 && !parencount
9732 && vi_lisp)))
9733 {
9734 if (*that == '"')
9735 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009736 if ((*that == '(' || *that == '[')
9737 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009739 if ((*that == ')' || *that == ']')
9740 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 --parencount;
9742 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009743 amount += lbr_chartabsize_adv(
9744 line, &that, (colnr_T)amount);
9745 amount += lbr_chartabsize_adv(
9746 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 }
9748 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009749 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009751 amount += lbr_chartabsize(
9752 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 that++;
9754 }
9755 if (!*that || *that == ';')
9756 amount = firsttry;
9757 }
9758 }
9759 }
9760 }
9761 }
9762 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009763 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764
9765 curwin->w_cursor = realpos;
9766
9767 return amount;
9768}
9769#endif /* FEAT_LISP */
9770
9771 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009772prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009774#if defined(SIGHUP) && defined(SIG_IGN)
9775 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9776 * makes Vim exit and then handling SIGHUP causes various reentrance
9777 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009778 signal(SIGHUP, SIG_IGN);
9779#endif
9780
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#ifdef FEAT_GUI
9782 if (gui.in_use)
9783 {
9784 gui.dying = TRUE;
9785 out_trash(); /* trash any pending output */
9786 }
9787 else
9788#endif
9789 {
9790 windgoto((int)Rows - 1, 0);
9791
9792 /*
9793 * Switch terminal mode back now, so messages end up on the "normal"
9794 * screen (if there are two screens).
9795 */
9796 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009797 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 out_flush();
9799 }
9800}
9801
9802/*
9803 * Preserve files and exit.
9804 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009805 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9806 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 */
9808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009809preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811 buf_T *buf;
9812
9813 prepare_to_exit();
9814
Bram Moolenaar4770d092006-01-12 23:22:24 +00009815 /* Setting this will prevent free() calls. That avoids calling free()
9816 * recursively when free() was invoked with a bad pointer. */
9817 really_exiting = TRUE;
9818
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 out_str(IObuff);
9820 screen_start(); /* don't know where cursor is now */
9821 out_flush();
9822
9823 ml_close_notmod(); /* close all not-modified buffers */
9824
Bram Moolenaar29323592016-07-24 22:04:11 +02009825 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
9827 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9828 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009829 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 screen_start(); /* don't know where cursor is now */
9831 out_flush();
9832 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9833 break;
9834 }
9835 }
9836
9837 ml_close_all(FALSE); /* close all memfiles, without deleting */
9838
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009839 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
9841 getout(1);
9842}
9843
9844/*
9845 * return TRUE if "fname" exists.
9846 */
9847 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009848vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009850 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
9852 if (mch_stat((char *)fname, &st))
9853 return FALSE;
9854 return TRUE;
9855}
9856
9857/*
9858 * Check for CTRL-C pressed, but only once in a while.
9859 * Should be used instead of ui_breakcheck() for functions that check for
9860 * each line in the file. Calling ui_breakcheck() each time takes too much
9861 * time, because it can be a system call.
9862 */
9863
9864#ifndef BREAKCHECK_SKIP
9865# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9866# define BREAKCHECK_SKIP 200
9867# else
9868# define BREAKCHECK_SKIP 32
9869# endif
9870#endif
9871
9872static int breakcheck_count = 0;
9873
9874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009875line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
9877 if (++breakcheck_count >= BREAKCHECK_SKIP)
9878 {
9879 breakcheck_count = 0;
9880 ui_breakcheck();
9881 }
9882}
9883
9884/*
9885 * Like line_breakcheck() but check 10 times less often.
9886 */
9887 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009888fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
9890 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9891 {
9892 breakcheck_count = 0;
9893 ui_breakcheck();
9894 }
9895}
9896
9897/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009898 * Invoke expand_wildcards() for one pattern.
9899 * Expand items like "%:h" before the expansion.
9900 * Returns OK or FAIL.
9901 */
9902 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009903expand_wildcards_eval(
9904 char_u **pat, /* pointer to input pattern */
9905 int *num_file, /* resulting number of files */
9906 char_u ***file, /* array of resulting files */
9907 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009908{
9909 int ret = FAIL;
9910 char_u *eval_pat = NULL;
9911 char_u *exp_pat = *pat;
9912 char_u *ignored_msg;
9913 int usedlen;
9914
9915 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9916 {
9917 ++emsg_off;
9918 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9919 NULL, &ignored_msg, NULL);
9920 --emsg_off;
9921 if (eval_pat != NULL)
9922 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9923 }
9924
9925 if (exp_pat != NULL)
9926 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9927
9928 if (eval_pat != NULL)
9929 {
9930 vim_free(exp_pat);
9931 vim_free(eval_pat);
9932 }
9933
9934 return ret;
9935}
9936
9937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9939 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009940 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 */
9942 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009943expand_wildcards(
9944 int num_pat, /* number of input patterns */
9945 char_u **pat, /* array of input patterns */
9946 int *num_files, /* resulting number of files */
9947 char_u ***files, /* array of resulting files */
9948 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949{
9950 int retval;
9951 int i, j;
9952 char_u *p;
9953 int non_suf_match; /* number without matching suffix */
9954
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009955 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956
9957 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009958 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 return retval;
9960
9961#ifdef FEAT_WILDIGN
9962 /*
9963 * Remove names that match 'wildignore'.
9964 */
9965 if (*p_wig)
9966 {
9967 char_u *ffname;
9968
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009969 /* check all files in (*files)[] */
9970 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009972 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 if (ffname == NULL) /* out of memory */
9974 break;
9975# ifdef VMS
9976 vms_remove_version(ffname);
9977# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009978 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009980 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009981 vim_free((*files)[i]);
9982 for (j = i; j + 1 < *num_files; ++j)
9983 (*files)[j] = (*files)[j + 1];
9984 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 --i;
9986 }
9987 vim_free(ffname);
9988 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009989
9990 /* If the number of matches is now zero, we fail. */
9991 if (*num_files == 0)
9992 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009993 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009994 return FAIL;
9995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 }
9997#endif
9998
9999 /*
10000 * Move the names where 'suffixes' match to the end.
10001 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010002 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 {
10004 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010005 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010007 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 {
10009 /*
10010 * Move the name without matching suffix to the front
10011 * of the list.
10012 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010013 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010015 (*files)[j] = (*files)[j - 1];
10016 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 }
10018 }
10019 }
10020
10021 return retval;
10022}
10023
10024/*
10025 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10026 */
10027 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010028match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 int fnamelen, setsuflen;
10031 char_u *setsuf;
10032#define MAXSUFLEN 30 /* maximum length of a file suffix */
10033 char_u suf_buf[MAXSUFLEN];
10034
10035 fnamelen = (int)STRLEN(fname);
10036 setsuflen = 0;
10037 for (setsuf = p_su; *setsuf; )
10038 {
10039 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010040 if (setsuflen == 0)
10041 {
10042 char_u *tail = gettail(fname);
10043
10044 /* empty entry: match name without a '.' */
10045 if (vim_strchr(tail, '.') == NULL)
10046 {
10047 setsuflen = 1;
10048 break;
10049 }
10050 }
10051 else
10052 {
10053 if (fnamelen >= setsuflen
10054 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10055 (size_t)setsuflen) == 0)
10056 break;
10057 setsuflen = 0;
10058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060 return (setsuflen != 0);
10061}
10062
10063#if !defined(NO_EXPANDPATH) || defined(PROTO)
10064
10065# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010066static int vim_backtick(char_u *p);
10067static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068# endif
10069
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010070# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071/*
10072 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10073 * it's shared between these systems.
10074 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010075# if defined(PROTO)
10076# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077# else
10078# ifdef __BORLANDC__
10079# define _cdecl _RTLENTRYF
10080# endif
10081# endif
10082
10083/*
10084 * comparison function for qsort in dos_expandpath()
10085 */
10086 static int _cdecl
10087pstrcmp(const void *a, const void *b)
10088{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010089 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090}
10091
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010093 * Recursively expand one path component into all matching files and/or
10094 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 * Return the number of matches found.
10096 * "path" has backslashes before chars that are not to be expanded, starting
10097 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010098 * Return the number of matches found.
10099 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 */
10101 static int
10102dos_expandpath(
10103 garray_T *gap,
10104 char_u *path,
10105 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010106 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010107 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010109 char_u *buf;
10110 char_u *path_end;
10111 char_u *p, *s, *e;
10112 int start_len = gap->ga_len;
10113 char_u *pat;
10114 regmatch_T regmatch;
10115 int starts_with_dot;
10116 int matches;
10117 int len;
10118 int starstar = FALSE;
10119 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 WIN32_FIND_DATA fb;
10121 HANDLE hFind = (HANDLE)0;
10122# ifdef FEAT_MBYTE
10123 WIN32_FIND_DATAW wfb;
10124 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010127 int ok;
10128
10129 /* Expanding "**" may take a long time, check for CTRL-C. */
10130 if (stardepth > 0)
10131 {
10132 ui_breakcheck();
10133 if (got_int)
10134 return 0;
10135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010137 /* Make room for file name. When doing encoding conversion the actual
10138 * length may be quite a bit longer, thus use the maximum possible length. */
10139 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 if (buf == NULL)
10141 return 0;
10142
10143 /*
10144 * Find the first part in the path name that contains a wildcard or a ~1.
10145 * Copy it into buf, including the preceding characters.
10146 */
10147 p = buf;
10148 s = buf;
10149 e = NULL;
10150 path_end = path;
10151 while (*path_end != NUL)
10152 {
10153 /* May ignore a wildcard that has a backslash before it; it will
10154 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10155 if (path_end >= path + wildoff && rem_backslash(path_end))
10156 *p++ = *path_end++;
10157 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10158 {
10159 if (e != NULL)
10160 break;
10161 s = p + 1;
10162 }
10163 else if (path_end >= path + wildoff
10164 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10165 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010166# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 if (has_mbyte)
10168 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010169 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 STRNCPY(p, path_end, len);
10171 p += len;
10172 path_end += len;
10173 }
10174 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 *p++ = *path_end++;
10177 }
10178 e = p;
10179 *e = NUL;
10180
10181 /* now we have one wildcard component between s and e */
10182 /* Remove backslashes between "wildoff" and the start of the wildcard
10183 * component. */
10184 for (p = buf + wildoff; p < s; ++p)
10185 if (rem_backslash(p))
10186 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010187 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 --e;
10189 --s;
10190 }
10191
Bram Moolenaar231334e2005-07-25 20:46:57 +000010192 /* Check for "**" between "s" and "e". */
10193 for (p = s; p < e; ++p)
10194 if (p[0] == '*' && p[1] == '*')
10195 starstar = TRUE;
10196
Bram Moolenaard82103e2016-01-17 17:04:05 +010010197 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10199 if (pat == NULL)
10200 {
10201 vim_free(buf);
10202 return 0;
10203 }
10204
10205 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010206 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010207 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 regmatch.rm_ic = TRUE; /* Always ignore case */
10209 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010210 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010211 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 vim_free(pat);
10213
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010214 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 {
10216 vim_free(buf);
10217 return 0;
10218 }
10219
10220 /* remember the pattern or file name being looked for */
10221 matchname = vim_strsave(s);
10222
Bram Moolenaar231334e2005-07-25 20:46:57 +000010223 /* If "**" is by itself, this is the first time we encounter it and more
10224 * is following then find matches without any directory. */
10225 if (!didstar && stardepth < 100 && starstar && e - s == 2
10226 && *path_end == '/')
10227 {
10228 STRCPY(s, path_end + 1);
10229 ++stardepth;
10230 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10231 --stardepth;
10232 }
10233
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 /* Scan all files in the directory with "dir/ *.*" */
10235 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236# ifdef FEAT_MBYTE
10237 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10238 {
10239 /* The active codepage differs from 'encoding'. Attempt using the
10240 * wide function. If it fails because it is not implemented fall back
10241 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010242 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 if (wn != NULL)
10244 {
10245 hFind = FindFirstFileW(wn, &wfb);
10246 if (hFind == INVALID_HANDLE_VALUE
10247 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010248 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 }
10250 }
10251
10252 if (wn == NULL)
10253# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010254 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256
10257 while (ok)
10258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259# ifdef FEAT_MBYTE
10260 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010261 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 else
10263# endif
10264 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 /* Ignore entries starting with a dot, unless when asked for. Accept
10266 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010267 if ((p[0] != '.' || starts_with_dot
10268 || ((flags & EW_DODOT)
10269 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010271 || (regmatch.regprog != NULL
10272 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010273 || ((flags & EW_NOTWILD)
10274 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010278
10279 if (starstar && stardepth < 100)
10280 {
10281 /* For "**" in the pattern first go deeper in the tree to
10282 * find matches. */
10283 STRCPY(buf + len, "/**");
10284 STRCPY(buf + len + 3, path_end);
10285 ++stardepth;
10286 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10287 --stardepth;
10288 }
10289
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 STRCPY(buf + len, path_end);
10291 if (mch_has_exp_wildcard(path_end))
10292 {
10293 /* need to expand another component of the path */
10294 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010295 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 }
10297 else
10298 {
10299 /* no more wildcards, check if there is a match */
10300 /* remove backslashes for the remaining components only */
10301 if (*path_end != 0)
10302 backslash_halve(buf + len + 1);
10303 if (mch_getperm(buf) >= 0) /* add existing file */
10304 addfile(gap, buf, flags);
10305 }
10306 }
10307
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308# ifdef FEAT_MBYTE
10309 if (wn != NULL)
10310 {
10311 vim_free(p);
10312 ok = FindNextFileW(hFind, &wfb);
10313 }
10314 else
10315# endif
10316 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317
10318 /* If no more matches and no match was used, try expanding the name
10319 * itself. Finds the long name of a short filename. */
10320 if (!ok && matchname != NULL && gap->ga_len == start_len)
10321 {
10322 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 FindClose(hFind);
10324# ifdef FEAT_MBYTE
10325 if (wn != NULL)
10326 {
10327 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010328 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 if (wn != NULL)
10330 hFind = FindFirstFileW(wn, &wfb);
10331 }
10332 if (wn == NULL)
10333# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010334 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010336 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 }
10338 }
10339
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 FindClose(hFind);
10341# ifdef FEAT_MBYTE
10342 vim_free(wn);
10343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010345 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 vim_free(matchname);
10347
10348 matches = gap->ga_len - start_len;
10349 if (matches > 0)
10350 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10351 sizeof(char_u *), pstrcmp);
10352 return matches;
10353}
10354
10355 int
10356mch_expandpath(
10357 garray_T *gap,
10358 char_u *path,
10359 int flags) /* EW_* flags */
10360{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010361 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010363# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364
Bram Moolenaar231334e2005-07-25 20:46:57 +000010365#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10366 || defined(PROTO)
10367/*
10368 * Unix style wildcard expansion code.
10369 * It's here because it's used both for Unix and Mac.
10370 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010371 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010372pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010373{
10374 return (pathcmp(*(char **)a, *(char **)b, -1));
10375}
10376
10377/*
10378 * Recursively expand one path component into all matching files and/or
10379 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10380 * "path" has backslashes before chars that are not to be expanded, starting
10381 * at "path + wildoff".
10382 * Return the number of matches found.
10383 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10384 */
10385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010386unix_expandpath(
10387 garray_T *gap,
10388 char_u *path,
10389 int wildoff,
10390 int flags, /* EW_* flags */
10391 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010392{
10393 char_u *buf;
10394 char_u *path_end;
10395 char_u *p, *s, *e;
10396 int start_len = gap->ga_len;
10397 char_u *pat;
10398 regmatch_T regmatch;
10399 int starts_with_dot;
10400 int matches;
10401 int len;
10402 int starstar = FALSE;
10403 static int stardepth = 0; /* depth for "**" expansion */
10404
10405 DIR *dirp;
10406 struct dirent *dp;
10407
10408 /* Expanding "**" may take a long time, check for CTRL-C. */
10409 if (stardepth > 0)
10410 {
10411 ui_breakcheck();
10412 if (got_int)
10413 return 0;
10414 }
10415
10416 /* make room for file name */
10417 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10418 if (buf == NULL)
10419 return 0;
10420
10421 /*
10422 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010423 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010424 * Copy it into "buf", including the preceding characters.
10425 */
10426 p = buf;
10427 s = buf;
10428 e = NULL;
10429 path_end = path;
10430 while (*path_end != NUL)
10431 {
10432 /* May ignore a wildcard that has a backslash before it; it will
10433 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10434 if (path_end >= path + wildoff && rem_backslash(path_end))
10435 *p++ = *path_end++;
10436 else if (*path_end == '/')
10437 {
10438 if (e != NULL)
10439 break;
10440 s = p + 1;
10441 }
10442 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010443 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010444 || (!p_fic && (flags & EW_ICASE)
10445 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010446 e = p;
10447#ifdef FEAT_MBYTE
10448 if (has_mbyte)
10449 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010450 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010451 STRNCPY(p, path_end, len);
10452 p += len;
10453 path_end += len;
10454 }
10455 else
10456#endif
10457 *p++ = *path_end++;
10458 }
10459 e = p;
10460 *e = NUL;
10461
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010462 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010463 /* Remove backslashes between "wildoff" and the start of the wildcard
10464 * component. */
10465 for (p = buf + wildoff; p < s; ++p)
10466 if (rem_backslash(p))
10467 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010468 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010469 --e;
10470 --s;
10471 }
10472
10473 /* Check for "**" between "s" and "e". */
10474 for (p = s; p < e; ++p)
10475 if (p[0] == '*' && p[1] == '*')
10476 starstar = TRUE;
10477
10478 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010479 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010480 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10481 if (pat == NULL)
10482 {
10483 vim_free(buf);
10484 return 0;
10485 }
10486
10487 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010488 if (flags & EW_ICASE)
10489 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10490 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010491 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010492 if (flags & (EW_NOERROR | EW_NOTWILD))
10493 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010494 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010495 if (flags & (EW_NOERROR | EW_NOTWILD))
10496 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010497 vim_free(pat);
10498
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010499 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010500 {
10501 vim_free(buf);
10502 return 0;
10503 }
10504
10505 /* If "**" is by itself, this is the first time we encounter it and more
10506 * is following then find matches without any directory. */
10507 if (!didstar && stardepth < 100 && starstar && e - s == 2
10508 && *path_end == '/')
10509 {
10510 STRCPY(s, path_end + 1);
10511 ++stardepth;
10512 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10513 --stardepth;
10514 }
10515
10516 /* open the directory for scanning */
10517 *s = NUL;
10518 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10519
10520 /* Find all matching entries */
10521 if (dirp != NULL)
10522 {
10523 for (;;)
10524 {
10525 dp = readdir(dirp);
10526 if (dp == NULL)
10527 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010528 if ((dp->d_name[0] != '.' || starts_with_dot
10529 || ((flags & EW_DODOT)
10530 && dp->d_name[1] != NUL
10531 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010532 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10533 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010534 || ((flags & EW_NOTWILD)
10535 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010536 {
10537 STRCPY(s, dp->d_name);
10538 len = STRLEN(buf);
10539
10540 if (starstar && stardepth < 100)
10541 {
10542 /* For "**" in the pattern first go deeper in the tree to
10543 * find matches. */
10544 STRCPY(buf + len, "/**");
10545 STRCPY(buf + len + 3, path_end);
10546 ++stardepth;
10547 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10548 --stardepth;
10549 }
10550
10551 STRCPY(buf + len, path_end);
10552 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10553 {
10554 /* need to expand another component of the path */
10555 /* remove backslashes for the remaining components only */
10556 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10557 }
10558 else
10559 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010560 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010561
Bram Moolenaar231334e2005-07-25 20:46:57 +000010562 /* no more wildcards, check if there is a match */
10563 /* remove backslashes for the remaining components only */
10564 if (*path_end != NUL)
10565 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010566 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010567 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010568 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010569 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010570#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010571 size_t precomp_len = STRLEN(buf)+1;
10572 char_u *precomp_buf =
10573 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010574
Bram Moolenaar231334e2005-07-25 20:46:57 +000010575 if (precomp_buf)
10576 {
10577 mch_memmove(buf, precomp_buf, precomp_len);
10578 vim_free(precomp_buf);
10579 }
10580#endif
10581 addfile(gap, buf, flags);
10582 }
10583 }
10584 }
10585 }
10586
10587 closedir(dirp);
10588 }
10589
10590 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010591 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010592
10593 matches = gap->ga_len - start_len;
10594 if (matches > 0)
10595 qsort(((char_u **)gap->ga_data) + start_len, matches,
10596 sizeof(char_u *), pstrcmp);
10597 return matches;
10598}
10599#endif
10600
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010601#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010602/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010603 * Moves "*psep" back to the previous path separator in "path".
10604 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010605 */
10606 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010607find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010608{
10609 /* skip the current separator */
10610 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010611 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010612
10613 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010614 while (*psep > path)
10615 {
10616 if (vim_ispathsep(**psep))
10617 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010618 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010619 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010620
10621 return FAIL;
10622}
10623
10624/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010625 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10626 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010627 */
10628 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010629is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010630{
10631 int j;
10632 int candidate_len;
10633 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010634 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010635 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010636
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010637 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010638 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010639 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010641
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010642 candidate_len = (int)STRLEN(maybe_unique);
10643 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010645 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010646
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010647 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010648 if (fnamecmp(maybe_unique, rival) == 0
10649 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010650 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010651 }
10652
Bram Moolenaar162bd912010-07-28 22:29:10 +020010653 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010654}
10655
10656/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010657 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010658 * paths are expanded to their equivalent fullpath. This includes the "."
10659 * (relative to current buffer directory) and empty path (relative to current
10660 * directory) notations.
10661 *
10662 * TODO: handle upward search (;) and path limiter (**N) notations by
10663 * expanding each into their equivalent path(s).
10664 */
10665 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010666expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010667{
10668 char_u *path_option = *curbuf->b_p_path == NUL
10669 ? p_path : curbuf->b_p_path;
10670 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010671 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010672 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010673
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010674 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010675 return;
10676
10677 while (*path_option != NUL)
10678 {
10679 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10680
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010681 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010682 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010683 /* Relative to current buffer:
10684 * "/path/file" + "." -> "/path/"
10685 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010686 if (curbuf->b_ffname == NULL)
10687 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010688 p = gettail(curbuf->b_ffname);
10689 len = (int)(p - curbuf->b_ffname);
10690 if (len + (int)STRLEN(buf) >= MAXPATHL)
10691 continue;
10692 if (buf[1] == NUL)
10693 buf[len] = NUL;
10694 else
10695 STRMOVE(buf + len, buf + 2);
10696 mch_memmove(buf, curbuf->b_ffname, len);
10697 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010698 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010699 else if (buf[0] == NUL)
10700 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010701 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010702 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010703 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010704 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010705 else if (!mch_isFullName(buf))
10706 {
10707 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010708 len = (int)STRLEN(curdir);
10709 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010710 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010711 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010712 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010713 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010714 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010715 }
10716
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010717 if (ga_grow(gap, 1) == FAIL)
10718 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010719
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010720# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010721 /* Avoid the path ending in a backslash, it fails when a comma is
10722 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010723 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010724 if (buf[len - 1] == '\\')
10725 buf[len - 1] = '/';
10726# endif
10727
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010728 p = vim_strsave(buf);
10729 if (p == NULL)
10730 break;
10731 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010732 }
10733
10734 vim_free(buf);
10735}
10736
10737/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010738 * Returns a pointer to the file or directory name in "fname" that matches the
10739 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010740 *
10741 * path: /foo/bar/baz
10742 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010743 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010744 */
10745 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010746get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010747{
10748 int i;
10749 int maxlen = 0;
10750 char_u **path_part = (char_u **)gap->ga_data;
10751 char_u *cutoff = NULL;
10752
10753 for (i = 0; i < gap->ga_len; i++)
10754 {
10755 int j = 0;
10756
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010757 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010758# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010759 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10760#endif
10761 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010762 j++;
10763 if (j > maxlen)
10764 {
10765 maxlen = j;
10766 cutoff = &fname[j];
10767 }
10768 }
10769
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010770 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010771 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010772 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010773 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010774
10775 return cutoff;
10776}
10777
10778/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010779 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10780 * that they are unique with respect to each other while conserving the part
10781 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010782 */
10783 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010784uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010785{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010786 int i;
10787 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010788 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010789 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010790 char_u *pat;
10791 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010792 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010793 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010794 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010795 char_u **in_curdir = NULL;
10796 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010797
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010798 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010799 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010800
10801 /*
10802 * We need to prepend a '*' at the beginning of file_pattern so that the
10803 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010804 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010805 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010806 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010807 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010808 if (file_pattern == NULL)
10809 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010810 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010811 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010812 STRCAT(file_pattern, pattern);
10813 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10814 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010815 if (pat == NULL)
10816 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010817
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010818 regmatch.rm_ic = TRUE; /* always ignore case */
10819 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10820 vim_free(pat);
10821 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010822 return;
10823
Bram Moolenaar162bd912010-07-28 22:29:10 +020010824 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010825 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010826 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010827 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010828
10829 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010830 if (in_curdir == NULL)
10831 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010832
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010833 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010834 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010835 char_u *path = fnames[i];
10836 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010837 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010838 char_u *pathsep_p;
10839 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010840
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010841 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010842 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010843 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010844 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010845 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010846
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010847 /* Shorten the filename while maintaining its uniqueness */
10848 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010849
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010850 /* Don't assume all files can be reached without path when search
10851 * pattern starts with star star slash, so only remove path_cutoff
10852 * when possible. */
10853 if (pattern[0] == '*' && pattern[1] == '*'
10854 && vim_ispathsep_nocolon(pattern[2])
10855 && path_cutoff != NULL
10856 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10857 && is_unique(path_cutoff, gap, i))
10858 {
10859 sort_again = TRUE;
10860 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10861 }
10862 else
10863 {
10864 /* Here all files can be reached without path, so get shortest
10865 * unique path. We start at the end of the path. */
10866 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010867
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010868 while (find_previous_pathsep(path, &pathsep_p))
10869 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10870 && is_unique(pathsep_p + 1, gap, i)
10871 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10872 {
10873 sort_again = TRUE;
10874 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10875 break;
10876 }
10877 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010878
10879 if (mch_isFullName(path))
10880 {
10881 /*
10882 * Last resort: shorten relative to curdir if possible.
10883 * 'possible' means:
10884 * 1. It is under the current directory.
10885 * 2. The result is actually shorter than the original.
10886 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010887 * Before curdir After
10888 * /foo/bar/file.txt /foo/bar ./file.txt
10889 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10890 * /file.txt / /file.txt
10891 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010892 */
10893 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010894 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010895#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010896 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010897 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010898 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010899 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010900 && !vim_ispathsep(*short_name)
10901#endif
10902 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010903 {
10904 STRCPY(path, ".");
10905 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010906 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010907 }
10908 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010909 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010910 }
10911
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010912 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010913 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010914 {
10915 char_u *rel_path;
10916 char_u *path = in_curdir[i];
10917
10918 if (path == NULL)
10919 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010920
10921 /* If the {filename} is not unique, change it to ./{filename}.
10922 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010923 short_name = shorten_fname(path, curdir);
10924 if (short_name == NULL)
10925 short_name = path;
10926 if (is_unique(short_name, gap, i))
10927 {
10928 STRCPY(fnames[i], short_name);
10929 continue;
10930 }
10931
10932 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10933 if (rel_path == NULL)
10934 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010935 STRCPY(rel_path, ".");
10936 add_pathsep(rel_path);
10937 STRCAT(rel_path, short_name);
10938
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010939 vim_free(fnames[i]);
10940 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010941 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010942 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010943 }
10944
Bram Moolenaar162bd912010-07-28 22:29:10 +020010945theend:
10946 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010947 if (in_curdir != NULL)
10948 {
10949 for (i = 0; i < gap->ga_len; i++)
10950 vim_free(in_curdir[i]);
10951 vim_free(in_curdir);
10952 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010953 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010954 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010955
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010956 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010957 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010958}
10959
10960/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010961 * Calls globpath() with 'path' values for the given pattern and stores the
10962 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010963 * Returns the total number of matches.
10964 */
10965 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010966expand_in_path(
10967 garray_T *gap,
10968 char_u *pattern,
10969 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010970{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010971 char_u *curdir;
10972 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010973 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010974 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010975
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010976 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010977 return 0;
10978 mch_dirname(curdir, MAXPATHL);
10979
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010980 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010981 expand_path_option(curdir, &path_ga);
10982 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010983 if (path_ga.ga_len == 0)
10984 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010985
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010986 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010987 ga_clear_strings(&path_ga);
10988 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010989 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010990
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010991 if (flags & EW_ICASE)
10992 glob_flags |= WILD_ICASE;
10993 if (flags & EW_ADDSLASH)
10994 glob_flags |= WILD_ADD_SLASH;
10995 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010996 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010997
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010998 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010999}
11000#endif
11001
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011002#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11003/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011004 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11005 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011006 */
11007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011008remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011009{
11010 int i;
11011 int j;
11012 char_u **fnames = (char_u **)gap->ga_data;
11013
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011014 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011015 for (i = gap->ga_len - 1; i > 0; --i)
11016 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11017 {
11018 vim_free(fnames[i]);
11019 for (j = i + 1; j < gap->ga_len; ++j)
11020 fnames[j - 1] = fnames[j];
11021 --gap->ga_len;
11022 }
11023}
11024#endif
11025
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011026/*
11027 * Return TRUE if "p" contains what looks like an environment variable.
11028 * Allowing for escaping.
11029 */
11030 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011031has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011032{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011033 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011034 {
11035 if (*p == '\\' && p[1] != NUL)
11036 ++p;
11037 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011038#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011039 "$%"
11040#else
11041 "$"
11042#endif
11043 , *p) != NULL)
11044 return TRUE;
11045 }
11046 return FALSE;
11047}
11048
11049#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011050/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011051 * Return TRUE if "p" contains a special wildcard character, one that Vim
11052 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011053 */
11054 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011055has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011056{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011057 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011058 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011059 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011060 if (*p == '\\' && p[1] != NUL)
11061 ++p;
11062 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11063 return TRUE;
11064 }
11065 return FALSE;
11066}
11067#endif
11068
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069/*
11070 * Generic wildcard expansion code.
11071 *
11072 * Characters in "pat" that should not be expanded must be preceded with a
11073 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11074 *
11075 * Return FAIL when no single file was found. In this case "num_file" is not
11076 * set, and "file" may contain an error message.
11077 * Return OK when some files found. "num_file" is set to the number of
11078 * matches, "file" to the array of matches. Call FreeWild() later.
11079 */
11080 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011081gen_expand_wildcards(
11082 int num_pat, /* number of input patterns */
11083 char_u **pat, /* array of input patterns */
11084 int *num_file, /* resulting number of files */
11085 char_u ***file, /* array of resulting files */
11086 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087{
11088 int i;
11089 garray_T ga;
11090 char_u *p;
11091 static int recursive = FALSE;
11092 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011093 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011094#if defined(FEAT_SEARCHPATH)
11095 int did_expand_in_path = FALSE;
11096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
11098 /*
11099 * expand_env() is called to expand things like "~user". If this fails,
11100 * it calls ExpandOne(), which brings us back here. In this case, always
11101 * call the machine specific expansion function, if possible. Otherwise,
11102 * return FAIL.
11103 */
11104 if (recursive)
11105#ifdef SPECIAL_WILDCHAR
11106 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11107#else
11108 return FAIL;
11109#endif
11110
11111#ifdef SPECIAL_WILDCHAR
11112 /*
11113 * If there are any special wildcard characters which we cannot handle
11114 * here, call machine specific function for all the expansion. This
11115 * avoids starting the shell for each argument separately.
11116 * For `=expr` do use the internal function.
11117 */
11118 for (i = 0; i < num_pat; i++)
11119 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011120 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121# ifdef VIM_BACKTICK
11122 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11123# endif
11124 )
11125 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11126 }
11127#endif
11128
11129 recursive = TRUE;
11130
11131 /*
11132 * The matching file names are stored in a growarray. Init it empty.
11133 */
11134 ga_init2(&ga, (int)sizeof(char_u *), 30);
11135
11136 for (i = 0; i < num_pat; ++i)
11137 {
11138 add_pat = -1;
11139 p = pat[i];
11140
11141#ifdef VIM_BACKTICK
11142 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011145 if (add_pat == -1)
11146 retval = FAIL;
11147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 else
11149#endif
11150 {
11151 /*
11152 * First expand environment variables, "~/" and "~user/".
11153 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011154 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011156 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 if (p == NULL)
11158 p = pat[i];
11159#ifdef UNIX
11160 /*
11161 * On Unix, if expand_env() can't expand an environment
11162 * variable, use the shell to do that. Discard previously
11163 * found file names and start all over again.
11164 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011165 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 {
11167 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011168 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011170 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 recursive = FALSE;
11172 return i;
11173 }
11174#endif
11175 }
11176
11177 /*
11178 * If there are wildcards: Expand file names and add each match to
11179 * the list. If there is no match, and EW_NOTFOUND is given, add
11180 * the pattern.
11181 * If there are no wildcards: Add the file name if it exists or
11182 * when EW_NOTFOUND is given.
11183 */
11184 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011185 {
11186#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011187 if ((flags & EW_PATH)
11188 && !mch_isFullName(p)
11189 && !(p[0] == '.'
11190 && (vim_ispathsep(p[1])
11191 || (p[1] == '.' && vim_ispathsep(p[2]))))
11192 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011193 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011194 /* :find completion where 'path' is used.
11195 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011196 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011197 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011198 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011199 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011200 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011201 else
11202#endif
11203 add_pat = mch_expandpath(&ga, p, flags);
11204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 }
11206
11207 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11208 {
11209 char_u *t = backslash_halve_save(p);
11210
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11212 * "vim c:/" work. */
11213 if (flags & EW_NOTFOUND)
11214 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 addfile(&ga, t, flags);
11217 vim_free(t);
11218 }
11219
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011220#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011221 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011222 uniquefy_paths(&ga, p);
11223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 if (p != pat[i])
11225 vim_free(p);
11226 }
11227
11228 *num_file = ga.ga_len;
11229 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11230
11231 recursive = FALSE;
11232
Bram Moolenaar336bd622016-01-17 18:23:58 +010011233 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234}
11235
11236# ifdef VIM_BACKTICK
11237
11238/*
11239 * Return TRUE if we can expand this backtick thing here.
11240 */
11241 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011242vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243{
11244 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11245}
11246
11247/*
11248 * Expand an item in `backticks` by executing it as a command.
11249 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011250 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 */
11252 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011253expand_backtick(
11254 garray_T *gap,
11255 char_u *pat,
11256 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
11258 char_u *p;
11259 char_u *cmd;
11260 char_u *buffer;
11261 int cnt = 0;
11262 int i;
11263
11264 /* Create the command: lop off the backticks. */
11265 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11266 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011267 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268
11269#ifdef FEAT_EVAL
11270 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011271 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 else
11273#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011274 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011275 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 vim_free(cmd);
11277 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011278 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279
11280 cmd = buffer;
11281 while (*cmd != NUL)
11282 {
11283 cmd = skipwhite(cmd); /* skip over white space */
11284 p = cmd;
11285 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11286 ++p;
11287 /* add an entry if it is not empty */
11288 if (p > cmd)
11289 {
11290 i = *p;
11291 *p = NUL;
11292 addfile(gap, cmd, flags);
11293 *p = i;
11294 ++cnt;
11295 }
11296 cmd = p;
11297 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11298 ++cmd;
11299 }
11300
11301 vim_free(buffer);
11302 return cnt;
11303}
11304# endif /* VIM_BACKTICK */
11305
11306/*
11307 * Add a file to a file list. Accepted flags:
11308 * EW_DIR add directories
11309 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011310 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 * EW_NOTFOUND add even when it doesn't exist
11312 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011313 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 */
11315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011316addfile(
11317 garray_T *gap,
11318 char_u *f, /* filename */
11319 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320{
11321 char_u *p;
11322 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011323 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011325 /* if the file/dir/link doesn't exist, may not add it */
11326 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011327 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 return;
11329
11330#ifdef FNAME_ILLEGAL
11331 /* if the file/dir contains illegal characters, don't add it */
11332 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11333 return;
11334#endif
11335
11336 isdir = mch_isdir(f);
11337 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11338 return;
11339
Bram Moolenaarb5971142015-03-21 17:32:19 +010011340 /* If the file isn't executable, may not add it. Do accept directories.
11341 * When invoked from expand_shellcmd() do not use $PATH. */
11342 if (!isdir && (flags & EW_EXEC)
11343 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011344 return;
11345
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 /* Make room for another item in the file list. */
11347 if (ga_grow(gap, 1) == FAIL)
11348 return;
11349
11350 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11351 if (p == NULL)
11352 return;
11353
11354 STRCPY(p, f);
11355#ifdef BACKSLASH_IN_FILENAME
11356 slash_adjust(p);
11357#endif
11358 /*
11359 * Append a slash or backslash after directory names if none is present.
11360 */
11361#ifndef DONT_ADD_PATHSEP_TO_DIR
11362 if (isdir && (flags & EW_ADDSLASH))
11363 add_pathsep(p);
11364#endif
11365 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366}
11367#endif /* !NO_EXPANDPATH */
11368
11369#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11370
11371#ifndef SEEK_SET
11372# define SEEK_SET 0
11373#endif
11374#ifndef SEEK_END
11375# define SEEK_END 2
11376#endif
11377
11378/*
11379 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011380 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11381 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 * Returns an allocated string, or NULL for error.
11383 */
11384 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011385get_cmd_output(
11386 char_u *cmd,
11387 char_u *infile, /* optional input file name */
11388 int flags, /* can be SHELL_SILENT */
11389 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390{
11391 char_u *tempname;
11392 char_u *command;
11393 char_u *buffer = NULL;
11394 int len;
11395 int i = 0;
11396 FILE *fd;
11397
11398 if (check_restricted() || check_secure())
11399 return NULL;
11400
11401 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011402 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 {
11404 EMSG(_(e_notmp));
11405 return NULL;
11406 }
11407
11408 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011409 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 if (command == NULL)
11411 goto done;
11412
11413 /*
11414 * Call the shell to execute the command (errors are ignored).
11415 * Don't check timestamps here.
11416 */
11417 ++no_check_timestamps;
11418 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11419 --no_check_timestamps;
11420
11421 vim_free(command);
11422
11423 /*
11424 * read the names from the file into memory
11425 */
11426# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011427 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 fd = mch_fopen((char *)tempname, "r");
11429# else
11430 fd = mch_fopen((char *)tempname, READBIN);
11431# endif
11432
11433 if (fd == NULL)
11434 {
11435 EMSG2(_(e_notopen), tempname);
11436 goto done;
11437 }
11438
11439 fseek(fd, 0L, SEEK_END);
11440 len = ftell(fd); /* get size of temp file */
11441 fseek(fd, 0L, SEEK_SET);
11442
11443 buffer = alloc(len + 1);
11444 if (buffer != NULL)
11445 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11446 fclose(fd);
11447 mch_remove(tempname);
11448 if (buffer == NULL)
11449 goto done;
11450#ifdef VMS
11451 len = i; /* VMS doesn't give us what we asked for... */
11452#endif
11453 if (i != len)
11454 {
11455 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011456 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011458 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011459 {
11460 /* Change NUL into SOH, otherwise the string is truncated. */
11461 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011462 if (buffer[i] == NUL)
11463 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011464
Bram Moolenaar162bd912010-07-28 22:29:10 +020011465 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011466 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011467 else
11468 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469
11470done:
11471 vim_free(tempname);
11472 return buffer;
11473}
11474#endif
11475
11476/*
11477 * Free the list of files returned by expand_wildcards() or other expansion
11478 * functions.
11479 */
11480 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011481FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011483 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 while (count--)
11486 vim_free(files[count]);
11487 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488}
11489
11490/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011491 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 * Don't do this when still processing a command or a mapping.
11493 * Don't do this when inside a ":normal" command.
11494 */
11495 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011496goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497{
11498 return (p_im && stuff_empty() && typebuf_typed());
11499}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011500
11501/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011502 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011503 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11504 * - Remove any argument. E.g., "csh -f" -> "csh".
11505 * But don't allow a space in the path, so that this works:
11506 * "/usr/bin/csh --rcfile ~/.cshrc"
11507 * But don't do that for Windows, it's common to have a space in the path.
11508 */
11509 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011510get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011511{
11512 char_u *p;
11513
11514#ifdef WIN3264
11515 p = gettail(p_sh);
11516 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11517#else
11518 p = skiptowhite(p_sh);
11519 if (*p == NUL)
11520 {
11521 /* No white space, use the tail. */
11522 p = vim_strsave(gettail(p_sh));
11523 }
11524 else
11525 {
11526 char_u *p1, *p2;
11527
11528 /* Find the last path separator before the space. */
11529 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011530 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011531 if (vim_ispathsep(*p2))
11532 p1 = p2 + 1;
11533 p = vim_strnsave(p1, (int)(p - p1));
11534 }
11535#endif
11536 return p;
11537}