blob: 75f4ab1ef589df13d441c88b6d4c1062253561c2 [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 Moolenaar4f974752019-02-17 17:44:42 +010017#if defined(FEAT_CMDL_COMPL) && defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +020018# include <lm.h>
19#endif
20
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static char_u *vim_version_dir(char_u *vimdir);
22static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010024#define URL_SLASH 1 /* path_is_url() has found "://" */
25#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
26
Bram Moolenaar24305862012-08-15 14:05:05 +020027/* All user names (for ~user completion as done by shell). */
28#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
29static garray_T ga_users;
30#endif
31
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
33 * Count the size (in window cells) of the indent in the current line.
34 */
35 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010036get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000037{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020038#ifdef FEAT_VARTABS
39 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
40 curbuf->b_p_vts_array, FALSE);
41#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020042 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044}
45
46/*
47 * Count the size (in window cells) of the indent in line "lnum".
48 */
49 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010050get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020052#ifdef FEAT_VARTABS
53 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
54 curbuf->b_p_vts_array, FALSE);
55#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020056 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000058}
59
60#if defined(FEAT_FOLDING) || defined(PROTO)
61/*
62 * Count the size (in window cells) of the indent in line "lnum" of buffer
63 * "buf".
64 */
65 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010066get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020068#ifdef FEAT_VARTABS
69 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
70 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
71#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020072 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000074}
75#endif
76
77/*
78 * count the size (in window cells) of the indent in line "ptr", with
79 * 'tabstop' at "ts"
80 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010082get_indent_str(
83 char_u *ptr,
84 int ts,
85 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87 int count = 0;
88
89 for ( ; *ptr; ++ptr)
90 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020091 if (*ptr == TAB)
92 {
93 if (!list || lcs_tab1) /* count a tab for what it is worth */
94 count += ts - (count % ts);
95 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020096 /* In list mode, when tab is not set, count screen char width
97 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020098 count += ptr2cells(ptr);
99 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 else if (*ptr == ' ')
101 ++count; /* count a space for one */
102 else
103 break;
104 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000105 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106}
107
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200108#ifdef FEAT_VARTABS
109/*
110 * Count the size (in window cells) of the indent in line "ptr", using
111 * variable tabstops.
112 * if "list" is TRUE, count only screen size for tabs.
113 */
114 int
115get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
116{
117 int count = 0;
118
119 for ( ; *ptr; ++ptr)
120 {
121 if (*ptr == TAB) /* count a tab for what it is worth */
122 {
123 if (!list || lcs_tab1)
124 count += tabstop_padding(count, ts, vts);
125 else
126 /* In list mode, when tab is not set, count screen char width
127 * for Tab, displays: ^I */
128 count += ptr2cells(ptr);
129 }
130 else if (*ptr == ' ')
131 ++count; /* count a space for one */
132 else
133 break;
134 }
135 return count;
136}
137#endif
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * Set the indent of the current line.
141 * Leaves the cursor on the first non-blank in the line.
142 * Caller must take care of undo.
143 * "flags":
144 * SIN_CHANGED: call changed_bytes() if the line was changed.
145 * SIN_INSERT: insert the indent in front of the line.
146 * SIN_UNDO: save line for undo before changing it.
147 * Returns TRUE if the line was changed.
148 */
149 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100150set_indent(
151 int size, /* measured in spaces */
152 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153{
154 char_u *p;
155 char_u *newline;
156 char_u *oldline;
157 char_u *s;
158 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000159 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 int line_len;
161 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000162 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200163#ifdef FEAT_VARTABS
164 int ind_col = 0;
165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000167 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000168 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000169 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170
171 /*
172 * First check if there is anything to do and compute the number of
173 * characters needed for the indent.
174 */
175 todo = size;
176 ind_len = 0;
177 p = oldline = ml_get_curline();
178
179 /* Calculate the buffer size for the new indent, and check to see if it
180 * isn't already set */
181
Bram Moolenaar5002c292007-07-24 13:26:15 +0000182 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
183 * 'preserveindent' are set count the number of characters at the
184 * beginning of the line to be copied */
185 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 {
187 /* If 'preserveindent' is set then reuse as much as possible of
188 * the existing indent structure for the new indent */
189 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
190 {
191 ind_done = 0;
192
193 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100194 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {
196 if (*p == TAB)
197 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200198#ifdef FEAT_VARTABS
199 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
200 curbuf->b_p_vts_array);
201#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 tab_pad = (int)curbuf->b_p_ts
203 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 /* stop if this tab will overshoot the target */
206 if (todo < tab_pad)
207 break;
208 todo -= tab_pad;
209 ++ind_len;
210 ind_done += tab_pad;
211 }
212 else
213 {
214 --todo;
215 ++ind_len;
216 ++ind_done;
217 }
218 ++p;
219 }
220
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200221#ifdef FEAT_VARTABS
222 /* These diverge from this point. */
223 ind_col = ind_done;
224#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000225 /* Set initial number of whitespace chars to copy if we are
226 * preserving indent but expandtab is set */
227 if (curbuf->b_p_et)
228 orig_char_len = ind_len;
229
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200231#ifdef FEAT_VARTABS
232 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
233 curbuf->b_p_vts_array);
234#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200236#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000237 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {
239 doit = TRUE;
240 todo -= tab_pad;
241 ++ind_len;
242 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200243#ifdef FEAT_VARTABS
244 ind_col += tab_pad;
245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 }
247 }
248
249 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200250#ifdef FEAT_VARTABS
251 for (;;)
252 {
253 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
254 curbuf->b_p_vts_array);
255 if (todo < tab_pad)
256 break;
257 if (*p != TAB)
258 doit = TRUE;
259 else
260 ++p;
261 todo -= tab_pad;
262 ++ind_len;
263 ind_col += tab_pad;
264 }
265#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 while (todo >= (int)curbuf->b_p_ts)
267 {
268 if (*p != TAB)
269 doit = TRUE;
270 else
271 ++p;
272 todo -= (int)curbuf->b_p_ts;
273 ++ind_len;
274 /* ind_done += (int)curbuf->b_p_ts; */
275 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 }
278 /* count spaces required for indent */
279 while (todo > 0)
280 {
281 if (*p != ' ')
282 doit = TRUE;
283 else
284 ++p;
285 --todo;
286 ++ind_len;
287 /* ++ind_done; */
288 }
289
290 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100291 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 return FALSE;
293
294 /* Allocate memory for the new line. */
295 if (flags & SIN_INSERT)
296 p = oldline;
297 else
298 p = skipwhite(p);
299 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000300
301 /* If 'preserveindent' and 'expandtab' are both set keep the original
302 * characters and allocate accordingly. We will fill the rest with spaces
303 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000304 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000305 {
306 newline = alloc(orig_char_len + size - ind_done + line_len);
307 if (newline == NULL)
308 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000309 todo = size - ind_done;
310 ind_len = orig_char_len + todo; /* Set total length of indent in
311 * characters, which may have been
312 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000313 p = oldline;
314 s = newline;
315 while (orig_char_len > 0)
316 {
317 *s++ = *p++;
318 orig_char_len--;
319 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000320
Bram Moolenaar5002c292007-07-24 13:26:15 +0000321 /* Skip over any additional white space (useful when newindent is less
322 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100323 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000324 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000325
Bram Moolenaar5002c292007-07-24 13:26:15 +0000326 }
327 else
328 {
329 todo = size;
330 newline = alloc(ind_len + line_len);
331 if (newline == NULL)
332 return FALSE;
333 s = newline;
334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
336 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 /* if 'expandtab' isn't set: use TABs */
338 if (!curbuf->b_p_et)
339 {
340 /* If 'preserveindent' is set then reuse as much as possible of
341 * the existing indent structure for the new indent */
342 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
343 {
344 p = oldline;
345 ind_done = 0;
346
Bram Moolenaar1c465442017-03-12 20:10:05 +0100347 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {
349 if (*p == TAB)
350 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200351#ifdef FEAT_VARTABS
352 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
353 curbuf->b_p_vts_array);
354#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 tab_pad = (int)curbuf->b_p_ts
356 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* stop if this tab will overshoot the target */
359 if (todo < tab_pad)
360 break;
361 todo -= tab_pad;
362 ind_done += tab_pad;
363 }
364 else
365 {
366 --todo;
367 ++ind_done;
368 }
369 *s++ = *p++;
370 }
371
372 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200373#ifdef FEAT_VARTABS
374 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
375 curbuf->b_p_vts_array);
376#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (todo >= tab_pad)
380 {
381 *s++ = TAB;
382 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200383#ifdef FEAT_VARTABS
384 ind_done += tab_pad;
385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
387
388 p = skipwhite(p);
389 }
390
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200391#ifdef FEAT_VARTABS
392 for (;;)
393 {
394 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
395 curbuf->b_p_vts_array);
396 if (todo < tab_pad)
397 break;
398 *s++ = TAB;
399 todo -= tab_pad;
400 ind_done += tab_pad;
401 }
402#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 while (todo >= (int)curbuf->b_p_ts)
404 {
405 *s++ = TAB;
406 todo -= (int)curbuf->b_p_ts;
407 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410 while (todo > 0)
411 {
412 *s++ = ' ';
413 --todo;
414 }
415 mch_memmove(s, p, (size_t)line_len);
416
Bram Moolenaar663bc892019-01-08 23:07:24 +0100417 // Replace the line (unless undo fails).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
419 {
420 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
421 if (flags & SIN_CHANGED)
422 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar663bc892019-01-08 23:07:24 +0100423
424 // Correct saved cursor position if it is in this line.
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200425 if (saved_cursor.lnum == curwin->w_cursor.lnum)
426 {
427 if (saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100428 // cursor was after the indent, adjust for the number of
429 // bytes added/removed
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200430 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
431 else if (saved_cursor.col >= (colnr_T)(s - newline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100432 // cursor was in the indent, and is now after it, put it back
433 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200434 saved_cursor.col = (colnr_T)(s - newline);
435 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100436#ifdef FEAT_TEXT_PROP
437 adjust_prop_columns(curwin->w_cursor.lnum, (colnr_T)(p - oldline),
438 ind_len - (colnr_T)(p - oldline));
439#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000440 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 }
442 else
443 vim_free(newline);
444
445 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000446 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447}
448
449/*
450 * Copy the indent from ptr to the current line (and fill to size)
451 * Leaves the cursor on the first non-blank in the line.
452 * Returns TRUE if the line was changed.
453 */
454 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100455copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457 char_u *p = NULL;
458 char_u *line = NULL;
459 char_u *s;
460 int todo;
461 int ind_len;
462 int line_len = 0;
463 int tab_pad;
464 int ind_done;
465 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200466#ifdef FEAT_VARTABS
467 int ind_col;
468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
470 /* Round 1: compute the number of characters needed for the indent
471 * Round 2: copy the characters. */
472 for (round = 1; round <= 2; ++round)
473 {
474 todo = size;
475 ind_len = 0;
476 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200477#ifdef FEAT_VARTABS
478 ind_col = 0;
479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 s = src;
481
482 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100483 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {
485 if (*s == TAB)
486 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200487#ifdef FEAT_VARTABS
488 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
489 curbuf->b_p_vts_array);
490#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 tab_pad = (int)curbuf->b_p_ts
492 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 /* Stop if this tab will overshoot the target */
495 if (todo < tab_pad)
496 break;
497 todo -= tab_pad;
498 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200499#ifdef FEAT_VARTABS
500 ind_col += tab_pad;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 else
504 {
505 --todo;
506 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200507#ifdef FEAT_VARTABS
508 ++ind_col;
509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 }
511 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000512 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 *p++ = *s;
514 ++s;
515 }
516
517 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200518#ifdef FEAT_VARTABS
519 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
520 curbuf->b_p_vts_array);
521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200523#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200524 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
526 todo -= tab_pad;
527 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200528#ifdef FEAT_VARTABS
529 ind_col += tab_pad;
530#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000531 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 *p++ = TAB;
533 }
534
535 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200536 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200538#ifdef FEAT_VARTABS
539 for (;;)
540 {
541 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
542 curbuf->b_p_vts_array);
543 if (todo < tab_pad)
544 break;
545 todo -= tab_pad;
546 ++ind_len;
547 ind_col += tab_pad;
548 if (p != NULL)
549 *p++ = TAB;
550 }
551#else
552 while (todo >= (int)curbuf->b_p_ts)
553 {
554 todo -= (int)curbuf->b_p_ts;
555 ++ind_len;
556 if (p != NULL)
557 *p++ = TAB;
558 }
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 }
561
562 /* Count/add spaces required for indent */
563 while (todo > 0)
564 {
565 --todo;
566 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000567 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 *p++ = ' ';
569 }
570
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000571 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
573 /* Allocate memory for the result: the copied indent, new indent
574 * and the rest of the line. */
575 line_len = (int)STRLEN(ml_get_curline()) + 1;
576 line = alloc(ind_len + line_len);
577 if (line == NULL)
578 return FALSE;
579 p = line;
580 }
581 }
582
583 /* Append the original line */
584 mch_memmove(p, ml_get_curline(), (size_t)line_len);
585
586 /* Replace the line */
587 ml_replace(curwin->w_cursor.lnum, line, FALSE);
588
589 /* Put the cursor after the indent. */
590 curwin->w_cursor.col = ind_len;
591 return TRUE;
592}
593
594/*
595 * Return the indent of the current line after a number. Return -1 if no
596 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000597 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 */
599 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100600get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 colnr_T col;
603 pos_T pos;
604
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200605 regmatch_T regmatch;
606 int lead_len = 0; /* length of comment leader */
607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (lnum > curbuf->b_ml.ml_line_count)
609 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000610 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200611
612#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200613 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
614 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200615 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000616#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200617 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
618 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200619 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200620 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200621
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200622 /* vim_regexec() expects a pointer to a line. This lets us
623 * start matching for the flp beyond any comment leader... */
624 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200625 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200626 pos.lnum = lnum;
627 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200628 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200629 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200630 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200631 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000632
633 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 getvcol(curwin, &pos, &col, NULL, NULL);
636 return (int)col;
637}
638
Bram Moolenaar597a4222014-06-25 14:39:50 +0200639#if defined(FEAT_LINEBREAK) || defined(PROTO)
640/*
641 * Return appropriate space number for breakindent, taking influencing
642 * parameters into account. Window must be specified, since it is not
643 * necessarily always the current one.
644 */
645 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100646get_breakindent_win(
647 win_T *wp,
648 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200649{
650 static int prev_indent = 0; /* cached indent value */
651 static long prev_ts = 0L; /* cached tabstop value */
652 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100653 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200654#ifdef FEAT_VARTABS
655 static int *prev_vts = NULL; /* cached vartabs values */
656#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200657 int bri = 0;
658 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200659 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200660 - ((wp->w_p_nu || wp->w_p_rnu)
661 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
662 ? number_width(wp) + 1 : 0);
663
664 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200665 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200666 || prev_tick != CHANGEDTICK(wp->w_buffer)
667#ifdef FEAT_VARTABS
668 || prev_vts != wp->w_buffer->b_p_vts_array
669#endif
670 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200671 {
672 prev_line = line;
673 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100674 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200675#ifdef FEAT_VARTABS
676 prev_vts = wp->w_buffer->b_p_vts_array;
677 prev_indent = get_indent_str_vtab(line,
678 (int)wp->w_buffer->b_p_ts,
679 wp->w_buffer->b_p_vts_array, wp->w_p_list);
680#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200681 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200682 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200683#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200684 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200685 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200686
687 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200688 if (wp->w_p_brisbr)
689 bri -= vim_strsize(p_sbr);
690
691 /* Add offset for number column, if 'n' is in 'cpoptions' */
692 bri += win_col_off2(wp);
693
694 /* never indent past left window margin */
695 if (bri < 0)
696 bri = 0;
697 /* always leave at least bri_min characters on the left,
698 * if text width is sufficient */
699 else if (bri > eff_wwidth - wp->w_p_brimin)
700 bri = (eff_wwidth - wp->w_p_brimin < 0)
701 ? 0 : eff_wwidth - wp->w_p_brimin;
702
703 return bri;
704}
705#endif
706
707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708/*
709 * open_line: Add a new line below or above the current line.
710 *
711 * For VREPLACE mode, we only add a new line when we get to the end of the
712 * file, otherwise we just start replacing the next line.
713 *
714 * Caller must take care of undo. Since VREPLACE may affect any number of
715 * lines however, it may call u_save_cursor() again when starting to change a
716 * new line.
717 * "flags": OPENLINE_DELSPACES delete spaces after cursor
718 * OPENLINE_DO_COM format comments
719 * OPENLINE_KEEPTRAIL keep trailing spaces
720 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200721 * OPENLINE_COM_LIST format comments with list or 2nd line indent
722 *
723 * "second_line_indent": indent for after ^^D in Insert mode or if flag
724 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200726 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
728 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100729open_line(
730 int dir, /* FORWARD or BACKWARD */
731 int flags,
732 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 char_u *saved_line; /* copy of the original line */
735 char_u *next_line = NULL; /* copy of the next line */
736 char_u *p_extra = NULL; /* what goes to next line */
737 int less_cols = 0; /* less columns for mark in new line */
738 int less_cols_off = 0; /* columns to skip for mark adjust */
739 pos_T old_cursor; /* old cursor position */
740 int newcol = 0; /* new cursor column */
741 int newindent = 0; /* auto-indent of the new line */
742 int n;
743 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200744 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#ifdef FEAT_COMMENTS
746 int extra_len = 0; /* length of p_extra string */
747 int lead_len; /* length of comment leader */
748 char_u *lead_flags; /* position in 'comments' for comment leader */
749 char_u *leader = NULL; /* copy of comment leader */
750#endif
751 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 int saved_char = NUL; /* init for GCC */
754#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
755 pos_T *pos;
756#endif
757#ifdef FEAT_SMARTINDENT
758 int do_si = (!p_paste && curbuf->b_p_si
759# ifdef FEAT_CINDENT
760 && !curbuf->b_p_cin
761# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200762# ifdef FEAT_EVAL
763 && *curbuf->b_p_inde == NUL
764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 );
766 int no_si = FALSE; /* reset did_si afterwards */
767 int first_char = NUL; /* init for GCC */
768#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200769#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 int vreplace_mode;
771#endif
772 int did_append; /* appended a new line */
773 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
774
775 /*
776 * make a copy of the current line so we can mess with it
777 */
778 saved_line = vim_strsave(ml_get_curline());
779 if (saved_line == NULL) /* out of memory! */
780 return FALSE;
781
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (State & VREPLACE_FLAG)
783 {
784 /*
785 * With VREPLACE we make a copy of the next line, which we will be
786 * starting to replace. First make the new line empty and let vim play
787 * with the indenting and comment leader to its heart's content. Then
788 * we grab what it ended up putting on the new line, put back the
789 * original line, and call ins_char() to put each new character onto
790 * the line, replacing what was there before and pushing the right
791 * stuff onto the replace stack. -- webb.
792 */
793 if (curwin->w_cursor.lnum < orig_line_count)
794 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
795 else
796 next_line = vim_strsave((char_u *)"");
797 if (next_line == NULL) /* out of memory! */
798 goto theend;
799
800 /*
801 * In VREPLACE mode, a NL replaces the rest of the line, and starts
802 * replacing the next line, so push all of the characters left on the
803 * line onto the replace stack. We'll push any other characters that
804 * might be replaced at the start of the next line (due to autoindent
805 * etc) a bit later.
806 */
807 replace_push(NUL); /* Call twice because BS over NL expects it */
808 replace_push(NUL);
809 p = saved_line + curwin->w_cursor.col;
810 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000811 {
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000812 if (has_mbyte)
813 p += replace_push_mb(p);
814 else
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000815 replace_push(*p++);
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 saved_line[curwin->w_cursor.col] = NUL;
818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200820 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {
822 p_extra = saved_line + curwin->w_cursor.col;
823#ifdef FEAT_SMARTINDENT
824 if (do_si) /* need first char after new line break */
825 {
826 p = skipwhite(p_extra);
827 first_char = *p;
828 }
829#endif
830#ifdef FEAT_COMMENTS
831 extra_len = (int)STRLEN(p_extra);
832#endif
833 saved_char = *p_extra;
834 *p_extra = NUL;
835 }
836
837 u_clearline(); /* cannot do "U" command when adding lines */
838#ifdef FEAT_SMARTINDENT
839 did_si = FALSE;
840#endif
841 ai_col = 0;
842
843 /*
844 * If we just did an auto-indent, then we didn't type anything on
845 * the prior line, and it should be truncated. Do this even if 'ai' is not
846 * set because automatically inserting a comment leader also sets did_ai.
847 */
848 if (dir == FORWARD && did_ai)
849 trunc_line = TRUE;
850
851 /*
852 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
853 * indent to use for the new line.
854 */
855 if (curbuf->b_p_ai
856#ifdef FEAT_SMARTINDENT
857 || do_si
858#endif
859 )
860 {
861 /*
862 * count white space on current line
863 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200864#ifdef FEAT_VARTABS
865 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
866 curbuf->b_p_vts_array, FALSE);
867#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200868 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200869#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200870 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
871 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873#ifdef FEAT_SMARTINDENT
874 /*
875 * Do smart indenting.
876 * In insert/replace mode (only when dir == FORWARD)
877 * we may move some text to the next line. If it starts with '{'
878 * don't add an indent. Fixes inserting a NL before '{' in line
879 * "if (condition) {"
880 */
881 if (!trunc_line && do_si && *saved_line != NUL
882 && (p_extra == NULL || first_char != '{'))
883 {
884 char_u *ptr;
885 char_u last_char;
886
887 old_cursor = curwin->w_cursor;
888 ptr = saved_line;
889# ifdef FEAT_COMMENTS
890 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200891 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 else
893 lead_len = 0;
894# endif
895 if (dir == FORWARD)
896 {
897 /*
898 * Skip preprocessor directives, unless they are
899 * recognised as comments.
900 */
901 if (
902# ifdef FEAT_COMMENTS
903 lead_len == 0 &&
904# endif
905 ptr[0] == '#')
906 {
907 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
908 ptr = ml_get(--curwin->w_cursor.lnum);
909 newindent = get_indent();
910 }
911# ifdef FEAT_COMMENTS
912 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200913 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 else
915 lead_len = 0;
916 if (lead_len > 0)
917 {
918 /*
919 * This case gets the following right:
920 * \*
921 * * A comment (read '\' as '/').
922 * *\
923 * #define IN_THE_WAY
924 * This should line up here;
925 */
926 p = skipwhite(ptr);
927 if (p[0] == '/' && p[1] == '*')
928 p++;
929 if (p[0] == '*')
930 {
931 for (p++; *p; p++)
932 {
933 if (p[0] == '/' && p[-1] == '*')
934 {
935 /*
936 * End of C comment, indent should line up
937 * with the line containing the start of
938 * the comment
939 */
940 curwin->w_cursor.col = (colnr_T)(p - ptr);
941 if ((pos = findmatch(NULL, NUL)) != NULL)
942 {
943 curwin->w_cursor.lnum = pos->lnum;
944 newindent = get_indent();
945 }
946 }
947 }
948 }
949 }
950 else /* Not a comment line */
951# endif
952 {
953 /* Find last non-blank in line */
954 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100955 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 --p;
957 last_char = *p;
958
959 /*
960 * find the character just before the '{' or ';'
961 */
962 if (last_char == '{' || last_char == ';')
963 {
964 if (p > ptr)
965 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100966 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 --p;
968 }
969 /*
970 * Try to catch lines that are split over multiple
971 * lines. eg:
972 * if (condition &&
973 * condition) {
974 * Should line up here!
975 * }
976 */
977 if (*p == ')')
978 {
979 curwin->w_cursor.col = (colnr_T)(p - ptr);
980 if ((pos = findmatch(NULL, '(')) != NULL)
981 {
982 curwin->w_cursor.lnum = pos->lnum;
983 newindent = get_indent();
984 ptr = ml_get_curline();
985 }
986 }
987 /*
988 * If last character is '{' do indent, without
989 * checking for "if" and the like.
990 */
991 if (last_char == '{')
992 {
993 did_si = TRUE; /* do indent */
994 no_si = TRUE; /* don't delete it when '{' typed */
995 }
996 /*
997 * Look for "if" and the like, use 'cinwords'.
998 * Don't do this if the previous line ended in ';' or
999 * '}'.
1000 */
1001 else if (last_char != ';' && last_char != '}'
1002 && cin_is_cinword(ptr))
1003 did_si = TRUE;
1004 }
1005 }
1006 else /* dir == BACKWARD */
1007 {
1008 /*
1009 * Skip preprocessor directives, unless they are
1010 * recognised as comments.
1011 */
1012 if (
1013# ifdef FEAT_COMMENTS
1014 lead_len == 0 &&
1015# endif
1016 ptr[0] == '#')
1017 {
1018 int was_backslashed = FALSE;
1019
1020 while ((ptr[0] == '#' || was_backslashed) &&
1021 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1022 {
1023 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1024 was_backslashed = TRUE;
1025 else
1026 was_backslashed = FALSE;
1027 ptr = ml_get(++curwin->w_cursor.lnum);
1028 }
1029 if (was_backslashed)
1030 newindent = 0; /* Got to end of file */
1031 else
1032 newindent = get_indent();
1033 }
1034 p = skipwhite(ptr);
1035 if (*p == '}') /* if line starts with '}': do indent */
1036 did_si = TRUE;
1037 else /* can delete indent when '{' typed */
1038 can_si_back = TRUE;
1039 }
1040 curwin->w_cursor = old_cursor;
1041 }
1042 if (do_si)
1043 can_si = TRUE;
1044#endif /* FEAT_SMARTINDENT */
1045
1046 did_ai = TRUE;
1047 }
1048
1049#ifdef FEAT_COMMENTS
1050 /*
1051 * Find out if the current line starts with a comment leader.
1052 * This may then be inserted in front of the new line.
1053 */
1054 end_comment_pending = NUL;
1055 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001056 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 else
1058 lead_len = 0;
1059 if (lead_len > 0)
1060 {
1061 char_u *lead_repl = NULL; /* replaces comment leader */
1062 int lead_repl_len = 0; /* length of *lead_repl */
1063 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1064 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1065 char_u *comment_end = NULL; /* where lead_end has been found */
1066 int extra_space = FALSE; /* append extra space */
1067 int current_flag;
1068 int require_blank = FALSE; /* requires blank after middle */
1069 char_u *p2;
1070
1071 /*
1072 * If the comment leader has the start, middle or end flag, it may not
1073 * be used or may be replaced with the middle leader.
1074 */
1075 for (p = lead_flags; *p && *p != ':'; ++p)
1076 {
1077 if (*p == COM_BLANK)
1078 {
1079 require_blank = TRUE;
1080 continue;
1081 }
1082 if (*p == COM_START || *p == COM_MIDDLE)
1083 {
1084 current_flag = *p;
1085 if (*p == COM_START)
1086 {
1087 /*
1088 * Doing "O" on a start of comment does not insert leader.
1089 */
1090 if (dir == BACKWARD)
1091 {
1092 lead_len = 0;
1093 break;
1094 }
1095
1096 /* find start of middle part */
1097 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1098 require_blank = FALSE;
1099 }
1100
1101 /*
1102 * Isolate the strings of the middle and end leader.
1103 */
1104 while (*p && p[-1] != ':') /* find end of middle flags */
1105 {
1106 if (*p == COM_BLANK)
1107 require_blank = TRUE;
1108 ++p;
1109 }
1110 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1111
1112 while (*p && p[-1] != ':') /* find end of end flags */
1113 {
1114 /* Check whether we allow automatic ending of comments */
1115 if (*p == COM_AUTO_END)
1116 end_comment_pending = -1; /* means we want to set it */
1117 ++p;
1118 }
1119 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1120
1121 if (end_comment_pending == -1) /* we can set it now */
1122 end_comment_pending = lead_end[n - 1];
1123
1124 /*
1125 * If the end of the comment is in the same line, don't use
1126 * the comment leader.
1127 */
1128 if (dir == FORWARD)
1129 {
1130 for (p = saved_line + lead_len; *p; ++p)
1131 if (STRNCMP(p, lead_end, n) == 0)
1132 {
1133 comment_end = p;
1134 lead_len = 0;
1135 break;
1136 }
1137 }
1138
1139 /*
1140 * Doing "o" on a start of comment inserts the middle leader.
1141 */
1142 if (lead_len > 0)
1143 {
1144 if (current_flag == COM_START)
1145 {
1146 lead_repl = lead_middle;
1147 lead_repl_len = (int)STRLEN(lead_middle);
1148 }
1149
1150 /*
1151 * If we have hit RETURN immediately after the start
1152 * comment leader, then put a space after the middle
1153 * comment leader on the next line.
1154 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001155 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && ((p_extra != NULL
1157 && (int)curwin->w_cursor.col == lead_len)
1158 || (p_extra == NULL
1159 && saved_line[lead_len] == NUL)
1160 || require_blank))
1161 extra_space = TRUE;
1162 }
1163 break;
1164 }
1165 if (*p == COM_END)
1166 {
1167 /*
1168 * Doing "o" on the end of a comment does not insert leader.
1169 * Remember where the end is, might want to use it to find the
1170 * start (for C-comments).
1171 */
1172 if (dir == FORWARD)
1173 {
1174 comment_end = skipwhite(saved_line);
1175 lead_len = 0;
1176 break;
1177 }
1178
1179 /*
1180 * Doing "O" on the end of a comment inserts the middle leader.
1181 * Find the string for the middle leader, searching backwards.
1182 */
1183 while (p > curbuf->b_p_com && *p != ',')
1184 --p;
1185 for (lead_repl = p; lead_repl > curbuf->b_p_com
1186 && lead_repl[-1] != ':'; --lead_repl)
1187 ;
1188 lead_repl_len = (int)(p - lead_repl);
1189
1190 /* We can probably always add an extra space when doing "O" on
1191 * the comment-end */
1192 extra_space = TRUE;
1193
1194 /* Check whether we allow automatic ending of comments */
1195 for (p2 = p; *p2 && *p2 != ':'; p2++)
1196 {
1197 if (*p2 == COM_AUTO_END)
1198 end_comment_pending = -1; /* means we want to set it */
1199 }
1200 if (end_comment_pending == -1)
1201 {
1202 /* Find last character in end-comment string */
1203 while (*p2 && *p2 != ',')
1204 p2++;
1205 end_comment_pending = p2[-1];
1206 }
1207 break;
1208 }
1209 if (*p == COM_FIRST)
1210 {
1211 /*
1212 * Comment leader for first line only: Don't repeat leader
1213 * when using "O", blank out leader when using "o".
1214 */
1215 if (dir == BACKWARD)
1216 lead_len = 0;
1217 else
1218 {
1219 lead_repl = (char_u *)"";
1220 lead_repl_len = 0;
1221 }
1222 break;
1223 }
1224 }
1225 if (lead_len)
1226 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001227 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001228 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001229 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 allocated = leader; /* remember to free it later */
1231
1232 if (leader == NULL)
1233 lead_len = 0;
1234 else
1235 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001236 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238 /*
1239 * Replace leader with lead_repl, right or left adjusted
1240 */
1241 if (lead_repl != NULL)
1242 {
1243 int c = 0;
1244 int off = 0;
1245
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001246 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001249 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 else if (VIM_ISDIGIT(*p) || *p == '-')
1251 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001252 else
1253 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 }
1255 if (c == COM_RIGHT) /* right adjusted leader */
1256 {
1257 /* find last non-white in the leader to line up with */
1258 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001259 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001262
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001263 /* Compute the length of the replaced characters in
1264 * screen characters, not bytes. */
1265 {
1266 int repl_size = vim_strnsize(lead_repl,
1267 lead_repl_len);
1268 int old_size = 0;
1269 char_u *endp = p;
1270 int l;
1271
1272 while (old_size < repl_size && p > leader)
1273 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001274 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001275 old_size += ptr2cells(p);
1276 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001277 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001278 if (l != 0)
1279 mch_memmove(endp + l, endp,
1280 (size_t)((leader + lead_len) - endp));
1281 lead_len += l;
1282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1284 if (p + lead_repl_len > leader + lead_len)
1285 p[lead_repl_len] = NUL;
1286
1287 /* blank-out any other chars from the old leader. */
1288 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001289 {
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001290 int l = mb_head_off(leader, p);
1291
1292 if (l > 1)
1293 {
1294 p -= l;
1295 if (ptr2cells(p) > 1)
1296 {
1297 p[1] = ' ';
1298 --l;
1299 }
1300 mch_memmove(p + 1, p + l + 1,
1301 (size_t)((leader + lead_len) - (p + l + 1)));
1302 lead_len -= l;
1303 *p = ' ';
1304 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001305 else if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 else /* left adjusted leader */
1310 {
1311 p = skipwhite(leader);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001312
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001313 /* Compute the length of the replaced characters in
1314 * screen characters, not bytes. Move the part that is
1315 * not to be overwritten. */
1316 {
1317 int repl_size = vim_strnsize(lead_repl,
1318 lead_repl_len);
1319 int i;
1320 int l;
1321
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001322 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001323 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001324 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001325 if (vim_strnsize(p, i + l) > repl_size)
1326 break;
1327 }
1328 if (i != lead_repl_len)
1329 {
1330 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001331 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001332 lead_len += lead_repl_len - i;
1333 }
1334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1336
1337 /* Replace any remaining non-white chars in the old
1338 * leader by spaces. Keep Tabs, the indent must
1339 * remain the same. */
1340 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001341 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
1343 /* Don't put a space before a TAB. */
1344 if (p + 1 < leader + lead_len && p[1] == TAB)
1345 {
1346 --lead_len;
1347 mch_memmove(p, p + 1,
1348 (leader + lead_len) - p);
1349 }
1350 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001351 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001352 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001353
1354 if (l > 1)
1355 {
1356 if (ptr2cells(p) > 1)
1357 {
1358 /* Replace a double-wide char with
1359 * two spaces */
1360 --l;
1361 *p++ = ' ';
1362 }
1363 mch_memmove(p + 1, p + l,
1364 (leader + lead_len) - p);
1365 lead_len -= l - 1;
1366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370 *p = NUL;
1371 }
1372
1373 /* Recompute the indent, it may have changed. */
1374 if (curbuf->b_p_ai
1375#ifdef FEAT_SMARTINDENT
1376 || do_si
1377#endif
1378 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001379#ifdef FEAT_VARTABS
1380 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1381 curbuf->b_p_vts_array, FALSE);
1382#else
1383 newindent = get_indent_str(leader,
1384 (int)curbuf->b_p_ts, FALSE);
1385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 /* Add the indent offset */
1388 if (newindent + off < 0)
1389 {
1390 off = -newindent;
1391 newindent = 0;
1392 }
1393 else
1394 newindent += off;
1395
1396 /* Correct trailing spaces for the shift, so that
1397 * alignment remains equal. */
1398 while (off > 0 && lead_len > 0
1399 && leader[lead_len - 1] == ' ')
1400 {
1401 /* Don't do it when there is a tab before the space */
1402 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1403 break;
1404 --lead_len;
1405 --off;
1406 }
1407
1408 /* If the leader ends in white space, don't add an
1409 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001410 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 extra_space = FALSE;
1412 leader[lead_len] = NUL;
1413 }
1414
1415 if (extra_space)
1416 {
1417 leader[lead_len++] = ' ';
1418 leader[lead_len] = NUL;
1419 }
1420
1421 newcol = lead_len;
1422
1423 /*
1424 * if a new indent will be set below, remove the indent that
1425 * is in the comment leader
1426 */
1427 if (newindent
1428#ifdef FEAT_SMARTINDENT
1429 || did_si
1430#endif
1431 )
1432 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001433 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
1435 --lead_len;
1436 --newcol;
1437 ++leader;
1438 }
1439 }
1440
1441 }
1442#ifdef FEAT_SMARTINDENT
1443 did_si = can_si = FALSE;
1444#endif
1445 }
1446 else if (comment_end != NULL)
1447 {
1448 /*
1449 * We have finished a comment, so we don't use the leader.
1450 * If this was a C-comment and 'ai' or 'si' is set do a normal
1451 * indent to align with the line containing the start of the
1452 * comment.
1453 */
1454 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1455 (curbuf->b_p_ai
1456#ifdef FEAT_SMARTINDENT
1457 || do_si
1458#endif
1459 ))
1460 {
1461 old_cursor = curwin->w_cursor;
1462 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1463 if ((pos = findmatch(NULL, NUL)) != NULL)
1464 {
1465 curwin->w_cursor.lnum = pos->lnum;
1466 newindent = get_indent();
1467 }
1468 curwin->w_cursor = old_cursor;
1469 }
1470 }
1471 }
1472#endif
1473
1474 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1475 if (p_extra != NULL)
1476 {
1477 *p_extra = saved_char; /* restore char that NUL replaced */
1478
1479 /*
1480 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1481 * non-blank.
1482 *
1483 * When in REPLACE mode, put the deleted blanks on the replace stack,
1484 * preceded by a NUL, so they can be put back when a BS is entered.
1485 */
1486 if (REPLACE_NORMAL(State))
1487 replace_push(NUL); /* end of extra blanks */
1488 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1489 {
1490 while ((*p_extra == ' ' || *p_extra == '\t')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 && (!enc_utf8
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001492 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
1494 if (REPLACE_NORMAL(State))
1495 replace_push(*p_extra);
1496 ++p_extra;
1497 ++less_cols_off;
1498 }
1499 }
1500 if (*p_extra != NUL)
1501 did_ai = FALSE; /* append some text, don't truncate now */
1502
1503 /* columns for marks adjusted for removed columns */
1504 less_cols = (int)(p_extra - saved_line);
1505 }
1506
1507 if (p_extra == NULL)
1508 p_extra = (char_u *)""; /* append empty line */
1509
1510#ifdef FEAT_COMMENTS
1511 /* concatenate leader and p_extra, if there is a leader */
1512 if (lead_len)
1513 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001514 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1515 {
1516 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001517 int padding = second_line_indent
1518 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001519
1520 /* Here whitespace is inserted after the comment char.
1521 * Below, set_indent(newindent, SIN_INSERT) will insert the
1522 * whitespace needed before the comment char. */
1523 for (i = 0; i < padding; i++)
1524 {
1525 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001526 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001527 newcol++;
1528 }
1529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 STRCAT(leader, p_extra);
1531 p_extra = leader;
1532 did_ai = TRUE; /* So truncating blanks works with comments */
1533 less_cols -= lead_len;
1534 }
1535 else
1536 end_comment_pending = NUL; /* turns out there was no leader */
1537#endif
1538
1539 old_cursor = curwin->w_cursor;
1540 if (dir == BACKWARD)
1541 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1545 == FAIL)
1546 goto theend;
1547 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001548 * with markers.
1549 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001550 * be marks there. But still needed in diff mode. */
1551 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1552#ifdef FEAT_DIFF
1553 || curwin->w_p_diff
1554#endif
1555 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001556 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 did_append = TRUE;
1558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 else
1560 {
1561 /*
1562 * In VREPLACE mode we are starting to replace the next line.
1563 */
1564 curwin->w_cursor.lnum++;
1565 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1566 {
1567 /* In case we NL to a new line, BS to the previous one, and NL
1568 * again, we don't want to save the new line for undo twice.
1569 */
1570 (void)u_save_cursor(); /* errors are ignored! */
1571 vr_lines_changed++;
1572 }
1573 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1574 changed_bytes(curwin->w_cursor.lnum, 0);
1575 curwin->w_cursor.lnum--;
1576 did_append = FALSE;
1577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 if (newindent
1580#ifdef FEAT_SMARTINDENT
1581 || did_si
1582#endif
1583 )
1584 {
1585 ++curwin->w_cursor.lnum;
1586#ifdef FEAT_SMARTINDENT
1587 if (did_si)
1588 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001589 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001592 newindent -= newindent % sw;
1593 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001596 /* Copy the indent */
1597 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 (void)copy_indent(newindent, saved_line);
1600
1601 /*
1602 * Set the 'preserveindent' option so that any further screwing
1603 * with the line doesn't entirely destroy our efforts to preserve
1604 * it. It gets restored at the function end.
1605 */
1606 curbuf->b_p_pi = TRUE;
1607 }
1608 else
1609 (void)set_indent(newindent, SIN_INSERT);
1610 less_cols -= curwin->w_cursor.col;
1611
1612 ai_col = curwin->w_cursor.col;
1613
1614 /*
1615 * In REPLACE mode, for each character in the new indent, there must
1616 * be a NUL on the replace stack, for when it is deleted with BS
1617 */
1618 if (REPLACE_NORMAL(State))
1619 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1620 replace_push(NUL);
1621 newcol += curwin->w_cursor.col;
1622#ifdef FEAT_SMARTINDENT
1623 if (no_si)
1624 did_si = FALSE;
1625#endif
1626 }
1627
1628#ifdef FEAT_COMMENTS
1629 /*
1630 * In REPLACE mode, for each character in the extra leader, there must be
1631 * a NUL on the replace stack, for when it is deleted with BS.
1632 */
1633 if (REPLACE_NORMAL(State))
1634 while (lead_len-- > 0)
1635 replace_push(NUL);
1636#endif
1637
1638 curwin->w_cursor = old_cursor;
1639
1640 if (dir == FORWARD)
1641 {
1642 if (trunc_line || (State & INSERT))
1643 {
1644 /* truncate current line at cursor */
1645 saved_line[curwin->w_cursor.col] = NUL;
1646 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1647 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1648 truncate_spaces(saved_line);
1649 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1650 saved_line = NULL;
1651 if (did_append)
1652 {
1653 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1654 curwin->w_cursor.lnum + 1, 1L);
1655 did_append = FALSE;
1656
1657 /* Move marks after the line break to the new line. */
1658 if (flags & OPENLINE_MARKFIX)
1659 mark_col_adjust(curwin->w_cursor.lnum,
1660 curwin->w_cursor.col + less_cols_off,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001661 1L, (long)-less_cols, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1665 }
1666
1667 /*
1668 * Put the cursor on the new line. Careful: the scrollup() above may
1669 * have moved w_cursor, we must use old_cursor.
1670 */
1671 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1672 }
1673 if (did_append)
1674 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1675
1676 curwin->w_cursor.col = newcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001679#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 /*
1681 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1682 * fixthisline() from doing it (via change_indent()) by telling it we're in
1683 * normal INSERT mode.
1684 */
1685 if (State & VREPLACE_FLAG)
1686 {
1687 vreplace_mode = State; /* So we know to put things right later */
1688 State = INSERT;
1689 }
1690 else
1691 vreplace_mode = 0;
1692#endif
1693#ifdef FEAT_LISP
1694 /*
1695 * May do lisp indenting.
1696 */
1697 if (!p_paste
1698# ifdef FEAT_COMMENTS
1699 && leader == NULL
1700# endif
1701 && curbuf->b_p_lisp
1702 && curbuf->b_p_ai)
1703 {
1704 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001705 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
1707#endif
1708#ifdef FEAT_CINDENT
1709 /*
1710 * May do indenting after opening a new line.
1711 */
1712 if (!p_paste
1713 && (curbuf->b_p_cin
1714# ifdef FEAT_EVAL
1715 || *curbuf->b_p_inde != NUL
1716# endif
1717 )
1718 && in_cinkeys(dir == FORWARD
1719 ? KEY_OPEN_FORW
1720 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1721 {
1722 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001723 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
1725#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001726#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (vreplace_mode != 0)
1728 State = vreplace_mode;
1729#endif
1730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 /*
1732 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1733 * original line, and inserts the new stuff char by char, pushing old stuff
1734 * onto the replace stack (via ins_char()).
1735 */
1736 if (State & VREPLACE_FLAG)
1737 {
1738 /* Put new line in p_extra */
1739 p_extra = vim_strsave(ml_get_curline());
1740 if (p_extra == NULL)
1741 goto theend;
1742
1743 /* Put back original line */
1744 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1745
1746 /* Insert new stuff into line again */
1747 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ins_bytes(p_extra); /* will call changed_bytes() */
1750 vim_free(p_extra);
1751 next_line = NULL;
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001754 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755theend:
1756 curbuf->b_p_pi = saved_pi;
1757 vim_free(saved_line);
1758 vim_free(next_line);
1759 vim_free(allocated);
1760 return retval;
1761}
1762
1763#if defined(FEAT_COMMENTS) || defined(PROTO)
1764/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001765 * get_leader_len() returns the length in bytes of the prefix of the given
1766 * string which introduces a comment. If this string is not a comment then
1767 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * When "flags" is not NULL, it is set to point to the flags of the recognized
1769 * comment leader.
1770 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001771 * If "include_space" is set, include trailing whitespace while calculating the
1772 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 */
1774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775get_leader_len(
1776 char_u *line,
1777 char_u **flags,
1778 int backward,
1779 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001782 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int got_com = FALSE;
1784 int found_one;
1785 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1786 char_u *string; /* pointer to comment string */
1787 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001788 int middle_match_len = 0;
1789 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001790 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar81340392012-06-06 16:12:59 +02001792 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001793 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 ++i;
1795
1796 /*
1797 * Repeat to match several nested comment strings.
1798 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001799 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
1801 /*
1802 * scan through the 'comments' option for a match
1803 */
1804 found_one = FALSE;
1805 for (list = curbuf->b_p_com; *list; )
1806 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001807 /* Get one option part into part_buf[]. Advance "list" to next
1808 * one. Put "string" at start of string. */
1809 if (!got_com && flags != NULL)
1810 *flags = list; /* remember where flags started */
1811 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1813 string = vim_strchr(part_buf, ':');
1814 if (string == NULL) /* missing ':', ignore this part */
1815 continue;
1816 *string++ = NUL; /* isolate flags from string */
1817
Bram Moolenaara4271d52011-05-10 13:38:27 +02001818 /* If we found a middle match previously, use that match when this
1819 * is not a middle or end. */
1820 if (middle_match_len != 0
1821 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1822 && vim_strchr(part_buf, COM_END) == NULL)
1823 break;
1824
1825 /* When we already found a nested comment, only accept further
1826 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1828 continue;
1829
Bram Moolenaara4271d52011-05-10 13:38:27 +02001830 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1832 continue;
1833
Bram Moolenaara4271d52011-05-10 13:38:27 +02001834 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 * When string starts with white space, must have some white space
1836 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001837 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001838 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001840 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001841 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001842 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 ++string;
1844 }
1845 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1846 ;
1847 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001848 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaara4271d52011-05-10 13:38:27 +02001850 /* When 'b' flag used, there must be white space or an
1851 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001853 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 continue;
1855
Bram Moolenaara4271d52011-05-10 13:38:27 +02001856 /* We have found a match, stop searching unless this is a middle
1857 * comment. The middle comment can be a substring of the end
1858 * comment in which case it's better to return the length of the
1859 * end comment and its flags. Thus we keep searching with middle
1860 * and end matches and use an end match if it matches better. */
1861 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1862 {
1863 if (middle_match_len == 0)
1864 {
1865 middle_match_len = j;
1866 saved_flags = prev_list;
1867 }
1868 continue;
1869 }
1870 if (middle_match_len != 0 && j > middle_match_len)
1871 /* Use this match instead of the middle match, since it's a
1872 * longer thus better match. */
1873 middle_match_len = 0;
1874
1875 if (middle_match_len == 0)
1876 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 found_one = TRUE;
1878 break;
1879 }
1880
Bram Moolenaara4271d52011-05-10 13:38:27 +02001881 if (middle_match_len != 0)
1882 {
1883 /* Use the previously found middle match after failing to find a
1884 * match with an end. */
1885 if (!got_com && flags != NULL)
1886 *flags = saved_flags;
1887 i += middle_match_len;
1888 found_one = TRUE;
1889 }
1890
1891 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (!found_one)
1893 break;
1894
Bram Moolenaar81340392012-06-06 16:12:59 +02001895 result = i;
1896
Bram Moolenaara4271d52011-05-10 13:38:27 +02001897 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001898 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 ++i;
1900
Bram Moolenaar81340392012-06-06 16:12:59 +02001901 if (include_space)
1902 result = i;
1903
Bram Moolenaara4271d52011-05-10 13:38:27 +02001904 /* If this comment doesn't nest, stop here. */
1905 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (vim_strchr(part_buf, COM_NEST) == NULL)
1907 break;
1908 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001909 return result;
1910}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001911
Bram Moolenaar81340392012-06-06 16:12:59 +02001912/*
1913 * Return the offset at which the last comment in line starts. If there is no
1914 * comment in the whole line, -1 is returned.
1915 *
1916 * When "flags" is not null, it is set to point to the flags describing the
1917 * recognized comment leader.
1918 */
1919 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001920get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001921{
1922 int result = -1;
1923 int i, j;
1924 int lower_check_bound = 0;
1925 char_u *string;
1926 char_u *com_leader;
1927 char_u *com_flags;
1928 char_u *list;
1929 int found_one;
1930 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1931
1932 /*
1933 * Repeat to match several nested comment strings.
1934 */
1935 i = (int)STRLEN(line);
1936 while (--i >= lower_check_bound)
1937 {
1938 /*
1939 * scan through the 'comments' option for a match
1940 */
1941 found_one = FALSE;
1942 for (list = curbuf->b_p_com; *list; )
1943 {
1944 char_u *flags_save = list;
1945
1946 /*
1947 * Get one option part into part_buf[]. Advance list to next one.
1948 * put string at start of string.
1949 */
1950 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1951 string = vim_strchr(part_buf, ':');
1952 if (string == NULL) /* If everything is fine, this cannot actually
1953 * happen. */
1954 {
1955 continue;
1956 }
1957 *string++ = NUL; /* Isolate flags from string. */
1958 com_leader = string;
1959
1960 /*
1961 * Line contents and string must match.
1962 * When string starts with white space, must have some white space
1963 * (but the amount does not need to match, there might be a mix of
1964 * TABs and spaces).
1965 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001966 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001967 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001968 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001969 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01001970 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02001971 ++string;
1972 }
1973 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1974 /* do nothing */;
1975 if (string[j] != NUL)
1976 continue;
1977
1978 /*
1979 * When 'b' flag used, there must be white space or an
1980 * end-of-line after the string in the line.
1981 */
1982 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001983 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02001984 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01001985
Bram Moolenaar4af72592018-12-09 15:00:52 +01001986 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +01001987 {
Bram Moolenaar4af72592018-12-09 15:00:52 +01001988 // For a middlepart comment, only consider it to match if
1989 // everything before the current position in the line is
1990 // whitespace. Otherwise we would think we are inside a
1991 // comment if the middle part appears somewhere in the middle
1992 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +01001993 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
1994 ;
1995 if (j < i)
1996 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02001997 }
1998
1999 /*
2000 * We have found a match, stop searching.
2001 */
2002 found_one = TRUE;
2003
2004 if (flags)
2005 *flags = flags_save;
2006 com_flags = flags_save;
2007
2008 break;
2009 }
2010
2011 if (found_one)
2012 {
2013 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2014 int len1, len2, off;
2015
2016 result = i;
2017 /*
2018 * If this comment nests, continue searching.
2019 */
2020 if (vim_strchr(part_buf, COM_NEST) != NULL)
2021 continue;
2022
2023 lower_check_bound = i;
2024
2025 /* Let's verify whether the comment leader found is a substring
2026 * of other comment leaders. If it is, let's adjust the
2027 * lower_check_bound so that we make sure that we have determined
2028 * the comment leader correctly.
2029 */
2030
Bram Moolenaar1c465442017-03-12 20:10:05 +01002031 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002032 ++com_leader;
2033 len1 = (int)STRLEN(com_leader);
2034
2035 for (list = curbuf->b_p_com; *list; )
2036 {
2037 char_u *flags_save = list;
2038
2039 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2040 if (flags_save == com_flags)
2041 continue;
2042 string = vim_strchr(part_buf2, ':');
2043 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002044 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002045 ++string;
2046 len2 = (int)STRLEN(string);
2047 if (len2 == 0)
2048 continue;
2049
2050 /* Now we have to verify whether string ends with a substring
2051 * beginning the com_leader. */
2052 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2053 {
2054 --off;
2055 if (!STRNCMP(string + off, com_leader, len2 - off))
2056 {
2057 if (i - off < lower_check_bound)
2058 lower_check_bound = i - off;
2059 }
2060 }
2061 }
2062 }
2063 }
2064 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065}
2066#endif
2067
2068/*
2069 * Return the number of window lines occupied by buffer line "lnum".
2070 */
2071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002072plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
2074 return plines_win(curwin, lnum, TRUE);
2075}
2076
2077 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002078plines_win(
2079 win_T *wp,
2080 linenr_T lnum,
2081 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083#if defined(FEAT_DIFF) || defined(PROTO)
2084 /* Check for filler lines above this buffer line. When folded the result
2085 * is one line anyway. */
2086 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2087}
2088
2089 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002090plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
2092 return plines_win_nofill(curwin, lnum, TRUE);
2093}
2094
2095 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096plines_win_nofill(
2097 win_T *wp,
2098 linenr_T lnum,
2099 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100{
2101#endif
2102 int lines;
2103
2104 if (!wp->w_p_wrap)
2105 return 1;
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (wp->w_width == 0)
2108 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
2110#ifdef FEAT_FOLDING
2111 /* A folded lines is handled just like an empty line. */
2112 /* NOTE: Caller must handle lines that are MAYBE folded. */
2113 if (lineFolded(wp, lnum) == TRUE)
2114 return 1;
2115#endif
2116
2117 lines = plines_win_nofold(wp, lnum);
2118 if (winheight > 0 && lines > wp->w_height)
2119 return (int)wp->w_height;
2120 return lines;
2121}
2122
2123/*
2124 * Return number of window lines physical line "lnum" will occupy in window
2125 * "wp". Does not care about folding, 'wrap' or 'diff'.
2126 */
2127 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002128plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 char_u *s;
2131 long col;
2132 int width;
2133
2134 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2135 if (*s == NUL) /* empty line */
2136 return 1;
2137 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2138
2139 /*
2140 * If list mode is on, then the '$' at the end of the line may take up one
2141 * extra column.
2142 */
2143 if (wp->w_p_list && lcs_eol != NUL)
2144 col += 1;
2145
2146 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002147 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002149 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if (width <= 0)
2151 return 32000;
2152 if (col <= width)
2153 return 1;
2154 col -= width;
2155 width += win_col_off2(wp);
2156 return (col + (width - 1)) / width + 1;
2157}
2158
2159/*
2160 * Like plines_win(), but only reports the number of physical screen lines
2161 * used from the start of the line to the given column number.
2162 */
2163 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002164plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 long col;
2167 char_u *s;
2168 int lines = 0;
2169 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002170 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172#ifdef FEAT_DIFF
2173 /* Check for filler lines above this buffer line. When folded the result
2174 * is one line anyway. */
2175 lines = diff_check_fill(wp, lnum);
2176#endif
2177
2178 if (!wp->w_p_wrap)
2179 return lines + 1;
2180
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (wp->w_width == 0)
2182 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
Bram Moolenaar597a4222014-06-25 14:39:50 +02002184 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
2186 col = 0;
2187 while (*s != NUL && --column >= 0)
2188 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002189 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002190 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 }
2192
2193 /*
2194 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2195 * INSERT mode, then col must be adjusted so that it represents the last
2196 * screen position of the TAB. This only fixes an error when the TAB wraps
2197 * from one screen line to the next (when 'columns' is not a multiple of
2198 * 'ts') -- webb.
2199 */
2200 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002201 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002204 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002206 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002207 if (width <= 0)
2208 return 9999;
2209
2210 lines += 1;
2211 if (col > width)
2212 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2213 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214}
2215
2216 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002217plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219 int count = 0;
2220
2221 while (first <= last)
2222 {
2223#ifdef FEAT_FOLDING
2224 int x;
2225
2226 /* Check if there are any really folded lines, but also included lines
2227 * that are maybe folded. */
2228 x = foldedCount(wp, first, NULL);
2229 if (x > 0)
2230 {
2231 ++count; /* count 1 for "+-- folded" line */
2232 first += x;
2233 }
2234 else
2235#endif
2236 {
2237#ifdef FEAT_DIFF
2238 if (first == wp->w_topline)
2239 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2240 else
2241#endif
2242 count += plines_win(wp, first, TRUE);
2243 ++first;
2244 }
2245 }
2246 return (count);
2247}
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249/*
2250 * Insert string "p" at the cursor position. Stops at a NUL byte.
2251 * Handles Replace mode and multi-byte characters.
2252 */
2253 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002254ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 ins_bytes_len(p, (int)STRLEN(p));
2257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259/*
2260 * Insert string "p" with length "len" at the cursor position.
2261 * Handles Replace mode and multi-byte characters.
2262 */
2263 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002264ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 int n;
2268
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002269 if (has_mbyte)
2270 for (i = 0; i < len; i += n)
2271 {
2272 if (enc_utf8)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002273 // avoid reading past p[len]
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002274 n = utfc_ptr2len_len(p + i, len - i);
2275 else
2276 n = (*mb_ptr2len)(p + i);
2277 ins_char_bytes(p + i, n);
2278 }
2279 else
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002280 for (i = 0; i < len; ++i)
2281 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284/*
2285 * Insert or replace a single character at the cursor position.
2286 * When in REPLACE or VREPLACE mode, replace any existing character.
2287 * Caller must have prepared for undo.
2288 * For multi-byte characters we get the whole character, the caller must
2289 * convert bytes to a character.
2290 */
2291 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002292ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002294 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002295 int n = (*mb_char2bytes)(c, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2298 * Happens for CTRL-Vu9900. */
2299 if (buf[0] == 0)
2300 buf[0] = '\n';
2301
2302 ins_char_bytes(buf, n);
2303}
2304
2305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002306ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 int c = buf[0];
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002309 int newlen; // nr of bytes inserted
2310 int oldlen; // nr of bytes deleted (0 when not replacing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 char_u *p;
2312 char_u *newp;
2313 char_u *oldp;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002314 int linelen; // length of old line including NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 colnr_T col;
2316 linenr_T lnum = curwin->w_cursor.lnum;
2317 int i;
2318
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 /* Break tabs if needed. */
2320 if (virtual_active() && curwin->w_cursor.coladd > 0)
2321 coladvance_force(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 col = curwin->w_cursor.col;
2324 oldp = ml_get(lnum);
2325 linelen = (int)STRLEN(oldp) + 1;
2326
2327 /* The lengths default to the values for when not replacing. */
2328 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 if (State & REPLACE_FLAG)
2332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (State & VREPLACE_FLAG)
2334 {
2335 colnr_T new_vcol = 0; /* init for GCC */
2336 colnr_T vcol;
2337 int old_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
2339 /*
2340 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2341 * Returns the old value of list, so when finished,
2342 * curwin->w_p_list should be set back to this.
2343 */
2344 old_list = curwin->w_p_list;
2345 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2346 curwin->w_p_list = FALSE;
2347
2348 /*
2349 * In virtual replace mode each character may replace one or more
2350 * characters (zero if it's a TAB). Count the number of bytes to
2351 * be deleted to make room for the new character, counting screen
2352 * cells. May result in adding spaces to fill a gap.
2353 */
2354 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 new_vcol = vcol + chartabsize(buf, vcol);
2356 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2357 {
2358 vcol += chartabsize(oldp + col + oldlen, vcol);
2359 /* Don't need to remove a TAB that takes us to the right
2360 * position. */
2361 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2362 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002363 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 /* Deleted a bit too much, insert spaces. */
2365 if (vcol > new_vcol)
2366 newlen += vcol - new_vcol;
2367 }
2368 curwin->w_p_list = old_list;
2369 }
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002370 else if (oldp[col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 /* normal replace */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002373 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375
2376
2377 /* Push the replaced bytes onto the replace stack, so that they can be
2378 * put back when BS is used. The bytes of a multi-byte character are
2379 * done the other way around, so that the first byte is popped off
2380 * first (it tells the byte length of the character). */
2381 replace_push(NUL);
2382 for (i = 0; i < oldlen; ++i)
2383 {
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002384 if (has_mbyte)
2385 i += replace_push_mb(oldp + col + i) - 1;
2386 else
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002387 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389 }
2390
2391 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2392 if (newp == NULL)
2393 return;
2394
2395 /* Copy bytes before the cursor. */
2396 if (col > 0)
2397 mch_memmove(newp, oldp, (size_t)col);
2398
2399 /* Copy bytes after the changed character(s). */
2400 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002401 if (linelen > col + oldlen)
2402 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 (size_t)(linelen - col - oldlen));
2404
2405 /* Insert or overwrite the new character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 mch_memmove(p, buf, charlen);
2407 i = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /* Fill with spaces when necessary. */
2410 while (i < newlen)
2411 p[i++] = ' ';
2412
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002413 // Replace the line in the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 ml_replace(lnum, newp, FALSE);
2415
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002416 // mark the buffer as changed and prepare for displaying
2417 inserted_bytes(lnum, col, newlen - oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419 /*
2420 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2421 * show the match for right parens and braces.
2422 */
2423 if (p_sm && (State & INSERT)
2424 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002425#ifdef FEAT_INS_EXPAND
2426 && !ins_compl_active()
2427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002429 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002430 if (has_mbyte)
2431 showmatch(mb_ptr2char(buf));
2432 else
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002433 showmatch(c);
2434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436#ifdef FEAT_RIGHTLEFT
2437 if (!p_ri || (State & REPLACE_FLAG))
2438#endif
2439 {
2440 /* Normal insert: move cursor right */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 curwin->w_cursor.col += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443 /*
2444 * TODO: should try to update w_row here, to avoid recomputing it later.
2445 */
2446}
2447
2448/*
2449 * Insert a string at the cursor position.
2450 * Note: Does NOT handle Replace mode.
2451 * Caller must have prepared for undo.
2452 */
2453 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002454ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
2456 char_u *oldp, *newp;
2457 int newlen = (int)STRLEN(s);
2458 int oldlen;
2459 colnr_T col;
2460 linenr_T lnum = curwin->w_cursor.lnum;
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (virtual_active() && curwin->w_cursor.coladd > 0)
2463 coladvance_force(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 col = curwin->w_cursor.col;
2466 oldp = ml_get(lnum);
2467 oldlen = (int)STRLEN(oldp);
2468
2469 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2470 if (newp == NULL)
2471 return;
2472 if (col > 0)
2473 mch_memmove(newp, oldp, (size_t)col);
2474 mch_memmove(newp + col, s, (size_t)newlen);
2475 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2476 ml_replace(lnum, newp, FALSE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002477 inserted_bytes(lnum, col, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 curwin->w_cursor.col += newlen;
2479}
2480
2481/*
2482 * Delete one character under the cursor.
2483 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2484 * Caller must have prepared for undo.
2485 *
2486 * return FAIL for failure, OK otherwise
2487 */
2488 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002489del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (has_mbyte)
2492 {
2493 /* Make sure the cursor is at the start of a character. */
2494 mb_adjust_cursor();
2495 if (*ml_get_cursor() == NUL)
2496 return FAIL;
2497 return del_chars(1L, fixpos);
2498 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002499 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502/*
2503 * Like del_bytes(), but delete characters instead of bytes.
2504 */
2505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002506del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 long bytes = 0;
2509 long i;
2510 char_u *p;
2511 int l;
2512
2513 p = ml_get_cursor();
2514 for (i = 0; i < count && *p != NUL; ++i)
2515 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002516 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 bytes += l;
2518 p += l;
2519 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002520 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523/*
2524 * Delete "count" bytes under the cursor.
2525 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2526 * Caller must have prepared for undo.
2527 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002528 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 */
2530 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002531del_bytes(
2532 long count,
2533 int fixpos_arg,
2534 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 char_u *oldp, *newp;
2537 colnr_T oldlen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002538 colnr_T newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 linenr_T lnum = curwin->w_cursor.lnum;
2540 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002541 int alloc_newp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002543 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 oldp = ml_get(lnum);
2546 oldlen = (int)STRLEN(oldp);
2547
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002548 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (col >= oldlen)
2550 return FAIL;
2551
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002552 /* If "count" is zero there is nothing to do. */
2553 if (count == 0)
2554 return OK;
2555
2556 /* If "count" is negative the caller must be doing something wrong. */
2557 if (count < 1)
2558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002559 siemsg("E950: Invalid count for del_bytes(): %ld", count);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002560 return FAIL;
2561 }
2562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 /* If 'delcombine' is set and deleting (less than) one character, only
2564 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002565 if (p_deco && use_delcombine && enc_utf8
2566 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002568 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 int n;
2570
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002571 (void)utfc_ptr2char(oldp + col, cc);
2572 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 /* Find the last composing char, there can be several. */
2575 n = col;
2576 do
2577 {
2578 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002579 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 n += count;
2581 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2582 fixpos = 0;
2583 }
2584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586 /*
2587 * When count is too big, reduce it.
2588 */
2589 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2590 if (movelen <= 1)
2591 {
2592 /*
2593 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002594 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2595 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002597 if (col > 0 && fixpos && restart_edit == 0
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002598 && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 --curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (has_mbyte)
2603 curwin->w_cursor.col -=
2604 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 count = oldlen - col;
2607 movelen = 1;
2608 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002609 newlen = oldlen - count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
2611 /*
2612 * If the old line has been allocated the deletion can be done in the
2613 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002614 * Can't do this when using Netbeans, because we would need to invoke
2615 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002616 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002619 if (netbeans_active())
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002620 alloc_newp = TRUE;
Bram Moolenaare21877a2008-02-13 09:58:14 +00002621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002623 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
2624 if (!alloc_newp)
2625 newp = oldp; // use same allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 else
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002627 { // need to allocate a new line
2628 newp = alloc((unsigned)(newlen + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 if (newp == NULL)
2630 return FAIL;
2631 mch_memmove(newp, oldp, (size_t)col);
2632 }
2633 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002634 if (alloc_newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 ml_replace(lnum, newp, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002636#ifdef FEAT_TEXT_PROP
2637 else
2638 {
2639 // Also move any following text properties.
2640 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
2641 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
2642 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
2643 curbuf->b_ml.ml_line_len -= count;
2644 }
2645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002647 // mark the buffer as changed and prepare for displaying
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01002648 inserted_bytes(lnum, curwin->w_cursor.col, -count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 return OK;
2651}
2652
2653/*
2654 * Delete from cursor to end of line.
2655 * Caller must have prepared for undo.
2656 *
2657 * return FAIL for failure, OK otherwise
2658 */
2659 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002660truncate_line(
2661 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663 char_u *newp;
2664 linenr_T lnum = curwin->w_cursor.lnum;
2665 colnr_T col = curwin->w_cursor.col;
2666
2667 if (col == 0)
2668 newp = vim_strsave((char_u *)"");
2669 else
2670 newp = vim_strnsave(ml_get(lnum), col);
2671
2672 if (newp == NULL)
2673 return FAIL;
2674
2675 ml_replace(lnum, newp, FALSE);
2676
2677 /* mark the buffer as changed and prepare for displaying */
2678 changed_bytes(lnum, curwin->w_cursor.col);
2679
2680 /*
2681 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2682 */
2683 if (fixpos && curwin->w_cursor.col > 0)
2684 --curwin->w_cursor.col;
2685
2686 return OK;
2687}
2688
2689/*
2690 * Delete "nlines" lines at the cursor.
2691 * Saves the lines for undo first if "undo" is TRUE.
2692 */
2693 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002694del_lines(
2695 long nlines, /* number of lines to delete */
2696 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002699 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 if (nlines <= 0)
2702 return;
2703
2704 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002705 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return;
2707
2708 for (n = 0; n < nlines; )
2709 {
2710 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2711 break;
2712
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002713 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 ++n;
2715
2716 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002717 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 break;
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002721 /* Correct the cursor position before calling deleted_lines_mark(), it may
2722 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 curwin->w_cursor.col = 0;
2724 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002725
2726 /* adjust marks, mark the buffer as changed and prepare for displaying */
2727 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728}
2729
2730 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002731gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002733 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002735 /* When searching columns is sometimes put at the end of a line. */
2736 if (pos->col == MAXCOL)
2737 return NUL;
2738 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if (has_mbyte)
2740 return (*mb_ptr2char)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 return (int)*ptr;
2742}
2743
2744 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002745gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (has_mbyte)
2748 return (*mb_ptr2char)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 return (int)*ml_get_cursor();
2750}
2751
2752/*
2753 * Write a character at the current cursor position.
2754 * It is directly written into the block.
2755 */
2756 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002757pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2760 + curwin->w_cursor.col) = c;
2761}
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763/*
2764 * When extra == 0: Return TRUE if the cursor is before or on the first
2765 * non-blank in the line.
2766 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2767 * the line.
2768 */
2769 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002770inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
2772 char_u *ptr;
2773 colnr_T col;
2774
Bram Moolenaar1c465442017-03-12 20:10:05 +01002775 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 ++ptr;
2777 if (col >= curwin->w_cursor.col + extra)
2778 return TRUE;
2779 else
2780 return FALSE;
2781}
2782
2783/*
2784 * Skip to next part of an option argument: Skip space and comma.
2785 */
2786 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002787skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 if (*p == ',')
2790 ++p;
2791 while (*p == ' ')
2792 ++p;
2793 return p;
2794}
2795
2796/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002797 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 *
2799 * Most often called through changed_bytes() and changed_lines(), which also
2800 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002801 *
2802 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002805changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
2807#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002808 if (p_imst == IM_ON_THE_SPOT)
2809 {
2810 /* The text of the preediting area is inserted, but this doesn't
2811 * mean a change of the buffer yet. That is delayed until the
2812 * text is committed. (this means preedit becomes empty) */
2813 if (im_is_preediting() && !xim_changed_while_preediting)
2814 return;
2815 xim_changed_while_preediting = FALSE;
2816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817#endif
2818
2819 if (!curbuf->b_changed)
2820 {
2821 int save_msg_scroll = msg_scroll;
2822
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002823 /* Give a warning about changing a read-only file. This may also
2824 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002826
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 /* Create a swap file if that is wanted.
2828 * Don't do this for "nofile" and "nowrite" buffer types. */
2829 if (curbuf->b_may_swap
2830#ifdef FEAT_QUICKFIX
2831 && !bt_dontwrite(curbuf)
2832#endif
2833 )
2834 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002835 int save_need_wait_return = need_wait_return;
2836
2837 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 ml_open_file(curbuf);
2839
2840 /* The ml_open_file() can cause an ATTENTION message.
2841 * Wait two seconds, to make sure the user reads this unexpected
2842 * message. Since we could be anywhere, call wait_return() now,
2843 * and don't let the emsg() set msg_scroll. */
2844 if (need_wait_return && emsg_silent == 0)
2845 {
2846 out_flush();
2847 ui_delay(2000L, TRUE);
2848 wait_return(TRUE);
2849 msg_scroll = save_msg_scroll;
2850 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002851 else
2852 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002854 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002856 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857}
2858
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002859/*
2860 * Internal part of changed(), no user interaction.
2861 */
2862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002863changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002864{
2865 curbuf->b_changed = TRUE;
2866 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002867 check_status(curbuf);
2868 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002869#ifdef FEAT_TITLE
2870 need_maketitle = TRUE; /* set window title later */
2871#endif
2872}
2873
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002874static void changedOneline(buf_T *buf, linenr_T lnum);
2875static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2876static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878/*
2879 * Changed bytes within a single line for the current buffer.
2880 * - marks the windows on this buffer to be redisplayed
2881 * - marks the buffer changed by calling changed()
2882 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002883 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 */
2885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002886changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002888 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002890
2891#ifdef FEAT_DIFF
2892 /* Diff highlighting in other diff windows may need to be updated too. */
2893 if (curwin->w_p_diff)
2894 {
2895 win_T *wp;
2896 linenr_T wlnum;
2897
Bram Moolenaar29323592016-07-24 22:04:11 +02002898 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002899 if (wp->w_p_diff && wp != curwin)
2900 {
2901 redraw_win_later(wp, VALID);
2902 wlnum = diff_lnum_win(lnum, wp);
2903 if (wlnum > 0)
2904 changedOneline(wp->w_buffer, wlnum);
2905 }
2906 }
2907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908}
2909
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002910/*
2911 * Like changed_bytes() but also adjust text properties for "added" bytes.
2912 * When "added" is negative text was deleted.
2913 */
2914 void
Bram Moolenaar402385a2019-01-11 14:10:03 +01002915inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002916{
2917 changed_bytes(lnum, col);
2918
2919#ifdef FEAT_TEXT_PROP
2920 if (curbuf->b_has_textprop && added != 0)
2921 adjust_prop_columns(lnum, col, added);
2922#endif
2923}
2924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002926changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002928 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 {
2930 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002931 if (lnum < buf->b_mod_top)
2932 buf->b_mod_top = lnum;
2933 else if (lnum >= buf->b_mod_bot)
2934 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936 else
2937 {
2938 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002939 buf->b_mod_set = TRUE;
2940 buf->b_mod_top = lnum;
2941 buf->b_mod_bot = lnum + 1;
2942 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944}
2945
2946/*
2947 * Appended "count" lines below line "lnum" in the current buffer.
2948 * Must be called AFTER the change and after mark_adjust().
2949 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2950 */
2951 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002952appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
2954 changed_lines(lnum + 1, 0, lnum + 1, count);
2955}
2956
2957/*
2958 * Like appended_lines(), but adjust marks first.
2959 */
2960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002961appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002963 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002964 * be marks there. But it's still needed in diff mode. */
2965 if (lnum + count < curbuf->b_ml.ml_line_count
2966#ifdef FEAT_DIFF
2967 || curwin->w_p_diff
2968#endif
2969 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002970 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 changed_lines(lnum + 1, 0, lnum + 1, count);
2972}
2973
2974/*
2975 * Deleted "count" lines at line "lnum" in the current buffer.
2976 * Must be called AFTER the change and after mark_adjust().
2977 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2978 */
2979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002980deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 changed_lines(lnum, 0, lnum + count, -count);
2983}
2984
2985/*
2986 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002987 * Make sure the cursor is on a valid line before calling, a GUI callback may
2988 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 */
2990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002991deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2994 changed_lines(lnum, 0, lnum + count, -count);
2995}
2996
2997/*
2998 * Changed lines for the current buffer.
2999 * Must be called AFTER the change and after mark_adjust().
3000 * - mark the buffer changed by calling changed()
3001 * - mark the windows on this buffer to be redisplayed
3002 * - invalidate cached values
3003 * "lnum" is the first line that needs displaying, "lnume" the first line
3004 * below the changed lines (BEFORE the change).
3005 * When only inserting lines, "lnum" and "lnume" are equal.
3006 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003007 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003010changed_lines(
3011 linenr_T lnum, /* first line with change */
3012 colnr_T col, /* column in first line with change */
3013 linenr_T lnume, /* line below last changed line */
3014 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003016 changed_lines_buf(curbuf, lnum, lnume, xtra);
3017
3018#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003019 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003020 {
3021 /* When the number of lines doesn't change then mark_adjust() isn't
3022 * called and other diff buffers still need to be marked for
3023 * displaying. */
3024 win_T *wp;
3025 linenr_T wlnum;
3026
Bram Moolenaar29323592016-07-24 22:04:11 +02003027 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003028 if (wp->w_p_diff && wp != curwin)
3029 {
3030 redraw_win_later(wp, VALID);
3031 wlnum = diff_lnum_win(lnum, wp);
3032 if (wlnum > 0)
3033 changed_lines_buf(wp->w_buffer, wlnum,
3034 lnume - lnum + wlnum, 0L);
3035 }
3036 }
3037#endif
3038
3039 changed_common(lnum, col, lnume, xtra);
3040}
3041
3042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003043changed_lines_buf(
3044 buf_T *buf,
3045 linenr_T lnum, /* first line with change */
3046 linenr_T lnume, /* line below last changed line */
3047 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003048{
3049 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
3051 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003052 if (lnum < buf->b_mod_top)
3053 buf->b_mod_top = lnum;
3054 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 {
3056 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003057 buf->b_mod_bot += xtra;
3058 if (buf->b_mod_bot < lnum)
3059 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003061 if (lnume + xtra > buf->b_mod_bot)
3062 buf->b_mod_bot = lnume + xtra;
3063 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 }
3065 else
3066 {
3067 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003068 buf->b_mod_set = TRUE;
3069 buf->b_mod_top = lnum;
3070 buf->b_mod_bot = lnume + xtra;
3071 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073}
3074
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003075/*
3076 * Common code for when a change is was made.
3077 * See changed_lines() for the arguments.
3078 * Careful: may trigger autocommands that reload the buffer.
3079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003081changed_common(
3082 linenr_T lnum,
3083 colnr_T col,
3084 linenr_T lnume,
3085 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003088 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 int i;
3090#ifdef FEAT_JUMPLIST
3091 int cols;
3092 pos_T *p;
3093 int add;
3094#endif
3095
3096 /* mark the buffer as modified */
3097 changed();
3098
Bram Moolenaare3521d92018-09-16 14:10:31 +02003099#ifdef FEAT_DIFF
3100 if (curwin->w_p_diff && diff_internal())
3101 curtab->tp_diff_update = TRUE;
3102#endif
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 /* set the '. mark */
3105 if (!cmdmod.keepjumps)
3106 {
3107 curbuf->b_last_change.lnum = lnum;
3108 curbuf->b_last_change.col = col;
3109
3110#ifdef FEAT_JUMPLIST
3111 /* Create a new entry if a new undo-able change was started or we
3112 * don't have an entry yet. */
3113 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3114 {
3115 if (curbuf->b_changelistlen == 0)
3116 add = TRUE;
3117 else
3118 {
3119 /* Don't create a new entry when the line number is the same
3120 * as the last one and the column is not too far away. Avoids
3121 * creating many entries for typing "xxxxx". */
3122 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3123 if (p->lnum != lnum)
3124 add = TRUE;
3125 else
3126 {
3127 cols = comp_textwidth(FALSE);
3128 if (cols == 0)
3129 cols = 79;
3130 add = (p->col + cols < col || col + cols < p->col);
3131 }
3132 }
3133 if (add)
3134 {
3135 /* This is the first of a new sequence of undo-able changes
3136 * and it's at some distance of the last change. Use a new
3137 * position in the changelist. */
3138 curbuf->b_new_change = FALSE;
3139
3140 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3141 {
3142 /* changelist is full: remove oldest entry */
3143 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3144 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3145 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003146 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 /* Correct position in changelist for other windows on
3149 * this buffer. */
3150 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3151 --wp->w_changelistidx;
3152 }
3153 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003154 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
3156 /* For other windows, if the position in the changelist is
3157 * at the end it stays at the end. */
3158 if (wp->w_buffer == curbuf
3159 && wp->w_changelistidx == curbuf->b_changelistlen)
3160 ++wp->w_changelistidx;
3161 }
3162 ++curbuf->b_changelistlen;
3163 }
3164 }
3165 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3166 curbuf->b_last_change;
3167 /* The current window is always after the last change, so that "g,"
3168 * takes you back to it. */
3169 curwin->w_changelistidx = curbuf->b_changelistlen;
3170#endif
3171 }
3172
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003173 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
3175 if (wp->w_buffer == curbuf)
3176 {
3177 /* Mark this window to be redrawn later. */
3178 if (wp->w_redr_type < VALID)
3179 wp->w_redr_type = VALID;
3180
3181 /* Check if a change in the buffer has invalidated the cached
3182 * values for the cursor. */
3183#ifdef FEAT_FOLDING
3184 /*
3185 * Update the folds for this window. Can't postpone this, because
3186 * a following operator might work on the whole fold: ">>dd".
3187 */
3188 foldUpdate(wp, lnum, lnume + xtra - 1);
3189
3190 /* The change may cause lines above or below the change to become
3191 * included in a fold. Set lnum/lnume to the first/last line that
3192 * might be displayed differently.
3193 * Set w_cline_folded here as an efficient way to update it when
3194 * inserting lines just above a closed fold. */
3195 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3196 if (wp->w_cursor.lnum == lnum)
3197 wp->w_cline_folded = i;
3198 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3199 if (wp->w_cursor.lnum == lnume)
3200 wp->w_cline_folded = i;
3201
3202 /* If the changed line is in a range of previously folded lines,
3203 * compare with the first line in that range. */
3204 if (wp->w_cursor.lnum <= lnum)
3205 {
3206 i = find_wl_entry(wp, lnum);
3207 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3208 changed_line_abv_curs_win(wp);
3209 }
3210#endif
3211
3212 if (wp->w_cursor.lnum > lnum)
3213 changed_line_abv_curs_win(wp);
3214 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3215 changed_cline_bef_curs_win(wp);
3216 if (wp->w_botline >= lnum)
3217 {
3218 /* Assume that botline doesn't change (inserted lines make
3219 * other lines scroll down below botline). */
3220 approximate_botline_win(wp);
3221 }
3222
3223 /* Check if any w_lines[] entries have become invalid.
3224 * For entries below the change: Correct the lnums for
3225 * inserted/deleted lines. Makes it possible to stop displaying
3226 * after the change. */
3227 for (i = 0; i < wp->w_lines_valid; ++i)
3228 if (wp->w_lines[i].wl_valid)
3229 {
3230 if (wp->w_lines[i].wl_lnum >= lnum)
3231 {
3232 if (wp->w_lines[i].wl_lnum < lnume)
3233 {
3234 /* line included in change */
3235 wp->w_lines[i].wl_valid = FALSE;
3236 }
3237 else if (xtra != 0)
3238 {
3239 /* line below change */
3240 wp->w_lines[i].wl_lnum += xtra;
3241#ifdef FEAT_FOLDING
3242 wp->w_lines[i].wl_lastlnum += xtra;
3243#endif
3244 }
3245 }
3246#ifdef FEAT_FOLDING
3247 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3248 {
3249 /* change somewhere inside this range of folded lines,
3250 * may need to be redrawn */
3251 wp->w_lines[i].wl_valid = FALSE;
3252 }
3253#endif
3254 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003255
3256#ifdef FEAT_FOLDING
3257 /* Take care of side effects for setting w_topline when folds have
3258 * changed. Esp. when the buffer was changed in another window. */
3259 if (hasAnyFolding(wp))
3260 set_topline(wp, wp->w_topline);
3261#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003262 /* relative numbering may require updating more */
3263 if (wp->w_p_rnu)
3264 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
3266 }
3267
3268 /* Call update_screen() later, which checks out what needs to be redrawn,
3269 * since it notices b_mod_set and then uses b_mod_*. */
3270 if (must_redraw < VALID)
3271 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003272
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003273 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003274 if (lnum <= curwin->w_cursor.lnum
3275 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003276 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277}
3278
3279/*
3280 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3281 */
3282 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283unchanged(
3284 buf_T *buf,
3285 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003287 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
3289 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003290 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (ff)
3292 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003294 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295#ifdef FEAT_TITLE
3296 need_maketitle = TRUE; /* set window title later */
3297#endif
3298 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003299 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#ifdef FEAT_NETBEANS_INTG
3301 netbeans_unmodified(buf);
3302#endif
3303}
3304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305/*
3306 * check_status: called when the status bars for the buffer 'buf'
3307 * need to be updated
3308 */
3309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003310check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
3312 win_T *wp;
3313
Bram Moolenaar29323592016-07-24 22:04:11 +02003314 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (wp->w_buffer == buf && wp->w_status_height)
3316 {
3317 wp->w_redr_status = TRUE;
3318 if (must_redraw < VALID)
3319 must_redraw = VALID;
3320 }
3321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323/*
3324 * If the file is readonly, give a warning message with the first change.
3325 * Don't do this for autocommands.
3326 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003327 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003329 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 */
3331 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003332change_warning(
3333 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 mode and 'showmode' is on */
3335{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003336 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (curbuf->b_did_warn == FALSE
3339 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 && curbuf->b_p_ro)
3342 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003343 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003345 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 if (!curbuf->b_p_ro)
3347 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 /*
3349 * Do what msg() does, but with a column offset if the warning should
3350 * be after the mode message.
3351 */
3352 msg_start();
3353 if (msg_row == Rows - 1)
3354 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003355 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003356 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003357#ifdef FEAT_EVAL
3358 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 msg_clr_eos();
3361 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003362 if (msg_silent == 0 && !silent_mode
3363#ifdef FEAT_EVAL
3364 && time_for_testing != 1
3365#endif
3366 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 out_flush();
3369 ui_delay(1000L, TRUE); /* give the user time to think about it */
3370 }
3371 curbuf->b_did_warn = TRUE;
3372 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3373 if (msg_row < Rows - 1)
3374 showmode();
3375 }
3376}
3377
3378/*
3379 * Ask for a reply from the user, a 'y' or a 'n'.
3380 * No other characters are accepted, the message is repeated until a valid
3381 * reply is entered or CTRL-C is hit.
3382 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3383 * from any buffers but directly from the user.
3384 *
3385 * return the 'y' or 'n'
3386 */
3387 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003388ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
3390 int r = ' ';
3391 int save_State = State;
3392
3393 if (exiting) /* put terminal in raw mode for this question */
3394 settmode(TMODE_RAW);
3395 ++no_wait_return;
3396#ifdef USE_ON_FLY_SCROLL
3397 dont_scroll = TRUE; /* disallow scrolling here */
3398#endif
3399 State = CONFIRM; /* mouse behaves like with :confirm */
3400#ifdef FEAT_MOUSE
3401 setmouse(); /* disables mouse for xterm */
3402#endif
3403 ++no_mapping;
3404 ++allow_keys; /* no mapping here, but recognize keys */
3405
3406 while (r != 'y' && r != 'n')
3407 {
3408 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003409 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (direct)
3411 r = get_keystroke();
3412 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003413 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (r == Ctrl_C || r == ESC)
3415 r = 'n';
3416 msg_putchar(r); /* show what you typed */
3417 out_flush();
3418 }
3419 --no_wait_return;
3420 State = save_State;
3421#ifdef FEAT_MOUSE
3422 setmouse();
3423#endif
3424 --no_mapping;
3425 --allow_keys;
3426
3427 return r;
3428}
3429
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003430#if defined(FEAT_MOUSE) || defined(PROTO)
3431/*
3432 * Return TRUE if "c" is a mouse key.
3433 */
3434 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003435is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003436{
3437 return c == K_LEFTMOUSE
3438 || c == K_LEFTMOUSE_NM
3439 || c == K_LEFTDRAG
3440 || c == K_LEFTRELEASE
3441 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003442 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003443 || c == K_MIDDLEMOUSE
3444 || c == K_MIDDLEDRAG
3445 || c == K_MIDDLERELEASE
3446 || c == K_RIGHTMOUSE
3447 || c == K_RIGHTDRAG
3448 || c == K_RIGHTRELEASE
3449 || c == K_MOUSEDOWN
3450 || c == K_MOUSEUP
3451 || c == K_MOUSELEFT
3452 || c == K_MOUSERIGHT
3453 || c == K_X1MOUSE
3454 || c == K_X1DRAG
3455 || c == K_X1RELEASE
3456 || c == K_X2MOUSE
3457 || c == K_X2DRAG
3458 || c == K_X2RELEASE;
3459}
3460#endif
3461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462/*
3463 * Get a key stroke directly from the user.
3464 * Ignores mouse clicks and scrollbar events, except a click for the left
3465 * button (used at the more prompt).
3466 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3467 * Disadvantage: typeahead is ignored.
3468 * Translates the interrupt character for unix to ESC.
3469 */
3470 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003471get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003473 char_u *buf = NULL;
3474 int buflen = 150;
3475 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 int len = 0;
3477 int n;
3478 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003479 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
3481 mapped_ctrl_c = FALSE; /* mappings are not used here */
3482 for (;;)
3483 {
3484 cursor_on();
3485 out_flush();
3486
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003487 /* Leave some room for check_termcode() to insert a key code into (max
3488 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3489 * bytes. */
3490 maxlen = (buflen - 6 - len) / 3;
3491 if (buf == NULL)
3492 buf = alloc(buflen);
3493 else if (maxlen < 10)
3494 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003495 char_u *t_buf = buf;
3496
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003497 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003498 * escape sequence. */
3499 buflen += 100;
3500 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003501 if (buf == NULL)
3502 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003503 maxlen = (buflen - 6 - len) / 3;
3504 }
3505 if (buf == NULL)
3506 {
3507 do_outofmem_msg((long_u)buflen);
3508 return ESC; /* panic! */
3509 }
3510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003512 * terminal code to complete. */
3513 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (n > 0)
3515 {
3516 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003517 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003519 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003521 else if (len > 0)
3522 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaar4395a712006-09-05 18:57:57 +00003524 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003525 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003526 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003528
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003529 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003530 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003531 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003532 {
3533 /* Redrawing was postponed, do it now. */
3534 update_screen(0);
3535 setcursor(); /* put cursor back where it belongs */
3536 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003537 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003538 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003539 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003541 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 continue;
3543
3544 /* Handle modifier and/or special key code. */
3545 n = buf[0];
3546 if (n == K_SPECIAL)
3547 {
3548 n = TO_SPECIAL(buf[1], buf[2]);
3549 if (buf[1] == KS_MODIFIER
3550 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003551#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003552 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003553#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003554#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 || n == K_VER_SCROLLBAR
3556 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557#endif
3558 )
3559 {
3560 if (buf[1] == KS_MODIFIER)
3561 mod_mask = buf[2];
3562 len -= 3;
3563 if (len > 0)
3564 mch_memmove(buf, buf + 3, (size_t)len);
3565 continue;
3566 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (has_mbyte)
3570 {
3571 if (MB_BYTE2LEN(n) > len)
3572 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003573 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 n = (*mb_ptr2char)(buf);
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#ifdef UNIX
3577 if (n == intr_char)
3578 n = ESC;
3579#endif
3580 break;
3581 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003582 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 mapped_ctrl_c = save_mapped_ctrl_c;
3585 return n;
3586}
3587
3588/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003589 * Get a number from the user.
3590 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 */
3592 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003593get_number(
3594 int colon, /* allow colon to abort */
3595 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
3597 int n = 0;
3598 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003599 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003601 if (mouse_used != NULL)
3602 *mouse_used = FALSE;
3603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 /* When not printing messages, the user won't know what to type, return a
3605 * zero (as if CR was hit). */
3606 if (msg_silent != 0)
3607 return 0;
3608
3609#ifdef USE_ON_FLY_SCROLL
3610 dont_scroll = TRUE; /* disallow scrolling here */
3611#endif
3612 ++no_mapping;
3613 ++allow_keys; /* no mapping here, but recognize keys */
3614 for (;;)
3615 {
3616 windgoto(msg_row, msg_col);
3617 c = safe_vgetc();
3618 if (VIM_ISDIGIT(c))
3619 {
3620 n = n * 10 + c - '0';
3621 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003622 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3625 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003626 if (typed > 0)
3627 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003628 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003629 --typed;
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003633#ifdef FEAT_MOUSE
3634 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3635 {
3636 *mouse_used = TRUE;
3637 n = mouse_row + 1;
3638 break;
3639 }
3640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 else if (n == 0 && c == ':' && colon)
3642 {
3643 stuffcharReadbuff(':');
3644 if (!exmode_active)
3645 cmdline_row = msg_row;
3646 skip_redraw = TRUE; /* skip redraw once */
3647 do_redraw = FALSE;
3648 break;
3649 }
3650 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3651 break;
3652 }
3653 --no_mapping;
3654 --allow_keys;
3655 return n;
3656}
3657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003658/*
3659 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003660 * When "mouse_used" is not NULL allow using the mouse and in that case return
3661 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003662 */
3663 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003664prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003665{
3666 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003667 int save_cmdline_row;
3668 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003669
3670 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003671 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003672 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003673 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003674 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003675
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003676 // Set the state such that text can be selected/copied/pasted and we still
3677 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3678 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003679 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003680 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003681 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003682 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003683#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003684 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003685 setmouse();
3686#endif
3687
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003688 i = get_number(TRUE, mouse_used);
3689 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003691 /* don't call wait_return() now */
3692 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003693 cmdline_row = msg_row - 1;
3694 need_wait_return = FALSE;
3695 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003696 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003697 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003698 else
3699 cmdline_row = save_cmdline_row;
3700 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003701#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003702 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003703 setmouse();
3704#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003706 return i;
3707}
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 long pn;
3713
3714 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3716 return;
3717
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003718 /* We don't want to overwrite another important message, but do overwrite
3719 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3720 * then "put" reports the last action. */
3721 if (keep_msg != NULL && !keep_msg_more)
3722 return;
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (n > 0)
3725 pn = n;
3726 else
3727 pn = -n;
3728
3729 if (pn > p_report)
3730 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003731 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003732 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003733 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003735 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003736 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003738 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
3739 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (msg(msg_buf))
3741 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003742 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003743 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 }
3746}
3747
3748/*
3749 * flush map and typeahead buffers and give a warning for an error
3750 */
3751 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003752beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
3754 if (emsg_silent == 0)
3755 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003756 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003757 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759}
3760
3761/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003762 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 */
3764 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003765vim_beep(
3766 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003768#ifdef FEAT_EVAL
3769 called_vim_beep = TRUE;
3770#endif
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (emsg_silent == 0)
3773 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003774 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3775 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003776#ifdef ELAPSED_FUNC
3777 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01003778 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003779
3780 /* Only beep once per half a second, otherwise a sequence of beeps
3781 * would freeze Vim. */
3782 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3783 {
3784 did_init = TRUE;
3785 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003787 if (p_vb
3788#ifdef FEAT_GUI
3789 /* While the GUI is starting up the termcap is set for
3790 * the GUI but the output still goes to a terminal. */
3791 && !(gui.in_use && gui.starting)
3792#endif
3793 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003794 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003795 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003796#ifdef FEAT_VTP
3797 /* No restore color information, refresh the screen. */
3798 if (has_vtp_working() != 0
3799# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003800 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003801# endif
3802 )
3803 {
3804 redraw_later(CLEAR);
3805 update_screen(0);
3806 redrawcmd();
3807 }
3808#endif
3809 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003810 else
3811 out_char(BELL);
3812#ifdef ELAPSED_FUNC
3813 }
3814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003816
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003817 /* When 'debug' contains "beep" produce a message. If we are sourcing
3818 * a script or executing a function give the user a hint where the beep
3819 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003820 if (vim_strchr(p_debug, 'e') != NULL)
3821 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003822 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003823 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
3826}
3827
3828/*
3829 * To get the "real" home directory:
3830 * - get value of $HOME
3831 * For Unix:
3832 * - go to that directory
3833 * - do mch_dirname() to get the real name of that directory.
3834 * This also works with mounts and links.
3835 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003836 * For Windows:
3837 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 */
3839static char_u *homedir = NULL;
3840
3841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003842init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
3844 char_u *var;
3845
Bram Moolenaar05159a02005-02-26 23:04:13 +00003846 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003847 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#ifdef VMS
3850 var = mch_getenv((char_u *)"SYS$LOGIN");
3851#else
3852 var = mch_getenv((char_u *)"HOME");
3853#endif
3854
Bram Moolenaar4f974752019-02-17 17:44:42 +01003855#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003857 * Typically, $HOME is not defined on Windows, unless the user has
3858 * specifically defined it for Vim's sake. However, on Windows NT
3859 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3860 * each user. Try constructing $HOME from these.
3861 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003862 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003863 {
3864 char_u *homedrive, *homepath;
3865
3866 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3867 homepath = mch_getenv((char_u *)"HOMEPATH");
3868 if (homepath == NULL || *homepath == NUL)
3869 homepath = (char_u *)"\\";
3870 if (homedrive != NULL
3871 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3872 {
3873 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3874 if (NameBuff[0] != NUL)
3875 var = NameBuff;
3876 }
3877 }
3878
3879 if (var == NULL)
3880 var = mch_getenv((char_u *)"USERPROFILE");
3881
3882 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 * Weird but true: $HOME may contain an indirect reference to another
3884 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3885 * when $HOME is being set.
3886 */
3887 if (var != NULL && *var == '%')
3888 {
3889 char_u *p;
3890 char_u *exp;
3891
3892 p = vim_strchr(var + 1, '%');
3893 if (p != NULL)
3894 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003895 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 exp = mch_getenv(NameBuff);
3897 if (exp != NULL && *exp != NUL
3898 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3899 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003900 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 }
3904 }
3905
Bram Moolenaar48340b62017-08-29 22:08:53 +02003906 if (var != NULL && *var == NUL) /* empty is same as not set */
3907 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
Bram Moolenaar05159a02005-02-26 23:04:13 +00003909 if (enc_utf8 && var != NULL)
3910 {
3911 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003912 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003913
3914 /* Convert from active codepage to UTF-8. Other conversions are
3915 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003916 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003917 if (pp != NULL)
3918 {
3919 homedir = pp;
3920 return;
3921 }
3922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 /*
3925 * Default home dir is C:/
3926 * Best assumption we can make in such a situation.
3927 */
3928 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003929 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (var != NULL)
3933 {
3934#ifdef UNIX
3935 /*
3936 * Change to the directory and get the actual path. This resolves
3937 * links. Don't do it when we can't return.
3938 */
3939 if (mch_dirname(NameBuff, MAXPATHL) == OK
3940 && mch_chdir((char *)NameBuff) == 0)
3941 {
3942 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3943 var = IObuff;
3944 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003945 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947#endif
3948 homedir = vim_strsave(var);
3949 }
3950}
3951
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003952#if defined(EXITFREE) || defined(PROTO)
3953 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003954free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003955{
3956 vim_free(homedir);
3957}
Bram Moolenaar24305862012-08-15 14:05:05 +02003958
3959# ifdef FEAT_CMDL_COMPL
3960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003961free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003962{
3963 ga_clear_strings(&ga_users);
3964}
3965# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003966#endif
3967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003969 * Call expand_env() and store the result in an allocated string.
3970 * This is not very memory efficient, this expects the result to be freed
3971 * again soon.
3972 */
3973 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003974expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003975{
3976 return expand_env_save_opt(src, FALSE);
3977}
3978
3979/*
3980 * Idem, but when "one" is TRUE handle the string as one file name, only
3981 * expand "~" at the start.
3982 */
3983 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003984expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003985{
3986 char_u *p;
3987
3988 p = alloc(MAXPATHL);
3989 if (p != NULL)
3990 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3991 return p;
3992}
3993
3994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 * Expand environment variable with path name.
3996 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003997 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * If anything fails no expansion is done and dst equals src.
3999 */
4000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004001expand_env(
4002 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4003 char_u *dst, /* where to put the result */
4004 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004006 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007}
4008
4009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004010expand_env_esc(
4011 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4012 char_u *dst, /* where to put the result */
4013 int dstlen, /* maximum length of the result */
4014 int esc, /* escape spaces in expanded variables */
4015 int one, /* "srcp" is one file name */
4016 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004018 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 char_u *tail;
4020 int c;
4021 char_u *var;
4022 int copy_char;
4023 int mustfree; /* var was allocated, need to free it later */
4024 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004025 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004027 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004028 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004029
4030 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 --dstlen; /* leave one char space for "\," */
4032 while (*src && dstlen > 0)
4033 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004034#ifdef FEAT_EVAL
4035 /* Skip over `=expr`. */
4036 if (src[0] == '`' && src[1] == '=')
4037 {
4038 size_t len;
4039
4040 var = src;
4041 src += 2;
4042 (void)skip_expr(&src);
4043 if (*src == '`')
4044 ++src;
4045 len = src - var;
4046 if (len > (size_t)dstlen)
4047 len = dstlen;
4048 vim_strncpy(dst, var, len);
4049 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004050 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004051 continue;
4052 }
4053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004055 if ((*src == '$'
4056#ifdef VMS
4057 && at_start
4058#endif
4059 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004060#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 || *src == '%'
4062#endif
4063 || (*src == '~' && at_start))
4064 {
4065 mustfree = FALSE;
4066
4067 /*
4068 * The variable name is copied into dst temporarily, because it may
4069 * be a string in read-only memory and a NUL needs to be appended.
4070 */
4071 if (*src != '~') /* environment var */
4072 {
4073 tail = src + 1;
4074 var = dst;
4075 c = dstlen - 1;
4076
4077#ifdef UNIX
4078 /* Unix has ${var-name} type environment vars */
4079 if (*tail == '{' && !vim_isIDc('{'))
4080 {
4081 tail++; /* ignore '{' */
4082 while (c-- > 0 && *tail && *tail != '}')
4083 *var++ = *tail++;
4084 }
4085 else
4086#endif
4087 {
4088 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004089#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 || (*src == '%' && *tail != '%')
4091#endif
4092 ))
4093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 }
4097
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004098#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099# ifdef UNIX
4100 if (src[1] == '{' && *tail != '}')
4101# else
4102 if (*src == '%' && *tail != '%')
4103# endif
4104 var = NULL;
4105 else
4106 {
4107# ifdef UNIX
4108 if (src[1] == '{')
4109# else
4110 if (*src == '%')
4111#endif
4112 ++tail;
4113#endif
4114 *var = NUL;
4115 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004116#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118#endif
4119 }
4120 /* home directory */
4121 else if ( src[1] == NUL
4122 || vim_ispathsep(src[1])
4123 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4124 {
4125 var = homedir;
4126 tail = src + 1;
4127 }
4128 else /* user directory */
4129 {
4130#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4131 /*
4132 * Copy ~user to dst[], so we can put a NUL after it.
4133 */
4134 tail = src;
4135 var = dst;
4136 c = dstlen - 1;
4137 while ( c-- > 0
4138 && *tail
4139 && vim_isfilec(*tail)
4140 && !vim_ispathsep(*tail))
4141 *var++ = *tail++;
4142 *var = NUL;
4143# ifdef UNIX
4144 /*
4145 * If the system supports getpwnam(), use it.
4146 * Otherwise, or if getpwnam() fails, the shell is used to
4147 * expand ~user. This is slower and may fail if the shell
4148 * does not support ~user (old versions of /bin/sh).
4149 */
4150# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4151 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004152 /* Note: memory allocated by getpwnam() is never freed.
4153 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004154 struct passwd *pw = (*dst == NUL)
4155 ? NULL : getpwnam((char *)dst + 1);
4156
4157 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159 if (var == NULL)
4160# endif
4161 {
4162 expand_T xpc;
4163
4164 ExpandInit(&xpc);
4165 xpc.xp_context = EXPAND_FILES;
4166 var = ExpandOne(&xpc, dst, NULL,
4167 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 mustfree = TRUE;
4169 }
4170
4171# else /* !UNIX, thus VMS */
4172 /*
4173 * USER_HOME is a comma-separated list of
4174 * directories to search for the user account in.
4175 */
4176 {
4177 char_u test[MAXPATHL], paths[MAXPATHL];
4178 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004179 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181 STRCPY(paths, USER_HOME);
4182 next_path = paths;
4183 while (*next_path)
4184 {
4185 for (path = next_path; *next_path && *next_path != ',';
4186 next_path++);
4187 if (*next_path)
4188 *next_path++ = NUL;
4189 STRCPY(test, path);
4190 STRCAT(test, "/");
4191 STRCAT(test, dst + 1);
4192 if (mch_stat(test, &st) == 0)
4193 {
4194 var = alloc(STRLEN(test) + 1);
4195 STRCPY(var, test);
4196 mustfree = TRUE;
4197 break;
4198 }
4199 }
4200 }
4201# endif /* UNIX */
4202#else
4203 /* cannot expand user's home directory, so don't try */
4204 var = NULL;
4205 tail = (char_u *)""; /* for gcc */
4206#endif /* UNIX || VMS */
4207 }
4208
4209#ifdef BACKSLASH_IN_FILENAME
4210 /* If 'shellslash' is set change backslashes to forward slashes.
4211 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4212 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4213 {
4214 char_u *p = vim_strsave(var);
4215
4216 if (p != NULL)
4217 {
4218 if (mustfree)
4219 vim_free(var);
4220 var = p;
4221 mustfree = TRUE;
4222 forward_slash(var);
4223 }
4224 }
4225#endif
4226
4227 /* If "var" contains white space, escape it with a backslash.
4228 * Required for ":e ~/tt" when $HOME includes a space. */
4229 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4230 {
4231 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4232
4233 if (p != NULL)
4234 {
4235 if (mustfree)
4236 vim_free(var);
4237 var = p;
4238 mustfree = TRUE;
4239 }
4240 }
4241
4242 if (var != NULL && *var != NUL
4243 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4244 {
4245 STRCPY(dst, var);
4246 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 /* if var[] ends in a path separator and tail[] starts
4249 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004250 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4252 && dst[-1] != ':'
4253#endif
4254 && vim_ispathsep(*tail))
4255 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004256 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 src = tail;
4258 copy_char = FALSE;
4259 }
4260 if (mustfree)
4261 vim_free(var);
4262 }
4263
4264 if (copy_char) /* copy at least one char */
4265 {
4266 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004267 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004268 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4269 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 */
4271 at_start = FALSE;
4272 if (src[0] == '\\' && src[1] != NUL)
4273 {
4274 *dst++ = *src++;
4275 --dstlen;
4276 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004277 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004279 if (dstlen > 0)
4280 {
4281 *dst++ = *src++;
4282 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004283
Bram Moolenaar1c864092017-08-06 18:15:45 +02004284 if (startstr != NULL && src - startstr_len >= srcp
4285 && STRNCMP(src - startstr_len, startstr,
4286 startstr_len) == 0)
4287 at_start = TRUE;
4288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 *dst = NUL;
4293}
4294
4295/*
4296 * Vim's version of getenv().
4297 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004298 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004299 * "mustfree" is set to TRUE when returned is allocated, it must be
4300 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
4302 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004303vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
4305 char_u *p;
4306 char_u *pend;
4307 int vimruntime;
4308
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004309#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 /* use "C:/" when $HOME is not set */
4311 if (STRCMP(name, "HOME") == 0)
4312 return homedir;
4313#endif
4314
4315 p = mch_getenv(name);
4316 if (p != NULL && *p == NUL) /* empty is the same as not set */
4317 p = NULL;
4318
4319 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004320 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004321#if defined(MSWIN)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004322 if (enc_utf8)
4323 {
4324 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004325 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004326
4327 /* Convert from active codepage to UTF-8. Other conversions are
4328 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004329 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004330 if (pp != NULL)
4331 {
4332 p = pp;
4333 *mustfree = TRUE;
4334 }
4335 }
4336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4341 if (!vimruntime && STRCMP(name, "VIM") != 0)
4342 return NULL;
4343
4344 /*
4345 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4346 * Don't do this when default_vimruntime_dir is non-empty.
4347 */
4348 if (vimruntime
4349#ifdef HAVE_PATHDEF
4350 && *default_vimruntime_dir == NUL
4351#endif
4352 )
4353 {
4354 p = mch_getenv((char_u *)"VIM");
4355 if (p != NULL && *p == NUL) /* empty is the same as not set */
4356 p = NULL;
4357 if (p != NULL)
4358 {
4359 p = vim_version_dir(p);
4360 if (p != NULL)
4361 *mustfree = TRUE;
4362 else
4363 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004364
Bram Moolenaar4f974752019-02-17 17:44:42 +01004365#if defined(MSWIN)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004366 if (enc_utf8)
4367 {
4368 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004369 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004370
4371 /* Convert from active codepage to UTF-8. Other conversions
4372 * are not done, because they would fail for non-ASCII
4373 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004374 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004375 if (pp != NULL)
4376 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004377 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004378 vim_free(p);
4379 p = pp;
4380 *mustfree = TRUE;
4381 }
4382 }
4383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385 }
4386
4387 /*
4388 * When expanding $VIM or $VIMRUNTIME fails, try using:
4389 * - the directory name from 'helpfile' (unless it contains '$')
4390 * - the executable name from argv[0]
4391 */
4392 if (p == NULL)
4393 {
4394 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4395 p = p_hf;
4396#ifdef USE_EXE_NAME
4397 /*
4398 * Use the name of the executable, obtained from argv[0].
4399 */
4400 else
4401 p = exe_name;
4402#endif
4403 if (p != NULL)
4404 {
4405 /* remove the file name */
4406 pend = gettail(p);
4407
4408 /* remove "doc/" from 'helpfile', if present */
4409 if (p == p_hf)
4410 pend = remove_tail(p, pend, (char_u *)"doc");
4411
4412#ifdef USE_EXE_NAME
4413# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004414 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (p == exe_name)
4416 {
4417 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004418 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004420 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4421 if (pend1 != pend)
4422 {
4423 pnew = alloc((unsigned)(pend1 - p) + 15);
4424 if (pnew != NULL)
4425 {
4426 STRNCPY(pnew, p, (pend1 - p));
4427 STRCPY(pnew + (pend1 - p), "Resources/vim");
4428 p = pnew;
4429 pend = p + STRLEN(p);
4430 }
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433# endif
4434 /* remove "src/" from exe_name, if present */
4435 if (p == exe_name)
4436 pend = remove_tail(p, pend, (char_u *)"src");
4437#endif
4438
4439 /* for $VIM, remove "runtime/" or "vim54/", if present */
4440 if (!vimruntime)
4441 {
4442 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4443 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4444 }
4445
4446 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004447 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004450#ifdef MACOS_X
4451 if (p == exe_name || p == p_hf)
4452#endif
4453 /* check that the result is a directory name */
4454 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004457 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 else
4459 {
4460#ifdef USE_EXE_NAME
4461 /* may add "/vim54" or "/runtime" if it exists */
4462 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4463 {
4464 vim_free(p);
4465 p = pend;
4466 }
4467#endif
4468 *mustfree = TRUE;
4469 }
4470 }
4471 }
4472
4473#ifdef HAVE_PATHDEF
4474 /* When there is a pathdef.c file we can use default_vim_dir and
4475 * default_vimruntime_dir */
4476 if (p == NULL)
4477 {
4478 /* Only use default_vimruntime_dir when it is not empty */
4479 if (vimruntime && *default_vimruntime_dir != NUL)
4480 {
4481 p = default_vimruntime_dir;
4482 *mustfree = FALSE;
4483 }
4484 else if (*default_vim_dir != NUL)
4485 {
4486 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4487 *mustfree = TRUE;
4488 else
4489 {
4490 p = default_vim_dir;
4491 *mustfree = FALSE;
4492 }
4493 }
4494 }
4495#endif
4496
4497 /*
4498 * Set the environment variable, so that the new value can be found fast
4499 * next time, and others can also use it (e.g. Perl).
4500 */
4501 if (p != NULL)
4502 {
4503 if (vimruntime)
4504 {
4505 vim_setenv((char_u *)"VIMRUNTIME", p);
4506 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 else
4509 {
4510 vim_setenv((char_u *)"VIM", p);
4511 didset_vim = TRUE;
4512 }
4513 }
4514 return p;
4515}
4516
4517/*
4518 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4519 * Return NULL if not, return its name in allocated memory otherwise.
4520 */
4521 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004522vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523{
4524 char_u *p;
4525
4526 if (vimdir == NULL || *vimdir == NUL)
4527 return NULL;
4528 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4529 if (p != NULL && mch_isdir(p))
4530 return p;
4531 vim_free(p);
4532 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4533 if (p != NULL && mch_isdir(p))
4534 return p;
4535 vim_free(p);
4536 return NULL;
4537}
4538
4539/*
4540 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4541 * the length of "name/". Otherwise return "pend".
4542 */
4543 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004544remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 int len = (int)STRLEN(name) + 1;
4547 char_u *newend = pend - len;
4548
4549 if (newend >= p
4550 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004551 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 return newend;
4553 return pend;
4554}
4555
Bram Moolenaar113e1072019-01-20 15:30:40 +01004556#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02004557 void
4558vim_unsetenv(char_u *var)
4559{
4560#ifdef HAVE_UNSETENV
4561 unsetenv((char *)var);
4562#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004563 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004564#endif
4565}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004566#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02004567
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 * Our portable version of setenv.
4571 */
4572 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004573vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574{
4575#ifdef HAVE_SETENV
4576 mch_setenv((char *)name, (char *)val, 1);
4577#else
4578 char_u *envbuf;
4579
4580 /*
4581 * Putenv does not copy the string, it has to remain
4582 * valid. The allocated memory will never be freed.
4583 */
4584 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4585 if (envbuf != NULL)
4586 {
4587 sprintf((char *)envbuf, "%s=%s", name, val);
4588 putenv((char *)envbuf);
4589 }
4590#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004591#ifdef FEAT_GETTEXT
4592 /*
4593 * When setting $VIMRUNTIME adjust the directory to find message
4594 * translations to $VIMRUNTIME/lang.
4595 */
4596 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4597 {
4598 char_u *buf = concat_str(val, (char_u *)"/lang");
4599
4600 if (buf != NULL)
4601 {
4602 bindtextdomain(VIMPACKAGE, (char *)buf);
4603 vim_free(buf);
4604 }
4605 }
4606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607}
4608
4609#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4610/*
4611 * Function given to ExpandGeneric() to obtain an environment variable name.
4612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004614get_env_name(
4615 expand_T *xp UNUSED,
4616 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
Bram Moolenaard0573012017-10-28 21:11:06 +02004618# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004620 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 */
4622 return NULL;
4623# else
4624# ifndef __WIN32__
4625 /* Borland C++ 5.2 has this in a header file. */
4626 extern char **environ;
4627# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004628# define ENVNAMELEN 100
4629 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 char_u *str;
4631 int n;
4632
4633 str = (char_u *)environ[idx];
4634 if (str == NULL)
4635 return NULL;
4636
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004637 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
4639 if (str[n] == '=' || str[n] == NUL)
4640 break;
4641 name[n] = str[n];
4642 }
4643 name[n] = NUL;
4644 return name;
4645# endif
4646}
Bram Moolenaar24305862012-08-15 14:05:05 +02004647
4648/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004649 * Add a user name to the list of users in ga_users.
4650 * Do nothing if user name is NULL or empty.
4651 */
4652 static void
4653add_user(char_u *user, int need_copy)
4654{
4655 char_u *user_copy = (user != NULL && need_copy)
4656 ? vim_strsave(user) : user;
4657
4658 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4659 {
4660 if (need_copy)
4661 vim_free(user);
4662 return;
4663 }
4664 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4665}
4666
4667/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004668 * Find all user names for user completion.
4669 * Done only once and then cached.
4670 */
4671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004672init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004673{
Bram Moolenaar24305862012-08-15 14:05:05 +02004674 static int lazy_init_done = FALSE;
4675
4676 if (lazy_init_done)
4677 return;
4678
4679 lazy_init_done = TRUE;
4680 ga_init2(&ga_users, sizeof(char_u *), 20);
4681
4682# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4683 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004684 struct passwd* pw;
4685
4686 setpwent();
4687 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004688 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004689 endpwent();
4690 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004691# elif defined(MSWIN)
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004692 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004693 DWORD nusers = 0, ntotal = 0, i;
4694 PUSER_INFO_0 uinfo;
4695
4696 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4697 &nusers, &ntotal, NULL) == NERR_Success)
4698 {
4699 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004700 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004701
4702 NetApiBufferFree(uinfo);
4703 }
4704 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004705# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004706# if defined(HAVE_GETPWNAM)
4707 {
4708 char_u *user_env = mch_getenv((char_u *)"USER");
4709
4710 // The $USER environment variable may be a valid remote user name (NIS,
4711 // LDAP) not already listed by getpwent(), as getpwent() only lists
4712 // local user names. If $USER is not already listed, check whether it
4713 // is a valid remote user name using getpwnam() and if it is, add it to
4714 // the list of user names.
4715
4716 if (user_env != NULL && *user_env != NUL)
4717 {
4718 int i;
4719
4720 for (i = 0; i < ga_users.ga_len; i++)
4721 {
4722 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4723
4724 if (STRCMP(local_user, user_env) == 0)
4725 break;
4726 }
4727
4728 if (i == ga_users.ga_len)
4729 {
4730 struct passwd *pw = getpwnam((char *)user_env);
4731
4732 if (pw != NULL)
4733 add_user((char_u *)pw->pw_name, TRUE);
4734 }
4735 }
4736 }
4737# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004738}
4739
4740/*
4741 * Function given to ExpandGeneric() to obtain an user names.
4742 */
4743 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004744get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004745{
4746 init_users();
4747 if (idx < ga_users.ga_len)
4748 return ((char_u **)ga_users.ga_data)[idx];
4749 return NULL;
4750}
4751
4752/*
4753 * Check whether name matches a user name. Return:
4754 * 0 if name does not match any user name.
4755 * 1 if name partially matches the beginning of a user name.
4756 * 2 is name fully matches a user name.
4757 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004758 int
4759match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004760{
4761 int i;
4762 int n = (int)STRLEN(name);
4763 int result = 0;
4764
4765 init_users();
4766 for (i = 0; i < ga_users.ga_len; i++)
4767 {
4768 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4769 return 2; /* full match */
4770 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4771 result = 1; /* partial match */
4772 }
4773 return result;
4774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775#endif
4776
4777/*
4778 * Replace home directory by "~" in each space or comma separated file name in
4779 * 'src'.
4780 * If anything fails (except when out of space) dst equals src.
4781 */
4782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004783home_replace(
4784 buf_T *buf, /* when not NULL, check for help files */
4785 char_u *src, /* input file name */
4786 char_u *dst, /* where to put the result */
4787 int dstlen, /* maximum length of the result */
4788 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 spaces and commas in the file name. */
4790{
4791 size_t dirlen = 0, envlen = 0;
4792 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004793 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 char_u *p;
4795
4796 if (src == NULL)
4797 {
4798 *dst = NUL;
4799 return;
4800 }
4801
4802 /*
4803 * If the file is a help file, remove the path completely.
4804 */
4805 if (buf != NULL && buf->b_help)
4806 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004807 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 return;
4809 }
4810
4811 /*
4812 * We check both the value of the $HOME environment variable and the
4813 * "real" home directory.
4814 */
4815 if (homedir != NULL)
4816 dirlen = STRLEN(homedir);
4817
4818#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004819 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004821 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4822#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004823#ifdef MSWIN
Bram Moolenaar48340b62017-08-29 22:08:53 +02004824 if (homedir_env == NULL)
4825 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4826#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004827 /* Empty is the same as not set. */
4828 if (homedir_env != NULL && *homedir_env == NUL)
4829 homedir_env = NULL;
4830
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004831#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004832 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004833 {
4834 int usedlen = 0;
4835 int flen;
4836 char_u *fbuf = NULL;
4837
4838 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004839 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004840 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004841 flen = (int)STRLEN(homedir_env);
4842 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4843 /* Remove the trailing / that is added to a directory. */
4844 homedir_env[flen - 1] = NUL;
4845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#endif
4847
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (homedir_env != NULL)
4849 envlen = STRLEN(homedir_env);
4850
4851 if (!one)
4852 src = skipwhite(src);
4853 while (*src && dstlen > 0)
4854 {
4855 /*
4856 * Here we are at the beginning of a file name.
4857 * First, check to see if the beginning of the file name matches
4858 * $HOME or the "real" home directory. Check that there is a '/'
4859 * after the match (so that if e.g. the file is "/home/pieter/bla",
4860 * and the home directory is "/home/piet", the file does not end up
4861 * as "~er/bla" (which would seem to indicate the file "bla" in user
4862 * er's home directory)).
4863 */
4864 p = homedir;
4865 len = dirlen;
4866 for (;;)
4867 {
4868 if ( len
4869 && fnamencmp(src, p, len) == 0
4870 && (vim_ispathsep(src[len])
4871 || (!one && (src[len] == ',' || src[len] == ' '))
4872 || src[len] == NUL))
4873 {
4874 src += len;
4875 if (--dstlen > 0)
4876 *dst++ = '~';
4877
4878 /*
4879 * If it's just the home directory, add "/".
4880 */
4881 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4882 *dst++ = '/';
4883 break;
4884 }
4885 if (p == homedir_env)
4886 break;
4887 p = homedir_env;
4888 len = envlen;
4889 }
4890
4891 /* if (!one) skip to separator: space or comma */
4892 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4893 *dst++ = *src++;
4894 /* skip separator */
4895 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4896 *dst++ = *src++;
4897 }
4898 /* if (dstlen == 0) out of space, what to do??? */
4899
4900 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004901
4902 if (homedir_env != homedir_env_orig)
4903 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904}
4905
4906/*
4907 * Like home_replace, store the replaced string in allocated memory.
4908 * When something fails, NULL is returned.
4909 */
4910 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004911home_replace_save(
4912 buf_T *buf, /* when not NULL, check for help files */
4913 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 char_u *dst;
4916 unsigned len;
4917
4918 len = 3; /* space for "~/" and trailing NUL */
4919 if (src != NULL) /* just in case */
4920 len += (unsigned)STRLEN(src);
4921 dst = alloc(len);
4922 if (dst != NULL)
4923 home_replace(buf, src, dst, len, TRUE);
4924 return dst;
4925}
4926
4927/*
4928 * Compare two file names and return:
4929 * FPC_SAME if they both exist and are the same file.
4930 * FPC_SAMEX if they both don't exist and have the same file name.
4931 * FPC_DIFF if they both exist and are different files.
4932 * FPC_NOTX if they both don't exist.
4933 * FPC_DIFFX if one of them doesn't exist.
4934 * For the first name environment variables are expanded
4935 */
4936 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004937fullpathcmp(
4938 char_u *s1,
4939 char_u *s2,
4940 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942#ifdef UNIX
4943 char_u exp1[MAXPATHL];
4944 char_u full1[MAXPATHL];
4945 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004946 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 int r1, r2;
4948
4949 expand_env(s1, exp1, MAXPATHL);
4950 r1 = mch_stat((char *)exp1, &st1);
4951 r2 = mch_stat((char *)s2, &st2);
4952 if (r1 != 0 && r2 != 0)
4953 {
4954 /* if mch_stat() doesn't work, may compare the names */
4955 if (checkname)
4956 {
4957 if (fnamecmp(exp1, s2) == 0)
4958 return FPC_SAMEX;
4959 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4960 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4961 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4962 return FPC_SAMEX;
4963 }
4964 return FPC_NOTX;
4965 }
4966 if (r1 != 0 || r2 != 0)
4967 return FPC_DIFFX;
4968 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4969 return FPC_SAME;
4970 return FPC_DIFF;
4971#else
4972 char_u *exp1; /* expanded s1 */
4973 char_u *full1; /* full path of s1 */
4974 char_u *full2; /* full path of s2 */
4975 int retval = FPC_DIFF;
4976 int r1, r2;
4977
4978 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4979 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4980 {
4981 full1 = exp1 + MAXPATHL;
4982 full2 = full1 + MAXPATHL;
4983
4984 expand_env(s1, exp1, MAXPATHL);
4985 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4986 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4987
4988 /* If vim_FullName() fails, the file probably doesn't exist. */
4989 if (r1 != OK && r2 != OK)
4990 {
4991 if (checkname && fnamecmp(exp1, s2) == 0)
4992 retval = FPC_SAMEX;
4993 else
4994 retval = FPC_NOTX;
4995 }
4996 else if (r1 != OK || r2 != OK)
4997 retval = FPC_DIFFX;
4998 else if (fnamecmp(full1, full2))
4999 retval = FPC_DIFF;
5000 else
5001 retval = FPC_SAME;
5002 vim_free(exp1);
5003 }
5004 return retval;
5005#endif
5006}
5007
5008/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005009 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005010 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005011 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 */
5013 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005014gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
5016 char_u *p1, *p2;
5017
5018 if (fname == NULL)
5019 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005020 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005022 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005024 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 return p1;
5027}
5028
5029/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005030 * Get pointer to tail of "fname", including path separators. Putting a NUL
5031 * here leaves the directory name. Takes care of "c:/" and "//".
5032 * Always returns a valid pointer.
5033 */
5034 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005035gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005036{
5037 char_u *p;
5038 char_u *t;
5039
5040 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5041 t = gettail(fname);
5042 while (t > p && after_pathsep(fname, t))
5043 --t;
5044#ifdef VMS
5045 /* path separator is part of the path */
5046 ++t;
5047#endif
5048 return t;
5049}
5050
5051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 * get the next path component (just after the next path separator).
5053 */
5054 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005055getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005058 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (*fname)
5060 ++fname;
5061 return fname;
5062}
5063
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064/*
5065 * Get a pointer to one character past the head of a path name.
5066 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5067 * If there is no head, path is returned.
5068 */
5069 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005070get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
5072 char_u *retval;
5073
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005074#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /* may skip "c:" */
5076 if (isalpha(path[0]) && path[1] == ':')
5077 retval = path + 2;
5078 else
5079 retval = path;
5080#else
5081# if defined(AMIGA)
5082 /* may skip "label:" */
5083 retval = vim_strchr(path, ':');
5084 if (retval == NULL)
5085 retval = path;
5086# else /* Unix */
5087 retval = path;
5088# endif
5089#endif
5090
5091 while (vim_ispathsep(*retval))
5092 ++retval;
5093
5094 return retval;
5095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
5097/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005098 * Return TRUE if 'c' is a path separator.
5099 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
5101 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005102vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005104#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005106#else
5107# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005109# else
5110# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5112 return (c == ':' || c == '[' || c == ']' || c == '/'
5113 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005114# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005116# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119}
5120
Bram Moolenaar69c35002013-11-04 02:54:12 +01005121/*
5122 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5123 */
5124 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005125vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005126{
5127 return vim_ispathsep(c)
5128#ifdef BACKSLASH_IN_FILENAME
5129 && c != ':'
5130#endif
5131 ;
5132}
5133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005134/*
5135 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5136 * It's done in-place.
5137 */
5138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005139shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005140{
5141 char_u *tail, *s, *d;
5142 int skip = FALSE;
5143
5144 tail = gettail(str);
5145 d = str;
5146 for (s = str; ; ++s)
5147 {
5148 if (s >= tail) /* copy the whole tail */
5149 {
5150 *d++ = *s;
5151 if (*s == NUL)
5152 break;
5153 }
5154 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5155 {
5156 *d++ = *s;
5157 skip = FALSE;
5158 }
5159 else if (!skip)
5160 {
5161 *d++ = *s; /* copy next char */
5162 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5163 skip = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005164 if (has_mbyte)
5165 {
5166 int l = mb_ptr2len(s);
5167
5168 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005169 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005170 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005171 }
5172 }
5173}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005174
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005175/*
5176 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5177 * Also returns TRUE if there is no directory name.
5178 * "fname" must be writable!.
5179 */
5180 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005182{
5183 char_u *p;
5184 int c;
5185 int retval;
5186
5187 p = gettail_sep(fname);
5188 if (p == fname)
5189 return TRUE;
5190 c = *p;
5191 *p = NUL;
5192 retval = mch_isdir(fname);
5193 *p = c;
5194 return retval;
5195}
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005198 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5199 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 */
5201 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005204#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005206#else
5207 if (p_fic)
5208 return MB_STRICMP(x, y);
5209 return STRCMP(x, y);
5210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211}
5212
5213 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005214vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005216#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005217 char_u *px = x;
5218 char_u *py = y;
5219 int cx = NUL;
5220 int cy = NUL;
5221
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005222 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005224 cx = PTR2CHAR(px);
5225 cy = PTR2CHAR(py);
5226 if (cx == NUL || cy == NUL
5227 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5228 && !(cx == '/' && cy == '\\')
5229 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005231 len -= MB_PTR2LEN(px);
5232 px += MB_PTR2LEN(px);
5233 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235 if (len == 0)
5236 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005237 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005238#else
5239 if (p_fic)
5240 return MB_STRNICMP(x, y, len);
5241 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245/*
5246 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005247 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 */
5249 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 char_u *dest;
5253
5254 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5255 if (dest != NULL)
5256 {
5257 STRCPY(dest, fname1);
5258 if (sep)
5259 add_pathsep(dest);
5260 STRCAT(dest, fname2);
5261 }
5262 return dest;
5263}
5264
Bram Moolenaard6754642005-01-17 22:18:45 +00005265/*
5266 * Concatenate two strings and return the result in allocated memory.
5267 * Returns NULL when out of memory.
5268 */
5269 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005270concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005271{
5272 char_u *dest;
5273 size_t l = STRLEN(str1);
5274
5275 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5276 if (dest != NULL)
5277 {
5278 STRCPY(dest, str1);
5279 STRCPY(dest + l, str2);
5280 }
5281 return dest;
5282}
Bram Moolenaard6754642005-01-17 22:18:45 +00005283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284/*
5285 * Add a path separator to a file name, unless it already ends in a path
5286 * separator.
5287 */
5288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005289add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005291 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 STRCAT(p, PATHSEPSTR);
5293}
5294
5295/*
5296 * FullName_save - Make an allocated copy of a full file name.
5297 * Returns NULL when out of memory.
5298 */
5299 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005300FullName_save(
5301 char_u *fname,
5302 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005303 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
5305 char_u *buf;
5306 char_u *new_fname = NULL;
5307
5308 if (fname == NULL)
5309 return NULL;
5310
5311 buf = alloc((unsigned)MAXPATHL);
5312 if (buf != NULL)
5313 {
5314 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5315 new_fname = vim_strsave(buf);
5316 else
5317 new_fname = vim_strsave(fname);
5318 vim_free(buf);
5319 }
5320 return new_fname;
5321}
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005324prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005326#if defined(SIGHUP) && defined(SIG_IGN)
5327 /* Ignore SIGHUP, because a dropped connection causes a read error, which
5328 * makes Vim exit and then handling SIGHUP causes various reentrance
5329 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005330 signal(SIGHUP, SIG_IGN);
5331#endif
5332
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333#ifdef FEAT_GUI
5334 if (gui.in_use)
5335 {
5336 gui.dying = TRUE;
5337 out_trash(); /* trash any pending output */
5338 }
5339 else
5340#endif
5341 {
5342 windgoto((int)Rows - 1, 0);
5343
5344 /*
5345 * Switch terminal mode back now, so messages end up on the "normal"
5346 * screen (if there are two screens).
5347 */
5348 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005349 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 out_flush();
5351 }
5352}
5353
5354/*
5355 * Preserve files and exit.
5356 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02005357 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
5358 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 */
5360 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005361preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
5363 buf_T *buf;
5364
5365 prepare_to_exit();
5366
Bram Moolenaar4770d092006-01-12 23:22:24 +00005367 /* Setting this will prevent free() calls. That avoids calling free()
5368 * recursively when free() was invoked with a bad pointer. */
5369 really_exiting = TRUE;
5370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 out_str(IObuff);
5372 screen_start(); /* don't know where cursor is now */
5373 out_flush();
5374
5375 ml_close_notmod(); /* close all not-modified buffers */
5376
Bram Moolenaar29323592016-07-24 22:04:11 +02005377 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
5380 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02005381 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 screen_start(); /* don't know where cursor is now */
5383 out_flush();
5384 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
5385 break;
5386 }
5387 }
5388
5389 ml_close_all(FALSE); /* close all memfiles, without deleting */
5390
Bram Moolenaarbec9c202013-09-05 21:41:39 +02005391 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
5393 getout(1);
5394}
5395
5396/*
5397 * return TRUE if "fname" exists.
5398 */
5399 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005400vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
Bram Moolenaar8767f522016-07-01 17:17:39 +02005402 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
5404 if (mch_stat((char *)fname, &st))
5405 return FALSE;
5406 return TRUE;
5407}
5408
5409/*
5410 * Check for CTRL-C pressed, but only once in a while.
5411 * Should be used instead of ui_breakcheck() for functions that check for
5412 * each line in the file. Calling ui_breakcheck() each time takes too much
5413 * time, because it can be a system call.
5414 */
5415
5416#ifndef BREAKCHECK_SKIP
5417# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
5418# define BREAKCHECK_SKIP 200
5419# else
5420# define BREAKCHECK_SKIP 32
5421# endif
5422#endif
5423
5424static int breakcheck_count = 0;
5425
5426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005427line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 if (++breakcheck_count >= BREAKCHECK_SKIP)
5430 {
5431 breakcheck_count = 0;
5432 ui_breakcheck();
5433 }
5434}
5435
5436/*
5437 * Like line_breakcheck() but check 10 times less often.
5438 */
5439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005440fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
5443 {
5444 breakcheck_count = 0;
5445 ui_breakcheck();
5446 }
5447}
5448
5449/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00005450 * Invoke expand_wildcards() for one pattern.
5451 * Expand items like "%:h" before the expansion.
5452 * Returns OK or FAIL.
5453 */
5454 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005455expand_wildcards_eval(
5456 char_u **pat, /* pointer to input pattern */
5457 int *num_file, /* resulting number of files */
5458 char_u ***file, /* array of resulting files */
5459 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00005460{
5461 int ret = FAIL;
5462 char_u *eval_pat = NULL;
5463 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005464 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00005465 int usedlen;
5466
5467 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
5468 {
5469 ++emsg_off;
5470 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
5471 NULL, &ignored_msg, NULL);
5472 --emsg_off;
5473 if (eval_pat != NULL)
5474 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
5475 }
5476
5477 if (exp_pat != NULL)
5478 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
5479
5480 if (eval_pat != NULL)
5481 {
5482 vim_free(exp_pat);
5483 vim_free(eval_pat);
5484 }
5485
5486 return ret;
5487}
5488
5489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
5491 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005492 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 */
5494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005495expand_wildcards(
5496 int num_pat, /* number of input patterns */
5497 char_u **pat, /* array of input patterns */
5498 int *num_files, /* resulting number of files */
5499 char_u ***files, /* array of resulting files */
5500 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 int retval;
5503 int i, j;
5504 char_u *p;
5505 int non_suf_match; /* number without matching suffix */
5506
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005507 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02005510 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 return retval;
5512
5513#ifdef FEAT_WILDIGN
5514 /*
5515 * Remove names that match 'wildignore'.
5516 */
5517 if (*p_wig)
5518 {
5519 char_u *ffname;
5520
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005521 /* check all files in (*files)[] */
5522 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005524 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 if (ffname == NULL) /* out of memory */
5526 break;
5527# ifdef VMS
5528 vms_remove_version(ffname);
5529# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005530 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01005532 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005533 vim_free((*files)[i]);
5534 for (j = i; j + 1 < *num_files; ++j)
5535 (*files)[j] = (*files)[j + 1];
5536 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 --i;
5538 }
5539 vim_free(ffname);
5540 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005541
5542 /* If the number of matches is now zero, we fail. */
5543 if (*num_files == 0)
5544 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01005545 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005546 return FAIL;
5547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549#endif
5550
5551 /*
5552 * Move the names where 'suffixes' match to the end.
5553 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005554 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
5556 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005557 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005559 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
5561 /*
5562 * Move the name without matching suffix to the front
5563 * of the list.
5564 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005565 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02005567 (*files)[j] = (*files)[j - 1];
5568 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 }
5571 }
5572
5573 return retval;
5574}
5575
5576/*
5577 * Return TRUE if "fname" matches with an entry in 'suffixes'.
5578 */
5579 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005580match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 int fnamelen, setsuflen;
5583 char_u *setsuf;
5584#define MAXSUFLEN 30 /* maximum length of a file suffix */
5585 char_u suf_buf[MAXSUFLEN];
5586
5587 fnamelen = (int)STRLEN(fname);
5588 setsuflen = 0;
5589 for (setsuf = p_su; *setsuf; )
5590 {
5591 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00005592 if (setsuflen == 0)
5593 {
5594 char_u *tail = gettail(fname);
5595
5596 /* empty entry: match name without a '.' */
5597 if (vim_strchr(tail, '.') == NULL)
5598 {
5599 setsuflen = 1;
5600 break;
5601 }
5602 }
5603 else
5604 {
5605 if (fnamelen >= setsuflen
5606 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
5607 (size_t)setsuflen) == 0)
5608 break;
5609 setsuflen = 0;
5610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612 return (setsuflen != 0);
5613}
5614
5615#if !defined(NO_EXPANDPATH) || defined(PROTO)
5616
5617# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005618static int vim_backtick(char_u *p);
5619static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620# endif
5621
Bram Moolenaar4f974752019-02-17 17:44:42 +01005622# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623/*
5624 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
5625 * it's shared between these systems.
5626 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005627# if defined(PROTO)
5628# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629# else
5630# ifdef __BORLANDC__
5631# define _cdecl _RTLENTRYF
5632# endif
5633# endif
5634
5635/*
5636 * comparison function for qsort in dos_expandpath()
5637 */
5638 static int _cdecl
5639pstrcmp(const void *a, const void *b)
5640{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005641 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00005645 * Recursively expand one path component into all matching files and/or
5646 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 * Return the number of matches found.
5648 * "path" has backslashes before chars that are not to be expanded, starting
5649 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00005650 * Return the number of matches found.
5651 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 */
5653 static int
5654dos_expandpath(
5655 garray_T *gap,
5656 char_u *path,
5657 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00005658 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00005659 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
Bram Moolenaar231334e2005-07-25 20:46:57 +00005661 char_u *buf;
5662 char_u *path_end;
5663 char_u *p, *s, *e;
5664 int start_len = gap->ga_len;
5665 char_u *pat;
5666 regmatch_T regmatch;
5667 int starts_with_dot;
5668 int matches;
5669 int len;
5670 int starstar = FALSE;
5671 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 WIN32_FIND_DATA fb;
5673 HANDLE hFind = (HANDLE)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 WIN32_FIND_DATAW wfb;
5675 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00005677 int ok;
5678
5679 /* Expanding "**" may take a long time, check for CTRL-C. */
5680 if (stardepth > 0)
5681 {
5682 ui_breakcheck();
5683 if (got_int)
5684 return 0;
5685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005687 // Make room for file name. When doing encoding conversion the actual
5688 // length may be quite a bit longer, thus use the maximum possible length.
Bram Moolenaar7314efd2015-10-31 15:32:52 +01005689 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (buf == NULL)
5691 return 0;
5692
5693 /*
5694 * Find the first part in the path name that contains a wildcard or a ~1.
5695 * Copy it into buf, including the preceding characters.
5696 */
5697 p = buf;
5698 s = buf;
5699 e = NULL;
5700 path_end = path;
5701 while (*path_end != NUL)
5702 {
5703 /* May ignore a wildcard that has a backslash before it; it will
5704 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
5705 if (path_end >= path + wildoff && rem_backslash(path_end))
5706 *p++ = *path_end++;
5707 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
5708 {
5709 if (e != NULL)
5710 break;
5711 s = p + 1;
5712 }
5713 else if (path_end >= path + wildoff
5714 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
5715 e = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (has_mbyte)
5717 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005718 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 STRNCPY(p, path_end, len);
5720 p += len;
5721 path_end += len;
5722 }
5723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 *p++ = *path_end++;
5725 }
5726 e = p;
5727 *e = NUL;
5728
5729 /* now we have one wildcard component between s and e */
5730 /* Remove backslashes between "wildoff" and the start of the wildcard
5731 * component. */
5732 for (p = buf + wildoff; p < s; ++p)
5733 if (rem_backslash(p))
5734 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005735 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 --e;
5737 --s;
5738 }
5739
Bram Moolenaar231334e2005-07-25 20:46:57 +00005740 /* Check for "**" between "s" and "e". */
5741 for (p = s; p < e; ++p)
5742 if (p[0] == '*' && p[1] == '*')
5743 starstar = TRUE;
5744
Bram Moolenaard82103e2016-01-17 17:04:05 +01005745 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
5747 if (pat == NULL)
5748 {
5749 vim_free(buf);
5750 return 0;
5751 }
5752
5753 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01005754 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02005755 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 regmatch.rm_ic = TRUE; /* Always ignore case */
5757 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01005758 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02005759 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 vim_free(pat);
5761
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01005762 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {
5764 vim_free(buf);
5765 return 0;
5766 }
5767
5768 /* remember the pattern or file name being looked for */
5769 matchname = vim_strsave(s);
5770
Bram Moolenaar231334e2005-07-25 20:46:57 +00005771 /* If "**" is by itself, this is the first time we encounter it and more
5772 * is following then find matches without any directory. */
5773 if (!didstar && stardepth < 100 && starstar && e - s == 2
5774 && *path_end == '/')
5775 {
5776 STRCPY(s, path_end + 1);
5777 ++stardepth;
5778 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
5779 --stardepth;
5780 }
5781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 /* Scan all files in the directory with "dir/ *.*" */
5783 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
5785 {
5786 /* The active codepage differs from 'encoding'. Attempt using the
5787 * wide function. If it fails because it is not implemented fall back
5788 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005789 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 if (wn != NULL)
5791 {
5792 hFind = FindFirstFileW(wn, &wfb);
5793 if (hFind == INVALID_HANDLE_VALUE
5794 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005795 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
5797 }
5798
5799 if (wn == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01005800 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
5803 while (ok)
5804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005806 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 /* Ignore entries starting with a dot, unless when asked for. Accept
5810 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01005811 if ((p[0] != '.' || starts_with_dot
5812 || ((flags & EW_DODOT)
5813 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01005815 || (regmatch.regprog != NULL
5816 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02005817 || ((flags & EW_NOTWILD)
5818 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00005822
5823 if (starstar && stardepth < 100)
5824 {
5825 /* For "**" in the pattern first go deeper in the tree to
5826 * find matches. */
5827 STRCPY(buf + len, "/**");
5828 STRCPY(buf + len + 3, path_end);
5829 ++stardepth;
5830 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
5831 --stardepth;
5832 }
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 STRCPY(buf + len, path_end);
5835 if (mch_has_exp_wildcard(path_end))
5836 {
5837 /* need to expand another component of the path */
5838 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00005839 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
5841 else
5842 {
5843 /* no more wildcards, check if there is a match */
5844 /* remove backslashes for the remaining components only */
5845 if (*path_end != 0)
5846 backslash_halve(buf + len + 1);
5847 if (mch_getperm(buf) >= 0) /* add existing file */
5848 addfile(gap, buf, flags);
5849 }
5850 }
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (wn != NULL)
5853 {
5854 vim_free(p);
5855 ok = FindNextFileW(hFind, &wfb);
5856 }
5857 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
5860 /* If no more matches and no match was used, try expanding the name
5861 * itself. Finds the long name of a short filename. */
5862 if (!ok && matchname != NULL && gap->ga_len == start_len)
5863 {
5864 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 if (wn != NULL)
5867 {
5868 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005869 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 if (wn != NULL)
5871 hFind = FindFirstFileW(wn, &wfb);
5872 }
5873 if (wn == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01005874 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +01005876 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 }
5878 }
5879
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 FindClose(hFind);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02005883 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 vim_free(matchname);
5885
5886 matches = gap->ga_len - start_len;
5887 if (matches > 0)
5888 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
5889 sizeof(char_u *), pstrcmp);
5890 return matches;
5891}
5892
5893 int
5894mch_expandpath(
5895 garray_T *gap,
5896 char_u *path,
5897 int flags) /* EW_* flags */
5898{
Bram Moolenaar231334e2005-07-25 20:46:57 +00005899 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900}
Bram Moolenaar4f974752019-02-17 17:44:42 +01005901# endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
Bram Moolenaar231334e2005-07-25 20:46:57 +00005903#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
5904 || defined(PROTO)
5905/*
5906 * Unix style wildcard expansion code.
5907 * It's here because it's used both for Unix and Mac.
5908 */
Bram Moolenaar231334e2005-07-25 20:46:57 +00005909 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005910pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +00005911{
5912 return (pathcmp(*(char **)a, *(char **)b, -1));
5913}
5914
5915/*
5916 * Recursively expand one path component into all matching files and/or
5917 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
5918 * "path" has backslashes before chars that are not to be expanded, starting
5919 * at "path + wildoff".
5920 * Return the number of matches found.
5921 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
5922 */
5923 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005924unix_expandpath(
5925 garray_T *gap,
5926 char_u *path,
5927 int wildoff,
5928 int flags, /* EW_* flags */
5929 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +00005930{
5931 char_u *buf;
5932 char_u *path_end;
5933 char_u *p, *s, *e;
5934 int start_len = gap->ga_len;
5935 char_u *pat;
5936 regmatch_T regmatch;
5937 int starts_with_dot;
5938 int matches;
5939 int len;
5940 int starstar = FALSE;
5941 static int stardepth = 0; /* depth for "**" expansion */
5942
5943 DIR *dirp;
5944 struct dirent *dp;
5945
5946 /* Expanding "**" may take a long time, check for CTRL-C. */
5947 if (stardepth > 0)
5948 {
5949 ui_breakcheck();
5950 if (got_int)
5951 return 0;
5952 }
5953
5954 /* make room for file name */
5955 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
5956 if (buf == NULL)
5957 return 0;
5958
5959 /*
5960 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02005961 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00005962 * Copy it into "buf", including the preceding characters.
5963 */
5964 p = buf;
5965 s = buf;
5966 e = NULL;
5967 path_end = path;
5968 while (*path_end != NUL)
5969 {
5970 /* May ignore a wildcard that has a backslash before it; it will
5971 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
5972 if (path_end >= path + wildoff && rem_backslash(path_end))
5973 *p++ = *path_end++;
5974 else if (*path_end == '/')
5975 {
5976 if (e != NULL)
5977 break;
5978 s = p + 1;
5979 }
5980 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02005981 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005982 || (!p_fic && (flags & EW_ICASE)
5983 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00005984 e = p;
Bram Moolenaar231334e2005-07-25 20:46:57 +00005985 if (has_mbyte)
5986 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005987 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00005988 STRNCPY(p, path_end, len);
5989 p += len;
5990 path_end += len;
5991 }
5992 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00005993 *p++ = *path_end++;
5994 }
5995 e = p;
5996 *e = NUL;
5997
Bram Moolenaar0b573a52011-07-27 17:31:47 +02005998 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00005999 /* Remove backslashes between "wildoff" and the start of the wildcard
6000 * component. */
6001 for (p = buf + wildoff; p < s; ++p)
6002 if (rem_backslash(p))
6003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006004 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00006005 --e;
6006 --s;
6007 }
6008
6009 /* Check for "**" between "s" and "e". */
6010 for (p = s; p < e; ++p)
6011 if (p[0] == '*' && p[1] == '*')
6012 starstar = TRUE;
6013
6014 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +01006015 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +00006016 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
6017 if (pat == NULL)
6018 {
6019 vim_free(buf);
6020 return 0;
6021 }
6022
6023 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01006024 if (flags & EW_ICASE)
6025 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
6026 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01006027 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01006028 if (flags & (EW_NOERROR | EW_NOTWILD))
6029 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00006030 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01006031 if (flags & (EW_NOERROR | EW_NOTWILD))
6032 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00006033 vim_free(pat);
6034
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01006035 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00006036 {
6037 vim_free(buf);
6038 return 0;
6039 }
6040
6041 /* If "**" is by itself, this is the first time we encounter it and more
6042 * is following then find matches without any directory. */
6043 if (!didstar && stardepth < 100 && starstar && e - s == 2
6044 && *path_end == '/')
6045 {
6046 STRCPY(s, path_end + 1);
6047 ++stardepth;
6048 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
6049 --stardepth;
6050 }
6051
6052 /* open the directory for scanning */
6053 *s = NUL;
6054 dirp = opendir(*buf == NUL ? "." : (char *)buf);
6055
6056 /* Find all matching entries */
6057 if (dirp != NULL)
6058 {
6059 for (;;)
6060 {
6061 dp = readdir(dirp);
6062 if (dp == NULL)
6063 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +01006064 if ((dp->d_name[0] != '.' || starts_with_dot
6065 || ((flags & EW_DODOT)
6066 && dp->d_name[1] != NUL
6067 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01006068 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
6069 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02006070 || ((flags & EW_NOTWILD)
6071 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00006072 {
6073 STRCPY(s, dp->d_name);
6074 len = STRLEN(buf);
6075
6076 if (starstar && stardepth < 100)
6077 {
6078 /* For "**" in the pattern first go deeper in the tree to
6079 * find matches. */
6080 STRCPY(buf + len, "/**");
6081 STRCPY(buf + len + 3, path_end);
6082 ++stardepth;
6083 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
6084 --stardepth;
6085 }
6086
6087 STRCPY(buf + len, path_end);
6088 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
6089 {
6090 /* need to expand another component of the path */
6091 /* remove backslashes for the remaining components only */
6092 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
6093 }
6094 else
6095 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02006096 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +01006097
Bram Moolenaar231334e2005-07-25 20:46:57 +00006098 /* no more wildcards, check if there is a match */
6099 /* remove backslashes for the remaining components only */
6100 if (*path_end != NUL)
6101 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +01006102 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +01006103 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +01006104 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00006105 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00006106#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00006107 size_t precomp_len = STRLEN(buf)+1;
6108 char_u *precomp_buf =
6109 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00006110
Bram Moolenaar231334e2005-07-25 20:46:57 +00006111 if (precomp_buf)
6112 {
6113 mch_memmove(buf, precomp_buf, precomp_len);
6114 vim_free(precomp_buf);
6115 }
6116#endif
6117 addfile(gap, buf, flags);
6118 }
6119 }
6120 }
6121 }
6122
6123 closedir(dirp);
6124 }
6125
6126 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02006127 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00006128
6129 matches = gap->ga_len - start_len;
6130 if (matches > 0)
6131 qsort(((char_u **)gap->ga_data) + start_len, matches,
6132 sizeof(char_u *), pstrcmp);
6133 return matches;
6134}
6135#endif
6136
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02006137#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
6138/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +02006139 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
6140 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02006141 */
6142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006143remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02006144{
6145 int i;
6146 int j;
6147 char_u **fnames = (char_u **)gap->ga_data;
6148
Bram Moolenaardc685ab2010-08-13 21:16:49 +02006149 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02006150 for (i = gap->ga_len - 1; i > 0; --i)
6151 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
6152 {
6153 vim_free(fnames[i]);
6154 for (j = i + 1; j < gap->ga_len; ++j)
6155 fnames[j - 1] = fnames[j];
6156 --gap->ga_len;
6157 }
6158}
6159#endif
6160
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006161/*
6162 * Return TRUE if "p" contains what looks like an environment variable.
6163 * Allowing for escaping.
6164 */
6165 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006166has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006167{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006168 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006169 {
6170 if (*p == '\\' && p[1] != NUL)
6171 ++p;
6172 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006173#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006174 "$%"
6175#else
6176 "$"
6177#endif
6178 , *p) != NULL)
6179 return TRUE;
6180 }
6181 return FALSE;
6182}
6183
6184#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006185/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +01006186 * Return TRUE if "p" contains a special wildcard character, one that Vim
6187 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006188 */
6189 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006190has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006191{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006192 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006193 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01006194 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006195 if (*p == '\\' && p[1] != NUL)
6196 ++p;
6197 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
6198 return TRUE;
6199 }
6200 return FALSE;
6201}
6202#endif
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204/*
6205 * Generic wildcard expansion code.
6206 *
6207 * Characters in "pat" that should not be expanded must be preceded with a
6208 * backslash. E.g., "/path\ with\ spaces/my\*star*"
6209 *
6210 * Return FAIL when no single file was found. In this case "num_file" is not
6211 * set, and "file" may contain an error message.
6212 * Return OK when some files found. "num_file" is set to the number of
6213 * matches, "file" to the array of matches. Call FreeWild() later.
6214 */
6215 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006216gen_expand_wildcards(
6217 int num_pat, /* number of input patterns */
6218 char_u **pat, /* array of input patterns */
6219 int *num_file, /* resulting number of files */
6220 char_u ***file, /* array of resulting files */
6221 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 int i;
6224 garray_T ga;
6225 char_u *p;
6226 static int recursive = FALSE;
6227 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +02006228 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02006229#if defined(FEAT_SEARCHPATH)
6230 int did_expand_in_path = FALSE;
6231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233 /*
6234 * expand_env() is called to expand things like "~user". If this fails,
6235 * it calls ExpandOne(), which brings us back here. In this case, always
6236 * call the machine specific expansion function, if possible. Otherwise,
6237 * return FAIL.
6238 */
6239 if (recursive)
6240#ifdef SPECIAL_WILDCHAR
6241 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
6242#else
6243 return FAIL;
6244#endif
6245
6246#ifdef SPECIAL_WILDCHAR
6247 /*
6248 * If there are any special wildcard characters which we cannot handle
6249 * here, call machine specific function for all the expansion. This
6250 * avoids starting the shell for each argument separately.
6251 * For `=expr` do use the internal function.
6252 */
6253 for (i = 0; i < num_pat; i++)
6254 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006255 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256# ifdef VIM_BACKTICK
6257 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
6258# endif
6259 )
6260 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
6261 }
6262#endif
6263
6264 recursive = TRUE;
6265
6266 /*
6267 * The matching file names are stored in a growarray. Init it empty.
6268 */
6269 ga_init2(&ga, (int)sizeof(char_u *), 30);
6270
6271 for (i = 0; i < num_pat; ++i)
6272 {
6273 add_pat = -1;
6274 p = pat[i];
6275
6276#ifdef VIM_BACKTICK
6277 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +02006278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +02006280 if (add_pat == -1)
6281 retval = FAIL;
6282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 else
6284#endif
6285 {
6286 /*
6287 * First expand environment variables, "~/" and "~user/".
6288 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006289 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00006291 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (p == NULL)
6293 p = pat[i];
6294#ifdef UNIX
6295 /*
6296 * On Unix, if expand_env() can't expand an environment
6297 * variable, use the shell to do that. Discard previously
6298 * found file names and start all over again.
6299 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +02006300 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
6302 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +00006303 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +02006305 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 recursive = FALSE;
6307 return i;
6308 }
6309#endif
6310 }
6311
6312 /*
6313 * If there are wildcards: Expand file names and add each match to
6314 * the list. If there is no match, and EW_NOTFOUND is given, add
6315 * the pattern.
6316 * If there are no wildcards: Add the file name if it exists or
6317 * when EW_NOTFOUND is given.
6318 */
6319 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006320 {
6321#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02006322 if ((flags & EW_PATH)
6323 && !mch_isFullName(p)
6324 && !(p[0] == '.'
6325 && (vim_ispathsep(p[1])
6326 || (p[1] == '.' && vim_ispathsep(p[2]))))
6327 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02006328 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +02006329 /* :find completion where 'path' is used.
6330 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02006331 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006332 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02006333 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +02006334 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02006335 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006336 else
6337#endif
6338 add_pat = mch_expandpath(&ga, p, flags);
6339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
6341
6342 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
6343 {
6344 char_u *t = backslash_halve_save(p);
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 /* When EW_NOTFOUND is used, always add files and dirs. Makes
6347 * "vim c:/" work. */
6348 if (flags & EW_NOTFOUND)
6349 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +02006350 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 addfile(&ga, t, flags);
6352 vim_free(t);
6353 }
6354
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02006355#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +02006356 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +02006357 uniquefy_paths(&ga, p);
6358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (p != pat[i])
6360 vim_free(p);
6361 }
6362
6363 *num_file = ga.ga_len;
6364 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
6365
6366 recursive = FALSE;
6367
Bram Moolenaar336bd622016-01-17 18:23:58 +01006368 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369}
6370
6371# ifdef VIM_BACKTICK
6372
6373/*
6374 * Return TRUE if we can expand this backtick thing here.
6375 */
6376 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006377vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
6380}
6381
6382/*
6383 * Expand an item in `backticks` by executing it as a command.
6384 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +02006385 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 */
6387 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006388expand_backtick(
6389 garray_T *gap,
6390 char_u *pat,
6391 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 char_u *p;
6394 char_u *cmd;
6395 char_u *buffer;
6396 int cnt = 0;
6397 int i;
6398
6399 /* Create the command: lop off the backticks. */
6400 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
6401 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02006402 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404#ifdef FEAT_EVAL
6405 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006406 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 else
6408#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006409 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02006410 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 vim_free(cmd);
6412 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +02006413 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415 cmd = buffer;
6416 while (*cmd != NUL)
6417 {
6418 cmd = skipwhite(cmd); /* skip over white space */
6419 p = cmd;
6420 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
6421 ++p;
6422 /* add an entry if it is not empty */
6423 if (p > cmd)
6424 {
6425 i = *p;
6426 *p = NUL;
6427 addfile(gap, cmd, flags);
6428 *p = i;
6429 ++cnt;
6430 }
6431 cmd = p;
6432 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
6433 ++cmd;
6434 }
6435
6436 vim_free(buffer);
6437 return cnt;
6438}
6439# endif /* VIM_BACKTICK */
6440
6441/*
6442 * Add a file to a file list. Accepted flags:
6443 * EW_DIR add directories
6444 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006445 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 * EW_NOTFOUND add even when it doesn't exist
6447 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +01006448 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 */
6450 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006451addfile(
6452 garray_T *gap,
6453 char_u *f, /* filename */
6454 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 char_u *p;
6457 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +02006458 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
Bram Moolenaard8b77f72015-03-05 21:21:19 +01006460 /* if the file/dir/link doesn't exist, may not add it */
6461 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +01006462 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 return;
6464
6465#ifdef FNAME_ILLEGAL
6466 /* if the file/dir contains illegal characters, don't add it */
6467 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
6468 return;
6469#endif
6470
6471 isdir = mch_isdir(f);
6472 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
6473 return;
6474
Bram Moolenaarb5971142015-03-21 17:32:19 +01006475 /* If the file isn't executable, may not add it. Do accept directories.
6476 * When invoked from expand_shellcmd() do not use $PATH. */
6477 if (!isdir && (flags & EW_EXEC)
6478 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006479 return;
6480
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 /* Make room for another item in the file list. */
6482 if (ga_grow(gap, 1) == FAIL)
6483 return;
6484
6485 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
6486 if (p == NULL)
6487 return;
6488
6489 STRCPY(p, f);
6490#ifdef BACKSLASH_IN_FILENAME
6491 slash_adjust(p);
6492#endif
6493 /*
6494 * Append a slash or backslash after directory names if none is present.
6495 */
6496#ifndef DONT_ADD_PATHSEP_TO_DIR
6497 if (isdir && (flags & EW_ADDSLASH))
6498 add_pathsep(p);
6499#endif
6500 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501}
6502#endif /* !NO_EXPANDPATH */
6503
6504#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
6505
6506#ifndef SEEK_SET
6507# define SEEK_SET 0
6508#endif
6509#ifndef SEEK_END
6510# define SEEK_END 2
6511#endif
6512
6513/*
6514 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02006515 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
6516 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 * Returns an allocated string, or NULL for error.
6518 */
6519 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006520get_cmd_output(
6521 char_u *cmd,
6522 char_u *infile, /* optional input file name */
6523 int flags, /* can be SHELL_SILENT */
6524 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 char_u *tempname;
6527 char_u *command;
6528 char_u *buffer = NULL;
6529 int len;
6530 int i = 0;
6531 FILE *fd;
6532
6533 if (check_restricted() || check_secure())
6534 return NULL;
6535
6536 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02006537 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006539 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 return NULL;
6541 }
6542
6543 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006544 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (command == NULL)
6546 goto done;
6547
6548 /*
6549 * Call the shell to execute the command (errors are ignored).
6550 * Don't check timestamps here.
6551 */
6552 ++no_check_timestamps;
6553 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
6554 --no_check_timestamps;
6555
6556 vim_free(command);
6557
6558 /*
6559 * read the names from the file into memory
6560 */
6561# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +00006562 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 fd = mch_fopen((char *)tempname, "r");
6564# else
6565 fd = mch_fopen((char *)tempname, READBIN);
6566# endif
6567
6568 if (fd == NULL)
6569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006570 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 goto done;
6572 }
6573
6574 fseek(fd, 0L, SEEK_END);
6575 len = ftell(fd); /* get size of temp file */
6576 fseek(fd, 0L, SEEK_SET);
6577
6578 buffer = alloc(len + 1);
6579 if (buffer != NULL)
6580 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
6581 fclose(fd);
6582 mch_remove(tempname);
6583 if (buffer == NULL)
6584 goto done;
6585#ifdef VMS
6586 len = i; /* VMS doesn't give us what we asked for... */
6587#endif
6588 if (i != len)
6589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006590 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006591 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02006593 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +02006594 {
6595 /* Change NUL into SOH, otherwise the string is truncated. */
6596 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +02006597 if (buffer[i] == NUL)
6598 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +02006599
Bram Moolenaar162bd912010-07-28 22:29:10 +02006600 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +02006601 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02006602 else
6603 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
6605done:
6606 vim_free(tempname);
6607 return buffer;
6608}
6609#endif
6610
6611/*
6612 * Free the list of files returned by expand_wildcards() or other expansion
6613 * functions.
6614 */
6615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006616FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006618 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 while (count--)
6621 vim_free(files[count]);
6622 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624
6625/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +02006626 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 * Don't do this when still processing a command or a mapping.
6628 * Don't do this when inside a ":normal" command.
6629 */
6630 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006631goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 return (p_im && stuff_empty() && typebuf_typed());
6634}
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006635
6636/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +02006637 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006638 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
6639 * - Remove any argument. E.g., "csh -f" -> "csh".
6640 * But don't allow a space in the path, so that this works:
6641 * "/usr/bin/csh --rcfile ~/.cshrc"
6642 * But don't do that for Windows, it's common to have a space in the path.
6643 */
6644 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006645get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006646{
6647 char_u *p;
6648
Bram Moolenaar4f974752019-02-17 17:44:42 +01006649#ifdef MSWIN
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006650 p = gettail(p_sh);
6651 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
6652#else
6653 p = skiptowhite(p_sh);
6654 if (*p == NUL)
6655 {
6656 /* No white space, use the tail. */
6657 p = vim_strsave(gettail(p_sh));
6658 }
6659 else
6660 {
6661 char_u *p1, *p2;
6662
6663 /* Find the last path separator before the space. */
6664 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006665 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006666 if (vim_ispathsep(*p2))
6667 p1 = p2 + 1;
6668 p = vim_strnsave(p1, (int)(p - p1));
6669 }
6670#endif
6671 return p;
6672}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01006673
6674/*
6675 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6676 * Also check for ":\\", which MS Internet Explorer accepts, return
6677 * URL_BACKSLASH.
6678 */
6679 int
6680path_is_url(char_u *p)
6681{
6682 if (STRNCMP(p, "://", (size_t)3) == 0)
6683 return URL_SLASH;
6684 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6685 return URL_BACKSLASH;
6686 return 0;
6687}
6688
6689/*
6690 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6691 * Return URL_BACKSLASH for "name:\\".
6692 * Return zero otherwise.
6693 */
6694 int
6695path_with_url(char_u *fname)
6696{
6697 char_u *p;
6698
6699 for (p = fname; isalpha(*p); ++p)
6700 ;
6701 return path_is_url(p);
6702}
6703
6704/*
6705 * Return TRUE if "name" is a full (absolute) path name or URL.
6706 */
6707 int
6708vim_isAbsName(char_u *name)
6709{
6710 return (path_with_url(name) != 0 || mch_isFullName(name));
6711}
6712
6713/*
6714 * Get absolute file name into buffer "buf[len]".
6715 *
6716 * return FAIL for failure, OK otherwise
6717 */
6718 int
6719vim_FullName(
6720 char_u *fname,
6721 char_u *buf,
6722 int len,
6723 int force) /* force expansion even when already absolute */
6724{
6725 int retval = OK;
6726 int url;
6727
6728 *buf = NUL;
6729 if (fname == NULL)
6730 return FAIL;
6731
6732 url = path_with_url(fname);
6733 if (!url)
6734 retval = mch_FullName(fname, buf, len, force);
6735 if (url || retval == FAIL)
6736 {
6737 /* something failed; use the file name (truncate when too long) */
6738 vim_strncpy(buf, fname, len - 1);
6739 }
6740#if defined(MSWIN)
6741 slash_adjust(buf);
6742#endif
6743 return retval;
6744}