blob: 9708502cad451e77a0e0955780ec21147486e3cd [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 Moolenaarb3f74062020-02-26 16:16:53 +010017#if defined(__HAIKU__)
18# include <storage/FindDirectory.h>
19#endif
20
Bram Moolenaar0a52df52019-08-18 22:26:31 +020021#if defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020022# include <lm.h>
23#endif
24
Bram Moolenaar85a20022019-12-21 18:25:54 +010025#define URL_SLASH 1 // path_is_url() has found "://"
26#define URL_BACKSLASH 2 // path_is_url() has found ":\\"
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010027
Bram Moolenaar0a52df52019-08-18 22:26:31 +020028// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020029static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +020032 * get_leader_len() returns the length in bytes of the prefix of the given
33 * string which introduces a comment. If this string is not a comment then
34 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 * When "flags" is not NULL, it is set to point to the flags of the recognized
36 * comment leader.
37 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +020038 * If "include_space" is set, include trailing whitespace while calculating the
39 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_leader_len(
43 char_u *line,
44 char_u **flags,
45 int backward,
46 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +000047{
48 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +020049 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 int got_com = FALSE;
51 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +010052 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
53 char_u *string; // pointer to comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +020055 int middle_match_len = 0;
56 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +020057 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar81340392012-06-06 16:12:59 +020059 result = i = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +010060 while (VIM_ISWHITE(line[i])) // leading white space is ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 ++i;
62
63 /*
64 * Repeat to match several nested comment strings.
65 */
Bram Moolenaara4271d52011-05-10 13:38:27 +020066 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 {
68 /*
69 * scan through the 'comments' option for a match
70 */
71 found_one = FALSE;
72 for (list = curbuf->b_p_com; *list; )
73 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010074 // Get one option part into part_buf[]. Advance "list" to next
75 // one. Put "string" at start of string.
Bram Moolenaara4271d52011-05-10 13:38:27 +020076 if (!got_com && flags != NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +010077 *flags = list; // remember where flags started
Bram Moolenaara4271d52011-05-10 13:38:27 +020078 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
80 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +010081 if (string == NULL) // missing ':', ignore this part
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +010083 *string++ = NUL; // isolate flags from string
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar85a20022019-12-21 18:25:54 +010085 // If we found a middle match previously, use that match when this
86 // is not a middle or end.
Bram Moolenaara4271d52011-05-10 13:38:27 +020087 if (middle_match_len != 0
88 && vim_strchr(part_buf, COM_MIDDLE) == NULL
89 && vim_strchr(part_buf, COM_END) == NULL)
90 break;
91
Bram Moolenaar85a20022019-12-21 18:25:54 +010092 // When we already found a nested comment, only accept further
93 // nested comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
95 continue;
96
Bram Moolenaar85a20022019-12-21 18:25:54 +010097 // When 'O' flag present and using "O" command skip this one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
99 continue;
100
Bram Moolenaar85a20022019-12-21 18:25:54 +0100101 // Line contents and string must match.
102 // When string starts with white space, must have some white space
103 // (but the amount does not need to match, there might be a mix of
104 // TABs and spaces).
Bram Moolenaar1c465442017-03-12 20:10:05 +0100105 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100107 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100108 continue; // missing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +0100109 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 ++string;
111 }
112 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
113 ;
114 if (string[j] != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100115 continue; // string doesn't match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar85a20022019-12-21 18:25:54 +0100117 // When 'b' flag used, there must be white space or an
118 // end-of-line after the string in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100120 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 continue;
122
Bram Moolenaar85a20022019-12-21 18:25:54 +0100123 // We have found a match, stop searching unless this is a middle
124 // comment. The middle comment can be a substring of the end
125 // comment in which case it's better to return the length of the
126 // end comment and its flags. Thus we keep searching with middle
127 // and end matches and use an end match if it matches better.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200128 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
129 {
130 if (middle_match_len == 0)
131 {
132 middle_match_len = j;
133 saved_flags = prev_list;
134 }
135 continue;
136 }
137 if (middle_match_len != 0 && j > middle_match_len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100138 // Use this match instead of the middle match, since it's a
139 // longer thus better match.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200140 middle_match_len = 0;
141
142 if (middle_match_len == 0)
143 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 found_one = TRUE;
145 break;
146 }
147
Bram Moolenaara4271d52011-05-10 13:38:27 +0200148 if (middle_match_len != 0)
149 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100150 // Use the previously found middle match after failing to find a
151 // match with an end.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200152 if (!got_com && flags != NULL)
153 *flags = saved_flags;
154 i += middle_match_len;
155 found_one = TRUE;
156 }
157
Bram Moolenaar85a20022019-12-21 18:25:54 +0100158 // No match found, stop scanning.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 if (!found_one)
160 break;
161
Bram Moolenaar81340392012-06-06 16:12:59 +0200162 result = i;
163
Bram Moolenaar85a20022019-12-21 18:25:54 +0100164 // Include any trailing white space.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100165 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++i;
167
Bram Moolenaar81340392012-06-06 16:12:59 +0200168 if (include_space)
169 result = i;
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // If this comment doesn't nest, stop here.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200172 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (vim_strchr(part_buf, COM_NEST) == NULL)
174 break;
175 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200176 return result;
177}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200178
Bram Moolenaar81340392012-06-06 16:12:59 +0200179/*
180 * Return the offset at which the last comment in line starts. If there is no
181 * comment in the whole line, -1 is returned.
182 *
183 * When "flags" is not null, it is set to point to the flags describing the
184 * recognized comment leader.
185 */
186 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100187get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200188{
189 int result = -1;
190 int i, j;
191 int lower_check_bound = 0;
192 char_u *string;
193 char_u *com_leader;
194 char_u *com_flags;
195 char_u *list;
196 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200198
199 /*
200 * Repeat to match several nested comment strings.
201 */
202 i = (int)STRLEN(line);
203 while (--i >= lower_check_bound)
204 {
205 /*
206 * scan through the 'comments' option for a match
207 */
208 found_one = FALSE;
209 for (list = curbuf->b_p_com; *list; )
210 {
211 char_u *flags_save = list;
212
213 /*
214 * Get one option part into part_buf[]. Advance list to next one.
215 * put string at start of string.
216 */
217 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
218 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 if (string == NULL) // If everything is fine, this cannot actually
220 // happen.
Bram Moolenaar81340392012-06-06 16:12:59 +0200221 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 *string++ = NUL; // Isolate flags from string.
Bram Moolenaar81340392012-06-06 16:12:59 +0200223 com_leader = string;
224
225 /*
226 * Line contents and string must match.
227 * When string starts with white space, must have some white space
228 * (but the amount does not need to match, there might be a mix of
229 * TABs and spaces).
230 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100231 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200232 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100233 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200234 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100235 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200236 ++string;
237 }
238 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
239 /* do nothing */;
240 if (string[j] != NUL)
241 continue;
242
243 /*
244 * When 'b' flag used, there must be white space or an
245 * end-of-line after the string in the line.
246 */
247 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100248 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200249 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100250
Bram Moolenaar4af72592018-12-09 15:00:52 +0100251 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100252 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100253 // For a middlepart comment, only consider it to match if
254 // everything before the current position in the line is
255 // whitespace. Otherwise we would think we are inside a
256 // comment if the middle part appears somewhere in the middle
257 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100258 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
259 ;
260 if (j < i)
261 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200262 }
263
264 /*
265 * We have found a match, stop searching.
266 */
267 found_one = TRUE;
268
269 if (flags)
270 *flags = flags_save;
271 com_flags = flags_save;
272
273 break;
274 }
275
276 if (found_one)
277 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 char_u part_buf2[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200279 int len1, len2, off;
280
281 result = i;
282 /*
283 * If this comment nests, continue searching.
284 */
285 if (vim_strchr(part_buf, COM_NEST) != NULL)
286 continue;
287
288 lower_check_bound = i;
289
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Let's verify whether the comment leader found is a substring
291 // of other comment leaders. If it is, let's adjust the
292 // lower_check_bound so that we make sure that we have determined
293 // the comment leader correctly.
Bram Moolenaar81340392012-06-06 16:12:59 +0200294
Bram Moolenaar1c465442017-03-12 20:10:05 +0100295 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200296 ++com_leader;
297 len1 = (int)STRLEN(com_leader);
298
299 for (list = curbuf->b_p_com; *list; )
300 {
301 char_u *flags_save = list;
302
303 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
304 if (flags_save == com_flags)
305 continue;
306 string = vim_strchr(part_buf2, ':');
307 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100308 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200309 ++string;
310 len2 = (int)STRLEN(string);
311 if (len2 == 0)
312 continue;
313
Bram Moolenaar85a20022019-12-21 18:25:54 +0100314 // Now we have to verify whether string ends with a substring
315 // beginning the com_leader.
Bram Moolenaar81340392012-06-06 16:12:59 +0200316 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
317 {
318 --off;
319 if (!STRNCMP(string + off, com_leader, len2 - off))
320 {
321 if (i - off < lower_check_bound)
322 lower_check_bound = i - off;
323 }
324 }
325 }
326 }
327 }
328 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
331/*
332 * Return the number of window lines occupied by buffer line "lnum".
333 */
334 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100335plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return plines_win(curwin, lnum, TRUE);
338}
339
340 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100341plines_win(
342 win_T *wp,
343 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100344 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346#if defined(FEAT_DIFF) || defined(PROTO)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100347 // Check for filler lines above this buffer line. When folded the result
348 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
350}
351
352 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100353plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 return plines_win_nofill(curwin, lnum, TRUE);
356}
357
358 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100359plines_win_nofill(
360 win_T *wp,
361 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364#endif
365 int lines;
366
367 if (!wp->w_p_wrap)
368 return 1;
369
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 if (wp->w_width == 0)
371 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100374 // A folded lines is handled just like an empty line.
375 // NOTE: Caller must handle lines that are MAYBE folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (lineFolded(wp, lnum) == TRUE)
377 return 1;
378#endif
379
380 lines = plines_win_nofold(wp, lnum);
381 if (winheight > 0 && lines > wp->w_height)
382 return (int)wp->w_height;
383 return lines;
384}
385
386/*
387 * Return number of window lines physical line "lnum" will occupy in window
388 * "wp". Does not care about folding, 'wrap' or 'diff'.
389 */
390 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100391plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392{
393 char_u *s;
394 long col;
395 int width;
396
397 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100398 if (*s == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 return 1;
400 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
401
402 /*
403 * If list mode is on, then the '$' at the end of the line may take up one
404 * extra column.
405 */
Bram Moolenaareed9d462021-02-15 20:38:25 +0100406 if (wp->w_p_list && wp->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 col += 1;
408
409 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200410 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200412 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (width <= 0)
414 return 32000;
415 if (col <= width)
416 return 1;
417 col -= width;
418 width += win_col_off2(wp);
419 return (col + (width - 1)) / width + 1;
420}
421
422/*
423 * Like plines_win(), but only reports the number of physical screen lines
424 * used from the start of the line to the given column number.
425 */
426 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100427plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429 long col;
430 char_u *s;
431 int lines = 0;
432 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200433 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100436 // Check for filler lines above this buffer line. When folded the result
437 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 lines = diff_check_fill(wp, lnum);
439#endif
440
441 if (!wp->w_p_wrap)
442 return lines + 1;
443
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (wp->w_width == 0)
445 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar597a4222014-06-25 14:39:50 +0200447 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449 col = 0;
450 while (*s != NUL && --column >= 0)
451 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200452 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100453 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 }
455
456 /*
457 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
458 * INSERT mode, then col must be adjusted so that it represents the last
459 * screen position of the TAB. This only fixes an error when the TAB wraps
460 * from one screen line to the next (when 'columns' is not a multiple of
461 * 'ts') -- webb.
462 */
Bram Moolenaareed9d462021-02-15 20:38:25 +0100463 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list ||
464 wp->w_lcs_chars.tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200465 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
467 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200468 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200470 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +0000471 if (width <= 0)
472 return 9999;
473
474 lines += 1;
475 if (col > width)
476 lines += (col - width) / (width + win_col_off2(wp)) + 1;
477 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478}
479
480 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100481plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482{
483 int count = 0;
484
485 while (first <= last)
486 {
487#ifdef FEAT_FOLDING
488 int x;
489
Bram Moolenaar85a20022019-12-21 18:25:54 +0100490 // Check if there are any really folded lines, but also included lines
491 // that are maybe folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 x = foldedCount(wp, first, NULL);
493 if (x > 0)
494 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100495 ++count; // count 1 for "+-- folded" line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 first += x;
497 }
498 else
499#endif
500 {
501#ifdef FEAT_DIFF
502 if (first == wp->w_topline)
503 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
504 else
505#endif
506 count += plines_win(wp, first, TRUE);
507 ++first;
508 }
509 }
510 return (count);
511}
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100514gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100516 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
Bram Moolenaar85a20022019-12-21 18:25:54 +0100518 // When searching columns is sometimes put at the end of a line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100519 if (pos->col == MAXCOL)
520 return NUL;
521 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (has_mbyte)
523 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 return (int)*ptr;
525}
526
527 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100528gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (has_mbyte)
531 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 return (int)*ml_get_cursor();
533}
534
535/*
536 * Write a character at the current cursor position.
537 * It is directly written into the block.
538 */
539 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100540pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
543 + curwin->w_cursor.col) = c;
544}
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Skip to next part of an option argument: Skip space and comma.
548 */
549 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100550skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
552 if (*p == ',')
553 ++p;
554 while (*p == ' ')
555 ++p;
556 return p;
557}
558
559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 * check_status: called when the status bars for the buffer 'buf'
561 * need to be updated
562 */
563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100564check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565{
566 win_T *wp;
567
Bram Moolenaar29323592016-07-24 22:04:11 +0200568 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (wp->w_buffer == buf && wp->w_status_height)
570 {
571 wp->w_redr_status = TRUE;
572 if (must_redraw < VALID)
573 must_redraw = VALID;
574 }
575}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 * Ask for a reply from the user, a 'y' or a 'n'.
579 * No other characters are accepted, the message is repeated until a valid
580 * reply is entered or CTRL-C is hit.
581 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
582 * from any buffers but directly from the user.
583 *
584 * return the 'y' or 'n'
585 */
586 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100587ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 int r = ' ';
590 int save_State = State;
591
Bram Moolenaar85a20022019-12-21 18:25:54 +0100592 if (exiting) // put terminal in raw mode for this question
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 settmode(TMODE_RAW);
594 ++no_wait_return;
595#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200596 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597#endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200598 State = CONFIRM; // mouse behaves like with :confirm
599 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200601 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603 while (r != 'y' && r != 'n')
604 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100605 // same highlighting as for wait_return
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100606 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (direct)
608 r = get_keystroke();
609 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000610 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 if (r == Ctrl_C || r == ESC)
612 r = 'n';
Bram Moolenaar85a20022019-12-21 18:25:54 +0100613 msg_putchar(r); // show what you typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 out_flush();
615 }
616 --no_wait_return;
617 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 --no_mapping;
620 --allow_keys;
621
622 return r;
623}
624
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200625#if defined(FEAT_EVAL) || defined(PROTO)
626
627/*
628 * "mode()" function
629 */
630 void
631f_mode(typval_T *argvars, typval_T *rettv)
632{
633 char_u buf[4];
634
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200635 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
636 return;
637
Bram Moolenaara80faa82020-04-12 19:37:17 +0200638 CLEAR_FIELD(buf);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200639
640 if (time_for_testing == 93784)
641 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100642 // Testing the two-character code.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200643 buf[0] = 'x';
644 buf[1] = '!';
645 }
646#ifdef FEAT_TERMINAL
647 else if (term_use_loop())
648 buf[0] = 't';
649#endif
650 else if (VIsual_active)
651 {
652 if (VIsual_select)
653 buf[0] = VIsual_mode + 's' - 'v';
654 else
zeertzjqeaf3f362021-07-28 16:51:53 +0200655 {
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200656 buf[0] = VIsual_mode;
zeertzjqeaf3f362021-07-28 16:51:53 +0200657 if (restart_VIsual_select)
658 buf[1] = 's';
659 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200660 }
661 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
662 || State == CONFIRM)
663 {
664 buf[0] = 'r';
665 if (State == ASKMORE)
666 buf[1] = 'm';
667 else if (State == CONFIRM)
668 buf[1] = '?';
669 }
670 else if (State == EXTERNCMD)
671 buf[0] = '!';
672 else if (State & INSERT)
673 {
674 if (State & VREPLACE_FLAG)
675 {
676 buf[0] = 'R';
677 buf[1] = 'v';
678 }
679 else
680 {
681 if (State & REPLACE_FLAG)
682 buf[0] = 'R';
683 else
684 buf[0] = 'i';
685 if (ins_compl_active())
686 buf[1] = 'c';
687 else if (ctrl_x_mode_not_defined_yet())
688 buf[1] = 'x';
689 }
690 }
691 else if ((State & CMDLINE) || exmode_active)
692 {
693 buf[0] = 'c';
694 if (exmode_active == EXMODE_VIM)
695 buf[1] = 'v';
696 else if (exmode_active == EXMODE_NORMAL)
697 buf[1] = 'e';
698 }
699 else
700 {
701 buf[0] = 'n';
702 if (finish_op)
703 {
704 buf[1] = 'o';
Yegappan Lakshmanana2438132021-07-10 21:29:18 +0200705 // to be able to detect force-linewise/blockwise/characterwise
706 // operations
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200707 buf[2] = motion_force;
708 }
709 else if (restart_edit == 'I' || restart_edit == 'R'
710 || restart_edit == 'V')
711 {
712 buf[1] = 'i';
713 buf[2] = restart_edit;
714 }
715 }
716
Bram Moolenaar85a20022019-12-21 18:25:54 +0100717 // Clear out the minor mode when the argument is not a non-zero number or
718 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200719 if (!non_zero_arg(&argvars[0]))
720 buf[1] = NUL;
721
722 rettv->vval.v_string = vim_strsave(buf);
723 rettv->v_type = VAR_STRING;
724}
725
726 static void
727may_add_state_char(garray_T *gap, char_u *include, int c)
728{
729 if (include == NULL || vim_strchr(include, c) != NULL)
730 ga_append(gap, c);
731}
732
733/*
734 * "state()" function
735 */
736 void
737f_state(typval_T *argvars, typval_T *rettv)
738{
739 garray_T ga;
740 char_u *include = NULL;
741 int i;
742
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200743 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
744 return;
745
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200746 ga_init2(&ga, 1, 20);
747 if (argvars[0].v_type != VAR_UNKNOWN)
748 include = tv_get_string(&argvars[0]);
749
750 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
751 may_add_state_char(&ga, include, 'm');
752 if (op_pending())
753 may_add_state_char(&ga, include, 'o');
754 if (autocmd_busy)
755 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200756 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200757 may_add_state_char(&ga, include, 'a');
758
759# ifdef FEAT_JOB_CHANNEL
760 if (channel_in_blocking_wait())
761 may_add_state_char(&ga, include, 'w');
762# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200763 if (!get_was_safe_state())
764 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200765 for (i = 0; i < get_callback_depth() && i < 3; ++i)
766 may_add_state_char(&ga, include, 'c');
767 if (msg_scrolled > 0)
768 may_add_state_char(&ga, include, 's');
769
770 rettv->v_type = VAR_STRING;
771 rettv->vval.v_string = ga.ga_data;
772}
773
774#endif // FEAT_EVAL
775
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776/*
777 * Get a key stroke directly from the user.
778 * Ignores mouse clicks and scrollbar events, except a click for the left
779 * button (used at the more prompt).
780 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
781 * Disadvantage: typeahead is ignored.
782 * Translates the interrupt character for unix to ESC.
783 */
784 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100785get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100787 char_u *buf = NULL;
788 int buflen = 150;
789 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 int len = 0;
791 int n;
792 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000793 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaar85a20022019-12-21 18:25:54 +0100795 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 for (;;)
797 {
798 cursor_on();
799 out_flush();
800
Bram Moolenaar85a20022019-12-21 18:25:54 +0100801 // Leave some room for check_termcode() to insert a key code into (max
802 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
803 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100804 maxlen = (buflen - 6 - len) / 3;
805 if (buf == NULL)
806 buf = alloc(buflen);
807 else if (maxlen < 10)
808 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100809 char_u *t_buf = buf;
810
Bram Moolenaar85a20022019-12-21 18:25:54 +0100811 // Need some more space. This might happen when receiving a long
812 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100813 buflen += 100;
814 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100815 if (buf == NULL)
816 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100817 maxlen = (buflen - 6 - len) / 3;
818 }
819 if (buf == NULL)
820 {
821 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100822 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100823 }
824
Bram Moolenaar85a20022019-12-21 18:25:54 +0100825 // First time: blocking wait. Second time: wait up to 100ms for a
826 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100827 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (n > 0)
829 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100830 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200831 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000833 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000835 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100836 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Bram Moolenaar85a20022019-12-21 18:25:54 +0100838 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100839 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000840 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000842
Bram Moolenaar85a20022019-12-21 18:25:54 +0100843 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100844 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100845 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100846 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100847 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100848 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100849 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100850 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100851 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100852 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100853 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100855 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 continue;
857
Bram Moolenaar85a20022019-12-21 18:25:54 +0100858 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 n = buf[0];
860 if (n == K_SPECIAL)
861 {
862 n = TO_SPECIAL(buf[1], buf[2]);
863 if (buf[1] == KS_MODIFIER
864 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100865 || (is_mouse_key(n) && n != K_LEFTMOUSE)
866#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 || n == K_VER_SCROLLBAR
868 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#endif
870 )
871 {
872 if (buf[1] == KS_MODIFIER)
873 mod_mask = buf[2];
874 len -= 3;
875 if (len > 0)
876 mch_memmove(buf, buf + 3, (size_t)len);
877 continue;
878 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000879 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (has_mbyte)
882 {
883 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100884 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100885 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 n = (*mb_ptr2char)(buf);
887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#ifdef UNIX
889 if (n == intr_char)
890 n = ESC;
891#endif
892 break;
893 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100894 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
896 mapped_ctrl_c = save_mapped_ctrl_c;
897 return n;
898}
899
900/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000901 * Get a number from the user.
902 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 */
904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100905get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100906 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100907 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 int n = 0;
910 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000911 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000913 if (mouse_used != NULL)
914 *mouse_used = FALSE;
915
Bram Moolenaar85a20022019-12-21 18:25:54 +0100916 // When not printing messages, the user won't know what to type, return a
917 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (msg_silent != 0)
919 return 0;
920
921#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100922 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#endif
924 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100925 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 for (;;)
927 {
928 windgoto(msg_row, msg_col);
929 c = safe_vgetc();
930 if (VIM_ISDIGIT(c))
931 {
932 n = n * 10 + c - '0';
933 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000934 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
936 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
937 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000938 if (typed > 0)
939 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100940 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000941 --typed;
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000945 else if (mouse_used != NULL && c == K_LEFTMOUSE)
946 {
947 *mouse_used = TRUE;
948 n = mouse_row + 1;
949 break;
950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else if (n == 0 && c == ':' && colon)
952 {
953 stuffcharReadbuff(':');
954 if (!exmode_active)
955 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100956 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 do_redraw = FALSE;
958 break;
959 }
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +0200960 else if (c == Ctrl_C || c == ESC || c == 'q')
961 {
962 n = 0;
963 break;
964 }
965 else if (c == CAR || c == NL )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 break;
967 }
968 --no_mapping;
969 --allow_keys;
970 return n;
971}
972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000973/*
974 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000975 * When "mouse_used" is not NULL allow using the mouse and in that case return
976 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000977 */
978 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100979prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000980{
981 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000982 int save_cmdline_row;
983 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000984
Bram Moolenaar85a20022019-12-21 18:25:54 +0100985 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000986 if (mouse_used != NULL)
Bram Moolenaareebd5552020-06-10 15:45:57 +0200987 msg_puts(_("Type number and <Enter> or click with the mouse (q or empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000988 else
Bram Moolenaareebd5552020-06-10 15:45:57 +0200989 msg_puts(_("Type number and <Enter> (q or empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000990
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200991 // Set the state such that text can be selected/copied/pasted and we still
992 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
993 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000994 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +0000995 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000996 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200997 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200998 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200999 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001000
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001001 i = get_number(TRUE, mouse_used);
1002 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001003 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001004 // don't call wait_return() now
1005 if (msg_row > 0)
1006 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001007 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +01001008 msg_didany = FALSE;
1009 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001010 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001011 else
1012 cmdline_row = save_cmdline_row;
1013 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001014 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001015 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001017 return i;
1018}
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001021msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 long pn;
1024
Bram Moolenaar85a20022019-12-21 18:25:54 +01001025 if (global_busy // no messages now, wait until global is finished
1026 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 return;
1028
Bram Moolenaar85a20022019-12-21 18:25:54 +01001029 // We don't want to overwrite another important message, but do overwrite
1030 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1031 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001032 if (keep_msg != NULL && !keep_msg_more)
1033 return;
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (n > 0)
1036 pn = n;
1037 else
1038 pn = -n;
1039
1040 if (pn > p_report)
1041 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001042 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001043 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001044 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001046 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001047 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001049 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1050 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (msg(msg_buf))
1052 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001053 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001054 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
1056 }
1057}
1058
1059/*
1060 * flush map and typeahead buffers and give a warning for an error
1061 */
1062 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
1065 if (emsg_silent == 0)
1066 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001067 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001068 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
1070}
1071
1072/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001073 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076vim_beep(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001077 unsigned val) // one of the BO_ values, e.g., BO_OPER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001079#ifdef FEAT_EVAL
1080 called_vim_beep = TRUE;
1081#endif
1082
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001083 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001085 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1086 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001087#ifdef ELAPSED_FUNC
1088 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001089 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001090
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // Only beep once per half a second, otherwise a sequence of beeps
1092 // would freeze Vim.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001093 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1094 {
1095 did_init = TRUE;
1096 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001098 if (p_vb
1099#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01001100 // While the GUI is starting up the termcap is set for
1101 // the GUI but the output still goes to a terminal.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001102 && !(gui.in_use && gui.starting)
1103#endif
1104 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001105 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001106 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001107#ifdef FEAT_VTP
Bram Moolenaar85a20022019-12-21 18:25:54 +01001108 // No restore color information, refresh the screen.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001109 if (has_vtp_working() != 0
1110# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001111 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001112# endif
1113 )
1114 {
1115 redraw_later(CLEAR);
1116 update_screen(0);
1117 redrawcmd();
1118 }
1119#endif
1120 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001121 else
1122 out_char(BELL);
1123#ifdef ELAPSED_FUNC
1124 }
1125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 // When 'debug' contains "beep" produce a message. If we are sourcing
1129 // a script or executing a function give the user a hint where the beep
1130 // comes from.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 if (vim_strchr(p_debug, 'e') != NULL)
1132 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001133 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001134 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137}
1138
1139/*
1140 * To get the "real" home directory:
1141 * - get value of $HOME
1142 * For Unix:
1143 * - go to that directory
1144 * - do mch_dirname() to get the real name of that directory.
1145 * This also works with mounts and links.
1146 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001147 * For Windows:
1148 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001151init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152{
1153 char_u *var;
1154
Bram Moolenaar85a20022019-12-21 18:25:54 +01001155 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001156 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#ifdef VMS
1159 var = mch_getenv((char_u *)"SYS$LOGIN");
1160#else
1161 var = mch_getenv((char_u *)"HOME");
1162#endif
1163
Bram Moolenaar4f974752019-02-17 17:44:42 +01001164#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001166 * Typically, $HOME is not defined on Windows, unless the user has
1167 * specifically defined it for Vim's sake. However, on Windows NT
1168 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1169 * each user. Try constructing $HOME from these.
1170 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001171 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001172 {
1173 char_u *homedrive, *homepath;
1174
1175 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1176 homepath = mch_getenv((char_u *)"HOMEPATH");
1177 if (homepath == NULL || *homepath == NUL)
1178 homepath = (char_u *)"\\";
1179 if (homedrive != NULL
1180 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1181 {
1182 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1183 if (NameBuff[0] != NUL)
1184 var = NameBuff;
1185 }
1186 }
1187
1188 if (var == NULL)
1189 var = mch_getenv((char_u *)"USERPROFILE");
1190
1191 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 * Weird but true: $HOME may contain an indirect reference to another
1193 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1194 * when $HOME is being set.
1195 */
1196 if (var != NULL && *var == '%')
1197 {
1198 char_u *p;
1199 char_u *exp;
1200
1201 p = vim_strchr(var + 1, '%');
1202 if (p != NULL)
1203 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001204 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 exp = mch_getenv(NameBuff);
1206 if (exp != NULL && *exp != NUL
1207 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1208 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001209 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212 }
1213 }
1214
Bram Moolenaar85a20022019-12-21 18:25:54 +01001215 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001216 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
Bram Moolenaar05159a02005-02-26 23:04:13 +00001218 if (enc_utf8 && var != NULL)
1219 {
1220 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001221 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001222
Bram Moolenaar85a20022019-12-21 18:25:54 +01001223 // Convert from active codepage to UTF-8. Other conversions are
1224 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001225 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001226 if (pp != NULL)
1227 {
1228 homedir = pp;
1229 return;
1230 }
1231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 /*
1234 * Default home dir is C:/
1235 * Best assumption we can make in such a situation.
1236 */
1237 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001238 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (var != NULL)
1242 {
1243#ifdef UNIX
1244 /*
1245 * Change to the directory and get the actual path. This resolves
1246 * links. Don't do it when we can't return.
1247 */
1248 if (mch_dirname(NameBuff, MAXPATHL) == OK
1249 && mch_chdir((char *)NameBuff) == 0)
1250 {
1251 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1252 var = IObuff;
1253 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256#endif
1257 homedir = vim_strsave(var);
1258 }
1259}
1260
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001261#if defined(EXITFREE) || defined(PROTO)
1262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001263free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001264{
1265 vim_free(homedir);
1266}
Bram Moolenaar24305862012-08-15 14:05:05 +02001267
Bram Moolenaar24305862012-08-15 14:05:05 +02001268 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001269free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001270{
1271 ga_clear_strings(&ga_users);
1272}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001273#endif
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001276 * Call expand_env() and store the result in an allocated string.
1277 * This is not very memory efficient, this expects the result to be freed
1278 * again soon.
1279 */
1280 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001281expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001282{
1283 return expand_env_save_opt(src, FALSE);
1284}
1285
1286/*
1287 * Idem, but when "one" is TRUE handle the string as one file name, only
1288 * expand "~" at the start.
1289 */
1290 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001291expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001292{
1293 char_u *p;
1294
1295 p = alloc(MAXPATHL);
1296 if (p != NULL)
1297 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1298 return p;
1299}
1300
1301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 * Expand environment variable with path name.
1303 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001304 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * If anything fails no expansion is done and dst equals src.
1306 */
1307 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001308expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001309 char_u *src, // input string e.g. "$HOME/vim.hlp"
1310 char_u *dst, // where to put the result
1311 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001313 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314}
1315
1316 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001317expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1319 char_u *dst, // where to put the result
1320 int dstlen, // maximum length of the result
1321 int esc, // escape spaces in expanded variables
1322 int one, // "srcp" is one file name
1323 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001325 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 char_u *tail;
1327 int c;
1328 char_u *var;
1329 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 int mustfree; // var was allocated, need to free it later
1331 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001332 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001334 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001335 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001336
1337 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 while (*src && dstlen > 0)
1340 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001341#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001342 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001343 if (src[0] == '`' && src[1] == '=')
1344 {
1345 size_t len;
1346
1347 var = src;
1348 src += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02001349 (void)skip_expr(&src, NULL);
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001350 if (*src == '`')
1351 ++src;
1352 len = src - var;
1353 if (len > (size_t)dstlen)
1354 len = dstlen;
1355 vim_strncpy(dst, var, len);
1356 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001357 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001358 continue;
1359 }
1360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001362 if ((*src == '$'
1363#ifdef VMS
1364 && at_start
1365#endif
1366 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001367#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 || *src == '%'
1369#endif
1370 || (*src == '~' && at_start))
1371 {
1372 mustfree = FALSE;
1373
1374 /*
1375 * The variable name is copied into dst temporarily, because it may
1376 * be a string in read-only memory and a NUL needs to be appended.
1377 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001378 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 tail = src + 1;
1381 var = dst;
1382 c = dstlen - 1;
1383
1384#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001385 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (*tail == '{' && !vim_isIDc('{'))
1387 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001388 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 while (c-- > 0 && *tail && *tail != '}')
1390 *var++ = *tail++;
1391 }
1392 else
1393#endif
1394 {
1395 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001396#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 || (*src == '%' && *tail != '%')
1398#endif
1399 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001403#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404# ifdef UNIX
1405 if (src[1] == '{' && *tail != '}')
1406# else
1407 if (*src == '%' && *tail != '%')
1408# endif
1409 var = NULL;
1410 else
1411 {
1412# ifdef UNIX
1413 if (src[1] == '{')
1414# else
1415 if (*src == '%')
1416#endif
1417 ++tail;
1418#endif
1419 *var = NUL;
1420 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001421#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 }
1423#endif
1424 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001425 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 else if ( src[1] == NUL
1427 || vim_ispathsep(src[1])
1428 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1429 {
1430 var = homedir;
1431 tail = src + 1;
1432 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001433 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
1435#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1436 /*
1437 * Copy ~user to dst[], so we can put a NUL after it.
1438 */
1439 tail = src;
1440 var = dst;
1441 c = dstlen - 1;
1442 while ( c-- > 0
1443 && *tail
1444 && vim_isfilec(*tail)
1445 && !vim_ispathsep(*tail))
1446 *var++ = *tail++;
1447 *var = NUL;
1448# ifdef UNIX
1449 /*
1450 * If the system supports getpwnam(), use it.
1451 * Otherwise, or if getpwnam() fails, the shell is used to
1452 * expand ~user. This is slower and may fail if the shell
1453 * does not support ~user (old versions of /bin/sh).
1454 */
1455# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1456 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001457 // Note: memory allocated by getpwnam() is never freed.
1458 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001459 struct passwd *pw = (*dst == NUL)
1460 ? NULL : getpwnam((char *)dst + 1);
1461
1462 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464 if (var == NULL)
1465# endif
1466 {
1467 expand_T xpc;
1468
1469 ExpandInit(&xpc);
1470 xpc.xp_context = EXPAND_FILES;
1471 var = ExpandOne(&xpc, dst, NULL,
1472 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 mustfree = TRUE;
1474 }
1475
Bram Moolenaar85a20022019-12-21 18:25:54 +01001476# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /*
1478 * USER_HOME is a comma-separated list of
1479 * directories to search for the user account in.
1480 */
1481 {
1482 char_u test[MAXPATHL], paths[MAXPATHL];
1483 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001484 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 STRCPY(paths, USER_HOME);
1487 next_path = paths;
1488 while (*next_path)
1489 {
1490 for (path = next_path; *next_path && *next_path != ',';
1491 next_path++);
1492 if (*next_path)
1493 *next_path++ = NUL;
1494 STRCPY(test, path);
1495 STRCAT(test, "/");
1496 STRCAT(test, dst + 1);
1497 if (mch_stat(test, &st) == 0)
1498 {
1499 var = alloc(STRLEN(test) + 1);
1500 STRCPY(var, test);
1501 mustfree = TRUE;
1502 break;
1503 }
1504 }
1505 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001506# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001508 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001510 tail = (char_u *)""; // for gcc
1511#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513
1514#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001515 // If 'shellslash' is set change backslashes to forward slashes.
1516 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1518 {
1519 char_u *p = vim_strsave(var);
1520
1521 if (p != NULL)
1522 {
1523 if (mustfree)
1524 vim_free(var);
1525 var = p;
1526 mustfree = TRUE;
1527 forward_slash(var);
1528 }
1529 }
1530#endif
1531
Bram Moolenaar85a20022019-12-21 18:25:54 +01001532 // If "var" contains white space, escape it with a backslash.
1533 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1535 {
1536 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1537
1538 if (p != NULL)
1539 {
1540 if (mustfree)
1541 vim_free(var);
1542 var = p;
1543 mustfree = TRUE;
1544 }
1545 }
1546
1547 if (var != NULL && *var != NUL
1548 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1549 {
1550 STRCPY(dst, var);
1551 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001552 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001553 // if var[] ends in a path separator and tail[] starts
1554 // with it, skip a character
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001555 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1557 && dst[-1] != ':'
1558#endif
1559 && vim_ispathsep(*tail))
1560 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001561 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 src = tail;
1563 copy_char = FALSE;
1564 }
1565 if (mustfree)
1566 vim_free(var);
1567 }
1568
Bram Moolenaar85a20022019-12-21 18:25:54 +01001569 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
1571 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001572 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001573 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1574 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 */
1576 at_start = FALSE;
1577 if (src[0] == '\\' && src[1] != NUL)
1578 {
1579 *dst++ = *src++;
1580 --dstlen;
1581 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001582 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001584 if (dstlen > 0)
1585 {
1586 *dst++ = *src++;
1587 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001588
Bram Moolenaar1c864092017-08-06 18:15:45 +02001589 if (startstr != NULL && src - startstr_len >= srcp
1590 && STRNCMP(src - startstr_len, startstr,
1591 startstr_len) == 0)
1592 at_start = TRUE;
1593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 *dst = NUL;
1598}
1599
1600/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001601 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1602 * the length of "name/". Otherwise return "pend".
1603 */
1604 static char_u *
1605remove_tail(char_u *p, char_u *pend, char_u *name)
1606{
1607 int len = (int)STRLEN(name) + 1;
1608 char_u *newend = pend - len;
1609
1610 if (newend >= p
1611 && fnamencmp(newend, name, len - 1) == 0
1612 && (newend == p || after_pathsep(p, newend)))
1613 return newend;
1614 return pend;
1615}
1616
1617/*
1618 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1619 * Return NULL if not, return its name in allocated memory otherwise.
1620 */
1621 static char_u *
1622vim_version_dir(char_u *vimdir)
1623{
1624 char_u *p;
1625
1626 if (vimdir == NULL || *vimdir == NUL)
1627 return NULL;
1628 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1629 if (p != NULL && mch_isdir(p))
1630 return p;
1631 vim_free(p);
1632 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1633 if (p != NULL && mch_isdir(p))
1634 return p;
1635 vim_free(p);
1636 return NULL;
1637}
1638
1639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 * Vim's version of getenv().
1641 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001642 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001643 * "mustfree" is set to TRUE when returned is allocated, it must be
1644 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 */
1646 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001647vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001649 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 char_u *pend;
1651 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001652#ifdef MSWIN
1653 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001655 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (STRCMP(name, "HOME") == 0)
1657 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001659 // Use Wide function
1660 wn = enc_to_utf16(name, NULL);
1661 if (wn == NULL)
1662 return NULL;
1663
1664 wp = _wgetenv(wn);
1665 vim_free(wn);
1666
1667 if (wp != NULL && *wp == NUL) // empty is the same as not set
1668 wp = NULL;
1669
1670 if (wp != NULL)
1671 {
1672 p = utf16_to_enc(wp, NULL);
1673 if (p == NULL)
1674 return NULL;
1675
1676 *mustfree = TRUE;
1677 return p;
1678 }
1679#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001681 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 p = NULL;
1683
1684 if (p != NULL)
1685 return p;
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001686
1687# ifdef __HAIKU__
1688 // special handling for user settings directory...
1689 if (STRCMP(name, "BE_USER_SETTINGS") == 0)
1690 {
1691 static char userSettingsPath[MAXPATHL];
1692
1693 if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false,
1694 userSettingsPath, MAXPATHL) == B_OK)
1695 return (char_u *)userSettingsPath;
1696 else
1697 return NULL;
1698 }
1699# endif
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001702 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1704 if (!vimruntime && STRCMP(name, "VIM") != 0)
1705 return NULL;
1706
1707 /*
1708 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1709 * Don't do this when default_vimruntime_dir is non-empty.
1710 */
1711 if (vimruntime
1712#ifdef HAVE_PATHDEF
1713 && *default_vimruntime_dir == NUL
1714#endif
1715 )
1716 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001717#ifdef MSWIN
1718 // Use Wide function
1719 wp = _wgetenv(L"VIM");
1720 if (wp != NULL && *wp == NUL) // empty is the same as not set
1721 wp = NULL;
1722 if (wp != NULL)
1723 {
1724 char_u *q = utf16_to_enc(wp, NULL);
1725 if (q != NULL)
1726 {
1727 p = vim_version_dir(q);
1728 *mustfree = TRUE;
1729 if (p == NULL)
1730 p = q;
1731 }
1732 }
1733#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001735 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 p = NULL;
1737 if (p != NULL)
1738 {
1739 p = vim_version_dir(p);
1740 if (p != NULL)
1741 *mustfree = TRUE;
1742 else
1743 p = mch_getenv((char_u *)"VIM");
1744 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747
1748 /*
1749 * When expanding $VIM or $VIMRUNTIME fails, try using:
1750 * - the directory name from 'helpfile' (unless it contains '$')
1751 * - the executable name from argv[0]
1752 */
1753 if (p == NULL)
1754 {
1755 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1756 p = p_hf;
1757#ifdef USE_EXE_NAME
1758 /*
1759 * Use the name of the executable, obtained from argv[0].
1760 */
1761 else
1762 p = exe_name;
1763#endif
1764 if (p != NULL)
1765 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001766 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 pend = gettail(p);
1768
Bram Moolenaar85a20022019-12-21 18:25:54 +01001769 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (p == p_hf)
1771 pend = remove_tail(p, pend, (char_u *)"doc");
1772
1773#ifdef USE_EXE_NAME
1774# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001775 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (p == exe_name)
1777 {
1778 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001779 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001781 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1782 if (pend1 != pend)
1783 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001784 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001785 if (pnew != NULL)
1786 {
1787 STRNCPY(pnew, p, (pend1 - p));
1788 STRCPY(pnew + (pend1 - p), "Resources/vim");
1789 p = pnew;
1790 pend = p + STRLEN(p);
1791 }
1792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
1794# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001795 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (p == exe_name)
1797 pend = remove_tail(p, pend, (char_u *)"src");
1798#endif
1799
Bram Moolenaar85a20022019-12-21 18:25:54 +01001800 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (!vimruntime)
1802 {
1803 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1804 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1805 }
1806
Bram Moolenaar85a20022019-12-21 18:25:54 +01001807 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001808 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001811#ifdef MACOS_X
1812 if (p == exe_name || p == p_hf)
1813#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001814 // check that the result is a directory name
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001815 p = vim_strnsave(p, pend - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001818 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 else
1820 {
1821#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001822 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1824 {
1825 vim_free(p);
1826 p = pend;
1827 }
1828#endif
1829 *mustfree = TRUE;
1830 }
1831 }
1832 }
1833
1834#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001835 // When there is a pathdef.c file we can use default_vim_dir and
1836 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (p == NULL)
1838 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001839 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (vimruntime && *default_vimruntime_dir != NUL)
1841 {
1842 p = default_vimruntime_dir;
1843 *mustfree = FALSE;
1844 }
1845 else if (*default_vim_dir != NUL)
1846 {
1847 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1848 *mustfree = TRUE;
1849 else
1850 {
1851 p = default_vim_dir;
1852 *mustfree = FALSE;
1853 }
1854 }
1855 }
1856#endif
1857
1858 /*
1859 * Set the environment variable, so that the new value can be found fast
1860 * next time, and others can also use it (e.g. Perl).
1861 */
1862 if (p != NULL)
1863 {
1864 if (vimruntime)
1865 {
1866 vim_setenv((char_u *)"VIMRUNTIME", p);
1867 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869 else
1870 {
1871 vim_setenv((char_u *)"VIM", p);
1872 didset_vim = TRUE;
1873 }
1874 }
1875 return p;
1876}
1877
Bram Moolenaar113e1072019-01-20 15:30:40 +01001878#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02001879 void
1880vim_unsetenv(char_u *var)
1881{
1882#ifdef HAVE_UNSETENV
1883 unsetenv((char *)var);
1884#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001885 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001886#endif
1887}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001888#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02001889
1890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001892 * Set environment variable "name" and take care of side effects.
1893 */
1894 void
1895vim_setenv_ext(char_u *name, char_u *val)
1896{
1897 vim_setenv(name, val);
1898 if (STRICMP(name, "HOME") == 0)
1899 init_homedir();
1900 else if (didset_vim && STRICMP(name, "VIM") == 0)
1901 didset_vim = FALSE;
1902 else if (didset_vimruntime
1903 && STRICMP(name, "VIMRUNTIME") == 0)
1904 didset_vimruntime = FALSE;
1905}
1906
1907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 * Our portable version of setenv.
1909 */
1910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001911vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913#ifdef HAVE_SETENV
1914 mch_setenv((char *)name, (char *)val, 1);
1915#else
1916 char_u *envbuf;
1917
1918 /*
1919 * Putenv does not copy the string, it has to remain
1920 * valid. The allocated memory will never be freed.
1921 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001922 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (envbuf != NULL)
1924 {
1925 sprintf((char *)envbuf, "%s=%s", name, val);
1926 putenv((char *)envbuf);
1927 }
1928#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001929#ifdef FEAT_GETTEXT
1930 /*
1931 * When setting $VIMRUNTIME adjust the directory to find message
1932 * translations to $VIMRUNTIME/lang.
1933 */
1934 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1935 {
1936 char_u *buf = concat_str(val, (char_u *)"/lang");
1937
1938 if (buf != NULL)
1939 {
1940 bindtextdomain(VIMPACKAGE, (char *)buf);
1941 vim_free(buf);
1942 }
1943 }
1944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945}
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947/*
1948 * Function given to ExpandGeneric() to obtain an environment variable name.
1949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001951get_env_name(
1952 expand_T *xp UNUSED,
1953 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
Bram Moolenaard0573012017-10-28 21:11:06 +02001955# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02001957 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 */
1959 return NULL;
1960# else
1961# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01001962 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 extern char **environ;
1964# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001965# define ENVNAMELEN 100
1966 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 char_u *str;
1968 int n;
1969
1970 str = (char_u *)environ[idx];
1971 if (str == NULL)
1972 return NULL;
1973
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001974 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 {
1976 if (str[n] == '=' || str[n] == NUL)
1977 break;
1978 name[n] = str[n];
1979 }
1980 name[n] = NUL;
1981 return name;
1982# endif
1983}
Bram Moolenaar24305862012-08-15 14:05:05 +02001984
1985/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001986 * Add a user name to the list of users in ga_users.
1987 * Do nothing if user name is NULL or empty.
1988 */
1989 static void
1990add_user(char_u *user, int need_copy)
1991{
1992 char_u *user_copy = (user != NULL && need_copy)
1993 ? vim_strsave(user) : user;
1994
1995 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
1996 {
1997 if (need_copy)
1998 vim_free(user);
1999 return;
2000 }
2001 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2002}
2003
2004/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002005 * Find all user names for user completion.
2006 * Done only once and then cached.
2007 */
2008 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002009init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002010{
Bram Moolenaar24305862012-08-15 14:05:05 +02002011 static int lazy_init_done = FALSE;
2012
2013 if (lazy_init_done)
2014 return;
2015
2016 lazy_init_done = TRUE;
2017 ga_init2(&ga_users, sizeof(char_u *), 20);
2018
2019# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2020 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002021 struct passwd* pw;
2022
2023 setpwent();
2024 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002025 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002026 endpwent();
2027 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002028# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002029 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002030 DWORD nusers = 0, ntotal = 0, i;
2031 PUSER_INFO_0 uinfo;
2032
2033 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2034 &nusers, &ntotal, NULL) == NERR_Success)
2035 {
2036 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002037 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002038
2039 NetApiBufferFree(uinfo);
2040 }
2041 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002042# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002043# if defined(HAVE_GETPWNAM)
2044 {
2045 char_u *user_env = mch_getenv((char_u *)"USER");
2046
2047 // The $USER environment variable may be a valid remote user name (NIS,
2048 // LDAP) not already listed by getpwent(), as getpwent() only lists
2049 // local user names. If $USER is not already listed, check whether it
2050 // is a valid remote user name using getpwnam() and if it is, add it to
2051 // the list of user names.
2052
2053 if (user_env != NULL && *user_env != NUL)
2054 {
2055 int i;
2056
2057 for (i = 0; i < ga_users.ga_len; i++)
2058 {
2059 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2060
2061 if (STRCMP(local_user, user_env) == 0)
2062 break;
2063 }
2064
2065 if (i == ga_users.ga_len)
2066 {
2067 struct passwd *pw = getpwnam((char *)user_env);
2068
2069 if (pw != NULL)
2070 add_user((char_u *)pw->pw_name, TRUE);
2071 }
2072 }
2073 }
2074# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002075}
2076
2077/*
2078 * Function given to ExpandGeneric() to obtain an user names.
2079 */
2080 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002081get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002082{
2083 init_users();
2084 if (idx < ga_users.ga_len)
2085 return ((char_u **)ga_users.ga_data)[idx];
2086 return NULL;
2087}
2088
2089/*
2090 * Check whether name matches a user name. Return:
2091 * 0 if name does not match any user name.
2092 * 1 if name partially matches the beginning of a user name.
2093 * 2 is name fully matches a user name.
2094 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002095 int
2096match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002097{
2098 int i;
2099 int n = (int)STRLEN(name);
2100 int result = 0;
2101
2102 init_users();
2103 for (i = 0; i < ga_users.ga_len; i++)
2104 {
2105 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002106 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002107 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002108 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002109 }
2110 return result;
2111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002114prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002116#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002117 // Ignore SIGHUP, because a dropped connection causes a read error, which
2118 // makes Vim exit and then handling SIGHUP causes various reentrance
2119 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002120 signal(SIGHUP, SIG_IGN);
2121#endif
2122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123#ifdef FEAT_GUI
2124 if (gui.in_use)
2125 {
2126 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002127 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129 else
2130#endif
2131 {
2132 windgoto((int)Rows - 1, 0);
2133
2134 /*
2135 * Switch terminal mode back now, so messages end up on the "normal"
2136 * screen (if there are two screens).
2137 */
2138 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002139 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 out_flush();
2141 }
2142}
2143
2144/*
2145 * Preserve files and exit.
2146 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002147 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2148 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 */
2150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002151preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 buf_T *buf;
2154
2155 prepare_to_exit();
2156
Bram Moolenaar85a20022019-12-21 18:25:54 +01002157 // Setting this will prevent free() calls. That avoids calling free()
2158 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002159 really_exiting = TRUE;
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002162 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 out_flush();
2164
Bram Moolenaar85a20022019-12-21 18:25:54 +01002165 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar29323592016-07-24 22:04:11 +02002167 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2170 {
Bram Moolenaar69212b12020-05-10 14:14:03 +02002171 OUT_STR("Vim: preserving files...\r\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002172 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002174 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 break;
2176 }
2177 }
2178
Bram Moolenaar85a20022019-12-21 18:25:54 +01002179 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
Bram Moolenaar69212b12020-05-10 14:14:03 +02002181 OUT_STR("Vim: Finished.\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183 getout(1);
2184}
2185
2186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 * Check for CTRL-C pressed, but only once in a while.
2188 * Should be used instead of ui_breakcheck() for functions that check for
2189 * each line in the file. Calling ui_breakcheck() each time takes too much
2190 * time, because it can be a system call.
2191 */
2192
2193#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002194# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196
2197static int breakcheck_count = 0;
2198
2199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002200line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201{
2202 if (++breakcheck_count >= BREAKCHECK_SKIP)
2203 {
2204 breakcheck_count = 0;
2205 ui_breakcheck();
2206 }
2207}
2208
2209/*
2210 * Like line_breakcheck() but check 10 times less often.
2211 */
2212 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002213fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2216 {
2217 breakcheck_count = 0;
2218 ui_breakcheck();
2219 }
2220}
2221
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002222/*
2223 * Like line_breakcheck() but check 100 times less often.
2224 */
2225 void
2226veryfast_breakcheck(void)
2227{
2228 if (++breakcheck_count >= BREAKCHECK_SKIP * 100)
2229 {
2230 breakcheck_count = 0;
2231 ui_breakcheck();
2232 }
2233}
2234
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002235#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2236 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2237 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239#ifndef SEEK_SET
2240# define SEEK_SET 0
2241#endif
2242#ifndef SEEK_END
2243# define SEEK_END 2
2244#endif
2245
2246/*
2247 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002248 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2249 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 * Returns an allocated string, or NULL for error.
2251 */
2252 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002253get_cmd_output(
2254 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002255 char_u *infile, // optional input file name
2256 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002257 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258{
2259 char_u *tempname;
2260 char_u *command;
2261 char_u *buffer = NULL;
2262 int len;
2263 int i = 0;
2264 FILE *fd;
2265
2266 if (check_restricted() || check_secure())
2267 return NULL;
2268
Bram Moolenaar85a20022019-12-21 18:25:54 +01002269 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002270 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 return NULL;
2274 }
2275
Bram Moolenaar85a20022019-12-21 18:25:54 +01002276 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002277 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (command == NULL)
2279 goto done;
2280
2281 /*
2282 * Call the shell to execute the command (errors are ignored).
2283 * Don't check timestamps here.
2284 */
2285 ++no_check_timestamps;
2286 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2287 --no_check_timestamps;
2288
2289 vim_free(command);
2290
2291 /*
2292 * read the names from the file into memory
2293 */
2294# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 fd = mch_fopen((char *)tempname, "r");
2297# else
2298 fd = mch_fopen((char *)tempname, READBIN);
2299# endif
2300
2301 if (fd == NULL)
2302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002303 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 goto done;
2305 }
2306
2307 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 len = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 fseek(fd, 0L, SEEK_SET);
2310
2311 buffer = alloc(len + 1);
2312 if (buffer != NULL)
2313 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2314 fclose(fd);
2315 mch_remove(tempname);
2316 if (buffer == NULL)
2317 goto done;
2318#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002319 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#endif
2321 if (i != len)
2322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002323 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002324 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002326 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002327 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002328 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002329 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002330 if (buffer[i] == NUL)
2331 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002332
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002334 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002335 else
2336 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338done:
2339 vim_free(tempname);
2340 return buffer;
2341}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002342
2343# if defined(FEAT_EVAL) || defined(PROTO)
2344
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002345 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002346get_cmd_output_as_rettv(
2347 typval_T *argvars,
2348 typval_T *rettv,
2349 int retlist)
2350{
2351 char_u *res = NULL;
2352 char_u *p;
2353 char_u *infile = NULL;
2354 int err = FALSE;
2355 FILE *fd;
2356 list_T *list = NULL;
2357 int flags = SHELL_SILENT;
2358
2359 rettv->v_type = VAR_STRING;
2360 rettv->vval.v_string = NULL;
2361 if (check_restricted() || check_secure())
2362 goto errret;
2363
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002364 if (in_vim9script()
2365 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +02002366 || check_for_opt_string_or_number_or_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002367 return;
2368
Bram Moolenaar26262f82019-09-04 20:59:15 +02002369 if (argvars[1].v_type != VAR_UNKNOWN)
2370 {
2371 /*
2372 * Write the text to a temp file, to be used for input of the shell
2373 * command.
2374 */
2375 if ((infile = vim_tempname('i', TRUE)) == NULL)
2376 {
2377 emsg(_(e_notmp));
2378 goto errret;
2379 }
2380
2381 fd = mch_fopen((char *)infile, WRITEBIN);
2382 if (fd == NULL)
2383 {
2384 semsg(_(e_notopen), infile);
2385 goto errret;
2386 }
2387 if (argvars[1].v_type == VAR_NUMBER)
2388 {
2389 linenr_T lnum;
2390 buf_T *buf;
2391
2392 buf = buflist_findnr(argvars[1].vval.v_number);
2393 if (buf == NULL)
2394 {
2395 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2396 fclose(fd);
2397 goto errret;
2398 }
2399
2400 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2401 {
2402 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2403 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2404 {
2405 err = TRUE;
2406 break;
2407 }
2408 if (putc(NL, fd) == EOF)
2409 {
2410 err = TRUE;
2411 break;
2412 }
2413 }
2414 }
2415 else if (argvars[1].v_type == VAR_LIST)
2416 {
2417 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2418 err = TRUE;
2419 }
2420 else
2421 {
2422 size_t len;
2423 char_u buf[NUMBUFLEN];
2424
2425 p = tv_get_string_buf_chk(&argvars[1], buf);
2426 if (p == NULL)
2427 {
2428 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002429 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002430 }
2431 len = STRLEN(p);
2432 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2433 err = TRUE;
2434 }
2435 if (fclose(fd) != 0)
2436 err = TRUE;
2437 if (err)
2438 {
2439 emsg(_("E677: Error writing temp file"));
2440 goto errret;
2441 }
2442 }
2443
Bram Moolenaar85a20022019-12-21 18:25:54 +01002444 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2445 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002446 if (!msg_silent)
2447 flags += SHELL_COOKED;
2448
2449 if (retlist)
2450 {
2451 int len;
2452 listitem_T *li;
2453 char_u *s = NULL;
2454 char_u *start;
2455 char_u *end;
2456 int i;
2457
2458 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2459 if (res == NULL)
2460 goto errret;
2461
2462 list = list_alloc();
2463 if (list == NULL)
2464 goto errret;
2465
2466 for (i = 0; i < len; ++i)
2467 {
2468 start = res + i;
2469 while (i < len && res[i] != NL)
2470 ++i;
2471 end = res + i;
2472
2473 s = alloc(end - start + 1);
2474 if (s == NULL)
2475 goto errret;
2476
2477 for (p = s; start < end; ++p, ++start)
2478 *p = *start == NUL ? NL : *start;
2479 *p = NUL;
2480
2481 li = listitem_alloc();
2482 if (li == NULL)
2483 {
2484 vim_free(s);
2485 goto errret;
2486 }
2487 li->li_tv.v_type = VAR_STRING;
2488 li->li_tv.v_lock = 0;
2489 li->li_tv.vval.v_string = s;
2490 list_append(list, li);
2491 }
2492
2493 rettv_list_set(rettv, list);
2494 list = NULL;
2495 }
2496 else
2497 {
2498 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2499#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002500 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002501 if (res != NULL)
2502 {
2503 char_u *s, *d;
2504
2505 d = res;
2506 for (s = res; *s; ++s)
2507 {
2508 if (s[0] == CAR && s[1] == NL)
2509 ++s;
2510 *d++ = *s;
2511 }
2512 *d = NUL;
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002515 rettv->vval.v_string = res;
2516 res = NULL;
2517 }
2518
2519errret:
2520 if (infile != NULL)
2521 {
2522 mch_remove(infile);
2523 vim_free(infile);
2524 }
2525 if (res != NULL)
2526 vim_free(res);
2527 if (list != NULL)
2528 list_free(list);
2529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002532 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
2534 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002535f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002537 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538}
2539
2540/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002541 * "systemlist()" function
2542 */
2543 void
2544f_systemlist(typval_T *argvars, typval_T *rettv)
2545{
2546 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2547}
2548# endif // FEAT_EVAL
2549
2550#endif
2551
2552/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002553 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 * Don't do this when still processing a command or a mapping.
2555 * Don't do this when inside a ":normal" command.
2556 */
2557 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002558goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 return (p_im && stuff_empty() && typebuf_typed());
2561}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002562
2563/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002564 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002565 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2566 * - Remove any argument. E.g., "csh -f" -> "csh".
2567 * But don't allow a space in the path, so that this works:
2568 * "/usr/bin/csh --rcfile ~/.cshrc"
2569 * But don't do that for Windows, it's common to have a space in the path.
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002570 * Returns NULL when out of memory.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002571 */
2572 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002573get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002574{
2575 char_u *p;
2576
Bram Moolenaar4f974752019-02-17 17:44:42 +01002577#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002578 p = gettail(p_sh);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002579 p = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002580#else
2581 p = skiptowhite(p_sh);
2582 if (*p == NUL)
2583 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002584 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002585 p = vim_strsave(gettail(p_sh));
2586 }
2587 else
2588 {
2589 char_u *p1, *p2;
2590
Bram Moolenaar85a20022019-12-21 18:25:54 +01002591 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002592 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002593 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002594 if (vim_ispathsep(*p2))
2595 p1 = p2 + 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002596 p = vim_strnsave(p1, p - p1);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002597 }
2598#endif
2599 return p;
2600}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002601
2602/*
2603 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2604 * Also check for ":\\", which MS Internet Explorer accepts, return
2605 * URL_BACKSLASH.
2606 */
2607 int
2608path_is_url(char_u *p)
2609{
2610 if (STRNCMP(p, "://", (size_t)3) == 0)
2611 return URL_SLASH;
2612 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2613 return URL_BACKSLASH;
2614 return 0;
2615}
2616
2617/*
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002618 * Check if "fname" starts with "name://" or "name:\\".
2619 * Return URL_SLASH for "name://", URL_BACKSLASH for "name:\\".
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002620 * Return zero otherwise.
2621 */
2622 int
2623path_with_url(char_u *fname)
2624{
2625 char_u *p;
2626
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002627 // We accept alphabetic characters and a dash in scheme part.
2628 // RFC 3986 allows for more, but it increases the risk of matching
2629 // non-URL text.
2630
2631 // first character must be alpha
2632 if (!isalpha(*fname))
2633 return 0;
2634
2635 // check body: alpha or dash
2636 for (p = fname; (isalpha(*p) || (*p == '-')); ++p)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002637 ;
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002638
2639 // check last char is not a dash
2640 if (p[-1] == '-')
2641 return 0;
2642
2643 // "://" or ":\\" must follow
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002644 return path_is_url(p);
2645}