blob: 019bbaf8dd327c0bc094b862d07e35a1a8534513 [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".
Bram Moolenaar61c4b042022-10-18 15:10:11 +0100333 * Includes any filler lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100336plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
338 return plines_win(curwin, lnum, TRUE);
339}
340
341 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100342plines_win(
343 win_T *wp,
344 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100345 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346{
347#if defined(FEAT_DIFF) || defined(PROTO)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100348 // Check for filler lines above this buffer line. When folded the result
349 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
351}
352
Bram Moolenaar61c4b042022-10-18 15:10:11 +0100353/*
354 * Return the number of window lines occupied by buffer line "lnum".
355 * Does not include filler lines.
356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100358plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
360 return plines_win_nofill(curwin, lnum, TRUE);
361}
362
363 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100364plines_win_nofill(
365 win_T *wp,
366 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100367 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368{
369#endif
370 int lines;
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 if (wp->w_width == 0)
373 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_FOLDING
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000376 // Folded lines are handled just like an empty line.
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // NOTE: Caller must handle lines that are MAYBE folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 if (lineFolded(wp, lnum) == TRUE)
379 return 1;
380#endif
381
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100382 if (!wp->w_p_wrap)
383 lines = 1
384#ifdef FEAT_PROP_POPUP
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100385 // add a line for each "above" and "below" aligned text property
386 + prop_count_above_below(wp->w_buffer, lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100387#endif
388 ;
389 else
390 lines = plines_win_nofold(wp, lnum);
391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 if (winheight > 0 && lines > wp->w_height)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000393 return wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 return lines;
395}
396
397/*
398 * Return number of window lines physical line "lnum" will occupy in window
399 * "wp". Does not care about folding, 'wrap' or 'diff'.
400 */
401 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100402plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
404 char_u *s;
405 long col;
406 int width;
Bram Moolenaar49a90792022-08-09 18:25:23 +0100407 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar49a90792022-08-09 18:25:23 +0100410 init_chartabsize_arg(&cts, wp, lnum, 0, s, s);
411 if (*s == NUL
412#ifdef FEAT_PROP_POPUP
413 && !cts.cts_has_prop_with_text
414#endif
415 )
416 return 1; // be quick for an empty line
417 win_linetabsize_cts(&cts, (colnr_T)MAXCOL);
418 clear_chartabsize_arg(&cts);
419 col = (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100421 // If list mode is on, then the '$' at the end of the line may take up one
422 // extra column.
Bram Moolenaareed9d462021-02-15 20:38:25 +0100423 if (wp->w_p_list && wp->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 col += 1;
425
426 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200427 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200429 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 if (width <= 0)
431 return 32000;
432 if (col <= width)
433 return 1;
434 col -= width;
435 width += win_col_off2(wp);
436 return (col + (width - 1)) / width + 1;
437}
438
439/*
440 * Like plines_win(), but only reports the number of physical screen lines
441 * used from the start of the line to the given column number.
442 */
443 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100444plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
446 long col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 int lines = 0;
448 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200449 char_u *line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100450 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 // Check for filler lines above this buffer line. When folded the result
454 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 lines = diff_check_fill(wp, lnum);
456#endif
457
458 if (!wp->w_p_wrap)
459 return lines + 1;
460
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 if (wp->w_width == 0)
462 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100464 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100466 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
467 while (*cts.cts_ptr != NUL && --column >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100469 cts.cts_vcol += win_lbr_chartabsize(&cts, NULL);
470 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 }
472
473 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100474 * If *cts.cts_ptr is a TAB, and the TAB is not displayed as ^I, and we're
475 * not in MODE_INSERT state, then col must be adjusted so that it
476 * represents the last screen position of the TAB. This only fixes an
477 * error when the TAB wraps from one screen line to the next (when
478 * 'columns' is not a multiple of 'ts') -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100480 col = cts.cts_vcol;
481 if (*cts.cts_ptr == TAB && (State & MODE_NORMAL)
Bram Moolenaar24959102022-05-07 20:01:16 +0100482 && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100483 col += win_lbr_chartabsize(&cts, NULL) - 1;
484 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
486 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200487 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200489 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +0000490 if (width <= 0)
491 return 9999;
492
493 lines += 1;
494 if (col > width)
495 lines += (col - width) / (width + win_col_off2(wp)) + 1;
496 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497}
498
499 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100500plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 int count = 0;
503
504 while (first <= last)
505 {
506#ifdef FEAT_FOLDING
507 int x;
508
Bram Moolenaar85a20022019-12-21 18:25:54 +0100509 // Check if there are any really folded lines, but also included lines
510 // that are maybe folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 x = foldedCount(wp, first, NULL);
512 if (x > 0)
513 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100514 ++count; // count 1 for "+-- folded" line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 first += x;
516 }
517 else
518#endif
519 {
520#ifdef FEAT_DIFF
521 if (first == wp->w_topline)
522 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
523 else
524#endif
525 count += plines_win(wp, first, TRUE);
526 ++first;
527 }
528 }
529 return (count);
530}
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100533gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100535 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
Bram Moolenaar85a20022019-12-21 18:25:54 +0100537 // When searching columns is sometimes put at the end of a line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100538 if (pos->col == MAXCOL)
539 return NUL;
540 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (has_mbyte)
542 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 return (int)*ptr;
544}
545
546 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (has_mbyte)
550 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 return (int)*ml_get_cursor();
552}
553
554/*
555 * Write a character at the current cursor position.
556 * It is directly written into the block.
557 */
558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100559pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
562 + curwin->w_cursor.col) = c;
563}
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 * Skip to next part of an option argument: Skip space and comma.
567 */
568 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100569skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 if (*p == ',')
572 ++p;
573 while (*p == ' ')
574 ++p;
575 return p;
576}
577
578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 * check_status: called when the status bars for the buffer 'buf'
580 * need to be updated
581 */
582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100583check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 win_T *wp;
586
Bram Moolenaar29323592016-07-24 22:04:11 +0200587 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (wp->w_buffer == buf && wp->w_status_height)
589 {
590 wp->w_redr_status = TRUE;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100591 set_must_redraw(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
595/*
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100596 * Ask for a reply from the user, a 'y' or a 'n', with prompt "str" (which
597 * should have been translated already).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * No other characters are accepted, the message is repeated until a valid
599 * reply is entered or CTRL-C is hit.
600 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
601 * from any buffers but directly from the user.
602 *
603 * return the 'y' or 'n'
604 */
605 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100606ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 int r = ' ';
609 int save_State = State;
610
Bram Moolenaar85a20022019-12-21 18:25:54 +0100611 if (exiting) // put terminal in raw mode for this question
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 settmode(TMODE_RAW);
613 ++no_wait_return;
614#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200615 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100617 State = MODE_CONFIRM; // mouse behaves like with :confirm
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200618 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200620 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 while (r != 'y' && r != 'n')
623 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100624 // same highlighting as for wait_return()
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100625 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (direct)
627 r = get_keystroke();
628 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000629 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (r == Ctrl_C || r == ESC)
631 r = 'n';
Bram Moolenaar85a20022019-12-21 18:25:54 +0100632 msg_putchar(r); // show what you typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 out_flush();
634 }
635 --no_wait_return;
636 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 --no_mapping;
639 --allow_keys;
640
641 return r;
642}
643
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200644#if defined(FEAT_EVAL) || defined(PROTO)
645
646/*
LemonBoy2bf52dd2022-04-09 18:17:34 +0100647 * Returns the current mode as a string in "buf[MODE_MAX_LENGTH]", NUL
648 * terminated.
649 * The first character represents the major mode, the following ones the minor
650 * ones.
651 */
652 void
653get_mode(char_u *buf)
654{
655 int i = 0;
656
657 if (time_for_testing == 93784)
658 {
659 // Testing the two-character code.
660 buf[i++] = 'x';
661 buf[i++] = '!';
662 }
663#ifdef FEAT_TERMINAL
664 else if (term_use_loop())
665 buf[i++] = 't';
666#endif
667 else if (VIsual_active)
668 {
669 if (VIsual_select)
670 buf[i++] = VIsual_mode + 's' - 'v';
671 else
672 {
673 buf[i++] = VIsual_mode;
674 if (restart_VIsual_select)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100675 buf[i++] = 's';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100676 }
677 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100678 else if (State == MODE_HITRETURN || State == MODE_ASKMORE
679 || State == MODE_SETWSIZE
680 || State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100681 {
682 buf[i++] = 'r';
Bram Moolenaar24959102022-05-07 20:01:16 +0100683 if (State == MODE_ASKMORE)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100684 buf[i++] = 'm';
Bram Moolenaar24959102022-05-07 20:01:16 +0100685 else if (State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100686 buf[i++] = '?';
687 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100688 else if (State == MODE_EXTERNCMD)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100689 buf[i++] = '!';
Bram Moolenaar24959102022-05-07 20:01:16 +0100690 else if (State & MODE_INSERT)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100691 {
692 if (State & VREPLACE_FLAG)
693 {
694 buf[i++] = 'R';
695 buf[i++] = 'v';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100696 }
697 else
698 {
699 if (State & REPLACE_FLAG)
700 buf[i++] = 'R';
701 else
702 buf[i++] = 'i';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100703 }
zeertzjq590f3652022-04-29 11:29:54 +0100704
705 if (ins_compl_active())
706 buf[i++] = 'c';
707 else if (ctrl_x_mode_not_defined_yet())
708 buf[i++] = 'x';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100709 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100710 else if ((State & MODE_CMDLINE) || exmode_active)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100711 {
712 buf[i++] = 'c';
713 if (exmode_active == EXMODE_VIM)
714 buf[i++] = 'v';
715 else if (exmode_active == EXMODE_NORMAL)
716 buf[i++] = 'e';
717 }
718 else
719 {
720 buf[i++] = 'n';
721 if (finish_op)
722 {
723 buf[i++] = 'o';
724 // to be able to detect force-linewise/blockwise/characterwise
725 // operations
726 buf[i++] = motion_force;
727 }
728 else if (restart_edit == 'I' || restart_edit == 'R'
729 || restart_edit == 'V')
730 {
731 buf[i++] = 'i';
732 buf[i++] = restart_edit;
733 }
734#ifdef FEAT_TERMINAL
735 else if (term_in_normal_mode())
736 buf[i++] = 't';
737#endif
738 }
739
740 buf[i] = NUL;
741}
742
743/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200744 * "mode()" function
745 */
746 void
747f_mode(typval_T *argvars, typval_T *rettv)
748{
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200749 char_u buf[MODE_MAX_LENGTH];
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200750
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200751 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
752 return;
753
LemonBoy2bf52dd2022-04-09 18:17:34 +0100754 get_mode(buf);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200755
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 // Clear out the minor mode when the argument is not a non-zero number or
757 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200758 if (!non_zero_arg(&argvars[0]))
759 buf[1] = NUL;
760
761 rettv->vval.v_string = vim_strsave(buf);
762 rettv->v_type = VAR_STRING;
763}
764
765 static void
766may_add_state_char(garray_T *gap, char_u *include, int c)
767{
768 if (include == NULL || vim_strchr(include, c) != NULL)
769 ga_append(gap, c);
770}
771
772/*
773 * "state()" function
774 */
775 void
776f_state(typval_T *argvars, typval_T *rettv)
777{
778 garray_T ga;
779 char_u *include = NULL;
780 int i;
781
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200782 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
783 return;
784
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200785 ga_init2(&ga, 1, 20);
786 if (argvars[0].v_type != VAR_UNKNOWN)
787 include = tv_get_string(&argvars[0]);
788
789 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
790 may_add_state_char(&ga, include, 'm');
791 if (op_pending())
792 may_add_state_char(&ga, include, 'o');
793 if (autocmd_busy)
794 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200795 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200796 may_add_state_char(&ga, include, 'a');
797
798# ifdef FEAT_JOB_CHANNEL
799 if (channel_in_blocking_wait())
800 may_add_state_char(&ga, include, 'w');
801# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200802 if (!get_was_safe_state())
803 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200804 for (i = 0; i < get_callback_depth() && i < 3; ++i)
805 may_add_state_char(&ga, include, 'c');
806 if (msg_scrolled > 0)
807 may_add_state_char(&ga, include, 's');
808
809 rettv->v_type = VAR_STRING;
810 rettv->vval.v_string = ga.ga_data;
811}
812
813#endif // FEAT_EVAL
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Get a key stroke directly from the user.
817 * Ignores mouse clicks and scrollbar events, except a click for the left
818 * button (used at the more prompt).
819 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
820 * Disadvantage: typeahead is ignored.
821 * Translates the interrupt character for unix to ESC.
822 */
823 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100824get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100826 char_u *buf = NULL;
827 int buflen = 150;
828 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 int len = 0;
830 int n;
831 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000832 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar85a20022019-12-21 18:25:54 +0100834 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 for (;;)
836 {
837 cursor_on();
838 out_flush();
839
Bram Moolenaar85a20022019-12-21 18:25:54 +0100840 // Leave some room for check_termcode() to insert a key code into (max
841 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
842 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100843 maxlen = (buflen - 6 - len) / 3;
844 if (buf == NULL)
845 buf = alloc(buflen);
846 else if (maxlen < 10)
847 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100848 char_u *t_buf = buf;
849
Bram Moolenaar85a20022019-12-21 18:25:54 +0100850 // Need some more space. This might happen when receiving a long
851 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100852 buflen += 100;
853 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100854 if (buf == NULL)
855 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100856 maxlen = (buflen - 6 - len) / 3;
857 }
858 if (buf == NULL)
859 {
860 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100861 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100862 }
863
Bram Moolenaar85a20022019-12-21 18:25:54 +0100864 // First time: blocking wait. Second time: wait up to 100ms for a
865 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100866 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (n > 0)
868 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100869 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200870 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000872 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000874 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100875 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100878 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000879 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000881
Bram Moolenaar85a20022019-12-21 18:25:54 +0100882 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100883 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100884 if (must_redraw != 0 && !need_wait_return && (State
885 & (MODE_CMDLINE | MODE_HITRETURN | MODE_ASKMORE)) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100886 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100888 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100889 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100890 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100891 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100892 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100893 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100895 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 continue;
897
Bram Moolenaar85a20022019-12-21 18:25:54 +0100898 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 n = buf[0];
900 if (n == K_SPECIAL)
901 {
902 n = TO_SPECIAL(buf[1], buf[2]);
903 if (buf[1] == KS_MODIFIER
904 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100905 || (is_mouse_key(n) && n != K_LEFTMOUSE)
906#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 || n == K_VER_SCROLLBAR
908 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#endif
910 )
911 {
912 if (buf[1] == KS_MODIFIER)
913 mod_mask = buf[2];
914 len -= 3;
915 if (len > 0)
916 mch_memmove(buf, buf + 3, (size_t)len);
917 continue;
918 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (has_mbyte)
922 {
923 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100924 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100925 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 n = (*mb_ptr2char)(buf);
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928#ifdef UNIX
929 if (n == intr_char)
930 n = ESC;
931#endif
932 break;
933 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100934 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
936 mapped_ctrl_c = save_mapped_ctrl_c;
937 return n;
938}
939
940/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000941 * Get a number from the user.
942 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 */
944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100945get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100946 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100947 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949 int n = 0;
950 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000951 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000953 if (mouse_used != NULL)
954 *mouse_used = FALSE;
955
Bram Moolenaar85a20022019-12-21 18:25:54 +0100956 // When not printing messages, the user won't know what to type, return a
957 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (msg_silent != 0)
959 return 0;
960
961#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100962 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#endif
964 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100965 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 for (;;)
967 {
968 windgoto(msg_row, msg_col);
969 c = safe_vgetc();
970 if (VIM_ISDIGIT(c))
971 {
972 n = n * 10 + c - '0';
973 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000974 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
977 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000978 if (typed > 0)
979 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100980 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000981 --typed;
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000985 else if (mouse_used != NULL && c == K_LEFTMOUSE)
986 {
987 *mouse_used = TRUE;
988 n = mouse_row + 1;
989 break;
990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 else if (n == 0 && c == ':' && colon)
992 {
993 stuffcharReadbuff(':');
994 if (!exmode_active)
995 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100996 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 do_redraw = FALSE;
998 break;
999 }
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +02001000 else if (c == Ctrl_C || c == ESC || c == 'q')
1001 {
1002 n = 0;
1003 break;
1004 }
1005 else if (c == CAR || c == NL )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 break;
1007 }
1008 --no_mapping;
1009 --allow_keys;
1010 return n;
1011}
1012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001013/*
1014 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001015 * When "mouse_used" is not NULL allow using the mouse and in that case return
1016 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001017 */
1018 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001019prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001020{
1021 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001022 int save_cmdline_row;
1023 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001024
Bram Moolenaar85a20022019-12-21 18:25:54 +01001025 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001026 if (mouse_used != NULL)
Bram Moolenaareebd5552020-06-10 15:45:57 +02001027 msg_puts(_("Type number and <Enter> or click with the mouse (q or empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001028 else
Bram Moolenaareebd5552020-06-10 15:45:57 +02001029 msg_puts(_("Type number and <Enter> (q or empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001030
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001031 // Set the state such that text can be selected/copied/pasted and we still
1032 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1033 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001034 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001035 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001036 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001037 State = MODE_CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001038 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001039 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001040
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001041 i = get_number(TRUE, mouse_used);
1042 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001043 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001044 // don't call wait_return() now
1045 if (msg_row > 0)
1046 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001047 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +01001048 msg_didany = FALSE;
1049 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001050 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001051 else
1052 cmdline_row = save_cmdline_row;
1053 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001054 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001055 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001057 return i;
1058}
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001061msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063 long pn;
1064
Bram Moolenaar85a20022019-12-21 18:25:54 +01001065 if (global_busy // no messages now, wait until global is finished
1066 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return;
1068
Bram Moolenaar85a20022019-12-21 18:25:54 +01001069 // We don't want to overwrite another important message, but do overwrite
1070 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1071 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001072 if (keep_msg != NULL && !keep_msg_more)
1073 return;
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (n > 0)
1076 pn = n;
1077 else
1078 pn = -n;
1079
1080 if (pn > p_report)
1081 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001082 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001083 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001084 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001086 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001087 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001089 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1090 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (msg(msg_buf))
1092 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001093 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001094 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096 }
1097}
1098
1099/*
1100 * flush map and typeahead buffers and give a warning for an error
1101 */
1102 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001103beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 if (emsg_silent == 0)
1106 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001107 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001108 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110}
1111
1112/*
Bram Moolenaaraae97622022-04-13 14:28:07 +01001113 * Give a warning for an error. "val" is one of the BO_ values, e.g., BO_OPER.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
1115 void
Bram Moolenaaraae97622022-04-13 14:28:07 +01001116vim_beep(unsigned val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001118#ifdef FEAT_EVAL
1119 called_vim_beep = TRUE;
1120#endif
1121
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001122 if (emsg_silent != 0 || in_assert_fails)
1123 return;
1124
1125 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001127#ifdef ELAPSED_FUNC
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001128 static int did_init = FALSE;
1129 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001130
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001131 // Only beep once per half a second, otherwise a sequence of beeps
1132 // would freeze Vim.
1133 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1134 {
1135 did_init = TRUE;
1136 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001138 if (p_vb
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001139#ifdef FEAT_GUI
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001140 // While the GUI is starting up the termcap is set for
1141 // the GUI but the output still goes to a terminal.
1142 && !(gui.in_use && gui.starting)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001143#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001144 )
1145 {
1146 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001147#ifdef FEAT_VTP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001148 // No restore color information, refresh the screen.
1149 if (has_vtp_working() != 0
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001150# ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001151 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001152# endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001153 )
1154 {
1155 redraw_later(UPD_CLEAR);
1156 update_screen(0);
1157 redrawcmd();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001158 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001159#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001160 }
1161 else
1162 out_char(BELL);
1163#ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001165#endif
1166 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001168 // When 'debug' contains "beep" produce a message. If we are sourcing
1169 // a script or executing a function give the user a hint where the beep
1170 // comes from.
1171 if (vim_strchr(p_debug, 'e') != NULL)
1172 {
1173 msg_source(HL_ATTR(HLF_W));
1174 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176}
1177
1178/*
1179 * To get the "real" home directory:
1180 * - get value of $HOME
1181 * For Unix:
1182 * - go to that directory
1183 * - do mch_dirname() to get the real name of that directory.
1184 * This also works with mounts and links.
1185 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001186 * For Windows:
1187 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001190init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191{
1192 char_u *var;
1193
Bram Moolenaar85a20022019-12-21 18:25:54 +01001194 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001195 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#ifdef VMS
1198 var = mch_getenv((char_u *)"SYS$LOGIN");
1199#else
1200 var = mch_getenv((char_u *)"HOME");
1201#endif
1202
Bram Moolenaar4f974752019-02-17 17:44:42 +01001203#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001205 * Typically, $HOME is not defined on Windows, unless the user has
1206 * specifically defined it for Vim's sake. However, on Windows NT
1207 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1208 * each user. Try constructing $HOME from these.
1209 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001210 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001211 {
1212 char_u *homedrive, *homepath;
1213
1214 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1215 homepath = mch_getenv((char_u *)"HOMEPATH");
1216 if (homepath == NULL || *homepath == NUL)
1217 homepath = (char_u *)"\\";
1218 if (homedrive != NULL
1219 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1220 {
1221 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1222 if (NameBuff[0] != NUL)
1223 var = NameBuff;
1224 }
1225 }
1226
1227 if (var == NULL)
1228 var = mch_getenv((char_u *)"USERPROFILE");
1229
1230 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 * Weird but true: $HOME may contain an indirect reference to another
1232 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1233 * when $HOME is being set.
1234 */
1235 if (var != NULL && *var == '%')
1236 {
1237 char_u *p;
1238 char_u *exp;
1239
1240 p = vim_strchr(var + 1, '%');
1241 if (p != NULL)
1242 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001243 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 exp = mch_getenv(NameBuff);
1245 if (exp != NULL && *exp != NUL
1246 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1247 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001248 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 }
1252 }
1253
Bram Moolenaar85a20022019-12-21 18:25:54 +01001254 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001255 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar05159a02005-02-26 23:04:13 +00001257 if (enc_utf8 && var != NULL)
1258 {
1259 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001260 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001261
Bram Moolenaar85a20022019-12-21 18:25:54 +01001262 // Convert from active codepage to UTF-8. Other conversions are
1263 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001264 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001265 if (pp != NULL)
1266 {
1267 homedir = pp;
1268 return;
1269 }
1270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 /*
1273 * Default home dir is C:/
1274 * Best assumption we can make in such a situation.
1275 */
1276 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001277 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 if (var != NULL)
1281 {
1282#ifdef UNIX
1283 /*
1284 * Change to the directory and get the actual path. This resolves
1285 * links. Don't do it when we can't return.
1286 */
1287 if (mch_dirname(NameBuff, MAXPATHL) == OK
1288 && mch_chdir((char *)NameBuff) == 0)
1289 {
1290 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1291 var = IObuff;
1292 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001293 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295#endif
1296 homedir = vim_strsave(var);
1297 }
1298}
1299
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001300#if defined(EXITFREE) || defined(PROTO)
1301 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001302free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001303{
1304 vim_free(homedir);
1305}
Bram Moolenaar24305862012-08-15 14:05:05 +02001306
Bram Moolenaar24305862012-08-15 14:05:05 +02001307 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001308free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001309{
1310 ga_clear_strings(&ga_users);
1311}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001312#endif
1313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001315 * Call expand_env() and store the result in an allocated string.
1316 * This is not very memory efficient, this expects the result to be freed
1317 * again soon.
1318 */
1319 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001320expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001321{
1322 return expand_env_save_opt(src, FALSE);
1323}
1324
1325/*
1326 * Idem, but when "one" is TRUE handle the string as one file name, only
1327 * expand "~" at the start.
1328 */
1329 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001330expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001331{
1332 char_u *p;
1333
1334 p = alloc(MAXPATHL);
1335 if (p != NULL)
1336 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1337 return p;
1338}
1339
1340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 * Expand environment variable with path name.
1342 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001343 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 * If anything fails no expansion is done and dst equals src.
1345 */
1346 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001347expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 char_u *src, // input string e.g. "$HOME/vim.hlp"
1349 char_u *dst, // where to put the result
1350 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001352 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353}
1354
1355 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001356expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001357 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1358 char_u *dst, // where to put the result
1359 int dstlen, // maximum length of the result
1360 int esc, // escape spaces in expanded variables
1361 int one, // "srcp" is one file name
1362 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001364 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *tail;
1366 int c;
1367 char_u *var;
1368 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001369 int mustfree; // var was allocated, need to free it later
1370 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001371 int startstr_len = 0;
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001372#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1373 char_u *save_dst = dst;
1374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001376 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001377 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001378
1379 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001380 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 while (*src && dstlen > 0)
1382 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001383#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001384 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001385 if (src[0] == '`' && src[1] == '=')
1386 {
1387 size_t len;
1388
1389 var = src;
1390 src += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02001391 (void)skip_expr(&src, NULL);
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001392 if (*src == '`')
1393 ++src;
1394 len = src - var;
1395 if (len > (size_t)dstlen)
1396 len = dstlen;
1397 vim_strncpy(dst, var, len);
1398 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001399 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001400 continue;
1401 }
1402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001404 if ((*src == '$'
1405#ifdef VMS
1406 && at_start
1407#endif
1408 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001409#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 || *src == '%'
1411#endif
1412 || (*src == '~' && at_start))
1413 {
1414 mustfree = FALSE;
1415
1416 /*
1417 * The variable name is copied into dst temporarily, because it may
1418 * be a string in read-only memory and a NUL needs to be appended.
1419 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001420 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
1422 tail = src + 1;
1423 var = dst;
1424 c = dstlen - 1;
1425
1426#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001427 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (*tail == '{' && !vim_isIDc('{'))
1429 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001430 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 while (c-- > 0 && *tail && *tail != '}')
1432 *var++ = *tail++;
1433 }
1434 else
1435#endif
1436 {
1437 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001438#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 || (*src == '%' && *tail != '%')
1440#endif
1441 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001445#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446# ifdef UNIX
1447 if (src[1] == '{' && *tail != '}')
1448# else
1449 if (*src == '%' && *tail != '%')
1450# endif
1451 var = NULL;
1452 else
1453 {
1454# ifdef UNIX
1455 if (src[1] == '{')
1456# else
1457 if (*src == '%')
1458#endif
1459 ++tail;
1460#endif
1461 *var = NUL;
1462 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001463#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465#endif
1466 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001467 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 else if ( src[1] == NUL
1469 || vim_ispathsep(src[1])
1470 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1471 {
1472 var = homedir;
1473 tail = src + 1;
1474 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001475 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1478 /*
1479 * Copy ~user to dst[], so we can put a NUL after it.
1480 */
1481 tail = src;
1482 var = dst;
1483 c = dstlen - 1;
1484 while ( c-- > 0
1485 && *tail
1486 && vim_isfilec(*tail)
1487 && !vim_ispathsep(*tail))
1488 *var++ = *tail++;
1489 *var = NUL;
1490# ifdef UNIX
1491 /*
1492 * If the system supports getpwnam(), use it.
1493 * Otherwise, or if getpwnam() fails, the shell is used to
1494 * expand ~user. This is slower and may fail if the shell
1495 * does not support ~user (old versions of /bin/sh).
1496 */
1497# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1498 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001499 // Note: memory allocated by getpwnam() is never freed.
1500 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001501 struct passwd *pw = (*dst == NUL)
1502 ? NULL : getpwnam((char *)dst + 1);
1503
1504 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 }
1506 if (var == NULL)
1507# endif
1508 {
1509 expand_T xpc;
1510
1511 ExpandInit(&xpc);
1512 xpc.xp_context = EXPAND_FILES;
1513 var = ExpandOne(&xpc, dst, NULL,
1514 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 mustfree = TRUE;
1516 }
1517
Bram Moolenaar85a20022019-12-21 18:25:54 +01001518# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 /*
1520 * USER_HOME is a comma-separated list of
1521 * directories to search for the user account in.
1522 */
1523 {
1524 char_u test[MAXPATHL], paths[MAXPATHL];
1525 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001526 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528 STRCPY(paths, USER_HOME);
1529 next_path = paths;
1530 while (*next_path)
1531 {
1532 for (path = next_path; *next_path && *next_path != ',';
1533 next_path++);
1534 if (*next_path)
1535 *next_path++ = NUL;
1536 STRCPY(test, path);
1537 STRCAT(test, "/");
1538 STRCAT(test, dst + 1);
1539 if (mch_stat(test, &st) == 0)
1540 {
1541 var = alloc(STRLEN(test) + 1);
1542 STRCPY(var, test);
1543 mustfree = TRUE;
1544 break;
1545 }
1546 }
1547 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001548# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001550 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001552 tail = (char_u *)""; // for gcc
1553#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 }
1555
1556#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001557 // If 'shellslash' is set change backslashes to forward slashes.
1558 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1560 {
1561 char_u *p = vim_strsave(var);
1562
1563 if (p != NULL)
1564 {
1565 if (mustfree)
1566 vim_free(var);
1567 var = p;
1568 mustfree = TRUE;
1569 forward_slash(var);
1570 }
1571 }
1572#endif
1573
Bram Moolenaar85a20022019-12-21 18:25:54 +01001574 // If "var" contains white space, escape it with a backslash.
1575 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1577 {
1578 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1579
1580 if (p != NULL)
1581 {
1582 if (mustfree)
1583 vim_free(var);
1584 var = p;
1585 mustfree = TRUE;
1586 }
1587 }
1588
1589 if (var != NULL && *var != NUL
1590 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1591 {
1592 STRCPY(dst, var);
1593 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001594 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001595 // if var[] ends in a path separator and tail[] starts
1596 // with it, skip a character
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001597 if (after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001599 && (dst == save_dst || dst[-1] != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600#endif
1601 && vim_ispathsep(*tail))
1602 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001603 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 src = tail;
1605 copy_char = FALSE;
1606 }
1607 if (mustfree)
1608 vim_free(var);
1609 }
1610
Bram Moolenaar85a20022019-12-21 18:25:54 +01001611 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001614 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001615 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1616 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
1618 at_start = FALSE;
1619 if (src[0] == '\\' && src[1] != NUL)
1620 {
1621 *dst++ = *src++;
1622 --dstlen;
1623 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001624 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001626 if (dstlen > 0)
1627 {
1628 *dst++ = *src++;
1629 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001630
Bram Moolenaar1c864092017-08-06 18:15:45 +02001631 if (startstr != NULL && src - startstr_len >= srcp
1632 && STRNCMP(src - startstr_len, startstr,
1633 startstr_len) == 0)
1634 at_start = TRUE;
1635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
1639 *dst = NUL;
1640}
1641
1642/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001643 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1644 * the length of "name/". Otherwise return "pend".
1645 */
1646 static char_u *
1647remove_tail(char_u *p, char_u *pend, char_u *name)
1648{
1649 int len = (int)STRLEN(name) + 1;
1650 char_u *newend = pend - len;
1651
1652 if (newend >= p
1653 && fnamencmp(newend, name, len - 1) == 0
1654 && (newend == p || after_pathsep(p, newend)))
1655 return newend;
1656 return pend;
1657}
1658
1659/*
1660 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1661 * Return NULL if not, return its name in allocated memory otherwise.
1662 */
1663 static char_u *
1664vim_version_dir(char_u *vimdir)
1665{
1666 char_u *p;
1667
1668 if (vimdir == NULL || *vimdir == NUL)
1669 return NULL;
1670 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1671 if (p != NULL && mch_isdir(p))
1672 return p;
1673 vim_free(p);
1674 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1675 if (p != NULL && mch_isdir(p))
Bram Moolenaar022f9ef2022-07-02 17:36:31 +01001676 {
1677 char_u *fname = concat_fnames(p, (char_u *)"defaults.vim", TRUE);
1678
1679 // Check that "defaults.vim" exists in this directory, to avoid picking
1680 // up a stray "runtime" directory, it would make many tests fail in
1681 // mysterious ways.
1682 if (fname != NULL)
1683 {
1684 int exists = file_is_readable(fname);
1685
1686 vim_free(fname);
1687 if (exists)
1688 return p;
1689 }
1690 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001691 vim_free(p);
1692 return NULL;
1693}
1694
1695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 * Vim's version of getenv().
1697 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001698 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001699 * "mustfree" is set to TRUE when returned is allocated, it must be
1700 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 */
1702 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001703vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001705 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 char_u *pend;
1707 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001708#ifdef MSWIN
1709 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001711 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if (STRCMP(name, "HOME") == 0)
1713 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001715 // Use Wide function
1716 wn = enc_to_utf16(name, NULL);
1717 if (wn == NULL)
1718 return NULL;
1719
1720 wp = _wgetenv(wn);
1721 vim_free(wn);
1722
1723 if (wp != NULL && *wp == NUL) // empty is the same as not set
1724 wp = NULL;
1725
1726 if (wp != NULL)
1727 {
1728 p = utf16_to_enc(wp, NULL);
1729 if (p == NULL)
1730 return NULL;
1731
1732 *mustfree = TRUE;
1733 return p;
1734 }
1735#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001737 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 p = NULL;
1739
1740 if (p != NULL)
1741 return p;
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001742
1743# ifdef __HAIKU__
1744 // special handling for user settings directory...
1745 if (STRCMP(name, "BE_USER_SETTINGS") == 0)
1746 {
1747 static char userSettingsPath[MAXPATHL];
1748
1749 if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false,
1750 userSettingsPath, MAXPATHL) == B_OK)
1751 return (char_u *)userSettingsPath;
1752 else
1753 return NULL;
1754 }
1755# endif
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001758 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1760 if (!vimruntime && STRCMP(name, "VIM") != 0)
1761 return NULL;
1762
1763 /*
1764 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1765 * Don't do this when default_vimruntime_dir is non-empty.
1766 */
1767 if (vimruntime
1768#ifdef HAVE_PATHDEF
1769 && *default_vimruntime_dir == NUL
1770#endif
1771 )
1772 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001773#ifdef MSWIN
1774 // Use Wide function
1775 wp = _wgetenv(L"VIM");
1776 if (wp != NULL && *wp == NUL) // empty is the same as not set
1777 wp = NULL;
1778 if (wp != NULL)
1779 {
1780 char_u *q = utf16_to_enc(wp, NULL);
1781 if (q != NULL)
1782 {
1783 p = vim_version_dir(q);
1784 *mustfree = TRUE;
1785 if (p == NULL)
1786 p = q;
1787 }
1788 }
1789#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001791 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 p = NULL;
1793 if (p != NULL)
1794 {
1795 p = vim_version_dir(p);
1796 if (p != NULL)
1797 *mustfree = TRUE;
1798 else
1799 p = mch_getenv((char_u *)"VIM");
1800 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803
1804 /*
1805 * When expanding $VIM or $VIMRUNTIME fails, try using:
1806 * - the directory name from 'helpfile' (unless it contains '$')
1807 * - the executable name from argv[0]
1808 */
1809 if (p == NULL)
1810 {
1811 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1812 p = p_hf;
1813#ifdef USE_EXE_NAME
1814 /*
1815 * Use the name of the executable, obtained from argv[0].
1816 */
1817 else
1818 p = exe_name;
1819#endif
1820 if (p != NULL)
1821 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001822 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 pend = gettail(p);
1824
Bram Moolenaar85a20022019-12-21 18:25:54 +01001825 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (p == p_hf)
1827 pend = remove_tail(p, pend, (char_u *)"doc");
1828
1829#ifdef USE_EXE_NAME
1830# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001831 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (p == exe_name)
1833 {
1834 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001835 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001837 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1838 if (pend1 != pend)
1839 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001840 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001841 if (pnew != NULL)
1842 {
1843 STRNCPY(pnew, p, (pend1 - p));
1844 STRCPY(pnew + (pend1 - p), "Resources/vim");
1845 p = pnew;
1846 pend = p + STRLEN(p);
1847 }
1848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001851 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (p == exe_name)
1853 pend = remove_tail(p, pend, (char_u *)"src");
1854#endif
1855
Bram Moolenaar85a20022019-12-21 18:25:54 +01001856 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (!vimruntime)
1858 {
1859 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1860 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1861 }
1862
Bram Moolenaar85a20022019-12-21 18:25:54 +01001863 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001864 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001867#ifdef MACOS_X
1868 if (p == exe_name || p == p_hf)
1869#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001870 // check that the result is a directory name
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001871 p = vim_strnsave(p, pend - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001874 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 else
1876 {
1877#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001878 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1880 {
1881 vim_free(p);
1882 p = pend;
1883 }
1884#endif
1885 *mustfree = TRUE;
1886 }
1887 }
1888 }
1889
1890#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001891 // When there is a pathdef.c file we can use default_vim_dir and
1892 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (p == NULL)
1894 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001895 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (vimruntime && *default_vimruntime_dir != NUL)
1897 {
1898 p = default_vimruntime_dir;
1899 *mustfree = FALSE;
1900 }
1901 else if (*default_vim_dir != NUL)
1902 {
1903 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1904 *mustfree = TRUE;
1905 else
1906 {
1907 p = default_vim_dir;
1908 *mustfree = FALSE;
1909 }
1910 }
1911 }
1912#endif
1913
1914 /*
1915 * Set the environment variable, so that the new value can be found fast
1916 * next time, and others can also use it (e.g. Perl).
1917 */
1918 if (p != NULL)
1919 {
1920 if (vimruntime)
1921 {
1922 vim_setenv((char_u *)"VIMRUNTIME", p);
1923 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 else
1926 {
1927 vim_setenv((char_u *)"VIM", p);
1928 didset_vim = TRUE;
1929 }
1930 }
1931 return p;
1932}
1933
Bram Moolenaar137374f2018-05-13 15:59:50 +02001934 void
1935vim_unsetenv(char_u *var)
1936{
1937#ifdef HAVE_UNSETENV
1938 unsetenv((char *)var);
1939#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001940 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001941#endif
1942}
1943
LemonBoy77142312022-04-15 20:50:46 +01001944/*
1945 * Removes environment variable "name" and take care of side effects.
1946 */
1947 void
1948vim_unsetenv_ext(char_u *var)
1949{
1950 vim_unsetenv(var);
1951
1952 // "homedir" is not cleared, keep using the old value until $HOME is set.
1953 if (STRICMP(var, "VIM") == 0)
1954 didset_vim = FALSE;
1955 else if (STRICMP(var, "VIMRUNTIME") == 0)
1956 didset_vimruntime = FALSE;
1957}
Bram Moolenaar137374f2018-05-13 15:59:50 +02001958
Bram Moolenaar092e09c2022-04-15 23:29:23 +01001959#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001961 * Set environment variable "name" and take care of side effects.
1962 */
1963 void
1964vim_setenv_ext(char_u *name, char_u *val)
1965{
1966 vim_setenv(name, val);
1967 if (STRICMP(name, "HOME") == 0)
1968 init_homedir();
1969 else if (didset_vim && STRICMP(name, "VIM") == 0)
1970 didset_vim = FALSE;
LemonBoy77142312022-04-15 20:50:46 +01001971 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001972 didset_vimruntime = FALSE;
1973}
Dominique Pelle748b3082022-01-08 12:41:16 +00001974#endif
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001975
1976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * Our portable version of setenv.
1978 */
1979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982#ifdef HAVE_SETENV
1983 mch_setenv((char *)name, (char *)val, 1);
1984#else
1985 char_u *envbuf;
1986
1987 /*
1988 * Putenv does not copy the string, it has to remain
1989 * valid. The allocated memory will never be freed.
1990 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001991 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (envbuf != NULL)
1993 {
1994 sprintf((char *)envbuf, "%s=%s", name, val);
1995 putenv((char *)envbuf);
1996 }
1997#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001998#ifdef FEAT_GETTEXT
1999 /*
2000 * When setting $VIMRUNTIME adjust the directory to find message
2001 * translations to $VIMRUNTIME/lang.
2002 */
2003 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2004 {
2005 char_u *buf = concat_str(val, (char_u *)"/lang");
2006
2007 if (buf != NULL)
2008 {
2009 bindtextdomain(VIMPACKAGE, (char *)buf);
2010 vim_free(buf);
2011 }
2012 }
2013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014}
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016/*
2017 * Function given to ExpandGeneric() to obtain an environment variable name.
2018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002020get_env_name(
2021 expand_T *xp UNUSED,
2022 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002024#if defined(AMIGA)
2025 // No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 return NULL;
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002027#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01002029 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 extern char **environ;
2031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 char_u *str;
2033 int n;
2034
2035 str = (char_u *)environ[idx];
2036 if (str == NULL)
2037 return NULL;
2038
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002039 for (n = 0; n < EXPAND_BUF_LEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 if (str[n] == '=' || str[n] == NUL)
2042 break;
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002043 xp->xp_buf[n] = str[n];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002045 xp->xp_buf[n] = NUL;
2046 return xp->xp_buf;
2047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048}
Bram Moolenaar24305862012-08-15 14:05:05 +02002049
2050/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002051 * Add a user name to the list of users in ga_users.
2052 * Do nothing if user name is NULL or empty.
2053 */
2054 static void
2055add_user(char_u *user, int need_copy)
2056{
2057 char_u *user_copy = (user != NULL && need_copy)
2058 ? vim_strsave(user) : user;
2059
2060 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2061 {
2062 if (need_copy)
2063 vim_free(user);
2064 return;
2065 }
2066 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2067}
2068
2069/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002070 * Find all user names for user completion.
2071 * Done only once and then cached.
2072 */
2073 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002074init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002075{
Bram Moolenaar24305862012-08-15 14:05:05 +02002076 static int lazy_init_done = FALSE;
2077
2078 if (lazy_init_done)
2079 return;
2080
2081 lazy_init_done = TRUE;
2082 ga_init2(&ga_users, sizeof(char_u *), 20);
2083
2084# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2085 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002086 struct passwd* pw;
2087
2088 setpwent();
2089 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002090 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002091 endpwent();
2092 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002093# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002094 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002095 DWORD nusers = 0, ntotal = 0, i;
2096 PUSER_INFO_0 uinfo;
2097
2098 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2099 &nusers, &ntotal, NULL) == NERR_Success)
2100 {
2101 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002102 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002103
2104 NetApiBufferFree(uinfo);
2105 }
2106 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002107# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002108# if defined(HAVE_GETPWNAM)
2109 {
2110 char_u *user_env = mch_getenv((char_u *)"USER");
2111
2112 // The $USER environment variable may be a valid remote user name (NIS,
2113 // LDAP) not already listed by getpwent(), as getpwent() only lists
2114 // local user names. If $USER is not already listed, check whether it
2115 // is a valid remote user name using getpwnam() and if it is, add it to
2116 // the list of user names.
2117
2118 if (user_env != NULL && *user_env != NUL)
2119 {
2120 int i;
2121
2122 for (i = 0; i < ga_users.ga_len; i++)
2123 {
2124 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2125
2126 if (STRCMP(local_user, user_env) == 0)
2127 break;
2128 }
2129
2130 if (i == ga_users.ga_len)
2131 {
2132 struct passwd *pw = getpwnam((char *)user_env);
2133
2134 if (pw != NULL)
2135 add_user((char_u *)pw->pw_name, TRUE);
2136 }
2137 }
2138 }
2139# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002140}
2141
2142/*
2143 * Function given to ExpandGeneric() to obtain an user names.
2144 */
2145 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002146get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002147{
2148 init_users();
2149 if (idx < ga_users.ga_len)
2150 return ((char_u **)ga_users.ga_data)[idx];
2151 return NULL;
2152}
2153
2154/*
2155 * Check whether name matches a user name. Return:
2156 * 0 if name does not match any user name.
2157 * 1 if name partially matches the beginning of a user name.
2158 * 2 is name fully matches a user name.
2159 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002160 int
2161match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002162{
2163 int i;
2164 int n = (int)STRLEN(name);
2165 int result = 0;
2166
2167 init_users();
2168 for (i = 0; i < ga_users.ga_len; i++)
2169 {
2170 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002171 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002172 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002173 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002174 }
2175 return result;
2176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002181#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002182 // Ignore SIGHUP, because a dropped connection causes a read error, which
2183 // makes Vim exit and then handling SIGHUP causes various reentrance
2184 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002185 signal(SIGHUP, SIG_IGN);
2186#endif
2187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188#ifdef FEAT_GUI
2189 if (gui.in_use)
2190 {
2191 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194 else
2195#endif
2196 {
2197 windgoto((int)Rows - 1, 0);
2198
2199 /*
2200 * Switch terminal mode back now, so messages end up on the "normal"
2201 * screen (if there are two screens).
2202 */
2203 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002204 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 out_flush();
2206 }
2207}
2208
2209/*
2210 * Preserve files and exit.
2211 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002212 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2213 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 */
2215 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002216preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
2218 buf_T *buf;
2219
2220 prepare_to_exit();
2221
Bram Moolenaar85a20022019-12-21 18:25:54 +01002222 // Setting this will prevent free() calls. That avoids calling free()
2223 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002224 really_exiting = TRUE;
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 out_flush();
2229
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaar29323592016-07-24 22:04:11 +02002232 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
2234 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2235 {
Bram Moolenaar69212b12020-05-10 14:14:03 +02002236 OUT_STR("Vim: preserving files...\r\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002237 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002239 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 break;
2241 }
2242 }
2243
Bram Moolenaar85a20022019-12-21 18:25:54 +01002244 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
Bram Moolenaar69212b12020-05-10 14:14:03 +02002246 OUT_STR("Vim: Finished.\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
2248 getout(1);
2249}
2250
2251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 * Check for CTRL-C pressed, but only once in a while.
2253 * Should be used instead of ui_breakcheck() for functions that check for
2254 * each line in the file. Calling ui_breakcheck() each time takes too much
2255 * time, because it can be a system call.
2256 */
2257
2258#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002259# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260#endif
2261
2262static int breakcheck_count = 0;
2263
2264 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002265line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 if (++breakcheck_count >= BREAKCHECK_SKIP)
2268 {
2269 breakcheck_count = 0;
2270 ui_breakcheck();
2271 }
2272}
2273
2274/*
2275 * Like line_breakcheck() but check 10 times less often.
2276 */
2277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002278fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2281 {
2282 breakcheck_count = 0;
2283 ui_breakcheck();
2284 }
2285}
2286
Dominique Pelle748b3082022-01-08 12:41:16 +00002287# if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002288/*
2289 * Like line_breakcheck() but check 100 times less often.
2290 */
2291 void
2292veryfast_breakcheck(void)
2293{
2294 if (++breakcheck_count >= BREAKCHECK_SKIP * 100)
2295 {
2296 breakcheck_count = 0;
2297 ui_breakcheck();
2298 }
2299}
Dominique Pelle748b3082022-01-08 12:41:16 +00002300#endif
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002301
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002302#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2303 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2304 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306#ifndef SEEK_SET
2307# define SEEK_SET 0
2308#endif
2309#ifndef SEEK_END
2310# define SEEK_END 2
2311#endif
2312
2313/*
2314 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002315 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2316 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 * Returns an allocated string, or NULL for error.
2318 */
2319 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002320get_cmd_output(
2321 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002322 char_u *infile, // optional input file name
2323 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002324 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 char_u *tempname;
2327 char_u *command;
2328 char_u *buffer = NULL;
2329 int len;
2330 int i = 0;
2331 FILE *fd;
2332
2333 if (check_restricted() || check_secure())
2334 return NULL;
2335
Bram Moolenaar85a20022019-12-21 18:25:54 +01002336 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002337 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002339 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 return NULL;
2341 }
2342
Bram Moolenaar85a20022019-12-21 18:25:54 +01002343 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002344 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (command == NULL)
2346 goto done;
2347
2348 /*
2349 * Call the shell to execute the command (errors are ignored).
2350 * Don't check timestamps here.
2351 */
2352 ++no_check_timestamps;
2353 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2354 --no_check_timestamps;
2355
2356 vim_free(command);
2357
2358 /*
2359 * read the names from the file into memory
2360 */
2361# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002362 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 fd = mch_fopen((char *)tempname, "r");
2364# else
2365 fd = mch_fopen((char *)tempname, READBIN);
2366# endif
2367
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002368 // Not being able to seek means we can't read the file.
2369 if (fd == NULL
2370 || fseek(fd, 0L, SEEK_END) == -1
2371 || (len = ftell(fd)) == -1 // get size of temp file
2372 || fseek(fd, 0L, SEEK_SET) == -1) // back to the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaara9549c92022-04-17 14:18:11 +01002374 semsg(_(e_cannot_read_from_str_2), tempname);
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002375 if (fd != NULL)
2376 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 goto done;
2378 }
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 buffer = alloc(len + 1);
2381 if (buffer != NULL)
2382 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2383 fclose(fd);
2384 mch_remove(tempname);
2385 if (buffer == NULL)
2386 goto done;
2387#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002388 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#endif
2390 if (i != len)
2391 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002392 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002393 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002395 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002396 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002397 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002398 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002399 if (buffer[i] == NUL)
2400 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002401
Bram Moolenaar85a20022019-12-21 18:25:54 +01002402 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002403 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002404 else
2405 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407done:
2408 vim_free(tempname);
2409 return buffer;
2410}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002411
2412# if defined(FEAT_EVAL) || defined(PROTO)
2413
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002414 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002415get_cmd_output_as_rettv(
2416 typval_T *argvars,
2417 typval_T *rettv,
2418 int retlist)
2419{
2420 char_u *res = NULL;
2421 char_u *p;
2422 char_u *infile = NULL;
2423 int err = FALSE;
2424 FILE *fd;
2425 list_T *list = NULL;
2426 int flags = SHELL_SILENT;
2427
2428 rettv->v_type = VAR_STRING;
2429 rettv->vval.v_string = NULL;
2430 if (check_restricted() || check_secure())
2431 goto errret;
2432
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002433 if (in_vim9script()
2434 && (check_for_string_arg(argvars, 0) == FAIL
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01002435 || check_for_opt_string_or_number_or_list_arg(argvars, 1)
2436 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002437 return;
2438
Bram Moolenaar26262f82019-09-04 20:59:15 +02002439 if (argvars[1].v_type != VAR_UNKNOWN)
2440 {
2441 /*
2442 * Write the text to a temp file, to be used for input of the shell
2443 * command.
2444 */
2445 if ((infile = vim_tempname('i', TRUE)) == NULL)
2446 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002447 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002448 goto errret;
2449 }
2450
2451 fd = mch_fopen((char *)infile, WRITEBIN);
2452 if (fd == NULL)
2453 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002454 semsg(_(e_cant_open_file_str), infile);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002455 goto errret;
2456 }
2457 if (argvars[1].v_type == VAR_NUMBER)
2458 {
2459 linenr_T lnum;
2460 buf_T *buf;
2461
2462 buf = buflist_findnr(argvars[1].vval.v_number);
2463 if (buf == NULL)
2464 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002465 semsg(_(e_buffer_nr_does_not_exist), argvars[1].vval.v_number);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002466 fclose(fd);
2467 goto errret;
2468 }
2469
2470 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2471 {
2472 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2473 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2474 {
2475 err = TRUE;
2476 break;
2477 }
2478 if (putc(NL, fd) == EOF)
2479 {
2480 err = TRUE;
2481 break;
2482 }
2483 }
2484 }
2485 else if (argvars[1].v_type == VAR_LIST)
2486 {
2487 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2488 err = TRUE;
2489 }
2490 else
2491 {
2492 size_t len;
2493 char_u buf[NUMBUFLEN];
2494
2495 p = tv_get_string_buf_chk(&argvars[1], buf);
2496 if (p == NULL)
2497 {
2498 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002499 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002500 }
2501 len = STRLEN(p);
2502 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2503 err = TRUE;
2504 }
2505 if (fclose(fd) != 0)
2506 err = TRUE;
2507 if (err)
2508 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002509 emsg(_(e_error_writing_temp_file));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002510 goto errret;
2511 }
2512 }
2513
Bram Moolenaar85a20022019-12-21 18:25:54 +01002514 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2515 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002516 if (!msg_silent)
2517 flags += SHELL_COOKED;
2518
2519 if (retlist)
2520 {
2521 int len;
2522 listitem_T *li;
2523 char_u *s = NULL;
2524 char_u *start;
2525 char_u *end;
2526 int i;
2527
2528 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2529 if (res == NULL)
2530 goto errret;
2531
2532 list = list_alloc();
2533 if (list == NULL)
2534 goto errret;
2535
2536 for (i = 0; i < len; ++i)
2537 {
2538 start = res + i;
2539 while (i < len && res[i] != NL)
2540 ++i;
2541 end = res + i;
2542
2543 s = alloc(end - start + 1);
2544 if (s == NULL)
2545 goto errret;
2546
2547 for (p = s; start < end; ++p, ++start)
2548 *p = *start == NUL ? NL : *start;
2549 *p = NUL;
2550
2551 li = listitem_alloc();
2552 if (li == NULL)
2553 {
2554 vim_free(s);
2555 goto errret;
2556 }
2557 li->li_tv.v_type = VAR_STRING;
2558 li->li_tv.v_lock = 0;
2559 li->li_tv.vval.v_string = s;
2560 list_append(list, li);
2561 }
2562
2563 rettv_list_set(rettv, list);
2564 list = NULL;
2565 }
2566 else
2567 {
2568 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2569#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002570 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002571 if (res != NULL)
2572 {
2573 char_u *s, *d;
2574
2575 d = res;
2576 for (s = res; *s; ++s)
2577 {
2578 if (s[0] == CAR && s[1] == NL)
2579 ++s;
2580 *d++ = *s;
2581 }
2582 *d = NUL;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002585 rettv->vval.v_string = res;
2586 res = NULL;
2587 }
2588
2589errret:
2590 if (infile != NULL)
2591 {
2592 mch_remove(infile);
2593 vim_free(infile);
2594 }
2595 if (res != NULL)
2596 vim_free(res);
2597 if (list != NULL)
2598 list_free(list);
2599}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
2601/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002602 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 */
2604 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002605f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002607 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608}
2609
2610/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002611 * "systemlist()" function
2612 */
2613 void
2614f_systemlist(typval_T *argvars, typval_T *rettv)
2615{
2616 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2617}
2618# endif // FEAT_EVAL
2619
2620#endif
2621
2622/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002623 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 * Don't do this when still processing a command or a mapping.
2625 * Don't do this when inside a ":normal" command.
2626 */
2627 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002628goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 return (p_im && stuff_empty() && typebuf_typed());
2631}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002632
2633/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002634 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002635 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2636 * - Remove any argument. E.g., "csh -f" -> "csh".
2637 * But don't allow a space in the path, so that this works:
2638 * "/usr/bin/csh --rcfile ~/.cshrc"
2639 * But don't do that for Windows, it's common to have a space in the path.
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002640 * Returns NULL when out of memory.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002641 */
2642 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002643get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002644{
2645 char_u *p;
2646
Bram Moolenaar4f974752019-02-17 17:44:42 +01002647#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002648 p = gettail(p_sh);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002649 p = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002650#else
2651 p = skiptowhite(p_sh);
2652 if (*p == NUL)
2653 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002654 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002655 p = vim_strsave(gettail(p_sh));
2656 }
2657 else
2658 {
2659 char_u *p1, *p2;
2660
Bram Moolenaar85a20022019-12-21 18:25:54 +01002661 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002662 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002663 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002664 if (vim_ispathsep(*p2))
2665 p1 = p2 + 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002666 p = vim_strnsave(p1, p - p1);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002667 }
2668#endif
2669 return p;
2670}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002671
2672/*
2673 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2674 * Also check for ":\\", which MS Internet Explorer accepts, return
2675 * URL_BACKSLASH.
2676 */
2677 int
2678path_is_url(char_u *p)
2679{
2680 if (STRNCMP(p, "://", (size_t)3) == 0)
2681 return URL_SLASH;
2682 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2683 return URL_BACKSLASH;
2684 return 0;
2685}
2686
2687/*
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002688 * Check if "fname" starts with "name://" or "name:\\".
2689 * Return URL_SLASH for "name://", URL_BACKSLASH for "name:\\".
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002690 * Return zero otherwise.
2691 */
2692 int
2693path_with_url(char_u *fname)
2694{
2695 char_u *p;
2696
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002697 // We accept alphabetic characters and a dash in scheme part.
2698 // RFC 3986 allows for more, but it increases the risk of matching
2699 // non-URL text.
2700
2701 // first character must be alpha
2702 if (!isalpha(*fname))
2703 return 0;
2704
2705 // check body: alpha or dash
itchyny94e7d342021-10-21 18:01:13 +01002706 for (p = fname + 1; (isalpha(*p) || (*p == '-')); ++p)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002707 ;
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002708
2709 // check last char is not a dash
2710 if (p[-1] == '-')
2711 return 0;
2712
2713 // "://" or ":\\" must follow
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002714 return path_is_url(p);
2715}
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002716
Bram Moolenaar3075a452021-11-17 15:51:52 +00002717#if defined(FEAT_EVAL) || defined(PROTO)
2718/*
2719 * Return the dictionary of v:event.
2720 * Save and clear the value in case it already has items.
2721 */
2722 dict_T *
2723get_v_event(save_v_event_T *sve)
2724{
2725 dict_T *v_event = get_vim_var_dict(VV_EVENT);
2726
2727 if (v_event->dv_hashtab.ht_used > 0)
2728 {
2729 // recursive use of v:event, save, make empty and restore later
2730 sve->sve_did_save = TRUE;
2731 sve->sve_hashtab = v_event->dv_hashtab;
2732 hash_init(&v_event->dv_hashtab);
2733 }
2734 else
2735 sve->sve_did_save = FALSE;
2736 return v_event;
2737}
2738
2739 void
2740restore_v_event(dict_T *v_event, save_v_event_T *sve)
2741{
2742 dict_free_contents(v_event);
2743 if (sve->sve_did_save)
2744 v_event->dv_hashtab = sve->sve_hashtab;
2745 else
2746 hash_init(&v_event->dv_hashtab);
2747}
2748#endif
2749
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002750/*
LemonBoy2bf52dd2022-04-09 18:17:34 +01002751 * Fires a ModeChanged autocmd event if appropriate.
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002752 */
2753 void
LemonBoy2bf52dd2022-04-09 18:17:34 +01002754may_trigger_modechanged()
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002755{
Bram Moolenaar3075a452021-11-17 15:51:52 +00002756#ifdef FEAT_EVAL
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002757 dict_T *v_event;
Bram Moolenaar3075a452021-11-17 15:51:52 +00002758 save_v_event_T save_v_event;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002759 char_u curr_mode[MODE_MAX_LENGTH];
2760 char_u pattern_buf[2 * MODE_MAX_LENGTH];
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002761
Bram Moolenaar61c4b042022-10-18 15:10:11 +01002762 // Skip this when got_int is set, the autocommand will not be executed.
2763 // Better trigger it next time.
2764 if (!has_modechanged() || got_int)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002765 return;
2766
LemonBoy2bf52dd2022-04-09 18:17:34 +01002767 get_mode(curr_mode);
2768 if (STRCMP(curr_mode, last_mode) == 0)
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002769 return;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002770
Bram Moolenaar3075a452021-11-17 15:51:52 +00002771 v_event = get_v_event(&save_v_event);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002772 (void)dict_add_string(v_event, "new_mode", curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002773 (void)dict_add_string(v_event, "old_mode", last_mode);
2774 dict_set_items_ro(v_event);
2775
2776 // concatenate modes in format "old_mode:new_mode"
LemonBoy2bf52dd2022-04-09 18:17:34 +01002777 vim_snprintf((char *)pattern_buf, sizeof(pattern_buf), "%s:%s", last_mode,
2778 curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002779
LemonBoy2bf52dd2022-04-09 18:17:34 +01002780 apply_autocmds(EVENT_MODECHANGED, pattern_buf, NULL, FALSE, curbuf);
2781 STRCPY(last_mode, curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002782
Bram Moolenaar3075a452021-11-17 15:51:52 +00002783 restore_v_event(v_event, &save_v_event);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002784#endif
2785}