blob: d43de06c1681dd431e946494613f837b9d896c47 [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
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
470 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200472
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200473 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
474 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200475 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200476 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200477
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200478 /* vim_regexec() expects a pointer to a line. This lets us
479 * start matching for the flp beyond any comment leader... */
480 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200481 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200482 pos.lnum = lnum;
483 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200484 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200485 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200486 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200487 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000488
489 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 getvcol(curwin, &pos, &col, NULL, NULL);
492 return (int)col;
493}
494
Bram Moolenaar597a4222014-06-25 14:39:50 +0200495#if defined(FEAT_LINEBREAK) || defined(PROTO)
496/*
497 * Return appropriate space number for breakindent, taking influencing
498 * parameters into account. Window must be specified, since it is not
499 * necessarily always the current one.
500 */
501 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100502get_breakindent_win(
503 win_T *wp,
504 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200505{
506 static int prev_indent = 0; /* cached indent value */
507 static long prev_ts = 0L; /* cached tabstop value */
508 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100509 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200510#ifdef FEAT_VARTABS
511 static int *prev_vts = NULL; /* cached vartabs values */
512#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200513 int bri = 0;
514 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200515 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 - ((wp->w_p_nu || wp->w_p_rnu)
517 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
518 ? number_width(wp) + 1 : 0);
519
520 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200521 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200522 || prev_tick != CHANGEDTICK(wp->w_buffer)
523#ifdef FEAT_VARTABS
524 || prev_vts != wp->w_buffer->b_p_vts_array
525#endif
526 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200527 {
528 prev_line = line;
529 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100530 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200531#ifdef FEAT_VARTABS
532 prev_vts = wp->w_buffer->b_p_vts_array;
533 prev_indent = get_indent_str_vtab(line,
534 (int)wp->w_buffer->b_p_ts,
535 wp->w_buffer->b_p_vts_array, wp->w_p_list);
536#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200537 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200538 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200539#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200540 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200541 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200542
543 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200544 if (wp->w_p_brisbr)
545 bri -= vim_strsize(p_sbr);
546
547 /* Add offset for number column, if 'n' is in 'cpoptions' */
548 bri += win_col_off2(wp);
549
550 /* never indent past left window margin */
551 if (bri < 0)
552 bri = 0;
553 /* always leave at least bri_min characters on the left,
554 * if text width is sufficient */
555 else if (bri > eff_wwidth - wp->w_p_brimin)
556 bri = (eff_wwidth - wp->w_p_brimin < 0)
557 ? 0 : eff_wwidth - wp->w_p_brimin;
558
559 return bri;
560}
561#endif
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200564 * get_leader_len() returns the length in bytes of the prefix of the given
565 * string which introduces a comment. If this string is not a comment then
566 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 * When "flags" is not NULL, it is set to point to the flags of the recognized
568 * comment leader.
569 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +0200570 * If "include_space" is set, include trailing whitespace while calculating the
571 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 */
573 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100574get_leader_len(
575 char_u *line,
576 char_u **flags,
577 int backward,
578 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +0200581 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 int got_com = FALSE;
583 int found_one;
584 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
585 char_u *string; /* pointer to comment string */
586 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +0200587 int middle_match_len = 0;
588 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +0200589 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar81340392012-06-06 16:12:59 +0200591 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100592 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 ++i;
594
595 /*
596 * Repeat to match several nested comment strings.
597 */
Bram Moolenaara4271d52011-05-10 13:38:27 +0200598 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
600 /*
601 * scan through the 'comments' option for a match
602 */
603 found_one = FALSE;
604 for (list = curbuf->b_p_com; *list; )
605 {
Bram Moolenaara4271d52011-05-10 13:38:27 +0200606 /* Get one option part into part_buf[]. Advance "list" to next
607 * one. Put "string" at start of string. */
608 if (!got_com && flags != NULL)
609 *flags = list; /* remember where flags started */
610 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
612 string = vim_strchr(part_buf, ':');
613 if (string == NULL) /* missing ':', ignore this part */
614 continue;
615 *string++ = NUL; /* isolate flags from string */
616
Bram Moolenaara4271d52011-05-10 13:38:27 +0200617 /* If we found a middle match previously, use that match when this
618 * is not a middle or end. */
619 if (middle_match_len != 0
620 && vim_strchr(part_buf, COM_MIDDLE) == NULL
621 && vim_strchr(part_buf, COM_END) == NULL)
622 break;
623
624 /* When we already found a nested comment, only accept further
625 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
627 continue;
628
Bram Moolenaara4271d52011-05-10 13:38:27 +0200629 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
631 continue;
632
Bram Moolenaara4271d52011-05-10 13:38:27 +0200633 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 * When string starts with white space, must have some white space
635 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +0200636 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100637 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100639 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200640 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100641 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 ++string;
643 }
644 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
645 ;
646 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +0200647 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaara4271d52011-05-10 13:38:27 +0200649 /* When 'b' flag used, there must be white space or an
650 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100652 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 continue;
654
Bram Moolenaara4271d52011-05-10 13:38:27 +0200655 /* We have found a match, stop searching unless this is a middle
656 * comment. The middle comment can be a substring of the end
657 * comment in which case it's better to return the length of the
658 * end comment and its flags. Thus we keep searching with middle
659 * and end matches and use an end match if it matches better. */
660 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
661 {
662 if (middle_match_len == 0)
663 {
664 middle_match_len = j;
665 saved_flags = prev_list;
666 }
667 continue;
668 }
669 if (middle_match_len != 0 && j > middle_match_len)
670 /* Use this match instead of the middle match, since it's a
671 * longer thus better match. */
672 middle_match_len = 0;
673
674 if (middle_match_len == 0)
675 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 found_one = TRUE;
677 break;
678 }
679
Bram Moolenaara4271d52011-05-10 13:38:27 +0200680 if (middle_match_len != 0)
681 {
682 /* Use the previously found middle match after failing to find a
683 * match with an end. */
684 if (!got_com && flags != NULL)
685 *flags = saved_flags;
686 i += middle_match_len;
687 found_one = TRUE;
688 }
689
690 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 if (!found_one)
692 break;
693
Bram Moolenaar81340392012-06-06 16:12:59 +0200694 result = i;
695
Bram Moolenaara4271d52011-05-10 13:38:27 +0200696 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100697 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++i;
699
Bram Moolenaar81340392012-06-06 16:12:59 +0200700 if (include_space)
701 result = i;
702
Bram Moolenaara4271d52011-05-10 13:38:27 +0200703 /* If this comment doesn't nest, stop here. */
704 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 if (vim_strchr(part_buf, COM_NEST) == NULL)
706 break;
707 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200708 return result;
709}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200710
Bram Moolenaar81340392012-06-06 16:12:59 +0200711/*
712 * Return the offset at which the last comment in line starts. If there is no
713 * comment in the whole line, -1 is returned.
714 *
715 * When "flags" is not null, it is set to point to the flags describing the
716 * recognized comment leader.
717 */
718 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100719get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200720{
721 int result = -1;
722 int i, j;
723 int lower_check_bound = 0;
724 char_u *string;
725 char_u *com_leader;
726 char_u *com_flags;
727 char_u *list;
728 int found_one;
729 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
730
731 /*
732 * Repeat to match several nested comment strings.
733 */
734 i = (int)STRLEN(line);
735 while (--i >= lower_check_bound)
736 {
737 /*
738 * scan through the 'comments' option for a match
739 */
740 found_one = FALSE;
741 for (list = curbuf->b_p_com; *list; )
742 {
743 char_u *flags_save = list;
744
745 /*
746 * Get one option part into part_buf[]. Advance list to next one.
747 * put string at start of string.
748 */
749 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
750 string = vim_strchr(part_buf, ':');
751 if (string == NULL) /* If everything is fine, this cannot actually
752 * happen. */
Bram Moolenaar81340392012-06-06 16:12:59 +0200753 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200754 *string++ = NUL; /* Isolate flags from string. */
755 com_leader = string;
756
757 /*
758 * Line contents and string must match.
759 * When string starts with white space, must have some white space
760 * (but the amount does not need to match, there might be a mix of
761 * TABs and spaces).
762 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100763 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200764 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100765 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200766 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100767 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200768 ++string;
769 }
770 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
771 /* do nothing */;
772 if (string[j] != NUL)
773 continue;
774
775 /*
776 * When 'b' flag used, there must be white space or an
777 * end-of-line after the string in the line.
778 */
779 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100780 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200781 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100782
Bram Moolenaar4af72592018-12-09 15:00:52 +0100783 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100784 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100785 // For a middlepart comment, only consider it to match if
786 // everything before the current position in the line is
787 // whitespace. Otherwise we would think we are inside a
788 // comment if the middle part appears somewhere in the middle
789 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100790 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
791 ;
792 if (j < i)
793 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200794 }
795
796 /*
797 * We have found a match, stop searching.
798 */
799 found_one = TRUE;
800
801 if (flags)
802 *flags = flags_save;
803 com_flags = flags_save;
804
805 break;
806 }
807
808 if (found_one)
809 {
810 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
811 int len1, len2, off;
812
813 result = i;
814 /*
815 * If this comment nests, continue searching.
816 */
817 if (vim_strchr(part_buf, COM_NEST) != NULL)
818 continue;
819
820 lower_check_bound = i;
821
822 /* Let's verify whether the comment leader found is a substring
823 * of other comment leaders. If it is, let's adjust the
824 * lower_check_bound so that we make sure that we have determined
825 * the comment leader correctly.
826 */
827
Bram Moolenaar1c465442017-03-12 20:10:05 +0100828 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200829 ++com_leader;
830 len1 = (int)STRLEN(com_leader);
831
832 for (list = curbuf->b_p_com; *list; )
833 {
834 char_u *flags_save = list;
835
836 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
837 if (flags_save == com_flags)
838 continue;
839 string = vim_strchr(part_buf2, ':');
840 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100841 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200842 ++string;
843 len2 = (int)STRLEN(string);
844 if (len2 == 0)
845 continue;
846
847 /* Now we have to verify whether string ends with a substring
848 * beginning the com_leader. */
849 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
850 {
851 --off;
852 if (!STRNCMP(string + off, com_leader, len2 - off))
853 {
854 if (i - off < lower_check_bound)
855 lower_check_bound = i - off;
856 }
857 }
858 }
859 }
860 }
861 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864/*
865 * Return the number of window lines occupied by buffer line "lnum".
866 */
867 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100868plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 return plines_win(curwin, lnum, TRUE);
871}
872
873 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100874plines_win(
875 win_T *wp,
876 linenr_T lnum,
877 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879#if defined(FEAT_DIFF) || defined(PROTO)
880 /* Check for filler lines above this buffer line. When folded the result
881 * is one line anyway. */
882 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
883}
884
885 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100886plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888 return plines_win_nofill(curwin, lnum, TRUE);
889}
890
891 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100892plines_win_nofill(
893 win_T *wp,
894 linenr_T lnum,
895 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897#endif
898 int lines;
899
900 if (!wp->w_p_wrap)
901 return 1;
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (wp->w_width == 0)
904 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
906#ifdef FEAT_FOLDING
907 /* A folded lines is handled just like an empty line. */
908 /* NOTE: Caller must handle lines that are MAYBE folded. */
909 if (lineFolded(wp, lnum) == TRUE)
910 return 1;
911#endif
912
913 lines = plines_win_nofold(wp, lnum);
914 if (winheight > 0 && lines > wp->w_height)
915 return (int)wp->w_height;
916 return lines;
917}
918
919/*
920 * Return number of window lines physical line "lnum" will occupy in window
921 * "wp". Does not care about folding, 'wrap' or 'diff'.
922 */
923 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100924plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
926 char_u *s;
927 long col;
928 int width;
929
930 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
931 if (*s == NUL) /* empty line */
932 return 1;
933 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
934
935 /*
936 * If list mode is on, then the '$' at the end of the line may take up one
937 * extra column.
938 */
939 if (wp->w_p_list && lcs_eol != NUL)
940 col += 1;
941
942 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200943 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200945 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (width <= 0)
947 return 32000;
948 if (col <= width)
949 return 1;
950 col -= width;
951 width += win_col_off2(wp);
952 return (col + (width - 1)) / width + 1;
953}
954
955/*
956 * Like plines_win(), but only reports the number of physical screen lines
957 * used from the start of the line to the given column number.
958 */
959 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100960plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 long col;
963 char_u *s;
964 int lines = 0;
965 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200966 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968#ifdef FEAT_DIFF
969 /* Check for filler lines above this buffer line. When folded the result
970 * is one line anyway. */
971 lines = diff_check_fill(wp, lnum);
972#endif
973
974 if (!wp->w_p_wrap)
975 return lines + 1;
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (wp->w_width == 0)
978 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar597a4222014-06-25 14:39:50 +0200980 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 col = 0;
983 while (*s != NUL && --column >= 0)
984 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200985 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100986 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988
989 /*
990 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
991 * INSERT mode, then col must be adjusted so that it represents the last
992 * screen position of the TAB. This only fixes an error when the TAB wraps
993 * from one screen line to the next (when 'columns' is not a multiple of
994 * 'ts') -- webb.
995 */
996 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200997 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
999 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001000 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 */
Bram Moolenaar02631462017-09-22 15:20:32 +02001002 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00001003 if (width <= 0)
1004 return 9999;
1005
1006 lines += 1;
1007 if (col > width)
1008 lines += (col - width) / (width + win_col_off2(wp)) + 1;
1009 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011
1012 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001013plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
1015 int count = 0;
1016
1017 while (first <= last)
1018 {
1019#ifdef FEAT_FOLDING
1020 int x;
1021
1022 /* Check if there are any really folded lines, but also included lines
1023 * that are maybe folded. */
1024 x = foldedCount(wp, first, NULL);
1025 if (x > 0)
1026 {
1027 ++count; /* count 1 for "+-- folded" line */
1028 first += x;
1029 }
1030 else
1031#endif
1032 {
1033#ifdef FEAT_DIFF
1034 if (first == wp->w_topline)
1035 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
1036 else
1037#endif
1038 count += plines_win(wp, first, TRUE);
1039 ++first;
1040 }
1041 }
1042 return (count);
1043}
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001046gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001048 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001050 /* When searching columns is sometimes put at the end of a line. */
1051 if (pos->col == MAXCOL)
1052 return NUL;
1053 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (has_mbyte)
1055 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 return (int)*ptr;
1057}
1058
1059 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001060gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (has_mbyte)
1063 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 return (int)*ml_get_cursor();
1065}
1066
1067/*
1068 * Write a character at the current cursor position.
1069 * It is directly written into the block.
1070 */
1071 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001072pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073{
1074 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
1075 + curwin->w_cursor.col) = c;
1076}
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078/*
1079 * When extra == 0: Return TRUE if the cursor is before or on the first
1080 * non-blank in the line.
1081 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1082 * the line.
1083 */
1084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001085inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
1087 char_u *ptr;
1088 colnr_T col;
1089
Bram Moolenaar1c465442017-03-12 20:10:05 +01001090 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ++ptr;
1092 if (col >= curwin->w_cursor.col + extra)
1093 return TRUE;
1094 else
1095 return FALSE;
1096}
1097
1098/*
1099 * Skip to next part of an option argument: Skip space and comma.
1100 */
1101 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001102skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 if (*p == ',')
1105 ++p;
1106 while (*p == ' ')
1107 ++p;
1108 return p;
1109}
1110
1111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 * check_status: called when the status bars for the buffer 'buf'
1113 * need to be updated
1114 */
1115 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001116check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 win_T *wp;
1119
Bram Moolenaar29323592016-07-24 22:04:11 +02001120 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (wp->w_buffer == buf && wp->w_status_height)
1122 {
1123 wp->w_redr_status = TRUE;
1124 if (must_redraw < VALID)
1125 must_redraw = VALID;
1126 }
1127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 * Ask for a reply from the user, a 'y' or a 'n'.
1131 * No other characters are accepted, the message is repeated until a valid
1132 * reply is entered or CTRL-C is hit.
1133 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
1134 * from any buffers but directly from the user.
1135 *
1136 * return the 'y' or 'n'
1137 */
1138 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001139ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
1141 int r = ' ';
1142 int save_State = State;
1143
1144 if (exiting) /* put terminal in raw mode for this question */
1145 settmode(TMODE_RAW);
1146 ++no_wait_return;
1147#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001148 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001150 State = CONFIRM; // mouse behaves like with :confirm
1151 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001153 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 while (r != 'y' && r != 'n')
1156 {
1157 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001158 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (direct)
1160 r = get_keystroke();
1161 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00001162 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (r == Ctrl_C || r == ESC)
1164 r = 'n';
1165 msg_putchar(r); /* show what you typed */
1166 out_flush();
1167 }
1168 --no_wait_return;
1169 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 --no_mapping;
1172 --allow_keys;
1173
1174 return r;
1175}
1176
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001177#if defined(FEAT_EVAL) || defined(PROTO)
1178
1179/*
1180 * "mode()" function
1181 */
1182 void
1183f_mode(typval_T *argvars, typval_T *rettv)
1184{
1185 char_u buf[4];
1186
1187 vim_memset(buf, 0, sizeof(buf));
1188
1189 if (time_for_testing == 93784)
1190 {
1191 /* Testing the two-character code. */
1192 buf[0] = 'x';
1193 buf[1] = '!';
1194 }
1195#ifdef FEAT_TERMINAL
1196 else if (term_use_loop())
1197 buf[0] = 't';
1198#endif
1199 else if (VIsual_active)
1200 {
1201 if (VIsual_select)
1202 buf[0] = VIsual_mode + 's' - 'v';
1203 else
1204 buf[0] = VIsual_mode;
1205 }
1206 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
1207 || State == CONFIRM)
1208 {
1209 buf[0] = 'r';
1210 if (State == ASKMORE)
1211 buf[1] = 'm';
1212 else if (State == CONFIRM)
1213 buf[1] = '?';
1214 }
1215 else if (State == EXTERNCMD)
1216 buf[0] = '!';
1217 else if (State & INSERT)
1218 {
1219 if (State & VREPLACE_FLAG)
1220 {
1221 buf[0] = 'R';
1222 buf[1] = 'v';
1223 }
1224 else
1225 {
1226 if (State & REPLACE_FLAG)
1227 buf[0] = 'R';
1228 else
1229 buf[0] = 'i';
1230 if (ins_compl_active())
1231 buf[1] = 'c';
1232 else if (ctrl_x_mode_not_defined_yet())
1233 buf[1] = 'x';
1234 }
1235 }
1236 else if ((State & CMDLINE) || exmode_active)
1237 {
1238 buf[0] = 'c';
1239 if (exmode_active == EXMODE_VIM)
1240 buf[1] = 'v';
1241 else if (exmode_active == EXMODE_NORMAL)
1242 buf[1] = 'e';
1243 }
1244 else
1245 {
1246 buf[0] = 'n';
1247 if (finish_op)
1248 {
1249 buf[1] = 'o';
1250 // to be able to detect force-linewise/blockwise/characterwise operations
1251 buf[2] = motion_force;
1252 }
1253 else if (restart_edit == 'I' || restart_edit == 'R'
1254 || restart_edit == 'V')
1255 {
1256 buf[1] = 'i';
1257 buf[2] = restart_edit;
1258 }
1259 }
1260
1261 /* Clear out the minor mode when the argument is not a non-zero number or
1262 * non-empty string. */
1263 if (!non_zero_arg(&argvars[0]))
1264 buf[1] = NUL;
1265
1266 rettv->vval.v_string = vim_strsave(buf);
1267 rettv->v_type = VAR_STRING;
1268}
1269
1270 static void
1271may_add_state_char(garray_T *gap, char_u *include, int c)
1272{
1273 if (include == NULL || vim_strchr(include, c) != NULL)
1274 ga_append(gap, c);
1275}
1276
1277/*
1278 * "state()" function
1279 */
1280 void
1281f_state(typval_T *argvars, typval_T *rettv)
1282{
1283 garray_T ga;
1284 char_u *include = NULL;
1285 int i;
1286
1287 ga_init2(&ga, 1, 20);
1288 if (argvars[0].v_type != VAR_UNKNOWN)
1289 include = tv_get_string(&argvars[0]);
1290
1291 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
1292 may_add_state_char(&ga, include, 'm');
1293 if (op_pending())
1294 may_add_state_char(&ga, include, 'o');
1295 if (autocmd_busy)
1296 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +02001297 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001298 may_add_state_char(&ga, include, 'a');
1299
1300# ifdef FEAT_JOB_CHANNEL
1301 if (channel_in_blocking_wait())
1302 may_add_state_char(&ga, include, 'w');
1303# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001304 if (!get_was_safe_state())
1305 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001306 for (i = 0; i < get_callback_depth() && i < 3; ++i)
1307 may_add_state_char(&ga, include, 'c');
1308 if (msg_scrolled > 0)
1309 may_add_state_char(&ga, include, 's');
1310
1311 rettv->v_type = VAR_STRING;
1312 rettv->vval.v_string = ga.ga_data;
1313}
1314
1315#endif // FEAT_EVAL
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317/*
1318 * Get a key stroke directly from the user.
1319 * Ignores mouse clicks and scrollbar events, except a click for the left
1320 * button (used at the more prompt).
1321 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
1322 * Disadvantage: typeahead is ignored.
1323 * Translates the interrupt character for unix to ESC.
1324 */
1325 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001326get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001328 char_u *buf = NULL;
1329 int buflen = 150;
1330 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 int len = 0;
1332 int n;
1333 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001334 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 mapped_ctrl_c = FALSE; /* mappings are not used here */
1337 for (;;)
1338 {
1339 cursor_on();
1340 out_flush();
1341
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001342 /* Leave some room for check_termcode() to insert a key code into (max
1343 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
1344 * bytes. */
1345 maxlen = (buflen - 6 - len) / 3;
1346 if (buf == NULL)
1347 buf = alloc(buflen);
1348 else if (maxlen < 10)
1349 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001350 char_u *t_buf = buf;
1351
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001352 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001353 * escape sequence. */
1354 buflen += 100;
1355 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001356 if (buf == NULL)
1357 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001358 maxlen = (buflen - 6 - len) / 3;
1359 }
1360 if (buf == NULL)
1361 {
1362 do_outofmem_msg((long_u)buflen);
1363 return ESC; /* panic! */
1364 }
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001367 * terminal code to complete. */
1368 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (n > 0)
1370 {
1371 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02001372 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001374 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00001376 else if (len > 0)
1377 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaar4395a712006-09-05 18:57:57 +00001379 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001380 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00001381 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001383
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001384 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001385 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001386 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001387 {
1388 /* Redrawing was postponed, do it now. */
1389 update_screen(0);
1390 setcursor(); /* put cursor back where it belongs */
1391 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001392 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001393 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001394 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001396 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 continue;
1398
1399 /* Handle modifier and/or special key code. */
1400 n = buf[0];
1401 if (n == K_SPECIAL)
1402 {
1403 n = TO_SPECIAL(buf[1], buf[2]);
1404 if (buf[1] == KS_MODIFIER
1405 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001406#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001407 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001408#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001409#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 || n == K_VER_SCROLLBAR
1411 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#endif
1413 )
1414 {
1415 if (buf[1] == KS_MODIFIER)
1416 mod_mask = buf[2];
1417 len -= 3;
1418 if (len > 0)
1419 mch_memmove(buf, buf + 3, (size_t)len);
1420 continue;
1421 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00001422 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (has_mbyte)
1425 {
1426 if (MB_BYTE2LEN(n) > len)
1427 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001428 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 n = (*mb_ptr2char)(buf);
1430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431#ifdef UNIX
1432 if (n == intr_char)
1433 n = ESC;
1434#endif
1435 break;
1436 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001437 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 mapped_ctrl_c = save_mapped_ctrl_c;
1440 return n;
1441}
1442
1443/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001444 * Get a number from the user.
1445 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 */
1447 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001448get_number(
1449 int colon, /* allow colon to abort */
1450 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 int n = 0;
1453 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001454 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001456 if (mouse_used != NULL)
1457 *mouse_used = FALSE;
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 /* When not printing messages, the user won't know what to type, return a
1460 * zero (as if CR was hit). */
1461 if (msg_silent != 0)
1462 return 0;
1463
1464#ifdef USE_ON_FLY_SCROLL
1465 dont_scroll = TRUE; /* disallow scrolling here */
1466#endif
1467 ++no_mapping;
1468 ++allow_keys; /* no mapping here, but recognize keys */
1469 for (;;)
1470 {
1471 windgoto(msg_row, msg_col);
1472 c = safe_vgetc();
1473 if (VIM_ISDIGIT(c))
1474 {
1475 n = n * 10 + c - '0';
1476 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001477 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
1479 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1480 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001481 if (typed > 0)
1482 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001483 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001484 --typed;
1485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001488#ifdef FEAT_MOUSE
1489 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1490 {
1491 *mouse_used = TRUE;
1492 n = mouse_row + 1;
1493 break;
1494 }
1495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 else if (n == 0 && c == ':' && colon)
1497 {
1498 stuffcharReadbuff(':');
1499 if (!exmode_active)
1500 cmdline_row = msg_row;
1501 skip_redraw = TRUE; /* skip redraw once */
1502 do_redraw = FALSE;
1503 break;
1504 }
1505 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
1506 break;
1507 }
1508 --no_mapping;
1509 --allow_keys;
1510 return n;
1511}
1512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001513/*
1514 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001515 * When "mouse_used" is not NULL allow using the mouse and in that case return
1516 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001517 */
1518 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001519prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001520{
1521 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001522 int save_cmdline_row;
1523 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001524
1525 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001526 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001527 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001528 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001529 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001530
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001531 // Set the state such that text can be selected/copied/pasted and we still
1532 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1533 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001534 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001535 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001536 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001537 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001538 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001539 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001540
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541 i = get_number(TRUE, mouse_used);
1542 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001544 // don't call wait_return() now
1545 if (msg_row > 0)
1546 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001547 need_wait_return = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001548 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001549 else
1550 cmdline_row = save_cmdline_row;
1551 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001552 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001553 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001555 return i;
1556}
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001559msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 long pn;
1562
1563 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1565 return;
1566
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001567 /* We don't want to overwrite another important message, but do overwrite
1568 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1569 * then "put" reports the last action. */
1570 if (keep_msg != NULL && !keep_msg_more)
1571 return;
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (n > 0)
1574 pn = n;
1575 else
1576 pn = -n;
1577
1578 if (pn > p_report)
1579 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001580 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001581 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001582 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001584 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001585 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001587 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1588 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (msg(msg_buf))
1590 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001591 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001592 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594 }
1595}
1596
1597/*
1598 * flush map and typeahead buffers and give a warning for an error
1599 */
1600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001601beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 if (emsg_silent == 0)
1604 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001605 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001606 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608}
1609
1610/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001611 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
1613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001614vim_beep(
1615 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001617#ifdef FEAT_EVAL
1618 called_vim_beep = TRUE;
1619#endif
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (emsg_silent == 0)
1622 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001623 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1624 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001625#ifdef ELAPSED_FUNC
1626 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001627 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001628
1629 /* Only beep once per half a second, otherwise a sequence of beeps
1630 * would freeze Vim. */
1631 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1632 {
1633 did_init = TRUE;
1634 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001636 if (p_vb
1637#ifdef FEAT_GUI
1638 /* While the GUI is starting up the termcap is set for
1639 * the GUI but the output still goes to a terminal. */
1640 && !(gui.in_use && gui.starting)
1641#endif
1642 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001643 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001644 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001645#ifdef FEAT_VTP
1646 /* No restore color information, refresh the screen. */
1647 if (has_vtp_working() != 0
1648# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001649 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001650# endif
1651 )
1652 {
1653 redraw_later(CLEAR);
1654 update_screen(0);
1655 redrawcmd();
1656 }
1657#endif
1658 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001659 else
1660 out_char(BELL);
1661#ifdef ELAPSED_FUNC
1662 }
1663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001665
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001666 /* When 'debug' contains "beep" produce a message. If we are sourcing
1667 * a script or executing a function give the user a hint where the beep
1668 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001669 if (vim_strchr(p_debug, 'e') != NULL)
1670 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001671 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001672 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
1675}
1676
1677/*
1678 * To get the "real" home directory:
1679 * - get value of $HOME
1680 * For Unix:
1681 * - go to that directory
1682 * - do mch_dirname() to get the real name of that directory.
1683 * This also works with mounts and links.
1684 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001685 * For Windows:
1686 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001689init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 char_u *var;
1692
Bram Moolenaar05159a02005-02-26 23:04:13 +00001693 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001694 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#ifdef VMS
1697 var = mch_getenv((char_u *)"SYS$LOGIN");
1698#else
1699 var = mch_getenv((char_u *)"HOME");
1700#endif
1701
Bram Moolenaar4f974752019-02-17 17:44:42 +01001702#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001704 * Typically, $HOME is not defined on Windows, unless the user has
1705 * specifically defined it for Vim's sake. However, on Windows NT
1706 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1707 * each user. Try constructing $HOME from these.
1708 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001709 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001710 {
1711 char_u *homedrive, *homepath;
1712
1713 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1714 homepath = mch_getenv((char_u *)"HOMEPATH");
1715 if (homepath == NULL || *homepath == NUL)
1716 homepath = (char_u *)"\\";
1717 if (homedrive != NULL
1718 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1719 {
1720 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1721 if (NameBuff[0] != NUL)
1722 var = NameBuff;
1723 }
1724 }
1725
1726 if (var == NULL)
1727 var = mch_getenv((char_u *)"USERPROFILE");
1728
1729 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 * Weird but true: $HOME may contain an indirect reference to another
1731 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1732 * when $HOME is being set.
1733 */
1734 if (var != NULL && *var == '%')
1735 {
1736 char_u *p;
1737 char_u *exp;
1738
1739 p = vim_strchr(var + 1, '%');
1740 if (p != NULL)
1741 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001742 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 exp = mch_getenv(NameBuff);
1744 if (exp != NULL && *exp != NUL
1745 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1746 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001747 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 }
1751 }
1752
Bram Moolenaar48340b62017-08-29 22:08:53 +02001753 if (var != NULL && *var == NUL) /* empty is same as not set */
1754 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar05159a02005-02-26 23:04:13 +00001756 if (enc_utf8 && var != NULL)
1757 {
1758 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001759 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001760
1761 /* Convert from active codepage to UTF-8. Other conversions are
1762 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001763 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001764 if (pp != NULL)
1765 {
1766 homedir = pp;
1767 return;
1768 }
1769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 /*
1772 * Default home dir is C:/
1773 * Best assumption we can make in such a situation.
1774 */
1775 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001776 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (var != NULL)
1780 {
1781#ifdef UNIX
1782 /*
1783 * Change to the directory and get the actual path. This resolves
1784 * links. Don't do it when we can't return.
1785 */
1786 if (mch_dirname(NameBuff, MAXPATHL) == OK
1787 && mch_chdir((char *)NameBuff) == 0)
1788 {
1789 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1790 var = IObuff;
1791 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001792 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
1794#endif
1795 homedir = vim_strsave(var);
1796 }
1797}
1798
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001799#if defined(EXITFREE) || defined(PROTO)
1800 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001801free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001802{
1803 vim_free(homedir);
1804}
Bram Moolenaar24305862012-08-15 14:05:05 +02001805
Bram Moolenaar24305862012-08-15 14:05:05 +02001806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001807free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001808{
1809 ga_clear_strings(&ga_users);
1810}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001811#endif
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001814 * Call expand_env() and store the result in an allocated string.
1815 * This is not very memory efficient, this expects the result to be freed
1816 * again soon.
1817 */
1818 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001819expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001820{
1821 return expand_env_save_opt(src, FALSE);
1822}
1823
1824/*
1825 * Idem, but when "one" is TRUE handle the string as one file name, only
1826 * expand "~" at the start.
1827 */
1828 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001829expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001830{
1831 char_u *p;
1832
1833 p = alloc(MAXPATHL);
1834 if (p != NULL)
1835 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1836 return p;
1837}
1838
1839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 * Expand environment variable with path name.
1841 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001842 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * If anything fails no expansion is done and dst equals src.
1844 */
1845 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001846expand_env(
1847 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1848 char_u *dst, /* where to put the result */
1849 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001851 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852}
1853
1854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001855expand_env_esc(
1856 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1857 char_u *dst, /* where to put the result */
1858 int dstlen, /* maximum length of the result */
1859 int esc, /* escape spaces in expanded variables */
1860 int one, /* "srcp" is one file name */
1861 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001863 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 char_u *tail;
1865 int c;
1866 char_u *var;
1867 int copy_char;
1868 int mustfree; /* var was allocated, need to free it later */
1869 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001870 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001872 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001873 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001874
1875 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 --dstlen; /* leave one char space for "\," */
1877 while (*src && dstlen > 0)
1878 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001879#ifdef FEAT_EVAL
1880 /* Skip over `=expr`. */
1881 if (src[0] == '`' && src[1] == '=')
1882 {
1883 size_t len;
1884
1885 var = src;
1886 src += 2;
1887 (void)skip_expr(&src);
1888 if (*src == '`')
1889 ++src;
1890 len = src - var;
1891 if (len > (size_t)dstlen)
1892 len = dstlen;
1893 vim_strncpy(dst, var, len);
1894 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001895 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001896 continue;
1897 }
1898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001900 if ((*src == '$'
1901#ifdef VMS
1902 && at_start
1903#endif
1904 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001905#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 || *src == '%'
1907#endif
1908 || (*src == '~' && at_start))
1909 {
1910 mustfree = FALSE;
1911
1912 /*
1913 * The variable name is copied into dst temporarily, because it may
1914 * be a string in read-only memory and a NUL needs to be appended.
1915 */
1916 if (*src != '~') /* environment var */
1917 {
1918 tail = src + 1;
1919 var = dst;
1920 c = dstlen - 1;
1921
1922#ifdef UNIX
1923 /* Unix has ${var-name} type environment vars */
1924 if (*tail == '{' && !vim_isIDc('{'))
1925 {
1926 tail++; /* ignore '{' */
1927 while (c-- > 0 && *tail && *tail != '}')
1928 *var++ = *tail++;
1929 }
1930 else
1931#endif
1932 {
1933 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001934#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 || (*src == '%' && *tail != '%')
1936#endif
1937 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001941#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942# ifdef UNIX
1943 if (src[1] == '{' && *tail != '}')
1944# else
1945 if (*src == '%' && *tail != '%')
1946# endif
1947 var = NULL;
1948 else
1949 {
1950# ifdef UNIX
1951 if (src[1] == '{')
1952# else
1953 if (*src == '%')
1954#endif
1955 ++tail;
1956#endif
1957 *var = NUL;
1958 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001959#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961#endif
1962 }
1963 /* home directory */
1964 else if ( src[1] == NUL
1965 || vim_ispathsep(src[1])
1966 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1967 {
1968 var = homedir;
1969 tail = src + 1;
1970 }
1971 else /* user directory */
1972 {
1973#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1974 /*
1975 * Copy ~user to dst[], so we can put a NUL after it.
1976 */
1977 tail = src;
1978 var = dst;
1979 c = dstlen - 1;
1980 while ( c-- > 0
1981 && *tail
1982 && vim_isfilec(*tail)
1983 && !vim_ispathsep(*tail))
1984 *var++ = *tail++;
1985 *var = NUL;
1986# ifdef UNIX
1987 /*
1988 * If the system supports getpwnam(), use it.
1989 * Otherwise, or if getpwnam() fails, the shell is used to
1990 * expand ~user. This is slower and may fail if the shell
1991 * does not support ~user (old versions of /bin/sh).
1992 */
1993# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1994 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001995 /* Note: memory allocated by getpwnam() is never freed.
1996 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001997 struct passwd *pw = (*dst == NUL)
1998 ? NULL : getpwnam((char *)dst + 1);
1999
2000 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 if (var == NULL)
2003# endif
2004 {
2005 expand_T xpc;
2006
2007 ExpandInit(&xpc);
2008 xpc.xp_context = EXPAND_FILES;
2009 var = ExpandOne(&xpc, dst, NULL,
2010 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 mustfree = TRUE;
2012 }
2013
2014# else /* !UNIX, thus VMS */
2015 /*
2016 * USER_HOME is a comma-separated list of
2017 * directories to search for the user account in.
2018 */
2019 {
2020 char_u test[MAXPATHL], paths[MAXPATHL];
2021 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02002022 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 STRCPY(paths, USER_HOME);
2025 next_path = paths;
2026 while (*next_path)
2027 {
2028 for (path = next_path; *next_path && *next_path != ',';
2029 next_path++);
2030 if (*next_path)
2031 *next_path++ = NUL;
2032 STRCPY(test, path);
2033 STRCAT(test, "/");
2034 STRCAT(test, dst + 1);
2035 if (mch_stat(test, &st) == 0)
2036 {
2037 var = alloc(STRLEN(test) + 1);
2038 STRCPY(var, test);
2039 mustfree = TRUE;
2040 break;
2041 }
2042 }
2043 }
2044# endif /* UNIX */
2045#else
2046 /* cannot expand user's home directory, so don't try */
2047 var = NULL;
2048 tail = (char_u *)""; /* for gcc */
2049#endif /* UNIX || VMS */
2050 }
2051
2052#ifdef BACKSLASH_IN_FILENAME
2053 /* If 'shellslash' is set change backslashes to forward slashes.
2054 * Can't use slash_adjust(), p_ssl may be set temporarily. */
2055 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
2056 {
2057 char_u *p = vim_strsave(var);
2058
2059 if (p != NULL)
2060 {
2061 if (mustfree)
2062 vim_free(var);
2063 var = p;
2064 mustfree = TRUE;
2065 forward_slash(var);
2066 }
2067 }
2068#endif
2069
2070 /* If "var" contains white space, escape it with a backslash.
2071 * Required for ":e ~/tt" when $HOME includes a space. */
2072 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
2073 {
2074 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
2075
2076 if (p != NULL)
2077 {
2078 if (mustfree)
2079 vim_free(var);
2080 var = p;
2081 mustfree = TRUE;
2082 }
2083 }
2084
2085 if (var != NULL && *var != NUL
2086 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
2087 {
2088 STRCPY(dst, var);
2089 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002090 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 /* if var[] ends in a path separator and tail[] starts
2092 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002093 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
2095 && dst[-1] != ':'
2096#endif
2097 && vim_ispathsep(*tail))
2098 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002099 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 src = tail;
2101 copy_char = FALSE;
2102 }
2103 if (mustfree)
2104 vim_free(var);
2105 }
2106
2107 if (copy_char) /* copy at least one char */
2108 {
2109 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00002110 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002111 * Don't do this when "one" is TRUE, to avoid expanding "~" in
2112 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 */
2114 at_start = FALSE;
2115 if (src[0] == '\\' && src[1] != NUL)
2116 {
2117 *dst++ = *src++;
2118 --dstlen;
2119 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002120 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02002122 if (dstlen > 0)
2123 {
2124 *dst++ = *src++;
2125 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002126
Bram Moolenaar1c864092017-08-06 18:15:45 +02002127 if (startstr != NULL && src - startstr_len >= srcp
2128 && STRNCMP(src - startstr_len, startstr,
2129 startstr_len) == 0)
2130 at_start = TRUE;
2131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02002133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 *dst = NUL;
2136}
2137
2138/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002139 * If the string between "p" and "pend" ends in "name/", return "pend" minus
2140 * the length of "name/". Otherwise return "pend".
2141 */
2142 static char_u *
2143remove_tail(char_u *p, char_u *pend, char_u *name)
2144{
2145 int len = (int)STRLEN(name) + 1;
2146 char_u *newend = pend - len;
2147
2148 if (newend >= p
2149 && fnamencmp(newend, name, len - 1) == 0
2150 && (newend == p || after_pathsep(p, newend)))
2151 return newend;
2152 return pend;
2153}
2154
2155/*
2156 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
2157 * Return NULL if not, return its name in allocated memory otherwise.
2158 */
2159 static char_u *
2160vim_version_dir(char_u *vimdir)
2161{
2162 char_u *p;
2163
2164 if (vimdir == NULL || *vimdir == NUL)
2165 return NULL;
2166 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
2167 if (p != NULL && mch_isdir(p))
2168 return p;
2169 vim_free(p);
2170 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
2171 if (p != NULL && mch_isdir(p))
2172 return p;
2173 vim_free(p);
2174 return NULL;
2175}
2176
2177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 * Vim's version of getenv().
2179 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00002180 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02002181 * "mustfree" is set to TRUE when returned is allocated, it must be
2182 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 */
2184 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002185vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002187 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 char_u *pend;
2189 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002190#ifdef MSWIN
2191 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002193 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (STRCMP(name, "HOME") == 0)
2195 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002197 // Use Wide function
2198 wn = enc_to_utf16(name, NULL);
2199 if (wn == NULL)
2200 return NULL;
2201
2202 wp = _wgetenv(wn);
2203 vim_free(wn);
2204
2205 if (wp != NULL && *wp == NUL) // empty is the same as not set
2206 wp = NULL;
2207
2208 if (wp != NULL)
2209 {
2210 p = utf16_to_enc(wp, NULL);
2211 if (p == NULL)
2212 return NULL;
2213
2214 *mustfree = TRUE;
2215 return p;
2216 }
2217#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002219 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 p = NULL;
2221
2222 if (p != NULL)
2223 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002226 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
2228 if (!vimruntime && STRCMP(name, "VIM") != 0)
2229 return NULL;
2230
2231 /*
2232 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
2233 * Don't do this when default_vimruntime_dir is non-empty.
2234 */
2235 if (vimruntime
2236#ifdef HAVE_PATHDEF
2237 && *default_vimruntime_dir == NUL
2238#endif
2239 )
2240 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002241#ifdef MSWIN
2242 // Use Wide function
2243 wp = _wgetenv(L"VIM");
2244 if (wp != NULL && *wp == NUL) // empty is the same as not set
2245 wp = NULL;
2246 if (wp != NULL)
2247 {
2248 char_u *q = utf16_to_enc(wp, NULL);
2249 if (q != NULL)
2250 {
2251 p = vim_version_dir(q);
2252 *mustfree = TRUE;
2253 if (p == NULL)
2254 p = q;
2255 }
2256 }
2257#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002259 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 p = NULL;
2261 if (p != NULL)
2262 {
2263 p = vim_version_dir(p);
2264 if (p != NULL)
2265 *mustfree = TRUE;
2266 else
2267 p = mch_getenv((char_u *)"VIM");
2268 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271
2272 /*
2273 * When expanding $VIM or $VIMRUNTIME fails, try using:
2274 * - the directory name from 'helpfile' (unless it contains '$')
2275 * - the executable name from argv[0]
2276 */
2277 if (p == NULL)
2278 {
2279 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
2280 p = p_hf;
2281#ifdef USE_EXE_NAME
2282 /*
2283 * Use the name of the executable, obtained from argv[0].
2284 */
2285 else
2286 p = exe_name;
2287#endif
2288 if (p != NULL)
2289 {
2290 /* remove the file name */
2291 pend = gettail(p);
2292
2293 /* remove "doc/" from 'helpfile', if present */
2294 if (p == p_hf)
2295 pend = remove_tail(p, pend, (char_u *)"doc");
2296
2297#ifdef USE_EXE_NAME
2298# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002299 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (p == exe_name)
2301 {
2302 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002303 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002305 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
2306 if (pend1 != pend)
2307 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002308 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002309 if (pnew != NULL)
2310 {
2311 STRNCPY(pnew, p, (pend1 - p));
2312 STRCPY(pnew + (pend1 - p), "Resources/vim");
2313 p = pnew;
2314 pend = p + STRLEN(p);
2315 }
2316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318# endif
2319 /* remove "src/" from exe_name, if present */
2320 if (p == exe_name)
2321 pend = remove_tail(p, pend, (char_u *)"src");
2322#endif
2323
2324 /* for $VIM, remove "runtime/" or "vim54/", if present */
2325 if (!vimruntime)
2326 {
2327 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
2328 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
2329 }
2330
2331 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002332 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002335#ifdef MACOS_X
2336 if (p == exe_name || p == p_hf)
2337#endif
2338 /* check that the result is a directory name */
2339 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
2341 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01002342 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 else
2344 {
2345#ifdef USE_EXE_NAME
2346 /* may add "/vim54" or "/runtime" if it exists */
2347 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
2348 {
2349 vim_free(p);
2350 p = pend;
2351 }
2352#endif
2353 *mustfree = TRUE;
2354 }
2355 }
2356 }
2357
2358#ifdef HAVE_PATHDEF
2359 /* When there is a pathdef.c file we can use default_vim_dir and
2360 * default_vimruntime_dir */
2361 if (p == NULL)
2362 {
2363 /* Only use default_vimruntime_dir when it is not empty */
2364 if (vimruntime && *default_vimruntime_dir != NUL)
2365 {
2366 p = default_vimruntime_dir;
2367 *mustfree = FALSE;
2368 }
2369 else if (*default_vim_dir != NUL)
2370 {
2371 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
2372 *mustfree = TRUE;
2373 else
2374 {
2375 p = default_vim_dir;
2376 *mustfree = FALSE;
2377 }
2378 }
2379 }
2380#endif
2381
2382 /*
2383 * Set the environment variable, so that the new value can be found fast
2384 * next time, and others can also use it (e.g. Perl).
2385 */
2386 if (p != NULL)
2387 {
2388 if (vimruntime)
2389 {
2390 vim_setenv((char_u *)"VIMRUNTIME", p);
2391 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393 else
2394 {
2395 vim_setenv((char_u *)"VIM", p);
2396 didset_vim = TRUE;
2397 }
2398 }
2399 return p;
2400}
2401
Bram Moolenaar113e1072019-01-20 15:30:40 +01002402#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02002403 void
2404vim_unsetenv(char_u *var)
2405{
2406#ifdef HAVE_UNSETENV
2407 unsetenv((char *)var);
2408#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02002409 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02002410#endif
2411}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002412#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02002413
2414
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 * Our portable version of setenv.
2417 */
2418 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002419vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421#ifdef HAVE_SETENV
2422 mch_setenv((char *)name, (char *)val, 1);
2423#else
2424 char_u *envbuf;
2425
2426 /*
2427 * Putenv does not copy the string, it has to remain
2428 * valid. The allocated memory will never be freed.
2429 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002430 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 if (envbuf != NULL)
2432 {
2433 sprintf((char *)envbuf, "%s=%s", name, val);
2434 putenv((char *)envbuf);
2435 }
2436#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002437#ifdef FEAT_GETTEXT
2438 /*
2439 * When setting $VIMRUNTIME adjust the directory to find message
2440 * translations to $VIMRUNTIME/lang.
2441 */
2442 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2443 {
2444 char_u *buf = concat_str(val, (char_u *)"/lang");
2445
2446 if (buf != NULL)
2447 {
2448 bindtextdomain(VIMPACKAGE, (char *)buf);
2449 vim_free(buf);
2450 }
2451 }
2452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453}
2454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455/*
2456 * Function given to ExpandGeneric() to obtain an environment variable name.
2457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002459get_env_name(
2460 expand_T *xp UNUSED,
2461 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
Bram Moolenaard0573012017-10-28 21:11:06 +02002463# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002465 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 */
2467 return NULL;
2468# else
2469# ifndef __WIN32__
2470 /* Borland C++ 5.2 has this in a header file. */
2471 extern char **environ;
2472# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002473# define ENVNAMELEN 100
2474 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 char_u *str;
2476 int n;
2477
2478 str = (char_u *)environ[idx];
2479 if (str == NULL)
2480 return NULL;
2481
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002482 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
2484 if (str[n] == '=' || str[n] == NUL)
2485 break;
2486 name[n] = str[n];
2487 }
2488 name[n] = NUL;
2489 return name;
2490# endif
2491}
Bram Moolenaar24305862012-08-15 14:05:05 +02002492
2493/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002494 * Add a user name to the list of users in ga_users.
2495 * Do nothing if user name is NULL or empty.
2496 */
2497 static void
2498add_user(char_u *user, int need_copy)
2499{
2500 char_u *user_copy = (user != NULL && need_copy)
2501 ? vim_strsave(user) : user;
2502
2503 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2504 {
2505 if (need_copy)
2506 vim_free(user);
2507 return;
2508 }
2509 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2510}
2511
2512/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002513 * Find all user names for user completion.
2514 * Done only once and then cached.
2515 */
2516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002517init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002518{
Bram Moolenaar24305862012-08-15 14:05:05 +02002519 static int lazy_init_done = FALSE;
2520
2521 if (lazy_init_done)
2522 return;
2523
2524 lazy_init_done = TRUE;
2525 ga_init2(&ga_users, sizeof(char_u *), 20);
2526
2527# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2528 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002529 struct passwd* pw;
2530
2531 setpwent();
2532 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002533 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002534 endpwent();
2535 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002536# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002537 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002538 DWORD nusers = 0, ntotal = 0, i;
2539 PUSER_INFO_0 uinfo;
2540
2541 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2542 &nusers, &ntotal, NULL) == NERR_Success)
2543 {
2544 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002545 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002546
2547 NetApiBufferFree(uinfo);
2548 }
2549 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002550# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002551# if defined(HAVE_GETPWNAM)
2552 {
2553 char_u *user_env = mch_getenv((char_u *)"USER");
2554
2555 // The $USER environment variable may be a valid remote user name (NIS,
2556 // LDAP) not already listed by getpwent(), as getpwent() only lists
2557 // local user names. If $USER is not already listed, check whether it
2558 // is a valid remote user name using getpwnam() and if it is, add it to
2559 // the list of user names.
2560
2561 if (user_env != NULL && *user_env != NUL)
2562 {
2563 int i;
2564
2565 for (i = 0; i < ga_users.ga_len; i++)
2566 {
2567 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2568
2569 if (STRCMP(local_user, user_env) == 0)
2570 break;
2571 }
2572
2573 if (i == ga_users.ga_len)
2574 {
2575 struct passwd *pw = getpwnam((char *)user_env);
2576
2577 if (pw != NULL)
2578 add_user((char_u *)pw->pw_name, TRUE);
2579 }
2580 }
2581 }
2582# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002583}
2584
2585/*
2586 * Function given to ExpandGeneric() to obtain an user names.
2587 */
2588 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002589get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002590{
2591 init_users();
2592 if (idx < ga_users.ga_len)
2593 return ((char_u **)ga_users.ga_data)[idx];
2594 return NULL;
2595}
2596
2597/*
2598 * Check whether name matches a user name. Return:
2599 * 0 if name does not match any user name.
2600 * 1 if name partially matches the beginning of a user name.
2601 * 2 is name fully matches a user name.
2602 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002603 int
2604match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002605{
2606 int i;
2607 int n = (int)STRLEN(name);
2608 int result = 0;
2609
2610 init_users();
2611 for (i = 0; i < ga_users.ga_len; i++)
2612 {
2613 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2614 return 2; /* full match */
2615 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2616 result = 1; /* partial match */
2617 }
2618 return result;
2619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002622 * Concatenate two strings and return the result in allocated memory.
2623 * Returns NULL when out of memory.
2624 */
2625 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002626concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002627{
2628 char_u *dest;
2629 size_t l = STRLEN(str1);
2630
Bram Moolenaar964b3742019-05-24 18:54:09 +02002631 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002632 if (dest != NULL)
2633 {
2634 STRCPY(dest, str1);
2635 STRCPY(dest + l, str2);
2636 }
2637 return dest;
2638}
Bram Moolenaard6754642005-01-17 22:18:45 +00002639
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002640 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002641prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002643#if defined(SIGHUP) && defined(SIG_IGN)
2644 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2645 * makes Vim exit and then handling SIGHUP causes various reentrance
2646 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002647 signal(SIGHUP, SIG_IGN);
2648#endif
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#ifdef FEAT_GUI
2651 if (gui.in_use)
2652 {
2653 gui.dying = TRUE;
2654 out_trash(); /* trash any pending output */
2655 }
2656 else
2657#endif
2658 {
2659 windgoto((int)Rows - 1, 0);
2660
2661 /*
2662 * Switch terminal mode back now, so messages end up on the "normal"
2663 * screen (if there are two screens).
2664 */
2665 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002666 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 out_flush();
2668 }
2669}
2670
2671/*
2672 * Preserve files and exit.
2673 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002674 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2675 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
2677 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002678preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 buf_T *buf;
2681
2682 prepare_to_exit();
2683
Bram Moolenaar4770d092006-01-12 23:22:24 +00002684 /* Setting this will prevent free() calls. That avoids calling free()
2685 * recursively when free() was invoked with a bad pointer. */
2686 really_exiting = TRUE;
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 out_str(IObuff);
2689 screen_start(); /* don't know where cursor is now */
2690 out_flush();
2691
2692 ml_close_notmod(); /* close all not-modified buffers */
2693
Bram Moolenaar29323592016-07-24 22:04:11 +02002694 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
2696 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2697 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002698 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 screen_start(); /* don't know where cursor is now */
2700 out_flush();
2701 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2702 break;
2703 }
2704 }
2705
2706 ml_close_all(FALSE); /* close all memfiles, without deleting */
2707
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002708 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 getout(1);
2711}
2712
2713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 * Check for CTRL-C pressed, but only once in a while.
2715 * Should be used instead of ui_breakcheck() for functions that check for
2716 * each line in the file. Calling ui_breakcheck() each time takes too much
2717 * time, because it can be a system call.
2718 */
2719
2720#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002721# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#endif
2723
2724static int breakcheck_count = 0;
2725
2726 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002727line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 if (++breakcheck_count >= BREAKCHECK_SKIP)
2730 {
2731 breakcheck_count = 0;
2732 ui_breakcheck();
2733 }
2734}
2735
2736/*
2737 * Like line_breakcheck() but check 10 times less often.
2738 */
2739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002740fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2743 {
2744 breakcheck_count = 0;
2745 ui_breakcheck();
2746 }
2747}
2748
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002749#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2750 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2751 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753#ifndef SEEK_SET
2754# define SEEK_SET 0
2755#endif
2756#ifndef SEEK_END
2757# define SEEK_END 2
2758#endif
2759
2760/*
2761 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002762 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2763 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * Returns an allocated string, or NULL for error.
2765 */
2766 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002767get_cmd_output(
2768 char_u *cmd,
2769 char_u *infile, /* optional input file name */
2770 int flags, /* can be SHELL_SILENT */
2771 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
2773 char_u *tempname;
2774 char_u *command;
2775 char_u *buffer = NULL;
2776 int len;
2777 int i = 0;
2778 FILE *fd;
2779
2780 if (check_restricted() || check_secure())
2781 return NULL;
2782
2783 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002784 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002786 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 return NULL;
2788 }
2789
2790 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002791 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (command == NULL)
2793 goto done;
2794
2795 /*
2796 * Call the shell to execute the command (errors are ignored).
2797 * Don't check timestamps here.
2798 */
2799 ++no_check_timestamps;
2800 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2801 --no_check_timestamps;
2802
2803 vim_free(command);
2804
2805 /*
2806 * read the names from the file into memory
2807 */
2808# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00002809 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 fd = mch_fopen((char *)tempname, "r");
2811# else
2812 fd = mch_fopen((char *)tempname, READBIN);
2813# endif
2814
2815 if (fd == NULL)
2816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002817 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 goto done;
2819 }
2820
2821 fseek(fd, 0L, SEEK_END);
2822 len = ftell(fd); /* get size of temp file */
2823 fseek(fd, 0L, SEEK_SET);
2824
2825 buffer = alloc(len + 1);
2826 if (buffer != NULL)
2827 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2828 fclose(fd);
2829 mch_remove(tempname);
2830 if (buffer == NULL)
2831 goto done;
2832#ifdef VMS
2833 len = i; /* VMS doesn't give us what we asked for... */
2834#endif
2835 if (i != len)
2836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002837 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002838 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002840 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002841 {
2842 /* Change NUL into SOH, otherwise the string is truncated. */
2843 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002844 if (buffer[i] == NUL)
2845 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002846
Bram Moolenaar162bd912010-07-28 22:29:10 +02002847 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002848 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002849 else
2850 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
2852done:
2853 vim_free(tempname);
2854 return buffer;
2855}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002856
2857# if defined(FEAT_EVAL) || defined(PROTO)
2858
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002859 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002860get_cmd_output_as_rettv(
2861 typval_T *argvars,
2862 typval_T *rettv,
2863 int retlist)
2864{
2865 char_u *res = NULL;
2866 char_u *p;
2867 char_u *infile = NULL;
2868 int err = FALSE;
2869 FILE *fd;
2870 list_T *list = NULL;
2871 int flags = SHELL_SILENT;
2872
2873 rettv->v_type = VAR_STRING;
2874 rettv->vval.v_string = NULL;
2875 if (check_restricted() || check_secure())
2876 goto errret;
2877
2878 if (argvars[1].v_type != VAR_UNKNOWN)
2879 {
2880 /*
2881 * Write the text to a temp file, to be used for input of the shell
2882 * command.
2883 */
2884 if ((infile = vim_tempname('i', TRUE)) == NULL)
2885 {
2886 emsg(_(e_notmp));
2887 goto errret;
2888 }
2889
2890 fd = mch_fopen((char *)infile, WRITEBIN);
2891 if (fd == NULL)
2892 {
2893 semsg(_(e_notopen), infile);
2894 goto errret;
2895 }
2896 if (argvars[1].v_type == VAR_NUMBER)
2897 {
2898 linenr_T lnum;
2899 buf_T *buf;
2900
2901 buf = buflist_findnr(argvars[1].vval.v_number);
2902 if (buf == NULL)
2903 {
2904 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2905 fclose(fd);
2906 goto errret;
2907 }
2908
2909 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2910 {
2911 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2912 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2913 {
2914 err = TRUE;
2915 break;
2916 }
2917 if (putc(NL, fd) == EOF)
2918 {
2919 err = TRUE;
2920 break;
2921 }
2922 }
2923 }
2924 else if (argvars[1].v_type == VAR_LIST)
2925 {
2926 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2927 err = TRUE;
2928 }
2929 else
2930 {
2931 size_t len;
2932 char_u buf[NUMBUFLEN];
2933
2934 p = tv_get_string_buf_chk(&argvars[1], buf);
2935 if (p == NULL)
2936 {
2937 fclose(fd);
2938 goto errret; /* type error; errmsg already given */
2939 }
2940 len = STRLEN(p);
2941 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2942 err = TRUE;
2943 }
2944 if (fclose(fd) != 0)
2945 err = TRUE;
2946 if (err)
2947 {
2948 emsg(_("E677: Error writing temp file"));
2949 goto errret;
2950 }
2951 }
2952
2953 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2954 * echoes typeahead, that messes up the display. */
2955 if (!msg_silent)
2956 flags += SHELL_COOKED;
2957
2958 if (retlist)
2959 {
2960 int len;
2961 listitem_T *li;
2962 char_u *s = NULL;
2963 char_u *start;
2964 char_u *end;
2965 int i;
2966
2967 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2968 if (res == NULL)
2969 goto errret;
2970
2971 list = list_alloc();
2972 if (list == NULL)
2973 goto errret;
2974
2975 for (i = 0; i < len; ++i)
2976 {
2977 start = res + i;
2978 while (i < len && res[i] != NL)
2979 ++i;
2980 end = res + i;
2981
2982 s = alloc(end - start + 1);
2983 if (s == NULL)
2984 goto errret;
2985
2986 for (p = s; start < end; ++p, ++start)
2987 *p = *start == NUL ? NL : *start;
2988 *p = NUL;
2989
2990 li = listitem_alloc();
2991 if (li == NULL)
2992 {
2993 vim_free(s);
2994 goto errret;
2995 }
2996 li->li_tv.v_type = VAR_STRING;
2997 li->li_tv.v_lock = 0;
2998 li->li_tv.vval.v_string = s;
2999 list_append(list, li);
3000 }
3001
3002 rettv_list_set(rettv, list);
3003 list = NULL;
3004 }
3005 else
3006 {
3007 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
3008#ifdef USE_CRNL
3009 /* translate <CR><NL> into <NL> */
3010 if (res != NULL)
3011 {
3012 char_u *s, *d;
3013
3014 d = res;
3015 for (s = res; *s; ++s)
3016 {
3017 if (s[0] == CAR && s[1] == NL)
3018 ++s;
3019 *d++ = *s;
3020 }
3021 *d = NUL;
3022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02003024 rettv->vval.v_string = res;
3025 res = NULL;
3026 }
3027
3028errret:
3029 if (infile != NULL)
3030 {
3031 mch_remove(infile);
3032 vim_free(infile);
3033 }
3034 if (res != NULL)
3035 vim_free(res);
3036 if (list != NULL)
3037 list_free(list);
3038}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02003041 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 */
3043 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02003044f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
Bram Moolenaar26262f82019-09-04 20:59:15 +02003046 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048
3049/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02003050 * "systemlist()" function
3051 */
3052 void
3053f_systemlist(typval_T *argvars, typval_T *rettv)
3054{
3055 get_cmd_output_as_rettv(argvars, rettv, TRUE);
3056}
3057# endif // FEAT_EVAL
3058
3059#endif
3060
3061/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02003062 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 * Don't do this when still processing a command or a mapping.
3064 * Don't do this when inside a ":normal" command.
3065 */
3066 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 return (p_im && stuff_empty() && typebuf_typed());
3070}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003071
3072/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02003073 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003074 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
3075 * - Remove any argument. E.g., "csh -f" -> "csh".
3076 * But don't allow a space in the path, so that this works:
3077 * "/usr/bin/csh --rcfile ~/.cshrc"
3078 * But don't do that for Windows, it's common to have a space in the path.
3079 */
3080 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003081get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003082{
3083 char_u *p;
3084
Bram Moolenaar4f974752019-02-17 17:44:42 +01003085#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003086 p = gettail(p_sh);
3087 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
3088#else
3089 p = skiptowhite(p_sh);
3090 if (*p == NUL)
3091 {
3092 /* No white space, use the tail. */
3093 p = vim_strsave(gettail(p_sh));
3094 }
3095 else
3096 {
3097 char_u *p1, *p2;
3098
3099 /* Find the last path separator before the space. */
3100 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003101 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003102 if (vim_ispathsep(*p2))
3103 p1 = p2 + 1;
3104 p = vim_strnsave(p1, (int)(p - p1));
3105 }
3106#endif
3107 return p;
3108}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01003109
3110/*
3111 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
3112 * Also check for ":\\", which MS Internet Explorer accepts, return
3113 * URL_BACKSLASH.
3114 */
3115 int
3116path_is_url(char_u *p)
3117{
3118 if (STRNCMP(p, "://", (size_t)3) == 0)
3119 return URL_SLASH;
3120 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
3121 return URL_BACKSLASH;
3122 return 0;
3123}
3124
3125/*
3126 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
3127 * Return URL_BACKSLASH for "name:\\".
3128 * Return zero otherwise.
3129 */
3130 int
3131path_with_url(char_u *fname)
3132{
3133 char_u *p;
3134
3135 for (p = fname; isalpha(*p); ++p)
3136 ;
3137 return path_is_url(p);
3138}