blob: 638392252166271023539bcbb46f193a7f20301b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaarb3f74062020-02-26 16:16:53 +010017#if defined(__HAIKU__)
18# include <storage/FindDirectory.h>
19#endif
20
Bram Moolenaar0a52df52019-08-18 22:26:31 +020021#if defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020022# include <lm.h>
23#endif
24
Bram Moolenaar85a20022019-12-21 18:25:54 +010025#define URL_SLASH 1 // path_is_url() has found "://"
26#define URL_BACKSLASH 2 // path_is_url() has found ":\\"
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010027
Bram Moolenaar0a52df52019-08-18 22:26:31 +020028// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020029static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +020032 * get_leader_len() returns the length in bytes of the prefix of the given
33 * string which introduces a comment. If this string is not a comment then
34 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 * When "flags" is not NULL, it is set to point to the flags of the recognized
36 * comment leader.
37 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +020038 * If "include_space" is set, include trailing whitespace while calculating the
39 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_leader_len(
43 char_u *line,
44 char_u **flags,
45 int backward,
46 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +000047{
48 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +020049 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 int got_com = FALSE;
51 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +010052 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
53 char_u *string; // pointer to comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +020055 int middle_match_len = 0;
56 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +020057 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar81340392012-06-06 16:12:59 +020059 result = i = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +010060 while (VIM_ISWHITE(line[i])) // leading white space is ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 ++i;
62
63 /*
64 * Repeat to match several nested comment strings.
65 */
Bram Moolenaara4271d52011-05-10 13:38:27 +020066 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 {
68 /*
69 * scan through the 'comments' option for a match
70 */
71 found_one = FALSE;
72 for (list = curbuf->b_p_com; *list; )
73 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010074 // Get one option part into part_buf[]. Advance "list" to next
75 // one. Put "string" at start of string.
Bram Moolenaara4271d52011-05-10 13:38:27 +020076 if (!got_com && flags != NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +010077 *flags = list; // remember where flags started
Bram Moolenaara4271d52011-05-10 13:38:27 +020078 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
80 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +010081 if (string == NULL) // missing ':', ignore this part
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +010083 *string++ = NUL; // isolate flags from string
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar85a20022019-12-21 18:25:54 +010085 // If we found a middle match previously, use that match when this
86 // is not a middle or end.
Bram Moolenaara4271d52011-05-10 13:38:27 +020087 if (middle_match_len != 0
88 && vim_strchr(part_buf, COM_MIDDLE) == NULL
89 && vim_strchr(part_buf, COM_END) == NULL)
90 break;
91
Bram Moolenaar85a20022019-12-21 18:25:54 +010092 // When we already found a nested comment, only accept further
93 // nested comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
95 continue;
96
Bram Moolenaar85a20022019-12-21 18:25:54 +010097 // When 'O' flag present and using "O" command skip this one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
99 continue;
100
Bram Moolenaar85a20022019-12-21 18:25:54 +0100101 // Line contents and string must match.
102 // When string starts with white space, must have some white space
103 // (but the amount does not need to match, there might be a mix of
104 // TABs and spaces).
Bram Moolenaar1c465442017-03-12 20:10:05 +0100105 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100107 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100108 continue; // missing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +0100109 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 ++string;
111 }
112 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
113 ;
114 if (string[j] != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100115 continue; // string doesn't match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar85a20022019-12-21 18:25:54 +0100117 // When 'b' flag used, there must be white space or an
118 // end-of-line after the string in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100120 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 continue;
122
Bram Moolenaar85a20022019-12-21 18:25:54 +0100123 // We have found a match, stop searching unless this is a middle
124 // comment. The middle comment can be a substring of the end
125 // comment in which case it's better to return the length of the
126 // end comment and its flags. Thus we keep searching with middle
127 // and end matches and use an end match if it matches better.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200128 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
129 {
130 if (middle_match_len == 0)
131 {
132 middle_match_len = j;
133 saved_flags = prev_list;
134 }
135 continue;
136 }
137 if (middle_match_len != 0 && j > middle_match_len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100138 // Use this match instead of the middle match, since it's a
139 // longer thus better match.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200140 middle_match_len = 0;
141
142 if (middle_match_len == 0)
143 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 found_one = TRUE;
145 break;
146 }
147
Bram Moolenaara4271d52011-05-10 13:38:27 +0200148 if (middle_match_len != 0)
149 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100150 // Use the previously found middle match after failing to find a
151 // match with an end.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200152 if (!got_com && flags != NULL)
153 *flags = saved_flags;
154 i += middle_match_len;
155 found_one = TRUE;
156 }
157
Bram Moolenaar85a20022019-12-21 18:25:54 +0100158 // No match found, stop scanning.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 if (!found_one)
160 break;
161
Bram Moolenaar81340392012-06-06 16:12:59 +0200162 result = i;
163
Bram Moolenaar85a20022019-12-21 18:25:54 +0100164 // Include any trailing white space.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100165 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++i;
167
Bram Moolenaar81340392012-06-06 16:12:59 +0200168 if (include_space)
169 result = i;
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // If this comment doesn't nest, stop here.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200172 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (vim_strchr(part_buf, COM_NEST) == NULL)
174 break;
175 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200176 return result;
177}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200178
Bram Moolenaar81340392012-06-06 16:12:59 +0200179/*
180 * Return the offset at which the last comment in line starts. If there is no
181 * comment in the whole line, -1 is returned.
182 *
183 * When "flags" is not null, it is set to point to the flags describing the
184 * recognized comment leader.
185 */
186 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100187get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200188{
189 int result = -1;
190 int i, j;
191 int lower_check_bound = 0;
192 char_u *string;
193 char_u *com_leader;
194 char_u *com_flags;
195 char_u *list;
196 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200198
199 /*
200 * Repeat to match several nested comment strings.
201 */
202 i = (int)STRLEN(line);
203 while (--i >= lower_check_bound)
204 {
205 /*
206 * scan through the 'comments' option for a match
207 */
208 found_one = FALSE;
209 for (list = curbuf->b_p_com; *list; )
210 {
211 char_u *flags_save = list;
212
213 /*
214 * Get one option part into part_buf[]. Advance list to next one.
215 * put string at start of string.
216 */
217 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
218 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 if (string == NULL) // If everything is fine, this cannot actually
220 // happen.
Bram Moolenaar81340392012-06-06 16:12:59 +0200221 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 *string++ = NUL; // Isolate flags from string.
Bram Moolenaar81340392012-06-06 16:12:59 +0200223 com_leader = string;
224
225 /*
226 * Line contents and string must match.
227 * When string starts with white space, must have some white space
228 * (but the amount does not need to match, there might be a mix of
229 * TABs and spaces).
230 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100231 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200232 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100233 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200234 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100235 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200236 ++string;
237 }
238 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
239 /* do nothing */;
240 if (string[j] != NUL)
241 continue;
242
243 /*
244 * When 'b' flag used, there must be white space or an
245 * end-of-line after the string in the line.
246 */
247 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100248 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200249 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100250
Bram Moolenaar4af72592018-12-09 15:00:52 +0100251 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100252 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100253 // For a middlepart comment, only consider it to match if
254 // everything before the current position in the line is
255 // whitespace. Otherwise we would think we are inside a
256 // comment if the middle part appears somewhere in the middle
257 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100258 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
259 ;
260 if (j < i)
261 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200262 }
263
264 /*
265 * We have found a match, stop searching.
266 */
267 found_one = TRUE;
268
269 if (flags)
270 *flags = flags_save;
271 com_flags = flags_save;
272
273 break;
274 }
275
276 if (found_one)
277 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 char_u part_buf2[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200279 int len1, len2, off;
280
281 result = i;
282 /*
283 * If this comment nests, continue searching.
284 */
285 if (vim_strchr(part_buf, COM_NEST) != NULL)
286 continue;
287
288 lower_check_bound = i;
289
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Let's verify whether the comment leader found is a substring
291 // of other comment leaders. If it is, let's adjust the
292 // lower_check_bound so that we make sure that we have determined
293 // the comment leader correctly.
Bram Moolenaar81340392012-06-06 16:12:59 +0200294
Bram Moolenaar1c465442017-03-12 20:10:05 +0100295 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200296 ++com_leader;
297 len1 = (int)STRLEN(com_leader);
298
299 for (list = curbuf->b_p_com; *list; )
300 {
301 char_u *flags_save = list;
302
303 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
304 if (flags_save == com_flags)
305 continue;
306 string = vim_strchr(part_buf2, ':');
307 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100308 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200309 ++string;
310 len2 = (int)STRLEN(string);
311 if (len2 == 0)
312 continue;
313
Bram Moolenaar85a20022019-12-21 18:25:54 +0100314 // Now we have to verify whether string ends with a substring
315 // beginning the com_leader.
Bram Moolenaar81340392012-06-06 16:12:59 +0200316 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
317 {
318 --off;
319 if (!STRNCMP(string + off, com_leader, len2 - off))
320 {
321 if (i - off < lower_check_bound)
322 lower_check_bound = i - off;
323 }
324 }
325 }
326 }
327 }
328 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
331/*
332 * Return the number of window lines occupied by buffer line "lnum".
333 */
334 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100335plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return plines_win(curwin, lnum, TRUE);
338}
339
340 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100341plines_win(
342 win_T *wp,
343 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100344 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346#if defined(FEAT_DIFF) || defined(PROTO)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100347 // Check for filler lines above this buffer line. When folded the result
348 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
350}
351
352 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100353plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 return plines_win_nofill(curwin, lnum, TRUE);
356}
357
358 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100359plines_win_nofill(
360 win_T *wp,
361 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364#endif
365 int lines;
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 if (wp->w_width == 0)
368 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
370#ifdef FEAT_FOLDING
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000371 // Folded lines are handled just like an empty line.
Bram Moolenaar85a20022019-12-21 18:25:54 +0100372 // NOTE: Caller must handle lines that are MAYBE folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 if (lineFolded(wp, lnum) == TRUE)
374 return 1;
375#endif
376
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100377 if (!wp->w_p_wrap)
378 lines = 1
379#ifdef FEAT_PROP_POPUP
380 // add a line for each "below" aligned text property
381 + prop_count_below(wp->w_buffer, lnum)
382#endif
383 ;
384 else
385 lines = plines_win_nofold(wp, lnum);
386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (winheight > 0 && lines > wp->w_height)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000388 return wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 return lines;
390}
391
392/*
393 * Return number of window lines physical line "lnum" will occupy in window
394 * "wp". Does not care about folding, 'wrap' or 'diff'.
395 */
396 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100397plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
399 char_u *s;
400 long col;
401 int width;
Bram Moolenaar49a90792022-08-09 18:25:23 +0100402 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar49a90792022-08-09 18:25:23 +0100405 init_chartabsize_arg(&cts, wp, lnum, 0, s, s);
406 if (*s == NUL
407#ifdef FEAT_PROP_POPUP
408 && !cts.cts_has_prop_with_text
409#endif
410 )
411 return 1; // be quick for an empty line
412 win_linetabsize_cts(&cts, (colnr_T)MAXCOL);
413 clear_chartabsize_arg(&cts);
414 col = (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100416 // If list mode is on, then the '$' at the end of the line may take up one
417 // extra column.
Bram Moolenaareed9d462021-02-15 20:38:25 +0100418 if (wp->w_p_list && wp->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 col += 1;
420
421 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200422 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200424 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 if (width <= 0)
426 return 32000;
427 if (col <= width)
428 return 1;
429 col -= width;
430 width += win_col_off2(wp);
431 return (col + (width - 1)) / width + 1;
432}
433
434/*
435 * Like plines_win(), but only reports the number of physical screen lines
436 * used from the start of the line to the given column number.
437 */
438 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100439plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
441 long col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 int lines = 0;
443 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200444 char_u *line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100445 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100448 // Check for filler lines above this buffer line. When folded the result
449 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 lines = diff_check_fill(wp, lnum);
451#endif
452
453 if (!wp->w_p_wrap)
454 return lines + 1;
455
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (wp->w_width == 0)
457 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100459 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100461 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
462 while (*cts.cts_ptr != NUL && --column >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100464 cts.cts_vcol += win_lbr_chartabsize(&cts, NULL);
465 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 }
467
468 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100469 * If *cts.cts_ptr is a TAB, and the TAB is not displayed as ^I, and we're
470 * not in MODE_INSERT state, then col must be adjusted so that it
471 * represents the last screen position of the TAB. This only fixes an
472 * error when the TAB wraps from one screen line to the next (when
473 * 'columns' is not a multiple of 'ts') -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100475 col = cts.cts_vcol;
476 if (*cts.cts_ptr == TAB && (State & MODE_NORMAL)
Bram Moolenaar24959102022-05-07 20:01:16 +0100477 && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100478 col += win_lbr_chartabsize(&cts, NULL) - 1;
479 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480
481 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200482 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200484 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +0000485 if (width <= 0)
486 return 9999;
487
488 lines += 1;
489 if (col > width)
490 lines += (col - width) / (width + win_col_off2(wp)) + 1;
491 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492}
493
494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100495plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
497 int count = 0;
498
499 while (first <= last)
500 {
501#ifdef FEAT_FOLDING
502 int x;
503
Bram Moolenaar85a20022019-12-21 18:25:54 +0100504 // Check if there are any really folded lines, but also included lines
505 // that are maybe folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 x = foldedCount(wp, first, NULL);
507 if (x > 0)
508 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100509 ++count; // count 1 for "+-- folded" line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 first += x;
511 }
512 else
513#endif
514 {
515#ifdef FEAT_DIFF
516 if (first == wp->w_topline)
517 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
518 else
519#endif
520 count += plines_win(wp, first, TRUE);
521 ++first;
522 }
523 }
524 return (count);
525}
526
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100528gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100530 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
Bram Moolenaar85a20022019-12-21 18:25:54 +0100532 // When searching columns is sometimes put at the end of a line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100533 if (pos->col == MAXCOL)
534 return NUL;
535 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 if (has_mbyte)
537 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 return (int)*ptr;
539}
540
541 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (has_mbyte)
545 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 return (int)*ml_get_cursor();
547}
548
549/*
550 * Write a character at the current cursor position.
551 * It is directly written into the block.
552 */
553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100554pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
557 + curwin->w_cursor.col) = c;
558}
559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 * Skip to next part of an option argument: Skip space and comma.
562 */
563 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100564skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565{
566 if (*p == ',')
567 ++p;
568 while (*p == ' ')
569 ++p;
570 return p;
571}
572
573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 * check_status: called when the status bars for the buffer 'buf'
575 * need to be updated
576 */
577 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100578check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 win_T *wp;
581
Bram Moolenaar29323592016-07-24 22:04:11 +0200582 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (wp->w_buffer == buf && wp->w_status_height)
584 {
585 wp->w_redr_status = TRUE;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100586 set_must_redraw(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590/*
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100591 * Ask for a reply from the user, a 'y' or a 'n', with prompt "str" (which
592 * should have been translated already).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 * No other characters are accepted, the message is repeated until a valid
594 * reply is entered or CTRL-C is hit.
595 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
596 * from any buffers but directly from the user.
597 *
598 * return the 'y' or 'n'
599 */
600 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100601ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
603 int r = ' ';
604 int save_State = State;
605
Bram Moolenaar85a20022019-12-21 18:25:54 +0100606 if (exiting) // put terminal in raw mode for this question
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 settmode(TMODE_RAW);
608 ++no_wait_return;
609#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200610 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100612 State = MODE_CONFIRM; // mouse behaves like with :confirm
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200613 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200615 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 while (r != 'y' && r != 'n')
618 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100619 // same highlighting as for wait_return
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100620 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 if (direct)
622 r = get_keystroke();
623 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000624 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (r == Ctrl_C || r == ESC)
626 r = 'n';
Bram Moolenaar85a20022019-12-21 18:25:54 +0100627 msg_putchar(r); // show what you typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 out_flush();
629 }
630 --no_wait_return;
631 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 --no_mapping;
634 --allow_keys;
635
636 return r;
637}
638
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200639#if defined(FEAT_EVAL) || defined(PROTO)
640
641/*
LemonBoy2bf52dd2022-04-09 18:17:34 +0100642 * Returns the current mode as a string in "buf[MODE_MAX_LENGTH]", NUL
643 * terminated.
644 * The first character represents the major mode, the following ones the minor
645 * ones.
646 */
647 void
648get_mode(char_u *buf)
649{
650 int i = 0;
651
652 if (time_for_testing == 93784)
653 {
654 // Testing the two-character code.
655 buf[i++] = 'x';
656 buf[i++] = '!';
657 }
658#ifdef FEAT_TERMINAL
659 else if (term_use_loop())
660 buf[i++] = 't';
661#endif
662 else if (VIsual_active)
663 {
664 if (VIsual_select)
665 buf[i++] = VIsual_mode + 's' - 'v';
666 else
667 {
668 buf[i++] = VIsual_mode;
669 if (restart_VIsual_select)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100670 buf[i++] = 's';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100671 }
672 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100673 else if (State == MODE_HITRETURN || State == MODE_ASKMORE
674 || State == MODE_SETWSIZE
675 || State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100676 {
677 buf[i++] = 'r';
Bram Moolenaar24959102022-05-07 20:01:16 +0100678 if (State == MODE_ASKMORE)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100679 buf[i++] = 'm';
Bram Moolenaar24959102022-05-07 20:01:16 +0100680 else if (State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100681 buf[i++] = '?';
682 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100683 else if (State == MODE_EXTERNCMD)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100684 buf[i++] = '!';
Bram Moolenaar24959102022-05-07 20:01:16 +0100685 else if (State & MODE_INSERT)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100686 {
687 if (State & VREPLACE_FLAG)
688 {
689 buf[i++] = 'R';
690 buf[i++] = 'v';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100691 }
692 else
693 {
694 if (State & REPLACE_FLAG)
695 buf[i++] = 'R';
696 else
697 buf[i++] = 'i';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100698 }
zeertzjq590f3652022-04-29 11:29:54 +0100699
700 if (ins_compl_active())
701 buf[i++] = 'c';
702 else if (ctrl_x_mode_not_defined_yet())
703 buf[i++] = 'x';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100704 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100705 else if ((State & MODE_CMDLINE) || exmode_active)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100706 {
707 buf[i++] = 'c';
708 if (exmode_active == EXMODE_VIM)
709 buf[i++] = 'v';
710 else if (exmode_active == EXMODE_NORMAL)
711 buf[i++] = 'e';
712 }
713 else
714 {
715 buf[i++] = 'n';
716 if (finish_op)
717 {
718 buf[i++] = 'o';
719 // to be able to detect force-linewise/blockwise/characterwise
720 // operations
721 buf[i++] = motion_force;
722 }
723 else if (restart_edit == 'I' || restart_edit == 'R'
724 || restart_edit == 'V')
725 {
726 buf[i++] = 'i';
727 buf[i++] = restart_edit;
728 }
729#ifdef FEAT_TERMINAL
730 else if (term_in_normal_mode())
731 buf[i++] = 't';
732#endif
733 }
734
735 buf[i] = NUL;
736}
737
738/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200739 * "mode()" function
740 */
741 void
742f_mode(typval_T *argvars, typval_T *rettv)
743{
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200744 char_u buf[MODE_MAX_LENGTH];
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200745
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200746 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
747 return;
748
LemonBoy2bf52dd2022-04-09 18:17:34 +0100749 get_mode(buf);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200750
Bram Moolenaar85a20022019-12-21 18:25:54 +0100751 // Clear out the minor mode when the argument is not a non-zero number or
752 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200753 if (!non_zero_arg(&argvars[0]))
754 buf[1] = NUL;
755
756 rettv->vval.v_string = vim_strsave(buf);
757 rettv->v_type = VAR_STRING;
758}
759
760 static void
761may_add_state_char(garray_T *gap, char_u *include, int c)
762{
763 if (include == NULL || vim_strchr(include, c) != NULL)
764 ga_append(gap, c);
765}
766
767/*
768 * "state()" function
769 */
770 void
771f_state(typval_T *argvars, typval_T *rettv)
772{
773 garray_T ga;
774 char_u *include = NULL;
775 int i;
776
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200777 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
778 return;
779
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200780 ga_init2(&ga, 1, 20);
781 if (argvars[0].v_type != VAR_UNKNOWN)
782 include = tv_get_string(&argvars[0]);
783
784 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
785 may_add_state_char(&ga, include, 'm');
786 if (op_pending())
787 may_add_state_char(&ga, include, 'o');
788 if (autocmd_busy)
789 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200790 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200791 may_add_state_char(&ga, include, 'a');
792
793# ifdef FEAT_JOB_CHANNEL
794 if (channel_in_blocking_wait())
795 may_add_state_char(&ga, include, 'w');
796# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200797 if (!get_was_safe_state())
798 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200799 for (i = 0; i < get_callback_depth() && i < 3; ++i)
800 may_add_state_char(&ga, include, 'c');
801 if (msg_scrolled > 0)
802 may_add_state_char(&ga, include, 's');
803
804 rettv->v_type = VAR_STRING;
805 rettv->vval.v_string = ga.ga_data;
806}
807
808#endif // FEAT_EVAL
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810/*
811 * Get a key stroke directly from the user.
812 * Ignores mouse clicks and scrollbar events, except a click for the left
813 * button (used at the more prompt).
814 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
815 * Disadvantage: typeahead is ignored.
816 * Translates the interrupt character for unix to ESC.
817 */
818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100821 char_u *buf = NULL;
822 int buflen = 150;
823 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 int len = 0;
825 int n;
826 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000827 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
Bram Moolenaar85a20022019-12-21 18:25:54 +0100829 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 for (;;)
831 {
832 cursor_on();
833 out_flush();
834
Bram Moolenaar85a20022019-12-21 18:25:54 +0100835 // Leave some room for check_termcode() to insert a key code into (max
836 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
837 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100838 maxlen = (buflen - 6 - len) / 3;
839 if (buf == NULL)
840 buf = alloc(buflen);
841 else if (maxlen < 10)
842 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100843 char_u *t_buf = buf;
844
Bram Moolenaar85a20022019-12-21 18:25:54 +0100845 // Need some more space. This might happen when receiving a long
846 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100847 buflen += 100;
848 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100849 if (buf == NULL)
850 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100851 maxlen = (buflen - 6 - len) / 3;
852 }
853 if (buf == NULL)
854 {
855 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100856 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100857 }
858
Bram Moolenaar85a20022019-12-21 18:25:54 +0100859 // First time: blocking wait. Second time: wait up to 100ms for a
860 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100861 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 if (n > 0)
863 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100864 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200865 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000867 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000869 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100870 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaar85a20022019-12-21 18:25:54 +0100872 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100873 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000874 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000876
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100878 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100879 if (must_redraw != 0 && !need_wait_return && (State
880 & (MODE_CMDLINE | MODE_HITRETURN | MODE_ASKMORE)) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100881 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100882 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100883 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100884 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100885 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100886 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100887 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100888 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100890 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 continue;
892
Bram Moolenaar85a20022019-12-21 18:25:54 +0100893 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 n = buf[0];
895 if (n == K_SPECIAL)
896 {
897 n = TO_SPECIAL(buf[1], buf[2]);
898 if (buf[1] == KS_MODIFIER
899 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100900 || (is_mouse_key(n) && n != K_LEFTMOUSE)
901#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 || n == K_VER_SCROLLBAR
903 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905 )
906 {
907 if (buf[1] == KS_MODIFIER)
908 mod_mask = buf[2];
909 len -= 3;
910 if (len > 0)
911 mch_memmove(buf, buf + 3, (size_t)len);
912 continue;
913 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if (has_mbyte)
917 {
918 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100919 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100920 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 n = (*mb_ptr2char)(buf);
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef UNIX
924 if (n == intr_char)
925 n = ESC;
926#endif
927 break;
928 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100929 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931 mapped_ctrl_c = save_mapped_ctrl_c;
932 return n;
933}
934
935/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000936 * Get a number from the user.
937 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 */
939 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100940get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100941 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100942 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 int n = 0;
945 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000946 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000948 if (mouse_used != NULL)
949 *mouse_used = FALSE;
950
Bram Moolenaar85a20022019-12-21 18:25:54 +0100951 // When not printing messages, the user won't know what to type, return a
952 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (msg_silent != 0)
954 return 0;
955
956#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100957 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958#endif
959 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100960 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 for (;;)
962 {
963 windgoto(msg_row, msg_col);
964 c = safe_vgetc();
965 if (VIM_ISDIGIT(c))
966 {
967 n = n * 10 + c - '0';
968 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000969 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
972 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000973 if (typed > 0)
974 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100975 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000976 --typed;
977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000980 else if (mouse_used != NULL && c == K_LEFTMOUSE)
981 {
982 *mouse_used = TRUE;
983 n = mouse_row + 1;
984 break;
985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 else if (n == 0 && c == ':' && colon)
987 {
988 stuffcharReadbuff(':');
989 if (!exmode_active)
990 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100991 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 do_redraw = FALSE;
993 break;
994 }
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +0200995 else if (c == Ctrl_C || c == ESC || c == 'q')
996 {
997 n = 0;
998 break;
999 }
1000 else if (c == CAR || c == NL )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 break;
1002 }
1003 --no_mapping;
1004 --allow_keys;
1005 return n;
1006}
1007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008/*
1009 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001010 * When "mouse_used" is not NULL allow using the mouse and in that case return
1011 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001012 */
1013 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001014prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001015{
1016 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001017 int save_cmdline_row;
1018 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001019
Bram Moolenaar85a20022019-12-21 18:25:54 +01001020 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001021 if (mouse_used != NULL)
Bram Moolenaareebd5552020-06-10 15:45:57 +02001022 msg_puts(_("Type number and <Enter> or click with the mouse (q or empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001023 else
Bram Moolenaareebd5552020-06-10 15:45:57 +02001024 msg_puts(_("Type number and <Enter> (q or empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001025
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001026 // Set the state such that text can be selected/copied/pasted and we still
1027 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1028 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001029 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001030 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001031 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001032 State = MODE_CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001033 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001034 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001035
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001036 i = get_number(TRUE, mouse_used);
1037 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001039 // don't call wait_return() now
1040 if (msg_row > 0)
1041 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001042 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +01001043 msg_didany = FALSE;
1044 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001045 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001046 else
1047 cmdline_row = save_cmdline_row;
1048 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001049 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001050 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001052 return i;
1053}
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001056msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
1058 long pn;
1059
Bram Moolenaar85a20022019-12-21 18:25:54 +01001060 if (global_busy // no messages now, wait until global is finished
1061 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 return;
1063
Bram Moolenaar85a20022019-12-21 18:25:54 +01001064 // We don't want to overwrite another important message, but do overwrite
1065 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1066 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001067 if (keep_msg != NULL && !keep_msg_more)
1068 return;
1069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (n > 0)
1071 pn = n;
1072 else
1073 pn = -n;
1074
1075 if (pn > p_report)
1076 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001077 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001078 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001079 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001081 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001082 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001084 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1085 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (msg(msg_buf))
1087 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001088 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001089 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092}
1093
1094/*
1095 * flush map and typeahead buffers and give a warning for an error
1096 */
1097 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001098beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 if (emsg_silent == 0)
1101 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001102 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001103 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105}
1106
1107/*
Bram Moolenaaraae97622022-04-13 14:28:07 +01001108 * Give a warning for an error. "val" is one of the BO_ values, e.g., BO_OPER.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 */
1110 void
Bram Moolenaaraae97622022-04-13 14:28:07 +01001111vim_beep(unsigned val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001113#ifdef FEAT_EVAL
1114 called_vim_beep = TRUE;
1115#endif
1116
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001117 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001119 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1120 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001121#ifdef ELAPSED_FUNC
1122 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001123 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001124
Bram Moolenaar85a20022019-12-21 18:25:54 +01001125 // Only beep once per half a second, otherwise a sequence of beeps
1126 // would freeze Vim.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001127 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1128 {
1129 did_init = TRUE;
1130 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001132 if (p_vb
1133#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01001134 // While the GUI is starting up the termcap is set for
1135 // the GUI but the output still goes to a terminal.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001136 && !(gui.in_use && gui.starting)
1137#endif
1138 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001139 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001140 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001141#ifdef FEAT_VTP
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 // No restore color information, refresh the screen.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001143 if (has_vtp_working() != 0
1144# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001145 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001146# endif
1147 )
1148 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001149 redraw_later(UPD_CLEAR);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001150 update_screen(0);
1151 redrawcmd();
1152 }
1153#endif
1154 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001155 else
1156 out_char(BELL);
1157#ifdef ELAPSED_FUNC
1158 }
1159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161
Bram Moolenaar85a20022019-12-21 18:25:54 +01001162 // When 'debug' contains "beep" produce a message. If we are sourcing
1163 // a script or executing a function give the user a hint where the beep
1164 // comes from.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 if (vim_strchr(p_debug, 'e') != NULL)
1166 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001167 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001168 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171}
1172
1173/*
1174 * To get the "real" home directory:
1175 * - get value of $HOME
1176 * For Unix:
1177 * - go to that directory
1178 * - do mch_dirname() to get the real name of that directory.
1179 * This also works with mounts and links.
1180 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001181 * For Windows:
1182 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001185init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
1187 char_u *var;
1188
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001190 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#ifdef VMS
1193 var = mch_getenv((char_u *)"SYS$LOGIN");
1194#else
1195 var = mch_getenv((char_u *)"HOME");
1196#endif
1197
Bram Moolenaar4f974752019-02-17 17:44:42 +01001198#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001200 * Typically, $HOME is not defined on Windows, unless the user has
1201 * specifically defined it for Vim's sake. However, on Windows NT
1202 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1203 * each user. Try constructing $HOME from these.
1204 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001205 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001206 {
1207 char_u *homedrive, *homepath;
1208
1209 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1210 homepath = mch_getenv((char_u *)"HOMEPATH");
1211 if (homepath == NULL || *homepath == NUL)
1212 homepath = (char_u *)"\\";
1213 if (homedrive != NULL
1214 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1215 {
1216 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1217 if (NameBuff[0] != NUL)
1218 var = NameBuff;
1219 }
1220 }
1221
1222 if (var == NULL)
1223 var = mch_getenv((char_u *)"USERPROFILE");
1224
1225 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 * Weird but true: $HOME may contain an indirect reference to another
1227 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1228 * when $HOME is being set.
1229 */
1230 if (var != NULL && *var == '%')
1231 {
1232 char_u *p;
1233 char_u *exp;
1234
1235 p = vim_strchr(var + 1, '%');
1236 if (p != NULL)
1237 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001238 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 exp = mch_getenv(NameBuff);
1240 if (exp != NULL && *exp != NUL
1241 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1242 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001243 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246 }
1247 }
1248
Bram Moolenaar85a20022019-12-21 18:25:54 +01001249 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001250 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar05159a02005-02-26 23:04:13 +00001252 if (enc_utf8 && var != NULL)
1253 {
1254 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001255 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001256
Bram Moolenaar85a20022019-12-21 18:25:54 +01001257 // Convert from active codepage to UTF-8. Other conversions are
1258 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001259 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001260 if (pp != NULL)
1261 {
1262 homedir = pp;
1263 return;
1264 }
1265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 /*
1268 * Default home dir is C:/
1269 * Best assumption we can make in such a situation.
1270 */
1271 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001272 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (var != NULL)
1276 {
1277#ifdef UNIX
1278 /*
1279 * Change to the directory and get the actual path. This resolves
1280 * links. Don't do it when we can't return.
1281 */
1282 if (mch_dirname(NameBuff, MAXPATHL) == OK
1283 && mch_chdir((char *)NameBuff) == 0)
1284 {
1285 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1286 var = IObuff;
1287 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001288 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290#endif
1291 homedir = vim_strsave(var);
1292 }
1293}
1294
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001295#if defined(EXITFREE) || defined(PROTO)
1296 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001297free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001298{
1299 vim_free(homedir);
1300}
Bram Moolenaar24305862012-08-15 14:05:05 +02001301
Bram Moolenaar24305862012-08-15 14:05:05 +02001302 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001303free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001304{
1305 ga_clear_strings(&ga_users);
1306}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001307#endif
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001310 * Call expand_env() and store the result in an allocated string.
1311 * This is not very memory efficient, this expects the result to be freed
1312 * again soon.
1313 */
1314 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001315expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001316{
1317 return expand_env_save_opt(src, FALSE);
1318}
1319
1320/*
1321 * Idem, but when "one" is TRUE handle the string as one file name, only
1322 * expand "~" at the start.
1323 */
1324 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001325expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001326{
1327 char_u *p;
1328
1329 p = alloc(MAXPATHL);
1330 if (p != NULL)
1331 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1332 return p;
1333}
1334
1335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Expand environment variable with path name.
1337 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001338 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 * If anything fails no expansion is done and dst equals src.
1340 */
1341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001342expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001343 char_u *src, // input string e.g. "$HOME/vim.hlp"
1344 char_u *dst, // where to put the result
1345 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001347 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348}
1349
1350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001351expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001352 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1353 char_u *dst, // where to put the result
1354 int dstlen, // maximum length of the result
1355 int esc, // escape spaces in expanded variables
1356 int one, // "srcp" is one file name
1357 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001359 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 char_u *tail;
1361 int c;
1362 char_u *var;
1363 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001364 int mustfree; // var was allocated, need to free it later
1365 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001366 int startstr_len = 0;
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001367#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1368 char_u *save_dst = dst;
1369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001371 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001372 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001373
1374 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001375 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 while (*src && dstlen > 0)
1377 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001378#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001379 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001380 if (src[0] == '`' && src[1] == '=')
1381 {
1382 size_t len;
1383
1384 var = src;
1385 src += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02001386 (void)skip_expr(&src, NULL);
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001387 if (*src == '`')
1388 ++src;
1389 len = src - var;
1390 if (len > (size_t)dstlen)
1391 len = dstlen;
1392 vim_strncpy(dst, var, len);
1393 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001394 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001395 continue;
1396 }
1397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001399 if ((*src == '$'
1400#ifdef VMS
1401 && at_start
1402#endif
1403 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001404#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 || *src == '%'
1406#endif
1407 || (*src == '~' && at_start))
1408 {
1409 mustfree = FALSE;
1410
1411 /*
1412 * The variable name is copied into dst temporarily, because it may
1413 * be a string in read-only memory and a NUL needs to be appended.
1414 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001415 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 {
1417 tail = src + 1;
1418 var = dst;
1419 c = dstlen - 1;
1420
1421#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001422 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (*tail == '{' && !vim_isIDc('{'))
1424 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001425 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 while (c-- > 0 && *tail && *tail != '}')
1427 *var++ = *tail++;
1428 }
1429 else
1430#endif
1431 {
1432 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001433#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 || (*src == '%' && *tail != '%')
1435#endif
1436 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001440#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441# ifdef UNIX
1442 if (src[1] == '{' && *tail != '}')
1443# else
1444 if (*src == '%' && *tail != '%')
1445# endif
1446 var = NULL;
1447 else
1448 {
1449# ifdef UNIX
1450 if (src[1] == '{')
1451# else
1452 if (*src == '%')
1453#endif
1454 ++tail;
1455#endif
1456 *var = NUL;
1457 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001458#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460#endif
1461 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001462 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 else if ( src[1] == NUL
1464 || vim_ispathsep(src[1])
1465 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1466 {
1467 var = homedir;
1468 tail = src + 1;
1469 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001470 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
1472#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1473 /*
1474 * Copy ~user to dst[], so we can put a NUL after it.
1475 */
1476 tail = src;
1477 var = dst;
1478 c = dstlen - 1;
1479 while ( c-- > 0
1480 && *tail
1481 && vim_isfilec(*tail)
1482 && !vim_ispathsep(*tail))
1483 *var++ = *tail++;
1484 *var = NUL;
1485# ifdef UNIX
1486 /*
1487 * If the system supports getpwnam(), use it.
1488 * Otherwise, or if getpwnam() fails, the shell is used to
1489 * expand ~user. This is slower and may fail if the shell
1490 * does not support ~user (old versions of /bin/sh).
1491 */
1492# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1493 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001494 // Note: memory allocated by getpwnam() is never freed.
1495 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001496 struct passwd *pw = (*dst == NUL)
1497 ? NULL : getpwnam((char *)dst + 1);
1498
1499 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 if (var == NULL)
1502# endif
1503 {
1504 expand_T xpc;
1505
1506 ExpandInit(&xpc);
1507 xpc.xp_context = EXPAND_FILES;
1508 var = ExpandOne(&xpc, dst, NULL,
1509 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 mustfree = TRUE;
1511 }
1512
Bram Moolenaar85a20022019-12-21 18:25:54 +01001513# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /*
1515 * USER_HOME is a comma-separated list of
1516 * directories to search for the user account in.
1517 */
1518 {
1519 char_u test[MAXPATHL], paths[MAXPATHL];
1520 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001521 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
1523 STRCPY(paths, USER_HOME);
1524 next_path = paths;
1525 while (*next_path)
1526 {
1527 for (path = next_path; *next_path && *next_path != ',';
1528 next_path++);
1529 if (*next_path)
1530 *next_path++ = NUL;
1531 STRCPY(test, path);
1532 STRCAT(test, "/");
1533 STRCAT(test, dst + 1);
1534 if (mch_stat(test, &st) == 0)
1535 {
1536 var = alloc(STRLEN(test) + 1);
1537 STRCPY(var, test);
1538 mustfree = TRUE;
1539 break;
1540 }
1541 }
1542 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001543# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001545 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001547 tail = (char_u *)""; // for gcc
1548#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550
1551#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001552 // If 'shellslash' is set change backslashes to forward slashes.
1553 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1555 {
1556 char_u *p = vim_strsave(var);
1557
1558 if (p != NULL)
1559 {
1560 if (mustfree)
1561 vim_free(var);
1562 var = p;
1563 mustfree = TRUE;
1564 forward_slash(var);
1565 }
1566 }
1567#endif
1568
Bram Moolenaar85a20022019-12-21 18:25:54 +01001569 // If "var" contains white space, escape it with a backslash.
1570 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1572 {
1573 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1574
1575 if (p != NULL)
1576 {
1577 if (mustfree)
1578 vim_free(var);
1579 var = p;
1580 mustfree = TRUE;
1581 }
1582 }
1583
1584 if (var != NULL && *var != NUL
1585 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1586 {
1587 STRCPY(dst, var);
1588 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001589 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001590 // if var[] ends in a path separator and tail[] starts
1591 // with it, skip a character
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001592 if (after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001594 && (dst == save_dst || dst[-1] != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595#endif
1596 && vim_ispathsep(*tail))
1597 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001598 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 src = tail;
1600 copy_char = FALSE;
1601 }
1602 if (mustfree)
1603 vim_free(var);
1604 }
1605
Bram Moolenaar85a20022019-12-21 18:25:54 +01001606 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001609 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001610 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1611 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
1613 at_start = FALSE;
1614 if (src[0] == '\\' && src[1] != NUL)
1615 {
1616 *dst++ = *src++;
1617 --dstlen;
1618 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001619 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001621 if (dstlen > 0)
1622 {
1623 *dst++ = *src++;
1624 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001625
Bram Moolenaar1c864092017-08-06 18:15:45 +02001626 if (startstr != NULL && src - startstr_len >= srcp
1627 && STRNCMP(src - startstr_len, startstr,
1628 startstr_len) == 0)
1629 at_start = TRUE;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 *dst = NUL;
1635}
1636
1637/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001638 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1639 * the length of "name/". Otherwise return "pend".
1640 */
1641 static char_u *
1642remove_tail(char_u *p, char_u *pend, char_u *name)
1643{
1644 int len = (int)STRLEN(name) + 1;
1645 char_u *newend = pend - len;
1646
1647 if (newend >= p
1648 && fnamencmp(newend, name, len - 1) == 0
1649 && (newend == p || after_pathsep(p, newend)))
1650 return newend;
1651 return pend;
1652}
1653
1654/*
1655 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1656 * Return NULL if not, return its name in allocated memory otherwise.
1657 */
1658 static char_u *
1659vim_version_dir(char_u *vimdir)
1660{
1661 char_u *p;
1662
1663 if (vimdir == NULL || *vimdir == NUL)
1664 return NULL;
1665 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1666 if (p != NULL && mch_isdir(p))
1667 return p;
1668 vim_free(p);
1669 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1670 if (p != NULL && mch_isdir(p))
Bram Moolenaar022f9ef2022-07-02 17:36:31 +01001671 {
1672 char_u *fname = concat_fnames(p, (char_u *)"defaults.vim", TRUE);
1673
1674 // Check that "defaults.vim" exists in this directory, to avoid picking
1675 // up a stray "runtime" directory, it would make many tests fail in
1676 // mysterious ways.
1677 if (fname != NULL)
1678 {
1679 int exists = file_is_readable(fname);
1680
1681 vim_free(fname);
1682 if (exists)
1683 return p;
1684 }
1685 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001686 vim_free(p);
1687 return NULL;
1688}
1689
1690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 * Vim's version of getenv().
1692 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001693 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001694 * "mustfree" is set to TRUE when returned is allocated, it must be
1695 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 */
1697 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001698vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001700 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 char_u *pend;
1702 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001703#ifdef MSWIN
1704 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001706 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (STRCMP(name, "HOME") == 0)
1708 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001710 // Use Wide function
1711 wn = enc_to_utf16(name, NULL);
1712 if (wn == NULL)
1713 return NULL;
1714
1715 wp = _wgetenv(wn);
1716 vim_free(wn);
1717
1718 if (wp != NULL && *wp == NUL) // empty is the same as not set
1719 wp = NULL;
1720
1721 if (wp != NULL)
1722 {
1723 p = utf16_to_enc(wp, NULL);
1724 if (p == NULL)
1725 return NULL;
1726
1727 *mustfree = TRUE;
1728 return p;
1729 }
1730#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001732 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 p = NULL;
1734
1735 if (p != NULL)
1736 return p;
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001737
1738# ifdef __HAIKU__
1739 // special handling for user settings directory...
1740 if (STRCMP(name, "BE_USER_SETTINGS") == 0)
1741 {
1742 static char userSettingsPath[MAXPATHL];
1743
1744 if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false,
1745 userSettingsPath, MAXPATHL) == B_OK)
1746 return (char_u *)userSettingsPath;
1747 else
1748 return NULL;
1749 }
1750# endif
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001753 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1755 if (!vimruntime && STRCMP(name, "VIM") != 0)
1756 return NULL;
1757
1758 /*
1759 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1760 * Don't do this when default_vimruntime_dir is non-empty.
1761 */
1762 if (vimruntime
1763#ifdef HAVE_PATHDEF
1764 && *default_vimruntime_dir == NUL
1765#endif
1766 )
1767 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001768#ifdef MSWIN
1769 // Use Wide function
1770 wp = _wgetenv(L"VIM");
1771 if (wp != NULL && *wp == NUL) // empty is the same as not set
1772 wp = NULL;
1773 if (wp != NULL)
1774 {
1775 char_u *q = utf16_to_enc(wp, NULL);
1776 if (q != NULL)
1777 {
1778 p = vim_version_dir(q);
1779 *mustfree = TRUE;
1780 if (p == NULL)
1781 p = q;
1782 }
1783 }
1784#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001786 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 p = NULL;
1788 if (p != NULL)
1789 {
1790 p = vim_version_dir(p);
1791 if (p != NULL)
1792 *mustfree = TRUE;
1793 else
1794 p = mch_getenv((char_u *)"VIM");
1795 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798
1799 /*
1800 * When expanding $VIM or $VIMRUNTIME fails, try using:
1801 * - the directory name from 'helpfile' (unless it contains '$')
1802 * - the executable name from argv[0]
1803 */
1804 if (p == NULL)
1805 {
1806 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1807 p = p_hf;
1808#ifdef USE_EXE_NAME
1809 /*
1810 * Use the name of the executable, obtained from argv[0].
1811 */
1812 else
1813 p = exe_name;
1814#endif
1815 if (p != NULL)
1816 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001817 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 pend = gettail(p);
1819
Bram Moolenaar85a20022019-12-21 18:25:54 +01001820 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (p == p_hf)
1822 pend = remove_tail(p, pend, (char_u *)"doc");
1823
1824#ifdef USE_EXE_NAME
1825# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001826 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (p == exe_name)
1828 {
1829 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001830 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001832 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1833 if (pend1 != pend)
1834 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001835 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001836 if (pnew != NULL)
1837 {
1838 STRNCPY(pnew, p, (pend1 - p));
1839 STRCPY(pnew + (pend1 - p), "Resources/vim");
1840 p = pnew;
1841 pend = p + STRLEN(p);
1842 }
1843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001846 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (p == exe_name)
1848 pend = remove_tail(p, pend, (char_u *)"src");
1849#endif
1850
Bram Moolenaar85a20022019-12-21 18:25:54 +01001851 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (!vimruntime)
1853 {
1854 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1855 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1856 }
1857
Bram Moolenaar85a20022019-12-21 18:25:54 +01001858 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001859 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001862#ifdef MACOS_X
1863 if (p == exe_name || p == p_hf)
1864#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001865 // check that the result is a directory name
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001866 p = vim_strnsave(p, pend - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001869 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 else
1871 {
1872#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001873 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1875 {
1876 vim_free(p);
1877 p = pend;
1878 }
1879#endif
1880 *mustfree = TRUE;
1881 }
1882 }
1883 }
1884
1885#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001886 // When there is a pathdef.c file we can use default_vim_dir and
1887 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (p == NULL)
1889 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001890 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (vimruntime && *default_vimruntime_dir != NUL)
1892 {
1893 p = default_vimruntime_dir;
1894 *mustfree = FALSE;
1895 }
1896 else if (*default_vim_dir != NUL)
1897 {
1898 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1899 *mustfree = TRUE;
1900 else
1901 {
1902 p = default_vim_dir;
1903 *mustfree = FALSE;
1904 }
1905 }
1906 }
1907#endif
1908
1909 /*
1910 * Set the environment variable, so that the new value can be found fast
1911 * next time, and others can also use it (e.g. Perl).
1912 */
1913 if (p != NULL)
1914 {
1915 if (vimruntime)
1916 {
1917 vim_setenv((char_u *)"VIMRUNTIME", p);
1918 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 else
1921 {
1922 vim_setenv((char_u *)"VIM", p);
1923 didset_vim = TRUE;
1924 }
1925 }
1926 return p;
1927}
1928
Bram Moolenaar137374f2018-05-13 15:59:50 +02001929 void
1930vim_unsetenv(char_u *var)
1931{
1932#ifdef HAVE_UNSETENV
1933 unsetenv((char *)var);
1934#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001935 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001936#endif
1937}
1938
LemonBoy77142312022-04-15 20:50:46 +01001939/*
1940 * Removes environment variable "name" and take care of side effects.
1941 */
1942 void
1943vim_unsetenv_ext(char_u *var)
1944{
1945 vim_unsetenv(var);
1946
1947 // "homedir" is not cleared, keep using the old value until $HOME is set.
1948 if (STRICMP(var, "VIM") == 0)
1949 didset_vim = FALSE;
1950 else if (STRICMP(var, "VIMRUNTIME") == 0)
1951 didset_vimruntime = FALSE;
1952}
Bram Moolenaar137374f2018-05-13 15:59:50 +02001953
Bram Moolenaar092e09c2022-04-15 23:29:23 +01001954#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001956 * Set environment variable "name" and take care of side effects.
1957 */
1958 void
1959vim_setenv_ext(char_u *name, char_u *val)
1960{
1961 vim_setenv(name, val);
1962 if (STRICMP(name, "HOME") == 0)
1963 init_homedir();
1964 else if (didset_vim && STRICMP(name, "VIM") == 0)
1965 didset_vim = FALSE;
LemonBoy77142312022-04-15 20:50:46 +01001966 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001967 didset_vimruntime = FALSE;
1968}
Dominique Pelle748b3082022-01-08 12:41:16 +00001969#endif
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001970
1971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 * Our portable version of setenv.
1973 */
1974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001975vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977#ifdef HAVE_SETENV
1978 mch_setenv((char *)name, (char *)val, 1);
1979#else
1980 char_u *envbuf;
1981
1982 /*
1983 * Putenv does not copy the string, it has to remain
1984 * valid. The allocated memory will never be freed.
1985 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001986 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (envbuf != NULL)
1988 {
1989 sprintf((char *)envbuf, "%s=%s", name, val);
1990 putenv((char *)envbuf);
1991 }
1992#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001993#ifdef FEAT_GETTEXT
1994 /*
1995 * When setting $VIMRUNTIME adjust the directory to find message
1996 * translations to $VIMRUNTIME/lang.
1997 */
1998 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1999 {
2000 char_u *buf = concat_str(val, (char_u *)"/lang");
2001
2002 if (buf != NULL)
2003 {
2004 bindtextdomain(VIMPACKAGE, (char *)buf);
2005 vim_free(buf);
2006 }
2007 }
2008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009}
2010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011/*
2012 * Function given to ExpandGeneric() to obtain an environment variable name.
2013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002015get_env_name(
2016 expand_T *xp UNUSED,
2017 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
Bram Moolenaard0573012017-10-28 21:11:06 +02002019# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002021 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 */
2023 return NULL;
2024# else
2025# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01002026 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 extern char **environ;
2028# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002029# define ENVNAMELEN 100
2030 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 char_u *str;
2032 int n;
2033
2034 str = (char_u *)environ[idx];
2035 if (str == NULL)
2036 return NULL;
2037
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002038 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 if (str[n] == '=' || str[n] == NUL)
2041 break;
2042 name[n] = str[n];
2043 }
2044 name[n] = NUL;
2045 return name;
2046# endif
2047}
Bram Moolenaar24305862012-08-15 14:05:05 +02002048
2049/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002050 * Add a user name to the list of users in ga_users.
2051 * Do nothing if user name is NULL or empty.
2052 */
2053 static void
2054add_user(char_u *user, int need_copy)
2055{
2056 char_u *user_copy = (user != NULL && need_copy)
2057 ? vim_strsave(user) : user;
2058
2059 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2060 {
2061 if (need_copy)
2062 vim_free(user);
2063 return;
2064 }
2065 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2066}
2067
2068/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002069 * Find all user names for user completion.
2070 * Done only once and then cached.
2071 */
2072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002073init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002074{
Bram Moolenaar24305862012-08-15 14:05:05 +02002075 static int lazy_init_done = FALSE;
2076
2077 if (lazy_init_done)
2078 return;
2079
2080 lazy_init_done = TRUE;
2081 ga_init2(&ga_users, sizeof(char_u *), 20);
2082
2083# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2084 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002085 struct passwd* pw;
2086
2087 setpwent();
2088 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002089 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002090 endpwent();
2091 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002092# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002093 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002094 DWORD nusers = 0, ntotal = 0, i;
2095 PUSER_INFO_0 uinfo;
2096
2097 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2098 &nusers, &ntotal, NULL) == NERR_Success)
2099 {
2100 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002101 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002102
2103 NetApiBufferFree(uinfo);
2104 }
2105 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002106# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002107# if defined(HAVE_GETPWNAM)
2108 {
2109 char_u *user_env = mch_getenv((char_u *)"USER");
2110
2111 // The $USER environment variable may be a valid remote user name (NIS,
2112 // LDAP) not already listed by getpwent(), as getpwent() only lists
2113 // local user names. If $USER is not already listed, check whether it
2114 // is a valid remote user name using getpwnam() and if it is, add it to
2115 // the list of user names.
2116
2117 if (user_env != NULL && *user_env != NUL)
2118 {
2119 int i;
2120
2121 for (i = 0; i < ga_users.ga_len; i++)
2122 {
2123 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2124
2125 if (STRCMP(local_user, user_env) == 0)
2126 break;
2127 }
2128
2129 if (i == ga_users.ga_len)
2130 {
2131 struct passwd *pw = getpwnam((char *)user_env);
2132
2133 if (pw != NULL)
2134 add_user((char_u *)pw->pw_name, TRUE);
2135 }
2136 }
2137 }
2138# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002139}
2140
2141/*
2142 * Function given to ExpandGeneric() to obtain an user names.
2143 */
2144 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002145get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002146{
2147 init_users();
2148 if (idx < ga_users.ga_len)
2149 return ((char_u **)ga_users.ga_data)[idx];
2150 return NULL;
2151}
2152
2153/*
2154 * Check whether name matches a user name. Return:
2155 * 0 if name does not match any user name.
2156 * 1 if name partially matches the beginning of a user name.
2157 * 2 is name fully matches a user name.
2158 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002159 int
2160match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002161{
2162 int i;
2163 int n = (int)STRLEN(name);
2164 int result = 0;
2165
2166 init_users();
2167 for (i = 0; i < ga_users.ga_len; i++)
2168 {
2169 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002170 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002171 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002172 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002173 }
2174 return result;
2175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002178prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002180#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002181 // Ignore SIGHUP, because a dropped connection causes a read error, which
2182 // makes Vim exit and then handling SIGHUP causes various reentrance
2183 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002184 signal(SIGHUP, SIG_IGN);
2185#endif
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#ifdef FEAT_GUI
2188 if (gui.in_use)
2189 {
2190 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002191 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 }
2193 else
2194#endif
2195 {
2196 windgoto((int)Rows - 1, 0);
2197
2198 /*
2199 * Switch terminal mode back now, so messages end up on the "normal"
2200 * screen (if there are two screens).
2201 */
2202 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002203 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 out_flush();
2205 }
2206}
2207
2208/*
2209 * Preserve files and exit.
2210 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002211 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2212 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 */
2214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002215preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
2217 buf_T *buf;
2218
2219 prepare_to_exit();
2220
Bram Moolenaar85a20022019-12-21 18:25:54 +01002221 // Setting this will prevent free() calls. That avoids calling free()
2222 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002223 really_exiting = TRUE;
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002226 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 out_flush();
2228
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
Bram Moolenaar29323592016-07-24 22:04:11 +02002231 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2234 {
Bram Moolenaar69212b12020-05-10 14:14:03 +02002235 OUT_STR("Vim: preserving files...\r\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002236 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002238 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 break;
2240 }
2241 }
2242
Bram Moolenaar85a20022019-12-21 18:25:54 +01002243 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaar69212b12020-05-10 14:14:03 +02002245 OUT_STR("Vim: Finished.\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247 getout(1);
2248}
2249
2250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * Check for CTRL-C pressed, but only once in a while.
2252 * Should be used instead of ui_breakcheck() for functions that check for
2253 * each line in the file. Calling ui_breakcheck() each time takes too much
2254 * time, because it can be a system call.
2255 */
2256
2257#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002258# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#endif
2260
2261static int breakcheck_count = 0;
2262
2263 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002264line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 if (++breakcheck_count >= BREAKCHECK_SKIP)
2267 {
2268 breakcheck_count = 0;
2269 ui_breakcheck();
2270 }
2271}
2272
2273/*
2274 * Like line_breakcheck() but check 10 times less often.
2275 */
2276 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002277fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2280 {
2281 breakcheck_count = 0;
2282 ui_breakcheck();
2283 }
2284}
2285
Dominique Pelle748b3082022-01-08 12:41:16 +00002286# if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002287/*
2288 * Like line_breakcheck() but check 100 times less often.
2289 */
2290 void
2291veryfast_breakcheck(void)
2292{
2293 if (++breakcheck_count >= BREAKCHECK_SKIP * 100)
2294 {
2295 breakcheck_count = 0;
2296 ui_breakcheck();
2297 }
2298}
Dominique Pelle748b3082022-01-08 12:41:16 +00002299#endif
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002300
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002301#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2302 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2303 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305#ifndef SEEK_SET
2306# define SEEK_SET 0
2307#endif
2308#ifndef SEEK_END
2309# define SEEK_END 2
2310#endif
2311
2312/*
2313 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002314 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2315 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 * Returns an allocated string, or NULL for error.
2317 */
2318 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002319get_cmd_output(
2320 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002321 char_u *infile, // optional input file name
2322 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002323 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324{
2325 char_u *tempname;
2326 char_u *command;
2327 char_u *buffer = NULL;
2328 int len;
2329 int i = 0;
2330 FILE *fd;
2331
2332 if (check_restricted() || check_secure())
2333 return NULL;
2334
Bram Moolenaar85a20022019-12-21 18:25:54 +01002335 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002336 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002338 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 return NULL;
2340 }
2341
Bram Moolenaar85a20022019-12-21 18:25:54 +01002342 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002343 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (command == NULL)
2345 goto done;
2346
2347 /*
2348 * Call the shell to execute the command (errors are ignored).
2349 * Don't check timestamps here.
2350 */
2351 ++no_check_timestamps;
2352 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2353 --no_check_timestamps;
2354
2355 vim_free(command);
2356
2357 /*
2358 * read the names from the file into memory
2359 */
2360# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002361 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 fd = mch_fopen((char *)tempname, "r");
2363# else
2364 fd = mch_fopen((char *)tempname, READBIN);
2365# endif
2366
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002367 // Not being able to seek means we can't read the file.
2368 if (fd == NULL
2369 || fseek(fd, 0L, SEEK_END) == -1
2370 || (len = ftell(fd)) == -1 // get size of temp file
2371 || fseek(fd, 0L, SEEK_SET) == -1) // back to the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaara9549c92022-04-17 14:18:11 +01002373 semsg(_(e_cannot_read_from_str_2), tempname);
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002374 if (fd != NULL)
2375 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 goto done;
2377 }
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 buffer = alloc(len + 1);
2380 if (buffer != NULL)
2381 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2382 fclose(fd);
2383 mch_remove(tempname);
2384 if (buffer == NULL)
2385 goto done;
2386#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#endif
2389 if (i != len)
2390 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002391 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002392 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002394 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002395 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002396 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002397 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002398 if (buffer[i] == NUL)
2399 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002400
Bram Moolenaar85a20022019-12-21 18:25:54 +01002401 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002402 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002403 else
2404 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406done:
2407 vim_free(tempname);
2408 return buffer;
2409}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002410
2411# if defined(FEAT_EVAL) || defined(PROTO)
2412
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002413 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002414get_cmd_output_as_rettv(
2415 typval_T *argvars,
2416 typval_T *rettv,
2417 int retlist)
2418{
2419 char_u *res = NULL;
2420 char_u *p;
2421 char_u *infile = NULL;
2422 int err = FALSE;
2423 FILE *fd;
2424 list_T *list = NULL;
2425 int flags = SHELL_SILENT;
2426
2427 rettv->v_type = VAR_STRING;
2428 rettv->vval.v_string = NULL;
2429 if (check_restricted() || check_secure())
2430 goto errret;
2431
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002432 if (in_vim9script()
2433 && (check_for_string_arg(argvars, 0) == FAIL
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01002434 || check_for_opt_string_or_number_or_list_arg(argvars, 1)
2435 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002436 return;
2437
Bram Moolenaar26262f82019-09-04 20:59:15 +02002438 if (argvars[1].v_type != VAR_UNKNOWN)
2439 {
2440 /*
2441 * Write the text to a temp file, to be used for input of the shell
2442 * command.
2443 */
2444 if ((infile = vim_tempname('i', TRUE)) == NULL)
2445 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002446 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002447 goto errret;
2448 }
2449
2450 fd = mch_fopen((char *)infile, WRITEBIN);
2451 if (fd == NULL)
2452 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002453 semsg(_(e_cant_open_file_str), infile);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002454 goto errret;
2455 }
2456 if (argvars[1].v_type == VAR_NUMBER)
2457 {
2458 linenr_T lnum;
2459 buf_T *buf;
2460
2461 buf = buflist_findnr(argvars[1].vval.v_number);
2462 if (buf == NULL)
2463 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002464 semsg(_(e_buffer_nr_does_not_exist), argvars[1].vval.v_number);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002465 fclose(fd);
2466 goto errret;
2467 }
2468
2469 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2470 {
2471 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2472 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2473 {
2474 err = TRUE;
2475 break;
2476 }
2477 if (putc(NL, fd) == EOF)
2478 {
2479 err = TRUE;
2480 break;
2481 }
2482 }
2483 }
2484 else if (argvars[1].v_type == VAR_LIST)
2485 {
2486 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2487 err = TRUE;
2488 }
2489 else
2490 {
2491 size_t len;
2492 char_u buf[NUMBUFLEN];
2493
2494 p = tv_get_string_buf_chk(&argvars[1], buf);
2495 if (p == NULL)
2496 {
2497 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002498 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002499 }
2500 len = STRLEN(p);
2501 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2502 err = TRUE;
2503 }
2504 if (fclose(fd) != 0)
2505 err = TRUE;
2506 if (err)
2507 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002508 emsg(_(e_error_writing_temp_file));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002509 goto errret;
2510 }
2511 }
2512
Bram Moolenaar85a20022019-12-21 18:25:54 +01002513 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2514 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002515 if (!msg_silent)
2516 flags += SHELL_COOKED;
2517
2518 if (retlist)
2519 {
2520 int len;
2521 listitem_T *li;
2522 char_u *s = NULL;
2523 char_u *start;
2524 char_u *end;
2525 int i;
2526
2527 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2528 if (res == NULL)
2529 goto errret;
2530
2531 list = list_alloc();
2532 if (list == NULL)
2533 goto errret;
2534
2535 for (i = 0; i < len; ++i)
2536 {
2537 start = res + i;
2538 while (i < len && res[i] != NL)
2539 ++i;
2540 end = res + i;
2541
2542 s = alloc(end - start + 1);
2543 if (s == NULL)
2544 goto errret;
2545
2546 for (p = s; start < end; ++p, ++start)
2547 *p = *start == NUL ? NL : *start;
2548 *p = NUL;
2549
2550 li = listitem_alloc();
2551 if (li == NULL)
2552 {
2553 vim_free(s);
2554 goto errret;
2555 }
2556 li->li_tv.v_type = VAR_STRING;
2557 li->li_tv.v_lock = 0;
2558 li->li_tv.vval.v_string = s;
2559 list_append(list, li);
2560 }
2561
2562 rettv_list_set(rettv, list);
2563 list = NULL;
2564 }
2565 else
2566 {
2567 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2568#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002569 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002570 if (res != NULL)
2571 {
2572 char_u *s, *d;
2573
2574 d = res;
2575 for (s = res; *s; ++s)
2576 {
2577 if (s[0] == CAR && s[1] == NL)
2578 ++s;
2579 *d++ = *s;
2580 }
2581 *d = NUL;
2582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002584 rettv->vval.v_string = res;
2585 res = NULL;
2586 }
2587
2588errret:
2589 if (infile != NULL)
2590 {
2591 mch_remove(infile);
2592 vim_free(infile);
2593 }
2594 if (res != NULL)
2595 vim_free(res);
2596 if (list != NULL)
2597 list_free(list);
2598}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002601 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
2603 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002604f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002606 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607}
2608
2609/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002610 * "systemlist()" function
2611 */
2612 void
2613f_systemlist(typval_T *argvars, typval_T *rettv)
2614{
2615 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2616}
2617# endif // FEAT_EVAL
2618
2619#endif
2620
2621/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002622 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 * Don't do this when still processing a command or a mapping.
2624 * Don't do this when inside a ":normal" command.
2625 */
2626 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002627goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 return (p_im && stuff_empty() && typebuf_typed());
2630}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002631
2632/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002633 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002634 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2635 * - Remove any argument. E.g., "csh -f" -> "csh".
2636 * But don't allow a space in the path, so that this works:
2637 * "/usr/bin/csh --rcfile ~/.cshrc"
2638 * But don't do that for Windows, it's common to have a space in the path.
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002639 * Returns NULL when out of memory.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002640 */
2641 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002642get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002643{
2644 char_u *p;
2645
Bram Moolenaar4f974752019-02-17 17:44:42 +01002646#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002647 p = gettail(p_sh);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002648 p = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002649#else
2650 p = skiptowhite(p_sh);
2651 if (*p == NUL)
2652 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002654 p = vim_strsave(gettail(p_sh));
2655 }
2656 else
2657 {
2658 char_u *p1, *p2;
2659
Bram Moolenaar85a20022019-12-21 18:25:54 +01002660 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002661 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002662 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002663 if (vim_ispathsep(*p2))
2664 p1 = p2 + 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002665 p = vim_strnsave(p1, p - p1);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002666 }
2667#endif
2668 return p;
2669}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002670
2671/*
2672 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2673 * Also check for ":\\", which MS Internet Explorer accepts, return
2674 * URL_BACKSLASH.
2675 */
2676 int
2677path_is_url(char_u *p)
2678{
2679 if (STRNCMP(p, "://", (size_t)3) == 0)
2680 return URL_SLASH;
2681 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2682 return URL_BACKSLASH;
2683 return 0;
2684}
2685
2686/*
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002687 * Check if "fname" starts with "name://" or "name:\\".
2688 * Return URL_SLASH for "name://", URL_BACKSLASH for "name:\\".
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002689 * Return zero otherwise.
2690 */
2691 int
2692path_with_url(char_u *fname)
2693{
2694 char_u *p;
2695
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002696 // We accept alphabetic characters and a dash in scheme part.
2697 // RFC 3986 allows for more, but it increases the risk of matching
2698 // non-URL text.
2699
2700 // first character must be alpha
2701 if (!isalpha(*fname))
2702 return 0;
2703
2704 // check body: alpha or dash
itchyny94e7d342021-10-21 18:01:13 +01002705 for (p = fname + 1; (isalpha(*p) || (*p == '-')); ++p)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002706 ;
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002707
2708 // check last char is not a dash
2709 if (p[-1] == '-')
2710 return 0;
2711
2712 // "://" or ":\\" must follow
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002713 return path_is_url(p);
2714}
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002715
Bram Moolenaar3075a452021-11-17 15:51:52 +00002716#if defined(FEAT_EVAL) || defined(PROTO)
2717/*
2718 * Return the dictionary of v:event.
2719 * Save and clear the value in case it already has items.
2720 */
2721 dict_T *
2722get_v_event(save_v_event_T *sve)
2723{
2724 dict_T *v_event = get_vim_var_dict(VV_EVENT);
2725
2726 if (v_event->dv_hashtab.ht_used > 0)
2727 {
2728 // recursive use of v:event, save, make empty and restore later
2729 sve->sve_did_save = TRUE;
2730 sve->sve_hashtab = v_event->dv_hashtab;
2731 hash_init(&v_event->dv_hashtab);
2732 }
2733 else
2734 sve->sve_did_save = FALSE;
2735 return v_event;
2736}
2737
2738 void
2739restore_v_event(dict_T *v_event, save_v_event_T *sve)
2740{
2741 dict_free_contents(v_event);
2742 if (sve->sve_did_save)
2743 v_event->dv_hashtab = sve->sve_hashtab;
2744 else
2745 hash_init(&v_event->dv_hashtab);
2746}
2747#endif
2748
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002749/*
LemonBoy2bf52dd2022-04-09 18:17:34 +01002750 * Fires a ModeChanged autocmd event if appropriate.
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002751 */
2752 void
LemonBoy2bf52dd2022-04-09 18:17:34 +01002753may_trigger_modechanged()
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002754{
Bram Moolenaar3075a452021-11-17 15:51:52 +00002755#ifdef FEAT_EVAL
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002756 dict_T *v_event;
Bram Moolenaar3075a452021-11-17 15:51:52 +00002757 save_v_event_T save_v_event;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002758 char_u curr_mode[MODE_MAX_LENGTH];
2759 char_u pattern_buf[2 * MODE_MAX_LENGTH];
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002760
2761 if (!has_modechanged())
2762 return;
2763
LemonBoy2bf52dd2022-04-09 18:17:34 +01002764 get_mode(curr_mode);
2765 if (STRCMP(curr_mode, last_mode) == 0)
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002766 return;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002767
Bram Moolenaar3075a452021-11-17 15:51:52 +00002768 v_event = get_v_event(&save_v_event);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002769 (void)dict_add_string(v_event, "new_mode", curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002770 (void)dict_add_string(v_event, "old_mode", last_mode);
2771 dict_set_items_ro(v_event);
2772
2773 // concatenate modes in format "old_mode:new_mode"
LemonBoy2bf52dd2022-04-09 18:17:34 +01002774 vim_snprintf((char *)pattern_buf, sizeof(pattern_buf), "%s:%s", last_mode,
2775 curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002776
LemonBoy2bf52dd2022-04-09 18:17:34 +01002777 apply_autocmds(EVENT_MODECHANGED, pattern_buf, NULL, FALSE, curbuf);
2778 STRCPY(last_mode, curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002779
Bram Moolenaar3075a452021-11-17 15:51:52 +00002780 restore_v_event(v_event, &save_v_event);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002781#endif
2782}