blob: 5dcf2684813152b2f15500a52b43fd2dac3898f9 [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
437 adjust_prop_columns(curwin->w_cursor.lnum, (colnr_T)(p - oldline),
438 ind_len - (colnr_T)(p - oldline));
439#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000440 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 }
442 else
443 vim_free(newline);
444
445 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000446 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447}
448
449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 * Return the indent of the current line after a number. Return -1 if no
451 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000452 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 */
454 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100455get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 colnr_T col;
458 pos_T pos;
459
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch_T regmatch;
461 int lead_len = 0; /* length of comment leader */
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (lnum > curbuf->b_ml.ml_line_count)
464 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000465 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200466
467#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200468 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
469 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000471#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200472 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
473 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200474 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200475 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200476
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200477 /* vim_regexec() expects a pointer to a line. This lets us
478 * start matching for the flp beyond any comment leader... */
479 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200480 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200481 pos.lnum = lnum;
482 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200483 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200484 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200485 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200486 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000487
488 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 getvcol(curwin, &pos, &col, NULL, NULL);
491 return (int)col;
492}
493
Bram Moolenaar597a4222014-06-25 14:39:50 +0200494#if defined(FEAT_LINEBREAK) || defined(PROTO)
495/*
496 * Return appropriate space number for breakindent, taking influencing
497 * parameters into account. Window must be specified, since it is not
498 * necessarily always the current one.
499 */
500 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100501get_breakindent_win(
502 win_T *wp,
503 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200504{
505 static int prev_indent = 0; /* cached indent value */
506 static long prev_ts = 0L; /* cached tabstop value */
507 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100508 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200509#ifdef FEAT_VARTABS
510 static int *prev_vts = NULL; /* cached vartabs values */
511#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 int bri = 0;
513 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200514 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200515 - ((wp->w_p_nu || wp->w_p_rnu)
516 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
517 ? number_width(wp) + 1 : 0);
518
519 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200520 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200521 || prev_tick != CHANGEDTICK(wp->w_buffer)
522#ifdef FEAT_VARTABS
523 || prev_vts != wp->w_buffer->b_p_vts_array
524#endif
525 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200526 {
527 prev_line = line;
528 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100529 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200530#ifdef FEAT_VARTABS
531 prev_vts = wp->w_buffer->b_p_vts_array;
532 prev_indent = get_indent_str_vtab(line,
533 (int)wp->w_buffer->b_p_ts,
534 wp->w_buffer->b_p_vts_array, wp->w_p_list);
535#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200536 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200537 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200538#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200539 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200540 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200541
542 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200543 if (wp->w_p_brisbr)
544 bri -= vim_strsize(p_sbr);
545
546 /* Add offset for number column, if 'n' is in 'cpoptions' */
547 bri += win_col_off2(wp);
548
549 /* never indent past left window margin */
550 if (bri < 0)
551 bri = 0;
552 /* always leave at least bri_min characters on the left,
553 * if text width is sufficient */
554 else if (bri > eff_wwidth - wp->w_p_brimin)
555 bri = (eff_wwidth - wp->w_p_brimin < 0)
556 ? 0 : eff_wwidth - wp->w_p_brimin;
557
558 return bri;
559}
560#endif
561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#if defined(FEAT_COMMENTS) || defined(PROTO)
563/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200564 * get_leader_len() returns the length in bytes of the prefix of the given
565 * string which introduces a comment. If this string is not a comment then
566 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 * When "flags" is not NULL, it is set to point to the flags of the recognized
568 * comment leader.
569 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +0200570 * If "include_space" is set, include trailing whitespace while calculating the
571 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 */
573 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100574get_leader_len(
575 char_u *line,
576 char_u **flags,
577 int backward,
578 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +0200581 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 int got_com = FALSE;
583 int found_one;
584 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
585 char_u *string; /* pointer to comment string */
586 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +0200587 int middle_match_len = 0;
588 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +0200589 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar81340392012-06-06 16:12:59 +0200591 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100592 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 ++i;
594
595 /*
596 * Repeat to match several nested comment strings.
597 */
Bram Moolenaara4271d52011-05-10 13:38:27 +0200598 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
600 /*
601 * scan through the 'comments' option for a match
602 */
603 found_one = FALSE;
604 for (list = curbuf->b_p_com; *list; )
605 {
Bram Moolenaara4271d52011-05-10 13:38:27 +0200606 /* Get one option part into part_buf[]. Advance "list" to next
607 * one. Put "string" at start of string. */
608 if (!got_com && flags != NULL)
609 *flags = list; /* remember where flags started */
610 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
612 string = vim_strchr(part_buf, ':');
613 if (string == NULL) /* missing ':', ignore this part */
614 continue;
615 *string++ = NUL; /* isolate flags from string */
616
Bram Moolenaara4271d52011-05-10 13:38:27 +0200617 /* If we found a middle match previously, use that match when this
618 * is not a middle or end. */
619 if (middle_match_len != 0
620 && vim_strchr(part_buf, COM_MIDDLE) == NULL
621 && vim_strchr(part_buf, COM_END) == NULL)
622 break;
623
624 /* When we already found a nested comment, only accept further
625 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
627 continue;
628
Bram Moolenaara4271d52011-05-10 13:38:27 +0200629 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
631 continue;
632
Bram Moolenaara4271d52011-05-10 13:38:27 +0200633 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 * When string starts with white space, must have some white space
635 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +0200636 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100637 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100639 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200640 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100641 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 ++string;
643 }
644 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
645 ;
646 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +0200647 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaara4271d52011-05-10 13:38:27 +0200649 /* When 'b' flag used, there must be white space or an
650 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100652 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 continue;
654
Bram Moolenaara4271d52011-05-10 13:38:27 +0200655 /* We have found a match, stop searching unless this is a middle
656 * comment. The middle comment can be a substring of the end
657 * comment in which case it's better to return the length of the
658 * end comment and its flags. Thus we keep searching with middle
659 * and end matches and use an end match if it matches better. */
660 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
661 {
662 if (middle_match_len == 0)
663 {
664 middle_match_len = j;
665 saved_flags = prev_list;
666 }
667 continue;
668 }
669 if (middle_match_len != 0 && j > middle_match_len)
670 /* Use this match instead of the middle match, since it's a
671 * longer thus better match. */
672 middle_match_len = 0;
673
674 if (middle_match_len == 0)
675 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 found_one = TRUE;
677 break;
678 }
679
Bram Moolenaara4271d52011-05-10 13:38:27 +0200680 if (middle_match_len != 0)
681 {
682 /* Use the previously found middle match after failing to find a
683 * match with an end. */
684 if (!got_com && flags != NULL)
685 *flags = saved_flags;
686 i += middle_match_len;
687 found_one = TRUE;
688 }
689
690 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 if (!found_one)
692 break;
693
Bram Moolenaar81340392012-06-06 16:12:59 +0200694 result = i;
695
Bram Moolenaara4271d52011-05-10 13:38:27 +0200696 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100697 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++i;
699
Bram Moolenaar81340392012-06-06 16:12:59 +0200700 if (include_space)
701 result = i;
702
Bram Moolenaara4271d52011-05-10 13:38:27 +0200703 /* If this comment doesn't nest, stop here. */
704 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 if (vim_strchr(part_buf, COM_NEST) == NULL)
706 break;
707 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200708 return result;
709}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200710
Bram Moolenaar81340392012-06-06 16:12:59 +0200711/*
712 * Return the offset at which the last comment in line starts. If there is no
713 * comment in the whole line, -1 is returned.
714 *
715 * When "flags" is not null, it is set to point to the flags describing the
716 * recognized comment leader.
717 */
718 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100719get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200720{
721 int result = -1;
722 int i, j;
723 int lower_check_bound = 0;
724 char_u *string;
725 char_u *com_leader;
726 char_u *com_flags;
727 char_u *list;
728 int found_one;
729 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
730
731 /*
732 * Repeat to match several nested comment strings.
733 */
734 i = (int)STRLEN(line);
735 while (--i >= lower_check_bound)
736 {
737 /*
738 * scan through the 'comments' option for a match
739 */
740 found_one = FALSE;
741 for (list = curbuf->b_p_com; *list; )
742 {
743 char_u *flags_save = list;
744
745 /*
746 * Get one option part into part_buf[]. Advance list to next one.
747 * put string at start of string.
748 */
749 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
750 string = vim_strchr(part_buf, ':');
751 if (string == NULL) /* If everything is fine, this cannot actually
752 * happen. */
Bram Moolenaar81340392012-06-06 16:12:59 +0200753 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200754 *string++ = NUL; /* Isolate flags from string. */
755 com_leader = string;
756
757 /*
758 * Line contents and string must match.
759 * When string starts with white space, must have some white space
760 * (but the amount does not need to match, there might be a mix of
761 * TABs and spaces).
762 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100763 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200764 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100765 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200766 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100767 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200768 ++string;
769 }
770 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
771 /* do nothing */;
772 if (string[j] != NUL)
773 continue;
774
775 /*
776 * When 'b' flag used, there must be white space or an
777 * end-of-line after the string in the line.
778 */
779 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100780 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200781 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100782
Bram Moolenaar4af72592018-12-09 15:00:52 +0100783 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100784 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100785 // For a middlepart comment, only consider it to match if
786 // everything before the current position in the line is
787 // whitespace. Otherwise we would think we are inside a
788 // comment if the middle part appears somewhere in the middle
789 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100790 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
791 ;
792 if (j < i)
793 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200794 }
795
796 /*
797 * We have found a match, stop searching.
798 */
799 found_one = TRUE;
800
801 if (flags)
802 *flags = flags_save;
803 com_flags = flags_save;
804
805 break;
806 }
807
808 if (found_one)
809 {
810 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
811 int len1, len2, off;
812
813 result = i;
814 /*
815 * If this comment nests, continue searching.
816 */
817 if (vim_strchr(part_buf, COM_NEST) != NULL)
818 continue;
819
820 lower_check_bound = i;
821
822 /* Let's verify whether the comment leader found is a substring
823 * of other comment leaders. If it is, let's adjust the
824 * lower_check_bound so that we make sure that we have determined
825 * the comment leader correctly.
826 */
827
Bram Moolenaar1c465442017-03-12 20:10:05 +0100828 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200829 ++com_leader;
830 len1 = (int)STRLEN(com_leader);
831
832 for (list = curbuf->b_p_com; *list; )
833 {
834 char_u *flags_save = list;
835
836 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
837 if (flags_save == com_flags)
838 continue;
839 string = vim_strchr(part_buf2, ':');
840 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100841 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200842 ++string;
843 len2 = (int)STRLEN(string);
844 if (len2 == 0)
845 continue;
846
847 /* Now we have to verify whether string ends with a substring
848 * beginning the com_leader. */
849 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
850 {
851 --off;
852 if (!STRNCMP(string + off, com_leader, len2 - off))
853 {
854 if (i - off < lower_check_bound)
855 lower_check_bound = i - off;
856 }
857 }
858 }
859 }
860 }
861 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
863#endif
864
865/*
866 * Return the number of window lines occupied by buffer line "lnum".
867 */
868 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100869plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 return plines_win(curwin, lnum, TRUE);
872}
873
874 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875plines_win(
876 win_T *wp,
877 linenr_T lnum,
878 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
880#if defined(FEAT_DIFF) || defined(PROTO)
881 /* Check for filler lines above this buffer line. When folded the result
882 * is one line anyway. */
883 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
884}
885
886 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100887plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
889 return plines_win_nofill(curwin, lnum, TRUE);
890}
891
892 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100893plines_win_nofill(
894 win_T *wp,
895 linenr_T lnum,
896 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898#endif
899 int lines;
900
901 if (!wp->w_p_wrap)
902 return 1;
903
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (wp->w_width == 0)
905 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907#ifdef FEAT_FOLDING
908 /* A folded lines is handled just like an empty line. */
909 /* NOTE: Caller must handle lines that are MAYBE folded. */
910 if (lineFolded(wp, lnum) == TRUE)
911 return 1;
912#endif
913
914 lines = plines_win_nofold(wp, lnum);
915 if (winheight > 0 && lines > wp->w_height)
916 return (int)wp->w_height;
917 return lines;
918}
919
920/*
921 * Return number of window lines physical line "lnum" will occupy in window
922 * "wp". Does not care about folding, 'wrap' or 'diff'.
923 */
924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100925plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 char_u *s;
928 long col;
929 int width;
930
931 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
932 if (*s == NUL) /* empty line */
933 return 1;
934 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
935
936 /*
937 * If list mode is on, then the '$' at the end of the line may take up one
938 * extra column.
939 */
940 if (wp->w_p_list && lcs_eol != NUL)
941 col += 1;
942
943 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200944 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200946 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (width <= 0)
948 return 32000;
949 if (col <= width)
950 return 1;
951 col -= width;
952 width += win_col_off2(wp);
953 return (col + (width - 1)) / width + 1;
954}
955
956/*
957 * Like plines_win(), but only reports the number of physical screen lines
958 * used from the start of the line to the given column number.
959 */
960 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100961plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
963 long col;
964 char_u *s;
965 int lines = 0;
966 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200967 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969#ifdef FEAT_DIFF
970 /* Check for filler lines above this buffer line. When folded the result
971 * is one line anyway. */
972 lines = diff_check_fill(wp, lnum);
973#endif
974
975 if (!wp->w_p_wrap)
976 return lines + 1;
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (wp->w_width == 0)
979 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar597a4222014-06-25 14:39:50 +0200981 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 col = 0;
984 while (*s != NUL && --column >= 0)
985 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200986 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100987 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989
990 /*
991 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
992 * INSERT mode, then col must be adjusted so that it represents the last
993 * screen position of the TAB. This only fixes an error when the TAB wraps
994 * from one screen line to the next (when 'columns' is not a multiple of
995 * 'ts') -- webb.
996 */
997 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200998 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001001 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 */
Bram Moolenaar02631462017-09-22 15:20:32 +02001003 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00001004 if (width <= 0)
1005 return 9999;
1006
1007 lines += 1;
1008 if (col > width)
1009 lines += (col - width) / (width + win_col_off2(wp)) + 1;
1010 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011}
1012
1013 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001014plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
1016 int count = 0;
1017
1018 while (first <= last)
1019 {
1020#ifdef FEAT_FOLDING
1021 int x;
1022
1023 /* Check if there are any really folded lines, but also included lines
1024 * that are maybe folded. */
1025 x = foldedCount(wp, first, NULL);
1026 if (x > 0)
1027 {
1028 ++count; /* count 1 for "+-- folded" line */
1029 first += x;
1030 }
1031 else
1032#endif
1033 {
1034#ifdef FEAT_DIFF
1035 if (first == wp->w_topline)
1036 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
1037 else
1038#endif
1039 count += plines_win(wp, first, TRUE);
1040 ++first;
1041 }
1042 }
1043 return (count);
1044}
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001047gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001049 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001051 /* When searching columns is sometimes put at the end of a line. */
1052 if (pos->col == MAXCOL)
1053 return NUL;
1054 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (has_mbyte)
1056 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 return (int)*ptr;
1058}
1059
1060 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001061gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (has_mbyte)
1064 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 return (int)*ml_get_cursor();
1066}
1067
1068/*
1069 * Write a character at the current cursor position.
1070 * It is directly written into the block.
1071 */
1072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001073pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
1076 + curwin->w_cursor.col) = c;
1077}
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079/*
1080 * When extra == 0: Return TRUE if the cursor is before or on the first
1081 * non-blank in the line.
1082 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1083 * the line.
1084 */
1085 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001086inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
1088 char_u *ptr;
1089 colnr_T col;
1090
Bram Moolenaar1c465442017-03-12 20:10:05 +01001091 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ++ptr;
1093 if (col >= curwin->w_cursor.col + extra)
1094 return TRUE;
1095 else
1096 return FALSE;
1097}
1098
1099/*
1100 * Skip to next part of an option argument: Skip space and comma.
1101 */
1102 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001103skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 if (*p == ',')
1106 ++p;
1107 while (*p == ' ')
1108 ++p;
1109 return p;
1110}
1111
1112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 * check_status: called when the status bars for the buffer 'buf'
1114 * need to be updated
1115 */
1116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001117check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 win_T *wp;
1120
Bram Moolenaar29323592016-07-24 22:04:11 +02001121 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (wp->w_buffer == buf && wp->w_status_height)
1123 {
1124 wp->w_redr_status = TRUE;
1125 if (must_redraw < VALID)
1126 must_redraw = VALID;
1127 }
1128}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * Ask for a reply from the user, a 'y' or a 'n'.
1132 * No other characters are accepted, the message is repeated until a valid
1133 * reply is entered or CTRL-C is hit.
1134 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
1135 * from any buffers but directly from the user.
1136 *
1137 * return the 'y' or 'n'
1138 */
1139 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001140ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141{
1142 int r = ' ';
1143 int save_State = State;
1144
1145 if (exiting) /* put terminal in raw mode for this question */
1146 settmode(TMODE_RAW);
1147 ++no_wait_return;
1148#ifdef USE_ON_FLY_SCROLL
1149 dont_scroll = TRUE; /* disallow scrolling here */
1150#endif
1151 State = CONFIRM; /* mouse behaves like with :confirm */
1152#ifdef FEAT_MOUSE
1153 setmouse(); /* disables mouse for xterm */
1154#endif
1155 ++no_mapping;
1156 ++allow_keys; /* no mapping here, but recognize keys */
1157
1158 while (r != 'y' && r != 'n')
1159 {
1160 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001161 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (direct)
1163 r = get_keystroke();
1164 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00001165 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (r == Ctrl_C || r == ESC)
1167 r = 'n';
1168 msg_putchar(r); /* show what you typed */
1169 out_flush();
1170 }
1171 --no_wait_return;
1172 State = save_State;
1173#ifdef FEAT_MOUSE
1174 setmouse();
1175#endif
1176 --no_mapping;
1177 --allow_keys;
1178
1179 return r;
1180}
1181
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001182#if defined(FEAT_MOUSE) || defined(PROTO)
1183/*
1184 * Return TRUE if "c" is a mouse key.
1185 */
1186 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001187is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001188{
1189 return c == K_LEFTMOUSE
1190 || c == K_LEFTMOUSE_NM
1191 || c == K_LEFTDRAG
1192 || c == K_LEFTRELEASE
1193 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001194 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001195 || c == K_MIDDLEMOUSE
1196 || c == K_MIDDLEDRAG
1197 || c == K_MIDDLERELEASE
1198 || c == K_RIGHTMOUSE
1199 || c == K_RIGHTDRAG
1200 || c == K_RIGHTRELEASE
1201 || c == K_MOUSEDOWN
1202 || c == K_MOUSEUP
1203 || c == K_MOUSELEFT
1204 || c == K_MOUSERIGHT
1205 || c == K_X1MOUSE
1206 || c == K_X1DRAG
1207 || c == K_X1RELEASE
1208 || c == K_X2MOUSE
1209 || c == K_X2DRAG
1210 || c == K_X2RELEASE;
1211}
1212#endif
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214/*
1215 * Get a key stroke directly from the user.
1216 * Ignores mouse clicks and scrollbar events, except a click for the left
1217 * button (used at the more prompt).
1218 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
1219 * Disadvantage: typeahead is ignored.
1220 * Translates the interrupt character for unix to ESC.
1221 */
1222 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001223get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001225 char_u *buf = NULL;
1226 int buflen = 150;
1227 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 int len = 0;
1229 int n;
1230 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001231 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
1233 mapped_ctrl_c = FALSE; /* mappings are not used here */
1234 for (;;)
1235 {
1236 cursor_on();
1237 out_flush();
1238
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001239 /* Leave some room for check_termcode() to insert a key code into (max
1240 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
1241 * bytes. */
1242 maxlen = (buflen - 6 - len) / 3;
1243 if (buf == NULL)
1244 buf = alloc(buflen);
1245 else if (maxlen < 10)
1246 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001247 char_u *t_buf = buf;
1248
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001249 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001250 * escape sequence. */
1251 buflen += 100;
1252 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001253 if (buf == NULL)
1254 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001255 maxlen = (buflen - 6 - len) / 3;
1256 }
1257 if (buf == NULL)
1258 {
1259 do_outofmem_msg((long_u)buflen);
1260 return ESC; /* panic! */
1261 }
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001264 * terminal code to complete. */
1265 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 if (n > 0)
1267 {
1268 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02001269 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001271 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00001273 else if (len > 0)
1274 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar4395a712006-09-05 18:57:57 +00001276 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001277 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00001278 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001280
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001281 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001282 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001283 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001284 {
1285 /* Redrawing was postponed, do it now. */
1286 update_screen(0);
1287 setcursor(); /* put cursor back where it belongs */
1288 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001289 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001290 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001291 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001293 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 continue;
1295
1296 /* Handle modifier and/or special key code. */
1297 n = buf[0];
1298 if (n == K_SPECIAL)
1299 {
1300 n = TO_SPECIAL(buf[1], buf[2]);
1301 if (buf[1] == KS_MODIFIER
1302 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001303#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001304 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001305#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001306#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 || n == K_VER_SCROLLBAR
1308 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#endif
1310 )
1311 {
1312 if (buf[1] == KS_MODIFIER)
1313 mod_mask = buf[2];
1314 len -= 3;
1315 if (len > 0)
1316 mch_memmove(buf, buf + 3, (size_t)len);
1317 continue;
1318 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00001319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (has_mbyte)
1322 {
1323 if (MB_BYTE2LEN(n) > len)
1324 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001325 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 n = (*mb_ptr2char)(buf);
1327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328#ifdef UNIX
1329 if (n == intr_char)
1330 n = ESC;
1331#endif
1332 break;
1333 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001334 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 mapped_ctrl_c = save_mapped_ctrl_c;
1337 return n;
1338}
1339
1340/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001341 * Get a number from the user.
1342 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 */
1344 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001345get_number(
1346 int colon, /* allow colon to abort */
1347 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int n = 0;
1350 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001351 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001353 if (mouse_used != NULL)
1354 *mouse_used = FALSE;
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 /* When not printing messages, the user won't know what to type, return a
1357 * zero (as if CR was hit). */
1358 if (msg_silent != 0)
1359 return 0;
1360
1361#ifdef USE_ON_FLY_SCROLL
1362 dont_scroll = TRUE; /* disallow scrolling here */
1363#endif
1364 ++no_mapping;
1365 ++allow_keys; /* no mapping here, but recognize keys */
1366 for (;;)
1367 {
1368 windgoto(msg_row, msg_col);
1369 c = safe_vgetc();
1370 if (VIM_ISDIGIT(c))
1371 {
1372 n = n * 10 + c - '0';
1373 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001374 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1377 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001378 if (typed > 0)
1379 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001380 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001381 --typed;
1382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001385#ifdef FEAT_MOUSE
1386 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1387 {
1388 *mouse_used = TRUE;
1389 n = mouse_row + 1;
1390 break;
1391 }
1392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 else if (n == 0 && c == ':' && colon)
1394 {
1395 stuffcharReadbuff(':');
1396 if (!exmode_active)
1397 cmdline_row = msg_row;
1398 skip_redraw = TRUE; /* skip redraw once */
1399 do_redraw = FALSE;
1400 break;
1401 }
1402 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
1403 break;
1404 }
1405 --no_mapping;
1406 --allow_keys;
1407 return n;
1408}
1409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001410/*
1411 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001412 * When "mouse_used" is not NULL allow using the mouse and in that case return
1413 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001414 */
1415 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001416prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001417{
1418 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001419 int save_cmdline_row;
1420 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001421
1422 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001423 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001424 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001425 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001426 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001427
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001428 // Set the state such that text can be selected/copied/pasted and we still
1429 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1430 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001431 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001432 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001433 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001434 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02001435#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001436 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001437 setmouse();
1438#endif
1439
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001440 i = get_number(TRUE, mouse_used);
1441 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001443 /* don't call wait_return() now */
1444 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001445 cmdline_row = msg_row - 1;
1446 need_wait_return = FALSE;
1447 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00001448 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001450 else
1451 cmdline_row = save_cmdline_row;
1452 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02001453#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001454 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001455 setmouse();
1456#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001458 return i;
1459}
1460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001462msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 long pn;
1465
1466 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1468 return;
1469
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001470 /* We don't want to overwrite another important message, but do overwrite
1471 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1472 * then "put" reports the last action. */
1473 if (keep_msg != NULL && !keep_msg_more)
1474 return;
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (n > 0)
1477 pn = n;
1478 else
1479 pn = -n;
1480
1481 if (pn > p_report)
1482 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001483 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001484 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001485 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001487 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001488 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001490 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1491 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (msg(msg_buf))
1493 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001494 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001495 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
1497 }
1498}
1499
1500/*
1501 * flush map and typeahead buffers and give a warning for an error
1502 */
1503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001504beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 if (emsg_silent == 0)
1507 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001508 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001509 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511}
1512
1513/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001514 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517vim_beep(
1518 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001520#ifdef FEAT_EVAL
1521 called_vim_beep = TRUE;
1522#endif
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (emsg_silent == 0)
1525 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001526 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1527 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001528#ifdef ELAPSED_FUNC
1529 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001530 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001531
1532 /* Only beep once per half a second, otherwise a sequence of beeps
1533 * would freeze Vim. */
1534 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1535 {
1536 did_init = TRUE;
1537 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001539 if (p_vb
1540#ifdef FEAT_GUI
1541 /* While the GUI is starting up the termcap is set for
1542 * the GUI but the output still goes to a terminal. */
1543 && !(gui.in_use && gui.starting)
1544#endif
1545 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001546 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001547 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001548#ifdef FEAT_VTP
1549 /* No restore color information, refresh the screen. */
1550 if (has_vtp_working() != 0
1551# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001552 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001553# endif
1554 )
1555 {
1556 redraw_later(CLEAR);
1557 update_screen(0);
1558 redrawcmd();
1559 }
1560#endif
1561 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001562 else
1563 out_char(BELL);
1564#ifdef ELAPSED_FUNC
1565 }
1566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001568
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001569 /* When 'debug' contains "beep" produce a message. If we are sourcing
1570 * a script or executing a function give the user a hint where the beep
1571 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001572 if (vim_strchr(p_debug, 'e') != NULL)
1573 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001574 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001575 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578}
1579
1580/*
1581 * To get the "real" home directory:
1582 * - get value of $HOME
1583 * For Unix:
1584 * - go to that directory
1585 * - do mch_dirname() to get the real name of that directory.
1586 * This also works with mounts and links.
1587 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001588 * For Windows:
1589 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591static char_u *homedir = NULL;
1592
1593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001594init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 char_u *var;
1597
Bram Moolenaar05159a02005-02-26 23:04:13 +00001598 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001599 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601#ifdef VMS
1602 var = mch_getenv((char_u *)"SYS$LOGIN");
1603#else
1604 var = mch_getenv((char_u *)"HOME");
1605#endif
1606
Bram Moolenaar4f974752019-02-17 17:44:42 +01001607#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001609 * Typically, $HOME is not defined on Windows, unless the user has
1610 * specifically defined it for Vim's sake. However, on Windows NT
1611 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1612 * each user. Try constructing $HOME from these.
1613 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001614 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001615 {
1616 char_u *homedrive, *homepath;
1617
1618 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1619 homepath = mch_getenv((char_u *)"HOMEPATH");
1620 if (homepath == NULL || *homepath == NUL)
1621 homepath = (char_u *)"\\";
1622 if (homedrive != NULL
1623 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1624 {
1625 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1626 if (NameBuff[0] != NUL)
1627 var = NameBuff;
1628 }
1629 }
1630
1631 if (var == NULL)
1632 var = mch_getenv((char_u *)"USERPROFILE");
1633
1634 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 * Weird but true: $HOME may contain an indirect reference to another
1636 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1637 * when $HOME is being set.
1638 */
1639 if (var != NULL && *var == '%')
1640 {
1641 char_u *p;
1642 char_u *exp;
1643
1644 p = vim_strchr(var + 1, '%');
1645 if (p != NULL)
1646 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001647 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 exp = mch_getenv(NameBuff);
1649 if (exp != NULL && *exp != NUL
1650 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1651 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001652 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
1655 }
1656 }
1657
Bram Moolenaar48340b62017-08-29 22:08:53 +02001658 if (var != NULL && *var == NUL) /* empty is same as not set */
1659 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar05159a02005-02-26 23:04:13 +00001661 if (enc_utf8 && var != NULL)
1662 {
1663 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001664 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001665
1666 /* Convert from active codepage to UTF-8. Other conversions are
1667 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001668 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001669 if (pp != NULL)
1670 {
1671 homedir = pp;
1672 return;
1673 }
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 /*
1677 * Default home dir is C:/
1678 * Best assumption we can make in such a situation.
1679 */
1680 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001681 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (var != NULL)
1685 {
1686#ifdef UNIX
1687 /*
1688 * Change to the directory and get the actual path. This resolves
1689 * links. Don't do it when we can't return.
1690 */
1691 if (mch_dirname(NameBuff, MAXPATHL) == OK
1692 && mch_chdir((char *)NameBuff) == 0)
1693 {
1694 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1695 var = IObuff;
1696 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001697 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699#endif
1700 homedir = vim_strsave(var);
1701 }
1702}
1703
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001704#if defined(EXITFREE) || defined(PROTO)
1705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001706free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001707{
1708 vim_free(homedir);
1709}
Bram Moolenaar24305862012-08-15 14:05:05 +02001710
1711# ifdef FEAT_CMDL_COMPL
1712 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001713free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001714{
1715 ga_clear_strings(&ga_users);
1716}
1717# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001718#endif
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001721 * Call expand_env() and store the result in an allocated string.
1722 * This is not very memory efficient, this expects the result to be freed
1723 * again soon.
1724 */
1725 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001726expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001727{
1728 return expand_env_save_opt(src, FALSE);
1729}
1730
1731/*
1732 * Idem, but when "one" is TRUE handle the string as one file name, only
1733 * expand "~" at the start.
1734 */
1735 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001736expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001737{
1738 char_u *p;
1739
1740 p = alloc(MAXPATHL);
1741 if (p != NULL)
1742 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1743 return p;
1744}
1745
1746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 * Expand environment variable with path name.
1748 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001749 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 * If anything fails no expansion is done and dst equals src.
1751 */
1752 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001753expand_env(
1754 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1755 char_u *dst, /* where to put the result */
1756 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001758 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759}
1760
1761 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001762expand_env_esc(
1763 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1764 char_u *dst, /* where to put the result */
1765 int dstlen, /* maximum length of the result */
1766 int esc, /* escape spaces in expanded variables */
1767 int one, /* "srcp" is one file name */
1768 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001770 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 char_u *tail;
1772 int c;
1773 char_u *var;
1774 int copy_char;
1775 int mustfree; /* var was allocated, need to free it later */
1776 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001777 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001779 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001780 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001781
1782 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 --dstlen; /* leave one char space for "\," */
1784 while (*src && dstlen > 0)
1785 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001786#ifdef FEAT_EVAL
1787 /* Skip over `=expr`. */
1788 if (src[0] == '`' && src[1] == '=')
1789 {
1790 size_t len;
1791
1792 var = src;
1793 src += 2;
1794 (void)skip_expr(&src);
1795 if (*src == '`')
1796 ++src;
1797 len = src - var;
1798 if (len > (size_t)dstlen)
1799 len = dstlen;
1800 vim_strncpy(dst, var, len);
1801 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001802 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001803 continue;
1804 }
1805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001807 if ((*src == '$'
1808#ifdef VMS
1809 && at_start
1810#endif
1811 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001812#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 || *src == '%'
1814#endif
1815 || (*src == '~' && at_start))
1816 {
1817 mustfree = FALSE;
1818
1819 /*
1820 * The variable name is copied into dst temporarily, because it may
1821 * be a string in read-only memory and a NUL needs to be appended.
1822 */
1823 if (*src != '~') /* environment var */
1824 {
1825 tail = src + 1;
1826 var = dst;
1827 c = dstlen - 1;
1828
1829#ifdef UNIX
1830 /* Unix has ${var-name} type environment vars */
1831 if (*tail == '{' && !vim_isIDc('{'))
1832 {
1833 tail++; /* ignore '{' */
1834 while (c-- > 0 && *tail && *tail != '}')
1835 *var++ = *tail++;
1836 }
1837 else
1838#endif
1839 {
1840 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001841#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 || (*src == '%' && *tail != '%')
1843#endif
1844 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001848#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849# ifdef UNIX
1850 if (src[1] == '{' && *tail != '}')
1851# else
1852 if (*src == '%' && *tail != '%')
1853# endif
1854 var = NULL;
1855 else
1856 {
1857# ifdef UNIX
1858 if (src[1] == '{')
1859# else
1860 if (*src == '%')
1861#endif
1862 ++tail;
1863#endif
1864 *var = NUL;
1865 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001866#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868#endif
1869 }
1870 /* home directory */
1871 else if ( src[1] == NUL
1872 || vim_ispathsep(src[1])
1873 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1874 {
1875 var = homedir;
1876 tail = src + 1;
1877 }
1878 else /* user directory */
1879 {
1880#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1881 /*
1882 * Copy ~user to dst[], so we can put a NUL after it.
1883 */
1884 tail = src;
1885 var = dst;
1886 c = dstlen - 1;
1887 while ( c-- > 0
1888 && *tail
1889 && vim_isfilec(*tail)
1890 && !vim_ispathsep(*tail))
1891 *var++ = *tail++;
1892 *var = NUL;
1893# ifdef UNIX
1894 /*
1895 * If the system supports getpwnam(), use it.
1896 * Otherwise, or if getpwnam() fails, the shell is used to
1897 * expand ~user. This is slower and may fail if the shell
1898 * does not support ~user (old versions of /bin/sh).
1899 */
1900# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1901 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001902 /* Note: memory allocated by getpwnam() is never freed.
1903 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001904 struct passwd *pw = (*dst == NUL)
1905 ? NULL : getpwnam((char *)dst + 1);
1906
1907 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 if (var == NULL)
1910# endif
1911 {
1912 expand_T xpc;
1913
1914 ExpandInit(&xpc);
1915 xpc.xp_context = EXPAND_FILES;
1916 var = ExpandOne(&xpc, dst, NULL,
1917 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 mustfree = TRUE;
1919 }
1920
1921# else /* !UNIX, thus VMS */
1922 /*
1923 * USER_HOME is a comma-separated list of
1924 * directories to search for the user account in.
1925 */
1926 {
1927 char_u test[MAXPATHL], paths[MAXPATHL];
1928 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001929 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 STRCPY(paths, USER_HOME);
1932 next_path = paths;
1933 while (*next_path)
1934 {
1935 for (path = next_path; *next_path && *next_path != ',';
1936 next_path++);
1937 if (*next_path)
1938 *next_path++ = NUL;
1939 STRCPY(test, path);
1940 STRCAT(test, "/");
1941 STRCAT(test, dst + 1);
1942 if (mch_stat(test, &st) == 0)
1943 {
1944 var = alloc(STRLEN(test) + 1);
1945 STRCPY(var, test);
1946 mustfree = TRUE;
1947 break;
1948 }
1949 }
1950 }
1951# endif /* UNIX */
1952#else
1953 /* cannot expand user's home directory, so don't try */
1954 var = NULL;
1955 tail = (char_u *)""; /* for gcc */
1956#endif /* UNIX || VMS */
1957 }
1958
1959#ifdef BACKSLASH_IN_FILENAME
1960 /* If 'shellslash' is set change backslashes to forward slashes.
1961 * Can't use slash_adjust(), p_ssl may be set temporarily. */
1962 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1963 {
1964 char_u *p = vim_strsave(var);
1965
1966 if (p != NULL)
1967 {
1968 if (mustfree)
1969 vim_free(var);
1970 var = p;
1971 mustfree = TRUE;
1972 forward_slash(var);
1973 }
1974 }
1975#endif
1976
1977 /* If "var" contains white space, escape it with a backslash.
1978 * Required for ":e ~/tt" when $HOME includes a space. */
1979 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1980 {
1981 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1982
1983 if (p != NULL)
1984 {
1985 if (mustfree)
1986 vim_free(var);
1987 var = p;
1988 mustfree = TRUE;
1989 }
1990 }
1991
1992 if (var != NULL && *var != NUL
1993 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1994 {
1995 STRCPY(dst, var);
1996 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001997 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 /* if var[] ends in a path separator and tail[] starts
1999 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002000 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
2002 && dst[-1] != ':'
2003#endif
2004 && vim_ispathsep(*tail))
2005 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002006 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 src = tail;
2008 copy_char = FALSE;
2009 }
2010 if (mustfree)
2011 vim_free(var);
2012 }
2013
2014 if (copy_char) /* copy at least one char */
2015 {
2016 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00002017 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002018 * Don't do this when "one" is TRUE, to avoid expanding "~" in
2019 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 */
2021 at_start = FALSE;
2022 if (src[0] == '\\' && src[1] != NUL)
2023 {
2024 *dst++ = *src++;
2025 --dstlen;
2026 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002027 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02002029 if (dstlen > 0)
2030 {
2031 *dst++ = *src++;
2032 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002033
Bram Moolenaar1c864092017-08-06 18:15:45 +02002034 if (startstr != NULL && src - startstr_len >= srcp
2035 && STRNCMP(src - startstr_len, startstr,
2036 startstr_len) == 0)
2037 at_start = TRUE;
2038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02002040
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
2042 *dst = NUL;
2043}
2044
2045/*
2046 * Vim's version of getenv().
2047 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00002048 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02002049 * "mustfree" is set to TRUE when returned is allocated, it must be
2050 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 */
2052 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002055 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 char_u *pend;
2057 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002058#ifdef MSWIN
2059 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002061 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (STRCMP(name, "HOME") == 0)
2063 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002065 // Use Wide function
2066 wn = enc_to_utf16(name, NULL);
2067 if (wn == NULL)
2068 return NULL;
2069
2070 wp = _wgetenv(wn);
2071 vim_free(wn);
2072
2073 if (wp != NULL && *wp == NUL) // empty is the same as not set
2074 wp = NULL;
2075
2076 if (wp != NULL)
2077 {
2078 p = utf16_to_enc(wp, NULL);
2079 if (p == NULL)
2080 return NULL;
2081
2082 *mustfree = TRUE;
2083 return p;
2084 }
2085#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002087 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 p = NULL;
2089
2090 if (p != NULL)
2091 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002094 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
2096 if (!vimruntime && STRCMP(name, "VIM") != 0)
2097 return NULL;
2098
2099 /*
2100 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
2101 * Don't do this when default_vimruntime_dir is non-empty.
2102 */
2103 if (vimruntime
2104#ifdef HAVE_PATHDEF
2105 && *default_vimruntime_dir == NUL
2106#endif
2107 )
2108 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002109#ifdef MSWIN
2110 // Use Wide function
2111 wp = _wgetenv(L"VIM");
2112 if (wp != NULL && *wp == NUL) // empty is the same as not set
2113 wp = NULL;
2114 if (wp != NULL)
2115 {
2116 char_u *q = utf16_to_enc(wp, NULL);
2117 if (q != NULL)
2118 {
2119 p = vim_version_dir(q);
2120 *mustfree = TRUE;
2121 if (p == NULL)
2122 p = q;
2123 }
2124 }
2125#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002127 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 p = NULL;
2129 if (p != NULL)
2130 {
2131 p = vim_version_dir(p);
2132 if (p != NULL)
2133 *mustfree = TRUE;
2134 else
2135 p = mch_getenv((char_u *)"VIM");
2136 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
2139
2140 /*
2141 * When expanding $VIM or $VIMRUNTIME fails, try using:
2142 * - the directory name from 'helpfile' (unless it contains '$')
2143 * - the executable name from argv[0]
2144 */
2145 if (p == NULL)
2146 {
2147 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
2148 p = p_hf;
2149#ifdef USE_EXE_NAME
2150 /*
2151 * Use the name of the executable, obtained from argv[0].
2152 */
2153 else
2154 p = exe_name;
2155#endif
2156 if (p != NULL)
2157 {
2158 /* remove the file name */
2159 pend = gettail(p);
2160
2161 /* remove "doc/" from 'helpfile', if present */
2162 if (p == p_hf)
2163 pend = remove_tail(p, pend, (char_u *)"doc");
2164
2165#ifdef USE_EXE_NAME
2166# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002167 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (p == exe_name)
2169 {
2170 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002171 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002173 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
2174 if (pend1 != pend)
2175 {
2176 pnew = alloc((unsigned)(pend1 - p) + 15);
2177 if (pnew != NULL)
2178 {
2179 STRNCPY(pnew, p, (pend1 - p));
2180 STRCPY(pnew + (pend1 - p), "Resources/vim");
2181 p = pnew;
2182 pend = p + STRLEN(p);
2183 }
2184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186# endif
2187 /* remove "src/" from exe_name, if present */
2188 if (p == exe_name)
2189 pend = remove_tail(p, pend, (char_u *)"src");
2190#endif
2191
2192 /* for $VIM, remove "runtime/" or "vim54/", if present */
2193 if (!vimruntime)
2194 {
2195 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
2196 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
2197 }
2198
2199 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002200 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002203#ifdef MACOS_X
2204 if (p == exe_name || p == p_hf)
2205#endif
2206 /* check that the result is a directory name */
2207 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01002210 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 else
2212 {
2213#ifdef USE_EXE_NAME
2214 /* may add "/vim54" or "/runtime" if it exists */
2215 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
2216 {
2217 vim_free(p);
2218 p = pend;
2219 }
2220#endif
2221 *mustfree = TRUE;
2222 }
2223 }
2224 }
2225
2226#ifdef HAVE_PATHDEF
2227 /* When there is a pathdef.c file we can use default_vim_dir and
2228 * default_vimruntime_dir */
2229 if (p == NULL)
2230 {
2231 /* Only use default_vimruntime_dir when it is not empty */
2232 if (vimruntime && *default_vimruntime_dir != NUL)
2233 {
2234 p = default_vimruntime_dir;
2235 *mustfree = FALSE;
2236 }
2237 else if (*default_vim_dir != NUL)
2238 {
2239 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
2240 *mustfree = TRUE;
2241 else
2242 {
2243 p = default_vim_dir;
2244 *mustfree = FALSE;
2245 }
2246 }
2247 }
2248#endif
2249
2250 /*
2251 * Set the environment variable, so that the new value can be found fast
2252 * next time, and others can also use it (e.g. Perl).
2253 */
2254 if (p != NULL)
2255 {
2256 if (vimruntime)
2257 {
2258 vim_setenv((char_u *)"VIMRUNTIME", p);
2259 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261 else
2262 {
2263 vim_setenv((char_u *)"VIM", p);
2264 didset_vim = TRUE;
2265 }
2266 }
2267 return p;
2268}
2269
2270/*
2271 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
2272 * Return NULL if not, return its name in allocated memory otherwise.
2273 */
2274 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002275vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 char_u *p;
2278
2279 if (vimdir == NULL || *vimdir == NUL)
2280 return NULL;
2281 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
2282 if (p != NULL && mch_isdir(p))
2283 return p;
2284 vim_free(p);
2285 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
2286 if (p != NULL && mch_isdir(p))
2287 return p;
2288 vim_free(p);
2289 return NULL;
2290}
2291
2292/*
2293 * If the string between "p" and "pend" ends in "name/", return "pend" minus
2294 * the length of "name/". Otherwise return "pend".
2295 */
2296 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002297remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 int len = (int)STRLEN(name) + 1;
2300 char_u *newend = pend - len;
2301
2302 if (newend >= p
2303 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002304 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 return newend;
2306 return pend;
2307}
2308
Bram Moolenaar113e1072019-01-20 15:30:40 +01002309#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02002310 void
2311vim_unsetenv(char_u *var)
2312{
2313#ifdef HAVE_UNSETENV
2314 unsetenv((char *)var);
2315#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02002316 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02002317#endif
2318}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002319#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02002320
2321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * Our portable version of setenv.
2324 */
2325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002326vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328#ifdef HAVE_SETENV
2329 mch_setenv((char *)name, (char *)val, 1);
2330#else
2331 char_u *envbuf;
2332
2333 /*
2334 * Putenv does not copy the string, it has to remain
2335 * valid. The allocated memory will never be freed.
2336 */
2337 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
2338 if (envbuf != NULL)
2339 {
2340 sprintf((char *)envbuf, "%s=%s", name, val);
2341 putenv((char *)envbuf);
2342 }
2343#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002344#ifdef FEAT_GETTEXT
2345 /*
2346 * When setting $VIMRUNTIME adjust the directory to find message
2347 * translations to $VIMRUNTIME/lang.
2348 */
2349 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2350 {
2351 char_u *buf = concat_str(val, (char_u *)"/lang");
2352
2353 if (buf != NULL)
2354 {
2355 bindtextdomain(VIMPACKAGE, (char *)buf);
2356 vim_free(buf);
2357 }
2358 }
2359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360}
2361
2362#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2363/*
2364 * Function given to ExpandGeneric() to obtain an environment variable name.
2365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002367get_env_name(
2368 expand_T *xp UNUSED,
2369 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
Bram Moolenaard0573012017-10-28 21:11:06 +02002371# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002373 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 */
2375 return NULL;
2376# else
2377# ifndef __WIN32__
2378 /* Borland C++ 5.2 has this in a header file. */
2379 extern char **environ;
2380# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002381# define ENVNAMELEN 100
2382 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 char_u *str;
2384 int n;
2385
2386 str = (char_u *)environ[idx];
2387 if (str == NULL)
2388 return NULL;
2389
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002390 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
2392 if (str[n] == '=' || str[n] == NUL)
2393 break;
2394 name[n] = str[n];
2395 }
2396 name[n] = NUL;
2397 return name;
2398# endif
2399}
Bram Moolenaar24305862012-08-15 14:05:05 +02002400
2401/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002402 * Add a user name to the list of users in ga_users.
2403 * Do nothing if user name is NULL or empty.
2404 */
2405 static void
2406add_user(char_u *user, int need_copy)
2407{
2408 char_u *user_copy = (user != NULL && need_copy)
2409 ? vim_strsave(user) : user;
2410
2411 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2412 {
2413 if (need_copy)
2414 vim_free(user);
2415 return;
2416 }
2417 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2418}
2419
2420/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002421 * Find all user names for user completion.
2422 * Done only once and then cached.
2423 */
2424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002425init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002426{
Bram Moolenaar24305862012-08-15 14:05:05 +02002427 static int lazy_init_done = FALSE;
2428
2429 if (lazy_init_done)
2430 return;
2431
2432 lazy_init_done = TRUE;
2433 ga_init2(&ga_users, sizeof(char_u *), 20);
2434
2435# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2436 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002437 struct passwd* pw;
2438
2439 setpwent();
2440 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002441 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002442 endpwent();
2443 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002444# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002445 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002446 DWORD nusers = 0, ntotal = 0, i;
2447 PUSER_INFO_0 uinfo;
2448
2449 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2450 &nusers, &ntotal, NULL) == NERR_Success)
2451 {
2452 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002453 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002454
2455 NetApiBufferFree(uinfo);
2456 }
2457 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002458# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002459# if defined(HAVE_GETPWNAM)
2460 {
2461 char_u *user_env = mch_getenv((char_u *)"USER");
2462
2463 // The $USER environment variable may be a valid remote user name (NIS,
2464 // LDAP) not already listed by getpwent(), as getpwent() only lists
2465 // local user names. If $USER is not already listed, check whether it
2466 // is a valid remote user name using getpwnam() and if it is, add it to
2467 // the list of user names.
2468
2469 if (user_env != NULL && *user_env != NUL)
2470 {
2471 int i;
2472
2473 for (i = 0; i < ga_users.ga_len; i++)
2474 {
2475 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2476
2477 if (STRCMP(local_user, user_env) == 0)
2478 break;
2479 }
2480
2481 if (i == ga_users.ga_len)
2482 {
2483 struct passwd *pw = getpwnam((char *)user_env);
2484
2485 if (pw != NULL)
2486 add_user((char_u *)pw->pw_name, TRUE);
2487 }
2488 }
2489 }
2490# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002491}
2492
2493/*
2494 * Function given to ExpandGeneric() to obtain an user names.
2495 */
2496 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002497get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002498{
2499 init_users();
2500 if (idx < ga_users.ga_len)
2501 return ((char_u **)ga_users.ga_data)[idx];
2502 return NULL;
2503}
2504
2505/*
2506 * Check whether name matches a user name. Return:
2507 * 0 if name does not match any user name.
2508 * 1 if name partially matches the beginning of a user name.
2509 * 2 is name fully matches a user name.
2510 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002511 int
2512match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002513{
2514 int i;
2515 int n = (int)STRLEN(name);
2516 int result = 0;
2517
2518 init_users();
2519 for (i = 0; i < ga_users.ga_len; i++)
2520 {
2521 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2522 return 2; /* full match */
2523 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2524 result = 1; /* partial match */
2525 }
2526 return result;
2527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528#endif
2529
2530/*
2531 * Replace home directory by "~" in each space or comma separated file name in
2532 * 'src'.
2533 * If anything fails (except when out of space) dst equals src.
2534 */
2535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002536home_replace(
2537 buf_T *buf, /* when not NULL, check for help files */
2538 char_u *src, /* input file name */
2539 char_u *dst, /* where to put the result */
2540 int dstlen, /* maximum length of the result */
2541 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 spaces and commas in the file name. */
2543{
2544 size_t dirlen = 0, envlen = 0;
2545 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002546 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 char_u *p;
2548
2549 if (src == NULL)
2550 {
2551 *dst = NUL;
2552 return;
2553 }
2554
2555 /*
2556 * If the file is a help file, remove the path completely.
2557 */
2558 if (buf != NULL && buf->b_help)
2559 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02002560 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 return;
2562 }
2563
2564 /*
2565 * We check both the value of the $HOME environment variable and the
2566 * "real" home directory.
2567 */
2568 if (homedir != NULL)
2569 dirlen = STRLEN(homedir);
2570
2571#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002572 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002574 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
2575#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002576#ifdef MSWIN
Bram Moolenaar48340b62017-08-29 22:08:53 +02002577 if (homedir_env == NULL)
2578 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
2579#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02002580 /* Empty is the same as not set. */
2581 if (homedir_env != NULL && *homedir_env == NUL)
2582 homedir_env = NULL;
2583
Bram Moolenaare60c2e52013-06-05 19:35:38 +02002584#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01002585 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002586 {
2587 int usedlen = 0;
2588 int flen;
2589 char_u *fbuf = NULL;
2590
2591 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02002592 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02002593 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002594 flen = (int)STRLEN(homedir_env);
2595 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
2596 /* Remove the trailing / that is added to a directory. */
2597 homedir_env[flen - 1] = NUL;
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599#endif
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 if (homedir_env != NULL)
2602 envlen = STRLEN(homedir_env);
2603
2604 if (!one)
2605 src = skipwhite(src);
2606 while (*src && dstlen > 0)
2607 {
2608 /*
2609 * Here we are at the beginning of a file name.
2610 * First, check to see if the beginning of the file name matches
2611 * $HOME or the "real" home directory. Check that there is a '/'
2612 * after the match (so that if e.g. the file is "/home/pieter/bla",
2613 * and the home directory is "/home/piet", the file does not end up
2614 * as "~er/bla" (which would seem to indicate the file "bla" in user
2615 * er's home directory)).
2616 */
2617 p = homedir;
2618 len = dirlen;
2619 for (;;)
2620 {
2621 if ( len
2622 && fnamencmp(src, p, len) == 0
2623 && (vim_ispathsep(src[len])
2624 || (!one && (src[len] == ',' || src[len] == ' '))
2625 || src[len] == NUL))
2626 {
2627 src += len;
2628 if (--dstlen > 0)
2629 *dst++ = '~';
2630
2631 /*
2632 * If it's just the home directory, add "/".
2633 */
2634 if (!vim_ispathsep(src[0]) && --dstlen > 0)
2635 *dst++ = '/';
2636 break;
2637 }
2638 if (p == homedir_env)
2639 break;
2640 p = homedir_env;
2641 len = envlen;
2642 }
2643
2644 /* if (!one) skip to separator: space or comma */
2645 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
2646 *dst++ = *src++;
2647 /* skip separator */
2648 while ((*src == ' ' || *src == ',') && --dstlen > 0)
2649 *dst++ = *src++;
2650 }
2651 /* if (dstlen == 0) out of space, what to do??? */
2652
2653 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002654
2655 if (homedir_env != homedir_env_orig)
2656 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657}
2658
2659/*
2660 * Like home_replace, store the replaced string in allocated memory.
2661 * When something fails, NULL is returned.
2662 */
2663 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002664home_replace_save(
2665 buf_T *buf, /* when not NULL, check for help files */
2666 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668 char_u *dst;
2669 unsigned len;
2670
2671 len = 3; /* space for "~/" and trailing NUL */
2672 if (src != NULL) /* just in case */
2673 len += (unsigned)STRLEN(src);
2674 dst = alloc(len);
2675 if (dst != NULL)
2676 home_replace(buf, src, dst, len, TRUE);
2677 return dst;
2678}
2679
2680/*
2681 * Compare two file names and return:
2682 * FPC_SAME if they both exist and are the same file.
2683 * FPC_SAMEX if they both don't exist and have the same file name.
2684 * FPC_DIFF if they both exist and are different files.
2685 * FPC_NOTX if they both don't exist.
2686 * FPC_DIFFX if one of them doesn't exist.
2687 * For the first name environment variables are expanded
2688 */
2689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690fullpathcmp(
2691 char_u *s1,
2692 char_u *s2,
2693 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695#ifdef UNIX
2696 char_u exp1[MAXPATHL];
2697 char_u full1[MAXPATHL];
2698 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02002699 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 int r1, r2;
2701
2702 expand_env(s1, exp1, MAXPATHL);
2703 r1 = mch_stat((char *)exp1, &st1);
2704 r2 = mch_stat((char *)s2, &st2);
2705 if (r1 != 0 && r2 != 0)
2706 {
2707 /* if mch_stat() doesn't work, may compare the names */
2708 if (checkname)
2709 {
2710 if (fnamecmp(exp1, s2) == 0)
2711 return FPC_SAMEX;
2712 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2713 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2714 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
2715 return FPC_SAMEX;
2716 }
2717 return FPC_NOTX;
2718 }
2719 if (r1 != 0 || r2 != 0)
2720 return FPC_DIFFX;
2721 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
2722 return FPC_SAME;
2723 return FPC_DIFF;
2724#else
2725 char_u *exp1; /* expanded s1 */
2726 char_u *full1; /* full path of s1 */
2727 char_u *full2; /* full path of s2 */
2728 int retval = FPC_DIFF;
2729 int r1, r2;
2730
2731 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
2732 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
2733 {
2734 full1 = exp1 + MAXPATHL;
2735 full2 = full1 + MAXPATHL;
2736
2737 expand_env(s1, exp1, MAXPATHL);
2738 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2739 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2740
2741 /* If vim_FullName() fails, the file probably doesn't exist. */
2742 if (r1 != OK && r2 != OK)
2743 {
2744 if (checkname && fnamecmp(exp1, s2) == 0)
2745 retval = FPC_SAMEX;
2746 else
2747 retval = FPC_NOTX;
2748 }
2749 else if (r1 != OK || r2 != OK)
2750 retval = FPC_DIFFX;
2751 else if (fnamecmp(full1, full2))
2752 retval = FPC_DIFF;
2753 else
2754 retval = FPC_SAME;
2755 vim_free(exp1);
2756 }
2757 return retval;
2758#endif
2759}
2760
2761/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002762 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02002763 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002764 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
2766 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002767gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
2769 char_u *p1, *p2;
2770
2771 if (fname == NULL)
2772 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01002773 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01002775 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002777 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779 return p1;
2780}
2781
2782/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002783 * Get pointer to tail of "fname", including path separators. Putting a NUL
2784 * here leaves the directory name. Takes care of "c:/" and "//".
2785 * Always returns a valid pointer.
2786 */
2787 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002788gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002789{
2790 char_u *p;
2791 char_u *t;
2792
2793 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
2794 t = gettail(fname);
2795 while (t > p && after_pathsep(fname, t))
2796 --t;
2797#ifdef VMS
2798 /* path separator is part of the path */
2799 ++t;
2800#endif
2801 return t;
2802}
2803
2804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 * get the next path component (just after the next path separator).
2806 */
2807 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002808getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002811 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (*fname)
2813 ++fname;
2814 return fname;
2815}
2816
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817/*
2818 * Get a pointer to one character past the head of a path name.
2819 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
2820 * If there is no head, path is returned.
2821 */
2822 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002823get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 char_u *retval;
2826
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002827#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 /* may skip "c:" */
2829 if (isalpha(path[0]) && path[1] == ':')
2830 retval = path + 2;
2831 else
2832 retval = path;
2833#else
2834# if defined(AMIGA)
2835 /* may skip "label:" */
2836 retval = vim_strchr(path, ':');
2837 if (retval == NULL)
2838 retval = path;
2839# else /* Unix */
2840 retval = path;
2841# endif
2842#endif
2843
2844 while (vim_ispathsep(*retval))
2845 ++retval;
2846
2847 return retval;
2848}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01002851 * Return TRUE if 'c' is a path separator.
2852 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 */
2854 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002855vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
Bram Moolenaare60acc12011-05-10 16:41:25 +02002857#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02002859#else
2860# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02002862# else
2863# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
2865 return (c == ':' || c == '[' || c == ']' || c == '/'
2866 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02002867# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02002869# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02002871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872}
2873
Bram Moolenaar69c35002013-11-04 02:54:12 +01002874/*
2875 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
2876 */
2877 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01002879{
2880 return vim_ispathsep(c)
2881#ifdef BACKSLASH_IN_FILENAME
2882 && c != ':'
2883#endif
2884 ;
2885}
2886
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002887/*
2888 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
2889 * It's done in-place.
2890 */
2891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002892shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002893{
2894 char_u *tail, *s, *d;
2895 int skip = FALSE;
2896
2897 tail = gettail(str);
2898 d = str;
2899 for (s = str; ; ++s)
2900 {
2901 if (s >= tail) /* copy the whole tail */
2902 {
2903 *d++ = *s;
2904 if (*s == NUL)
2905 break;
2906 }
2907 else if (vim_ispathsep(*s)) /* copy '/' and next char */
2908 {
2909 *d++ = *s;
2910 skip = FALSE;
2911 }
2912 else if (!skip)
2913 {
2914 *d++ = *s; /* copy next char */
2915 if (*s != '~' && *s != '.') /* and leading "~" and "." */
2916 skip = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002917 if (has_mbyte)
2918 {
2919 int l = mb_ptr2len(s);
2920
2921 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00002922 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002923 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002924 }
2925 }
2926}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002927
Bram Moolenaar900b4d72005-12-12 22:05:50 +00002928/*
2929 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
2930 * Also returns TRUE if there is no directory name.
2931 * "fname" must be writable!.
2932 */
2933 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002934dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00002935{
2936 char_u *p;
2937 int c;
2938 int retval;
2939
2940 p = gettail_sep(fname);
2941 if (p == fname)
2942 return TRUE;
2943 c = *p;
2944 *p = NUL;
2945 retval = mch_isdir(fname);
2946 *p = c;
2947 return retval;
2948}
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002951 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
2952 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
2954 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002955vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002957#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002959#else
2960 if (p_fic)
2961 return MB_STRICMP(x, y);
2962 return STRCMP(x, y);
2963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964}
2965
2966 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002967vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002969#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002970 char_u *px = x;
2971 char_u *py = y;
2972 int cx = NUL;
2973 int cy = NUL;
2974
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002975 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002977 cx = PTR2CHAR(px);
2978 cy = PTR2CHAR(py);
2979 if (cx == NUL || cy == NUL
2980 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
2981 && !(cx == '/' && cy == '\\')
2982 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002984 len -= MB_PTR2LEN(px);
2985 px += MB_PTR2LEN(px);
2986 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
2988 if (len == 0)
2989 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002990 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002991#else
2992 if (p_fic)
2993 return MB_STRNICMP(x, y, len);
2994 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002996}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998/*
2999 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00003000 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 */
3002 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003003concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 char_u *dest;
3006
3007 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
3008 if (dest != NULL)
3009 {
3010 STRCPY(dest, fname1);
3011 if (sep)
3012 add_pathsep(dest);
3013 STRCAT(dest, fname2);
3014 }
3015 return dest;
3016}
3017
Bram Moolenaard6754642005-01-17 22:18:45 +00003018/*
3019 * Concatenate two strings and return the result in allocated memory.
3020 * Returns NULL when out of memory.
3021 */
3022 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003023concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00003024{
3025 char_u *dest;
3026 size_t l = STRLEN(str1);
3027
3028 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
3029 if (dest != NULL)
3030 {
3031 STRCPY(dest, str1);
3032 STRCPY(dest + l, str2);
3033 }
3034 return dest;
3035}
Bram Moolenaard6754642005-01-17 22:18:45 +00003036
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037/*
3038 * Add a path separator to a file name, unless it already ends in a path
3039 * separator.
3040 */
3041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003042add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003044 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 STRCAT(p, PATHSEPSTR);
3046}
3047
3048/*
3049 * FullName_save - Make an allocated copy of a full file name.
3050 * Returns NULL when out of memory.
3051 */
3052 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003053FullName_save(
3054 char_u *fname,
3055 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02003056 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 char_u *buf;
3059 char_u *new_fname = NULL;
3060
3061 if (fname == NULL)
3062 return NULL;
3063
3064 buf = alloc((unsigned)MAXPATHL);
3065 if (buf != NULL)
3066 {
3067 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
3068 new_fname = vim_strsave(buf);
3069 else
3070 new_fname = vim_strsave(fname);
3071 vim_free(buf);
3072 }
3073 return new_fname;
3074}
3075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003077prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003079#if defined(SIGHUP) && defined(SIG_IGN)
3080 /* Ignore SIGHUP, because a dropped connection causes a read error, which
3081 * makes Vim exit and then handling SIGHUP causes various reentrance
3082 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003083 signal(SIGHUP, SIG_IGN);
3084#endif
3085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#ifdef FEAT_GUI
3087 if (gui.in_use)
3088 {
3089 gui.dying = TRUE;
3090 out_trash(); /* trash any pending output */
3091 }
3092 else
3093#endif
3094 {
3095 windgoto((int)Rows - 1, 0);
3096
3097 /*
3098 * Switch terminal mode back now, so messages end up on the "normal"
3099 * screen (if there are two screens).
3100 */
3101 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003102 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 out_flush();
3104 }
3105}
3106
3107/*
3108 * Preserve files and exit.
3109 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003110 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
3111 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 */
3113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003114preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 buf_T *buf;
3117
3118 prepare_to_exit();
3119
Bram Moolenaar4770d092006-01-12 23:22:24 +00003120 /* Setting this will prevent free() calls. That avoids calling free()
3121 * recursively when free() was invoked with a bad pointer. */
3122 really_exiting = TRUE;
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 out_str(IObuff);
3125 screen_start(); /* don't know where cursor is now */
3126 out_flush();
3127
3128 ml_close_notmod(); /* close all not-modified buffers */
3129
Bram Moolenaar29323592016-07-24 22:04:11 +02003130 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 {
3132 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
3133 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003134 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 screen_start(); /* don't know where cursor is now */
3136 out_flush();
3137 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3138 break;
3139 }
3140 }
3141
3142 ml_close_all(FALSE); /* close all memfiles, without deleting */
3143
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003144 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 getout(1);
3147}
3148
3149/*
3150 * return TRUE if "fname" exists.
3151 */
3152 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003153vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003155 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 if (mch_stat((char *)fname, &st))
3158 return FALSE;
3159 return TRUE;
3160}
3161
3162/*
3163 * Check for CTRL-C pressed, but only once in a while.
3164 * Should be used instead of ui_breakcheck() for functions that check for
3165 * each line in the file. Calling ui_breakcheck() each time takes too much
3166 * time, because it can be a system call.
3167 */
3168
3169#ifndef BREAKCHECK_SKIP
3170# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
3171# define BREAKCHECK_SKIP 200
3172# else
3173# define BREAKCHECK_SKIP 32
3174# endif
3175#endif
3176
3177static int breakcheck_count = 0;
3178
3179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003180line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 if (++breakcheck_count >= BREAKCHECK_SKIP)
3183 {
3184 breakcheck_count = 0;
3185 ui_breakcheck();
3186 }
3187}
3188
3189/*
3190 * Like line_breakcheck() but check 10 times less often.
3191 */
3192 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003193fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
3195 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
3196 {
3197 breakcheck_count = 0;
3198 ui_breakcheck();
3199 }
3200}
3201
3202/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00003203 * Invoke expand_wildcards() for one pattern.
3204 * Expand items like "%:h" before the expansion.
3205 * Returns OK or FAIL.
3206 */
3207 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003208expand_wildcards_eval(
3209 char_u **pat, /* pointer to input pattern */
3210 int *num_file, /* resulting number of files */
3211 char_u ***file, /* array of resulting files */
3212 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00003213{
3214 int ret = FAIL;
3215 char_u *eval_pat = NULL;
3216 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003217 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00003218 int usedlen;
3219
3220 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
3221 {
3222 ++emsg_off;
3223 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
3224 NULL, &ignored_msg, NULL);
3225 --emsg_off;
3226 if (eval_pat != NULL)
3227 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
3228 }
3229
3230 if (exp_pat != NULL)
3231 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
3232
3233 if (eval_pat != NULL)
3234 {
3235 vim_free(exp_pat);
3236 vim_free(eval_pat);
3237 }
3238
3239 return ret;
3240}
3241
3242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
3244 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003245 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
3247 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003248expand_wildcards(
3249 int num_pat, /* number of input patterns */
3250 char_u **pat, /* array of input patterns */
3251 int *num_files, /* resulting number of files */
3252 char_u ***files, /* array of resulting files */
3253 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 int retval;
3256 int i, j;
3257 char_u *p;
3258 int non_suf_match; /* number without matching suffix */
3259
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003260 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02003263 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 return retval;
3265
3266#ifdef FEAT_WILDIGN
3267 /*
3268 * Remove names that match 'wildignore'.
3269 */
3270 if (*p_wig)
3271 {
3272 char_u *ffname;
3273
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003274 /* check all files in (*files)[] */
3275 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003277 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (ffname == NULL) /* out of memory */
3279 break;
3280# ifdef VMS
3281 vms_remove_version(ffname);
3282# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003283 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01003285 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003286 vim_free((*files)[i]);
3287 for (j = i; j + 1 < *num_files; ++j)
3288 (*files)[j] = (*files)[j + 1];
3289 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 --i;
3291 }
3292 vim_free(ffname);
3293 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003294
3295 /* If the number of matches is now zero, we fail. */
3296 if (*num_files == 0)
3297 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003298 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003299 return FAIL;
3300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302#endif
3303
3304 /*
3305 * Move the names where 'suffixes' match to the end.
3306 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003307 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003310 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003312 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 /*
3315 * Move the name without matching suffix to the front
3316 * of the list.
3317 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003318 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003320 (*files)[j] = (*files)[j - 1];
3321 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323 }
3324 }
3325
3326 return retval;
3327}
3328
3329/*
3330 * Return TRUE if "fname" matches with an entry in 'suffixes'.
3331 */
3332 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003333match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 int fnamelen, setsuflen;
3336 char_u *setsuf;
3337#define MAXSUFLEN 30 /* maximum length of a file suffix */
3338 char_u suf_buf[MAXSUFLEN];
3339
3340 fnamelen = (int)STRLEN(fname);
3341 setsuflen = 0;
3342 for (setsuf = p_su; *setsuf; )
3343 {
3344 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00003345 if (setsuflen == 0)
3346 {
3347 char_u *tail = gettail(fname);
3348
3349 /* empty entry: match name without a '.' */
3350 if (vim_strchr(tail, '.') == NULL)
3351 {
3352 setsuflen = 1;
3353 break;
3354 }
3355 }
3356 else
3357 {
3358 if (fnamelen >= setsuflen
3359 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
3360 (size_t)setsuflen) == 0)
3361 break;
3362 setsuflen = 0;
3363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365 return (setsuflen != 0);
3366}
3367
3368#if !defined(NO_EXPANDPATH) || defined(PROTO)
3369
3370# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003371static int vim_backtick(char_u *p);
3372static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373# endif
3374
Bram Moolenaar4f974752019-02-17 17:44:42 +01003375# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
3377 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
3378 * it's shared between these systems.
3379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381/*
3382 * comparison function for qsort in dos_expandpath()
3383 */
Bram Moolenaareae1b912019-05-09 15:12:55 +02003384 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385pstrcmp(const void *a, const void *b)
3386{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003387 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388}
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00003391 * Recursively expand one path component into all matching files and/or
3392 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Return the number of matches found.
3394 * "path" has backslashes before chars that are not to be expanded, starting
3395 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00003396 * Return the number of matches found.
3397 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 */
3399 static int
3400dos_expandpath(
3401 garray_T *gap,
3402 char_u *path,
3403 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00003404 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00003405 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar231334e2005-07-25 20:46:57 +00003407 char_u *buf;
3408 char_u *path_end;
3409 char_u *p, *s, *e;
3410 int start_len = gap->ga_len;
3411 char_u *pat;
3412 regmatch_T regmatch;
3413 int starts_with_dot;
3414 int matches;
3415 int len;
3416 int starstar = FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003417 static int stardepth = 0; // depth for "**" expansion
3418 HANDLE hFind = INVALID_HANDLE_VALUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 WIN32_FIND_DATAW wfb;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003420 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003422 int ok;
3423
3424 /* Expanding "**" may take a long time, check for CTRL-C. */
3425 if (stardepth > 0)
3426 {
3427 ui_breakcheck();
3428 if (got_int)
3429 return 0;
3430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003432 // Make room for file name. When doing encoding conversion the actual
3433 // length may be quite a bit longer, thus use the maximum possible length.
Bram Moolenaar7314efd2015-10-31 15:32:52 +01003434 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (buf == NULL)
3436 return 0;
3437
3438 /*
3439 * Find the first part in the path name that contains a wildcard or a ~1.
3440 * Copy it into buf, including the preceding characters.
3441 */
3442 p = buf;
3443 s = buf;
3444 e = NULL;
3445 path_end = path;
3446 while (*path_end != NUL)
3447 {
3448 /* May ignore a wildcard that has a backslash before it; it will
3449 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
3450 if (path_end >= path + wildoff && rem_backslash(path_end))
3451 *p++ = *path_end++;
3452 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
3453 {
3454 if (e != NULL)
3455 break;
3456 s = p + 1;
3457 }
3458 else if (path_end >= path + wildoff
3459 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
3460 e = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (has_mbyte)
3462 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003463 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 STRNCPY(p, path_end, len);
3465 p += len;
3466 path_end += len;
3467 }
3468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 *p++ = *path_end++;
3470 }
3471 e = p;
3472 *e = NUL;
3473
3474 /* now we have one wildcard component between s and e */
3475 /* Remove backslashes between "wildoff" and the start of the wildcard
3476 * component. */
3477 for (p = buf + wildoff; p < s; ++p)
3478 if (rem_backslash(p))
3479 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003480 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 --e;
3482 --s;
3483 }
3484
Bram Moolenaar231334e2005-07-25 20:46:57 +00003485 /* Check for "**" between "s" and "e". */
3486 for (p = s; p < e; ++p)
3487 if (p[0] == '*' && p[1] == '*')
3488 starstar = TRUE;
3489
Bram Moolenaard82103e2016-01-17 17:04:05 +01003490 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3492 if (pat == NULL)
3493 {
3494 vim_free(buf);
3495 return 0;
3496 }
3497
3498 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003499 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02003500 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 regmatch.rm_ic = TRUE; /* Always ignore case */
3502 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003503 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02003504 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 vim_free(pat);
3506
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003507 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 vim_free(buf);
3510 return 0;
3511 }
3512
3513 /* remember the pattern or file name being looked for */
3514 matchname = vim_strsave(s);
3515
Bram Moolenaar231334e2005-07-25 20:46:57 +00003516 /* If "**" is by itself, this is the first time we encounter it and more
3517 * is following then find matches without any directory. */
3518 if (!didstar && stardepth < 100 && starstar && e - s == 2
3519 && *path_end == '/')
3520 {
3521 STRCPY(s, path_end + 1);
3522 ++stardepth;
3523 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3524 --stardepth;
3525 }
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 /* Scan all files in the directory with "dir/ *.*" */
3528 STRCPY(s, "*.*");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003529 wn = enc_to_utf16(buf, NULL);
3530 if (wn != NULL)
3531 hFind = FindFirstFileW(wn, &wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
3534 while (ok)
3535 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003536 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
Bram Moolenaar543c9b12019-04-05 22:50:40 +02003537 if (p == NULL)
3538 break; // out of memory
3539
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003540 // Ignore entries starting with a dot, unless when asked for. Accept
3541 // all entries found with "matchname".
Bram Moolenaard82103e2016-01-17 17:04:05 +01003542 if ((p[0] != '.' || starts_with_dot
3543 || ((flags & EW_DODOT)
3544 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003546 || (regmatch.regprog != NULL
3547 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003548 || ((flags & EW_NOTWILD)
3549 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003553
3554 if (starstar && stardepth < 100)
3555 {
3556 /* For "**" in the pattern first go deeper in the tree to
3557 * find matches. */
3558 STRCPY(buf + len, "/**");
3559 STRCPY(buf + len + 3, path_end);
3560 ++stardepth;
3561 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
3562 --stardepth;
3563 }
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 STRCPY(buf + len, path_end);
3566 if (mch_has_exp_wildcard(path_end))
3567 {
3568 /* need to expand another component of the path */
3569 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003570 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 else
3573 {
3574 /* no more wildcards, check if there is a match */
3575 /* remove backslashes for the remaining components only */
3576 if (*path_end != 0)
3577 backslash_halve(buf + len + 1);
3578 if (mch_getperm(buf) >= 0) /* add existing file */
3579 addfile(gap, buf, flags);
3580 }
3581 }
3582
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003583 vim_free(p);
3584 ok = FindNextFileW(hFind, &wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 /* If no more matches and no match was used, try expanding the name
3587 * itself. Finds the long name of a short filename. */
3588 if (!ok && matchname != NULL && gap->ga_len == start_len)
3589 {
3590 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003592 vim_free(wn);
3593 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (wn != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003595 hFind = FindFirstFileW(wn, &wfb);
3596 else
3597 hFind = INVALID_HANDLE_VALUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003599 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601 }
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02003606 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 vim_free(matchname);
3608
3609 matches = gap->ga_len - start_len;
3610 if (matches > 0)
3611 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
3612 sizeof(char_u *), pstrcmp);
3613 return matches;
3614}
3615
3616 int
3617mch_expandpath(
3618 garray_T *gap,
3619 char_u *path,
3620 int flags) /* EW_* flags */
3621{
Bram Moolenaar231334e2005-07-25 20:46:57 +00003622 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623}
Bram Moolenaar4f974752019-02-17 17:44:42 +01003624# endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaar231334e2005-07-25 20:46:57 +00003626#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
3627 || defined(PROTO)
3628/*
3629 * Unix style wildcard expansion code.
3630 * It's here because it's used both for Unix and Mac.
3631 */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003632 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003633pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003634{
3635 return (pathcmp(*(char **)a, *(char **)b, -1));
3636}
3637
3638/*
3639 * Recursively expand one path component into all matching files and/or
3640 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3641 * "path" has backslashes before chars that are not to be expanded, starting
3642 * at "path + wildoff".
3643 * Return the number of matches found.
3644 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
3645 */
3646 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003647unix_expandpath(
3648 garray_T *gap,
3649 char_u *path,
3650 int wildoff,
3651 int flags, /* EW_* flags */
3652 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003653{
3654 char_u *buf;
3655 char_u *path_end;
3656 char_u *p, *s, *e;
3657 int start_len = gap->ga_len;
3658 char_u *pat;
3659 regmatch_T regmatch;
3660 int starts_with_dot;
3661 int matches;
3662 int len;
3663 int starstar = FALSE;
3664 static int stardepth = 0; /* depth for "**" expansion */
3665
3666 DIR *dirp;
3667 struct dirent *dp;
3668
3669 /* Expanding "**" may take a long time, check for CTRL-C. */
3670 if (stardepth > 0)
3671 {
3672 ui_breakcheck();
3673 if (got_int)
3674 return 0;
3675 }
3676
3677 /* make room for file name */
3678 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
3679 if (buf == NULL)
3680 return 0;
3681
3682 /*
3683 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02003684 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00003685 * Copy it into "buf", including the preceding characters.
3686 */
3687 p = buf;
3688 s = buf;
3689 e = NULL;
3690 path_end = path;
3691 while (*path_end != NUL)
3692 {
3693 /* May ignore a wildcard that has a backslash before it; it will
3694 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
3695 if (path_end >= path + wildoff && rem_backslash(path_end))
3696 *p++ = *path_end++;
3697 else if (*path_end == '/')
3698 {
3699 if (e != NULL)
3700 break;
3701 s = p + 1;
3702 }
3703 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02003704 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003705 || (!p_fic && (flags & EW_ICASE)
3706 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00003707 e = p;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003708 if (has_mbyte)
3709 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003710 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003711 STRNCPY(p, path_end, len);
3712 p += len;
3713 path_end += len;
3714 }
3715 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00003716 *p++ = *path_end++;
3717 }
3718 e = p;
3719 *e = NUL;
3720
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003721 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003722 /* Remove backslashes between "wildoff" and the start of the wildcard
3723 * component. */
3724 for (p = buf + wildoff; p < s; ++p)
3725 if (rem_backslash(p))
3726 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003727 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003728 --e;
3729 --s;
3730 }
3731
3732 /* Check for "**" between "s" and "e". */
3733 for (p = s; p < e; ++p)
3734 if (p[0] == '*' && p[1] == '*')
3735 starstar = TRUE;
3736
3737 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +01003738 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +00003739 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3740 if (pat == NULL)
3741 {
3742 vim_free(buf);
3743 return 0;
3744 }
3745
3746 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01003747 if (flags & EW_ICASE)
3748 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
3749 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003750 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003751 if (flags & (EW_NOERROR | EW_NOTWILD))
3752 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003753 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003754 if (flags & (EW_NOERROR | EW_NOTWILD))
3755 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003756 vim_free(pat);
3757
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003758 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003759 {
3760 vim_free(buf);
3761 return 0;
3762 }
3763
3764 /* If "**" is by itself, this is the first time we encounter it and more
3765 * is following then find matches without any directory. */
3766 if (!didstar && stardepth < 100 && starstar && e - s == 2
3767 && *path_end == '/')
3768 {
3769 STRCPY(s, path_end + 1);
3770 ++stardepth;
3771 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3772 --stardepth;
3773 }
3774
3775 /* open the directory for scanning */
3776 *s = NUL;
3777 dirp = opendir(*buf == NUL ? "." : (char *)buf);
3778
3779 /* Find all matching entries */
3780 if (dirp != NULL)
3781 {
3782 for (;;)
3783 {
3784 dp = readdir(dirp);
3785 if (dp == NULL)
3786 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +01003787 if ((dp->d_name[0] != '.' || starts_with_dot
3788 || ((flags & EW_DODOT)
3789 && dp->d_name[1] != NUL
3790 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003791 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
3792 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003793 || ((flags & EW_NOTWILD)
3794 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00003795 {
3796 STRCPY(s, dp->d_name);
3797 len = STRLEN(buf);
3798
3799 if (starstar && stardepth < 100)
3800 {
3801 /* For "**" in the pattern first go deeper in the tree to
3802 * find matches. */
3803 STRCPY(buf + len, "/**");
3804 STRCPY(buf + len + 3, path_end);
3805 ++stardepth;
3806 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
3807 --stardepth;
3808 }
3809
3810 STRCPY(buf + len, path_end);
3811 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
3812 {
3813 /* need to expand another component of the path */
3814 /* remove backslashes for the remaining components only */
3815 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
3816 }
3817 else
3818 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003819 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003820
Bram Moolenaar231334e2005-07-25 20:46:57 +00003821 /* no more wildcards, check if there is a match */
3822 /* remove backslashes for the remaining components only */
3823 if (*path_end != NUL)
3824 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003825 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +01003826 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003827 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003828 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00003829#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00003830 size_t precomp_len = STRLEN(buf)+1;
3831 char_u *precomp_buf =
3832 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00003833
Bram Moolenaar231334e2005-07-25 20:46:57 +00003834 if (precomp_buf)
3835 {
3836 mch_memmove(buf, precomp_buf, precomp_len);
3837 vim_free(precomp_buf);
3838 }
3839#endif
3840 addfile(gap, buf, flags);
3841 }
3842 }
3843 }
3844 }
3845
3846 closedir(dirp);
3847 }
3848
3849 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02003850 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003851
3852 matches = gap->ga_len - start_len;
3853 if (matches > 0)
3854 qsort(((char_u **)gap->ga_data) + start_len, matches,
3855 sizeof(char_u *), pstrcmp);
3856 return matches;
3857}
3858#endif
3859
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003860#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
3861/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +02003862 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
3863 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003864 */
3865 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003866remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003867{
3868 int i;
3869 int j;
3870 char_u **fnames = (char_u **)gap->ga_data;
3871
Bram Moolenaardc685ab2010-08-13 21:16:49 +02003872 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003873 for (i = gap->ga_len - 1; i > 0; --i)
3874 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
3875 {
3876 vim_free(fnames[i]);
3877 for (j = i + 1; j < gap->ga_len; ++j)
3878 fnames[j - 1] = fnames[j];
3879 --gap->ga_len;
3880 }
3881}
3882#endif
3883
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003884/*
3885 * Return TRUE if "p" contains what looks like an environment variable.
3886 * Allowing for escaping.
3887 */
3888 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003889has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003890{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003891 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003892 {
3893 if (*p == '\\' && p[1] != NUL)
3894 ++p;
3895 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003896#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003897 "$%"
3898#else
3899 "$"
3900#endif
3901 , *p) != NULL)
3902 return TRUE;
3903 }
3904 return FALSE;
3905}
3906
3907#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003908/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +01003909 * Return TRUE if "p" contains a special wildcard character, one that Vim
3910 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003911 */
3912 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003913has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003914{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003915 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003916 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003917 // Disallow line break characters.
3918 if (*p == '\r' || *p == '\n')
3919 break;
3920 // Allow for escaping.
3921 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n')
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003922 ++p;
3923 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003924 {
3925 // A { must be followed by a matching }.
3926 if (*p == '{' && vim_strchr(p, '}') == NULL)
3927 continue;
3928 // A quote and backtick must be followed by another one.
3929 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL)
3930 continue;
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003931 return TRUE;
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003932 }
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003933 }
3934 return FALSE;
3935}
3936#endif
3937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938/*
3939 * Generic wildcard expansion code.
3940 *
3941 * Characters in "pat" that should not be expanded must be preceded with a
3942 * backslash. E.g., "/path\ with\ spaces/my\*star*"
3943 *
3944 * Return FAIL when no single file was found. In this case "num_file" is not
3945 * set, and "file" may contain an error message.
3946 * Return OK when some files found. "num_file" is set to the number of
3947 * matches, "file" to the array of matches. Call FreeWild() later.
3948 */
3949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003950gen_expand_wildcards(
3951 int num_pat, /* number of input patterns */
3952 char_u **pat, /* array of input patterns */
3953 int *num_file, /* resulting number of files */
3954 char_u ***file, /* array of resulting files */
3955 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
3957 int i;
3958 garray_T ga;
3959 char_u *p;
3960 static int recursive = FALSE;
3961 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +02003962 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02003963#if defined(FEAT_SEARCHPATH)
3964 int did_expand_in_path = FALSE;
3965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967 /*
3968 * expand_env() is called to expand things like "~user". If this fails,
3969 * it calls ExpandOne(), which brings us back here. In this case, always
3970 * call the machine specific expansion function, if possible. Otherwise,
3971 * return FAIL.
3972 */
3973 if (recursive)
3974#ifdef SPECIAL_WILDCHAR
3975 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3976#else
3977 return FAIL;
3978#endif
3979
3980#ifdef SPECIAL_WILDCHAR
3981 /*
3982 * If there are any special wildcard characters which we cannot handle
3983 * here, call machine specific function for all the expansion. This
3984 * avoids starting the shell for each argument separately.
3985 * For `=expr` do use the internal function.
3986 */
3987 for (i = 0; i < num_pat; i++)
3988 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003989 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990# ifdef VIM_BACKTICK
3991 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
3992# endif
3993 )
3994 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3995 }
3996#endif
3997
3998 recursive = TRUE;
3999
4000 /*
4001 * The matching file names are stored in a growarray. Init it empty.
4002 */
4003 ga_init2(&ga, (int)sizeof(char_u *), 30);
4004
4005 for (i = 0; i < num_pat; ++i)
4006 {
4007 add_pat = -1;
4008 p = pat[i];
4009
4010#ifdef VIM_BACKTICK
4011 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +02004012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +02004014 if (add_pat == -1)
4015 retval = FAIL;
4016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 else
4018#endif
4019 {
4020 /*
4021 * First expand environment variables, "~/" and "~user/".
4022 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02004023 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004025 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 if (p == NULL)
4027 p = pat[i];
4028#ifdef UNIX
4029 /*
4030 * On Unix, if expand_env() can't expand an environment
4031 * variable, use the shell to do that. Discard previously
4032 * found file names and start all over again.
4033 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02004034 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
4036 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +00004037 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +02004039 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 recursive = FALSE;
4041 return i;
4042 }
4043#endif
4044 }
4045
4046 /*
4047 * If there are wildcards: Expand file names and add each match to
4048 * the list. If there is no match, and EW_NOTFOUND is given, add
4049 * the pattern.
4050 * If there are no wildcards: Add the file name if it exists or
4051 * when EW_NOTFOUND is given.
4052 */
4053 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004054 {
4055#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004056 if ((flags & EW_PATH)
4057 && !mch_isFullName(p)
4058 && !(p[0] == '.'
4059 && (vim_ispathsep(p[1])
4060 || (p[1] == '.' && vim_ispathsep(p[2]))))
4061 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004062 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004063 /* :find completion where 'path' is used.
4064 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004065 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004066 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004067 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004068 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004069 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004070 else
4071#endif
4072 add_pat = mch_expandpath(&ga, p, flags);
4073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075
4076 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
4077 {
4078 char_u *t = backslash_halve_save(p);
4079
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 /* When EW_NOTFOUND is used, always add files and dirs. Makes
4081 * "vim c:/" work. */
4082 if (flags & EW_NOTFOUND)
4083 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +02004084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 addfile(&ga, t, flags);
4086 vim_free(t);
4087 }
4088
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02004089#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004090 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02004091 uniquefy_paths(&ga, p);
4092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (p != pat[i])
4094 vim_free(p);
4095 }
4096
4097 *num_file = ga.ga_len;
4098 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
4099
4100 recursive = FALSE;
4101
Bram Moolenaar336bd622016-01-17 18:23:58 +01004102 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103}
4104
4105# ifdef VIM_BACKTICK
4106
4107/*
4108 * Return TRUE if we can expand this backtick thing here.
4109 */
4110 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004111vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
4114}
4115
4116/*
4117 * Expand an item in `backticks` by executing it as a command.
4118 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +02004119 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 */
4121 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004122expand_backtick(
4123 garray_T *gap,
4124 char_u *pat,
4125 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 char_u *p;
4128 char_u *cmd;
4129 char_u *buffer;
4130 int cnt = 0;
4131 int i;
4132
4133 /* Create the command: lop off the backticks. */
4134 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
4135 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02004136 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138#ifdef FEAT_EVAL
4139 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004140 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 else
4142#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004143 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004144 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 vim_free(cmd);
4146 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02004147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 cmd = buffer;
4150 while (*cmd != NUL)
4151 {
4152 cmd = skipwhite(cmd); /* skip over white space */
4153 p = cmd;
4154 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
4155 ++p;
4156 /* add an entry if it is not empty */
4157 if (p > cmd)
4158 {
4159 i = *p;
4160 *p = NUL;
4161 addfile(gap, cmd, flags);
4162 *p = i;
4163 ++cnt;
4164 }
4165 cmd = p;
4166 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
4167 ++cmd;
4168 }
4169
4170 vim_free(buffer);
4171 return cnt;
4172}
4173# endif /* VIM_BACKTICK */
4174
4175/*
4176 * Add a file to a file list. Accepted flags:
4177 * EW_DIR add directories
4178 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004179 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * EW_NOTFOUND add even when it doesn't exist
4181 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +01004182 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
4184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185addfile(
4186 garray_T *gap,
4187 char_u *f, /* filename */
4188 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
4190 char_u *p;
4191 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004192 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaard8b77f72015-03-05 21:21:19 +01004194 /* if the file/dir/link doesn't exist, may not add it */
4195 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +01004196 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 return;
4198
4199#ifdef FNAME_ILLEGAL
4200 /* if the file/dir contains illegal characters, don't add it */
4201 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
4202 return;
4203#endif
4204
4205 isdir = mch_isdir(f);
4206 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
4207 return;
4208
Bram Moolenaarb5971142015-03-21 17:32:19 +01004209 /* If the file isn't executable, may not add it. Do accept directories.
4210 * When invoked from expand_shellcmd() do not use $PATH. */
4211 if (!isdir && (flags & EW_EXEC)
4212 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004213 return;
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 /* Make room for another item in the file list. */
4216 if (ga_grow(gap, 1) == FAIL)
4217 return;
4218
4219 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
4220 if (p == NULL)
4221 return;
4222
4223 STRCPY(p, f);
4224#ifdef BACKSLASH_IN_FILENAME
4225 slash_adjust(p);
4226#endif
4227 /*
4228 * Append a slash or backslash after directory names if none is present.
4229 */
4230#ifndef DONT_ADD_PATHSEP_TO_DIR
4231 if (isdir && (flags & EW_ADDSLASH))
4232 add_pathsep(p);
4233#endif
4234 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236#endif /* !NO_EXPANDPATH */
4237
4238#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
4239
4240#ifndef SEEK_SET
4241# define SEEK_SET 0
4242#endif
4243#ifndef SEEK_END
4244# define SEEK_END 2
4245#endif
4246
4247/*
4248 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004249 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
4250 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 * Returns an allocated string, or NULL for error.
4252 */
4253 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004254get_cmd_output(
4255 char_u *cmd,
4256 char_u *infile, /* optional input file name */
4257 int flags, /* can be SHELL_SILENT */
4258 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 char_u *tempname;
4261 char_u *command;
4262 char_u *buffer = NULL;
4263 int len;
4264 int i = 0;
4265 FILE *fd;
4266
4267 if (check_restricted() || check_secure())
4268 return NULL;
4269
4270 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004271 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004273 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return NULL;
4275 }
4276
4277 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004278 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (command == NULL)
4280 goto done;
4281
4282 /*
4283 * Call the shell to execute the command (errors are ignored).
4284 * Don't check timestamps here.
4285 */
4286 ++no_check_timestamps;
4287 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
4288 --no_check_timestamps;
4289
4290 vim_free(command);
4291
4292 /*
4293 * read the names from the file into memory
4294 */
4295# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00004296 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 fd = mch_fopen((char *)tempname, "r");
4298# else
4299 fd = mch_fopen((char *)tempname, READBIN);
4300# endif
4301
4302 if (fd == NULL)
4303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004304 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 goto done;
4306 }
4307
4308 fseek(fd, 0L, SEEK_END);
4309 len = ftell(fd); /* get size of temp file */
4310 fseek(fd, 0L, SEEK_SET);
4311
4312 buffer = alloc(len + 1);
4313 if (buffer != NULL)
4314 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
4315 fclose(fd);
4316 mch_remove(tempname);
4317 if (buffer == NULL)
4318 goto done;
4319#ifdef VMS
4320 len = i; /* VMS doesn't give us what we asked for... */
4321#endif
4322 if (i != len)
4323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004324 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004325 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004327 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004328 {
4329 /* Change NUL into SOH, otherwise the string is truncated. */
4330 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02004331 if (buffer[i] == NUL)
4332 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004333
Bram Moolenaar162bd912010-07-28 22:29:10 +02004334 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004335 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004336 else
4337 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339done:
4340 vim_free(tempname);
4341 return buffer;
4342}
4343#endif
4344
4345/*
4346 * Free the list of files returned by expand_wildcards() or other expansion
4347 * functions.
4348 */
4349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004350FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004352 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 while (count--)
4355 vim_free(files[count]);
4356 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357}
4358
4359/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02004360 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 * Don't do this when still processing a command or a mapping.
4362 * Don't do this when inside a ":normal" command.
4363 */
4364 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004365goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
4367 return (p_im && stuff_empty() && typebuf_typed());
4368}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004369
4370/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02004371 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004372 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
4373 * - Remove any argument. E.g., "csh -f" -> "csh".
4374 * But don't allow a space in the path, so that this works:
4375 * "/usr/bin/csh --rcfile ~/.cshrc"
4376 * But don't do that for Windows, it's common to have a space in the path.
4377 */
4378 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004379get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004380{
4381 char_u *p;
4382
Bram Moolenaar4f974752019-02-17 17:44:42 +01004383#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004384 p = gettail(p_sh);
4385 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
4386#else
4387 p = skiptowhite(p_sh);
4388 if (*p == NUL)
4389 {
4390 /* No white space, use the tail. */
4391 p = vim_strsave(gettail(p_sh));
4392 }
4393 else
4394 {
4395 char_u *p1, *p2;
4396
4397 /* Find the last path separator before the space. */
4398 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004399 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004400 if (vim_ispathsep(*p2))
4401 p1 = p2 + 1;
4402 p = vim_strnsave(p1, (int)(p - p1));
4403 }
4404#endif
4405 return p;
4406}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01004407
4408/*
4409 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
4410 * Also check for ":\\", which MS Internet Explorer accepts, return
4411 * URL_BACKSLASH.
4412 */
4413 int
4414path_is_url(char_u *p)
4415{
4416 if (STRNCMP(p, "://", (size_t)3) == 0)
4417 return URL_SLASH;
4418 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
4419 return URL_BACKSLASH;
4420 return 0;
4421}
4422
4423/*
4424 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
4425 * Return URL_BACKSLASH for "name:\\".
4426 * Return zero otherwise.
4427 */
4428 int
4429path_with_url(char_u *fname)
4430{
4431 char_u *p;
4432
4433 for (p = fname; isalpha(*p); ++p)
4434 ;
4435 return path_is_url(p);
4436}
4437
4438/*
4439 * Return TRUE if "name" is a full (absolute) path name or URL.
4440 */
4441 int
4442vim_isAbsName(char_u *name)
4443{
4444 return (path_with_url(name) != 0 || mch_isFullName(name));
4445}
4446
4447/*
4448 * Get absolute file name into buffer "buf[len]".
4449 *
4450 * return FAIL for failure, OK otherwise
4451 */
4452 int
4453vim_FullName(
4454 char_u *fname,
4455 char_u *buf,
4456 int len,
4457 int force) /* force expansion even when already absolute */
4458{
4459 int retval = OK;
4460 int url;
4461
4462 *buf = NUL;
4463 if (fname == NULL)
4464 return FAIL;
4465
4466 url = path_with_url(fname);
4467 if (!url)
4468 retval = mch_FullName(fname, buf, len, force);
4469 if (url || retval == FAIL)
4470 {
4471 /* something failed; use the file name (truncate when too long) */
4472 vim_strncpy(buf, fname, len - 1);
4473 }
4474#if defined(MSWIN)
4475 slash_adjust(buf);
4476#endif
4477 return retval;
4478}