blob: ed3f57e110caf793d30df363d7f8ede070674060 [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 Moolenaar0a52df52019-08-18 22:26:31 +020017#if 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 Moolenaar0a52df52019-08-18 22:26:31 +020027// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020028static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/*
31 * Count the size (in window cells) of the indent in the current line.
32 */
33 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010034get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000035{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020036#ifdef FEAT_VARTABS
37 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
38 curbuf->b_p_vts_array, FALSE);
39#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020040 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000042}
43
44/*
45 * Count the size (in window cells) of the indent in line "lnum".
46 */
47 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010048get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000049{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020050#ifdef FEAT_VARTABS
51 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
52 curbuf->b_p_vts_array, FALSE);
53#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020054 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57
58#if defined(FEAT_FOLDING) || defined(PROTO)
59/*
60 * Count the size (in window cells) of the indent in line "lnum" of buffer
61 * "buf".
62 */
63 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000065{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020066#ifdef FEAT_VARTABS
67 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
68 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
69#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020070 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000072}
73#endif
74
75/*
76 * count the size (in window cells) of the indent in line "ptr", with
77 * 'tabstop' at "ts"
78 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010080get_indent_str(
81 char_u *ptr,
82 int ts,
83 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084{
85 int count = 0;
86
87 for ( ; *ptr; ++ptr)
88 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020089 if (*ptr == TAB)
90 {
91 if (!list || lcs_tab1) /* count a tab for what it is worth */
92 count += ts - (count % ts);
93 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020094 /* In list mode, when tab is not set, count screen char width
95 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020096 count += ptr2cells(ptr);
97 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 else if (*ptr == ' ')
99 ++count; /* count a space for one */
100 else
101 break;
102 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000103 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104}
105
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200106#ifdef FEAT_VARTABS
107/*
108 * Count the size (in window cells) of the indent in line "ptr", using
109 * variable tabstops.
110 * if "list" is TRUE, count only screen size for tabs.
111 */
112 int
113get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
114{
115 int count = 0;
116
117 for ( ; *ptr; ++ptr)
118 {
119 if (*ptr == TAB) /* count a tab for what it is worth */
120 {
121 if (!list || lcs_tab1)
122 count += tabstop_padding(count, ts, vts);
123 else
124 /* In list mode, when tab is not set, count screen char width
125 * for Tab, displays: ^I */
126 count += ptr2cells(ptr);
127 }
128 else if (*ptr == ' ')
129 ++count; /* count a space for one */
130 else
131 break;
132 }
133 return count;
134}
135#endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
138 * Set the indent of the current line.
139 * Leaves the cursor on the first non-blank in the line.
140 * Caller must take care of undo.
141 * "flags":
142 * SIN_CHANGED: call changed_bytes() if the line was changed.
143 * SIN_INSERT: insert the indent in front of the line.
144 * SIN_UNDO: save line for undo before changing it.
145 * Returns TRUE if the line was changed.
146 */
147 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100148set_indent(
149 int size, /* measured in spaces */
150 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151{
152 char_u *p;
153 char_u *newline;
154 char_u *oldline;
155 char_u *s;
156 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000157 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 int line_len;
159 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000160 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200161#ifdef FEAT_VARTABS
162 int ind_col = 0;
163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000165 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000166 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000167 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169 /*
170 * First check if there is anything to do and compute the number of
171 * characters needed for the indent.
172 */
173 todo = size;
174 ind_len = 0;
175 p = oldline = ml_get_curline();
176
177 /* Calculate the buffer size for the new indent, and check to see if it
178 * isn't already set */
179
Bram Moolenaar5002c292007-07-24 13:26:15 +0000180 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
181 * 'preserveindent' are set count the number of characters at the
182 * beginning of the line to be copied */
183 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 {
185 /* If 'preserveindent' is set then reuse as much as possible of
186 * the existing indent structure for the new indent */
187 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
188 {
189 ind_done = 0;
190
191 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100192 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 {
194 if (*p == TAB)
195 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200196#ifdef FEAT_VARTABS
197 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
198 curbuf->b_p_vts_array);
199#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 tab_pad = (int)curbuf->b_p_ts
201 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 /* stop if this tab will overshoot the target */
204 if (todo < tab_pad)
205 break;
206 todo -= tab_pad;
207 ++ind_len;
208 ind_done += tab_pad;
209 }
210 else
211 {
212 --todo;
213 ++ind_len;
214 ++ind_done;
215 }
216 ++p;
217 }
218
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200219#ifdef FEAT_VARTABS
220 /* These diverge from this point. */
221 ind_col = ind_done;
222#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000223 /* Set initial number of whitespace chars to copy if we are
224 * preserving indent but expandtab is set */
225 if (curbuf->b_p_et)
226 orig_char_len = ind_len;
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200229#ifdef FEAT_VARTABS
230 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
231 curbuf->b_p_vts_array);
232#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200234#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000235 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {
237 doit = TRUE;
238 todo -= tab_pad;
239 ++ind_len;
240 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200241#ifdef FEAT_VARTABS
242 ind_col += tab_pad;
243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245 }
246
247 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200248#ifdef FEAT_VARTABS
249 for (;;)
250 {
251 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
252 curbuf->b_p_vts_array);
253 if (todo < tab_pad)
254 break;
255 if (*p != TAB)
256 doit = TRUE;
257 else
258 ++p;
259 todo -= tab_pad;
260 ++ind_len;
261 ind_col += tab_pad;
262 }
263#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 while (todo >= (int)curbuf->b_p_ts)
265 {
266 if (*p != TAB)
267 doit = TRUE;
268 else
269 ++p;
270 todo -= (int)curbuf->b_p_ts;
271 ++ind_len;
272 /* ind_done += (int)curbuf->b_p_ts; */
273 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 }
276 /* count spaces required for indent */
277 while (todo > 0)
278 {
279 if (*p != ' ')
280 doit = TRUE;
281 else
282 ++p;
283 --todo;
284 ++ind_len;
285 /* ++ind_done; */
286 }
287
288 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100289 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 return FALSE;
291
292 /* Allocate memory for the new line. */
293 if (flags & SIN_INSERT)
294 p = oldline;
295 else
296 p = skipwhite(p);
297 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000298
299 /* If 'preserveindent' and 'expandtab' are both set keep the original
300 * characters and allocate accordingly. We will fill the rest with spaces
301 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000302 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000303 {
304 newline = alloc(orig_char_len + size - ind_done + line_len);
305 if (newline == NULL)
306 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000307 todo = size - ind_done;
308 ind_len = orig_char_len + todo; /* Set total length of indent in
309 * characters, which may have been
310 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000311 p = oldline;
312 s = newline;
313 while (orig_char_len > 0)
314 {
315 *s++ = *p++;
316 orig_char_len--;
317 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000318
Bram Moolenaar5002c292007-07-24 13:26:15 +0000319 /* Skip over any additional white space (useful when newindent is less
320 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100321 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000322 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000323
Bram Moolenaar5002c292007-07-24 13:26:15 +0000324 }
325 else
326 {
327 todo = size;
328 newline = alloc(ind_len + line_len);
329 if (newline == NULL)
330 return FALSE;
331 s = newline;
332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
334 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 /* if 'expandtab' isn't set: use TABs */
336 if (!curbuf->b_p_et)
337 {
338 /* If 'preserveindent' is set then reuse as much as possible of
339 * the existing indent structure for the new indent */
340 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
341 {
342 p = oldline;
343 ind_done = 0;
344
Bram Moolenaar1c465442017-03-12 20:10:05 +0100345 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
347 if (*p == TAB)
348 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200349#ifdef FEAT_VARTABS
350 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
351 curbuf->b_p_vts_array);
352#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 tab_pad = (int)curbuf->b_p_ts
354 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 /* stop if this tab will overshoot the target */
357 if (todo < tab_pad)
358 break;
359 todo -= tab_pad;
360 ind_done += tab_pad;
361 }
362 else
363 {
364 --todo;
365 ++ind_done;
366 }
367 *s++ = *p++;
368 }
369
370 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200371#ifdef FEAT_VARTABS
372 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
373 curbuf->b_p_vts_array);
374#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 if (todo >= tab_pad)
378 {
379 *s++ = TAB;
380 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200381#ifdef FEAT_VARTABS
382 ind_done += tab_pad;
383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 }
385
386 p = skipwhite(p);
387 }
388
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200389#ifdef FEAT_VARTABS
390 for (;;)
391 {
392 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
393 curbuf->b_p_vts_array);
394 if (todo < tab_pad)
395 break;
396 *s++ = TAB;
397 todo -= tab_pad;
398 ind_done += tab_pad;
399 }
400#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 while (todo >= (int)curbuf->b_p_ts)
402 {
403 *s++ = TAB;
404 todo -= (int)curbuf->b_p_ts;
405 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408 while (todo > 0)
409 {
410 *s++ = ' ';
411 --todo;
412 }
413 mch_memmove(s, p, (size_t)line_len);
414
Bram Moolenaar663bc892019-01-08 23:07:24 +0100415 // Replace the line (unless undo fails).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
417 {
418 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
419 if (flags & SIN_CHANGED)
420 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar663bc892019-01-08 23:07:24 +0100421
422 // Correct saved cursor position if it is in this line.
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200423 if (saved_cursor.lnum == curwin->w_cursor.lnum)
424 {
425 if (saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100426 // cursor was after the indent, adjust for the number of
427 // bytes added/removed
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200428 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
429 else if (saved_cursor.col >= (colnr_T)(s - newline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100430 // cursor was in the indent, and is now after it, put it back
431 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200432 saved_cursor.col = (colnr_T)(s - newline);
433 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100434#ifdef FEAT_TEXT_PROP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200435 {
436 int added = ind_len - (colnr_T)(p - oldline);
437
438 // When increasing indent this behaves like spaces were inserted at
439 // the old indent, when decreasing indent it behaves like spaces
440 // were deleted at the new indent.
441 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200442 (colnr_T)(added > 0 ? (p - oldline) : ind_len), added, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200443 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100444#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000445 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 }
447 else
448 vim_free(newline);
449
450 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000451 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452}
453
454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 * Return the indent of the current line after a number. Return -1 if no
456 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000457 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 */
459 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100460get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 colnr_T col;
463 pos_T pos;
464
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200465 regmatch_T regmatch;
466 int lead_len = 0; /* length of comment leader */
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 if (lnum > curbuf->b_ml.ml_line_count)
469 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000470 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471
472#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200473 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
474 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200475 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000476#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200477 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
478 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200479 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200480 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200481
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200482 /* vim_regexec() expects a pointer to a line. This lets us
483 * start matching for the flp beyond any comment leader... */
484 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200485 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200486 pos.lnum = lnum;
487 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200488 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200489 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200490 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200491 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000492
493 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 getvcol(curwin, &pos, &col, NULL, NULL);
496 return (int)col;
497}
498
Bram Moolenaar597a4222014-06-25 14:39:50 +0200499#if defined(FEAT_LINEBREAK) || defined(PROTO)
500/*
501 * Return appropriate space number for breakindent, taking influencing
502 * parameters into account. Window must be specified, since it is not
503 * necessarily always the current one.
504 */
505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100506get_breakindent_win(
507 win_T *wp,
508 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200509{
510 static int prev_indent = 0; /* cached indent value */
511 static long prev_ts = 0L; /* cached tabstop value */
512 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100513 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200514#ifdef FEAT_VARTABS
515 static int *prev_vts = NULL; /* cached vartabs values */
516#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200517 int bri = 0;
518 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200519 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200520 - ((wp->w_p_nu || wp->w_p_rnu)
521 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
522 ? number_width(wp) + 1 : 0);
523
524 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200525 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200526 || prev_tick != CHANGEDTICK(wp->w_buffer)
527#ifdef FEAT_VARTABS
528 || prev_vts != wp->w_buffer->b_p_vts_array
529#endif
530 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200531 {
532 prev_line = line;
533 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100534 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200535#ifdef FEAT_VARTABS
536 prev_vts = wp->w_buffer->b_p_vts_array;
537 prev_indent = get_indent_str_vtab(line,
538 (int)wp->w_buffer->b_p_ts,
539 wp->w_buffer->b_p_vts_array, wp->w_p_list);
540#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200541 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200542 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200543#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200544 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200545 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200546
547 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200548 if (wp->w_p_brisbr)
549 bri -= vim_strsize(p_sbr);
550
551 /* Add offset for number column, if 'n' is in 'cpoptions' */
552 bri += win_col_off2(wp);
553
554 /* never indent past left window margin */
555 if (bri < 0)
556 bri = 0;
557 /* always leave at least bri_min characters on the left,
558 * if text width is sufficient */
559 else if (bri > eff_wwidth - wp->w_p_brimin)
560 bri = (eff_wwidth - wp->w_p_brimin < 0)
561 ? 0 : eff_wwidth - wp->w_p_brimin;
562
563 return bri;
564}
565#endif
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#if defined(FEAT_COMMENTS) || defined(PROTO)
568/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200569 * get_leader_len() returns the length in bytes of the prefix of the given
570 * string which introduces a comment. If this string is not a comment then
571 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 * When "flags" is not NULL, it is set to point to the flags of the recognized
573 * comment leader.
574 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +0200575 * If "include_space" is set, include trailing whitespace while calculating the
576 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 */
578 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579get_leader_len(
580 char_u *line,
581 char_u **flags,
582 int backward,
583 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +0200586 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 int got_com = FALSE;
588 int found_one;
589 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
590 char_u *string; /* pointer to comment string */
591 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +0200592 int middle_match_len = 0;
593 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +0200594 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
Bram Moolenaar81340392012-06-06 16:12:59 +0200596 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100597 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 ++i;
599
600 /*
601 * Repeat to match several nested comment strings.
602 */
Bram Moolenaara4271d52011-05-10 13:38:27 +0200603 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
605 /*
606 * scan through the 'comments' option for a match
607 */
608 found_one = FALSE;
609 for (list = curbuf->b_p_com; *list; )
610 {
Bram Moolenaara4271d52011-05-10 13:38:27 +0200611 /* Get one option part into part_buf[]. Advance "list" to next
612 * one. Put "string" at start of string. */
613 if (!got_com && flags != NULL)
614 *flags = list; /* remember where flags started */
615 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
617 string = vim_strchr(part_buf, ':');
618 if (string == NULL) /* missing ':', ignore this part */
619 continue;
620 *string++ = NUL; /* isolate flags from string */
621
Bram Moolenaara4271d52011-05-10 13:38:27 +0200622 /* If we found a middle match previously, use that match when this
623 * is not a middle or end. */
624 if (middle_match_len != 0
625 && vim_strchr(part_buf, COM_MIDDLE) == NULL
626 && vim_strchr(part_buf, COM_END) == NULL)
627 break;
628
629 /* When we already found a nested comment, only accept further
630 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
632 continue;
633
Bram Moolenaara4271d52011-05-10 13:38:27 +0200634 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
636 continue;
637
Bram Moolenaara4271d52011-05-10 13:38:27 +0200638 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 * When string starts with white space, must have some white space
640 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +0200641 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100642 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100644 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200645 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100646 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 ++string;
648 }
649 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
650 ;
651 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +0200652 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaara4271d52011-05-10 13:38:27 +0200654 /* When 'b' flag used, there must be white space or an
655 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100657 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 continue;
659
Bram Moolenaara4271d52011-05-10 13:38:27 +0200660 /* We have found a match, stop searching unless this is a middle
661 * comment. The middle comment can be a substring of the end
662 * comment in which case it's better to return the length of the
663 * end comment and its flags. Thus we keep searching with middle
664 * and end matches and use an end match if it matches better. */
665 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
666 {
667 if (middle_match_len == 0)
668 {
669 middle_match_len = j;
670 saved_flags = prev_list;
671 }
672 continue;
673 }
674 if (middle_match_len != 0 && j > middle_match_len)
675 /* Use this match instead of the middle match, since it's a
676 * longer thus better match. */
677 middle_match_len = 0;
678
679 if (middle_match_len == 0)
680 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 found_one = TRUE;
682 break;
683 }
684
Bram Moolenaara4271d52011-05-10 13:38:27 +0200685 if (middle_match_len != 0)
686 {
687 /* Use the previously found middle match after failing to find a
688 * match with an end. */
689 if (!got_com && flags != NULL)
690 *flags = saved_flags;
691 i += middle_match_len;
692 found_one = TRUE;
693 }
694
695 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (!found_one)
697 break;
698
Bram Moolenaar81340392012-06-06 16:12:59 +0200699 result = i;
700
Bram Moolenaara4271d52011-05-10 13:38:27 +0200701 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100702 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 ++i;
704
Bram Moolenaar81340392012-06-06 16:12:59 +0200705 if (include_space)
706 result = i;
707
Bram Moolenaara4271d52011-05-10 13:38:27 +0200708 /* If this comment doesn't nest, stop here. */
709 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 if (vim_strchr(part_buf, COM_NEST) == NULL)
711 break;
712 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200713 return result;
714}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200715
Bram Moolenaar81340392012-06-06 16:12:59 +0200716/*
717 * Return the offset at which the last comment in line starts. If there is no
718 * comment in the whole line, -1 is returned.
719 *
720 * When "flags" is not null, it is set to point to the flags describing the
721 * recognized comment leader.
722 */
723 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200725{
726 int result = -1;
727 int i, j;
728 int lower_check_bound = 0;
729 char_u *string;
730 char_u *com_leader;
731 char_u *com_flags;
732 char_u *list;
733 int found_one;
734 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
735
736 /*
737 * Repeat to match several nested comment strings.
738 */
739 i = (int)STRLEN(line);
740 while (--i >= lower_check_bound)
741 {
742 /*
743 * scan through the 'comments' option for a match
744 */
745 found_one = FALSE;
746 for (list = curbuf->b_p_com; *list; )
747 {
748 char_u *flags_save = list;
749
750 /*
751 * Get one option part into part_buf[]. Advance list to next one.
752 * put string at start of string.
753 */
754 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
755 string = vim_strchr(part_buf, ':');
756 if (string == NULL) /* If everything is fine, this cannot actually
757 * happen. */
Bram Moolenaar81340392012-06-06 16:12:59 +0200758 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200759 *string++ = NUL; /* Isolate flags from string. */
760 com_leader = string;
761
762 /*
763 * Line contents and string must match.
764 * When string starts with white space, must have some white space
765 * (but the amount does not need to match, there might be a mix of
766 * TABs and spaces).
767 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100768 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200769 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100770 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200771 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100772 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200773 ++string;
774 }
775 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
776 /* do nothing */;
777 if (string[j] != NUL)
778 continue;
779
780 /*
781 * When 'b' flag used, there must be white space or an
782 * end-of-line after the string in the line.
783 */
784 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100785 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200786 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100787
Bram Moolenaar4af72592018-12-09 15:00:52 +0100788 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100789 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100790 // For a middlepart comment, only consider it to match if
791 // everything before the current position in the line is
792 // whitespace. Otherwise we would think we are inside a
793 // comment if the middle part appears somewhere in the middle
794 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100795 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
796 ;
797 if (j < i)
798 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200799 }
800
801 /*
802 * We have found a match, stop searching.
803 */
804 found_one = TRUE;
805
806 if (flags)
807 *flags = flags_save;
808 com_flags = flags_save;
809
810 break;
811 }
812
813 if (found_one)
814 {
815 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
816 int len1, len2, off;
817
818 result = i;
819 /*
820 * If this comment nests, continue searching.
821 */
822 if (vim_strchr(part_buf, COM_NEST) != NULL)
823 continue;
824
825 lower_check_bound = i;
826
827 /* Let's verify whether the comment leader found is a substring
828 * of other comment leaders. If it is, let's adjust the
829 * lower_check_bound so that we make sure that we have determined
830 * the comment leader correctly.
831 */
832
Bram Moolenaar1c465442017-03-12 20:10:05 +0100833 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200834 ++com_leader;
835 len1 = (int)STRLEN(com_leader);
836
837 for (list = curbuf->b_p_com; *list; )
838 {
839 char_u *flags_save = list;
840
841 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
842 if (flags_save == com_flags)
843 continue;
844 string = vim_strchr(part_buf2, ':');
845 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100846 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200847 ++string;
848 len2 = (int)STRLEN(string);
849 if (len2 == 0)
850 continue;
851
852 /* Now we have to verify whether string ends with a substring
853 * beginning the com_leader. */
854 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
855 {
856 --off;
857 if (!STRNCMP(string + off, com_leader, len2 - off))
858 {
859 if (i - off < lower_check_bound)
860 lower_check_bound = i - off;
861 }
862 }
863 }
864 }
865 }
866 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868#endif
869
870/*
871 * Return the number of window lines occupied by buffer line "lnum".
872 */
873 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100874plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
876 return plines_win(curwin, lnum, TRUE);
877}
878
879 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100880plines_win(
881 win_T *wp,
882 linenr_T lnum,
883 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885#if defined(FEAT_DIFF) || defined(PROTO)
886 /* Check for filler lines above this buffer line. When folded the result
887 * is one line anyway. */
888 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
889}
890
891 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100892plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 return plines_win_nofill(curwin, lnum, TRUE);
895}
896
897 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100898plines_win_nofill(
899 win_T *wp,
900 linenr_T lnum,
901 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902{
903#endif
904 int lines;
905
906 if (!wp->w_p_wrap)
907 return 1;
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (wp->w_width == 0)
910 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912#ifdef FEAT_FOLDING
913 /* A folded lines is handled just like an empty line. */
914 /* NOTE: Caller must handle lines that are MAYBE folded. */
915 if (lineFolded(wp, lnum) == TRUE)
916 return 1;
917#endif
918
919 lines = plines_win_nofold(wp, lnum);
920 if (winheight > 0 && lines > wp->w_height)
921 return (int)wp->w_height;
922 return lines;
923}
924
925/*
926 * Return number of window lines physical line "lnum" will occupy in window
927 * "wp". Does not care about folding, 'wrap' or 'diff'.
928 */
929 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100930plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 char_u *s;
933 long col;
934 int width;
935
936 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
937 if (*s == NUL) /* empty line */
938 return 1;
939 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
940
941 /*
942 * If list mode is on, then the '$' at the end of the line may take up one
943 * extra column.
944 */
945 if (wp->w_p_list && lcs_eol != NUL)
946 col += 1;
947
948 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200949 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200951 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (width <= 0)
953 return 32000;
954 if (col <= width)
955 return 1;
956 col -= width;
957 width += win_col_off2(wp);
958 return (col + (width - 1)) / width + 1;
959}
960
961/*
962 * Like plines_win(), but only reports the number of physical screen lines
963 * used from the start of the line to the given column number.
964 */
965 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100966plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968 long col;
969 char_u *s;
970 int lines = 0;
971 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200972 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974#ifdef FEAT_DIFF
975 /* Check for filler lines above this buffer line. When folded the result
976 * is one line anyway. */
977 lines = diff_check_fill(wp, lnum);
978#endif
979
980 if (!wp->w_p_wrap)
981 return lines + 1;
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (wp->w_width == 0)
984 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
Bram Moolenaar597a4222014-06-25 14:39:50 +0200986 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 col = 0;
989 while (*s != NUL && --column >= 0)
990 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200991 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100992 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994
995 /*
996 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
997 * INSERT mode, then col must be adjusted so that it represents the last
998 * screen position of the TAB. This only fixes an error when the TAB wraps
999 * from one screen line to the next (when 'columns' is not a multiple of
1000 * 'ts') -- webb.
1001 */
1002 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02001003 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001006 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 */
Bram Moolenaar02631462017-09-22 15:20:32 +02001008 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00001009 if (width <= 0)
1010 return 9999;
1011
1012 lines += 1;
1013 if (col > width)
1014 lines += (col - width) / (width + win_col_off2(wp)) + 1;
1015 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016}
1017
1018 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001019plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 int count = 0;
1022
1023 while (first <= last)
1024 {
1025#ifdef FEAT_FOLDING
1026 int x;
1027
1028 /* Check if there are any really folded lines, but also included lines
1029 * that are maybe folded. */
1030 x = foldedCount(wp, first, NULL);
1031 if (x > 0)
1032 {
1033 ++count; /* count 1 for "+-- folded" line */
1034 first += x;
1035 }
1036 else
1037#endif
1038 {
1039#ifdef FEAT_DIFF
1040 if (first == wp->w_topline)
1041 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
1042 else
1043#endif
1044 count += plines_win(wp, first, TRUE);
1045 ++first;
1046 }
1047 }
1048 return (count);
1049}
1050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001052gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001054 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001056 /* When searching columns is sometimes put at the end of a line. */
1057 if (pos->col == MAXCOL)
1058 return NUL;
1059 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (has_mbyte)
1061 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 return (int)*ptr;
1063}
1064
1065 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001066gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (has_mbyte)
1069 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 return (int)*ml_get_cursor();
1071}
1072
1073/*
1074 * Write a character at the current cursor position.
1075 * It is directly written into the block.
1076 */
1077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001078pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
1080 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
1081 + curwin->w_cursor.col) = c;
1082}
1083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084/*
1085 * When extra == 0: Return TRUE if the cursor is before or on the first
1086 * non-blank in the line.
1087 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1088 * the line.
1089 */
1090 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001091inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
1093 char_u *ptr;
1094 colnr_T col;
1095
Bram Moolenaar1c465442017-03-12 20:10:05 +01001096 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 ++ptr;
1098 if (col >= curwin->w_cursor.col + extra)
1099 return TRUE;
1100 else
1101 return FALSE;
1102}
1103
1104/*
1105 * Skip to next part of an option argument: Skip space and comma.
1106 */
1107 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 if (*p == ',')
1111 ++p;
1112 while (*p == ' ')
1113 ++p;
1114 return p;
1115}
1116
1117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 * check_status: called when the status bars for the buffer 'buf'
1119 * need to be updated
1120 */
1121 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001122check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 win_T *wp;
1125
Bram Moolenaar29323592016-07-24 22:04:11 +02001126 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (wp->w_buffer == buf && wp->w_status_height)
1128 {
1129 wp->w_redr_status = TRUE;
1130 if (must_redraw < VALID)
1131 must_redraw = VALID;
1132 }
1133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * Ask for a reply from the user, a 'y' or a 'n'.
1137 * No other characters are accepted, the message is repeated until a valid
1138 * reply is entered or CTRL-C is hit.
1139 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
1140 * from any buffers but directly from the user.
1141 *
1142 * return the 'y' or 'n'
1143 */
1144 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001145ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
1147 int r = ' ';
1148 int save_State = State;
1149
1150 if (exiting) /* put terminal in raw mode for this question */
1151 settmode(TMODE_RAW);
1152 ++no_wait_return;
1153#ifdef USE_ON_FLY_SCROLL
1154 dont_scroll = TRUE; /* disallow scrolling here */
1155#endif
1156 State = CONFIRM; /* mouse behaves like with :confirm */
1157#ifdef FEAT_MOUSE
1158 setmouse(); /* disables mouse for xterm */
1159#endif
1160 ++no_mapping;
1161 ++allow_keys; /* no mapping here, but recognize keys */
1162
1163 while (r != 'y' && r != 'n')
1164 {
1165 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001166 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (direct)
1168 r = get_keystroke();
1169 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00001170 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (r == Ctrl_C || r == ESC)
1172 r = 'n';
1173 msg_putchar(r); /* show what you typed */
1174 out_flush();
1175 }
1176 --no_wait_return;
1177 State = save_State;
1178#ifdef FEAT_MOUSE
1179 setmouse();
1180#endif
1181 --no_mapping;
1182 --allow_keys;
1183
1184 return r;
1185}
1186
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001187#if defined(FEAT_MOUSE) || defined(PROTO)
1188/*
1189 * Return TRUE if "c" is a mouse key.
1190 */
1191 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001192is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001193{
1194 return c == K_LEFTMOUSE
1195 || c == K_LEFTMOUSE_NM
1196 || c == K_LEFTDRAG
1197 || c == K_LEFTRELEASE
1198 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001199 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001200 || c == K_MIDDLEMOUSE
1201 || c == K_MIDDLEDRAG
1202 || c == K_MIDDLERELEASE
1203 || c == K_RIGHTMOUSE
1204 || c == K_RIGHTDRAG
1205 || c == K_RIGHTRELEASE
1206 || c == K_MOUSEDOWN
1207 || c == K_MOUSEUP
1208 || c == K_MOUSELEFT
1209 || c == K_MOUSERIGHT
1210 || c == K_X1MOUSE
1211 || c == K_X1DRAG
1212 || c == K_X1RELEASE
1213 || c == K_X2MOUSE
1214 || c == K_X2DRAG
1215 || c == K_X2RELEASE;
1216}
1217#endif
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219/*
1220 * Get a key stroke directly from the user.
1221 * Ignores mouse clicks and scrollbar events, except a click for the left
1222 * button (used at the more prompt).
1223 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
1224 * Disadvantage: typeahead is ignored.
1225 * Translates the interrupt character for unix to ESC.
1226 */
1227 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001228get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001230 char_u *buf = NULL;
1231 int buflen = 150;
1232 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 int len = 0;
1234 int n;
1235 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001236 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238 mapped_ctrl_c = FALSE; /* mappings are not used here */
1239 for (;;)
1240 {
1241 cursor_on();
1242 out_flush();
1243
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001244 /* Leave some room for check_termcode() to insert a key code into (max
1245 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
1246 * bytes. */
1247 maxlen = (buflen - 6 - len) / 3;
1248 if (buf == NULL)
1249 buf = alloc(buflen);
1250 else if (maxlen < 10)
1251 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001252 char_u *t_buf = buf;
1253
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001254 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001255 * escape sequence. */
1256 buflen += 100;
1257 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001258 if (buf == NULL)
1259 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001260 maxlen = (buflen - 6 - len) / 3;
1261 }
1262 if (buf == NULL)
1263 {
1264 do_outofmem_msg((long_u)buflen);
1265 return ESC; /* panic! */
1266 }
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001269 * terminal code to complete. */
1270 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (n > 0)
1272 {
1273 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02001274 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001276 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00001278 else if (len > 0)
1279 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
Bram Moolenaar4395a712006-09-05 18:57:57 +00001281 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001282 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00001283 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001285
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001286 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001287 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001288 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001289 {
1290 /* Redrawing was postponed, do it now. */
1291 update_screen(0);
1292 setcursor(); /* put cursor back where it belongs */
1293 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001294 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001295 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001296 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001298 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 continue;
1300
1301 /* Handle modifier and/or special key code. */
1302 n = buf[0];
1303 if (n == K_SPECIAL)
1304 {
1305 n = TO_SPECIAL(buf[1], buf[2]);
1306 if (buf[1] == KS_MODIFIER
1307 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001308#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001309 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001310#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001311#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 || n == K_VER_SCROLLBAR
1313 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#endif
1315 )
1316 {
1317 if (buf[1] == KS_MODIFIER)
1318 mod_mask = buf[2];
1319 len -= 3;
1320 if (len > 0)
1321 mch_memmove(buf, buf + 3, (size_t)len);
1322 continue;
1323 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00001324 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (has_mbyte)
1327 {
1328 if (MB_BYTE2LEN(n) > len)
1329 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001330 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 n = (*mb_ptr2char)(buf);
1332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#ifdef UNIX
1334 if (n == intr_char)
1335 n = ESC;
1336#endif
1337 break;
1338 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001339 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 mapped_ctrl_c = save_mapped_ctrl_c;
1342 return n;
1343}
1344
1345/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001346 * Get a number from the user.
1347 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001350get_number(
1351 int colon, /* allow colon to abort */
1352 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 int n = 0;
1355 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001356 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001358 if (mouse_used != NULL)
1359 *mouse_used = FALSE;
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 /* When not printing messages, the user won't know what to type, return a
1362 * zero (as if CR was hit). */
1363 if (msg_silent != 0)
1364 return 0;
1365
1366#ifdef USE_ON_FLY_SCROLL
1367 dont_scroll = TRUE; /* disallow scrolling here */
1368#endif
1369 ++no_mapping;
1370 ++allow_keys; /* no mapping here, but recognize keys */
1371 for (;;)
1372 {
1373 windgoto(msg_row, msg_col);
1374 c = safe_vgetc();
1375 if (VIM_ISDIGIT(c))
1376 {
1377 n = n * 10 + c - '0';
1378 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001379 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1382 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001383 if (typed > 0)
1384 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001385 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001386 --typed;
1387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001390#ifdef FEAT_MOUSE
1391 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1392 {
1393 *mouse_used = TRUE;
1394 n = mouse_row + 1;
1395 break;
1396 }
1397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 else if (n == 0 && c == ':' && colon)
1399 {
1400 stuffcharReadbuff(':');
1401 if (!exmode_active)
1402 cmdline_row = msg_row;
1403 skip_redraw = TRUE; /* skip redraw once */
1404 do_redraw = FALSE;
1405 break;
1406 }
1407 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
1408 break;
1409 }
1410 --no_mapping;
1411 --allow_keys;
1412 return n;
1413}
1414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001415/*
1416 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001417 * When "mouse_used" is not NULL allow using the mouse and in that case return
1418 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001419 */
1420 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001421prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001422{
1423 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001424 int save_cmdline_row;
1425 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001426
1427 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001428 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001429 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001430 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001431 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001432
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001433 // Set the state such that text can be selected/copied/pasted and we still
1434 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1435 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001436 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001437 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001438 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001439 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02001440#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001441 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001442 setmouse();
1443#endif
1444
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001445 i = get_number(TRUE, mouse_used);
1446 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001447 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001448 // don't call wait_return() now
1449 if (msg_row > 0)
1450 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001451 need_wait_return = FALSE;
1452 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00001453 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001454 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001455 else
1456 cmdline_row = save_cmdline_row;
1457 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02001458#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001459 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001460 setmouse();
1461#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001463 return i;
1464}
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001467msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 long pn;
1470
1471 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1473 return;
1474
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001475 /* We don't want to overwrite another important message, but do overwrite
1476 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1477 * then "put" reports the last action. */
1478 if (keep_msg != NULL && !keep_msg_more)
1479 return;
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (n > 0)
1482 pn = n;
1483 else
1484 pn = -n;
1485
1486 if (pn > p_report)
1487 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001488 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001489 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001490 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001492 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001493 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001495 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1496 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 if (msg(msg_buf))
1498 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001499 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001500 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 }
1503}
1504
1505/*
1506 * flush map and typeahead buffers and give a warning for an error
1507 */
1508 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001509beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 if (emsg_silent == 0)
1512 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001513 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001514 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516}
1517
1518/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001519 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
1521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001522vim_beep(
1523 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001525#ifdef FEAT_EVAL
1526 called_vim_beep = TRUE;
1527#endif
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (emsg_silent == 0)
1530 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001531 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1532 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001533#ifdef ELAPSED_FUNC
1534 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001535 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001536
1537 /* Only beep once per half a second, otherwise a sequence of beeps
1538 * would freeze Vim. */
1539 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1540 {
1541 did_init = TRUE;
1542 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001544 if (p_vb
1545#ifdef FEAT_GUI
1546 /* While the GUI is starting up the termcap is set for
1547 * the GUI but the output still goes to a terminal. */
1548 && !(gui.in_use && gui.starting)
1549#endif
1550 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001551 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001552 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001553#ifdef FEAT_VTP
1554 /* No restore color information, refresh the screen. */
1555 if (has_vtp_working() != 0
1556# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001557 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001558# endif
1559 )
1560 {
1561 redraw_later(CLEAR);
1562 update_screen(0);
1563 redrawcmd();
1564 }
1565#endif
1566 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001567 else
1568 out_char(BELL);
1569#ifdef ELAPSED_FUNC
1570 }
1571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001573
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001574 /* When 'debug' contains "beep" produce a message. If we are sourcing
1575 * a script or executing a function give the user a hint where the beep
1576 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001577 if (vim_strchr(p_debug, 'e') != NULL)
1578 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001579 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001580 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583}
1584
1585/*
1586 * To get the "real" home directory:
1587 * - get value of $HOME
1588 * For Unix:
1589 * - go to that directory
1590 * - do mch_dirname() to get the real name of that directory.
1591 * This also works with mounts and links.
1592 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001593 * For Windows:
1594 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 */
1596static char_u *homedir = NULL;
1597
1598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001599init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
1601 char_u *var;
1602
Bram Moolenaar05159a02005-02-26 23:04:13 +00001603 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001604 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606#ifdef VMS
1607 var = mch_getenv((char_u *)"SYS$LOGIN");
1608#else
1609 var = mch_getenv((char_u *)"HOME");
1610#endif
1611
Bram Moolenaar4f974752019-02-17 17:44:42 +01001612#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001614 * Typically, $HOME is not defined on Windows, unless the user has
1615 * specifically defined it for Vim's sake. However, on Windows NT
1616 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1617 * each user. Try constructing $HOME from these.
1618 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001619 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001620 {
1621 char_u *homedrive, *homepath;
1622
1623 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1624 homepath = mch_getenv((char_u *)"HOMEPATH");
1625 if (homepath == NULL || *homepath == NUL)
1626 homepath = (char_u *)"\\";
1627 if (homedrive != NULL
1628 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1629 {
1630 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1631 if (NameBuff[0] != NUL)
1632 var = NameBuff;
1633 }
1634 }
1635
1636 if (var == NULL)
1637 var = mch_getenv((char_u *)"USERPROFILE");
1638
1639 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 * Weird but true: $HOME may contain an indirect reference to another
1641 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1642 * when $HOME is being set.
1643 */
1644 if (var != NULL && *var == '%')
1645 {
1646 char_u *p;
1647 char_u *exp;
1648
1649 p = vim_strchr(var + 1, '%');
1650 if (p != NULL)
1651 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001652 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 exp = mch_getenv(NameBuff);
1654 if (exp != NULL && *exp != NUL
1655 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1656 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001657 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
1660 }
1661 }
1662
Bram Moolenaar48340b62017-08-29 22:08:53 +02001663 if (var != NULL && *var == NUL) /* empty is same as not set */
1664 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar05159a02005-02-26 23:04:13 +00001666 if (enc_utf8 && var != NULL)
1667 {
1668 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001669 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001670
1671 /* Convert from active codepage to UTF-8. Other conversions are
1672 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001673 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001674 if (pp != NULL)
1675 {
1676 homedir = pp;
1677 return;
1678 }
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 /*
1682 * Default home dir is C:/
1683 * Best assumption we can make in such a situation.
1684 */
1685 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001686 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (var != NULL)
1690 {
1691#ifdef UNIX
1692 /*
1693 * Change to the directory and get the actual path. This resolves
1694 * links. Don't do it when we can't return.
1695 */
1696 if (mch_dirname(NameBuff, MAXPATHL) == OK
1697 && mch_chdir((char *)NameBuff) == 0)
1698 {
1699 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1700 var = IObuff;
1701 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001702 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
1704#endif
1705 homedir = vim_strsave(var);
1706 }
1707}
1708
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001709#if defined(EXITFREE) || defined(PROTO)
1710 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001711free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001712{
1713 vim_free(homedir);
1714}
Bram Moolenaar24305862012-08-15 14:05:05 +02001715
Bram Moolenaar24305862012-08-15 14:05:05 +02001716 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001717free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001718{
1719 ga_clear_strings(&ga_users);
1720}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001721#endif
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001724 * Call expand_env() and store the result in an allocated string.
1725 * This is not very memory efficient, this expects the result to be freed
1726 * again soon.
1727 */
1728 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001729expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001730{
1731 return expand_env_save_opt(src, FALSE);
1732}
1733
1734/*
1735 * Idem, but when "one" is TRUE handle the string as one file name, only
1736 * expand "~" at the start.
1737 */
1738 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001739expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001740{
1741 char_u *p;
1742
1743 p = alloc(MAXPATHL);
1744 if (p != NULL)
1745 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1746 return p;
1747}
1748
1749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 * Expand environment variable with path name.
1751 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001752 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 * If anything fails no expansion is done and dst equals src.
1754 */
1755 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001756expand_env(
1757 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1758 char_u *dst, /* where to put the result */
1759 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001761 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762}
1763
1764 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001765expand_env_esc(
1766 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1767 char_u *dst, /* where to put the result */
1768 int dstlen, /* maximum length of the result */
1769 int esc, /* escape spaces in expanded variables */
1770 int one, /* "srcp" is one file name */
1771 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001773 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 char_u *tail;
1775 int c;
1776 char_u *var;
1777 int copy_char;
1778 int mustfree; /* var was allocated, need to free it later */
1779 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001780 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001782 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001783 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001784
1785 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 --dstlen; /* leave one char space for "\," */
1787 while (*src && dstlen > 0)
1788 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001789#ifdef FEAT_EVAL
1790 /* Skip over `=expr`. */
1791 if (src[0] == '`' && src[1] == '=')
1792 {
1793 size_t len;
1794
1795 var = src;
1796 src += 2;
1797 (void)skip_expr(&src);
1798 if (*src == '`')
1799 ++src;
1800 len = src - var;
1801 if (len > (size_t)dstlen)
1802 len = dstlen;
1803 vim_strncpy(dst, var, len);
1804 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001805 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001806 continue;
1807 }
1808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001810 if ((*src == '$'
1811#ifdef VMS
1812 && at_start
1813#endif
1814 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001815#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 || *src == '%'
1817#endif
1818 || (*src == '~' && at_start))
1819 {
1820 mustfree = FALSE;
1821
1822 /*
1823 * The variable name is copied into dst temporarily, because it may
1824 * be a string in read-only memory and a NUL needs to be appended.
1825 */
1826 if (*src != '~') /* environment var */
1827 {
1828 tail = src + 1;
1829 var = dst;
1830 c = dstlen - 1;
1831
1832#ifdef UNIX
1833 /* Unix has ${var-name} type environment vars */
1834 if (*tail == '{' && !vim_isIDc('{'))
1835 {
1836 tail++; /* ignore '{' */
1837 while (c-- > 0 && *tail && *tail != '}')
1838 *var++ = *tail++;
1839 }
1840 else
1841#endif
1842 {
1843 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001844#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 || (*src == '%' && *tail != '%')
1846#endif
1847 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001851#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852# ifdef UNIX
1853 if (src[1] == '{' && *tail != '}')
1854# else
1855 if (*src == '%' && *tail != '%')
1856# endif
1857 var = NULL;
1858 else
1859 {
1860# ifdef UNIX
1861 if (src[1] == '{')
1862# else
1863 if (*src == '%')
1864#endif
1865 ++tail;
1866#endif
1867 *var = NUL;
1868 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001869#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871#endif
1872 }
1873 /* home directory */
1874 else if ( src[1] == NUL
1875 || vim_ispathsep(src[1])
1876 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1877 {
1878 var = homedir;
1879 tail = src + 1;
1880 }
1881 else /* user directory */
1882 {
1883#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1884 /*
1885 * Copy ~user to dst[], so we can put a NUL after it.
1886 */
1887 tail = src;
1888 var = dst;
1889 c = dstlen - 1;
1890 while ( c-- > 0
1891 && *tail
1892 && vim_isfilec(*tail)
1893 && !vim_ispathsep(*tail))
1894 *var++ = *tail++;
1895 *var = NUL;
1896# ifdef UNIX
1897 /*
1898 * If the system supports getpwnam(), use it.
1899 * Otherwise, or if getpwnam() fails, the shell is used to
1900 * expand ~user. This is slower and may fail if the shell
1901 * does not support ~user (old versions of /bin/sh).
1902 */
1903# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1904 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001905 /* Note: memory allocated by getpwnam() is never freed.
1906 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001907 struct passwd *pw = (*dst == NUL)
1908 ? NULL : getpwnam((char *)dst + 1);
1909
1910 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 if (var == NULL)
1913# endif
1914 {
1915 expand_T xpc;
1916
1917 ExpandInit(&xpc);
1918 xpc.xp_context = EXPAND_FILES;
1919 var = ExpandOne(&xpc, dst, NULL,
1920 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 mustfree = TRUE;
1922 }
1923
1924# else /* !UNIX, thus VMS */
1925 /*
1926 * USER_HOME is a comma-separated list of
1927 * directories to search for the user account in.
1928 */
1929 {
1930 char_u test[MAXPATHL], paths[MAXPATHL];
1931 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001932 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
1934 STRCPY(paths, USER_HOME);
1935 next_path = paths;
1936 while (*next_path)
1937 {
1938 for (path = next_path; *next_path && *next_path != ',';
1939 next_path++);
1940 if (*next_path)
1941 *next_path++ = NUL;
1942 STRCPY(test, path);
1943 STRCAT(test, "/");
1944 STRCAT(test, dst + 1);
1945 if (mch_stat(test, &st) == 0)
1946 {
1947 var = alloc(STRLEN(test) + 1);
1948 STRCPY(var, test);
1949 mustfree = TRUE;
1950 break;
1951 }
1952 }
1953 }
1954# endif /* UNIX */
1955#else
1956 /* cannot expand user's home directory, so don't try */
1957 var = NULL;
1958 tail = (char_u *)""; /* for gcc */
1959#endif /* UNIX || VMS */
1960 }
1961
1962#ifdef BACKSLASH_IN_FILENAME
1963 /* If 'shellslash' is set change backslashes to forward slashes.
1964 * Can't use slash_adjust(), p_ssl may be set temporarily. */
1965 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1966 {
1967 char_u *p = vim_strsave(var);
1968
1969 if (p != NULL)
1970 {
1971 if (mustfree)
1972 vim_free(var);
1973 var = p;
1974 mustfree = TRUE;
1975 forward_slash(var);
1976 }
1977 }
1978#endif
1979
1980 /* If "var" contains white space, escape it with a backslash.
1981 * Required for ":e ~/tt" when $HOME includes a space. */
1982 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1983 {
1984 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1985
1986 if (p != NULL)
1987 {
1988 if (mustfree)
1989 vim_free(var);
1990 var = p;
1991 mustfree = TRUE;
1992 }
1993 }
1994
1995 if (var != NULL && *var != NUL
1996 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1997 {
1998 STRCPY(dst, var);
1999 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002000 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 /* if var[] ends in a path separator and tail[] starts
2002 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002003 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
2005 && dst[-1] != ':'
2006#endif
2007 && vim_ispathsep(*tail))
2008 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002009 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 src = tail;
2011 copy_char = FALSE;
2012 }
2013 if (mustfree)
2014 vim_free(var);
2015 }
2016
2017 if (copy_char) /* copy at least one char */
2018 {
2019 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00002020 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002021 * Don't do this when "one" is TRUE, to avoid expanding "~" in
2022 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
2024 at_start = FALSE;
2025 if (src[0] == '\\' && src[1] != NUL)
2026 {
2027 *dst++ = *src++;
2028 --dstlen;
2029 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002030 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02002032 if (dstlen > 0)
2033 {
2034 *dst++ = *src++;
2035 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002036
Bram Moolenaar1c864092017-08-06 18:15:45 +02002037 if (startstr != NULL && src - startstr_len >= srcp
2038 && STRNCMP(src - startstr_len, startstr,
2039 startstr_len) == 0)
2040 at_start = TRUE;
2041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02002043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045 *dst = NUL;
2046}
2047
2048/*
2049 * Vim's version of getenv().
2050 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00002051 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02002052 * "mustfree" is set to TRUE when returned is allocated, it must be
2053 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 */
2055 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002056vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002058 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 char_u *pend;
2060 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002061#ifdef MSWIN
2062 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002064 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (STRCMP(name, "HOME") == 0)
2066 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002068 // Use Wide function
2069 wn = enc_to_utf16(name, NULL);
2070 if (wn == NULL)
2071 return NULL;
2072
2073 wp = _wgetenv(wn);
2074 vim_free(wn);
2075
2076 if (wp != NULL && *wp == NUL) // empty is the same as not set
2077 wp = NULL;
2078
2079 if (wp != NULL)
2080 {
2081 p = utf16_to_enc(wp, NULL);
2082 if (p == NULL)
2083 return NULL;
2084
2085 *mustfree = TRUE;
2086 return p;
2087 }
2088#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002090 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 p = NULL;
2092
2093 if (p != NULL)
2094 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002097 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
2099 if (!vimruntime && STRCMP(name, "VIM") != 0)
2100 return NULL;
2101
2102 /*
2103 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
2104 * Don't do this when default_vimruntime_dir is non-empty.
2105 */
2106 if (vimruntime
2107#ifdef HAVE_PATHDEF
2108 && *default_vimruntime_dir == NUL
2109#endif
2110 )
2111 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002112#ifdef MSWIN
2113 // Use Wide function
2114 wp = _wgetenv(L"VIM");
2115 if (wp != NULL && *wp == NUL) // empty is the same as not set
2116 wp = NULL;
2117 if (wp != NULL)
2118 {
2119 char_u *q = utf16_to_enc(wp, NULL);
2120 if (q != NULL)
2121 {
2122 p = vim_version_dir(q);
2123 *mustfree = TRUE;
2124 if (p == NULL)
2125 p = q;
2126 }
2127 }
2128#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002130 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 p = NULL;
2132 if (p != NULL)
2133 {
2134 p = vim_version_dir(p);
2135 if (p != NULL)
2136 *mustfree = TRUE;
2137 else
2138 p = mch_getenv((char_u *)"VIM");
2139 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142
2143 /*
2144 * When expanding $VIM or $VIMRUNTIME fails, try using:
2145 * - the directory name from 'helpfile' (unless it contains '$')
2146 * - the executable name from argv[0]
2147 */
2148 if (p == NULL)
2149 {
2150 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
2151 p = p_hf;
2152#ifdef USE_EXE_NAME
2153 /*
2154 * Use the name of the executable, obtained from argv[0].
2155 */
2156 else
2157 p = exe_name;
2158#endif
2159 if (p != NULL)
2160 {
2161 /* remove the file name */
2162 pend = gettail(p);
2163
2164 /* remove "doc/" from 'helpfile', if present */
2165 if (p == p_hf)
2166 pend = remove_tail(p, pend, (char_u *)"doc");
2167
2168#ifdef USE_EXE_NAME
2169# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002170 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (p == exe_name)
2172 {
2173 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002174 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002176 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
2177 if (pend1 != pend)
2178 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002179 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002180 if (pnew != NULL)
2181 {
2182 STRNCPY(pnew, p, (pend1 - p));
2183 STRCPY(pnew + (pend1 - p), "Resources/vim");
2184 p = pnew;
2185 pend = p + STRLEN(p);
2186 }
2187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 }
2189# endif
2190 /* remove "src/" from exe_name, if present */
2191 if (p == exe_name)
2192 pend = remove_tail(p, pend, (char_u *)"src");
2193#endif
2194
2195 /* for $VIM, remove "runtime/" or "vim54/", if present */
2196 if (!vimruntime)
2197 {
2198 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
2199 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
2200 }
2201
2202 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002203 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002206#ifdef MACOS_X
2207 if (p == exe_name || p == p_hf)
2208#endif
2209 /* check that the result is a directory name */
2210 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01002213 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else
2215 {
2216#ifdef USE_EXE_NAME
2217 /* may add "/vim54" or "/runtime" if it exists */
2218 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
2219 {
2220 vim_free(p);
2221 p = pend;
2222 }
2223#endif
2224 *mustfree = TRUE;
2225 }
2226 }
2227 }
2228
2229#ifdef HAVE_PATHDEF
2230 /* When there is a pathdef.c file we can use default_vim_dir and
2231 * default_vimruntime_dir */
2232 if (p == NULL)
2233 {
2234 /* Only use default_vimruntime_dir when it is not empty */
2235 if (vimruntime && *default_vimruntime_dir != NUL)
2236 {
2237 p = default_vimruntime_dir;
2238 *mustfree = FALSE;
2239 }
2240 else if (*default_vim_dir != NUL)
2241 {
2242 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
2243 *mustfree = TRUE;
2244 else
2245 {
2246 p = default_vim_dir;
2247 *mustfree = FALSE;
2248 }
2249 }
2250 }
2251#endif
2252
2253 /*
2254 * Set the environment variable, so that the new value can be found fast
2255 * next time, and others can also use it (e.g. Perl).
2256 */
2257 if (p != NULL)
2258 {
2259 if (vimruntime)
2260 {
2261 vim_setenv((char_u *)"VIMRUNTIME", p);
2262 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 else
2265 {
2266 vim_setenv((char_u *)"VIM", p);
2267 didset_vim = TRUE;
2268 }
2269 }
2270 return p;
2271}
2272
2273/*
2274 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
2275 * Return NULL if not, return its name in allocated memory otherwise.
2276 */
2277 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002278vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 char_u *p;
2281
2282 if (vimdir == NULL || *vimdir == NUL)
2283 return NULL;
2284 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
2285 if (p != NULL && mch_isdir(p))
2286 return p;
2287 vim_free(p);
2288 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
2289 if (p != NULL && mch_isdir(p))
2290 return p;
2291 vim_free(p);
2292 return NULL;
2293}
2294
2295/*
2296 * If the string between "p" and "pend" ends in "name/", return "pend" minus
2297 * the length of "name/". Otherwise return "pend".
2298 */
2299 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002300remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 int len = (int)STRLEN(name) + 1;
2303 char_u *newend = pend - len;
2304
2305 if (newend >= p
2306 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002307 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return newend;
2309 return pend;
2310}
2311
Bram Moolenaar113e1072019-01-20 15:30:40 +01002312#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02002313 void
2314vim_unsetenv(char_u *var)
2315{
2316#ifdef HAVE_UNSETENV
2317 unsetenv((char *)var);
2318#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02002319 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02002320#endif
2321}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002322#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02002323
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 * Our portable version of setenv.
2327 */
2328 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002329vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331#ifdef HAVE_SETENV
2332 mch_setenv((char *)name, (char *)val, 1);
2333#else
2334 char_u *envbuf;
2335
2336 /*
2337 * Putenv does not copy the string, it has to remain
2338 * valid. The allocated memory will never be freed.
2339 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002340 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (envbuf != NULL)
2342 {
2343 sprintf((char *)envbuf, "%s=%s", name, val);
2344 putenv((char *)envbuf);
2345 }
2346#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002347#ifdef FEAT_GETTEXT
2348 /*
2349 * When setting $VIMRUNTIME adjust the directory to find message
2350 * translations to $VIMRUNTIME/lang.
2351 */
2352 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2353 {
2354 char_u *buf = concat_str(val, (char_u *)"/lang");
2355
2356 if (buf != NULL)
2357 {
2358 bindtextdomain(VIMPACKAGE, (char *)buf);
2359 vim_free(buf);
2360 }
2361 }
2362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363}
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365/*
2366 * Function given to ExpandGeneric() to obtain an environment variable name.
2367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002369get_env_name(
2370 expand_T *xp UNUSED,
2371 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
Bram Moolenaard0573012017-10-28 21:11:06 +02002373# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002375 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 */
2377 return NULL;
2378# else
2379# ifndef __WIN32__
2380 /* Borland C++ 5.2 has this in a header file. */
2381 extern char **environ;
2382# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002383# define ENVNAMELEN 100
2384 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 char_u *str;
2386 int n;
2387
2388 str = (char_u *)environ[idx];
2389 if (str == NULL)
2390 return NULL;
2391
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002392 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 if (str[n] == '=' || str[n] == NUL)
2395 break;
2396 name[n] = str[n];
2397 }
2398 name[n] = NUL;
2399 return name;
2400# endif
2401}
Bram Moolenaar24305862012-08-15 14:05:05 +02002402
2403/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002404 * Add a user name to the list of users in ga_users.
2405 * Do nothing if user name is NULL or empty.
2406 */
2407 static void
2408add_user(char_u *user, int need_copy)
2409{
2410 char_u *user_copy = (user != NULL && need_copy)
2411 ? vim_strsave(user) : user;
2412
2413 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2414 {
2415 if (need_copy)
2416 vim_free(user);
2417 return;
2418 }
2419 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2420}
2421
2422/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002423 * Find all user names for user completion.
2424 * Done only once and then cached.
2425 */
2426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002427init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002428{
Bram Moolenaar24305862012-08-15 14:05:05 +02002429 static int lazy_init_done = FALSE;
2430
2431 if (lazy_init_done)
2432 return;
2433
2434 lazy_init_done = TRUE;
2435 ga_init2(&ga_users, sizeof(char_u *), 20);
2436
2437# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2438 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002439 struct passwd* pw;
2440
2441 setpwent();
2442 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002443 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002444 endpwent();
2445 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002446# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002447 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002448 DWORD nusers = 0, ntotal = 0, i;
2449 PUSER_INFO_0 uinfo;
2450
2451 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2452 &nusers, &ntotal, NULL) == NERR_Success)
2453 {
2454 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002455 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002456
2457 NetApiBufferFree(uinfo);
2458 }
2459 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002460# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002461# if defined(HAVE_GETPWNAM)
2462 {
2463 char_u *user_env = mch_getenv((char_u *)"USER");
2464
2465 // The $USER environment variable may be a valid remote user name (NIS,
2466 // LDAP) not already listed by getpwent(), as getpwent() only lists
2467 // local user names. If $USER is not already listed, check whether it
2468 // is a valid remote user name using getpwnam() and if it is, add it to
2469 // the list of user names.
2470
2471 if (user_env != NULL && *user_env != NUL)
2472 {
2473 int i;
2474
2475 for (i = 0; i < ga_users.ga_len; i++)
2476 {
2477 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2478
2479 if (STRCMP(local_user, user_env) == 0)
2480 break;
2481 }
2482
2483 if (i == ga_users.ga_len)
2484 {
2485 struct passwd *pw = getpwnam((char *)user_env);
2486
2487 if (pw != NULL)
2488 add_user((char_u *)pw->pw_name, TRUE);
2489 }
2490 }
2491 }
2492# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002493}
2494
2495/*
2496 * Function given to ExpandGeneric() to obtain an user names.
2497 */
2498 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002499get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002500{
2501 init_users();
2502 if (idx < ga_users.ga_len)
2503 return ((char_u **)ga_users.ga_data)[idx];
2504 return NULL;
2505}
2506
2507/*
2508 * Check whether name matches a user name. Return:
2509 * 0 if name does not match any user name.
2510 * 1 if name partially matches the beginning of a user name.
2511 * 2 is name fully matches a user name.
2512 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002513 int
2514match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002515{
2516 int i;
2517 int n = (int)STRLEN(name);
2518 int result = 0;
2519
2520 init_users();
2521 for (i = 0; i < ga_users.ga_len; i++)
2522 {
2523 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2524 return 2; /* full match */
2525 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2526 result = 1; /* partial match */
2527 }
2528 return result;
2529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531/*
2532 * Replace home directory by "~" in each space or comma separated file name in
2533 * 'src'.
2534 * If anything fails (except when out of space) dst equals src.
2535 */
2536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002537home_replace(
2538 buf_T *buf, /* when not NULL, check for help files */
2539 char_u *src, /* input file name */
2540 char_u *dst, /* where to put the result */
2541 int dstlen, /* maximum length of the result */
2542 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 spaces and commas in the file name. */
2544{
2545 size_t dirlen = 0, envlen = 0;
2546 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002547 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 char_u *p;
2549
2550 if (src == NULL)
2551 {
2552 *dst = NUL;
2553 return;
2554 }
2555
2556 /*
2557 * If the file is a help file, remove the path completely.
2558 */
2559 if (buf != NULL && buf->b_help)
2560 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02002561 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 return;
2563 }
2564
2565 /*
2566 * We check both the value of the $HOME environment variable and the
2567 * "real" home directory.
2568 */
2569 if (homedir != NULL)
2570 dirlen = STRLEN(homedir);
2571
2572#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002573 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002575 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
2576#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002577#ifdef MSWIN
Bram Moolenaar48340b62017-08-29 22:08:53 +02002578 if (homedir_env == NULL)
2579 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
2580#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02002581 /* Empty is the same as not set. */
2582 if (homedir_env != NULL && *homedir_env == NUL)
2583 homedir_env = NULL;
2584
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
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (homedir_env != NULL)
2601 envlen = STRLEN(homedir_env);
2602
2603 if (!one)
2604 src = skipwhite(src);
2605 while (*src && dstlen > 0)
2606 {
2607 /*
2608 * Here we are at the beginning of a file name.
2609 * First, check to see if the beginning of the file name matches
2610 * $HOME or the "real" home directory. Check that there is a '/'
2611 * after the match (so that if e.g. the file is "/home/pieter/bla",
2612 * and the home directory is "/home/piet", the file does not end up
2613 * as "~er/bla" (which would seem to indicate the file "bla" in user
2614 * er's home directory)).
2615 */
2616 p = homedir;
2617 len = dirlen;
2618 for (;;)
2619 {
2620 if ( len
2621 && fnamencmp(src, p, len) == 0
2622 && (vim_ispathsep(src[len])
2623 || (!one && (src[len] == ',' || src[len] == ' '))
2624 || src[len] == NUL))
2625 {
2626 src += len;
2627 if (--dstlen > 0)
2628 *dst++ = '~';
2629
2630 /*
2631 * If it's just the home directory, add "/".
2632 */
2633 if (!vim_ispathsep(src[0]) && --dstlen > 0)
2634 *dst++ = '/';
2635 break;
2636 }
2637 if (p == homedir_env)
2638 break;
2639 p = homedir_env;
2640 len = envlen;
2641 }
2642
2643 /* if (!one) skip to separator: space or comma */
2644 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
2645 *dst++ = *src++;
2646 /* skip separator */
2647 while ((*src == ' ' || *src == ',') && --dstlen > 0)
2648 *dst++ = *src++;
2649 }
2650 /* if (dstlen == 0) out of space, what to do??? */
2651
2652 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02002653
2654 if (homedir_env != homedir_env_orig)
2655 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656}
2657
2658/*
2659 * Like home_replace, store the replaced string in allocated memory.
2660 * When something fails, NULL is returned.
2661 */
2662 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002663home_replace_save(
2664 buf_T *buf, /* when not NULL, check for help files */
2665 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 char_u *dst;
2668 unsigned len;
2669
2670 len = 3; /* space for "~/" and trailing NUL */
2671 if (src != NULL) /* just in case */
2672 len += (unsigned)STRLEN(src);
2673 dst = alloc(len);
2674 if (dst != NULL)
2675 home_replace(buf, src, dst, len, TRUE);
2676 return dst;
2677}
2678
2679/*
2680 * Compare two file names and return:
2681 * FPC_SAME if they both exist and are the same file.
2682 * FPC_SAMEX if they both don't exist and have the same file name.
2683 * FPC_DIFF if they both exist and are different files.
2684 * FPC_NOTX if they both don't exist.
2685 * FPC_DIFFX if one of them doesn't exist.
Bram Moolenaar99499b12019-05-23 21:35:48 +02002686 * For the first name environment variables are expanded if "expandenv" is
2687 * TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
2689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690fullpathcmp(
2691 char_u *s1,
2692 char_u *s2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02002693 int checkname, // when both don't exist, check file names
2694 int expandenv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696#ifdef UNIX
2697 char_u exp1[MAXPATHL];
2698 char_u full1[MAXPATHL];
2699 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02002700 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 int r1, r2;
2702
Bram Moolenaar99499b12019-05-23 21:35:48 +02002703 if (expandenv)
2704 expand_env(s1, exp1, MAXPATHL);
2705 else
2706 vim_strncpy(exp1, s1, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 r1 = mch_stat((char *)exp1, &st1);
2708 r2 = mch_stat((char *)s2, &st2);
2709 if (r1 != 0 && r2 != 0)
2710 {
2711 /* if mch_stat() doesn't work, may compare the names */
2712 if (checkname)
2713 {
2714 if (fnamecmp(exp1, s2) == 0)
2715 return FPC_SAMEX;
2716 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2717 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2718 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
2719 return FPC_SAMEX;
2720 }
2721 return FPC_NOTX;
2722 }
2723 if (r1 != 0 || r2 != 0)
2724 return FPC_DIFFX;
2725 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
2726 return FPC_SAME;
2727 return FPC_DIFF;
2728#else
2729 char_u *exp1; /* expanded s1 */
2730 char_u *full1; /* full path of s1 */
2731 char_u *full2; /* full path of s2 */
2732 int retval = FPC_DIFF;
2733 int r1, r2;
2734
2735 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
2736 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
2737 {
2738 full1 = exp1 + MAXPATHL;
2739 full2 = full1 + MAXPATHL;
2740
Bram Moolenaar99499b12019-05-23 21:35:48 +02002741 if (expandenv)
2742 expand_env(s1, exp1, MAXPATHL);
2743 else
2744 vim_strncpy(exp1, s1, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
2746 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
2747
2748 /* If vim_FullName() fails, the file probably doesn't exist. */
2749 if (r1 != OK && r2 != OK)
2750 {
2751 if (checkname && fnamecmp(exp1, s2) == 0)
2752 retval = FPC_SAMEX;
2753 else
2754 retval = FPC_NOTX;
2755 }
2756 else if (r1 != OK || r2 != OK)
2757 retval = FPC_DIFFX;
2758 else if (fnamecmp(full1, full2))
2759 retval = FPC_DIFF;
2760 else
2761 retval = FPC_SAME;
2762 vim_free(exp1);
2763 }
2764 return retval;
2765#endif
2766}
2767
2768/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002769 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02002770 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00002771 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 */
2773 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002774gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 char_u *p1, *p2;
2777
2778 if (fname == NULL)
2779 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01002780 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01002782 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002784 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 return p1;
2787}
2788
2789/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002790 * Get pointer to tail of "fname", including path separators. Putting a NUL
2791 * here leaves the directory name. Takes care of "c:/" and "//".
2792 * Always returns a valid pointer.
2793 */
2794 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002795gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002796{
2797 char_u *p;
2798 char_u *t;
2799
2800 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
2801 t = gettail(fname);
2802 while (t > p && after_pathsep(fname, t))
2803 --t;
2804#ifdef VMS
2805 /* path separator is part of the path */
2806 ++t;
2807#endif
2808 return t;
2809}
2810
2811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 * get the next path component (just after the next path separator).
2813 */
2814 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002815getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
2817 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002818 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (*fname)
2820 ++fname;
2821 return fname;
2822}
2823
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824/*
2825 * Get a pointer to one character past the head of a path name.
2826 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
2827 * If there is no head, path is returned.
2828 */
2829 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002830get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 char_u *retval;
2833
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002834#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /* may skip "c:" */
2836 if (isalpha(path[0]) && path[1] == ':')
2837 retval = path + 2;
2838 else
2839 retval = path;
2840#else
2841# if defined(AMIGA)
2842 /* may skip "label:" */
2843 retval = vim_strchr(path, ':');
2844 if (retval == NULL)
2845 retval = path;
2846# else /* Unix */
2847 retval = path;
2848# endif
2849#endif
2850
2851 while (vim_ispathsep(*retval))
2852 ++retval;
2853
2854 return retval;
2855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01002858 * Return TRUE if 'c' is a path separator.
2859 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 */
2861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
Bram Moolenaare60acc12011-05-10 16:41:25 +02002864#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02002866#else
2867# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02002869# else
2870# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
2872 return (c == ':' || c == '[' || c == ']' || c == '/'
2873 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02002874# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02002876# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02002878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879}
2880
Bram Moolenaar69c35002013-11-04 02:54:12 +01002881/*
2882 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
2883 */
2884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002885vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01002886{
2887 return vim_ispathsep(c)
2888#ifdef BACKSLASH_IN_FILENAME
2889 && c != ':'
2890#endif
2891 ;
2892}
2893
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002894/*
2895 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
2896 * It's done in-place.
2897 */
2898 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002899shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002900{
2901 char_u *tail, *s, *d;
2902 int skip = FALSE;
2903
2904 tail = gettail(str);
2905 d = str;
2906 for (s = str; ; ++s)
2907 {
2908 if (s >= tail) /* copy the whole tail */
2909 {
2910 *d++ = *s;
2911 if (*s == NUL)
2912 break;
2913 }
2914 else if (vim_ispathsep(*s)) /* copy '/' and next char */
2915 {
2916 *d++ = *s;
2917 skip = FALSE;
2918 }
2919 else if (!skip)
2920 {
2921 *d++ = *s; /* copy next char */
2922 if (*s != '~' && *s != '.') /* and leading "~" and "." */
2923 skip = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002924 if (has_mbyte)
2925 {
2926 int l = mb_ptr2len(s);
2927
2928 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00002929 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002930 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002931 }
2932 }
2933}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002934
Bram Moolenaar900b4d72005-12-12 22:05:50 +00002935/*
2936 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
2937 * Also returns TRUE if there is no directory name.
2938 * "fname" must be writable!.
2939 */
2940 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002941dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00002942{
2943 char_u *p;
2944 int c;
2945 int retval;
2946
2947 p = gettail_sep(fname);
2948 if (p == fname)
2949 return TRUE;
2950 c = *p;
2951 *p = NUL;
2952 retval = mch_isdir(fname);
2953 *p = c;
2954 return retval;
2955}
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002958 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
2959 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 */
2961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002962vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002964#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002966#else
2967 if (p_fic)
2968 return MB_STRICMP(x, y);
2969 return STRCMP(x, y);
2970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
2973 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002974vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002976#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002977 char_u *px = x;
2978 char_u *py = y;
2979 int cx = NUL;
2980 int cy = NUL;
2981
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002982 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002984 cx = PTR2CHAR(px);
2985 cy = PTR2CHAR(py);
2986 if (cx == NUL || cy == NUL
2987 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
2988 && !(cx == '/' && cy == '\\')
2989 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002991 len -= MB_PTR2LEN(px);
2992 px += MB_PTR2LEN(px);
2993 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 if (len == 0)
2996 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01002997 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002998#else
2999 if (p_fic)
3000 return MB_STRNICMP(x, y, len);
3001 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005/*
3006 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00003007 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003010concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 char_u *dest;
3013
Bram Moolenaar964b3742019-05-24 18:54:09 +02003014 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 if (dest != NULL)
3016 {
3017 STRCPY(dest, fname1);
3018 if (sep)
3019 add_pathsep(dest);
3020 STRCAT(dest, fname2);
3021 }
3022 return dest;
3023}
3024
Bram Moolenaard6754642005-01-17 22:18:45 +00003025/*
3026 * Concatenate two strings and return the result in allocated memory.
3027 * Returns NULL when out of memory.
3028 */
3029 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003030concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00003031{
3032 char_u *dest;
3033 size_t l = STRLEN(str1);
3034
Bram Moolenaar964b3742019-05-24 18:54:09 +02003035 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00003036 if (dest != NULL)
3037 {
3038 STRCPY(dest, str1);
3039 STRCPY(dest + l, str2);
3040 }
3041 return dest;
3042}
Bram Moolenaard6754642005-01-17 22:18:45 +00003043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * Add a path separator to a file name, unless it already ends in a path
3046 * separator.
3047 */
3048 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003049add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003051 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 STRCAT(p, PATHSEPSTR);
3053}
3054
3055/*
3056 * FullName_save - Make an allocated copy of a full file name.
3057 * Returns NULL when out of memory.
3058 */
3059 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003060FullName_save(
3061 char_u *fname,
3062 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02003063 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 char_u *buf;
3066 char_u *new_fname = NULL;
3067
3068 if (fname == NULL)
3069 return NULL;
3070
Bram Moolenaar964b3742019-05-24 18:54:09 +02003071 buf = alloc(MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (buf != NULL)
3073 {
3074 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
3075 new_fname = vim_strsave(buf);
3076 else
3077 new_fname = vim_strsave(fname);
3078 vim_free(buf);
3079 }
3080 return new_fname;
3081}
3082
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003083 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003084prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003086#if defined(SIGHUP) && defined(SIG_IGN)
3087 /* Ignore SIGHUP, because a dropped connection causes a read error, which
3088 * makes Vim exit and then handling SIGHUP causes various reentrance
3089 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003090 signal(SIGHUP, SIG_IGN);
3091#endif
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#ifdef FEAT_GUI
3094 if (gui.in_use)
3095 {
3096 gui.dying = TRUE;
3097 out_trash(); /* trash any pending output */
3098 }
3099 else
3100#endif
3101 {
3102 windgoto((int)Rows - 1, 0);
3103
3104 /*
3105 * Switch terminal mode back now, so messages end up on the "normal"
3106 * screen (if there are two screens).
3107 */
3108 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003109 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 out_flush();
3111 }
3112}
3113
3114/*
3115 * Preserve files and exit.
3116 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003117 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
3118 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 */
3120 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003121preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 buf_T *buf;
3124
3125 prepare_to_exit();
3126
Bram Moolenaar4770d092006-01-12 23:22:24 +00003127 /* Setting this will prevent free() calls. That avoids calling free()
3128 * recursively when free() was invoked with a bad pointer. */
3129 really_exiting = TRUE;
3130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 out_str(IObuff);
3132 screen_start(); /* don't know where cursor is now */
3133 out_flush();
3134
3135 ml_close_notmod(); /* close all not-modified buffers */
3136
Bram Moolenaar29323592016-07-24 22:04:11 +02003137 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
3140 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003141 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 screen_start(); /* don't know where cursor is now */
3143 out_flush();
3144 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
3145 break;
3146 }
3147 }
3148
3149 ml_close_all(FALSE); /* close all memfiles, without deleting */
3150
Bram Moolenaarbec9c202013-09-05 21:41:39 +02003151 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 getout(1);
3154}
3155
3156/*
3157 * return TRUE if "fname" exists.
3158 */
3159 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003160vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003162 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 if (mch_stat((char *)fname, &st))
3165 return FALSE;
3166 return TRUE;
3167}
3168
3169/*
3170 * Check for CTRL-C pressed, but only once in a while.
3171 * Should be used instead of ui_breakcheck() for functions that check for
3172 * each line in the file. Calling ui_breakcheck() each time takes too much
3173 * time, because it can be a system call.
3174 */
3175
3176#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02003177# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178#endif
3179
3180static int breakcheck_count = 0;
3181
3182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003183line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 if (++breakcheck_count >= BREAKCHECK_SKIP)
3186 {
3187 breakcheck_count = 0;
3188 ui_breakcheck();
3189 }
3190}
3191
3192/*
3193 * Like line_breakcheck() but check 10 times less often.
3194 */
3195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003196fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
3199 {
3200 breakcheck_count = 0;
3201 ui_breakcheck();
3202 }
3203}
3204
3205/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00003206 * Invoke expand_wildcards() for one pattern.
3207 * Expand items like "%:h" before the expansion.
3208 * Returns OK or FAIL.
3209 */
3210 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003211expand_wildcards_eval(
3212 char_u **pat, /* pointer to input pattern */
3213 int *num_file, /* resulting number of files */
3214 char_u ***file, /* array of resulting files */
3215 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00003216{
3217 int ret = FAIL;
3218 char_u *eval_pat = NULL;
3219 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003220 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00003221 int usedlen;
3222
3223 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
3224 {
3225 ++emsg_off;
3226 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
3227 NULL, &ignored_msg, NULL);
3228 --emsg_off;
3229 if (eval_pat != NULL)
3230 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
3231 }
3232
3233 if (exp_pat != NULL)
3234 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
3235
3236 if (eval_pat != NULL)
3237 {
3238 vim_free(exp_pat);
3239 vim_free(eval_pat);
3240 }
3241
3242 return ret;
3243}
3244
3245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
3247 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003248 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
3250 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003251expand_wildcards(
3252 int num_pat, /* number of input patterns */
3253 char_u **pat, /* array of input patterns */
3254 int *num_files, /* resulting number of files */
3255 char_u ***files, /* array of resulting files */
3256 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 int retval;
3259 int i, j;
3260 char_u *p;
3261 int non_suf_match; /* number without matching suffix */
3262
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003263 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
3265 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02003266 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 return retval;
3268
3269#ifdef FEAT_WILDIGN
3270 /*
3271 * Remove names that match 'wildignore'.
3272 */
3273 if (*p_wig)
3274 {
3275 char_u *ffname;
3276
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003277 /* check all files in (*files)[] */
3278 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003280 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (ffname == NULL) /* out of memory */
3282 break;
3283# ifdef VMS
3284 vms_remove_version(ffname);
3285# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003286 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01003288 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003289 vim_free((*files)[i]);
3290 for (j = i; j + 1 < *num_files; ++j)
3291 (*files)[j] = (*files)[j + 1];
3292 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 --i;
3294 }
3295 vim_free(ffname);
3296 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003297
3298 /* If the number of matches is now zero, we fail. */
3299 if (*num_files == 0)
3300 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003301 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003302 return FAIL;
3303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305#endif
3306
3307 /*
3308 * Move the names where 'suffixes' match to the end.
3309 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003310 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003313 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003315 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 /*
3318 * Move the name without matching suffix to the front
3319 * of the list.
3320 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003321 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02003323 (*files)[j] = (*files)[j - 1];
3324 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326 }
3327 }
3328
3329 return retval;
3330}
3331
3332/*
3333 * Return TRUE if "fname" matches with an entry in 'suffixes'.
3334 */
3335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003336match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 int fnamelen, setsuflen;
3339 char_u *setsuf;
3340#define MAXSUFLEN 30 /* maximum length of a file suffix */
3341 char_u suf_buf[MAXSUFLEN];
3342
3343 fnamelen = (int)STRLEN(fname);
3344 setsuflen = 0;
3345 for (setsuf = p_su; *setsuf; )
3346 {
3347 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00003348 if (setsuflen == 0)
3349 {
3350 char_u *tail = gettail(fname);
3351
3352 /* empty entry: match name without a '.' */
3353 if (vim_strchr(tail, '.') == NULL)
3354 {
3355 setsuflen = 1;
3356 break;
3357 }
3358 }
3359 else
3360 {
3361 if (fnamelen >= setsuflen
3362 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
3363 (size_t)setsuflen) == 0)
3364 break;
3365 setsuflen = 0;
3366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 return (setsuflen != 0);
3369}
3370
3371#if !defined(NO_EXPANDPATH) || defined(PROTO)
3372
3373# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003374static int vim_backtick(char_u *p);
3375static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376# endif
3377
Bram Moolenaar4f974752019-02-17 17:44:42 +01003378# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379/*
3380 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
3381 * it's shared between these systems.
3382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384/*
3385 * comparison function for qsort in dos_expandpath()
3386 */
Bram Moolenaareae1b912019-05-09 15:12:55 +02003387 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388pstrcmp(const void *a, const void *b)
3389{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003390 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391}
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00003394 * Recursively expand one path component into all matching files and/or
3395 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * Return the number of matches found.
3397 * "path" has backslashes before chars that are not to be expanded, starting
3398 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00003399 * Return the number of matches found.
3400 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 static int
3403dos_expandpath(
3404 garray_T *gap,
3405 char_u *path,
3406 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00003407 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00003408 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
Bram Moolenaar231334e2005-07-25 20:46:57 +00003410 char_u *buf;
3411 char_u *path_end;
3412 char_u *p, *s, *e;
3413 int start_len = gap->ga_len;
3414 char_u *pat;
3415 regmatch_T regmatch;
3416 int starts_with_dot;
3417 int matches;
3418 int len;
3419 int starstar = FALSE;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003420 static int stardepth = 0; // depth for "**" expansion
3421 HANDLE hFind = INVALID_HANDLE_VALUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 WIN32_FIND_DATAW wfb;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003423 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003425 int ok;
3426
3427 /* Expanding "**" may take a long time, check for CTRL-C. */
3428 if (stardepth > 0)
3429 {
3430 ui_breakcheck();
3431 if (got_int)
3432 return 0;
3433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003435 // Make room for file name. When doing encoding conversion the actual
3436 // length may be quite a bit longer, thus use the maximum possible length.
Bram Moolenaar51e14382019-05-25 20:21:28 +02003437 buf = alloc(MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (buf == NULL)
3439 return 0;
3440
3441 /*
3442 * Find the first part in the path name that contains a wildcard or a ~1.
3443 * Copy it into buf, including the preceding characters.
3444 */
3445 p = buf;
3446 s = buf;
3447 e = NULL;
3448 path_end = path;
3449 while (*path_end != NUL)
3450 {
3451 /* May ignore a wildcard that has a backslash before it; it will
3452 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
3453 if (path_end >= path + wildoff && rem_backslash(path_end))
3454 *p++ = *path_end++;
3455 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
3456 {
3457 if (e != NULL)
3458 break;
3459 s = p + 1;
3460 }
3461 else if (path_end >= path + wildoff
3462 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
3463 e = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (has_mbyte)
3465 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003466 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 STRNCPY(p, path_end, len);
3468 p += len;
3469 path_end += len;
3470 }
3471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 *p++ = *path_end++;
3473 }
3474 e = p;
3475 *e = NUL;
3476
3477 /* now we have one wildcard component between s and e */
3478 /* Remove backslashes between "wildoff" and the start of the wildcard
3479 * component. */
3480 for (p = buf + wildoff; p < s; ++p)
3481 if (rem_backslash(p))
3482 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003483 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 --e;
3485 --s;
3486 }
3487
Bram Moolenaar231334e2005-07-25 20:46:57 +00003488 /* Check for "**" between "s" and "e". */
3489 for (p = s; p < e; ++p)
3490 if (p[0] == '*' && p[1] == '*')
3491 starstar = TRUE;
3492
Bram Moolenaard82103e2016-01-17 17:04:05 +01003493 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3495 if (pat == NULL)
3496 {
3497 vim_free(buf);
3498 return 0;
3499 }
3500
3501 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003502 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02003503 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 regmatch.rm_ic = TRUE; /* Always ignore case */
3505 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003506 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02003507 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 vim_free(pat);
3509
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003510 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
3512 vim_free(buf);
3513 return 0;
3514 }
3515
3516 /* remember the pattern or file name being looked for */
3517 matchname = vim_strsave(s);
3518
Bram Moolenaar231334e2005-07-25 20:46:57 +00003519 /* If "**" is by itself, this is the first time we encounter it and more
3520 * is following then find matches without any directory. */
3521 if (!didstar && stardepth < 100 && starstar && e - s == 2
3522 && *path_end == '/')
3523 {
3524 STRCPY(s, path_end + 1);
3525 ++stardepth;
3526 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3527 --stardepth;
3528 }
3529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 /* Scan all files in the directory with "dir/ *.*" */
3531 STRCPY(s, "*.*");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003532 wn = enc_to_utf16(buf, NULL);
3533 if (wn != NULL)
3534 hFind = FindFirstFileW(wn, &wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 while (ok)
3538 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003539 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
Bram Moolenaar543c9b12019-04-05 22:50:40 +02003540 if (p == NULL)
3541 break; // out of memory
3542
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003543 // Ignore entries starting with a dot, unless when asked for. Accept
3544 // all entries found with "matchname".
Bram Moolenaard82103e2016-01-17 17:04:05 +01003545 if ((p[0] != '.' || starts_with_dot
3546 || ((flags & EW_DODOT)
3547 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003549 || (regmatch.regprog != NULL
3550 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003551 || ((flags & EW_NOTWILD)
3552 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003556
3557 if (starstar && stardepth < 100)
3558 {
3559 /* For "**" in the pattern first go deeper in the tree to
3560 * find matches. */
3561 STRCPY(buf + len, "/**");
3562 STRCPY(buf + len + 3, path_end);
3563 ++stardepth;
3564 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
3565 --stardepth;
3566 }
3567
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 STRCPY(buf + len, path_end);
3569 if (mch_has_exp_wildcard(path_end))
3570 {
3571 /* need to expand another component of the path */
3572 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003573 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 else
3576 {
3577 /* no more wildcards, check if there is a match */
3578 /* remove backslashes for the remaining components only */
3579 if (*path_end != 0)
3580 backslash_halve(buf + len + 1);
3581 if (mch_getperm(buf) >= 0) /* add existing file */
3582 addfile(gap, buf, flags);
3583 }
3584 }
3585
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003586 vim_free(p);
3587 ok = FindNextFileW(hFind, &wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 /* If no more matches and no match was used, try expanding the name
3590 * itself. Finds the long name of a short filename. */
3591 if (!ok && matchname != NULL && gap->ga_len == start_len)
3592 {
3593 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 FindClose(hFind);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003595 vim_free(wn);
3596 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (wn != NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003598 hFind = FindFirstFileW(wn, &wfb);
3599 else
3600 hFind = INVALID_HANDLE_VALUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003602 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
3604 }
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02003609 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 vim_free(matchname);
3611
3612 matches = gap->ga_len - start_len;
3613 if (matches > 0)
3614 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
3615 sizeof(char_u *), pstrcmp);
3616 return matches;
3617}
3618
3619 int
3620mch_expandpath(
3621 garray_T *gap,
3622 char_u *path,
3623 int flags) /* EW_* flags */
3624{
Bram Moolenaar231334e2005-07-25 20:46:57 +00003625 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626}
Bram Moolenaar4f974752019-02-17 17:44:42 +01003627# endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaar231334e2005-07-25 20:46:57 +00003629#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
3630 || defined(PROTO)
3631/*
3632 * Unix style wildcard expansion code.
3633 * It's here because it's used both for Unix and Mac.
3634 */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003635 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003636pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003637{
3638 return (pathcmp(*(char **)a, *(char **)b, -1));
3639}
3640
3641/*
3642 * Recursively expand one path component into all matching files and/or
3643 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
3644 * "path" has backslashes before chars that are not to be expanded, starting
3645 * at "path + wildoff".
3646 * Return the number of matches found.
3647 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
3648 */
3649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003650unix_expandpath(
3651 garray_T *gap,
3652 char_u *path,
3653 int wildoff,
3654 int flags, /* EW_* flags */
3655 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003656{
3657 char_u *buf;
3658 char_u *path_end;
3659 char_u *p, *s, *e;
3660 int start_len = gap->ga_len;
3661 char_u *pat;
3662 regmatch_T regmatch;
3663 int starts_with_dot;
3664 int matches;
3665 int len;
3666 int starstar = FALSE;
3667 static int stardepth = 0; /* depth for "**" expansion */
3668
3669 DIR *dirp;
3670 struct dirent *dp;
3671
3672 /* Expanding "**" may take a long time, check for CTRL-C. */
3673 if (stardepth > 0)
3674 {
3675 ui_breakcheck();
3676 if (got_int)
3677 return 0;
3678 }
3679
3680 /* make room for file name */
Bram Moolenaar51e14382019-05-25 20:21:28 +02003681 buf = alloc(STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003682 if (buf == NULL)
3683 return 0;
3684
3685 /*
3686 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02003687 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00003688 * Copy it into "buf", including the preceding characters.
3689 */
3690 p = buf;
3691 s = buf;
3692 e = NULL;
3693 path_end = path;
3694 while (*path_end != NUL)
3695 {
3696 /* May ignore a wildcard that has a backslash before it; it will
3697 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
3698 if (path_end >= path + wildoff && rem_backslash(path_end))
3699 *p++ = *path_end++;
3700 else if (*path_end == '/')
3701 {
3702 if (e != NULL)
3703 break;
3704 s = p + 1;
3705 }
3706 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02003707 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003708 || (!p_fic && (flags & EW_ICASE)
3709 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00003710 e = p;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003711 if (has_mbyte)
3712 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003713 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003714 STRNCPY(p, path_end, len);
3715 p += len;
3716 path_end += len;
3717 }
3718 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00003719 *p++ = *path_end++;
3720 }
3721 e = p;
3722 *e = NUL;
3723
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003724 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00003725 /* Remove backslashes between "wildoff" and the start of the wildcard
3726 * component. */
3727 for (p = buf + wildoff; p < s; ++p)
3728 if (rem_backslash(p))
3729 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003730 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003731 --e;
3732 --s;
3733 }
3734
3735 /* Check for "**" between "s" and "e". */
3736 for (p = s; p < e; ++p)
3737 if (p[0] == '*' && p[1] == '*')
3738 starstar = TRUE;
3739
3740 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +01003741 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +00003742 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
3743 if (pat == NULL)
3744 {
3745 vim_free(buf);
3746 return 0;
3747 }
3748
3749 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01003750 if (flags & EW_ICASE)
3751 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
3752 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003753 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
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 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003757 if (flags & (EW_NOERROR | EW_NOTWILD))
3758 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00003759 vim_free(pat);
3760
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003761 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003762 {
3763 vim_free(buf);
3764 return 0;
3765 }
3766
3767 /* If "**" is by itself, this is the first time we encounter it and more
3768 * is following then find matches without any directory. */
3769 if (!didstar && stardepth < 100 && starstar && e - s == 2
3770 && *path_end == '/')
3771 {
3772 STRCPY(s, path_end + 1);
3773 ++stardepth;
3774 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
3775 --stardepth;
3776 }
3777
3778 /* open the directory for scanning */
3779 *s = NUL;
3780 dirp = opendir(*buf == NUL ? "." : (char *)buf);
3781
3782 /* Find all matching entries */
3783 if (dirp != NULL)
3784 {
3785 for (;;)
3786 {
3787 dp = readdir(dirp);
3788 if (dp == NULL)
3789 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +01003790 if ((dp->d_name[0] != '.' || starts_with_dot
3791 || ((flags & EW_DODOT)
3792 && dp->d_name[1] != NUL
3793 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01003794 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
3795 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02003796 || ((flags & EW_NOTWILD)
3797 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00003798 {
3799 STRCPY(s, dp->d_name);
3800 len = STRLEN(buf);
3801
3802 if (starstar && stardepth < 100)
3803 {
3804 /* For "**" in the pattern first go deeper in the tree to
3805 * find matches. */
3806 STRCPY(buf + len, "/**");
3807 STRCPY(buf + len + 3, path_end);
3808 ++stardepth;
3809 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
3810 --stardepth;
3811 }
3812
3813 STRCPY(buf + len, path_end);
3814 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
3815 {
3816 /* need to expand another component of the path */
3817 /* remove backslashes for the remaining components only */
3818 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
3819 }
3820 else
3821 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003822 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003823
Bram Moolenaar231334e2005-07-25 20:46:57 +00003824 /* no more wildcards, check if there is a match */
3825 /* remove backslashes for the remaining components only */
3826 if (*path_end != NUL)
3827 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003828 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +01003829 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +01003830 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00003831 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00003832#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00003833 size_t precomp_len = STRLEN(buf)+1;
3834 char_u *precomp_buf =
3835 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00003836
Bram Moolenaar231334e2005-07-25 20:46:57 +00003837 if (precomp_buf)
3838 {
3839 mch_memmove(buf, precomp_buf, precomp_len);
3840 vim_free(precomp_buf);
3841 }
3842#endif
3843 addfile(gap, buf, flags);
3844 }
3845 }
3846 }
3847 }
3848
3849 closedir(dirp);
3850 }
3851
3852 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02003853 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00003854
3855 matches = gap->ga_len - start_len;
3856 if (matches > 0)
3857 qsort(((char_u **)gap->ga_data) + start_len, matches,
3858 sizeof(char_u *), pstrcmp);
3859 return matches;
3860}
3861#endif
3862
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003863/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +02003864 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
3865 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003866 */
3867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003868remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003869{
3870 int i;
3871 int j;
3872 char_u **fnames = (char_u **)gap->ga_data;
3873
Bram Moolenaardc685ab2010-08-13 21:16:49 +02003874 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003875 for (i = gap->ga_len - 1; i > 0; --i)
3876 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
3877 {
3878 vim_free(fnames[i]);
3879 for (j = i + 1; j < gap->ga_len; ++j)
3880 fnames[j - 1] = fnames[j];
3881 --gap->ga_len;
3882 }
3883}
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003884
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003885/*
3886 * Return TRUE if "p" contains what looks like an environment variable.
3887 * Allowing for escaping.
3888 */
3889 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003890has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003891{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003892 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003893 {
3894 if (*p == '\\' && p[1] != NUL)
3895 ++p;
3896 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003897#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003898 "$%"
3899#else
3900 "$"
3901#endif
3902 , *p) != NULL)
3903 return TRUE;
3904 }
3905 return FALSE;
3906}
3907
3908#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003909/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +01003910 * Return TRUE if "p" contains a special wildcard character, one that Vim
3911 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003912 */
3913 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003914has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003915{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003916 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003917 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003918 // Disallow line break characters.
3919 if (*p == '\r' || *p == '\n')
3920 break;
3921 // Allow for escaping.
3922 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n')
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003923 ++p;
3924 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003925 {
3926 // A { must be followed by a matching }.
3927 if (*p == '{' && vim_strchr(p, '}') == NULL)
3928 continue;
3929 // A quote and backtick must be followed by another one.
3930 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL)
3931 continue;
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003932 return TRUE;
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003933 }
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003934 }
3935 return FALSE;
3936}
3937#endif
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939/*
3940 * Generic wildcard expansion code.
3941 *
3942 * Characters in "pat" that should not be expanded must be preceded with a
3943 * backslash. E.g., "/path\ with\ spaces/my\*star*"
3944 *
3945 * Return FAIL when no single file was found. In this case "num_file" is not
3946 * set, and "file" may contain an error message.
3947 * Return OK when some files found. "num_file" is set to the number of
3948 * matches, "file" to the array of matches. Call FreeWild() later.
3949 */
3950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003951gen_expand_wildcards(
3952 int num_pat, /* number of input patterns */
3953 char_u **pat, /* array of input patterns */
3954 int *num_file, /* resulting number of files */
3955 char_u ***file, /* array of resulting files */
3956 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 int i;
3959 garray_T ga;
3960 char_u *p;
3961 static int recursive = FALSE;
3962 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +02003963 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02003964#if defined(FEAT_SEARCHPATH)
3965 int did_expand_in_path = FALSE;
3966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967
3968 /*
3969 * expand_env() is called to expand things like "~user". If this fails,
3970 * it calls ExpandOne(), which brings us back here. In this case, always
3971 * call the machine specific expansion function, if possible. Otherwise,
3972 * return FAIL.
3973 */
3974 if (recursive)
3975#ifdef SPECIAL_WILDCHAR
3976 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3977#else
3978 return FAIL;
3979#endif
3980
3981#ifdef SPECIAL_WILDCHAR
3982 /*
3983 * If there are any special wildcard characters which we cannot handle
3984 * here, call machine specific function for all the expansion. This
3985 * avoids starting the shell for each argument separately.
3986 * For `=expr` do use the internal function.
3987 */
3988 for (i = 0; i < num_pat; i++)
3989 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +02003990 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991# ifdef VIM_BACKTICK
3992 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
3993# endif
3994 )
3995 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
3996 }
3997#endif
3998
3999 recursive = TRUE;
4000
4001 /*
4002 * The matching file names are stored in a growarray. Init it empty.
4003 */
4004 ga_init2(&ga, (int)sizeof(char_u *), 30);
4005
4006 for (i = 0; i < num_pat; ++i)
4007 {
4008 add_pat = -1;
4009 p = pat[i];
4010
4011#ifdef VIM_BACKTICK
4012 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +02004013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +02004015 if (add_pat == -1)
4016 retval = FAIL;
4017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 else
4019#endif
4020 {
4021 /*
4022 * First expand environment variables, "~/" and "~user/".
4023 */
Bram Moolenaar99499b12019-05-23 21:35:48 +02004024 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004026 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if (p == NULL)
4028 p = pat[i];
4029#ifdef UNIX
4030 /*
4031 * On Unix, if expand_env() can't expand an environment
4032 * variable, use the shell to do that. Discard previously
4033 * found file names and start all over again.
4034 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02004035 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
4037 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +00004038 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +02004040 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 recursive = FALSE;
4042 return i;
4043 }
4044#endif
4045 }
4046
4047 /*
4048 * If there are wildcards: Expand file names and add each match to
4049 * the list. If there is no match, and EW_NOTFOUND is given, add
4050 * the pattern.
4051 * If there are no wildcards: Add the file name if it exists or
4052 * when EW_NOTFOUND is given.
4053 */
4054 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004055 {
4056#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004057 if ((flags & EW_PATH)
4058 && !mch_isFullName(p)
4059 && !(p[0] == '.'
4060 && (vim_ispathsep(p[1])
4061 || (p[1] == '.' && vim_ispathsep(p[2]))))
4062 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004063 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004064 /* :find completion where 'path' is used.
4065 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004066 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004067 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004068 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004069 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02004070 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004071 else
4072#endif
4073 add_pat = mch_expandpath(&ga, p, flags);
4074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076
4077 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
4078 {
4079 char_u *t = backslash_halve_save(p);
4080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 /* When EW_NOTFOUND is used, always add files and dirs. Makes
4082 * "vim c:/" work. */
4083 if (flags & EW_NOTFOUND)
4084 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +02004085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 addfile(&ga, t, flags);
Bram Moolenaarf1552d02019-08-21 12:54:18 +02004087
4088 if (t != p)
4089 vim_free(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02004092#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02004093 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02004094 uniquefy_paths(&ga, p);
4095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (p != pat[i])
4097 vim_free(p);
4098 }
4099
4100 *num_file = ga.ga_len;
4101 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
4102
4103 recursive = FALSE;
4104
Bram Moolenaar336bd622016-01-17 18:23:58 +01004105 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106}
4107
4108# ifdef VIM_BACKTICK
4109
4110/*
4111 * Return TRUE if we can expand this backtick thing here.
4112 */
4113 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004114vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
4117}
4118
4119/*
4120 * Expand an item in `backticks` by executing it as a command.
4121 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +02004122 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 */
4124 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004125expand_backtick(
4126 garray_T *gap,
4127 char_u *pat,
4128 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 char_u *p;
4131 char_u *cmd;
4132 char_u *buffer;
4133 int cnt = 0;
4134 int i;
4135
4136 /* Create the command: lop off the backticks. */
4137 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
4138 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02004139 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141#ifdef FEAT_EVAL
4142 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004143 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 else
4145#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004146 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004147 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 vim_free(cmd);
4149 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02004150 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 cmd = buffer;
4153 while (*cmd != NUL)
4154 {
4155 cmd = skipwhite(cmd); /* skip over white space */
4156 p = cmd;
4157 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
4158 ++p;
4159 /* add an entry if it is not empty */
4160 if (p > cmd)
4161 {
4162 i = *p;
4163 *p = NUL;
4164 addfile(gap, cmd, flags);
4165 *p = i;
4166 ++cnt;
4167 }
4168 cmd = p;
4169 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
4170 ++cmd;
4171 }
4172
4173 vim_free(buffer);
4174 return cnt;
4175}
4176# endif /* VIM_BACKTICK */
4177
4178/*
4179 * Add a file to a file list. Accepted flags:
4180 * EW_DIR add directories
4181 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004182 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 * EW_NOTFOUND add even when it doesn't exist
4184 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +01004185 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 */
4187 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004188addfile(
4189 garray_T *gap,
4190 char_u *f, /* filename */
4191 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 char_u *p;
4194 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004195 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaard8b77f72015-03-05 21:21:19 +01004197 /* if the file/dir/link doesn't exist, may not add it */
4198 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +01004199 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return;
4201
4202#ifdef FNAME_ILLEGAL
4203 /* if the file/dir contains illegal characters, don't add it */
4204 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
4205 return;
4206#endif
4207
4208 isdir = mch_isdir(f);
4209 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
4210 return;
4211
Bram Moolenaarb5971142015-03-21 17:32:19 +01004212 /* If the file isn't executable, may not add it. Do accept directories.
4213 * When invoked from expand_shellcmd() do not use $PATH. */
4214 if (!isdir && (flags & EW_EXEC)
4215 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004216 return;
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 /* Make room for another item in the file list. */
4219 if (ga_grow(gap, 1) == FAIL)
4220 return;
4221
Bram Moolenaar964b3742019-05-24 18:54:09 +02004222 p = alloc(STRLEN(f) + 1 + isdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 if (p == NULL)
4224 return;
4225
4226 STRCPY(p, f);
4227#ifdef BACKSLASH_IN_FILENAME
4228 slash_adjust(p);
4229#endif
4230 /*
4231 * Append a slash or backslash after directory names if none is present.
4232 */
4233#ifndef DONT_ADD_PATHSEP_TO_DIR
4234 if (isdir && (flags & EW_ADDSLASH))
4235 add_pathsep(p);
4236#endif
4237 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238}
4239#endif /* !NO_EXPANDPATH */
4240
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004241#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
4242 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4243 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245#ifndef SEEK_SET
4246# define SEEK_SET 0
4247#endif
4248#ifndef SEEK_END
4249# define SEEK_END 2
4250#endif
4251
4252/*
4253 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004254 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
4255 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 * Returns an allocated string, or NULL for error.
4257 */
4258 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004259get_cmd_output(
4260 char_u *cmd,
4261 char_u *infile, /* optional input file name */
4262 int flags, /* can be SHELL_SILENT */
4263 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264{
4265 char_u *tempname;
4266 char_u *command;
4267 char_u *buffer = NULL;
4268 int len;
4269 int i = 0;
4270 FILE *fd;
4271
4272 if (check_restricted() || check_secure())
4273 return NULL;
4274
4275 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004276 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004278 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return NULL;
4280 }
4281
4282 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004283 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (command == NULL)
4285 goto done;
4286
4287 /*
4288 * Call the shell to execute the command (errors are ignored).
4289 * Don't check timestamps here.
4290 */
4291 ++no_check_timestamps;
4292 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
4293 --no_check_timestamps;
4294
4295 vim_free(command);
4296
4297 /*
4298 * read the names from the file into memory
4299 */
4300# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00004301 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 fd = mch_fopen((char *)tempname, "r");
4303# else
4304 fd = mch_fopen((char *)tempname, READBIN);
4305# endif
4306
4307 if (fd == NULL)
4308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004309 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 goto done;
4311 }
4312
4313 fseek(fd, 0L, SEEK_END);
4314 len = ftell(fd); /* get size of temp file */
4315 fseek(fd, 0L, SEEK_SET);
4316
4317 buffer = alloc(len + 1);
4318 if (buffer != NULL)
4319 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
4320 fclose(fd);
4321 mch_remove(tempname);
4322 if (buffer == NULL)
4323 goto done;
4324#ifdef VMS
4325 len = i; /* VMS doesn't give us what we asked for... */
4326#endif
4327 if (i != len)
4328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004329 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004330 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004332 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004333 {
4334 /* Change NUL into SOH, otherwise the string is truncated. */
4335 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02004336 if (buffer[i] == NUL)
4337 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004338
Bram Moolenaar162bd912010-07-28 22:29:10 +02004339 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02004340 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02004341 else
4342 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
4344done:
4345 vim_free(tempname);
4346 return buffer;
4347}
4348#endif
4349
4350/*
4351 * Free the list of files returned by expand_wildcards() or other expansion
4352 * functions.
4353 */
4354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004355FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004357 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 while (count--)
4360 vim_free(files[count]);
4361 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362}
4363
4364/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02004365 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 * Don't do this when still processing a command or a mapping.
4367 * Don't do this when inside a ":normal" command.
4368 */
4369 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004370goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 return (p_im && stuff_empty() && typebuf_typed());
4373}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004374
4375/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02004376 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004377 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
4378 * - Remove any argument. E.g., "csh -f" -> "csh".
4379 * But don't allow a space in the path, so that this works:
4380 * "/usr/bin/csh --rcfile ~/.cshrc"
4381 * But don't do that for Windows, it's common to have a space in the path.
4382 */
4383 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004384get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004385{
4386 char_u *p;
4387
Bram Moolenaar4f974752019-02-17 17:44:42 +01004388#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004389 p = gettail(p_sh);
4390 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
4391#else
4392 p = skiptowhite(p_sh);
4393 if (*p == NUL)
4394 {
4395 /* No white space, use the tail. */
4396 p = vim_strsave(gettail(p_sh));
4397 }
4398 else
4399 {
4400 char_u *p1, *p2;
4401
4402 /* Find the last path separator before the space. */
4403 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004404 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004405 if (vim_ispathsep(*p2))
4406 p1 = p2 + 1;
4407 p = vim_strnsave(p1, (int)(p - p1));
4408 }
4409#endif
4410 return p;
4411}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01004412
4413/*
4414 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
4415 * Also check for ":\\", which MS Internet Explorer accepts, return
4416 * URL_BACKSLASH.
4417 */
4418 int
4419path_is_url(char_u *p)
4420{
4421 if (STRNCMP(p, "://", (size_t)3) == 0)
4422 return URL_SLASH;
4423 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
4424 return URL_BACKSLASH;
4425 return 0;
4426}
4427
4428/*
4429 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
4430 * Return URL_BACKSLASH for "name:\\".
4431 * Return zero otherwise.
4432 */
4433 int
4434path_with_url(char_u *fname)
4435{
4436 char_u *p;
4437
4438 for (p = fname; isalpha(*p); ++p)
4439 ;
4440 return path_is_url(p);
4441}
4442
4443/*
4444 * Return TRUE if "name" is a full (absolute) path name or URL.
4445 */
4446 int
4447vim_isAbsName(char_u *name)
4448{
4449 return (path_with_url(name) != 0 || mch_isFullName(name));
4450}
4451
4452/*
4453 * Get absolute file name into buffer "buf[len]".
4454 *
4455 * return FAIL for failure, OK otherwise
4456 */
4457 int
4458vim_FullName(
4459 char_u *fname,
4460 char_u *buf,
4461 int len,
4462 int force) /* force expansion even when already absolute */
4463{
4464 int retval = OK;
4465 int url;
4466
4467 *buf = NUL;
4468 if (fname == NULL)
4469 return FAIL;
4470
4471 url = path_with_url(fname);
4472 if (!url)
4473 retval = mch_FullName(fname, buf, len, force);
4474 if (url || retval == FAIL)
4475 {
4476 /* something failed; use the file name (truncate when too long) */
4477 vim_strncpy(buf, fname, len - 1);
4478 }
4479#if defined(MSWIN)
4480 slash_adjust(buf);
4481#endif
4482 return retval;
4483}