blob: 85ab727d323774a47b2e1de046f2c47ec8b52ee8 [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 Moolenaar0a52df52019-08-18 22:26:31 +020017#if defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020018# include <lm.h>
19#endif
20
Bram Moolenaar85a20022019-12-21 18:25:54 +010021#define URL_SLASH 1 // path_is_url() has found "://"
22#define URL_BACKSLASH 2 // path_is_url() has found ":\\"
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010023
Bram Moolenaar0a52df52019-08-18 22:26:31 +020024// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020025static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020026
Bram Moolenaar071d4272004-06-13 20:20:40 +000027/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +020028 * get_leader_len() returns the length in bytes of the prefix of the given
29 * string which introduces a comment. If this string is not a comment then
30 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 * When "flags" is not NULL, it is set to point to the flags of the recognized
32 * comment leader.
33 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +020034 * If "include_space" is set, include trailing whitespace while calculating the
35 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 */
37 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010038get_leader_len(
39 char_u *line,
40 char_u **flags,
41 int backward,
42 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
44 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +020045 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 int got_com = FALSE;
47 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +010048 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
49 char_u *string; // pointer to comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +020051 int middle_match_len = 0;
52 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +020053 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaar81340392012-06-06 16:12:59 +020055 result = i = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +010056 while (VIM_ISWHITE(line[i])) // leading white space is ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 ++i;
58
59 /*
60 * Repeat to match several nested comment strings.
61 */
Bram Moolenaara4271d52011-05-10 13:38:27 +020062 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 {
64 /*
65 * scan through the 'comments' option for a match
66 */
67 found_one = FALSE;
68 for (list = curbuf->b_p_com; *list; )
69 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010070 // Get one option part into part_buf[]. Advance "list" to next
71 // one. Put "string" at start of string.
Bram Moolenaara4271d52011-05-10 13:38:27 +020072 if (!got_com && flags != NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +010073 *flags = list; // remember where flags started
Bram Moolenaara4271d52011-05-10 13:38:27 +020074 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
76 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +010077 if (string == NULL) // missing ':', ignore this part
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +010079 *string++ = NUL; // isolate flags from string
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar85a20022019-12-21 18:25:54 +010081 // If we found a middle match previously, use that match when this
82 // is not a middle or end.
Bram Moolenaara4271d52011-05-10 13:38:27 +020083 if (middle_match_len != 0
84 && vim_strchr(part_buf, COM_MIDDLE) == NULL
85 && vim_strchr(part_buf, COM_END) == NULL)
86 break;
87
Bram Moolenaar85a20022019-12-21 18:25:54 +010088 // When we already found a nested comment, only accept further
89 // nested comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
91 continue;
92
Bram Moolenaar85a20022019-12-21 18:25:54 +010093 // When 'O' flag present and using "O" command skip this one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
95 continue;
96
Bram Moolenaar85a20022019-12-21 18:25:54 +010097 // Line contents and string must match.
98 // When string starts with white space, must have some white space
99 // (but the amount does not need to match, there might be a mix of
100 // TABs and spaces).
Bram Moolenaar1c465442017-03-12 20:10:05 +0100101 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100103 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100104 continue; // missing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +0100105 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 ++string;
107 }
108 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
109 ;
110 if (string[j] != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100111 continue; // string doesn't match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaar85a20022019-12-21 18:25:54 +0100113 // When 'b' flag used, there must be white space or an
114 // end-of-line after the string in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100116 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 continue;
118
Bram Moolenaar85a20022019-12-21 18:25:54 +0100119 // We have found a match, stop searching unless this is a middle
120 // comment. The middle comment can be a substring of the end
121 // comment in which case it's better to return the length of the
122 // end comment and its flags. Thus we keep searching with middle
123 // and end matches and use an end match if it matches better.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200124 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
125 {
126 if (middle_match_len == 0)
127 {
128 middle_match_len = j;
129 saved_flags = prev_list;
130 }
131 continue;
132 }
133 if (middle_match_len != 0 && j > middle_match_len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100134 // Use this match instead of the middle match, since it's a
135 // longer thus better match.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200136 middle_match_len = 0;
137
138 if (middle_match_len == 0)
139 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 found_one = TRUE;
141 break;
142 }
143
Bram Moolenaara4271d52011-05-10 13:38:27 +0200144 if (middle_match_len != 0)
145 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100146 // Use the previously found middle match after failing to find a
147 // match with an end.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200148 if (!got_com && flags != NULL)
149 *flags = saved_flags;
150 i += middle_match_len;
151 found_one = TRUE;
152 }
153
Bram Moolenaar85a20022019-12-21 18:25:54 +0100154 // No match found, stop scanning.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (!found_one)
156 break;
157
Bram Moolenaar81340392012-06-06 16:12:59 +0200158 result = i;
159
Bram Moolenaar85a20022019-12-21 18:25:54 +0100160 // Include any trailing white space.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100161 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 ++i;
163
Bram Moolenaar81340392012-06-06 16:12:59 +0200164 if (include_space)
165 result = i;
166
Bram Moolenaar85a20022019-12-21 18:25:54 +0100167 // If this comment doesn't nest, stop here.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200168 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 if (vim_strchr(part_buf, COM_NEST) == NULL)
170 break;
171 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200172 return result;
173}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200174
Bram Moolenaar81340392012-06-06 16:12:59 +0200175/*
176 * Return the offset at which the last comment in line starts. If there is no
177 * comment in the whole line, -1 is returned.
178 *
179 * When "flags" is not null, it is set to point to the flags describing the
180 * recognized comment leader.
181 */
182 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100183get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200184{
185 int result = -1;
186 int i, j;
187 int lower_check_bound = 0;
188 char_u *string;
189 char_u *com_leader;
190 char_u *com_flags;
191 char_u *list;
192 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100193 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200194
195 /*
196 * Repeat to match several nested comment strings.
197 */
198 i = (int)STRLEN(line);
199 while (--i >= lower_check_bound)
200 {
201 /*
202 * scan through the 'comments' option for a match
203 */
204 found_one = FALSE;
205 for (list = curbuf->b_p_com; *list; )
206 {
207 char_u *flags_save = list;
208
209 /*
210 * Get one option part into part_buf[]. Advance list to next one.
211 * put string at start of string.
212 */
213 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
214 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +0100215 if (string == NULL) // If everything is fine, this cannot actually
216 // happen.
Bram Moolenaar81340392012-06-06 16:12:59 +0200217 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100218 *string++ = NUL; // Isolate flags from string.
Bram Moolenaar81340392012-06-06 16:12:59 +0200219 com_leader = string;
220
221 /*
222 * Line contents and string must match.
223 * When string starts with white space, must have some white space
224 * (but the amount does not need to match, there might be a mix of
225 * TABs and spaces).
226 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100227 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200228 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100229 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200230 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100231 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200232 ++string;
233 }
234 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
235 /* do nothing */;
236 if (string[j] != NUL)
237 continue;
238
239 /*
240 * When 'b' flag used, there must be white space or an
241 * end-of-line after the string in the line.
242 */
243 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100244 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200245 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100246
Bram Moolenaar4af72592018-12-09 15:00:52 +0100247 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100248 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100249 // For a middlepart comment, only consider it to match if
250 // everything before the current position in the line is
251 // whitespace. Otherwise we would think we are inside a
252 // comment if the middle part appears somewhere in the middle
253 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100254 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
255 ;
256 if (j < i)
257 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200258 }
259
260 /*
261 * We have found a match, stop searching.
262 */
263 found_one = TRUE;
264
265 if (flags)
266 *flags = flags_save;
267 com_flags = flags_save;
268
269 break;
270 }
271
272 if (found_one)
273 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100274 char_u part_buf2[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200275 int len1, len2, off;
276
277 result = i;
278 /*
279 * If this comment nests, continue searching.
280 */
281 if (vim_strchr(part_buf, COM_NEST) != NULL)
282 continue;
283
284 lower_check_bound = i;
285
Bram Moolenaar85a20022019-12-21 18:25:54 +0100286 // Let's verify whether the comment leader found is a substring
287 // of other comment leaders. If it is, let's adjust the
288 // lower_check_bound so that we make sure that we have determined
289 // the comment leader correctly.
Bram Moolenaar81340392012-06-06 16:12:59 +0200290
Bram Moolenaar1c465442017-03-12 20:10:05 +0100291 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200292 ++com_leader;
293 len1 = (int)STRLEN(com_leader);
294
295 for (list = curbuf->b_p_com; *list; )
296 {
297 char_u *flags_save = list;
298
299 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
300 if (flags_save == com_flags)
301 continue;
302 string = vim_strchr(part_buf2, ':');
303 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100304 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200305 ++string;
306 len2 = (int)STRLEN(string);
307 if (len2 == 0)
308 continue;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // Now we have to verify whether string ends with a substring
311 // beginning the com_leader.
Bram Moolenaar81340392012-06-06 16:12:59 +0200312 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
313 {
314 --off;
315 if (!STRNCMP(string + off, com_leader, len2 - off))
316 {
317 if (i - off < lower_check_bound)
318 lower_check_bound = i - off;
319 }
320 }
321 }
322 }
323 }
324 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
327/*
328 * Return the number of window lines occupied by buffer line "lnum".
329 */
330 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100331plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332{
333 return plines_win(curwin, lnum, TRUE);
334}
335
336 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100337plines_win(
338 win_T *wp,
339 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
342#if defined(FEAT_DIFF) || defined(PROTO)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100343 // Check for filler lines above this buffer line. When folded the result
344 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
346}
347
348 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100349plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
351 return plines_win_nofill(curwin, lnum, TRUE);
352}
353
354 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100355plines_win_nofill(
356 win_T *wp,
357 linenr_T lnum,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100358 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
360#endif
361 int lines;
362
363 if (!wp->w_p_wrap)
364 return 1;
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 if (wp->w_width == 0)
367 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
369#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100370 // A folded lines is handled just like an empty line.
371 // NOTE: Caller must handle lines that are MAYBE folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 if (lineFolded(wp, lnum) == TRUE)
373 return 1;
374#endif
375
376 lines = plines_win_nofold(wp, lnum);
377 if (winheight > 0 && lines > wp->w_height)
378 return (int)wp->w_height;
379 return lines;
380}
381
382/*
383 * Return number of window lines physical line "lnum" will occupy in window
384 * "wp". Does not care about folding, 'wrap' or 'diff'.
385 */
386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100387plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388{
389 char_u *s;
390 long col;
391 int width;
392
393 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100394 if (*s == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 return 1;
396 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
397
398 /*
399 * If list mode is on, then the '$' at the end of the line may take up one
400 * extra column.
401 */
402 if (wp->w_p_list && lcs_eol != NUL)
403 col += 1;
404
405 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200406 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200408 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 if (width <= 0)
410 return 32000;
411 if (col <= width)
412 return 1;
413 col -= width;
414 width += win_col_off2(wp);
415 return (col + (width - 1)) / width + 1;
416}
417
418/*
419 * Like plines_win(), but only reports the number of physical screen lines
420 * used from the start of the line to the given column number.
421 */
422 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100423plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424{
425 long col;
426 char_u *s;
427 int lines = 0;
428 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200429 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100432 // Check for filler lines above this buffer line. When folded the result
433 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 lines = diff_check_fill(wp, lnum);
435#endif
436
437 if (!wp->w_p_wrap)
438 return lines + 1;
439
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 if (wp->w_width == 0)
441 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
Bram Moolenaar597a4222014-06-25 14:39:50 +0200443 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445 col = 0;
446 while (*s != NUL && --column >= 0)
447 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200448 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100449 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 }
451
452 /*
453 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
454 * INSERT mode, then col must be adjusted so that it represents the last
455 * screen position of the TAB. This only fixes an error when the TAB wraps
456 * from one screen line to the next (when 'columns' is not a multiple of
457 * 'ts') -- webb.
458 */
459 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200460 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
462 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200463 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200465 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +0000466 if (width <= 0)
467 return 9999;
468
469 lines += 1;
470 if (col > width)
471 lines += (col - width) / (width + win_col_off2(wp)) + 1;
472 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473}
474
475 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100476plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 int count = 0;
479
480 while (first <= last)
481 {
482#ifdef FEAT_FOLDING
483 int x;
484
Bram Moolenaar85a20022019-12-21 18:25:54 +0100485 // Check if there are any really folded lines, but also included lines
486 // that are maybe folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 x = foldedCount(wp, first, NULL);
488 if (x > 0)
489 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100490 ++count; // count 1 for "+-- folded" line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 first += x;
492 }
493 else
494#endif
495 {
496#ifdef FEAT_DIFF
497 if (first == wp->w_topline)
498 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
499 else
500#endif
501 count += plines_win(wp, first, TRUE);
502 ++first;
503 }
504 }
505 return (count);
506}
507
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100509gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100511 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar85a20022019-12-21 18:25:54 +0100513 // When searching columns is sometimes put at the end of a line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100514 if (pos->col == MAXCOL)
515 return NUL;
516 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (has_mbyte)
518 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 return (int)*ptr;
520}
521
522 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 if (has_mbyte)
526 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 return (int)*ml_get_cursor();
528}
529
530/*
531 * Write a character at the current cursor position.
532 * It is directly written into the block.
533 */
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
538 + curwin->w_cursor.col) = c;
539}
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * Skip to next part of an option argument: Skip space and comma.
543 */
544 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100545skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 if (*p == ',')
548 ++p;
549 while (*p == ' ')
550 ++p;
551 return p;
552}
553
554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 * check_status: called when the status bars for the buffer 'buf'
556 * need to be updated
557 */
558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100559check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 win_T *wp;
562
Bram Moolenaar29323592016-07-24 22:04:11 +0200563 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (wp->w_buffer == buf && wp->w_status_height)
565 {
566 wp->w_redr_status = TRUE;
567 if (must_redraw < VALID)
568 must_redraw = VALID;
569 }
570}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 * Ask for a reply from the user, a 'y' or a 'n'.
574 * No other characters are accepted, the message is repeated until a valid
575 * reply is entered or CTRL-C is hit.
576 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
577 * from any buffers but directly from the user.
578 *
579 * return the 'y' or 'n'
580 */
581 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100582ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 int r = ' ';
585 int save_State = State;
586
Bram Moolenaar85a20022019-12-21 18:25:54 +0100587 if (exiting) // put terminal in raw mode for this question
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 settmode(TMODE_RAW);
589 ++no_wait_return;
590#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200591 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592#endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200593 State = CONFIRM; // mouse behaves like with :confirm
594 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200596 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598 while (r != 'y' && r != 'n')
599 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100600 // same highlighting as for wait_return
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100601 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (direct)
603 r = get_keystroke();
604 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000605 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (r == Ctrl_C || r == ESC)
607 r = 'n';
Bram Moolenaar85a20022019-12-21 18:25:54 +0100608 msg_putchar(r); // show what you typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 out_flush();
610 }
611 --no_wait_return;
612 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 --no_mapping;
615 --allow_keys;
616
617 return r;
618}
619
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200620#if defined(FEAT_EVAL) || defined(PROTO)
621
622/*
623 * "mode()" function
624 */
625 void
626f_mode(typval_T *argvars, typval_T *rettv)
627{
628 char_u buf[4];
629
630 vim_memset(buf, 0, sizeof(buf));
631
632 if (time_for_testing == 93784)
633 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100634 // Testing the two-character code.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200635 buf[0] = 'x';
636 buf[1] = '!';
637 }
638#ifdef FEAT_TERMINAL
639 else if (term_use_loop())
640 buf[0] = 't';
641#endif
642 else if (VIsual_active)
643 {
644 if (VIsual_select)
645 buf[0] = VIsual_mode + 's' - 'v';
646 else
647 buf[0] = VIsual_mode;
648 }
649 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
650 || State == CONFIRM)
651 {
652 buf[0] = 'r';
653 if (State == ASKMORE)
654 buf[1] = 'm';
655 else if (State == CONFIRM)
656 buf[1] = '?';
657 }
658 else if (State == EXTERNCMD)
659 buf[0] = '!';
660 else if (State & INSERT)
661 {
662 if (State & VREPLACE_FLAG)
663 {
664 buf[0] = 'R';
665 buf[1] = 'v';
666 }
667 else
668 {
669 if (State & REPLACE_FLAG)
670 buf[0] = 'R';
671 else
672 buf[0] = 'i';
673 if (ins_compl_active())
674 buf[1] = 'c';
675 else if (ctrl_x_mode_not_defined_yet())
676 buf[1] = 'x';
677 }
678 }
679 else if ((State & CMDLINE) || exmode_active)
680 {
681 buf[0] = 'c';
682 if (exmode_active == EXMODE_VIM)
683 buf[1] = 'v';
684 else if (exmode_active == EXMODE_NORMAL)
685 buf[1] = 'e';
686 }
687 else
688 {
689 buf[0] = 'n';
690 if (finish_op)
691 {
692 buf[1] = 'o';
693 // to be able to detect force-linewise/blockwise/characterwise operations
694 buf[2] = motion_force;
695 }
696 else if (restart_edit == 'I' || restart_edit == 'R'
697 || restart_edit == 'V')
698 {
699 buf[1] = 'i';
700 buf[2] = restart_edit;
701 }
702 }
703
Bram Moolenaar85a20022019-12-21 18:25:54 +0100704 // Clear out the minor mode when the argument is not a non-zero number or
705 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200706 if (!non_zero_arg(&argvars[0]))
707 buf[1] = NUL;
708
709 rettv->vval.v_string = vim_strsave(buf);
710 rettv->v_type = VAR_STRING;
711}
712
713 static void
714may_add_state_char(garray_T *gap, char_u *include, int c)
715{
716 if (include == NULL || vim_strchr(include, c) != NULL)
717 ga_append(gap, c);
718}
719
720/*
721 * "state()" function
722 */
723 void
724f_state(typval_T *argvars, typval_T *rettv)
725{
726 garray_T ga;
727 char_u *include = NULL;
728 int i;
729
730 ga_init2(&ga, 1, 20);
731 if (argvars[0].v_type != VAR_UNKNOWN)
732 include = tv_get_string(&argvars[0]);
733
734 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
735 may_add_state_char(&ga, include, 'm');
736 if (op_pending())
737 may_add_state_char(&ga, include, 'o');
738 if (autocmd_busy)
739 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200740 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200741 may_add_state_char(&ga, include, 'a');
742
743# ifdef FEAT_JOB_CHANNEL
744 if (channel_in_blocking_wait())
745 may_add_state_char(&ga, include, 'w');
746# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200747 if (!get_was_safe_state())
748 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200749 for (i = 0; i < get_callback_depth() && i < 3; ++i)
750 may_add_state_char(&ga, include, 'c');
751 if (msg_scrolled > 0)
752 may_add_state_char(&ga, include, 's');
753
754 rettv->v_type = VAR_STRING;
755 rettv->vval.v_string = ga.ga_data;
756}
757
758#endif // FEAT_EVAL
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760/*
761 * Get a key stroke directly from the user.
762 * Ignores mouse clicks and scrollbar events, except a click for the left
763 * button (used at the more prompt).
764 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
765 * Disadvantage: typeahead is ignored.
766 * Translates the interrupt character for unix to ESC.
767 */
768 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100769get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100771 char_u *buf = NULL;
772 int buflen = 150;
773 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int len = 0;
775 int n;
776 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000777 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
Bram Moolenaar85a20022019-12-21 18:25:54 +0100779 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 for (;;)
781 {
782 cursor_on();
783 out_flush();
784
Bram Moolenaar85a20022019-12-21 18:25:54 +0100785 // Leave some room for check_termcode() to insert a key code into (max
786 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
787 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100788 maxlen = (buflen - 6 - len) / 3;
789 if (buf == NULL)
790 buf = alloc(buflen);
791 else if (maxlen < 10)
792 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100793 char_u *t_buf = buf;
794
Bram Moolenaar85a20022019-12-21 18:25:54 +0100795 // Need some more space. This might happen when receiving a long
796 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100797 buflen += 100;
798 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100799 if (buf == NULL)
800 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100801 maxlen = (buflen - 6 - len) / 3;
802 }
803 if (buf == NULL)
804 {
805 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100806 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100807 }
808
Bram Moolenaar85a20022019-12-21 18:25:54 +0100809 // First time: blocking wait. Second time: wait up to 100ms for a
810 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100811 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if (n > 0)
813 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100814 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200815 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000817 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000819 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100820 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Bram Moolenaar85a20022019-12-21 18:25:54 +0100822 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100823 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000824 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000826
Bram Moolenaar85a20022019-12-21 18:25:54 +0100827 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100828 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100829 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100830 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100831 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100832 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100833 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100834 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100835 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100836 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100837 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100839 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 continue;
841
Bram Moolenaar85a20022019-12-21 18:25:54 +0100842 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 n = buf[0];
844 if (n == K_SPECIAL)
845 {
846 n = TO_SPECIAL(buf[1], buf[2]);
847 if (buf[1] == KS_MODIFIER
848 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100849 || (is_mouse_key(n) && n != K_LEFTMOUSE)
850#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 || n == K_VER_SCROLLBAR
852 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853#endif
854 )
855 {
856 if (buf[1] == KS_MODIFIER)
857 mod_mask = buf[2];
858 len -= 3;
859 if (len > 0)
860 mch_memmove(buf, buf + 3, (size_t)len);
861 continue;
862 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000863 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (has_mbyte)
866 {
867 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100868 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100869 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 n = (*mb_ptr2char)(buf);
871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#ifdef UNIX
873 if (n == intr_char)
874 n = ESC;
875#endif
876 break;
877 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100878 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
880 mapped_ctrl_c = save_mapped_ctrl_c;
881 return n;
882}
883
884/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000885 * Get a number from the user.
886 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 */
888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100889get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100890 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100891 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 int n = 0;
894 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000895 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000897 if (mouse_used != NULL)
898 *mouse_used = FALSE;
899
Bram Moolenaar85a20022019-12-21 18:25:54 +0100900 // When not printing messages, the user won't know what to type, return a
901 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (msg_silent != 0)
903 return 0;
904
905#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100906 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#endif
908 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100909 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 for (;;)
911 {
912 windgoto(msg_row, msg_col);
913 c = safe_vgetc();
914 if (VIM_ISDIGIT(c))
915 {
916 n = n * 10 + c - '0';
917 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000918 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
921 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000922 if (typed > 0)
923 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100924 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000925 --typed;
926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000929 else if (mouse_used != NULL && c == K_LEFTMOUSE)
930 {
931 *mouse_used = TRUE;
932 n = mouse_row + 1;
933 break;
934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 else if (n == 0 && c == ':' && colon)
936 {
937 stuffcharReadbuff(':');
938 if (!exmode_active)
939 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100940 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 do_redraw = FALSE;
942 break;
943 }
944 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
945 break;
946 }
947 --no_mapping;
948 --allow_keys;
949 return n;
950}
951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000952/*
953 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000954 * When "mouse_used" is not NULL allow using the mouse and in that case return
955 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000956 */
957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100958prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000959{
960 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000961 int save_cmdline_row;
962 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000963
Bram Moolenaar85a20022019-12-21 18:25:54 +0100964 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000965 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100966 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000967 else
Bram Moolenaar32526b32019-01-19 17:43:09 +0100968 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000969
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200970 // Set the state such that text can be selected/copied/pasted and we still
971 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
972 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000973 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +0000974 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000975 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200976 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200977 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200978 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +0200979
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000980 i = get_number(TRUE, mouse_used);
981 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000982 {
Bram Moolenaar954bb062019-06-09 16:40:46 +0200983 // don't call wait_return() now
984 if (msg_row > 0)
985 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000986 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +0100987 msg_didany = FALSE;
988 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000989 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000990 else
991 cmdline_row = save_cmdline_row;
992 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200993 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200994 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 return i;
997}
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
1002 long pn;
1003
Bram Moolenaar85a20022019-12-21 18:25:54 +01001004 if (global_busy // no messages now, wait until global is finished
1005 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 return;
1007
Bram Moolenaar85a20022019-12-21 18:25:54 +01001008 // We don't want to overwrite another important message, but do overwrite
1009 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1010 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001011 if (keep_msg != NULL && !keep_msg_more)
1012 return;
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 if (n > 0)
1015 pn = n;
1016 else
1017 pn = -n;
1018
1019 if (pn > p_report)
1020 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001021 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001022 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001023 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001025 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001026 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001028 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1029 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (msg(msg_buf))
1031 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001032 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001033 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035 }
1036}
1037
1038/*
1039 * flush map and typeahead buffers and give a warning for an error
1040 */
1041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001042beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044 if (emsg_silent == 0)
1045 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001046 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001047 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049}
1050
1051/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001052 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 */
1054 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001055vim_beep(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 unsigned val) // one of the BO_ values, e.g., BO_OPER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001058#ifdef FEAT_EVAL
1059 called_vim_beep = TRUE;
1060#endif
1061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (emsg_silent == 0)
1063 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001064 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1065 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001066#ifdef ELAPSED_FUNC
1067 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001068 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001069
Bram Moolenaar85a20022019-12-21 18:25:54 +01001070 // Only beep once per half a second, otherwise a sequence of beeps
1071 // would freeze Vim.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001072 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1073 {
1074 did_init = TRUE;
1075 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001077 if (p_vb
1078#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01001079 // While the GUI is starting up the termcap is set for
1080 // the GUI but the output still goes to a terminal.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001081 && !(gui.in_use && gui.starting)
1082#endif
1083 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001084 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001085 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001086#ifdef FEAT_VTP
Bram Moolenaar85a20022019-12-21 18:25:54 +01001087 // No restore color information, refresh the screen.
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001088 if (has_vtp_working() != 0
1089# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001090 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001091# endif
1092 )
1093 {
1094 redraw_later(CLEAR);
1095 update_screen(0);
1096 redrawcmd();
1097 }
1098#endif
1099 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001100 else
1101 out_char(BELL);
1102#ifdef ELAPSED_FUNC
1103 }
1104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106
Bram Moolenaar85a20022019-12-21 18:25:54 +01001107 // When 'debug' contains "beep" produce a message. If we are sourcing
1108 // a script or executing a function give the user a hint where the beep
1109 // comes from.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 if (vim_strchr(p_debug, 'e') != NULL)
1111 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001112 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001113 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 }
1116}
1117
1118/*
1119 * To get the "real" home directory:
1120 * - get value of $HOME
1121 * For Unix:
1122 * - go to that directory
1123 * - do mch_dirname() to get the real name of that directory.
1124 * This also works with mounts and links.
1125 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001126 * For Windows:
1127 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001130init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
1132 char_u *var;
1133
Bram Moolenaar85a20022019-12-21 18:25:54 +01001134 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001135 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef VMS
1138 var = mch_getenv((char_u *)"SYS$LOGIN");
1139#else
1140 var = mch_getenv((char_u *)"HOME");
1141#endif
1142
Bram Moolenaar4f974752019-02-17 17:44:42 +01001143#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001145 * Typically, $HOME is not defined on Windows, unless the user has
1146 * specifically defined it for Vim's sake. However, on Windows NT
1147 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1148 * each user. Try constructing $HOME from these.
1149 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001150 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001151 {
1152 char_u *homedrive, *homepath;
1153
1154 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1155 homepath = mch_getenv((char_u *)"HOMEPATH");
1156 if (homepath == NULL || *homepath == NUL)
1157 homepath = (char_u *)"\\";
1158 if (homedrive != NULL
1159 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1160 {
1161 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1162 if (NameBuff[0] != NUL)
1163 var = NameBuff;
1164 }
1165 }
1166
1167 if (var == NULL)
1168 var = mch_getenv((char_u *)"USERPROFILE");
1169
1170 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 * Weird but true: $HOME may contain an indirect reference to another
1172 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1173 * when $HOME is being set.
1174 */
1175 if (var != NULL && *var == '%')
1176 {
1177 char_u *p;
1178 char_u *exp;
1179
1180 p = vim_strchr(var + 1, '%');
1181 if (p != NULL)
1182 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001183 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 exp = mch_getenv(NameBuff);
1185 if (exp != NULL && *exp != NUL
1186 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1187 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001188 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 }
1192 }
1193
Bram Moolenaar85a20022019-12-21 18:25:54 +01001194 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001195 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
Bram Moolenaar05159a02005-02-26 23:04:13 +00001197 if (enc_utf8 && var != NULL)
1198 {
1199 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001200 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001201
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // Convert from active codepage to UTF-8. Other conversions are
1203 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001204 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001205 if (pp != NULL)
1206 {
1207 homedir = pp;
1208 return;
1209 }
1210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 /*
1213 * Default home dir is C:/
1214 * Best assumption we can make in such a situation.
1215 */
1216 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001217 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (var != NULL)
1221 {
1222#ifdef UNIX
1223 /*
1224 * Change to the directory and get the actual path. This resolves
1225 * links. Don't do it when we can't return.
1226 */
1227 if (mch_dirname(NameBuff, MAXPATHL) == OK
1228 && mch_chdir((char *)NameBuff) == 0)
1229 {
1230 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1231 var = IObuff;
1232 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001233 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 }
1235#endif
1236 homedir = vim_strsave(var);
1237 }
1238}
1239
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001240#if defined(EXITFREE) || defined(PROTO)
1241 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001242free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001243{
1244 vim_free(homedir);
1245}
Bram Moolenaar24305862012-08-15 14:05:05 +02001246
Bram Moolenaar24305862012-08-15 14:05:05 +02001247 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001248free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001249{
1250 ga_clear_strings(&ga_users);
1251}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001252#endif
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001255 * Call expand_env() and store the result in an allocated string.
1256 * This is not very memory efficient, this expects the result to be freed
1257 * again soon.
1258 */
1259 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001260expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001261{
1262 return expand_env_save_opt(src, FALSE);
1263}
1264
1265/*
1266 * Idem, but when "one" is TRUE handle the string as one file name, only
1267 * expand "~" at the start.
1268 */
1269 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001270expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001271{
1272 char_u *p;
1273
1274 p = alloc(MAXPATHL);
1275 if (p != NULL)
1276 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1277 return p;
1278}
1279
1280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 * Expand environment variable with path name.
1282 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001283 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 * If anything fails no expansion is done and dst equals src.
1285 */
1286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001287expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001288 char_u *src, // input string e.g. "$HOME/vim.hlp"
1289 char_u *dst, // where to put the result
1290 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001292 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293}
1294
1295 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001296expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001297 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1298 char_u *dst, // where to put the result
1299 int dstlen, // maximum length of the result
1300 int esc, // escape spaces in expanded variables
1301 int one, // "srcp" is one file name
1302 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001304 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *tail;
1306 int c;
1307 char_u *var;
1308 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001309 int mustfree; // var was allocated, need to free it later
1310 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001311 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001313 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001314 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001315
1316 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001317 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 while (*src && dstlen > 0)
1319 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001320#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001321 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001322 if (src[0] == '`' && src[1] == '=')
1323 {
1324 size_t len;
1325
1326 var = src;
1327 src += 2;
1328 (void)skip_expr(&src);
1329 if (*src == '`')
1330 ++src;
1331 len = src - var;
1332 if (len > (size_t)dstlen)
1333 len = dstlen;
1334 vim_strncpy(dst, var, len);
1335 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001336 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001337 continue;
1338 }
1339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001341 if ((*src == '$'
1342#ifdef VMS
1343 && at_start
1344#endif
1345 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001346#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 || *src == '%'
1348#endif
1349 || (*src == '~' && at_start))
1350 {
1351 mustfree = FALSE;
1352
1353 /*
1354 * The variable name is copied into dst temporarily, because it may
1355 * be a string in read-only memory and a NUL needs to be appended.
1356 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001357 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 tail = src + 1;
1360 var = dst;
1361 c = dstlen - 1;
1362
1363#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001364 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (*tail == '{' && !vim_isIDc('{'))
1366 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001367 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 while (c-- > 0 && *tail && *tail != '}')
1369 *var++ = *tail++;
1370 }
1371 else
1372#endif
1373 {
1374 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001375#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 || (*src == '%' && *tail != '%')
1377#endif
1378 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001382#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383# ifdef UNIX
1384 if (src[1] == '{' && *tail != '}')
1385# else
1386 if (*src == '%' && *tail != '%')
1387# endif
1388 var = NULL;
1389 else
1390 {
1391# ifdef UNIX
1392 if (src[1] == '{')
1393# else
1394 if (*src == '%')
1395#endif
1396 ++tail;
1397#endif
1398 *var = NUL;
1399 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001400#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402#endif
1403 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001404 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 else if ( src[1] == NUL
1406 || vim_ispathsep(src[1])
1407 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1408 {
1409 var = homedir;
1410 tail = src + 1;
1411 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001412 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
1414#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1415 /*
1416 * Copy ~user to dst[], so we can put a NUL after it.
1417 */
1418 tail = src;
1419 var = dst;
1420 c = dstlen - 1;
1421 while ( c-- > 0
1422 && *tail
1423 && vim_isfilec(*tail)
1424 && !vim_ispathsep(*tail))
1425 *var++ = *tail++;
1426 *var = NUL;
1427# ifdef UNIX
1428 /*
1429 * If the system supports getpwnam(), use it.
1430 * Otherwise, or if getpwnam() fails, the shell is used to
1431 * expand ~user. This is slower and may fail if the shell
1432 * does not support ~user (old versions of /bin/sh).
1433 */
1434# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001436 // Note: memory allocated by getpwnam() is never freed.
1437 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001438 struct passwd *pw = (*dst == NUL)
1439 ? NULL : getpwnam((char *)dst + 1);
1440
1441 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443 if (var == NULL)
1444# endif
1445 {
1446 expand_T xpc;
1447
1448 ExpandInit(&xpc);
1449 xpc.xp_context = EXPAND_FILES;
1450 var = ExpandOne(&xpc, dst, NULL,
1451 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 mustfree = TRUE;
1453 }
1454
Bram Moolenaar85a20022019-12-21 18:25:54 +01001455# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 /*
1457 * USER_HOME is a comma-separated list of
1458 * directories to search for the user account in.
1459 */
1460 {
1461 char_u test[MAXPATHL], paths[MAXPATHL];
1462 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001463 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
1465 STRCPY(paths, USER_HOME);
1466 next_path = paths;
1467 while (*next_path)
1468 {
1469 for (path = next_path; *next_path && *next_path != ',';
1470 next_path++);
1471 if (*next_path)
1472 *next_path++ = NUL;
1473 STRCPY(test, path);
1474 STRCAT(test, "/");
1475 STRCAT(test, dst + 1);
1476 if (mch_stat(test, &st) == 0)
1477 {
1478 var = alloc(STRLEN(test) + 1);
1479 STRCPY(var, test);
1480 mustfree = TRUE;
1481 break;
1482 }
1483 }
1484 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001485# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001487 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001489 tail = (char_u *)""; // for gcc
1490#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492
1493#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001494 // If 'shellslash' is set change backslashes to forward slashes.
1495 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1497 {
1498 char_u *p = vim_strsave(var);
1499
1500 if (p != NULL)
1501 {
1502 if (mustfree)
1503 vim_free(var);
1504 var = p;
1505 mustfree = TRUE;
1506 forward_slash(var);
1507 }
1508 }
1509#endif
1510
Bram Moolenaar85a20022019-12-21 18:25:54 +01001511 // If "var" contains white space, escape it with a backslash.
1512 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1514 {
1515 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1516
1517 if (p != NULL)
1518 {
1519 if (mustfree)
1520 vim_free(var);
1521 var = p;
1522 mustfree = TRUE;
1523 }
1524 }
1525
1526 if (var != NULL && *var != NUL
1527 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1528 {
1529 STRCPY(dst, var);
1530 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001531 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001532 // if var[] ends in a path separator and tail[] starts
1533 // with it, skip a character
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001534 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1536 && dst[-1] != ':'
1537#endif
1538 && vim_ispathsep(*tail))
1539 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001540 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 src = tail;
1542 copy_char = FALSE;
1543 }
1544 if (mustfree)
1545 vim_free(var);
1546 }
1547
Bram Moolenaar85a20022019-12-21 18:25:54 +01001548 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
1550 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001551 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001552 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1553 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
1555 at_start = FALSE;
1556 if (src[0] == '\\' && src[1] != NUL)
1557 {
1558 *dst++ = *src++;
1559 --dstlen;
1560 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001561 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001563 if (dstlen > 0)
1564 {
1565 *dst++ = *src++;
1566 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567
Bram Moolenaar1c864092017-08-06 18:15:45 +02001568 if (startstr != NULL && src - startstr_len >= srcp
1569 && STRNCMP(src - startstr_len, startstr,
1570 startstr_len) == 0)
1571 at_start = TRUE;
1572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576 *dst = NUL;
1577}
1578
1579/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001580 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1581 * the length of "name/". Otherwise return "pend".
1582 */
1583 static char_u *
1584remove_tail(char_u *p, char_u *pend, char_u *name)
1585{
1586 int len = (int)STRLEN(name) + 1;
1587 char_u *newend = pend - len;
1588
1589 if (newend >= p
1590 && fnamencmp(newend, name, len - 1) == 0
1591 && (newend == p || after_pathsep(p, newend)))
1592 return newend;
1593 return pend;
1594}
1595
1596/*
1597 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1598 * Return NULL if not, return its name in allocated memory otherwise.
1599 */
1600 static char_u *
1601vim_version_dir(char_u *vimdir)
1602{
1603 char_u *p;
1604
1605 if (vimdir == NULL || *vimdir == NUL)
1606 return NULL;
1607 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1608 if (p != NULL && mch_isdir(p))
1609 return p;
1610 vim_free(p);
1611 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1612 if (p != NULL && mch_isdir(p))
1613 return p;
1614 vim_free(p);
1615 return NULL;
1616}
1617
1618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 * Vim's version of getenv().
1620 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001621 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001622 * "mustfree" is set to TRUE when returned is allocated, it must be
1623 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
1625 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001626vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001628 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 char_u *pend;
1630 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001631#ifdef MSWIN
1632 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001634 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (STRCMP(name, "HOME") == 0)
1636 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001638 // Use Wide function
1639 wn = enc_to_utf16(name, NULL);
1640 if (wn == NULL)
1641 return NULL;
1642
1643 wp = _wgetenv(wn);
1644 vim_free(wn);
1645
1646 if (wp != NULL && *wp == NUL) // empty is the same as not set
1647 wp = NULL;
1648
1649 if (wp != NULL)
1650 {
1651 p = utf16_to_enc(wp, NULL);
1652 if (p == NULL)
1653 return NULL;
1654
1655 *mustfree = TRUE;
1656 return p;
1657 }
1658#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001660 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 p = NULL;
1662
1663 if (p != NULL)
1664 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001667 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1669 if (!vimruntime && STRCMP(name, "VIM") != 0)
1670 return NULL;
1671
1672 /*
1673 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1674 * Don't do this when default_vimruntime_dir is non-empty.
1675 */
1676 if (vimruntime
1677#ifdef HAVE_PATHDEF
1678 && *default_vimruntime_dir == NUL
1679#endif
1680 )
1681 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001682#ifdef MSWIN
1683 // Use Wide function
1684 wp = _wgetenv(L"VIM");
1685 if (wp != NULL && *wp == NUL) // empty is the same as not set
1686 wp = NULL;
1687 if (wp != NULL)
1688 {
1689 char_u *q = utf16_to_enc(wp, NULL);
1690 if (q != NULL)
1691 {
1692 p = vim_version_dir(q);
1693 *mustfree = TRUE;
1694 if (p == NULL)
1695 p = q;
1696 }
1697 }
1698#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001700 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 p = NULL;
1702 if (p != NULL)
1703 {
1704 p = vim_version_dir(p);
1705 if (p != NULL)
1706 *mustfree = TRUE;
1707 else
1708 p = mch_getenv((char_u *)"VIM");
1709 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 }
1712
1713 /*
1714 * When expanding $VIM or $VIMRUNTIME fails, try using:
1715 * - the directory name from 'helpfile' (unless it contains '$')
1716 * - the executable name from argv[0]
1717 */
1718 if (p == NULL)
1719 {
1720 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1721 p = p_hf;
1722#ifdef USE_EXE_NAME
1723 /*
1724 * Use the name of the executable, obtained from argv[0].
1725 */
1726 else
1727 p = exe_name;
1728#endif
1729 if (p != NULL)
1730 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001731 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 pend = gettail(p);
1733
Bram Moolenaar85a20022019-12-21 18:25:54 +01001734 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (p == p_hf)
1736 pend = remove_tail(p, pend, (char_u *)"doc");
1737
1738#ifdef USE_EXE_NAME
1739# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001740 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (p == exe_name)
1742 {
1743 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001744 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001746 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1747 if (pend1 != pend)
1748 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001749 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001750 if (pnew != NULL)
1751 {
1752 STRNCPY(pnew, p, (pend1 - p));
1753 STRCPY(pnew + (pend1 - p), "Resources/vim");
1754 p = pnew;
1755 pend = p + STRLEN(p);
1756 }
1757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001760 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if (p == exe_name)
1762 pend = remove_tail(p, pend, (char_u *)"src");
1763#endif
1764
Bram Moolenaar85a20022019-12-21 18:25:54 +01001765 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (!vimruntime)
1767 {
1768 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1769 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1770 }
1771
Bram Moolenaar85a20022019-12-21 18:25:54 +01001772 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001773 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001776#ifdef MACOS_X
1777 if (p == exe_name || p == p_hf)
1778#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 // check that the result is a directory name
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001780 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001783 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 else
1785 {
1786#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001787 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1789 {
1790 vim_free(p);
1791 p = pend;
1792 }
1793#endif
1794 *mustfree = TRUE;
1795 }
1796 }
1797 }
1798
1799#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001800 // When there is a pathdef.c file we can use default_vim_dir and
1801 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (p == NULL)
1803 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001804 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (vimruntime && *default_vimruntime_dir != NUL)
1806 {
1807 p = default_vimruntime_dir;
1808 *mustfree = FALSE;
1809 }
1810 else if (*default_vim_dir != NUL)
1811 {
1812 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1813 *mustfree = TRUE;
1814 else
1815 {
1816 p = default_vim_dir;
1817 *mustfree = FALSE;
1818 }
1819 }
1820 }
1821#endif
1822
1823 /*
1824 * Set the environment variable, so that the new value can be found fast
1825 * next time, and others can also use it (e.g. Perl).
1826 */
1827 if (p != NULL)
1828 {
1829 if (vimruntime)
1830 {
1831 vim_setenv((char_u *)"VIMRUNTIME", p);
1832 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 else
1835 {
1836 vim_setenv((char_u *)"VIM", p);
1837 didset_vim = TRUE;
1838 }
1839 }
1840 return p;
1841}
1842
Bram Moolenaar113e1072019-01-20 15:30:40 +01001843#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02001844 void
1845vim_unsetenv(char_u *var)
1846{
1847#ifdef HAVE_UNSETENV
1848 unsetenv((char *)var);
1849#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001850 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001851#endif
1852}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001853#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02001854
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 * Our portable version of setenv.
1858 */
1859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001860vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862#ifdef HAVE_SETENV
1863 mch_setenv((char *)name, (char *)val, 1);
1864#else
1865 char_u *envbuf;
1866
1867 /*
1868 * Putenv does not copy the string, it has to remain
1869 * valid. The allocated memory will never be freed.
1870 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001871 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (envbuf != NULL)
1873 {
1874 sprintf((char *)envbuf, "%s=%s", name, val);
1875 putenv((char *)envbuf);
1876 }
1877#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001878#ifdef FEAT_GETTEXT
1879 /*
1880 * When setting $VIMRUNTIME adjust the directory to find message
1881 * translations to $VIMRUNTIME/lang.
1882 */
1883 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1884 {
1885 char_u *buf = concat_str(val, (char_u *)"/lang");
1886
1887 if (buf != NULL)
1888 {
1889 bindtextdomain(VIMPACKAGE, (char *)buf);
1890 vim_free(buf);
1891 }
1892 }
1893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894}
1895
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896/*
1897 * Function given to ExpandGeneric() to obtain an environment variable name.
1898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001900get_env_name(
1901 expand_T *xp UNUSED,
1902 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
Bram Moolenaard0573012017-10-28 21:11:06 +02001904# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02001906 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 */
1908 return NULL;
1909# else
1910# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01001911 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 extern char **environ;
1913# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001914# define ENVNAMELEN 100
1915 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 char_u *str;
1917 int n;
1918
1919 str = (char_u *)environ[idx];
1920 if (str == NULL)
1921 return NULL;
1922
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001923 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 if (str[n] == '=' || str[n] == NUL)
1926 break;
1927 name[n] = str[n];
1928 }
1929 name[n] = NUL;
1930 return name;
1931# endif
1932}
Bram Moolenaar24305862012-08-15 14:05:05 +02001933
1934/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001935 * Add a user name to the list of users in ga_users.
1936 * Do nothing if user name is NULL or empty.
1937 */
1938 static void
1939add_user(char_u *user, int need_copy)
1940{
1941 char_u *user_copy = (user != NULL && need_copy)
1942 ? vim_strsave(user) : user;
1943
1944 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
1945 {
1946 if (need_copy)
1947 vim_free(user);
1948 return;
1949 }
1950 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
1951}
1952
1953/*
Bram Moolenaar24305862012-08-15 14:05:05 +02001954 * Find all user names for user completion.
1955 * Done only once and then cached.
1956 */
1957 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001958init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02001959{
Bram Moolenaar24305862012-08-15 14:05:05 +02001960 static int lazy_init_done = FALSE;
1961
1962 if (lazy_init_done)
1963 return;
1964
1965 lazy_init_done = TRUE;
1966 ga_init2(&ga_users, sizeof(char_u *), 20);
1967
1968# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
1969 {
Bram Moolenaar24305862012-08-15 14:05:05 +02001970 struct passwd* pw;
1971
1972 setpwent();
1973 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001974 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02001975 endpwent();
1976 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01001977# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001978 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001979 DWORD nusers = 0, ntotal = 0, i;
1980 PUSER_INFO_0 uinfo;
1981
1982 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
1983 &nusers, &ntotal, NULL) == NERR_Success)
1984 {
1985 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001986 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001987
1988 NetApiBufferFree(uinfo);
1989 }
1990 }
Bram Moolenaar24305862012-08-15 14:05:05 +02001991# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001992# if defined(HAVE_GETPWNAM)
1993 {
1994 char_u *user_env = mch_getenv((char_u *)"USER");
1995
1996 // The $USER environment variable may be a valid remote user name (NIS,
1997 // LDAP) not already listed by getpwent(), as getpwent() only lists
1998 // local user names. If $USER is not already listed, check whether it
1999 // is a valid remote user name using getpwnam() and if it is, add it to
2000 // the list of user names.
2001
2002 if (user_env != NULL && *user_env != NUL)
2003 {
2004 int i;
2005
2006 for (i = 0; i < ga_users.ga_len; i++)
2007 {
2008 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2009
2010 if (STRCMP(local_user, user_env) == 0)
2011 break;
2012 }
2013
2014 if (i == ga_users.ga_len)
2015 {
2016 struct passwd *pw = getpwnam((char *)user_env);
2017
2018 if (pw != NULL)
2019 add_user((char_u *)pw->pw_name, TRUE);
2020 }
2021 }
2022 }
2023# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002024}
2025
2026/*
2027 * Function given to ExpandGeneric() to obtain an user names.
2028 */
2029 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002031{
2032 init_users();
2033 if (idx < ga_users.ga_len)
2034 return ((char_u **)ga_users.ga_data)[idx];
2035 return NULL;
2036}
2037
2038/*
2039 * Check whether name matches a user name. Return:
2040 * 0 if name does not match any user name.
2041 * 1 if name partially matches the beginning of a user name.
2042 * 2 is name fully matches a user name.
2043 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002044 int
2045match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002046{
2047 int i;
2048 int n = (int)STRLEN(name);
2049 int result = 0;
2050
2051 init_users();
2052 for (i = 0; i < ga_users.ga_len; i++)
2053 {
2054 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002055 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002056 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002057 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002058 }
2059 return result;
2060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002063 * Concatenate two strings and return the result in allocated memory.
2064 * Returns NULL when out of memory.
2065 */
2066 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002067concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002068{
2069 char_u *dest;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 size_t l = str1 == NULL ? 0 : STRLEN(str1);
Bram Moolenaard6754642005-01-17 22:18:45 +00002071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 dest = alloc(l + (str2 == NULL ? 0 : STRLEN(str2)) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002073 if (dest != NULL)
2074 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 if (str1 == NULL)
2076 *dest = NUL;
2077 else
2078 STRCPY(dest, str1);
2079 if (str2 != NULL)
2080 STRCPY(dest + l, str2);
Bram Moolenaard6754642005-01-17 22:18:45 +00002081 }
2082 return dest;
2083}
Bram Moolenaard6754642005-01-17 22:18:45 +00002084
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002085 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002086prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002088#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002089 // Ignore SIGHUP, because a dropped connection causes a read error, which
2090 // makes Vim exit and then handling SIGHUP causes various reentrance
2091 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002092 signal(SIGHUP, SIG_IGN);
2093#endif
2094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#ifdef FEAT_GUI
2096 if (gui.in_use)
2097 {
2098 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002099 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101 else
2102#endif
2103 {
2104 windgoto((int)Rows - 1, 0);
2105
2106 /*
2107 * Switch terminal mode back now, so messages end up on the "normal"
2108 * screen (if there are two screens).
2109 */
2110 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002111 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 out_flush();
2113 }
2114}
2115
2116/*
2117 * Preserve files and exit.
2118 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002119 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2120 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 */
2122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 buf_T *buf;
2126
2127 prepare_to_exit();
2128
Bram Moolenaar85a20022019-12-21 18:25:54 +01002129 // Setting this will prevent free() calls. That avoids calling free()
2130 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002131 really_exiting = TRUE;
2132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002134 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 out_flush();
2136
Bram Moolenaar85a20022019-12-21 18:25:54 +01002137 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar29323592016-07-24 22:04:11 +02002139 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2142 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002143 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002146 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 break;
2148 }
2149 }
2150
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002153 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 getout(1);
2156}
2157
2158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 * Check for CTRL-C pressed, but only once in a while.
2160 * Should be used instead of ui_breakcheck() for functions that check for
2161 * each line in the file. Calling ui_breakcheck() each time takes too much
2162 * time, because it can be a system call.
2163 */
2164
2165#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002166# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#endif
2168
2169static int breakcheck_count = 0;
2170
2171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002172line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 if (++breakcheck_count >= BREAKCHECK_SKIP)
2175 {
2176 breakcheck_count = 0;
2177 ui_breakcheck();
2178 }
2179}
2180
2181/*
2182 * Like line_breakcheck() but check 10 times less often.
2183 */
2184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002185fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2188 {
2189 breakcheck_count = 0;
2190 ui_breakcheck();
2191 }
2192}
2193
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002194#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2195 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2196 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198#ifndef SEEK_SET
2199# define SEEK_SET 0
2200#endif
2201#ifndef SEEK_END
2202# define SEEK_END 2
2203#endif
2204
2205/*
2206 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002207 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2208 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 * Returns an allocated string, or NULL for error.
2210 */
2211 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002212get_cmd_output(
2213 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002214 char_u *infile, // optional input file name
2215 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002216 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
2218 char_u *tempname;
2219 char_u *command;
2220 char_u *buffer = NULL;
2221 int len;
2222 int i = 0;
2223 FILE *fd;
2224
2225 if (check_restricted() || check_secure())
2226 return NULL;
2227
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002229 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002231 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return NULL;
2233 }
2234
Bram Moolenaar85a20022019-12-21 18:25:54 +01002235 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002236 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (command == NULL)
2238 goto done;
2239
2240 /*
2241 * Call the shell to execute the command (errors are ignored).
2242 * Don't check timestamps here.
2243 */
2244 ++no_check_timestamps;
2245 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2246 --no_check_timestamps;
2247
2248 vim_free(command);
2249
2250 /*
2251 * read the names from the file into memory
2252 */
2253# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 fd = mch_fopen((char *)tempname, "r");
2256# else
2257 fd = mch_fopen((char *)tempname, READBIN);
2258# endif
2259
2260 if (fd == NULL)
2261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 goto done;
2264 }
2265
2266 fseek(fd, 0L, SEEK_END);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002267 len = ftell(fd); // get size of temp file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 fseek(fd, 0L, SEEK_SET);
2269
2270 buffer = alloc(len + 1);
2271 if (buffer != NULL)
2272 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2273 fclose(fd);
2274 mch_remove(tempname);
2275 if (buffer == NULL)
2276 goto done;
2277#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002278 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279#endif
2280 if (i != len)
2281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002282 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002283 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002285 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002286 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002287 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002288 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002289 if (buffer[i] == NUL)
2290 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002291
Bram Moolenaar85a20022019-12-21 18:25:54 +01002292 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002293 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002294 else
2295 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297done:
2298 vim_free(tempname);
2299 return buffer;
2300}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002301
2302# if defined(FEAT_EVAL) || defined(PROTO)
2303
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002304 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002305get_cmd_output_as_rettv(
2306 typval_T *argvars,
2307 typval_T *rettv,
2308 int retlist)
2309{
2310 char_u *res = NULL;
2311 char_u *p;
2312 char_u *infile = NULL;
2313 int err = FALSE;
2314 FILE *fd;
2315 list_T *list = NULL;
2316 int flags = SHELL_SILENT;
2317
2318 rettv->v_type = VAR_STRING;
2319 rettv->vval.v_string = NULL;
2320 if (check_restricted() || check_secure())
2321 goto errret;
2322
2323 if (argvars[1].v_type != VAR_UNKNOWN)
2324 {
2325 /*
2326 * Write the text to a temp file, to be used for input of the shell
2327 * command.
2328 */
2329 if ((infile = vim_tempname('i', TRUE)) == NULL)
2330 {
2331 emsg(_(e_notmp));
2332 goto errret;
2333 }
2334
2335 fd = mch_fopen((char *)infile, WRITEBIN);
2336 if (fd == NULL)
2337 {
2338 semsg(_(e_notopen), infile);
2339 goto errret;
2340 }
2341 if (argvars[1].v_type == VAR_NUMBER)
2342 {
2343 linenr_T lnum;
2344 buf_T *buf;
2345
2346 buf = buflist_findnr(argvars[1].vval.v_number);
2347 if (buf == NULL)
2348 {
2349 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2350 fclose(fd);
2351 goto errret;
2352 }
2353
2354 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2355 {
2356 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2357 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2358 {
2359 err = TRUE;
2360 break;
2361 }
2362 if (putc(NL, fd) == EOF)
2363 {
2364 err = TRUE;
2365 break;
2366 }
2367 }
2368 }
2369 else if (argvars[1].v_type == VAR_LIST)
2370 {
2371 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2372 err = TRUE;
2373 }
2374 else
2375 {
2376 size_t len;
2377 char_u buf[NUMBUFLEN];
2378
2379 p = tv_get_string_buf_chk(&argvars[1], buf);
2380 if (p == NULL)
2381 {
2382 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002383 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002384 }
2385 len = STRLEN(p);
2386 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2387 err = TRUE;
2388 }
2389 if (fclose(fd) != 0)
2390 err = TRUE;
2391 if (err)
2392 {
2393 emsg(_("E677: Error writing temp file"));
2394 goto errret;
2395 }
2396 }
2397
Bram Moolenaar85a20022019-12-21 18:25:54 +01002398 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2399 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002400 if (!msg_silent)
2401 flags += SHELL_COOKED;
2402
2403 if (retlist)
2404 {
2405 int len;
2406 listitem_T *li;
2407 char_u *s = NULL;
2408 char_u *start;
2409 char_u *end;
2410 int i;
2411
2412 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2413 if (res == NULL)
2414 goto errret;
2415
2416 list = list_alloc();
2417 if (list == NULL)
2418 goto errret;
2419
2420 for (i = 0; i < len; ++i)
2421 {
2422 start = res + i;
2423 while (i < len && res[i] != NL)
2424 ++i;
2425 end = res + i;
2426
2427 s = alloc(end - start + 1);
2428 if (s == NULL)
2429 goto errret;
2430
2431 for (p = s; start < end; ++p, ++start)
2432 *p = *start == NUL ? NL : *start;
2433 *p = NUL;
2434
2435 li = listitem_alloc();
2436 if (li == NULL)
2437 {
2438 vim_free(s);
2439 goto errret;
2440 }
2441 li->li_tv.v_type = VAR_STRING;
2442 li->li_tv.v_lock = 0;
2443 li->li_tv.vval.v_string = s;
2444 list_append(list, li);
2445 }
2446
2447 rettv_list_set(rettv, list);
2448 list = NULL;
2449 }
2450 else
2451 {
2452 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2453#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002454 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002455 if (res != NULL)
2456 {
2457 char_u *s, *d;
2458
2459 d = res;
2460 for (s = res; *s; ++s)
2461 {
2462 if (s[0] == CAR && s[1] == NL)
2463 ++s;
2464 *d++ = *s;
2465 }
2466 *d = NUL;
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002469 rettv->vval.v_string = res;
2470 res = NULL;
2471 }
2472
2473errret:
2474 if (infile != NULL)
2475 {
2476 mch_remove(infile);
2477 vim_free(infile);
2478 }
2479 if (res != NULL)
2480 vim_free(res);
2481 if (list != NULL)
2482 list_free(list);
2483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002486 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 */
2488 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002489f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002491 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492}
2493
2494/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002495 * "systemlist()" function
2496 */
2497 void
2498f_systemlist(typval_T *argvars, typval_T *rettv)
2499{
2500 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2501}
2502# endif // FEAT_EVAL
2503
2504#endif
2505
2506/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002507 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * Don't do this when still processing a command or a mapping.
2509 * Don't do this when inside a ":normal" command.
2510 */
2511 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002512goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514 return (p_im && stuff_empty() && typebuf_typed());
2515}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002516
2517/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002518 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002519 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2520 * - Remove any argument. E.g., "csh -f" -> "csh".
2521 * But don't allow a space in the path, so that this works:
2522 * "/usr/bin/csh --rcfile ~/.cshrc"
2523 * But don't do that for Windows, it's common to have a space in the path.
2524 */
2525 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002526get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002527{
2528 char_u *p;
2529
Bram Moolenaar4f974752019-02-17 17:44:42 +01002530#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002531 p = gettail(p_sh);
2532 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
2533#else
2534 p = skiptowhite(p_sh);
2535 if (*p == NUL)
2536 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002537 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002538 p = vim_strsave(gettail(p_sh));
2539 }
2540 else
2541 {
2542 char_u *p1, *p2;
2543
Bram Moolenaar85a20022019-12-21 18:25:54 +01002544 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002545 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002546 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002547 if (vim_ispathsep(*p2))
2548 p1 = p2 + 1;
2549 p = vim_strnsave(p1, (int)(p - p1));
2550 }
2551#endif
2552 return p;
2553}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002554
2555/*
2556 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2557 * Also check for ":\\", which MS Internet Explorer accepts, return
2558 * URL_BACKSLASH.
2559 */
2560 int
2561path_is_url(char_u *p)
2562{
2563 if (STRNCMP(p, "://", (size_t)3) == 0)
2564 return URL_SLASH;
2565 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2566 return URL_BACKSLASH;
2567 return 0;
2568}
2569
2570/*
2571 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
2572 * Return URL_BACKSLASH for "name:\\".
2573 * Return zero otherwise.
2574 */
2575 int
2576path_with_url(char_u *fname)
2577{
2578 char_u *p;
2579
2580 for (p = fname; isalpha(*p); ++p)
2581 ;
2582 return path_is_url(p);
2583}
Bram Moolenaar52410572019-10-27 05:12:45 +01002584
2585/*
2586 * Put timestamp "tt" in "buf[buflen]" in a nice format.
2587 */
2588 void
2589add_time(char_u *buf, size_t buflen, time_t tt)
2590{
2591#ifdef HAVE_STRFTIME
2592 struct tm tmval;
2593 struct tm *curtime;
2594
2595 if (vim_time() - tt >= 100)
2596 {
2597 curtime = vim_localtime(&tt, &tmval);
2598 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002599 // within 12 hours
Bram Moolenaar52410572019-10-27 05:12:45 +01002600 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
2601 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002602 // longer ago
Bram Moolenaar52410572019-10-27 05:12:45 +01002603 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
2604 }
2605 else
2606#endif
2607 {
2608 long seconds = (long)(vim_time() - tt);
2609
2610 vim_snprintf((char *)buf, buflen,
2611 NGETTEXT("%ld second ago", "%ld seconds ago", seconds),
2612 seconds);
2613 }
2614}