blob: b27c527dfdce056d25f7ee0d420bada08532593f [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 Moolenaardc968e72019-11-05 21:53:20 +0100988 msg_didany = FALSE;
989 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000990 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000991 else
992 cmdline_row = save_cmdline_row;
993 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200994 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +0200995 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000997 return i;
998}
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 long pn;
1004
1005 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1007 return;
1008
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001009 /* We don't want to overwrite another important message, but do overwrite
1010 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1011 * then "put" reports the last action. */
1012 if (keep_msg != NULL && !keep_msg_more)
1013 return;
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (n > 0)
1016 pn = n;
1017 else
1018 pn = -n;
1019
1020 if (pn > p_report)
1021 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001022 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001023 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001024 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001026 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001027 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001029 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1030 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (msg(msg_buf))
1032 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001033 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001034 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 }
1037}
1038
1039/*
1040 * flush map and typeahead buffers and give a warning for an error
1041 */
1042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001043beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
1045 if (emsg_silent == 0)
1046 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001047 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001048 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050}
1051
1052/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001053 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 */
1055 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001056vim_beep(
1057 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001059#ifdef FEAT_EVAL
1060 called_vim_beep = TRUE;
1061#endif
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (emsg_silent == 0)
1064 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001065 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1066 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001067#ifdef ELAPSED_FUNC
1068 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001069 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001070
1071 /* Only beep once per half a second, otherwise a sequence of beeps
1072 * would freeze Vim. */
1073 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1074 {
1075 did_init = TRUE;
1076 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001078 if (p_vb
1079#ifdef FEAT_GUI
1080 /* While the GUI is starting up the termcap is set for
1081 * the GUI but the output still goes to a terminal. */
1082 && !(gui.in_use && gui.starting)
1083#endif
1084 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001085 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001086 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001087#ifdef FEAT_VTP
1088 /* No restore color information, refresh the screen. */
1089 if (has_vtp_working() != 0
1090# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001091 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001092# endif
1093 )
1094 {
1095 redraw_later(CLEAR);
1096 update_screen(0);
1097 redrawcmd();
1098 }
1099#endif
1100 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001101 else
1102 out_char(BELL);
1103#ifdef ELAPSED_FUNC
1104 }
1105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001108 /* When 'debug' contains "beep" produce a message. If we are sourcing
1109 * a script or executing a function give the user a hint where the beep
1110 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 if (vim_strchr(p_debug, 'e') != NULL)
1112 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001113 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001114 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 }
1117}
1118
1119/*
1120 * To get the "real" home directory:
1121 * - get value of $HOME
1122 * For Unix:
1123 * - go to that directory
1124 * - do mch_dirname() to get the real name of that directory.
1125 * This also works with mounts and links.
1126 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001127 * For Windows:
1128 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001131init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 char_u *var;
1134
Bram Moolenaar05159a02005-02-26 23:04:13 +00001135 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001136 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef VMS
1139 var = mch_getenv((char_u *)"SYS$LOGIN");
1140#else
1141 var = mch_getenv((char_u *)"HOME");
1142#endif
1143
Bram Moolenaar4f974752019-02-17 17:44:42 +01001144#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001146 * Typically, $HOME is not defined on Windows, unless the user has
1147 * specifically defined it for Vim's sake. However, on Windows NT
1148 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1149 * each user. Try constructing $HOME from these.
1150 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001151 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001152 {
1153 char_u *homedrive, *homepath;
1154
1155 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1156 homepath = mch_getenv((char_u *)"HOMEPATH");
1157 if (homepath == NULL || *homepath == NUL)
1158 homepath = (char_u *)"\\";
1159 if (homedrive != NULL
1160 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1161 {
1162 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1163 if (NameBuff[0] != NUL)
1164 var = NameBuff;
1165 }
1166 }
1167
1168 if (var == NULL)
1169 var = mch_getenv((char_u *)"USERPROFILE");
1170
1171 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 * Weird but true: $HOME may contain an indirect reference to another
1173 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1174 * when $HOME is being set.
1175 */
1176 if (var != NULL && *var == '%')
1177 {
1178 char_u *p;
1179 char_u *exp;
1180
1181 p = vim_strchr(var + 1, '%');
1182 if (p != NULL)
1183 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001184 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 exp = mch_getenv(NameBuff);
1186 if (exp != NULL && *exp != NUL
1187 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1188 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001189 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 }
1193 }
1194
Bram Moolenaar48340b62017-08-29 22:08:53 +02001195 if (var != NULL && *var == NUL) /* empty is same as not set */
1196 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Bram Moolenaar05159a02005-02-26 23:04:13 +00001198 if (enc_utf8 && var != NULL)
1199 {
1200 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001201 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001202
1203 /* Convert from active codepage to UTF-8. Other conversions are
1204 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001205 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001206 if (pp != NULL)
1207 {
1208 homedir = pp;
1209 return;
1210 }
1211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 /*
1214 * Default home dir is C:/
1215 * Best assumption we can make in such a situation.
1216 */
1217 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001218 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (var != NULL)
1222 {
1223#ifdef UNIX
1224 /*
1225 * Change to the directory and get the actual path. This resolves
1226 * links. Don't do it when we can't return.
1227 */
1228 if (mch_dirname(NameBuff, MAXPATHL) == OK
1229 && mch_chdir((char *)NameBuff) == 0)
1230 {
1231 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1232 var = IObuff;
1233 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001234 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236#endif
1237 homedir = vim_strsave(var);
1238 }
1239}
1240
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001241#if defined(EXITFREE) || defined(PROTO)
1242 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001243free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001244{
1245 vim_free(homedir);
1246}
Bram Moolenaar24305862012-08-15 14:05:05 +02001247
Bram Moolenaar24305862012-08-15 14:05:05 +02001248 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001249free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001250{
1251 ga_clear_strings(&ga_users);
1252}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001253#endif
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001256 * Call expand_env() and store the result in an allocated string.
1257 * This is not very memory efficient, this expects the result to be freed
1258 * again soon.
1259 */
1260 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001261expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001262{
1263 return expand_env_save_opt(src, FALSE);
1264}
1265
1266/*
1267 * Idem, but when "one" is TRUE handle the string as one file name, only
1268 * expand "~" at the start.
1269 */
1270 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001271expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001272{
1273 char_u *p;
1274
1275 p = alloc(MAXPATHL);
1276 if (p != NULL)
1277 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1278 return p;
1279}
1280
1281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 * Expand environment variable with path name.
1283 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001284 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 * If anything fails no expansion is done and dst equals src.
1286 */
1287 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001288expand_env(
1289 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1290 char_u *dst, /* where to put the result */
1291 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001293 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294}
1295
1296 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001297expand_env_esc(
1298 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1299 char_u *dst, /* where to put the result */
1300 int dstlen, /* maximum length of the result */
1301 int esc, /* escape spaces in expanded variables */
1302 int one, /* "srcp" is one file name */
1303 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001305 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 char_u *tail;
1307 int c;
1308 char_u *var;
1309 int copy_char;
1310 int mustfree; /* var was allocated, need to free it later */
1311 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001312 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001314 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001315 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001316
1317 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 --dstlen; /* leave one char space for "\," */
1319 while (*src && dstlen > 0)
1320 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001321#ifdef FEAT_EVAL
1322 /* Skip over `=expr`. */
1323 if (src[0] == '`' && src[1] == '=')
1324 {
1325 size_t len;
1326
1327 var = src;
1328 src += 2;
1329 (void)skip_expr(&src);
1330 if (*src == '`')
1331 ++src;
1332 len = src - var;
1333 if (len > (size_t)dstlen)
1334 len = dstlen;
1335 vim_strncpy(dst, var, len);
1336 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001337 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001338 continue;
1339 }
1340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001342 if ((*src == '$'
1343#ifdef VMS
1344 && at_start
1345#endif
1346 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001347#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 || *src == '%'
1349#endif
1350 || (*src == '~' && at_start))
1351 {
1352 mustfree = FALSE;
1353
1354 /*
1355 * The variable name is copied into dst temporarily, because it may
1356 * be a string in read-only memory and a NUL needs to be appended.
1357 */
1358 if (*src != '~') /* environment var */
1359 {
1360 tail = src + 1;
1361 var = dst;
1362 c = dstlen - 1;
1363
1364#ifdef UNIX
1365 /* Unix has ${var-name} type environment vars */
1366 if (*tail == '{' && !vim_isIDc('{'))
1367 {
1368 tail++; /* ignore '{' */
1369 while (c-- > 0 && *tail && *tail != '}')
1370 *var++ = *tail++;
1371 }
1372 else
1373#endif
1374 {
1375 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001376#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 || (*src == '%' && *tail != '%')
1378#endif
1379 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001383#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384# ifdef UNIX
1385 if (src[1] == '{' && *tail != '}')
1386# else
1387 if (*src == '%' && *tail != '%')
1388# endif
1389 var = NULL;
1390 else
1391 {
1392# ifdef UNIX
1393 if (src[1] == '{')
1394# else
1395 if (*src == '%')
1396#endif
1397 ++tail;
1398#endif
1399 *var = NUL;
1400 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001401#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403#endif
1404 }
1405 /* home directory */
1406 else if ( src[1] == NUL
1407 || vim_ispathsep(src[1])
1408 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1409 {
1410 var = homedir;
1411 tail = src + 1;
1412 }
1413 else /* user directory */
1414 {
1415#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1416 /*
1417 * Copy ~user to dst[], so we can put a NUL after it.
1418 */
1419 tail = src;
1420 var = dst;
1421 c = dstlen - 1;
1422 while ( c-- > 0
1423 && *tail
1424 && vim_isfilec(*tail)
1425 && !vim_ispathsep(*tail))
1426 *var++ = *tail++;
1427 *var = NUL;
1428# ifdef UNIX
1429 /*
1430 * If the system supports getpwnam(), use it.
1431 * Otherwise, or if getpwnam() fails, the shell is used to
1432 * expand ~user. This is slower and may fail if the shell
1433 * does not support ~user (old versions of /bin/sh).
1434 */
1435# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1436 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001437 /* Note: memory allocated by getpwnam() is never freed.
1438 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001439 struct passwd *pw = (*dst == NUL)
1440 ? NULL : getpwnam((char *)dst + 1);
1441
1442 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444 if (var == NULL)
1445# endif
1446 {
1447 expand_T xpc;
1448
1449 ExpandInit(&xpc);
1450 xpc.xp_context = EXPAND_FILES;
1451 var = ExpandOne(&xpc, dst, NULL,
1452 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 mustfree = TRUE;
1454 }
1455
1456# else /* !UNIX, thus VMS */
1457 /*
1458 * USER_HOME is a comma-separated list of
1459 * directories to search for the user account in.
1460 */
1461 {
1462 char_u test[MAXPATHL], paths[MAXPATHL];
1463 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001464 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466 STRCPY(paths, USER_HOME);
1467 next_path = paths;
1468 while (*next_path)
1469 {
1470 for (path = next_path; *next_path && *next_path != ',';
1471 next_path++);
1472 if (*next_path)
1473 *next_path++ = NUL;
1474 STRCPY(test, path);
1475 STRCAT(test, "/");
1476 STRCAT(test, dst + 1);
1477 if (mch_stat(test, &st) == 0)
1478 {
1479 var = alloc(STRLEN(test) + 1);
1480 STRCPY(var, test);
1481 mustfree = TRUE;
1482 break;
1483 }
1484 }
1485 }
1486# endif /* UNIX */
1487#else
1488 /* cannot expand user's home directory, so don't try */
1489 var = NULL;
1490 tail = (char_u *)""; /* for gcc */
1491#endif /* UNIX || VMS */
1492 }
1493
1494#ifdef BACKSLASH_IN_FILENAME
1495 /* If 'shellslash' is set change backslashes to forward slashes.
1496 * Can't use slash_adjust(), p_ssl may be set temporarily. */
1497 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1498 {
1499 char_u *p = vim_strsave(var);
1500
1501 if (p != NULL)
1502 {
1503 if (mustfree)
1504 vim_free(var);
1505 var = p;
1506 mustfree = TRUE;
1507 forward_slash(var);
1508 }
1509 }
1510#endif
1511
1512 /* If "var" contains white space, escape it with a backslash.
1513 * Required for ":e ~/tt" when $HOME includes a space. */
1514 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1515 {
1516 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1517
1518 if (p != NULL)
1519 {
1520 if (mustfree)
1521 vim_free(var);
1522 var = p;
1523 mustfree = TRUE;
1524 }
1525 }
1526
1527 if (var != NULL && *var != NUL
1528 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1529 {
1530 STRCPY(dst, var);
1531 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001532 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 /* if var[] ends in a path separator and tail[] starts
1534 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001535 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
1537 && dst[-1] != ':'
1538#endif
1539 && vim_ispathsep(*tail))
1540 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001541 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 src = tail;
1543 copy_char = FALSE;
1544 }
1545 if (mustfree)
1546 vim_free(var);
1547 }
1548
1549 if (copy_char) /* copy at least one char */
1550 {
1551 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001552 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001553 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1554 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
1556 at_start = FALSE;
1557 if (src[0] == '\\' && src[1] != NUL)
1558 {
1559 *dst++ = *src++;
1560 --dstlen;
1561 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001562 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001564 if (dstlen > 0)
1565 {
1566 *dst++ = *src++;
1567 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001568
Bram Moolenaar1c864092017-08-06 18:15:45 +02001569 if (startstr != NULL && src - startstr_len >= srcp
1570 && STRNCMP(src - startstr_len, startstr,
1571 startstr_len) == 0)
1572 at_start = TRUE;
1573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
1577 *dst = NUL;
1578}
1579
1580/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001581 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1582 * the length of "name/". Otherwise return "pend".
1583 */
1584 static char_u *
1585remove_tail(char_u *p, char_u *pend, char_u *name)
1586{
1587 int len = (int)STRLEN(name) + 1;
1588 char_u *newend = pend - len;
1589
1590 if (newend >= p
1591 && fnamencmp(newend, name, len - 1) == 0
1592 && (newend == p || after_pathsep(p, newend)))
1593 return newend;
1594 return pend;
1595}
1596
1597/*
1598 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1599 * Return NULL if not, return its name in allocated memory otherwise.
1600 */
1601 static char_u *
1602vim_version_dir(char_u *vimdir)
1603{
1604 char_u *p;
1605
1606 if (vimdir == NULL || *vimdir == NUL)
1607 return NULL;
1608 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1609 if (p != NULL && mch_isdir(p))
1610 return p;
1611 vim_free(p);
1612 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1613 if (p != NULL && mch_isdir(p))
1614 return p;
1615 vim_free(p);
1616 return NULL;
1617}
1618
1619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 * Vim's version of getenv().
1621 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001622 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02001623 * "mustfree" is set to TRUE when returned is allocated, it must be
1624 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 */
1626 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001627vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001629 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 char_u *pend;
1631 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001632#ifdef MSWIN
1633 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001635 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (STRCMP(name, "HOME") == 0)
1637 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001639 // Use Wide function
1640 wn = enc_to_utf16(name, NULL);
1641 if (wn == NULL)
1642 return NULL;
1643
1644 wp = _wgetenv(wn);
1645 vim_free(wn);
1646
1647 if (wp != NULL && *wp == NUL) // empty is the same as not set
1648 wp = NULL;
1649
1650 if (wp != NULL)
1651 {
1652 p = utf16_to_enc(wp, NULL);
1653 if (p == NULL)
1654 return NULL;
1655
1656 *mustfree = TRUE;
1657 return p;
1658 }
1659#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001661 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 p = NULL;
1663
1664 if (p != NULL)
1665 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001668 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1670 if (!vimruntime && STRCMP(name, "VIM") != 0)
1671 return NULL;
1672
1673 /*
1674 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1675 * Don't do this when default_vimruntime_dir is non-empty.
1676 */
1677 if (vimruntime
1678#ifdef HAVE_PATHDEF
1679 && *default_vimruntime_dir == NUL
1680#endif
1681 )
1682 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001683#ifdef MSWIN
1684 // Use Wide function
1685 wp = _wgetenv(L"VIM");
1686 if (wp != NULL && *wp == NUL) // empty is the same as not set
1687 wp = NULL;
1688 if (wp != NULL)
1689 {
1690 char_u *q = utf16_to_enc(wp, NULL);
1691 if (q != NULL)
1692 {
1693 p = vim_version_dir(q);
1694 *mustfree = TRUE;
1695 if (p == NULL)
1696 p = q;
1697 }
1698 }
1699#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001701 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 p = NULL;
1703 if (p != NULL)
1704 {
1705 p = vim_version_dir(p);
1706 if (p != NULL)
1707 *mustfree = TRUE;
1708 else
1709 p = mch_getenv((char_u *)"VIM");
1710 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713
1714 /*
1715 * When expanding $VIM or $VIMRUNTIME fails, try using:
1716 * - the directory name from 'helpfile' (unless it contains '$')
1717 * - the executable name from argv[0]
1718 */
1719 if (p == NULL)
1720 {
1721 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1722 p = p_hf;
1723#ifdef USE_EXE_NAME
1724 /*
1725 * Use the name of the executable, obtained from argv[0].
1726 */
1727 else
1728 p = exe_name;
1729#endif
1730 if (p != NULL)
1731 {
1732 /* remove the file name */
1733 pend = gettail(p);
1734
1735 /* remove "doc/" from 'helpfile', if present */
1736 if (p == p_hf)
1737 pend = remove_tail(p, pend, (char_u *)"doc");
1738
1739#ifdef USE_EXE_NAME
1740# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001741 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (p == exe_name)
1743 {
1744 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001745 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001747 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1748 if (pend1 != pend)
1749 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001750 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001751 if (pnew != NULL)
1752 {
1753 STRNCPY(pnew, p, (pend1 - p));
1754 STRCPY(pnew + (pend1 - p), "Resources/vim");
1755 p = pnew;
1756 pend = p + STRLEN(p);
1757 }
1758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760# endif
1761 /* remove "src/" from exe_name, if present */
1762 if (p == exe_name)
1763 pend = remove_tail(p, pend, (char_u *)"src");
1764#endif
1765
1766 /* for $VIM, remove "runtime/" or "vim54/", if present */
1767 if (!vimruntime)
1768 {
1769 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1770 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1771 }
1772
1773 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001774 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001777#ifdef MACOS_X
1778 if (p == exe_name || p == p_hf)
1779#endif
1780 /* check that the result is a directory name */
1781 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001784 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 else
1786 {
1787#ifdef USE_EXE_NAME
1788 /* may add "/vim54" or "/runtime" if it exists */
1789 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1790 {
1791 vim_free(p);
1792 p = pend;
1793 }
1794#endif
1795 *mustfree = TRUE;
1796 }
1797 }
1798 }
1799
1800#ifdef HAVE_PATHDEF
1801 /* When there is a pathdef.c file we can use default_vim_dir and
1802 * default_vimruntime_dir */
1803 if (p == NULL)
1804 {
1805 /* Only use default_vimruntime_dir when it is not empty */
1806 if (vimruntime && *default_vimruntime_dir != NUL)
1807 {
1808 p = default_vimruntime_dir;
1809 *mustfree = FALSE;
1810 }
1811 else if (*default_vim_dir != NUL)
1812 {
1813 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1814 *mustfree = TRUE;
1815 else
1816 {
1817 p = default_vim_dir;
1818 *mustfree = FALSE;
1819 }
1820 }
1821 }
1822#endif
1823
1824 /*
1825 * Set the environment variable, so that the new value can be found fast
1826 * next time, and others can also use it (e.g. Perl).
1827 */
1828 if (p != NULL)
1829 {
1830 if (vimruntime)
1831 {
1832 vim_setenv((char_u *)"VIMRUNTIME", p);
1833 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
1835 else
1836 {
1837 vim_setenv((char_u *)"VIM", p);
1838 didset_vim = TRUE;
1839 }
1840 }
1841 return p;
1842}
1843
Bram Moolenaar113e1072019-01-20 15:30:40 +01001844#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02001845 void
1846vim_unsetenv(char_u *var)
1847{
1848#ifdef HAVE_UNSETENV
1849 unsetenv((char *)var);
1850#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001851 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001852#endif
1853}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001854#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02001855
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 * Our portable version of setenv.
1859 */
1860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001861vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862{
1863#ifdef HAVE_SETENV
1864 mch_setenv((char *)name, (char *)val, 1);
1865#else
1866 char_u *envbuf;
1867
1868 /*
1869 * Putenv does not copy the string, it has to remain
1870 * valid. The allocated memory will never be freed.
1871 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001872 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (envbuf != NULL)
1874 {
1875 sprintf((char *)envbuf, "%s=%s", name, val);
1876 putenv((char *)envbuf);
1877 }
1878#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01001879#ifdef FEAT_GETTEXT
1880 /*
1881 * When setting $VIMRUNTIME adjust the directory to find message
1882 * translations to $VIMRUNTIME/lang.
1883 */
1884 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
1885 {
1886 char_u *buf = concat_str(val, (char_u *)"/lang");
1887
1888 if (buf != NULL)
1889 {
1890 bindtextdomain(VIMPACKAGE, (char *)buf);
1891 vim_free(buf);
1892 }
1893 }
1894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895}
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/*
1898 * Function given to ExpandGeneric() to obtain an environment variable name.
1899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001901get_env_name(
1902 expand_T *xp UNUSED,
1903 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
Bram Moolenaard0573012017-10-28 21:11:06 +02001905# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02001907 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 */
1909 return NULL;
1910# else
1911# ifndef __WIN32__
1912 /* Borland C++ 5.2 has this in a header file. */
1913 extern char **environ;
1914# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001915# define ENVNAMELEN 100
1916 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 char_u *str;
1918 int n;
1919
1920 str = (char_u *)environ[idx];
1921 if (str == NULL)
1922 return NULL;
1923
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001924 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 if (str[n] == '=' || str[n] == NUL)
1927 break;
1928 name[n] = str[n];
1929 }
1930 name[n] = NUL;
1931 return name;
1932# endif
1933}
Bram Moolenaar24305862012-08-15 14:05:05 +02001934
1935/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001936 * Add a user name to the list of users in ga_users.
1937 * Do nothing if user name is NULL or empty.
1938 */
1939 static void
1940add_user(char_u *user, int need_copy)
1941{
1942 char_u *user_copy = (user != NULL && need_copy)
1943 ? vim_strsave(user) : user;
1944
1945 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
1946 {
1947 if (need_copy)
1948 vim_free(user);
1949 return;
1950 }
1951 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
1952}
1953
1954/*
Bram Moolenaar24305862012-08-15 14:05:05 +02001955 * Find all user names for user completion.
1956 * Done only once and then cached.
1957 */
1958 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001959init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02001960{
Bram Moolenaar24305862012-08-15 14:05:05 +02001961 static int lazy_init_done = FALSE;
1962
1963 if (lazy_init_done)
1964 return;
1965
1966 lazy_init_done = TRUE;
1967 ga_init2(&ga_users, sizeof(char_u *), 20);
1968
1969# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
1970 {
Bram Moolenaar24305862012-08-15 14:05:05 +02001971 struct passwd* pw;
1972
1973 setpwent();
1974 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001975 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02001976 endpwent();
1977 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01001978# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001979 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001980 DWORD nusers = 0, ntotal = 0, i;
1981 PUSER_INFO_0 uinfo;
1982
1983 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
1984 &nusers, &ntotal, NULL) == NERR_Success)
1985 {
1986 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001987 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02001988
1989 NetApiBufferFree(uinfo);
1990 }
1991 }
Bram Moolenaar24305862012-08-15 14:05:05 +02001992# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02001993# if defined(HAVE_GETPWNAM)
1994 {
1995 char_u *user_env = mch_getenv((char_u *)"USER");
1996
1997 // The $USER environment variable may be a valid remote user name (NIS,
1998 // LDAP) not already listed by getpwent(), as getpwent() only lists
1999 // local user names. If $USER is not already listed, check whether it
2000 // is a valid remote user name using getpwnam() and if it is, add it to
2001 // the list of user names.
2002
2003 if (user_env != NULL && *user_env != NUL)
2004 {
2005 int i;
2006
2007 for (i = 0; i < ga_users.ga_len; i++)
2008 {
2009 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2010
2011 if (STRCMP(local_user, user_env) == 0)
2012 break;
2013 }
2014
2015 if (i == ga_users.ga_len)
2016 {
2017 struct passwd *pw = getpwnam((char *)user_env);
2018
2019 if (pw != NULL)
2020 add_user((char_u *)pw->pw_name, TRUE);
2021 }
2022 }
2023 }
2024# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002025}
2026
2027/*
2028 * Function given to ExpandGeneric() to obtain an user names.
2029 */
2030 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002032{
2033 init_users();
2034 if (idx < ga_users.ga_len)
2035 return ((char_u **)ga_users.ga_data)[idx];
2036 return NULL;
2037}
2038
2039/*
2040 * Check whether name matches a user name. Return:
2041 * 0 if name does not match any user name.
2042 * 1 if name partially matches the beginning of a user name.
2043 * 2 is name fully matches a user name.
2044 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002045 int
2046match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002047{
2048 int i;
2049 int n = (int)STRLEN(name);
2050 int result = 0;
2051
2052 init_users();
2053 for (i = 0; i < ga_users.ga_len; i++)
2054 {
2055 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2056 return 2; /* full match */
2057 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2058 result = 1; /* partial match */
2059 }
2060 return result;
2061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002064 * Concatenate two strings and return the result in allocated memory.
2065 * Returns NULL when out of memory.
2066 */
2067 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002068concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002069{
2070 char_u *dest;
2071 size_t l = STRLEN(str1);
2072
Bram Moolenaar964b3742019-05-24 18:54:09 +02002073 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002074 if (dest != NULL)
2075 {
2076 STRCPY(dest, str1);
2077 STRCPY(dest + l, str2);
2078 }
2079 return dest;
2080}
Bram Moolenaard6754642005-01-17 22:18:45 +00002081
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002083prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002085#if defined(SIGHUP) && defined(SIG_IGN)
2086 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2087 * makes Vim exit and then handling SIGHUP causes various reentrance
2088 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002089 signal(SIGHUP, SIG_IGN);
2090#endif
2091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092#ifdef FEAT_GUI
2093 if (gui.in_use)
2094 {
2095 gui.dying = TRUE;
2096 out_trash(); /* trash any pending output */
2097 }
2098 else
2099#endif
2100 {
2101 windgoto((int)Rows - 1, 0);
2102
2103 /*
2104 * Switch terminal mode back now, so messages end up on the "normal"
2105 * screen (if there are two screens).
2106 */
2107 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002108 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 out_flush();
2110 }
2111}
2112
2113/*
2114 * Preserve files and exit.
2115 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002116 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2117 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
2119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002120preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
2122 buf_T *buf;
2123
2124 prepare_to_exit();
2125
Bram Moolenaar4770d092006-01-12 23:22:24 +00002126 /* Setting this will prevent free() calls. That avoids calling free()
2127 * recursively when free() was invoked with a bad pointer. */
2128 really_exiting = TRUE;
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 out_str(IObuff);
2131 screen_start(); /* don't know where cursor is now */
2132 out_flush();
2133
2134 ml_close_notmod(); /* close all not-modified buffers */
2135
Bram Moolenaar29323592016-07-24 22:04:11 +02002136 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2139 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002140 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 screen_start(); /* don't know where cursor is now */
2142 out_flush();
2143 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2144 break;
2145 }
2146 }
2147
2148 ml_close_all(FALSE); /* close all memfiles, without deleting */
2149
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002150 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 getout(1);
2153}
2154
2155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 * Check for CTRL-C pressed, but only once in a while.
2157 * Should be used instead of ui_breakcheck() for functions that check for
2158 * each line in the file. Calling ui_breakcheck() each time takes too much
2159 * time, because it can be a system call.
2160 */
2161
2162#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002163# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#endif
2165
2166static int breakcheck_count = 0;
2167
2168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002169line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
2171 if (++breakcheck_count >= BREAKCHECK_SKIP)
2172 {
2173 breakcheck_count = 0;
2174 ui_breakcheck();
2175 }
2176}
2177
2178/*
2179 * Like line_breakcheck() but check 10 times less often.
2180 */
2181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002182fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
2184 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2185 {
2186 breakcheck_count = 0;
2187 ui_breakcheck();
2188 }
2189}
2190
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002191#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2192 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2193 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195#ifndef SEEK_SET
2196# define SEEK_SET 0
2197#endif
2198#ifndef SEEK_END
2199# define SEEK_END 2
2200#endif
2201
2202/*
2203 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002204 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2205 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 * Returns an allocated string, or NULL for error.
2207 */
2208 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002209get_cmd_output(
2210 char_u *cmd,
2211 char_u *infile, /* optional input file name */
2212 int flags, /* can be SHELL_SILENT */
2213 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 char_u *tempname;
2216 char_u *command;
2217 char_u *buffer = NULL;
2218 int len;
2219 int i = 0;
2220 FILE *fd;
2221
2222 if (check_restricted() || check_secure())
2223 return NULL;
2224
2225 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002226 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002228 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 return NULL;
2230 }
2231
2232 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002233 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (command == NULL)
2235 goto done;
2236
2237 /*
2238 * Call the shell to execute the command (errors are ignored).
2239 * Don't check timestamps here.
2240 */
2241 ++no_check_timestamps;
2242 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2243 --no_check_timestamps;
2244
2245 vim_free(command);
2246
2247 /*
2248 * read the names from the file into memory
2249 */
2250# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00002251 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 fd = mch_fopen((char *)tempname, "r");
2253# else
2254 fd = mch_fopen((char *)tempname, READBIN);
2255# endif
2256
2257 if (fd == NULL)
2258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002259 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 goto done;
2261 }
2262
2263 fseek(fd, 0L, SEEK_END);
2264 len = ftell(fd); /* get size of temp file */
2265 fseek(fd, 0L, SEEK_SET);
2266
2267 buffer = alloc(len + 1);
2268 if (buffer != NULL)
2269 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2270 fclose(fd);
2271 mch_remove(tempname);
2272 if (buffer == NULL)
2273 goto done;
2274#ifdef VMS
2275 len = i; /* VMS doesn't give us what we asked for... */
2276#endif
2277 if (i != len)
2278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002279 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002280 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002282 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002283 {
2284 /* Change NUL into SOH, otherwise the string is truncated. */
2285 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002286 if (buffer[i] == NUL)
2287 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002288
Bram Moolenaar162bd912010-07-28 22:29:10 +02002289 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002290 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002291 else
2292 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294done:
2295 vim_free(tempname);
2296 return buffer;
2297}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002298
2299# if defined(FEAT_EVAL) || defined(PROTO)
2300
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002301 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002302get_cmd_output_as_rettv(
2303 typval_T *argvars,
2304 typval_T *rettv,
2305 int retlist)
2306{
2307 char_u *res = NULL;
2308 char_u *p;
2309 char_u *infile = NULL;
2310 int err = FALSE;
2311 FILE *fd;
2312 list_T *list = NULL;
2313 int flags = SHELL_SILENT;
2314
2315 rettv->v_type = VAR_STRING;
2316 rettv->vval.v_string = NULL;
2317 if (check_restricted() || check_secure())
2318 goto errret;
2319
2320 if (argvars[1].v_type != VAR_UNKNOWN)
2321 {
2322 /*
2323 * Write the text to a temp file, to be used for input of the shell
2324 * command.
2325 */
2326 if ((infile = vim_tempname('i', TRUE)) == NULL)
2327 {
2328 emsg(_(e_notmp));
2329 goto errret;
2330 }
2331
2332 fd = mch_fopen((char *)infile, WRITEBIN);
2333 if (fd == NULL)
2334 {
2335 semsg(_(e_notopen), infile);
2336 goto errret;
2337 }
2338 if (argvars[1].v_type == VAR_NUMBER)
2339 {
2340 linenr_T lnum;
2341 buf_T *buf;
2342
2343 buf = buflist_findnr(argvars[1].vval.v_number);
2344 if (buf == NULL)
2345 {
2346 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2347 fclose(fd);
2348 goto errret;
2349 }
2350
2351 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2352 {
2353 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2354 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2355 {
2356 err = TRUE;
2357 break;
2358 }
2359 if (putc(NL, fd) == EOF)
2360 {
2361 err = TRUE;
2362 break;
2363 }
2364 }
2365 }
2366 else if (argvars[1].v_type == VAR_LIST)
2367 {
2368 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2369 err = TRUE;
2370 }
2371 else
2372 {
2373 size_t len;
2374 char_u buf[NUMBUFLEN];
2375
2376 p = tv_get_string_buf_chk(&argvars[1], buf);
2377 if (p == NULL)
2378 {
2379 fclose(fd);
2380 goto errret; /* type error; errmsg already given */
2381 }
2382 len = STRLEN(p);
2383 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2384 err = TRUE;
2385 }
2386 if (fclose(fd) != 0)
2387 err = TRUE;
2388 if (err)
2389 {
2390 emsg(_("E677: Error writing temp file"));
2391 goto errret;
2392 }
2393 }
2394
2395 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2396 * echoes typeahead, that messes up the display. */
2397 if (!msg_silent)
2398 flags += SHELL_COOKED;
2399
2400 if (retlist)
2401 {
2402 int len;
2403 listitem_T *li;
2404 char_u *s = NULL;
2405 char_u *start;
2406 char_u *end;
2407 int i;
2408
2409 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2410 if (res == NULL)
2411 goto errret;
2412
2413 list = list_alloc();
2414 if (list == NULL)
2415 goto errret;
2416
2417 for (i = 0; i < len; ++i)
2418 {
2419 start = res + i;
2420 while (i < len && res[i] != NL)
2421 ++i;
2422 end = res + i;
2423
2424 s = alloc(end - start + 1);
2425 if (s == NULL)
2426 goto errret;
2427
2428 for (p = s; start < end; ++p, ++start)
2429 *p = *start == NUL ? NL : *start;
2430 *p = NUL;
2431
2432 li = listitem_alloc();
2433 if (li == NULL)
2434 {
2435 vim_free(s);
2436 goto errret;
2437 }
2438 li->li_tv.v_type = VAR_STRING;
2439 li->li_tv.v_lock = 0;
2440 li->li_tv.vval.v_string = s;
2441 list_append(list, li);
2442 }
2443
2444 rettv_list_set(rettv, list);
2445 list = NULL;
2446 }
2447 else
2448 {
2449 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2450#ifdef USE_CRNL
2451 /* translate <CR><NL> into <NL> */
2452 if (res != NULL)
2453 {
2454 char_u *s, *d;
2455
2456 d = res;
2457 for (s = res; *s; ++s)
2458 {
2459 if (s[0] == CAR && s[1] == NL)
2460 ++s;
2461 *d++ = *s;
2462 }
2463 *d = NUL;
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002466 rettv->vval.v_string = res;
2467 res = NULL;
2468 }
2469
2470errret:
2471 if (infile != NULL)
2472 {
2473 mch_remove(infile);
2474 vim_free(infile);
2475 }
2476 if (res != NULL)
2477 vim_free(res);
2478 if (list != NULL)
2479 list_free(list);
2480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
2482/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002483 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
2485 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002486f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002488 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489}
2490
2491/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002492 * "systemlist()" function
2493 */
2494 void
2495f_systemlist(typval_T *argvars, typval_T *rettv)
2496{
2497 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2498}
2499# endif // FEAT_EVAL
2500
2501#endif
2502
2503/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002504 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * Don't do this when still processing a command or a mapping.
2506 * Don't do this when inside a ":normal" command.
2507 */
2508 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002509goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 return (p_im && stuff_empty() && typebuf_typed());
2512}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002513
2514/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002515 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002516 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2517 * - Remove any argument. E.g., "csh -f" -> "csh".
2518 * But don't allow a space in the path, so that this works:
2519 * "/usr/bin/csh --rcfile ~/.cshrc"
2520 * But don't do that for Windows, it's common to have a space in the path.
2521 */
2522 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002523get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002524{
2525 char_u *p;
2526
Bram Moolenaar4f974752019-02-17 17:44:42 +01002527#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002528 p = gettail(p_sh);
2529 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
2530#else
2531 p = skiptowhite(p_sh);
2532 if (*p == NUL)
2533 {
2534 /* No white space, use the tail. */
2535 p = vim_strsave(gettail(p_sh));
2536 }
2537 else
2538 {
2539 char_u *p1, *p2;
2540
2541 /* Find the last path separator before the space. */
2542 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002543 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002544 if (vim_ispathsep(*p2))
2545 p1 = p2 + 1;
2546 p = vim_strnsave(p1, (int)(p - p1));
2547 }
2548#endif
2549 return p;
2550}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002551
2552/*
2553 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2554 * Also check for ":\\", which MS Internet Explorer accepts, return
2555 * URL_BACKSLASH.
2556 */
2557 int
2558path_is_url(char_u *p)
2559{
2560 if (STRNCMP(p, "://", (size_t)3) == 0)
2561 return URL_SLASH;
2562 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2563 return URL_BACKSLASH;
2564 return 0;
2565}
2566
2567/*
2568 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
2569 * Return URL_BACKSLASH for "name:\\".
2570 * Return zero otherwise.
2571 */
2572 int
2573path_with_url(char_u *fname)
2574{
2575 char_u *p;
2576
2577 for (p = fname; isalpha(*p); ++p)
2578 ;
2579 return path_is_url(p);
2580}
Bram Moolenaar52410572019-10-27 05:12:45 +01002581
2582/*
2583 * Put timestamp "tt" in "buf[buflen]" in a nice format.
2584 */
2585 void
2586add_time(char_u *buf, size_t buflen, time_t tt)
2587{
2588#ifdef HAVE_STRFTIME
2589 struct tm tmval;
2590 struct tm *curtime;
2591
2592 if (vim_time() - tt >= 100)
2593 {
2594 curtime = vim_localtime(&tt, &tmval);
2595 if (vim_time() - tt < (60L * 60L * 12L))
2596 /* within 12 hours */
2597 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
2598 else
2599 /* longer ago */
2600 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
2601 }
2602 else
2603#endif
2604 {
2605 long seconds = (long)(vim_time() - tt);
2606
2607 vim_snprintf((char *)buf, buflen,
2608 NGETTEXT("%ld second ago", "%ld seconds ago", seconds),
2609 seconds);
2610 }
2611}