blob: 3c0bd412809e983cd80ecc14912ecb33925ff15f [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
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001151 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001153 State = CONFIRM; // mouse behaves like with :confirm
1154 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001156 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 while (r != 'y' && r != 'n')
1159 {
1160 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001161 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (direct)
1163 r = get_keystroke();
1164 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00001165 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (r == Ctrl_C || r == ESC)
1167 r = 'n';
1168 msg_putchar(r); /* show what you typed */
1169 out_flush();
1170 }
1171 --no_wait_return;
1172 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 --no_mapping;
1175 --allow_keys;
1176
1177 return r;
1178}
1179
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001180#if defined(FEAT_EVAL) || defined(PROTO)
1181
1182/*
1183 * "mode()" function
1184 */
1185 void
1186f_mode(typval_T *argvars, typval_T *rettv)
1187{
1188 char_u buf[4];
1189
1190 vim_memset(buf, 0, sizeof(buf));
1191
1192 if (time_for_testing == 93784)
1193 {
1194 /* Testing the two-character code. */
1195 buf[0] = 'x';
1196 buf[1] = '!';
1197 }
1198#ifdef FEAT_TERMINAL
1199 else if (term_use_loop())
1200 buf[0] = 't';
1201#endif
1202 else if (VIsual_active)
1203 {
1204 if (VIsual_select)
1205 buf[0] = VIsual_mode + 's' - 'v';
1206 else
1207 buf[0] = VIsual_mode;
1208 }
1209 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
1210 || State == CONFIRM)
1211 {
1212 buf[0] = 'r';
1213 if (State == ASKMORE)
1214 buf[1] = 'm';
1215 else if (State == CONFIRM)
1216 buf[1] = '?';
1217 }
1218 else if (State == EXTERNCMD)
1219 buf[0] = '!';
1220 else if (State & INSERT)
1221 {
1222 if (State & VREPLACE_FLAG)
1223 {
1224 buf[0] = 'R';
1225 buf[1] = 'v';
1226 }
1227 else
1228 {
1229 if (State & REPLACE_FLAG)
1230 buf[0] = 'R';
1231 else
1232 buf[0] = 'i';
1233 if (ins_compl_active())
1234 buf[1] = 'c';
1235 else if (ctrl_x_mode_not_defined_yet())
1236 buf[1] = 'x';
1237 }
1238 }
1239 else if ((State & CMDLINE) || exmode_active)
1240 {
1241 buf[0] = 'c';
1242 if (exmode_active == EXMODE_VIM)
1243 buf[1] = 'v';
1244 else if (exmode_active == EXMODE_NORMAL)
1245 buf[1] = 'e';
1246 }
1247 else
1248 {
1249 buf[0] = 'n';
1250 if (finish_op)
1251 {
1252 buf[1] = 'o';
1253 // to be able to detect force-linewise/blockwise/characterwise operations
1254 buf[2] = motion_force;
1255 }
1256 else if (restart_edit == 'I' || restart_edit == 'R'
1257 || restart_edit == 'V')
1258 {
1259 buf[1] = 'i';
1260 buf[2] = restart_edit;
1261 }
1262 }
1263
1264 /* Clear out the minor mode when the argument is not a non-zero number or
1265 * non-empty string. */
1266 if (!non_zero_arg(&argvars[0]))
1267 buf[1] = NUL;
1268
1269 rettv->vval.v_string = vim_strsave(buf);
1270 rettv->v_type = VAR_STRING;
1271}
1272
1273 static void
1274may_add_state_char(garray_T *gap, char_u *include, int c)
1275{
1276 if (include == NULL || vim_strchr(include, c) != NULL)
1277 ga_append(gap, c);
1278}
1279
1280/*
1281 * "state()" function
1282 */
1283 void
1284f_state(typval_T *argvars, typval_T *rettv)
1285{
1286 garray_T ga;
1287 char_u *include = NULL;
1288 int i;
1289
1290 ga_init2(&ga, 1, 20);
1291 if (argvars[0].v_type != VAR_UNKNOWN)
1292 include = tv_get_string(&argvars[0]);
1293
1294 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
1295 may_add_state_char(&ga, include, 'm');
1296 if (op_pending())
1297 may_add_state_char(&ga, include, 'o');
1298 if (autocmd_busy)
1299 may_add_state_char(&ga, include, 'x');
1300 if (!ctrl_x_mode_none())
1301 may_add_state_char(&ga, include, 'a');
1302
1303# ifdef FEAT_JOB_CHANNEL
1304 if (channel_in_blocking_wait())
1305 may_add_state_char(&ga, include, 'w');
1306# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001307 if (!get_was_safe_state())
1308 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001309 for (i = 0; i < get_callback_depth() && i < 3; ++i)
1310 may_add_state_char(&ga, include, 'c');
1311 if (msg_scrolled > 0)
1312 may_add_state_char(&ga, include, 's');
1313
1314 rettv->v_type = VAR_STRING;
1315 rettv->vval.v_string = ga.ga_data;
1316}
1317
1318#endif // FEAT_EVAL
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320/*
1321 * Get a key stroke directly from the user.
1322 * Ignores mouse clicks and scrollbar events, except a click for the left
1323 * button (used at the more prompt).
1324 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
1325 * Disadvantage: typeahead is ignored.
1326 * Translates the interrupt character for unix to ESC.
1327 */
1328 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001329get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001331 char_u *buf = NULL;
1332 int buflen = 150;
1333 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 int len = 0;
1335 int n;
1336 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001337 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 mapped_ctrl_c = FALSE; /* mappings are not used here */
1340 for (;;)
1341 {
1342 cursor_on();
1343 out_flush();
1344
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001345 /* Leave some room for check_termcode() to insert a key code into (max
1346 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
1347 * bytes. */
1348 maxlen = (buflen - 6 - len) / 3;
1349 if (buf == NULL)
1350 buf = alloc(buflen);
1351 else if (maxlen < 10)
1352 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001353 char_u *t_buf = buf;
1354
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001355 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001356 * escape sequence. */
1357 buflen += 100;
1358 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001359 if (buf == NULL)
1360 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001361 maxlen = (buflen - 6 - len) / 3;
1362 }
1363 if (buf == NULL)
1364 {
1365 do_outofmem_msg((long_u)buflen);
1366 return ESC; /* panic! */
1367 }
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001370 * terminal code to complete. */
1371 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (n > 0)
1373 {
1374 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02001375 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001377 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00001379 else if (len > 0)
1380 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaar4395a712006-09-05 18:57:57 +00001382 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001383 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00001384 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001386
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001387 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001388 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001389 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001390 {
1391 /* Redrawing was postponed, do it now. */
1392 update_screen(0);
1393 setcursor(); /* put cursor back where it belongs */
1394 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001395 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001396 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001397 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001399 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 continue;
1401
1402 /* Handle modifier and/or special key code. */
1403 n = buf[0];
1404 if (n == K_SPECIAL)
1405 {
1406 n = TO_SPECIAL(buf[1], buf[2]);
1407 if (buf[1] == KS_MODIFIER
1408 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001409#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001410 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001411#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001412#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 || n == K_VER_SCROLLBAR
1414 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#endif
1416 )
1417 {
1418 if (buf[1] == KS_MODIFIER)
1419 mod_mask = buf[2];
1420 len -= 3;
1421 if (len > 0)
1422 mch_memmove(buf, buf + 3, (size_t)len);
1423 continue;
1424 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00001425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (has_mbyte)
1428 {
1429 if (MB_BYTE2LEN(n) > len)
1430 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001431 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 n = (*mb_ptr2char)(buf);
1433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#ifdef UNIX
1435 if (n == intr_char)
1436 n = ESC;
1437#endif
1438 break;
1439 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001440 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 mapped_ctrl_c = save_mapped_ctrl_c;
1443 return n;
1444}
1445
1446/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001447 * Get a number from the user.
1448 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
1450 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001451get_number(
1452 int colon, /* allow colon to abort */
1453 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 int n = 0;
1456 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001457 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001459 if (mouse_used != NULL)
1460 *mouse_used = FALSE;
1461
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 /* When not printing messages, the user won't know what to type, return a
1463 * zero (as if CR was hit). */
1464 if (msg_silent != 0)
1465 return 0;
1466
1467#ifdef USE_ON_FLY_SCROLL
1468 dont_scroll = TRUE; /* disallow scrolling here */
1469#endif
1470 ++no_mapping;
1471 ++allow_keys; /* no mapping here, but recognize keys */
1472 for (;;)
1473 {
1474 windgoto(msg_row, msg_col);
1475 c = safe_vgetc();
1476 if (VIM_ISDIGIT(c))
1477 {
1478 n = n * 10 + c - '0';
1479 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001480 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 }
1482 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1483 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001484 if (typed > 0)
1485 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001486 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001487 --typed;
1488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001491#ifdef FEAT_MOUSE
1492 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1493 {
1494 *mouse_used = TRUE;
1495 n = mouse_row + 1;
1496 break;
1497 }
1498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 else if (n == 0 && c == ':' && colon)
1500 {
1501 stuffcharReadbuff(':');
1502 if (!exmode_active)
1503 cmdline_row = msg_row;
1504 skip_redraw = TRUE; /* skip redraw once */
1505 do_redraw = FALSE;
1506 break;
1507 }
1508 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
1509 break;
1510 }
1511 --no_mapping;
1512 --allow_keys;
1513 return n;
1514}
1515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001516/*
1517 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001518 * When "mouse_used" is not NULL allow using the mouse and in that case return
1519 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001520 */
1521 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001522prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001523{
1524 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001525 int save_cmdline_row;
1526 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001527
1528 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001529 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001530 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001531 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001532 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001533
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001534 // Set the state such that text can be selected/copied/pasted and we still
1535 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1536 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001537 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001538 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001539 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001540 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001541 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001542 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001543
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544 i = get_number(TRUE, mouse_used);
1545 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001546 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001547 // don't call wait_return() now
1548 if (msg_row > 0)
1549 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001550 need_wait_return = FALSE;
1551 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00001552 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001553 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001554 else
1555 cmdline_row = save_cmdline_row;
1556 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001557 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001558 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001560 return i;
1561}
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001564msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 long pn;
1567
1568 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1570 return;
1571
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001572 /* We don't want to overwrite another important message, but do overwrite
1573 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1574 * then "put" reports the last action. */
1575 if (keep_msg != NULL && !keep_msg_more)
1576 return;
1577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (n > 0)
1579 pn = n;
1580 else
1581 pn = -n;
1582
1583 if (pn > p_report)
1584 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001585 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001586 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001587 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001589 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001590 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001592 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1593 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (msg(msg_buf))
1595 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001596 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001597 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 }
1600}
1601
1602/*
1603 * flush map and typeahead buffers and give a warning for an error
1604 */
1605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001606beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 if (emsg_silent == 0)
1609 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001610 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001611 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613}
1614
1615/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001616 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
1618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001619vim_beep(
1620 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001622#ifdef FEAT_EVAL
1623 called_vim_beep = TRUE;
1624#endif
1625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (emsg_silent == 0)
1627 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001628 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1629 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001630#ifdef ELAPSED_FUNC
1631 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001632 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001633
1634 /* Only beep once per half a second, otherwise a sequence of beeps
1635 * would freeze Vim. */
1636 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1637 {
1638 did_init = TRUE;
1639 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001641 if (p_vb
1642#ifdef FEAT_GUI
1643 /* While the GUI is starting up the termcap is set for
1644 * the GUI but the output still goes to a terminal. */
1645 && !(gui.in_use && gui.starting)
1646#endif
1647 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001648 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001649 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001650#ifdef FEAT_VTP
1651 /* No restore color information, refresh the screen. */
1652 if (has_vtp_working() != 0
1653# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001654 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001655# endif
1656 )
1657 {
1658 redraw_later(CLEAR);
1659 update_screen(0);
1660 redrawcmd();
1661 }
1662#endif
1663 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001664 else
1665 out_char(BELL);
1666#ifdef ELAPSED_FUNC
1667 }
1668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001670
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001671 /* When 'debug' contains "beep" produce a message. If we are sourcing
1672 * a script or executing a function give the user a hint where the beep
1673 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001674 if (vim_strchr(p_debug, 'e') != NULL)
1675 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001676 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001677 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
1680}
1681
1682/*
1683 * To get the "real" home directory:
1684 * - get value of $HOME
1685 * For Unix:
1686 * - go to that directory
1687 * - do mch_dirname() to get the real name of that directory.
1688 * This also works with mounts and links.
1689 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001690 * For Windows:
1691 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001694init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 char_u *var;
1697
Bram Moolenaar05159a02005-02-26 23:04:13 +00001698 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001699 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#ifdef VMS
1702 var = mch_getenv((char_u *)"SYS$LOGIN");
1703#else
1704 var = mch_getenv((char_u *)"HOME");
1705#endif
1706
Bram Moolenaar4f974752019-02-17 17:44:42 +01001707#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001709 * Typically, $HOME is not defined on Windows, unless the user has
1710 * specifically defined it for Vim's sake. However, on Windows NT
1711 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1712 * each user. Try constructing $HOME from these.
1713 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001714 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001715 {
1716 char_u *homedrive, *homepath;
1717
1718 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1719 homepath = mch_getenv((char_u *)"HOMEPATH");
1720 if (homepath == NULL || *homepath == NUL)
1721 homepath = (char_u *)"\\";
1722 if (homedrive != NULL
1723 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1724 {
1725 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1726 if (NameBuff[0] != NUL)
1727 var = NameBuff;
1728 }
1729 }
1730
1731 if (var == NULL)
1732 var = mch_getenv((char_u *)"USERPROFILE");
1733
1734 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 * Weird but true: $HOME may contain an indirect reference to another
1736 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1737 * when $HOME is being set.
1738 */
1739 if (var != NULL && *var == '%')
1740 {
1741 char_u *p;
1742 char_u *exp;
1743
1744 p = vim_strchr(var + 1, '%');
1745 if (p != NULL)
1746 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001747 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 exp = mch_getenv(NameBuff);
1749 if (exp != NULL && *exp != NUL
1750 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1751 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001752 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
1755 }
1756 }
1757
Bram Moolenaar48340b62017-08-29 22:08:53 +02001758 if (var != NULL && *var == NUL) /* empty is same as not set */
1759 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
Bram Moolenaar05159a02005-02-26 23:04:13 +00001761 if (enc_utf8 && var != NULL)
1762 {
1763 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001764 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001765
1766 /* Convert from active codepage to UTF-8. Other conversions are
1767 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001768 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001769 if (pp != NULL)
1770 {
1771 homedir = pp;
1772 return;
1773 }
1774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 /*
1777 * Default home dir is C:/
1778 * Best assumption we can make in such a situation.
1779 */
1780 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001781 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001783
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (var != NULL)
1785 {
1786#ifdef UNIX
1787 /*
1788 * Change to the directory and get the actual path. This resolves
1789 * links. Don't do it when we can't return.
1790 */
1791 if (mch_dirname(NameBuff, MAXPATHL) == OK
1792 && mch_chdir((char *)NameBuff) == 0)
1793 {
1794 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1795 var = IObuff;
1796 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001797 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
1799#endif
1800 homedir = vim_strsave(var);
1801 }
1802}
1803
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001804#if defined(EXITFREE) || defined(PROTO)
1805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001807{
1808 vim_free(homedir);
1809}
Bram Moolenaar24305862012-08-15 14:05:05 +02001810
Bram Moolenaar24305862012-08-15 14:05:05 +02001811 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001812free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001813{
1814 ga_clear_strings(&ga_users);
1815}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001819 * Call expand_env() and store the result in an allocated string.
1820 * This is not very memory efficient, this expects the result to be freed
1821 * again soon.
1822 */
1823 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001824expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001825{
1826 return expand_env_save_opt(src, FALSE);
1827}
1828
1829/*
1830 * Idem, but when "one" is TRUE handle the string as one file name, only
1831 * expand "~" at the start.
1832 */
1833 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001834expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001835{
1836 char_u *p;
1837
1838 p = alloc(MAXPATHL);
1839 if (p != NULL)
1840 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1841 return p;
1842}
1843
1844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 * Expand environment variable with path name.
1846 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001847 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 * If anything fails no expansion is done and dst equals src.
1849 */
1850 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001851expand_env(
1852 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1853 char_u *dst, /* where to put the result */
1854 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001856 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857}
1858
1859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001860expand_env_esc(
1861 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1862 char_u *dst, /* where to put the result */
1863 int dstlen, /* maximum length of the result */
1864 int esc, /* escape spaces in expanded variables */
1865 int one, /* "srcp" is one file name */
1866 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001868 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 char_u *tail;
1870 int c;
1871 char_u *var;
1872 int copy_char;
1873 int mustfree; /* var was allocated, need to free it later */
1874 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001875 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001877 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001878 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001879
1880 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 --dstlen; /* leave one char space for "\," */
1882 while (*src && dstlen > 0)
1883 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001884#ifdef FEAT_EVAL
1885 /* Skip over `=expr`. */
1886 if (src[0] == '`' && src[1] == '=')
1887 {
1888 size_t len;
1889
1890 var = src;
1891 src += 2;
1892 (void)skip_expr(&src);
1893 if (*src == '`')
1894 ++src;
1895 len = src - var;
1896 if (len > (size_t)dstlen)
1897 len = dstlen;
1898 vim_strncpy(dst, var, len);
1899 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001900 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001901 continue;
1902 }
1903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001905 if ((*src == '$'
1906#ifdef VMS
1907 && at_start
1908#endif
1909 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001910#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 || *src == '%'
1912#endif
1913 || (*src == '~' && at_start))
1914 {
1915 mustfree = FALSE;
1916
1917 /*
1918 * The variable name is copied into dst temporarily, because it may
1919 * be a string in read-only memory and a NUL needs to be appended.
1920 */
1921 if (*src != '~') /* environment var */
1922 {
1923 tail = src + 1;
1924 var = dst;
1925 c = dstlen - 1;
1926
1927#ifdef UNIX
1928 /* Unix has ${var-name} type environment vars */
1929 if (*tail == '{' && !vim_isIDc('{'))
1930 {
1931 tail++; /* ignore '{' */
1932 while (c-- > 0 && *tail && *tail != '}')
1933 *var++ = *tail++;
1934 }
1935 else
1936#endif
1937 {
1938 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001939#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 || (*src == '%' && *tail != '%')
1941#endif
1942 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001946#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947# ifdef UNIX
1948 if (src[1] == '{' && *tail != '}')
1949# else
1950 if (*src == '%' && *tail != '%')
1951# endif
1952 var = NULL;
1953 else
1954 {
1955# ifdef UNIX
1956 if (src[1] == '{')
1957# else
1958 if (*src == '%')
1959#endif
1960 ++tail;
1961#endif
1962 *var = NUL;
1963 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001964#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966#endif
1967 }
1968 /* home directory */
1969 else if ( src[1] == NUL
1970 || vim_ispathsep(src[1])
1971 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1972 {
1973 var = homedir;
1974 tail = src + 1;
1975 }
1976 else /* user directory */
1977 {
1978#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1979 /*
1980 * Copy ~user to dst[], so we can put a NUL after it.
1981 */
1982 tail = src;
1983 var = dst;
1984 c = dstlen - 1;
1985 while ( c-- > 0
1986 && *tail
1987 && vim_isfilec(*tail)
1988 && !vim_ispathsep(*tail))
1989 *var++ = *tail++;
1990 *var = NUL;
1991# ifdef UNIX
1992 /*
1993 * If the system supports getpwnam(), use it.
1994 * Otherwise, or if getpwnam() fails, the shell is used to
1995 * expand ~user. This is slower and may fail if the shell
1996 * does not support ~user (old versions of /bin/sh).
1997 */
1998# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1999 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002000 /* Note: memory allocated by getpwnam() is never freed.
2001 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01002002 struct passwd *pw = (*dst == NUL)
2003 ? NULL : getpwnam((char *)dst + 1);
2004
2005 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 if (var == NULL)
2008# endif
2009 {
2010 expand_T xpc;
2011
2012 ExpandInit(&xpc);
2013 xpc.xp_context = EXPAND_FILES;
2014 var = ExpandOne(&xpc, dst, NULL,
2015 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 mustfree = TRUE;
2017 }
2018
2019# else /* !UNIX, thus VMS */
2020 /*
2021 * USER_HOME is a comma-separated list of
2022 * directories to search for the user account in.
2023 */
2024 {
2025 char_u test[MAXPATHL], paths[MAXPATHL];
2026 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02002027 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 STRCPY(paths, USER_HOME);
2030 next_path = paths;
2031 while (*next_path)
2032 {
2033 for (path = next_path; *next_path && *next_path != ',';
2034 next_path++);
2035 if (*next_path)
2036 *next_path++ = NUL;
2037 STRCPY(test, path);
2038 STRCAT(test, "/");
2039 STRCAT(test, dst + 1);
2040 if (mch_stat(test, &st) == 0)
2041 {
2042 var = alloc(STRLEN(test) + 1);
2043 STRCPY(var, test);
2044 mustfree = TRUE;
2045 break;
2046 }
2047 }
2048 }
2049# endif /* UNIX */
2050#else
2051 /* cannot expand user's home directory, so don't try */
2052 var = NULL;
2053 tail = (char_u *)""; /* for gcc */
2054#endif /* UNIX || VMS */
2055 }
2056
2057#ifdef BACKSLASH_IN_FILENAME
2058 /* If 'shellslash' is set change backslashes to forward slashes.
2059 * Can't use slash_adjust(), p_ssl may be set temporarily. */
2060 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
2061 {
2062 char_u *p = vim_strsave(var);
2063
2064 if (p != NULL)
2065 {
2066 if (mustfree)
2067 vim_free(var);
2068 var = p;
2069 mustfree = TRUE;
2070 forward_slash(var);
2071 }
2072 }
2073#endif
2074
2075 /* If "var" contains white space, escape it with a backslash.
2076 * Required for ":e ~/tt" when $HOME includes a space. */
2077 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
2078 {
2079 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
2080
2081 if (p != NULL)
2082 {
2083 if (mustfree)
2084 vim_free(var);
2085 var = p;
2086 mustfree = TRUE;
2087 }
2088 }
2089
2090 if (var != NULL && *var != NUL
2091 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
2092 {
2093 STRCPY(dst, var);
2094 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 /* if var[] ends in a path separator and tail[] starts
2097 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002098 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
2100 && dst[-1] != ':'
2101#endif
2102 && vim_ispathsep(*tail))
2103 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002104 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 src = tail;
2106 copy_char = FALSE;
2107 }
2108 if (mustfree)
2109 vim_free(var);
2110 }
2111
2112 if (copy_char) /* copy at least one char */
2113 {
2114 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00002115 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002116 * Don't do this when "one" is TRUE, to avoid expanding "~" in
2117 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
2119 at_start = FALSE;
2120 if (src[0] == '\\' && src[1] != NUL)
2121 {
2122 *dst++ = *src++;
2123 --dstlen;
2124 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002125 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02002127 if (dstlen > 0)
2128 {
2129 *dst++ = *src++;
2130 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002131
Bram Moolenaar1c864092017-08-06 18:15:45 +02002132 if (startstr != NULL && src - startstr_len >= srcp
2133 && STRNCMP(src - startstr_len, startstr,
2134 startstr_len) == 0)
2135 at_start = TRUE;
2136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02002138
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
2140 *dst = NUL;
2141}
2142
2143/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002144 * If the string between "p" and "pend" ends in "name/", return "pend" minus
2145 * the length of "name/". Otherwise return "pend".
2146 */
2147 static char_u *
2148remove_tail(char_u *p, char_u *pend, char_u *name)
2149{
2150 int len = (int)STRLEN(name) + 1;
2151 char_u *newend = pend - len;
2152
2153 if (newend >= p
2154 && fnamencmp(newend, name, len - 1) == 0
2155 && (newend == p || after_pathsep(p, newend)))
2156 return newend;
2157 return pend;
2158}
2159
2160/*
2161 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
2162 * Return NULL if not, return its name in allocated memory otherwise.
2163 */
2164 static char_u *
2165vim_version_dir(char_u *vimdir)
2166{
2167 char_u *p;
2168
2169 if (vimdir == NULL || *vimdir == NUL)
2170 return NULL;
2171 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
2172 if (p != NULL && mch_isdir(p))
2173 return p;
2174 vim_free(p);
2175 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
2176 if (p != NULL && mch_isdir(p))
2177 return p;
2178 vim_free(p);
2179 return NULL;
2180}
2181
2182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 * Vim's version of getenv().
2184 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00002185 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02002186 * "mustfree" is set to TRUE when returned is allocated, it must be
2187 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
2189 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002190vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002192 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 char_u *pend;
2194 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002195#ifdef MSWIN
2196 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002198 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (STRCMP(name, "HOME") == 0)
2200 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002202 // Use Wide function
2203 wn = enc_to_utf16(name, NULL);
2204 if (wn == NULL)
2205 return NULL;
2206
2207 wp = _wgetenv(wn);
2208 vim_free(wn);
2209
2210 if (wp != NULL && *wp == NUL) // empty is the same as not set
2211 wp = NULL;
2212
2213 if (wp != NULL)
2214 {
2215 p = utf16_to_enc(wp, NULL);
2216 if (p == NULL)
2217 return NULL;
2218
2219 *mustfree = TRUE;
2220 return p;
2221 }
2222#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002224 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 p = NULL;
2226
2227 if (p != NULL)
2228 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002231 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
2233 if (!vimruntime && STRCMP(name, "VIM") != 0)
2234 return NULL;
2235
2236 /*
2237 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
2238 * Don't do this when default_vimruntime_dir is non-empty.
2239 */
2240 if (vimruntime
2241#ifdef HAVE_PATHDEF
2242 && *default_vimruntime_dir == NUL
2243#endif
2244 )
2245 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002246#ifdef MSWIN
2247 // Use Wide function
2248 wp = _wgetenv(L"VIM");
2249 if (wp != NULL && *wp == NUL) // empty is the same as not set
2250 wp = NULL;
2251 if (wp != NULL)
2252 {
2253 char_u *q = utf16_to_enc(wp, NULL);
2254 if (q != NULL)
2255 {
2256 p = vim_version_dir(q);
2257 *mustfree = TRUE;
2258 if (p == NULL)
2259 p = q;
2260 }
2261 }
2262#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002264 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 p = NULL;
2266 if (p != NULL)
2267 {
2268 p = vim_version_dir(p);
2269 if (p != NULL)
2270 *mustfree = TRUE;
2271 else
2272 p = mch_getenv((char_u *)"VIM");
2273 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276
2277 /*
2278 * When expanding $VIM or $VIMRUNTIME fails, try using:
2279 * - the directory name from 'helpfile' (unless it contains '$')
2280 * - the executable name from argv[0]
2281 */
2282 if (p == NULL)
2283 {
2284 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
2285 p = p_hf;
2286#ifdef USE_EXE_NAME
2287 /*
2288 * Use the name of the executable, obtained from argv[0].
2289 */
2290 else
2291 p = exe_name;
2292#endif
2293 if (p != NULL)
2294 {
2295 /* remove the file name */
2296 pend = gettail(p);
2297
2298 /* remove "doc/" from 'helpfile', if present */
2299 if (p == p_hf)
2300 pend = remove_tail(p, pend, (char_u *)"doc");
2301
2302#ifdef USE_EXE_NAME
2303# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002304 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (p == exe_name)
2306 {
2307 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002308 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002310 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
2311 if (pend1 != pend)
2312 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002313 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002314 if (pnew != NULL)
2315 {
2316 STRNCPY(pnew, p, (pend1 - p));
2317 STRCPY(pnew + (pend1 - p), "Resources/vim");
2318 p = pnew;
2319 pend = p + STRLEN(p);
2320 }
2321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323# endif
2324 /* remove "src/" from exe_name, if present */
2325 if (p == exe_name)
2326 pend = remove_tail(p, pend, (char_u *)"src");
2327#endif
2328
2329 /* for $VIM, remove "runtime/" or "vim54/", if present */
2330 if (!vimruntime)
2331 {
2332 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
2333 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
2334 }
2335
2336 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002337 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002340#ifdef MACOS_X
2341 if (p == exe_name || p == p_hf)
2342#endif
2343 /* check that the result is a directory name */
2344 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01002347 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 else
2349 {
2350#ifdef USE_EXE_NAME
2351 /* may add "/vim54" or "/runtime" if it exists */
2352 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
2353 {
2354 vim_free(p);
2355 p = pend;
2356 }
2357#endif
2358 *mustfree = TRUE;
2359 }
2360 }
2361 }
2362
2363#ifdef HAVE_PATHDEF
2364 /* When there is a pathdef.c file we can use default_vim_dir and
2365 * default_vimruntime_dir */
2366 if (p == NULL)
2367 {
2368 /* Only use default_vimruntime_dir when it is not empty */
2369 if (vimruntime && *default_vimruntime_dir != NUL)
2370 {
2371 p = default_vimruntime_dir;
2372 *mustfree = FALSE;
2373 }
2374 else if (*default_vim_dir != NUL)
2375 {
2376 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
2377 *mustfree = TRUE;
2378 else
2379 {
2380 p = default_vim_dir;
2381 *mustfree = FALSE;
2382 }
2383 }
2384 }
2385#endif
2386
2387 /*
2388 * Set the environment variable, so that the new value can be found fast
2389 * next time, and others can also use it (e.g. Perl).
2390 */
2391 if (p != NULL)
2392 {
2393 if (vimruntime)
2394 {
2395 vim_setenv((char_u *)"VIMRUNTIME", p);
2396 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398 else
2399 {
2400 vim_setenv((char_u *)"VIM", p);
2401 didset_vim = TRUE;
2402 }
2403 }
2404 return p;
2405}
2406
Bram Moolenaar113e1072019-01-20 15:30:40 +01002407#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02002408 void
2409vim_unsetenv(char_u *var)
2410{
2411#ifdef HAVE_UNSETENV
2412 unsetenv((char *)var);
2413#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02002414 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02002415#endif
2416}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002417#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02002418
2419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 * Our portable version of setenv.
2422 */
2423 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002424vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426#ifdef HAVE_SETENV
2427 mch_setenv((char *)name, (char *)val, 1);
2428#else
2429 char_u *envbuf;
2430
2431 /*
2432 * Putenv does not copy the string, it has to remain
2433 * valid. The allocated memory will never be freed.
2434 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002435 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 if (envbuf != NULL)
2437 {
2438 sprintf((char *)envbuf, "%s=%s", name, val);
2439 putenv((char *)envbuf);
2440 }
2441#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002442#ifdef FEAT_GETTEXT
2443 /*
2444 * When setting $VIMRUNTIME adjust the directory to find message
2445 * translations to $VIMRUNTIME/lang.
2446 */
2447 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2448 {
2449 char_u *buf = concat_str(val, (char_u *)"/lang");
2450
2451 if (buf != NULL)
2452 {
2453 bindtextdomain(VIMPACKAGE, (char *)buf);
2454 vim_free(buf);
2455 }
2456 }
2457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458}
2459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460/*
2461 * Function given to ExpandGeneric() to obtain an environment variable name.
2462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002464get_env_name(
2465 expand_T *xp UNUSED,
2466 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
Bram Moolenaard0573012017-10-28 21:11:06 +02002468# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002470 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 */
2472 return NULL;
2473# else
2474# ifndef __WIN32__
2475 /* Borland C++ 5.2 has this in a header file. */
2476 extern char **environ;
2477# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002478# define ENVNAMELEN 100
2479 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 char_u *str;
2481 int n;
2482
2483 str = (char_u *)environ[idx];
2484 if (str == NULL)
2485 return NULL;
2486
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002487 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 if (str[n] == '=' || str[n] == NUL)
2490 break;
2491 name[n] = str[n];
2492 }
2493 name[n] = NUL;
2494 return name;
2495# endif
2496}
Bram Moolenaar24305862012-08-15 14:05:05 +02002497
2498/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002499 * Add a user name to the list of users in ga_users.
2500 * Do nothing if user name is NULL or empty.
2501 */
2502 static void
2503add_user(char_u *user, int need_copy)
2504{
2505 char_u *user_copy = (user != NULL && need_copy)
2506 ? vim_strsave(user) : user;
2507
2508 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2509 {
2510 if (need_copy)
2511 vim_free(user);
2512 return;
2513 }
2514 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2515}
2516
2517/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002518 * Find all user names for user completion.
2519 * Done only once and then cached.
2520 */
2521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002522init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002523{
Bram Moolenaar24305862012-08-15 14:05:05 +02002524 static int lazy_init_done = FALSE;
2525
2526 if (lazy_init_done)
2527 return;
2528
2529 lazy_init_done = TRUE;
2530 ga_init2(&ga_users, sizeof(char_u *), 20);
2531
2532# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2533 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002534 struct passwd* pw;
2535
2536 setpwent();
2537 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002538 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002539 endpwent();
2540 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002541# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002542 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002543 DWORD nusers = 0, ntotal = 0, i;
2544 PUSER_INFO_0 uinfo;
2545
2546 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2547 &nusers, &ntotal, NULL) == NERR_Success)
2548 {
2549 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002550 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002551
2552 NetApiBufferFree(uinfo);
2553 }
2554 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002555# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002556# if defined(HAVE_GETPWNAM)
2557 {
2558 char_u *user_env = mch_getenv((char_u *)"USER");
2559
2560 // The $USER environment variable may be a valid remote user name (NIS,
2561 // LDAP) not already listed by getpwent(), as getpwent() only lists
2562 // local user names. If $USER is not already listed, check whether it
2563 // is a valid remote user name using getpwnam() and if it is, add it to
2564 // the list of user names.
2565
2566 if (user_env != NULL && *user_env != NUL)
2567 {
2568 int i;
2569
2570 for (i = 0; i < ga_users.ga_len; i++)
2571 {
2572 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2573
2574 if (STRCMP(local_user, user_env) == 0)
2575 break;
2576 }
2577
2578 if (i == ga_users.ga_len)
2579 {
2580 struct passwd *pw = getpwnam((char *)user_env);
2581
2582 if (pw != NULL)
2583 add_user((char_u *)pw->pw_name, TRUE);
2584 }
2585 }
2586 }
2587# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002588}
2589
2590/*
2591 * Function given to ExpandGeneric() to obtain an user names.
2592 */
2593 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002594get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002595{
2596 init_users();
2597 if (idx < ga_users.ga_len)
2598 return ((char_u **)ga_users.ga_data)[idx];
2599 return NULL;
2600}
2601
2602/*
2603 * Check whether name matches a user name. Return:
2604 * 0 if name does not match any user name.
2605 * 1 if name partially matches the beginning of a user name.
2606 * 2 is name fully matches a user name.
2607 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002608 int
2609match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002610{
2611 int i;
2612 int n = (int)STRLEN(name);
2613 int result = 0;
2614
2615 init_users();
2616 for (i = 0; i < ga_users.ga_len; i++)
2617 {
2618 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2619 return 2; /* full match */
2620 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2621 result = 1; /* partial match */
2622 }
2623 return result;
2624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002627 * Concatenate two strings and return the result in allocated memory.
2628 * Returns NULL when out of memory.
2629 */
2630 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002631concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002632{
2633 char_u *dest;
2634 size_t l = STRLEN(str1);
2635
Bram Moolenaar964b3742019-05-24 18:54:09 +02002636 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002637 if (dest != NULL)
2638 {
2639 STRCPY(dest, str1);
2640 STRCPY(dest + l, str2);
2641 }
2642 return dest;
2643}
Bram Moolenaard6754642005-01-17 22:18:45 +00002644
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002646prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002648#if defined(SIGHUP) && defined(SIG_IGN)
2649 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2650 * makes Vim exit and then handling SIGHUP causes various reentrance
2651 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002652 signal(SIGHUP, SIG_IGN);
2653#endif
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#ifdef FEAT_GUI
2656 if (gui.in_use)
2657 {
2658 gui.dying = TRUE;
2659 out_trash(); /* trash any pending output */
2660 }
2661 else
2662#endif
2663 {
2664 windgoto((int)Rows - 1, 0);
2665
2666 /*
2667 * Switch terminal mode back now, so messages end up on the "normal"
2668 * screen (if there are two screens).
2669 */
2670 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002671 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 out_flush();
2673 }
2674}
2675
2676/*
2677 * Preserve files and exit.
2678 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002679 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2680 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 */
2682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002683preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 buf_T *buf;
2686
2687 prepare_to_exit();
2688
Bram Moolenaar4770d092006-01-12 23:22:24 +00002689 /* Setting this will prevent free() calls. That avoids calling free()
2690 * recursively when free() was invoked with a bad pointer. */
2691 really_exiting = TRUE;
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 out_str(IObuff);
2694 screen_start(); /* don't know where cursor is now */
2695 out_flush();
2696
2697 ml_close_notmod(); /* close all not-modified buffers */
2698
Bram Moolenaar29323592016-07-24 22:04:11 +02002699 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2702 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002703 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 screen_start(); /* don't know where cursor is now */
2705 out_flush();
2706 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2707 break;
2708 }
2709 }
2710
2711 ml_close_all(FALSE); /* close all memfiles, without deleting */
2712
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002713 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715 getout(1);
2716}
2717
2718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 * Check for CTRL-C pressed, but only once in a while.
2720 * Should be used instead of ui_breakcheck() for functions that check for
2721 * each line in the file. Calling ui_breakcheck() each time takes too much
2722 * time, because it can be a system call.
2723 */
2724
2725#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002726# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727#endif
2728
2729static int breakcheck_count = 0;
2730
2731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002732line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 if (++breakcheck_count >= BREAKCHECK_SKIP)
2735 {
2736 breakcheck_count = 0;
2737 ui_breakcheck();
2738 }
2739}
2740
2741/*
2742 * Like line_breakcheck() but check 10 times less often.
2743 */
2744 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002745fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2748 {
2749 breakcheck_count = 0;
2750 ui_breakcheck();
2751 }
2752}
2753
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002754#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2755 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2756 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
2758#ifndef SEEK_SET
2759# define SEEK_SET 0
2760#endif
2761#ifndef SEEK_END
2762# define SEEK_END 2
2763#endif
2764
2765/*
2766 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002767 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2768 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 * Returns an allocated string, or NULL for error.
2770 */
2771 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002772get_cmd_output(
2773 char_u *cmd,
2774 char_u *infile, /* optional input file name */
2775 int flags, /* can be SHELL_SILENT */
2776 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 char_u *tempname;
2779 char_u *command;
2780 char_u *buffer = NULL;
2781 int len;
2782 int i = 0;
2783 FILE *fd;
2784
2785 if (check_restricted() || check_secure())
2786 return NULL;
2787
2788 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002789 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002791 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 return NULL;
2793 }
2794
2795 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002796 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (command == NULL)
2798 goto done;
2799
2800 /*
2801 * Call the shell to execute the command (errors are ignored).
2802 * Don't check timestamps here.
2803 */
2804 ++no_check_timestamps;
2805 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2806 --no_check_timestamps;
2807
2808 vim_free(command);
2809
2810 /*
2811 * read the names from the file into memory
2812 */
2813# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00002814 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 fd = mch_fopen((char *)tempname, "r");
2816# else
2817 fd = mch_fopen((char *)tempname, READBIN);
2818# endif
2819
2820 if (fd == NULL)
2821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002822 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 goto done;
2824 }
2825
2826 fseek(fd, 0L, SEEK_END);
2827 len = ftell(fd); /* get size of temp file */
2828 fseek(fd, 0L, SEEK_SET);
2829
2830 buffer = alloc(len + 1);
2831 if (buffer != NULL)
2832 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2833 fclose(fd);
2834 mch_remove(tempname);
2835 if (buffer == NULL)
2836 goto done;
2837#ifdef VMS
2838 len = i; /* VMS doesn't give us what we asked for... */
2839#endif
2840 if (i != len)
2841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002843 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002845 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002846 {
2847 /* Change NUL into SOH, otherwise the string is truncated. */
2848 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002849 if (buffer[i] == NUL)
2850 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002851
Bram Moolenaar162bd912010-07-28 22:29:10 +02002852 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002853 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002854 else
2855 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857done:
2858 vim_free(tempname);
2859 return buffer;
2860}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002861
2862# if defined(FEAT_EVAL) || defined(PROTO)
2863
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002864 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002865get_cmd_output_as_rettv(
2866 typval_T *argvars,
2867 typval_T *rettv,
2868 int retlist)
2869{
2870 char_u *res = NULL;
2871 char_u *p;
2872 char_u *infile = NULL;
2873 int err = FALSE;
2874 FILE *fd;
2875 list_T *list = NULL;
2876 int flags = SHELL_SILENT;
2877
2878 rettv->v_type = VAR_STRING;
2879 rettv->vval.v_string = NULL;
2880 if (check_restricted() || check_secure())
2881 goto errret;
2882
2883 if (argvars[1].v_type != VAR_UNKNOWN)
2884 {
2885 /*
2886 * Write the text to a temp file, to be used for input of the shell
2887 * command.
2888 */
2889 if ((infile = vim_tempname('i', TRUE)) == NULL)
2890 {
2891 emsg(_(e_notmp));
2892 goto errret;
2893 }
2894
2895 fd = mch_fopen((char *)infile, WRITEBIN);
2896 if (fd == NULL)
2897 {
2898 semsg(_(e_notopen), infile);
2899 goto errret;
2900 }
2901 if (argvars[1].v_type == VAR_NUMBER)
2902 {
2903 linenr_T lnum;
2904 buf_T *buf;
2905
2906 buf = buflist_findnr(argvars[1].vval.v_number);
2907 if (buf == NULL)
2908 {
2909 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2910 fclose(fd);
2911 goto errret;
2912 }
2913
2914 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2915 {
2916 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2917 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2918 {
2919 err = TRUE;
2920 break;
2921 }
2922 if (putc(NL, fd) == EOF)
2923 {
2924 err = TRUE;
2925 break;
2926 }
2927 }
2928 }
2929 else if (argvars[1].v_type == VAR_LIST)
2930 {
2931 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2932 err = TRUE;
2933 }
2934 else
2935 {
2936 size_t len;
2937 char_u buf[NUMBUFLEN];
2938
2939 p = tv_get_string_buf_chk(&argvars[1], buf);
2940 if (p == NULL)
2941 {
2942 fclose(fd);
2943 goto errret; /* type error; errmsg already given */
2944 }
2945 len = STRLEN(p);
2946 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2947 err = TRUE;
2948 }
2949 if (fclose(fd) != 0)
2950 err = TRUE;
2951 if (err)
2952 {
2953 emsg(_("E677: Error writing temp file"));
2954 goto errret;
2955 }
2956 }
2957
2958 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2959 * echoes typeahead, that messes up the display. */
2960 if (!msg_silent)
2961 flags += SHELL_COOKED;
2962
2963 if (retlist)
2964 {
2965 int len;
2966 listitem_T *li;
2967 char_u *s = NULL;
2968 char_u *start;
2969 char_u *end;
2970 int i;
2971
2972 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2973 if (res == NULL)
2974 goto errret;
2975
2976 list = list_alloc();
2977 if (list == NULL)
2978 goto errret;
2979
2980 for (i = 0; i < len; ++i)
2981 {
2982 start = res + i;
2983 while (i < len && res[i] != NL)
2984 ++i;
2985 end = res + i;
2986
2987 s = alloc(end - start + 1);
2988 if (s == NULL)
2989 goto errret;
2990
2991 for (p = s; start < end; ++p, ++start)
2992 *p = *start == NUL ? NL : *start;
2993 *p = NUL;
2994
2995 li = listitem_alloc();
2996 if (li == NULL)
2997 {
2998 vim_free(s);
2999 goto errret;
3000 }
3001 li->li_tv.v_type = VAR_STRING;
3002 li->li_tv.v_lock = 0;
3003 li->li_tv.vval.v_string = s;
3004 list_append(list, li);
3005 }
3006
3007 rettv_list_set(rettv, list);
3008 list = NULL;
3009 }
3010 else
3011 {
3012 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
3013#ifdef USE_CRNL
3014 /* translate <CR><NL> into <NL> */
3015 if (res != NULL)
3016 {
3017 char_u *s, *d;
3018
3019 d = res;
3020 for (s = res; *s; ++s)
3021 {
3022 if (s[0] == CAR && s[1] == NL)
3023 ++s;
3024 *d++ = *s;
3025 }
3026 *d = NUL;
3027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02003029 rettv->vval.v_string = res;
3030 res = NULL;
3031 }
3032
3033errret:
3034 if (infile != NULL)
3035 {
3036 mch_remove(infile);
3037 vim_free(infile);
3038 }
3039 if (res != NULL)
3040 vim_free(res);
3041 if (list != NULL)
3042 list_free(list);
3043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02003046 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 */
3048 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02003049f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaar26262f82019-09-04 20:59:15 +02003051 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052}
3053
3054/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02003055 * "systemlist()" function
3056 */
3057 void
3058f_systemlist(typval_T *argvars, typval_T *rettv)
3059{
3060 get_cmd_output_as_rettv(argvars, rettv, TRUE);
3061}
3062# endif // FEAT_EVAL
3063
3064#endif
3065
3066/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02003067 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 * Don't do this when still processing a command or a mapping.
3069 * Don't do this when inside a ":normal" command.
3070 */
3071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003072goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 return (p_im && stuff_empty() && typebuf_typed());
3075}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003076
3077/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02003078 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003079 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
3080 * - Remove any argument. E.g., "csh -f" -> "csh".
3081 * But don't allow a space in the path, so that this works:
3082 * "/usr/bin/csh --rcfile ~/.cshrc"
3083 * But don't do that for Windows, it's common to have a space in the path.
3084 */
3085 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003086get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003087{
3088 char_u *p;
3089
Bram Moolenaar4f974752019-02-17 17:44:42 +01003090#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003091 p = gettail(p_sh);
3092 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
3093#else
3094 p = skiptowhite(p_sh);
3095 if (*p == NUL)
3096 {
3097 /* No white space, use the tail. */
3098 p = vim_strsave(gettail(p_sh));
3099 }
3100 else
3101 {
3102 char_u *p1, *p2;
3103
3104 /* Find the last path separator before the space. */
3105 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003106 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003107 if (vim_ispathsep(*p2))
3108 p1 = p2 + 1;
3109 p = vim_strnsave(p1, (int)(p - p1));
3110 }
3111#endif
3112 return p;
3113}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01003114
3115/*
3116 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
3117 * Also check for ":\\", which MS Internet Explorer accepts, return
3118 * URL_BACKSLASH.
3119 */
3120 int
3121path_is_url(char_u *p)
3122{
3123 if (STRNCMP(p, "://", (size_t)3) == 0)
3124 return URL_SLASH;
3125 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
3126 return URL_BACKSLASH;
3127 return 0;
3128}
3129
3130/*
3131 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
3132 * Return URL_BACKSLASH for "name:\\".
3133 * Return zero otherwise.
3134 */
3135 int
3136path_with_url(char_u *fname)
3137{
3138 char_u *p;
3139
3140 for (p = fname; isalpha(*p); ++p)
3141 ;
3142 return path_is_url(p);
3143}