blob: e05ad1654eb42ed678fdb6312a8f74dae32ae9b4 [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 Moolenaar5fd0f502019-02-13 23:13:28 +010021#define URL_SLASH 1 /* path_is_url() has found "://" */
22#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
23
Bram Moolenaar0a52df52019-08-18 22:26:31 +020024// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020025static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020026
Bram Moolenaar071d4272004-06-13 20:20:40 +000027/*
28 * Count the size (in window cells) of the indent in the current line.
29 */
30 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010031get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000032{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020033#ifdef FEAT_VARTABS
34 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
35 curbuf->b_p_vts_array, FALSE);
36#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020037 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039}
40
41/*
42 * Count the size (in window cells) of the indent in line "lnum".
43 */
44 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010045get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020047#ifdef FEAT_VARTABS
48 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
49 curbuf->b_p_vts_array, FALSE);
50#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020051 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053}
54
55#if defined(FEAT_FOLDING) || defined(PROTO)
56/*
57 * Count the size (in window cells) of the indent in line "lnum" of buffer
58 * "buf".
59 */
60 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010061get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020063#ifdef FEAT_VARTABS
64 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
65 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
66#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020067 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069}
70#endif
71
72/*
73 * count the size (in window cells) of the indent in line "ptr", with
74 * 'tabstop' at "ts"
75 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000076 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010077get_indent_str(
78 char_u *ptr,
79 int ts,
80 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081{
82 int count = 0;
83
84 for ( ; *ptr; ++ptr)
85 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020086 if (*ptr == TAB)
87 {
88 if (!list || lcs_tab1) /* count a tab for what it is worth */
89 count += ts - (count % ts);
90 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020091 /* In list mode, when tab is not set, count screen char width
92 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020093 count += ptr2cells(ptr);
94 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 else if (*ptr == ' ')
96 ++count; /* count a space for one */
97 else
98 break;
99 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000100 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101}
102
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200103#ifdef FEAT_VARTABS
104/*
105 * Count the size (in window cells) of the indent in line "ptr", using
106 * variable tabstops.
107 * if "list" is TRUE, count only screen size for tabs.
108 */
109 int
110get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
111{
112 int count = 0;
113
114 for ( ; *ptr; ++ptr)
115 {
116 if (*ptr == TAB) /* count a tab for what it is worth */
117 {
118 if (!list || lcs_tab1)
119 count += tabstop_padding(count, ts, vts);
120 else
121 /* In list mode, when tab is not set, count screen char width
122 * for Tab, displays: ^I */
123 count += ptr2cells(ptr);
124 }
125 else if (*ptr == ' ')
126 ++count; /* count a space for one */
127 else
128 break;
129 }
130 return count;
131}
132#endif
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Set the indent of the current line.
136 * Leaves the cursor on the first non-blank in the line.
137 * Caller must take care of undo.
138 * "flags":
139 * SIN_CHANGED: call changed_bytes() if the line was changed.
140 * SIN_INSERT: insert the indent in front of the line.
141 * SIN_UNDO: save line for undo before changing it.
142 * Returns TRUE if the line was changed.
143 */
144 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100145set_indent(
146 int size, /* measured in spaces */
147 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148{
149 char_u *p;
150 char_u *newline;
151 char_u *oldline;
152 char_u *s;
153 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000154 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 int line_len;
156 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000157 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200158#ifdef FEAT_VARTABS
159 int ind_col = 0;
160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000162 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000163 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
166 /*
167 * First check if there is anything to do and compute the number of
168 * characters needed for the indent.
169 */
170 todo = size;
171 ind_len = 0;
172 p = oldline = ml_get_curline();
173
174 /* Calculate the buffer size for the new indent, and check to see if it
175 * isn't already set */
176
Bram Moolenaar5002c292007-07-24 13:26:15 +0000177 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
178 * 'preserveindent' are set count the number of characters at the
179 * beginning of the line to be copied */
180 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 {
182 /* If 'preserveindent' is set then reuse as much as possible of
183 * the existing indent structure for the new indent */
184 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
185 {
186 ind_done = 0;
187
188 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100189 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {
191 if (*p == TAB)
192 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200193#ifdef FEAT_VARTABS
194 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
195 curbuf->b_p_vts_array);
196#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 tab_pad = (int)curbuf->b_p_ts
198 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 /* stop if this tab will overshoot the target */
201 if (todo < tab_pad)
202 break;
203 todo -= tab_pad;
204 ++ind_len;
205 ind_done += tab_pad;
206 }
207 else
208 {
209 --todo;
210 ++ind_len;
211 ++ind_done;
212 }
213 ++p;
214 }
215
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200216#ifdef FEAT_VARTABS
217 /* These diverge from this point. */
218 ind_col = ind_done;
219#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000220 /* Set initial number of whitespace chars to copy if we are
221 * preserving indent but expandtab is set */
222 if (curbuf->b_p_et)
223 orig_char_len = ind_len;
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200226#ifdef FEAT_VARTABS
227 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
228 curbuf->b_p_vts_array);
229#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200231#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000232 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {
234 doit = TRUE;
235 todo -= tab_pad;
236 ++ind_len;
237 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200238#ifdef FEAT_VARTABS
239 ind_col += tab_pad;
240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 }
242 }
243
244 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200245#ifdef FEAT_VARTABS
246 for (;;)
247 {
248 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
249 curbuf->b_p_vts_array);
250 if (todo < tab_pad)
251 break;
252 if (*p != TAB)
253 doit = TRUE;
254 else
255 ++p;
256 todo -= tab_pad;
257 ++ind_len;
258 ind_col += tab_pad;
259 }
260#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 while (todo >= (int)curbuf->b_p_ts)
262 {
263 if (*p != TAB)
264 doit = TRUE;
265 else
266 ++p;
267 todo -= (int)curbuf->b_p_ts;
268 ++ind_len;
269 /* ind_done += (int)curbuf->b_p_ts; */
270 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273 /* count spaces required for indent */
274 while (todo > 0)
275 {
276 if (*p != ' ')
277 doit = TRUE;
278 else
279 ++p;
280 --todo;
281 ++ind_len;
282 /* ++ind_done; */
283 }
284
285 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100286 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 return FALSE;
288
289 /* Allocate memory for the new line. */
290 if (flags & SIN_INSERT)
291 p = oldline;
292 else
293 p = skipwhite(p);
294 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000295
296 /* If 'preserveindent' and 'expandtab' are both set keep the original
297 * characters and allocate accordingly. We will fill the rest with spaces
298 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000299 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000300 {
301 newline = alloc(orig_char_len + size - ind_done + line_len);
302 if (newline == NULL)
303 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000304 todo = size - ind_done;
305 ind_len = orig_char_len + todo; /* Set total length of indent in
306 * characters, which may have been
307 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000308 p = oldline;
309 s = newline;
310 while (orig_char_len > 0)
311 {
312 *s++ = *p++;
313 orig_char_len--;
314 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000315
Bram Moolenaar5002c292007-07-24 13:26:15 +0000316 /* Skip over any additional white space (useful when newindent is less
317 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100318 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000319 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000320
Bram Moolenaar5002c292007-07-24 13:26:15 +0000321 }
322 else
323 {
324 todo = size;
325 newline = alloc(ind_len + line_len);
326 if (newline == NULL)
327 return FALSE;
328 s = newline;
329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
331 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 /* if 'expandtab' isn't set: use TABs */
333 if (!curbuf->b_p_et)
334 {
335 /* If 'preserveindent' is set then reuse as much as possible of
336 * the existing indent structure for the new indent */
337 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
338 {
339 p = oldline;
340 ind_done = 0;
341
Bram Moolenaar1c465442017-03-12 20:10:05 +0100342 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
344 if (*p == TAB)
345 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200346#ifdef FEAT_VARTABS
347 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
348 curbuf->b_p_vts_array);
349#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 tab_pad = (int)curbuf->b_p_ts
351 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /* stop if this tab will overshoot the target */
354 if (todo < tab_pad)
355 break;
356 todo -= tab_pad;
357 ind_done += tab_pad;
358 }
359 else
360 {
361 --todo;
362 ++ind_done;
363 }
364 *s++ = *p++;
365 }
366
367 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200368#ifdef FEAT_VARTABS
369 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
370 curbuf->b_p_vts_array);
371#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (todo >= tab_pad)
375 {
376 *s++ = TAB;
377 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200378#ifdef FEAT_VARTABS
379 ind_done += tab_pad;
380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 }
382
383 p = skipwhite(p);
384 }
385
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200386#ifdef FEAT_VARTABS
387 for (;;)
388 {
389 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
390 curbuf->b_p_vts_array);
391 if (todo < tab_pad)
392 break;
393 *s++ = TAB;
394 todo -= tab_pad;
395 ind_done += tab_pad;
396 }
397#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 while (todo >= (int)curbuf->b_p_ts)
399 {
400 *s++ = TAB;
401 todo -= (int)curbuf->b_p_ts;
402 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 }
405 while (todo > 0)
406 {
407 *s++ = ' ';
408 --todo;
409 }
410 mch_memmove(s, p, (size_t)line_len);
411
Bram Moolenaar663bc892019-01-08 23:07:24 +0100412 // Replace the line (unless undo fails).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
414 {
415 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
416 if (flags & SIN_CHANGED)
417 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar663bc892019-01-08 23:07:24 +0100418
419 // Correct saved cursor position if it is in this line.
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200420 if (saved_cursor.lnum == curwin->w_cursor.lnum)
421 {
422 if (saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100423 // cursor was after the indent, adjust for the number of
424 // bytes added/removed
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200425 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
426 else if (saved_cursor.col >= (colnr_T)(s - newline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100427 // cursor was in the indent, and is now after it, put it back
428 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200429 saved_cursor.col = (colnr_T)(s - newline);
430 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100431#ifdef FEAT_TEXT_PROP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200432 {
433 int added = ind_len - (colnr_T)(p - oldline);
434
435 // When increasing indent this behaves like spaces were inserted at
436 // the old indent, when decreasing indent it behaves like spaces
437 // were deleted at the new indent.
438 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200439 (colnr_T)(added > 0 ? (p - oldline) : ind_len), added, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200440 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100441#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000442 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 }
444 else
445 vim_free(newline);
446
447 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000448 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449}
450
451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 * Return the indent of the current line after a number. Return -1 if no
453 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000454 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 */
456 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100457get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 colnr_T col;
460 pos_T pos;
461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 regmatch_T regmatch;
463 int lead_len = 0; /* length of comment leader */
464
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (lnum > curbuf->b_ml.ml_line_count)
466 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000467 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468
469#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200470 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
471 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200472 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000473#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200474 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
475 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200476 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200477 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200478
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200479 /* vim_regexec() expects a pointer to a line. This lets us
480 * start matching for the flp beyond any comment leader... */
481 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200482 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200483 pos.lnum = lnum;
484 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200485 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200486 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200487 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200488 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000489
490 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 getvcol(curwin, &pos, &col, NULL, NULL);
493 return (int)col;
494}
495
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496#if defined(FEAT_LINEBREAK) || defined(PROTO)
497/*
498 * Return appropriate space number for breakindent, taking influencing
499 * parameters into account. Window must be specified, since it is not
500 * necessarily always the current one.
501 */
502 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100503get_breakindent_win(
504 win_T *wp,
505 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506{
507 static int prev_indent = 0; /* cached indent value */
508 static long prev_ts = 0L; /* cached tabstop value */
509 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100510 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200511#ifdef FEAT_VARTABS
512 static int *prev_vts = NULL; /* cached vartabs values */
513#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514 int bri = 0;
515 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200516 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200517 - ((wp->w_p_nu || wp->w_p_rnu)
518 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
519 ? number_width(wp) + 1 : 0);
520
521 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200522 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200523 || prev_tick != CHANGEDTICK(wp->w_buffer)
524#ifdef FEAT_VARTABS
525 || prev_vts != wp->w_buffer->b_p_vts_array
526#endif
527 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200528 {
529 prev_line = line;
530 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100531 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200532#ifdef FEAT_VARTABS
533 prev_vts = wp->w_buffer->b_p_vts_array;
534 prev_indent = get_indent_str_vtab(line,
535 (int)wp->w_buffer->b_p_ts,
536 wp->w_buffer->b_p_vts_array, wp->w_p_list);
537#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200538 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200539 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200540#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200541 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200542 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200543
544 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200545 if (wp->w_p_brisbr)
546 bri -= vim_strsize(p_sbr);
547
548 /* Add offset for number column, if 'n' is in 'cpoptions' */
549 bri += win_col_off2(wp);
550
551 /* never indent past left window margin */
552 if (bri < 0)
553 bri = 0;
554 /* always leave at least bri_min characters on the left,
555 * if text width is sufficient */
556 else if (bri > eff_wwidth - wp->w_p_brimin)
557 bri = (eff_wwidth - wp->w_p_brimin < 0)
558 ? 0 : eff_wwidth - wp->w_p_brimin;
559
560 return bri;
561}
562#endif
563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#if defined(FEAT_COMMENTS) || defined(PROTO)
565/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200566 * get_leader_len() returns the length in bytes of the prefix of the given
567 * string which introduces a comment. If this string is not a comment then
568 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 * When "flags" is not NULL, it is set to point to the flags of the recognized
570 * comment leader.
571 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +0200572 * If "include_space" is set, include trailing whitespace while calculating the
573 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 */
575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100576get_leader_len(
577 char_u *line,
578 char_u **flags,
579 int backward,
580 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
582 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +0200583 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 int got_com = FALSE;
585 int found_one;
586 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
587 char_u *string; /* pointer to comment string */
588 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +0200589 int middle_match_len = 0;
590 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +0200591 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar81340392012-06-06 16:12:59 +0200593 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100594 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 ++i;
596
597 /*
598 * Repeat to match several nested comment strings.
599 */
Bram Moolenaara4271d52011-05-10 13:38:27 +0200600 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {
602 /*
603 * scan through the 'comments' option for a match
604 */
605 found_one = FALSE;
606 for (list = curbuf->b_p_com; *list; )
607 {
Bram Moolenaara4271d52011-05-10 13:38:27 +0200608 /* Get one option part into part_buf[]. Advance "list" to next
609 * one. Put "string" at start of string. */
610 if (!got_com && flags != NULL)
611 *flags = list; /* remember where flags started */
612 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
614 string = vim_strchr(part_buf, ':');
615 if (string == NULL) /* missing ':', ignore this part */
616 continue;
617 *string++ = NUL; /* isolate flags from string */
618
Bram Moolenaara4271d52011-05-10 13:38:27 +0200619 /* If we found a middle match previously, use that match when this
620 * is not a middle or end. */
621 if (middle_match_len != 0
622 && vim_strchr(part_buf, COM_MIDDLE) == NULL
623 && vim_strchr(part_buf, COM_END) == NULL)
624 break;
625
626 /* When we already found a nested comment, only accept further
627 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
629 continue;
630
Bram Moolenaara4271d52011-05-10 13:38:27 +0200631 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
633 continue;
634
Bram Moolenaara4271d52011-05-10 13:38:27 +0200635 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 * When string starts with white space, must have some white space
637 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +0200638 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100639 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100641 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200642 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100643 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 ++string;
645 }
646 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
647 ;
648 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +0200649 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
Bram Moolenaara4271d52011-05-10 13:38:27 +0200651 /* When 'b' flag used, there must be white space or an
652 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100654 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 continue;
656
Bram Moolenaara4271d52011-05-10 13:38:27 +0200657 /* We have found a match, stop searching unless this is a middle
658 * comment. The middle comment can be a substring of the end
659 * comment in which case it's better to return the length of the
660 * end comment and its flags. Thus we keep searching with middle
661 * and end matches and use an end match if it matches better. */
662 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
663 {
664 if (middle_match_len == 0)
665 {
666 middle_match_len = j;
667 saved_flags = prev_list;
668 }
669 continue;
670 }
671 if (middle_match_len != 0 && j > middle_match_len)
672 /* Use this match instead of the middle match, since it's a
673 * longer thus better match. */
674 middle_match_len = 0;
675
676 if (middle_match_len == 0)
677 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 found_one = TRUE;
679 break;
680 }
681
Bram Moolenaara4271d52011-05-10 13:38:27 +0200682 if (middle_match_len != 0)
683 {
684 /* Use the previously found middle match after failing to find a
685 * match with an end. */
686 if (!got_com && flags != NULL)
687 *flags = saved_flags;
688 i += middle_match_len;
689 found_one = TRUE;
690 }
691
692 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (!found_one)
694 break;
695
Bram Moolenaar81340392012-06-06 16:12:59 +0200696 result = i;
697
Bram Moolenaara4271d52011-05-10 13:38:27 +0200698 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100699 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++i;
701
Bram Moolenaar81340392012-06-06 16:12:59 +0200702 if (include_space)
703 result = i;
704
Bram Moolenaara4271d52011-05-10 13:38:27 +0200705 /* If this comment doesn't nest, stop here. */
706 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (vim_strchr(part_buf, COM_NEST) == NULL)
708 break;
709 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200710 return result;
711}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200712
Bram Moolenaar81340392012-06-06 16:12:59 +0200713/*
714 * Return the offset at which the last comment in line starts. If there is no
715 * comment in the whole line, -1 is returned.
716 *
717 * When "flags" is not null, it is set to point to the flags describing the
718 * recognized comment leader.
719 */
720 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200722{
723 int result = -1;
724 int i, j;
725 int lower_check_bound = 0;
726 char_u *string;
727 char_u *com_leader;
728 char_u *com_flags;
729 char_u *list;
730 int found_one;
731 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
732
733 /*
734 * Repeat to match several nested comment strings.
735 */
736 i = (int)STRLEN(line);
737 while (--i >= lower_check_bound)
738 {
739 /*
740 * scan through the 'comments' option for a match
741 */
742 found_one = FALSE;
743 for (list = curbuf->b_p_com; *list; )
744 {
745 char_u *flags_save = list;
746
747 /*
748 * Get one option part into part_buf[]. Advance list to next one.
749 * put string at start of string.
750 */
751 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
752 string = vim_strchr(part_buf, ':');
753 if (string == NULL) /* If everything is fine, this cannot actually
754 * happen. */
Bram Moolenaar81340392012-06-06 16:12:59 +0200755 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200756 *string++ = NUL; /* Isolate flags from string. */
757 com_leader = string;
758
759 /*
760 * Line contents and string must match.
761 * When string starts with white space, must have some white space
762 * (but the amount does not need to match, there might be a mix of
763 * TABs and spaces).
764 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100765 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200766 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100767 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200768 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100769 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200770 ++string;
771 }
772 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
773 /* do nothing */;
774 if (string[j] != NUL)
775 continue;
776
777 /*
778 * When 'b' flag used, there must be white space or an
779 * end-of-line after the string in the line.
780 */
781 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100782 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200783 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100784
Bram Moolenaar4af72592018-12-09 15:00:52 +0100785 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100786 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100787 // For a middlepart comment, only consider it to match if
788 // everything before the current position in the line is
789 // whitespace. Otherwise we would think we are inside a
790 // comment if the middle part appears somewhere in the middle
791 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100792 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
793 ;
794 if (j < i)
795 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200796 }
797
798 /*
799 * We have found a match, stop searching.
800 */
801 found_one = TRUE;
802
803 if (flags)
804 *flags = flags_save;
805 com_flags = flags_save;
806
807 break;
808 }
809
810 if (found_one)
811 {
812 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
813 int len1, len2, off;
814
815 result = i;
816 /*
817 * If this comment nests, continue searching.
818 */
819 if (vim_strchr(part_buf, COM_NEST) != NULL)
820 continue;
821
822 lower_check_bound = i;
823
824 /* Let's verify whether the comment leader found is a substring
825 * of other comment leaders. If it is, let's adjust the
826 * lower_check_bound so that we make sure that we have determined
827 * the comment leader correctly.
828 */
829
Bram Moolenaar1c465442017-03-12 20:10:05 +0100830 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200831 ++com_leader;
832 len1 = (int)STRLEN(com_leader);
833
834 for (list = curbuf->b_p_com; *list; )
835 {
836 char_u *flags_save = list;
837
838 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
839 if (flags_save == com_flags)
840 continue;
841 string = vim_strchr(part_buf2, ':');
842 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100843 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200844 ++string;
845 len2 = (int)STRLEN(string);
846 if (len2 == 0)
847 continue;
848
849 /* Now we have to verify whether string ends with a substring
850 * beginning the com_leader. */
851 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
852 {
853 --off;
854 if (!STRNCMP(string + off, com_leader, len2 - off))
855 {
856 if (i - off < lower_check_bound)
857 lower_check_bound = i - off;
858 }
859 }
860 }
861 }
862 }
863 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864}
865#endif
866
867/*
868 * Return the number of window lines occupied by buffer line "lnum".
869 */
870 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100871plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 return plines_win(curwin, lnum, TRUE);
874}
875
876 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877plines_win(
878 win_T *wp,
879 linenr_T lnum,
880 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882#if defined(FEAT_DIFF) || defined(PROTO)
883 /* Check for filler lines above this buffer line. When folded the result
884 * is one line anyway. */
885 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
886}
887
888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100889plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 return plines_win_nofill(curwin, lnum, TRUE);
892}
893
894 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100895plines_win_nofill(
896 win_T *wp,
897 linenr_T lnum,
898 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900#endif
901 int lines;
902
903 if (!wp->w_p_wrap)
904 return 1;
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (wp->w_width == 0)
907 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909#ifdef FEAT_FOLDING
910 /* A folded lines is handled just like an empty line. */
911 /* NOTE: Caller must handle lines that are MAYBE folded. */
912 if (lineFolded(wp, lnum) == TRUE)
913 return 1;
914#endif
915
916 lines = plines_win_nofold(wp, lnum);
917 if (winheight > 0 && lines > wp->w_height)
918 return (int)wp->w_height;
919 return lines;
920}
921
922/*
923 * Return number of window lines physical line "lnum" will occupy in window
924 * "wp". Does not care about folding, 'wrap' or 'diff'.
925 */
926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100927plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 char_u *s;
930 long col;
931 int width;
932
933 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
934 if (*s == NUL) /* empty line */
935 return 1;
936 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
937
938 /*
939 * If list mode is on, then the '$' at the end of the line may take up one
940 * extra column.
941 */
942 if (wp->w_p_list && lcs_eol != NUL)
943 col += 1;
944
945 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200946 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200948 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (width <= 0)
950 return 32000;
951 if (col <= width)
952 return 1;
953 col -= width;
954 width += win_col_off2(wp);
955 return (col + (width - 1)) / width + 1;
956}
957
958/*
959 * Like plines_win(), but only reports the number of physical screen lines
960 * used from the start of the line to the given column number.
961 */
962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100963plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 long col;
966 char_u *s;
967 int lines = 0;
968 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200969 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971#ifdef FEAT_DIFF
972 /* Check for filler lines above this buffer line. When folded the result
973 * is one line anyway. */
974 lines = diff_check_fill(wp, lnum);
975#endif
976
977 if (!wp->w_p_wrap)
978 return lines + 1;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (wp->w_width == 0)
981 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar597a4222014-06-25 14:39:50 +0200983 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 col = 0;
986 while (*s != NUL && --column >= 0)
987 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200988 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100989 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991
992 /*
993 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
994 * INSERT mode, then col must be adjusted so that it represents the last
995 * screen position of the TAB. This only fixes an error when the TAB wraps
996 * from one screen line to the next (when 'columns' is not a multiple of
997 * 'ts') -- webb.
998 */
999 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02001000 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001003 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 */
Bram Moolenaar02631462017-09-22 15:20:32 +02001005 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00001006 if (width <= 0)
1007 return 9999;
1008
1009 lines += 1;
1010 if (col > width)
1011 lines += (col - width) / (width + win_col_off2(wp)) + 1;
1012 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001016plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
1018 int count = 0;
1019
1020 while (first <= last)
1021 {
1022#ifdef FEAT_FOLDING
1023 int x;
1024
1025 /* Check if there are any really folded lines, but also included lines
1026 * that are maybe folded. */
1027 x = foldedCount(wp, first, NULL);
1028 if (x > 0)
1029 {
1030 ++count; /* count 1 for "+-- folded" line */
1031 first += x;
1032 }
1033 else
1034#endif
1035 {
1036#ifdef FEAT_DIFF
1037 if (first == wp->w_topline)
1038 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
1039 else
1040#endif
1041 count += plines_win(wp, first, TRUE);
1042 ++first;
1043 }
1044 }
1045 return (count);
1046}
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001049gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001051 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001053 /* When searching columns is sometimes put at the end of a line. */
1054 if (pos->col == MAXCOL)
1055 return NUL;
1056 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (has_mbyte)
1058 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 return (int)*ptr;
1060}
1061
1062 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (has_mbyte)
1066 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return (int)*ml_get_cursor();
1068}
1069
1070/*
1071 * Write a character at the current cursor position.
1072 * It is directly written into the block.
1073 */
1074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
1078 + curwin->w_cursor.col) = c;
1079}
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081/*
1082 * When extra == 0: Return TRUE if the cursor is before or on the first
1083 * non-blank in the line.
1084 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1085 * the line.
1086 */
1087 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001088inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 char_u *ptr;
1091 colnr_T col;
1092
Bram Moolenaar1c465442017-03-12 20:10:05 +01001093 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 ++ptr;
1095 if (col >= curwin->w_cursor.col + extra)
1096 return TRUE;
1097 else
1098 return FALSE;
1099}
1100
1101/*
1102 * Skip to next part of an option argument: Skip space and comma.
1103 */
1104 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001105skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
1107 if (*p == ',')
1108 ++p;
1109 while (*p == ' ')
1110 ++p;
1111 return p;
1112}
1113
1114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * check_status: called when the status bars for the buffer 'buf'
1116 * need to be updated
1117 */
1118 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001119check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
1121 win_T *wp;
1122
Bram Moolenaar29323592016-07-24 22:04:11 +02001123 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (wp->w_buffer == buf && wp->w_status_height)
1125 {
1126 wp->w_redr_status = TRUE;
1127 if (must_redraw < VALID)
1128 must_redraw = VALID;
1129 }
1130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * Ask for a reply from the user, a 'y' or a 'n'.
1134 * No other characters are accepted, the message is repeated until a valid
1135 * reply is entered or CTRL-C is hit.
1136 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
1137 * from any buffers but directly from the user.
1138 *
1139 * return the 'y' or 'n'
1140 */
1141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001142ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143{
1144 int r = ' ';
1145 int save_State = State;
1146
1147 if (exiting) /* put terminal in raw mode for this question */
1148 settmode(TMODE_RAW);
1149 ++no_wait_return;
1150#ifdef USE_ON_FLY_SCROLL
1151 dont_scroll = TRUE; /* disallow scrolling here */
1152#endif
1153 State = CONFIRM; /* mouse behaves like with :confirm */
1154#ifdef FEAT_MOUSE
1155 setmouse(); /* disables mouse for xterm */
1156#endif
1157 ++no_mapping;
1158 ++allow_keys; /* no mapping here, but recognize keys */
1159
1160 while (r != 'y' && r != 'n')
1161 {
1162 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001163 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (direct)
1165 r = get_keystroke();
1166 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00001167 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (r == Ctrl_C || r == ESC)
1169 r = 'n';
1170 msg_putchar(r); /* show what you typed */
1171 out_flush();
1172 }
1173 --no_wait_return;
1174 State = save_State;
1175#ifdef FEAT_MOUSE
1176 setmouse();
1177#endif
1178 --no_mapping;
1179 --allow_keys;
1180
1181 return r;
1182}
1183
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001184#if defined(FEAT_MOUSE) || defined(PROTO)
1185/*
1186 * Return TRUE if "c" is a mouse key.
1187 */
1188 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001189is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001190{
1191 return c == K_LEFTMOUSE
1192 || c == K_LEFTMOUSE_NM
1193 || c == K_LEFTDRAG
1194 || c == K_LEFTRELEASE
1195 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001196 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001197 || c == K_MIDDLEMOUSE
1198 || c == K_MIDDLEDRAG
1199 || c == K_MIDDLERELEASE
1200 || c == K_RIGHTMOUSE
1201 || c == K_RIGHTDRAG
1202 || c == K_RIGHTRELEASE
1203 || c == K_MOUSEDOWN
1204 || c == K_MOUSEUP
1205 || c == K_MOUSELEFT
1206 || c == K_MOUSERIGHT
1207 || c == K_X1MOUSE
1208 || c == K_X1DRAG
1209 || c == K_X1RELEASE
1210 || c == K_X2MOUSE
1211 || c == K_X2DRAG
1212 || c == K_X2RELEASE;
1213}
1214#endif
1215
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001216#if defined(FEAT_EVAL) || defined(PROTO)
1217
1218/*
1219 * "mode()" function
1220 */
1221 void
1222f_mode(typval_T *argvars, typval_T *rettv)
1223{
1224 char_u buf[4];
1225
1226 vim_memset(buf, 0, sizeof(buf));
1227
1228 if (time_for_testing == 93784)
1229 {
1230 /* Testing the two-character code. */
1231 buf[0] = 'x';
1232 buf[1] = '!';
1233 }
1234#ifdef FEAT_TERMINAL
1235 else if (term_use_loop())
1236 buf[0] = 't';
1237#endif
1238 else if (VIsual_active)
1239 {
1240 if (VIsual_select)
1241 buf[0] = VIsual_mode + 's' - 'v';
1242 else
1243 buf[0] = VIsual_mode;
1244 }
1245 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
1246 || State == CONFIRM)
1247 {
1248 buf[0] = 'r';
1249 if (State == ASKMORE)
1250 buf[1] = 'm';
1251 else if (State == CONFIRM)
1252 buf[1] = '?';
1253 }
1254 else if (State == EXTERNCMD)
1255 buf[0] = '!';
1256 else if (State & INSERT)
1257 {
1258 if (State & VREPLACE_FLAG)
1259 {
1260 buf[0] = 'R';
1261 buf[1] = 'v';
1262 }
1263 else
1264 {
1265 if (State & REPLACE_FLAG)
1266 buf[0] = 'R';
1267 else
1268 buf[0] = 'i';
1269 if (ins_compl_active())
1270 buf[1] = 'c';
1271 else if (ctrl_x_mode_not_defined_yet())
1272 buf[1] = 'x';
1273 }
1274 }
1275 else if ((State & CMDLINE) || exmode_active)
1276 {
1277 buf[0] = 'c';
1278 if (exmode_active == EXMODE_VIM)
1279 buf[1] = 'v';
1280 else if (exmode_active == EXMODE_NORMAL)
1281 buf[1] = 'e';
1282 }
1283 else
1284 {
1285 buf[0] = 'n';
1286 if (finish_op)
1287 {
1288 buf[1] = 'o';
1289 // to be able to detect force-linewise/blockwise/characterwise operations
1290 buf[2] = motion_force;
1291 }
1292 else if (restart_edit == 'I' || restart_edit == 'R'
1293 || restart_edit == 'V')
1294 {
1295 buf[1] = 'i';
1296 buf[2] = restart_edit;
1297 }
1298 }
1299
1300 /* Clear out the minor mode when the argument is not a non-zero number or
1301 * non-empty string. */
1302 if (!non_zero_arg(&argvars[0]))
1303 buf[1] = NUL;
1304
1305 rettv->vval.v_string = vim_strsave(buf);
1306 rettv->v_type = VAR_STRING;
1307}
1308
1309 static void
1310may_add_state_char(garray_T *gap, char_u *include, int c)
1311{
1312 if (include == NULL || vim_strchr(include, c) != NULL)
1313 ga_append(gap, c);
1314}
1315
1316/*
1317 * "state()" function
1318 */
1319 void
1320f_state(typval_T *argvars, typval_T *rettv)
1321{
1322 garray_T ga;
1323 char_u *include = NULL;
1324 int i;
1325
1326 ga_init2(&ga, 1, 20);
1327 if (argvars[0].v_type != VAR_UNKNOWN)
1328 include = tv_get_string(&argvars[0]);
1329
1330 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
1331 may_add_state_char(&ga, include, 'm');
1332 if (op_pending())
1333 may_add_state_char(&ga, include, 'o');
1334 if (autocmd_busy)
1335 may_add_state_char(&ga, include, 'x');
1336 if (!ctrl_x_mode_none())
1337 may_add_state_char(&ga, include, 'a');
1338
1339# ifdef FEAT_JOB_CHANNEL
1340 if (channel_in_blocking_wait())
1341 may_add_state_char(&ga, include, 'w');
1342# endif
1343 for (i = 0; i < get_callback_depth() && i < 3; ++i)
1344 may_add_state_char(&ga, include, 'c');
1345 if (msg_scrolled > 0)
1346 may_add_state_char(&ga, include, 's');
1347
1348 rettv->v_type = VAR_STRING;
1349 rettv->vval.v_string = ga.ga_data;
1350}
1351
1352#endif // FEAT_EVAL
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354/*
1355 * Get a key stroke directly from the user.
1356 * Ignores mouse clicks and scrollbar events, except a click for the left
1357 * button (used at the more prompt).
1358 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
1359 * Disadvantage: typeahead is ignored.
1360 * Translates the interrupt character for unix to ESC.
1361 */
1362 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001363get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001365 char_u *buf = NULL;
1366 int buflen = 150;
1367 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 int len = 0;
1369 int n;
1370 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001371 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 mapped_ctrl_c = FALSE; /* mappings are not used here */
1374 for (;;)
1375 {
1376 cursor_on();
1377 out_flush();
1378
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001379 /* Leave some room for check_termcode() to insert a key code into (max
1380 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
1381 * bytes. */
1382 maxlen = (buflen - 6 - len) / 3;
1383 if (buf == NULL)
1384 buf = alloc(buflen);
1385 else if (maxlen < 10)
1386 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001387 char_u *t_buf = buf;
1388
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001389 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001390 * escape sequence. */
1391 buflen += 100;
1392 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001393 if (buf == NULL)
1394 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001395 maxlen = (buflen - 6 - len) / 3;
1396 }
1397 if (buf == NULL)
1398 {
1399 do_outofmem_msg((long_u)buflen);
1400 return ESC; /* panic! */
1401 }
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001404 * terminal code to complete. */
1405 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (n > 0)
1407 {
1408 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02001409 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001411 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00001413 else if (len > 0)
1414 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar4395a712006-09-05 18:57:57 +00001416 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001417 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00001418 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001420
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001421 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001422 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001423 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001424 {
1425 /* Redrawing was postponed, do it now. */
1426 update_screen(0);
1427 setcursor(); /* put cursor back where it belongs */
1428 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001429 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001430 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001431 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001433 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 continue;
1435
1436 /* Handle modifier and/or special key code. */
1437 n = buf[0];
1438 if (n == K_SPECIAL)
1439 {
1440 n = TO_SPECIAL(buf[1], buf[2]);
1441 if (buf[1] == KS_MODIFIER
1442 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001443#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001444 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001445#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001446#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 || n == K_VER_SCROLLBAR
1448 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449#endif
1450 )
1451 {
1452 if (buf[1] == KS_MODIFIER)
1453 mod_mask = buf[2];
1454 len -= 3;
1455 if (len > 0)
1456 mch_memmove(buf, buf + 3, (size_t)len);
1457 continue;
1458 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00001459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if (has_mbyte)
1462 {
1463 if (MB_BYTE2LEN(n) > len)
1464 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001465 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 n = (*mb_ptr2char)(buf);
1467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#ifdef UNIX
1469 if (n == intr_char)
1470 n = ESC;
1471#endif
1472 break;
1473 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001474 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 mapped_ctrl_c = save_mapped_ctrl_c;
1477 return n;
1478}
1479
1480/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001481 * Get a number from the user.
1482 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
1484 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001485get_number(
1486 int colon, /* allow colon to abort */
1487 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 int n = 0;
1490 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001491 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 if (mouse_used != NULL)
1494 *mouse_used = FALSE;
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /* When not printing messages, the user won't know what to type, return a
1497 * zero (as if CR was hit). */
1498 if (msg_silent != 0)
1499 return 0;
1500
1501#ifdef USE_ON_FLY_SCROLL
1502 dont_scroll = TRUE; /* disallow scrolling here */
1503#endif
1504 ++no_mapping;
1505 ++allow_keys; /* no mapping here, but recognize keys */
1506 for (;;)
1507 {
1508 windgoto(msg_row, msg_col);
1509 c = safe_vgetc();
1510 if (VIM_ISDIGIT(c))
1511 {
1512 n = n * 10 + c - '0';
1513 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001514 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1517 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001518 if (typed > 0)
1519 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001520 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001521 --typed;
1522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001525#ifdef FEAT_MOUSE
1526 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1527 {
1528 *mouse_used = TRUE;
1529 n = mouse_row + 1;
1530 break;
1531 }
1532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 else if (n == 0 && c == ':' && colon)
1534 {
1535 stuffcharReadbuff(':');
1536 if (!exmode_active)
1537 cmdline_row = msg_row;
1538 skip_redraw = TRUE; /* skip redraw once */
1539 do_redraw = FALSE;
1540 break;
1541 }
1542 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
1543 break;
1544 }
1545 --no_mapping;
1546 --allow_keys;
1547 return n;
1548}
1549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001550/*
1551 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001552 * When "mouse_used" is not NULL allow using the mouse and in that case return
1553 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001554 */
1555 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001556prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001557{
1558 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001559 int save_cmdline_row;
1560 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001561
1562 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001563 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001564 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001565 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001566 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001567
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001568 // Set the state such that text can be selected/copied/pasted and we still
1569 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1570 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001571 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001572 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001573 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001574 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02001575#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001576 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001577 setmouse();
1578#endif
1579
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580 i = get_number(TRUE, mouse_used);
1581 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001582 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001583 // don't call wait_return() now
1584 if (msg_row > 0)
1585 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001586 need_wait_return = FALSE;
1587 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00001588 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001589 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001590 else
1591 cmdline_row = save_cmdline_row;
1592 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02001593#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001594 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001595 setmouse();
1596#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001598 return i;
1599}
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 long pn;
1605
1606 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1608 return;
1609
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001610 /* We don't want to overwrite another important message, but do overwrite
1611 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1612 * then "put" reports the last action. */
1613 if (keep_msg != NULL && !keep_msg_more)
1614 return;
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (n > 0)
1617 pn = n;
1618 else
1619 pn = -n;
1620
1621 if (pn > p_report)
1622 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001623 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001624 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001625 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001627 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001628 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001630 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1631 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (msg(msg_buf))
1633 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001634 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001635 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 }
1638}
1639
1640/*
1641 * flush map and typeahead buffers and give a warning for an error
1642 */
1643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001644beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 if (emsg_silent == 0)
1647 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001648 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001649 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651}
1652
1653/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001654 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 */
1656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001657vim_beep(
1658 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001660#ifdef FEAT_EVAL
1661 called_vim_beep = TRUE;
1662#endif
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (emsg_silent == 0)
1665 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001666 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1667 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001668#ifdef ELAPSED_FUNC
1669 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001670 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001671
1672 /* Only beep once per half a second, otherwise a sequence of beeps
1673 * would freeze Vim. */
1674 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1675 {
1676 did_init = TRUE;
1677 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001679 if (p_vb
1680#ifdef FEAT_GUI
1681 /* While the GUI is starting up the termcap is set for
1682 * the GUI but the output still goes to a terminal. */
1683 && !(gui.in_use && gui.starting)
1684#endif
1685 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001686 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001687 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001688#ifdef FEAT_VTP
1689 /* No restore color information, refresh the screen. */
1690 if (has_vtp_working() != 0
1691# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001692 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001693# endif
1694 )
1695 {
1696 redraw_later(CLEAR);
1697 update_screen(0);
1698 redrawcmd();
1699 }
1700#endif
1701 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001702 else
1703 out_char(BELL);
1704#ifdef ELAPSED_FUNC
1705 }
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001708
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001709 /* When 'debug' contains "beep" produce a message. If we are sourcing
1710 * a script or executing a function give the user a hint where the beep
1711 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001712 if (vim_strchr(p_debug, 'e') != NULL)
1713 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001714 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001715 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
1718}
1719
1720/*
1721 * To get the "real" home directory:
1722 * - get value of $HOME
1723 * For Unix:
1724 * - go to that directory
1725 * - do mch_dirname() to get the real name of that directory.
1726 * This also works with mounts and links.
1727 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001728 * For Windows:
1729 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001732init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 char_u *var;
1735
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001737 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#ifdef VMS
1740 var = mch_getenv((char_u *)"SYS$LOGIN");
1741#else
1742 var = mch_getenv((char_u *)"HOME");
1743#endif
1744
Bram Moolenaar4f974752019-02-17 17:44:42 +01001745#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001747 * Typically, $HOME is not defined on Windows, unless the user has
1748 * specifically defined it for Vim's sake. However, on Windows NT
1749 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1750 * each user. Try constructing $HOME from these.
1751 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001752 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001753 {
1754 char_u *homedrive, *homepath;
1755
1756 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1757 homepath = mch_getenv((char_u *)"HOMEPATH");
1758 if (homepath == NULL || *homepath == NUL)
1759 homepath = (char_u *)"\\";
1760 if (homedrive != NULL
1761 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1762 {
1763 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1764 if (NameBuff[0] != NUL)
1765 var = NameBuff;
1766 }
1767 }
1768
1769 if (var == NULL)
1770 var = mch_getenv((char_u *)"USERPROFILE");
1771
1772 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 * Weird but true: $HOME may contain an indirect reference to another
1774 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1775 * when $HOME is being set.
1776 */
1777 if (var != NULL && *var == '%')
1778 {
1779 char_u *p;
1780 char_u *exp;
1781
1782 p = vim_strchr(var + 1, '%');
1783 if (p != NULL)
1784 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001785 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 exp = mch_getenv(NameBuff);
1787 if (exp != NULL && *exp != NUL
1788 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1789 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001790 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
1793 }
1794 }
1795
Bram Moolenaar48340b62017-08-29 22:08:53 +02001796 if (var != NULL && *var == NUL) /* empty is same as not set */
1797 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
Bram Moolenaar05159a02005-02-26 23:04:13 +00001799 if (enc_utf8 && var != NULL)
1800 {
1801 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001802 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001803
1804 /* Convert from active codepage to UTF-8. Other conversions are
1805 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001806 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001807 if (pp != NULL)
1808 {
1809 homedir = pp;
1810 return;
1811 }
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 /*
1815 * Default home dir is C:/
1816 * Best assumption we can make in such a situation.
1817 */
1818 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001819 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (var != NULL)
1823 {
1824#ifdef UNIX
1825 /*
1826 * Change to the directory and get the actual path. This resolves
1827 * links. Don't do it when we can't return.
1828 */
1829 if (mch_dirname(NameBuff, MAXPATHL) == OK
1830 && mch_chdir((char *)NameBuff) == 0)
1831 {
1832 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1833 var = IObuff;
1834 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001835 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837#endif
1838 homedir = vim_strsave(var);
1839 }
1840}
1841
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001842#if defined(EXITFREE) || defined(PROTO)
1843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001844free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001845{
1846 vim_free(homedir);
1847}
Bram Moolenaar24305862012-08-15 14:05:05 +02001848
Bram Moolenaar24305862012-08-15 14:05:05 +02001849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001850free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001851{
1852 ga_clear_strings(&ga_users);
1853}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001854#endif
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001857 * Call expand_env() and store the result in an allocated string.
1858 * This is not very memory efficient, this expects the result to be freed
1859 * again soon.
1860 */
1861 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001862expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001863{
1864 return expand_env_save_opt(src, FALSE);
1865}
1866
1867/*
1868 * Idem, but when "one" is TRUE handle the string as one file name, only
1869 * expand "~" at the start.
1870 */
1871 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001872expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001873{
1874 char_u *p;
1875
1876 p = alloc(MAXPATHL);
1877 if (p != NULL)
1878 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1879 return p;
1880}
1881
1882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 * Expand environment variable with path name.
1884 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001885 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * If anything fails no expansion is done and dst equals src.
1887 */
1888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001889expand_env(
1890 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1891 char_u *dst, /* where to put the result */
1892 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001894 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895}
1896
1897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001898expand_env_esc(
1899 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1900 char_u *dst, /* where to put the result */
1901 int dstlen, /* maximum length of the result */
1902 int esc, /* escape spaces in expanded variables */
1903 int one, /* "srcp" is one file name */
1904 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001906 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 char_u *tail;
1908 int c;
1909 char_u *var;
1910 int copy_char;
1911 int mustfree; /* var was allocated, need to free it later */
1912 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001913 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001915 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001916 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001917
1918 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 --dstlen; /* leave one char space for "\," */
1920 while (*src && dstlen > 0)
1921 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001922#ifdef FEAT_EVAL
1923 /* Skip over `=expr`. */
1924 if (src[0] == '`' && src[1] == '=')
1925 {
1926 size_t len;
1927
1928 var = src;
1929 src += 2;
1930 (void)skip_expr(&src);
1931 if (*src == '`')
1932 ++src;
1933 len = src - var;
1934 if (len > (size_t)dstlen)
1935 len = dstlen;
1936 vim_strncpy(dst, var, len);
1937 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001938 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001939 continue;
1940 }
1941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001943 if ((*src == '$'
1944#ifdef VMS
1945 && at_start
1946#endif
1947 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001948#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 || *src == '%'
1950#endif
1951 || (*src == '~' && at_start))
1952 {
1953 mustfree = FALSE;
1954
1955 /*
1956 * The variable name is copied into dst temporarily, because it may
1957 * be a string in read-only memory and a NUL needs to be appended.
1958 */
1959 if (*src != '~') /* environment var */
1960 {
1961 tail = src + 1;
1962 var = dst;
1963 c = dstlen - 1;
1964
1965#ifdef UNIX
1966 /* Unix has ${var-name} type environment vars */
1967 if (*tail == '{' && !vim_isIDc('{'))
1968 {
1969 tail++; /* ignore '{' */
1970 while (c-- > 0 && *tail && *tail != '}')
1971 *var++ = *tail++;
1972 }
1973 else
1974#endif
1975 {
1976 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001977#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 || (*src == '%' && *tail != '%')
1979#endif
1980 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001984#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985# ifdef UNIX
1986 if (src[1] == '{' && *tail != '}')
1987# else
1988 if (*src == '%' && *tail != '%')
1989# endif
1990 var = NULL;
1991 else
1992 {
1993# ifdef UNIX
1994 if (src[1] == '{')
1995# else
1996 if (*src == '%')
1997#endif
1998 ++tail;
1999#endif
2000 *var = NUL;
2001 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002002#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004#endif
2005 }
2006 /* home directory */
2007 else if ( src[1] == NUL
2008 || vim_ispathsep(src[1])
2009 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
2010 {
2011 var = homedir;
2012 tail = src + 1;
2013 }
2014 else /* user directory */
2015 {
2016#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
2017 /*
2018 * Copy ~user to dst[], so we can put a NUL after it.
2019 */
2020 tail = src;
2021 var = dst;
2022 c = dstlen - 1;
2023 while ( c-- > 0
2024 && *tail
2025 && vim_isfilec(*tail)
2026 && !vim_ispathsep(*tail))
2027 *var++ = *tail++;
2028 *var = NUL;
2029# ifdef UNIX
2030 /*
2031 * If the system supports getpwnam(), use it.
2032 * Otherwise, or if getpwnam() fails, the shell is used to
2033 * expand ~user. This is slower and may fail if the shell
2034 * does not support ~user (old versions of /bin/sh).
2035 */
2036# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
2037 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002038 /* Note: memory allocated by getpwnam() is never freed.
2039 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01002040 struct passwd *pw = (*dst == NUL)
2041 ? NULL : getpwnam((char *)dst + 1);
2042
2043 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045 if (var == NULL)
2046# endif
2047 {
2048 expand_T xpc;
2049
2050 ExpandInit(&xpc);
2051 xpc.xp_context = EXPAND_FILES;
2052 var = ExpandOne(&xpc, dst, NULL,
2053 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 mustfree = TRUE;
2055 }
2056
2057# else /* !UNIX, thus VMS */
2058 /*
2059 * USER_HOME is a comma-separated list of
2060 * directories to search for the user account in.
2061 */
2062 {
2063 char_u test[MAXPATHL], paths[MAXPATHL];
2064 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02002065 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 STRCPY(paths, USER_HOME);
2068 next_path = paths;
2069 while (*next_path)
2070 {
2071 for (path = next_path; *next_path && *next_path != ',';
2072 next_path++);
2073 if (*next_path)
2074 *next_path++ = NUL;
2075 STRCPY(test, path);
2076 STRCAT(test, "/");
2077 STRCAT(test, dst + 1);
2078 if (mch_stat(test, &st) == 0)
2079 {
2080 var = alloc(STRLEN(test) + 1);
2081 STRCPY(var, test);
2082 mustfree = TRUE;
2083 break;
2084 }
2085 }
2086 }
2087# endif /* UNIX */
2088#else
2089 /* cannot expand user's home directory, so don't try */
2090 var = NULL;
2091 tail = (char_u *)""; /* for gcc */
2092#endif /* UNIX || VMS */
2093 }
2094
2095#ifdef BACKSLASH_IN_FILENAME
2096 /* If 'shellslash' is set change backslashes to forward slashes.
2097 * Can't use slash_adjust(), p_ssl may be set temporarily. */
2098 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
2099 {
2100 char_u *p = vim_strsave(var);
2101
2102 if (p != NULL)
2103 {
2104 if (mustfree)
2105 vim_free(var);
2106 var = p;
2107 mustfree = TRUE;
2108 forward_slash(var);
2109 }
2110 }
2111#endif
2112
2113 /* If "var" contains white space, escape it with a backslash.
2114 * Required for ":e ~/tt" when $HOME includes a space. */
2115 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
2116 {
2117 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
2118
2119 if (p != NULL)
2120 {
2121 if (mustfree)
2122 vim_free(var);
2123 var = p;
2124 mustfree = TRUE;
2125 }
2126 }
2127
2128 if (var != NULL && *var != NUL
2129 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
2130 {
2131 STRCPY(dst, var);
2132 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002133 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 /* if var[] ends in a path separator and tail[] starts
2135 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
2138 && dst[-1] != ':'
2139#endif
2140 && vim_ispathsep(*tail))
2141 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002142 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 src = tail;
2144 copy_char = FALSE;
2145 }
2146 if (mustfree)
2147 vim_free(var);
2148 }
2149
2150 if (copy_char) /* copy at least one char */
2151 {
2152 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00002153 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002154 * Don't do this when "one" is TRUE, to avoid expanding "~" in
2155 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 */
2157 at_start = FALSE;
2158 if (src[0] == '\\' && src[1] != NUL)
2159 {
2160 *dst++ = *src++;
2161 --dstlen;
2162 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002163 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02002165 if (dstlen > 0)
2166 {
2167 *dst++ = *src++;
2168 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002169
Bram Moolenaar1c864092017-08-06 18:15:45 +02002170 if (startstr != NULL && src - startstr_len >= srcp
2171 && STRNCMP(src - startstr_len, startstr,
2172 startstr_len) == 0)
2173 at_start = TRUE;
2174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02002176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178 *dst = NUL;
2179}
2180
2181/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002182 * If the string between "p" and "pend" ends in "name/", return "pend" minus
2183 * the length of "name/". Otherwise return "pend".
2184 */
2185 static char_u *
2186remove_tail(char_u *p, char_u *pend, char_u *name)
2187{
2188 int len = (int)STRLEN(name) + 1;
2189 char_u *newend = pend - len;
2190
2191 if (newend >= p
2192 && fnamencmp(newend, name, len - 1) == 0
2193 && (newend == p || after_pathsep(p, newend)))
2194 return newend;
2195 return pend;
2196}
2197
2198/*
2199 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
2200 * Return NULL if not, return its name in allocated memory otherwise.
2201 */
2202 static char_u *
2203vim_version_dir(char_u *vimdir)
2204{
2205 char_u *p;
2206
2207 if (vimdir == NULL || *vimdir == NUL)
2208 return NULL;
2209 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
2210 if (p != NULL && mch_isdir(p))
2211 return p;
2212 vim_free(p);
2213 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
2214 if (p != NULL && mch_isdir(p))
2215 return p;
2216 vim_free(p);
2217 return NULL;
2218}
2219
2220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * Vim's version of getenv().
2222 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00002223 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02002224 * "mustfree" is set to TRUE when returned is allocated, it must be
2225 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 */
2227 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002228vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002230 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 char_u *pend;
2232 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002233#ifdef MSWIN
2234 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002236 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (STRCMP(name, "HOME") == 0)
2238 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002240 // Use Wide function
2241 wn = enc_to_utf16(name, NULL);
2242 if (wn == NULL)
2243 return NULL;
2244
2245 wp = _wgetenv(wn);
2246 vim_free(wn);
2247
2248 if (wp != NULL && *wp == NUL) // empty is the same as not set
2249 wp = NULL;
2250
2251 if (wp != NULL)
2252 {
2253 p = utf16_to_enc(wp, NULL);
2254 if (p == NULL)
2255 return NULL;
2256
2257 *mustfree = TRUE;
2258 return p;
2259 }
2260#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002262 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 p = NULL;
2264
2265 if (p != NULL)
2266 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002269 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
2271 if (!vimruntime && STRCMP(name, "VIM") != 0)
2272 return NULL;
2273
2274 /*
2275 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
2276 * Don't do this when default_vimruntime_dir is non-empty.
2277 */
2278 if (vimruntime
2279#ifdef HAVE_PATHDEF
2280 && *default_vimruntime_dir == NUL
2281#endif
2282 )
2283 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002284#ifdef MSWIN
2285 // Use Wide function
2286 wp = _wgetenv(L"VIM");
2287 if (wp != NULL && *wp == NUL) // empty is the same as not set
2288 wp = NULL;
2289 if (wp != NULL)
2290 {
2291 char_u *q = utf16_to_enc(wp, NULL);
2292 if (q != NULL)
2293 {
2294 p = vim_version_dir(q);
2295 *mustfree = TRUE;
2296 if (p == NULL)
2297 p = q;
2298 }
2299 }
2300#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002302 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 p = NULL;
2304 if (p != NULL)
2305 {
2306 p = vim_version_dir(p);
2307 if (p != NULL)
2308 *mustfree = TRUE;
2309 else
2310 p = mch_getenv((char_u *)"VIM");
2311 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314
2315 /*
2316 * When expanding $VIM or $VIMRUNTIME fails, try using:
2317 * - the directory name from 'helpfile' (unless it contains '$')
2318 * - the executable name from argv[0]
2319 */
2320 if (p == NULL)
2321 {
2322 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
2323 p = p_hf;
2324#ifdef USE_EXE_NAME
2325 /*
2326 * Use the name of the executable, obtained from argv[0].
2327 */
2328 else
2329 p = exe_name;
2330#endif
2331 if (p != NULL)
2332 {
2333 /* remove the file name */
2334 pend = gettail(p);
2335
2336 /* remove "doc/" from 'helpfile', if present */
2337 if (p == p_hf)
2338 pend = remove_tail(p, pend, (char_u *)"doc");
2339
2340#ifdef USE_EXE_NAME
2341# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002342 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (p == exe_name)
2344 {
2345 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002346 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002348 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
2349 if (pend1 != pend)
2350 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002351 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002352 if (pnew != NULL)
2353 {
2354 STRNCPY(pnew, p, (pend1 - p));
2355 STRCPY(pnew + (pend1 - p), "Resources/vim");
2356 p = pnew;
2357 pend = p + STRLEN(p);
2358 }
2359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361# endif
2362 /* remove "src/" from exe_name, if present */
2363 if (p == exe_name)
2364 pend = remove_tail(p, pend, (char_u *)"src");
2365#endif
2366
2367 /* for $VIM, remove "runtime/" or "vim54/", if present */
2368 if (!vimruntime)
2369 {
2370 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
2371 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
2372 }
2373
2374 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002375 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002378#ifdef MACOS_X
2379 if (p == exe_name || p == p_hf)
2380#endif
2381 /* check that the result is a directory name */
2382 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01002385 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 else
2387 {
2388#ifdef USE_EXE_NAME
2389 /* may add "/vim54" or "/runtime" if it exists */
2390 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
2391 {
2392 vim_free(p);
2393 p = pend;
2394 }
2395#endif
2396 *mustfree = TRUE;
2397 }
2398 }
2399 }
2400
2401#ifdef HAVE_PATHDEF
2402 /* When there is a pathdef.c file we can use default_vim_dir and
2403 * default_vimruntime_dir */
2404 if (p == NULL)
2405 {
2406 /* Only use default_vimruntime_dir when it is not empty */
2407 if (vimruntime && *default_vimruntime_dir != NUL)
2408 {
2409 p = default_vimruntime_dir;
2410 *mustfree = FALSE;
2411 }
2412 else if (*default_vim_dir != NUL)
2413 {
2414 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
2415 *mustfree = TRUE;
2416 else
2417 {
2418 p = default_vim_dir;
2419 *mustfree = FALSE;
2420 }
2421 }
2422 }
2423#endif
2424
2425 /*
2426 * Set the environment variable, so that the new value can be found fast
2427 * next time, and others can also use it (e.g. Perl).
2428 */
2429 if (p != NULL)
2430 {
2431 if (vimruntime)
2432 {
2433 vim_setenv((char_u *)"VIMRUNTIME", p);
2434 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436 else
2437 {
2438 vim_setenv((char_u *)"VIM", p);
2439 didset_vim = TRUE;
2440 }
2441 }
2442 return p;
2443}
2444
Bram Moolenaar113e1072019-01-20 15:30:40 +01002445#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02002446 void
2447vim_unsetenv(char_u *var)
2448{
2449#ifdef HAVE_UNSETENV
2450 unsetenv((char *)var);
2451#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02002452 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02002453#endif
2454}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002455#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02002456
2457
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * Our portable version of setenv.
2460 */
2461 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002462vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464#ifdef HAVE_SETENV
2465 mch_setenv((char *)name, (char *)val, 1);
2466#else
2467 char_u *envbuf;
2468
2469 /*
2470 * Putenv does not copy the string, it has to remain
2471 * valid. The allocated memory will never be freed.
2472 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002473 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (envbuf != NULL)
2475 {
2476 sprintf((char *)envbuf, "%s=%s", name, val);
2477 putenv((char *)envbuf);
2478 }
2479#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002480#ifdef FEAT_GETTEXT
2481 /*
2482 * When setting $VIMRUNTIME adjust the directory to find message
2483 * translations to $VIMRUNTIME/lang.
2484 */
2485 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2486 {
2487 char_u *buf = concat_str(val, (char_u *)"/lang");
2488
2489 if (buf != NULL)
2490 {
2491 bindtextdomain(VIMPACKAGE, (char *)buf);
2492 vim_free(buf);
2493 }
2494 }
2495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496}
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498/*
2499 * Function given to ExpandGeneric() to obtain an environment variable name.
2500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002502get_env_name(
2503 expand_T *xp UNUSED,
2504 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
Bram Moolenaard0573012017-10-28 21:11:06 +02002506# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002508 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 */
2510 return NULL;
2511# else
2512# ifndef __WIN32__
2513 /* Borland C++ 5.2 has this in a header file. */
2514 extern char **environ;
2515# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002516# define ENVNAMELEN 100
2517 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 char_u *str;
2519 int n;
2520
2521 str = (char_u *)environ[idx];
2522 if (str == NULL)
2523 return NULL;
2524
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002525 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 if (str[n] == '=' || str[n] == NUL)
2528 break;
2529 name[n] = str[n];
2530 }
2531 name[n] = NUL;
2532 return name;
2533# endif
2534}
Bram Moolenaar24305862012-08-15 14:05:05 +02002535
2536/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002537 * Add a user name to the list of users in ga_users.
2538 * Do nothing if user name is NULL or empty.
2539 */
2540 static void
2541add_user(char_u *user, int need_copy)
2542{
2543 char_u *user_copy = (user != NULL && need_copy)
2544 ? vim_strsave(user) : user;
2545
2546 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2547 {
2548 if (need_copy)
2549 vim_free(user);
2550 return;
2551 }
2552 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2553}
2554
2555/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002556 * Find all user names for user completion.
2557 * Done only once and then cached.
2558 */
2559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002560init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002561{
Bram Moolenaar24305862012-08-15 14:05:05 +02002562 static int lazy_init_done = FALSE;
2563
2564 if (lazy_init_done)
2565 return;
2566
2567 lazy_init_done = TRUE;
2568 ga_init2(&ga_users, sizeof(char_u *), 20);
2569
2570# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2571 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002572 struct passwd* pw;
2573
2574 setpwent();
2575 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002576 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002577 endpwent();
2578 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002579# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002580 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002581 DWORD nusers = 0, ntotal = 0, i;
2582 PUSER_INFO_0 uinfo;
2583
2584 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2585 &nusers, &ntotal, NULL) == NERR_Success)
2586 {
2587 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002588 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002589
2590 NetApiBufferFree(uinfo);
2591 }
2592 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002593# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002594# if defined(HAVE_GETPWNAM)
2595 {
2596 char_u *user_env = mch_getenv((char_u *)"USER");
2597
2598 // The $USER environment variable may be a valid remote user name (NIS,
2599 // LDAP) not already listed by getpwent(), as getpwent() only lists
2600 // local user names. If $USER is not already listed, check whether it
2601 // is a valid remote user name using getpwnam() and if it is, add it to
2602 // the list of user names.
2603
2604 if (user_env != NULL && *user_env != NUL)
2605 {
2606 int i;
2607
2608 for (i = 0; i < ga_users.ga_len; i++)
2609 {
2610 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2611
2612 if (STRCMP(local_user, user_env) == 0)
2613 break;
2614 }
2615
2616 if (i == ga_users.ga_len)
2617 {
2618 struct passwd *pw = getpwnam((char *)user_env);
2619
2620 if (pw != NULL)
2621 add_user((char_u *)pw->pw_name, TRUE);
2622 }
2623 }
2624 }
2625# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002626}
2627
2628/*
2629 * Function given to ExpandGeneric() to obtain an user names.
2630 */
2631 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002632get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002633{
2634 init_users();
2635 if (idx < ga_users.ga_len)
2636 return ((char_u **)ga_users.ga_data)[idx];
2637 return NULL;
2638}
2639
2640/*
2641 * Check whether name matches a user name. Return:
2642 * 0 if name does not match any user name.
2643 * 1 if name partially matches the beginning of a user name.
2644 * 2 is name fully matches a user name.
2645 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002646 int
2647match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002648{
2649 int i;
2650 int n = (int)STRLEN(name);
2651 int result = 0;
2652
2653 init_users();
2654 for (i = 0; i < ga_users.ga_len; i++)
2655 {
2656 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2657 return 2; /* full match */
2658 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2659 result = 1; /* partial match */
2660 }
2661 return result;
2662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
2664/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002665 * Concatenate two strings and return the result in allocated memory.
2666 * Returns NULL when out of memory.
2667 */
2668 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002669concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002670{
2671 char_u *dest;
2672 size_t l = STRLEN(str1);
2673
Bram Moolenaar964b3742019-05-24 18:54:09 +02002674 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002675 if (dest != NULL)
2676 {
2677 STRCPY(dest, str1);
2678 STRCPY(dest + l, str2);
2679 }
2680 return dest;
2681}
Bram Moolenaard6754642005-01-17 22:18:45 +00002682
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002683 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002684prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002686#if defined(SIGHUP) && defined(SIG_IGN)
2687 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2688 * makes Vim exit and then handling SIGHUP causes various reentrance
2689 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002690 signal(SIGHUP, SIG_IGN);
2691#endif
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693#ifdef FEAT_GUI
2694 if (gui.in_use)
2695 {
2696 gui.dying = TRUE;
2697 out_trash(); /* trash any pending output */
2698 }
2699 else
2700#endif
2701 {
2702 windgoto((int)Rows - 1, 0);
2703
2704 /*
2705 * Switch terminal mode back now, so messages end up on the "normal"
2706 * screen (if there are two screens).
2707 */
2708 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002709 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 out_flush();
2711 }
2712}
2713
2714/*
2715 * Preserve files and exit.
2716 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002717 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2718 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 */
2720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002721preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 buf_T *buf;
2724
2725 prepare_to_exit();
2726
Bram Moolenaar4770d092006-01-12 23:22:24 +00002727 /* Setting this will prevent free() calls. That avoids calling free()
2728 * recursively when free() was invoked with a bad pointer. */
2729 really_exiting = TRUE;
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 out_str(IObuff);
2732 screen_start(); /* don't know where cursor is now */
2733 out_flush();
2734
2735 ml_close_notmod(); /* close all not-modified buffers */
2736
Bram Moolenaar29323592016-07-24 22:04:11 +02002737 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
2739 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2740 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002741 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 screen_start(); /* don't know where cursor is now */
2743 out_flush();
2744 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2745 break;
2746 }
2747 }
2748
2749 ml_close_all(FALSE); /* close all memfiles, without deleting */
2750
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002751 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 getout(1);
2754}
2755
2756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 * Check for CTRL-C pressed, but only once in a while.
2758 * Should be used instead of ui_breakcheck() for functions that check for
2759 * each line in the file. Calling ui_breakcheck() each time takes too much
2760 * time, because it can be a system call.
2761 */
2762
2763#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002764# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765#endif
2766
2767static int breakcheck_count = 0;
2768
2769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002770line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
2772 if (++breakcheck_count >= BREAKCHECK_SKIP)
2773 {
2774 breakcheck_count = 0;
2775 ui_breakcheck();
2776 }
2777}
2778
2779/*
2780 * Like line_breakcheck() but check 10 times less often.
2781 */
2782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002783fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2786 {
2787 breakcheck_count = 0;
2788 ui_breakcheck();
2789 }
2790}
2791
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002792#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2793 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2794 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
2796#ifndef SEEK_SET
2797# define SEEK_SET 0
2798#endif
2799#ifndef SEEK_END
2800# define SEEK_END 2
2801#endif
2802
2803/*
2804 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002805 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2806 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 * Returns an allocated string, or NULL for error.
2808 */
2809 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002810get_cmd_output(
2811 char_u *cmd,
2812 char_u *infile, /* optional input file name */
2813 int flags, /* can be SHELL_SILENT */
2814 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 char_u *tempname;
2817 char_u *command;
2818 char_u *buffer = NULL;
2819 int len;
2820 int i = 0;
2821 FILE *fd;
2822
2823 if (check_restricted() || check_secure())
2824 return NULL;
2825
2826 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002827 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002829 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 return NULL;
2831 }
2832
2833 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002834 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if (command == NULL)
2836 goto done;
2837
2838 /*
2839 * Call the shell to execute the command (errors are ignored).
2840 * Don't check timestamps here.
2841 */
2842 ++no_check_timestamps;
2843 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2844 --no_check_timestamps;
2845
2846 vim_free(command);
2847
2848 /*
2849 * read the names from the file into memory
2850 */
2851# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00002852 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 fd = mch_fopen((char *)tempname, "r");
2854# else
2855 fd = mch_fopen((char *)tempname, READBIN);
2856# endif
2857
2858 if (fd == NULL)
2859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002860 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 goto done;
2862 }
2863
2864 fseek(fd, 0L, SEEK_END);
2865 len = ftell(fd); /* get size of temp file */
2866 fseek(fd, 0L, SEEK_SET);
2867
2868 buffer = alloc(len + 1);
2869 if (buffer != NULL)
2870 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2871 fclose(fd);
2872 mch_remove(tempname);
2873 if (buffer == NULL)
2874 goto done;
2875#ifdef VMS
2876 len = i; /* VMS doesn't give us what we asked for... */
2877#endif
2878 if (i != len)
2879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002881 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002883 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002884 {
2885 /* Change NUL into SOH, otherwise the string is truncated. */
2886 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002887 if (buffer[i] == NUL)
2888 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002889
Bram Moolenaar162bd912010-07-28 22:29:10 +02002890 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002891 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002892 else
2893 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
2895done:
2896 vim_free(tempname);
2897 return buffer;
2898}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002899
2900# if defined(FEAT_EVAL) || defined(PROTO)
2901
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002902 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002903get_cmd_output_as_rettv(
2904 typval_T *argvars,
2905 typval_T *rettv,
2906 int retlist)
2907{
2908 char_u *res = NULL;
2909 char_u *p;
2910 char_u *infile = NULL;
2911 int err = FALSE;
2912 FILE *fd;
2913 list_T *list = NULL;
2914 int flags = SHELL_SILENT;
2915
2916 rettv->v_type = VAR_STRING;
2917 rettv->vval.v_string = NULL;
2918 if (check_restricted() || check_secure())
2919 goto errret;
2920
2921 if (argvars[1].v_type != VAR_UNKNOWN)
2922 {
2923 /*
2924 * Write the text to a temp file, to be used for input of the shell
2925 * command.
2926 */
2927 if ((infile = vim_tempname('i', TRUE)) == NULL)
2928 {
2929 emsg(_(e_notmp));
2930 goto errret;
2931 }
2932
2933 fd = mch_fopen((char *)infile, WRITEBIN);
2934 if (fd == NULL)
2935 {
2936 semsg(_(e_notopen), infile);
2937 goto errret;
2938 }
2939 if (argvars[1].v_type == VAR_NUMBER)
2940 {
2941 linenr_T lnum;
2942 buf_T *buf;
2943
2944 buf = buflist_findnr(argvars[1].vval.v_number);
2945 if (buf == NULL)
2946 {
2947 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2948 fclose(fd);
2949 goto errret;
2950 }
2951
2952 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2953 {
2954 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2955 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2956 {
2957 err = TRUE;
2958 break;
2959 }
2960 if (putc(NL, fd) == EOF)
2961 {
2962 err = TRUE;
2963 break;
2964 }
2965 }
2966 }
2967 else if (argvars[1].v_type == VAR_LIST)
2968 {
2969 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2970 err = TRUE;
2971 }
2972 else
2973 {
2974 size_t len;
2975 char_u buf[NUMBUFLEN];
2976
2977 p = tv_get_string_buf_chk(&argvars[1], buf);
2978 if (p == NULL)
2979 {
2980 fclose(fd);
2981 goto errret; /* type error; errmsg already given */
2982 }
2983 len = STRLEN(p);
2984 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2985 err = TRUE;
2986 }
2987 if (fclose(fd) != 0)
2988 err = TRUE;
2989 if (err)
2990 {
2991 emsg(_("E677: Error writing temp file"));
2992 goto errret;
2993 }
2994 }
2995
2996 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2997 * echoes typeahead, that messes up the display. */
2998 if (!msg_silent)
2999 flags += SHELL_COOKED;
3000
3001 if (retlist)
3002 {
3003 int len;
3004 listitem_T *li;
3005 char_u *s = NULL;
3006 char_u *start;
3007 char_u *end;
3008 int i;
3009
3010 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
3011 if (res == NULL)
3012 goto errret;
3013
3014 list = list_alloc();
3015 if (list == NULL)
3016 goto errret;
3017
3018 for (i = 0; i < len; ++i)
3019 {
3020 start = res + i;
3021 while (i < len && res[i] != NL)
3022 ++i;
3023 end = res + i;
3024
3025 s = alloc(end - start + 1);
3026 if (s == NULL)
3027 goto errret;
3028
3029 for (p = s; start < end; ++p, ++start)
3030 *p = *start == NUL ? NL : *start;
3031 *p = NUL;
3032
3033 li = listitem_alloc();
3034 if (li == NULL)
3035 {
3036 vim_free(s);
3037 goto errret;
3038 }
3039 li->li_tv.v_type = VAR_STRING;
3040 li->li_tv.v_lock = 0;
3041 li->li_tv.vval.v_string = s;
3042 list_append(list, li);
3043 }
3044
3045 rettv_list_set(rettv, list);
3046 list = NULL;
3047 }
3048 else
3049 {
3050 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
3051#ifdef USE_CRNL
3052 /* translate <CR><NL> into <NL> */
3053 if (res != NULL)
3054 {
3055 char_u *s, *d;
3056
3057 d = res;
3058 for (s = res; *s; ++s)
3059 {
3060 if (s[0] == CAR && s[1] == NL)
3061 ++s;
3062 *d++ = *s;
3063 }
3064 *d = NUL;
3065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02003067 rettv->vval.v_string = res;
3068 res = NULL;
3069 }
3070
3071errret:
3072 if (infile != NULL)
3073 {
3074 mch_remove(infile);
3075 vim_free(infile);
3076 }
3077 if (res != NULL)
3078 vim_free(res);
3079 if (list != NULL)
3080 list_free(list);
3081}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02003084 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
3086 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02003087f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaar26262f82019-09-04 20:59:15 +02003089 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090}
3091
3092/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02003093 * "systemlist()" function
3094 */
3095 void
3096f_systemlist(typval_T *argvars, typval_T *rettv)
3097{
3098 get_cmd_output_as_rettv(argvars, rettv, TRUE);
3099}
3100# endif // FEAT_EVAL
3101
3102#endif
3103
3104/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02003105 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * Don't do this when still processing a command or a mapping.
3107 * Don't do this when inside a ":normal" command.
3108 */
3109 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003110goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 return (p_im && stuff_empty() && typebuf_typed());
3113}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003114
3115/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02003116 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003117 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
3118 * - Remove any argument. E.g., "csh -f" -> "csh".
3119 * But don't allow a space in the path, so that this works:
3120 * "/usr/bin/csh --rcfile ~/.cshrc"
3121 * But don't do that for Windows, it's common to have a space in the path.
3122 */
3123 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003124get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003125{
3126 char_u *p;
3127
Bram Moolenaar4f974752019-02-17 17:44:42 +01003128#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003129 p = gettail(p_sh);
3130 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
3131#else
3132 p = skiptowhite(p_sh);
3133 if (*p == NUL)
3134 {
3135 /* No white space, use the tail. */
3136 p = vim_strsave(gettail(p_sh));
3137 }
3138 else
3139 {
3140 char_u *p1, *p2;
3141
3142 /* Find the last path separator before the space. */
3143 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003144 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003145 if (vim_ispathsep(*p2))
3146 p1 = p2 + 1;
3147 p = vim_strnsave(p1, (int)(p - p1));
3148 }
3149#endif
3150 return p;
3151}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01003152
3153/*
3154 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
3155 * Also check for ":\\", which MS Internet Explorer accepts, return
3156 * URL_BACKSLASH.
3157 */
3158 int
3159path_is_url(char_u *p)
3160{
3161 if (STRNCMP(p, "://", (size_t)3) == 0)
3162 return URL_SLASH;
3163 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
3164 return URL_BACKSLASH;
3165 return 0;
3166}
3167
3168/*
3169 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
3170 * Return URL_BACKSLASH for "name:\\".
3171 * Return zero otherwise.
3172 */
3173 int
3174path_with_url(char_u *fname)
3175{
3176 char_u *p;
3177
3178 for (p = fname; isalpha(*p); ++p)
3179 ;
3180 return path_is_url(p);
3181}