blob: 90cf914742b115a183d61b594009c7302b7aa0d5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaarb3f74062020-02-26 16:16:53 +010017#if defined(__HAIKU__)
18# include <storage/FindDirectory.h>
19#endif
20
Bram Moolenaar0a52df52019-08-18 22:26:31 +020021#if defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020022# include <lm.h>
23#endif
24
Bram Moolenaar85a20022019-12-21 18:25:54 +010025#define URL_SLASH 1 // path_is_url() has found "://"
26#define URL_BACKSLASH 2 // path_is_url() has found ":\\"
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010027
Bram Moolenaar0a52df52019-08-18 22:26:31 +020028// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020029static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +020032 * get_leader_len() returns the length in bytes of the prefix of the given
33 * string which introduces a comment. If this string is not a comment then
34 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 * When "flags" is not NULL, it is set to point to the flags of the recognized
36 * comment leader.
37 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +020038 * If "include_space" is set, include trailing whitespace while calculating the
39 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_leader_len(
43 char_u *line,
44 char_u **flags,
45 int backward,
46 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +000047{
48 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +020049 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 int got_com = FALSE;
51 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +010052 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
53 char_u *string; // pointer to comment string
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +020055 int middle_match_len = 0;
56 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +020057 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar81340392012-06-06 16:12:59 +020059 result = i = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +010060 while (VIM_ISWHITE(line[i])) // leading white space is ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 ++i;
62
63 /*
64 * Repeat to match several nested comment strings.
65 */
Bram Moolenaara4271d52011-05-10 13:38:27 +020066 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 {
68 /*
69 * scan through the 'comments' option for a match
70 */
71 found_one = FALSE;
72 for (list = curbuf->b_p_com; *list; )
73 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010074 // Get one option part into part_buf[]. Advance "list" to next
75 // one. Put "string" at start of string.
Bram Moolenaara4271d52011-05-10 13:38:27 +020076 if (!got_com && flags != NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +010077 *flags = list; // remember where flags started
Bram Moolenaara4271d52011-05-10 13:38:27 +020078 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
80 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +010081 if (string == NULL) // missing ':', ignore this part
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +010083 *string++ = NUL; // isolate flags from string
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar85a20022019-12-21 18:25:54 +010085 // If we found a middle match previously, use that match when this
86 // is not a middle or end.
Bram Moolenaara4271d52011-05-10 13:38:27 +020087 if (middle_match_len != 0
88 && vim_strchr(part_buf, COM_MIDDLE) == NULL
89 && vim_strchr(part_buf, COM_END) == NULL)
90 break;
91
Bram Moolenaar85a20022019-12-21 18:25:54 +010092 // When we already found a nested comment, only accept further
93 // nested comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
95 continue;
96
Bram Moolenaar85a20022019-12-21 18:25:54 +010097 // When 'O' flag present and using "O" command skip this one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
99 continue;
100
Bram Moolenaar85a20022019-12-21 18:25:54 +0100101 // Line contents and string must match.
102 // When string starts with white space, must have some white space
103 // (but the amount does not need to match, there might be a mix of
104 // TABs and spaces).
Bram Moolenaar1c465442017-03-12 20:10:05 +0100105 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100107 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100108 continue; // missing white space
Bram Moolenaar1c465442017-03-12 20:10:05 +0100109 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 ++string;
111 }
112 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
113 ;
114 if (string[j] != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100115 continue; // string doesn't match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar85a20022019-12-21 18:25:54 +0100117 // When 'b' flag used, there must be white space or an
118 // end-of-line after the string in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100120 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 continue;
122
Bram Moolenaar85a20022019-12-21 18:25:54 +0100123 // We have found a match, stop searching unless this is a middle
124 // comment. The middle comment can be a substring of the end
125 // comment in which case it's better to return the length of the
126 // end comment and its flags. Thus we keep searching with middle
127 // and end matches and use an end match if it matches better.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200128 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
129 {
130 if (middle_match_len == 0)
131 {
132 middle_match_len = j;
133 saved_flags = prev_list;
134 }
135 continue;
136 }
137 if (middle_match_len != 0 && j > middle_match_len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100138 // Use this match instead of the middle match, since it's a
139 // longer thus better match.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200140 middle_match_len = 0;
141
142 if (middle_match_len == 0)
143 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 found_one = TRUE;
145 break;
146 }
147
Bram Moolenaara4271d52011-05-10 13:38:27 +0200148 if (middle_match_len != 0)
149 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100150 // Use the previously found middle match after failing to find a
151 // match with an end.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200152 if (!got_com && flags != NULL)
153 *flags = saved_flags;
154 i += middle_match_len;
155 found_one = TRUE;
156 }
157
Bram Moolenaar85a20022019-12-21 18:25:54 +0100158 // No match found, stop scanning.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 if (!found_one)
160 break;
161
Bram Moolenaar81340392012-06-06 16:12:59 +0200162 result = i;
163
Bram Moolenaar85a20022019-12-21 18:25:54 +0100164 // Include any trailing white space.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100165 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++i;
167
Bram Moolenaar81340392012-06-06 16:12:59 +0200168 if (include_space)
169 result = i;
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // If this comment doesn't nest, stop here.
Bram Moolenaara4271d52011-05-10 13:38:27 +0200172 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (vim_strchr(part_buf, COM_NEST) == NULL)
174 break;
175 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200176 return result;
177}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200178
Bram Moolenaar81340392012-06-06 16:12:59 +0200179/*
180 * Return the offset at which the last comment in line starts. If there is no
181 * comment in the whole line, -1 is returned.
182 *
183 * When "flags" is not null, it is set to point to the flags describing the
184 * recognized comment leader.
185 */
186 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100187get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200188{
189 int result = -1;
190 int i, j;
191 int lower_check_bound = 0;
192 char_u *string;
193 char_u *com_leader;
194 char_u *com_flags;
195 char_u *list;
196 int found_one;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 char_u part_buf[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200198
199 /*
200 * Repeat to match several nested comment strings.
201 */
202 i = (int)STRLEN(line);
203 while (--i >= lower_check_bound)
204 {
205 /*
206 * scan through the 'comments' option for a match
207 */
208 found_one = FALSE;
209 for (list = curbuf->b_p_com; *list; )
210 {
211 char_u *flags_save = list;
212
213 /*
214 * Get one option part into part_buf[]. Advance list to next one.
215 * put string at start of string.
216 */
217 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
218 string = vim_strchr(part_buf, ':');
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 if (string == NULL) // If everything is fine, this cannot actually
220 // happen.
Bram Moolenaar81340392012-06-06 16:12:59 +0200221 continue;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 *string++ = NUL; // Isolate flags from string.
Bram Moolenaar81340392012-06-06 16:12:59 +0200223 com_leader = string;
224
225 /*
226 * Line contents and string must match.
227 * When string starts with white space, must have some white space
228 * (but the amount does not need to match, there might be a mix of
229 * TABs and spaces).
230 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100231 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200232 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100233 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200234 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100235 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200236 ++string;
237 }
238 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
239 /* do nothing */;
240 if (string[j] != NUL)
241 continue;
242
243 /*
244 * When 'b' flag used, there must be white space or an
245 * end-of-line after the string in the line.
246 */
247 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100248 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200249 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100250
Bram Moolenaar4af72592018-12-09 15:00:52 +0100251 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100252 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100253 // For a middlepart comment, only consider it to match if
254 // everything before the current position in the line is
255 // whitespace. Otherwise we would think we are inside a
256 // comment if the middle part appears somewhere in the middle
257 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100258 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
259 ;
260 if (j < i)
261 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200262 }
263
264 /*
265 * We have found a match, stop searching.
266 */
267 found_one = TRUE;
268
269 if (flags)
270 *flags = flags_save;
271 com_flags = flags_save;
272
273 break;
274 }
275
276 if (found_one)
277 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 char_u part_buf2[COM_MAX_LEN]; // buffer for one option part
Bram Moolenaar81340392012-06-06 16:12:59 +0200279 int len1, len2, off;
280
281 result = i;
282 /*
283 * If this comment nests, continue searching.
284 */
285 if (vim_strchr(part_buf, COM_NEST) != NULL)
286 continue;
287
288 lower_check_bound = i;
289
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Let's verify whether the comment leader found is a substring
291 // of other comment leaders. If it is, let's adjust the
292 // lower_check_bound so that we make sure that we have determined
293 // the comment leader correctly.
Bram Moolenaar81340392012-06-06 16:12:59 +0200294
Bram Moolenaar1c465442017-03-12 20:10:05 +0100295 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200296 ++com_leader;
297 len1 = (int)STRLEN(com_leader);
298
299 for (list = curbuf->b_p_com; *list; )
300 {
301 char_u *flags_save = list;
302
303 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
304 if (flags_save == com_flags)
305 continue;
306 string = vim_strchr(part_buf2, ':');
307 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100308 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200309 ++string;
310 len2 = (int)STRLEN(string);
311 if (len2 == 0)
312 continue;
313
Bram Moolenaar85a20022019-12-21 18:25:54 +0100314 // Now we have to verify whether string ends with a substring
315 // beginning the com_leader.
Bram Moolenaar81340392012-06-06 16:12:59 +0200316 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
317 {
318 --off;
319 if (!STRNCMP(string + off, com_leader, len2 - off))
320 {
321 if (i - off < lower_check_bound)
322 lower_check_bound = i - off;
323 }
324 }
325 }
326 }
327 }
328 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
331/*
332 * Return the number of window lines occupied by buffer line "lnum".
Bram Moolenaar61c4b042022-10-18 15:10:11 +0100333 * Includes any filler lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100336plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
338 return plines_win(curwin, lnum, TRUE);
339}
340
341 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100342plines_win(
343 win_T *wp,
344 linenr_T lnum,
zeertzjqbfe377b2023-08-17 22:58:53 +0200345 int limit_winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346{
347#if defined(FEAT_DIFF) || defined(PROTO)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100348 // Check for filler lines above this buffer line. When folded the result
349 // is one line anyway.
zeertzjqbfe377b2023-08-17 22:58:53 +0200350 return plines_win_nofill(wp, lnum, limit_winheight)
351 + diff_check_fill(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352}
353
Bram Moolenaar61c4b042022-10-18 15:10:11 +0100354/*
355 * Return the number of window lines occupied by buffer line "lnum".
356 * Does not include filler lines.
357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100359plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
361 return plines_win_nofill(curwin, lnum, TRUE);
362}
363
364 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100365plines_win_nofill(
366 win_T *wp,
367 linenr_T lnum,
zeertzjqbfe377b2023-08-17 22:58:53 +0200368 int limit_winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
370#endif
371 int lines;
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 if (wp->w_width == 0)
374 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
376#ifdef FEAT_FOLDING
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000377 // Folded lines are handled just like an empty line.
Bram Moolenaar85a20022019-12-21 18:25:54 +0100378 // NOTE: Caller must handle lines that are MAYBE folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (lineFolded(wp, lnum) == TRUE)
380 return 1;
381#endif
382
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100383 if (!wp->w_p_wrap)
384 lines = 1
385#ifdef FEAT_PROP_POPUP
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100386 // add a line for each "above" and "below" aligned text property
387 + prop_count_above_below(wp->w_buffer, lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100388#endif
389 ;
390 else
391 lines = plines_win_nofold(wp, lnum);
392
zeertzjqbfe377b2023-08-17 22:58:53 +0200393 if (limit_winheight && lines > wp->w_height)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000394 return wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 return lines;
396}
397
398/*
399 * Return number of window lines physical line "lnum" will occupy in window
400 * "wp". Does not care about folding, 'wrap' or 'diff'.
401 */
402 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100403plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
405 char_u *s;
406 long col;
407 int width;
Bram Moolenaar49a90792022-08-09 18:25:23 +0100408 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
410 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar49a90792022-08-09 18:25:23 +0100411 init_chartabsize_arg(&cts, wp, lnum, 0, s, s);
412 if (*s == NUL
413#ifdef FEAT_PROP_POPUP
414 && !cts.cts_has_prop_with_text
415#endif
416 )
417 return 1; // be quick for an empty line
418 win_linetabsize_cts(&cts, (colnr_T)MAXCOL);
419 clear_chartabsize_arg(&cts);
420 col = (int)cts.cts_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100422 // If list mode is on, then the '$' at the end of the line may take up one
423 // extra column.
Bram Moolenaareed9d462021-02-15 20:38:25 +0100424 if (wp->w_p_list && wp->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 col += 1;
426
427 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200428 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200430 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 if (width <= 0)
432 return 32000;
433 if (col <= width)
434 return 1;
435 col -= width;
436 width += win_col_off2(wp);
437 return (col + (width - 1)) / width + 1;
438}
439
440/*
441 * Like plines_win(), but only reports the number of physical screen lines
442 * used from the start of the line to the given column number.
443 */
444 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100445plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446{
447 long col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 int lines = 0;
449 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200450 char_u *line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100451 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100454 // Check for filler lines above this buffer line. When folded the result
455 // is one line anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 lines = diff_check_fill(wp, lnum);
457#endif
458
459 if (!wp->w_p_wrap)
460 return lines + 1;
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 if (wp->w_width == 0)
463 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100465 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100467 init_chartabsize_arg(&cts, wp, lnum, 0, line, line);
468 while (*cts.cts_ptr != NUL && --column >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100470 cts.cts_vcol += win_lbr_chartabsize(&cts, NULL);
471 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 }
473
474 /*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100475 * If *cts.cts_ptr is a TAB, and the TAB is not displayed as ^I, and we're
476 * not in MODE_INSERT state, then col must be adjusted so that it
477 * represents the last screen position of the TAB. This only fixes an
478 * error when the TAB wraps from one screen line to the next (when
479 * 'columns' is not a multiple of 'ts') -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100481 col = cts.cts_vcol;
482 if (*cts.cts_ptr == TAB && (State & MODE_NORMAL)
Bram Moolenaar24959102022-05-07 20:01:16 +0100483 && (!wp->w_p_list || wp->w_lcs_chars.tab1))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100484 col += win_lbr_chartabsize(&cts, NULL) - 1;
485 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200488 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200490 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +0000491 if (width <= 0)
492 return 9999;
493
494 lines += 1;
495 if (col > width)
496 lines += (col - width) / (width + win_col_off2(wp)) + 1;
497 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498}
499
Luuk van Baal32d701f2024-04-28 16:24:02 +0200500/*
501 * Return number of window lines the physical line range from "first" until
502 * "last" will occupy in window "wp". Takes into account folding, 'wrap',
503 * topfill and filler lines beyond the end of the buffer. Limit to "max" lines.
504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 int
Luuk van Baal32d701f2024-04-28 16:24:02 +0200506plines_m_win(win_T *wp, linenr_T first, linenr_T last, int max)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508 int count = 0;
509
Luuk van Baal32d701f2024-04-28 16:24:02 +0200510 while (first <= last && count < max)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {
512#ifdef FEAT_FOLDING
513 int x;
514
Bram Moolenaar85a20022019-12-21 18:25:54 +0100515 // Check if there are any really folded lines, but also included lines
516 // that are maybe folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 x = foldedCount(wp, first, NULL);
518 if (x > 0)
519 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100520 ++count; // count 1 for "+-- folded" line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 first += x;
522 }
523 else
524#endif
525 {
526#ifdef FEAT_DIFF
527 if (first == wp->w_topline)
Luuk van Baal08b0f632024-04-09 22:43:49 +0200528 count += plines_win_nofill(wp, first, FALSE) + wp->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 else
530#endif
Luuk van Baal08b0f632024-04-09 22:43:49 +0200531 count += plines_win(wp, first, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 ++first;
533 }
534 }
Luuk van Baal08b0f632024-04-09 22:43:49 +0200535#ifdef FEAT_DIFF
536 if (first == wp->w_buffer->b_ml.ml_line_count + 1)
537 count += diff_check_fill(wp, first);
538#endif
Luuk van Baal32d701f2024-04-28 16:24:02 +0200539 return MIN(max, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
541
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100543gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100545 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
Bram Moolenaar85a20022019-12-21 18:25:54 +0100547 // When searching columns is sometimes put at the end of a line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100548 if (pos->col == MAXCOL)
549 return NUL;
550 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (has_mbyte)
552 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 return (int)*ptr;
554}
555
556 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100557gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (has_mbyte)
560 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 return (int)*ml_get_cursor();
562}
563
564/*
565 * Write a character at the current cursor position.
566 * It is directly written into the block.
567 */
568 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100569pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
572 + curwin->w_cursor.col) = c;
573}
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 * Skip to next part of an option argument: Skip space and comma.
577 */
578 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
581 if (*p == ',')
582 ++p;
583 while (*p == ' ')
584 ++p;
585 return p;
586}
587
588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * check_status: called when the status bars for the buffer 'buf'
590 * need to be updated
591 */
592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100593check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 win_T *wp;
596
Bram Moolenaar29323592016-07-24 22:04:11 +0200597 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (wp->w_buffer == buf && wp->w_status_height)
599 {
600 wp->w_redr_status = TRUE;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100601 set_must_redraw(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
605/*
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100606 * Ask for a reply from the user, a 'y' or a 'n', with prompt "str" (which
607 * should have been translated already).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * No other characters are accepted, the message is repeated until a valid
609 * reply is entered or CTRL-C is hit.
610 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
611 * from any buffers but directly from the user.
612 *
613 * return the 'y' or 'n'
614 */
615 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100616ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 int r = ' ';
619 int save_State = State;
620
Bram Moolenaar85a20022019-12-21 18:25:54 +0100621 if (exiting) // put terminal in raw mode for this question
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 settmode(TMODE_RAW);
623 ++no_wait_return;
624#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200625 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100627 State = MODE_CONFIRM; // mouse behaves like with :confirm
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200628 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200630 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 while (r != 'y' && r != 'n')
633 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100634 // same highlighting as for wait_return()
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100635 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (direct)
637 r = get_keystroke();
638 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000639 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (r == Ctrl_C || r == ESC)
641 r = 'n';
Bram Moolenaar85a20022019-12-21 18:25:54 +0100642 msg_putchar(r); // show what you typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 out_flush();
644 }
645 --no_wait_return;
646 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 --no_mapping;
649 --allow_keys;
650
651 return r;
652}
653
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200654#if defined(FEAT_EVAL) || defined(PROTO)
655
656/*
LemonBoy2bf52dd2022-04-09 18:17:34 +0100657 * Returns the current mode as a string in "buf[MODE_MAX_LENGTH]", NUL
658 * terminated.
659 * The first character represents the major mode, the following ones the minor
660 * ones.
661 */
662 void
663get_mode(char_u *buf)
664{
665 int i = 0;
666
667 if (time_for_testing == 93784)
668 {
669 // Testing the two-character code.
670 buf[i++] = 'x';
671 buf[i++] = '!';
672 }
673#ifdef FEAT_TERMINAL
674 else if (term_use_loop())
h-east71ebf3b2023-09-03 17:12:55 +0200675 {
676 if (State & MODE_CMDLINE)
677 buf[i++] = 'c';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100678 buf[i++] = 't';
h-east71ebf3b2023-09-03 17:12:55 +0200679 }
LemonBoy2bf52dd2022-04-09 18:17:34 +0100680#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100681 else if (State == MODE_HITRETURN || State == MODE_ASKMORE
682 || State == MODE_SETWSIZE
683 || State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100684 {
685 buf[i++] = 'r';
Bram Moolenaar24959102022-05-07 20:01:16 +0100686 if (State == MODE_ASKMORE)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100687 buf[i++] = 'm';
Bram Moolenaar24959102022-05-07 20:01:16 +0100688 else if (State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100689 buf[i++] = '?';
690 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100691 else if (State == MODE_EXTERNCMD)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100692 buf[i++] = '!';
Bram Moolenaar24959102022-05-07 20:01:16 +0100693 else if (State & MODE_INSERT)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100694 {
695 if (State & VREPLACE_FLAG)
696 {
697 buf[i++] = 'R';
698 buf[i++] = 'v';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100699 }
700 else
701 {
702 if (State & REPLACE_FLAG)
703 buf[i++] = 'R';
704 else
705 buf[i++] = 'i';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100706 }
zeertzjq590f3652022-04-29 11:29:54 +0100707
708 if (ins_compl_active())
709 buf[i++] = 'c';
710 else if (ctrl_x_mode_not_defined_yet())
711 buf[i++] = 'x';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100712 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100713 else if ((State & MODE_CMDLINE) || exmode_active)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100714 {
715 buf[i++] = 'c';
716 if (exmode_active == EXMODE_VIM)
717 buf[i++] = 'v';
718 else if (exmode_active == EXMODE_NORMAL)
719 buf[i++] = 'e';
Sam-programsd1c3ef12023-11-27 22:22:51 +0100720 if ((State & MODE_CMDLINE) && cmdline_overstrike())
721 buf[i++] = 'r';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100722 }
kuuote0fd1cb12024-08-20 19:53:17 +0200723 else if (VIsual_active)
724 {
725 if (VIsual_select)
726 buf[i++] = VIsual_mode + 's' - 'v';
727 else
728 {
729 buf[i++] = VIsual_mode;
730 if (restart_VIsual_select)
731 buf[i++] = 's';
732 }
733 }
LemonBoy2bf52dd2022-04-09 18:17:34 +0100734 else
735 {
736 buf[i++] = 'n';
737 if (finish_op)
738 {
739 buf[i++] = 'o';
740 // to be able to detect force-linewise/blockwise/characterwise
741 // operations
742 buf[i++] = motion_force;
743 }
744 else if (restart_edit == 'I' || restart_edit == 'R'
745 || restart_edit == 'V')
746 {
747 buf[i++] = 'i';
748 buf[i++] = restart_edit;
749 }
750#ifdef FEAT_TERMINAL
751 else if (term_in_normal_mode())
752 buf[i++] = 't';
753#endif
754 }
755
756 buf[i] = NUL;
757}
758
759/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200760 * "mode()" function
761 */
762 void
763f_mode(typval_T *argvars, typval_T *rettv)
764{
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200765 char_u buf[MODE_MAX_LENGTH];
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200766
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200767 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
768 return;
769
LemonBoy2bf52dd2022-04-09 18:17:34 +0100770 get_mode(buf);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200771
Bram Moolenaar85a20022019-12-21 18:25:54 +0100772 // Clear out the minor mode when the argument is not a non-zero number or
773 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200774 if (!non_zero_arg(&argvars[0]))
775 buf[1] = NUL;
776
777 rettv->vval.v_string = vim_strsave(buf);
778 rettv->v_type = VAR_STRING;
779}
780
781 static void
782may_add_state_char(garray_T *gap, char_u *include, int c)
783{
784 if (include == NULL || vim_strchr(include, c) != NULL)
785 ga_append(gap, c);
786}
787
788/*
789 * "state()" function
790 */
791 void
792f_state(typval_T *argvars, typval_T *rettv)
793{
794 garray_T ga;
795 char_u *include = NULL;
796 int i;
797
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200798 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
799 return;
800
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200801 ga_init2(&ga, 1, 20);
802 if (argvars[0].v_type != VAR_UNKNOWN)
803 include = tv_get_string(&argvars[0]);
804
805 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
806 may_add_state_char(&ga, include, 'm');
807 if (op_pending())
808 may_add_state_char(&ga, include, 'o');
809 if (autocmd_busy)
810 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200811 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200812 may_add_state_char(&ga, include, 'a');
813
814# ifdef FEAT_JOB_CHANNEL
815 if (channel_in_blocking_wait())
816 may_add_state_char(&ga, include, 'w');
817# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200818 if (!get_was_safe_state())
819 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200820 for (i = 0; i < get_callback_depth() && i < 3; ++i)
821 may_add_state_char(&ga, include, 'c');
822 if (msg_scrolled > 0)
823 may_add_state_char(&ga, include, 's');
824
825 rettv->v_type = VAR_STRING;
826 rettv->vval.v_string = ga.ga_data;
827}
828
829#endif // FEAT_EVAL
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831/*
832 * Get a key stroke directly from the user.
833 * Ignores mouse clicks and scrollbar events, except a click for the left
834 * button (used at the more prompt).
835 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
836 * Disadvantage: typeahead is ignored.
837 * Translates the interrupt character for unix to ESC.
838 */
839 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100840get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100842 char_u *buf = NULL;
843 int buflen = 150;
844 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 int len = 0;
846 int n;
847 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000848 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar85a20022019-12-21 18:25:54 +0100850 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 for (;;)
852 {
853 cursor_on();
854 out_flush();
855
Bram Moolenaar85a20022019-12-21 18:25:54 +0100856 // Leave some room for check_termcode() to insert a key code into (max
857 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
858 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100859 maxlen = (buflen - 6 - len) / 3;
860 if (buf == NULL)
861 buf = alloc(buflen);
862 else if (maxlen < 10)
863 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100864 char_u *t_buf = buf;
865
Bram Moolenaar85a20022019-12-21 18:25:54 +0100866 // Need some more space. This might happen when receiving a long
867 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100868 buflen += 100;
869 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100870 if (buf == NULL)
871 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100872 maxlen = (buflen - 6 - len) / 3;
873 }
874 if (buf == NULL)
875 {
876 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100878 }
879
Bram Moolenaar85a20022019-12-21 18:25:54 +0100880 // First time: blocking wait. Second time: wait up to 100ms for a
881 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100882 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (n > 0)
884 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100885 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200886 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000888 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000890 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100891 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaar85a20022019-12-21 18:25:54 +0100893 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100894 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000895 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000897
Bram Moolenaar85a20022019-12-21 18:25:54 +0100898 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100899 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100900 if (must_redraw != 0 && !need_wait_return && (State
901 & (MODE_CMDLINE | MODE_HITRETURN | MODE_ASKMORE)) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100902 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100903 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100904 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100905 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100906 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100907 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100908 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100909 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100911 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 continue;
913
Bram Moolenaar85a20022019-12-21 18:25:54 +0100914 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 n = buf[0];
916 if (n == K_SPECIAL)
917 {
918 n = TO_SPECIAL(buf[1], buf[2]);
919 if (buf[1] == KS_MODIFIER
920 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100921 || (is_mouse_key(n) && n != K_LEFTMOUSE)
922#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 || n == K_VER_SCROLLBAR
924 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#endif
926 )
927 {
928 if (buf[1] == KS_MODIFIER)
929 mod_mask = buf[2];
930 len -= 3;
931 if (len > 0)
932 mch_memmove(buf, buf + 3, (size_t)len);
933 continue;
934 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if (has_mbyte)
938 {
939 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100940 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100941 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 n = (*mb_ptr2char)(buf);
943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#ifdef UNIX
945 if (n == intr_char)
946 n = ESC;
947#endif
948 break;
949 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100950 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 mapped_ctrl_c = save_mapped_ctrl_c;
953 return n;
954}
955
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +0100956// For overflow detection, add a digit safely to an int value.
957 static int
958vim_append_digit_int(int *value, int digit)
959{
960 int x = *value;
961 if (x > ((INT_MAX - digit) / 10))
962 return FAIL;
963 *value = x * 10 + digit;
964 return OK;
965}
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000968 * Get a number from the user.
969 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 */
971 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100972get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100973 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100974 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 int n = 0;
977 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000978 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000980 if (mouse_used != NULL)
981 *mouse_used = FALSE;
982
Bram Moolenaar85a20022019-12-21 18:25:54 +0100983 // When not printing messages, the user won't know what to type, return a
984 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (msg_silent != 0)
986 return 0;
987
988#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100989 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
991 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100992 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 for (;;)
994 {
995 windgoto(msg_row, msg_col);
996 c = safe_vgetc();
997 if (VIM_ISDIGIT(c))
998 {
Christian Brabandt22cbc8a2023-11-19 10:47:21 +0100999 if (vim_append_digit_int(&n, c - '0') == FAIL)
Christian Brabandt73b2d372023-11-14 21:58:26 +01001000 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001002 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1005 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001006 if (typed > 0)
1007 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001008 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001009 --typed;
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001013 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1014 {
1015 *mouse_used = TRUE;
1016 n = mouse_row + 1;
1017 break;
1018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 else if (n == 0 && c == ':' && colon)
1020 {
1021 stuffcharReadbuff(':');
1022 if (!exmode_active)
1023 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001024 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 do_redraw = FALSE;
1026 break;
1027 }
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +02001028 else if (c == Ctrl_C || c == ESC || c == 'q')
1029 {
1030 n = 0;
1031 break;
1032 }
1033 else if (c == CAR || c == NL )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 break;
1035 }
1036 --no_mapping;
1037 --allow_keys;
1038 return n;
1039}
1040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001041/*
1042 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001043 * When "mouse_used" is not NULL allow using the mouse and in that case return
1044 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001045 */
1046 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001047prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001048{
1049 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001050 int save_cmdline_row;
1051 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001052
Bram Moolenaar85a20022019-12-21 18:25:54 +01001053 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001054 if (mouse_used != NULL)
Bram Moolenaareebd5552020-06-10 15:45:57 +02001055 msg_puts(_("Type number and <Enter> or click with the mouse (q or empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001056 else
Bram Moolenaareebd5552020-06-10 15:45:57 +02001057 msg_puts(_("Type number and <Enter> (q or empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001058
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001059 // Set the state such that text can be selected/copied/pasted and we still
1060 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1061 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001062 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001063 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001064 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001065 State = MODE_CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001066 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001067 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001068
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001069 i = get_number(TRUE, mouse_used);
1070 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001071 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001072 // don't call wait_return() now
1073 if (msg_row > 0)
1074 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001075 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +01001076 msg_didany = FALSE;
1077 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001078 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001079 else
1080 cmdline_row = save_cmdline_row;
1081 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001082 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001083 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001085 return i;
1086}
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001089msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 long pn;
1092
Bram Moolenaar85a20022019-12-21 18:25:54 +01001093 if (global_busy // no messages now, wait until global is finished
1094 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 return;
1096
Bram Moolenaar85a20022019-12-21 18:25:54 +01001097 // We don't want to overwrite another important message, but do overwrite
1098 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1099 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001100 if (keep_msg != NULL && !keep_msg_more)
1101 return;
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (n > 0)
1104 pn = n;
1105 else
1106 pn = -n;
1107
1108 if (pn > p_report)
1109 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001110 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001111 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001112 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001114 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001115 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001117 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1118 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (msg(msg_buf))
1120 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001121 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001122 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124 }
1125}
1126
1127/*
1128 * flush map and typeahead buffers and give a warning for an error
1129 */
1130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001131beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 if (emsg_silent == 0)
1134 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001135 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001136 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
1138}
1139
1140/*
Bram Moolenaaraae97622022-04-13 14:28:07 +01001141 * Give a warning for an error. "val" is one of the BO_ values, e.g., BO_OPER.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 */
1143 void
Bram Moolenaaraae97622022-04-13 14:28:07 +01001144vim_beep(unsigned val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001146#ifdef FEAT_EVAL
1147 called_vim_beep = TRUE;
1148#endif
1149
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001150 if (emsg_silent != 0 || in_assert_fails)
1151 return;
1152
1153 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001155#ifdef ELAPSED_FUNC
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001156 static int did_init = FALSE;
1157 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001158
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001159 // Only beep once per half a second, otherwise a sequence of beeps
1160 // would freeze Vim.
1161 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1162 {
1163 did_init = TRUE;
1164 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001166 if (p_vb
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001167#ifdef FEAT_GUI
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001168 // While the GUI is starting up the termcap is set for
1169 // the GUI but the output still goes to a terminal.
1170 && !(gui.in_use && gui.starting)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001171#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001172 )
1173 {
1174 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001175#ifdef FEAT_VTP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001176 // No restore color information, refresh the screen.
1177 if (has_vtp_working() != 0
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001178# ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001179 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001180# endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001181 )
1182 {
1183 redraw_later(UPD_CLEAR);
1184 update_screen(0);
1185 redrawcmd();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001186 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001187#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001188 }
1189 else
1190 out_char(BELL);
1191#ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001193#endif
1194 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001196 // When 'debug' contains "beep" produce a message. If we are sourcing
1197 // a script or executing a function give the user a hint where the beep
1198 // comes from.
1199 if (vim_strchr(p_debug, 'e') != NULL)
1200 {
1201 msg_source(HL_ATTR(HLF_W));
1202 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204}
1205
1206/*
1207 * To get the "real" home directory:
1208 * - get value of $HOME
1209 * For Unix:
1210 * - go to that directory
1211 * - do mch_dirname() to get the real name of that directory.
1212 * This also works with mounts and links.
1213 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001214 * For Windows:
1215 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001218init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
1220 char_u *var;
1221
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001223 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#ifdef VMS
1226 var = mch_getenv((char_u *)"SYS$LOGIN");
1227#else
1228 var = mch_getenv((char_u *)"HOME");
1229#endif
1230
Bram Moolenaar4f974752019-02-17 17:44:42 +01001231#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001233 * Typically, $HOME is not defined on Windows, unless the user has
1234 * specifically defined it for Vim's sake. However, on Windows NT
1235 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1236 * each user. Try constructing $HOME from these.
1237 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001238 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001239 {
1240 char_u *homedrive, *homepath;
1241
1242 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1243 homepath = mch_getenv((char_u *)"HOMEPATH");
1244 if (homepath == NULL || *homepath == NUL)
1245 homepath = (char_u *)"\\";
1246 if (homedrive != NULL
1247 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1248 {
1249 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1250 if (NameBuff[0] != NUL)
1251 var = NameBuff;
1252 }
1253 }
1254
1255 if (var == NULL)
1256 var = mch_getenv((char_u *)"USERPROFILE");
1257
1258 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 * Weird but true: $HOME may contain an indirect reference to another
1260 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1261 * when $HOME is being set.
1262 */
1263 if (var != NULL && *var == '%')
1264 {
1265 char_u *p;
1266 char_u *exp;
1267
1268 p = vim_strchr(var + 1, '%');
1269 if (p != NULL)
1270 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001271 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 exp = mch_getenv(NameBuff);
1273 if (exp != NULL && *exp != NUL
1274 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1275 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001276 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 }
1281
Bram Moolenaar85a20022019-12-21 18:25:54 +01001282 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001283 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
Bram Moolenaar05159a02005-02-26 23:04:13 +00001285 if (enc_utf8 && var != NULL)
1286 {
1287 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001288 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001289
Bram Moolenaar85a20022019-12-21 18:25:54 +01001290 // Convert from active codepage to UTF-8. Other conversions are
1291 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001292 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001293 if (pp != NULL)
1294 {
1295 homedir = pp;
1296 return;
1297 }
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 /*
1301 * Default home dir is C:/
1302 * Best assumption we can make in such a situation.
1303 */
1304 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001305 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (var != NULL)
1309 {
1310#ifdef UNIX
1311 /*
1312 * Change to the directory and get the actual path. This resolves
1313 * links. Don't do it when we can't return.
1314 */
1315 if (mch_dirname(NameBuff, MAXPATHL) == OK
1316 && mch_chdir((char *)NameBuff) == 0)
1317 {
1318 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1319 var = IObuff;
1320 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001321 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323#endif
1324 homedir = vim_strsave(var);
1325 }
1326}
1327
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001328#if defined(EXITFREE) || defined(PROTO)
1329 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001330free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001331{
1332 vim_free(homedir);
1333}
Bram Moolenaar24305862012-08-15 14:05:05 +02001334
Bram Moolenaar24305862012-08-15 14:05:05 +02001335 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001336free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001337{
1338 ga_clear_strings(&ga_users);
1339}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001340#endif
1341
K.Takatace3189d2023-02-15 19:13:43 +00001342#if defined(MSWIN) || defined(PROTO)
1343/*
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001344 * Initialize $VIM and $VIMRUNTIME when 'enc' is updated.
K.Takatace3189d2023-02-15 19:13:43 +00001345 */
1346 void
1347init_vimdir(void)
1348{
1349 int mustfree;
1350 char_u *p;
1351
1352 mch_get_exe_name();
1353
1354 mustfree = FALSE;
1355 didset_vim = FALSE;
1356 p = vim_getenv((char_u *)"VIM", &mustfree);
1357 if (mustfree)
1358 vim_free(p);
1359
1360 mustfree = FALSE;
1361 didset_vimruntime = FALSE;
1362 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1363 if (mustfree)
1364 vim_free(p);
1365}
1366#endif
1367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001369 * Call expand_env() and store the result in an allocated string.
1370 * This is not very memory efficient, this expects the result to be freed
1371 * again soon.
1372 */
1373 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001374expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001375{
1376 return expand_env_save_opt(src, FALSE);
1377}
1378
1379/*
1380 * Idem, but when "one" is TRUE handle the string as one file name, only
1381 * expand "~" at the start.
1382 */
1383 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001384expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001385{
1386 char_u *p;
1387
1388 p = alloc(MAXPATHL);
1389 if (p != NULL)
1390 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1391 return p;
1392}
1393
1394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 * Expand environment variable with path name.
1396 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001397 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * If anything fails no expansion is done and dst equals src.
1399 */
1400 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001401expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001402 char_u *src, // input string e.g. "$HOME/vim.hlp"
1403 char_u *dst, // where to put the result
1404 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001406 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407}
1408
1409 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001410expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001411 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1412 char_u *dst, // where to put the result
1413 int dstlen, // maximum length of the result
1414 int esc, // escape spaces in expanded variables
1415 int one, // "srcp" is one file name
1416 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001418 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 char_u *tail;
1420 int c;
1421 char_u *var;
1422 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001423 int mustfree; // var was allocated, need to free it later
1424 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001425 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001427 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001428 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001429
1430 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001431 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 while (*src && dstlen > 0)
1433 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001434#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001435 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001436 if (src[0] == '`' && src[1] == '=')
1437 {
1438 size_t len;
1439
1440 var = src;
1441 src += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02001442 (void)skip_expr(&src, NULL);
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001443 if (*src == '`')
1444 ++src;
1445 len = src - var;
1446 if (len > (size_t)dstlen)
1447 len = dstlen;
1448 vim_strncpy(dst, var, len);
1449 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001450 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001451 continue;
1452 }
1453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001455 if ((*src == '$'
1456#ifdef VMS
1457 && at_start
1458#endif
1459 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001460#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 || *src == '%'
1462#endif
1463 || (*src == '~' && at_start))
1464 {
1465 mustfree = FALSE;
1466
1467 /*
1468 * The variable name is copied into dst temporarily, because it may
1469 * be a string in read-only memory and a NUL needs to be appended.
1470 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001471 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
1473 tail = src + 1;
1474 var = dst;
1475 c = dstlen - 1;
1476
1477#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001478 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (*tail == '{' && !vim_isIDc('{'))
1480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001481 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 while (c-- > 0 && *tail && *tail != '}')
1483 *var++ = *tail++;
1484 }
1485 else
1486#endif
1487 {
1488 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001489#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 || (*src == '%' && *tail != '%')
1491#endif
1492 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001496#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497# ifdef UNIX
1498 if (src[1] == '{' && *tail != '}')
1499# else
1500 if (*src == '%' && *tail != '%')
1501# endif
1502 var = NULL;
1503 else
1504 {
1505# ifdef UNIX
1506 if (src[1] == '{')
1507# else
1508 if (*src == '%')
1509#endif
1510 ++tail;
1511#endif
1512 *var = NUL;
1513 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001514#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516#endif
1517 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001518 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 else if ( src[1] == NUL
1520 || vim_ispathsep(src[1])
1521 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1522 {
1523 var = homedir;
1524 tail = src + 1;
1525 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001526 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 {
1528#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1529 /*
1530 * Copy ~user to dst[], so we can put a NUL after it.
1531 */
1532 tail = src;
1533 var = dst;
1534 c = dstlen - 1;
1535 while ( c-- > 0
1536 && *tail
1537 && vim_isfilec(*tail)
1538 && !vim_ispathsep(*tail))
1539 *var++ = *tail++;
1540 *var = NUL;
1541# ifdef UNIX
1542 /*
1543 * If the system supports getpwnam(), use it.
1544 * Otherwise, or if getpwnam() fails, the shell is used to
1545 * expand ~user. This is slower and may fail if the shell
1546 * does not support ~user (old versions of /bin/sh).
1547 */
1548# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1549 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001550 // Note: memory allocated by getpwnam() is never freed.
1551 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001552 struct passwd *pw = (*dst == NUL)
1553 ? NULL : getpwnam((char *)dst + 1);
1554
1555 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557 if (var == NULL)
1558# endif
1559 {
1560 expand_T xpc;
1561
1562 ExpandInit(&xpc);
1563 xpc.xp_context = EXPAND_FILES;
1564 var = ExpandOne(&xpc, dst, NULL,
1565 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 mustfree = TRUE;
1567 }
1568
Bram Moolenaar85a20022019-12-21 18:25:54 +01001569# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 /*
1571 * USER_HOME is a comma-separated list of
1572 * directories to search for the user account in.
1573 */
1574 {
1575 char_u test[MAXPATHL], paths[MAXPATHL];
1576 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001577 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 STRCPY(paths, USER_HOME);
1580 next_path = paths;
1581 while (*next_path)
1582 {
1583 for (path = next_path; *next_path && *next_path != ',';
1584 next_path++);
1585 if (*next_path)
1586 *next_path++ = NUL;
1587 STRCPY(test, path);
1588 STRCAT(test, "/");
1589 STRCAT(test, dst + 1);
1590 if (mch_stat(test, &st) == 0)
1591 {
1592 var = alloc(STRLEN(test) + 1);
1593 STRCPY(var, test);
1594 mustfree = TRUE;
1595 break;
1596 }
1597 }
1598 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001599# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 tail = (char_u *)""; // for gcc
1604#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606
1607#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 // If 'shellslash' is set change backslashes to forward slashes.
1609 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1611 {
1612 char_u *p = vim_strsave(var);
1613
1614 if (p != NULL)
1615 {
1616 if (mustfree)
1617 vim_free(var);
1618 var = p;
1619 mustfree = TRUE;
1620 forward_slash(var);
1621 }
1622 }
1623#endif
1624
Bram Moolenaar85a20022019-12-21 18:25:54 +01001625 // If "var" contains white space, escape it with a backslash.
1626 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1628 {
1629 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1630
1631 if (p != NULL)
1632 {
1633 if (mustfree)
1634 vim_free(var);
1635 var = p;
1636 mustfree = TRUE;
1637 }
1638 }
1639
1640 if (var != NULL && *var != NUL
1641 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1642 {
1643 STRCPY(dst, var);
1644 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001645 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001646 // if var[] ends in a path separator and tail[] starts
1647 // with it, skip a character
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001648 if (after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
zeertzjq13a01442024-03-09 17:44:46 +01001650 && dst[c - 1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#endif
1652 && vim_ispathsep(*tail))
1653 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001654 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 src = tail;
1656 copy_char = FALSE;
1657 }
1658 if (mustfree)
1659 vim_free(var);
1660 }
1661
Bram Moolenaar85a20022019-12-21 18:25:54 +01001662 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001665 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001666 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1667 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 */
1669 at_start = FALSE;
1670 if (src[0] == '\\' && src[1] != NUL)
1671 {
1672 *dst++ = *src++;
1673 --dstlen;
1674 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001675 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001677 if (dstlen > 0)
1678 {
1679 *dst++ = *src++;
1680 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001681
Bram Moolenaar1c864092017-08-06 18:15:45 +02001682 if (startstr != NULL && src - startstr_len >= srcp
1683 && STRNCMP(src - startstr_len, startstr,
1684 startstr_len) == 0)
1685 at_start = TRUE;
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 *dst = NUL;
1691}
1692
1693/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001694 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1695 * the length of "name/". Otherwise return "pend".
1696 */
1697 static char_u *
1698remove_tail(char_u *p, char_u *pend, char_u *name)
1699{
1700 int len = (int)STRLEN(name) + 1;
1701 char_u *newend = pend - len;
1702
1703 if (newend >= p
1704 && fnamencmp(newend, name, len - 1) == 0
1705 && (newend == p || after_pathsep(p, newend)))
1706 return newend;
1707 return pend;
1708}
1709
1710/*
1711 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1712 * Return NULL if not, return its name in allocated memory otherwise.
1713 */
1714 static char_u *
1715vim_version_dir(char_u *vimdir)
1716{
1717 char_u *p;
1718
1719 if (vimdir == NULL || *vimdir == NUL)
1720 return NULL;
1721 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1722 if (p != NULL && mch_isdir(p))
1723 return p;
1724 vim_free(p);
1725 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1726 if (p != NULL && mch_isdir(p))
Bram Moolenaar022f9ef2022-07-02 17:36:31 +01001727 {
1728 char_u *fname = concat_fnames(p, (char_u *)"defaults.vim", TRUE);
1729
1730 // Check that "defaults.vim" exists in this directory, to avoid picking
1731 // up a stray "runtime" directory, it would make many tests fail in
1732 // mysterious ways.
1733 if (fname != NULL)
1734 {
1735 int exists = file_is_readable(fname);
1736
1737 vim_free(fname);
1738 if (exists)
1739 return p;
1740 }
1741 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001742 vim_free(p);
1743 return NULL;
1744}
1745
1746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 * Vim's version of getenv().
1748 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001749 * Also does ACP to 'enc' conversion for Win32.
K.Takatace3189d2023-02-15 19:13:43 +00001750 * "mustfree" is set to TRUE when the returned string is allocated. It must be
Bram Moolenaarb453a532011-04-28 17:48:44 +02001751 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 */
1753 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001754vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001756 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 char_u *pend;
1758 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001759#ifdef MSWIN
1760 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001762 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (STRCMP(name, "HOME") == 0)
1764 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001766 // Use Wide function
1767 wn = enc_to_utf16(name, NULL);
1768 if (wn == NULL)
1769 return NULL;
1770
1771 wp = _wgetenv(wn);
1772 vim_free(wn);
1773
1774 if (wp != NULL && *wp == NUL) // empty is the same as not set
1775 wp = NULL;
1776
1777 if (wp != NULL)
1778 {
1779 p = utf16_to_enc(wp, NULL);
1780 if (p == NULL)
1781 return NULL;
1782
1783 *mustfree = TRUE;
1784 return p;
1785 }
1786#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001788 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 p = NULL;
1790
1791 if (p != NULL)
1792 return p;
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001793
1794# ifdef __HAIKU__
1795 // special handling for user settings directory...
1796 if (STRCMP(name, "BE_USER_SETTINGS") == 0)
1797 {
1798 static char userSettingsPath[MAXPATHL];
1799
1800 if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false,
1801 userSettingsPath, MAXPATHL) == B_OK)
1802 return (char_u *)userSettingsPath;
1803 else
1804 return NULL;
1805 }
1806# endif
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001809 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1811 if (!vimruntime && STRCMP(name, "VIM") != 0)
1812 return NULL;
1813
1814 /*
1815 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1816 * Don't do this when default_vimruntime_dir is non-empty.
1817 */
1818 if (vimruntime
1819#ifdef HAVE_PATHDEF
1820 && *default_vimruntime_dir == NUL
1821#endif
1822 )
1823 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001824#ifdef MSWIN
1825 // Use Wide function
1826 wp = _wgetenv(L"VIM");
1827 if (wp != NULL && *wp == NUL) // empty is the same as not set
1828 wp = NULL;
1829 if (wp != NULL)
1830 {
1831 char_u *q = utf16_to_enc(wp, NULL);
1832 if (q != NULL)
1833 {
1834 p = vim_version_dir(q);
1835 *mustfree = TRUE;
1836 if (p == NULL)
1837 p = q;
1838 }
1839 }
1840#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001842 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 p = NULL;
1844 if (p != NULL)
1845 {
1846 p = vim_version_dir(p);
1847 if (p != NULL)
1848 *mustfree = TRUE;
1849 else
1850 p = mch_getenv((char_u *)"VIM");
1851 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
1855 /*
1856 * When expanding $VIM or $VIMRUNTIME fails, try using:
1857 * - the directory name from 'helpfile' (unless it contains '$')
1858 * - the executable name from argv[0]
1859 */
1860 if (p == NULL)
1861 {
1862 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1863 p = p_hf;
1864#ifdef USE_EXE_NAME
1865 /*
1866 * Use the name of the executable, obtained from argv[0].
1867 */
1868 else
1869 p = exe_name;
1870#endif
1871 if (p != NULL)
1872 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001873 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 pend = gettail(p);
1875
Bram Moolenaar85a20022019-12-21 18:25:54 +01001876 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (p == p_hf)
1878 pend = remove_tail(p, pend, (char_u *)"doc");
1879
1880#ifdef USE_EXE_NAME
1881# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001882 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (p == exe_name)
1884 {
1885 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001886 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001888 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1889 if (pend1 != pend)
1890 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001891 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001892 if (pnew != NULL)
1893 {
1894 STRNCPY(pnew, p, (pend1 - p));
1895 STRCPY(pnew + (pend1 - p), "Resources/vim");
1896 p = pnew;
1897 pend = p + STRLEN(p);
1898 }
1899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001902 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (p == exe_name)
1904 pend = remove_tail(p, pend, (char_u *)"src");
1905#endif
1906
Bram Moolenaar85a20022019-12-21 18:25:54 +01001907 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (!vimruntime)
1909 {
1910 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1911 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1912 }
1913
Bram Moolenaar85a20022019-12-21 18:25:54 +01001914 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001915 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001918#ifdef MACOS_X
1919 if (p == exe_name || p == p_hf)
1920#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001921 // check that the result is a directory name
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001922 p = vim_strnsave(p, pend - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001925 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 else
1927 {
1928#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001929 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1931 {
1932 vim_free(p);
1933 p = pend;
1934 }
1935#endif
1936 *mustfree = TRUE;
1937 }
1938 }
1939 }
1940
1941#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001942 // When there is a pathdef.c file we can use default_vim_dir and
1943 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (p == NULL)
1945 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001946 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (vimruntime && *default_vimruntime_dir != NUL)
1948 {
1949 p = default_vimruntime_dir;
1950 *mustfree = FALSE;
1951 }
1952 else if (*default_vim_dir != NUL)
1953 {
1954 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1955 *mustfree = TRUE;
1956 else
1957 {
1958 p = default_vim_dir;
1959 *mustfree = FALSE;
1960 }
1961 }
1962 }
1963#endif
1964
1965 /*
1966 * Set the environment variable, so that the new value can be found fast
1967 * next time, and others can also use it (e.g. Perl).
1968 */
1969 if (p != NULL)
1970 {
1971 if (vimruntime)
1972 {
1973 vim_setenv((char_u *)"VIMRUNTIME", p);
1974 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 else
1977 {
1978 vim_setenv((char_u *)"VIM", p);
1979 didset_vim = TRUE;
1980 }
1981 }
1982 return p;
1983}
1984
Bram Moolenaar137374f2018-05-13 15:59:50 +02001985 void
1986vim_unsetenv(char_u *var)
1987{
1988#ifdef HAVE_UNSETENV
1989 unsetenv((char *)var);
1990#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001991 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001992#endif
1993}
1994
LemonBoy77142312022-04-15 20:50:46 +01001995/*
1996 * Removes environment variable "name" and take care of side effects.
1997 */
1998 void
1999vim_unsetenv_ext(char_u *var)
2000{
2001 vim_unsetenv(var);
2002
2003 // "homedir" is not cleared, keep using the old value until $HOME is set.
2004 if (STRICMP(var, "VIM") == 0)
2005 didset_vim = FALSE;
2006 else if (STRICMP(var, "VIMRUNTIME") == 0)
2007 didset_vimruntime = FALSE;
2008}
Bram Moolenaar137374f2018-05-13 15:59:50 +02002009
Bram Moolenaar092e09c2022-04-15 23:29:23 +01002010#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002012 * Set environment variable "name" and take care of side effects.
2013 */
2014 void
2015vim_setenv_ext(char_u *name, char_u *val)
2016{
2017 vim_setenv(name, val);
2018 if (STRICMP(name, "HOME") == 0)
2019 init_homedir();
2020 else if (didset_vim && STRICMP(name, "VIM") == 0)
2021 didset_vim = FALSE;
LemonBoy77142312022-04-15 20:50:46 +01002022 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002023 didset_vimruntime = FALSE;
2024}
Dominique Pelle748b3082022-01-08 12:41:16 +00002025#endif
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002026
2027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 * Our portable version of setenv.
2029 */
2030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
2033#ifdef HAVE_SETENV
2034 mch_setenv((char *)name, (char *)val, 1);
2035#else
2036 char_u *envbuf;
2037
2038 /*
2039 * Putenv does not copy the string, it has to remain
2040 * valid. The allocated memory will never be freed.
2041 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002042 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (envbuf != NULL)
2044 {
2045 sprintf((char *)envbuf, "%s=%s", name, val);
2046 putenv((char *)envbuf);
2047 }
2048#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002049#ifdef FEAT_GETTEXT
2050 /*
2051 * When setting $VIMRUNTIME adjust the directory to find message
2052 * translations to $VIMRUNTIME/lang.
2053 */
2054 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2055 {
2056 char_u *buf = concat_str(val, (char_u *)"/lang");
2057
2058 if (buf != NULL)
2059 {
2060 bindtextdomain(VIMPACKAGE, (char *)buf);
2061 vim_free(buf);
2062 }
2063 }
2064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065}
2066
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067/*
2068 * Function given to ExpandGeneric() to obtain an environment variable name.
2069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002071get_env_name(
2072 expand_T *xp UNUSED,
2073 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002075#if defined(AMIGA)
2076 // No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 return NULL;
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002078#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01002080 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 extern char **environ;
2082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 char_u *str;
2084 int n;
2085
2086 str = (char_u *)environ[idx];
2087 if (str == NULL)
2088 return NULL;
2089
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002090 for (n = 0; n < EXPAND_BUF_LEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
2092 if (str[n] == '=' || str[n] == NUL)
2093 break;
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002094 xp->xp_buf[n] = str[n];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002096 xp->xp_buf[n] = NUL;
2097 return xp->xp_buf;
2098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099}
Bram Moolenaar24305862012-08-15 14:05:05 +02002100
2101/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002102 * Add a user name to the list of users in ga_users.
2103 * Do nothing if user name is NULL or empty.
2104 */
2105 static void
2106add_user(char_u *user, int need_copy)
2107{
2108 char_u *user_copy = (user != NULL && need_copy)
2109 ? vim_strsave(user) : user;
2110
2111 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2112 {
2113 if (need_copy)
Christian Brabandt7a2f2172024-03-24 09:50:03 +01002114 vim_free(user_copy);
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002115 return;
2116 }
2117 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2118}
2119
2120/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002121 * Find all user names for user completion.
2122 * Done only once and then cached.
2123 */
2124 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002125init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002126{
Bram Moolenaar24305862012-08-15 14:05:05 +02002127 static int lazy_init_done = FALSE;
2128
2129 if (lazy_init_done)
2130 return;
2131
2132 lazy_init_done = TRUE;
2133 ga_init2(&ga_users, sizeof(char_u *), 20);
2134
2135# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2136 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002137 struct passwd* pw;
2138
2139 setpwent();
2140 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002141 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002142 endpwent();
2143 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002144# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002145 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002146 DWORD nusers = 0, ntotal = 0, i;
2147 PUSER_INFO_0 uinfo;
2148
2149 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2150 &nusers, &ntotal, NULL) == NERR_Success)
2151 {
2152 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002153 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002154
2155 NetApiBufferFree(uinfo);
2156 }
2157 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002158# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002159# if defined(HAVE_GETPWNAM)
2160 {
2161 char_u *user_env = mch_getenv((char_u *)"USER");
2162
2163 // The $USER environment variable may be a valid remote user name (NIS,
2164 // LDAP) not already listed by getpwent(), as getpwent() only lists
2165 // local user names. If $USER is not already listed, check whether it
2166 // is a valid remote user name using getpwnam() and if it is, add it to
2167 // the list of user names.
2168
2169 if (user_env != NULL && *user_env != NUL)
2170 {
2171 int i;
2172
2173 for (i = 0; i < ga_users.ga_len; i++)
2174 {
2175 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2176
2177 if (STRCMP(local_user, user_env) == 0)
2178 break;
2179 }
2180
2181 if (i == ga_users.ga_len)
2182 {
2183 struct passwd *pw = getpwnam((char *)user_env);
2184
2185 if (pw != NULL)
2186 add_user((char_u *)pw->pw_name, TRUE);
2187 }
2188 }
2189 }
2190# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002191}
2192
2193/*
zeertzjqc029c132024-03-28 11:37:26 +01002194 * Function given to ExpandGeneric() to obtain user names.
Bram Moolenaar24305862012-08-15 14:05:05 +02002195 */
2196 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002197get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002198{
2199 init_users();
2200 if (idx < ga_users.ga_len)
2201 return ((char_u **)ga_users.ga_data)[idx];
2202 return NULL;
2203}
2204
2205/*
2206 * Check whether name matches a user name. Return:
2207 * 0 if name does not match any user name.
2208 * 1 if name partially matches the beginning of a user name.
2209 * 2 is name fully matches a user name.
2210 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002211 int
2212match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002213{
2214 int i;
2215 int n = (int)STRLEN(name);
2216 int result = 0;
2217
2218 init_users();
2219 for (i = 0; i < ga_users.ga_len; i++)
2220 {
2221 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002222 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002223 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002225 }
2226 return result;
2227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002230prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002232#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002233 // Ignore SIGHUP, because a dropped connection causes a read error, which
2234 // makes Vim exit and then handling SIGHUP causes various reentrance
2235 // problems.
ichizok378447f2023-05-11 22:25:42 +01002236 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002237#endif
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#ifdef FEAT_GUI
2240 if (gui.in_use)
2241 {
2242 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002243 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245 else
2246#endif
2247 {
2248 windgoto((int)Rows - 1, 0);
2249
2250 /*
2251 * Switch terminal mode back now, so messages end up on the "normal"
2252 * screen (if there are two screens).
2253 */
2254 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002255 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 out_flush();
2257 }
2258}
2259
2260/*
2261 * Preserve files and exit.
2262 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002263 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2264 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 */
2266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002267preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 buf_T *buf;
2270
2271 prepare_to_exit();
2272
Bram Moolenaar85a20022019-12-21 18:25:54 +01002273 // Setting this will prevent free() calls. That avoids calling free()
2274 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002275 really_exiting = TRUE;
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002278 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 out_flush();
2280
Bram Moolenaar85a20022019-12-21 18:25:54 +01002281 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar29323592016-07-24 22:04:11 +02002283 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2286 {
Bram Moolenaar69212b12020-05-10 14:14:03 +02002287 OUT_STR("Vim: preserving files...\r\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002288 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002290 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 break;
2292 }
2293 }
2294
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaar69212b12020-05-10 14:14:03 +02002297 OUT_STR("Vim: Finished.\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 getout(1);
2300}
2301
2302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 * Check for CTRL-C pressed, but only once in a while.
2304 * Should be used instead of ui_breakcheck() for functions that check for
2305 * each line in the file. Calling ui_breakcheck() each time takes too much
2306 * time, because it can be a system call.
2307 */
2308
2309#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002310# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#endif
2312
2313static int breakcheck_count = 0;
2314
2315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002316line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 if (++breakcheck_count >= BREAKCHECK_SKIP)
2319 {
2320 breakcheck_count = 0;
2321 ui_breakcheck();
2322 }
2323}
2324
2325/*
2326 * Like line_breakcheck() but check 10 times less often.
2327 */
2328 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002329fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2332 {
2333 breakcheck_count = 0;
2334 ui_breakcheck();
2335 }
2336}
2337
Dominique Pelle748b3082022-01-08 12:41:16 +00002338# if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002339/*
2340 * Like line_breakcheck() but check 100 times less often.
2341 */
2342 void
2343veryfast_breakcheck(void)
2344{
2345 if (++breakcheck_count >= BREAKCHECK_SKIP * 100)
2346 {
2347 breakcheck_count = 0;
2348 ui_breakcheck();
2349 }
2350}
Dominique Pelle748b3082022-01-08 12:41:16 +00002351#endif
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002352
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002353#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2354 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2355 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357#ifndef SEEK_SET
2358# define SEEK_SET 0
2359#endif
2360#ifndef SEEK_END
2361# define SEEK_END 2
2362#endif
2363
2364/*
2365 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002366 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2367 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 * Returns an allocated string, or NULL for error.
2369 */
2370 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002371get_cmd_output(
2372 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 char_u *infile, // optional input file name
2374 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002375 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
2377 char_u *tempname;
2378 char_u *command;
2379 char_u *buffer = NULL;
2380 int len;
2381 int i = 0;
2382 FILE *fd;
2383
2384 if (check_restricted() || check_secure())
2385 return NULL;
2386
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002388 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002390 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 return NULL;
2392 }
2393
Bram Moolenaar85a20022019-12-21 18:25:54 +01002394 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002395 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (command == NULL)
2397 goto done;
2398
2399 /*
2400 * Call the shell to execute the command (errors are ignored).
2401 * Don't check timestamps here.
2402 */
2403 ++no_check_timestamps;
2404 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2405 --no_check_timestamps;
2406
2407 vim_free(command);
2408
2409 /*
2410 * read the names from the file into memory
2411 */
2412# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 fd = mch_fopen((char *)tempname, "r");
2415# else
2416 fd = mch_fopen((char *)tempname, READBIN);
2417# endif
2418
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002419 // Not being able to seek means we can't read the file.
2420 if (fd == NULL
2421 || fseek(fd, 0L, SEEK_END) == -1
2422 || (len = ftell(fd)) == -1 // get size of temp file
2423 || fseek(fd, 0L, SEEK_SET) == -1) // back to the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 {
Bram Moolenaara9549c92022-04-17 14:18:11 +01002425 semsg(_(e_cannot_read_from_str_2), tempname);
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002426 if (fd != NULL)
2427 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 goto done;
2429 }
2430
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 buffer = alloc(len + 1);
2432 if (buffer != NULL)
2433 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2434 fclose(fd);
2435 mch_remove(tempname);
2436 if (buffer == NULL)
2437 goto done;
2438#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#endif
2441 if (i != len)
2442 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002443 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002444 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002446 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002447 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002448 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002449 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002450 if (buffer[i] == NUL)
2451 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002452
Bram Moolenaar85a20022019-12-21 18:25:54 +01002453 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002454 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002455 else
2456 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458done:
2459 vim_free(tempname);
2460 return buffer;
2461}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002462
2463# if defined(FEAT_EVAL) || defined(PROTO)
2464
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002465 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002466get_cmd_output_as_rettv(
2467 typval_T *argvars,
2468 typval_T *rettv,
2469 int retlist)
2470{
2471 char_u *res = NULL;
2472 char_u *p;
2473 char_u *infile = NULL;
2474 int err = FALSE;
2475 FILE *fd;
2476 list_T *list = NULL;
2477 int flags = SHELL_SILENT;
2478
2479 rettv->v_type = VAR_STRING;
2480 rettv->vval.v_string = NULL;
2481 if (check_restricted() || check_secure())
2482 goto errret;
2483
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002484 if (in_vim9script()
2485 && (check_for_string_arg(argvars, 0) == FAIL
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01002486 || check_for_opt_string_or_number_or_list_arg(argvars, 1)
2487 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002488 return;
2489
Bram Moolenaar26262f82019-09-04 20:59:15 +02002490 if (argvars[1].v_type != VAR_UNKNOWN)
2491 {
2492 /*
2493 * Write the text to a temp file, to be used for input of the shell
2494 * command.
2495 */
2496 if ((infile = vim_tempname('i', TRUE)) == NULL)
2497 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002498 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002499 goto errret;
2500 }
2501
2502 fd = mch_fopen((char *)infile, WRITEBIN);
2503 if (fd == NULL)
2504 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002505 semsg(_(e_cant_open_file_str), infile);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002506 goto errret;
2507 }
2508 if (argvars[1].v_type == VAR_NUMBER)
2509 {
2510 linenr_T lnum;
2511 buf_T *buf;
2512
2513 buf = buflist_findnr(argvars[1].vval.v_number);
2514 if (buf == NULL)
2515 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002516 semsg(_(e_buffer_nr_does_not_exist), argvars[1].vval.v_number);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002517 fclose(fd);
2518 goto errret;
2519 }
2520
2521 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2522 {
2523 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2524 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2525 {
2526 err = TRUE;
2527 break;
2528 }
2529 if (putc(NL, fd) == EOF)
2530 {
2531 err = TRUE;
2532 break;
2533 }
2534 }
2535 }
2536 else if (argvars[1].v_type == VAR_LIST)
2537 {
2538 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2539 err = TRUE;
2540 }
2541 else
2542 {
2543 size_t len;
2544 char_u buf[NUMBUFLEN];
2545
2546 p = tv_get_string_buf_chk(&argvars[1], buf);
2547 if (p == NULL)
2548 {
2549 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002550 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002551 }
2552 len = STRLEN(p);
2553 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2554 err = TRUE;
2555 }
2556 if (fclose(fd) != 0)
2557 err = TRUE;
2558 if (err)
2559 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002560 emsg(_(e_error_writing_temp_file));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002561 goto errret;
2562 }
2563 }
2564
Bram Moolenaar85a20022019-12-21 18:25:54 +01002565 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2566 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002567 if (!msg_silent)
2568 flags += SHELL_COOKED;
2569
2570 if (retlist)
2571 {
2572 int len;
2573 listitem_T *li;
2574 char_u *s = NULL;
2575 char_u *start;
2576 char_u *end;
2577 int i;
2578
2579 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2580 if (res == NULL)
2581 goto errret;
2582
2583 list = list_alloc();
2584 if (list == NULL)
2585 goto errret;
2586
2587 for (i = 0; i < len; ++i)
2588 {
2589 start = res + i;
2590 while (i < len && res[i] != NL)
2591 ++i;
2592 end = res + i;
2593
2594 s = alloc(end - start + 1);
2595 if (s == NULL)
2596 goto errret;
2597
2598 for (p = s; start < end; ++p, ++start)
2599 *p = *start == NUL ? NL : *start;
2600 *p = NUL;
2601
2602 li = listitem_alloc();
2603 if (li == NULL)
2604 {
2605 vim_free(s);
2606 goto errret;
2607 }
2608 li->li_tv.v_type = VAR_STRING;
2609 li->li_tv.v_lock = 0;
2610 li->li_tv.vval.v_string = s;
2611 list_append(list, li);
2612 }
2613
2614 rettv_list_set(rettv, list);
2615 list = NULL;
2616 }
2617 else
2618 {
2619 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2620#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002621 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002622 if (res != NULL)
2623 {
2624 char_u *s, *d;
2625
2626 d = res;
2627 for (s = res; *s; ++s)
2628 {
2629 if (s[0] == CAR && s[1] == NL)
2630 ++s;
2631 *d++ = *s;
2632 }
2633 *d = NUL;
2634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002636 rettv->vval.v_string = res;
2637 res = NULL;
2638 }
2639
2640errret:
2641 if (infile != NULL)
2642 {
2643 mch_remove(infile);
2644 vim_free(infile);
2645 }
2646 if (res != NULL)
2647 vim_free(res);
2648 if (list != NULL)
2649 list_free(list);
2650}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002653 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 */
2655 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002656f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002658 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659}
2660
2661/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002662 * "systemlist()" function
2663 */
2664 void
2665f_systemlist(typval_T *argvars, typval_T *rettv)
2666{
2667 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2668}
2669# endif // FEAT_EVAL
2670
2671#endif
2672
2673/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002674 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 * Don't do this when still processing a command or a mapping.
2676 * Don't do this when inside a ":normal" command.
2677 */
2678 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002679goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 return (p_im && stuff_empty() && typebuf_typed());
2682}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002683
2684/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002685 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002686 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2687 * - Remove any argument. E.g., "csh -f" -> "csh".
2688 * But don't allow a space in the path, so that this works:
2689 * "/usr/bin/csh --rcfile ~/.cshrc"
2690 * But don't do that for Windows, it's common to have a space in the path.
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002691 * Returns NULL when out of memory.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002692 */
2693 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002694get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002695{
2696 char_u *p;
2697
Bram Moolenaar4f974752019-02-17 17:44:42 +01002698#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002699 p = gettail(p_sh);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002700 p = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002701#else
2702 p = skiptowhite(p_sh);
2703 if (*p == NUL)
2704 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002706 p = vim_strsave(gettail(p_sh));
2707 }
2708 else
2709 {
2710 char_u *p1, *p2;
2711
Bram Moolenaar85a20022019-12-21 18:25:54 +01002712 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002713 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002714 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002715 if (vim_ispathsep(*p2))
2716 p1 = p2 + 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002717 p = vim_strnsave(p1, p - p1);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002718 }
2719#endif
2720 return p;
2721}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002722
2723/*
2724 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2725 * Also check for ":\\", which MS Internet Explorer accepts, return
2726 * URL_BACKSLASH.
2727 */
2728 int
2729path_is_url(char_u *p)
2730{
2731 if (STRNCMP(p, "://", (size_t)3) == 0)
2732 return URL_SLASH;
2733 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2734 return URL_BACKSLASH;
2735 return 0;
2736}
2737
2738/*
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002739 * Check if "fname" starts with "name://" or "name:\\".
2740 * Return URL_SLASH for "name://", URL_BACKSLASH for "name:\\".
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002741 * Return zero otherwise.
2742 */
2743 int
2744path_with_url(char_u *fname)
2745{
2746 char_u *p;
2747
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002748 // We accept alphabetic characters and a dash in scheme part.
2749 // RFC 3986 allows for more, but it increases the risk of matching
2750 // non-URL text.
2751
2752 // first character must be alpha
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002753 if (!ASCII_ISALPHA(*fname))
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002754 return 0;
2755
2756 // check body: alpha or dash
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002757 for (p = fname + 1; (ASCII_ISALPHA(*p) || (*p == '-')); ++p)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002758 ;
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002759
2760 // check last char is not a dash
2761 if (p[-1] == '-')
2762 return 0;
2763
2764 // "://" or ":\\" must follow
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002765 return path_is_url(p);
2766}
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002767
Bram Moolenaar3075a452021-11-17 15:51:52 +00002768#if defined(FEAT_EVAL) || defined(PROTO)
2769/*
2770 * Return the dictionary of v:event.
2771 * Save and clear the value in case it already has items.
2772 */
2773 dict_T *
2774get_v_event(save_v_event_T *sve)
2775{
2776 dict_T *v_event = get_vim_var_dict(VV_EVENT);
2777
2778 if (v_event->dv_hashtab.ht_used > 0)
2779 {
2780 // recursive use of v:event, save, make empty and restore later
2781 sve->sve_did_save = TRUE;
2782 sve->sve_hashtab = v_event->dv_hashtab;
2783 hash_init(&v_event->dv_hashtab);
2784 }
2785 else
2786 sve->sve_did_save = FALSE;
2787 return v_event;
2788}
2789
2790 void
2791restore_v_event(dict_T *v_event, save_v_event_T *sve)
2792{
2793 dict_free_contents(v_event);
2794 if (sve->sve_did_save)
2795 v_event->dv_hashtab = sve->sve_hashtab;
2796 else
2797 hash_init(&v_event->dv_hashtab);
2798}
2799#endif
2800
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002801/*
LemonBoy2bf52dd2022-04-09 18:17:34 +01002802 * Fires a ModeChanged autocmd event if appropriate.
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002803 */
2804 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002805may_trigger_modechanged(void)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002806{
Bram Moolenaar3075a452021-11-17 15:51:52 +00002807#ifdef FEAT_EVAL
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002808 dict_T *v_event;
Bram Moolenaar3075a452021-11-17 15:51:52 +00002809 save_v_event_T save_v_event;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002810 char_u curr_mode[MODE_MAX_LENGTH];
2811 char_u pattern_buf[2 * MODE_MAX_LENGTH];
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002812
Bram Moolenaar61c4b042022-10-18 15:10:11 +01002813 // Skip this when got_int is set, the autocommand will not be executed.
2814 // Better trigger it next time.
2815 if (!has_modechanged() || got_int)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002816 return;
2817
LemonBoy2bf52dd2022-04-09 18:17:34 +01002818 get_mode(curr_mode);
2819 if (STRCMP(curr_mode, last_mode) == 0)
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002820 return;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002821
Bram Moolenaar3075a452021-11-17 15:51:52 +00002822 v_event = get_v_event(&save_v_event);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002823 (void)dict_add_string(v_event, "new_mode", curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002824 (void)dict_add_string(v_event, "old_mode", last_mode);
2825 dict_set_items_ro(v_event);
2826
2827 // concatenate modes in format "old_mode:new_mode"
LemonBoy2bf52dd2022-04-09 18:17:34 +01002828 vim_snprintf((char *)pattern_buf, sizeof(pattern_buf), "%s:%s", last_mode,
2829 curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002830
LemonBoy2bf52dd2022-04-09 18:17:34 +01002831 apply_autocmds(EVENT_MODECHANGED, pattern_buf, NULL, FALSE, curbuf);
2832 STRCPY(last_mode, curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002833
Bram Moolenaar3075a452021-11-17 15:51:52 +00002834 restore_v_event(v_event, &save_v_event);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002835#endif
2836}
Christian Brabandt22cbc8a2023-11-19 10:47:21 +01002837
Christian Brabandt22cbc8a2023-11-19 10:47:21 +01002838// For overflow detection, add a digit safely to a long value.
2839 int
2840vim_append_digit_long(long *value, int digit)
2841{
2842 long x = *value;
2843 if (x > ((LONG_MAX - (long)digit) / 10))
2844 return FAIL;
2845 *value = x * 10 + (long)digit;
2846 return OK;
2847}
Ernie Rael2b0882f2023-11-23 20:21:45 +01002848
2849// Return something that fits into an int.
2850 int
Ernie Raelfda700c2023-11-30 18:20:00 +01002851trim_to_int(vimlong_T x)
Ernie Rael2b0882f2023-11-23 20:21:45 +01002852{
2853 return x > INT_MAX ? INT_MAX : x < INT_MIN ? INT_MIN : x;
2854}
2855