blob: c5a0c385271e2fec9dcb4878ddf4cbaa00cdd511 [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
500 int
zeertzjqbfe377b2023-08-17 22:58:53 +0200501plines_m_win(win_T *wp, linenr_T first, linenr_T last, int limit_winheight)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 int count = 0;
504
Luuk van Baal08b0f632024-04-09 22:43:49 +0200505 while (first <= last && (!limit_winheight || count < wp->w_height))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
507#ifdef FEAT_FOLDING
508 int x;
509
Bram Moolenaar85a20022019-12-21 18:25:54 +0100510 // Check if there are any really folded lines, but also included lines
511 // that are maybe folded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 x = foldedCount(wp, first, NULL);
513 if (x > 0)
514 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100515 ++count; // count 1 for "+-- folded" line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 first += x;
517 }
518 else
519#endif
520 {
521#ifdef FEAT_DIFF
522 if (first == wp->w_topline)
Luuk van Baal08b0f632024-04-09 22:43:49 +0200523 count += plines_win_nofill(wp, first, FALSE) + wp->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 else
525#endif
Luuk van Baal08b0f632024-04-09 22:43:49 +0200526 count += plines_win(wp, first, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 ++first;
528 }
529 }
Luuk van Baal08b0f632024-04-09 22:43:49 +0200530#ifdef FEAT_DIFF
531 if (first == wp->w_buffer->b_ml.ml_line_count + 1)
532 count += diff_check_fill(wp, first);
533#endif
534 if (limit_winheight && count > wp->w_height)
535 return wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 return (count);
537}
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100540gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100542 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
Bram Moolenaar85a20022019-12-21 18:25:54 +0100544 // When searching columns is sometimes put at the end of a line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100545 if (pos->col == MAXCOL)
546 return NUL;
547 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 if (has_mbyte)
549 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 return (int)*ptr;
551}
552
553 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100554gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (has_mbyte)
557 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 return (int)*ml_get_cursor();
559}
560
561/*
562 * Write a character at the current cursor position.
563 * It is directly written into the block.
564 */
565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100566pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
569 + curwin->w_cursor.col) = c;
570}
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 * Skip to next part of an option argument: Skip space and comma.
574 */
575 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100576skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
578 if (*p == ',')
579 ++p;
580 while (*p == ' ')
581 ++p;
582 return p;
583}
584
585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 * check_status: called when the status bars for the buffer 'buf'
587 * need to be updated
588 */
589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100590check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 win_T *wp;
593
Bram Moolenaar29323592016-07-24 22:04:11 +0200594 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (wp->w_buffer == buf && wp->w_status_height)
596 {
597 wp->w_redr_status = TRUE;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100598 set_must_redraw(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
600}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602/*
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100603 * Ask for a reply from the user, a 'y' or a 'n', with prompt "str" (which
604 * should have been translated already).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 * No other characters are accepted, the message is repeated until a valid
606 * reply is entered or CTRL-C is hit.
607 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
608 * from any buffers but directly from the user.
609 *
610 * return the 'y' or 'n'
611 */
612 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100613ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 int r = ' ';
616 int save_State = State;
617
Bram Moolenaar85a20022019-12-21 18:25:54 +0100618 if (exiting) // put terminal in raw mode for this question
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 settmode(TMODE_RAW);
620 ++no_wait_return;
621#ifdef USE_ON_FLY_SCROLL
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200622 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100624 State = MODE_CONFIRM; // mouse behaves like with :confirm
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200625 setmouse(); // disables mouse for xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ++no_mapping;
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200627 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
629 while (r != 'y' && r != 'n')
630 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100631 // same highlighting as for wait_return()
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100632 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (direct)
634 r = get_keystroke();
635 else
Bram Moolenaar913626c2008-01-03 11:43:42 +0000636 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (r == Ctrl_C || r == ESC)
638 r = 'n';
Bram Moolenaar85a20022019-12-21 18:25:54 +0100639 msg_putchar(r); // show what you typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 out_flush();
641 }
642 --no_wait_return;
643 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 --no_mapping;
646 --allow_keys;
647
648 return r;
649}
650
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200651#if defined(FEAT_EVAL) || defined(PROTO)
652
653/*
LemonBoy2bf52dd2022-04-09 18:17:34 +0100654 * Returns the current mode as a string in "buf[MODE_MAX_LENGTH]", NUL
655 * terminated.
656 * The first character represents the major mode, the following ones the minor
657 * ones.
658 */
659 void
660get_mode(char_u *buf)
661{
662 int i = 0;
663
664 if (time_for_testing == 93784)
665 {
666 // Testing the two-character code.
667 buf[i++] = 'x';
668 buf[i++] = '!';
669 }
670#ifdef FEAT_TERMINAL
671 else if (term_use_loop())
h-east71ebf3b2023-09-03 17:12:55 +0200672 {
673 if (State & MODE_CMDLINE)
674 buf[i++] = 'c';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100675 buf[i++] = 't';
h-east71ebf3b2023-09-03 17:12:55 +0200676 }
LemonBoy2bf52dd2022-04-09 18:17:34 +0100677#endif
678 else if (VIsual_active)
679 {
680 if (VIsual_select)
681 buf[i++] = VIsual_mode + 's' - 'v';
682 else
683 {
684 buf[i++] = VIsual_mode;
685 if (restart_VIsual_select)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100686 buf[i++] = 's';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100687 }
688 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100689 else if (State == MODE_HITRETURN || State == MODE_ASKMORE
690 || State == MODE_SETWSIZE
691 || State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100692 {
693 buf[i++] = 'r';
Bram Moolenaar24959102022-05-07 20:01:16 +0100694 if (State == MODE_ASKMORE)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100695 buf[i++] = 'm';
Bram Moolenaar24959102022-05-07 20:01:16 +0100696 else if (State == MODE_CONFIRM)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100697 buf[i++] = '?';
698 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100699 else if (State == MODE_EXTERNCMD)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100700 buf[i++] = '!';
Bram Moolenaar24959102022-05-07 20:01:16 +0100701 else if (State & MODE_INSERT)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100702 {
703 if (State & VREPLACE_FLAG)
704 {
705 buf[i++] = 'R';
706 buf[i++] = 'v';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100707 }
708 else
709 {
710 if (State & REPLACE_FLAG)
711 buf[i++] = 'R';
712 else
713 buf[i++] = 'i';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100714 }
zeertzjq590f3652022-04-29 11:29:54 +0100715
716 if (ins_compl_active())
717 buf[i++] = 'c';
718 else if (ctrl_x_mode_not_defined_yet())
719 buf[i++] = 'x';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100720 }
Bram Moolenaar24959102022-05-07 20:01:16 +0100721 else if ((State & MODE_CMDLINE) || exmode_active)
LemonBoy2bf52dd2022-04-09 18:17:34 +0100722 {
723 buf[i++] = 'c';
724 if (exmode_active == EXMODE_VIM)
725 buf[i++] = 'v';
726 else if (exmode_active == EXMODE_NORMAL)
727 buf[i++] = 'e';
Sam-programsd1c3ef12023-11-27 22:22:51 +0100728 if ((State & MODE_CMDLINE) && cmdline_overstrike())
729 buf[i++] = 'r';
LemonBoy2bf52dd2022-04-09 18:17:34 +0100730 }
731 else
732 {
733 buf[i++] = 'n';
734 if (finish_op)
735 {
736 buf[i++] = 'o';
737 // to be able to detect force-linewise/blockwise/characterwise
738 // operations
739 buf[i++] = motion_force;
740 }
741 else if (restart_edit == 'I' || restart_edit == 'R'
742 || restart_edit == 'V')
743 {
744 buf[i++] = 'i';
745 buf[i++] = restart_edit;
746 }
747#ifdef FEAT_TERMINAL
748 else if (term_in_normal_mode())
749 buf[i++] = 't';
750#endif
751 }
752
753 buf[i] = NUL;
754}
755
756/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200757 * "mode()" function
758 */
759 void
760f_mode(typval_T *argvars, typval_T *rettv)
761{
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200762 char_u buf[MODE_MAX_LENGTH];
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200763
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200764 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
765 return;
766
LemonBoy2bf52dd2022-04-09 18:17:34 +0100767 get_mode(buf);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200768
Bram Moolenaar85a20022019-12-21 18:25:54 +0100769 // Clear out the minor mode when the argument is not a non-zero number or
770 // non-empty string.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200771 if (!non_zero_arg(&argvars[0]))
772 buf[1] = NUL;
773
774 rettv->vval.v_string = vim_strsave(buf);
775 rettv->v_type = VAR_STRING;
776}
777
778 static void
779may_add_state_char(garray_T *gap, char_u *include, int c)
780{
781 if (include == NULL || vim_strchr(include, c) != NULL)
782 ga_append(gap, c);
783}
784
785/*
786 * "state()" function
787 */
788 void
789f_state(typval_T *argvars, typval_T *rettv)
790{
791 garray_T ga;
792 char_u *include = NULL;
793 int i;
794
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200795 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
796 return;
797
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200798 ga_init2(&ga, 1, 20);
799 if (argvars[0].v_type != VAR_UNKNOWN)
800 include = tv_get_string(&argvars[0]);
801
802 if (!(stuff_empty() && typebuf.tb_len == 0 && scriptin[curscript] == NULL))
803 may_add_state_char(&ga, include, 'm');
804 if (op_pending())
805 may_add_state_char(&ga, include, 'o');
806 if (autocmd_busy)
807 may_add_state_char(&ga, include, 'x');
Bram Moolenaarc2585492019-09-22 21:29:53 +0200808 if (ins_compl_active())
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200809 may_add_state_char(&ga, include, 'a');
810
811# ifdef FEAT_JOB_CHANNEL
812 if (channel_in_blocking_wait())
813 may_add_state_char(&ga, include, 'w');
814# endif
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200815 if (!get_was_safe_state())
816 may_add_state_char(&ga, include, 'S');
Bram Moolenaar0e57dd82019-09-16 22:56:03 +0200817 for (i = 0; i < get_callback_depth() && i < 3; ++i)
818 may_add_state_char(&ga, include, 'c');
819 if (msg_scrolled > 0)
820 may_add_state_char(&ga, include, 's');
821
822 rettv->v_type = VAR_STRING;
823 rettv->vval.v_string = ga.ga_data;
824}
825
826#endif // FEAT_EVAL
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828/*
829 * Get a key stroke directly from the user.
830 * Ignores mouse clicks and scrollbar events, except a click for the left
831 * button (used at the more prompt).
832 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
833 * Disadvantage: typeahead is ignored.
834 * Translates the interrupt character for unix to ESC.
835 */
836 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100839 char_u *buf = NULL;
840 int buflen = 150;
841 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int len = 0;
843 int n;
844 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000845 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaar85a20022019-12-21 18:25:54 +0100847 mapped_ctrl_c = FALSE; // mappings are not used here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 for (;;)
849 {
850 cursor_on();
851 out_flush();
852
Bram Moolenaar85a20022019-12-21 18:25:54 +0100853 // Leave some room for check_termcode() to insert a key code into (max
854 // 5 chars plus NUL). And fix_input_buffer() can triple the number of
855 // bytes.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100856 maxlen = (buflen - 6 - len) / 3;
857 if (buf == NULL)
858 buf = alloc(buflen);
859 else if (maxlen < 10)
860 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100861 char_u *t_buf = buf;
862
Bram Moolenaar85a20022019-12-21 18:25:54 +0100863 // Need some more space. This might happen when receiving a long
864 // escape sequence.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100865 buflen += 100;
866 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100867 if (buf == NULL)
868 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100869 maxlen = (buflen - 6 - len) / 3;
870 }
871 if (buf == NULL)
872 {
873 do_outofmem_msg((long_u)buflen);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100874 return ESC; // panic!
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100875 }
876
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 // First time: blocking wait. Second time: wait up to 100ms for a
878 // terminal code to complete.
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100879 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (n > 0)
881 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100882 // Replace zero and CSI by a special key code.
Bram Moolenaar6bff02e2016-08-16 22:50:55 +0200883 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000885 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
Bram Moolenaar4395a712006-09-05 18:57:57 +0000887 else if (len > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100888 ++waited; // keep track of the waiting time
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaar85a20022019-12-21 18:25:54 +0100890 // Incomplete termcode and not timed out yet: get more characters
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100891 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +0000892 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +0000894
Bram Moolenaar85a20022019-12-21 18:25:54 +0100895 if (n == KEYLEN_REMOVED) // key code removed
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100896 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100897 if (must_redraw != 0 && !need_wait_return && (State
898 & (MODE_CMDLINE | MODE_HITRETURN | MODE_ASKMORE)) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100899 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100900 // Redrawing was postponed, do it now.
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100901 update_screen(0);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100902 setcursor(); // put cursor back where it belongs
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100903 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +0100904 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +0100905 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100906 if (n > 0) // found a termcode: adjust length
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 len = n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100908 if (len == 0) // nothing typed yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 continue;
910
Bram Moolenaar85a20022019-12-21 18:25:54 +0100911 // Handle modifier and/or special key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 n = buf[0];
913 if (n == K_SPECIAL)
914 {
915 n = TO_SPECIAL(buf[1], buf[2]);
916 if (buf[1] == KS_MODIFIER
917 || n == K_IGNORE
Bram Moolenaar2526ef22013-03-16 14:20:51 +0100918 || (is_mouse_key(n) && n != K_LEFTMOUSE)
919#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 || n == K_VER_SCROLLBAR
921 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922#endif
923 )
924 {
925 if (buf[1] == KS_MODIFIER)
926 mod_mask = buf[2];
927 len -= 3;
928 if (len > 0)
929 mch_memmove(buf, buf + 3, (size_t)len);
930 continue;
931 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (has_mbyte)
935 {
936 if (MB_BYTE2LEN(n) > len)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100937 continue; // more bytes to get
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100938 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 n = (*mb_ptr2char)(buf);
940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#ifdef UNIX
942 if (n == intr_char)
943 n = ESC;
944#endif
945 break;
946 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +0100947 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 mapped_ctrl_c = save_mapped_ctrl_c;
950 return n;
951}
952
953/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000954 * Get a number from the user.
955 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 */
957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100958get_number(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100959 int colon, // allow colon to abort
Bram Moolenaar9b578142016-01-30 19:39:49 +0100960 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 int n = 0;
963 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000964 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000966 if (mouse_used != NULL)
967 *mouse_used = FALSE;
968
Bram Moolenaar85a20022019-12-21 18:25:54 +0100969 // When not printing messages, the user won't know what to type, return a
970 // zero (as if CR was hit).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (msg_silent != 0)
972 return 0;
973
974#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100975 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#endif
977 ++no_mapping;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100978 ++allow_keys; // no mapping here, but recognize keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 for (;;)
980 {
981 windgoto(msg_row, msg_col);
982 c = safe_vgetc();
983 if (VIM_ISDIGIT(c))
984 {
Christian Brabandt22cbc8a2023-11-19 10:47:21 +0100985 if (vim_append_digit_int(&n, c - '0') == FAIL)
Christian Brabandt73b2d372023-11-14 21:58:26 +0100986 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000988 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
991 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000992 if (typed > 0)
993 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100994 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +0000995 --typed;
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000999 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1000 {
1001 *mouse_used = TRUE;
1002 n = mouse_row + 1;
1003 break;
1004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 else if (n == 0 && c == ':' && colon)
1006 {
1007 stuffcharReadbuff(':');
1008 if (!exmode_active)
1009 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001010 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 do_redraw = FALSE;
1012 break;
1013 }
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +02001014 else if (c == Ctrl_C || c == ESC || c == 'q')
1015 {
1016 n = 0;
1017 break;
1018 }
1019 else if (c == CAR || c == NL )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021 }
1022 --no_mapping;
1023 --allow_keys;
1024 return n;
1025}
1026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001027/*
1028 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001029 * When "mouse_used" is not NULL allow using the mouse and in that case return
1030 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001031 */
1032 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001033prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001034{
1035 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001036 int save_cmdline_row;
1037 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038
Bram Moolenaar85a20022019-12-21 18:25:54 +01001039 // When using ":silent" assume that <CR> was entered.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001040 if (mouse_used != NULL)
Bram Moolenaareebd5552020-06-10 15:45:57 +02001041 msg_puts(_("Type number and <Enter> or click with the mouse (q or empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001042 else
Bram Moolenaareebd5552020-06-10 15:45:57 +02001043 msg_puts(_("Type number and <Enter> (q or empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001044
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001045 // Set the state such that text can be selected/copied/pasted and we still
1046 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1047 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001048 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001049 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001050 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01001051 State = MODE_CMDLINE;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001052 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001053 setmouse();
Bram Moolenaar73658312018-04-24 17:41:57 +02001054
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001055 i = get_number(TRUE, mouse_used);
1056 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001057 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001058 // don't call wait_return() now
1059 if (msg_row > 0)
1060 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001061 need_wait_return = FALSE;
Bram Moolenaardc968e72019-11-05 21:53:20 +01001062 msg_didany = FALSE;
1063 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001064 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001065 else
1066 cmdline_row = save_cmdline_row;
1067 State = save_State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001068 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001069 setmouse();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001071 return i;
1072}
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 long pn;
1078
Bram Moolenaar85a20022019-12-21 18:25:54 +01001079 if (global_busy // no messages now, wait until global is finished
1080 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 return;
1082
Bram Moolenaar85a20022019-12-21 18:25:54 +01001083 // We don't want to overwrite another important message, but do overwrite
1084 // a previous "more lines" or "fewer lines" message, so that "5dd" and
1085 // then "put" reports the last action.
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001086 if (keep_msg != NULL && !keep_msg_more)
1087 return;
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (n > 0)
1090 pn = n;
1091 else
1092 pn = -n;
1093
1094 if (pn > p_report)
1095 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001096 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001097 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001098 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001100 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001101 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001103 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1104 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (msg(msg_buf))
1106 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001107 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001108 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110 }
1111}
1112
1113/*
1114 * flush map and typeahead buffers and give a warning for an error
1115 */
1116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001117beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 if (emsg_silent == 0)
1120 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001121 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001122 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124}
1125
1126/*
Bram Moolenaaraae97622022-04-13 14:28:07 +01001127 * Give a warning for an error. "val" is one of the BO_ values, e.g., BO_OPER.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
1129 void
Bram Moolenaaraae97622022-04-13 14:28:07 +01001130vim_beep(unsigned val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001132#ifdef FEAT_EVAL
1133 called_vim_beep = TRUE;
1134#endif
1135
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001136 if (emsg_silent != 0 || in_assert_fails)
1137 return;
1138
1139 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001141#ifdef ELAPSED_FUNC
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001142 static int did_init = FALSE;
1143 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001144
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001145 // Only beep once per half a second, otherwise a sequence of beeps
1146 // would freeze Vim.
1147 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1148 {
1149 did_init = TRUE;
1150 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001152 if (p_vb
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001153#ifdef FEAT_GUI
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001154 // While the GUI is starting up the termcap is set for
1155 // the GUI but the output still goes to a terminal.
1156 && !(gui.in_use && gui.starting)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001157#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001158 )
1159 {
1160 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001161#ifdef FEAT_VTP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001162 // No restore color information, refresh the screen.
1163 if (has_vtp_working() != 0
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001164# ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001165 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001166# endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001167 )
1168 {
1169 redraw_later(UPD_CLEAR);
1170 update_screen(0);
1171 redrawcmd();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001172 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001173#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001174 }
1175 else
1176 out_char(BELL);
1177#ifdef ELAPSED_FUNC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001179#endif
1180 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001182 // When 'debug' contains "beep" produce a message. If we are sourcing
1183 // a script or executing a function give the user a hint where the beep
1184 // comes from.
1185 if (vim_strchr(p_debug, 'e') != NULL)
1186 {
1187 msg_source(HL_ATTR(HLF_W));
1188 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190}
1191
1192/*
1193 * To get the "real" home directory:
1194 * - get value of $HOME
1195 * For Unix:
1196 * - go to that directory
1197 * - do mch_dirname() to get the real name of that directory.
1198 * This also works with mounts and links.
1199 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001200 * For Windows:
1201 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001204init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
1206 char_u *var;
1207
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // In case we are called a second time (when 'encoding' changes).
Bram Moolenaard23a8232018-02-10 18:45:26 +01001209 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211#ifdef VMS
1212 var = mch_getenv((char_u *)"SYS$LOGIN");
1213#else
1214 var = mch_getenv((char_u *)"HOME");
1215#endif
1216
Bram Moolenaar4f974752019-02-17 17:44:42 +01001217#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001219 * Typically, $HOME is not defined on Windows, unless the user has
1220 * specifically defined it for Vim's sake. However, on Windows NT
1221 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1222 * each user. Try constructing $HOME from these.
1223 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001224 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001225 {
1226 char_u *homedrive, *homepath;
1227
1228 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1229 homepath = mch_getenv((char_u *)"HOMEPATH");
1230 if (homepath == NULL || *homepath == NUL)
1231 homepath = (char_u *)"\\";
1232 if (homedrive != NULL
1233 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1234 {
1235 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1236 if (NameBuff[0] != NUL)
1237 var = NameBuff;
1238 }
1239 }
1240
1241 if (var == NULL)
1242 var = mch_getenv((char_u *)"USERPROFILE");
1243
1244 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 * Weird but true: $HOME may contain an indirect reference to another
1246 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1247 * when $HOME is being set.
1248 */
1249 if (var != NULL && *var == '%')
1250 {
1251 char_u *p;
1252 char_u *exp;
1253
1254 p = vim_strchr(var + 1, '%');
1255 if (p != NULL)
1256 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001257 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 exp = mch_getenv(NameBuff);
1259 if (exp != NULL && *exp != NUL
1260 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1261 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001262 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265 }
1266 }
1267
Bram Moolenaar85a20022019-12-21 18:25:54 +01001268 if (var != NULL && *var == NUL) // empty is same as not set
Bram Moolenaar48340b62017-08-29 22:08:53 +02001269 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaar05159a02005-02-26 23:04:13 +00001271 if (enc_utf8 && var != NULL)
1272 {
1273 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001274 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001275
Bram Moolenaar85a20022019-12-21 18:25:54 +01001276 // Convert from active codepage to UTF-8. Other conversions are
1277 // not done, because they would fail for non-ASCII characters.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001278 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001279 if (pp != NULL)
1280 {
1281 homedir = pp;
1282 return;
1283 }
1284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 /*
1287 * Default home dir is C:/
1288 * Best assumption we can make in such a situation.
1289 */
1290 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001291 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (var != NULL)
1295 {
1296#ifdef UNIX
1297 /*
1298 * Change to the directory and get the actual path. This resolves
1299 * links. Don't do it when we can't return.
1300 */
1301 if (mch_dirname(NameBuff, MAXPATHL) == OK
1302 && mch_chdir((char *)NameBuff) == 0)
1303 {
1304 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1305 var = IObuff;
1306 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001307 emsg(_(e_cannot_go_back_to_previous_directory));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309#endif
1310 homedir = vim_strsave(var);
1311 }
1312}
1313
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001314#if defined(EXITFREE) || defined(PROTO)
1315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001317{
1318 vim_free(homedir);
1319}
Bram Moolenaar24305862012-08-15 14:05:05 +02001320
Bram Moolenaar24305862012-08-15 14:05:05 +02001321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001322free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001323{
1324 ga_clear_strings(&ga_users);
1325}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001326#endif
1327
K.Takatace3189d2023-02-15 19:13:43 +00001328#if defined(MSWIN) || defined(PROTO)
1329/*
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001330 * Initialize $VIM and $VIMRUNTIME when 'enc' is updated.
K.Takatace3189d2023-02-15 19:13:43 +00001331 */
1332 void
1333init_vimdir(void)
1334{
1335 int mustfree;
1336 char_u *p;
1337
1338 mch_get_exe_name();
1339
1340 mustfree = FALSE;
1341 didset_vim = FALSE;
1342 p = vim_getenv((char_u *)"VIM", &mustfree);
1343 if (mustfree)
1344 vim_free(p);
1345
1346 mustfree = FALSE;
1347 didset_vimruntime = FALSE;
1348 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1349 if (mustfree)
1350 vim_free(p);
1351}
1352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001355 * Call expand_env() and store the result in an allocated string.
1356 * This is not very memory efficient, this expects the result to be freed
1357 * again soon.
1358 */
1359 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001361{
1362 return expand_env_save_opt(src, FALSE);
1363}
1364
1365/*
1366 * Idem, but when "one" is TRUE handle the string as one file name, only
1367 * expand "~" at the start.
1368 */
1369 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001370expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001371{
1372 char_u *p;
1373
1374 p = alloc(MAXPATHL);
1375 if (p != NULL)
1376 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1377 return p;
1378}
1379
1380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 * Expand environment variable with path name.
1382 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001383 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 * If anything fails no expansion is done and dst equals src.
1385 */
1386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001387expand_env(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001388 char_u *src, // input string e.g. "$HOME/vim.hlp"
1389 char_u *dst, // where to put the result
1390 int dstlen) // maximum length of the result
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001392 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393}
1394
1395 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001396expand_env_esc(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001397 char_u *srcp, // input string e.g. "$HOME/vim.hlp"
1398 char_u *dst, // where to put the result
1399 int dstlen, // maximum length of the result
1400 int esc, // escape spaces in expanded variables
1401 int one, // "srcp" is one file name
1402 char_u *startstr) // start again after this (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001404 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *tail;
1406 int c;
1407 char_u *var;
1408 int copy_char;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001409 int mustfree; // var was allocated, need to free it later
1410 int at_start = TRUE; // at start of a name
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001411 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001413 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001414 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001415
1416 src = skipwhite(srcp);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001417 --dstlen; // leave one char space for "\,"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 while (*src && dstlen > 0)
1419 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001420#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001421 // Skip over `=expr`.
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001422 if (src[0] == '`' && src[1] == '=')
1423 {
1424 size_t len;
1425
1426 var = src;
1427 src += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02001428 (void)skip_expr(&src, NULL);
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001429 if (*src == '`')
1430 ++src;
1431 len = src - var;
1432 if (len > (size_t)dstlen)
1433 len = dstlen;
1434 vim_strncpy(dst, var, len);
1435 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001436 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001437 continue;
1438 }
1439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001441 if ((*src == '$'
1442#ifdef VMS
1443 && at_start
1444#endif
1445 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001446#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 || *src == '%'
1448#endif
1449 || (*src == '~' && at_start))
1450 {
1451 mustfree = FALSE;
1452
1453 /*
1454 * The variable name is copied into dst temporarily, because it may
1455 * be a string in read-only memory and a NUL needs to be appended.
1456 */
Bram Moolenaar85a20022019-12-21 18:25:54 +01001457 if (*src != '~') // environment var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
1459 tail = src + 1;
1460 var = dst;
1461 c = dstlen - 1;
1462
1463#ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001464 // Unix has ${var-name} type environment vars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (*tail == '{' && !vim_isIDc('{'))
1466 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001467 tail++; // ignore '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 while (c-- > 0 && *tail && *tail != '}')
1469 *var++ = *tail++;
1470 }
1471 else
1472#endif
1473 {
1474 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001475#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 || (*src == '%' && *tail != '%')
1477#endif
1478 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001482#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483# ifdef UNIX
1484 if (src[1] == '{' && *tail != '}')
1485# else
1486 if (*src == '%' && *tail != '%')
1487# endif
1488 var = NULL;
1489 else
1490 {
1491# ifdef UNIX
1492 if (src[1] == '{')
1493# else
1494 if (*src == '%')
1495#endif
1496 ++tail;
1497#endif
1498 *var = NUL;
1499 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001500#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502#endif
1503 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001504 // home directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 else if ( src[1] == NUL
1506 || vim_ispathsep(src[1])
1507 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1508 {
1509 var = homedir;
1510 tail = src + 1;
1511 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001512 else // user directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1515 /*
1516 * Copy ~user to dst[], so we can put a NUL after it.
1517 */
1518 tail = src;
1519 var = dst;
1520 c = dstlen - 1;
1521 while ( c-- > 0
1522 && *tail
1523 && vim_isfilec(*tail)
1524 && !vim_ispathsep(*tail))
1525 *var++ = *tail++;
1526 *var = NUL;
1527# ifdef UNIX
1528 /*
1529 * If the system supports getpwnam(), use it.
1530 * Otherwise, or if getpwnam() fails, the shell is used to
1531 * expand ~user. This is slower and may fail if the shell
1532 * does not support ~user (old versions of /bin/sh).
1533 */
1534# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1535 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001536 // Note: memory allocated by getpwnam() is never freed.
1537 // Calling endpwent() apparently doesn't help.
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001538 struct passwd *pw = (*dst == NUL)
1539 ? NULL : getpwnam((char *)dst + 1);
1540
1541 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 }
1543 if (var == NULL)
1544# endif
1545 {
1546 expand_T xpc;
1547
1548 ExpandInit(&xpc);
1549 xpc.xp_context = EXPAND_FILES;
1550 var = ExpandOne(&xpc, dst, NULL,
1551 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 mustfree = TRUE;
1553 }
1554
Bram Moolenaar85a20022019-12-21 18:25:54 +01001555# else // !UNIX, thus VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 /*
1557 * USER_HOME is a comma-separated list of
1558 * directories to search for the user account in.
1559 */
1560 {
1561 char_u test[MAXPATHL], paths[MAXPATHL];
1562 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001563 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
1565 STRCPY(paths, USER_HOME);
1566 next_path = paths;
1567 while (*next_path)
1568 {
1569 for (path = next_path; *next_path && *next_path != ',';
1570 next_path++);
1571 if (*next_path)
1572 *next_path++ = NUL;
1573 STRCPY(test, path);
1574 STRCAT(test, "/");
1575 STRCAT(test, dst + 1);
1576 if (mch_stat(test, &st) == 0)
1577 {
1578 var = alloc(STRLEN(test) + 1);
1579 STRCPY(var, test);
1580 mustfree = TRUE;
1581 break;
1582 }
1583 }
1584 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001585# endif // UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586#else
Bram Moolenaar85a20022019-12-21 18:25:54 +01001587 // cannot expand user's home directory, so don't try
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 var = NULL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001589 tail = (char_u *)""; // for gcc
1590#endif // UNIX || VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592
1593#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 // If 'shellslash' is set change backslashes to forward slashes.
1595 // Can't use slash_adjust(), p_ssl may be set temporarily.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1597 {
1598 char_u *p = vim_strsave(var);
1599
1600 if (p != NULL)
1601 {
1602 if (mustfree)
1603 vim_free(var);
1604 var = p;
1605 mustfree = TRUE;
1606 forward_slash(var);
1607 }
1608 }
1609#endif
1610
Bram Moolenaar85a20022019-12-21 18:25:54 +01001611 // If "var" contains white space, escape it with a backslash.
1612 // Required for ":e ~/tt" when $HOME includes a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1614 {
1615 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1616
1617 if (p != NULL)
1618 {
1619 if (mustfree)
1620 vim_free(var);
1621 var = p;
1622 mustfree = TRUE;
1623 }
1624 }
1625
1626 if (var != NULL && *var != NUL
1627 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1628 {
1629 STRCPY(dst, var);
1630 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001631 c = (int)STRLEN(var);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001632 // if var[] ends in a path separator and tail[] starts
1633 // with it, skip a character
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001634 if (after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
zeertzjq13a01442024-03-09 17:44:46 +01001636 && dst[c - 1] != ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638 && vim_ispathsep(*tail))
1639 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001640 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 src = tail;
1642 copy_char = FALSE;
1643 }
1644 if (mustfree)
1645 vim_free(var);
1646 }
1647
Bram Moolenaar85a20022019-12-21 18:25:54 +01001648 if (copy_char) // copy at least one char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00001651 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001652 * Don't do this when "one" is TRUE, to avoid expanding "~" in
1653 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 */
1655 at_start = FALSE;
1656 if (src[0] == '\\' && src[1] != NUL)
1657 {
1658 *dst++ = *src++;
1659 --dstlen;
1660 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001661 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02001663 if (dstlen > 0)
1664 {
1665 *dst++ = *src++;
1666 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001667
Bram Moolenaar1c864092017-08-06 18:15:45 +02001668 if (startstr != NULL && src - startstr_len >= srcp
1669 && STRNCMP(src - startstr_len, startstr,
1670 startstr_len) == 0)
1671 at_start = TRUE;
1672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02001674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676 *dst = NUL;
1677}
1678
1679/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02001680 * If the string between "p" and "pend" ends in "name/", return "pend" minus
1681 * the length of "name/". Otherwise return "pend".
1682 */
1683 static char_u *
1684remove_tail(char_u *p, char_u *pend, char_u *name)
1685{
1686 int len = (int)STRLEN(name) + 1;
1687 char_u *newend = pend - len;
1688
1689 if (newend >= p
1690 && fnamencmp(newend, name, len - 1) == 0
1691 && (newend == p || after_pathsep(p, newend)))
1692 return newend;
1693 return pend;
1694}
1695
1696/*
1697 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
1698 * Return NULL if not, return its name in allocated memory otherwise.
1699 */
1700 static char_u *
1701vim_version_dir(char_u *vimdir)
1702{
1703 char_u *p;
1704
1705 if (vimdir == NULL || *vimdir == NUL)
1706 return NULL;
1707 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
1708 if (p != NULL && mch_isdir(p))
1709 return p;
1710 vim_free(p);
1711 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
1712 if (p != NULL && mch_isdir(p))
Bram Moolenaar022f9ef2022-07-02 17:36:31 +01001713 {
1714 char_u *fname = concat_fnames(p, (char_u *)"defaults.vim", TRUE);
1715
1716 // Check that "defaults.vim" exists in this directory, to avoid picking
1717 // up a stray "runtime" directory, it would make many tests fail in
1718 // mysterious ways.
1719 if (fname != NULL)
1720 {
1721 int exists = file_is_readable(fname);
1722
1723 vim_free(fname);
1724 if (exists)
1725 return p;
1726 }
1727 }
Bram Moolenaar26262f82019-09-04 20:59:15 +02001728 vim_free(p);
1729 return NULL;
1730}
1731
1732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 * Vim's version of getenv().
1734 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00001735 * Also does ACP to 'enc' conversion for Win32.
K.Takatace3189d2023-02-15 19:13:43 +00001736 * "mustfree" is set to TRUE when the returned string is allocated. It must be
Bram Moolenaarb453a532011-04-28 17:48:44 +02001737 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 */
1739 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001740vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001742 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 char_u *pend;
1744 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001745#ifdef MSWIN
1746 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001748 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (STRCMP(name, "HOME") == 0)
1750 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001752 // Use Wide function
1753 wn = enc_to_utf16(name, NULL);
1754 if (wn == NULL)
1755 return NULL;
1756
1757 wp = _wgetenv(wn);
1758 vim_free(wn);
1759
1760 if (wp != NULL && *wp == NUL) // empty is the same as not set
1761 wp = NULL;
1762
1763 if (wp != NULL)
1764 {
1765 p = utf16_to_enc(wp, NULL);
1766 if (p == NULL)
1767 return NULL;
1768
1769 *mustfree = TRUE;
1770 return p;
1771 }
1772#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001774 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 p = NULL;
1776
1777 if (p != NULL)
1778 return p;
Bram Moolenaar80a8d382020-05-03 22:57:32 +02001779
1780# ifdef __HAIKU__
1781 // special handling for user settings directory...
1782 if (STRCMP(name, "BE_USER_SETTINGS") == 0)
1783 {
1784 static char userSettingsPath[MAXPATHL];
1785
1786 if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false,
1787 userSettingsPath, MAXPATHL) == B_OK)
1788 return (char_u *)userSettingsPath;
1789 else
1790 return NULL;
1791 }
1792# endif
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001795 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
1797 if (!vimruntime && STRCMP(name, "VIM") != 0)
1798 return NULL;
1799
1800 /*
1801 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
1802 * Don't do this when default_vimruntime_dir is non-empty.
1803 */
1804 if (vimruntime
1805#ifdef HAVE_PATHDEF
1806 && *default_vimruntime_dir == NUL
1807#endif
1808 )
1809 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001810#ifdef MSWIN
1811 // Use Wide function
1812 wp = _wgetenv(L"VIM");
1813 if (wp != NULL && *wp == NUL) // empty is the same as not set
1814 wp = NULL;
1815 if (wp != NULL)
1816 {
1817 char_u *q = utf16_to_enc(wp, NULL);
1818 if (q != NULL)
1819 {
1820 p = vim_version_dir(q);
1821 *mustfree = TRUE;
1822 if (p == NULL)
1823 p = q;
1824 }
1825 }
1826#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001828 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 p = NULL;
1830 if (p != NULL)
1831 {
1832 p = vim_version_dir(p);
1833 if (p != NULL)
1834 *mustfree = TRUE;
1835 else
1836 p = mch_getenv((char_u *)"VIM");
1837 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01001838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840
1841 /*
1842 * When expanding $VIM or $VIMRUNTIME fails, try using:
1843 * - the directory name from 'helpfile' (unless it contains '$')
1844 * - the executable name from argv[0]
1845 */
1846 if (p == NULL)
1847 {
1848 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
1849 p = p_hf;
1850#ifdef USE_EXE_NAME
1851 /*
1852 * Use the name of the executable, obtained from argv[0].
1853 */
1854 else
1855 p = exe_name;
1856#endif
1857 if (p != NULL)
1858 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001859 // remove the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 pend = gettail(p);
1861
Bram Moolenaar85a20022019-12-21 18:25:54 +01001862 // remove "doc/" from 'helpfile', if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (p == p_hf)
1864 pend = remove_tail(p, pend, (char_u *)"doc");
1865
1866#ifdef USE_EXE_NAME
1867# ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001868 // remove "MacOS" from exe_name and add "Resources/vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (p == exe_name)
1870 {
1871 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001872 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001874 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
1875 if (pend1 != pend)
1876 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001877 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001878 if (pnew != NULL)
1879 {
1880 STRNCPY(pnew, p, (pend1 - p));
1881 STRCPY(pnew + (pend1 - p), "Resources/vim");
1882 p = pnew;
1883 pend = p + STRLEN(p);
1884 }
1885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001888 // remove "src/" from exe_name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (p == exe_name)
1890 pend = remove_tail(p, pend, (char_u *)"src");
1891#endif
1892
Bram Moolenaar85a20022019-12-21 18:25:54 +01001893 // for $VIM, remove "runtime/" or "vim54/", if present
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (!vimruntime)
1895 {
1896 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
1897 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
1898 }
1899
Bram Moolenaar85a20022019-12-21 18:25:54 +01001900 // remove trailing path separator
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001901 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaar95e9b492006-03-15 23:04:43 +00001904#ifdef MACOS_X
1905 if (p == exe_name || p == p_hf)
1906#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001907 // check that the result is a directory name
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001908 p = vim_strnsave(p, pend - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001911 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 else
1913 {
1914#ifdef USE_EXE_NAME
Bram Moolenaar85a20022019-12-21 18:25:54 +01001915 // may add "/vim54" or "/runtime" if it exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
1917 {
1918 vim_free(p);
1919 p = pend;
1920 }
1921#endif
1922 *mustfree = TRUE;
1923 }
1924 }
1925 }
1926
1927#ifdef HAVE_PATHDEF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001928 // When there is a pathdef.c file we can use default_vim_dir and
1929 // default_vimruntime_dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (p == NULL)
1931 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001932 // Only use default_vimruntime_dir when it is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (vimruntime && *default_vimruntime_dir != NUL)
1934 {
1935 p = default_vimruntime_dir;
1936 *mustfree = FALSE;
1937 }
1938 else if (*default_vim_dir != NUL)
1939 {
1940 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
1941 *mustfree = TRUE;
1942 else
1943 {
1944 p = default_vim_dir;
1945 *mustfree = FALSE;
1946 }
1947 }
1948 }
1949#endif
1950
1951 /*
1952 * Set the environment variable, so that the new value can be found fast
1953 * next time, and others can also use it (e.g. Perl).
1954 */
1955 if (p != NULL)
1956 {
1957 if (vimruntime)
1958 {
1959 vim_setenv((char_u *)"VIMRUNTIME", p);
1960 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962 else
1963 {
1964 vim_setenv((char_u *)"VIM", p);
1965 didset_vim = TRUE;
1966 }
1967 }
1968 return p;
1969}
1970
Bram Moolenaar137374f2018-05-13 15:59:50 +02001971 void
1972vim_unsetenv(char_u *var)
1973{
1974#ifdef HAVE_UNSETENV
1975 unsetenv((char *)var);
1976#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02001977 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02001978#endif
1979}
1980
LemonBoy77142312022-04-15 20:50:46 +01001981/*
1982 * Removes environment variable "name" and take care of side effects.
1983 */
1984 void
1985vim_unsetenv_ext(char_u *var)
1986{
1987 vim_unsetenv(var);
1988
1989 // "homedir" is not cleared, keep using the old value until $HOME is set.
1990 if (STRICMP(var, "VIM") == 0)
1991 didset_vim = FALSE;
1992 else if (STRICMP(var, "VIMRUNTIME") == 0)
1993 didset_vimruntime = FALSE;
1994}
Bram Moolenaar137374f2018-05-13 15:59:50 +02001995
Bram Moolenaar092e09c2022-04-15 23:29:23 +01001996#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001998 * Set environment variable "name" and take care of side effects.
1999 */
2000 void
2001vim_setenv_ext(char_u *name, char_u *val)
2002{
2003 vim_setenv(name, val);
2004 if (STRICMP(name, "HOME") == 0)
2005 init_homedir();
2006 else if (didset_vim && STRICMP(name, "VIM") == 0)
2007 didset_vim = FALSE;
LemonBoy77142312022-04-15 20:50:46 +01002008 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002009 didset_vimruntime = FALSE;
2010}
Dominique Pelle748b3082022-01-08 12:41:16 +00002011#endif
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002012
2013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 * Our portable version of setenv.
2015 */
2016 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002017vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
2019#ifdef HAVE_SETENV
2020 mch_setenv((char *)name, (char *)val, 1);
2021#else
2022 char_u *envbuf;
2023
2024 /*
2025 * Putenv does not copy the string, it has to remain
2026 * valid. The allocated memory will never be freed.
2027 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002028 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (envbuf != NULL)
2030 {
2031 sprintf((char *)envbuf, "%s=%s", name, val);
2032 putenv((char *)envbuf);
2033 }
2034#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002035#ifdef FEAT_GETTEXT
2036 /*
2037 * When setting $VIMRUNTIME adjust the directory to find message
2038 * translations to $VIMRUNTIME/lang.
2039 */
2040 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2041 {
2042 char_u *buf = concat_str(val, (char_u *)"/lang");
2043
2044 if (buf != NULL)
2045 {
2046 bindtextdomain(VIMPACKAGE, (char *)buf);
2047 vim_free(buf);
2048 }
2049 }
2050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051}
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053/*
2054 * Function given to ExpandGeneric() to obtain an environment variable name.
2055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002057get_env_name(
2058 expand_T *xp UNUSED,
2059 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002061#if defined(AMIGA)
2062 // No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 return NULL;
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002064#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065# ifndef __WIN32__
Bram Moolenaar85a20022019-12-21 18:25:54 +01002066 // Borland C++ 5.2 has this in a header file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 extern char **environ;
2068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 char_u *str;
2070 int n;
2071
2072 str = (char_u *)environ[idx];
2073 if (str == NULL)
2074 return NULL;
2075
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002076 for (n = 0; n < EXPAND_BUF_LEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
2078 if (str[n] == '=' || str[n] == NUL)
2079 break;
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002080 xp->xp_buf[n] = str[n];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
Bram Moolenaar5ff595d2022-08-26 22:36:41 +01002082 xp->xp_buf[n] = NUL;
2083 return xp->xp_buf;
2084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085}
Bram Moolenaar24305862012-08-15 14:05:05 +02002086
2087/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002088 * Add a user name to the list of users in ga_users.
2089 * Do nothing if user name is NULL or empty.
2090 */
2091 static void
2092add_user(char_u *user, int need_copy)
2093{
2094 char_u *user_copy = (user != NULL && need_copy)
2095 ? vim_strsave(user) : user;
2096
2097 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2098 {
2099 if (need_copy)
Christian Brabandt7a2f2172024-03-24 09:50:03 +01002100 vim_free(user_copy);
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002101 return;
2102 }
2103 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2104}
2105
2106/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002107 * Find all user names for user completion.
2108 * Done only once and then cached.
2109 */
2110 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002111init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002112{
Bram Moolenaar24305862012-08-15 14:05:05 +02002113 static int lazy_init_done = FALSE;
2114
2115 if (lazy_init_done)
2116 return;
2117
2118 lazy_init_done = TRUE;
2119 ga_init2(&ga_users, sizeof(char_u *), 20);
2120
2121# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2122 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002123 struct passwd* pw;
2124
2125 setpwent();
2126 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002127 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002128 endpwent();
2129 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002130# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002131 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002132 DWORD nusers = 0, ntotal = 0, i;
2133 PUSER_INFO_0 uinfo;
2134
2135 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2136 &nusers, &ntotal, NULL) == NERR_Success)
2137 {
2138 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002139 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002140
2141 NetApiBufferFree(uinfo);
2142 }
2143 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002144# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002145# if defined(HAVE_GETPWNAM)
2146 {
2147 char_u *user_env = mch_getenv((char_u *)"USER");
2148
2149 // The $USER environment variable may be a valid remote user name (NIS,
2150 // LDAP) not already listed by getpwent(), as getpwent() only lists
2151 // local user names. If $USER is not already listed, check whether it
2152 // is a valid remote user name using getpwnam() and if it is, add it to
2153 // the list of user names.
2154
2155 if (user_env != NULL && *user_env != NUL)
2156 {
2157 int i;
2158
2159 for (i = 0; i < ga_users.ga_len; i++)
2160 {
2161 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2162
2163 if (STRCMP(local_user, user_env) == 0)
2164 break;
2165 }
2166
2167 if (i == ga_users.ga_len)
2168 {
2169 struct passwd *pw = getpwnam((char *)user_env);
2170
2171 if (pw != NULL)
2172 add_user((char_u *)pw->pw_name, TRUE);
2173 }
2174 }
2175 }
2176# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002177}
2178
2179/*
zeertzjqc029c132024-03-28 11:37:26 +01002180 * Function given to ExpandGeneric() to obtain user names.
Bram Moolenaar24305862012-08-15 14:05:05 +02002181 */
2182 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002183get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002184{
2185 init_users();
2186 if (idx < ga_users.ga_len)
2187 return ((char_u **)ga_users.ga_data)[idx];
2188 return NULL;
2189}
2190
2191/*
2192 * Check whether name matches a user name. Return:
2193 * 0 if name does not match any user name.
2194 * 1 if name partially matches the beginning of a user name.
2195 * 2 is name fully matches a user name.
2196 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002197 int
2198match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002199{
2200 int i;
2201 int n = (int)STRLEN(name);
2202 int result = 0;
2203
2204 init_users();
2205 for (i = 0; i < ga_users.ga_len; i++)
2206 {
2207 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002208 return 2; // full match
Bram Moolenaar24305862012-08-15 14:05:05 +02002209 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002210 result = 1; // partial match
Bram Moolenaar24305862012-08-15 14:05:05 +02002211 }
2212 return result;
2213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002216prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002218#if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002219 // Ignore SIGHUP, because a dropped connection causes a read error, which
2220 // makes Vim exit and then handling SIGHUP causes various reentrance
2221 // problems.
ichizok378447f2023-05-11 22:25:42 +01002222 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002223#endif
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#ifdef FEAT_GUI
2226 if (gui.in_use)
2227 {
2228 gui.dying = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 out_trash(); // trash any pending output
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231 else
2232#endif
2233 {
2234 windgoto((int)Rows - 1, 0);
2235
2236 /*
2237 * Switch terminal mode back now, so messages end up on the "normal"
2238 * screen (if there are two screens).
2239 */
2240 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002241 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 out_flush();
2243 }
2244}
2245
2246/*
2247 * Preserve files and exit.
2248 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002249 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2250 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
2252 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002253preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 buf_T *buf;
2256
2257 prepare_to_exit();
2258
Bram Moolenaar85a20022019-12-21 18:25:54 +01002259 // Setting this will prevent free() calls. That avoids calling free()
2260 // recursively when free() was invoked with a bad pointer.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002261 really_exiting = TRUE;
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 out_str(IObuff);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002264 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 out_flush();
2266
Bram Moolenaar85a20022019-12-21 18:25:54 +01002267 ml_close_notmod(); // close all not-modified buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268
Bram Moolenaar29323592016-07-24 22:04:11 +02002269 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2272 {
Bram Moolenaar69212b12020-05-10 14:14:03 +02002273 OUT_STR("Vim: preserving files...\r\n");
Bram Moolenaar85a20022019-12-21 18:25:54 +01002274 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 out_flush();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002276 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 break;
2278 }
2279 }
2280
Bram Moolenaar85a20022019-12-21 18:25:54 +01002281 ml_close_all(FALSE); // close all memfiles, without deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar69212b12020-05-10 14:14:03 +02002283 OUT_STR("Vim: Finished.\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 getout(1);
2286}
2287
2288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 * Check for CTRL-C pressed, but only once in a while.
2290 * Should be used instead of ui_breakcheck() for functions that check for
2291 * each line in the file. Calling ui_breakcheck() each time takes too much
2292 * time, because it can be a system call.
2293 */
2294
2295#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002296# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297#endif
2298
2299static int breakcheck_count = 0;
2300
2301 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002302line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
2304 if (++breakcheck_count >= BREAKCHECK_SKIP)
2305 {
2306 breakcheck_count = 0;
2307 ui_breakcheck();
2308 }
2309}
2310
2311/*
2312 * Like line_breakcheck() but check 10 times less often.
2313 */
2314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002315fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2318 {
2319 breakcheck_count = 0;
2320 ui_breakcheck();
2321 }
2322}
2323
Dominique Pelle748b3082022-01-08 12:41:16 +00002324# if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002325/*
2326 * Like line_breakcheck() but check 100 times less often.
2327 */
2328 void
2329veryfast_breakcheck(void)
2330{
2331 if (++breakcheck_count >= BREAKCHECK_SKIP * 100)
2332 {
2333 breakcheck_count = 0;
2334 ui_breakcheck();
2335 }
2336}
Dominique Pelle748b3082022-01-08 12:41:16 +00002337#endif
Bram Moolenaarf1ec3782020-03-20 19:37:47 +01002338
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002339#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2340 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2341 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343#ifndef SEEK_SET
2344# define SEEK_SET 0
2345#endif
2346#ifndef SEEK_END
2347# define SEEK_END 2
2348#endif
2349
2350/*
2351 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002352 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2353 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * Returns an allocated string, or NULL for error.
2355 */
2356 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002357get_cmd_output(
2358 char_u *cmd,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002359 char_u *infile, // optional input file name
2360 int flags, // can be SHELL_SILENT
Bram Moolenaar9b578142016-01-30 19:39:49 +01002361 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 char_u *tempname;
2364 char_u *command;
2365 char_u *buffer = NULL;
2366 int len;
2367 int i = 0;
2368 FILE *fd;
2369
2370 if (check_restricted() || check_secure())
2371 return NULL;
2372
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 // get a name for the temp file
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002374 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002376 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return NULL;
2378 }
2379
Bram Moolenaar85a20022019-12-21 18:25:54 +01002380 // Add the redirection stuff
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002381 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (command == NULL)
2383 goto done;
2384
2385 /*
2386 * Call the shell to execute the command (errors are ignored).
2387 * Don't check timestamps here.
2388 */
2389 ++no_check_timestamps;
2390 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2391 --no_check_timestamps;
2392
2393 vim_free(command);
2394
2395 /*
2396 * read the names from the file into memory
2397 */
2398# ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // created temporary file is not always readable as binary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 fd = mch_fopen((char *)tempname, "r");
2401# else
2402 fd = mch_fopen((char *)tempname, READBIN);
2403# endif
2404
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002405 // Not being able to seek means we can't read the file.
2406 if (fd == NULL
2407 || fseek(fd, 0L, SEEK_END) == -1
2408 || (len = ftell(fd)) == -1 // get size of temp file
2409 || fseek(fd, 0L, SEEK_SET) == -1) // back to the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaara9549c92022-04-17 14:18:11 +01002411 semsg(_(e_cannot_read_from_str_2), tempname);
Bram Moolenaar3df8f6e2022-04-17 14:01:51 +01002412 if (fd != NULL)
2413 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 goto done;
2415 }
2416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 buffer = alloc(len + 1);
2418 if (buffer != NULL)
2419 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2420 fclose(fd);
2421 mch_remove(tempname);
2422 if (buffer == NULL)
2423 goto done;
2424#ifdef VMS
Bram Moolenaar85a20022019-12-21 18:25:54 +01002425 len = i; // VMS doesn't give us what we asked for...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426#endif
2427 if (i != len)
2428 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002429 semsg(_(e_cant_read_file_str), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002430 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002432 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002433 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002434 // Change NUL into SOH, otherwise the string is truncated.
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002435 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002436 if (buffer[i] == NUL)
2437 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002438
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 buffer[len] = NUL; // make sure the buffer is terminated
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002440 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002441 else
2442 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444done:
2445 vim_free(tempname);
2446 return buffer;
2447}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002448
2449# if defined(FEAT_EVAL) || defined(PROTO)
2450
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002451 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002452get_cmd_output_as_rettv(
2453 typval_T *argvars,
2454 typval_T *rettv,
2455 int retlist)
2456{
2457 char_u *res = NULL;
2458 char_u *p;
2459 char_u *infile = NULL;
2460 int err = FALSE;
2461 FILE *fd;
2462 list_T *list = NULL;
2463 int flags = SHELL_SILENT;
2464
2465 rettv->v_type = VAR_STRING;
2466 rettv->vval.v_string = NULL;
2467 if (check_restricted() || check_secure())
2468 goto errret;
2469
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002470 if (in_vim9script()
2471 && (check_for_string_arg(argvars, 0) == FAIL
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01002472 || check_for_opt_string_or_number_or_list_arg(argvars, 1)
2473 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002474 return;
2475
Bram Moolenaar26262f82019-09-04 20:59:15 +02002476 if (argvars[1].v_type != VAR_UNKNOWN)
2477 {
2478 /*
2479 * Write the text to a temp file, to be used for input of the shell
2480 * command.
2481 */
2482 if ((infile = vim_tempname('i', TRUE)) == NULL)
2483 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002484 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002485 goto errret;
2486 }
2487
2488 fd = mch_fopen((char *)infile, WRITEBIN);
2489 if (fd == NULL)
2490 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002491 semsg(_(e_cant_open_file_str), infile);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002492 goto errret;
2493 }
2494 if (argvars[1].v_type == VAR_NUMBER)
2495 {
2496 linenr_T lnum;
2497 buf_T *buf;
2498
2499 buf = buflist_findnr(argvars[1].vval.v_number);
2500 if (buf == NULL)
2501 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00002502 semsg(_(e_buffer_nr_does_not_exist), argvars[1].vval.v_number);
Bram Moolenaar26262f82019-09-04 20:59:15 +02002503 fclose(fd);
2504 goto errret;
2505 }
2506
2507 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2508 {
2509 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2510 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2511 {
2512 err = TRUE;
2513 break;
2514 }
2515 if (putc(NL, fd) == EOF)
2516 {
2517 err = TRUE;
2518 break;
2519 }
2520 }
2521 }
2522 else if (argvars[1].v_type == VAR_LIST)
2523 {
2524 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2525 err = TRUE;
2526 }
2527 else
2528 {
2529 size_t len;
2530 char_u buf[NUMBUFLEN];
2531
2532 p = tv_get_string_buf_chk(&argvars[1], buf);
2533 if (p == NULL)
2534 {
2535 fclose(fd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 goto errret; // type error; errmsg already given
Bram Moolenaar26262f82019-09-04 20:59:15 +02002537 }
2538 len = STRLEN(p);
2539 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2540 err = TRUE;
2541 }
2542 if (fclose(fd) != 0)
2543 err = TRUE;
2544 if (err)
2545 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00002546 emsg(_(e_error_writing_temp_file));
Bram Moolenaar26262f82019-09-04 20:59:15 +02002547 goto errret;
2548 }
2549 }
2550
Bram Moolenaar85a20022019-12-21 18:25:54 +01002551 // Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2552 // echoes typeahead, that messes up the display.
Bram Moolenaar26262f82019-09-04 20:59:15 +02002553 if (!msg_silent)
2554 flags += SHELL_COOKED;
2555
2556 if (retlist)
2557 {
2558 int len;
2559 listitem_T *li;
2560 char_u *s = NULL;
2561 char_u *start;
2562 char_u *end;
2563 int i;
2564
2565 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2566 if (res == NULL)
2567 goto errret;
2568
2569 list = list_alloc();
2570 if (list == NULL)
2571 goto errret;
2572
2573 for (i = 0; i < len; ++i)
2574 {
2575 start = res + i;
2576 while (i < len && res[i] != NL)
2577 ++i;
2578 end = res + i;
2579
2580 s = alloc(end - start + 1);
2581 if (s == NULL)
2582 goto errret;
2583
2584 for (p = s; start < end; ++p, ++start)
2585 *p = *start == NUL ? NL : *start;
2586 *p = NUL;
2587
2588 li = listitem_alloc();
2589 if (li == NULL)
2590 {
2591 vim_free(s);
2592 goto errret;
2593 }
2594 li->li_tv.v_type = VAR_STRING;
2595 li->li_tv.v_lock = 0;
2596 li->li_tv.vval.v_string = s;
2597 list_append(list, li);
2598 }
2599
2600 rettv_list_set(rettv, list);
2601 list = NULL;
2602 }
2603 else
2604 {
2605 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2606#ifdef USE_CRNL
Bram Moolenaar85a20022019-12-21 18:25:54 +01002607 // translate <CR><NL> into <NL>
Bram Moolenaar26262f82019-09-04 20:59:15 +02002608 if (res != NULL)
2609 {
2610 char_u *s, *d;
2611
2612 d = res;
2613 for (s = res; *s; ++s)
2614 {
2615 if (s[0] == CAR && s[1] == NL)
2616 ++s;
2617 *d++ = *s;
2618 }
2619 *d = NUL;
2620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002622 rettv->vval.v_string = res;
2623 res = NULL;
2624 }
2625
2626errret:
2627 if (infile != NULL)
2628 {
2629 mch_remove(infile);
2630 vim_free(infile);
2631 }
2632 if (res != NULL)
2633 vim_free(res);
2634 if (list != NULL)
2635 list_free(list);
2636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002639 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 */
2641 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002642f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002644 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645}
2646
2647/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002648 * "systemlist()" function
2649 */
2650 void
2651f_systemlist(typval_T *argvars, typval_T *rettv)
2652{
2653 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2654}
2655# endif // FEAT_EVAL
2656
2657#endif
2658
2659/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002660 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 * Don't do this when still processing a command or a mapping.
2662 * Don't do this when inside a ":normal" command.
2663 */
2664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002665goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 return (p_im && stuff_empty() && typebuf_typed());
2668}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002669
2670/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002671 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002672 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2673 * - Remove any argument. E.g., "csh -f" -> "csh".
2674 * But don't allow a space in the path, so that this works:
2675 * "/usr/bin/csh --rcfile ~/.cshrc"
2676 * But don't do that for Windows, it's common to have a space in the path.
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002677 * Returns NULL when out of memory.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002678 */
2679 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002680get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002681{
2682 char_u *p;
2683
Bram Moolenaar4f974752019-02-17 17:44:42 +01002684#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002685 p = gettail(p_sh);
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002686 p = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002687#else
2688 p = skiptowhite(p_sh);
2689 if (*p == NUL)
2690 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002691 // No white space, use the tail.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002692 p = vim_strsave(gettail(p_sh));
2693 }
2694 else
2695 {
2696 char_u *p1, *p2;
2697
Bram Moolenaar85a20022019-12-21 18:25:54 +01002698 // Find the last path separator before the space.
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002699 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002700 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002701 if (vim_ispathsep(*p2))
2702 p1 = p2 + 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002703 p = vim_strnsave(p1, p - p1);
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002704 }
2705#endif
2706 return p;
2707}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002708
2709/*
2710 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
2711 * Also check for ":\\", which MS Internet Explorer accepts, return
2712 * URL_BACKSLASH.
2713 */
2714 int
2715path_is_url(char_u *p)
2716{
2717 if (STRNCMP(p, "://", (size_t)3) == 0)
2718 return URL_SLASH;
2719 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
2720 return URL_BACKSLASH;
2721 return 0;
2722}
2723
2724/*
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002725 * Check if "fname" starts with "name://" or "name:\\".
2726 * Return URL_SLASH for "name://", URL_BACKSLASH for "name:\\".
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002727 * Return zero otherwise.
2728 */
2729 int
2730path_with_url(char_u *fname)
2731{
2732 char_u *p;
2733
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002734 // We accept alphabetic characters and a dash in scheme part.
2735 // RFC 3986 allows for more, but it increases the risk of matching
2736 // non-URL text.
2737
2738 // first character must be alpha
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002739 if (!ASCII_ISALPHA(*fname))
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002740 return 0;
2741
2742 // check body: alpha or dash
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002743 for (p = fname + 1; (ASCII_ISALPHA(*p) || (*p == '-')); ++p)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002744 ;
Tsuyoshi CHO7b7a1182021-07-11 21:51:17 +02002745
2746 // check last char is not a dash
2747 if (p[-1] == '-')
2748 return 0;
2749
2750 // "://" or ":\\" must follow
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002751 return path_is_url(p);
2752}
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002753
Bram Moolenaar3075a452021-11-17 15:51:52 +00002754#if defined(FEAT_EVAL) || defined(PROTO)
2755/*
2756 * Return the dictionary of v:event.
2757 * Save and clear the value in case it already has items.
2758 */
2759 dict_T *
2760get_v_event(save_v_event_T *sve)
2761{
2762 dict_T *v_event = get_vim_var_dict(VV_EVENT);
2763
2764 if (v_event->dv_hashtab.ht_used > 0)
2765 {
2766 // recursive use of v:event, save, make empty and restore later
2767 sve->sve_did_save = TRUE;
2768 sve->sve_hashtab = v_event->dv_hashtab;
2769 hash_init(&v_event->dv_hashtab);
2770 }
2771 else
2772 sve->sve_did_save = FALSE;
2773 return v_event;
2774}
2775
2776 void
2777restore_v_event(dict_T *v_event, save_v_event_T *sve)
2778{
2779 dict_free_contents(v_event);
2780 if (sve->sve_did_save)
2781 v_event->dv_hashtab = sve->sve_hashtab;
2782 else
2783 hash_init(&v_event->dv_hashtab);
2784}
2785#endif
2786
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002787/*
LemonBoy2bf52dd2022-04-09 18:17:34 +01002788 * Fires a ModeChanged autocmd event if appropriate.
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002789 */
2790 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002791may_trigger_modechanged(void)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002792{
Bram Moolenaar3075a452021-11-17 15:51:52 +00002793#ifdef FEAT_EVAL
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002794 dict_T *v_event;
Bram Moolenaar3075a452021-11-17 15:51:52 +00002795 save_v_event_T save_v_event;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002796 char_u curr_mode[MODE_MAX_LENGTH];
2797 char_u pattern_buf[2 * MODE_MAX_LENGTH];
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002798
Bram Moolenaar61c4b042022-10-18 15:10:11 +01002799 // Skip this when got_int is set, the autocommand will not be executed.
2800 // Better trigger it next time.
2801 if (!has_modechanged() || got_int)
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002802 return;
2803
LemonBoy2bf52dd2022-04-09 18:17:34 +01002804 get_mode(curr_mode);
2805 if (STRCMP(curr_mode, last_mode) == 0)
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002806 return;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002807
Bram Moolenaar3075a452021-11-17 15:51:52 +00002808 v_event = get_v_event(&save_v_event);
LemonBoy2bf52dd2022-04-09 18:17:34 +01002809 (void)dict_add_string(v_event, "new_mode", curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002810 (void)dict_add_string(v_event, "old_mode", last_mode);
2811 dict_set_items_ro(v_event);
2812
2813 // concatenate modes in format "old_mode:new_mode"
LemonBoy2bf52dd2022-04-09 18:17:34 +01002814 vim_snprintf((char *)pattern_buf, sizeof(pattern_buf), "%s:%s", last_mode,
2815 curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002816
LemonBoy2bf52dd2022-04-09 18:17:34 +01002817 apply_autocmds(EVENT_MODECHANGED, pattern_buf, NULL, FALSE, curbuf);
2818 STRCPY(last_mode, curr_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002819
Bram Moolenaar3075a452021-11-17 15:51:52 +00002820 restore_v_event(v_event, &save_v_event);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002821#endif
2822}
Christian Brabandt22cbc8a2023-11-19 10:47:21 +01002823
2824// For overflow detection, add a digit safely to an int value.
2825 int
2826vim_append_digit_int(int *value, int digit)
2827{
2828 int x = *value;
2829 if (x > ((INT_MAX - digit) / 10))
2830 return FAIL;
2831 *value = x * 10 + digit;
2832 return OK;
2833}
2834
2835// For overflow detection, add a digit safely to a long value.
2836 int
2837vim_append_digit_long(long *value, int digit)
2838{
2839 long x = *value;
2840 if (x > ((LONG_MAX - (long)digit) / 10))
2841 return FAIL;
2842 *value = x * 10 + (long)digit;
2843 return OK;
2844}
Ernie Rael2b0882f2023-11-23 20:21:45 +01002845
2846// Return something that fits into an int.
2847 int
Ernie Raelfda700c2023-11-30 18:20:00 +01002848trim_to_int(vimlong_T x)
Ernie Rael2b0882f2023-11-23 20:21:45 +01002849{
2850 return x > INT_MAX ? INT_MAX : x < INT_MIN ? INT_MIN : x;
2851}
2852