blob: b4b74f7ef5a35802a0e6bebea4843cacceeb91dd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaarb3f74062020-02-26 16:16:53 +010017#if defined(__HAIKU__)
18# include <storage/FindDirectory.h>
19#endif
20
Bram Moolenaar0a52df52019-08-18 22:26:31 +020021#if defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020022# include <lm.h>
23#endif
24
Bram Moolenaar85a20022019-12-21 18:25:54 +010025#define URL_SLASH 1 // path_is_url() has found "://"
26#define URL_BACKSLASH 2 // path_is_url() has found ":\\"
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010027
Bram Moolenaar0a52df52019-08-18 22:26:31 +020028// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020029static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +020032 * get_leader_len() returns the length in bytes of the prefix of the given
33 * string which introduces a comment. If this string is not a comment then
34 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 * When "flags" is not NULL, it is set to point to the flags of the recognized
36 * comment leader.
37 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +020038 * If "include_space" is set, include trailing whitespace while calculating the
39 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_leader_len(
43 char_u *line,
44 char_u **flags,
45 int backward,
46 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +000047{
48 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +020049 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 int got_com = FALSE;
51 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +010052 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
53 char_u *string; // pointer to comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +020055 int middle_match_len = 0;
56 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +020057 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar81340392012-06-06 16:12:59 +020059 result = i = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +010060 while (VIM_ISWHITE(line[i])) // leading white space is ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 ++i;
62
63 /*
64 * Repeat to match several nested comment strings.
65 */
Bram Moolenaara4271d52011-05-10 13:38:27 +020066 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 {
68 /*
69 * scan through the 'comments' option for a match
70 */
71 found_one = FALSE;
72 for (list = curbuf->b_p_com; *list; )
73 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010074 // Get one option part into part_buf[]. Advance "list" to next
75 // one. Put "string" at start of string.
Bram Moolenaara4271d52011-05-10 13:38:27 +020076 if (!got_com && flags != NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +010077 *flags = list; // remember where flags started
Bram Moolenaara4271d52011-05-10 13:38:27 +020078 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
80 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +010081 if (string == NULL) // missing ':', ignore this part
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +010083 *string++ = NUL; // isolate flags from string
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar85a20022019-12-21 18:25:54 +010085 // If we found a middle match previously, use that match when this
86 // is not a middle or end.
Bram Moolenaara4271d52011-05-10 13:38:27 +020087 if (middle_match_len != 0
88 && vim_strchr(part_buf, COM_MIDDLE) == NULL
89 && vim_strchr(part_buf, COM_END) == NULL)
90 break;
91
Bram Moolenaar85a20022019-12-21 18:25:54 +010092 // When we already found a nested comment, only accept further
93 // nested comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
95 continue;
96
Bram Moolenaar85a20022019-12-21 18:25:54 +010097 // When 'O' flag present and using "O" command skip this one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
99 continue;
100
Bram Moolenaar85a20022019-12-21 18:25:54 +0100101 // Line contents and string must match.
102 // When string starts with white space, must have some white space
103 // (but the amount does not need to match, there might be a mix of
104 // TABs and spaces).
Bram Moolenaar1c465442017-03-12 20:10:05 +0100105 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100107 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100108 continue; // missing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +0100109 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 ++string;
111 }
112 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
113 ;
114 if (string[j] != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100115 continue; // string doesn't match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar85a20022019-12-21 18:25:54 +0100117 // When 'b' flag used, there must be white space or an
118 // end-of-line after the string in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100120 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 continue;
122
Bram Moolenaar85a20022019-12-21 18:25:54 +0100123 // We have found a match, stop searching unless this is a middle
124 // comment. The middle comment can be a substring of the end
125 // comment in which case it's better to return the length of the
126 // end comment and its flags. Thus we keep searching with middle
127 // and end matches and use an end match if it matches better.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200128 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
129 {
130 if (middle_match_len == 0)
131 {
132 middle_match_len = j;
133 saved_flags = prev_list;
134 }
135 continue;
136 }
137 if (middle_match_len != 0 && j > middle_match_len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100138 // Use this match instead of the middle match, since it's a
139 // longer thus better match.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200140 middle_match_len = 0;
141
142 if (middle_match_len == 0)
143 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 found_one = TRUE;
145 break;
146 }
147
Bram Moolenaara4271d52011-05-10 13:38:27 +0200148 if (middle_match_len != 0)
149 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100150 // Use the previously found middle match after failing to find a
151 // match with an end.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200152 if (!got_com && flags != NULL)
153 *flags = saved_flags;
154 i += middle_match_len;
155 found_one = TRUE;
156 }
157
Bram Moolenaar85a20022019-12-21 18:25:54 +0100158 // No match found, stop scanning.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 if (!found_one)
160 break;
161
Bram Moolenaar81340392012-06-06 16:12:59 +0200162 result = i;
163
Bram Moolenaar85a20022019-12-21 18:25:54 +0100164 // Include any trailing white space.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100165 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++i;
167
Bram Moolenaar81340392012-06-06 16:12:59 +0200168 if (include_space)
169 result = i;
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // If this comment doesn't nest, stop here.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200172 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (vim_strchr(part_buf, COM_NEST) == NULL)
174 break;
175 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200176 return result;
177}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200178
Bram Moolenaar81340392012-06-06 16:12:59 +0200179/*
180 * Return the offset at which the last comment in line starts. If there is no
181 * comment in the whole line, -1 is returned.
182 *
183 * When "flags" is not null, it is set to point to the flags describing the
184 * recognized comment leader.
185 */
186 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100187get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200188{
189 int result = -1;
190 int i, j;
191 int lower_check_bound = 0;
192 char_u *string;
193 char_u *com_leader;
194 char_u *com_flags;
195 char_u *list;
196 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200198
199 /*
200 * Repeat to match several nested comment strings.
201 */
202 i = (int)STRLEN(line);
203 while (--i >= lower_check_bound)
204 {
205 /*
206 * scan through the 'comments' option for a match
207 */
208 found_one = FALSE;
209 for (list = curbuf->b_p_com; *list; )
210 {
211 char_u *flags_save = list;
212
213 /*
214 * Get one option part into part_buf[]. Advance list to next one.
215 * put string at start of string.
216 */
217 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
218 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 if (string == NULL) // If everything is fine, this cannot actually
220 // happen.
Bram Moolenaar81340392012-06-06 16:12:59 +0200221 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 *string++ = NUL; // Isolate flags from string.
Bram Moolenaar81340392012-06-06 16:12:59 +0200223 com_leader = string;
224
225 /*
226 * Line contents and string must match.
227 * When string starts with white space, must have some white space
228 * (but the amount does not need to match, there might be a mix of
229 * TABs and spaces).
230 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100231 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200232 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100233 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200234 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100235 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200236 ++string;
237 }
238 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
239 /* do nothing */;
240 if (string[j] != NUL)
241 continue;
242
243 /*
244 * When 'b' flag used, there must be white space or an
245 * end-of-line after the string in the line.
246 */
247 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100248 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200249 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100250
Bram Moolenaar4af72592018-12-09 15:00:52 +0100251 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100252 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100253 // For a middlepart comment, only consider it to match if
254 // everything before the current position in the line is
255 // whitespace. Otherwise we would think we are inside a
256 // comment if the middle part appears somewhere in the middle
257 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100258 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
259 ;
260 if (j < i)
261 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200262 }
263
264 /*
265 * We have found a match, stop searching.
266 */
267 found_one = TRUE;
268
269 if (flags)
270 *flags = flags_save;
271 com_flags = flags_save;
272
273 break;
274 }
275
276 if (found_one)
277 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 char_u part_buf2[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200279 int len1, len2, off;
280
281 result = i;
282 /*
283 * If this comment nests, continue searching.
284 */
285 if (vim_strchr(part_buf, COM_NEST) != NULL)
286 continue;
287
288 lower_check_bound = i;
289
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Let's verify whether the comment leader found is a substring
291 // of other comment leaders. If it is, let's adjust the
292 // lower_check_bound so that we make sure that we have determined
293 // the comment leader correctly.
Bram Moolenaar81340392012-06-06 16:12:59 +0200294
Bram Moolenaar1c465442017-03-12 20:10:05 +0100295 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200296 ++com_leader;
297 len1 = (int)STRLEN(com_leader);
298
299 for (list = curbuf->b_p_com; *list; )
300 {
301 char_u *flags_save = list;
302
303 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
304 if (flags_save == com_flags)
305 continue;
306 string = vim_strchr(part_buf2, ':');
307 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100308 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200309 ++string;
310 len2 = (int)STRLEN(string);
311 if (len2 == 0)
312 continue;
313
Bram Moolenaar85a20022019-12-21 18:25:54 +0100314 // Now we have to verify whether string ends with a substring
315 // beginning the com_leader.
Bram Moolenaar81340392012-06-06 16:12:59 +0200316 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
317 {
318 --off;
319 if (!STRNCMP(string + off, com_leader, len2 - off))
320 {
321 if (i - off < lower_check_bound)
322 lower_check_bound = i - off;
323 }
324 }
325 }
326 }
327 }
328 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
331/*
332 * Return the number of window lines occupied by buffer line "lnum".
333 */
334 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100335plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return plines_win(curwin, lnum, TRUE);
338}
339
340 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100341plines_win(
342 win_T *wp,
343 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100344 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346#if defined(FEAT_DIFF) || defined(PROTO)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100347 // Check for filler lines above this buffer line. When folded the result
348 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
350}
351
352 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100353plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 return plines_win_nofill(curwin, lnum, TRUE);
356}
357
358 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100359plines_win_nofill(
360 win_T *wp,
361 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364#endif
365 int lines;
366
367 if (!wp->w_p_wrap)
368 return 1;
369
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 if (wp->w_width == 0)
371 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100374 // A folded lines is handled just like an empty line.
375 // NOTE: Caller must handle lines that are MAYBE folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (lineFolded(wp, lnum) == TRUE)
377 return 1;
378#endif
379
380 lines = plines_win_nofold(wp, lnum);
381 if (winheight > 0 && lines > wp->w_height)
382 return (int)wp->w_height;
383 return lines;
384}
385
386/*
387 * Return number of window lines physical line "lnum" will occupy in window
388 * "wp". Does not care about folding, 'wrap' or 'diff'.
389 */
390 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100391plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392{
393 char_u *s;
394 long col;
395 int width;
396
397 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100398 if (*s == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 return 1;
400 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
401
402 /*
403 * If list mode is on, then the '$' at the end of the line may take up one
404 * extra column.
405 */
406 if (wp->w_p_list && lcs_eol != NUL)
407 col += 1;
408
409 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200410 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200412 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (width <= 0)
414 return 32000;
415 if (col <= width)
416 return 1;
417 col -= width;
418 width += win_col_off2(wp);
419 return (col + (width - 1)) / width + 1;
420}
421
422/*
423 * Like plines_win(), but only reports the number of physical screen lines
424 * used from the start of the line to the given column number.
425 */
426 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100427plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429 long col;
430 char_u *s;
431 int lines = 0;
432 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200433 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100436 // Check for filler lines above this buffer line. When folded the result
437 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 lines = diff_check_fill(wp, lnum);
439#endif
440
441 if (!wp->w_p_wrap)
442 return lines + 1;
443
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (wp->w_width == 0)
445 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar597a4222014-06-25 14:39:50 +0200447 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449 col = 0;
450 while (*s != NUL && --column >= 0)
451 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200452 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100453 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 }
455
456 /*
457 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
458 * INSERT mode, then col must be adjusted so that it represents the last
459 * screen position of the TAB. This only fixes an error when the TAB wraps
460 * from one screen line to the next (when 'columns' is not a multiple of
461 * 'ts') -- webb.
462 */
463 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200464 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200467 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200469 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +0000470 if (width <= 0)
471 return 9999;
472
473 lines += 1;
474 if (col > width)
475 lines += (col - width) / (width + win_col_off2(wp)) + 1;
476 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477}
478
479 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100480plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
482 int count = 0;
483
484 while (first <= last)
485 {
486#ifdef FEAT_FOLDING
487 int x;
488
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // Check if there are any really folded lines, but also included lines
490 // that are maybe folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 x = foldedCount(wp, first, NULL);
492 if (x > 0)
493 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100494 ++count; // count 1 for "+-- folded" line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 first += x;
496 }
497 else
498#endif
499 {
500#ifdef FEAT_DIFF
501 if (first == wp->w_topline)
502 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
503 else
504#endif
505 count += plines_win(wp, first, TRUE);
506 ++first;
507 }
508 }
509 return (count);
510}
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100513gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100515 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
Bram Moolenaar85a20022019-12-21 18:25:54 +0100517 // When searching columns is sometimes put at the end of a line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100518 if (pos->col == MAXCOL)
519 return NUL;
520 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (has_mbyte)
522 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 return (int)*ptr;
524}
525
526 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100527gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 if (has_mbyte)
530 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 return (int)*ml_get_cursor();
532}
533
534/*
535 * Write a character at the current cursor position.
536 * It is directly written into the block.
537 */
538 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100539pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
542 + curwin->w_cursor.col) = c;
543}
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * Skip to next part of an option argument: Skip space and comma.
547 */
548 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100549skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
551 if (*p == ',')
552 ++p;
553 while (*p == ' ')
554 ++p;
555 return p;
556}
557
558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 * check_status: called when the status bars for the buffer 'buf'
560 * need to be updated
561 */
562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100563check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 win_T *wp;
566
Bram Moolenaar29323592016-07-24 22:04:11 +0200567 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (wp->w_buffer == buf && wp->w_status_height)
569 {
570 wp->w_redr_status = TRUE;
571 if (must_redraw < VALID)
572 must_redraw = VALID;
573 }
574}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 * Ask for a reply from the user, a 'y' or a 'n'.
578 * No other characters are accepted, the message is repeated until a valid
579 * reply is entered or CTRL-C is hit.
580 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
581 * from any buffers but directly from the user.
582 *
583 * return the 'y' or 'n'
584 */
585 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100586ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int r = ' ';
589 int save_State = State;
590
Bram Moolenaar85a20022019-12-21 18:25:54 +0100591 if (exiting) // put terminal in raw mode for this question
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 settmode(TMODE_RAW);
593 ++no_wait_return;
594#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200595 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200597 State = CONFIRM; // mouse behaves like with :confirm
598 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200600 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602 while (r != 'y' && r != 'n')
603 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100604 // same highlighting as for wait_return
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100605 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (direct)
607 r = get_keystroke();
608 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000609 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 if (r == Ctrl_C || r == ESC)
611 r = 'n';
Bram Moolenaar85a20022019-12-21 18:25:54 +0100612 msg_putchar(r); // show what you typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 out_flush();
614 }
615 --no_wait_return;
616 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 --no_mapping;
619 --allow_keys;
620
621 return r;
622}
623
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200624#if defined(FEAT_EVAL) || defined(PROTO)
625
626/*
627 * "mode()" function
628 */
629 void
630f_mode(typval_T *argvars, typval_T *rettv)
631{
632 char_u buf[4];
633
634 vim_memset(buf, 0, sizeof(buf));
635
636 if (time_for_testing == 93784)
637 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100638 // Testing the two-character code.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200639 buf[0] = 'x';
640 buf[1] = '!';
641 }
642#ifdef FEAT_TERMINAL
643 else if (term_use_loop())
644 buf[0] = 't';
645#endif
646 else if (VIsual_active)
647 {
648 if (VIsual_select)
649 buf[0] = VIsual_mode + 's' - 'v';
650 else
651 buf[0] = VIsual_mode;
652 }
653 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
654 || State == CONFIRM)
655 {
656 buf[0] = 'r';
657 if (State == ASKMORE)
658 buf[1] = 'm';
659 else if (State == CONFIRM)
660 buf[1] = '?';
661 }
662 else if (State == EXTERNCMD)
663 buf[0] = '!';
664 else if (State & INSERT)
665 {
666 if (State & VREPLACE_FLAG)
667 {
668 buf[0] = 'R';
669 buf[1] = 'v';
670 }
671 else
672 {
673 if (State & REPLACE_FLAG)
674 buf[0] = 'R';
675 else
676 buf[0] = 'i';
677 if (ins_compl_active())
678 buf[1] = 'c';
679 else if (ctrl_x_mode_not_defined_yet())
680 buf[1] = 'x';
681 }
682 }
683 else if ((State & CMDLINE) || exmode_active)
684 {
685 buf[0] = 'c';
686 if (exmode_active == EXMODE_VIM)
687 buf[1] = 'v';
688 else if (exmode_active == EXMODE_NORMAL)
689 buf[1] = 'e';
690 }
691 else
692 {
693 buf[0] = 'n';
694 if (finish_op)
695 {
696 buf[1] = 'o';
697 // to be able to detect force-linewise/blockwise/characterwise operations
698 buf[2] = motion_force;
699 }
700 else if (restart_edit == 'I' || restart_edit == 'R'
701 || restart_edit == 'V')
702 {
703 buf[1] = 'i';
704 buf[2] = restart_edit;
705 }
706 }
707
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 // Clear out the minor mode when the argument is not a non-zero number or
709 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200710 if (!non_zero_arg(&argvars[0]))
711 buf[1] = NUL;
712
713 rettv->vval.v_string = vim_strsave(buf);
714 rettv->v_type = VAR_STRING;
715}
716
717 static void
718may_add_state_char(garray_T *gap, char_u *include, int c)
719{
720 if (include == NULL || vim_strchr(include, c) != NULL)
721 ga_append(gap, c);
722}
723
724/*
725 * "state()" function
726 */
727 void
728f_state(typval_T *argvars, typval_T *rettv)
729{
730 garray_T ga;
731 char_u *include = NULL;
732 int i;
733
734 ga_init2(&ga, 1, 20);
735 if (argvars[0].v_type != VAR_UNKNOWN)
736 include = tv_get_string(&argvars[0]);
737
738 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
739 may_add_state_char(&ga, include, 'm');
740 if (op_pending())
741 may_add_state_char(&ga, include, 'o');
742 if (autocmd_busy)
743 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200744 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200745 may_add_state_char(&ga, include, 'a');
746
747# ifdef FEAT_JOB_CHANNEL
748 if (channel_in_blocking_wait())
749 may_add_state_char(&ga, include, 'w');
750# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200751 if (!get_was_safe_state())
752 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200753 for (i = 0; i < get_callback_depth() && i < 3; ++i)
754 may_add_state_char(&ga, include, 'c');
755 if (msg_scrolled > 0)
756 may_add_state_char(&ga, include, 's');
757
758 rettv->v_type = VAR_STRING;
759 rettv->vval.v_string = ga.ga_data;
760}
761
762#endif // FEAT_EVAL
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764/*
765 * Get a key stroke directly from the user.
766 * Ignores mouse clicks and scrollbar events, except a click for the left
767 * button (used at the more prompt).
768 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
769 * Disadvantage: typeahead is ignored.
770 * Translates the interrupt character for unix to ESC.
771 */
772 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100773get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100775 char_u *buf = NULL;
776 int buflen = 150;
777 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 int len = 0;
779 int n;
780 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000781 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
Bram Moolenaar85a20022019-12-21 18:25:54 +0100783 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 for (;;)
785 {
786 cursor_on();
787 out_flush();
788
Bram Moolenaar85a20022019-12-21 18:25:54 +0100789 // Leave some room for check_termcode() to insert a key code into (max
790 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
791 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100792 maxlen = (buflen - 6 - len) / 3;
793 if (buf == NULL)
794 buf = alloc(buflen);
795 else if (maxlen < 10)
796 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100797 char_u *t_buf = buf;
798
Bram Moolenaar85a20022019-12-21 18:25:54 +0100799 // Need some more space. This might happen when receiving a long
800 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100801 buflen += 100;
802 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100803 if (buf == NULL)
804 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100805 maxlen = (buflen - 6 - len) / 3;
806 }
807 if (buf == NULL)
808 {
809 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100810 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100811 }
812
Bram Moolenaar85a20022019-12-21 18:25:54 +0100813 // First time: blocking wait. Second time: wait up to 100ms for a
814 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100815 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (n > 0)
817 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100818 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200819 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000821 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000823 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100824 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
Bram Moolenaar85a20022019-12-21 18:25:54 +0100826 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100827 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000828 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000830
Bram Moolenaar85a20022019-12-21 18:25:54 +0100831 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100832 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100833 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100834 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100835 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100836 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100837 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100838 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100839 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100840 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100841 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100843 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 continue;
845
Bram Moolenaar85a20022019-12-21 18:25:54 +0100846 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 n = buf[0];
848 if (n == K_SPECIAL)
849 {
850 n = TO_SPECIAL(buf[1], buf[2]);
851 if (buf[1] == KS_MODIFIER
852 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100853 || (is_mouse_key(n) && n != K_LEFTMOUSE)
854#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 || n == K_VER_SCROLLBAR
856 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858 )
859 {
860 if (buf[1] == KS_MODIFIER)
861 mod_mask = buf[2];
862 len -= 3;
863 if (len > 0)
864 mch_memmove(buf, buf + 3, (size_t)len);
865 continue;
866 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000867 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (has_mbyte)
870 {
871 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100872 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100873 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 n = (*mb_ptr2char)(buf);
875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef UNIX
877 if (n == intr_char)
878 n = ESC;
879#endif
880 break;
881 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100882 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884 mapped_ctrl_c = save_mapped_ctrl_c;
885 return n;
886}
887
888/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000889 * Get a number from the user.
890 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 */
892 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100893get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100894 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100895 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897 int n = 0;
898 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000899 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000901 if (mouse_used != NULL)
902 *mouse_used = FALSE;
903
Bram Moolenaar85a20022019-12-21 18:25:54 +0100904 // When not printing messages, the user won't know what to type, return a
905 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (msg_silent != 0)
907 return 0;
908
909#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100910 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#endif
912 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100913 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 for (;;)
915 {
916 windgoto(msg_row, msg_col);
917 c = safe_vgetc();
918 if (VIM_ISDIGIT(c))
919 {
920 n = n * 10 + c - '0';
921 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000922 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
925 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000926 if (typed > 0)
927 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100928 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000929 --typed;
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000933 else if (mouse_used != NULL && c == K_LEFTMOUSE)
934 {
935 *mouse_used = TRUE;
936 n = mouse_row + 1;
937 break;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 else if (n == 0 && c == ':' && colon)
940 {
941 stuffcharReadbuff(':');
942 if (!exmode_active)
943 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100944 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 do_redraw = FALSE;
946 break;
947 }
948 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
949 break;
950 }
951 --no_mapping;
952 --allow_keys;
953 return n;
954}
955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000956/*
957 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000958 * When "mouse_used" is not NULL allow using the mouse and in that case return
959 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000960 */
961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100962prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000963{
964 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000965 int save_cmdline_row;
966 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000967
Bram Moolenaar85a20022019-12-21 18:25:54 +0100968 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000969 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100970 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000971 else
Bram Moolenaar32526b32019-01-19 17:43:09 +0100972 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000973
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200974 // Set the state such that text can be selected/copied/pasted and we still
975 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
976 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000977 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +0000978 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000979 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200980 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200981 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200982 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +0200983
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000984 i = get_number(TRUE, mouse_used);
985 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000986 {
Bram Moolenaar954bb062019-06-09 16:40:46 +0200987 // don't call wait_return() now
988 if (msg_row > 0)
989 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000990 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +0100991 msg_didany = FALSE;
992 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000993 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000994 else
995 cmdline_row = save_cmdline_row;
996 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200997 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200998 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001000 return i;
1001}
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001004msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 long pn;
1007
Bram Moolenaar85a20022019-12-21 18:25:54 +01001008 if (global_busy // no messages now, wait until global is finished
1009 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 return;
1011
Bram Moolenaar85a20022019-12-21 18:25:54 +01001012 // We don't want to overwrite another important message, but do overwrite
1013 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1014 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001015 if (keep_msg != NULL && !keep_msg_more)
1016 return;
1017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (n > 0)
1019 pn = n;
1020 else
1021 pn = -n;
1022
1023 if (pn > p_report)
1024 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001025 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001026 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001027 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001029 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001030 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001032 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1033 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (msg(msg_buf))
1035 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001036 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001037 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039 }
1040}
1041
1042/*
1043 * flush map and typeahead buffers and give a warning for an error
1044 */
1045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001046beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 if (emsg_silent == 0)
1049 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001050 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001051 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 }
1053}
1054
1055/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001056 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
1058 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059vim_beep(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001060 unsigned val) // one of the BO_ values, e.g., BO_OPER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001062#ifdef FEAT_EVAL
1063 called_vim_beep = TRUE;
1064#endif
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (emsg_silent == 0)
1067 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001068 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1069 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001070#ifdef ELAPSED_FUNC
1071 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001072 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001073
Bram Moolenaar85a20022019-12-21 18:25:54 +01001074 // Only beep once per half a second, otherwise a sequence of beeps
1075 // would freeze Vim.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001076 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1077 {
1078 did_init = TRUE;
1079 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001081 if (p_vb
1082#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01001083 // While the GUI is starting up the termcap is set for
1084 // the GUI but the output still goes to a terminal.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001085 && !(gui.in_use && gui.starting)
1086#endif
1087 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001088 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001089 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001090#ifdef FEAT_VTP
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // No restore color information, refresh the screen.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001092 if (has_vtp_working() != 0
1093# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001094 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001095# endif
1096 )
1097 {
1098 redraw_later(CLEAR);
1099 update_screen(0);
1100 redrawcmd();
1101 }
1102#endif
1103 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001104 else
1105 out_char(BELL);
1106#ifdef ELAPSED_FUNC
1107 }
1108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
Bram Moolenaar85a20022019-12-21 18:25:54 +01001111 // When 'debug' contains "beep" produce a message. If we are sourcing
1112 // a script or executing a function give the user a hint where the beep
1113 // comes from.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 if (vim_strchr(p_debug, 'e') != NULL)
1115 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001116 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001117 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120}
1121
1122/*
1123 * To get the "real" home directory:
1124 * - get value of $HOME
1125 * For Unix:
1126 * - go to that directory
1127 * - do mch_dirname() to get the real name of that directory.
1128 * This also works with mounts and links.
1129 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001130 * For Windows:
1131 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001134init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
1136 char_u *var;
1137
Bram Moolenaar85a20022019-12-21 18:25:54 +01001138 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001139 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#ifdef VMS
1142 var = mch_getenv((char_u *)"SYS$LOGIN");
1143#else
1144 var = mch_getenv((char_u *)"HOME");
1145#endif
1146
Bram Moolenaar4f974752019-02-17 17:44:42 +01001147#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001149 * Typically, $HOME is not defined on Windows, unless the user has
1150 * specifically defined it for Vim's sake. However, on Windows NT
1151 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1152 * each user. Try constructing $HOME from these.
1153 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001154 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001155 {
1156 char_u *homedrive, *homepath;
1157
1158 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1159 homepath = mch_getenv((char_u *)"HOMEPATH");
1160 if (homepath == NULL || *homepath == NUL)
1161 homepath = (char_u *)"\\";
1162 if (homedrive != NULL
1163 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1164 {
1165 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1166 if (NameBuff[0] != NUL)
1167 var = NameBuff;
1168 }
1169 }
1170
1171 if (var == NULL)
1172 var = mch_getenv((char_u *)"USERPROFILE");
1173
1174 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * Weird but true: $HOME may contain an indirect reference to another
1176 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1177 * when $HOME is being set.
1178 */
1179 if (var != NULL && *var == '%')
1180 {
1181 char_u *p;
1182 char_u *exp;
1183
1184 p = vim_strchr(var + 1, '%');
1185 if (p != NULL)
1186 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001187 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 exp = mch_getenv(NameBuff);
1189 if (exp != NULL && *exp != NUL
1190 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1191 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001192 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 }
1196 }
1197
Bram Moolenaar85a20022019-12-21 18:25:54 +01001198 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001199 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaar05159a02005-02-26 23:04:13 +00001201 if (enc_utf8 && var != NULL)
1202 {
1203 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001204 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001205
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // Convert from active codepage to UTF-8. Other conversions are
1207 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001208 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001209 if (pp != NULL)
1210 {
1211 homedir = pp;
1212 return;
1213 }
1214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 /*
1217 * Default home dir is C:/
1218 * Best assumption we can make in such a situation.
1219 */
1220 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001221 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (var != NULL)
1225 {
1226#ifdef UNIX
1227 /*
1228 * Change to the directory and get the actual path. This resolves
1229 * links. Don't do it when we can't return.
1230 */
1231 if (mch_dirname(NameBuff, MAXPATHL) == OK
1232 && mch_chdir((char *)NameBuff) == 0)
1233 {
1234 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1235 var = IObuff;
1236 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001237 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239#endif
1240 homedir = vim_strsave(var);
1241 }
1242}
1243
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001244#if defined(EXITFREE) || defined(PROTO)
1245 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001246free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001247{
1248 vim_free(homedir);
1249}
Bram Moolenaar24305862012-08-15 14:05:05 +02001250
Bram Moolenaar24305862012-08-15 14:05:05 +02001251 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001252free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001253{
1254 ga_clear_strings(&ga_users);
1255}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001256#endif
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001259 * Call expand_env() and store the result in an allocated string.
1260 * This is not very memory efficient, this expects the result to be freed
1261 * again soon.
1262 */
1263 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001264expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001265{
1266 return expand_env_save_opt(src, FALSE);
1267}
1268
1269/*
1270 * Idem, but when "one" is TRUE handle the string as one file name, only
1271 * expand "~" at the start.
1272 */
1273 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001274expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001275{
1276 char_u *p;
1277
1278 p = alloc(MAXPATHL);
1279 if (p != NULL)
1280 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1281 return p;
1282}
1283
1284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 * Expand environment variable with path name.
1286 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001287 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * If anything fails no expansion is done and dst equals src.
1289 */
1290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001291expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001292 char_u *src, // input string e.g. "$HOME/vim.hlp"
1293 char_u *dst, // where to put the result
1294 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001296 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297}
1298
1299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001300expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001301 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1302 char_u *dst, // where to put the result
1303 int dstlen, // maximum length of the result
1304 int esc, // escape spaces in expanded variables
1305 int one, // "srcp" is one file name
1306 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001308 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 char_u *tail;
1310 int c;
1311 char_u *var;
1312 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001313 int mustfree; // var was allocated, need to free it later
1314 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001315 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001317 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001318 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001319
1320 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001321 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 while (*src && dstlen > 0)
1323 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001324#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001325 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001326 if (src[0] == '`' && src[1] == '=')
1327 {
1328 size_t len;
1329
1330 var = src;
1331 src += 2;
1332 (void)skip_expr(&src);
1333 if (*src == '`')
1334 ++src;
1335 len = src - var;
1336 if (len > (size_t)dstlen)
1337 len = dstlen;
1338 vim_strncpy(dst, var, len);
1339 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001340 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001341 continue;
1342 }
1343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001345 if ((*src == '$'
1346#ifdef VMS
1347 && at_start
1348#endif
1349 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001350#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 || *src == '%'
1352#endif
1353 || (*src == '~' && at_start))
1354 {
1355 mustfree = FALSE;
1356
1357 /*
1358 * The variable name is copied into dst temporarily, because it may
1359 * be a string in read-only memory and a NUL needs to be appended.
1360 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001361 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
1363 tail = src + 1;
1364 var = dst;
1365 c = dstlen - 1;
1366
1367#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001368 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (*tail == '{' && !vim_isIDc('{'))
1370 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001371 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 while (c-- > 0 && *tail && *tail != '}')
1373 *var++ = *tail++;
1374 }
1375 else
1376#endif
1377 {
1378 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001379#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 || (*src == '%' && *tail != '%')
1381#endif
1382 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001386#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387# ifdef UNIX
1388 if (src[1] == '{' && *tail != '}')
1389# else
1390 if (*src == '%' && *tail != '%')
1391# endif
1392 var = NULL;
1393 else
1394 {
1395# ifdef UNIX
1396 if (src[1] == '{')
1397# else
1398 if (*src == '%')
1399#endif
1400 ++tail;
1401#endif
1402 *var = NUL;
1403 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001404#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
1406#endif
1407 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001408 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 else if ( src[1] == NUL
1410 || vim_ispathsep(src[1])
1411 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1412 {
1413 var = homedir;
1414 tail = src + 1;
1415 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001416 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
1418#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1419 /*
1420 * Copy ~user to dst[], so we can put a NUL after it.
1421 */
1422 tail = src;
1423 var = dst;
1424 c = dstlen - 1;
1425 while ( c-- > 0
1426 && *tail
1427 && vim_isfilec(*tail)
1428 && !vim_ispathsep(*tail))
1429 *var++ = *tail++;
1430 *var = NUL;
1431# ifdef UNIX
1432 /*
1433 * If the system supports getpwnam(), use it.
1434 * Otherwise, or if getpwnam() fails, the shell is used to
1435 * expand ~user. This is slower and may fail if the shell
1436 * does not support ~user (old versions of /bin/sh).
1437 */
1438# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1439 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001440 // Note: memory allocated by getpwnam() is never freed.
1441 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001442 struct passwd *pw = (*dst == NUL)
1443 ? NULL : getpwnam((char *)dst + 1);
1444
1445 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447 if (var == NULL)
1448# endif
1449 {
1450 expand_T xpc;
1451
1452 ExpandInit(&xpc);
1453 xpc.xp_context = EXPAND_FILES;
1454 var = ExpandOne(&xpc, dst, NULL,
1455 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 mustfree = TRUE;
1457 }
1458
Bram Moolenaar85a20022019-12-21 18:25:54 +01001459# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 /*
1461 * USER_HOME is a comma-separated list of
1462 * directories to search for the user account in.
1463 */
1464 {
1465 char_u test[MAXPATHL], paths[MAXPATHL];
1466 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001467 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
1469 STRCPY(paths, USER_HOME);
1470 next_path = paths;
1471 while (*next_path)
1472 {
1473 for (path = next_path; *next_path && *next_path != ',';
1474 next_path++);
1475 if (*next_path)
1476 *next_path++ = NUL;
1477 STRCPY(test, path);
1478 STRCAT(test, "/");
1479 STRCAT(test, dst + 1);
1480 if (mch_stat(test, &st) == 0)
1481 {
1482 var = alloc(STRLEN(test) + 1);
1483 STRCPY(var, test);
1484 mustfree = TRUE;
1485 break;
1486 }
1487 }
1488 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001489# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001491 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001493 tail = (char_u *)""; // for gcc
1494#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496
1497#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001498 // If 'shellslash' is set change backslashes to forward slashes.
1499 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1501 {
1502 char_u *p = vim_strsave(var);
1503
1504 if (p != NULL)
1505 {
1506 if (mustfree)
1507 vim_free(var);
1508 var = p;
1509 mustfree = TRUE;
1510 forward_slash(var);
1511 }
1512 }
1513#endif
1514
Bram Moolenaar85a20022019-12-21 18:25:54 +01001515 // If "var" contains white space, escape it with a backslash.
1516 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1518 {
1519 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1520
1521 if (p != NULL)
1522 {
1523 if (mustfree)
1524 vim_free(var);
1525 var = p;
1526 mustfree = TRUE;
1527 }
1528 }
1529
1530 if (var != NULL && *var != NUL
1531 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1532 {
1533 STRCPY(dst, var);
1534 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001535 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001536 // if var[] ends in a path separator and tail[] starts
1537 // with it, skip a character
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001538 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1540 && dst[-1] != ':'
1541#endif
1542 && vim_ispathsep(*tail))
1543 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001544 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 src = tail;
1546 copy_char = FALSE;
1547 }
1548 if (mustfree)
1549 vim_free(var);
1550 }
1551
Bram Moolenaar85a20022019-12-21 18:25:54 +01001552 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001555 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001556 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1557 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
1559 at_start = FALSE;
1560 if (src[0] == '\\' && src[1] != NUL)
1561 {
1562 *dst++ = *src++;
1563 --dstlen;
1564 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001565 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001567 if (dstlen > 0)
1568 {
1569 *dst++ = *src++;
1570 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001571
Bram Moolenaar1c864092017-08-06 18:15:45 +02001572 if (startstr != NULL && src - startstr_len >= srcp
1573 && STRNCMP(src - startstr_len, startstr,
1574 startstr_len) == 0)
1575 at_start = TRUE;
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 *dst = NUL;
1581}
1582
1583/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001584 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1585 * the length of "name/". Otherwise return "pend".
1586 */
1587 static char_u *
1588remove_tail(char_u *p, char_u *pend, char_u *name)
1589{
1590 int len = (int)STRLEN(name) + 1;
1591 char_u *newend = pend - len;
1592
1593 if (newend >= p
1594 && fnamencmp(newend, name, len - 1) == 0
1595 && (newend == p || after_pathsep(p, newend)))
1596 return newend;
1597 return pend;
1598}
1599
1600/*
1601 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1602 * Return NULL if not, return its name in allocated memory otherwise.
1603 */
1604 static char_u *
1605vim_version_dir(char_u *vimdir)
1606{
1607 char_u *p;
1608
1609 if (vimdir == NULL || *vimdir == NUL)
1610 return NULL;
1611 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1612 if (p != NULL && mch_isdir(p))
1613 return p;
1614 vim_free(p);
1615 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1616 if (p != NULL && mch_isdir(p))
1617 return p;
1618 vim_free(p);
1619 return NULL;
1620}
1621
1622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * Vim's version of getenv().
1624 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001625 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001626 * "mustfree" is set to TRUE when returned is allocated, it must be
1627 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 */
1629 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001630vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001632 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 char_u *pend;
1634 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001635#ifdef MSWIN
1636 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001638 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (STRCMP(name, "HOME") == 0)
1640 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001642 // Use Wide function
1643 wn = enc_to_utf16(name, NULL);
1644 if (wn == NULL)
1645 return NULL;
1646
1647 wp = _wgetenv(wn);
1648 vim_free(wn);
1649
1650 if (wp != NULL && *wp == NUL) // empty is the same as not set
1651 wp = NULL;
1652
1653 if (wp != NULL)
1654 {
1655 p = utf16_to_enc(wp, NULL);
1656 if (p == NULL)
1657 return NULL;
1658
1659 *mustfree = TRUE;
1660 return p;
1661 }
1662#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001664 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 p = NULL;
1666
1667 if (p != NULL)
1668 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001671 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1673 if (!vimruntime && STRCMP(name, "VIM") != 0)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001674#if defined(__HAIKU__)
1675 // special handling for user settings directory...
1676 if (STRCMP(name, "BE_USER_SETTINGS") == 0)
1677 {
1678 static char userSettingsPath[MAXPATHL] = {0};
1679
1680 if (B_OK == find_directory(B_USER_SETTINGS_DIRECTORY, 0,
1681 false, userSettingsPath, MAXPATHL))
1682 return userSettingsPath;
1683 }
1684 else
1685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 return NULL;
1687
1688 /*
1689 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1690 * Don't do this when default_vimruntime_dir is non-empty.
1691 */
1692 if (vimruntime
1693#ifdef HAVE_PATHDEF
1694 && *default_vimruntime_dir == NUL
1695#endif
1696 )
1697 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001698#ifdef MSWIN
1699 // Use Wide function
1700 wp = _wgetenv(L"VIM");
1701 if (wp != NULL && *wp == NUL) // empty is the same as not set
1702 wp = NULL;
1703 if (wp != NULL)
1704 {
1705 char_u *q = utf16_to_enc(wp, NULL);
1706 if (q != NULL)
1707 {
1708 p = vim_version_dir(q);
1709 *mustfree = TRUE;
1710 if (p == NULL)
1711 p = q;
1712 }
1713 }
1714#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001716 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 p = NULL;
1718 if (p != NULL)
1719 {
1720 p = vim_version_dir(p);
1721 if (p != NULL)
1722 *mustfree = TRUE;
1723 else
1724 p = mch_getenv((char_u *)"VIM");
1725 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
1728
1729 /*
1730 * When expanding $VIM or $VIMRUNTIME fails, try using:
1731 * - the directory name from 'helpfile' (unless it contains '$')
1732 * - the executable name from argv[0]
1733 */
1734 if (p == NULL)
1735 {
1736 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1737 p = p_hf;
1738#ifdef USE_EXE_NAME
1739 /*
1740 * Use the name of the executable, obtained from argv[0].
1741 */
1742 else
1743 p = exe_name;
1744#endif
1745 if (p != NULL)
1746 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001747 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 pend = gettail(p);
1749
Bram Moolenaar85a20022019-12-21 18:25:54 +01001750 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 if (p == p_hf)
1752 pend = remove_tail(p, pend, (char_u *)"doc");
1753
1754#ifdef USE_EXE_NAME
1755# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001756 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (p == exe_name)
1758 {
1759 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001760 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001762 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1763 if (pend1 != pend)
1764 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001765 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001766 if (pnew != NULL)
1767 {
1768 STRNCPY(pnew, p, (pend1 - p));
1769 STRCPY(pnew + (pend1 - p), "Resources/vim");
1770 p = pnew;
1771 pend = p + STRLEN(p);
1772 }
1773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001776 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 if (p == exe_name)
1778 pend = remove_tail(p, pend, (char_u *)"src");
1779#endif
1780
Bram Moolenaar85a20022019-12-21 18:25:54 +01001781 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (!vimruntime)
1783 {
1784 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1785 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1786 }
1787
Bram Moolenaar85a20022019-12-21 18:25:54 +01001788 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001789 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001792#ifdef MACOS_X
1793 if (p == exe_name || p == p_hf)
1794#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001795 // check that the result is a directory name
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001796 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001799 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 else
1801 {
1802#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001803 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1805 {
1806 vim_free(p);
1807 p = pend;
1808 }
1809#endif
1810 *mustfree = TRUE;
1811 }
1812 }
1813 }
1814
1815#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001816 // When there is a pathdef.c file we can use default_vim_dir and
1817 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (p == NULL)
1819 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001820 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (vimruntime && *default_vimruntime_dir != NUL)
1822 {
1823 p = default_vimruntime_dir;
1824 *mustfree = FALSE;
1825 }
1826 else if (*default_vim_dir != NUL)
1827 {
1828 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1829 *mustfree = TRUE;
1830 else
1831 {
1832 p = default_vim_dir;
1833 *mustfree = FALSE;
1834 }
1835 }
1836 }
1837#endif
1838
1839 /*
1840 * Set the environment variable, so that the new value can be found fast
1841 * next time, and others can also use it (e.g. Perl).
1842 */
1843 if (p != NULL)
1844 {
1845 if (vimruntime)
1846 {
1847 vim_setenv((char_u *)"VIMRUNTIME", p);
1848 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 else
1851 {
1852 vim_setenv((char_u *)"VIM", p);
1853 didset_vim = TRUE;
1854 }
1855 }
1856 return p;
1857}
1858
Bram Moolenaar113e1072019-01-20 15:30:40 +01001859#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02001860 void
1861vim_unsetenv(char_u *var)
1862{
1863#ifdef HAVE_UNSETENV
1864 unsetenv((char *)var);
1865#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001866 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001867#endif
1868}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001869#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02001870
1871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001873 * Set environment variable "name" and take care of side effects.
1874 */
1875 void
1876vim_setenv_ext(char_u *name, char_u *val)
1877{
1878 vim_setenv(name, val);
1879 if (STRICMP(name, "HOME") == 0)
1880 init_homedir();
1881 else if (didset_vim && STRICMP(name, "VIM") == 0)
1882 didset_vim = FALSE;
1883 else if (didset_vimruntime
1884 && STRICMP(name, "VIMRUNTIME") == 0)
1885 didset_vimruntime = FALSE;
1886}
1887
1888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 * Our portable version of setenv.
1890 */
1891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001892vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894#ifdef HAVE_SETENV
1895 mch_setenv((char *)name, (char *)val, 1);
1896#else
1897 char_u *envbuf;
1898
1899 /*
1900 * Putenv does not copy the string, it has to remain
1901 * valid. The allocated memory will never be freed.
1902 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001903 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (envbuf != NULL)
1905 {
1906 sprintf((char *)envbuf, "%s=%s", name, val);
1907 putenv((char *)envbuf);
1908 }
1909#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001910#ifdef FEAT_GETTEXT
1911 /*
1912 * When setting $VIMRUNTIME adjust the directory to find message
1913 * translations to $VIMRUNTIME/lang.
1914 */
1915 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1916 {
1917 char_u *buf = concat_str(val, (char_u *)"/lang");
1918
1919 if (buf != NULL)
1920 {
1921 bindtextdomain(VIMPACKAGE, (char *)buf);
1922 vim_free(buf);
1923 }
1924 }
1925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926}
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928/*
1929 * Function given to ExpandGeneric() to obtain an environment variable name.
1930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001932get_env_name(
1933 expand_T *xp UNUSED,
1934 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935{
Bram Moolenaard0573012017-10-28 21:11:06 +02001936# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02001938 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
1940 return NULL;
1941# else
1942# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01001943 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 extern char **environ;
1945# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001946# define ENVNAMELEN 100
1947 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 char_u *str;
1949 int n;
1950
1951 str = (char_u *)environ[idx];
1952 if (str == NULL)
1953 return NULL;
1954
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001955 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 if (str[n] == '=' || str[n] == NUL)
1958 break;
1959 name[n] = str[n];
1960 }
1961 name[n] = NUL;
1962 return name;
1963# endif
1964}
Bram Moolenaar24305862012-08-15 14:05:05 +02001965
1966/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001967 * Add a user name to the list of users in ga_users.
1968 * Do nothing if user name is NULL or empty.
1969 */
1970 static void
1971add_user(char_u *user, int need_copy)
1972{
1973 char_u *user_copy = (user != NULL && need_copy)
1974 ? vim_strsave(user) : user;
1975
1976 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
1977 {
1978 if (need_copy)
1979 vim_free(user);
1980 return;
1981 }
1982 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
1983}
1984
1985/*
Bram Moolenaar24305862012-08-15 14:05:05 +02001986 * Find all user names for user completion.
1987 * Done only once and then cached.
1988 */
1989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001990init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02001991{
Bram Moolenaar24305862012-08-15 14:05:05 +02001992 static int lazy_init_done = FALSE;
1993
1994 if (lazy_init_done)
1995 return;
1996
1997 lazy_init_done = TRUE;
1998 ga_init2(&ga_users, sizeof(char_u *), 20);
1999
2000# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2001 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002002 struct passwd* pw;
2003
2004 setpwent();
2005 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002006 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002007 endpwent();
2008 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002009# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002010 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002011 DWORD nusers = 0, ntotal = 0, i;
2012 PUSER_INFO_0 uinfo;
2013
2014 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2015 &nusers, &ntotal, NULL) == NERR_Success)
2016 {
2017 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002018 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002019
2020 NetApiBufferFree(uinfo);
2021 }
2022 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002023# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002024# if defined(HAVE_GETPWNAM)
2025 {
2026 char_u *user_env = mch_getenv((char_u *)"USER");
2027
2028 // The $USER environment variable may be a valid remote user name (NIS,
2029 // LDAP) not already listed by getpwent(), as getpwent() only lists
2030 // local user names. If $USER is not already listed, check whether it
2031 // is a valid remote user name using getpwnam() and if it is, add it to
2032 // the list of user names.
2033
2034 if (user_env != NULL && *user_env != NUL)
2035 {
2036 int i;
2037
2038 for (i = 0; i < ga_users.ga_len; i++)
2039 {
2040 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2041
2042 if (STRCMP(local_user, user_env) == 0)
2043 break;
2044 }
2045
2046 if (i == ga_users.ga_len)
2047 {
2048 struct passwd *pw = getpwnam((char *)user_env);
2049
2050 if (pw != NULL)
2051 add_user((char_u *)pw->pw_name, TRUE);
2052 }
2053 }
2054 }
2055# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002056}
2057
2058/*
2059 * Function given to ExpandGeneric() to obtain an user names.
2060 */
2061 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002062get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002063{
2064 init_users();
2065 if (idx < ga_users.ga_len)
2066 return ((char_u **)ga_users.ga_data)[idx];
2067 return NULL;
2068}
2069
2070/*
2071 * Check whether name matches a user name. Return:
2072 * 0 if name does not match any user name.
2073 * 1 if name partially matches the beginning of a user name.
2074 * 2 is name fully matches a user name.
2075 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002076 int
2077match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002078{
2079 int i;
2080 int n = (int)STRLEN(name);
2081 int result = 0;
2082
2083 init_users();
2084 for (i = 0; i < ga_users.ga_len; i++)
2085 {
2086 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002087 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002088 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002089 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002090 }
2091 return result;
2092}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002095 * Concatenate two strings and return the result in allocated memory.
2096 * Returns NULL when out of memory.
2097 */
2098 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002099concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002100{
2101 char_u *dest;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 size_t l = str1 == NULL ? 0 : STRLEN(str1);
Bram Moolenaard6754642005-01-17 22:18:45 +00002103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 dest = alloc(l + (str2 == NULL ? 0 : STRLEN(str2)) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002105 if (dest != NULL)
2106 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 if (str1 == NULL)
2108 *dest = NUL;
2109 else
2110 STRCPY(dest, str1);
2111 if (str2 != NULL)
2112 STRCPY(dest + l, str2);
Bram Moolenaard6754642005-01-17 22:18:45 +00002113 }
2114 return dest;
2115}
Bram Moolenaard6754642005-01-17 22:18:45 +00002116
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002120#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002121 // Ignore SIGHUP, because a dropped connection causes a read error, which
2122 // makes Vim exit and then handling SIGHUP causes various reentrance
2123 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002124 signal(SIGHUP, SIG_IGN);
2125#endif
2126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127#ifdef FEAT_GUI
2128 if (gui.in_use)
2129 {
2130 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002131 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133 else
2134#endif
2135 {
2136 windgoto((int)Rows - 1, 0);
2137
2138 /*
2139 * Switch terminal mode back now, so messages end up on the "normal"
2140 * screen (if there are two screens).
2141 */
2142 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002143 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 out_flush();
2145 }
2146}
2147
2148/*
2149 * Preserve files and exit.
2150 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002151 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2152 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 */
2154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002155preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 buf_T *buf;
2158
2159 prepare_to_exit();
2160
Bram Moolenaar85a20022019-12-21 18:25:54 +01002161 // Setting this will prevent free() calls. That avoids calling free()
2162 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002163 really_exiting = TRUE;
2164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002166 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 out_flush();
2168
Bram Moolenaar85a20022019-12-21 18:25:54 +01002169 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaar29323592016-07-24 22:04:11 +02002171 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2174 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002175 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002176 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002178 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 break;
2180 }
2181 }
2182
Bram Moolenaar85a20022019-12-21 18:25:54 +01002183 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002185 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 getout(1);
2188}
2189
2190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 * Check for CTRL-C pressed, but only once in a while.
2192 * Should be used instead of ui_breakcheck() for functions that check for
2193 * each line in the file. Calling ui_breakcheck() each time takes too much
2194 * time, because it can be a system call.
2195 */
2196
2197#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002198# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#endif
2200
2201static int breakcheck_count = 0;
2202
2203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002204line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 if (++breakcheck_count >= BREAKCHECK_SKIP)
2207 {
2208 breakcheck_count = 0;
2209 ui_breakcheck();
2210 }
2211}
2212
2213/*
2214 * Like line_breakcheck() but check 10 times less often.
2215 */
2216 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002217fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2220 {
2221 breakcheck_count = 0;
2222 ui_breakcheck();
2223 }
2224}
2225
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002226/*
2227 * Like line_breakcheck() but check 100 times less often.
2228 */
2229 void
2230veryfast_breakcheck(void)
2231{
2232 if (++breakcheck_count >= BREAKCHECK_SKIP * 100)
2233 {
2234 breakcheck_count = 0;
2235 ui_breakcheck();
2236 }
2237}
2238
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002239#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2240 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2241 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
2243#ifndef SEEK_SET
2244# define SEEK_SET 0
2245#endif
2246#ifndef SEEK_END
2247# define SEEK_END 2
2248#endif
2249
2250/*
2251 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002252 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2253 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 * Returns an allocated string, or NULL for error.
2255 */
2256 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002257get_cmd_output(
2258 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002259 char_u *infile, // optional input file name
2260 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002261 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
2263 char_u *tempname;
2264 char_u *command;
2265 char_u *buffer = NULL;
2266 int len;
2267 int i = 0;
2268 FILE *fd;
2269
2270 if (check_restricted() || check_secure())
2271 return NULL;
2272
Bram Moolenaar85a20022019-12-21 18:25:54 +01002273 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002274 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002276 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return NULL;
2278 }
2279
Bram Moolenaar85a20022019-12-21 18:25:54 +01002280 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002281 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (command == NULL)
2283 goto done;
2284
2285 /*
2286 * Call the shell to execute the command (errors are ignored).
2287 * Don't check timestamps here.
2288 */
2289 ++no_check_timestamps;
2290 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2291 --no_check_timestamps;
2292
2293 vim_free(command);
2294
2295 /*
2296 * read the names from the file into memory
2297 */
2298# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002299 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 fd = mch_fopen((char *)tempname, "r");
2301# else
2302 fd = mch_fopen((char *)tempname, READBIN);
2303# endif
2304
2305 if (fd == NULL)
2306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002307 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 goto done;
2309 }
2310
2311 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002312 len = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 fseek(fd, 0L, SEEK_SET);
2314
2315 buffer = alloc(len + 1);
2316 if (buffer != NULL)
2317 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2318 fclose(fd);
2319 mch_remove(tempname);
2320 if (buffer == NULL)
2321 goto done;
2322#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002323 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#endif
2325 if (i != len)
2326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002327 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002328 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002330 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002333 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002334 if (buffer[i] == NUL)
2335 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002336
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002338 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002339 else
2340 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342done:
2343 vim_free(tempname);
2344 return buffer;
2345}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002346
2347# if defined(FEAT_EVAL) || defined(PROTO)
2348
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002349 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002350get_cmd_output_as_rettv(
2351 typval_T *argvars,
2352 typval_T *rettv,
2353 int retlist)
2354{
2355 char_u *res = NULL;
2356 char_u *p;
2357 char_u *infile = NULL;
2358 int err = FALSE;
2359 FILE *fd;
2360 list_T *list = NULL;
2361 int flags = SHELL_SILENT;
2362
2363 rettv->v_type = VAR_STRING;
2364 rettv->vval.v_string = NULL;
2365 if (check_restricted() || check_secure())
2366 goto errret;
2367
2368 if (argvars[1].v_type != VAR_UNKNOWN)
2369 {
2370 /*
2371 * Write the text to a temp file, to be used for input of the shell
2372 * command.
2373 */
2374 if ((infile = vim_tempname('i', TRUE)) == NULL)
2375 {
2376 emsg(_(e_notmp));
2377 goto errret;
2378 }
2379
2380 fd = mch_fopen((char *)infile, WRITEBIN);
2381 if (fd == NULL)
2382 {
2383 semsg(_(e_notopen), infile);
2384 goto errret;
2385 }
2386 if (argvars[1].v_type == VAR_NUMBER)
2387 {
2388 linenr_T lnum;
2389 buf_T *buf;
2390
2391 buf = buflist_findnr(argvars[1].vval.v_number);
2392 if (buf == NULL)
2393 {
2394 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2395 fclose(fd);
2396 goto errret;
2397 }
2398
2399 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2400 {
2401 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2402 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2403 {
2404 err = TRUE;
2405 break;
2406 }
2407 if (putc(NL, fd) == EOF)
2408 {
2409 err = TRUE;
2410 break;
2411 }
2412 }
2413 }
2414 else if (argvars[1].v_type == VAR_LIST)
2415 {
2416 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2417 err = TRUE;
2418 }
2419 else
2420 {
2421 size_t len;
2422 char_u buf[NUMBUFLEN];
2423
2424 p = tv_get_string_buf_chk(&argvars[1], buf);
2425 if (p == NULL)
2426 {
2427 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002428 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002429 }
2430 len = STRLEN(p);
2431 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2432 err = TRUE;
2433 }
2434 if (fclose(fd) != 0)
2435 err = TRUE;
2436 if (err)
2437 {
2438 emsg(_("E677: Error writing temp file"));
2439 goto errret;
2440 }
2441 }
2442
Bram Moolenaar85a20022019-12-21 18:25:54 +01002443 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2444 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002445 if (!msg_silent)
2446 flags += SHELL_COOKED;
2447
2448 if (retlist)
2449 {
2450 int len;
2451 listitem_T *li;
2452 char_u *s = NULL;
2453 char_u *start;
2454 char_u *end;
2455 int i;
2456
2457 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2458 if (res == NULL)
2459 goto errret;
2460
2461 list = list_alloc();
2462 if (list == NULL)
2463 goto errret;
2464
2465 for (i = 0; i < len; ++i)
2466 {
2467 start = res + i;
2468 while (i < len && res[i] != NL)
2469 ++i;
2470 end = res + i;
2471
2472 s = alloc(end - start + 1);
2473 if (s == NULL)
2474 goto errret;
2475
2476 for (p = s; start < end; ++p, ++start)
2477 *p = *start == NUL ? NL : *start;
2478 *p = NUL;
2479
2480 li = listitem_alloc();
2481 if (li == NULL)
2482 {
2483 vim_free(s);
2484 goto errret;
2485 }
2486 li->li_tv.v_type = VAR_STRING;
2487 li->li_tv.v_lock = 0;
2488 li->li_tv.vval.v_string = s;
2489 list_append(list, li);
2490 }
2491
2492 rettv_list_set(rettv, list);
2493 list = NULL;
2494 }
2495 else
2496 {
2497 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2498#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002499 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002500 if (res != NULL)
2501 {
2502 char_u *s, *d;
2503
2504 d = res;
2505 for (s = res; *s; ++s)
2506 {
2507 if (s[0] == CAR && s[1] == NL)
2508 ++s;
2509 *d++ = *s;
2510 }
2511 *d = NUL;
2512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002514 rettv->vval.v_string = res;
2515 res = NULL;
2516 }
2517
2518errret:
2519 if (infile != NULL)
2520 {
2521 mch_remove(infile);
2522 vim_free(infile);
2523 }
2524 if (res != NULL)
2525 vim_free(res);
2526 if (list != NULL)
2527 list_free(list);
2528}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002531 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 */
2533 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002534f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002536 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537}
2538
2539/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002540 * "systemlist()" function
2541 */
2542 void
2543f_systemlist(typval_T *argvars, typval_T *rettv)
2544{
2545 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2546}
2547# endif // FEAT_EVAL
2548
2549#endif
2550
2551/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002552 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 * Don't do this when still processing a command or a mapping.
2554 * Don't do this when inside a ":normal" command.
2555 */
2556 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002557goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
2559 return (p_im && stuff_empty() && typebuf_typed());
2560}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002561
2562/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002563 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002564 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2565 * - Remove any argument. E.g., "csh -f" -> "csh".
2566 * But don't allow a space in the path, so that this works:
2567 * "/usr/bin/csh --rcfile ~/.cshrc"
2568 * But don't do that for Windows, it's common to have a space in the path.
2569 */
2570 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002571get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002572{
2573 char_u *p;
2574
Bram Moolenaar4f974752019-02-17 17:44:42 +01002575#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002576 p = gettail(p_sh);
2577 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
2578#else
2579 p = skiptowhite(p_sh);
2580 if (*p == NUL)
2581 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002582 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002583 p = vim_strsave(gettail(p_sh));
2584 }
2585 else
2586 {
2587 char_u *p1, *p2;
2588
Bram Moolenaar85a20022019-12-21 18:25:54 +01002589 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002590 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002591 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002592 if (vim_ispathsep(*p2))
2593 p1 = p2 + 1;
2594 p = vim_strnsave(p1, (int)(p - p1));
2595 }
2596#endif
2597 return p;
2598}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002599
2600/*
2601 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2602 * Also check for ":\\", which MS Internet Explorer accepts, return
2603 * URL_BACKSLASH.
2604 */
2605 int
2606path_is_url(char_u *p)
2607{
2608 if (STRNCMP(p, "://", (size_t)3) == 0)
2609 return URL_SLASH;
2610 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2611 return URL_BACKSLASH;
2612 return 0;
2613}
2614
2615/*
2616 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
2617 * Return URL_BACKSLASH for "name:\\".
2618 * Return zero otherwise.
2619 */
2620 int
2621path_with_url(char_u *fname)
2622{
2623 char_u *p;
2624
2625 for (p = fname; isalpha(*p); ++p)
2626 ;
2627 return path_is_url(p);
2628}