blob: 0e182555ac651e18674e9d6365fb8c78df10a608 [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 Moolenaar2526ef22013-03-16 14:20:51 +0100850 || (is_mouse_key(n) && n != K_LEFTMOUSE)
851#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 || n == K_VER_SCROLLBAR
853 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855 )
856 {
857 if (buf[1] == KS_MODIFIER)
858 mod_mask = buf[2];
859 len -= 3;
860 if (len > 0)
861 mch_memmove(buf, buf + 3, (size_t)len);
862 continue;
863 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000864 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (has_mbyte)
867 {
868 if (MB_BYTE2LEN(n) > len)
869 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100870 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 n = (*mb_ptr2char)(buf);
872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#ifdef UNIX
874 if (n == intr_char)
875 n = ESC;
876#endif
877 break;
878 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100879 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
881 mapped_ctrl_c = save_mapped_ctrl_c;
882 return n;
883}
884
885/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000886 * Get a number from the user.
887 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 */
889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100890get_number(
891 int colon, /* allow colon to abort */
892 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 int n = 0;
895 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000896 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000898 if (mouse_used != NULL)
899 *mouse_used = FALSE;
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 /* When not printing messages, the user won't know what to type, return a
902 * zero (as if CR was hit). */
903 if (msg_silent != 0)
904 return 0;
905
906#ifdef USE_ON_FLY_SCROLL
907 dont_scroll = TRUE; /* disallow scrolling here */
908#endif
909 ++no_mapping;
910 ++allow_keys; /* no mapping here, but recognize keys */
911 for (;;)
912 {
913 windgoto(msg_row, msg_col);
914 c = safe_vgetc();
915 if (VIM_ISDIGIT(c))
916 {
917 n = n * 10 + c - '0';
918 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000919 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
922 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000923 if (typed > 0)
924 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100925 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000926 --typed;
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000930 else if (mouse_used != NULL && c == K_LEFTMOUSE)
931 {
932 *mouse_used = TRUE;
933 n = mouse_row + 1;
934 break;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 else if (n == 0 && c == ':' && colon)
937 {
938 stuffcharReadbuff(':');
939 if (!exmode_active)
940 cmdline_row = msg_row;
941 skip_redraw = TRUE; /* skip redraw once */
942 do_redraw = FALSE;
943 break;
944 }
945 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
946 break;
947 }
948 --no_mapping;
949 --allow_keys;
950 return n;
951}
952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000953/*
954 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000955 * When "mouse_used" is not NULL allow using the mouse and in that case return
956 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000957 */
958 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100959prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000960{
961 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000962 int save_cmdline_row;
963 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000964
965 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000966 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100967 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000968 else
Bram Moolenaar32526b32019-01-19 17:43:09 +0100969 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000970
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200971 // Set the state such that text can be selected/copied/pasted and we still
972 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
973 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000974 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +0000975 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000976 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200977 State = CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200978 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200979 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +0200980
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000981 i = get_number(TRUE, mouse_used);
982 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000983 {
Bram Moolenaar954bb062019-06-09 16:40:46 +0200984 // don't call wait_return() now
985 if (msg_row > 0)
986 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000987 need_wait_return = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000988 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000989 else
990 cmdline_row = save_cmdline_row;
991 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200992 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200993 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000995 return i;
996}
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100999msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
1001 long pn;
1002
1003 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1005 return;
1006
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001007 /* We don't want to overwrite another important message, but do overwrite
1008 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1009 * then "put" reports the last action. */
1010 if (keep_msg != NULL && !keep_msg_more)
1011 return;
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (n > 0)
1014 pn = n;
1015 else
1016 pn = -n;
1017
1018 if (pn > p_report)
1019 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001020 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001021 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001022 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001024 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001025 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001027 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1028 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (msg(msg_buf))
1030 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001031 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001032 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
1034 }
1035}
1036
1037/*
1038 * flush map and typeahead buffers and give a warning for an error
1039 */
1040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001041beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 if (emsg_silent == 0)
1044 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001045 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001046 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048}
1049
1050/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001051 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
1053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001054vim_beep(
1055 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001057#ifdef FEAT_EVAL
1058 called_vim_beep = TRUE;
1059#endif
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (emsg_silent == 0)
1062 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001063 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1064 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001065#ifdef ELAPSED_FUNC
1066 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001067 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001068
1069 /* Only beep once per half a second, otherwise a sequence of beeps
1070 * would freeze Vim. */
1071 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1072 {
1073 did_init = TRUE;
1074 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001076 if (p_vb
1077#ifdef FEAT_GUI
1078 /* While the GUI is starting up the termcap is set for
1079 * the GUI but the output still goes to a terminal. */
1080 && !(gui.in_use && gui.starting)
1081#endif
1082 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001083 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001084 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001085#ifdef FEAT_VTP
1086 /* No restore color information, refresh the screen. */
1087 if (has_vtp_working() != 0
1088# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001089 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001090# endif
1091 )
1092 {
1093 redraw_later(CLEAR);
1094 update_screen(0);
1095 redrawcmd();
1096 }
1097#endif
1098 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001099 else
1100 out_char(BELL);
1101#ifdef ELAPSED_FUNC
1102 }
1103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001106 /* When 'debug' contains "beep" produce a message. If we are sourcing
1107 * a script or executing a function give the user a hint where the beep
1108 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 if (vim_strchr(p_debug, 'e') != NULL)
1110 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001111 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001112 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115}
1116
1117/*
1118 * To get the "real" home directory:
1119 * - get value of $HOME
1120 * For Unix:
1121 * - go to that directory
1122 * - do mch_dirname() to get the real name of that directory.
1123 * This also works with mounts and links.
1124 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001125 * For Windows:
1126 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001129init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 char_u *var;
1132
Bram Moolenaar05159a02005-02-26 23:04:13 +00001133 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001134 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136#ifdef VMS
1137 var = mch_getenv((char_u *)"SYS$LOGIN");
1138#else
1139 var = mch_getenv((char_u *)"HOME");
1140#endif
1141
Bram Moolenaar4f974752019-02-17 17:44:42 +01001142#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001144 * Typically, $HOME is not defined on Windows, unless the user has
1145 * specifically defined it for Vim's sake. However, on Windows NT
1146 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1147 * each user. Try constructing $HOME from these.
1148 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001149 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001150 {
1151 char_u *homedrive, *homepath;
1152
1153 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1154 homepath = mch_getenv((char_u *)"HOMEPATH");
1155 if (homepath == NULL || *homepath == NUL)
1156 homepath = (char_u *)"\\";
1157 if (homedrive != NULL
1158 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1159 {
1160 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1161 if (NameBuff[0] != NUL)
1162 var = NameBuff;
1163 }
1164 }
1165
1166 if (var == NULL)
1167 var = mch_getenv((char_u *)"USERPROFILE");
1168
1169 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 * Weird but true: $HOME may contain an indirect reference to another
1171 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1172 * when $HOME is being set.
1173 */
1174 if (var != NULL && *var == '%')
1175 {
1176 char_u *p;
1177 char_u *exp;
1178
1179 p = vim_strchr(var + 1, '%');
1180 if (p != NULL)
1181 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001182 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 exp = mch_getenv(NameBuff);
1184 if (exp != NULL && *exp != NUL
1185 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1186 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001187 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 }
1191 }
1192
Bram Moolenaar48340b62017-08-29 22:08:53 +02001193 if (var != NULL && *var == NUL) /* empty is same as not set */
1194 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
Bram Moolenaar05159a02005-02-26 23:04:13 +00001196 if (enc_utf8 && var != NULL)
1197 {
1198 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001199 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001200
1201 /* Convert from active codepage to UTF-8. Other conversions are
1202 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001203 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001204 if (pp != NULL)
1205 {
1206 homedir = pp;
1207 return;
1208 }
1209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 /*
1212 * Default home dir is C:/
1213 * Best assumption we can make in such a situation.
1214 */
1215 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001216 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (var != NULL)
1220 {
1221#ifdef UNIX
1222 /*
1223 * Change to the directory and get the actual path. This resolves
1224 * links. Don't do it when we can't return.
1225 */
1226 if (mch_dirname(NameBuff, MAXPATHL) == OK
1227 && mch_chdir((char *)NameBuff) == 0)
1228 {
1229 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1230 var = IObuff;
1231 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001232 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234#endif
1235 homedir = vim_strsave(var);
1236 }
1237}
1238
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001239#if defined(EXITFREE) || defined(PROTO)
1240 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001241free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001242{
1243 vim_free(homedir);
1244}
Bram Moolenaar24305862012-08-15 14:05:05 +02001245
Bram Moolenaar24305862012-08-15 14:05:05 +02001246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001247free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001248{
1249 ga_clear_strings(&ga_users);
1250}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001251#endif
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001254 * Call expand_env() and store the result in an allocated string.
1255 * This is not very memory efficient, this expects the result to be freed
1256 * again soon.
1257 */
1258 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001259expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001260{
1261 return expand_env_save_opt(src, FALSE);
1262}
1263
1264/*
1265 * Idem, but when "one" is TRUE handle the string as one file name, only
1266 * expand "~" at the start.
1267 */
1268 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001269expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001270{
1271 char_u *p;
1272
1273 p = alloc(MAXPATHL);
1274 if (p != NULL)
1275 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1276 return p;
1277}
1278
1279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 * Expand environment variable with path name.
1281 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001282 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 * If anything fails no expansion is done and dst equals src.
1284 */
1285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001286expand_env(
1287 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1288 char_u *dst, /* where to put the result */
1289 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001291 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292}
1293
1294 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001295expand_env_esc(
1296 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1297 char_u *dst, /* where to put the result */
1298 int dstlen, /* maximum length of the result */
1299 int esc, /* escape spaces in expanded variables */
1300 int one, /* "srcp" is one file name */
1301 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001303 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 char_u *tail;
1305 int c;
1306 char_u *var;
1307 int copy_char;
1308 int mustfree; /* var was allocated, need to free it later */
1309 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001310 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001312 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001313 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001314
1315 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 --dstlen; /* leave one char space for "\," */
1317 while (*src && dstlen > 0)
1318 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001319#ifdef FEAT_EVAL
1320 /* Skip over `=expr`. */
1321 if (src[0] == '`' && src[1] == '=')
1322 {
1323 size_t len;
1324
1325 var = src;
1326 src += 2;
1327 (void)skip_expr(&src);
1328 if (*src == '`')
1329 ++src;
1330 len = src - var;
1331 if (len > (size_t)dstlen)
1332 len = dstlen;
1333 vim_strncpy(dst, var, len);
1334 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001335 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001336 continue;
1337 }
1338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001340 if ((*src == '$'
1341#ifdef VMS
1342 && at_start
1343#endif
1344 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001345#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 || *src == '%'
1347#endif
1348 || (*src == '~' && at_start))
1349 {
1350 mustfree = FALSE;
1351
1352 /*
1353 * The variable name is copied into dst temporarily, because it may
1354 * be a string in read-only memory and a NUL needs to be appended.
1355 */
1356 if (*src != '~') /* environment var */
1357 {
1358 tail = src + 1;
1359 var = dst;
1360 c = dstlen - 1;
1361
1362#ifdef UNIX
1363 /* Unix has ${var-name} type environment vars */
1364 if (*tail == '{' && !vim_isIDc('{'))
1365 {
1366 tail++; /* ignore '{' */
1367 while (c-- > 0 && *tail && *tail != '}')
1368 *var++ = *tail++;
1369 }
1370 else
1371#endif
1372 {
1373 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001374#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 || (*src == '%' && *tail != '%')
1376#endif
1377 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001381#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# ifdef UNIX
1383 if (src[1] == '{' && *tail != '}')
1384# else
1385 if (*src == '%' && *tail != '%')
1386# endif
1387 var = NULL;
1388 else
1389 {
1390# ifdef UNIX
1391 if (src[1] == '{')
1392# else
1393 if (*src == '%')
1394#endif
1395 ++tail;
1396#endif
1397 *var = NUL;
1398 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001399#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401#endif
1402 }
1403 /* home directory */
1404 else if ( src[1] == NUL
1405 || vim_ispathsep(src[1])
1406 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1407 {
1408 var = homedir;
1409 tail = src + 1;
1410 }
1411 else /* user directory */
1412 {
1413#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1414 /*
1415 * Copy ~user to dst[], so we can put a NUL after it.
1416 */
1417 tail = src;
1418 var = dst;
1419 c = dstlen - 1;
1420 while ( c-- > 0
1421 && *tail
1422 && vim_isfilec(*tail)
1423 && !vim_ispathsep(*tail))
1424 *var++ = *tail++;
1425 *var = NUL;
1426# ifdef UNIX
1427 /*
1428 * If the system supports getpwnam(), use it.
1429 * Otherwise, or if getpwnam() fails, the shell is used to
1430 * expand ~user. This is slower and may fail if the shell
1431 * does not support ~user (old versions of /bin/sh).
1432 */
1433# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1434 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001435 /* Note: memory allocated by getpwnam() is never freed.
1436 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001437 struct passwd *pw = (*dst == NUL)
1438 ? NULL : getpwnam((char *)dst + 1);
1439
1440 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442 if (var == NULL)
1443# endif
1444 {
1445 expand_T xpc;
1446
1447 ExpandInit(&xpc);
1448 xpc.xp_context = EXPAND_FILES;
1449 var = ExpandOne(&xpc, dst, NULL,
1450 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 mustfree = TRUE;
1452 }
1453
1454# else /* !UNIX, thus VMS */
1455 /*
1456 * USER_HOME is a comma-separated list of
1457 * directories to search for the user account in.
1458 */
1459 {
1460 char_u test[MAXPATHL], paths[MAXPATHL];
1461 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001462 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
1464 STRCPY(paths, USER_HOME);
1465 next_path = paths;
1466 while (*next_path)
1467 {
1468 for (path = next_path; *next_path && *next_path != ',';
1469 next_path++);
1470 if (*next_path)
1471 *next_path++ = NUL;
1472 STRCPY(test, path);
1473 STRCAT(test, "/");
1474 STRCAT(test, dst + 1);
1475 if (mch_stat(test, &st) == 0)
1476 {
1477 var = alloc(STRLEN(test) + 1);
1478 STRCPY(var, test);
1479 mustfree = TRUE;
1480 break;
1481 }
1482 }
1483 }
1484# endif /* UNIX */
1485#else
1486 /* cannot expand user's home directory, so don't try */
1487 var = NULL;
1488 tail = (char_u *)""; /* for gcc */
1489#endif /* UNIX || VMS */
1490 }
1491
1492#ifdef BACKSLASH_IN_FILENAME
1493 /* If 'shellslash' is set change backslashes to forward slashes.
1494 * Can't use slash_adjust(), p_ssl may be set temporarily. */
1495 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1496 {
1497 char_u *p = vim_strsave(var);
1498
1499 if (p != NULL)
1500 {
1501 if (mustfree)
1502 vim_free(var);
1503 var = p;
1504 mustfree = TRUE;
1505 forward_slash(var);
1506 }
1507 }
1508#endif
1509
1510 /* If "var" contains white space, escape it with a backslash.
1511 * Required for ":e ~/tt" when $HOME includes a space. */
1512 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1513 {
1514 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1515
1516 if (p != NULL)
1517 {
1518 if (mustfree)
1519 vim_free(var);
1520 var = p;
1521 mustfree = TRUE;
1522 }
1523 }
1524
1525 if (var != NULL && *var != NUL
1526 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1527 {
1528 STRCPY(dst, var);
1529 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001530 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 /* if var[] ends in a path separator and tail[] starts
1532 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001533 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1535 && dst[-1] != ':'
1536#endif
1537 && vim_ispathsep(*tail))
1538 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001539 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 src = tail;
1541 copy_char = FALSE;
1542 }
1543 if (mustfree)
1544 vim_free(var);
1545 }
1546
1547 if (copy_char) /* copy at least one char */
1548 {
1549 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001550 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001551 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1552 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
1554 at_start = FALSE;
1555 if (src[0] == '\\' && src[1] != NUL)
1556 {
1557 *dst++ = *src++;
1558 --dstlen;
1559 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001560 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001562 if (dstlen > 0)
1563 {
1564 *dst++ = *src++;
1565 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001566
Bram Moolenaar1c864092017-08-06 18:15:45 +02001567 if (startstr != NULL && src - startstr_len >= srcp
1568 && STRNCMP(src - startstr_len, startstr,
1569 startstr_len) == 0)
1570 at_start = TRUE;
1571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575 *dst = NUL;
1576}
1577
1578/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001579 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1580 * the length of "name/". Otherwise return "pend".
1581 */
1582 static char_u *
1583remove_tail(char_u *p, char_u *pend, char_u *name)
1584{
1585 int len = (int)STRLEN(name) + 1;
1586 char_u *newend = pend - len;
1587
1588 if (newend >= p
1589 && fnamencmp(newend, name, len - 1) == 0
1590 && (newend == p || after_pathsep(p, newend)))
1591 return newend;
1592 return pend;
1593}
1594
1595/*
1596 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1597 * Return NULL if not, return its name in allocated memory otherwise.
1598 */
1599 static char_u *
1600vim_version_dir(char_u *vimdir)
1601{
1602 char_u *p;
1603
1604 if (vimdir == NULL || *vimdir == NUL)
1605 return NULL;
1606 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1607 if (p != NULL && mch_isdir(p))
1608 return p;
1609 vim_free(p);
1610 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1611 if (p != NULL && mch_isdir(p))
1612 return p;
1613 vim_free(p);
1614 return NULL;
1615}
1616
1617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 * Vim's version of getenv().
1619 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001620 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001621 * "mustfree" is set to TRUE when returned is allocated, it must be
1622 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 */
1624 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001625vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001627 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 char_u *pend;
1629 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001630#ifdef MSWIN
1631 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001633 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (STRCMP(name, "HOME") == 0)
1635 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001637 // Use Wide function
1638 wn = enc_to_utf16(name, NULL);
1639 if (wn == NULL)
1640 return NULL;
1641
1642 wp = _wgetenv(wn);
1643 vim_free(wn);
1644
1645 if (wp != NULL && *wp == NUL) // empty is the same as not set
1646 wp = NULL;
1647
1648 if (wp != NULL)
1649 {
1650 p = utf16_to_enc(wp, NULL);
1651 if (p == NULL)
1652 return NULL;
1653
1654 *mustfree = TRUE;
1655 return p;
1656 }
1657#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001659 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 p = NULL;
1661
1662 if (p != NULL)
1663 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001666 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1668 if (!vimruntime && STRCMP(name, "VIM") != 0)
1669 return NULL;
1670
1671 /*
1672 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1673 * Don't do this when default_vimruntime_dir is non-empty.
1674 */
1675 if (vimruntime
1676#ifdef HAVE_PATHDEF
1677 && *default_vimruntime_dir == NUL
1678#endif
1679 )
1680 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001681#ifdef MSWIN
1682 // Use Wide function
1683 wp = _wgetenv(L"VIM");
1684 if (wp != NULL && *wp == NUL) // empty is the same as not set
1685 wp = NULL;
1686 if (wp != NULL)
1687 {
1688 char_u *q = utf16_to_enc(wp, NULL);
1689 if (q != NULL)
1690 {
1691 p = vim_version_dir(q);
1692 *mustfree = TRUE;
1693 if (p == NULL)
1694 p = q;
1695 }
1696 }
1697#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001699 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 p = NULL;
1701 if (p != NULL)
1702 {
1703 p = vim_version_dir(p);
1704 if (p != NULL)
1705 *mustfree = TRUE;
1706 else
1707 p = mch_getenv((char_u *)"VIM");
1708 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711
1712 /*
1713 * When expanding $VIM or $VIMRUNTIME fails, try using:
1714 * - the directory name from 'helpfile' (unless it contains '$')
1715 * - the executable name from argv[0]
1716 */
1717 if (p == NULL)
1718 {
1719 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1720 p = p_hf;
1721#ifdef USE_EXE_NAME
1722 /*
1723 * Use the name of the executable, obtained from argv[0].
1724 */
1725 else
1726 p = exe_name;
1727#endif
1728 if (p != NULL)
1729 {
1730 /* remove the file name */
1731 pend = gettail(p);
1732
1733 /* remove "doc/" from 'helpfile', if present */
1734 if (p == p_hf)
1735 pend = remove_tail(p, pend, (char_u *)"doc");
1736
1737#ifdef USE_EXE_NAME
1738# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001739 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (p == exe_name)
1741 {
1742 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001743 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001745 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1746 if (pend1 != pend)
1747 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001748 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001749 if (pnew != NULL)
1750 {
1751 STRNCPY(pnew, p, (pend1 - p));
1752 STRCPY(pnew + (pend1 - p), "Resources/vim");
1753 p = pnew;
1754 pend = p + STRLEN(p);
1755 }
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758# endif
1759 /* remove "src/" from exe_name, if present */
1760 if (p == exe_name)
1761 pend = remove_tail(p, pend, (char_u *)"src");
1762#endif
1763
1764 /* for $VIM, remove "runtime/" or "vim54/", if present */
1765 if (!vimruntime)
1766 {
1767 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1768 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1769 }
1770
1771 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001772 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001775#ifdef MACOS_X
1776 if (p == exe_name || p == p_hf)
1777#endif
1778 /* check that the result is a directory name */
1779 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001782 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 else
1784 {
1785#ifdef USE_EXE_NAME
1786 /* may add "/vim54" or "/runtime" if it exists */
1787 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1788 {
1789 vim_free(p);
1790 p = pend;
1791 }
1792#endif
1793 *mustfree = TRUE;
1794 }
1795 }
1796 }
1797
1798#ifdef HAVE_PATHDEF
1799 /* When there is a pathdef.c file we can use default_vim_dir and
1800 * default_vimruntime_dir */
1801 if (p == NULL)
1802 {
1803 /* Only use default_vimruntime_dir when it is not empty */
1804 if (vimruntime && *default_vimruntime_dir != NUL)
1805 {
1806 p = default_vimruntime_dir;
1807 *mustfree = FALSE;
1808 }
1809 else if (*default_vim_dir != NUL)
1810 {
1811 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1812 *mustfree = TRUE;
1813 else
1814 {
1815 p = default_vim_dir;
1816 *mustfree = FALSE;
1817 }
1818 }
1819 }
1820#endif
1821
1822 /*
1823 * Set the environment variable, so that the new value can be found fast
1824 * next time, and others can also use it (e.g. Perl).
1825 */
1826 if (p != NULL)
1827 {
1828 if (vimruntime)
1829 {
1830 vim_setenv((char_u *)"VIMRUNTIME", p);
1831 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 else
1834 {
1835 vim_setenv((char_u *)"VIM", p);
1836 didset_vim = TRUE;
1837 }
1838 }
1839 return p;
1840}
1841
Bram Moolenaar113e1072019-01-20 15:30:40 +01001842#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02001843 void
1844vim_unsetenv(char_u *var)
1845{
1846#ifdef HAVE_UNSETENV
1847 unsetenv((char *)var);
1848#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001849 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001850#endif
1851}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001852#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02001853
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 * Our portable version of setenv.
1857 */
1858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001859vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861#ifdef HAVE_SETENV
1862 mch_setenv((char *)name, (char *)val, 1);
1863#else
1864 char_u *envbuf;
1865
1866 /*
1867 * Putenv does not copy the string, it has to remain
1868 * valid. The allocated memory will never be freed.
1869 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001870 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (envbuf != NULL)
1872 {
1873 sprintf((char *)envbuf, "%s=%s", name, val);
1874 putenv((char *)envbuf);
1875 }
1876#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001877#ifdef FEAT_GETTEXT
1878 /*
1879 * When setting $VIMRUNTIME adjust the directory to find message
1880 * translations to $VIMRUNTIME/lang.
1881 */
1882 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1883 {
1884 char_u *buf = concat_str(val, (char_u *)"/lang");
1885
1886 if (buf != NULL)
1887 {
1888 bindtextdomain(VIMPACKAGE, (char *)buf);
1889 vim_free(buf);
1890 }
1891 }
1892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893}
1894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895/*
1896 * Function given to ExpandGeneric() to obtain an environment variable name.
1897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001899get_env_name(
1900 expand_T *xp UNUSED,
1901 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
Bram Moolenaard0573012017-10-28 21:11:06 +02001903# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02001905 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 */
1907 return NULL;
1908# else
1909# ifndef __WIN32__
1910 /* Borland C++ 5.2 has this in a header file. */
1911 extern char **environ;
1912# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001913# define ENVNAMELEN 100
1914 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 char_u *str;
1916 int n;
1917
1918 str = (char_u *)environ[idx];
1919 if (str == NULL)
1920 return NULL;
1921
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001922 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
1924 if (str[n] == '=' || str[n] == NUL)
1925 break;
1926 name[n] = str[n];
1927 }
1928 name[n] = NUL;
1929 return name;
1930# endif
1931}
Bram Moolenaar24305862012-08-15 14:05:05 +02001932
1933/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001934 * Add a user name to the list of users in ga_users.
1935 * Do nothing if user name is NULL or empty.
1936 */
1937 static void
1938add_user(char_u *user, int need_copy)
1939{
1940 char_u *user_copy = (user != NULL && need_copy)
1941 ? vim_strsave(user) : user;
1942
1943 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
1944 {
1945 if (need_copy)
1946 vim_free(user);
1947 return;
1948 }
1949 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
1950}
1951
1952/*
Bram Moolenaar24305862012-08-15 14:05:05 +02001953 * Find all user names for user completion.
1954 * Done only once and then cached.
1955 */
1956 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001957init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02001958{
Bram Moolenaar24305862012-08-15 14:05:05 +02001959 static int lazy_init_done = FALSE;
1960
1961 if (lazy_init_done)
1962 return;
1963
1964 lazy_init_done = TRUE;
1965 ga_init2(&ga_users, sizeof(char_u *), 20);
1966
1967# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
1968 {
Bram Moolenaar24305862012-08-15 14:05:05 +02001969 struct passwd* pw;
1970
1971 setpwent();
1972 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001973 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02001974 endpwent();
1975 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01001976# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001977 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001978 DWORD nusers = 0, ntotal = 0, i;
1979 PUSER_INFO_0 uinfo;
1980
1981 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
1982 &nusers, &ntotal, NULL) == NERR_Success)
1983 {
1984 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001985 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001986
1987 NetApiBufferFree(uinfo);
1988 }
1989 }
Bram Moolenaar24305862012-08-15 14:05:05 +02001990# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001991# if defined(HAVE_GETPWNAM)
1992 {
1993 char_u *user_env = mch_getenv((char_u *)"USER");
1994
1995 // The $USER environment variable may be a valid remote user name (NIS,
1996 // LDAP) not already listed by getpwent(), as getpwent() only lists
1997 // local user names. If $USER is not already listed, check whether it
1998 // is a valid remote user name using getpwnam() and if it is, add it to
1999 // the list of user names.
2000
2001 if (user_env != NULL && *user_env != NUL)
2002 {
2003 int i;
2004
2005 for (i = 0; i < ga_users.ga_len; i++)
2006 {
2007 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2008
2009 if (STRCMP(local_user, user_env) == 0)
2010 break;
2011 }
2012
2013 if (i == ga_users.ga_len)
2014 {
2015 struct passwd *pw = getpwnam((char *)user_env);
2016
2017 if (pw != NULL)
2018 add_user((char_u *)pw->pw_name, TRUE);
2019 }
2020 }
2021 }
2022# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002023}
2024
2025/*
2026 * Function given to ExpandGeneric() to obtain an user names.
2027 */
2028 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002029get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002030{
2031 init_users();
2032 if (idx < ga_users.ga_len)
2033 return ((char_u **)ga_users.ga_data)[idx];
2034 return NULL;
2035}
2036
2037/*
2038 * Check whether name matches a user name. Return:
2039 * 0 if name does not match any user name.
2040 * 1 if name partially matches the beginning of a user name.
2041 * 2 is name fully matches a user name.
2042 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002043 int
2044match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002045{
2046 int i;
2047 int n = (int)STRLEN(name);
2048 int result = 0;
2049
2050 init_users();
2051 for (i = 0; i < ga_users.ga_len; i++)
2052 {
2053 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2054 return 2; /* full match */
2055 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2056 result = 1; /* partial match */
2057 }
2058 return result;
2059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002062 * Concatenate two strings and return the result in allocated memory.
2063 * Returns NULL when out of memory.
2064 */
2065 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002066concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002067{
2068 char_u *dest;
2069 size_t l = STRLEN(str1);
2070
Bram Moolenaar964b3742019-05-24 18:54:09 +02002071 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002072 if (dest != NULL)
2073 {
2074 STRCPY(dest, str1);
2075 STRCPY(dest + l, str2);
2076 }
2077 return dest;
2078}
Bram Moolenaard6754642005-01-17 22:18:45 +00002079
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002081prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002083#if defined(SIGHUP) && defined(SIG_IGN)
2084 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2085 * makes Vim exit and then handling SIGHUP causes various reentrance
2086 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002087 signal(SIGHUP, SIG_IGN);
2088#endif
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090#ifdef FEAT_GUI
2091 if (gui.in_use)
2092 {
2093 gui.dying = TRUE;
2094 out_trash(); /* trash any pending output */
2095 }
2096 else
2097#endif
2098 {
2099 windgoto((int)Rows - 1, 0);
2100
2101 /*
2102 * Switch terminal mode back now, so messages end up on the "normal"
2103 * screen (if there are two screens).
2104 */
2105 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002106 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 out_flush();
2108 }
2109}
2110
2111/*
2112 * Preserve files and exit.
2113 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002114 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2115 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 */
2117 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 buf_T *buf;
2121
2122 prepare_to_exit();
2123
Bram Moolenaar4770d092006-01-12 23:22:24 +00002124 /* Setting this will prevent free() calls. That avoids calling free()
2125 * recursively when free() was invoked with a bad pointer. */
2126 really_exiting = TRUE;
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 out_str(IObuff);
2129 screen_start(); /* don't know where cursor is now */
2130 out_flush();
2131
2132 ml_close_notmod(); /* close all not-modified buffers */
2133
Bram Moolenaar29323592016-07-24 22:04:11 +02002134 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2137 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002138 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 screen_start(); /* don't know where cursor is now */
2140 out_flush();
2141 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2142 break;
2143 }
2144 }
2145
2146 ml_close_all(FALSE); /* close all memfiles, without deleting */
2147
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002148 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 getout(1);
2151}
2152
2153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 * Check for CTRL-C pressed, but only once in a while.
2155 * Should be used instead of ui_breakcheck() for functions that check for
2156 * each line in the file. Calling ui_breakcheck() each time takes too much
2157 * time, because it can be a system call.
2158 */
2159
2160#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002161# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#endif
2163
2164static int breakcheck_count = 0;
2165
2166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002167line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 if (++breakcheck_count >= BREAKCHECK_SKIP)
2170 {
2171 breakcheck_count = 0;
2172 ui_breakcheck();
2173 }
2174}
2175
2176/*
2177 * Like line_breakcheck() but check 10 times less often.
2178 */
2179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002180fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2183 {
2184 breakcheck_count = 0;
2185 ui_breakcheck();
2186 }
2187}
2188
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002189#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2190 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2191 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193#ifndef SEEK_SET
2194# define SEEK_SET 0
2195#endif
2196#ifndef SEEK_END
2197# define SEEK_END 2
2198#endif
2199
2200/*
2201 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002202 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2203 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 * Returns an allocated string, or NULL for error.
2205 */
2206 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002207get_cmd_output(
2208 char_u *cmd,
2209 char_u *infile, /* optional input file name */
2210 int flags, /* can be SHELL_SILENT */
2211 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 char_u *tempname;
2214 char_u *command;
2215 char_u *buffer = NULL;
2216 int len;
2217 int i = 0;
2218 FILE *fd;
2219
2220 if (check_restricted() || check_secure())
2221 return NULL;
2222
2223 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002224 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002226 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 return NULL;
2228 }
2229
2230 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002231 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (command == NULL)
2233 goto done;
2234
2235 /*
2236 * Call the shell to execute the command (errors are ignored).
2237 * Don't check timestamps here.
2238 */
2239 ++no_check_timestamps;
2240 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2241 --no_check_timestamps;
2242
2243 vim_free(command);
2244
2245 /*
2246 * read the names from the file into memory
2247 */
2248# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00002249 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 fd = mch_fopen((char *)tempname, "r");
2251# else
2252 fd = mch_fopen((char *)tempname, READBIN);
2253# endif
2254
2255 if (fd == NULL)
2256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002257 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 goto done;
2259 }
2260
2261 fseek(fd, 0L, SEEK_END);
2262 len = ftell(fd); /* get size of temp file */
2263 fseek(fd, 0L, SEEK_SET);
2264
2265 buffer = alloc(len + 1);
2266 if (buffer != NULL)
2267 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2268 fclose(fd);
2269 mch_remove(tempname);
2270 if (buffer == NULL)
2271 goto done;
2272#ifdef VMS
2273 len = i; /* VMS doesn't give us what we asked for... */
2274#endif
2275 if (i != len)
2276 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002277 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002278 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002280 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002281 {
2282 /* Change NUL into SOH, otherwise the string is truncated. */
2283 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002284 if (buffer[i] == NUL)
2285 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002286
Bram Moolenaar162bd912010-07-28 22:29:10 +02002287 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002288 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002289 else
2290 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
2292done:
2293 vim_free(tempname);
2294 return buffer;
2295}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002296
2297# if defined(FEAT_EVAL) || defined(PROTO)
2298
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002299 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002300get_cmd_output_as_rettv(
2301 typval_T *argvars,
2302 typval_T *rettv,
2303 int retlist)
2304{
2305 char_u *res = NULL;
2306 char_u *p;
2307 char_u *infile = NULL;
2308 int err = FALSE;
2309 FILE *fd;
2310 list_T *list = NULL;
2311 int flags = SHELL_SILENT;
2312
2313 rettv->v_type = VAR_STRING;
2314 rettv->vval.v_string = NULL;
2315 if (check_restricted() || check_secure())
2316 goto errret;
2317
2318 if (argvars[1].v_type != VAR_UNKNOWN)
2319 {
2320 /*
2321 * Write the text to a temp file, to be used for input of the shell
2322 * command.
2323 */
2324 if ((infile = vim_tempname('i', TRUE)) == NULL)
2325 {
2326 emsg(_(e_notmp));
2327 goto errret;
2328 }
2329
2330 fd = mch_fopen((char *)infile, WRITEBIN);
2331 if (fd == NULL)
2332 {
2333 semsg(_(e_notopen), infile);
2334 goto errret;
2335 }
2336 if (argvars[1].v_type == VAR_NUMBER)
2337 {
2338 linenr_T lnum;
2339 buf_T *buf;
2340
2341 buf = buflist_findnr(argvars[1].vval.v_number);
2342 if (buf == NULL)
2343 {
2344 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2345 fclose(fd);
2346 goto errret;
2347 }
2348
2349 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2350 {
2351 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2352 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2353 {
2354 err = TRUE;
2355 break;
2356 }
2357 if (putc(NL, fd) == EOF)
2358 {
2359 err = TRUE;
2360 break;
2361 }
2362 }
2363 }
2364 else if (argvars[1].v_type == VAR_LIST)
2365 {
2366 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2367 err = TRUE;
2368 }
2369 else
2370 {
2371 size_t len;
2372 char_u buf[NUMBUFLEN];
2373
2374 p = tv_get_string_buf_chk(&argvars[1], buf);
2375 if (p == NULL)
2376 {
2377 fclose(fd);
2378 goto errret; /* type error; errmsg already given */
2379 }
2380 len = STRLEN(p);
2381 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2382 err = TRUE;
2383 }
2384 if (fclose(fd) != 0)
2385 err = TRUE;
2386 if (err)
2387 {
2388 emsg(_("E677: Error writing temp file"));
2389 goto errret;
2390 }
2391 }
2392
2393 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2394 * echoes typeahead, that messes up the display. */
2395 if (!msg_silent)
2396 flags += SHELL_COOKED;
2397
2398 if (retlist)
2399 {
2400 int len;
2401 listitem_T *li;
2402 char_u *s = NULL;
2403 char_u *start;
2404 char_u *end;
2405 int i;
2406
2407 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2408 if (res == NULL)
2409 goto errret;
2410
2411 list = list_alloc();
2412 if (list == NULL)
2413 goto errret;
2414
2415 for (i = 0; i < len; ++i)
2416 {
2417 start = res + i;
2418 while (i < len && res[i] != NL)
2419 ++i;
2420 end = res + i;
2421
2422 s = alloc(end - start + 1);
2423 if (s == NULL)
2424 goto errret;
2425
2426 for (p = s; start < end; ++p, ++start)
2427 *p = *start == NUL ? NL : *start;
2428 *p = NUL;
2429
2430 li = listitem_alloc();
2431 if (li == NULL)
2432 {
2433 vim_free(s);
2434 goto errret;
2435 }
2436 li->li_tv.v_type = VAR_STRING;
2437 li->li_tv.v_lock = 0;
2438 li->li_tv.vval.v_string = s;
2439 list_append(list, li);
2440 }
2441
2442 rettv_list_set(rettv, list);
2443 list = NULL;
2444 }
2445 else
2446 {
2447 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2448#ifdef USE_CRNL
2449 /* translate <CR><NL> into <NL> */
2450 if (res != NULL)
2451 {
2452 char_u *s, *d;
2453
2454 d = res;
2455 for (s = res; *s; ++s)
2456 {
2457 if (s[0] == CAR && s[1] == NL)
2458 ++s;
2459 *d++ = *s;
2460 }
2461 *d = NUL;
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002464 rettv->vval.v_string = res;
2465 res = NULL;
2466 }
2467
2468errret:
2469 if (infile != NULL)
2470 {
2471 mch_remove(infile);
2472 vim_free(infile);
2473 }
2474 if (res != NULL)
2475 vim_free(res);
2476 if (list != NULL)
2477 list_free(list);
2478}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002481 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
2483 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002484f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002486 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487}
2488
2489/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002490 * "systemlist()" function
2491 */
2492 void
2493f_systemlist(typval_T *argvars, typval_T *rettv)
2494{
2495 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2496}
2497# endif // FEAT_EVAL
2498
2499#endif
2500
2501/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002502 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * Don't do this when still processing a command or a mapping.
2504 * Don't do this when inside a ":normal" command.
2505 */
2506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002507goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 return (p_im && stuff_empty() && typebuf_typed());
2510}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002511
2512/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002513 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002514 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2515 * - Remove any argument. E.g., "csh -f" -> "csh".
2516 * But don't allow a space in the path, so that this works:
2517 * "/usr/bin/csh --rcfile ~/.cshrc"
2518 * But don't do that for Windows, it's common to have a space in the path.
2519 */
2520 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002521get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002522{
2523 char_u *p;
2524
Bram Moolenaar4f974752019-02-17 17:44:42 +01002525#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002526 p = gettail(p_sh);
2527 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
2528#else
2529 p = skiptowhite(p_sh);
2530 if (*p == NUL)
2531 {
2532 /* No white space, use the tail. */
2533 p = vim_strsave(gettail(p_sh));
2534 }
2535 else
2536 {
2537 char_u *p1, *p2;
2538
2539 /* Find the last path separator before the space. */
2540 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002541 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002542 if (vim_ispathsep(*p2))
2543 p1 = p2 + 1;
2544 p = vim_strnsave(p1, (int)(p - p1));
2545 }
2546#endif
2547 return p;
2548}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002549
2550/*
2551 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2552 * Also check for ":\\", which MS Internet Explorer accepts, return
2553 * URL_BACKSLASH.
2554 */
2555 int
2556path_is_url(char_u *p)
2557{
2558 if (STRNCMP(p, "://", (size_t)3) == 0)
2559 return URL_SLASH;
2560 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2561 return URL_BACKSLASH;
2562 return 0;
2563}
2564
2565/*
2566 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
2567 * Return URL_BACKSLASH for "name:\\".
2568 * Return zero otherwise.
2569 */
2570 int
2571path_with_url(char_u *fname)
2572{
2573 char_u *p;
2574
2575 for (p = fname; isalpha(*p); ++p)
2576 ;
2577 return path_is_url(p);
2578}