blob: ca5afef00b15ae3bee8067521e8236b6ced6f599 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar0a52df52019-08-18 22:26:31 +020017#if defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020018# include <lm.h>
19#endif
20
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010021#define URL_SLASH 1 /* path_is_url() has found "://" */
22#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
23
Bram Moolenaar0a52df52019-08-18 22:26:31 +020024// All user names (for ~user completion as done by shell).
Bram Moolenaar24305862012-08-15 14:05:05 +020025static garray_T ga_users;
Bram Moolenaar24305862012-08-15 14:05:05 +020026
Bram Moolenaar071d4272004-06-13 20:20:40 +000027/*
28 * Count the size (in window cells) of the indent in the current line.
29 */
30 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010031get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000032{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020033#ifdef FEAT_VARTABS
34 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
35 curbuf->b_p_vts_array, FALSE);
36#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020037 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039}
40
41/*
42 * Count the size (in window cells) of the indent in line "lnum".
43 */
44 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010045get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020047#ifdef FEAT_VARTABS
48 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
49 curbuf->b_p_vts_array, FALSE);
50#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020051 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053}
54
55#if defined(FEAT_FOLDING) || defined(PROTO)
56/*
57 * Count the size (in window cells) of the indent in line "lnum" of buffer
58 * "buf".
59 */
60 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010061get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020063#ifdef FEAT_VARTABS
64 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
65 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
66#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020067 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069}
70#endif
71
72/*
73 * count the size (in window cells) of the indent in line "ptr", with
74 * 'tabstop' at "ts"
75 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000076 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010077get_indent_str(
78 char_u *ptr,
79 int ts,
80 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081{
82 int count = 0;
83
84 for ( ; *ptr; ++ptr)
85 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020086 if (*ptr == TAB)
87 {
88 if (!list || lcs_tab1) /* count a tab for what it is worth */
89 count += ts - (count % ts);
90 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020091 /* In list mode, when tab is not set, count screen char width
92 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020093 count += ptr2cells(ptr);
94 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 else if (*ptr == ' ')
96 ++count; /* count a space for one */
97 else
98 break;
99 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000100 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101}
102
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200103#ifdef FEAT_VARTABS
104/*
105 * Count the size (in window cells) of the indent in line "ptr", using
106 * variable tabstops.
107 * if "list" is TRUE, count only screen size for tabs.
108 */
109 int
110get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
111{
112 int count = 0;
113
114 for ( ; *ptr; ++ptr)
115 {
116 if (*ptr == TAB) /* count a tab for what it is worth */
117 {
118 if (!list || lcs_tab1)
119 count += tabstop_padding(count, ts, vts);
120 else
121 /* In list mode, when tab is not set, count screen char width
122 * for Tab, displays: ^I */
123 count += ptr2cells(ptr);
124 }
125 else if (*ptr == ' ')
126 ++count; /* count a space for one */
127 else
128 break;
129 }
130 return count;
131}
132#endif
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Set the indent of the current line.
136 * Leaves the cursor on the first non-blank in the line.
137 * Caller must take care of undo.
138 * "flags":
139 * SIN_CHANGED: call changed_bytes() if the line was changed.
140 * SIN_INSERT: insert the indent in front of the line.
141 * SIN_UNDO: save line for undo before changing it.
142 * Returns TRUE if the line was changed.
143 */
144 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100145set_indent(
146 int size, /* measured in spaces */
147 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148{
149 char_u *p;
150 char_u *newline;
151 char_u *oldline;
152 char_u *s;
153 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000154 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 int line_len;
156 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000157 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200158#ifdef FEAT_VARTABS
159 int ind_col = 0;
160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000162 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000163 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
166 /*
167 * First check if there is anything to do and compute the number of
168 * characters needed for the indent.
169 */
170 todo = size;
171 ind_len = 0;
172 p = oldline = ml_get_curline();
173
174 /* Calculate the buffer size for the new indent, and check to see if it
175 * isn't already set */
176
Bram Moolenaar5002c292007-07-24 13:26:15 +0000177 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
178 * 'preserveindent' are set count the number of characters at the
179 * beginning of the line to be copied */
180 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 {
182 /* If 'preserveindent' is set then reuse as much as possible of
183 * the existing indent structure for the new indent */
184 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
185 {
186 ind_done = 0;
187
188 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100189 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {
191 if (*p == TAB)
192 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200193#ifdef FEAT_VARTABS
194 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
195 curbuf->b_p_vts_array);
196#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 tab_pad = (int)curbuf->b_p_ts
198 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 /* stop if this tab will overshoot the target */
201 if (todo < tab_pad)
202 break;
203 todo -= tab_pad;
204 ++ind_len;
205 ind_done += tab_pad;
206 }
207 else
208 {
209 --todo;
210 ++ind_len;
211 ++ind_done;
212 }
213 ++p;
214 }
215
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200216#ifdef FEAT_VARTABS
217 /* These diverge from this point. */
218 ind_col = ind_done;
219#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000220 /* Set initial number of whitespace chars to copy if we are
221 * preserving indent but expandtab is set */
222 if (curbuf->b_p_et)
223 orig_char_len = ind_len;
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200226#ifdef FEAT_VARTABS
227 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
228 curbuf->b_p_vts_array);
229#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200231#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000232 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {
234 doit = TRUE;
235 todo -= tab_pad;
236 ++ind_len;
237 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200238#ifdef FEAT_VARTABS
239 ind_col += tab_pad;
240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 }
242 }
243
244 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200245#ifdef FEAT_VARTABS
246 for (;;)
247 {
248 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
249 curbuf->b_p_vts_array);
250 if (todo < tab_pad)
251 break;
252 if (*p != TAB)
253 doit = TRUE;
254 else
255 ++p;
256 todo -= tab_pad;
257 ++ind_len;
258 ind_col += tab_pad;
259 }
260#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 while (todo >= (int)curbuf->b_p_ts)
262 {
263 if (*p != TAB)
264 doit = TRUE;
265 else
266 ++p;
267 todo -= (int)curbuf->b_p_ts;
268 ++ind_len;
269 /* ind_done += (int)curbuf->b_p_ts; */
270 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273 /* count spaces required for indent */
274 while (todo > 0)
275 {
276 if (*p != ' ')
277 doit = TRUE;
278 else
279 ++p;
280 --todo;
281 ++ind_len;
282 /* ++ind_done; */
283 }
284
285 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100286 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 return FALSE;
288
289 /* Allocate memory for the new line. */
290 if (flags & SIN_INSERT)
291 p = oldline;
292 else
293 p = skipwhite(p);
294 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000295
296 /* If 'preserveindent' and 'expandtab' are both set keep the original
297 * characters and allocate accordingly. We will fill the rest with spaces
298 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000299 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000300 {
301 newline = alloc(orig_char_len + size - ind_done + line_len);
302 if (newline == NULL)
303 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000304 todo = size - ind_done;
305 ind_len = orig_char_len + todo; /* Set total length of indent in
306 * characters, which may have been
307 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000308 p = oldline;
309 s = newline;
310 while (orig_char_len > 0)
311 {
312 *s++ = *p++;
313 orig_char_len--;
314 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000315
Bram Moolenaar5002c292007-07-24 13:26:15 +0000316 /* Skip over any additional white space (useful when newindent is less
317 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100318 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000319 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000320
Bram Moolenaar5002c292007-07-24 13:26:15 +0000321 }
322 else
323 {
324 todo = size;
325 newline = alloc(ind_len + line_len);
326 if (newline == NULL)
327 return FALSE;
328 s = newline;
329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
331 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 /* if 'expandtab' isn't set: use TABs */
333 if (!curbuf->b_p_et)
334 {
335 /* If 'preserveindent' is set then reuse as much as possible of
336 * the existing indent structure for the new indent */
337 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
338 {
339 p = oldline;
340 ind_done = 0;
341
Bram Moolenaar1c465442017-03-12 20:10:05 +0100342 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
344 if (*p == TAB)
345 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200346#ifdef FEAT_VARTABS
347 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
348 curbuf->b_p_vts_array);
349#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 tab_pad = (int)curbuf->b_p_ts
351 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /* stop if this tab will overshoot the target */
354 if (todo < tab_pad)
355 break;
356 todo -= tab_pad;
357 ind_done += tab_pad;
358 }
359 else
360 {
361 --todo;
362 ++ind_done;
363 }
364 *s++ = *p++;
365 }
366
367 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200368#ifdef FEAT_VARTABS
369 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
370 curbuf->b_p_vts_array);
371#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (todo >= tab_pad)
375 {
376 *s++ = TAB;
377 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200378#ifdef FEAT_VARTABS
379 ind_done += tab_pad;
380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 }
382
383 p = skipwhite(p);
384 }
385
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200386#ifdef FEAT_VARTABS
387 for (;;)
388 {
389 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
390 curbuf->b_p_vts_array);
391 if (todo < tab_pad)
392 break;
393 *s++ = TAB;
394 todo -= tab_pad;
395 ind_done += tab_pad;
396 }
397#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 while (todo >= (int)curbuf->b_p_ts)
399 {
400 *s++ = TAB;
401 todo -= (int)curbuf->b_p_ts;
402 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 }
405 while (todo > 0)
406 {
407 *s++ = ' ';
408 --todo;
409 }
410 mch_memmove(s, p, (size_t)line_len);
411
Bram Moolenaar663bc892019-01-08 23:07:24 +0100412 // Replace the line (unless undo fails).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
414 {
415 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
416 if (flags & SIN_CHANGED)
417 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar663bc892019-01-08 23:07:24 +0100418
419 // Correct saved cursor position if it is in this line.
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200420 if (saved_cursor.lnum == curwin->w_cursor.lnum)
421 {
422 if (saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100423 // cursor was after the indent, adjust for the number of
424 // bytes added/removed
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200425 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
426 else if (saved_cursor.col >= (colnr_T)(s - newline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100427 // cursor was in the indent, and is now after it, put it back
428 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200429 saved_cursor.col = (colnr_T)(s - newline);
430 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100431#ifdef FEAT_TEXT_PROP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200432 {
433 int added = ind_len - (colnr_T)(p - oldline);
434
435 // When increasing indent this behaves like spaces were inserted at
436 // the old indent, when decreasing indent it behaves like spaces
437 // were deleted at the new indent.
438 adjust_prop_columns(curwin->w_cursor.lnum,
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200439 (colnr_T)(added > 0 ? (p - oldline) : ind_len), added, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200440 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100441#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000442 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 }
444 else
445 vim_free(newline);
446
447 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000448 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449}
450
451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 * Return the indent of the current line after a number. Return -1 if no
453 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000454 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 */
456 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100457get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 colnr_T col;
460 pos_T pos;
461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 regmatch_T regmatch;
463 int lead_len = 0; /* length of comment leader */
464
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (lnum > curbuf->b_ml.ml_line_count)
466 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000467 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468
469#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200470 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
471 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200472 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000473#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200474 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
475 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200476 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200477 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200478
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200479 /* vim_regexec() expects a pointer to a line. This lets us
480 * start matching for the flp beyond any comment leader... */
481 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200482 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200483 pos.lnum = lnum;
484 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200485 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200486 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200487 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200488 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000489
490 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 getvcol(curwin, &pos, &col, NULL, NULL);
493 return (int)col;
494}
495
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496#if defined(FEAT_LINEBREAK) || defined(PROTO)
497/*
498 * Return appropriate space number for breakindent, taking influencing
499 * parameters into account. Window must be specified, since it is not
500 * necessarily always the current one.
501 */
502 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100503get_breakindent_win(
504 win_T *wp,
505 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506{
507 static int prev_indent = 0; /* cached indent value */
508 static long prev_ts = 0L; /* cached tabstop value */
509 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100510 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200511#ifdef FEAT_VARTABS
512 static int *prev_vts = NULL; /* cached vartabs values */
513#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514 int bri = 0;
515 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200516 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200517 - ((wp->w_p_nu || wp->w_p_rnu)
518 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
519 ? number_width(wp) + 1 : 0);
520
521 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200522 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200523 || prev_tick != CHANGEDTICK(wp->w_buffer)
524#ifdef FEAT_VARTABS
525 || prev_vts != wp->w_buffer->b_p_vts_array
526#endif
527 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200528 {
529 prev_line = line;
530 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100531 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200532#ifdef FEAT_VARTABS
533 prev_vts = wp->w_buffer->b_p_vts_array;
534 prev_indent = get_indent_str_vtab(line,
535 (int)wp->w_buffer->b_p_ts,
536 wp->w_buffer->b_p_vts_array, wp->w_p_list);
537#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200538 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200539 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200540#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200541 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200542 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200543
544 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200545 if (wp->w_p_brisbr)
546 bri -= vim_strsize(p_sbr);
547
548 /* Add offset for number column, if 'n' is in 'cpoptions' */
549 bri += win_col_off2(wp);
550
551 /* never indent past left window margin */
552 if (bri < 0)
553 bri = 0;
554 /* always leave at least bri_min characters on the left,
555 * if text width is sufficient */
556 else if (bri > eff_wwidth - wp->w_p_brimin)
557 bri = (eff_wwidth - wp->w_p_brimin < 0)
558 ? 0 : eff_wwidth - wp->w_p_brimin;
559
560 return bri;
561}
562#endif
563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#if defined(FEAT_COMMENTS) || defined(PROTO)
565/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200566 * get_leader_len() returns the length in bytes of the prefix of the given
567 * string which introduces a comment. If this string is not a comment then
568 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 * When "flags" is not NULL, it is set to point to the flags of the recognized
570 * comment leader.
571 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +0200572 * If "include_space" is set, include trailing whitespace while calculating the
573 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 */
575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100576get_leader_len(
577 char_u *line,
578 char_u **flags,
579 int backward,
580 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
582 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +0200583 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 int got_com = FALSE;
585 int found_one;
586 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
587 char_u *string; /* pointer to comment string */
588 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +0200589 int middle_match_len = 0;
590 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +0200591 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar81340392012-06-06 16:12:59 +0200593 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100594 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 ++i;
596
597 /*
598 * Repeat to match several nested comment strings.
599 */
Bram Moolenaara4271d52011-05-10 13:38:27 +0200600 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {
602 /*
603 * scan through the 'comments' option for a match
604 */
605 found_one = FALSE;
606 for (list = curbuf->b_p_com; *list; )
607 {
Bram Moolenaara4271d52011-05-10 13:38:27 +0200608 /* Get one option part into part_buf[]. Advance "list" to next
609 * one. Put "string" at start of string. */
610 if (!got_com && flags != NULL)
611 *flags = list; /* remember where flags started */
612 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
614 string = vim_strchr(part_buf, ':');
615 if (string == NULL) /* missing ':', ignore this part */
616 continue;
617 *string++ = NUL; /* isolate flags from string */
618
Bram Moolenaara4271d52011-05-10 13:38:27 +0200619 /* If we found a middle match previously, use that match when this
620 * is not a middle or end. */
621 if (middle_match_len != 0
622 && vim_strchr(part_buf, COM_MIDDLE) == NULL
623 && vim_strchr(part_buf, COM_END) == NULL)
624 break;
625
626 /* When we already found a nested comment, only accept further
627 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
629 continue;
630
Bram Moolenaara4271d52011-05-10 13:38:27 +0200631 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
633 continue;
634
Bram Moolenaara4271d52011-05-10 13:38:27 +0200635 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 * When string starts with white space, must have some white space
637 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +0200638 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100639 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100641 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200642 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100643 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 ++string;
645 }
646 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
647 ;
648 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +0200649 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
Bram Moolenaara4271d52011-05-10 13:38:27 +0200651 /* When 'b' flag used, there must be white space or an
652 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100654 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 continue;
656
Bram Moolenaara4271d52011-05-10 13:38:27 +0200657 /* We have found a match, stop searching unless this is a middle
658 * comment. The middle comment can be a substring of the end
659 * comment in which case it's better to return the length of the
660 * end comment and its flags. Thus we keep searching with middle
661 * and end matches and use an end match if it matches better. */
662 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
663 {
664 if (middle_match_len == 0)
665 {
666 middle_match_len = j;
667 saved_flags = prev_list;
668 }
669 continue;
670 }
671 if (middle_match_len != 0 && j > middle_match_len)
672 /* Use this match instead of the middle match, since it's a
673 * longer thus better match. */
674 middle_match_len = 0;
675
676 if (middle_match_len == 0)
677 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 found_one = TRUE;
679 break;
680 }
681
Bram Moolenaara4271d52011-05-10 13:38:27 +0200682 if (middle_match_len != 0)
683 {
684 /* Use the previously found middle match after failing to find a
685 * match with an end. */
686 if (!got_com && flags != NULL)
687 *flags = saved_flags;
688 i += middle_match_len;
689 found_one = TRUE;
690 }
691
692 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (!found_one)
694 break;
695
Bram Moolenaar81340392012-06-06 16:12:59 +0200696 result = i;
697
Bram Moolenaara4271d52011-05-10 13:38:27 +0200698 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100699 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++i;
701
Bram Moolenaar81340392012-06-06 16:12:59 +0200702 if (include_space)
703 result = i;
704
Bram Moolenaara4271d52011-05-10 13:38:27 +0200705 /* If this comment doesn't nest, stop here. */
706 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (vim_strchr(part_buf, COM_NEST) == NULL)
708 break;
709 }
Bram Moolenaar81340392012-06-06 16:12:59 +0200710 return result;
711}
Bram Moolenaara4271d52011-05-10 13:38:27 +0200712
Bram Moolenaar81340392012-06-06 16:12:59 +0200713/*
714 * Return the offset at which the last comment in line starts. If there is no
715 * comment in the whole line, -1 is returned.
716 *
717 * When "flags" is not null, it is set to point to the flags describing the
718 * recognized comment leader.
719 */
720 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +0200722{
723 int result = -1;
724 int i, j;
725 int lower_check_bound = 0;
726 char_u *string;
727 char_u *com_leader;
728 char_u *com_flags;
729 char_u *list;
730 int found_one;
731 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
732
733 /*
734 * Repeat to match several nested comment strings.
735 */
736 i = (int)STRLEN(line);
737 while (--i >= lower_check_bound)
738 {
739 /*
740 * scan through the 'comments' option for a match
741 */
742 found_one = FALSE;
743 for (list = curbuf->b_p_com; *list; )
744 {
745 char_u *flags_save = list;
746
747 /*
748 * Get one option part into part_buf[]. Advance list to next one.
749 * put string at start of string.
750 */
751 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
752 string = vim_strchr(part_buf, ':');
753 if (string == NULL) /* If everything is fine, this cannot actually
754 * happen. */
Bram Moolenaar81340392012-06-06 16:12:59 +0200755 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200756 *string++ = NUL; /* Isolate flags from string. */
757 com_leader = string;
758
759 /*
760 * Line contents and string must match.
761 * When string starts with white space, must have some white space
762 * (but the amount does not need to match, there might be a mix of
763 * TABs and spaces).
764 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100765 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200766 {
Bram Moolenaar1c465442017-03-12 20:10:05 +0100767 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +0200768 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100769 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200770 ++string;
771 }
772 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
773 /* do nothing */;
774 if (string[j] != NUL)
775 continue;
776
777 /*
778 * When 'b' flag used, there must be white space or an
779 * end-of-line after the string in the line.
780 */
781 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +0100782 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +0200783 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +0100784
Bram Moolenaar4af72592018-12-09 15:00:52 +0100785 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +0100786 {
Bram Moolenaar4af72592018-12-09 15:00:52 +0100787 // For a middlepart comment, only consider it to match if
788 // everything before the current position in the line is
789 // whitespace. Otherwise we would think we are inside a
790 // comment if the middle part appears somewhere in the middle
791 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +0100792 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
793 ;
794 if (j < i)
795 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +0200796 }
797
798 /*
799 * We have found a match, stop searching.
800 */
801 found_one = TRUE;
802
803 if (flags)
804 *flags = flags_save;
805 com_flags = flags_save;
806
807 break;
808 }
809
810 if (found_one)
811 {
812 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
813 int len1, len2, off;
814
815 result = i;
816 /*
817 * If this comment nests, continue searching.
818 */
819 if (vim_strchr(part_buf, COM_NEST) != NULL)
820 continue;
821
822 lower_check_bound = i;
823
824 /* Let's verify whether the comment leader found is a substring
825 * of other comment leaders. If it is, let's adjust the
826 * lower_check_bound so that we make sure that we have determined
827 * the comment leader correctly.
828 */
829
Bram Moolenaar1c465442017-03-12 20:10:05 +0100830 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +0200831 ++com_leader;
832 len1 = (int)STRLEN(com_leader);
833
834 for (list = curbuf->b_p_com; *list; )
835 {
836 char_u *flags_save = list;
837
838 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
839 if (flags_save == com_flags)
840 continue;
841 string = vim_strchr(part_buf2, ':');
842 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100843 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +0200844 ++string;
845 len2 = (int)STRLEN(string);
846 if (len2 == 0)
847 continue;
848
849 /* Now we have to verify whether string ends with a substring
850 * beginning the com_leader. */
851 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
852 {
853 --off;
854 if (!STRNCMP(string + off, com_leader, len2 - off))
855 {
856 if (i - off < lower_check_bound)
857 lower_check_bound = i - off;
858 }
859 }
860 }
861 }
862 }
863 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864}
865#endif
866
867/*
868 * Return the number of window lines occupied by buffer line "lnum".
869 */
870 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100871plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 return plines_win(curwin, lnum, TRUE);
874}
875
876 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877plines_win(
878 win_T *wp,
879 linenr_T lnum,
880 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882#if defined(FEAT_DIFF) || defined(PROTO)
883 /* Check for filler lines above this buffer line. When folded the result
884 * is one line anyway. */
885 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
886}
887
888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100889plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 return plines_win_nofill(curwin, lnum, TRUE);
892}
893
894 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100895plines_win_nofill(
896 win_T *wp,
897 linenr_T lnum,
898 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900#endif
901 int lines;
902
903 if (!wp->w_p_wrap)
904 return 1;
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (wp->w_width == 0)
907 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909#ifdef FEAT_FOLDING
910 /* A folded lines is handled just like an empty line. */
911 /* NOTE: Caller must handle lines that are MAYBE folded. */
912 if (lineFolded(wp, lnum) == TRUE)
913 return 1;
914#endif
915
916 lines = plines_win_nofold(wp, lnum);
917 if (winheight > 0 && lines > wp->w_height)
918 return (int)wp->w_height;
919 return lines;
920}
921
922/*
923 * Return number of window lines physical line "lnum" will occupy in window
924 * "wp". Does not care about folding, 'wrap' or 'diff'.
925 */
926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100927plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 char_u *s;
930 long col;
931 int width;
932
933 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
934 if (*s == NUL) /* empty line */
935 return 1;
936 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
937
938 /*
939 * If list mode is on, then the '$' at the end of the line may take up one
940 * extra column.
941 */
942 if (wp->w_p_list && lcs_eol != NUL)
943 col += 1;
944
945 /*
Bram Moolenaar64486672010-05-16 15:46:46 +0200946 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200948 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (width <= 0)
950 return 32000;
951 if (col <= width)
952 return 1;
953 col -= width;
954 width += win_col_off2(wp);
955 return (col + (width - 1)) / width + 1;
956}
957
958/*
959 * Like plines_win(), but only reports the number of physical screen lines
960 * used from the start of the line to the given column number.
961 */
962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100963plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 long col;
966 char_u *s;
967 int lines = 0;
968 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200969 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971#ifdef FEAT_DIFF
972 /* Check for filler lines above this buffer line. When folded the result
973 * is one line anyway. */
974 lines = diff_check_fill(wp, lnum);
975#endif
976
977 if (!wp->w_p_wrap)
978 return lines + 1;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (wp->w_width == 0)
981 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar597a4222014-06-25 14:39:50 +0200983 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 col = 0;
986 while (*s != NUL && --column >= 0)
987 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200988 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100989 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991
992 /*
993 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
994 * INSERT mode, then col must be adjusted so that it represents the last
995 * screen position of the TAB. This only fixes an error when the TAB wraps
996 * from one screen line to the next (when 'columns' is not a multiple of
997 * 'ts') -- webb.
998 */
999 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02001000 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001003 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 */
Bram Moolenaar02631462017-09-22 15:20:32 +02001005 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00001006 if (width <= 0)
1007 return 9999;
1008
1009 lines += 1;
1010 if (col > width)
1011 lines += (col - width) / (width + win_col_off2(wp)) + 1;
1012 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001016plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
1018 int count = 0;
1019
1020 while (first <= last)
1021 {
1022#ifdef FEAT_FOLDING
1023 int x;
1024
1025 /* Check if there are any really folded lines, but also included lines
1026 * that are maybe folded. */
1027 x = foldedCount(wp, first, NULL);
1028 if (x > 0)
1029 {
1030 ++count; /* count 1 for "+-- folded" line */
1031 first += x;
1032 }
1033 else
1034#endif
1035 {
1036#ifdef FEAT_DIFF
1037 if (first == wp->w_topline)
1038 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
1039 else
1040#endif
1041 count += plines_win(wp, first, TRUE);
1042 ++first;
1043 }
1044 }
1045 return (count);
1046}
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001049gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001051 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001053 /* When searching columns is sometimes put at the end of a line. */
1054 if (pos->col == MAXCOL)
1055 return NUL;
1056 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (has_mbyte)
1058 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 return (int)*ptr;
1060}
1061
1062 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (has_mbyte)
1066 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return (int)*ml_get_cursor();
1068}
1069
1070/*
1071 * Write a character at the current cursor position.
1072 * It is directly written into the block.
1073 */
1074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
1078 + curwin->w_cursor.col) = c;
1079}
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081/*
1082 * When extra == 0: Return TRUE if the cursor is before or on the first
1083 * non-blank in the line.
1084 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
1085 * the line.
1086 */
1087 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001088inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 char_u *ptr;
1091 colnr_T col;
1092
Bram Moolenaar1c465442017-03-12 20:10:05 +01001093 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 ++ptr;
1095 if (col >= curwin->w_cursor.col + extra)
1096 return TRUE;
1097 else
1098 return FALSE;
1099}
1100
1101/*
1102 * Skip to next part of an option argument: Skip space and comma.
1103 */
1104 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001105skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
1107 if (*p == ',')
1108 ++p;
1109 while (*p == ' ')
1110 ++p;
1111 return p;
1112}
1113
1114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * check_status: called when the status bars for the buffer 'buf'
1116 * need to be updated
1117 */
1118 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001119check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
1121 win_T *wp;
1122
Bram Moolenaar29323592016-07-24 22:04:11 +02001123 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (wp->w_buffer == buf && wp->w_status_height)
1125 {
1126 wp->w_redr_status = TRUE;
1127 if (must_redraw < VALID)
1128 must_redraw = VALID;
1129 }
1130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * Ask for a reply from the user, a 'y' or a 'n'.
1134 * No other characters are accepted, the message is repeated until a valid
1135 * reply is entered or CTRL-C is hit.
1136 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
1137 * from any buffers but directly from the user.
1138 *
1139 * return the 'y' or 'n'
1140 */
1141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001142ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143{
1144 int r = ' ';
1145 int save_State = State;
1146
1147 if (exiting) /* put terminal in raw mode for this question */
1148 settmode(TMODE_RAW);
1149 ++no_wait_return;
1150#ifdef USE_ON_FLY_SCROLL
1151 dont_scroll = TRUE; /* disallow scrolling here */
1152#endif
1153 State = CONFIRM; /* mouse behaves like with :confirm */
1154#ifdef FEAT_MOUSE
1155 setmouse(); /* disables mouse for xterm */
1156#endif
1157 ++no_mapping;
1158 ++allow_keys; /* no mapping here, but recognize keys */
1159
1160 while (r != 'y' && r != 'n')
1161 {
1162 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001163 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (direct)
1165 r = get_keystroke();
1166 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00001167 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (r == Ctrl_C || r == ESC)
1169 r = 'n';
1170 msg_putchar(r); /* show what you typed */
1171 out_flush();
1172 }
1173 --no_wait_return;
1174 State = save_State;
1175#ifdef FEAT_MOUSE
1176 setmouse();
1177#endif
1178 --no_mapping;
1179 --allow_keys;
1180
1181 return r;
1182}
1183
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001184#if defined(FEAT_MOUSE) || defined(PROTO)
1185/*
1186 * Return TRUE if "c" is a mouse key.
1187 */
1188 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001189is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001190{
1191 return c == K_LEFTMOUSE
1192 || c == K_LEFTMOUSE_NM
1193 || c == K_LEFTDRAG
1194 || c == K_LEFTRELEASE
1195 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001196 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001197 || c == K_MIDDLEMOUSE
1198 || c == K_MIDDLEDRAG
1199 || c == K_MIDDLERELEASE
1200 || c == K_RIGHTMOUSE
1201 || c == K_RIGHTDRAG
1202 || c == K_RIGHTRELEASE
1203 || c == K_MOUSEDOWN
1204 || c == K_MOUSEUP
1205 || c == K_MOUSELEFT
1206 || c == K_MOUSERIGHT
1207 || c == K_X1MOUSE
1208 || c == K_X1DRAG
1209 || c == K_X1RELEASE
1210 || c == K_X2MOUSE
1211 || c == K_X2DRAG
1212 || c == K_X2RELEASE;
1213}
1214#endif
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
1217 * Get a key stroke directly from the user.
1218 * Ignores mouse clicks and scrollbar events, except a click for the left
1219 * button (used at the more prompt).
1220 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
1221 * Disadvantage: typeahead is ignored.
1222 * Translates the interrupt character for unix to ESC.
1223 */
1224 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001225get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001227 char_u *buf = NULL;
1228 int buflen = 150;
1229 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 int len = 0;
1231 int n;
1232 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001233 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
1235 mapped_ctrl_c = FALSE; /* mappings are not used here */
1236 for (;;)
1237 {
1238 cursor_on();
1239 out_flush();
1240
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001241 /* Leave some room for check_termcode() to insert a key code into (max
1242 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
1243 * bytes. */
1244 maxlen = (buflen - 6 - len) / 3;
1245 if (buf == NULL)
1246 buf = alloc(buflen);
1247 else if (maxlen < 10)
1248 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001249 char_u *t_buf = buf;
1250
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001251 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001252 * escape sequence. */
1253 buflen += 100;
1254 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001255 if (buf == NULL)
1256 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001257 maxlen = (buflen - 6 - len) / 3;
1258 }
1259 if (buf == NULL)
1260 {
1261 do_outofmem_msg((long_u)buflen);
1262 return ESC; /* panic! */
1263 }
1264
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001266 * terminal code to complete. */
1267 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 if (n > 0)
1269 {
1270 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02001271 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001273 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00001275 else if (len > 0)
1276 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar4395a712006-09-05 18:57:57 +00001278 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001279 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00001280 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00001282
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001283 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001284 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001285 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001286 {
1287 /* Redrawing was postponed, do it now. */
1288 update_screen(0);
1289 setcursor(); /* put cursor back where it belongs */
1290 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001291 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01001292 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001293 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01001295 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 continue;
1297
1298 /* Handle modifier and/or special key code. */
1299 n = buf[0];
1300 if (n == K_SPECIAL)
1301 {
1302 n = TO_SPECIAL(buf[1], buf[2]);
1303 if (buf[1] == KS_MODIFIER
1304 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001305#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001306 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01001307#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01001308#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 || n == K_VER_SCROLLBAR
1310 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#endif
1312 )
1313 {
1314 if (buf[1] == KS_MODIFIER)
1315 mod_mask = buf[2];
1316 len -= 3;
1317 if (len > 0)
1318 mch_memmove(buf, buf + 3, (size_t)len);
1319 continue;
1320 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00001321 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (has_mbyte)
1324 {
1325 if (MB_BYTE2LEN(n) > len)
1326 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001327 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 n = (*mb_ptr2char)(buf);
1329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef UNIX
1331 if (n == intr_char)
1332 n = ESC;
1333#endif
1334 break;
1335 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01001336 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
1338 mapped_ctrl_c = save_mapped_ctrl_c;
1339 return n;
1340}
1341
1342/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001343 * Get a number from the user.
1344 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 */
1346 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001347get_number(
1348 int colon, /* allow colon to abort */
1349 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 int n = 0;
1352 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001353 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001355 if (mouse_used != NULL)
1356 *mouse_used = FALSE;
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 /* When not printing messages, the user won't know what to type, return a
1359 * zero (as if CR was hit). */
1360 if (msg_silent != 0)
1361 return 0;
1362
1363#ifdef USE_ON_FLY_SCROLL
1364 dont_scroll = TRUE; /* disallow scrolling here */
1365#endif
1366 ++no_mapping;
1367 ++allow_keys; /* no mapping here, but recognize keys */
1368 for (;;)
1369 {
1370 windgoto(msg_row, msg_col);
1371 c = safe_vgetc();
1372 if (VIM_ISDIGIT(c))
1373 {
1374 n = n * 10 + c - '0';
1375 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001376 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
1379 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001380 if (typed > 0)
1381 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001382 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001383 --typed;
1384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001387#ifdef FEAT_MOUSE
1388 else if (mouse_used != NULL && c == K_LEFTMOUSE)
1389 {
1390 *mouse_used = TRUE;
1391 n = mouse_row + 1;
1392 break;
1393 }
1394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 else if (n == 0 && c == ':' && colon)
1396 {
1397 stuffcharReadbuff(':');
1398 if (!exmode_active)
1399 cmdline_row = msg_row;
1400 skip_redraw = TRUE; /* skip redraw once */
1401 do_redraw = FALSE;
1402 break;
1403 }
1404 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
1405 break;
1406 }
1407 --no_mapping;
1408 --allow_keys;
1409 return n;
1410}
1411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001412/*
1413 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001414 * When "mouse_used" is not NULL allow using the mouse and in that case return
1415 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001416 */
1417 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001418prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001419{
1420 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001421 int save_cmdline_row;
1422 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423
1424 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001425 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001426 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001427 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001428 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001429
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001430 // Set the state such that text can be selected/copied/pasted and we still
1431 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
1432 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001433 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001434 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001435 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001436 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02001437#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001438 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001439 setmouse();
1440#endif
1441
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001442 i = get_number(TRUE, mouse_used);
1443 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001444 {
Bram Moolenaar954bb062019-06-09 16:40:46 +02001445 // don't call wait_return() now
1446 if (msg_row > 0)
1447 cmdline_row = msg_row - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001448 need_wait_return = FALSE;
1449 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00001450 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001451 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001452 else
1453 cmdline_row = save_cmdline_row;
1454 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02001455#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02001456 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02001457 setmouse();
1458#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001460 return i;
1461}
1462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001464msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 long pn;
1467
1468 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1470 return;
1471
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001472 /* We don't want to overwrite another important message, but do overwrite
1473 * a previous "more lines" or "fewer lines" message, so that "5dd" and
1474 * then "put" reports the last action. */
1475 if (keep_msg != NULL && !keep_msg_more)
1476 return;
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (n > 0)
1479 pn = n;
1480 else
1481 pn = -n;
1482
1483 if (pn > p_report)
1484 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001485 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001486 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001487 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001489 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001490 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001492 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
1493 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if (msg(msg_buf))
1495 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001496 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001497 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 }
1500}
1501
1502/*
1503 * flush map and typeahead buffers and give a warning for an error
1504 */
1505 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001506beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 if (emsg_silent == 0)
1509 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001510 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02001511 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513}
1514
1515/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02001516 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 */
1518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001519vim_beep(
1520 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001522#ifdef FEAT_EVAL
1523 called_vim_beep = TRUE;
1524#endif
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (emsg_silent == 0)
1527 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001528 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
1529 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001530#ifdef ELAPSED_FUNC
1531 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01001532 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001533
1534 /* Only beep once per half a second, otherwise a sequence of beeps
1535 * would freeze Vim. */
1536 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
1537 {
1538 did_init = TRUE;
1539 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001541 if (p_vb
1542#ifdef FEAT_GUI
1543 /* While the GUI is starting up the termcap is set for
1544 * the GUI but the output still goes to a terminal. */
1545 && !(gui.in_use && gui.starting)
1546#endif
1547 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001548 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001549 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001550#ifdef FEAT_VTP
1551 /* No restore color information, refresh the screen. */
1552 if (has_vtp_working() != 0
1553# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02001554 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01001555# endif
1556 )
1557 {
1558 redraw_later(CLEAR);
1559 update_screen(0);
1560 redrawcmd();
1561 }
1562#endif
1563 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02001564 else
1565 out_char(BELL);
1566#ifdef ELAPSED_FUNC
1567 }
1568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001570
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001571 /* When 'debug' contains "beep" produce a message. If we are sourcing
1572 * a script or executing a function give the user a hint where the beep
1573 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001574 if (vim_strchr(p_debug, 'e') != NULL)
1575 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001576 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01001577 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580}
1581
1582/*
1583 * To get the "real" home directory:
1584 * - get value of $HOME
1585 * For Unix:
1586 * - go to that directory
1587 * - do mch_dirname() to get the real name of that directory.
1588 * This also works with mounts and links.
1589 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01001590 * For Windows:
1591 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001594init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 char_u *var;
1597
Bram Moolenaar05159a02005-02-26 23:04:13 +00001598 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001599 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601#ifdef VMS
1602 var = mch_getenv((char_u *)"SYS$LOGIN");
1603#else
1604 var = mch_getenv((char_u *)"HOME");
1605#endif
1606
Bram Moolenaar4f974752019-02-17 17:44:42 +01001607#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02001609 * Typically, $HOME is not defined on Windows, unless the user has
1610 * specifically defined it for Vim's sake. However, on Windows NT
1611 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
1612 * each user. Try constructing $HOME from these.
1613 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02001614 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02001615 {
1616 char_u *homedrive, *homepath;
1617
1618 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
1619 homepath = mch_getenv((char_u *)"HOMEPATH");
1620 if (homepath == NULL || *homepath == NUL)
1621 homepath = (char_u *)"\\";
1622 if (homedrive != NULL
1623 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
1624 {
1625 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
1626 if (NameBuff[0] != NUL)
1627 var = NameBuff;
1628 }
1629 }
1630
1631 if (var == NULL)
1632 var = mch_getenv((char_u *)"USERPROFILE");
1633
1634 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 * Weird but true: $HOME may contain an indirect reference to another
1636 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
1637 * when $HOME is being set.
1638 */
1639 if (var != NULL && *var == '%')
1640 {
1641 char_u *p;
1642 char_u *exp;
1643
1644 p = vim_strchr(var + 1, '%');
1645 if (p != NULL)
1646 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001647 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 exp = mch_getenv(NameBuff);
1649 if (exp != NULL && *exp != NUL
1650 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
1651 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00001652 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
1655 }
1656 }
1657
Bram Moolenaar48340b62017-08-29 22:08:53 +02001658 if (var != NULL && *var == NUL) /* empty is same as not set */
1659 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar05159a02005-02-26 23:04:13 +00001661 if (enc_utf8 && var != NULL)
1662 {
1663 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02001664 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001665
1666 /* Convert from active codepage to UTF-8. Other conversions are
1667 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001668 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001669 if (pp != NULL)
1670 {
1671 homedir = pp;
1672 return;
1673 }
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 /*
1677 * Default home dir is C:/
1678 * Best assumption we can make in such a situation.
1679 */
1680 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001681 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02001683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (var != NULL)
1685 {
1686#ifdef UNIX
1687 /*
1688 * Change to the directory and get the actual path. This resolves
1689 * links. Don't do it when we can't return.
1690 */
1691 if (mch_dirname(NameBuff, MAXPATHL) == OK
1692 && mch_chdir((char *)NameBuff) == 0)
1693 {
1694 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
1695 var = IObuff;
1696 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001697 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699#endif
1700 homedir = vim_strsave(var);
1701 }
1702}
1703
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001704#if defined(EXITFREE) || defined(PROTO)
1705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001706free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001707{
1708 vim_free(homedir);
1709}
Bram Moolenaar24305862012-08-15 14:05:05 +02001710
Bram Moolenaar24305862012-08-15 14:05:05 +02001711 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001712free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02001713{
1714 ga_clear_strings(&ga_users);
1715}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001716#endif
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001719 * Call expand_env() and store the result in an allocated string.
1720 * This is not very memory efficient, this expects the result to be freed
1721 * again soon.
1722 */
1723 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001724expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001725{
1726 return expand_env_save_opt(src, FALSE);
1727}
1728
1729/*
1730 * Idem, but when "one" is TRUE handle the string as one file name, only
1731 * expand "~" at the start.
1732 */
1733 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001734expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001735{
1736 char_u *p;
1737
1738 p = alloc(MAXPATHL);
1739 if (p != NULL)
1740 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
1741 return p;
1742}
1743
1744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 * Expand environment variable with path name.
1746 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001747 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 * If anything fails no expansion is done and dst equals src.
1749 */
1750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001751expand_env(
1752 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
1753 char_u *dst, /* where to put the result */
1754 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00001756 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757}
1758
1759 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760expand_env_esc(
1761 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
1762 char_u *dst, /* where to put the result */
1763 int dstlen, /* maximum length of the result */
1764 int esc, /* escape spaces in expanded variables */
1765 int one, /* "srcp" is one file name */
1766 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001768 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 char_u *tail;
1770 int c;
1771 char_u *var;
1772 int copy_char;
1773 int mustfree; /* var was allocated, need to free it later */
1774 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001775 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001777 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001778 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001779
1780 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 --dstlen; /* leave one char space for "\," */
1782 while (*src && dstlen > 0)
1783 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001784#ifdef FEAT_EVAL
1785 /* Skip over `=expr`. */
1786 if (src[0] == '`' && src[1] == '=')
1787 {
1788 size_t len;
1789
1790 var = src;
1791 src += 2;
1792 (void)skip_expr(&src);
1793 if (*src == '`')
1794 ++src;
1795 len = src - var;
1796 if (len > (size_t)dstlen)
1797 len = dstlen;
1798 vim_strncpy(dst, var, len);
1799 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02001800 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02001801 continue;
1802 }
1803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001805 if ((*src == '$'
1806#ifdef VMS
1807 && at_start
1808#endif
1809 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001810#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 || *src == '%'
1812#endif
1813 || (*src == '~' && at_start))
1814 {
1815 mustfree = FALSE;
1816
1817 /*
1818 * The variable name is copied into dst temporarily, because it may
1819 * be a string in read-only memory and a NUL needs to be appended.
1820 */
1821 if (*src != '~') /* environment var */
1822 {
1823 tail = src + 1;
1824 var = dst;
1825 c = dstlen - 1;
1826
1827#ifdef UNIX
1828 /* Unix has ${var-name} type environment vars */
1829 if (*tail == '{' && !vim_isIDc('{'))
1830 {
1831 tail++; /* ignore '{' */
1832 while (c-- > 0 && *tail && *tail != '}')
1833 *var++ = *tail++;
1834 }
1835 else
1836#endif
1837 {
1838 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001839#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 || (*src == '%' && *tail != '%')
1841#endif
1842 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001846#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847# ifdef UNIX
1848 if (src[1] == '{' && *tail != '}')
1849# else
1850 if (*src == '%' && *tail != '%')
1851# endif
1852 var = NULL;
1853 else
1854 {
1855# ifdef UNIX
1856 if (src[1] == '{')
1857# else
1858 if (*src == '%')
1859#endif
1860 ++tail;
1861#endif
1862 *var = NUL;
1863 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001864#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866#endif
1867 }
1868 /* home directory */
1869 else if ( src[1] == NUL
1870 || vim_ispathsep(src[1])
1871 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
1872 {
1873 var = homedir;
1874 tail = src + 1;
1875 }
1876 else /* user directory */
1877 {
1878#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
1879 /*
1880 * Copy ~user to dst[], so we can put a NUL after it.
1881 */
1882 tail = src;
1883 var = dst;
1884 c = dstlen - 1;
1885 while ( c-- > 0
1886 && *tail
1887 && vim_isfilec(*tail)
1888 && !vim_ispathsep(*tail))
1889 *var++ = *tail++;
1890 *var = NUL;
1891# ifdef UNIX
1892 /*
1893 * If the system supports getpwnam(), use it.
1894 * Otherwise, or if getpwnam() fails, the shell is used to
1895 * expand ~user. This is slower and may fail if the shell
1896 * does not support ~user (old versions of /bin/sh).
1897 */
1898# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
1899 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001900 /* Note: memory allocated by getpwnam() is never freed.
1901 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01001902 struct passwd *pw = (*dst == NUL)
1903 ? NULL : getpwnam((char *)dst + 1);
1904
1905 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 if (var == NULL)
1908# endif
1909 {
1910 expand_T xpc;
1911
1912 ExpandInit(&xpc);
1913 xpc.xp_context = EXPAND_FILES;
1914 var = ExpandOne(&xpc, dst, NULL,
1915 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 mustfree = TRUE;
1917 }
1918
1919# else /* !UNIX, thus VMS */
1920 /*
1921 * USER_HOME is a comma-separated list of
1922 * directories to search for the user account in.
1923 */
1924 {
1925 char_u test[MAXPATHL], paths[MAXPATHL];
1926 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001927 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
1929 STRCPY(paths, USER_HOME);
1930 next_path = paths;
1931 while (*next_path)
1932 {
1933 for (path = next_path; *next_path && *next_path != ',';
1934 next_path++);
1935 if (*next_path)
1936 *next_path++ = NUL;
1937 STRCPY(test, path);
1938 STRCAT(test, "/");
1939 STRCAT(test, dst + 1);
1940 if (mch_stat(test, &st) == 0)
1941 {
1942 var = alloc(STRLEN(test) + 1);
1943 STRCPY(var, test);
1944 mustfree = TRUE;
1945 break;
1946 }
1947 }
1948 }
1949# endif /* UNIX */
1950#else
1951 /* cannot expand user's home directory, so don't try */
1952 var = NULL;
1953 tail = (char_u *)""; /* for gcc */
1954#endif /* UNIX || VMS */
1955 }
1956
1957#ifdef BACKSLASH_IN_FILENAME
1958 /* If 'shellslash' is set change backslashes to forward slashes.
1959 * Can't use slash_adjust(), p_ssl may be set temporarily. */
1960 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
1961 {
1962 char_u *p = vim_strsave(var);
1963
1964 if (p != NULL)
1965 {
1966 if (mustfree)
1967 vim_free(var);
1968 var = p;
1969 mustfree = TRUE;
1970 forward_slash(var);
1971 }
1972 }
1973#endif
1974
1975 /* If "var" contains white space, escape it with a backslash.
1976 * Required for ":e ~/tt" when $HOME includes a space. */
1977 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
1978 {
1979 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
1980
1981 if (p != NULL)
1982 {
1983 if (mustfree)
1984 vim_free(var);
1985 var = p;
1986 mustfree = TRUE;
1987 }
1988 }
1989
1990 if (var != NULL && *var != NUL
1991 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
1992 {
1993 STRCPY(dst, var);
1994 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001995 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 /* if var[] ends in a path separator and tail[] starts
1997 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001998 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
2000 && dst[-1] != ':'
2001#endif
2002 && vim_ispathsep(*tail))
2003 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002004 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 src = tail;
2006 copy_char = FALSE;
2007 }
2008 if (mustfree)
2009 vim_free(var);
2010 }
2011
2012 if (copy_char) /* copy at least one char */
2013 {
2014 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00002015 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002016 * Don't do this when "one" is TRUE, to avoid expanding "~" in
2017 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 */
2019 at_start = FALSE;
2020 if (src[0] == '\\' && src[1] != NUL)
2021 {
2022 *dst++ = *src++;
2023 --dstlen;
2024 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002025 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02002027 if (dstlen > 0)
2028 {
2029 *dst++ = *src++;
2030 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002031
Bram Moolenaar1c864092017-08-06 18:15:45 +02002032 if (startstr != NULL && src - startstr_len >= srcp
2033 && STRNCMP(src - startstr_len, startstr,
2034 startstr_len) == 0)
2035 at_start = TRUE;
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02002038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
2040 *dst = NUL;
2041}
2042
2043/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002044 * If the string between "p" and "pend" ends in "name/", return "pend" minus
2045 * the length of "name/". Otherwise return "pend".
2046 */
2047 static char_u *
2048remove_tail(char_u *p, char_u *pend, char_u *name)
2049{
2050 int len = (int)STRLEN(name) + 1;
2051 char_u *newend = pend - len;
2052
2053 if (newend >= p
2054 && fnamencmp(newend, name, len - 1) == 0
2055 && (newend == p || after_pathsep(p, newend)))
2056 return newend;
2057 return pend;
2058}
2059
2060/*
2061 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
2062 * Return NULL if not, return its name in allocated memory otherwise.
2063 */
2064 static char_u *
2065vim_version_dir(char_u *vimdir)
2066{
2067 char_u *p;
2068
2069 if (vimdir == NULL || *vimdir == NUL)
2070 return NULL;
2071 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
2072 if (p != NULL && mch_isdir(p))
2073 return p;
2074 vim_free(p);
2075 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
2076 if (p != NULL && mch_isdir(p))
2077 return p;
2078 vim_free(p);
2079 return NULL;
2080}
2081
2082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 * Vim's version of getenv().
2084 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00002085 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02002086 * "mustfree" is set to TRUE when returned is allocated, it must be
2087 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
2089 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002090vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002092 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 char_u *pend;
2094 int vimruntime;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002095#ifdef MSWIN
2096 WCHAR *wn, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002098 // use "C:/" when $HOME is not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (STRCMP(name, "HOME") == 0)
2100 return homedir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002102 // Use Wide function
2103 wn = enc_to_utf16(name, NULL);
2104 if (wn == NULL)
2105 return NULL;
2106
2107 wp = _wgetenv(wn);
2108 vim_free(wn);
2109
2110 if (wp != NULL && *wp == NUL) // empty is the same as not set
2111 wp = NULL;
2112
2113 if (wp != NULL)
2114 {
2115 p = utf16_to_enc(wp, NULL);
2116 if (p == NULL)
2117 return NULL;
2118
2119 *mustfree = TRUE;
2120 return p;
2121 }
2122#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 p = mch_getenv(name);
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002124 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 p = NULL;
2126
2127 if (p != NULL)
2128 return p;
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002131 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
2133 if (!vimruntime && STRCMP(name, "VIM") != 0)
2134 return NULL;
2135
2136 /*
2137 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
2138 * Don't do this when default_vimruntime_dir is non-empty.
2139 */
2140 if (vimruntime
2141#ifdef HAVE_PATHDEF
2142 && *default_vimruntime_dir == NUL
2143#endif
2144 )
2145 {
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002146#ifdef MSWIN
2147 // Use Wide function
2148 wp = _wgetenv(L"VIM");
2149 if (wp != NULL && *wp == NUL) // empty is the same as not set
2150 wp = NULL;
2151 if (wp != NULL)
2152 {
2153 char_u *q = utf16_to_enc(wp, NULL);
2154 if (q != NULL)
2155 {
2156 p = vim_version_dir(q);
2157 *mustfree = TRUE;
2158 if (p == NULL)
2159 p = q;
2160 }
2161 }
2162#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 p = mch_getenv((char_u *)"VIM");
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002164 if (p != NULL && *p == NUL) // empty is the same as not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 p = NULL;
2166 if (p != NULL)
2167 {
2168 p = vim_version_dir(p);
2169 if (p != NULL)
2170 *mustfree = TRUE;
2171 else
2172 p = mch_getenv((char_u *)"VIM");
2173 }
Bram Moolenaarf0908e62019-03-30 20:11:50 +01002174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176
2177 /*
2178 * When expanding $VIM or $VIMRUNTIME fails, try using:
2179 * - the directory name from 'helpfile' (unless it contains '$')
2180 * - the executable name from argv[0]
2181 */
2182 if (p == NULL)
2183 {
2184 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
2185 p = p_hf;
2186#ifdef USE_EXE_NAME
2187 /*
2188 * Use the name of the executable, obtained from argv[0].
2189 */
2190 else
2191 p = exe_name;
2192#endif
2193 if (p != NULL)
2194 {
2195 /* remove the file name */
2196 pend = gettail(p);
2197
2198 /* remove "doc/" from 'helpfile', if present */
2199 if (p == p_hf)
2200 pend = remove_tail(p, pend, (char_u *)"doc");
2201
2202#ifdef USE_EXE_NAME
2203# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002204 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (p == exe_name)
2206 {
2207 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002208 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002210 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
2211 if (pend1 != pend)
2212 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002213 pnew = alloc(pend1 - p + 15);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002214 if (pnew != NULL)
2215 {
2216 STRNCPY(pnew, p, (pend1 - p));
2217 STRCPY(pnew + (pend1 - p), "Resources/vim");
2218 p = pnew;
2219 pend = p + STRLEN(p);
2220 }
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223# endif
2224 /* remove "src/" from exe_name, if present */
2225 if (p == exe_name)
2226 pend = remove_tail(p, pend, (char_u *)"src");
2227#endif
2228
2229 /* for $VIM, remove "runtime/" or "vim54/", if present */
2230 if (!vimruntime)
2231 {
2232 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
2233 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
2234 }
2235
2236 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002237 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar95e9b492006-03-15 23:04:43 +00002240#ifdef MACOS_X
2241 if (p == exe_name || p == p_hf)
2242#endif
2243 /* check that the result is a directory name */
2244 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01002247 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 else
2249 {
2250#ifdef USE_EXE_NAME
2251 /* may add "/vim54" or "/runtime" if it exists */
2252 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
2253 {
2254 vim_free(p);
2255 p = pend;
2256 }
2257#endif
2258 *mustfree = TRUE;
2259 }
2260 }
2261 }
2262
2263#ifdef HAVE_PATHDEF
2264 /* When there is a pathdef.c file we can use default_vim_dir and
2265 * default_vimruntime_dir */
2266 if (p == NULL)
2267 {
2268 /* Only use default_vimruntime_dir when it is not empty */
2269 if (vimruntime && *default_vimruntime_dir != NUL)
2270 {
2271 p = default_vimruntime_dir;
2272 *mustfree = FALSE;
2273 }
2274 else if (*default_vim_dir != NUL)
2275 {
2276 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
2277 *mustfree = TRUE;
2278 else
2279 {
2280 p = default_vim_dir;
2281 *mustfree = FALSE;
2282 }
2283 }
2284 }
2285#endif
2286
2287 /*
2288 * Set the environment variable, so that the new value can be found fast
2289 * next time, and others can also use it (e.g. Perl).
2290 */
2291 if (p != NULL)
2292 {
2293 if (vimruntime)
2294 {
2295 vim_setenv((char_u *)"VIMRUNTIME", p);
2296 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298 else
2299 {
2300 vim_setenv((char_u *)"VIM", p);
2301 didset_vim = TRUE;
2302 }
2303 }
2304 return p;
2305}
2306
Bram Moolenaar113e1072019-01-20 15:30:40 +01002307#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02002308 void
2309vim_unsetenv(char_u *var)
2310{
2311#ifdef HAVE_UNSETENV
2312 unsetenv((char *)var);
2313#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02002314 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02002315#endif
2316}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002317#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02002318
2319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 * Our portable version of setenv.
2322 */
2323 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002324vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326#ifdef HAVE_SETENV
2327 mch_setenv((char *)name, (char *)val, 1);
2328#else
2329 char_u *envbuf;
2330
2331 /*
2332 * Putenv does not copy the string, it has to remain
2333 * valid. The allocated memory will never be freed.
2334 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002335 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (envbuf != NULL)
2337 {
2338 sprintf((char *)envbuf, "%s=%s", name, val);
2339 putenv((char *)envbuf);
2340 }
2341#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01002342#ifdef FEAT_GETTEXT
2343 /*
2344 * When setting $VIMRUNTIME adjust the directory to find message
2345 * translations to $VIMRUNTIME/lang.
2346 */
2347 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
2348 {
2349 char_u *buf = concat_str(val, (char_u *)"/lang");
2350
2351 if (buf != NULL)
2352 {
2353 bindtextdomain(VIMPACKAGE, (char *)buf);
2354 vim_free(buf);
2355 }
2356 }
2357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358}
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360/*
2361 * Function given to ExpandGeneric() to obtain an environment variable name.
2362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002364get_env_name(
2365 expand_T *xp UNUSED,
2366 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
Bram Moolenaard0573012017-10-28 21:11:06 +02002368# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02002370 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 */
2372 return NULL;
2373# else
2374# ifndef __WIN32__
2375 /* Borland C++ 5.2 has this in a header file. */
2376 extern char **environ;
2377# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002378# define ENVNAMELEN 100
2379 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 char_u *str;
2381 int n;
2382
2383 str = (char_u *)environ[idx];
2384 if (str == NULL)
2385 return NULL;
2386
Bram Moolenaar21cf8232004-07-16 20:18:37 +00002387 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 if (str[n] == '=' || str[n] == NUL)
2390 break;
2391 name[n] = str[n];
2392 }
2393 name[n] = NUL;
2394 return name;
2395# endif
2396}
Bram Moolenaar24305862012-08-15 14:05:05 +02002397
2398/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002399 * Add a user name to the list of users in ga_users.
2400 * Do nothing if user name is NULL or empty.
2401 */
2402 static void
2403add_user(char_u *user, int need_copy)
2404{
2405 char_u *user_copy = (user != NULL && need_copy)
2406 ? vim_strsave(user) : user;
2407
2408 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
2409 {
2410 if (need_copy)
2411 vim_free(user);
2412 return;
2413 }
2414 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
2415}
2416
2417/*
Bram Moolenaar24305862012-08-15 14:05:05 +02002418 * Find all user names for user completion.
2419 * Done only once and then cached.
2420 */
2421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002422init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02002423{
Bram Moolenaar24305862012-08-15 14:05:05 +02002424 static int lazy_init_done = FALSE;
2425
2426 if (lazy_init_done)
2427 return;
2428
2429 lazy_init_done = TRUE;
2430 ga_init2(&ga_users, sizeof(char_u *), 20);
2431
2432# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
2433 {
Bram Moolenaar24305862012-08-15 14:05:05 +02002434 struct passwd* pw;
2435
2436 setpwent();
2437 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002438 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02002439 endpwent();
2440 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002441# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002442 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002443 DWORD nusers = 0, ntotal = 0, i;
2444 PUSER_INFO_0 uinfo;
2445
2446 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
2447 &nusers, &ntotal, NULL) == NERR_Success)
2448 {
2449 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002450 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02002451
2452 NetApiBufferFree(uinfo);
2453 }
2454 }
Bram Moolenaar24305862012-08-15 14:05:05 +02002455# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02002456# if defined(HAVE_GETPWNAM)
2457 {
2458 char_u *user_env = mch_getenv((char_u *)"USER");
2459
2460 // The $USER environment variable may be a valid remote user name (NIS,
2461 // LDAP) not already listed by getpwent(), as getpwent() only lists
2462 // local user names. If $USER is not already listed, check whether it
2463 // is a valid remote user name using getpwnam() and if it is, add it to
2464 // the list of user names.
2465
2466 if (user_env != NULL && *user_env != NUL)
2467 {
2468 int i;
2469
2470 for (i = 0; i < ga_users.ga_len; i++)
2471 {
2472 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
2473
2474 if (STRCMP(local_user, user_env) == 0)
2475 break;
2476 }
2477
2478 if (i == ga_users.ga_len)
2479 {
2480 struct passwd *pw = getpwnam((char *)user_env);
2481
2482 if (pw != NULL)
2483 add_user((char_u *)pw->pw_name, TRUE);
2484 }
2485 }
2486 }
2487# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02002488}
2489
2490/*
2491 * Function given to ExpandGeneric() to obtain an user names.
2492 */
2493 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01002494get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02002495{
2496 init_users();
2497 if (idx < ga_users.ga_len)
2498 return ((char_u **)ga_users.ga_data)[idx];
2499 return NULL;
2500}
2501
2502/*
2503 * Check whether name matches a user name. Return:
2504 * 0 if name does not match any user name.
2505 * 1 if name partially matches the beginning of a user name.
2506 * 2 is name fully matches a user name.
2507 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02002508 int
2509match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02002510{
2511 int i;
2512 int n = (int)STRLEN(name);
2513 int result = 0;
2514
2515 init_users();
2516 for (i = 0; i < ga_users.ga_len; i++)
2517 {
2518 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
2519 return 2; /* full match */
2520 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
2521 result = 1; /* partial match */
2522 }
2523 return result;
2524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
2526/*
Bram Moolenaard6754642005-01-17 22:18:45 +00002527 * Concatenate two strings and return the result in allocated memory.
2528 * Returns NULL when out of memory.
2529 */
2530 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002531concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00002532{
2533 char_u *dest;
2534 size_t l = STRLEN(str1);
2535
Bram Moolenaar964b3742019-05-24 18:54:09 +02002536 dest = alloc(l + STRLEN(str2) + 1L);
Bram Moolenaard6754642005-01-17 22:18:45 +00002537 if (dest != NULL)
2538 {
2539 STRCPY(dest, str1);
2540 STRCPY(dest + l, str2);
2541 }
2542 return dest;
2543}
Bram Moolenaard6754642005-01-17 22:18:45 +00002544
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002546prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002548#if defined(SIGHUP) && defined(SIG_IGN)
2549 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2550 * makes Vim exit and then handling SIGHUP causes various reentrance
2551 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002552 signal(SIGHUP, SIG_IGN);
2553#endif
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555#ifdef FEAT_GUI
2556 if (gui.in_use)
2557 {
2558 gui.dying = TRUE;
2559 out_trash(); /* trash any pending output */
2560 }
2561 else
2562#endif
2563 {
2564 windgoto((int)Rows - 1, 0);
2565
2566 /*
2567 * Switch terminal mode back now, so messages end up on the "normal"
2568 * screen (if there are two screens).
2569 */
2570 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002571 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 out_flush();
2573 }
2574}
2575
2576/*
2577 * Preserve files and exit.
2578 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002579 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
2580 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 */
2582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002583preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 buf_T *buf;
2586
2587 prepare_to_exit();
2588
Bram Moolenaar4770d092006-01-12 23:22:24 +00002589 /* Setting this will prevent free() calls. That avoids calling free()
2590 * recursively when free() was invoked with a bad pointer. */
2591 really_exiting = TRUE;
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 out_str(IObuff);
2594 screen_start(); /* don't know where cursor is now */
2595 out_flush();
2596
2597 ml_close_notmod(); /* close all not-modified buffers */
2598
Bram Moolenaar29323592016-07-24 22:04:11 +02002599 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
2602 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002603 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 screen_start(); /* don't know where cursor is now */
2605 out_flush();
2606 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2607 break;
2608 }
2609 }
2610
2611 ml_close_all(FALSE); /* close all memfiles, without deleting */
2612
Bram Moolenaarbec9c202013-09-05 21:41:39 +02002613 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615 getout(1);
2616}
2617
2618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 * Check for CTRL-C pressed, but only once in a while.
2620 * Should be used instead of ui_breakcheck() for functions that check for
2621 * each line in the file. Calling ui_breakcheck() each time takes too much
2622 * time, because it can be a system call.
2623 */
2624
2625#ifndef BREAKCHECK_SKIP
Bram Moolenaarb4fe0eb2019-07-21 14:50:21 +02002626# define BREAKCHECK_SKIP 1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627#endif
2628
2629static int breakcheck_count = 0;
2630
2631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002632line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 if (++breakcheck_count >= BREAKCHECK_SKIP)
2635 {
2636 breakcheck_count = 0;
2637 ui_breakcheck();
2638 }
2639}
2640
2641/*
2642 * Like line_breakcheck() but check 10 times less often.
2643 */
2644 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002645fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
2648 {
2649 breakcheck_count = 0;
2650 ui_breakcheck();
2651 }
2652}
2653
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002654#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) \
2655 || (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
2656 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658#ifndef SEEK_SET
2659# define SEEK_SET 0
2660#endif
2661#ifndef SEEK_END
2662# define SEEK_END 2
2663#endif
2664
2665/*
2666 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002667 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
2668 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 * Returns an allocated string, or NULL for error.
2670 */
2671 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002672get_cmd_output(
2673 char_u *cmd,
2674 char_u *infile, /* optional input file name */
2675 int flags, /* can be SHELL_SILENT */
2676 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 char_u *tempname;
2679 char_u *command;
2680 char_u *buffer = NULL;
2681 int len;
2682 int i = 0;
2683 FILE *fd;
2684
2685 if (check_restricted() || check_secure())
2686 return NULL;
2687
2688 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02002689 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002691 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return NULL;
2693 }
2694
2695 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002696 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (command == NULL)
2698 goto done;
2699
2700 /*
2701 * Call the shell to execute the command (errors are ignored).
2702 * Don't check timestamps here.
2703 */
2704 ++no_check_timestamps;
2705 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
2706 --no_check_timestamps;
2707
2708 vim_free(command);
2709
2710 /*
2711 * read the names from the file into memory
2712 */
2713# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00002714 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 fd = mch_fopen((char *)tempname, "r");
2716# else
2717 fd = mch_fopen((char *)tempname, READBIN);
2718# endif
2719
2720 if (fd == NULL)
2721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002722 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 goto done;
2724 }
2725
2726 fseek(fd, 0L, SEEK_END);
2727 len = ftell(fd); /* get size of temp file */
2728 fseek(fd, 0L, SEEK_SET);
2729
2730 buffer = alloc(len + 1);
2731 if (buffer != NULL)
2732 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
2733 fclose(fd);
2734 mch_remove(tempname);
2735 if (buffer == NULL)
2736 goto done;
2737#ifdef VMS
2738 len = i; /* VMS doesn't give us what we asked for... */
2739#endif
2740 if (i != len)
2741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002742 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002743 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002745 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002746 {
2747 /* Change NUL into SOH, otherwise the string is truncated. */
2748 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02002749 if (buffer[i] == NUL)
2750 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002751
Bram Moolenaar162bd912010-07-28 22:29:10 +02002752 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02002753 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02002754 else
2755 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757done:
2758 vim_free(tempname);
2759 return buffer;
2760}
Bram Moolenaar26262f82019-09-04 20:59:15 +02002761
2762# if defined(FEAT_EVAL) || defined(PROTO)
2763
Bram Moolenaar840d16f2019-09-10 21:27:18 +02002764 static void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002765get_cmd_output_as_rettv(
2766 typval_T *argvars,
2767 typval_T *rettv,
2768 int retlist)
2769{
2770 char_u *res = NULL;
2771 char_u *p;
2772 char_u *infile = NULL;
2773 int err = FALSE;
2774 FILE *fd;
2775 list_T *list = NULL;
2776 int flags = SHELL_SILENT;
2777
2778 rettv->v_type = VAR_STRING;
2779 rettv->vval.v_string = NULL;
2780 if (check_restricted() || check_secure())
2781 goto errret;
2782
2783 if (argvars[1].v_type != VAR_UNKNOWN)
2784 {
2785 /*
2786 * Write the text to a temp file, to be used for input of the shell
2787 * command.
2788 */
2789 if ((infile = vim_tempname('i', TRUE)) == NULL)
2790 {
2791 emsg(_(e_notmp));
2792 goto errret;
2793 }
2794
2795 fd = mch_fopen((char *)infile, WRITEBIN);
2796 if (fd == NULL)
2797 {
2798 semsg(_(e_notopen), infile);
2799 goto errret;
2800 }
2801 if (argvars[1].v_type == VAR_NUMBER)
2802 {
2803 linenr_T lnum;
2804 buf_T *buf;
2805
2806 buf = buflist_findnr(argvars[1].vval.v_number);
2807 if (buf == NULL)
2808 {
2809 semsg(_(e_nobufnr), argvars[1].vval.v_number);
2810 fclose(fd);
2811 goto errret;
2812 }
2813
2814 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
2815 {
2816 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
2817 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
2818 {
2819 err = TRUE;
2820 break;
2821 }
2822 if (putc(NL, fd) == EOF)
2823 {
2824 err = TRUE;
2825 break;
2826 }
2827 }
2828 }
2829 else if (argvars[1].v_type == VAR_LIST)
2830 {
2831 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
2832 err = TRUE;
2833 }
2834 else
2835 {
2836 size_t len;
2837 char_u buf[NUMBUFLEN];
2838
2839 p = tv_get_string_buf_chk(&argvars[1], buf);
2840 if (p == NULL)
2841 {
2842 fclose(fd);
2843 goto errret; /* type error; errmsg already given */
2844 }
2845 len = STRLEN(p);
2846 if (len > 0 && fwrite(p, len, 1, fd) != 1)
2847 err = TRUE;
2848 }
2849 if (fclose(fd) != 0)
2850 err = TRUE;
2851 if (err)
2852 {
2853 emsg(_("E677: Error writing temp file"));
2854 goto errret;
2855 }
2856 }
2857
2858 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
2859 * echoes typeahead, that messes up the display. */
2860 if (!msg_silent)
2861 flags += SHELL_COOKED;
2862
2863 if (retlist)
2864 {
2865 int len;
2866 listitem_T *li;
2867 char_u *s = NULL;
2868 char_u *start;
2869 char_u *end;
2870 int i;
2871
2872 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
2873 if (res == NULL)
2874 goto errret;
2875
2876 list = list_alloc();
2877 if (list == NULL)
2878 goto errret;
2879
2880 for (i = 0; i < len; ++i)
2881 {
2882 start = res + i;
2883 while (i < len && res[i] != NL)
2884 ++i;
2885 end = res + i;
2886
2887 s = alloc(end - start + 1);
2888 if (s == NULL)
2889 goto errret;
2890
2891 for (p = s; start < end; ++p, ++start)
2892 *p = *start == NUL ? NL : *start;
2893 *p = NUL;
2894
2895 li = listitem_alloc();
2896 if (li == NULL)
2897 {
2898 vim_free(s);
2899 goto errret;
2900 }
2901 li->li_tv.v_type = VAR_STRING;
2902 li->li_tv.v_lock = 0;
2903 li->li_tv.vval.v_string = s;
2904 list_append(list, li);
2905 }
2906
2907 rettv_list_set(rettv, list);
2908 list = NULL;
2909 }
2910 else
2911 {
2912 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
2913#ifdef USE_CRNL
2914 /* translate <CR><NL> into <NL> */
2915 if (res != NULL)
2916 {
2917 char_u *s, *d;
2918
2919 d = res;
2920 for (s = res; *s; ++s)
2921 {
2922 if (s[0] == CAR && s[1] == NL)
2923 ++s;
2924 *d++ = *s;
2925 }
2926 *d = NUL;
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928#endif
Bram Moolenaar26262f82019-09-04 20:59:15 +02002929 rettv->vval.v_string = res;
2930 res = NULL;
2931 }
2932
2933errret:
2934 if (infile != NULL)
2935 {
2936 mch_remove(infile);
2937 vim_free(infile);
2938 }
2939 if (res != NULL)
2940 vim_free(res);
2941 if (list != NULL)
2942 list_free(list);
2943}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002946 * "system()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 */
2948 void
Bram Moolenaar26262f82019-09-04 20:59:15 +02002949f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
Bram Moolenaar26262f82019-09-04 20:59:15 +02002951 get_cmd_output_as_rettv(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952}
2953
2954/*
Bram Moolenaar26262f82019-09-04 20:59:15 +02002955 * "systemlist()" function
2956 */
2957 void
2958f_systemlist(typval_T *argvars, typval_T *rettv)
2959{
2960 get_cmd_output_as_rettv(argvars, rettv, TRUE);
2961}
2962# endif // FEAT_EVAL
2963
2964#endif
2965
2966/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02002967 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 * Don't do this when still processing a command or a mapping.
2969 * Don't do this when inside a ":normal" command.
2970 */
2971 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002972goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
2974 return (p_im && stuff_empty() && typebuf_typed());
2975}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002976
2977/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02002978 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002979 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
2980 * - Remove any argument. E.g., "csh -f" -> "csh".
2981 * But don't allow a space in the path, so that this works:
2982 * "/usr/bin/csh --rcfile ~/.cshrc"
2983 * But don't do that for Windows, it's common to have a space in the path.
2984 */
2985 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002986get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002987{
2988 char_u *p;
2989
Bram Moolenaar4f974752019-02-17 17:44:42 +01002990#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02002991 p = gettail(p_sh);
2992 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
2993#else
2994 p = skiptowhite(p_sh);
2995 if (*p == NUL)
2996 {
2997 /* No white space, use the tail. */
2998 p = vim_strsave(gettail(p_sh));
2999 }
3000 else
3001 {
3002 char_u *p1, *p2;
3003
3004 /* Find the last path separator before the space. */
3005 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003006 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003007 if (vim_ispathsep(*p2))
3008 p1 = p2 + 1;
3009 p = vim_strnsave(p1, (int)(p - p1));
3010 }
3011#endif
3012 return p;
3013}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01003014
3015/*
3016 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
3017 * Also check for ":\\", which MS Internet Explorer accepts, return
3018 * URL_BACKSLASH.
3019 */
3020 int
3021path_is_url(char_u *p)
3022{
3023 if (STRNCMP(p, "://", (size_t)3) == 0)
3024 return URL_SLASH;
3025 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
3026 return URL_BACKSLASH;
3027 return 0;
3028}
3029
3030/*
3031 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
3032 * Return URL_BACKSLASH for "name:\\".
3033 * Return zero otherwise.
3034 */
3035 int
3036path_with_url(char_u *fname)
3037{
3038 char_u *p;
3039
3040 for (p = fname; isalpha(*p); ++p)
3041 ;
3042 return path_is_url(p);
3043}