blob: 2d41e358be12c6e194c4b1c5f93f47413678a1da [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
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000374 // Folded lines are handled just like an empty line.
Bram Moolenaar85a20022019-12-21 18:25:54 +0100375 // 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)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000382 return wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 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/*
LemonBoy2bf52dd2022-04-09 18:17:34 +0100628 * Returns the current mode as a string in "buf[MODE_MAX_LENGTH]", NUL
629 * terminated.
630 * The first character represents the major mode, the following ones the minor
631 * ones.
632 */
633 void
634get_mode(char_u *buf)
635{
636 int i = 0;
637
638 if (time_for_testing == 93784)
639 {
640 // Testing the two-character code.
641 buf[i++] = 'x';
642 buf[i++] = '!';
643 }
644#ifdef FEAT_TERMINAL
645 else if (term_use_loop())
646 buf[i++] = 't';
647#endif
648 else if (VIsual_active)
649 {
650 if (VIsual_select)
651 buf[i++] = VIsual_mode + 's' - 'v';
652 else
653 {
654 buf[i++] = VIsual_mode;
655 if (restart_VIsual_select)
656 buf[i++] = 's';
657 }
658 }
659 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
660 || State == CONFIRM)
661 {
662 buf[i++] = 'r';
663 if (State == ASKMORE)
664 buf[i++] = 'm';
665 else if (State == CONFIRM)
666 buf[i++] = '?';
667 }
668 else if (State == EXTERNCMD)
669 buf[i++] = '!';
670 else if (State & INSERT)
671 {
672 if (State & VREPLACE_FLAG)
673 {
674 buf[i++] = 'R';
675 buf[i++] = 'v';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100676 }
677 else
678 {
679 if (State & REPLACE_FLAG)
680 buf[i++] = 'R';
681 else
682 buf[i++] = 'i';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100683 }
zeertzjq590f3652022-04-29 11:29:54 +0100684
685 if (ins_compl_active())
686 buf[i++] = 'c';
687 else if (ctrl_x_mode_not_defined_yet())
688 buf[i++] = 'x';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100689 }
690 else if ((State & CMDLINE) || exmode_active)
691 {
692 buf[i++] = 'c';
693 if (exmode_active == EXMODE_VIM)
694 buf[i++] = 'v';
695 else if (exmode_active == EXMODE_NORMAL)
696 buf[i++] = 'e';
697 }
698 else
699 {
700 buf[i++] = 'n';
701 if (finish_op)
702 {
703 buf[i++] = 'o';
704 // to be able to detect force-linewise/blockwise/characterwise
705 // operations
706 buf[i++] = motion_force;
707 }
708 else if (restart_edit == 'I' || restart_edit == 'R'
709 || restart_edit == 'V')
710 {
711 buf[i++] = 'i';
712 buf[i++] = restart_edit;
713 }
714#ifdef FEAT_TERMINAL
715 else if (term_in_normal_mode())
716 buf[i++] = 't';
717#endif
718 }
719
720 buf[i] = NUL;
721}
722
723/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200724 * "mode()" function
725 */
726 void
727f_mode(typval_T *argvars, typval_T *rettv)
728{
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200729 char_u buf[MODE_MAX_LENGTH];
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200730
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200731 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
732 return;
733
LemonBoy2bf52dd2022-04-09 18:17:34 +0100734 get_mode(buf);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200735
Bram Moolenaar85a20022019-12-21 18:25:54 +0100736 // Clear out the minor mode when the argument is not a non-zero number or
737 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200738 if (!non_zero_arg(&argvars[0]))
739 buf[1] = NUL;
740
741 rettv->vval.v_string = vim_strsave(buf);
742 rettv->v_type = VAR_STRING;
743}
744
745 static void
746may_add_state_char(garray_T *gap, char_u *include, int c)
747{
748 if (include == NULL || vim_strchr(include, c) != NULL)
749 ga_append(gap, c);
750}
751
752/*
753 * "state()" function
754 */
755 void
756f_state(typval_T *argvars, typval_T *rettv)
757{
758 garray_T ga;
759 char_u *include = NULL;
760 int i;
761
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200762 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
763 return;
764
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200765 ga_init2(&ga, 1, 20);
766 if (argvars[0].v_type != VAR_UNKNOWN)
767 include = tv_get_string(&argvars[0]);
768
769 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
770 may_add_state_char(&ga, include, 'm');
771 if (op_pending())
772 may_add_state_char(&ga, include, 'o');
773 if (autocmd_busy)
774 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200775 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200776 may_add_state_char(&ga, include, 'a');
777
778# ifdef FEAT_JOB_CHANNEL
779 if (channel_in_blocking_wait())
780 may_add_state_char(&ga, include, 'w');
781# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200782 if (!get_was_safe_state())
783 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200784 for (i = 0; i < get_callback_depth() && i < 3; ++i)
785 may_add_state_char(&ga, include, 'c');
786 if (msg_scrolled > 0)
787 may_add_state_char(&ga, include, 's');
788
789 rettv->v_type = VAR_STRING;
790 rettv->vval.v_string = ga.ga_data;
791}
792
793#endif // FEAT_EVAL
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795/*
796 * Get a key stroke directly from the user.
797 * Ignores mouse clicks and scrollbar events, except a click for the left
798 * button (used at the more prompt).
799 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
800 * Disadvantage: typeahead is ignored.
801 * Translates the interrupt character for unix to ESC.
802 */
803 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100804get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100806 char_u *buf = NULL;
807 int buflen = 150;
808 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 int len = 0;
810 int n;
811 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000812 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar85a20022019-12-21 18:25:54 +0100814 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 for (;;)
816 {
817 cursor_on();
818 out_flush();
819
Bram Moolenaar85a20022019-12-21 18:25:54 +0100820 // Leave some room for check_termcode() to insert a key code into (max
821 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
822 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100823 maxlen = (buflen - 6 - len) / 3;
824 if (buf == NULL)
825 buf = alloc(buflen);
826 else if (maxlen < 10)
827 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100828 char_u *t_buf = buf;
829
Bram Moolenaar85a20022019-12-21 18:25:54 +0100830 // Need some more space. This might happen when receiving a long
831 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100832 buflen += 100;
833 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100834 if (buf == NULL)
835 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100836 maxlen = (buflen - 6 - len) / 3;
837 }
838 if (buf == NULL)
839 {
840 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100841 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100842 }
843
Bram Moolenaar85a20022019-12-21 18:25:54 +0100844 // First time: blocking wait. Second time: wait up to 100ms for a
845 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100846 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (n > 0)
848 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100849 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200850 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000852 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000854 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100855 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaar85a20022019-12-21 18:25:54 +0100857 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100858 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000859 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000861
Bram Moolenaar85a20022019-12-21 18:25:54 +0100862 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100863 {
Bram Moolenaar23bdef22021-12-04 17:20:27 +0000864 if (must_redraw != 0 && !need_wait_return
865 && (State & (CMDLINE|HITRETURN|ASKMORE)) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100866 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100867 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100868 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100869 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100870 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100871 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100872 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100873 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100875 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 continue;
877
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 n = buf[0];
880 if (n == K_SPECIAL)
881 {
882 n = TO_SPECIAL(buf[1], buf[2]);
883 if (buf[1] == KS_MODIFIER
884 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100885 || (is_mouse_key(n) && n != K_LEFTMOUSE)
886#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 || n == K_VER_SCROLLBAR
888 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889#endif
890 )
891 {
892 if (buf[1] == KS_MODIFIER)
893 mod_mask = buf[2];
894 len -= 3;
895 if (len > 0)
896 mch_memmove(buf, buf + 3, (size_t)len);
897 continue;
898 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000899 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (has_mbyte)
902 {
903 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100904 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100905 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 n = (*mb_ptr2char)(buf);
907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#ifdef UNIX
909 if (n == intr_char)
910 n = ESC;
911#endif
912 break;
913 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100914 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
916 mapped_ctrl_c = save_mapped_ctrl_c;
917 return n;
918}
919
920/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000921 * Get a number from the user.
922 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100925get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100926 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100927 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 int n = 0;
930 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000931 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000933 if (mouse_used != NULL)
934 *mouse_used = FALSE;
935
Bram Moolenaar85a20022019-12-21 18:25:54 +0100936 // When not printing messages, the user won't know what to type, return a
937 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 if (msg_silent != 0)
939 return 0;
940
941#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100942 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#endif
944 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100945 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 for (;;)
947 {
948 windgoto(msg_row, msg_col);
949 c = safe_vgetc();
950 if (VIM_ISDIGIT(c))
951 {
952 n = n * 10 + c - '0';
953 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000954 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
957 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000958 if (typed > 0)
959 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100960 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000961 --typed;
962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000965 else if (mouse_used != NULL && c == K_LEFTMOUSE)
966 {
967 *mouse_used = TRUE;
968 n = mouse_row + 1;
969 break;
970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 else if (n == 0 && c == ':' && colon)
972 {
973 stuffcharReadbuff(':');
974 if (!exmode_active)
975 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100976 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 do_redraw = FALSE;
978 break;
979 }
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +0200980 else if (c == Ctrl_C || c == ESC || c == 'q')
981 {
982 n = 0;
983 break;
984 }
985 else if (c == CAR || c == NL )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 break;
987 }
988 --no_mapping;
989 --allow_keys;
990 return n;
991}
992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000993/*
994 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000995 * When "mouse_used" is not NULL allow using the mouse and in that case return
996 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000997 */
998 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100999prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001000{
1001 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001002 int save_cmdline_row;
1003 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001004
Bram Moolenaar85a20022019-12-21 18:25:54 +01001005 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001006 if (mouse_used != NULL)
Bram Moolenaareebd5552020-06-10 15:45:57 +02001007 msg_puts(_("Type number and <Enter> or click with the mouse (q or empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001008 else
Bram Moolenaareebd5552020-06-10 15:45:57 +02001009 msg_puts(_("Type number and <Enter> (q or empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001010
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001011 // Set the state such that text can be selected/copied/pasted and we still
1012 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1013 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001014 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001015 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001016 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001017 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001018 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001019 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001020
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001021 i = get_number(TRUE, mouse_used);
1022 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001023 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001024 // don't call wait_return() now
1025 if (msg_row > 0)
1026 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001027 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +01001028 msg_didany = FALSE;
1029 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001030 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001031 else
1032 cmdline_row = save_cmdline_row;
1033 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001034 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001035 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001037 return i;
1038}
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001041msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 long pn;
1044
Bram Moolenaar85a20022019-12-21 18:25:54 +01001045 if (global_busy // no messages now, wait until global is finished
1046 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 return;
1048
Bram Moolenaar85a20022019-12-21 18:25:54 +01001049 // We don't want to overwrite another important message, but do overwrite
1050 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1051 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001052 if (keep_msg != NULL && !keep_msg_more)
1053 return;
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (n > 0)
1056 pn = n;
1057 else
1058 pn = -n;
1059
1060 if (pn > p_report)
1061 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001062 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001063 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001064 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001066 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001067 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001069 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1070 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (msg(msg_buf))
1072 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001073 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001074 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 }
1076 }
1077}
1078
1079/*
1080 * flush map and typeahead buffers and give a warning for an error
1081 */
1082 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001083beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
1085 if (emsg_silent == 0)
1086 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001087 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001088 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090}
1091
1092/*
Bram Moolenaaraae97622022-04-13 14:28:07 +01001093 * Give a warning for an error. "val" is one of the BO_ values, e.g., BO_OPER.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
1095 void
Bram Moolenaaraae97622022-04-13 14:28:07 +01001096vim_beep(unsigned val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001098#ifdef FEAT_EVAL
1099 called_vim_beep = TRUE;
1100#endif
1101
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001102 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001104 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1105 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001106#ifdef ELAPSED_FUNC
1107 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001108 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001109
Bram Moolenaar85a20022019-12-21 18:25:54 +01001110 // Only beep once per half a second, otherwise a sequence of beeps
1111 // would freeze Vim.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001112 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1113 {
1114 did_init = TRUE;
1115 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001117 if (p_vb
1118#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01001119 // While the GUI is starting up the termcap is set for
1120 // the GUI but the output still goes to a terminal.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001121 && !(gui.in_use && gui.starting)
1122#endif
1123 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001124 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001125 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001126#ifdef FEAT_VTP
Bram Moolenaar85a20022019-12-21 18:25:54 +01001127 // No restore color information, refresh the screen.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001128 if (has_vtp_working() != 0
1129# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001130 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001131# endif
1132 )
1133 {
1134 redraw_later(CLEAR);
1135 update_screen(0);
1136 redrawcmd();
1137 }
1138#endif
1139 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001140 else
1141 out_char(BELL);
1142#ifdef ELAPSED_FUNC
1143 }
1144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 // When 'debug' contains "beep" produce a message. If we are sourcing
1148 // a script or executing a function give the user a hint where the beep
1149 // comes from.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (vim_strchr(p_debug, 'e') != NULL)
1151 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001152 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001153 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156}
1157
1158/*
1159 * To get the "real" home directory:
1160 * - get value of $HOME
1161 * For Unix:
1162 * - go to that directory
1163 * - do mch_dirname() to get the real name of that directory.
1164 * This also works with mounts and links.
1165 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001166 * For Windows:
1167 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001170init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 char_u *var;
1173
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001175 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef VMS
1178 var = mch_getenv((char_u *)"SYS$LOGIN");
1179#else
1180 var = mch_getenv((char_u *)"HOME");
1181#endif
1182
Bram Moolenaar4f974752019-02-17 17:44:42 +01001183#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001185 * Typically, $HOME is not defined on Windows, unless the user has
1186 * specifically defined it for Vim's sake. However, on Windows NT
1187 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1188 * each user. Try constructing $HOME from these.
1189 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001190 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001191 {
1192 char_u *homedrive, *homepath;
1193
1194 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1195 homepath = mch_getenv((char_u *)"HOMEPATH");
1196 if (homepath == NULL || *homepath == NUL)
1197 homepath = (char_u *)"\\";
1198 if (homedrive != NULL
1199 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1200 {
1201 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1202 if (NameBuff[0] != NUL)
1203 var = NameBuff;
1204 }
1205 }
1206
1207 if (var == NULL)
1208 var = mch_getenv((char_u *)"USERPROFILE");
1209
1210 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * Weird but true: $HOME may contain an indirect reference to another
1212 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1213 * when $HOME is being set.
1214 */
1215 if (var != NULL && *var == '%')
1216 {
1217 char_u *p;
1218 char_u *exp;
1219
1220 p = vim_strchr(var + 1, '%');
1221 if (p != NULL)
1222 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001223 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 exp = mch_getenv(NameBuff);
1225 if (exp != NULL && *exp != NUL
1226 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1227 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001228 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
1231 }
1232 }
1233
Bram Moolenaar85a20022019-12-21 18:25:54 +01001234 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001235 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaar05159a02005-02-26 23:04:13 +00001237 if (enc_utf8 && var != NULL)
1238 {
1239 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001240 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001241
Bram Moolenaar85a20022019-12-21 18:25:54 +01001242 // Convert from active codepage to UTF-8. Other conversions are
1243 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001244 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001245 if (pp != NULL)
1246 {
1247 homedir = pp;
1248 return;
1249 }
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 /*
1253 * Default home dir is C:/
1254 * Best assumption we can make in such a situation.
1255 */
1256 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001257 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (var != NULL)
1261 {
1262#ifdef UNIX
1263 /*
1264 * Change to the directory and get the actual path. This resolves
1265 * links. Don't do it when we can't return.
1266 */
1267 if (mch_dirname(NameBuff, MAXPATHL) == OK
1268 && mch_chdir((char *)NameBuff) == 0)
1269 {
1270 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1271 var = IObuff;
1272 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001273 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275#endif
1276 homedir = vim_strsave(var);
1277 }
1278}
1279
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001280#if defined(EXITFREE) || defined(PROTO)
1281 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001282free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001283{
1284 vim_free(homedir);
1285}
Bram Moolenaar24305862012-08-15 14:05:05 +02001286
Bram Moolenaar24305862012-08-15 14:05:05 +02001287 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001288free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001289{
1290 ga_clear_strings(&ga_users);
1291}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001292#endif
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001295 * Call expand_env() and store the result in an allocated string.
1296 * This is not very memory efficient, this expects the result to be freed
1297 * again soon.
1298 */
1299 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001300expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001301{
1302 return expand_env_save_opt(src, FALSE);
1303}
1304
1305/*
1306 * Idem, but when "one" is TRUE handle the string as one file name, only
1307 * expand "~" at the start.
1308 */
1309 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001310expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001311{
1312 char_u *p;
1313
1314 p = alloc(MAXPATHL);
1315 if (p != NULL)
1316 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1317 return p;
1318}
1319
1320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 * Expand environment variable with path name.
1322 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001323 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 * If anything fails no expansion is done and dst equals src.
1325 */
1326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001327expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001328 char_u *src, // input string e.g. "$HOME/vim.hlp"
1329 char_u *dst, // where to put the result
1330 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001332 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333}
1334
1335 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001336expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001337 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1338 char_u *dst, // where to put the result
1339 int dstlen, // maximum length of the result
1340 int esc, // escape spaces in expanded variables
1341 int one, // "srcp" is one file name
1342 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001344 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *tail;
1346 int c;
1347 char_u *var;
1348 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001349 int mustfree; // var was allocated, need to free it later
1350 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001351 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001353 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001354 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001355
1356 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001357 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 while (*src && dstlen > 0)
1359 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001360#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001361 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001362 if (src[0] == '`' && src[1] == '=')
1363 {
1364 size_t len;
1365
1366 var = src;
1367 src += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02001368 (void)skip_expr(&src, NULL);
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001369 if (*src == '`')
1370 ++src;
1371 len = src - var;
1372 if (len > (size_t)dstlen)
1373 len = dstlen;
1374 vim_strncpy(dst, var, len);
1375 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001376 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001377 continue;
1378 }
1379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001381 if ((*src == '$'
1382#ifdef VMS
1383 && at_start
1384#endif
1385 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001386#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 || *src == '%'
1388#endif
1389 || (*src == '~' && at_start))
1390 {
1391 mustfree = FALSE;
1392
1393 /*
1394 * The variable name is copied into dst temporarily, because it may
1395 * be a string in read-only memory and a NUL needs to be appended.
1396 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001397 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
1399 tail = src + 1;
1400 var = dst;
1401 c = dstlen - 1;
1402
1403#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001404 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (*tail == '{' && !vim_isIDc('{'))
1406 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001407 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 while (c-- > 0 && *tail && *tail != '}')
1409 *var++ = *tail++;
1410 }
1411 else
1412#endif
1413 {
1414 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001415#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 || (*src == '%' && *tail != '%')
1417#endif
1418 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001422#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423# ifdef UNIX
1424 if (src[1] == '{' && *tail != '}')
1425# else
1426 if (*src == '%' && *tail != '%')
1427# endif
1428 var = NULL;
1429 else
1430 {
1431# ifdef UNIX
1432 if (src[1] == '{')
1433# else
1434 if (*src == '%')
1435#endif
1436 ++tail;
1437#endif
1438 *var = NUL;
1439 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001440#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442#endif
1443 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001444 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 else if ( src[1] == NUL
1446 || vim_ispathsep(src[1])
1447 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1448 {
1449 var = homedir;
1450 tail = src + 1;
1451 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001452 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 {
1454#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1455 /*
1456 * Copy ~user to dst[], so we can put a NUL after it.
1457 */
1458 tail = src;
1459 var = dst;
1460 c = dstlen - 1;
1461 while ( c-- > 0
1462 && *tail
1463 && vim_isfilec(*tail)
1464 && !vim_ispathsep(*tail))
1465 *var++ = *tail++;
1466 *var = NUL;
1467# ifdef UNIX
1468 /*
1469 * If the system supports getpwnam(), use it.
1470 * Otherwise, or if getpwnam() fails, the shell is used to
1471 * expand ~user. This is slower and may fail if the shell
1472 * does not support ~user (old versions of /bin/sh).
1473 */
1474# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1475 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001476 // Note: memory allocated by getpwnam() is never freed.
1477 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001478 struct passwd *pw = (*dst == NUL)
1479 ? NULL : getpwnam((char *)dst + 1);
1480
1481 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483 if (var == NULL)
1484# endif
1485 {
1486 expand_T xpc;
1487
1488 ExpandInit(&xpc);
1489 xpc.xp_context = EXPAND_FILES;
1490 var = ExpandOne(&xpc, dst, NULL,
1491 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 mustfree = TRUE;
1493 }
1494
Bram Moolenaar85a20022019-12-21 18:25:54 +01001495# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /*
1497 * USER_HOME is a comma-separated list of
1498 * directories to search for the user account in.
1499 */
1500 {
1501 char_u test[MAXPATHL], paths[MAXPATHL];
1502 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001503 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 STRCPY(paths, USER_HOME);
1506 next_path = paths;
1507 while (*next_path)
1508 {
1509 for (path = next_path; *next_path && *next_path != ',';
1510 next_path++);
1511 if (*next_path)
1512 *next_path++ = NUL;
1513 STRCPY(test, path);
1514 STRCAT(test, "/");
1515 STRCAT(test, dst + 1);
1516 if (mch_stat(test, &st) == 0)
1517 {
1518 var = alloc(STRLEN(test) + 1);
1519 STRCPY(var, test);
1520 mustfree = TRUE;
1521 break;
1522 }
1523 }
1524 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001525# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001527 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001529 tail = (char_u *)""; // for gcc
1530#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532
1533#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001534 // If 'shellslash' is set change backslashes to forward slashes.
1535 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1537 {
1538 char_u *p = vim_strsave(var);
1539
1540 if (p != NULL)
1541 {
1542 if (mustfree)
1543 vim_free(var);
1544 var = p;
1545 mustfree = TRUE;
1546 forward_slash(var);
1547 }
1548 }
1549#endif
1550
Bram Moolenaar85a20022019-12-21 18:25:54 +01001551 // If "var" contains white space, escape it with a backslash.
1552 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1554 {
1555 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1556
1557 if (p != NULL)
1558 {
1559 if (mustfree)
1560 vim_free(var);
1561 var = p;
1562 mustfree = TRUE;
1563 }
1564 }
1565
1566 if (var != NULL && *var != NUL
1567 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1568 {
1569 STRCPY(dst, var);
1570 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001571 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001572 // if var[] ends in a path separator and tail[] starts
1573 // with it, skip a character
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001574 if (after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1576 && dst[-1] != ':'
1577#endif
1578 && vim_ispathsep(*tail))
1579 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001580 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 src = tail;
1582 copy_char = FALSE;
1583 }
1584 if (mustfree)
1585 vim_free(var);
1586 }
1587
Bram Moolenaar85a20022019-12-21 18:25:54 +01001588 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001591 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001592 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1593 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
1595 at_start = FALSE;
1596 if (src[0] == '\\' && src[1] != NUL)
1597 {
1598 *dst++ = *src++;
1599 --dstlen;
1600 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001601 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001603 if (dstlen > 0)
1604 {
1605 *dst++ = *src++;
1606 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001607
Bram Moolenaar1c864092017-08-06 18:15:45 +02001608 if (startstr != NULL && src - startstr_len >= srcp
1609 && STRNCMP(src - startstr_len, startstr,
1610 startstr_len) == 0)
1611 at_start = TRUE;
1612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 *dst = NUL;
1617}
1618
1619/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001620 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1621 * the length of "name/". Otherwise return "pend".
1622 */
1623 static char_u *
1624remove_tail(char_u *p, char_u *pend, char_u *name)
1625{
1626 int len = (int)STRLEN(name) + 1;
1627 char_u *newend = pend - len;
1628
1629 if (newend >= p
1630 && fnamencmp(newend, name, len - 1) == 0
1631 && (newend == p || after_pathsep(p, newend)))
1632 return newend;
1633 return pend;
1634}
1635
1636/*
1637 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1638 * Return NULL if not, return its name in allocated memory otherwise.
1639 */
1640 static char_u *
1641vim_version_dir(char_u *vimdir)
1642{
1643 char_u *p;
1644
1645 if (vimdir == NULL || *vimdir == NUL)
1646 return NULL;
1647 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1648 if (p != NULL && mch_isdir(p))
1649 return p;
1650 vim_free(p);
1651 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1652 if (p != NULL && mch_isdir(p))
1653 return p;
1654 vim_free(p);
1655 return NULL;
1656}
1657
1658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * Vim's version of getenv().
1660 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001661 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001662 * "mustfree" is set to TRUE when returned is allocated, it must be
1663 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 */
1665 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001666vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001668 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 char_u *pend;
1670 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001671#ifdef MSWIN
1672 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001674 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (STRCMP(name, "HOME") == 0)
1676 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001678 // Use Wide function
1679 wn = enc_to_utf16(name, NULL);
1680 if (wn == NULL)
1681 return NULL;
1682
1683 wp = _wgetenv(wn);
1684 vim_free(wn);
1685
1686 if (wp != NULL && *wp == NUL) // empty is the same as not set
1687 wp = NULL;
1688
1689 if (wp != NULL)
1690 {
1691 p = utf16_to_enc(wp, NULL);
1692 if (p == NULL)
1693 return NULL;
1694
1695 *mustfree = TRUE;
1696 return p;
1697 }
1698#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001700 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 p = NULL;
1702
1703 if (p != NULL)
1704 return p;
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001705
1706# ifdef __HAIKU__
1707 // special handling for user settings directory...
1708 if (STRCMP(name, "BE_USER_SETTINGS") == 0)
1709 {
1710 static char userSettingsPath[MAXPATHL];
1711
1712 if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false,
1713 userSettingsPath, MAXPATHL) == B_OK)
1714 return (char_u *)userSettingsPath;
1715 else
1716 return NULL;
1717 }
1718# endif
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001721 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1723 if (!vimruntime && STRCMP(name, "VIM") != 0)
1724 return NULL;
1725
1726 /*
1727 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1728 * Don't do this when default_vimruntime_dir is non-empty.
1729 */
1730 if (vimruntime
1731#ifdef HAVE_PATHDEF
1732 && *default_vimruntime_dir == NUL
1733#endif
1734 )
1735 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001736#ifdef MSWIN
1737 // Use Wide function
1738 wp = _wgetenv(L"VIM");
1739 if (wp != NULL && *wp == NUL) // empty is the same as not set
1740 wp = NULL;
1741 if (wp != NULL)
1742 {
1743 char_u *q = utf16_to_enc(wp, NULL);
1744 if (q != NULL)
1745 {
1746 p = vim_version_dir(q);
1747 *mustfree = TRUE;
1748 if (p == NULL)
1749 p = q;
1750 }
1751 }
1752#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001754 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 p = NULL;
1756 if (p != NULL)
1757 {
1758 p = vim_version_dir(p);
1759 if (p != NULL)
1760 *mustfree = TRUE;
1761 else
1762 p = mch_getenv((char_u *)"VIM");
1763 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766
1767 /*
1768 * When expanding $VIM or $VIMRUNTIME fails, try using:
1769 * - the directory name from 'helpfile' (unless it contains '$')
1770 * - the executable name from argv[0]
1771 */
1772 if (p == NULL)
1773 {
1774 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1775 p = p_hf;
1776#ifdef USE_EXE_NAME
1777 /*
1778 * Use the name of the executable, obtained from argv[0].
1779 */
1780 else
1781 p = exe_name;
1782#endif
1783 if (p != NULL)
1784 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001785 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 pend = gettail(p);
1787
Bram Moolenaar85a20022019-12-21 18:25:54 +01001788 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (p == p_hf)
1790 pend = remove_tail(p, pend, (char_u *)"doc");
1791
1792#ifdef USE_EXE_NAME
1793# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001794 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (p == exe_name)
1796 {
1797 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001798 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001800 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1801 if (pend1 != pend)
1802 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001803 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001804 if (pnew != NULL)
1805 {
1806 STRNCPY(pnew, p, (pend1 - p));
1807 STRCPY(pnew + (pend1 - p), "Resources/vim");
1808 p = pnew;
1809 pend = p + STRLEN(p);
1810 }
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001814 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (p == exe_name)
1816 pend = remove_tail(p, pend, (char_u *)"src");
1817#endif
1818
Bram Moolenaar85a20022019-12-21 18:25:54 +01001819 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (!vimruntime)
1821 {
1822 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1823 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1824 }
1825
Bram Moolenaar85a20022019-12-21 18:25:54 +01001826 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001827 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001830#ifdef MACOS_X
1831 if (p == exe_name || p == p_hf)
1832#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001833 // check that the result is a directory name
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001834 p = vim_strnsave(p, pend - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001837 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 else
1839 {
1840#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001841 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1843 {
1844 vim_free(p);
1845 p = pend;
1846 }
1847#endif
1848 *mustfree = TRUE;
1849 }
1850 }
1851 }
1852
1853#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001854 // When there is a pathdef.c file we can use default_vim_dir and
1855 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (p == NULL)
1857 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001858 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (vimruntime && *default_vimruntime_dir != NUL)
1860 {
1861 p = default_vimruntime_dir;
1862 *mustfree = FALSE;
1863 }
1864 else if (*default_vim_dir != NUL)
1865 {
1866 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1867 *mustfree = TRUE;
1868 else
1869 {
1870 p = default_vim_dir;
1871 *mustfree = FALSE;
1872 }
1873 }
1874 }
1875#endif
1876
1877 /*
1878 * Set the environment variable, so that the new value can be found fast
1879 * next time, and others can also use it (e.g. Perl).
1880 */
1881 if (p != NULL)
1882 {
1883 if (vimruntime)
1884 {
1885 vim_setenv((char_u *)"VIMRUNTIME", p);
1886 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 else
1889 {
1890 vim_setenv((char_u *)"VIM", p);
1891 didset_vim = TRUE;
1892 }
1893 }
1894 return p;
1895}
1896
Bram Moolenaar137374f2018-05-13 15:59:50 +02001897 void
1898vim_unsetenv(char_u *var)
1899{
1900#ifdef HAVE_UNSETENV
1901 unsetenv((char *)var);
1902#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001903 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001904#endif
1905}
1906
LemonBoy77142312022-04-15 20:50:46 +01001907/*
1908 * Removes environment variable "name" and take care of side effects.
1909 */
1910 void
1911vim_unsetenv_ext(char_u *var)
1912{
1913 vim_unsetenv(var);
1914
1915 // "homedir" is not cleared, keep using the old value until $HOME is set.
1916 if (STRICMP(var, "VIM") == 0)
1917 didset_vim = FALSE;
1918 else if (STRICMP(var, "VIMRUNTIME") == 0)
1919 didset_vimruntime = FALSE;
1920}
Bram Moolenaar137374f2018-05-13 15:59:50 +02001921
Bram Moolenaar092e09c2022-04-15 23:29:23 +01001922#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001924 * Set environment variable "name" and take care of side effects.
1925 */
1926 void
1927vim_setenv_ext(char_u *name, char_u *val)
1928{
1929 vim_setenv(name, val);
1930 if (STRICMP(name, "HOME") == 0)
1931 init_homedir();
1932 else if (didset_vim && STRICMP(name, "VIM") == 0)
1933 didset_vim = FALSE;
LemonBoy77142312022-04-15 20:50:46 +01001934 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001935 didset_vimruntime = FALSE;
1936}
Dominique Pelle748b3082022-01-08 12:41:16 +00001937#endif
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001938
1939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 * Our portable version of setenv.
1941 */
1942 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001943vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944{
1945#ifdef HAVE_SETENV
1946 mch_setenv((char *)name, (char *)val, 1);
1947#else
1948 char_u *envbuf;
1949
1950 /*
1951 * Putenv does not copy the string, it has to remain
1952 * valid. The allocated memory will never be freed.
1953 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001954 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (envbuf != NULL)
1956 {
1957 sprintf((char *)envbuf, "%s=%s", name, val);
1958 putenv((char *)envbuf);
1959 }
1960#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001961#ifdef FEAT_GETTEXT
1962 /*
1963 * When setting $VIMRUNTIME adjust the directory to find message
1964 * translations to $VIMRUNTIME/lang.
1965 */
1966 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1967 {
1968 char_u *buf = concat_str(val, (char_u *)"/lang");
1969
1970 if (buf != NULL)
1971 {
1972 bindtextdomain(VIMPACKAGE, (char *)buf);
1973 vim_free(buf);
1974 }
1975 }
1976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977}
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979/*
1980 * Function given to ExpandGeneric() to obtain an environment variable name.
1981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001983get_env_name(
1984 expand_T *xp UNUSED,
1985 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
Bram Moolenaard0573012017-10-28 21:11:06 +02001987# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02001989 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
1991 return NULL;
1992# else
1993# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01001994 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 extern char **environ;
1996# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001997# define ENVNAMELEN 100
1998 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 char_u *str;
2000 int n;
2001
2002 str = (char_u *)environ[idx];
2003 if (str == NULL)
2004 return NULL;
2005
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002006 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 if (str[n] == '=' || str[n] == NUL)
2009 break;
2010 name[n] = str[n];
2011 }
2012 name[n] = NUL;
2013 return name;
2014# endif
2015}
Bram Moolenaar24305862012-08-15 14:05:05 +02002016
2017/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002018 * Add a user name to the list of users in ga_users.
2019 * Do nothing if user name is NULL or empty.
2020 */
2021 static void
2022add_user(char_u *user, int need_copy)
2023{
2024 char_u *user_copy = (user != NULL && need_copy)
2025 ? vim_strsave(user) : user;
2026
2027 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2028 {
2029 if (need_copy)
2030 vim_free(user);
2031 return;
2032 }
2033 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2034}
2035
2036/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002037 * Find all user names for user completion.
2038 * Done only once and then cached.
2039 */
2040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002041init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002042{
Bram Moolenaar24305862012-08-15 14:05:05 +02002043 static int lazy_init_done = FALSE;
2044
2045 if (lazy_init_done)
2046 return;
2047
2048 lazy_init_done = TRUE;
2049 ga_init2(&ga_users, sizeof(char_u *), 20);
2050
2051# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2052 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002053 struct passwd* pw;
2054
2055 setpwent();
2056 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002057 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002058 endpwent();
2059 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002060# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002061 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002062 DWORD nusers = 0, ntotal = 0, i;
2063 PUSER_INFO_0 uinfo;
2064
2065 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2066 &nusers, &ntotal, NULL) == NERR_Success)
2067 {
2068 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002069 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002070
2071 NetApiBufferFree(uinfo);
2072 }
2073 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002074# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002075# if defined(HAVE_GETPWNAM)
2076 {
2077 char_u *user_env = mch_getenv((char_u *)"USER");
2078
2079 // The $USER environment variable may be a valid remote user name (NIS,
2080 // LDAP) not already listed by getpwent(), as getpwent() only lists
2081 // local user names. If $USER is not already listed, check whether it
2082 // is a valid remote user name using getpwnam() and if it is, add it to
2083 // the list of user names.
2084
2085 if (user_env != NULL && *user_env != NUL)
2086 {
2087 int i;
2088
2089 for (i = 0; i < ga_users.ga_len; i++)
2090 {
2091 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2092
2093 if (STRCMP(local_user, user_env) == 0)
2094 break;
2095 }
2096
2097 if (i == ga_users.ga_len)
2098 {
2099 struct passwd *pw = getpwnam((char *)user_env);
2100
2101 if (pw != NULL)
2102 add_user((char_u *)pw->pw_name, TRUE);
2103 }
2104 }
2105 }
2106# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002107}
2108
2109/*
2110 * Function given to ExpandGeneric() to obtain an user names.
2111 */
2112 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002113get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002114{
2115 init_users();
2116 if (idx < ga_users.ga_len)
2117 return ((char_u **)ga_users.ga_data)[idx];
2118 return NULL;
2119}
2120
2121/*
2122 * Check whether name matches a user name. Return:
2123 * 0 if name does not match any user name.
2124 * 1 if name partially matches the beginning of a user name.
2125 * 2 is name fully matches a user name.
2126 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002127 int
2128match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002129{
2130 int i;
2131 int n = (int)STRLEN(name);
2132 int result = 0;
2133
2134 init_users();
2135 for (i = 0; i < ga_users.ga_len; i++)
2136 {
2137 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002138 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002139 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002140 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002141 }
2142 return result;
2143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002145 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002146prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002148#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002149 // Ignore SIGHUP, because a dropped connection causes a read error, which
2150 // makes Vim exit and then handling SIGHUP causes various reentrance
2151 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002152 signal(SIGHUP, SIG_IGN);
2153#endif
2154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#ifdef FEAT_GUI
2156 if (gui.in_use)
2157 {
2158 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002159 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161 else
2162#endif
2163 {
2164 windgoto((int)Rows - 1, 0);
2165
2166 /*
2167 * Switch terminal mode back now, so messages end up on the "normal"
2168 * screen (if there are two screens).
2169 */
2170 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002171 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 out_flush();
2173 }
2174}
2175
2176/*
2177 * Preserve files and exit.
2178 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002179 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2180 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 */
2182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002183preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
2185 buf_T *buf;
2186
2187 prepare_to_exit();
2188
Bram Moolenaar85a20022019-12-21 18:25:54 +01002189 // Setting this will prevent free() calls. That avoids calling free()
2190 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002191 really_exiting = TRUE;
2192
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002194 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 out_flush();
2196
Bram Moolenaar85a20022019-12-21 18:25:54 +01002197 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
Bram Moolenaar29323592016-07-24 22:04:11 +02002199 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
2201 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2202 {
Bram Moolenaar69212b12020-05-10 14:14:03 +02002203 OUT_STR("Vim: preserving files...\r\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002206 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 break;
2208 }
2209 }
2210
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
Bram Moolenaar69212b12020-05-10 14:14:03 +02002213 OUT_STR("Vim: Finished.\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 getout(1);
2216}
2217
2218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * Check for CTRL-C pressed, but only once in a while.
2220 * Should be used instead of ui_breakcheck() for functions that check for
2221 * each line in the file. Calling ui_breakcheck() each time takes too much
2222 * time, because it can be a system call.
2223 */
2224
2225#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002226# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227#endif
2228
2229static int breakcheck_count = 0;
2230
2231 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002232line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 if (++breakcheck_count >= BREAKCHECK_SKIP)
2235 {
2236 breakcheck_count = 0;
2237 ui_breakcheck();
2238 }
2239}
2240
2241/*
2242 * Like line_breakcheck() but check 10 times less often.
2243 */
2244 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002245fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2248 {
2249 breakcheck_count = 0;
2250 ui_breakcheck();
2251 }
2252}
2253
Dominique Pelle748b3082022-01-08 12:41:16 +00002254# if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002255/*
2256 * Like line_breakcheck() but check 100 times less often.
2257 */
2258 void
2259veryfast_breakcheck(void)
2260{
2261 if (++breakcheck_count >= BREAKCHECK_SKIP * 100)
2262 {
2263 breakcheck_count = 0;
2264 ui_breakcheck();
2265 }
2266}
Dominique Pelle748b3082022-01-08 12:41:16 +00002267#endif
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002268
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002269#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2270 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2271 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273#ifndef SEEK_SET
2274# define SEEK_SET 0
2275#endif
2276#ifndef SEEK_END
2277# define SEEK_END 2
2278#endif
2279
2280/*
2281 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002282 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2283 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 * Returns an allocated string, or NULL for error.
2285 */
2286 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287get_cmd_output(
2288 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002289 char_u *infile, // optional input file name
2290 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 char_u *tempname;
2294 char_u *command;
2295 char_u *buffer = NULL;
2296 int len;
2297 int i = 0;
2298 FILE *fd;
2299
2300 if (check_restricted() || check_secure())
2301 return NULL;
2302
Bram Moolenaar85a20022019-12-21 18:25:54 +01002303 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002304 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002306 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 return NULL;
2308 }
2309
Bram Moolenaar85a20022019-12-21 18:25:54 +01002310 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002311 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (command == NULL)
2313 goto done;
2314
2315 /*
2316 * Call the shell to execute the command (errors are ignored).
2317 * Don't check timestamps here.
2318 */
2319 ++no_check_timestamps;
2320 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2321 --no_check_timestamps;
2322
2323 vim_free(command);
2324
2325 /*
2326 * read the names from the file into memory
2327 */
2328# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002329 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 fd = mch_fopen((char *)tempname, "r");
2331# else
2332 fd = mch_fopen((char *)tempname, READBIN);
2333# endif
2334
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002335 // Not being able to seek means we can't read the file.
2336 if (fd == NULL
2337 || fseek(fd, 0L, SEEK_END) == -1
2338 || (len = ftell(fd)) == -1 // get size of temp file
2339 || fseek(fd, 0L, SEEK_SET) == -1) // back to the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaara9549c92022-04-17 14:18:11 +01002341 semsg(_(e_cannot_read_from_str_2), tempname);
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002342 if (fd != NULL)
2343 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 goto done;
2345 }
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 buffer = alloc(len + 1);
2348 if (buffer != NULL)
2349 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2350 fclose(fd);
2351 mch_remove(tempname);
2352 if (buffer == NULL)
2353 goto done;
2354#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002355 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
2357 if (i != len)
2358 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002359 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002360 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002362 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002363 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002364 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002365 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002366 if (buffer[i] == NUL)
2367 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002368
Bram Moolenaar85a20022019-12-21 18:25:54 +01002369 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002370 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002371 else
2372 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374done:
2375 vim_free(tempname);
2376 return buffer;
2377}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002378
2379# if defined(FEAT_EVAL) || defined(PROTO)
2380
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002381 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002382get_cmd_output_as_rettv(
2383 typval_T *argvars,
2384 typval_T *rettv,
2385 int retlist)
2386{
2387 char_u *res = NULL;
2388 char_u *p;
2389 char_u *infile = NULL;
2390 int err = FALSE;
2391 FILE *fd;
2392 list_T *list = NULL;
2393 int flags = SHELL_SILENT;
2394
2395 rettv->v_type = VAR_STRING;
2396 rettv->vval.v_string = NULL;
2397 if (check_restricted() || check_secure())
2398 goto errret;
2399
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002400 if (in_vim9script()
2401 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +02002402 || check_for_opt_string_or_number_or_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002403 return;
2404
Bram Moolenaar26262f82019-09-04 20:59:15 +02002405 if (argvars[1].v_type != VAR_UNKNOWN)
2406 {
2407 /*
2408 * Write the text to a temp file, to be used for input of the shell
2409 * command.
2410 */
2411 if ((infile = vim_tempname('i', TRUE)) == NULL)
2412 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002413 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002414 goto errret;
2415 }
2416
2417 fd = mch_fopen((char *)infile, WRITEBIN);
2418 if (fd == NULL)
2419 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002420 semsg(_(e_cant_open_file_str), infile);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002421 goto errret;
2422 }
2423 if (argvars[1].v_type == VAR_NUMBER)
2424 {
2425 linenr_T lnum;
2426 buf_T *buf;
2427
2428 buf = buflist_findnr(argvars[1].vval.v_number);
2429 if (buf == NULL)
2430 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002431 semsg(_(e_buffer_nr_does_not_exist), argvars[1].vval.v_number);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002432 fclose(fd);
2433 goto errret;
2434 }
2435
2436 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2437 {
2438 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2439 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2440 {
2441 err = TRUE;
2442 break;
2443 }
2444 if (putc(NL, fd) == EOF)
2445 {
2446 err = TRUE;
2447 break;
2448 }
2449 }
2450 }
2451 else if (argvars[1].v_type == VAR_LIST)
2452 {
2453 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2454 err = TRUE;
2455 }
2456 else
2457 {
2458 size_t len;
2459 char_u buf[NUMBUFLEN];
2460
2461 p = tv_get_string_buf_chk(&argvars[1], buf);
2462 if (p == NULL)
2463 {
2464 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002465 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002466 }
2467 len = STRLEN(p);
2468 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2469 err = TRUE;
2470 }
2471 if (fclose(fd) != 0)
2472 err = TRUE;
2473 if (err)
2474 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002475 emsg(_(e_error_writing_temp_file));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002476 goto errret;
2477 }
2478 }
2479
Bram Moolenaar85a20022019-12-21 18:25:54 +01002480 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2481 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002482 if (!msg_silent)
2483 flags += SHELL_COOKED;
2484
2485 if (retlist)
2486 {
2487 int len;
2488 listitem_T *li;
2489 char_u *s = NULL;
2490 char_u *start;
2491 char_u *end;
2492 int i;
2493
2494 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2495 if (res == NULL)
2496 goto errret;
2497
2498 list = list_alloc();
2499 if (list == NULL)
2500 goto errret;
2501
2502 for (i = 0; i < len; ++i)
2503 {
2504 start = res + i;
2505 while (i < len && res[i] != NL)
2506 ++i;
2507 end = res + i;
2508
2509 s = alloc(end - start + 1);
2510 if (s == NULL)
2511 goto errret;
2512
2513 for (p = s; start < end; ++p, ++start)
2514 *p = *start == NUL ? NL : *start;
2515 *p = NUL;
2516
2517 li = listitem_alloc();
2518 if (li == NULL)
2519 {
2520 vim_free(s);
2521 goto errret;
2522 }
2523 li->li_tv.v_type = VAR_STRING;
2524 li->li_tv.v_lock = 0;
2525 li->li_tv.vval.v_string = s;
2526 list_append(list, li);
2527 }
2528
2529 rettv_list_set(rettv, list);
2530 list = NULL;
2531 }
2532 else
2533 {
2534 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2535#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002537 if (res != NULL)
2538 {
2539 char_u *s, *d;
2540
2541 d = res;
2542 for (s = res; *s; ++s)
2543 {
2544 if (s[0] == CAR && s[1] == NL)
2545 ++s;
2546 *d++ = *s;
2547 }
2548 *d = NUL;
2549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002551 rettv->vval.v_string = res;
2552 res = NULL;
2553 }
2554
2555errret:
2556 if (infile != NULL)
2557 {
2558 mch_remove(infile);
2559 vim_free(infile);
2560 }
2561 if (res != NULL)
2562 vim_free(res);
2563 if (list != NULL)
2564 list_free(list);
2565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
2567/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002568 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
2570 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002571f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002573 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574}
2575
2576/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002577 * "systemlist()" function
2578 */
2579 void
2580f_systemlist(typval_T *argvars, typval_T *rettv)
2581{
2582 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2583}
2584# endif // FEAT_EVAL
2585
2586#endif
2587
2588/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002589 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 * Don't do this when still processing a command or a mapping.
2591 * Don't do this when inside a ":normal" command.
2592 */
2593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002594goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 return (p_im && stuff_empty() && typebuf_typed());
2597}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002598
2599/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002600 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002601 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2602 * - Remove any argument. E.g., "csh -f" -> "csh".
2603 * But don't allow a space in the path, so that this works:
2604 * "/usr/bin/csh --rcfile ~/.cshrc"
2605 * But don't do that for Windows, it's common to have a space in the path.
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002606 * Returns NULL when out of memory.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002607 */
2608 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002609get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002610{
2611 char_u *p;
2612
Bram Moolenaar4f974752019-02-17 17:44:42 +01002613#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002614 p = gettail(p_sh);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002615 p = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002616#else
2617 p = skiptowhite(p_sh);
2618 if (*p == NUL)
2619 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002620 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002621 p = vim_strsave(gettail(p_sh));
2622 }
2623 else
2624 {
2625 char_u *p1, *p2;
2626
Bram Moolenaar85a20022019-12-21 18:25:54 +01002627 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002628 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002629 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002630 if (vim_ispathsep(*p2))
2631 p1 = p2 + 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002632 p = vim_strnsave(p1, p - p1);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002633 }
2634#endif
2635 return p;
2636}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002637
2638/*
2639 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2640 * Also check for ":\\", which MS Internet Explorer accepts, return
2641 * URL_BACKSLASH.
2642 */
2643 int
2644path_is_url(char_u *p)
2645{
2646 if (STRNCMP(p, "://", (size_t)3) == 0)
2647 return URL_SLASH;
2648 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2649 return URL_BACKSLASH;
2650 return 0;
2651}
2652
2653/*
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002654 * Check if "fname" starts with "name://" or "name:\\".
2655 * Return URL_SLASH for "name://", URL_BACKSLASH for "name:\\".
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002656 * Return zero otherwise.
2657 */
2658 int
2659path_with_url(char_u *fname)
2660{
2661 char_u *p;
2662
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002663 // We accept alphabetic characters and a dash in scheme part.
2664 // RFC 3986 allows for more, but it increases the risk of matching
2665 // non-URL text.
2666
2667 // first character must be alpha
2668 if (!isalpha(*fname))
2669 return 0;
2670
2671 // check body: alpha or dash
itchyny94e7d342021-10-21 18:01:13 +01002672 for (p = fname + 1; (isalpha(*p) || (*p == '-')); ++p)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002673 ;
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002674
2675 // check last char is not a dash
2676 if (p[-1] == '-')
2677 return 0;
2678
2679 // "://" or ":\\" must follow
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002680 return path_is_url(p);
2681}
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002682
Bram Moolenaar3075a452021-11-17 15:51:52 +00002683#if defined(FEAT_EVAL) || defined(PROTO)
2684/*
2685 * Return the dictionary of v:event.
2686 * Save and clear the value in case it already has items.
2687 */
2688 dict_T *
2689get_v_event(save_v_event_T *sve)
2690{
2691 dict_T *v_event = get_vim_var_dict(VV_EVENT);
2692
2693 if (v_event->dv_hashtab.ht_used > 0)
2694 {
2695 // recursive use of v:event, save, make empty and restore later
2696 sve->sve_did_save = TRUE;
2697 sve->sve_hashtab = v_event->dv_hashtab;
2698 hash_init(&v_event->dv_hashtab);
2699 }
2700 else
2701 sve->sve_did_save = FALSE;
2702 return v_event;
2703}
2704
2705 void
2706restore_v_event(dict_T *v_event, save_v_event_T *sve)
2707{
2708 dict_free_contents(v_event);
2709 if (sve->sve_did_save)
2710 v_event->dv_hashtab = sve->sve_hashtab;
2711 else
2712 hash_init(&v_event->dv_hashtab);
2713}
2714#endif
2715
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002716/*
LemonBoy2bf52dd2022-04-09 18:17:34 +01002717 * Fires a ModeChanged autocmd event if appropriate.
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002718 */
2719 void
LemonBoy2bf52dd2022-04-09 18:17:34 +01002720may_trigger_modechanged()
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002721{
Bram Moolenaar3075a452021-11-17 15:51:52 +00002722#ifdef FEAT_EVAL
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002723 dict_T *v_event;
Bram Moolenaar3075a452021-11-17 15:51:52 +00002724 save_v_event_T save_v_event;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002725 char_u curr_mode[MODE_MAX_LENGTH];
2726 char_u pattern_buf[2 * MODE_MAX_LENGTH];
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002727
2728 if (!has_modechanged())
2729 return;
2730
LemonBoy2bf52dd2022-04-09 18:17:34 +01002731 get_mode(curr_mode);
2732 if (STRCMP(curr_mode, last_mode) == 0)
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002733 return;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002734
Bram Moolenaar3075a452021-11-17 15:51:52 +00002735 v_event = get_v_event(&save_v_event);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002736 (void)dict_add_string(v_event, "new_mode", curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002737 (void)dict_add_string(v_event, "old_mode", last_mode);
2738 dict_set_items_ro(v_event);
2739
2740 // concatenate modes in format "old_mode:new_mode"
LemonBoy2bf52dd2022-04-09 18:17:34 +01002741 vim_snprintf((char *)pattern_buf, sizeof(pattern_buf), "%s:%s", last_mode,
2742 curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002743
LemonBoy2bf52dd2022-04-09 18:17:34 +01002744 apply_autocmds(EVENT_MODECHANGED, pattern_buf, NULL, FALSE, curbuf);
2745 STRCPY(last_mode, curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002746
Bram Moolenaar3075a452021-11-17 15:51:52 +00002747 restore_v_event(v_event, &save_v_event);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002748#endif
2749}