blob: d359ffd97811fd91f0d42182be2e48da9e3db59c [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 Moolenaar5fd0f502019-02-13 23:13:28 +010021#define URL_SLASH 1 /* path_is_url() has found "://" */
22#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
23
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;
48 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
49 char_u *string; /* pointer to comment string */
50 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 Moolenaar1c465442017-03-12 20:10:05 +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 Moolenaara4271d52011-05-10 13:38:27 +020070 /* Get one option part into part_buf[]. Advance "list" to next
71 * one. Put "string" at start of string. */
72 if (!got_com && flags != NULL)
73 *flags = list; /* remember where flags started */
74 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, ':');
77 if (string == NULL) /* missing ':', ignore this part */
78 continue;
79 *string++ = NUL; /* isolate flags from string */
80
Bram Moolenaara4271d52011-05-10 13:38:27 +020081 /* If we found a middle match previously, use that match when this
82 * is not a middle or end. */
83 if (middle_match_len != 0
84 && vim_strchr(part_buf, COM_MIDDLE) == NULL
85 && vim_strchr(part_buf, COM_END) == NULL)
86 break;
87
88 /* 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 Moolenaara4271d52011-05-10 13:38:27 +020093 /* 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 Moolenaara4271d52011-05-10 13:38:27 +020097 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 * 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
Bram Moolenaara4271d52011-05-10 13:38:27 +0200100 * 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 Moolenaar84a05ac2013-05-06 04:24:17 +0200104 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 Moolenaara4271d52011-05-10 13:38:27 +0200111 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaara4271d52011-05-10 13:38:27 +0200113 /* 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 Moolenaara4271d52011-05-10 13:38:27 +0200119 /* 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. */
124 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)
134 /* Use this match instead of the middle match, since it's a
135 * longer thus better match. */
136 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 {
146 /* Use the previously found middle match after failing to find a
147 * match with an end. */
148 if (!got_com && flags != NULL)
149 *flags = saved_flags;
150 i += middle_match_len;
151 found_one = TRUE;
152 }
153
154 /* 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 Moolenaara4271d52011-05-10 13:38:27 +0200160 /* 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 Moolenaara4271d52011-05-10 13:38:27 +0200167 /* If this comment doesn't nest, stop here. */
168 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;
193 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
194
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, ':');
215 if (string == NULL) /* If everything is fine, this cannot actually
216 * happen. */
Bram Moolenaar81340392012-06-06 16:12:59 +0200217 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200218 *string++ = NUL; /* Isolate flags from string. */
219 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 {
274 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
275 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
286 /* 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.
290 */
291
Bram Moolenaar1c465442017-03-12 20:10:05 +0100292 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200293 ++com_leader;
294 len1 = (int)STRLEN(com_leader);
295
296 for (list = curbuf->b_p_com; *list; )
297 {
298 char_u *flags_save = list;
299
300 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
301 if (flags_save == com_flags)
302 continue;
303 string = vim_strchr(part_buf2, ':');
304 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100305 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200306 ++string;
307 len2 = (int)STRLEN(string);
308 if (len2 == 0)
309 continue;
310
311 /* Now we have to verify whether string ends with a substring
312 * beginning the com_leader. */
313 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
314 {
315 --off;
316 if (!STRNCMP(string + off, com_leader, len2 - off))
317 {
318 if (i - off < lower_check_bound)
319 lower_check_bound = i - off;
320 }
321 }
322 }
323 }
324 }
325 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
328/*
329 * Return the number of window lines occupied by buffer line "lnum".
330 */
331 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100332plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
334 return plines_win(curwin, lnum, TRUE);
335}
336
337 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338plines_win(
339 win_T *wp,
340 linenr_T lnum,
341 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342{
343#if defined(FEAT_DIFF) || defined(PROTO)
344 /* Check for filler lines above this buffer line. When folded the result
345 * is one line anyway. */
346 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
347}
348
349 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100350plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351{
352 return plines_win_nofill(curwin, lnum, TRUE);
353}
354
355 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100356plines_win_nofill(
357 win_T *wp,
358 linenr_T lnum,
359 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
361#endif
362 int lines;
363
364 if (!wp->w_p_wrap)
365 return 1;
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 if (wp->w_width == 0)
368 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
370#ifdef FEAT_FOLDING
371 /* A folded lines is handled just like an empty line. */
372 /* NOTE: Caller must handle lines that are MAYBE folded. */
373 if (lineFolded(wp, lnum) == TRUE)
374 return 1;
375#endif
376
377 lines = plines_win_nofold(wp, lnum);
378 if (winheight > 0 && lines > wp->w_height)
379 return (int)wp->w_height;
380 return lines;
381}
382
383/*
384 * Return number of window lines physical line "lnum" will occupy in window
385 * "wp". Does not care about folding, 'wrap' or 'diff'.
386 */
387 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100388plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 char_u *s;
391 long col;
392 int width;
393
394 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
395 if (*s == NUL) /* empty line */
396 return 1;
397 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
398
399 /*
400 * If list mode is on, then the '$' at the end of the line may take up one
401 * extra column.
402 */
403 if (wp->w_p_list && lcs_eol != NUL)
404 col += 1;
405
406 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200407 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200409 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 if (width <= 0)
411 return 32000;
412 if (col <= width)
413 return 1;
414 col -= width;
415 width += win_col_off2(wp);
416 return (col + (width - 1)) / width + 1;
417}
418
419/*
420 * Like plines_win(), but only reports the number of physical screen lines
421 * used from the start of the line to the given column number.
422 */
423 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100424plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425{
426 long col;
427 char_u *s;
428 int lines = 0;
429 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200430 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431
432#ifdef FEAT_DIFF
433 /* Check for filler lines above this buffer line. When folded the result
434 * is one line anyway. */
435 lines = diff_check_fill(wp, lnum);
436#endif
437
438 if (!wp->w_p_wrap)
439 return lines + 1;
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 if (wp->w_width == 0)
442 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar597a4222014-06-25 14:39:50 +0200444 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446 col = 0;
447 while (*s != NUL && --column >= 0)
448 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200449 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100450 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 }
452
453 /*
454 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
455 * INSERT mode, then col must be adjusted so that it represents the last
456 * screen position of the TAB. This only fixes an error when the TAB wraps
457 * from one screen line to the next (when 'columns' is not a multiple of
458 * 'ts') -- webb.
459 */
460 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200461 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200464 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200466 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +0000467 if (width <= 0)
468 return 9999;
469
470 lines += 1;
471 if (col > width)
472 lines += (col - width) / (width + win_col_off2(wp)) + 1;
473 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474}
475
476 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100477plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 int count = 0;
480
481 while (first <= last)
482 {
483#ifdef FEAT_FOLDING
484 int x;
485
486 /* Check if there are any really folded lines, but also included lines
487 * that are maybe folded. */
488 x = foldedCount(wp, first, NULL);
489 if (x > 0)
490 {
491 ++count; /* count 1 for "+-- folded" line */
492 first += x;
493 }
494 else
495#endif
496 {
497#ifdef FEAT_DIFF
498 if (first == wp->w_topline)
499 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
500 else
501#endif
502 count += plines_win(wp, first, TRUE);
503 ++first;
504 }
505 }
506 return (count);
507}
508
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100510gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100512 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100514 /* When searching columns is sometimes put at the end of a line. */
515 if (pos->col == MAXCOL)
516 return NUL;
517 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (has_mbyte)
519 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 return (int)*ptr;
521}
522
523 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100524gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (has_mbyte)
527 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 return (int)*ml_get_cursor();
529}
530
531/*
532 * Write a character at the current cursor position.
533 * It is directly written into the block.
534 */
535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
539 + curwin->w_cursor.col) = c;
540}
541
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Skip to next part of an option argument: Skip space and comma.
544 */
545 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100546skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 if (*p == ',')
549 ++p;
550 while (*p == ' ')
551 ++p;
552 return p;
553}
554
555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 * check_status: called when the status bars for the buffer 'buf'
557 * need to be updated
558 */
559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100560check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
562 win_T *wp;
563
Bram Moolenaar29323592016-07-24 22:04:11 +0200564 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (wp->w_buffer == buf && wp->w_status_height)
566 {
567 wp->w_redr_status = TRUE;
568 if (must_redraw < VALID)
569 must_redraw = VALID;
570 }
571}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 * Ask for a reply from the user, a 'y' or a 'n'.
575 * No other characters are accepted, the message is repeated until a valid
576 * reply is entered or CTRL-C is hit.
577 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
578 * from any buffers but directly from the user.
579 *
580 * return the 'y' or 'n'
581 */
582 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100583ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 int r = ' ';
586 int save_State = State;
587
588 if (exiting) /* put terminal in raw mode for this question */
589 settmode(TMODE_RAW);
590 ++no_wait_return;
591#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200592 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200594 State = CONFIRM; // mouse behaves like with :confirm
595 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200597 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 while (r != 'y' && r != 'n')
600 {
601 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100602 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (direct)
604 r = get_keystroke();
605 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000606 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (r == Ctrl_C || r == ESC)
608 r = 'n';
609 msg_putchar(r); /* show what you typed */
610 out_flush();
611 }
612 --no_wait_return;
613 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 --no_mapping;
616 --allow_keys;
617
618 return r;
619}
620
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200621#if defined(FEAT_EVAL) || defined(PROTO)
622
623/*
624 * "mode()" function
625 */
626 void
627f_mode(typval_T *argvars, typval_T *rettv)
628{
629 char_u buf[4];
630
631 vim_memset(buf, 0, sizeof(buf));
632
633 if (time_for_testing == 93784)
634 {
635 /* Testing the two-character code. */
636 buf[0] = 'x';
637 buf[1] = '!';
638 }
639#ifdef FEAT_TERMINAL
640 else if (term_use_loop())
641 buf[0] = 't';
642#endif
643 else if (VIsual_active)
644 {
645 if (VIsual_select)
646 buf[0] = VIsual_mode + 's' - 'v';
647 else
648 buf[0] = VIsual_mode;
649 }
650 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
651 || State == CONFIRM)
652 {
653 buf[0] = 'r';
654 if (State == ASKMORE)
655 buf[1] = 'm';
656 else if (State == CONFIRM)
657 buf[1] = '?';
658 }
659 else if (State == EXTERNCMD)
660 buf[0] = '!';
661 else if (State & INSERT)
662 {
663 if (State & VREPLACE_FLAG)
664 {
665 buf[0] = 'R';
666 buf[1] = 'v';
667 }
668 else
669 {
670 if (State & REPLACE_FLAG)
671 buf[0] = 'R';
672 else
673 buf[0] = 'i';
674 if (ins_compl_active())
675 buf[1] = 'c';
676 else if (ctrl_x_mode_not_defined_yet())
677 buf[1] = 'x';
678 }
679 }
680 else if ((State & CMDLINE) || exmode_active)
681 {
682 buf[0] = 'c';
683 if (exmode_active == EXMODE_VIM)
684 buf[1] = 'v';
685 else if (exmode_active == EXMODE_NORMAL)
686 buf[1] = 'e';
687 }
688 else
689 {
690 buf[0] = 'n';
691 if (finish_op)
692 {
693 buf[1] = 'o';
694 // to be able to detect force-linewise/blockwise/characterwise operations
695 buf[2] = motion_force;
696 }
697 else if (restart_edit == 'I' || restart_edit == 'R'
698 || restart_edit == 'V')
699 {
700 buf[1] = 'i';
701 buf[2] = restart_edit;
702 }
703 }
704
705 /* Clear out the minor mode when the argument is not a non-zero number or
706 * non-empty string. */
707 if (!non_zero_arg(&argvars[0]))
708 buf[1] = NUL;
709
710 rettv->vval.v_string = vim_strsave(buf);
711 rettv->v_type = VAR_STRING;
712}
713
714 static void
715may_add_state_char(garray_T *gap, char_u *include, int c)
716{
717 if (include == NULL || vim_strchr(include, c) != NULL)
718 ga_append(gap, c);
719}
720
721/*
722 * "state()" function
723 */
724 void
725f_state(typval_T *argvars, typval_T *rettv)
726{
727 garray_T ga;
728 char_u *include = NULL;
729 int i;
730
731 ga_init2(&ga, 1, 20);
732 if (argvars[0].v_type != VAR_UNKNOWN)
733 include = tv_get_string(&argvars[0]);
734
735 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
736 may_add_state_char(&ga, include, 'm');
737 if (op_pending())
738 may_add_state_char(&ga, include, 'o');
739 if (autocmd_busy)
740 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200741 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200742 may_add_state_char(&ga, include, 'a');
743
744# ifdef FEAT_JOB_CHANNEL
745 if (channel_in_blocking_wait())
746 may_add_state_char(&ga, include, 'w');
747# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200748 if (!get_was_safe_state())
749 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200750 for (i = 0; i < get_callback_depth() && i < 3; ++i)
751 may_add_state_char(&ga, include, 'c');
752 if (msg_scrolled > 0)
753 may_add_state_char(&ga, include, 's');
754
755 rettv->v_type = VAR_STRING;
756 rettv->vval.v_string = ga.ga_data;
757}
758
759#endif // FEAT_EVAL
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761/*
762 * Get a key stroke directly from the user.
763 * Ignores mouse clicks and scrollbar events, except a click for the left
764 * button (used at the more prompt).
765 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
766 * Disadvantage: typeahead is ignored.
767 * Translates the interrupt character for unix to ESC.
768 */
769 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100770get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100772 char_u *buf = NULL;
773 int buflen = 150;
774 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 int len = 0;
776 int n;
777 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000778 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 mapped_ctrl_c = FALSE; /* mappings are not used here */
781 for (;;)
782 {
783 cursor_on();
784 out_flush();
785
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100786 /* Leave some room for check_termcode() to insert a key code into (max
787 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
788 * bytes. */
789 maxlen = (buflen - 6 - len) / 3;
790 if (buf == NULL)
791 buf = alloc(buflen);
792 else if (maxlen < 10)
793 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100794 char_u *t_buf = buf;
795
Bram Moolenaardc7e85e2012-06-20 12:40:08 +0200796 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100797 * escape sequence. */
798 buflen += 100;
799 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100800 if (buf == NULL)
801 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100802 maxlen = (buflen - 6 - len) / 3;
803 }
804 if (buf == NULL)
805 {
806 do_outofmem_msg((long_u)buflen);
807 return ESC; /* panic! */
808 }
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100811 * terminal code to complete. */
812 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if (n > 0)
814 {
815 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200816 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000818 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000820 else if (len > 0)
821 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar4395a712006-09-05 18:57:57 +0000823 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100824 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000825 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000827
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100828 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100829 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100830 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100831 {
832 /* Redrawing was postponed, do it now. */
833 update_screen(0);
834 setcursor(); /* put cursor back where it belongs */
835 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100836 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100837 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100838 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100840 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 continue;
842
843 /* Handle modifier and/or special key code. */
844 n = buf[0];
845 if (n == K_SPECIAL)
846 {
847 n = TO_SPECIAL(buf[1], buf[2]);
848 if (buf[1] == KS_MODIFIER
849 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +0100850#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100851 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +0100852#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100853#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 || n == K_VER_SCROLLBAR
855 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857 )
858 {
859 if (buf[1] == KS_MODIFIER)
860 mod_mask = buf[2];
861 len -= 3;
862 if (len > 0)
863 mch_memmove(buf, buf + 3, (size_t)len);
864 continue;
865 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (has_mbyte)
869 {
870 if (MB_BYTE2LEN(n) > len)
871 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100872 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 n = (*mb_ptr2char)(buf);
874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#ifdef UNIX
876 if (n == intr_char)
877 n = ESC;
878#endif
879 break;
880 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100881 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
883 mapped_ctrl_c = save_mapped_ctrl_c;
884 return n;
885}
886
887/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000888 * Get a number from the user.
889 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 */
891 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100892get_number(
893 int colon, /* allow colon to abort */
894 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 int n = 0;
897 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000898 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000900 if (mouse_used != NULL)
901 *mouse_used = FALSE;
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 /* When not printing messages, the user won't know what to type, return a
904 * zero (as if CR was hit). */
905 if (msg_silent != 0)
906 return 0;
907
908#ifdef USE_ON_FLY_SCROLL
909 dont_scroll = TRUE; /* disallow scrolling here */
910#endif
911 ++no_mapping;
912 ++allow_keys; /* no mapping here, but recognize keys */
913 for (;;)
914 {
915 windgoto(msg_row, msg_col);
916 c = safe_vgetc();
917 if (VIM_ISDIGIT(c))
918 {
919 n = n * 10 + c - '0';
920 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000921 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
924 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000925 if (typed > 0)
926 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100927 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000928 --typed;
929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000932#ifdef FEAT_MOUSE
933 else if (mouse_used != NULL && c == K_LEFTMOUSE)
934 {
935 *mouse_used = TRUE;
936 n = mouse_row + 1;
937 break;
938 }
939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 else if (n == 0 && c == ':' && colon)
941 {
942 stuffcharReadbuff(':');
943 if (!exmode_active)
944 cmdline_row = msg_row;
945 skip_redraw = TRUE; /* skip redraw once */
946 do_redraw = FALSE;
947 break;
948 }
949 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
950 break;
951 }
952 --no_mapping;
953 --allow_keys;
954 return n;
955}
956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000957/*
958 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000959 * When "mouse_used" is not NULL allow using the mouse and in that case return
960 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000961 */
962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100963prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000964{
965 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000966 int save_cmdline_row;
967 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000968
969 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000970 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100971 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000972 else
Bram Moolenaar32526b32019-01-19 17:43:09 +0100973 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000974
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200975 // Set the state such that text can be selected/copied/pasted and we still
976 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
977 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000978 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +0000979 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000980 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200981 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200982 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200983 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +0200984
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000985 i = get_number(TRUE, mouse_used);
986 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000987 {
Bram Moolenaar954bb062019-06-09 16:40:46 +0200988 // don't call wait_return() now
989 if (msg_row > 0)
990 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000991 need_wait_return = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000992 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000993 else
994 cmdline_row = save_cmdline_row;
995 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200996 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200997 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000999 return i;
1000}
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001003msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 long pn;
1006
1007 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1009 return;
1010
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001011 /* We don't want to overwrite another important message, but do overwrite
1012 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1013 * then "put" reports the last action. */
1014 if (keep_msg != NULL && !keep_msg_more)
1015 return;
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (n > 0)
1018 pn = n;
1019 else
1020 pn = -n;
1021
1022 if (pn > p_report)
1023 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001024 if (n > 0)
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 more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001028 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001029 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001031 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1032 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (msg(msg_buf))
1034 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001035 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001036 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 }
1039}
1040
1041/*
1042 * flush map and typeahead buffers and give a warning for an error
1043 */
1044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001045beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
1047 if (emsg_silent == 0)
1048 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001049 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001050 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 }
1052}
1053
1054/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001055 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
1057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001058vim_beep(
1059 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001061#ifdef FEAT_EVAL
1062 called_vim_beep = TRUE;
1063#endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (emsg_silent == 0)
1066 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001067 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1068 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001069#ifdef ELAPSED_FUNC
1070 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001071 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001072
1073 /* Only beep once per half a second, otherwise a sequence of beeps
1074 * would freeze Vim. */
1075 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1076 {
1077 did_init = TRUE;
1078 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001080 if (p_vb
1081#ifdef FEAT_GUI
1082 /* While the GUI is starting up the termcap is set for
1083 * the GUI but the output still goes to a terminal. */
1084 && !(gui.in_use && gui.starting)
1085#endif
1086 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001087 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001088 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001089#ifdef FEAT_VTP
1090 /* No restore color information, refresh the screen. */
1091 if (has_vtp_working() != 0
1092# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001093 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001094# endif
1095 )
1096 {
1097 redraw_later(CLEAR);
1098 update_screen(0);
1099 redrawcmd();
1100 }
1101#endif
1102 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001103 else
1104 out_char(BELL);
1105#ifdef ELAPSED_FUNC
1106 }
1107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001110 /* When 'debug' contains "beep" produce a message. If we are sourcing
1111 * a script or executing a function give the user a hint where the beep
1112 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (vim_strchr(p_debug, 'e') != NULL)
1114 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001115 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001116 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
1119}
1120
1121/*
1122 * To get the "real" home directory:
1123 * - get value of $HOME
1124 * For Unix:
1125 * - go to that directory
1126 * - do mch_dirname() to get the real name of that directory.
1127 * This also works with mounts and links.
1128 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001129 * For Windows:
1130 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001133init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
1135 char_u *var;
1136
Bram Moolenaar05159a02005-02-26 23:04:13 +00001137 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001138 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001139
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#ifdef VMS
1141 var = mch_getenv((char_u *)"SYS$LOGIN");
1142#else
1143 var = mch_getenv((char_u *)"HOME");
1144#endif
1145
Bram Moolenaar4f974752019-02-17 17:44:42 +01001146#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001148 * Typically, $HOME is not defined on Windows, unless the user has
1149 * specifically defined it for Vim's sake. However, on Windows NT
1150 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1151 * each user. Try constructing $HOME from these.
1152 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001153 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001154 {
1155 char_u *homedrive, *homepath;
1156
1157 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1158 homepath = mch_getenv((char_u *)"HOMEPATH");
1159 if (homepath == NULL || *homepath == NUL)
1160 homepath = (char_u *)"\\";
1161 if (homedrive != NULL
1162 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1163 {
1164 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1165 if (NameBuff[0] != NUL)
1166 var = NameBuff;
1167 }
1168 }
1169
1170 if (var == NULL)
1171 var = mch_getenv((char_u *)"USERPROFILE");
1172
1173 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * Weird but true: $HOME may contain an indirect reference to another
1175 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1176 * when $HOME is being set.
1177 */
1178 if (var != NULL && *var == '%')
1179 {
1180 char_u *p;
1181 char_u *exp;
1182
1183 p = vim_strchr(var + 1, '%');
1184 if (p != NULL)
1185 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001186 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 exp = mch_getenv(NameBuff);
1188 if (exp != NULL && *exp != NUL
1189 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1190 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001191 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 }
1195 }
1196
Bram Moolenaar48340b62017-08-29 22:08:53 +02001197 if (var != NULL && *var == NUL) /* empty is same as not set */
1198 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
Bram Moolenaar05159a02005-02-26 23:04:13 +00001200 if (enc_utf8 && var != NULL)
1201 {
1202 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001203 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001204
1205 /* Convert from active codepage to UTF-8. Other conversions are
1206 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001207 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001208 if (pp != NULL)
1209 {
1210 homedir = pp;
1211 return;
1212 }
1213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 /*
1216 * Default home dir is C:/
1217 * Best assumption we can make in such a situation.
1218 */
1219 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001220 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (var != NULL)
1224 {
1225#ifdef UNIX
1226 /*
1227 * Change to the directory and get the actual path. This resolves
1228 * links. Don't do it when we can't return.
1229 */
1230 if (mch_dirname(NameBuff, MAXPATHL) == OK
1231 && mch_chdir((char *)NameBuff) == 0)
1232 {
1233 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1234 var = IObuff;
1235 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
1238#endif
1239 homedir = vim_strsave(var);
1240 }
1241}
1242
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001243#if defined(EXITFREE) || defined(PROTO)
1244 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001245free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001246{
1247 vim_free(homedir);
1248}
Bram Moolenaar24305862012-08-15 14:05:05 +02001249
Bram Moolenaar24305862012-08-15 14:05:05 +02001250 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001251free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001252{
1253 ga_clear_strings(&ga_users);
1254}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001255#endif
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001258 * Call expand_env() and store the result in an allocated string.
1259 * This is not very memory efficient, this expects the result to be freed
1260 * again soon.
1261 */
1262 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001263expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001264{
1265 return expand_env_save_opt(src, FALSE);
1266}
1267
1268/*
1269 * Idem, but when "one" is TRUE handle the string as one file name, only
1270 * expand "~" at the start.
1271 */
1272 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001273expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001274{
1275 char_u *p;
1276
1277 p = alloc(MAXPATHL);
1278 if (p != NULL)
1279 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1280 return p;
1281}
1282
1283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 * Expand environment variable with path name.
1285 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001286 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * If anything fails no expansion is done and dst equals src.
1288 */
1289 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001290expand_env(
1291 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1292 char_u *dst, /* where to put the result */
1293 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001295 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296}
1297
1298 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001299expand_env_esc(
1300 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1301 char_u *dst, /* where to put the result */
1302 int dstlen, /* maximum length of the result */
1303 int esc, /* escape spaces in expanded variables */
1304 int one, /* "srcp" is one file name */
1305 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001307 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 char_u *tail;
1309 int c;
1310 char_u *var;
1311 int copy_char;
1312 int mustfree; /* var was allocated, need to free it later */
1313 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001314 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001316 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001317 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001318
1319 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 --dstlen; /* leave one char space for "\," */
1321 while (*src && dstlen > 0)
1322 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001323#ifdef FEAT_EVAL
1324 /* Skip over `=expr`. */
1325 if (src[0] == '`' && src[1] == '=')
1326 {
1327 size_t len;
1328
1329 var = src;
1330 src += 2;
1331 (void)skip_expr(&src);
1332 if (*src == '`')
1333 ++src;
1334 len = src - var;
1335 if (len > (size_t)dstlen)
1336 len = dstlen;
1337 vim_strncpy(dst, var, len);
1338 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001339 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001340 continue;
1341 }
1342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001344 if ((*src == '$'
1345#ifdef VMS
1346 && at_start
1347#endif
1348 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001349#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 || *src == '%'
1351#endif
1352 || (*src == '~' && at_start))
1353 {
1354 mustfree = FALSE;
1355
1356 /*
1357 * The variable name is copied into dst temporarily, because it may
1358 * be a string in read-only memory and a NUL needs to be appended.
1359 */
1360 if (*src != '~') /* environment var */
1361 {
1362 tail = src + 1;
1363 var = dst;
1364 c = dstlen - 1;
1365
1366#ifdef UNIX
1367 /* Unix has ${var-name} type environment vars */
1368 if (*tail == '{' && !vim_isIDc('{'))
1369 {
1370 tail++; /* ignore '{' */
1371 while (c-- > 0 && *tail && *tail != '}')
1372 *var++ = *tail++;
1373 }
1374 else
1375#endif
1376 {
1377 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001378#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 || (*src == '%' && *tail != '%')
1380#endif
1381 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001385#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386# ifdef UNIX
1387 if (src[1] == '{' && *tail != '}')
1388# else
1389 if (*src == '%' && *tail != '%')
1390# endif
1391 var = NULL;
1392 else
1393 {
1394# ifdef UNIX
1395 if (src[1] == '{')
1396# else
1397 if (*src == '%')
1398#endif
1399 ++tail;
1400#endif
1401 *var = NUL;
1402 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001403#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405#endif
1406 }
1407 /* home directory */
1408 else if ( src[1] == NUL
1409 || vim_ispathsep(src[1])
1410 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1411 {
1412 var = homedir;
1413 tail = src + 1;
1414 }
1415 else /* user directory */
1416 {
1417#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1418 /*
1419 * Copy ~user to dst[], so we can put a NUL after it.
1420 */
1421 tail = src;
1422 var = dst;
1423 c = dstlen - 1;
1424 while ( c-- > 0
1425 && *tail
1426 && vim_isfilec(*tail)
1427 && !vim_ispathsep(*tail))
1428 *var++ = *tail++;
1429 *var = NUL;
1430# ifdef UNIX
1431 /*
1432 * If the system supports getpwnam(), use it.
1433 * Otherwise, or if getpwnam() fails, the shell is used to
1434 * expand ~user. This is slower and may fail if the shell
1435 * does not support ~user (old versions of /bin/sh).
1436 */
1437# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1438 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001439 /* Note: memory allocated by getpwnam() is never freed.
1440 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001441 struct passwd *pw = (*dst == NUL)
1442 ? NULL : getpwnam((char *)dst + 1);
1443
1444 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 if (var == NULL)
1447# endif
1448 {
1449 expand_T xpc;
1450
1451 ExpandInit(&xpc);
1452 xpc.xp_context = EXPAND_FILES;
1453 var = ExpandOne(&xpc, dst, NULL,
1454 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 mustfree = TRUE;
1456 }
1457
1458# else /* !UNIX, thus VMS */
1459 /*
1460 * USER_HOME is a comma-separated list of
1461 * directories to search for the user account in.
1462 */
1463 {
1464 char_u test[MAXPATHL], paths[MAXPATHL];
1465 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001466 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
1468 STRCPY(paths, USER_HOME);
1469 next_path = paths;
1470 while (*next_path)
1471 {
1472 for (path = next_path; *next_path && *next_path != ',';
1473 next_path++);
1474 if (*next_path)
1475 *next_path++ = NUL;
1476 STRCPY(test, path);
1477 STRCAT(test, "/");
1478 STRCAT(test, dst + 1);
1479 if (mch_stat(test, &st) == 0)
1480 {
1481 var = alloc(STRLEN(test) + 1);
1482 STRCPY(var, test);
1483 mustfree = TRUE;
1484 break;
1485 }
1486 }
1487 }
1488# endif /* UNIX */
1489#else
1490 /* cannot expand user's home directory, so don't try */
1491 var = NULL;
1492 tail = (char_u *)""; /* for gcc */
1493#endif /* UNIX || VMS */
1494 }
1495
1496#ifdef BACKSLASH_IN_FILENAME
1497 /* If 'shellslash' is set change backslashes to forward slashes.
1498 * Can't use slash_adjust(), p_ssl may be set temporarily. */
1499 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1500 {
1501 char_u *p = vim_strsave(var);
1502
1503 if (p != NULL)
1504 {
1505 if (mustfree)
1506 vim_free(var);
1507 var = p;
1508 mustfree = TRUE;
1509 forward_slash(var);
1510 }
1511 }
1512#endif
1513
1514 /* If "var" contains white space, escape it with a backslash.
1515 * Required for ":e ~/tt" when $HOME includes a space. */
1516 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1517 {
1518 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1519
1520 if (p != NULL)
1521 {
1522 if (mustfree)
1523 vim_free(var);
1524 var = p;
1525 mustfree = TRUE;
1526 }
1527 }
1528
1529 if (var != NULL && *var != NUL
1530 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1531 {
1532 STRCPY(dst, var);
1533 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001534 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 /* if var[] ends in a path separator and tail[] starts
1536 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001537 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1539 && dst[-1] != ':'
1540#endif
1541 && vim_ispathsep(*tail))
1542 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001543 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 src = tail;
1545 copy_char = FALSE;
1546 }
1547 if (mustfree)
1548 vim_free(var);
1549 }
1550
1551 if (copy_char) /* copy at least one char */
1552 {
1553 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001554 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001555 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1556 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
1558 at_start = FALSE;
1559 if (src[0] == '\\' && src[1] != NUL)
1560 {
1561 *dst++ = *src++;
1562 --dstlen;
1563 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001564 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001566 if (dstlen > 0)
1567 {
1568 *dst++ = *src++;
1569 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570
Bram Moolenaar1c864092017-08-06 18:15:45 +02001571 if (startstr != NULL && src - startstr_len >= srcp
1572 && STRNCMP(src - startstr_len, startstr,
1573 startstr_len) == 0)
1574 at_start = TRUE;
1575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 *dst = NUL;
1580}
1581
1582/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001583 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1584 * the length of "name/". Otherwise return "pend".
1585 */
1586 static char_u *
1587remove_tail(char_u *p, char_u *pend, char_u *name)
1588{
1589 int len = (int)STRLEN(name) + 1;
1590 char_u *newend = pend - len;
1591
1592 if (newend >= p
1593 && fnamencmp(newend, name, len - 1) == 0
1594 && (newend == p || after_pathsep(p, newend)))
1595 return newend;
1596 return pend;
1597}
1598
1599/*
1600 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1601 * Return NULL if not, return its name in allocated memory otherwise.
1602 */
1603 static char_u *
1604vim_version_dir(char_u *vimdir)
1605{
1606 char_u *p;
1607
1608 if (vimdir == NULL || *vimdir == NUL)
1609 return NULL;
1610 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1611 if (p != NULL && mch_isdir(p))
1612 return p;
1613 vim_free(p);
1614 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1615 if (p != NULL && mch_isdir(p))
1616 return p;
1617 vim_free(p);
1618 return NULL;
1619}
1620
1621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 * Vim's version of getenv().
1623 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001624 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001625 * "mustfree" is set to TRUE when returned is allocated, it must be
1626 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 */
1628 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001629vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001631 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 char_u *pend;
1633 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001634#ifdef MSWIN
1635 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001637 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (STRCMP(name, "HOME") == 0)
1639 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001641 // Use Wide function
1642 wn = enc_to_utf16(name, NULL);
1643 if (wn == NULL)
1644 return NULL;
1645
1646 wp = _wgetenv(wn);
1647 vim_free(wn);
1648
1649 if (wp != NULL && *wp == NUL) // empty is the same as not set
1650 wp = NULL;
1651
1652 if (wp != NULL)
1653 {
1654 p = utf16_to_enc(wp, NULL);
1655 if (p == NULL)
1656 return NULL;
1657
1658 *mustfree = TRUE;
1659 return p;
1660 }
1661#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001663 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 p = NULL;
1665
1666 if (p != NULL)
1667 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001670 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1672 if (!vimruntime && STRCMP(name, "VIM") != 0)
1673 return NULL;
1674
1675 /*
1676 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1677 * Don't do this when default_vimruntime_dir is non-empty.
1678 */
1679 if (vimruntime
1680#ifdef HAVE_PATHDEF
1681 && *default_vimruntime_dir == NUL
1682#endif
1683 )
1684 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001685#ifdef MSWIN
1686 // Use Wide function
1687 wp = _wgetenv(L"VIM");
1688 if (wp != NULL && *wp == NUL) // empty is the same as not set
1689 wp = NULL;
1690 if (wp != NULL)
1691 {
1692 char_u *q = utf16_to_enc(wp, NULL);
1693 if (q != NULL)
1694 {
1695 p = vim_version_dir(q);
1696 *mustfree = TRUE;
1697 if (p == NULL)
1698 p = q;
1699 }
1700 }
1701#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001703 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 p = NULL;
1705 if (p != NULL)
1706 {
1707 p = vim_version_dir(p);
1708 if (p != NULL)
1709 *mustfree = TRUE;
1710 else
1711 p = mch_getenv((char_u *)"VIM");
1712 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715
1716 /*
1717 * When expanding $VIM or $VIMRUNTIME fails, try using:
1718 * - the directory name from 'helpfile' (unless it contains '$')
1719 * - the executable name from argv[0]
1720 */
1721 if (p == NULL)
1722 {
1723 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1724 p = p_hf;
1725#ifdef USE_EXE_NAME
1726 /*
1727 * Use the name of the executable, obtained from argv[0].
1728 */
1729 else
1730 p = exe_name;
1731#endif
1732 if (p != NULL)
1733 {
1734 /* remove the file name */
1735 pend = gettail(p);
1736
1737 /* remove "doc/" from 'helpfile', if present */
1738 if (p == p_hf)
1739 pend = remove_tail(p, pend, (char_u *)"doc");
1740
1741#ifdef USE_EXE_NAME
1742# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001743 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (p == exe_name)
1745 {
1746 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001747 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001749 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1750 if (pend1 != pend)
1751 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001752 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001753 if (pnew != NULL)
1754 {
1755 STRNCPY(pnew, p, (pend1 - p));
1756 STRCPY(pnew + (pend1 - p), "Resources/vim");
1757 p = pnew;
1758 pend = p + STRLEN(p);
1759 }
1760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762# endif
1763 /* remove "src/" from exe_name, if present */
1764 if (p == exe_name)
1765 pend = remove_tail(p, pend, (char_u *)"src");
1766#endif
1767
1768 /* for $VIM, remove "runtime/" or "vim54/", if present */
1769 if (!vimruntime)
1770 {
1771 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1772 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1773 }
1774
1775 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001776 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001779#ifdef MACOS_X
1780 if (p == exe_name || p == p_hf)
1781#endif
1782 /* check that the result is a directory name */
1783 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001786 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 else
1788 {
1789#ifdef USE_EXE_NAME
1790 /* may add "/vim54" or "/runtime" if it exists */
1791 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1792 {
1793 vim_free(p);
1794 p = pend;
1795 }
1796#endif
1797 *mustfree = TRUE;
1798 }
1799 }
1800 }
1801
1802#ifdef HAVE_PATHDEF
1803 /* When there is a pathdef.c file we can use default_vim_dir and
1804 * default_vimruntime_dir */
1805 if (p == NULL)
1806 {
1807 /* Only use default_vimruntime_dir when it is not empty */
1808 if (vimruntime && *default_vimruntime_dir != NUL)
1809 {
1810 p = default_vimruntime_dir;
1811 *mustfree = FALSE;
1812 }
1813 else if (*default_vim_dir != NUL)
1814 {
1815 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1816 *mustfree = TRUE;
1817 else
1818 {
1819 p = default_vim_dir;
1820 *mustfree = FALSE;
1821 }
1822 }
1823 }
1824#endif
1825
1826 /*
1827 * Set the environment variable, so that the new value can be found fast
1828 * next time, and others can also use it (e.g. Perl).
1829 */
1830 if (p != NULL)
1831 {
1832 if (vimruntime)
1833 {
1834 vim_setenv((char_u *)"VIMRUNTIME", p);
1835 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837 else
1838 {
1839 vim_setenv((char_u *)"VIM", p);
1840 didset_vim = TRUE;
1841 }
1842 }
1843 return p;
1844}
1845
Bram Moolenaar113e1072019-01-20 15:30:40 +01001846#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02001847 void
1848vim_unsetenv(char_u *var)
1849{
1850#ifdef HAVE_UNSETENV
1851 unsetenv((char *)var);
1852#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001853 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001854#endif
1855}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001856#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02001857
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 * Our portable version of setenv.
1861 */
1862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001863vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
1865#ifdef HAVE_SETENV
1866 mch_setenv((char *)name, (char *)val, 1);
1867#else
1868 char_u *envbuf;
1869
1870 /*
1871 * Putenv does not copy the string, it has to remain
1872 * valid. The allocated memory will never be freed.
1873 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001874 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (envbuf != NULL)
1876 {
1877 sprintf((char *)envbuf, "%s=%s", name, val);
1878 putenv((char *)envbuf);
1879 }
1880#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001881#ifdef FEAT_GETTEXT
1882 /*
1883 * When setting $VIMRUNTIME adjust the directory to find message
1884 * translations to $VIMRUNTIME/lang.
1885 */
1886 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1887 {
1888 char_u *buf = concat_str(val, (char_u *)"/lang");
1889
1890 if (buf != NULL)
1891 {
1892 bindtextdomain(VIMPACKAGE, (char *)buf);
1893 vim_free(buf);
1894 }
1895 }
1896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897}
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899/*
1900 * Function given to ExpandGeneric() to obtain an environment variable name.
1901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001903get_env_name(
1904 expand_T *xp UNUSED,
1905 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
Bram Moolenaard0573012017-10-28 21:11:06 +02001907# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02001909 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 */
1911 return NULL;
1912# else
1913# ifndef __WIN32__
1914 /* Borland C++ 5.2 has this in a header file. */
1915 extern char **environ;
1916# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001917# define ENVNAMELEN 100
1918 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 char_u *str;
1920 int n;
1921
1922 str = (char_u *)environ[idx];
1923 if (str == NULL)
1924 return NULL;
1925
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001926 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
1928 if (str[n] == '=' || str[n] == NUL)
1929 break;
1930 name[n] = str[n];
1931 }
1932 name[n] = NUL;
1933 return name;
1934# endif
1935}
Bram Moolenaar24305862012-08-15 14:05:05 +02001936
1937/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001938 * Add a user name to the list of users in ga_users.
1939 * Do nothing if user name is NULL or empty.
1940 */
1941 static void
1942add_user(char_u *user, int need_copy)
1943{
1944 char_u *user_copy = (user != NULL && need_copy)
1945 ? vim_strsave(user) : user;
1946
1947 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
1948 {
1949 if (need_copy)
1950 vim_free(user);
1951 return;
1952 }
1953 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
1954}
1955
1956/*
Bram Moolenaar24305862012-08-15 14:05:05 +02001957 * Find all user names for user completion.
1958 * Done only once and then cached.
1959 */
1960 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001961init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02001962{
Bram Moolenaar24305862012-08-15 14:05:05 +02001963 static int lazy_init_done = FALSE;
1964
1965 if (lazy_init_done)
1966 return;
1967
1968 lazy_init_done = TRUE;
1969 ga_init2(&ga_users, sizeof(char_u *), 20);
1970
1971# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
1972 {
Bram Moolenaar24305862012-08-15 14:05:05 +02001973 struct passwd* pw;
1974
1975 setpwent();
1976 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001977 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02001978 endpwent();
1979 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01001980# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001981 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001982 DWORD nusers = 0, ntotal = 0, i;
1983 PUSER_INFO_0 uinfo;
1984
1985 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
1986 &nusers, &ntotal, NULL) == NERR_Success)
1987 {
1988 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001989 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001990
1991 NetApiBufferFree(uinfo);
1992 }
1993 }
Bram Moolenaar24305862012-08-15 14:05:05 +02001994# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001995# if defined(HAVE_GETPWNAM)
1996 {
1997 char_u *user_env = mch_getenv((char_u *)"USER");
1998
1999 // The $USER environment variable may be a valid remote user name (NIS,
2000 // LDAP) not already listed by getpwent(), as getpwent() only lists
2001 // local user names. If $USER is not already listed, check whether it
2002 // is a valid remote user name using getpwnam() and if it is, add it to
2003 // the list of user names.
2004
2005 if (user_env != NULL && *user_env != NUL)
2006 {
2007 int i;
2008
2009 for (i = 0; i < ga_users.ga_len; i++)
2010 {
2011 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2012
2013 if (STRCMP(local_user, user_env) == 0)
2014 break;
2015 }
2016
2017 if (i == ga_users.ga_len)
2018 {
2019 struct passwd *pw = getpwnam((char *)user_env);
2020
2021 if (pw != NULL)
2022 add_user((char_u *)pw->pw_name, TRUE);
2023 }
2024 }
2025 }
2026# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002027}
2028
2029/*
2030 * Function given to ExpandGeneric() to obtain an user names.
2031 */
2032 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002033get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002034{
2035 init_users();
2036 if (idx < ga_users.ga_len)
2037 return ((char_u **)ga_users.ga_data)[idx];
2038 return NULL;
2039}
2040
2041/*
2042 * Check whether name matches a user name. Return:
2043 * 0 if name does not match any user name.
2044 * 1 if name partially matches the beginning of a user name.
2045 * 2 is name fully matches a user name.
2046 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002047 int
2048match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002049{
2050 int i;
2051 int n = (int)STRLEN(name);
2052 int result = 0;
2053
2054 init_users();
2055 for (i = 0; i < ga_users.ga_len; i++)
2056 {
2057 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2058 return 2; /* full match */
2059 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2060 result = 1; /* partial match */
2061 }
2062 return result;
2063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002066 * Concatenate two strings and return the result in allocated memory.
2067 * Returns NULL when out of memory.
2068 */
2069 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002070concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002071{
2072 char_u *dest;
2073 size_t l = STRLEN(str1);
2074
Bram Moolenaar964b3742019-05-24 18:54:09 +02002075 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002076 if (dest != NULL)
2077 {
2078 STRCPY(dest, str1);
2079 STRCPY(dest + l, str2);
2080 }
2081 return dest;
2082}
Bram Moolenaard6754642005-01-17 22:18:45 +00002083
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002084 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002085prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002087#if defined(SIGHUP) && defined(SIG_IGN)
2088 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2089 * makes Vim exit and then handling SIGHUP causes various reentrance
2090 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002091 signal(SIGHUP, SIG_IGN);
2092#endif
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094#ifdef FEAT_GUI
2095 if (gui.in_use)
2096 {
2097 gui.dying = TRUE;
2098 out_trash(); /* trash any pending output */
2099 }
2100 else
2101#endif
2102 {
2103 windgoto((int)Rows - 1, 0);
2104
2105 /*
2106 * Switch terminal mode back now, so messages end up on the "normal"
2107 * screen (if there are two screens).
2108 */
2109 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002110 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 out_flush();
2112 }
2113}
2114
2115/*
2116 * Preserve files and exit.
2117 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002118 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2119 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 */
2121 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002122preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123{
2124 buf_T *buf;
2125
2126 prepare_to_exit();
2127
Bram Moolenaar4770d092006-01-12 23:22:24 +00002128 /* Setting this will prevent free() calls. That avoids calling free()
2129 * recursively when free() was invoked with a bad pointer. */
2130 really_exiting = TRUE;
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 out_str(IObuff);
2133 screen_start(); /* don't know where cursor is now */
2134 out_flush();
2135
2136 ml_close_notmod(); /* close all not-modified buffers */
2137
Bram Moolenaar29323592016-07-24 22:04:11 +02002138 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2141 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002142 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 screen_start(); /* don't know where cursor is now */
2144 out_flush();
2145 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2146 break;
2147 }
2148 }
2149
2150 ml_close_all(FALSE); /* close all memfiles, without deleting */
2151
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002152 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 getout(1);
2155}
2156
2157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 * Check for CTRL-C pressed, but only once in a while.
2159 * Should be used instead of ui_breakcheck() for functions that check for
2160 * each line in the file. Calling ui_breakcheck() each time takes too much
2161 * time, because it can be a system call.
2162 */
2163
2164#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002165# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#endif
2167
2168static int breakcheck_count = 0;
2169
2170 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002171line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 if (++breakcheck_count >= BREAKCHECK_SKIP)
2174 {
2175 breakcheck_count = 0;
2176 ui_breakcheck();
2177 }
2178}
2179
2180/*
2181 * Like line_breakcheck() but check 10 times less often.
2182 */
2183 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002184fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2187 {
2188 breakcheck_count = 0;
2189 ui_breakcheck();
2190 }
2191}
2192
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002193#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2194 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2195 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197#ifndef SEEK_SET
2198# define SEEK_SET 0
2199#endif
2200#ifndef SEEK_END
2201# define SEEK_END 2
2202#endif
2203
2204/*
2205 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002206 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2207 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 * Returns an allocated string, or NULL for error.
2209 */
2210 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002211get_cmd_output(
2212 char_u *cmd,
2213 char_u *infile, /* optional input file name */
2214 int flags, /* can be SHELL_SILENT */
2215 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
2217 char_u *tempname;
2218 char_u *command;
2219 char_u *buffer = NULL;
2220 int len;
2221 int i = 0;
2222 FILE *fd;
2223
2224 if (check_restricted() || check_secure())
2225 return NULL;
2226
2227 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002228 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002230 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return NULL;
2232 }
2233
2234 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002235 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (command == NULL)
2237 goto done;
2238
2239 /*
2240 * Call the shell to execute the command (errors are ignored).
2241 * Don't check timestamps here.
2242 */
2243 ++no_check_timestamps;
2244 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2245 --no_check_timestamps;
2246
2247 vim_free(command);
2248
2249 /*
2250 * read the names from the file into memory
2251 */
2252# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00002253 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 fd = mch_fopen((char *)tempname, "r");
2255# else
2256 fd = mch_fopen((char *)tempname, READBIN);
2257# endif
2258
2259 if (fd == NULL)
2260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002261 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 goto done;
2263 }
2264
2265 fseek(fd, 0L, SEEK_END);
2266 len = ftell(fd); /* get size of temp file */
2267 fseek(fd, 0L, SEEK_SET);
2268
2269 buffer = alloc(len + 1);
2270 if (buffer != NULL)
2271 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2272 fclose(fd);
2273 mch_remove(tempname);
2274 if (buffer == NULL)
2275 goto done;
2276#ifdef VMS
2277 len = i; /* VMS doesn't give us what we asked for... */
2278#endif
2279 if (i != len)
2280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002282 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002284 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002285 {
2286 /* Change NUL into SOH, otherwise the string is truncated. */
2287 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002288 if (buffer[i] == NUL)
2289 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002290
Bram Moolenaar162bd912010-07-28 22:29:10 +02002291 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002292 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002293 else
2294 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296done:
2297 vim_free(tempname);
2298 return buffer;
2299}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002300
2301# if defined(FEAT_EVAL) || defined(PROTO)
2302
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002303 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002304get_cmd_output_as_rettv(
2305 typval_T *argvars,
2306 typval_T *rettv,
2307 int retlist)
2308{
2309 char_u *res = NULL;
2310 char_u *p;
2311 char_u *infile = NULL;
2312 int err = FALSE;
2313 FILE *fd;
2314 list_T *list = NULL;
2315 int flags = SHELL_SILENT;
2316
2317 rettv->v_type = VAR_STRING;
2318 rettv->vval.v_string = NULL;
2319 if (check_restricted() || check_secure())
2320 goto errret;
2321
2322 if (argvars[1].v_type != VAR_UNKNOWN)
2323 {
2324 /*
2325 * Write the text to a temp file, to be used for input of the shell
2326 * command.
2327 */
2328 if ((infile = vim_tempname('i', TRUE)) == NULL)
2329 {
2330 emsg(_(e_notmp));
2331 goto errret;
2332 }
2333
2334 fd = mch_fopen((char *)infile, WRITEBIN);
2335 if (fd == NULL)
2336 {
2337 semsg(_(e_notopen), infile);
2338 goto errret;
2339 }
2340 if (argvars[1].v_type == VAR_NUMBER)
2341 {
2342 linenr_T lnum;
2343 buf_T *buf;
2344
2345 buf = buflist_findnr(argvars[1].vval.v_number);
2346 if (buf == NULL)
2347 {
2348 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2349 fclose(fd);
2350 goto errret;
2351 }
2352
2353 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2354 {
2355 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2356 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2357 {
2358 err = TRUE;
2359 break;
2360 }
2361 if (putc(NL, fd) == EOF)
2362 {
2363 err = TRUE;
2364 break;
2365 }
2366 }
2367 }
2368 else if (argvars[1].v_type == VAR_LIST)
2369 {
2370 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2371 err = TRUE;
2372 }
2373 else
2374 {
2375 size_t len;
2376 char_u buf[NUMBUFLEN];
2377
2378 p = tv_get_string_buf_chk(&argvars[1], buf);
2379 if (p == NULL)
2380 {
2381 fclose(fd);
2382 goto errret; /* type error; errmsg already given */
2383 }
2384 len = STRLEN(p);
2385 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2386 err = TRUE;
2387 }
2388 if (fclose(fd) != 0)
2389 err = TRUE;
2390 if (err)
2391 {
2392 emsg(_("E677: Error writing temp file"));
2393 goto errret;
2394 }
2395 }
2396
2397 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2398 * echoes typeahead, that messes up the display. */
2399 if (!msg_silent)
2400 flags += SHELL_COOKED;
2401
2402 if (retlist)
2403 {
2404 int len;
2405 listitem_T *li;
2406 char_u *s = NULL;
2407 char_u *start;
2408 char_u *end;
2409 int i;
2410
2411 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2412 if (res == NULL)
2413 goto errret;
2414
2415 list = list_alloc();
2416 if (list == NULL)
2417 goto errret;
2418
2419 for (i = 0; i < len; ++i)
2420 {
2421 start = res + i;
2422 while (i < len && res[i] != NL)
2423 ++i;
2424 end = res + i;
2425
2426 s = alloc(end - start + 1);
2427 if (s == NULL)
2428 goto errret;
2429
2430 for (p = s; start < end; ++p, ++start)
2431 *p = *start == NUL ? NL : *start;
2432 *p = NUL;
2433
2434 li = listitem_alloc();
2435 if (li == NULL)
2436 {
2437 vim_free(s);
2438 goto errret;
2439 }
2440 li->li_tv.v_type = VAR_STRING;
2441 li->li_tv.v_lock = 0;
2442 li->li_tv.vval.v_string = s;
2443 list_append(list, li);
2444 }
2445
2446 rettv_list_set(rettv, list);
2447 list = NULL;
2448 }
2449 else
2450 {
2451 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2452#ifdef USE_CRNL
2453 /* translate <CR><NL> into <NL> */
2454 if (res != NULL)
2455 {
2456 char_u *s, *d;
2457
2458 d = res;
2459 for (s = res; *s; ++s)
2460 {
2461 if (s[0] == CAR && s[1] == NL)
2462 ++s;
2463 *d++ = *s;
2464 }
2465 *d = NUL;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002468 rettv->vval.v_string = res;
2469 res = NULL;
2470 }
2471
2472errret:
2473 if (infile != NULL)
2474 {
2475 mch_remove(infile);
2476 vim_free(infile);
2477 }
2478 if (res != NULL)
2479 vim_free(res);
2480 if (list != NULL)
2481 list_free(list);
2482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002485 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
2487 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002488f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002490 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491}
2492
2493/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002494 * "systemlist()" function
2495 */
2496 void
2497f_systemlist(typval_T *argvars, typval_T *rettv)
2498{
2499 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2500}
2501# endif // FEAT_EVAL
2502
2503#endif
2504
2505/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002506 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 * Don't do this when still processing a command or a mapping.
2508 * Don't do this when inside a ":normal" command.
2509 */
2510 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002511goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 return (p_im && stuff_empty() && typebuf_typed());
2514}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002515
2516/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002517 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002518 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2519 * - Remove any argument. E.g., "csh -f" -> "csh".
2520 * But don't allow a space in the path, so that this works:
2521 * "/usr/bin/csh --rcfile ~/.cshrc"
2522 * But don't do that for Windows, it's common to have a space in the path.
2523 */
2524 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002525get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002526{
2527 char_u *p;
2528
Bram Moolenaar4f974752019-02-17 17:44:42 +01002529#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002530 p = gettail(p_sh);
2531 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
2532#else
2533 p = skiptowhite(p_sh);
2534 if (*p == NUL)
2535 {
2536 /* No white space, use the tail. */
2537 p = vim_strsave(gettail(p_sh));
2538 }
2539 else
2540 {
2541 char_u *p1, *p2;
2542
2543 /* Find the last path separator before the space. */
2544 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002545 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002546 if (vim_ispathsep(*p2))
2547 p1 = p2 + 1;
2548 p = vim_strnsave(p1, (int)(p - p1));
2549 }
2550#endif
2551 return p;
2552}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002553
2554/*
2555 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2556 * Also check for ":\\", which MS Internet Explorer accepts, return
2557 * URL_BACKSLASH.
2558 */
2559 int
2560path_is_url(char_u *p)
2561{
2562 if (STRNCMP(p, "://", (size_t)3) == 0)
2563 return URL_SLASH;
2564 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2565 return URL_BACKSLASH;
2566 return 0;
2567}
2568
2569/*
2570 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
2571 * Return URL_BACKSLASH for "name:\\".
2572 * Return zero otherwise.
2573 */
2574 int
2575path_with_url(char_u *fname)
2576{
2577 char_u *p;
2578
2579 for (p = fname; isalpha(*p); ++p)
2580 ;
2581 return path_is_url(p);
2582}