blob: 1eac5189aeda2b1ca33329d356d89341ac84a069 [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 Moolenaar4f974752019-02-17 17:44:42 +010017#if defined(FEAT_CMDL_COMPL) && defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020018# 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 Moolenaar5fd0f502019-02-13 23:13:28 +010024#define URL_SLASH 1 /* path_is_url() has found "://" */
25#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
26
Bram Moolenaar24305862012-08-15 14:05:05 +020027/* All user names (for ~user completion as done by shell). */
28#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
29static garray_T ga_users;
30#endif
31
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
33 * Count the size (in window cells) of the indent in the current line.
34 */
35 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010036get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000037{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020038#ifdef FEAT_VARTABS
39 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
40 curbuf->b_p_vts_array, FALSE);
41#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020042 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044}
45
46/*
47 * Count the size (in window cells) of the indent in line "lnum".
48 */
49 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010050get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020052#ifdef FEAT_VARTABS
53 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
54 curbuf->b_p_vts_array, FALSE);
55#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020056 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000058}
59
60#if defined(FEAT_FOLDING) || defined(PROTO)
61/*
62 * Count the size (in window cells) of the indent in line "lnum" of buffer
63 * "buf".
64 */
65 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010066get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020068#ifdef FEAT_VARTABS
69 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
70 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
71#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020072 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000074}
75#endif
76
77/*
78 * count the size (in window cells) of the indent in line "ptr", with
79 * 'tabstop' at "ts"
80 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010082get_indent_str(
83 char_u *ptr,
84 int ts,
85 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 int count = 0;
88
89 for ( ; *ptr; ++ptr)
90 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020091 if (*ptr == TAB)
92 {
93 if (!list || lcs_tab1) /* count a tab for what it is worth */
94 count += ts - (count % ts);
95 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020096 /* In list mode, when tab is not set, count screen char width
97 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020098 count += ptr2cells(ptr);
99 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 else if (*ptr == ' ')
101 ++count; /* count a space for one */
102 else
103 break;
104 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000105 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106}
107
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200108#ifdef FEAT_VARTABS
109/*
110 * Count the size (in window cells) of the indent in line "ptr", using
111 * variable tabstops.
112 * if "list" is TRUE, count only screen size for tabs.
113 */
114 int
115get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
116{
117 int count = 0;
118
119 for ( ; *ptr; ++ptr)
120 {
121 if (*ptr == TAB) /* count a tab for what it is worth */
122 {
123 if (!list || lcs_tab1)
124 count += tabstop_padding(count, ts, vts);
125 else
126 /* In list mode, when tab is not set, count screen char width
127 * for Tab, displays: ^I */
128 count += ptr2cells(ptr);
129 }
130 else if (*ptr == ' ')
131 ++count; /* count a space for one */
132 else
133 break;
134 }
135 return count;
136}
137#endif
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * Set the indent of the current line.
141 * Leaves the cursor on the first non-blank in the line.
142 * Caller must take care of undo.
143 * "flags":
144 * SIN_CHANGED: call changed_bytes() if the line was changed.
145 * SIN_INSERT: insert the indent in front of the line.
146 * SIN_UNDO: save line for undo before changing it.
147 * Returns TRUE if the line was changed.
148 */
149 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100150set_indent(
151 int size, /* measured in spaces */
152 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153{
154 char_u *p;
155 char_u *newline;
156 char_u *oldline;
157 char_u *s;
158 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000159 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 int line_len;
161 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000162 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200163#ifdef FEAT_VARTABS
164 int ind_col = 0;
165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000167 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000168 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000169 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170
171 /*
172 * First check if there is anything to do and compute the number of
173 * characters needed for the indent.
174 */
175 todo = size;
176 ind_len = 0;
177 p = oldline = ml_get_curline();
178
179 /* Calculate the buffer size for the new indent, and check to see if it
180 * isn't already set */
181
Bram Moolenaar5002c292007-07-24 13:26:15 +0000182 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
183 * 'preserveindent' are set count the number of characters at the
184 * beginning of the line to be copied */
185 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 {
187 /* If 'preserveindent' is set then reuse as much as possible of
188 * the existing indent structure for the new indent */
189 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
190 {
191 ind_done = 0;
192
193 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100194 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {
196 if (*p == TAB)
197 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200198#ifdef FEAT_VARTABS
199 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
200 curbuf->b_p_vts_array);
201#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 tab_pad = (int)curbuf->b_p_ts
203 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 /* stop if this tab will overshoot the target */
206 if (todo < tab_pad)
207 break;
208 todo -= tab_pad;
209 ++ind_len;
210 ind_done += tab_pad;
211 }
212 else
213 {
214 --todo;
215 ++ind_len;
216 ++ind_done;
217 }
218 ++p;
219 }
220
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200221#ifdef FEAT_VARTABS
222 /* These diverge from this point. */
223 ind_col = ind_done;
224#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000225 /* Set initial number of whitespace chars to copy if we are
226 * preserving indent but expandtab is set */
227 if (curbuf->b_p_et)
228 orig_char_len = ind_len;
229
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200231#ifdef FEAT_VARTABS
232 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
233 curbuf->b_p_vts_array);
234#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200236#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000237 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {
239 doit = TRUE;
240 todo -= tab_pad;
241 ++ind_len;
242 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200243#ifdef FEAT_VARTABS
244 ind_col += tab_pad;
245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 }
247 }
248
249 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200250#ifdef FEAT_VARTABS
251 for (;;)
252 {
253 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
254 curbuf->b_p_vts_array);
255 if (todo < tab_pad)
256 break;
257 if (*p != TAB)
258 doit = TRUE;
259 else
260 ++p;
261 todo -= tab_pad;
262 ++ind_len;
263 ind_col += tab_pad;
264 }
265#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 while (todo >= (int)curbuf->b_p_ts)
267 {
268 if (*p != TAB)
269 doit = TRUE;
270 else
271 ++p;
272 todo -= (int)curbuf->b_p_ts;
273 ++ind_len;
274 /* ind_done += (int)curbuf->b_p_ts; */
275 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 }
278 /* count spaces required for indent */
279 while (todo > 0)
280 {
281 if (*p != ' ')
282 doit = TRUE;
283 else
284 ++p;
285 --todo;
286 ++ind_len;
287 /* ++ind_done; */
288 }
289
290 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100291 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 return FALSE;
293
294 /* Allocate memory for the new line. */
295 if (flags & SIN_INSERT)
296 p = oldline;
297 else
298 p = skipwhite(p);
299 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000300
301 /* If 'preserveindent' and 'expandtab' are both set keep the original
302 * characters and allocate accordingly. We will fill the rest with spaces
303 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000304 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000305 {
306 newline = alloc(orig_char_len + size - ind_done + line_len);
307 if (newline == NULL)
308 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000309 todo = size - ind_done;
310 ind_len = orig_char_len + todo; /* Set total length of indent in
311 * characters, which may have been
312 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000313 p = oldline;
314 s = newline;
315 while (orig_char_len > 0)
316 {
317 *s++ = *p++;
318 orig_char_len--;
319 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000320
Bram Moolenaar5002c292007-07-24 13:26:15 +0000321 /* Skip over any additional white space (useful when newindent is less
322 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100323 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000324 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000325
Bram Moolenaar5002c292007-07-24 13:26:15 +0000326 }
327 else
328 {
329 todo = size;
330 newline = alloc(ind_len + line_len);
331 if (newline == NULL)
332 return FALSE;
333 s = newline;
334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
336 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 /* if 'expandtab' isn't set: use TABs */
338 if (!curbuf->b_p_et)
339 {
340 /* If 'preserveindent' is set then reuse as much as possible of
341 * the existing indent structure for the new indent */
342 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
343 {
344 p = oldline;
345 ind_done = 0;
346
Bram Moolenaar1c465442017-03-12 20:10:05 +0100347 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {
349 if (*p == TAB)
350 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200351#ifdef FEAT_VARTABS
352 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
353 curbuf->b_p_vts_array);
354#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 tab_pad = (int)curbuf->b_p_ts
356 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* stop if this tab will overshoot the target */
359 if (todo < tab_pad)
360 break;
361 todo -= tab_pad;
362 ind_done += tab_pad;
363 }
364 else
365 {
366 --todo;
367 ++ind_done;
368 }
369 *s++ = *p++;
370 }
371
372 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200373#ifdef FEAT_VARTABS
374 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
375 curbuf->b_p_vts_array);
376#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (todo >= tab_pad)
380 {
381 *s++ = TAB;
382 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200383#ifdef FEAT_VARTABS
384 ind_done += tab_pad;
385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
387
388 p = skipwhite(p);
389 }
390
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200391#ifdef FEAT_VARTABS
392 for (;;)
393 {
394 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
395 curbuf->b_p_vts_array);
396 if (todo < tab_pad)
397 break;
398 *s++ = TAB;
399 todo -= tab_pad;
400 ind_done += tab_pad;
401 }
402#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 while (todo >= (int)curbuf->b_p_ts)
404 {
405 *s++ = TAB;
406 todo -= (int)curbuf->b_p_ts;
407 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410 while (todo > 0)
411 {
412 *s++ = ' ';
413 --todo;
414 }
415 mch_memmove(s, p, (size_t)line_len);
416
Bram Moolenaar663bc892019-01-08 23:07:24 +0100417 // Replace the line (unless undo fails).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
419 {
420 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
421 if (flags & SIN_CHANGED)
422 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar663bc892019-01-08 23:07:24 +0100423
424 // Correct saved cursor position if it is in this line.
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200425 if (saved_cursor.lnum == curwin->w_cursor.lnum)
426 {
427 if (saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100428 // cursor was after the indent, adjust for the number of
429 // bytes added/removed
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200430 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
431 else if (saved_cursor.col >= (colnr_T)(s - newline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100432 // cursor was in the indent, and is now after it, put it back
433 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200434 saved_cursor.col = (colnr_T)(s - newline);
435 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100436#ifdef FEAT_TEXT_PROP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200437 {
438 int added = ind_len - (colnr_T)(p - oldline);
439
440 // When increasing indent this behaves like spaces were inserted at
441 // the old indent, when decreasing indent it behaves like spaces
442 // were deleted at the new indent.
443 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200444 (colnr_T)(added > 0 ? (p - oldline) : ind_len), added, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200445 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100446#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000447 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 }
449 else
450 vim_free(newline);
451
452 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000453 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454}
455
456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 * Return the indent of the current line after a number. Return -1 if no
458 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000459 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 */
461 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100462get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 colnr_T col;
465 pos_T pos;
466
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200467 regmatch_T regmatch;
468 int lead_len = 0; /* length of comment leader */
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (lnum > curbuf->b_ml.ml_line_count)
471 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000472 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473
474#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200475 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
476 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200477 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000478#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200479 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
480 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200481 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200482 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200483
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200484 /* vim_regexec() expects a pointer to a line. This lets us
485 * start matching for the flp beyond any comment leader... */
486 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200487 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200488 pos.lnum = lnum;
489 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200490 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200491 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200492 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200493 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000494
495 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 getvcol(curwin, &pos, &col, NULL, NULL);
498 return (int)col;
499}
500
Bram Moolenaar597a4222014-06-25 14:39:50 +0200501#if defined(FEAT_LINEBREAK) || defined(PROTO)
502/*
503 * Return appropriate space number for breakindent, taking influencing
504 * parameters into account. Window must be specified, since it is not
505 * necessarily always the current one.
506 */
507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100508get_breakindent_win(
509 win_T *wp,
510 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200511{
512 static int prev_indent = 0; /* cached indent value */
513 static long prev_ts = 0L; /* cached tabstop value */
514 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100515 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200516#ifdef FEAT_VARTABS
517 static int *prev_vts = NULL; /* cached vartabs values */
518#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200519 int bri = 0;
520 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200521 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200522 - ((wp->w_p_nu || wp->w_p_rnu)
523 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
524 ? number_width(wp) + 1 : 0);
525
526 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200527 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200528 || prev_tick != CHANGEDTICK(wp->w_buffer)
529#ifdef FEAT_VARTABS
530 || prev_vts != wp->w_buffer->b_p_vts_array
531#endif
532 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200533 {
534 prev_line = line;
535 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100536 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200537#ifdef FEAT_VARTABS
538 prev_vts = wp->w_buffer->b_p_vts_array;
539 prev_indent = get_indent_str_vtab(line,
540 (int)wp->w_buffer->b_p_ts,
541 wp->w_buffer->b_p_vts_array, wp->w_p_list);
542#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200543 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200544 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200545#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200546 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200547 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200548
549 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200550 if (wp->w_p_brisbr)
551 bri -= vim_strsize(p_sbr);
552
553 /* Add offset for number column, if 'n' is in 'cpoptions' */
554 bri += win_col_off2(wp);
555
556 /* never indent past left window margin */
557 if (bri < 0)
558 bri = 0;
559 /* always leave at least bri_min characters on the left,
560 * if text width is sufficient */
561 else if (bri > eff_wwidth - wp->w_p_brimin)
562 bri = (eff_wwidth - wp->w_p_brimin < 0)
563 ? 0 : eff_wwidth - wp->w_p_brimin;
564
565 return bri;
566}
567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#if defined(FEAT_COMMENTS) || defined(PROTO)
570/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200571 * get_leader_len() returns the length in bytes of the prefix of the given
572 * string which introduces a comment. If this string is not a comment then
573 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 * When "flags" is not NULL, it is set to point to the flags of the recognized
575 * comment leader.
576 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +0200577 * If "include_space" is set, include trailing whitespace while calculating the
578 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 */
580 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100581get_leader_len(
582 char_u *line,
583 char_u **flags,
584 int backward,
585 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +0200588 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 int got_com = FALSE;
590 int found_one;
591 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
592 char_u *string; /* pointer to comment string */
593 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +0200594 int middle_match_len = 0;
595 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +0200596 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar81340392012-06-06 16:12:59 +0200598 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100599 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 ++i;
601
602 /*
603 * Repeat to match several nested comment strings.
604 */
Bram Moolenaara4271d52011-05-10 13:38:27 +0200605 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 /*
608 * scan through the 'comments' option for a match
609 */
610 found_one = FALSE;
611 for (list = curbuf->b_p_com; *list; )
612 {
Bram Moolenaara4271d52011-05-10 13:38:27 +0200613 /* Get one option part into part_buf[]. Advance "list" to next
614 * one. Put "string" at start of string. */
615 if (!got_com && flags != NULL)
616 *flags = list; /* remember where flags started */
617 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
619 string = vim_strchr(part_buf, ':');
620 if (string == NULL) /* missing ':', ignore this part */
621 continue;
622 *string++ = NUL; /* isolate flags from string */
623
Bram Moolenaara4271d52011-05-10 13:38:27 +0200624 /* If we found a middle match previously, use that match when this
625 * is not a middle or end. */
626 if (middle_match_len != 0
627 && vim_strchr(part_buf, COM_MIDDLE) == NULL
628 && vim_strchr(part_buf, COM_END) == NULL)
629 break;
630
631 /* When we already found a nested comment, only accept further
632 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
634 continue;
635
Bram Moolenaara4271d52011-05-10 13:38:27 +0200636 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
638 continue;
639
Bram Moolenaara4271d52011-05-10 13:38:27 +0200640 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 * When string starts with white space, must have some white space
642 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +0200643 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100644 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100646 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200647 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100648 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 ++string;
650 }
651 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
652 ;
653 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +0200654 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaara4271d52011-05-10 13:38:27 +0200656 /* When 'b' flag used, there must be white space or an
657 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100659 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 continue;
661
Bram Moolenaara4271d52011-05-10 13:38:27 +0200662 /* We have found a match, stop searching unless this is a middle
663 * comment. The middle comment can be a substring of the end
664 * comment in which case it's better to return the length of the
665 * end comment and its flags. Thus we keep searching with middle
666 * and end matches and use an end match if it matches better. */
667 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
668 {
669 if (middle_match_len == 0)
670 {
671 middle_match_len = j;
672 saved_flags = prev_list;
673 }
674 continue;
675 }
676 if (middle_match_len != 0 && j > middle_match_len)
677 /* Use this match instead of the middle match, since it's a
678 * longer thus better match. */
679 middle_match_len = 0;
680
681 if (middle_match_len == 0)
682 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 found_one = TRUE;
684 break;
685 }
686
Bram Moolenaara4271d52011-05-10 13:38:27 +0200687 if (middle_match_len != 0)
688 {
689 /* Use the previously found middle match after failing to find a
690 * match with an end. */
691 if (!got_com && flags != NULL)
692 *flags = saved_flags;
693 i += middle_match_len;
694 found_one = TRUE;
695 }
696
697 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (!found_one)
699 break;
700
Bram Moolenaar81340392012-06-06 16:12:59 +0200701 result = i;
702
Bram Moolenaara4271d52011-05-10 13:38:27 +0200703 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100704 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ++i;
706
Bram Moolenaar81340392012-06-06 16:12:59 +0200707 if (include_space)
708 result = i;
709
Bram Moolenaara4271d52011-05-10 13:38:27 +0200710 /* If this comment doesn't nest, stop here. */
711 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (vim_strchr(part_buf, COM_NEST) == NULL)
713 break;
714 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200715 return result;
716}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200717
Bram Moolenaar81340392012-06-06 16:12:59 +0200718/*
719 * Return the offset at which the last comment in line starts. If there is no
720 * comment in the whole line, -1 is returned.
721 *
722 * When "flags" is not null, it is set to point to the flags describing the
723 * recognized comment leader.
724 */
725 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200727{
728 int result = -1;
729 int i, j;
730 int lower_check_bound = 0;
731 char_u *string;
732 char_u *com_leader;
733 char_u *com_flags;
734 char_u *list;
735 int found_one;
736 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
737
738 /*
739 * Repeat to match several nested comment strings.
740 */
741 i = (int)STRLEN(line);
742 while (--i >= lower_check_bound)
743 {
744 /*
745 * scan through the 'comments' option for a match
746 */
747 found_one = FALSE;
748 for (list = curbuf->b_p_com; *list; )
749 {
750 char_u *flags_save = list;
751
752 /*
753 * Get one option part into part_buf[]. Advance list to next one.
754 * put string at start of string.
755 */
756 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
757 string = vim_strchr(part_buf, ':');
758 if (string == NULL) /* If everything is fine, this cannot actually
759 * happen. */
Bram Moolenaar81340392012-06-06 16:12:59 +0200760 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200761 *string++ = NUL; /* Isolate flags from string. */
762 com_leader = string;
763
764 /*
765 * Line contents and string must match.
766 * When string starts with white space, must have some white space
767 * (but the amount does not need to match, there might be a mix of
768 * TABs and spaces).
769 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100770 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200771 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100772 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200773 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100774 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200775 ++string;
776 }
777 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
778 /* do nothing */;
779 if (string[j] != NUL)
780 continue;
781
782 /*
783 * When 'b' flag used, there must be white space or an
784 * end-of-line after the string in the line.
785 */
786 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100787 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200788 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100789
Bram Moolenaar4af72592018-12-09 15:00:52 +0100790 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100791 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100792 // For a middlepart comment, only consider it to match if
793 // everything before the current position in the line is
794 // whitespace. Otherwise we would think we are inside a
795 // comment if the middle part appears somewhere in the middle
796 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100797 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
798 ;
799 if (j < i)
800 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200801 }
802
803 /*
804 * We have found a match, stop searching.
805 */
806 found_one = TRUE;
807
808 if (flags)
809 *flags = flags_save;
810 com_flags = flags_save;
811
812 break;
813 }
814
815 if (found_one)
816 {
817 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
818 int len1, len2, off;
819
820 result = i;
821 /*
822 * If this comment nests, continue searching.
823 */
824 if (vim_strchr(part_buf, COM_NEST) != NULL)
825 continue;
826
827 lower_check_bound = i;
828
829 /* Let's verify whether the comment leader found is a substring
830 * of other comment leaders. If it is, let's adjust the
831 * lower_check_bound so that we make sure that we have determined
832 * the comment leader correctly.
833 */
834
Bram Moolenaar1c465442017-03-12 20:10:05 +0100835 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200836 ++com_leader;
837 len1 = (int)STRLEN(com_leader);
838
839 for (list = curbuf->b_p_com; *list; )
840 {
841 char_u *flags_save = list;
842
843 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
844 if (flags_save == com_flags)
845 continue;
846 string = vim_strchr(part_buf2, ':');
847 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100848 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200849 ++string;
850 len2 = (int)STRLEN(string);
851 if (len2 == 0)
852 continue;
853
854 /* Now we have to verify whether string ends with a substring
855 * beginning the com_leader. */
856 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
857 {
858 --off;
859 if (!STRNCMP(string + off, com_leader, len2 - off))
860 {
861 if (i - off < lower_check_bound)
862 lower_check_bound = i - off;
863 }
864 }
865 }
866 }
867 }
868 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870#endif
871
872/*
873 * Return the number of window lines occupied by buffer line "lnum".
874 */
875 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100876plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
878 return plines_win(curwin, lnum, TRUE);
879}
880
881 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100882plines_win(
883 win_T *wp,
884 linenr_T lnum,
885 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
887#if defined(FEAT_DIFF) || defined(PROTO)
888 /* Check for filler lines above this buffer line. When folded the result
889 * is one line anyway. */
890 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
891}
892
893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100894plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 return plines_win_nofill(curwin, lnum, TRUE);
897}
898
899 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100900plines_win_nofill(
901 win_T *wp,
902 linenr_T lnum,
903 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905#endif
906 int lines;
907
908 if (!wp->w_p_wrap)
909 return 1;
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (wp->w_width == 0)
912 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
914#ifdef FEAT_FOLDING
915 /* A folded lines is handled just like an empty line. */
916 /* NOTE: Caller must handle lines that are MAYBE folded. */
917 if (lineFolded(wp, lnum) == TRUE)
918 return 1;
919#endif
920
921 lines = plines_win_nofold(wp, lnum);
922 if (winheight > 0 && lines > wp->w_height)
923 return (int)wp->w_height;
924 return lines;
925}
926
927/*
928 * Return number of window lines physical line "lnum" will occupy in window
929 * "wp". Does not care about folding, 'wrap' or 'diff'.
930 */
931 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100932plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 char_u *s;
935 long col;
936 int width;
937
938 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
939 if (*s == NUL) /* empty line */
940 return 1;
941 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
942
943 /*
944 * If list mode is on, then the '$' at the end of the line may take up one
945 * extra column.
946 */
947 if (wp->w_p_list && lcs_eol != NUL)
948 col += 1;
949
950 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200951 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200953 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (width <= 0)
955 return 32000;
956 if (col <= width)
957 return 1;
958 col -= width;
959 width += win_col_off2(wp);
960 return (col + (width - 1)) / width + 1;
961}
962
963/*
964 * Like plines_win(), but only reports the number of physical screen lines
965 * used from the start of the line to the given column number.
966 */
967 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100968plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
970 long col;
971 char_u *s;
972 int lines = 0;
973 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200974 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976#ifdef FEAT_DIFF
977 /* Check for filler lines above this buffer line. When folded the result
978 * is one line anyway. */
979 lines = diff_check_fill(wp, lnum);
980#endif
981
982 if (!wp->w_p_wrap)
983 return lines + 1;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (wp->w_width == 0)
986 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar597a4222014-06-25 14:39:50 +0200988 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 col = 0;
991 while (*s != NUL && --column >= 0)
992 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200993 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100994 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996
997 /*
998 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
999 * INSERT mode, then col must be adjusted so that it represents the last
1000 * screen position of the TAB. This only fixes an error when the TAB wraps
1001 * from one screen line to the next (when 'columns' is not a multiple of
1002 * 'ts') -- webb.
1003 */
1004 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02001005 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001008 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 */
Bram Moolenaar02631462017-09-22 15:20:32 +02001010 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00001011 if (width <= 0)
1012 return 9999;
1013
1014 lines += 1;
1015 if (col > width)
1016 lines += (col - width) / (width + win_col_off2(wp)) + 1;
1017 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018}
1019
1020 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001021plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 int count = 0;
1024
1025 while (first <= last)
1026 {
1027#ifdef FEAT_FOLDING
1028 int x;
1029
1030 /* Check if there are any really folded lines, but also included lines
1031 * that are maybe folded. */
1032 x = foldedCount(wp, first, NULL);
1033 if (x > 0)
1034 {
1035 ++count; /* count 1 for "+-- folded" line */
1036 first += x;
1037 }
1038 else
1039#endif
1040 {
1041#ifdef FEAT_DIFF
1042 if (first == wp->w_topline)
1043 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
1044 else
1045#endif
1046 count += plines_win(wp, first, TRUE);
1047 ++first;
1048 }
1049 }
1050 return (count);
1051}
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001054gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001056 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001058 /* When searching columns is sometimes put at the end of a line. */
1059 if (pos->col == MAXCOL)
1060 return NUL;
1061 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (has_mbyte)
1063 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 return (int)*ptr;
1065}
1066
1067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001068gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (has_mbyte)
1071 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return (int)*ml_get_cursor();
1073}
1074
1075/*
1076 * Write a character at the current cursor position.
1077 * It is directly written into the block.
1078 */
1079 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001080pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
1083 + curwin->w_cursor.col) = c;
1084}
1085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086/*
1087 * When extra == 0: Return TRUE if the cursor is before or on the first
1088 * non-blank in the line.
1089 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1090 * the line.
1091 */
1092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001093inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 char_u *ptr;
1096 colnr_T col;
1097
Bram Moolenaar1c465442017-03-12 20:10:05 +01001098 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 ++ptr;
1100 if (col >= curwin->w_cursor.col + extra)
1101 return TRUE;
1102 else
1103 return FALSE;
1104}
1105
1106/*
1107 * Skip to next part of an option argument: Skip space and comma.
1108 */
1109 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001110skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 if (*p == ',')
1113 ++p;
1114 while (*p == ' ')
1115 ++p;
1116 return p;
1117}
1118
1119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 * check_status: called when the status bars for the buffer 'buf'
1121 * need to be updated
1122 */
1123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001124check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
1126 win_T *wp;
1127
Bram Moolenaar29323592016-07-24 22:04:11 +02001128 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (wp->w_buffer == buf && wp->w_status_height)
1130 {
1131 wp->w_redr_status = TRUE;
1132 if (must_redraw < VALID)
1133 must_redraw = VALID;
1134 }
1135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 * Ask for a reply from the user, a 'y' or a 'n'.
1139 * No other characters are accepted, the message is repeated until a valid
1140 * reply is entered or CTRL-C is hit.
1141 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
1142 * from any buffers but directly from the user.
1143 *
1144 * return the 'y' or 'n'
1145 */
1146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001147ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149 int r = ' ';
1150 int save_State = State;
1151
1152 if (exiting) /* put terminal in raw mode for this question */
1153 settmode(TMODE_RAW);
1154 ++no_wait_return;
1155#ifdef USE_ON_FLY_SCROLL
1156 dont_scroll = TRUE; /* disallow scrolling here */
1157#endif
1158 State = CONFIRM; /* mouse behaves like with :confirm */
1159#ifdef FEAT_MOUSE
1160 setmouse(); /* disables mouse for xterm */
1161#endif
1162 ++no_mapping;
1163 ++allow_keys; /* no mapping here, but recognize keys */
1164
1165 while (r != 'y' && r != 'n')
1166 {
1167 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001168 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (direct)
1170 r = get_keystroke();
1171 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00001172 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (r == Ctrl_C || r == ESC)
1174 r = 'n';
1175 msg_putchar(r); /* show what you typed */
1176 out_flush();
1177 }
1178 --no_wait_return;
1179 State = save_State;
1180#ifdef FEAT_MOUSE
1181 setmouse();
1182#endif
1183 --no_mapping;
1184 --allow_keys;
1185
1186 return r;
1187}
1188
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001189#if defined(FEAT_MOUSE) || defined(PROTO)
1190/*
1191 * Return TRUE if "c" is a mouse key.
1192 */
1193 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001194is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001195{
1196 return c == K_LEFTMOUSE
1197 || c == K_LEFTMOUSE_NM
1198 || c == K_LEFTDRAG
1199 || c == K_LEFTRELEASE
1200 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001201 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001202 || c == K_MIDDLEMOUSE
1203 || c == K_MIDDLEDRAG
1204 || c == K_MIDDLERELEASE
1205 || c == K_RIGHTMOUSE
1206 || c == K_RIGHTDRAG
1207 || c == K_RIGHTRELEASE
1208 || c == K_MOUSEDOWN
1209 || c == K_MOUSEUP
1210 || c == K_MOUSELEFT
1211 || c == K_MOUSERIGHT
1212 || c == K_X1MOUSE
1213 || c == K_X1DRAG
1214 || c == K_X1RELEASE
1215 || c == K_X2MOUSE
1216 || c == K_X2DRAG
1217 || c == K_X2RELEASE;
1218}
1219#endif
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221/*
1222 * Get a key stroke directly from the user.
1223 * Ignores mouse clicks and scrollbar events, except a click for the left
1224 * button (used at the more prompt).
1225 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
1226 * Disadvantage: typeahead is ignored.
1227 * Translates the interrupt character for unix to ESC.
1228 */
1229 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001230get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001232 char_u *buf = NULL;
1233 int buflen = 150;
1234 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 int len = 0;
1236 int n;
1237 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001238 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 mapped_ctrl_c = FALSE; /* mappings are not used here */
1241 for (;;)
1242 {
1243 cursor_on();
1244 out_flush();
1245
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001246 /* Leave some room for check_termcode() to insert a key code into (max
1247 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
1248 * bytes. */
1249 maxlen = (buflen - 6 - len) / 3;
1250 if (buf == NULL)
1251 buf = alloc(buflen);
1252 else if (maxlen < 10)
1253 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001254 char_u *t_buf = buf;
1255
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001256 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001257 * escape sequence. */
1258 buflen += 100;
1259 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001260 if (buf == NULL)
1261 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001262 maxlen = (buflen - 6 - len) / 3;
1263 }
1264 if (buf == NULL)
1265 {
1266 do_outofmem_msg((long_u)buflen);
1267 return ESC; /* panic! */
1268 }
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001271 * terminal code to complete. */
1272 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (n > 0)
1274 {
1275 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02001276 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001278 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00001280 else if (len > 0)
1281 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Bram Moolenaar4395a712006-09-05 18:57:57 +00001283 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001284 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00001285 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001287
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001288 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001289 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001290 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001291 {
1292 /* Redrawing was postponed, do it now. */
1293 update_screen(0);
1294 setcursor(); /* put cursor back where it belongs */
1295 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001296 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001297 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001298 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001300 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 continue;
1302
1303 /* Handle modifier and/or special key code. */
1304 n = buf[0];
1305 if (n == K_SPECIAL)
1306 {
1307 n = TO_SPECIAL(buf[1], buf[2]);
1308 if (buf[1] == KS_MODIFIER
1309 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001310#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001311 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001312#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001313#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 || n == K_VER_SCROLLBAR
1315 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316#endif
1317 )
1318 {
1319 if (buf[1] == KS_MODIFIER)
1320 mod_mask = buf[2];
1321 len -= 3;
1322 if (len > 0)
1323 mch_memmove(buf, buf + 3, (size_t)len);
1324 continue;
1325 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00001326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (has_mbyte)
1329 {
1330 if (MB_BYTE2LEN(n) > len)
1331 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001332 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 n = (*mb_ptr2char)(buf);
1334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#ifdef UNIX
1336 if (n == intr_char)
1337 n = ESC;
1338#endif
1339 break;
1340 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001341 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 mapped_ctrl_c = save_mapped_ctrl_c;
1344 return n;
1345}
1346
1347/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001348 * Get a number from the user.
1349 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 */
1351 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001352get_number(
1353 int colon, /* allow colon to abort */
1354 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 int n = 0;
1357 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001358 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001360 if (mouse_used != NULL)
1361 *mouse_used = FALSE;
1362
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 /* When not printing messages, the user won't know what to type, return a
1364 * zero (as if CR was hit). */
1365 if (msg_silent != 0)
1366 return 0;
1367
1368#ifdef USE_ON_FLY_SCROLL
1369 dont_scroll = TRUE; /* disallow scrolling here */
1370#endif
1371 ++no_mapping;
1372 ++allow_keys; /* no mapping here, but recognize keys */
1373 for (;;)
1374 {
1375 windgoto(msg_row, msg_col);
1376 c = safe_vgetc();
1377 if (VIM_ISDIGIT(c))
1378 {
1379 n = n * 10 + c - '0';
1380 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001381 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1384 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001385 if (typed > 0)
1386 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001387 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001388 --typed;
1389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001392#ifdef FEAT_MOUSE
1393 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1394 {
1395 *mouse_used = TRUE;
1396 n = mouse_row + 1;
1397 break;
1398 }
1399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 else if (n == 0 && c == ':' && colon)
1401 {
1402 stuffcharReadbuff(':');
1403 if (!exmode_active)
1404 cmdline_row = msg_row;
1405 skip_redraw = TRUE; /* skip redraw once */
1406 do_redraw = FALSE;
1407 break;
1408 }
1409 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
1410 break;
1411 }
1412 --no_mapping;
1413 --allow_keys;
1414 return n;
1415}
1416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001417/*
1418 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001419 * When "mouse_used" is not NULL allow using the mouse and in that case return
1420 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001421 */
1422 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001423prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001424{
1425 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001426 int save_cmdline_row;
1427 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001428
1429 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001430 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001431 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001432 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001433 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001434
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001435 // Set the state such that text can be selected/copied/pasted and we still
1436 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1437 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001438 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001439 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001440 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001441 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02001442#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001443 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001444 setmouse();
1445#endif
1446
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001447 i = get_number(TRUE, mouse_used);
1448 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001450 /* don't call wait_return() now */
1451 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001452 cmdline_row = msg_row - 1;
1453 need_wait_return = FALSE;
1454 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00001455 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001456 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001457 else
1458 cmdline_row = save_cmdline_row;
1459 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02001460#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001461 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001462 setmouse();
1463#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001465 return i;
1466}
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001469msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 long pn;
1472
1473 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1475 return;
1476
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001477 /* We don't want to overwrite another important message, but do overwrite
1478 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1479 * then "put" reports the last action. */
1480 if (keep_msg != NULL && !keep_msg_more)
1481 return;
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (n > 0)
1484 pn = n;
1485 else
1486 pn = -n;
1487
1488 if (pn > p_report)
1489 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001490 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001491 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001492 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001494 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001495 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001497 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1498 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (msg(msg_buf))
1500 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001501 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001502 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 }
1505}
1506
1507/*
1508 * flush map and typeahead buffers and give a warning for an error
1509 */
1510 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001511beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 if (emsg_silent == 0)
1514 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001515 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001516 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518}
1519
1520/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001521 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
1523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001524vim_beep(
1525 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001527#ifdef FEAT_EVAL
1528 called_vim_beep = TRUE;
1529#endif
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if (emsg_silent == 0)
1532 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001533 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1534 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001535#ifdef ELAPSED_FUNC
1536 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001537 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001538
1539 /* Only beep once per half a second, otherwise a sequence of beeps
1540 * would freeze Vim. */
1541 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1542 {
1543 did_init = TRUE;
1544 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001546 if (p_vb
1547#ifdef FEAT_GUI
1548 /* While the GUI is starting up the termcap is set for
1549 * the GUI but the output still goes to a terminal. */
1550 && !(gui.in_use && gui.starting)
1551#endif
1552 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001553 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001554 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001555#ifdef FEAT_VTP
1556 /* No restore color information, refresh the screen. */
1557 if (has_vtp_working() != 0
1558# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001559 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001560# endif
1561 )
1562 {
1563 redraw_later(CLEAR);
1564 update_screen(0);
1565 redrawcmd();
1566 }
1567#endif
1568 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001569 else
1570 out_char(BELL);
1571#ifdef ELAPSED_FUNC
1572 }
1573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001575
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001576 /* When 'debug' contains "beep" produce a message. If we are sourcing
1577 * a script or executing a function give the user a hint where the beep
1578 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001579 if (vim_strchr(p_debug, 'e') != NULL)
1580 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001581 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001582 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585}
1586
1587/*
1588 * To get the "real" home directory:
1589 * - get value of $HOME
1590 * For Unix:
1591 * - go to that directory
1592 * - do mch_dirname() to get the real name of that directory.
1593 * This also works with mounts and links.
1594 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001595 * For Windows:
1596 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 */
1598static char_u *homedir = NULL;
1599
1600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001601init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 char_u *var;
1604
Bram Moolenaar05159a02005-02-26 23:04:13 +00001605 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001606 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608#ifdef VMS
1609 var = mch_getenv((char_u *)"SYS$LOGIN");
1610#else
1611 var = mch_getenv((char_u *)"HOME");
1612#endif
1613
Bram Moolenaar4f974752019-02-17 17:44:42 +01001614#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001616 * Typically, $HOME is not defined on Windows, unless the user has
1617 * specifically defined it for Vim's sake. However, on Windows NT
1618 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1619 * each user. Try constructing $HOME from these.
1620 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001621 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001622 {
1623 char_u *homedrive, *homepath;
1624
1625 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1626 homepath = mch_getenv((char_u *)"HOMEPATH");
1627 if (homepath == NULL || *homepath == NUL)
1628 homepath = (char_u *)"\\";
1629 if (homedrive != NULL
1630 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1631 {
1632 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1633 if (NameBuff[0] != NUL)
1634 var = NameBuff;
1635 }
1636 }
1637
1638 if (var == NULL)
1639 var = mch_getenv((char_u *)"USERPROFILE");
1640
1641 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 * Weird but true: $HOME may contain an indirect reference to another
1643 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1644 * when $HOME is being set.
1645 */
1646 if (var != NULL && *var == '%')
1647 {
1648 char_u *p;
1649 char_u *exp;
1650
1651 p = vim_strchr(var + 1, '%');
1652 if (p != NULL)
1653 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001654 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 exp = mch_getenv(NameBuff);
1656 if (exp != NULL && *exp != NUL
1657 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1658 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001659 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 }
1663 }
1664
Bram Moolenaar48340b62017-08-29 22:08:53 +02001665 if (var != NULL && *var == NUL) /* empty is same as not set */
1666 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar05159a02005-02-26 23:04:13 +00001668 if (enc_utf8 && var != NULL)
1669 {
1670 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001671 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001672
1673 /* Convert from active codepage to UTF-8. Other conversions are
1674 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001675 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001676 if (pp != NULL)
1677 {
1678 homedir = pp;
1679 return;
1680 }
1681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 /*
1684 * Default home dir is C:/
1685 * Best assumption we can make in such a situation.
1686 */
1687 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001688 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (var != NULL)
1692 {
1693#ifdef UNIX
1694 /*
1695 * Change to the directory and get the actual path. This resolves
1696 * links. Don't do it when we can't return.
1697 */
1698 if (mch_dirname(NameBuff, MAXPATHL) == OK
1699 && mch_chdir((char *)NameBuff) == 0)
1700 {
1701 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1702 var = IObuff;
1703 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001704 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
1706#endif
1707 homedir = vim_strsave(var);
1708 }
1709}
1710
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001711#if defined(EXITFREE) || defined(PROTO)
1712 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001713free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001714{
1715 vim_free(homedir);
1716}
Bram Moolenaar24305862012-08-15 14:05:05 +02001717
1718# ifdef FEAT_CMDL_COMPL
1719 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001720free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001721{
1722 ga_clear_strings(&ga_users);
1723}
1724# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001725#endif
1726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001728 * Call expand_env() and store the result in an allocated string.
1729 * This is not very memory efficient, this expects the result to be freed
1730 * again soon.
1731 */
1732 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001733expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001734{
1735 return expand_env_save_opt(src, FALSE);
1736}
1737
1738/*
1739 * Idem, but when "one" is TRUE handle the string as one file name, only
1740 * expand "~" at the start.
1741 */
1742 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001743expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001744{
1745 char_u *p;
1746
1747 p = alloc(MAXPATHL);
1748 if (p != NULL)
1749 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1750 return p;
1751}
1752
1753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 * Expand environment variable with path name.
1755 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001756 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 * If anything fails no expansion is done and dst equals src.
1758 */
1759 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760expand_env(
1761 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1762 char_u *dst, /* where to put the result */
1763 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001765 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
1768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001769expand_env_esc(
1770 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1771 char_u *dst, /* where to put the result */
1772 int dstlen, /* maximum length of the result */
1773 int esc, /* escape spaces in expanded variables */
1774 int one, /* "srcp" is one file name */
1775 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001777 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 char_u *tail;
1779 int c;
1780 char_u *var;
1781 int copy_char;
1782 int mustfree; /* var was allocated, need to free it later */
1783 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001784 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001786 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001787 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001788
1789 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 --dstlen; /* leave one char space for "\," */
1791 while (*src && dstlen > 0)
1792 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001793#ifdef FEAT_EVAL
1794 /* Skip over `=expr`. */
1795 if (src[0] == '`' && src[1] == '=')
1796 {
1797 size_t len;
1798
1799 var = src;
1800 src += 2;
1801 (void)skip_expr(&src);
1802 if (*src == '`')
1803 ++src;
1804 len = src - var;
1805 if (len > (size_t)dstlen)
1806 len = dstlen;
1807 vim_strncpy(dst, var, len);
1808 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001809 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001810 continue;
1811 }
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001814 if ((*src == '$'
1815#ifdef VMS
1816 && at_start
1817#endif
1818 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001819#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 || *src == '%'
1821#endif
1822 || (*src == '~' && at_start))
1823 {
1824 mustfree = FALSE;
1825
1826 /*
1827 * The variable name is copied into dst temporarily, because it may
1828 * be a string in read-only memory and a NUL needs to be appended.
1829 */
1830 if (*src != '~') /* environment var */
1831 {
1832 tail = src + 1;
1833 var = dst;
1834 c = dstlen - 1;
1835
1836#ifdef UNIX
1837 /* Unix has ${var-name} type environment vars */
1838 if (*tail == '{' && !vim_isIDc('{'))
1839 {
1840 tail++; /* ignore '{' */
1841 while (c-- > 0 && *tail && *tail != '}')
1842 *var++ = *tail++;
1843 }
1844 else
1845#endif
1846 {
1847 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001848#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 || (*src == '%' && *tail != '%')
1850#endif
1851 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001855#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856# ifdef UNIX
1857 if (src[1] == '{' && *tail != '}')
1858# else
1859 if (*src == '%' && *tail != '%')
1860# endif
1861 var = NULL;
1862 else
1863 {
1864# ifdef UNIX
1865 if (src[1] == '{')
1866# else
1867 if (*src == '%')
1868#endif
1869 ++tail;
1870#endif
1871 *var = NUL;
1872 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001873#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875#endif
1876 }
1877 /* home directory */
1878 else if ( src[1] == NUL
1879 || vim_ispathsep(src[1])
1880 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1881 {
1882 var = homedir;
1883 tail = src + 1;
1884 }
1885 else /* user directory */
1886 {
1887#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1888 /*
1889 * Copy ~user to dst[], so we can put a NUL after it.
1890 */
1891 tail = src;
1892 var = dst;
1893 c = dstlen - 1;
1894 while ( c-- > 0
1895 && *tail
1896 && vim_isfilec(*tail)
1897 && !vim_ispathsep(*tail))
1898 *var++ = *tail++;
1899 *var = NUL;
1900# ifdef UNIX
1901 /*
1902 * If the system supports getpwnam(), use it.
1903 * Otherwise, or if getpwnam() fails, the shell is used to
1904 * expand ~user. This is slower and may fail if the shell
1905 * does not support ~user (old versions of /bin/sh).
1906 */
1907# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1908 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001909 /* Note: memory allocated by getpwnam() is never freed.
1910 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001911 struct passwd *pw = (*dst == NUL)
1912 ? NULL : getpwnam((char *)dst + 1);
1913
1914 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 if (var == NULL)
1917# endif
1918 {
1919 expand_T xpc;
1920
1921 ExpandInit(&xpc);
1922 xpc.xp_context = EXPAND_FILES;
1923 var = ExpandOne(&xpc, dst, NULL,
1924 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 mustfree = TRUE;
1926 }
1927
1928# else /* !UNIX, thus VMS */
1929 /*
1930 * USER_HOME is a comma-separated list of
1931 * directories to search for the user account in.
1932 */
1933 {
1934 char_u test[MAXPATHL], paths[MAXPATHL];
1935 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001936 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 STRCPY(paths, USER_HOME);
1939 next_path = paths;
1940 while (*next_path)
1941 {
1942 for (path = next_path; *next_path && *next_path != ',';
1943 next_path++);
1944 if (*next_path)
1945 *next_path++ = NUL;
1946 STRCPY(test, path);
1947 STRCAT(test, "/");
1948 STRCAT(test, dst + 1);
1949 if (mch_stat(test, &st) == 0)
1950 {
1951 var = alloc(STRLEN(test) + 1);
1952 STRCPY(var, test);
1953 mustfree = TRUE;
1954 break;
1955 }
1956 }
1957 }
1958# endif /* UNIX */
1959#else
1960 /* cannot expand user's home directory, so don't try */
1961 var = NULL;
1962 tail = (char_u *)""; /* for gcc */
1963#endif /* UNIX || VMS */
1964 }
1965
1966#ifdef BACKSLASH_IN_FILENAME
1967 /* If 'shellslash' is set change backslashes to forward slashes.
1968 * Can't use slash_adjust(), p_ssl may be set temporarily. */
1969 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1970 {
1971 char_u *p = vim_strsave(var);
1972
1973 if (p != NULL)
1974 {
1975 if (mustfree)
1976 vim_free(var);
1977 var = p;
1978 mustfree = TRUE;
1979 forward_slash(var);
1980 }
1981 }
1982#endif
1983
1984 /* If "var" contains white space, escape it with a backslash.
1985 * Required for ":e ~/tt" when $HOME includes a space. */
1986 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1987 {
1988 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1989
1990 if (p != NULL)
1991 {
1992 if (mustfree)
1993 vim_free(var);
1994 var = p;
1995 mustfree = TRUE;
1996 }
1997 }
1998
1999 if (var != NULL && *var != NUL
2000 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
2001 {
2002 STRCPY(dst, var);
2003 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002004 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 /* if var[] ends in a path separator and tail[] starts
2006 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002007 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
2009 && dst[-1] != ':'
2010#endif
2011 && vim_ispathsep(*tail))
2012 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002013 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 src = tail;
2015 copy_char = FALSE;
2016 }
2017 if (mustfree)
2018 vim_free(var);
2019 }
2020
2021 if (copy_char) /* copy at least one char */
2022 {
2023 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00002024 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002025 * Don't do this when "one" is TRUE, to avoid expanding "~" in
2026 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 */
2028 at_start = FALSE;
2029 if (src[0] == '\\' && src[1] != NUL)
2030 {
2031 *dst++ = *src++;
2032 --dstlen;
2033 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002034 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02002036 if (dstlen > 0)
2037 {
2038 *dst++ = *src++;
2039 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002040
Bram Moolenaar1c864092017-08-06 18:15:45 +02002041 if (startstr != NULL && src - startstr_len >= srcp
2042 && STRNCMP(src - startstr_len, startstr,
2043 startstr_len) == 0)
2044 at_start = TRUE;
2045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02002047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 *dst = NUL;
2050}
2051
2052/*
2053 * Vim's version of getenv().
2054 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00002055 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02002056 * "mustfree" is set to TRUE when returned is allocated, it must be
2057 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 */
2059 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002060vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002062 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 char_u *pend;
2064 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002065#ifdef MSWIN
2066 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002068 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if (STRCMP(name, "HOME") == 0)
2070 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002072 // Use Wide function
2073 wn = enc_to_utf16(name, NULL);
2074 if (wn == NULL)
2075 return NULL;
2076
2077 wp = _wgetenv(wn);
2078 vim_free(wn);
2079
2080 if (wp != NULL && *wp == NUL) // empty is the same as not set
2081 wp = NULL;
2082
2083 if (wp != NULL)
2084 {
2085 p = utf16_to_enc(wp, NULL);
2086 if (p == NULL)
2087 return NULL;
2088
2089 *mustfree = TRUE;
2090 return p;
2091 }
2092#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002094 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 p = NULL;
2096
2097 if (p != NULL)
2098 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002101 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
2103 if (!vimruntime && STRCMP(name, "VIM") != 0)
2104 return NULL;
2105
2106 /*
2107 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
2108 * Don't do this when default_vimruntime_dir is non-empty.
2109 */
2110 if (vimruntime
2111#ifdef HAVE_PATHDEF
2112 && *default_vimruntime_dir == NUL
2113#endif
2114 )
2115 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002116#ifdef MSWIN
2117 // Use Wide function
2118 wp = _wgetenv(L"VIM");
2119 if (wp != NULL && *wp == NUL) // empty is the same as not set
2120 wp = NULL;
2121 if (wp != NULL)
2122 {
2123 char_u *q = utf16_to_enc(wp, NULL);
2124 if (q != NULL)
2125 {
2126 p = vim_version_dir(q);
2127 *mustfree = TRUE;
2128 if (p == NULL)
2129 p = q;
2130 }
2131 }
2132#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002134 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 p = NULL;
2136 if (p != NULL)
2137 {
2138 p = vim_version_dir(p);
2139 if (p != NULL)
2140 *mustfree = TRUE;
2141 else
2142 p = mch_getenv((char_u *)"VIM");
2143 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146
2147 /*
2148 * When expanding $VIM or $VIMRUNTIME fails, try using:
2149 * - the directory name from 'helpfile' (unless it contains '$')
2150 * - the executable name from argv[0]
2151 */
2152 if (p == NULL)
2153 {
2154 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
2155 p = p_hf;
2156#ifdef USE_EXE_NAME
2157 /*
2158 * Use the name of the executable, obtained from argv[0].
2159 */
2160 else
2161 p = exe_name;
2162#endif
2163 if (p != NULL)
2164 {
2165 /* remove the file name */
2166 pend = gettail(p);
2167
2168 /* remove "doc/" from 'helpfile', if present */
2169 if (p == p_hf)
2170 pend = remove_tail(p, pend, (char_u *)"doc");
2171
2172#ifdef USE_EXE_NAME
2173# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002174 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (p == exe_name)
2176 {
2177 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002178 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002180 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
2181 if (pend1 != pend)
2182 {
2183 pnew = alloc((unsigned)(pend1 - p) + 15);
2184 if (pnew != NULL)
2185 {
2186 STRNCPY(pnew, p, (pend1 - p));
2187 STRCPY(pnew + (pend1 - p), "Resources/vim");
2188 p = pnew;
2189 pend = p + STRLEN(p);
2190 }
2191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 }
2193# endif
2194 /* remove "src/" from exe_name, if present */
2195 if (p == exe_name)
2196 pend = remove_tail(p, pend, (char_u *)"src");
2197#endif
2198
2199 /* for $VIM, remove "runtime/" or "vim54/", if present */
2200 if (!vimruntime)
2201 {
2202 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
2203 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
2204 }
2205
2206 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002207 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002210#ifdef MACOS_X
2211 if (p == exe_name || p == p_hf)
2212#endif
2213 /* check that the result is a directory name */
2214 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01002217 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 else
2219 {
2220#ifdef USE_EXE_NAME
2221 /* may add "/vim54" or "/runtime" if it exists */
2222 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
2223 {
2224 vim_free(p);
2225 p = pend;
2226 }
2227#endif
2228 *mustfree = TRUE;
2229 }
2230 }
2231 }
2232
2233#ifdef HAVE_PATHDEF
2234 /* When there is a pathdef.c file we can use default_vim_dir and
2235 * default_vimruntime_dir */
2236 if (p == NULL)
2237 {
2238 /* Only use default_vimruntime_dir when it is not empty */
2239 if (vimruntime && *default_vimruntime_dir != NUL)
2240 {
2241 p = default_vimruntime_dir;
2242 *mustfree = FALSE;
2243 }
2244 else if (*default_vim_dir != NUL)
2245 {
2246 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
2247 *mustfree = TRUE;
2248 else
2249 {
2250 p = default_vim_dir;
2251 *mustfree = FALSE;
2252 }
2253 }
2254 }
2255#endif
2256
2257 /*
2258 * Set the environment variable, so that the new value can be found fast
2259 * next time, and others can also use it (e.g. Perl).
2260 */
2261 if (p != NULL)
2262 {
2263 if (vimruntime)
2264 {
2265 vim_setenv((char_u *)"VIMRUNTIME", p);
2266 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 else
2269 {
2270 vim_setenv((char_u *)"VIM", p);
2271 didset_vim = TRUE;
2272 }
2273 }
2274 return p;
2275}
2276
2277/*
2278 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
2279 * Return NULL if not, return its name in allocated memory otherwise.
2280 */
2281 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002282vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 char_u *p;
2285
2286 if (vimdir == NULL || *vimdir == NUL)
2287 return NULL;
2288 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
2289 if (p != NULL && mch_isdir(p))
2290 return p;
2291 vim_free(p);
2292 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
2293 if (p != NULL && mch_isdir(p))
2294 return p;
2295 vim_free(p);
2296 return NULL;
2297}
2298
2299/*
2300 * If the string between "p" and "pend" ends in "name/", return "pend" minus
2301 * the length of "name/". Otherwise return "pend".
2302 */
2303 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 int len = (int)STRLEN(name) + 1;
2307 char_u *newend = pend - len;
2308
2309 if (newend >= p
2310 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002311 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return newend;
2313 return pend;
2314}
2315
Bram Moolenaar113e1072019-01-20 15:30:40 +01002316#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02002317 void
2318vim_unsetenv(char_u *var)
2319{
2320#ifdef HAVE_UNSETENV
2321 unsetenv((char *)var);
2322#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02002323 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02002324#endif
2325}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002326#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02002327
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 * Our portable version of setenv.
2331 */
2332 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002333vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335#ifdef HAVE_SETENV
2336 mch_setenv((char *)name, (char *)val, 1);
2337#else
2338 char_u *envbuf;
2339
2340 /*
2341 * Putenv does not copy the string, it has to remain
2342 * valid. The allocated memory will never be freed.
2343 */
2344 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
2345 if (envbuf != NULL)
2346 {
2347 sprintf((char *)envbuf, "%s=%s", name, val);
2348 putenv((char *)envbuf);
2349 }
2350#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002351#ifdef FEAT_GETTEXT
2352 /*
2353 * When setting $VIMRUNTIME adjust the directory to find message
2354 * translations to $VIMRUNTIME/lang.
2355 */
2356 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2357 {
2358 char_u *buf = concat_str(val, (char_u *)"/lang");
2359
2360 if (buf != NULL)
2361 {
2362 bindtextdomain(VIMPACKAGE, (char *)buf);
2363 vim_free(buf);
2364 }
2365 }
2366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367}
2368
2369#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2370/*
2371 * Function given to ExpandGeneric() to obtain an environment variable name.
2372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002374get_env_name(
2375 expand_T *xp UNUSED,
2376 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaard0573012017-10-28 21:11:06 +02002378# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002380 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 */
2382 return NULL;
2383# else
2384# ifndef __WIN32__
2385 /* Borland C++ 5.2 has this in a header file. */
2386 extern char **environ;
2387# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002388# define ENVNAMELEN 100
2389 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 char_u *str;
2391 int n;
2392
2393 str = (char_u *)environ[idx];
2394 if (str == NULL)
2395 return NULL;
2396
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002397 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
2399 if (str[n] == '=' || str[n] == NUL)
2400 break;
2401 name[n] = str[n];
2402 }
2403 name[n] = NUL;
2404 return name;
2405# endif
2406}
Bram Moolenaar24305862012-08-15 14:05:05 +02002407
2408/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002409 * Add a user name to the list of users in ga_users.
2410 * Do nothing if user name is NULL or empty.
2411 */
2412 static void
2413add_user(char_u *user, int need_copy)
2414{
2415 char_u *user_copy = (user != NULL && need_copy)
2416 ? vim_strsave(user) : user;
2417
2418 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2419 {
2420 if (need_copy)
2421 vim_free(user);
2422 return;
2423 }
2424 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2425}
2426
2427/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002428 * Find all user names for user completion.
2429 * Done only once and then cached.
2430 */
2431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002432init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002433{
Bram Moolenaar24305862012-08-15 14:05:05 +02002434 static int lazy_init_done = FALSE;
2435
2436 if (lazy_init_done)
2437 return;
2438
2439 lazy_init_done = TRUE;
2440 ga_init2(&ga_users, sizeof(char_u *), 20);
2441
2442# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2443 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002444 struct passwd* pw;
2445
2446 setpwent();
2447 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002448 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002449 endpwent();
2450 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002451# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002452 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002453 DWORD nusers = 0, ntotal = 0, i;
2454 PUSER_INFO_0 uinfo;
2455
2456 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2457 &nusers, &ntotal, NULL) == NERR_Success)
2458 {
2459 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002460 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002461
2462 NetApiBufferFree(uinfo);
2463 }
2464 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002465# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002466# if defined(HAVE_GETPWNAM)
2467 {
2468 char_u *user_env = mch_getenv((char_u *)"USER");
2469
2470 // The $USER environment variable may be a valid remote user name (NIS,
2471 // LDAP) not already listed by getpwent(), as getpwent() only lists
2472 // local user names. If $USER is not already listed, check whether it
2473 // is a valid remote user name using getpwnam() and if it is, add it to
2474 // the list of user names.
2475
2476 if (user_env != NULL && *user_env != NUL)
2477 {
2478 int i;
2479
2480 for (i = 0; i < ga_users.ga_len; i++)
2481 {
2482 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2483
2484 if (STRCMP(local_user, user_env) == 0)
2485 break;
2486 }
2487
2488 if (i == ga_users.ga_len)
2489 {
2490 struct passwd *pw = getpwnam((char *)user_env);
2491
2492 if (pw != NULL)
2493 add_user((char_u *)pw->pw_name, TRUE);
2494 }
2495 }
2496 }
2497# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002498}
2499
2500/*
2501 * Function given to ExpandGeneric() to obtain an user names.
2502 */
2503 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002504get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002505{
2506 init_users();
2507 if (idx < ga_users.ga_len)
2508 return ((char_u **)ga_users.ga_data)[idx];
2509 return NULL;
2510}
2511
2512/*
2513 * Check whether name matches a user name. Return:
2514 * 0 if name does not match any user name.
2515 * 1 if name partially matches the beginning of a user name.
2516 * 2 is name fully matches a user name.
2517 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002518 int
2519match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002520{
2521 int i;
2522 int n = (int)STRLEN(name);
2523 int result = 0;
2524
2525 init_users();
2526 for (i = 0; i < ga_users.ga_len; i++)
2527 {
2528 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2529 return 2; /* full match */
2530 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2531 result = 1; /* partial match */
2532 }
2533 return result;
2534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#endif
2536
2537/*
2538 * Replace home directory by "~" in each space or comma separated file name in
2539 * 'src'.
2540 * If anything fails (except when out of space) dst equals src.
2541 */
2542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002543home_replace(
2544 buf_T *buf, /* when not NULL, check for help files */
2545 char_u *src, /* input file name */
2546 char_u *dst, /* where to put the result */
2547 int dstlen, /* maximum length of the result */
2548 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 spaces and commas in the file name. */
2550{
2551 size_t dirlen = 0, envlen = 0;
2552 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002553 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 char_u *p;
2555
2556 if (src == NULL)
2557 {
2558 *dst = NUL;
2559 return;
2560 }
2561
2562 /*
2563 * If the file is a help file, remove the path completely.
2564 */
2565 if (buf != NULL && buf->b_help)
2566 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02002567 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return;
2569 }
2570
2571 /*
2572 * We check both the value of the $HOME environment variable and the
2573 * "real" home directory.
2574 */
2575 if (homedir != NULL)
2576 dirlen = STRLEN(homedir);
2577
2578#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002579 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002581 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
2582#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002583#ifdef MSWIN
Bram Moolenaar48340b62017-08-29 22:08:53 +02002584 if (homedir_env == NULL)
2585 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
2586#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02002587 /* Empty is the same as not set. */
2588 if (homedir_env != NULL && *homedir_env == NUL)
2589 homedir_env = NULL;
2590
Bram Moolenaare60c2e52013-06-05 19:35:38 +02002591#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01002592 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002593 {
2594 int usedlen = 0;
2595 int flen;
2596 char_u *fbuf = NULL;
2597
2598 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02002599 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02002600 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002601 flen = (int)STRLEN(homedir_env);
2602 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
2603 /* Remove the trailing / that is added to a directory. */
2604 homedir_env[flen - 1] = NUL;
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606#endif
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if (homedir_env != NULL)
2609 envlen = STRLEN(homedir_env);
2610
2611 if (!one)
2612 src = skipwhite(src);
2613 while (*src && dstlen > 0)
2614 {
2615 /*
2616 * Here we are at the beginning of a file name.
2617 * First, check to see if the beginning of the file name matches
2618 * $HOME or the "real" home directory. Check that there is a '/'
2619 * after the match (so that if e.g. the file is "/home/pieter/bla",
2620 * and the home directory is "/home/piet", the file does not end up
2621 * as "~er/bla" (which would seem to indicate the file "bla" in user
2622 * er's home directory)).
2623 */
2624 p = homedir;
2625 len = dirlen;
2626 for (;;)
2627 {
2628 if ( len
2629 && fnamencmp(src, p, len) == 0
2630 && (vim_ispathsep(src[len])
2631 || (!one && (src[len] == ',' || src[len] == ' '))
2632 || src[len] == NUL))
2633 {
2634 src += len;
2635 if (--dstlen > 0)
2636 *dst++ = '~';
2637
2638 /*
2639 * If it's just the home directory, add "/".
2640 */
2641 if (!vim_ispathsep(src[0]) && --dstlen > 0)
2642 *dst++ = '/';
2643 break;
2644 }
2645 if (p == homedir_env)
2646 break;
2647 p = homedir_env;
2648 len = envlen;
2649 }
2650
2651 /* if (!one) skip to separator: space or comma */
2652 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
2653 *dst++ = *src++;
2654 /* skip separator */
2655 while ((*src == ' ' || *src == ',') && --dstlen > 0)
2656 *dst++ = *src++;
2657 }
2658 /* if (dstlen == 0) out of space, what to do??? */
2659
2660 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002661
2662 if (homedir_env != homedir_env_orig)
2663 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664}
2665
2666/*
2667 * Like home_replace, store the replaced string in allocated memory.
2668 * When something fails, NULL is returned.
2669 */
2670 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002671home_replace_save(
2672 buf_T *buf, /* when not NULL, check for help files */
2673 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 char_u *dst;
2676 unsigned len;
2677
2678 len = 3; /* space for "~/" and trailing NUL */
2679 if (src != NULL) /* just in case */
2680 len += (unsigned)STRLEN(src);
2681 dst = alloc(len);
2682 if (dst != NULL)
2683 home_replace(buf, src, dst, len, TRUE);
2684 return dst;
2685}
2686
2687/*
2688 * Compare two file names and return:
2689 * FPC_SAME if they both exist and are the same file.
2690 * FPC_SAMEX if they both don't exist and have the same file name.
2691 * FPC_DIFF if they both exist and are different files.
2692 * FPC_NOTX if they both don't exist.
2693 * FPC_DIFFX if one of them doesn't exist.
2694 * For the first name environment variables are expanded
2695 */
2696 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002697fullpathcmp(
2698 char_u *s1,
2699 char_u *s2,
2700 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702#ifdef UNIX
2703 char_u exp1[MAXPATHL];
2704 char_u full1[MAXPATHL];
2705 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02002706 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 int r1, r2;
2708
2709 expand_env(s1, exp1, MAXPATHL);
2710 r1 = mch_stat((char *)exp1, &st1);
2711 r2 = mch_stat((char *)s2, &st2);
2712 if (r1 != 0 && r2 != 0)
2713 {
2714 /* if mch_stat() doesn't work, may compare the names */
2715 if (checkname)
2716 {
2717 if (fnamecmp(exp1, s2) == 0)
2718 return FPC_SAMEX;
2719 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2720 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2721 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
2722 return FPC_SAMEX;
2723 }
2724 return FPC_NOTX;
2725 }
2726 if (r1 != 0 || r2 != 0)
2727 return FPC_DIFFX;
2728 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
2729 return FPC_SAME;
2730 return FPC_DIFF;
2731#else
2732 char_u *exp1; /* expanded s1 */
2733 char_u *full1; /* full path of s1 */
2734 char_u *full2; /* full path of s2 */
2735 int retval = FPC_DIFF;
2736 int r1, r2;
2737
2738 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
2739 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
2740 {
2741 full1 = exp1 + MAXPATHL;
2742 full2 = full1 + MAXPATHL;
2743
2744 expand_env(s1, exp1, MAXPATHL);
2745 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2746 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2747
2748 /* If vim_FullName() fails, the file probably doesn't exist. */
2749 if (r1 != OK && r2 != OK)
2750 {
2751 if (checkname && fnamecmp(exp1, s2) == 0)
2752 retval = FPC_SAMEX;
2753 else
2754 retval = FPC_NOTX;
2755 }
2756 else if (r1 != OK || r2 != OK)
2757 retval = FPC_DIFFX;
2758 else if (fnamecmp(full1, full2))
2759 retval = FPC_DIFF;
2760 else
2761 retval = FPC_SAME;
2762 vim_free(exp1);
2763 }
2764 return retval;
2765#endif
2766}
2767
2768/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002769 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02002770 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002771 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 */
2773 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002774gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 char_u *p1, *p2;
2777
2778 if (fname == NULL)
2779 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01002780 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01002782 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002784 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 return p1;
2787}
2788
2789/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002790 * Get pointer to tail of "fname", including path separators. Putting a NUL
2791 * here leaves the directory name. Takes care of "c:/" and "//".
2792 * Always returns a valid pointer.
2793 */
2794 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002795gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002796{
2797 char_u *p;
2798 char_u *t;
2799
2800 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
2801 t = gettail(fname);
2802 while (t > p && after_pathsep(fname, t))
2803 --t;
2804#ifdef VMS
2805 /* path separator is part of the path */
2806 ++t;
2807#endif
2808 return t;
2809}
2810
2811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 * get the next path component (just after the next path separator).
2813 */
2814 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002815getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
2817 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002818 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (*fname)
2820 ++fname;
2821 return fname;
2822}
2823
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824/*
2825 * Get a pointer to one character past the head of a path name.
2826 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
2827 * If there is no head, path is returned.
2828 */
2829 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002830get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 char_u *retval;
2833
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002834#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /* may skip "c:" */
2836 if (isalpha(path[0]) && path[1] == ':')
2837 retval = path + 2;
2838 else
2839 retval = path;
2840#else
2841# if defined(AMIGA)
2842 /* may skip "label:" */
2843 retval = vim_strchr(path, ':');
2844 if (retval == NULL)
2845 retval = path;
2846# else /* Unix */
2847 retval = path;
2848# endif
2849#endif
2850
2851 while (vim_ispathsep(*retval))
2852 ++retval;
2853
2854 return retval;
2855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01002858 * Return TRUE if 'c' is a path separator.
2859 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 */
2861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
Bram Moolenaare60acc12011-05-10 16:41:25 +02002864#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02002866#else
2867# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02002869# else
2870# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
2872 return (c == ':' || c == '[' || c == ']' || c == '/'
2873 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02002874# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02002876# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02002878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879}
2880
Bram Moolenaar69c35002013-11-04 02:54:12 +01002881/*
2882 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
2883 */
2884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002885vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01002886{
2887 return vim_ispathsep(c)
2888#ifdef BACKSLASH_IN_FILENAME
2889 && c != ':'
2890#endif
2891 ;
2892}
2893
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002894/*
2895 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
2896 * It's done in-place.
2897 */
2898 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002899shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002900{
2901 char_u *tail, *s, *d;
2902 int skip = FALSE;
2903
2904 tail = gettail(str);
2905 d = str;
2906 for (s = str; ; ++s)
2907 {
2908 if (s >= tail) /* copy the whole tail */
2909 {
2910 *d++ = *s;
2911 if (*s == NUL)
2912 break;
2913 }
2914 else if (vim_ispathsep(*s)) /* copy '/' and next char */
2915 {
2916 *d++ = *s;
2917 skip = FALSE;
2918 }
2919 else if (!skip)
2920 {
2921 *d++ = *s; /* copy next char */
2922 if (*s != '~' && *s != '.') /* and leading "~" and "." */
2923 skip = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002924 if (has_mbyte)
2925 {
2926 int l = mb_ptr2len(s);
2927
2928 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00002929 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002930 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002931 }
2932 }
2933}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002934
Bram Moolenaar900b4d72005-12-12 22:05:50 +00002935/*
2936 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
2937 * Also returns TRUE if there is no directory name.
2938 * "fname" must be writable!.
2939 */
2940 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002941dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00002942{
2943 char_u *p;
2944 int c;
2945 int retval;
2946
2947 p = gettail_sep(fname);
2948 if (p == fname)
2949 return TRUE;
2950 c = *p;
2951 *p = NUL;
2952 retval = mch_isdir(fname);
2953 *p = c;
2954 return retval;
2955}
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002958 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
2959 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 */
2961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002962vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002964#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002966#else
2967 if (p_fic)
2968 return MB_STRICMP(x, y);
2969 return STRCMP(x, y);
2970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
2973 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002974vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002976#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002977 char_u *px = x;
2978 char_u *py = y;
2979 int cx = NUL;
2980 int cy = NUL;
2981
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002982 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002984 cx = PTR2CHAR(px);
2985 cy = PTR2CHAR(py);
2986 if (cx == NUL || cy == NUL
2987 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
2988 && !(cx == '/' && cy == '\\')
2989 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002991 len -= MB_PTR2LEN(px);
2992 px += MB_PTR2LEN(px);
2993 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 if (len == 0)
2996 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002997 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002998#else
2999 if (p_fic)
3000 return MB_STRNICMP(x, y, len);
3001 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005/*
3006 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00003007 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003010concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 char_u *dest;
3013
3014 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
3015 if (dest != NULL)
3016 {
3017 STRCPY(dest, fname1);
3018 if (sep)
3019 add_pathsep(dest);
3020 STRCAT(dest, fname2);
3021 }
3022 return dest;
3023}
3024
Bram Moolenaard6754642005-01-17 22:18:45 +00003025/*
3026 * Concatenate two strings and return the result in allocated memory.
3027 * Returns NULL when out of memory.
3028 */
3029 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003030concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00003031{
3032 char_u *dest;
3033 size_t l = STRLEN(str1);
3034
3035 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
3036 if (dest != NULL)
3037 {
3038 STRCPY(dest, str1);
3039 STRCPY(dest + l, str2);
3040 }
3041 return dest;
3042}
Bram Moolenaard6754642005-01-17 22:18:45 +00003043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * Add a path separator to a file name, unless it already ends in a path
3046 * separator.
3047 */
3048 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003049add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003051 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 STRCAT(p, PATHSEPSTR);
3053}
3054
3055/*
3056 * FullName_save - Make an allocated copy of a full file name.
3057 * Returns NULL when out of memory.
3058 */
3059 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003060FullName_save(
3061 char_u *fname,
3062 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02003063 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 char_u *buf;
3066 char_u *new_fname = NULL;
3067
3068 if (fname == NULL)
3069 return NULL;
3070
3071 buf = alloc((unsigned)MAXPATHL);
3072 if (buf != NULL)
3073 {
3074 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
3075 new_fname = vim_strsave(buf);
3076 else
3077 new_fname = vim_strsave(fname);
3078 vim_free(buf);
3079 }
3080 return new_fname;
3081}
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003084prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003086#if defined(SIGHUP) && defined(SIG_IGN)
3087 /* Ignore SIGHUP, because a dropped connection causes a read error, which
3088 * makes Vim exit and then handling SIGHUP causes various reentrance
3089 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003090 signal(SIGHUP, SIG_IGN);
3091#endif
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#ifdef FEAT_GUI
3094 if (gui.in_use)
3095 {
3096 gui.dying = TRUE;
3097 out_trash(); /* trash any pending output */
3098 }
3099 else
3100#endif
3101 {
3102 windgoto((int)Rows - 1, 0);
3103
3104 /*
3105 * Switch terminal mode back now, so messages end up on the "normal"
3106 * screen (if there are two screens).
3107 */
3108 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003109 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 out_flush();
3111 }
3112}
3113
3114/*
3115 * Preserve files and exit.
3116 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003117 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
3118 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 */
3120 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003121preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 buf_T *buf;
3124
3125 prepare_to_exit();
3126
Bram Moolenaar4770d092006-01-12 23:22:24 +00003127 /* Setting this will prevent free() calls. That avoids calling free()
3128 * recursively when free() was invoked with a bad pointer. */
3129 really_exiting = TRUE;
3130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 out_str(IObuff);
3132 screen_start(); /* don't know where cursor is now */
3133 out_flush();
3134
3135 ml_close_notmod(); /* close all not-modified buffers */
3136
Bram Moolenaar29323592016-07-24 22:04:11 +02003137 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
3140 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003141 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 screen_start(); /* don't know where cursor is now */
3143 out_flush();
3144 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3145 break;
3146 }
3147 }
3148
3149 ml_close_all(FALSE); /* close all memfiles, without deleting */
3150
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003151 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 getout(1);
3154}
3155
3156/*
3157 * return TRUE if "fname" exists.
3158 */
3159 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003160vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003162 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 if (mch_stat((char *)fname, &st))
3165 return FALSE;
3166 return TRUE;
3167}
3168
3169/*
3170 * Check for CTRL-C pressed, but only once in a while.
3171 * Should be used instead of ui_breakcheck() for functions that check for
3172 * each line in the file. Calling ui_breakcheck() each time takes too much
3173 * time, because it can be a system call.
3174 */
3175
3176#ifndef BREAKCHECK_SKIP
3177# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
3178# define BREAKCHECK_SKIP 200
3179# else
3180# define BREAKCHECK_SKIP 32
3181# endif
3182#endif
3183
3184static int breakcheck_count = 0;
3185
3186 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003187line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 if (++breakcheck_count >= BREAKCHECK_SKIP)
3190 {
3191 breakcheck_count = 0;
3192 ui_breakcheck();
3193 }
3194}
3195
3196/*
3197 * Like line_breakcheck() but check 10 times less often.
3198 */
3199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003200fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
3203 {
3204 breakcheck_count = 0;
3205 ui_breakcheck();
3206 }
3207}
3208
3209/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00003210 * Invoke expand_wildcards() for one pattern.
3211 * Expand items like "%:h" before the expansion.
3212 * Returns OK or FAIL.
3213 */
3214 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003215expand_wildcards_eval(
3216 char_u **pat, /* pointer to input pattern */
3217 int *num_file, /* resulting number of files */
3218 char_u ***file, /* array of resulting files */
3219 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00003220{
3221 int ret = FAIL;
3222 char_u *eval_pat = NULL;
3223 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003224 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00003225 int usedlen;
3226
3227 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
3228 {
3229 ++emsg_off;
3230 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
3231 NULL, &ignored_msg, NULL);
3232 --emsg_off;
3233 if (eval_pat != NULL)
3234 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
3235 }
3236
3237 if (exp_pat != NULL)
3238 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
3239
3240 if (eval_pat != NULL)
3241 {
3242 vim_free(exp_pat);
3243 vim_free(eval_pat);
3244 }
3245
3246 return ret;
3247}
3248
3249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
3251 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003252 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 */
3254 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003255expand_wildcards(
3256 int num_pat, /* number of input patterns */
3257 char_u **pat, /* array of input patterns */
3258 int *num_files, /* resulting number of files */
3259 char_u ***files, /* array of resulting files */
3260 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 int retval;
3263 int i, j;
3264 char_u *p;
3265 int non_suf_match; /* number without matching suffix */
3266
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003267 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
3269 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02003270 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 return retval;
3272
3273#ifdef FEAT_WILDIGN
3274 /*
3275 * Remove names that match 'wildignore'.
3276 */
3277 if (*p_wig)
3278 {
3279 char_u *ffname;
3280
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003281 /* check all files in (*files)[] */
3282 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003284 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (ffname == NULL) /* out of memory */
3286 break;
3287# ifdef VMS
3288 vms_remove_version(ffname);
3289# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003290 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01003292 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003293 vim_free((*files)[i]);
3294 for (j = i; j + 1 < *num_files; ++j)
3295 (*files)[j] = (*files)[j + 1];
3296 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 --i;
3298 }
3299 vim_free(ffname);
3300 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003301
3302 /* If the number of matches is now zero, we fail. */
3303 if (*num_files == 0)
3304 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003305 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003306 return FAIL;
3307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309#endif
3310
3311 /*
3312 * Move the names where 'suffixes' match to the end.
3313 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003314 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
3316 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003317 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003319 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 /*
3322 * Move the name without matching suffix to the front
3323 * of the list.
3324 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003325 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003327 (*files)[j] = (*files)[j - 1];
3328 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330 }
3331 }
3332
3333 return retval;
3334}
3335
3336/*
3337 * Return TRUE if "fname" matches with an entry in 'suffixes'.
3338 */
3339 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003340match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 int fnamelen, setsuflen;
3343 char_u *setsuf;
3344#define MAXSUFLEN 30 /* maximum length of a file suffix */
3345 char_u suf_buf[MAXSUFLEN];
3346
3347 fnamelen = (int)STRLEN(fname);
3348 setsuflen = 0;
3349 for (setsuf = p_su; *setsuf; )
3350 {
3351 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00003352 if (setsuflen == 0)
3353 {
3354 char_u *tail = gettail(fname);
3355
3356 /* empty entry: match name without a '.' */
3357 if (vim_strchr(tail, '.') == NULL)
3358 {
3359 setsuflen = 1;
3360 break;
3361 }
3362 }
3363 else
3364 {
3365 if (fnamelen >= setsuflen
3366 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
3367 (size_t)setsuflen) == 0)
3368 break;
3369 setsuflen = 0;
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372 return (setsuflen != 0);
3373}
3374
3375#if !defined(NO_EXPANDPATH) || defined(PROTO)
3376
3377# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003378static int vim_backtick(char_u *p);
3379static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380# endif
3381
Bram Moolenaar4f974752019-02-17 17:44:42 +01003382# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383/*
3384 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
3385 * it's shared between these systems.
3386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388/*
3389 * comparison function for qsort in dos_expandpath()
3390 */
Bram Moolenaareae1b912019-05-09 15:12:55 +02003391 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392pstrcmp(const void *a, const void *b)
3393{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003394 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395}
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00003398 * Recursively expand one path component into all matching files and/or
3399 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 * Return the number of matches found.
3401 * "path" has backslashes before chars that are not to be expanded, starting
3402 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00003403 * Return the number of matches found.
3404 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 */
3406 static int
3407dos_expandpath(
3408 garray_T *gap,
3409 char_u *path,
3410 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00003411 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00003412 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
Bram Moolenaar231334e2005-07-25 20:46:57 +00003414 char_u *buf;
3415 char_u *path_end;
3416 char_u *p, *s, *e;
3417 int start_len = gap->ga_len;
3418 char_u *pat;
3419 regmatch_T regmatch;
3420 int starts_with_dot;
3421 int matches;
3422 int len;
3423 int starstar = FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003424 static int stardepth = 0; // depth for "**" expansion
3425 HANDLE hFind = INVALID_HANDLE_VALUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 WIN32_FIND_DATAW wfb;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003427 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003429 int ok;
3430
3431 /* Expanding "**" may take a long time, check for CTRL-C. */
3432 if (stardepth > 0)
3433 {
3434 ui_breakcheck();
3435 if (got_int)
3436 return 0;
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003439 // Make room for file name. When doing encoding conversion the actual
3440 // length may be quite a bit longer, thus use the maximum possible length.
Bram Moolenaar7314efd2015-10-31 15:32:52 +01003441 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (buf == NULL)
3443 return 0;
3444
3445 /*
3446 * Find the first part in the path name that contains a wildcard or a ~1.
3447 * Copy it into buf, including the preceding characters.
3448 */
3449 p = buf;
3450 s = buf;
3451 e = NULL;
3452 path_end = path;
3453 while (*path_end != NUL)
3454 {
3455 /* May ignore a wildcard that has a backslash before it; it will
3456 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
3457 if (path_end >= path + wildoff && rem_backslash(path_end))
3458 *p++ = *path_end++;
3459 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
3460 {
3461 if (e != NULL)
3462 break;
3463 s = p + 1;
3464 }
3465 else if (path_end >= path + wildoff
3466 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
3467 e = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (has_mbyte)
3469 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003470 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 STRNCPY(p, path_end, len);
3472 p += len;
3473 path_end += len;
3474 }
3475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 *p++ = *path_end++;
3477 }
3478 e = p;
3479 *e = NUL;
3480
3481 /* now we have one wildcard component between s and e */
3482 /* Remove backslashes between "wildoff" and the start of the wildcard
3483 * component. */
3484 for (p = buf + wildoff; p < s; ++p)
3485 if (rem_backslash(p))
3486 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003487 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 --e;
3489 --s;
3490 }
3491
Bram Moolenaar231334e2005-07-25 20:46:57 +00003492 /* Check for "**" between "s" and "e". */
3493 for (p = s; p < e; ++p)
3494 if (p[0] == '*' && p[1] == '*')
3495 starstar = TRUE;
3496
Bram Moolenaard82103e2016-01-17 17:04:05 +01003497 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3499 if (pat == NULL)
3500 {
3501 vim_free(buf);
3502 return 0;
3503 }
3504
3505 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003506 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02003507 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 regmatch.rm_ic = TRUE; /* Always ignore case */
3509 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003510 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02003511 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 vim_free(pat);
3513
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003514 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 vim_free(buf);
3517 return 0;
3518 }
3519
3520 /* remember the pattern or file name being looked for */
3521 matchname = vim_strsave(s);
3522
Bram Moolenaar231334e2005-07-25 20:46:57 +00003523 /* If "**" is by itself, this is the first time we encounter it and more
3524 * is following then find matches without any directory. */
3525 if (!didstar && stardepth < 100 && starstar && e - s == 2
3526 && *path_end == '/')
3527 {
3528 STRCPY(s, path_end + 1);
3529 ++stardepth;
3530 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3531 --stardepth;
3532 }
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 /* Scan all files in the directory with "dir/ *.*" */
3535 STRCPY(s, "*.*");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003536 wn = enc_to_utf16(buf, NULL);
3537 if (wn != NULL)
3538 hFind = FindFirstFileW(wn, &wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541 while (ok)
3542 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003543 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
Bram Moolenaar543c9b12019-04-05 22:50:40 +02003544 if (p == NULL)
3545 break; // out of memory
3546
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003547 // Ignore entries starting with a dot, unless when asked for. Accept
3548 // all entries found with "matchname".
Bram Moolenaard82103e2016-01-17 17:04:05 +01003549 if ((p[0] != '.' || starts_with_dot
3550 || ((flags & EW_DODOT)
3551 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003553 || (regmatch.regprog != NULL
3554 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003555 || ((flags & EW_NOTWILD)
3556 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003560
3561 if (starstar && stardepth < 100)
3562 {
3563 /* For "**" in the pattern first go deeper in the tree to
3564 * find matches. */
3565 STRCPY(buf + len, "/**");
3566 STRCPY(buf + len + 3, path_end);
3567 ++stardepth;
3568 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
3569 --stardepth;
3570 }
3571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 STRCPY(buf + len, path_end);
3573 if (mch_has_exp_wildcard(path_end))
3574 {
3575 /* need to expand another component of the path */
3576 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003577 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 else
3580 {
3581 /* no more wildcards, check if there is a match */
3582 /* remove backslashes for the remaining components only */
3583 if (*path_end != 0)
3584 backslash_halve(buf + len + 1);
3585 if (mch_getperm(buf) >= 0) /* add existing file */
3586 addfile(gap, buf, flags);
3587 }
3588 }
3589
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003590 vim_free(p);
3591 ok = FindNextFileW(hFind, &wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 /* If no more matches and no match was used, try expanding the name
3594 * itself. Finds the long name of a short filename. */
3595 if (!ok && matchname != NULL && gap->ga_len == start_len)
3596 {
3597 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003599 vim_free(wn);
3600 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (wn != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003602 hFind = FindFirstFileW(wn, &wfb);
3603 else
3604 hFind = INVALID_HANDLE_VALUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003606 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 }
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02003613 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 vim_free(matchname);
3615
3616 matches = gap->ga_len - start_len;
3617 if (matches > 0)
3618 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
3619 sizeof(char_u *), pstrcmp);
3620 return matches;
3621}
3622
3623 int
3624mch_expandpath(
3625 garray_T *gap,
3626 char_u *path,
3627 int flags) /* EW_* flags */
3628{
Bram Moolenaar231334e2005-07-25 20:46:57 +00003629 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630}
Bram Moolenaar4f974752019-02-17 17:44:42 +01003631# endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar231334e2005-07-25 20:46:57 +00003633#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
3634 || defined(PROTO)
3635/*
3636 * Unix style wildcard expansion code.
3637 * It's here because it's used both for Unix and Mac.
3638 */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003639 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003640pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003641{
3642 return (pathcmp(*(char **)a, *(char **)b, -1));
3643}
3644
3645/*
3646 * Recursively expand one path component into all matching files and/or
3647 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3648 * "path" has backslashes before chars that are not to be expanded, starting
3649 * at "path + wildoff".
3650 * Return the number of matches found.
3651 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
3652 */
3653 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003654unix_expandpath(
3655 garray_T *gap,
3656 char_u *path,
3657 int wildoff,
3658 int flags, /* EW_* flags */
3659 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003660{
3661 char_u *buf;
3662 char_u *path_end;
3663 char_u *p, *s, *e;
3664 int start_len = gap->ga_len;
3665 char_u *pat;
3666 regmatch_T regmatch;
3667 int starts_with_dot;
3668 int matches;
3669 int len;
3670 int starstar = FALSE;
3671 static int stardepth = 0; /* depth for "**" expansion */
3672
3673 DIR *dirp;
3674 struct dirent *dp;
3675
3676 /* Expanding "**" may take a long time, check for CTRL-C. */
3677 if (stardepth > 0)
3678 {
3679 ui_breakcheck();
3680 if (got_int)
3681 return 0;
3682 }
3683
3684 /* make room for file name */
3685 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
3686 if (buf == NULL)
3687 return 0;
3688
3689 /*
3690 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02003691 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00003692 * Copy it into "buf", including the preceding characters.
3693 */
3694 p = buf;
3695 s = buf;
3696 e = NULL;
3697 path_end = path;
3698 while (*path_end != NUL)
3699 {
3700 /* May ignore a wildcard that has a backslash before it; it will
3701 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
3702 if (path_end >= path + wildoff && rem_backslash(path_end))
3703 *p++ = *path_end++;
3704 else if (*path_end == '/')
3705 {
3706 if (e != NULL)
3707 break;
3708 s = p + 1;
3709 }
3710 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02003711 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003712 || (!p_fic && (flags & EW_ICASE)
3713 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00003714 e = p;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003715 if (has_mbyte)
3716 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003717 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003718 STRNCPY(p, path_end, len);
3719 p += len;
3720 path_end += len;
3721 }
3722 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00003723 *p++ = *path_end++;
3724 }
3725 e = p;
3726 *e = NUL;
3727
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003728 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003729 /* Remove backslashes between "wildoff" and the start of the wildcard
3730 * component. */
3731 for (p = buf + wildoff; p < s; ++p)
3732 if (rem_backslash(p))
3733 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003734 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003735 --e;
3736 --s;
3737 }
3738
3739 /* Check for "**" between "s" and "e". */
3740 for (p = s; p < e; ++p)
3741 if (p[0] == '*' && p[1] == '*')
3742 starstar = TRUE;
3743
3744 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +01003745 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +00003746 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3747 if (pat == NULL)
3748 {
3749 vim_free(buf);
3750 return 0;
3751 }
3752
3753 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01003754 if (flags & EW_ICASE)
3755 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
3756 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003757 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003758 if (flags & (EW_NOERROR | EW_NOTWILD))
3759 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003760 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003761 if (flags & (EW_NOERROR | EW_NOTWILD))
3762 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003763 vim_free(pat);
3764
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003765 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003766 {
3767 vim_free(buf);
3768 return 0;
3769 }
3770
3771 /* If "**" is by itself, this is the first time we encounter it and more
3772 * is following then find matches without any directory. */
3773 if (!didstar && stardepth < 100 && starstar && e - s == 2
3774 && *path_end == '/')
3775 {
3776 STRCPY(s, path_end + 1);
3777 ++stardepth;
3778 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3779 --stardepth;
3780 }
3781
3782 /* open the directory for scanning */
3783 *s = NUL;
3784 dirp = opendir(*buf == NUL ? "." : (char *)buf);
3785
3786 /* Find all matching entries */
3787 if (dirp != NULL)
3788 {
3789 for (;;)
3790 {
3791 dp = readdir(dirp);
3792 if (dp == NULL)
3793 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +01003794 if ((dp->d_name[0] != '.' || starts_with_dot
3795 || ((flags & EW_DODOT)
3796 && dp->d_name[1] != NUL
3797 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003798 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
3799 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003800 || ((flags & EW_NOTWILD)
3801 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00003802 {
3803 STRCPY(s, dp->d_name);
3804 len = STRLEN(buf);
3805
3806 if (starstar && stardepth < 100)
3807 {
3808 /* For "**" in the pattern first go deeper in the tree to
3809 * find matches. */
3810 STRCPY(buf + len, "/**");
3811 STRCPY(buf + len + 3, path_end);
3812 ++stardepth;
3813 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
3814 --stardepth;
3815 }
3816
3817 STRCPY(buf + len, path_end);
3818 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
3819 {
3820 /* need to expand another component of the path */
3821 /* remove backslashes for the remaining components only */
3822 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
3823 }
3824 else
3825 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003826 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003827
Bram Moolenaar231334e2005-07-25 20:46:57 +00003828 /* no more wildcards, check if there is a match */
3829 /* remove backslashes for the remaining components only */
3830 if (*path_end != NUL)
3831 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003832 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +01003833 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003834 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003835 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00003836#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00003837 size_t precomp_len = STRLEN(buf)+1;
3838 char_u *precomp_buf =
3839 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00003840
Bram Moolenaar231334e2005-07-25 20:46:57 +00003841 if (precomp_buf)
3842 {
3843 mch_memmove(buf, precomp_buf, precomp_len);
3844 vim_free(precomp_buf);
3845 }
3846#endif
3847 addfile(gap, buf, flags);
3848 }
3849 }
3850 }
3851 }
3852
3853 closedir(dirp);
3854 }
3855
3856 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02003857 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003858
3859 matches = gap->ga_len - start_len;
3860 if (matches > 0)
3861 qsort(((char_u **)gap->ga_data) + start_len, matches,
3862 sizeof(char_u *), pstrcmp);
3863 return matches;
3864}
3865#endif
3866
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003867#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
3868/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +02003869 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
3870 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003871 */
3872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003873remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003874{
3875 int i;
3876 int j;
3877 char_u **fnames = (char_u **)gap->ga_data;
3878
Bram Moolenaardc685ab2010-08-13 21:16:49 +02003879 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003880 for (i = gap->ga_len - 1; i > 0; --i)
3881 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
3882 {
3883 vim_free(fnames[i]);
3884 for (j = i + 1; j < gap->ga_len; ++j)
3885 fnames[j - 1] = fnames[j];
3886 --gap->ga_len;
3887 }
3888}
3889#endif
3890
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003891/*
3892 * Return TRUE if "p" contains what looks like an environment variable.
3893 * Allowing for escaping.
3894 */
3895 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003896has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003897{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003898 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003899 {
3900 if (*p == '\\' && p[1] != NUL)
3901 ++p;
3902 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003903#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003904 "$%"
3905#else
3906 "$"
3907#endif
3908 , *p) != NULL)
3909 return TRUE;
3910 }
3911 return FALSE;
3912}
3913
3914#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003915/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +01003916 * Return TRUE if "p" contains a special wildcard character, one that Vim
3917 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003918 */
3919 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003920has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003921{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003922 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003923 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003924 // Disallow line break characters.
3925 if (*p == '\r' || *p == '\n')
3926 break;
3927 // Allow for escaping.
3928 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n')
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003929 ++p;
3930 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003931 {
3932 // A { must be followed by a matching }.
3933 if (*p == '{' && vim_strchr(p, '}') == NULL)
3934 continue;
3935 // A quote and backtick must be followed by another one.
3936 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL)
3937 continue;
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003938 return TRUE;
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003939 }
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003940 }
3941 return FALSE;
3942}
3943#endif
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945/*
3946 * Generic wildcard expansion code.
3947 *
3948 * Characters in "pat" that should not be expanded must be preceded with a
3949 * backslash. E.g., "/path\ with\ spaces/my\*star*"
3950 *
3951 * Return FAIL when no single file was found. In this case "num_file" is not
3952 * set, and "file" may contain an error message.
3953 * Return OK when some files found. "num_file" is set to the number of
3954 * matches, "file" to the array of matches. Call FreeWild() later.
3955 */
3956 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003957gen_expand_wildcards(
3958 int num_pat, /* number of input patterns */
3959 char_u **pat, /* array of input patterns */
3960 int *num_file, /* resulting number of files */
3961 char_u ***file, /* array of resulting files */
3962 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963{
3964 int i;
3965 garray_T ga;
3966 char_u *p;
3967 static int recursive = FALSE;
3968 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +02003969 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02003970#if defined(FEAT_SEARCHPATH)
3971 int did_expand_in_path = FALSE;
3972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 /*
3975 * expand_env() is called to expand things like "~user". If this fails,
3976 * it calls ExpandOne(), which brings us back here. In this case, always
3977 * call the machine specific expansion function, if possible. Otherwise,
3978 * return FAIL.
3979 */
3980 if (recursive)
3981#ifdef SPECIAL_WILDCHAR
3982 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3983#else
3984 return FAIL;
3985#endif
3986
3987#ifdef SPECIAL_WILDCHAR
3988 /*
3989 * If there are any special wildcard characters which we cannot handle
3990 * here, call machine specific function for all the expansion. This
3991 * avoids starting the shell for each argument separately.
3992 * For `=expr` do use the internal function.
3993 */
3994 for (i = 0; i < num_pat; i++)
3995 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003996 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997# ifdef VIM_BACKTICK
3998 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
3999# endif
4000 )
4001 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
4002 }
4003#endif
4004
4005 recursive = TRUE;
4006
4007 /*
4008 * The matching file names are stored in a growarray. Init it empty.
4009 */
4010 ga_init2(&ga, (int)sizeof(char_u *), 30);
4011
4012 for (i = 0; i < num_pat; ++i)
4013 {
4014 add_pat = -1;
4015 p = pat[i];
4016
4017#ifdef VIM_BACKTICK
4018 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +02004019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +02004021 if (add_pat == -1)
4022 retval = FAIL;
4023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 else
4025#endif
4026 {
4027 /*
4028 * First expand environment variables, "~/" and "~user/".
4029 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02004030 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004032 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (p == NULL)
4034 p = pat[i];
4035#ifdef UNIX
4036 /*
4037 * On Unix, if expand_env() can't expand an environment
4038 * variable, use the shell to do that. Discard previously
4039 * found file names and start all over again.
4040 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02004041 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +00004044 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +02004046 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 recursive = FALSE;
4048 return i;
4049 }
4050#endif
4051 }
4052
4053 /*
4054 * If there are wildcards: Expand file names and add each match to
4055 * the list. If there is no match, and EW_NOTFOUND is given, add
4056 * the pattern.
4057 * If there are no wildcards: Add the file name if it exists or
4058 * when EW_NOTFOUND is given.
4059 */
4060 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004061 {
4062#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004063 if ((flags & EW_PATH)
4064 && !mch_isFullName(p)
4065 && !(p[0] == '.'
4066 && (vim_ispathsep(p[1])
4067 || (p[1] == '.' && vim_ispathsep(p[2]))))
4068 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004069 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004070 /* :find completion where 'path' is used.
4071 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004072 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004073 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004074 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004075 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004076 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004077 else
4078#endif
4079 add_pat = mch_expandpath(&ga, p, flags);
4080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082
4083 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
4084 {
4085 char_u *t = backslash_halve_save(p);
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 /* When EW_NOTFOUND is used, always add files and dirs. Makes
4088 * "vim c:/" work. */
4089 if (flags & EW_NOTFOUND)
4090 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +02004091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 addfile(&ga, t, flags);
4093 vim_free(t);
4094 }
4095
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02004096#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004097 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02004098 uniquefy_paths(&ga, p);
4099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (p != pat[i])
4101 vim_free(p);
4102 }
4103
4104 *num_file = ga.ga_len;
4105 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
4106
4107 recursive = FALSE;
4108
Bram Moolenaar336bd622016-01-17 18:23:58 +01004109 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110}
4111
4112# ifdef VIM_BACKTICK
4113
4114/*
4115 * Return TRUE if we can expand this backtick thing here.
4116 */
4117 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004118vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
4121}
4122
4123/*
4124 * Expand an item in `backticks` by executing it as a command.
4125 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +02004126 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 */
4128 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004129expand_backtick(
4130 garray_T *gap,
4131 char_u *pat,
4132 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 char_u *p;
4135 char_u *cmd;
4136 char_u *buffer;
4137 int cnt = 0;
4138 int i;
4139
4140 /* Create the command: lop off the backticks. */
4141 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
4142 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02004143 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145#ifdef FEAT_EVAL
4146 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004147 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 else
4149#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004150 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004151 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 vim_free(cmd);
4153 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02004154 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 cmd = buffer;
4157 while (*cmd != NUL)
4158 {
4159 cmd = skipwhite(cmd); /* skip over white space */
4160 p = cmd;
4161 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
4162 ++p;
4163 /* add an entry if it is not empty */
4164 if (p > cmd)
4165 {
4166 i = *p;
4167 *p = NUL;
4168 addfile(gap, cmd, flags);
4169 *p = i;
4170 ++cnt;
4171 }
4172 cmd = p;
4173 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
4174 ++cmd;
4175 }
4176
4177 vim_free(buffer);
4178 return cnt;
4179}
4180# endif /* VIM_BACKTICK */
4181
4182/*
4183 * Add a file to a file list. Accepted flags:
4184 * EW_DIR add directories
4185 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004186 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * EW_NOTFOUND add even when it doesn't exist
4188 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +01004189 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 */
4191 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004192addfile(
4193 garray_T *gap,
4194 char_u *f, /* filename */
4195 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197 char_u *p;
4198 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004199 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaard8b77f72015-03-05 21:21:19 +01004201 /* if the file/dir/link doesn't exist, may not add it */
4202 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +01004203 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 return;
4205
4206#ifdef FNAME_ILLEGAL
4207 /* if the file/dir contains illegal characters, don't add it */
4208 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
4209 return;
4210#endif
4211
4212 isdir = mch_isdir(f);
4213 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
4214 return;
4215
Bram Moolenaarb5971142015-03-21 17:32:19 +01004216 /* If the file isn't executable, may not add it. Do accept directories.
4217 * When invoked from expand_shellcmd() do not use $PATH. */
4218 if (!isdir && (flags & EW_EXEC)
4219 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004220 return;
4221
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 /* Make room for another item in the file list. */
4223 if (ga_grow(gap, 1) == FAIL)
4224 return;
4225
4226 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
4227 if (p == NULL)
4228 return;
4229
4230 STRCPY(p, f);
4231#ifdef BACKSLASH_IN_FILENAME
4232 slash_adjust(p);
4233#endif
4234 /*
4235 * Append a slash or backslash after directory names if none is present.
4236 */
4237#ifndef DONT_ADD_PATHSEP_TO_DIR
4238 if (isdir && (flags & EW_ADDSLASH))
4239 add_pathsep(p);
4240#endif
4241 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242}
4243#endif /* !NO_EXPANDPATH */
4244
4245#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
4246
4247#ifndef SEEK_SET
4248# define SEEK_SET 0
4249#endif
4250#ifndef SEEK_END
4251# define SEEK_END 2
4252#endif
4253
4254/*
4255 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004256 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
4257 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 * Returns an allocated string, or NULL for error.
4259 */
4260 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004261get_cmd_output(
4262 char_u *cmd,
4263 char_u *infile, /* optional input file name */
4264 int flags, /* can be SHELL_SILENT */
4265 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
4267 char_u *tempname;
4268 char_u *command;
4269 char_u *buffer = NULL;
4270 int len;
4271 int i = 0;
4272 FILE *fd;
4273
4274 if (check_restricted() || check_secure())
4275 return NULL;
4276
4277 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004278 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004280 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 return NULL;
4282 }
4283
4284 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004285 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (command == NULL)
4287 goto done;
4288
4289 /*
4290 * Call the shell to execute the command (errors are ignored).
4291 * Don't check timestamps here.
4292 */
4293 ++no_check_timestamps;
4294 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
4295 --no_check_timestamps;
4296
4297 vim_free(command);
4298
4299 /*
4300 * read the names from the file into memory
4301 */
4302# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00004303 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 fd = mch_fopen((char *)tempname, "r");
4305# else
4306 fd = mch_fopen((char *)tempname, READBIN);
4307# endif
4308
4309 if (fd == NULL)
4310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004311 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 goto done;
4313 }
4314
4315 fseek(fd, 0L, SEEK_END);
4316 len = ftell(fd); /* get size of temp file */
4317 fseek(fd, 0L, SEEK_SET);
4318
4319 buffer = alloc(len + 1);
4320 if (buffer != NULL)
4321 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
4322 fclose(fd);
4323 mch_remove(tempname);
4324 if (buffer == NULL)
4325 goto done;
4326#ifdef VMS
4327 len = i; /* VMS doesn't give us what we asked for... */
4328#endif
4329 if (i != len)
4330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004331 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004332 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004334 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004335 {
4336 /* Change NUL into SOH, otherwise the string is truncated. */
4337 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02004338 if (buffer[i] == NUL)
4339 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004340
Bram Moolenaar162bd912010-07-28 22:29:10 +02004341 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004342 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004343 else
4344 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
4346done:
4347 vim_free(tempname);
4348 return buffer;
4349}
4350#endif
4351
4352/*
4353 * Free the list of files returned by expand_wildcards() or other expansion
4354 * functions.
4355 */
4356 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004357FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004359 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 while (count--)
4362 vim_free(files[count]);
4363 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364}
4365
4366/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02004367 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 * Don't do this when still processing a command or a mapping.
4369 * Don't do this when inside a ":normal" command.
4370 */
4371 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004372goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
4374 return (p_im && stuff_empty() && typebuf_typed());
4375}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004376
4377/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02004378 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004379 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
4380 * - Remove any argument. E.g., "csh -f" -> "csh".
4381 * But don't allow a space in the path, so that this works:
4382 * "/usr/bin/csh --rcfile ~/.cshrc"
4383 * But don't do that for Windows, it's common to have a space in the path.
4384 */
4385 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004386get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004387{
4388 char_u *p;
4389
Bram Moolenaar4f974752019-02-17 17:44:42 +01004390#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004391 p = gettail(p_sh);
4392 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
4393#else
4394 p = skiptowhite(p_sh);
4395 if (*p == NUL)
4396 {
4397 /* No white space, use the tail. */
4398 p = vim_strsave(gettail(p_sh));
4399 }
4400 else
4401 {
4402 char_u *p1, *p2;
4403
4404 /* Find the last path separator before the space. */
4405 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004406 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004407 if (vim_ispathsep(*p2))
4408 p1 = p2 + 1;
4409 p = vim_strnsave(p1, (int)(p - p1));
4410 }
4411#endif
4412 return p;
4413}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01004414
4415/*
4416 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
4417 * Also check for ":\\", which MS Internet Explorer accepts, return
4418 * URL_BACKSLASH.
4419 */
4420 int
4421path_is_url(char_u *p)
4422{
4423 if (STRNCMP(p, "://", (size_t)3) == 0)
4424 return URL_SLASH;
4425 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
4426 return URL_BACKSLASH;
4427 return 0;
4428}
4429
4430/*
4431 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
4432 * Return URL_BACKSLASH for "name:\\".
4433 * Return zero otherwise.
4434 */
4435 int
4436path_with_url(char_u *fname)
4437{
4438 char_u *p;
4439
4440 for (p = fname; isalpha(*p); ++p)
4441 ;
4442 return path_is_url(p);
4443}
4444
4445/*
4446 * Return TRUE if "name" is a full (absolute) path name or URL.
4447 */
4448 int
4449vim_isAbsName(char_u *name)
4450{
4451 return (path_with_url(name) != 0 || mch_isFullName(name));
4452}
4453
4454/*
4455 * Get absolute file name into buffer "buf[len]".
4456 *
4457 * return FAIL for failure, OK otherwise
4458 */
4459 int
4460vim_FullName(
4461 char_u *fname,
4462 char_u *buf,
4463 int len,
4464 int force) /* force expansion even when already absolute */
4465{
4466 int retval = OK;
4467 int url;
4468
4469 *buf = NUL;
4470 if (fname == NULL)
4471 return FAIL;
4472
4473 url = path_with_url(fname);
4474 if (!url)
4475 retval = mch_FullName(fname, buf, len, force);
4476 if (url || retval == FAIL)
4477 {
4478 /* something failed; use the file name (truncate when too long) */
4479 vim_strncpy(buf, fname, len - 1);
4480 }
4481#if defined(MSWIN)
4482 slash_adjust(buf);
4483#endif
4484 return retval;
4485}