blob: 657e164913139f59c7b194e34eae95ea1633035b [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 * misc2.c: Various functions.
12 */
13#include "vim.h"
14
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000015static char_u *username = NULL; /* cached result of mch_get_user_name() */
16
17static char_u *ff_expand_buffer = NULL; /* used for expanding filenames */
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
21/*
22 * Return TRUE if in the current mode we need to use virtual.
23 */
24 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010025virtual_active(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026{
27 /* While an operator is being executed we return "virtual_op", because
28 * VIsual_active has already been reset, thus we can't check for "block"
29 * being used. */
30 if (virtual_op != MAYBE)
31 return virtual_op;
32 return (ve_flags == VE_ALL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 || ((ve_flags & VE_INSERT) && (State & INSERT)));
35}
36
37/*
38 * Get the screen position of the cursor.
39 */
40 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010041getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042{
43 colnr_T x;
44
45 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
46 return (int)x;
47}
48
49/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000050 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 * cursor in that column.
52 * The caller must have saved the cursor line for undo!
53 */
54 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010055coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
58
59 if (wcol == MAXCOL)
60 curwin->w_valid &= ~VALID_VIRTCOL;
61 else
62 {
63 /* Virtcol is valid */
64 curwin->w_valid |= VALID_VIRTCOL;
65 curwin->w_virtcol = wcol;
66 }
67 return rc;
68}
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010071 * Get the screen position of character col with a coladd in the cursor line.
72 */
73 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010074getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010075{
76 colnr_T x;
77 pos_T pos;
78
79 pos.lnum = curwin->w_cursor.lnum;
80 pos.col = col;
Bram Moolenaar977239e2019-01-11 16:16:01 +010081 pos.coladd = coladd;
Bram Moolenaar977239e2019-01-11 16:16:01 +010082 getvvcol(curwin, &pos, &x, NULL, NULL);
83 return (int)x;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Try to advance the Cursor to the specified screen column.
88 * If virtual editing: fine tune the cursor position.
89 * Note that all virtual positions off the end of a line should share
90 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
91 * beginning at coladd 0.
92 *
93 * return OK if desired column is reached, FAIL if not
94 */
95 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010096coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097{
98 int rc = getvpos(&curwin->w_cursor, wcol);
99
100 if (wcol == MAXCOL || rc == FAIL)
101 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000102 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000104 /* Virtcol is valid when not on a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 curwin->w_valid |= VALID_VIRTCOL;
106 curwin->w_virtcol = wcol;
107 }
108 return rc;
109}
110
111/*
112 * Return in "pos" the position of the cursor advanced to screen column "wcol".
113 * return OK if desired column is reached, FAIL if not
114 */
115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100116getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 return coladvance2(pos, FALSE, virtual_active(), wcol);
119}
120
121 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100122coladvance2(
123 pos_T *pos,
124 int addspaces, /* change the text to achieve our goal? */
125 int finetune, /* change char offset for the exact column */
126 colnr_T wcol) /* column to move to */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 int idx;
129 char_u *ptr;
130 char_u *line;
131 colnr_T col = 0;
132 int csize = 0;
133 int one_more;
134#ifdef FEAT_LINEBREAK
135 int head = 0;
136#endif
137
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000138 one_more = (State & INSERT)
139 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000140 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100141 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL) ;
Bram Moolenaara1381de2009-11-03 15:44:21 +0000142 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144 if (wcol >= MAXCOL)
145 {
146 idx = (int)STRLEN(line) - 1 + one_more;
147 col = wcol;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 if ((addspaces || finetune) && !VIsual_active)
150 {
151 curwin->w_curswant = linetabsize(line) + one_more;
152 if (curwin->w_curswant > 0)
153 --curwin->w_curswant;
154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 }
156 else
157 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200158 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaarebefac62005-12-28 22:39:57 +0000160 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 && curwin->w_width != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 && wcol >= (colnr_T)width)
164 {
165 csize = linetabsize(line);
166 if (csize > 0)
167 csize--;
168
Bram Moolenaarebefac62005-12-28 22:39:57 +0000169 if (wcol / width > (colnr_T)csize / width
170 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 {
172 /* In case of line wrapping don't move the cursor beyond the
Bram Moolenaarebefac62005-12-28 22:39:57 +0000173 * right screen edge. In Insert mode allow going just beyond
174 * the last character (like what happens when typing and
175 * reaching the right window edge). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 wcol = (csize / width + 1) * width - 1;
177 }
178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 ptr = line;
181 while (col <= wcol && *ptr != NUL)
182 {
183 /* Count a tab for what it's worth (if list mode not on) */
184#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200185 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100186 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200188 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190 col += csize;
191 }
192 idx = (int)(ptr - line);
193 /*
194 * Handle all the special cases. The virtual_active() check
195 * is needed to ensure that a virtual position off the end of
196 * a line has the correct indexing. The one_more comparison
197 * replaces an explicit add of one_more later on.
198 */
199 if (col > wcol || (!virtual_active() && one_more == 0))
200 {
201 idx -= 1;
202# ifdef FEAT_LINEBREAK
203 /* Don't count the chars from 'showbreak'. */
204 csize -= head;
205# endif
206 col -= csize;
207 }
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 if (virtual_active()
210 && addspaces
211 && ((col != wcol && col != wcol + 1) || csize > 1))
212 {
213 /* 'virtualedit' is set: The difference between wcol and col is
214 * filled with spaces. */
215
216 if (line[idx] == NUL)
217 {
218 /* Append spaces */
219 int correct = wcol - col;
220 char_u *newline = alloc(idx + correct + 1);
221 int t;
222
223 if (newline == NULL)
224 return FAIL;
225
226 for (t = 0; t < idx; ++t)
227 newline[t] = line[t];
228
229 for (t = 0; t < correct; ++t)
230 newline[t + idx] = ' ';
231
232 newline[idx + correct] = NUL;
233
234 ml_replace(pos->lnum, newline, FALSE);
235 changed_bytes(pos->lnum, (colnr_T)idx);
236 idx += correct;
237 col = wcol;
238 }
239 else
240 {
241 /* Break a tab */
242 int linelen = (int)STRLEN(line);
243 int correct = wcol - col - csize + 1; /* negative!! */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000244 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 int t, s = 0;
246 int v;
247
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000248 if (-correct > csize)
249 return FAIL;
250
251 newline = alloc(linelen + csize);
252 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 return FAIL;
254
255 for (t = 0; t < linelen; t++)
256 {
257 if (t != idx)
258 newline[s++] = line[t];
259 else
260 for (v = 0; v < csize; v++)
261 newline[s++] = ' ';
262 }
263
264 newline[linelen + csize - 1] = NUL;
265
266 ml_replace(pos->lnum, newline, FALSE);
267 changed_bytes(pos->lnum, idx);
268 idx += (csize - 1 + correct);
269 col += correct;
270 }
271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273
274 if (idx < 0)
275 pos->col = 0;
276 else
277 pos->col = idx;
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 pos->coladd = 0;
280
281 if (finetune)
282 {
283 if (wcol == MAXCOL)
284 {
285 /* The width of the last character is used to set coladd. */
286 if (!one_more)
287 {
288 colnr_T scol, ecol;
289
290 getvcol(curwin, pos, &scol, NULL, &ecol);
291 pos->coladd = ecol - scol;
292 }
293 }
294 else
295 {
296 int b = (int)wcol - (int)col;
297
298 /* The difference between wcol and col is used to set coladd. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200299 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 pos->coladd = b;
301
302 col += b;
303 }
304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
Bram Moolenaara1381de2009-11-03 15:44:21 +0000306 /* prevent from moving onto a trail byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200308 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
310 if (col < wcol)
311 return FAIL;
312 return OK;
313}
314
315/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000316 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 */
318 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100319inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320{
321 return inc(&curwin->w_cursor);
322}
323
Bram Moolenaar446cb832008-06-24 21:56:24 +0000324/*
325 * Increment the line pointer "lp" crossing line boundaries as necessary.
326 * Return 1 when going to the next line.
327 * Return 2 when moving forward onto a NUL at the end of the line).
328 * Return -1 when at the end of file.
329 * Return 0 otherwise.
330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100332inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100334 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100336 /* when searching position may be set to end of a line */
337 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100339 p = ml_get_pos(lp);
340 if (*p != NUL) /* still within line, move to next char (may be NUL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100342 if (has_mbyte)
343 {
344 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100346 lp->col += l;
347 return ((p[l] != NUL) ? 0 : 2);
348 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100349 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100350 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 return ((p[1] != NUL) ? 0 : 2);
352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
354 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
355 {
356 lp->col = 0;
357 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 return 1;
360 }
361 return -1;
362}
363
364/*
365 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
366 */
367 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100368incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
370 int r;
371
372 if ((r = inc(lp)) >= 1 && lp->col)
373 r = inc(lp);
374 return r;
375}
376
377/*
378 * dec(p)
379 *
380 * Decrement the line pointer 'p' crossing line boundaries as necessary.
381 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
382 */
383 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100384dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100386 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387}
388
389 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100390dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
392 char_u *p;
393
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100395 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100397 /* past end of line */
398 p = ml_get(lp->lnum);
399 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100400 if (has_mbyte)
401 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100402 return 0;
403 }
404
405 if (lp->col > 0)
406 {
407 /* still within line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 if (has_mbyte)
410 {
411 p = ml_get(lp->lnum);
412 lp->col -= (*mb_head_off)(p, p + lp->col);
413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 return 0;
415 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100416
417 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100419 /* there is a prior line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 lp->lnum--;
421 p = ml_get(lp->lnum);
422 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 if (has_mbyte)
424 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 return 1;
426 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100427
428 /* at start of file */
429 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430}
431
432/*
433 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
434 */
435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int r;
439
440 if ((r = dec(lp)) == 1 && lp->col)
441 r = dec(lp);
442 return r;
443}
444
445/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200446 * Get the line number relative to the current cursor position, i.e. the
447 * difference between line number and cursor position. Only look for lines that
448 * can be visible, folded lines don't count.
449 */
450 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451get_cursor_rel_lnum(
452 win_T *wp,
453 linenr_T lnum) /* line number to get the result for */
Bram Moolenaar64486672010-05-16 15:46:46 +0200454{
455 linenr_T cursor = wp->w_cursor.lnum;
456 linenr_T retval = 0;
457
458#ifdef FEAT_FOLDING
459 if (hasAnyFolding(wp))
460 {
461 if (lnum > cursor)
462 {
463 while (lnum > cursor)
464 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100465 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200466 /* if lnum and cursor are in the same fold,
467 * now lnum <= cursor */
468 if (lnum > cursor)
469 retval++;
470 lnum--;
471 }
472 }
473 else if (lnum < cursor)
474 {
475 while (lnum < cursor)
476 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100477 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200478 /* if lnum and cursor are in the same fold,
479 * now lnum >= cursor */
480 if (lnum < cursor)
481 retval--;
482 lnum++;
483 }
484 }
485 /* else if (lnum == cursor)
486 * retval = 0;
487 */
488 }
489 else
490#endif
491 retval = lnum - cursor;
492
493 return retval;
494}
495
496/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200497 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
498 * This allows for the col to be on the NUL byte.
499 */
500 void
501check_pos(buf_T *buf, pos_T *pos)
502{
503 char_u *line;
504 colnr_T len;
505
506 if (pos->lnum > buf->b_ml.ml_line_count)
507 pos->lnum = buf->b_ml.ml_line_count;
508
509 if (pos->col > 0)
510 {
511 line = ml_get_buf(buf, pos->lnum, FALSE);
512 len = (colnr_T)STRLEN(line);
513 if (pos->col > len)
514 pos->col = len;
515 }
516}
517
518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 * Make sure curwin->w_cursor.lnum is valid.
520 */
521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100522check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
525 {
526#ifdef FEAT_FOLDING
527 /* If there is a closed fold at the end of the file, put the cursor in
528 * its first line. Otherwise in the last line. */
529 if (!hasFolding(curbuf->b_ml.ml_line_count,
530 &curwin->w_cursor.lnum, NULL))
531#endif
532 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
533 }
534 if (curwin->w_cursor.lnum <= 0)
535 curwin->w_cursor.lnum = 1;
536}
537
538/*
539 * Make sure curwin->w_cursor.col is valid.
540 */
541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200544 check_cursor_col_win(curwin);
545}
546
547/*
548 * Make sure win->w_cursor.col is valid.
549 */
550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100551check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200552{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 colnr_T len;
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200554 colnr_T oldcol = win->w_cursor.col;
555 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200557 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200559 win->w_cursor.col = 0;
560 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000562 /* Allow cursor past end-of-line when:
563 * - in Insert mode or restarting Insert mode
564 * - in Visual mode and 'selection' isn't "old"
565 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000566 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000568 || (ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200570 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000572 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200573 win->w_cursor.col = len - 1;
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200574 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000575 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200576 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200579 else if (win->w_cursor.col < 0)
580 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 /* If virtual editing is on, we can leave the cursor on the old position,
583 * only we must set it to virtual. But don't do it when at the end of the
584 * line. */
585 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200586 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000588 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200589 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200590 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200591 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200592
593 /* Make sure that coladd is not more than the char width.
594 * Not for the last character, coladd is then used when the cursor
595 * is actually after the last character. */
596 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200597 {
598 int cs, ce;
599
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200600 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
601 if (win->w_cursor.coladd > ce - cs)
602 win->w_cursor.coladd = ce - cs;
603 }
604 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000605 else
606 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200607 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609}
610
611/*
612 * make sure curwin->w_cursor in on a valid character
613 */
614 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100615check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616{
617 check_cursor_lnum();
618 check_cursor_col();
619}
620
621#if defined(FEAT_TEXTOBJ) || defined(PROTO)
622/*
623 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
624 * Allow it when in Visual mode and 'selection' is not "old".
625 */
626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100627adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 && gchar_cursor() == NUL)
632 --curwin->w_cursor.col;
633}
634#endif
635
636/*
637 * When curwin->w_leftcol has changed, adjust the cursor position.
638 * Return TRUE if the cursor was moved.
639 */
640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100641leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 long lastcol;
644 colnr_T s, e;
645 int retval = FALSE;
646
647 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200648 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 validate_virtcol();
650
651 /*
652 * If the cursor is right or left of the screen, move it to last or first
653 * character.
654 */
655 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
656 {
657 retval = TRUE;
658 coladvance((colnr_T)(lastcol - p_siso));
659 }
660 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
661 {
662 retval = TRUE;
663 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
664 }
665
666 /*
667 * If the start of the character under the cursor is not on the screen,
668 * advance the cursor one more char. If this fails (last char of the
669 * line) adjust the scrolling.
670 */
671 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
672 if (e > (colnr_T)lastcol)
673 {
674 retval = TRUE;
675 coladvance(s - 1);
676 }
677 else if (s < curwin->w_leftcol)
678 {
679 retval = TRUE;
680 if (coladvance(e + 1) == FAIL) /* there isn't another character */
681 {
682 curwin->w_leftcol = s; /* adjust w_leftcol instead */
683 changed_cline_bef_curs();
684 }
685 }
686
687 if (retval)
688 curwin->w_set_curswant = TRUE;
689 redraw_later(NOT_VALID);
690 return retval;
691}
692
693/**********************************************************************
694 * Various routines dealing with allocation and deallocation of memory.
695 */
696
697#if defined(MEM_PROFILE) || defined(PROTO)
698
699# define MEM_SIZES 8200
700static long_u mem_allocs[MEM_SIZES];
701static long_u mem_frees[MEM_SIZES];
702static long_u mem_allocated;
703static long_u mem_freed;
704static long_u mem_peak;
705static long_u num_alloc;
706static long_u num_freed;
707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100709mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 *sizep += sizeof(size_t);
712}
713
714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100715mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
717 *sizep += sizeof(size_t);
718}
719
720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721mem_post_alloc(
722 void **pp,
723 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
725 if (*pp == NULL)
726 return;
727 size -= sizeof(size_t);
728 *(long_u *)*pp = size;
729 if (size <= MEM_SIZES-1)
730 mem_allocs[size-1]++;
731 else
732 mem_allocs[MEM_SIZES-1]++;
733 mem_allocated += size;
734 if (mem_allocated - mem_freed > mem_peak)
735 mem_peak = mem_allocated - mem_freed;
736 num_alloc++;
737 *pp = (void *)((char *)*pp + sizeof(size_t));
738}
739
740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 long_u size;
744
745 *pp = (void *)((char *)*pp - sizeof(size_t));
746 size = *(size_t *)*pp;
747 if (size <= MEM_SIZES-1)
748 mem_frees[size-1]++;
749 else
750 mem_frees[MEM_SIZES-1]++;
751 mem_freed += size;
752 num_freed++;
753}
754
755/*
756 * called on exit via atexit()
757 */
758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100759vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
761 int i, j;
762
763 printf("\r\n");
764 j = 0;
765 for (i = 0; i < MEM_SIZES - 1; i++)
766 {
767 if (mem_allocs[i] || mem_frees[i])
768 {
769 if (mem_frees[i] > mem_allocs[i])
770 printf("\r\n%s", _("ERROR: "));
771 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
772 j++;
773 if (j > 3)
774 {
775 j = 0;
776 printf("\r\n");
777 }
778 }
779 }
780
781 i = MEM_SIZES - 1;
782 if (mem_allocs[i])
783 {
784 printf("\r\n");
785 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200786 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
788 }
789
790 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
791 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
792 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
793 num_alloc, num_freed);
794}
795
796#endif /* MEM_PROFILE */
797
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100798#ifdef FEAT_EVAL
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100799 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100800alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100801{
802 if (alloc_fail_countdown == 0)
803 {
804 if (--alloc_fail_repeat <= 0)
805 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100806 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100807 return TRUE;
808 }
809 --alloc_fail_countdown;
810 return FALSE;
811}
812#endif
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814/*
815 * Some memory is reserved for error messages and for being able to
816 * call mf_release_all(), which needs some memory for mf_trans_add().
817 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100818#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200819#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000822 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Use lalloc for larger blocks.
824 */
825 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100826alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
828 return (lalloc((long_u)size, TRUE));
829}
830
831/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100832 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100833 */
834 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100835alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100836{
837#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100838 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100839 return NULL;
840#endif
841 return (lalloc((long_u)size, TRUE));
842}
843
844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * Allocate memory and set all bytes to zero.
846 */
847 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100848alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850 char_u *p;
851
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200852 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (p != NULL)
854 (void)vim_memset(p, 0, (size_t)size);
855 return p;
856}
857
858/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100859 * Same as alloc_clear() but with allocation id for testing
860 */
861 char_u *
862alloc_clear_id(unsigned size, alloc_id_T id UNUSED)
863{
864#ifdef FEAT_EVAL
865 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
866 return NULL;
867#endif
868 return alloc_clear(size);
869}
870
871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 * alloc() with check for maximum line length
873 */
874 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200877#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (sizeof(int) == 2 && size > 0x7fff)
879 {
880 /* Don't hide this message */
881 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100882 emsg(_("E340: Line is becoming too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 return NULL;
884 }
885#endif
886 return (lalloc((long_u)size, TRUE));
887}
888
889/*
890 * Allocate memory like lalloc() and set all bytes to zero.
891 */
892 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100893lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
895 char_u *p;
896
897 p = (lalloc(size, message));
898 if (p != NULL)
899 (void)vim_memset(p, 0, (size_t)size);
900 return p;
901}
902
903/*
904 * Low level memory allocation function.
905 * This is used often, KEEP IT FAST!
906 */
907 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100908lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 char_u *p; /* pointer to new storage space */
911 static int releasing = FALSE; /* don't do mf_release_all() recursive */
912 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100913#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 static long_u allocated = 0; /* allocated since last avail check */
915#endif
916
917 /* Safety check for allocating zero bytes */
918 if (size == 0)
919 {
920 /* Don't hide this message */
921 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 siemsg(_("E341: Internal error: lalloc(%ld, )"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 return NULL;
924 }
925
926#ifdef MEM_PROFILE
927 mem_pre_alloc_l(&size);
928#endif
929
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 /*
931 * Loop when out of memory: Try to release some memfile blocks and
932 * if some blocks are released call malloc again.
933 */
934 for (;;)
935 {
936 /*
937 * Handle three kind of systems:
938 * 1. No check for available memory: Just return.
939 * 2. Slow check for available memory: call mch_avail_mem() after
940 * allocating KEEP_ROOM amount of memory.
941 * 3. Strict check for available memory: call mch_avail_mem()
942 */
943 if ((p = (char_u *)malloc((size_t)size)) != NULL)
944 {
945#ifndef HAVE_AVAIL_MEM
946 /* 1. No check for available memory: Just return. */
947 goto theend;
948#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 /* 2. Slow check for available memory: call mch_avail_mem() after
950 * allocating (KEEP_ROOM / 2) amount of memory. */
951 allocated += size;
952 if (allocated < KEEP_ROOM / 2)
953 goto theend;
954 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200957 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000959 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 p = NULL;
961 }
962 else
963 goto theend;
964#endif
965 }
966 /*
967 * Remember that mf_release_all() is being called to avoid an endless
968 * loop, because mf_release_all() may call alloc() recursively.
969 */
970 if (releasing)
971 break;
972 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000973
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100974 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000975 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 releasing = FALSE;
978 if (!try_again)
979 break;
980 }
981
982 if (message && p == NULL)
983 do_outofmem_msg(size);
984
985theend:
986#ifdef MEM_PROFILE
987 mem_post_alloc((void **)&p, (size_t)size);
988#endif
989 return p;
990}
991
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100992/*
993 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100994 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100995#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100996 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100997lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100998{
999#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001000 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001001 return NULL;
1002#endif
1003 return (lalloc((long_u)size, message));
1004}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001005#endif
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#if defined(MEM_PROFILE) || defined(PROTO)
1008/*
1009 * realloc() with memory profiling.
1010 */
1011 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001012mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 void *p;
1015
1016 mem_pre_free(&ptr);
1017 mem_pre_alloc_s(&size);
1018
1019 p = realloc(ptr, size);
1020
1021 mem_post_alloc(&p, size);
1022
1023 return p;
1024}
1025#endif
1026
1027/*
1028* Avoid repeating the error message many times (they take 1 second each).
1029* Did_outofmem_msg is reset when a character is read.
1030*/
1031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001032do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 if (!did_outofmem_msg)
1035 {
1036 /* Don't hide this message */
1037 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001038
1039 /* Must come first to avoid coming back here when printing the error
1040 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001042
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001043 semsg(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045}
1046
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001047#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001048
1049# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001050static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001051# endif
1052
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001053/*
1054 * Free everything that we allocated.
1055 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001056 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1057 * surprised if Vim crashes...
1058 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001059 */
1060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001061free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001062{
1063 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001064
1065 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1066 * Don't try freeing everything again. */
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001067 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001068 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001069 entered_free_all_mem = TRUE;
Bram Moolenaara9673212016-06-01 22:21:06 +02001070
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001071 /* Don't want to trigger autocommands from here on. */
1072 block_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001073
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001074 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1075 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001076 if (first_tabpage->tp_next != NULL)
1077 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001078 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001079 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001080
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001081# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001082 /* Free all spell info. */
1083 spell_free_all();
1084# endif
1085
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001086#if defined(FEAT_INS_EXPAND) && defined(FEAT_BEVAL_TERM)
1087 ui_remove_balloon();
1088# endif
1089
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001090# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001091 /* Clear user commands (before deleting buffers). */
1092 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001093# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001094
1095# ifdef FEAT_MENU
1096 /* Clear menus. */
1097 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001098# ifdef FEAT_MULTI_LANG
1099 do_cmdline_cmd((char_u *)"menutranslate clear");
1100# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001101# endif
1102
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001103 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001104 do_cmdline_cmd((char_u *)"lmapclear");
1105 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001106 do_cmdline_cmd((char_u *)"mapclear");
1107 do_cmdline_cmd((char_u *)"mapclear!");
1108 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001109# if defined(FEAT_EVAL)
1110 do_cmdline_cmd((char_u *)"breakdel *");
1111# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001112# if defined(FEAT_PROFILE)
1113 do_cmdline_cmd((char_u *)"profdel *");
1114# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001115# if defined(FEAT_KEYMAP)
1116 do_cmdline_cmd((char_u *)"set keymap=");
1117#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001118
1119# ifdef FEAT_TITLE
1120 free_titles();
1121# endif
1122# if defined(FEAT_SEARCHPATH)
1123 free_findfile();
1124# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001125
1126 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001127 free_all_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001128 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001129 free_all_marks();
1130 alist_clear(&global_alist);
1131 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001132# if defined(FEAT_CMDL_COMPL)
1133 free_users();
1134# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001135 free_search_patterns();
1136 free_old_sub();
1137 free_last_insert();
1138 free_prev_shellcmd();
1139 free_regexp_stuff();
1140 free_tag_stuff();
1141 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001142# ifdef FEAT_SIGNS
1143 free_signs();
1144# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001145# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001146 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001147# endif
1148# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001149 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001150# endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001151 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001152
1153 /* Free some global vars. */
1154 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001155# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001156 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001157# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001158 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001159# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001160 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001161# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001162 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001163 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001164
1165 /* Clear cmdline history. */
1166 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001167# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001168 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001169# endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001170#ifdef FEAT_TEXT_PROP
1171 clear_global_prop_types();
1172#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001173
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001174#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001175 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001176 win_T *win;
1177 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001178
1179 qf_free_all(NULL);
1180 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001181 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001182 qf_free_all(win);
1183 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001184#endif
1185
1186 /* Close all script inputs. */
1187 close_all_scripts();
1188
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001189 /* Destroy all windows. Must come before freeing buffers. */
1190 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001191
Bram Moolenaar4f198282017-10-23 21:53:30 +02001192 /* Free all option values. Must come after closing windows. */
1193 free_all_options();
1194
Bram Moolenaar2a329742008-04-01 12:53:43 +00001195 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1196 * were freed already. */
1197#ifdef FEAT_AUTOCHDIR
1198 p_acd = FALSE;
1199#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001200 for (buf = firstbuf; buf != NULL; )
1201 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001202 bufref_T bufref;
1203
1204 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001205 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001206 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001207 if (bufref_valid(&bufref))
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001208 buf = nextbuf; /* didn't work, try next one */
1209 else
1210 buf = firstbuf;
1211 }
1212
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001213# ifdef FEAT_ARABIC
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001214 free_cmdline_buf();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001215# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001216
1217 /* Clear registers. */
1218 clear_registers();
1219 ResetRedobuff();
1220 ResetRedobuff();
1221
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001222# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001223 vim_free(serverDelayedStartName);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001224# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001225
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001226 /* highlight info */
1227 free_highlight();
1228
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001229 reset_last_sourcing();
1230
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001231 free_tabpage(first_tabpage);
1232 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001233
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001234# ifdef UNIX
1235 /* Machine-specific free. */
1236 mch_free_mem();
1237# endif
1238
1239 /* message history */
1240 for (;;)
1241 if (delete_first_msg() == FAIL)
1242 break;
1243
Bram Moolenaar655da312016-05-28 22:22:34 +02001244# ifdef FEAT_JOB_CHANNEL
1245 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001246# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001247# ifdef FEAT_TIMERS
Bram Moolenaar623e2632016-07-30 22:47:56 +02001248 timer_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001249# endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001250# ifdef FEAT_EVAL
1251 /* must be after channel_free_all() with unrefs partials */
1252 eval_clear();
1253# endif
1254# ifdef FEAT_JOB_CHANNEL
1255 /* must be after eval_clear() with unrefs jobs */
1256 job_free_all();
1257# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001258
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001259 free_termoptions();
1260
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001261 /* screenlines (can't display anything now!) */
1262 free_screenlines();
1263
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001264# if defined(USE_XSMP)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001265 xsmp_close();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001266# endif
1267# ifdef FEAT_GUI_GTK
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001268 gui_mch_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001269# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001270 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001271
1272 vim_free(IObuff);
1273 vim_free(NameBuff);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001274# ifdef FEAT_QUICKFIX
1275 check_quickfix_busy();
1276# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001277}
1278#endif
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001281 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 */
1283 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001284vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 char_u *p;
1287 unsigned len;
1288
1289 len = (unsigned)STRLEN(string) + 1;
1290 p = alloc(len);
1291 if (p != NULL)
1292 mch_memmove(p, string, (size_t)len);
1293 return p;
1294}
1295
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001296/*
1297 * Copy up to "len" bytes of "string" into newly allocated memory and
1298 * terminate with a NUL.
1299 * The allocated memory always has size "len + 1", also when "string" is
1300 * shorter.
1301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001303vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 char_u *p;
1306
1307 p = alloc((unsigned)(len + 1));
1308 if (p != NULL)
1309 {
1310 STRNCPY(p, string, len);
1311 p[len] = NUL;
1312 }
1313 return p;
1314}
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001317 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1318 * Returns NULL when out of memory.
1319 */
1320 char_u *
1321vim_memsave(char_u *p, int len)
1322{
1323 char_u *ret = alloc((unsigned)len);
1324
1325 if (ret != NULL)
1326 mch_memmove(ret, p, (size_t)len);
1327 return ret;
1328}
1329
1330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1332 * by a backslash.
1333 */
1334 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001335vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001337 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338}
1339
1340/*
1341 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1342 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001343 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 */
1345 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001346vim_strsave_escaped_ext(
1347 char_u *string,
1348 char_u *esc_chars,
1349 int cc,
1350 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 char_u *p;
1353 char_u *p2;
1354 char_u *escaped_string;
1355 unsigned length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
1358 /*
1359 * First count the number of backslashes required.
1360 * Then allocate the memory and insert them.
1361 */
1362 length = 1; /* count the trailing NUL */
1363 for (p = string; *p; p++)
1364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001365 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
1367 length += l; /* count a multibyte char */
1368 p += l - 1;
1369 continue;
1370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1372 ++length; /* count a backslash */
1373 ++length; /* count an ordinary char */
1374 }
1375 escaped_string = alloc(length);
1376 if (escaped_string != NULL)
1377 {
1378 p2 = escaped_string;
1379 for (p = string; *p; p++)
1380 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001381 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
1383 mch_memmove(p2, p, (size_t)l);
1384 p2 += l;
1385 p += l - 1; /* skip multibyte char */
1386 continue;
1387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001389 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 *p2++ = *p;
1391 }
1392 *p2 = NUL;
1393 }
1394 return escaped_string;
1395}
1396
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001397/*
1398 * Return TRUE when 'shell' has "csh" in the tail.
1399 */
1400 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001401csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001402{
1403 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1404}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001405
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001406/*
1407 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001408 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001409 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001410 * Escape a newline, depending on the 'shell' option.
1411 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1412 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001413 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001414 * Returns the result in allocated memory, NULL if we have run out.
1415 */
1416 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001417vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001418{
1419 unsigned length;
1420 char_u *p;
1421 char_u *d;
1422 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001423 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001424 int csh_like;
1425
1426 /* Only csh and similar shells expand '!' within single quotes. For sh and
1427 * the like we must not put a backslash before it, it will be taken
1428 * literally. If do_special is set the '!' will be escaped twice.
1429 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001430 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001431
1432 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001433 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001434 for (p = string; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001435 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001436# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001437 if (!p_ssl)
1438 {
1439 if (*p == '"')
1440 ++length; /* " -> "" */
1441 }
1442 else
1443# endif
1444 if (*p == '\'')
1445 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001446 if ((*p == '\n' && (csh_like || do_newline))
1447 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001448 {
1449 ++length; /* insert backslash */
1450 if (csh_like && do_special)
1451 ++length; /* insert backslash */
1452 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001453 if (do_special && find_cmdline_var(p, &l) >= 0)
1454 {
1455 ++length; /* insert backslash */
1456 p += l - 1;
1457 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001458 }
1459
1460 /* Allocate memory for the result and fill it. */
1461 escaped_string = alloc(length);
1462 if (escaped_string != NULL)
1463 {
1464 d = escaped_string;
1465
1466 /* add opening quote */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001467# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001468 if (!p_ssl)
1469 *d++ = '"';
1470 else
1471# endif
1472 *d++ = '\'';
1473
1474 for (p = string; *p != NUL; )
1475 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001476# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001477 if (!p_ssl)
1478 {
1479 if (*p == '"')
1480 {
1481 *d++ = '"';
1482 *d++ = '"';
1483 ++p;
1484 continue;
1485 }
1486 }
1487 else
1488# endif
1489 if (*p == '\'')
1490 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001491 *d++ = '\'';
1492 *d++ = '\\';
1493 *d++ = '\'';
1494 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001495 ++p;
1496 continue;
1497 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001498 if ((*p == '\n' && (csh_like || do_newline))
1499 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001500 {
1501 *d++ = '\\';
1502 if (csh_like && do_special)
1503 *d++ = '\\';
1504 *d++ = *p++;
1505 continue;
1506 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001507 if (do_special && find_cmdline_var(p, &l) >= 0)
1508 {
1509 *d++ = '\\'; /* insert backslash */
1510 while (--l >= 0) /* copy the var */
1511 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001512 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001513 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001514
1515 MB_COPY_CHAR(p, d);
1516 }
1517
1518 /* add terminating quote and finish with a NUL */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001519# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001520 if (!p_ssl)
1521 *d++ = '"';
1522 else
1523# endif
1524 *d++ = '\'';
1525 *d = NUL;
1526 }
1527
1528 return escaped_string;
1529}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531/*
1532 * Like vim_strsave(), but make all characters uppercase.
1533 * This uses ASCII lower-to-upper case translation, language independent.
1534 */
1535 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 char_u *p1;
1539
1540 p1 = vim_strsave(string);
1541 vim_strup(p1);
1542 return p1;
1543}
1544
1545/*
1546 * Like vim_strnsave(), but make all characters uppercase.
1547 * This uses ASCII lower-to-upper case translation, language independent.
1548 */
1549 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001550vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 char_u *p1;
1553
1554 p1 = vim_strnsave(string, len);
1555 vim_strup(p1);
1556 return p1;
1557}
1558
1559/*
1560 * ASCII lower-to-upper case translation, language independent.
1561 */
1562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001563vim_strup(
1564 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 char_u *p2;
1567 int c;
1568
1569 if (p != NULL)
1570 {
1571 p2 = p;
1572 while ((c = *p2) != NUL)
1573#ifdef EBCDIC
1574 *p2++ = isalpha(c) ? toupper(c) : c;
1575#else
1576 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1577#endif
1578 }
1579}
1580
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001581#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001582/*
1583 * Make string "s" all upper-case and return it in allocated memory.
1584 * Handles multi-byte characters as well as possible.
1585 * Returns NULL when out of memory.
1586 */
1587 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001588strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001589{
1590 char_u *p;
1591 char_u *res;
1592
1593 res = p = vim_strsave(orig);
1594
1595 if (res != NULL)
1596 while (*p != NUL)
1597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001598 int l;
1599
1600 if (enc_utf8)
1601 {
1602 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001603 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001604 char_u *s;
1605
1606 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001607 l = utf_ptr2len(p);
1608 if (c == 0)
1609 {
1610 /* overlong sequence, use only the first byte */
1611 c = *p;
1612 l = 1;
1613 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001614 uc = utf_toupper(c);
1615
1616 /* Reallocate string when byte count changes. This is rare,
1617 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001618 newl = utf_char2len(uc);
1619 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001620 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001621 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001622 if (s == NULL)
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001623 {
1624 vim_free(res);
1625 return NULL;
1626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001627 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001628 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001629 p = s + (p - res);
1630 vim_free(res);
1631 res = s;
1632 }
1633
1634 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001635 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001636 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001637 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001638 p += l; /* skip multi-byte character */
1639 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001640 {
1641 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1642 p++;
1643 }
1644 }
1645
1646 return res;
1647}
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001648
1649/*
1650 * Make string "s" all lower-case and return it in allocated memory.
1651 * Handles multi-byte characters as well as possible.
1652 * Returns NULL when out of memory.
1653 */
1654 char_u *
1655strlow_save(char_u *orig)
1656{
1657 char_u *p;
1658 char_u *res;
1659
1660 res = p = vim_strsave(orig);
1661
1662 if (res != NULL)
1663 while (*p != NUL)
1664 {
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001665 int l;
1666
1667 if (enc_utf8)
1668 {
1669 int c, lc;
1670 int newl;
1671 char_u *s;
1672
1673 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001674 l = utf_ptr2len(p);
1675 if (c == 0)
1676 {
1677 /* overlong sequence, use only the first byte */
1678 c = *p;
1679 l = 1;
1680 }
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001681 lc = utf_tolower(c);
1682
1683 /* Reallocate string when byte count changes. This is rare,
1684 * thus it's OK to do another malloc()/free(). */
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001685 newl = utf_char2len(lc);
1686 if (newl != l)
1687 {
1688 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
1689 if (s == NULL)
1690 {
1691 vim_free(res);
1692 return NULL;
1693 }
1694 mch_memmove(s, res, p - res);
1695 STRCPY(s + (p - res) + newl, p + l);
1696 p = s + (p - res);
1697 vim_free(res);
1698 res = s;
1699 }
1700
1701 utf_char2bytes(lc, p);
1702 p += newl;
1703 }
1704 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1705 p += l; /* skip multi-byte character */
1706 else
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001707 {
1708 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
1709 p++;
1710 }
1711 }
1712
1713 return res;
1714}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001715#endif
1716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 * delete spaces at the end of a string
1719 */
1720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001721del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 char_u *q;
1724
1725 q = ptr + STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001726 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 *q = NUL;
1728}
1729
1730/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001731 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001732 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 */
1734 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001735vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001737 STRNCPY(to, from, len);
1738 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739}
1740
1741/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001742 * Like strcat(), but make sure the result fits in "tosize" bytes and is
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001743 * always NUL terminated. "from" and "to" may overlap.
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001744 */
1745 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001746vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001747{
1748 size_t tolen = STRLEN(to);
1749 size_t fromlen = STRLEN(from);
1750
1751 if (tolen + fromlen + 1 > tosize)
1752 {
1753 mch_memmove(to + tolen, from, tosize - tolen - 1);
1754 to[tosize - 1] = NUL;
1755 }
1756 else
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001757 mch_memmove(to + tolen, from, fromlen + 1);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001758}
1759
1760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 * Isolate one part of a string option where parts are separated with
1762 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001763 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 * "*option" is advanced to the next part.
1765 * The length is returned.
1766 */
1767 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001768copy_option_part(
1769 char_u **option,
1770 char_u *buf,
1771 int maxlen,
1772 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 int len = 0;
1775 char_u *p = *option;
1776
1777 /* skip '.' at start of option part, for 'suffixes' */
1778 if (*p == '.')
1779 buf[len++] = *p++;
1780 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1781 {
1782 /*
1783 * Skip backslash before a separator character and space.
1784 */
1785 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1786 ++p;
1787 if (len < maxlen - 1)
1788 buf[len++] = *p;
1789 ++p;
1790 }
1791 buf[len] = NUL;
1792
1793 if (*p != NUL && *p != ',') /* skip non-standard separator */
1794 ++p;
1795 p = skip_to_option_part(p); /* p points to next file name */
1796
1797 *option = p;
1798 return len;
1799}
1800
1801/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001802 * Replacement for free() that ignores NULL pointers.
1803 * Also skip free() when exiting for sure, this helps when we caught a deadly
1804 * signal that was caused by a crash in free().
Bram Moolenaard23a8232018-02-10 18:45:26 +01001805 * If you want to set NULL after calling this function, you should use
1806 * VIM_CLEAR() instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 */
1808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001811 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813#ifdef MEM_PROFILE
1814 mem_pre_free(&x);
1815#endif
1816 free(x);
1817 }
1818}
1819
1820#ifndef HAVE_MEMSET
1821 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001822vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 char *p = ptr;
1825
1826 while (size-- > 0)
1827 *p++ = c;
1828 return ptr;
1829}
1830#endif
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1833/*
1834 * Compare two strings, ignoring case, using current locale.
1835 * Doesn't work for multi-byte characters.
1836 * return 0 for match, < 0 for smaller, > 0 for bigger
1837 */
1838 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001839vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 int i;
1842
1843 for (;;)
1844 {
1845 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1846 if (i != 0)
1847 return i; /* this character different */
1848 if (*s1 == NUL)
1849 break; /* strings match until NUL */
1850 ++s1;
1851 ++s2;
1852 }
1853 return 0; /* strings match */
1854}
1855#endif
1856
1857#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1858/*
1859 * Compare two strings, for length "len", ignoring case, using current locale.
1860 * Doesn't work for multi-byte characters.
1861 * return 0 for match, < 0 for smaller, > 0 for bigger
1862 */
1863 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001864vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865{
1866 int i;
1867
1868 while (len > 0)
1869 {
1870 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1871 if (i != 0)
1872 return i; /* this character different */
1873 if (*s1 == NUL)
1874 break; /* strings match until NUL */
1875 ++s1;
1876 ++s2;
1877 --len;
1878 }
1879 return 0; /* strings match */
1880}
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/*
1884 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001885 * with characters from 128 to 255 correctly. It also doesn't return a
1886 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 */
1888 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001889vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 char_u *p;
1892 int b;
1893
1894 p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (enc_utf8 && c >= 0x80)
1896 {
1897 while (*p != NUL)
1898 {
Bram Moolenaarace95982017-03-29 17:30:27 +02001899 int l = utfc_ptr2len(p);
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001900
1901 /* Avoid matching an illegal byte here. */
1902 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001904 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 return NULL;
1907 }
1908 if (enc_dbcs != 0 && c > 255)
1909 {
1910 int n2 = c & 0xff;
1911
1912 c = ((unsigned)c >> 8) & 0xff;
1913 while ((b = *p) != NUL)
1914 {
1915 if (b == c && p[1] == n2)
1916 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001917 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919 return NULL;
1920 }
1921 if (has_mbyte)
1922 {
1923 while ((b = *p) != NUL)
1924 {
1925 if (b == c)
1926 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001927 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
1929 return NULL;
1930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 while ((b = *p) != NUL)
1932 {
1933 if (b == c)
1934 return p;
1935 ++p;
1936 }
1937 return NULL;
1938}
1939
1940/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001941 * Version of strchr() that only works for bytes and handles unsigned char
1942 * strings with characters above 128 correctly. It also doesn't return a
1943 * pointer to the NUL at the end of the string.
1944 */
1945 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001946vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001947{
1948 char_u *p = string;
1949
1950 while (*p != NUL)
1951 {
1952 if (*p == c)
1953 return p;
1954 ++p;
1955 }
1956 return NULL;
1957}
1958
1959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001961 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001962 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 */
1964 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001965vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001968 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
Bram Moolenaar05159a02005-02-26 23:04:13 +00001970 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001972 if (*p == c)
1973 retval = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001974 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 return retval;
1977}
1978
1979/*
1980 * Vim's version of strpbrk(), in case it's missing.
1981 * Don't generate a prototype for this, causes problems when it's not used.
1982 */
1983#ifndef PROTO
1984# ifndef HAVE_STRPBRK
1985# ifdef vim_strpbrk
1986# undef vim_strpbrk
1987# endif
1988 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001989vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 while (*s)
1992 {
1993 if (vim_strchr(charset, *s) != NULL)
1994 return s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001995 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997 return NULL;
1998}
1999# endif
2000#endif
2001
2002/*
2003 * Vim has its own isspace() function, because on some machines isspace()
2004 * can't handle characters above 128.
2005 */
2006 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002007vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 return ((x >= 9 && x <= 13) || x == ' ');
2010}
2011
2012/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002013 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 */
2015
2016/*
2017 * Clear an allocated growing array.
2018 */
2019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002020ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
2022 vim_free(gap->ga_data);
2023 ga_init(gap);
2024}
2025
2026/*
2027 * Clear a growing array that contains a list of strings.
2028 */
2029 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 int i;
2033
2034 for (i = 0; i < gap->ga_len; ++i)
2035 vim_free(((char_u **)(gap->ga_data))[i]);
2036 ga_clear(gap);
2037}
2038
2039/*
2040 * Initialize a growing array. Don't forget to set ga_itemsize and
2041 * ga_growsize! Or use ga_init2().
2042 */
2043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002047 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 gap->ga_len = 0;
2049}
2050
2051 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002052ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 ga_init(gap);
2055 gap->ga_itemsize = itemsize;
2056 gap->ga_growsize = growsize;
2057}
2058
2059/*
2060 * Make room in growing array "gap" for at least "n" items.
2061 * Return FAIL for failure, OK otherwise.
2062 */
2063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002064ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002066 size_t old_len;
2067 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 char_u *pp;
2069
Bram Moolenaar86b68352004-12-27 21:59:20 +00002070 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 if (n < gap->ga_growsize)
2073 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002074 new_len = gap->ga_itemsize * (gap->ga_len + n);
2075 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002076 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (pp == NULL)
2078 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002079 old_len = gap->ga_itemsize * gap->ga_maxlen;
2080 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002081 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 gap->ga_data = pp;
2083 }
2084 return OK;
2085}
2086
Bram Moolenaar113e1072019-01-20 15:30:40 +01002087#if defined(FEAT_EVAL) || defined(FEAT_SEARCHPATH) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002089 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002090 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002091 * Returns NULL when out of memory.
2092 */
2093 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002094ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002095{
2096 int i;
2097 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002098 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002099 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002100 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002101
2102 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002103 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002104
2105 s = alloc(len + 1);
2106 if (s != NULL)
2107 {
2108 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002109 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002110 for (i = 0; i < gap->ga_len; ++i)
2111 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002112 if (p != s)
2113 {
2114 STRCPY(p, sep);
2115 p += sep_len;
2116 }
2117 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2118 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002119 }
2120 }
2121 return s;
2122}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002123#endif
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002124
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01002125#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002126/*
2127 * Make a copy of string "p" and add it to "gap".
2128 * When out of memory nothing changes.
2129 */
2130 void
2131ga_add_string(garray_T *gap, char_u *p)
2132{
2133 char_u *cp = vim_strsave(p);
2134
2135 if (cp != NULL)
2136 {
2137 if (ga_grow(gap, 1) == OK)
2138 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2139 else
2140 vim_free(cp);
2141 }
2142}
2143#endif
2144
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002145/*
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002146 * Concatenate a string to a growarray which contains bytes.
Bram Moolenaar43345542015-11-29 17:35:35 +01002147 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * Note: Does NOT copy the NUL at the end!
2149 */
2150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002151ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
Bram Moolenaar43345542015-11-29 17:35:35 +01002153 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaar478af672017-04-10 22:22:42 +02002155 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01002156 return;
2157 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (ga_grow(gap, len) == OK)
2159 {
2160 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2161 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163}
2164
2165/*
2166 * Append one byte to a growarray which contains bytes.
2167 */
2168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002169ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
2171 if (ga_grow(gap, 1) == OK)
2172 {
2173 *((char *)gap->ga_data + gap->ga_len) = c;
2174 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176}
2177
Bram Moolenaar597a4222014-06-25 14:39:50 +02002178#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2179 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002180/*
2181 * Append the text in "gap" below the cursor line and clear "gap".
2182 */
2183 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002184append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002185{
2186 /* Remove trailing CR. */
2187 if (gap->ga_len > 0
2188 && !curbuf->b_p_bin
2189 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2190 --gap->ga_len;
2191 ga_append(gap, NUL);
2192 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2193 gap->ga_len = 0;
2194}
2195#endif
2196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197/************************************************************************
2198 * functions that use lookup tables for various things, generally to do with
2199 * special key codes.
2200 */
2201
2202/*
2203 * Some useful tables.
2204 */
2205
2206static struct modmasktable
2207{
2208 short mod_mask; /* Bit-mask for particular key modifier */
2209 short mod_flag; /* Bit(s) for particular key modifier */
2210 char_u name; /* Single letter name of modifier */
2211} mod_mask_table[] =
2212{
2213 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002214 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2216 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2217 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2218 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2219 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02002220#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2222#endif
2223 /* 'A' must be the last one */
2224 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2225 {0, 0, NUL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002226 /* NOTE: when adding an entry, update MAX_KEY_NAME_LEN! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227};
2228
2229/*
2230 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002231 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 */
2233#define MOD_KEYS_ENTRY_SIZE 5
2234
2235static char_u modifier_keys_table[] =
2236{
2237/* mod mask with modifier without modifier */
2238 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2239 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2240 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2241 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2242 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2243 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2244 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2245 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2246 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2247 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2248 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2249 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2250 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2251 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2252 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2253 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2254 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2255 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2256 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2257 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2258 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2259 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2260 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2261 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2262 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2263 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2264 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2265 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2266 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2267 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2268 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2269 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2270 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2271
2272 /* vt100 F1 */
2273 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2277
2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2288
2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2299
2300 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2301 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2306 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2307 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2308 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2309 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2310
2311 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2312 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2313 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2314 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2315 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2316 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2317 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2318
2319 /* TAB pseudo code*/
2320 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2321
2322 NUL
2323};
2324
2325static struct key_name_entry
2326{
2327 int key; /* Special key code or ascii value */
2328 char_u *name; /* Name of key */
2329} key_names_table[] =
2330{
2331 {' ', (char_u *)"Space"},
2332 {TAB, (char_u *)"Tab"},
2333 {K_TAB, (char_u *)"Tab"},
2334 {NL, (char_u *)"NL"},
2335 {NL, (char_u *)"NewLine"}, /* Alternative name */
2336 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2337 {NL, (char_u *)"LF"}, /* Alternative name */
2338 {CAR, (char_u *)"CR"},
2339 {CAR, (char_u *)"Return"}, /* Alternative name */
2340 {CAR, (char_u *)"Enter"}, /* Alternative name */
2341 {K_BS, (char_u *)"BS"},
2342 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2343 {ESC, (char_u *)"Esc"},
2344 {CSI, (char_u *)"CSI"},
2345 {K_CSI, (char_u *)"xCSI"},
2346 {'|', (char_u *)"Bar"},
2347 {'\\', (char_u *)"Bslash"},
2348 {K_DEL, (char_u *)"Del"},
2349 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2350 {K_KDEL, (char_u *)"kDel"},
2351 {K_UP, (char_u *)"Up"},
2352 {K_DOWN, (char_u *)"Down"},
2353 {K_LEFT, (char_u *)"Left"},
2354 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002355 {K_XUP, (char_u *)"xUp"},
2356 {K_XDOWN, (char_u *)"xDown"},
2357 {K_XLEFT, (char_u *)"xLeft"},
2358 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01002359 {K_PS, (char_u *)"PasteStart"},
2360 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 {K_F1, (char_u *)"F1"},
2363 {K_F2, (char_u *)"F2"},
2364 {K_F3, (char_u *)"F3"},
2365 {K_F4, (char_u *)"F4"},
2366 {K_F5, (char_u *)"F5"},
2367 {K_F6, (char_u *)"F6"},
2368 {K_F7, (char_u *)"F7"},
2369 {K_F8, (char_u *)"F8"},
2370 {K_F9, (char_u *)"F9"},
2371 {K_F10, (char_u *)"F10"},
2372
2373 {K_F11, (char_u *)"F11"},
2374 {K_F12, (char_u *)"F12"},
2375 {K_F13, (char_u *)"F13"},
2376 {K_F14, (char_u *)"F14"},
2377 {K_F15, (char_u *)"F15"},
2378 {K_F16, (char_u *)"F16"},
2379 {K_F17, (char_u *)"F17"},
2380 {K_F18, (char_u *)"F18"},
2381 {K_F19, (char_u *)"F19"},
2382 {K_F20, (char_u *)"F20"},
2383
2384 {K_F21, (char_u *)"F21"},
2385 {K_F22, (char_u *)"F22"},
2386 {K_F23, (char_u *)"F23"},
2387 {K_F24, (char_u *)"F24"},
2388 {K_F25, (char_u *)"F25"},
2389 {K_F26, (char_u *)"F26"},
2390 {K_F27, (char_u *)"F27"},
2391 {K_F28, (char_u *)"F28"},
2392 {K_F29, (char_u *)"F29"},
2393 {K_F30, (char_u *)"F30"},
2394
2395 {K_F31, (char_u *)"F31"},
2396 {K_F32, (char_u *)"F32"},
2397 {K_F33, (char_u *)"F33"},
2398 {K_F34, (char_u *)"F34"},
2399 {K_F35, (char_u *)"F35"},
2400 {K_F36, (char_u *)"F36"},
2401 {K_F37, (char_u *)"F37"},
2402
2403 {K_XF1, (char_u *)"xF1"},
2404 {K_XF2, (char_u *)"xF2"},
2405 {K_XF3, (char_u *)"xF3"},
2406 {K_XF4, (char_u *)"xF4"},
2407
2408 {K_HELP, (char_u *)"Help"},
2409 {K_UNDO, (char_u *)"Undo"},
2410 {K_INS, (char_u *)"Insert"},
2411 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2412 {K_KINS, (char_u *)"kInsert"},
2413 {K_HOME, (char_u *)"Home"},
2414 {K_KHOME, (char_u *)"kHome"},
2415 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002416 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {K_END, (char_u *)"End"},
2418 {K_KEND, (char_u *)"kEnd"},
2419 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002420 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {K_PAGEUP, (char_u *)"PageUp"},
2422 {K_PAGEDOWN, (char_u *)"PageDown"},
2423 {K_KPAGEUP, (char_u *)"kPageUp"},
2424 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2425
2426 {K_KPLUS, (char_u *)"kPlus"},
2427 {K_KMINUS, (char_u *)"kMinus"},
2428 {K_KDIVIDE, (char_u *)"kDivide"},
2429 {K_KMULTIPLY, (char_u *)"kMultiply"},
2430 {K_KENTER, (char_u *)"kEnter"},
2431 {K_KPOINT, (char_u *)"kPoint"},
2432
2433 {K_K0, (char_u *)"k0"},
2434 {K_K1, (char_u *)"k1"},
2435 {K_K2, (char_u *)"k2"},
2436 {K_K3, (char_u *)"k3"},
2437 {K_K4, (char_u *)"k4"},
2438 {K_K5, (char_u *)"k5"},
2439 {K_K6, (char_u *)"k6"},
2440 {K_K7, (char_u *)"k7"},
2441 {K_K8, (char_u *)"k8"},
2442 {K_K9, (char_u *)"k9"},
2443
2444 {'<', (char_u *)"lt"},
2445
2446 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002447#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002449#endif
2450#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002452#endif
2453#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002455#endif
2456#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002458#endif
2459#ifdef FEAT_MOUSE_URXVT
2460 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2461#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002462#ifdef FEAT_MOUSE_SGR
2463 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaara529ce02017-06-22 22:37:57 +02002464 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2467 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2468 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2469 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2470 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002471 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2473 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2474 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2475 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2476 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2477 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002478 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2479 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2480 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2481 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2482 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2483 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {K_X1MOUSE, (char_u *)"X1Mouse"},
2485 {K_X1DRAG, (char_u *)"X1Drag"},
2486 {K_X1RELEASE, (char_u *)"X1Release"},
2487 {K_X2MOUSE, (char_u *)"X2Mouse"},
2488 {K_X2DRAG, (char_u *)"X2Drag"},
2489 {K_X2RELEASE, (char_u *)"X2Release"},
2490 {K_DROP, (char_u *)"Drop"},
2491 {K_ZERO, (char_u *)"Nul"},
2492#ifdef FEAT_EVAL
2493 {K_SNR, (char_u *)"SNR"},
2494#endif
2495 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002496 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {0, NULL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002498 /* NOTE: When adding a long name update MAX_KEY_NAME_LEN. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499};
2500
2501#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2502
2503#ifdef FEAT_MOUSE
2504static struct mousetable
2505{
2506 int pseudo_code; /* Code for pseudo mouse event */
2507 int button; /* Which mouse button is it? */
2508 int is_click; /* Is it a mouse button click event? */
2509 int is_drag; /* Is it a mouse drag event? */
2510} mouse_table[] =
2511{
2512 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2513#ifdef FEAT_GUI
2514 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2515#endif
2516 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2517 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2518#ifdef FEAT_GUI
2519 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2520#endif
2521 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2522 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2523 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2524 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2525 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2526 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2527 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2528 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2529 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2530 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2531 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2532 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2533 /* DRAG without CLICK */
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002534 {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 /* RELEASE without CLICK */
2536 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2537 {0, 0, 0, 0},
2538};
2539#endif /* FEAT_MOUSE */
2540
2541/*
2542 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2543 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2544 */
2545 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002546name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 int i;
2549
2550 c = TOUPPER_ASC(c);
2551 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2552 if (c == mod_mask_table[i].name)
2553 return mod_mask_table[i].mod_flag;
2554 return 0;
2555}
2556
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557/*
2558 * Check if if there is a special key code for "key" that includes the
2559 * modifiers specified.
2560 */
2561 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002562simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 int i;
2565 int key0;
2566 int key1;
2567
2568 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2569 {
2570 /* TAB is a special case */
2571 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2572 {
2573 *modifiers &= ~MOD_MASK_SHIFT;
2574 return K_S_TAB;
2575 }
2576 key0 = KEY2TERMCAP0(key);
2577 key1 = KEY2TERMCAP1(key);
2578 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2579 if (key0 == modifier_keys_table[i + 3]
2580 && key1 == modifier_keys_table[i + 4]
2581 && (*modifiers & modifier_keys_table[i]))
2582 {
2583 *modifiers &= ~modifier_keys_table[i];
2584 return TERMCAP2KEY(modifier_keys_table[i + 1],
2585 modifier_keys_table[i + 2]);
2586 }
2587 }
2588 return key;
2589}
2590
2591/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002592 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002593 */
2594 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002595handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002596{
2597 switch (key)
2598 {
2599 case K_XUP: return K_UP;
2600 case K_XDOWN: return K_DOWN;
2601 case K_XLEFT: return K_LEFT;
2602 case K_XRIGHT: return K_RIGHT;
2603 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002604 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002605 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002606 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002607 case K_XF1: return K_F1;
2608 case K_XF2: return K_F2;
2609 case K_XF3: return K_F3;
2610 case K_XF4: return K_F4;
2611 case K_S_XF1: return K_S_F1;
2612 case K_S_XF2: return K_S_F2;
2613 case K_S_XF3: return K_S_F3;
2614 case K_S_XF4: return K_S_F4;
2615 }
2616 return key;
2617}
2618
2619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 * Return a string which contains the name of the given key when the given
2621 * modifiers are down.
2622 */
2623 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002624get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 static char_u string[MAX_KEY_NAME_LEN + 1];
2627
2628 int i, idx;
2629 int table_idx;
2630 char_u *s;
2631
2632 string[0] = '<';
2633 idx = 1;
2634
2635 /* Key that stands for a normal character. */
2636 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2637 c = KEY2TERMCAP1(c);
2638
2639 /*
2640 * Translate shifted special keys into unshifted keys and set modifier.
2641 * Same for CTRL and ALT modifiers.
2642 */
2643 if (IS_SPECIAL(c))
2644 {
2645 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2646 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2647 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2648 {
2649 modifiers |= modifier_keys_table[i];
2650 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2651 modifier_keys_table[i + 4]);
2652 break;
2653 }
2654 }
2655
2656 /* try to find the key in the special key table */
2657 table_idx = find_special_key_in_table(c);
2658
2659 /*
2660 * When not a known special key, and not a printable character, try to
2661 * extract modifiers.
2662 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002663 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 if (table_idx < 0
2666 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2667 && (c & 0x80))
2668 {
2669 c &= 0x7f;
2670 modifiers |= MOD_MASK_ALT;
2671 /* try again, to find the un-alted key in the special key table */
2672 table_idx = find_special_key_in_table(c);
2673 }
2674 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2675 {
2676#ifdef EBCDIC
2677 c = CtrlChar(c);
2678#else
2679 c += '@';
2680#endif
2681 modifiers |= MOD_MASK_CTRL;
2682 }
2683 }
2684
2685 /* translate the modifier into a string */
2686 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2687 if ((modifiers & mod_mask_table[i].mod_mask)
2688 == mod_mask_table[i].mod_flag)
2689 {
2690 string[idx++] = mod_mask_table[i].name;
2691 string[idx++] = (char_u)'-';
2692 }
2693
2694 if (table_idx < 0) /* unknown special key, may output t_xx */
2695 {
2696 if (IS_SPECIAL(c))
2697 {
2698 string[idx++] = 't';
2699 string[idx++] = '_';
2700 string[idx++] = KEY2TERMCAP0(c);
2701 string[idx++] = KEY2TERMCAP1(c);
2702 }
2703 /* Not a special key, only modifiers, output directly */
2704 else
2705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (has_mbyte && (*mb_char2len)(c) > 1)
2707 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002708 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 string[idx++] = c;
2710 else
2711 {
2712 s = transchar(c);
2713 while (*s)
2714 string[idx++] = *s++;
2715 }
2716 }
2717 }
2718 else /* use name of special key */
2719 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002720 size_t len = STRLEN(key_names_table[table_idx].name);
2721
2722 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2723 {
2724 STRCPY(string + idx, key_names_table[table_idx].name);
2725 idx += (int)len;
2726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 string[idx++] = '>';
2729 string[idx] = NUL;
2730 return string;
2731}
2732
2733/*
2734 * Try translating a <> name at (*srcp)[] to dst[].
2735 * Return the number of characters added to dst[], zero for no match.
2736 * If there is a match, srcp is advanced to after the <> name.
2737 * dst[] must be big enough to hold the result (up to six characters)!
2738 */
2739 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002740trans_special(
2741 char_u **srcp,
2742 char_u *dst,
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002743 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2744 int in_string) /* TRUE when inside a double quoted string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 int modifiers = 0;
2747 int key;
2748 int dlen = 0;
2749
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002750 key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (key == 0)
2752 return 0;
2753
2754 /* Put the appropriate modifier in a string */
2755 if (modifiers != 0)
2756 {
2757 dst[dlen++] = K_SPECIAL;
2758 dst[dlen++] = KS_MODIFIER;
2759 dst[dlen++] = modifiers;
2760 }
2761
2762 if (IS_SPECIAL(key))
2763 {
2764 dst[dlen++] = K_SPECIAL;
2765 dst[dlen++] = KEY2TERMCAP0(key);
2766 dst[dlen++] = KEY2TERMCAP1(key);
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 else if (has_mbyte && !keycode)
2769 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else if (keycode)
2771 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2772 else
2773 dst[dlen++] = key;
2774
2775 return dlen;
2776}
2777
2778/*
2779 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2780 * srcp is advanced to after the <> name.
2781 * returns 0 if there is no match.
2782 */
2783 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002784find_special_key(
2785 char_u **srcp,
2786 int *modp,
2787 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002788 int keep_x_key, /* don't translate xHome to Home key */
2789 int in_string) /* TRUE in string, double quote is escaped */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 char_u *last_dash;
2792 char_u *end_of_name;
2793 char_u *src;
2794 char_u *bp;
2795 int modifiers;
2796 int bit;
2797 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002798 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002799 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
2801 src = *srcp;
2802 if (src[0] != '<')
2803 return 0;
2804
2805 /* Find end of modifier list */
2806 last_dash = src;
2807 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2808 {
2809 if (*bp == '-')
2810 {
2811 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002812 if (bp[1] != NUL)
2813 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002814 if (has_mbyte)
2815 l = mb_ptr2len(bp + 1);
2816 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002817 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002818 /* Anything accepted, like <C-?>.
2819 * <C-"> or <M-"> are not special in strings as " is
2820 * the string delimiter. With a backslash it works: <M-\"> */
2821 if (!(in_string && bp[1] == '"') && bp[2] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002822 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002823 else if (in_string && bp[1] == '\\' && bp[2] == '"'
2824 && bp[3] == '>')
2825 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2829 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002830 else if (STRNICMP(bp, "char-", 5) == 0)
2831 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002832 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002833 bp += l + 5;
2834 break;
2835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837
2838 if (*bp == '>') /* found matching '>' */
2839 {
2840 end_of_name = bp + 1;
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 /* Which modifiers are given? */
2843 modifiers = 0x0;
2844 for (bp = src + 1; bp < last_dash; bp++)
2845 {
2846 if (*bp != '-')
2847 {
2848 bit = name_to_mod_mask(*bp);
2849 if (bit == 0x0)
2850 break; /* Illegal modifier name */
2851 modifiers |= bit;
2852 }
2853 }
2854
2855 /*
2856 * Legal modifier name.
2857 */
2858 if (bp >= last_dash)
2859 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002860 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2861 && VIM_ISDIGIT(last_dash[6]))
2862 {
2863 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002864 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002865 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002868 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002869 int off = 1;
2870
2871 /* Modifier with single letter, or special key name. */
2872 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2873 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002874 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002875 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002876 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02002877 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002878 if (modifiers != 0 && last_dash[l + off] == '>')
2879 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002880 else
2881 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002882 key = get_special_key_code(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002883 if (!keep_x_key)
2884 key = handle_x_keys(key);
2885 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
2888 /*
2889 * get_special_key_code() may return NUL for invalid
2890 * special key name.
2891 */
2892 if (key != NUL)
2893 {
2894 /*
2895 * Only use a modifier when there is no special key code that
2896 * includes the modifier.
2897 */
2898 key = simplify_key(key, &modifiers);
2899
2900 if (!keycode)
2901 {
2902 /* don't want keycode, use single byte code */
2903 if (key == K_BS)
2904 key = BS;
2905 else if (key == K_DEL || key == K_KDEL)
2906 key = DEL;
2907 }
2908
2909 /*
2910 * Normal Key with modifier: Try to make a single byte code.
2911 */
2912 if (!IS_SPECIAL(key))
2913 key = extract_modifiers(key, &modifiers);
2914
2915 *modp = modifiers;
2916 *srcp = end_of_name;
2917 return key;
2918 }
2919 }
2920 }
2921 return 0;
2922}
2923
2924/*
2925 * Try to include modifiers in the key.
2926 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2927 */
2928 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002929extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 int modifiers = *modp;
2932
Bram Moolenaard0573012017-10-28 21:11:06 +02002933#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002934 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (!(modifiers & MOD_MASK_CMD))
2936#endif
2937 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2938 {
2939 key = TOUPPER_ASC(key);
2940 modifiers &= ~MOD_MASK_SHIFT;
2941 }
2942 if ((modifiers & MOD_MASK_CTRL)
2943#ifdef EBCDIC
2944 /* * TODO: EBCDIC Better use:
2945 * && (Ctrl_chr(key) || key == '?')
2946 * ??? */
2947 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2948 != NULL
2949#else
2950 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2951#endif
2952 )
2953 {
2954 key = Ctrl_chr(key);
2955 modifiers &= ~MOD_MASK_CTRL;
2956 /* <C-@> is <Nul> */
2957 if (key == 0)
2958 key = K_ZERO;
2959 }
Bram Moolenaard0573012017-10-28 21:11:06 +02002960#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002961 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if (!(modifiers & MOD_MASK_CMD))
2963#endif
2964 if ((modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002965 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
2967 key |= 0x80;
2968 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2969 }
2970
2971 *modp = modifiers;
2972 return key;
2973}
2974
2975/*
2976 * Try to find key "c" in the special key table.
2977 * Return the index when found, -1 when not found.
2978 */
2979 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002980find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 int i;
2983
2984 for (i = 0; key_names_table[i].name != NULL; i++)
2985 if (c == key_names_table[i].key)
2986 break;
2987 if (key_names_table[i].name == NULL)
2988 i = -1;
2989 return i;
2990}
2991
2992/*
2993 * Find the special key with the given name (the given string does not have to
2994 * end with NUL, the name is assumed to end before the first non-idchar).
2995 * If the name starts with "t_" the next two characters are interpreted as a
2996 * termcap name.
2997 * Return the key code, or 0 if not found.
2998 */
2999 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003000get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 char_u *table_name;
3003 char_u string[3];
3004 int i, j;
3005
3006 /*
3007 * If it's <t_xx> we get the code for xx from the termcap
3008 */
3009 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3010 {
3011 string[0] = name[2];
3012 string[1] = name[3];
3013 string[2] = NUL;
3014 if (add_termcap_entry(string, FALSE) == OK)
3015 return TERMCAP2KEY(name[2], name[3]);
3016 }
3017 else
3018 for (i = 0; key_names_table[i].name != NULL; i++)
3019 {
3020 table_name = key_names_table[i].name;
3021 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3022 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3023 break;
3024 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3025 return key_names_table[i].key;
3026 }
3027 return 0;
3028}
3029
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003030#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003032get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003034 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 return NULL;
3036 return key_names_table[i].name;
3037}
3038#endif
3039
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003040#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041/*
3042 * Look up the given mouse code to return the relevant information in the other
3043 * arguments. Return which button is down or was released.
3044 */
3045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003046get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 int i;
3049
3050 for (i = 0; mouse_table[i].pseudo_code; i++)
3051 if (code == mouse_table[i].pseudo_code)
3052 {
3053 *is_click = mouse_table[i].is_click;
3054 *is_drag = mouse_table[i].is_drag;
3055 return mouse_table[i].button;
3056 }
3057 return 0; /* Shouldn't get here */
3058}
3059
3060/*
3061 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3062 * the given information about which mouse button is down, and whether the
3063 * mouse was clicked, dragged or released.
3064 */
3065 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003066get_pseudo_mouse_code(
3067 int button, /* eg MOUSE_LEFT */
3068 int is_click,
3069 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 int i;
3072
3073 for (i = 0; mouse_table[i].pseudo_code; i++)
3074 if (button == mouse_table[i].button
3075 && is_click == mouse_table[i].is_click
3076 && is_drag == mouse_table[i].is_drag)
3077 {
3078#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003079 /* Trick: a non mappable left click and release has mouse_col -1
3080 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3081 * gui_mouse_moved() */
3082 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003084 if (mouse_col < 0)
3085 mouse_col = 0;
3086 else
3087 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3089 return (int)KE_LEFTMOUSE_NM;
3090 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3091 return (int)KE_LEFTRELEASE_NM;
3092 }
3093#endif
3094 return mouse_table[i].pseudo_code;
3095 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003096 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097}
3098#endif /* FEAT_MOUSE */
3099
3100/*
3101 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3102 */
3103 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003104get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 int c = *buf->b_p_ff;
3107
3108 if (buf->b_p_bin || c == 'u')
3109 return EOL_UNIX;
3110 if (c == 'm')
3111 return EOL_MAC;
3112 return EOL_DOS;
3113}
3114
3115/*
3116 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3117 * argument.
3118 */
3119 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003120get_fileformat_force(
3121 buf_T *buf,
3122 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 int c;
3125
3126 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003127 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 else
3129 {
3130 if ((eap != NULL && eap->force_bin != 0)
3131 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3132 return EOL_UNIX;
3133 c = *buf->b_p_ff;
3134 }
3135 if (c == 'u')
3136 return EOL_UNIX;
3137 if (c == 'm')
3138 return EOL_MAC;
3139 return EOL_DOS;
3140}
3141
3142/*
3143 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3144 * Sets both 'textmode' and 'fileformat'.
3145 * Note: Does _not_ set global value of 'textmode'!
3146 */
3147 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003148set_fileformat(
3149 int t,
3150 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 char *p = NULL;
3153
3154 switch (t)
3155 {
3156 case EOL_DOS:
3157 p = FF_DOS;
3158 curbuf->b_p_tx = TRUE;
3159 break;
3160 case EOL_UNIX:
3161 p = FF_UNIX;
3162 curbuf->b_p_tx = FALSE;
3163 break;
3164 case EOL_MAC:
3165 p = FF_MAC;
3166 curbuf->b_p_tx = FALSE;
3167 break;
3168 }
3169 if (p != NULL)
3170 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003171 OPT_FREE | opt_flags, 0);
3172
Bram Moolenaarf740b292006-02-16 22:11:02 +00003173 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003175 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#ifdef FEAT_TITLE
3177 need_maketitle = TRUE; /* set window title later */
3178#endif
3179}
3180
3181/*
3182 * Return the default fileformat from 'fileformats'.
3183 */
3184 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003185default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 switch (*p_ffs)
3188 {
3189 case 'm': return EOL_MAC;
3190 case 'd': return EOL_DOS;
3191 }
3192 return EOL_UNIX;
3193}
3194
3195/*
3196 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3197 */
3198 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003199call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 char_u *ncmd;
3202 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003203#ifdef FEAT_PROFILE
3204 proftime_T wait_time;
3205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 if (p_verbose > 3)
3208 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003209 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003210 smsg(_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 cmd == NULL ? p_sh : cmd);
3212 out_char('\n');
3213 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003214 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216
Bram Moolenaar05159a02005-02-26 23:04:13 +00003217#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003218 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003219 prof_child_enter(&wait_time);
3220#endif
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (*p_sh == NUL)
3223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003224 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 retval = -1;
3226 }
3227 else
3228 {
3229#ifdef FEAT_GUI_MSWIN
3230 /* Don't hide the pointer while executing a shell command. */
3231 gui_mch_mousehide(FALSE);
3232#endif
3233#ifdef FEAT_GUI
3234 ++hold_gui_events;
3235#endif
3236 /* The external command may update a tags file, clear cached tags. */
3237 tag_freematch();
3238
3239 if (cmd == NULL || *p_sxq == NUL)
3240 retval = mch_call_shell(cmd, opt);
3241 else
3242 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003243 char_u *ecmd = cmd;
3244
3245 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3246 {
3247 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3248 if (ecmd == NULL)
3249 ecmd = cmd;
3250 }
3251 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (ncmd != NULL)
3253 {
3254 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003255 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003256 /* When 'shellxquote' is ( append ).
3257 * When 'shellxquote' is "( append )". */
3258 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3259 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3260 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 retval = mch_call_shell(ncmd, opt);
3262 vim_free(ncmd);
3263 }
3264 else
3265 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003266 if (ecmd != cmd)
3267 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269#ifdef FEAT_GUI
3270 --hold_gui_events;
3271#endif
3272 /*
3273 * Check the window size, in case it changed while executing the
3274 * external command.
3275 */
3276 shell_resized_check();
3277 }
3278
3279#ifdef FEAT_EVAL
3280 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003281# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003282 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003283 prof_child_exit(&wait_time);
3284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#endif
3286
3287 return retval;
3288}
3289
3290/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003291 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3292 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 */
3294 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003295get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 if (State & NORMAL)
3298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003300 {
3301 if (VIsual_select)
3302 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003304 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003305 else if (finish_op)
3306 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 return State;
3309}
3310
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003311/*
3312 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003313 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003314 * "b" must point to the start of the file name
3315 */
3316 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003317after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003318{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003319 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003320 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3321}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003322
3323/*
3324 * Return TRUE if file names "f1" and "f2" are in the same directory.
3325 * "f1" may be a short name, "f2" must be a full path.
3326 */
3327 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003328same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003329{
3330 char_u ffname[MAXPATHL];
3331 char_u *t1;
3332 char_u *t2;
3333
3334 /* safety check */
3335 if (f1 == NULL || f2 == NULL)
3336 return FALSE;
3337
3338 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3339 t1 = gettail_sep(ffname);
3340 t2 = gettail_sep(f2);
3341 return (t1 - ffname == t2 - f2
3342 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3343}
3344
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02003345#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
3346 || defined(MSWIN) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01003347 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 || defined(PROTO)
3349/*
3350 * Change to a file's directory.
3351 * Caller must call shorten_fnames()!
3352 * Return OK or FAIL.
3353 */
3354 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003355vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003357 char_u old_dir[MAXPATHL];
3358 char_u new_dir[MAXPATHL];
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003359 int res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003361 if (mch_dirname(old_dir, MAXPATHL) != OK)
3362 *old_dir = NUL;
3363
3364 vim_strncpy(new_dir, fname, MAXPATHL - 1);
3365 *gettail_sep(new_dir) = NUL;
3366
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01003367 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003368 // nothing to do
3369 res = OK;
3370 else
3371 {
3372 res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
3373
3374 if (res == OK && trigger_autocmd != NULL)
3375 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
3376 new_dir, FALSE, curbuf);
3377 }
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003378 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380#endif
3381
3382#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3383/*
3384 * Check if "name" ends in a slash and is not a directory.
3385 * Used for systems where stat() ignores a trailing slash on a file name.
3386 * The Vim code assumes a trailing slash is only ignored for a directory.
3387 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003388 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01003389illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 if (name[0] == NUL)
3392 return FALSE; /* no file name is not illegal */
3393 if (name[strlen(name) - 1] != '/')
3394 return FALSE; /* no trailing slash */
3395 if (mch_isdir((char_u *)name))
3396 return FALSE; /* trailing slash for a directory */
3397 return TRUE;
3398}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003399
3400/*
3401 * Special implementation of mch_stat() for Solaris.
3402 */
3403 int
3404vim_stat(const char *name, stat_T *stp)
3405{
3406 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
3407 * the name ends in "/" and it's not a directory. */
Bram Moolenaard8492792017-03-16 12:22:38 +01003408 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003409}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#endif
3411
3412#if defined(CURSOR_SHAPE) || defined(PROTO)
3413
3414/*
3415 * Handling of cursor and mouse pointer shapes in various modes.
3416 */
3417
3418cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3419{
3420 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3421 * defaults when Vim starts.
3422 * Adjust the SHAPE_IDX_ defines when making changes! */
3423 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3424 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3425 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3426 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3427 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3428 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3429 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3430 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3431 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3432 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3433 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3434 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3435 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3436 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3437 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3438 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3439 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3440};
3441
3442#ifdef FEAT_MOUSESHAPE
3443/*
3444 * Table with names for mouse shapes. Keep in sync with all the tables for
3445 * mch_set_mouse_shape()!.
3446 */
3447static char * mshape_names[] =
3448{
3449 "arrow", /* default, must be the first one */
3450 "blank", /* hidden */
3451 "beam",
3452 "updown",
3453 "udsizing",
3454 "leftright",
3455 "lrsizing",
3456 "busy",
3457 "no",
3458 "crosshair",
3459 "hand1",
3460 "hand2",
3461 "pencil",
3462 "question",
3463 "rightup-arrow",
3464 "up-arrow",
3465 NULL
3466};
3467#endif
3468
3469/*
3470 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3471 * ("what" is SHAPE_MOUSE).
3472 * Returns error message for an illegal option, NULL otherwise.
3473 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003474 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003475parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 char_u *modep;
3478 char_u *colonp;
3479 char_u *commap;
3480 char_u *slashp;
3481 char_u *p, *endp;
3482 int idx = 0; /* init for GCC */
3483 int all_idx;
3484 int len;
3485 int i;
3486 long n;
3487 int found_ve = FALSE; /* found "ve" flag */
3488 int round;
3489
3490 /*
3491 * First round: check for errors; second round: do it for real.
3492 */
3493 for (round = 1; round <= 2; ++round)
3494 {
3495 /*
3496 * Repeat for all comma separated parts.
3497 */
3498#ifdef FEAT_MOUSESHAPE
3499 if (what == SHAPE_MOUSE)
3500 modep = p_mouseshape;
3501 else
3502#endif
3503 modep = p_guicursor;
3504 while (*modep != NUL)
3505 {
3506 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01003507 commap = vim_strchr(modep, ',');
3508
3509 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003510 return N_("E545: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (colonp == modep)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003512 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 /*
3515 * Repeat for all mode's before the colon.
3516 * For the 'a' mode, we loop to handle all the modes.
3517 */
3518 all_idx = -1;
3519 while (modep < colonp || all_idx >= 0)
3520 {
3521 if (all_idx < 0)
3522 {
3523 /* Find the mode. */
3524 if (modep[1] == '-' || modep[1] == ':')
3525 len = 1;
3526 else
3527 len = 2;
3528 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3529 all_idx = SHAPE_IDX_COUNT - 1;
3530 else
3531 {
3532 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3533 if (STRNICMP(modep, shape_table[idx].name, len)
3534 == 0)
3535 break;
3536 if (idx == SHAPE_IDX_COUNT
3537 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003538 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3540 found_ve = TRUE;
3541 }
3542 modep += len + 1;
3543 }
3544
3545 if (all_idx >= 0)
3546 idx = all_idx--;
3547 else if (round == 2)
3548 {
3549#ifdef FEAT_MOUSESHAPE
3550 if (what == SHAPE_MOUSE)
3551 {
3552 /* Set the default, for the missing parts */
3553 shape_table[idx].mshape = 0;
3554 }
3555 else
3556#endif
3557 {
3558 /* Set the defaults, for the missing parts */
3559 shape_table[idx].shape = SHAPE_BLOCK;
3560 shape_table[idx].blinkwait = 700L;
3561 shape_table[idx].blinkon = 400L;
3562 shape_table[idx].blinkoff = 250L;
3563 }
3564 }
3565
3566 /* Parse the part after the colon */
3567 for (p = colonp + 1; *p && *p != ','; )
3568 {
3569#ifdef FEAT_MOUSESHAPE
3570 if (what == SHAPE_MOUSE)
3571 {
3572 for (i = 0; ; ++i)
3573 {
3574 if (mshape_names[i] == NULL)
3575 {
3576 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003577 return N_("E547: Illegal mouseshape");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (round == 2)
3579 shape_table[idx].mshape =
3580 getdigits(&p) + MSHAPE_NUMBERED;
3581 else
3582 (void)getdigits(&p);
3583 break;
3584 }
3585 len = (int)STRLEN(mshape_names[i]);
3586 if (STRNICMP(p, mshape_names[i], len) == 0)
3587 {
3588 if (round == 2)
3589 shape_table[idx].mshape = i;
3590 p += len;
3591 break;
3592 }
3593 }
3594 }
3595 else /* if (what == SHAPE_MOUSE) */
3596#endif
3597 {
3598 /*
3599 * First handle the ones with a number argument.
3600 */
3601 i = *p;
3602 len = 0;
3603 if (STRNICMP(p, "ver", 3) == 0)
3604 len = 3;
3605 else if (STRNICMP(p, "hor", 3) == 0)
3606 len = 3;
3607 else if (STRNICMP(p, "blinkwait", 9) == 0)
3608 len = 9;
3609 else if (STRNICMP(p, "blinkon", 7) == 0)
3610 len = 7;
3611 else if (STRNICMP(p, "blinkoff", 8) == 0)
3612 len = 8;
3613 if (len != 0)
3614 {
3615 p += len;
3616 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003617 return N_("E548: digit expected");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 n = getdigits(&p);
3619 if (len == 3) /* "ver" or "hor" */
3620 {
3621 if (n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003622 return N_("E549: Illegal percentage");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (round == 2)
3624 {
3625 if (TOLOWER_ASC(i) == 'v')
3626 shape_table[idx].shape = SHAPE_VER;
3627 else
3628 shape_table[idx].shape = SHAPE_HOR;
3629 shape_table[idx].percentage = n;
3630 }
3631 }
3632 else if (round == 2)
3633 {
3634 if (len == 9)
3635 shape_table[idx].blinkwait = n;
3636 else if (len == 7)
3637 shape_table[idx].blinkon = n;
3638 else
3639 shape_table[idx].blinkoff = n;
3640 }
3641 }
3642 else if (STRNICMP(p, "block", 5) == 0)
3643 {
3644 if (round == 2)
3645 shape_table[idx].shape = SHAPE_BLOCK;
3646 p += 5;
3647 }
3648 else /* must be a highlight group name then */
3649 {
3650 endp = vim_strchr(p, '-');
3651 if (commap == NULL) /* last part */
3652 {
3653 if (endp == NULL)
3654 endp = p + STRLEN(p); /* find end of part */
3655 }
3656 else if (endp > commap || endp == NULL)
3657 endp = commap;
3658 slashp = vim_strchr(p, '/');
3659 if (slashp != NULL && slashp < endp)
3660 {
3661 /* "group/langmap_group" */
3662 i = syn_check_group(p, (int)(slashp - p));
3663 p = slashp + 1;
3664 }
3665 if (round == 2)
3666 {
3667 shape_table[idx].id = syn_check_group(p,
3668 (int)(endp - p));
3669 shape_table[idx].id_lm = shape_table[idx].id;
3670 if (slashp != NULL && slashp < endp)
3671 shape_table[idx].id = i;
3672 }
3673 p = endp;
3674 }
3675 } /* if (what != SHAPE_MOUSE) */
3676
3677 if (*p == '-')
3678 ++p;
3679 }
3680 }
3681 modep = p;
3682 if (*modep == ',')
3683 ++modep;
3684 }
3685 }
3686
3687 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3688 if (!found_ve)
3689 {
3690#ifdef FEAT_MOUSESHAPE
3691 if (what == SHAPE_MOUSE)
3692 {
3693 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3694 }
3695 else
3696#endif
3697 {
3698 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3699 shape_table[SHAPE_IDX_VE].percentage =
3700 shape_table[SHAPE_IDX_V].percentage;
3701 shape_table[SHAPE_IDX_VE].blinkwait =
3702 shape_table[SHAPE_IDX_V].blinkwait;
3703 shape_table[SHAPE_IDX_VE].blinkon =
3704 shape_table[SHAPE_IDX_V].blinkon;
3705 shape_table[SHAPE_IDX_VE].blinkoff =
3706 shape_table[SHAPE_IDX_V].blinkoff;
3707 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3708 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3709 }
3710 }
3711
3712 return NULL;
3713}
3714
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003715# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3716 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717/*
3718 * Return the index into shape_table[] for the current mode.
3719 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3720 */
3721 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003722get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724#ifdef FEAT_MOUSESHAPE
3725 if (mouse && (State == HITRETURN || State == ASKMORE))
3726 {
3727# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003728 int x, y;
3729 gui_mch_getmouse(&x, &y);
3730 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return SHAPE_IDX_MOREL;
3732# endif
3733 return SHAPE_IDX_MORE;
3734 }
3735 if (mouse && drag_status_line)
3736 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (mouse && drag_sep_line)
3738 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739#endif
3740 if (!mouse && State == SHOWMATCH)
3741 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (State & VREPLACE_FLAG)
3743 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (State & REPLACE_FLAG)
3745 return SHAPE_IDX_R;
3746 if (State & INSERT)
3747 return SHAPE_IDX_I;
3748 if (State & CMDLINE)
3749 {
3750 if (cmdline_at_end())
3751 return SHAPE_IDX_C;
3752 if (cmdline_overstrike())
3753 return SHAPE_IDX_CR;
3754 return SHAPE_IDX_CI;
3755 }
3756 if (finish_op)
3757 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 if (VIsual_active)
3759 {
3760 if (*p_sel == 'e')
3761 return SHAPE_IDX_VE;
3762 else
3763 return SHAPE_IDX_V;
3764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return SHAPE_IDX_N;
3766}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3770static int old_mouse_shape = 0;
3771
3772/*
3773 * Set the mouse shape:
3774 * If "shape" is -1, use shape depending on the current mode,
3775 * depending on the current state.
3776 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3777 * when the mouse moves off the status or command line).
3778 */
3779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003780update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
3782 int new_mouse_shape;
3783
3784 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003785 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return;
3787
3788 /* Postpone the updating when more is to come. Speeds up executing of
3789 * mappings. */
3790 if (shape_idx == -1 && char_avail())
3791 {
3792 postponed_mouseshape = TRUE;
3793 return;
3794 }
3795
Bram Moolenaar14716812006-05-04 21:54:08 +00003796 /* When ignoring the mouse don't change shape on the statusline. */
3797 if (*p_mouse == NUL
3798 && (shape_idx == SHAPE_IDX_CLINE
3799 || shape_idx == SHAPE_IDX_STATUS
3800 || shape_idx == SHAPE_IDX_VSEP))
3801 shape_idx = -2;
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (shape_idx == -2
3804 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3805 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3806 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3807 return;
3808 if (shape_idx < 0)
3809 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3810 else
3811 new_mouse_shape = shape_table[shape_idx].mshape;
3812 if (new_mouse_shape != old_mouse_shape)
3813 {
3814 mch_set_mouse_shape(new_mouse_shape);
3815 old_mouse_shape = new_mouse_shape;
3816 }
3817 postponed_mouseshape = FALSE;
3818}
3819# endif
3820
3821#endif /* CURSOR_SHAPE */
3822
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824/* TODO: make some #ifdef for this */
3825/*--------[ file searching ]-------------------------------------------------*/
3826/*
3827 * File searching functions for 'path', 'tags' and 'cdpath' options.
3828 * External visible functions:
3829 * vim_findfile_init() creates/initialises the search context
3830 * vim_findfile_free_visited() free list of visited files/dirs of search
3831 * context
3832 * vim_findfile() find a file in the search context
3833 * vim_findfile_cleanup() cleanup/free search context created by
3834 * vim_findfile_init()
3835 *
3836 * All static functions and variables start with 'ff_'
3837 *
3838 * In general it works like this:
3839 * First you create yourself a search context by calling vim_findfile_init().
3840 * It is possible to give a search context from a previous call to
3841 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3842 * until you are satisfied with the result or it returns NULL. On every call it
3843 * returns the next file which matches the conditions given to
3844 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3845 *
3846 * It is possible to call vim_findfile_init() again to reinitialise your search
3847 * with some new parameters. Don't forget to pass your old search context to
3848 * it, so it can reuse it and especially reuse the list of already visited
3849 * directories. If you want to delete the list of already visited directories
3850 * simply call vim_findfile_free_visited().
3851 *
3852 * When you are done call vim_findfile_cleanup() to free the search context.
3853 *
3854 * The function vim_findfile_init() has a long comment, which describes the
3855 * needed parameters.
3856 *
3857 *
3858 *
3859 * ATTENTION:
3860 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003861 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 * thread-safe!!!!!
3863 *
3864 * To minimize parameter passing (or because I'm to lazy), only the
3865 * external visible functions get a search context as a parameter. This is
3866 * then assigned to a static global, which is used throughout the local
3867 * functions.
3868 */
3869
3870/*
3871 * type for the directory search stack
3872 */
3873typedef struct ff_stack
3874{
3875 struct ff_stack *ffs_prev;
3876
3877 /* the fix part (no wildcards) and the part containing the wildcards
3878 * of the search path
3879 */
3880 char_u *ffs_fix_path;
3881#ifdef FEAT_PATH_EXTRA
3882 char_u *ffs_wc_path;
3883#endif
3884
3885 /* files/dirs found in the above directory, matched by the first wildcard
3886 * of wc_part
3887 */
3888 char_u **ffs_filearray;
3889 int ffs_filearray_size;
3890 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3891
3892 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003893 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 int ffs_stage;
3897
3898 /* How deep are we in the directory tree?
3899 * Counts backward from value of level parameter to vim_findfile_init
3900 */
3901 int ffs_level;
3902
3903 /* Did we already expand '**' to an empty string? */
3904 int ffs_star_star_empty;
3905} ff_stack_T;
3906
3907/*
3908 * type for already visited directories or files.
3909 */
3910typedef struct ff_visited
3911{
3912 struct ff_visited *ffv_next;
3913
3914#ifdef FEAT_PATH_EXTRA
3915 /* Visited directories are different if the wildcard string are
3916 * different. So we have to save it.
3917 */
3918 char_u *ffv_wc_path;
3919#endif
3920 /* for unix use inode etc for comparison (needed because of links), else
3921 * use filename.
3922 */
3923#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003924 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3925 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 ino_t ffv_ino; /* inode number */
3927#endif
3928 /* The memory for this struct is allocated according to the length of
3929 * ffv_fname.
3930 */
3931 char_u ffv_fname[1]; /* actually longer */
3932} ff_visited_T;
3933
3934/*
3935 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003936 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3938 * So we have to do 3 searches:
3939 * 1) search from the current files directory downward for the file "tags"
3940 * 2) search from the current files directory downward for the file "TAGS"
3941 * 3) search from Vims current directory downwards for the file "tags"
3942 * As you can see, the first and the third search are for the same file, so for
3943 * the third search we can use the visited list of the first search. For the
3944 * second search we must start from a empty visited list.
3945 * The struct ff_visited_list_hdr is used to manage a linked list of already
3946 * visited lists.
3947 */
3948typedef struct ff_visited_list_hdr
3949{
3950 struct ff_visited_list_hdr *ffvl_next;
3951
3952 /* the filename the attached visited list is for */
3953 char_u *ffvl_filename;
3954
3955 ff_visited_T *ffvl_visited_list;
3956
3957} ff_visited_list_hdr_T;
3958
3959
3960/*
3961 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003962 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 */
3964#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966/*
3967 * The search context:
3968 * ffsc_stack_ptr: the stack for the dirs to search
3969 * ffsc_visited_list: the currently active visited list
3970 * ffsc_dir_visited_list: the currently active visited list for search dirs
3971 * ffsc_visited_lists_list: the list of all visited lists
3972 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3973 * ffsc_file_to_search: the file to search for
3974 * ffsc_start_dir: the starting directory, if search path was relative
3975 * ffsc_fix_path: the fix part of the given path (without wildcards)
3976 * Needed for upward search.
3977 * ffsc_wc_path: the part of the given path containing wildcards
3978 * ffsc_level: how many levels of dirs to search downwards
3979 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003980 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003981 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 */
3983typedef struct ff_search_ctx_T
3984{
3985 ff_stack_T *ffsc_stack_ptr;
3986 ff_visited_list_hdr_T *ffsc_visited_list;
3987 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3988 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3989 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3990 char_u *ffsc_file_to_search;
3991 char_u *ffsc_start_dir;
3992 char_u *ffsc_fix_path;
3993#ifdef FEAT_PATH_EXTRA
3994 char_u *ffsc_wc_path;
3995 int ffsc_level;
3996 char_u **ffsc_stopdirs_v;
3997#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003998 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003999 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004000} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002/* locally needed functions */
4003#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004004static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004006static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004008static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
4009static void ff_free_visited_list(ff_visited_T *vl);
4010static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, ff_visited_list_hdr_T **list_headp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004012static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
4013static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
4014static void ff_clear(ff_search_ctx_T *search_ctx);
4015static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004017static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004019static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#endif
4021#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004022static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#endif
4024
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004025static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027#if 0
4028/*
4029 * if someone likes findfirst/findnext, here are the functions
4030 * NOT TESTED!!
4031 */
4032
4033static void *ff_fn_search_context = NULL;
4034
4035 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004036vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
4038 ff_fn_search_context =
4039 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4040 ff_fn_search_context, rel_fname);
4041 if (NULL == ff_fn_search_context)
4042 return NULL;
4043 else
4044 return vim_findnext()
4045}
4046
4047 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004048vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
4050 char_u *ret = vim_findfile(ff_fn_search_context);
4051
4052 if (NULL == ret)
4053 {
4054 vim_findfile_cleanup(ff_fn_search_context);
4055 ff_fn_search_context = NULL;
4056 }
4057 return ret;
4058}
4059#endif
4060
4061/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004062 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004064 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 *
4066 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4067 * with the search context.
4068 *
4069 * Find the file 'filename' in the directory 'path'.
4070 * The parameter 'path' may contain wildcards. If so only search 'level'
4071 * directories deep. The parameter 'level' is the absolute maximum and is
4072 * not related to restricts given to the '**' wildcard. If 'level' is 100
4073 * and you use '**200' vim_findfile() will stop after 100 levels.
4074 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004075 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4076 * escape special characters.
4077 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4079 * restarted on the next higher directory level. This is repeated until the
4080 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4081 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4082 *
4083 * If the 'path' is relative, the starting dir for the search is either VIM's
4084 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004085 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 * the first wildcard.
4087 *
4088 * Upward search is only done on the starting dir.
4089 *
4090 * If 'free_visited' is TRUE the list of already visited files/directories is
4091 * cleared. Set this to FALSE if you just want to search from another
4092 * directory, but want to be sure that no directory from a previous search is
4093 * searched again. This is useful if you search for a file at different places.
4094 * The list of visited files/dirs can also be cleared with the function
4095 * vim_findfile_free_visited().
4096 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004097 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4098 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 *
4100 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004101 * passed in the parameter "search_ctx_arg". This context is reused and
4102 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004104 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4105 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004107 * If you don't have a search context from a previous call "search_ctx_arg"
4108 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 *
4110 * This function silently ignores a few errors, vim_findfile() will have
4111 * limited functionality then.
4112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004114vim_findfile_init(
4115 char_u *path,
4116 char_u *filename,
4117 char_u *stopdirs UNUSED,
4118 int level,
4119 int free_visited,
4120 int find_what,
4121 void *search_ctx_arg,
4122 int tagfile, /* expanding names of tags files */
4123 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004126 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004128 ff_stack_T *sptr;
4129 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 /* If a search context is given by the caller, reuse it, else allocate a
4132 * new one.
4133 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004134 if (search_ctx_arg != NULL)
4135 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 else
4137 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004138 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4139 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004141 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004143 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004144 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004147 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /* clear visited list if wanted */
4150 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004151 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 else
4153 {
4154 /* Reuse old visited lists. Get the visited list for the given
4155 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004156 * one. */
4157 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4158 &search_ctx->ffsc_visited_lists_list);
4159 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004161 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4162 &search_ctx->ffsc_dir_visited_lists_list);
4163 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 goto error_return;
4165 }
4166
4167 if (ff_expand_buffer == NULL)
4168 {
4169 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4170 if (ff_expand_buffer == NULL)
4171 goto error_return;
4172 }
4173
4174 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004175 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 if (path[0] == '.'
4177 && (vim_ispathsep(path[1]) || path[1] == NUL)
4178 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4179 && rel_fname != NULL)
4180 {
4181 int len = (int)(gettail(rel_fname) - rel_fname);
4182
4183 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4184 {
4185 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004186 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004187 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004190 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4191 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 goto error_return;
4193 if (*++path != NUL)
4194 ++path;
4195 }
4196 else if (*path == NUL || !vim_isAbsName(path))
4197 {
4198#ifdef BACKSLASH_IN_FILENAME
4199 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4200 if (*path != NUL && path[1] == ':')
4201 {
4202 char_u drive[3];
4203
4204 drive[0] = path[0];
4205 drive[1] = ':';
4206 drive[2] = NUL;
4207 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4208 goto error_return;
4209 path += 2;
4210 }
4211 else
4212#endif
4213 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4214 goto error_return;
4215
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004216 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4217 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 goto error_return;
4219
4220#ifdef BACKSLASH_IN_FILENAME
4221 /* A path that starts with "/dir" is relative to the drive, not to the
4222 * directory (but not for "//machine/dir"). Only use the drive name. */
4223 if ((*path == '/' || *path == '\\')
4224 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004225 && search_ctx->ffsc_start_dir[1] == ':')
4226 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227#endif
4228 }
4229
4230#ifdef FEAT_PATH_EXTRA
4231 /*
4232 * If stopdirs are given, split them into an array of pointers.
4233 * If this fails (mem allocation), there is no upward search at all or a
4234 * stop directory is not recognized -> continue silently.
4235 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004236 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 * is handled as unlimited upward search. See function
4238 * ff_path_in_stoplist() for details.
4239 */
4240 if (stopdirs != NULL)
4241 {
4242 char_u *walker = stopdirs;
4243 int dircount;
4244
4245 while (*walker == ';')
4246 walker++;
4247
4248 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004249 search_ctx->ffsc_stopdirs_v =
4250 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004252 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
4254 do
4255 {
4256 char_u *helper;
4257 void *ptr;
4258
4259 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004260 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 (dircount + 1) * sizeof(char_u *));
4262 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004263 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 else
4265 /* ignore, keep what we have and continue */
4266 break;
4267 walker = vim_strchr(walker, ';');
4268 if (walker)
4269 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004270 search_ctx->ffsc_stopdirs_v[dircount-1] =
4271 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 walker++;
4273 }
4274 else
4275 /* this might be "", which means ascent till top
4276 * of directory tree.
4277 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004278 search_ctx->ffsc_stopdirs_v[dircount-1] =
4279 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 dircount++;
4282
4283 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004284 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287#endif
4288
4289#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004290 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 /* split into:
4293 * -fix path
4294 * -wildcard_stuff (might be NULL)
4295 */
4296 wc_part = vim_strchr(path, '*');
4297 if (wc_part != NULL)
4298 {
4299 int llevel;
4300 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004301 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004304 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004308 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4310 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4311 * For EBCDIC you get different character values.
4312 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004313 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 * string.
4315 */
4316 len = 0;
4317 while (*wc_part != NUL)
4318 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004319 if (len + 5 >= MAXPATHL)
4320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004321 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004322 break;
4323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 if (STRNCMP(wc_part, "**", 2) == 0)
4325 {
4326 ff_expand_buffer[len++] = *wc_part++;
4327 ff_expand_buffer[len++] = *wc_part++;
4328
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004329 llevel = strtol((char *)wc_part, &errpt, 10);
4330 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004332 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 /* restrict is 0 -> remove already added '**' */
4334 len -= 2;
4335 else
4336 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004337 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004338 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004340 semsg(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 goto error_return;
4342 }
4343 }
4344 else
4345 ff_expand_buffer[len++] = *wc_part++;
4346 }
4347 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004348 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004350 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 goto error_return;
4352 }
4353 else
4354#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004355 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004357 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
4359 /* store the fix part as startdir.
4360 * This is needed if the parameter path is fully qualified.
4361 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004362 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004363 if (search_ctx->ffsc_start_dir == NULL)
4364 goto error_return;
4365 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367
4368 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004369 if (STRLEN(search_ctx->ffsc_start_dir)
4370 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004372 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004373 goto error_return;
4374 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004375 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004377 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004378 int eb_len = (int)STRLEN(ff_expand_buffer);
4379 char_u *buf = alloc(eb_len
4380 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004381
4382 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004383 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004384 if (mch_isdir(buf))
4385 {
4386 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4387 add_pathsep(ff_expand_buffer);
4388 }
4389#ifdef FEAT_PATH_EXTRA
4390 else
4391 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004392 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004393 char_u *wc_path = NULL;
4394 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004395 int len = 0;
4396
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004397 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004398 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004399 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004400 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4401 add_pathsep(ff_expand_buffer);
4402 }
4403 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004404 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004405
4406 if (search_ctx->ffsc_wc_path != NULL)
4407 {
4408 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004409 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004410 + STRLEN(search_ctx->ffsc_fix_path + len)
4411 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004412 if (temp == NULL || wc_path == NULL)
4413 {
4414 vim_free(buf);
4415 vim_free(temp);
4416 vim_free(wc_path);
4417 goto error_return;
4418 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004419
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004420 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4421 STRCAT(temp, search_ctx->ffsc_wc_path);
4422 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004423 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004424 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004425 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004426 }
4427#endif
4428 vim_free(buf);
4429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 sptr = ff_create_stack_element(ff_expand_buffer,
4432#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004433 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434#endif
4435 level, 0);
4436
4437 if (sptr == NULL)
4438 goto error_return;
4439
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004440 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004442 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4443 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 goto error_return;
4445
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004446 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448error_return:
4449 /*
4450 * We clear the search context now!
4451 * Even when the caller gave us a (perhaps valid) context we free it here,
4452 * as we might have already destroyed it.
4453 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004454 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return NULL;
4456}
4457
4458#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4459/*
4460 * Get the stopdir string. Check that ';' is not escaped.
4461 */
4462 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004463vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465 char_u *r_ptr = buf;
4466
4467 while (*r_ptr != NUL && *r_ptr != ';')
4468 {
4469 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4470 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004471 /* Overwrite the escape char,
4472 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004473 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 r_ptr++;
4475 }
4476 r_ptr++;
4477 }
4478 if (*r_ptr == ';')
4479 {
4480 *r_ptr = 0;
4481 r_ptr++;
4482 }
4483 else if (*r_ptr == NUL)
4484 r_ptr = NULL;
4485 return r_ptr;
4486}
4487#endif
4488
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004489/*
4490 * Clean up the given search context. Can handle a NULL pointer.
4491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004493vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004495 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 return;
4497
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004499 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501}
4502
4503/*
4504 * Find a file in a search context.
4505 * The search context was created with vim_findfile_init() above.
4506 * Return a pointer to an allocated file name or NULL if nothing found.
4507 * To get all matching files call this function until you get NULL.
4508 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004509 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 *
4511 * The search algorithm is depth first. To change this replace the
4512 * stack with a list (don't forget to leave partly searched directories on the
4513 * top of the list).
4514 */
4515 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004516vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
4518 char_u *file_path;
4519#ifdef FEAT_PATH_EXTRA
4520 char_u *rest_of_wildcards;
4521 char_u *path_end = NULL;
4522#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004523 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4525 int len;
4526#endif
4527 int i;
4528 char_u *p;
4529#ifdef FEAT_SEARCHPATH
4530 char_u *suf;
4531#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004532 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004534 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 return NULL;
4536
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004537 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
4539 /*
4540 * filepath is used as buffer for various actions and as the storage to
4541 * return a found filename.
4542 */
4543 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4544 return NULL;
4545
4546#ifdef FEAT_PATH_EXTRA
4547 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004548 if (search_ctx->ffsc_start_dir != NULL)
4549 path_end = &search_ctx->ffsc_start_dir[
4550 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#endif
4552
4553#ifdef FEAT_PATH_EXTRA
4554 /* upward search loop */
4555 for (;;)
4556 {
4557#endif
4558 /* downward search loop */
4559 for (;;)
4560 {
4561 /* check if user user wants to stop the search*/
4562 ui_breakcheck();
4563 if (got_int)
4564 break;
4565
4566 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004567 stackp = ff_pop(search_ctx);
4568 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 break;
4570
4571 /*
4572 * TODO: decide if we leave this test in
4573 *
4574 * GOOD: don't search a directory(-tree) twice.
4575 * BAD: - check linked list for every new directory entered.
4576 * - check for double files also done below
4577 *
4578 * Here we check if we already searched this directory.
4579 * We already searched a directory if:
4580 * 1) The directory is the same.
4581 * 2) We would use the same wildcard string.
4582 *
4583 * Good if you have links on same directory via several ways
4584 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4585 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4586 *
4587 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004588 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004590 if (stackp->ffs_filearray == NULL
4591 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004593 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004595 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#endif
4597 ) == FAIL)
4598 {
4599#ifdef FF_VERBOSE
4600 if (p_verbose >= 5)
4601 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004602 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004603 smsg("Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004604 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004606 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004607 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004610 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 continue;
4612 }
4613#ifdef FF_VERBOSE
4614 else if (p_verbose >= 5)
4615 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004616 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004617 smsg("Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004618 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004620 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004621 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623#endif
4624
4625 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004626 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004628 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 continue;
4630 }
4631
4632 file_path[0] = NUL;
4633
4634 /*
4635 * If no filearray till now expand wildcards
4636 * The function expand_wildcards() can handle an array of paths
4637 * and all possible expands are returned in one array. We use this
4638 * to handle the expansion of '**' into an empty string.
4639 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004640 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
4642 char_u *dirptrs[2];
4643
4644 /* we use filepath to build the path expand_wildcards() should
4645 * expand.
4646 */
4647 dirptrs[0] = file_path;
4648 dirptrs[1] = NULL;
4649
4650 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004651 if (!vim_isAbsName(stackp->ffs_fix_path)
4652 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004654 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4655 {
4656 STRCPY(file_path, search_ctx->ffsc_start_dir);
4657 add_pathsep(file_path);
4658 }
4659 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004660 {
4661 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004662 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665
4666 /* append the fix part of the search path */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004667 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4668 {
4669 STRCAT(file_path, stackp->ffs_fix_path);
4670 add_pathsep(file_path);
4671 }
4672 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004673 {
4674 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004675 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004679 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if (*rest_of_wildcards != NUL)
4681 {
4682 len = (int)STRLEN(file_path);
4683 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4684 {
4685 /* pointer to the restrict byte
4686 * The restrict byte is not a character!
4687 */
4688 p = rest_of_wildcards + 2;
4689
4690 if (*p > 0)
4691 {
4692 (*p)--;
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004693 if (len + 1 < MAXPATHL)
4694 file_path[len++] = '*';
4695 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004696 {
4697 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004698 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701
4702 if (*p == 0)
4703 {
4704 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004705 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 else
4708 rest_of_wildcards += 3;
4709
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004710 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
4712 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004713 stackp->ffs_star_star_empty = 1;
4714 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716 }
4717
4718 /*
4719 * Here we copy until the next path separator or the end of
4720 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004721 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 * pushing every directory returned from expand_wildcards()
4723 * on the stack again for further search.
4724 */
4725 while (*rest_of_wildcards
4726 && !vim_ispathsep(*rest_of_wildcards))
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004727 if (len + 1 < MAXPATHL)
4728 file_path[len++] = *rest_of_wildcards++;
4729 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004730 {
4731 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004732 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
4735 file_path[len] = NUL;
4736 if (vim_ispathsep(*rest_of_wildcards))
4737 rest_of_wildcards++;
4738 }
4739#endif
4740
4741 /*
4742 * Expand wildcards like "*" and "$VAR".
4743 * If the path is a URL don't try this.
4744 */
4745 if (path_with_url(dirptrs[0]))
4746 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004747 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004749 if (stackp->ffs_filearray != NULL
4750 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004752 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004754 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004757 /* Add EW_NOTWILD because the expanded path may contain
4758 * wildcard characters that are to be taken literally.
4759 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004761 &stackp->ffs_filearray_size,
4762 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004763 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004765 stackp->ffs_filearray_cur = 0;
4766 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768#ifdef FEAT_PATH_EXTRA
4769 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004770 rest_of_wildcards = &stackp->ffs_wc_path[
4771 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772#endif
4773
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004774 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 /* this is the first time we work on this directory */
4777#ifdef FEAT_PATH_EXTRA
4778 if (*rest_of_wildcards == NUL)
4779#endif
4780 {
4781 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004782 * We don't have further wildcards to expand, so we have to
4783 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004785 for (i = stackp->ffs_filearray_cur;
4786 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004788 if (!path_with_url(stackp->ffs_filearray[i])
4789 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 continue; /* not a directory */
4791
Bram Moolenaar21160b92009-11-11 15:56:10 +00004792 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 * below */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004794 if (STRLEN(stackp->ffs_filearray[i]) + 1
4795 + STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4796 {
4797 STRCPY(file_path, stackp->ffs_filearray[i]);
4798 add_pathsep(file_path);
4799 STRCAT(file_path, search_ctx->ffsc_file_to_search);
4800 }
4801 else
Bram Moolenaare0de2162019-01-29 20:17:28 +01004802 {
4803 ff_free_stack_element(stackp);
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004804 goto fail;
Bram Moolenaare0de2162019-01-29 20:17:28 +01004805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /*
4808 * Try without extra suffix and then with suffixes
4809 * from 'suffixesadd'.
4810 */
4811#ifdef FEAT_SEARCHPATH
4812 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004813 if (search_ctx->ffsc_tagfile)
4814 suf = (char_u *)"";
4815 else
4816 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 for (;;)
4818#endif
4819 {
4820 /* if file exists and we didn't already find it */
4821 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004822 || (mch_getperm(file_path) >= 0
4823 && (search_ctx->ffsc_find_what
4824 == FINDFILE_BOTH
4825 || ((search_ctx->ffsc_find_what
4826 == FINDFILE_DIR)
4827 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#ifndef FF_VERBOSE
4829 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004830 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 file_path
4832#ifdef FEAT_PATH_EXTRA
4833 , (char_u *)""
4834#endif
4835 ) == OK)
4836#endif
4837 )
4838 {
4839#ifdef FF_VERBOSE
4840 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004841 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 file_path
4843#ifdef FEAT_PATH_EXTRA
4844 , (char_u *)""
4845#endif
4846 ) == FAIL)
4847 {
4848 if (p_verbose >= 5)
4849 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004850 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004851 smsg("Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 file_path);
4853 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004854 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004855 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 continue;
4858 }
4859#endif
4860
4861 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004862 stackp->ffs_filearray_cur = i + 1;
4863 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004865 if (!path_with_url(file_path))
4866 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4868 == OK)
4869 {
4870 p = shorten_fname(file_path,
4871 ff_expand_buffer);
4872 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004873 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875#ifdef FF_VERBOSE
4876 if (p_verbose >= 5)
4877 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004878 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004879 smsg("HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004881 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004882 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884#endif
4885 return file_path;
4886 }
4887
4888#ifdef FEAT_SEARCHPATH
4889 /* Not found or found already, try next suffix. */
4890 if (*suf == NUL)
4891 break;
4892 copy_option_part(&suf, file_path + len,
4893 MAXPATHL - len, ",");
4894#endif
4895 }
4896 }
4897 }
4898#ifdef FEAT_PATH_EXTRA
4899 else
4900 {
4901 /*
4902 * still wildcards left, push the directories for further
4903 * search
4904 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004905 for (i = stackp->ffs_filearray_cur;
4906 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004908 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 continue; /* not a directory */
4910
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004911 ff_push(search_ctx,
4912 ff_create_stack_element(
4913 stackp->ffs_filearray[i],
4914 rest_of_wildcards,
4915 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 }
4918#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004919 stackp->ffs_filearray_cur = 0;
4920 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922
4923#ifdef FEAT_PATH_EXTRA
4924 /*
4925 * if wildcards contains '**' we have to descent till we reach the
4926 * leaves of the directory tree.
4927 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004928 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004930 for (i = stackp->ffs_filearray_cur;
4931 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004933 if (fnamecmp(stackp->ffs_filearray[i],
4934 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004936 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004938 ff_push(search_ctx,
4939 ff_create_stack_element(stackp->ffs_filearray[i],
4940 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 }
4943#endif
4944
4945 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004946 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 }
4949
4950#ifdef FEAT_PATH_EXTRA
4951 /* If we reached this, we didn't find anything downwards.
4952 * Let's check if we should do an upward search.
4953 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004954 if (search_ctx->ffsc_start_dir
4955 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 ff_stack_T *sptr;
4958
4959 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004960 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4961 (int)(path_end - search_ctx->ffsc_start_dir),
4962 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 break;
4964
4965 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004966 while (path_end > search_ctx->ffsc_start_dir
4967 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004969 while (path_end > search_ctx->ffsc_start_dir
4970 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 path_end--;
4972 *path_end = 0;
4973 path_end--;
4974
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004975 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 break;
4977
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004978 if (STRLEN(search_ctx->ffsc_start_dir) + 1
4979 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
4980 {
4981 STRCPY(file_path, search_ctx->ffsc_start_dir);
4982 add_pathsep(file_path);
4983 STRCAT(file_path, search_ctx->ffsc_fix_path);
4984 }
4985 else
4986 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
4988 /* create a new stack entry */
4989 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004990 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (sptr == NULL)
4992 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004993 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 else
4996 break;
4997 }
4998#endif
4999
Bram Moolenaar15618fa2017-03-19 21:37:13 +01005000fail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 vim_free(file_path);
5002 return NULL;
5003}
5004
5005/*
5006 * Free the list of lists of visited files and directories
5007 * Can handle it if the passed search_context is NULL;
5008 */
5009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005010vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005012 ff_search_ctx_T *search_ctx;
5013
5014 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 return;
5016
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005017 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
5018 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
5019 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020}
5021
5022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005023vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 ff_visited_list_hdr_T *vp;
5026
5027 while (*list_headp != NULL)
5028 {
5029 vp = (*list_headp)->ffvl_next;
5030 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5031
5032 vim_free((*list_headp)->ffvl_filename);
5033 vim_free(*list_headp);
5034 *list_headp = vp;
5035 }
5036 *list_headp = NULL;
5037}
5038
5039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005040ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 ff_visited_T *vp;
5043
5044 while (vl != NULL)
5045 {
5046 vp = vl->ffv_next;
5047#ifdef FEAT_PATH_EXTRA
5048 vim_free(vl->ffv_wc_path);
5049#endif
5050 vim_free(vl);
5051 vl = vp;
5052 }
5053 vl = NULL;
5054}
5055
5056/*
5057 * Returns the already visited list for the given filename. If none is found it
5058 * allocates a new one.
5059 */
5060 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01005061ff_get_visited_list(
5062 char_u *filename,
5063 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 ff_visited_list_hdr_T *retptr = NULL;
5066
5067 /* check if a visited list for the given filename exists */
5068 if (*list_headp != NULL)
5069 {
5070 retptr = *list_headp;
5071 while (retptr != NULL)
5072 {
5073 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5074 {
5075#ifdef FF_VERBOSE
5076 if (p_verbose >= 5)
5077 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005078 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005079 smsg("ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 filename);
5081 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005082 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005083 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085#endif
5086 return retptr;
5087 }
5088 retptr = retptr->ffvl_next;
5089 }
5090 }
5091
5092#ifdef FF_VERBOSE
5093 if (p_verbose >= 5)
5094 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005095 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005096 smsg("ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005098 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005099 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101#endif
5102
5103 /*
5104 * if we reach this we didn't find a list and we have to allocate new list
5105 */
5106 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5107 if (retptr == NULL)
5108 return NULL;
5109
5110 retptr->ffvl_visited_list = NULL;
5111 retptr->ffvl_filename = vim_strsave(filename);
5112 if (retptr->ffvl_filename == NULL)
5113 {
5114 vim_free(retptr);
5115 return NULL;
5116 }
5117 retptr->ffvl_next = *list_headp;
5118 *list_headp = retptr;
5119
5120 return retptr;
5121}
5122
5123#ifdef FEAT_PATH_EXTRA
5124/*
5125 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5126 * They are equal if:
5127 * - both paths are NULL
5128 * - they have the same length
5129 * - char by char comparison is OK
5130 * - the only differences are in the counters behind a '**', so
5131 * '**\20' is equal to '**\24'
5132 */
5133 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005134ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005136 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005137 int c1 = NUL;
5138 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005139 int prev1 = NUL;
5140 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 if (s1 == s2)
5143 return TRUE;
5144
5145 if (s1 == NULL || s2 == NULL)
5146 return FALSE;
5147
Bram Moolenaard43f0952015-08-27 22:30:47 +02005148 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005150 c1 = PTR2CHAR(s1 + i);
5151 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005152
5153 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5154 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005155 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005156 prev2 = prev1;
5157 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005158
Bram Moolenaar15675582018-02-09 12:29:56 +01005159 i += MB_PTR2LEN(s1 + i);
5160 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005162 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163}
5164#endif
5165
5166/*
5167 * maintains the list of already visited files and dirs
5168 * returns FAIL if the given file/dir is already in the list
5169 * returns OK if it is newly added
5170 *
5171 * TODO: What to do on memory allocation problems?
5172 * -> return TRUE - Better the file is found several times instead of
5173 * never.
5174 */
5175 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005176ff_check_visited(
5177 ff_visited_T **visited_list,
5178 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005180 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005182 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 ff_visited_T *vp;
5185#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02005186 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 int url = FALSE;
5188#endif
5189
5190 /* For an URL we only compare the name, otherwise we compare the
5191 * device/inode (unix) or the full path name (not Unix). */
5192 if (path_with_url(fname))
5193 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005194 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#ifdef UNIX
5196 url = TRUE;
5197#endif
5198 }
5199 else
5200 {
5201 ff_expand_buffer[0] = NUL;
5202#ifdef UNIX
5203 if (mch_stat((char *)fname, &st) < 0)
5204#else
5205 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5206#endif
5207 return FAIL;
5208 }
5209
5210 /* check against list of already visited files */
5211 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5212 {
5213 if (
5214#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005215 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5216 && vp->ffv_ino == st.st_ino)
5217 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#endif
5219 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5220 )
5221 {
5222#ifdef FEAT_PATH_EXTRA
5223 /* are the wildcard parts equal */
5224 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5225#endif
5226 /* already visited */
5227 return FAIL;
5228 }
5229 }
5230
5231 /*
5232 * New file/dir. Add it to the list of visited files/dirs.
5233 */
5234 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5235 + STRLEN(ff_expand_buffer)));
5236
5237 if (vp != NULL)
5238 {
5239#ifdef UNIX
5240 if (!url)
5241 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005242 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 vp->ffv_ino = st.st_ino;
5244 vp->ffv_dev = st.st_dev;
5245 vp->ffv_fname[0] = NUL;
5246 }
5247 else
5248 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005249 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#endif
5251 STRCPY(vp->ffv_fname, ff_expand_buffer);
5252#ifdef UNIX
5253 }
5254#endif
5255#ifdef FEAT_PATH_EXTRA
5256 if (wc_path != NULL)
5257 vp->ffv_wc_path = vim_strsave(wc_path);
5258 else
5259 vp->ffv_wc_path = NULL;
5260#endif
5261
5262 vp->ffv_next = *visited_list;
5263 *visited_list = vp;
5264 }
5265
5266 return OK;
5267}
5268
5269/*
5270 * create stack element from given path pieces
5271 */
5272 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273ff_create_stack_element(
5274 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005276 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278 int level,
5279 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 ff_stack_T *new;
5282
5283 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5284 if (new == NULL)
5285 return NULL;
5286
5287 new->ffs_prev = NULL;
5288 new->ffs_filearray = NULL;
5289 new->ffs_filearray_size = 0;
5290 new->ffs_filearray_cur = 0;
5291 new->ffs_stage = 0;
5292 new->ffs_level = level;
Bram Moolenaar945ec092016-06-08 21:17:43 +02005293 new->ffs_star_star_empty = star_star_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /* the following saves NULL pointer checks in vim_findfile */
5296 if (fix_part == NULL)
5297 fix_part = (char_u *)"";
5298 new->ffs_fix_path = vim_strsave(fix_part);
5299
5300#ifdef FEAT_PATH_EXTRA
5301 if (wc_part == NULL)
5302 wc_part = (char_u *)"";
5303 new->ffs_wc_path = vim_strsave(wc_part);
5304#endif
5305
5306 if (new->ffs_fix_path == NULL
5307#ifdef FEAT_PATH_EXTRA
5308 || new->ffs_wc_path == NULL
5309#endif
5310 )
5311 {
5312 ff_free_stack_element(new);
5313 new = NULL;
5314 }
5315
5316 return new;
5317}
5318
5319/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005320 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 */
5322 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005323ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324{
5325 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005326 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005327 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005329 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5330 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332}
5333
5334/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005335 * Pop a dir from the directory stack.
5336 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
5338 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005339ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
5341 ff_stack_T *sptr;
5342
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005343 sptr = search_ctx->ffsc_stack_ptr;
5344 if (search_ctx->ffsc_stack_ptr != NULL)
5345 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 return sptr;
5348}
5349
5350/*
5351 * free the given stack element
5352 */
5353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005354ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005357 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005359 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#endif
5361
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005362 if (stack_ptr->ffs_filearray != NULL)
5363 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005365 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366}
5367
5368/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005369 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 */
5371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 ff_stack_T *sptr;
5375
5376 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005377 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 ff_free_stack_element(sptr);
5379
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005380 vim_free(search_ctx->ffsc_file_to_search);
5381 vim_free(search_ctx->ffsc_start_dir);
5382 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005384 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#endif
5386
5387#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005388 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
5390 int i = 0;
5391
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005392 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005394 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 i++;
5396 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005397 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005399 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400#endif
5401
5402 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005403 search_ctx->ffsc_file_to_search = NULL;
5404 search_ctx->ffsc_start_dir = NULL;
5405 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005407 search_ctx->ffsc_wc_path = NULL;
5408 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#endif
5410}
5411
5412#ifdef FEAT_PATH_EXTRA
5413/*
5414 * check if the given path is in the stopdirs
5415 * returns TRUE if yes else FALSE
5416 */
5417 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005418ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419{
5420 int i = 0;
5421
5422 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005423 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 path_len--;
5425
5426 /* if no path consider it as match */
5427 if (path_len == 0)
5428 return TRUE;
5429
5430 for (i = 0; stopdirs_v[i] != NULL; i++)
5431 {
5432 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5433 {
5434 /* match for parent directory. So '/home' also matches
5435 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5436 * '/home/r' would also match '/home/rks'
5437 */
5438 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005439 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 return TRUE;
5441 }
5442 else
5443 {
5444 if (fnamecmp(stopdirs_v[i], path) == 0)
5445 return TRUE;
5446 }
5447 }
5448 return FALSE;
5449}
5450#endif
5451
5452#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5453/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005454 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 *
5456 * On the first call set the parameter 'first' to TRUE to initialize
5457 * the search. For repeating calls to FALSE.
5458 *
5459 * Repeating calls will return other files called 'ptr[len]' from the path.
5460 *
5461 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5462 * don't need valid values.
5463 *
5464 * If nothing found on the first call the option FNAME_MESS will issue the
5465 * message:
5466 * 'Can't find file "<file>" in path'
5467 * On repeating calls:
5468 * 'No more file "<file>" found in path'
5469 *
5470 * options:
5471 * FNAME_MESS give error message when not found
5472 *
5473 * Uses NameBuff[]!
5474 *
5475 * Returns an allocated string for the file name. NULL for error.
5476 *
5477 */
5478 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005479find_file_in_path(
5480 char_u *ptr, /* file name */
5481 int len, /* length of file name */
5482 int options,
5483 int first, /* use count'th matching file name */
5484 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 return find_file_in_path_option(ptr, len, options, first,
5487 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005488 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489}
5490
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005491static char_u *ff_file_to_find = NULL;
5492static void *fdip_search_ctx = NULL;
5493
5494#if defined(EXITFREE)
5495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005496free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005497{
5498 vim_free(ff_file_to_find);
5499 vim_findfile_cleanup(fdip_search_ctx);
5500}
5501#endif
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503/*
5504 * Find the directory name "ptr[len]" in the path.
5505 *
5506 * options:
5507 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005508 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 *
5510 * Uses NameBuff[]!
5511 *
5512 * Returns an allocated string for the file name. NULL for error.
5513 */
5514 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005515find_directory_in_path(
5516 char_u *ptr, /* file name */
5517 int len, /* length of file name */
5518 int options,
5519 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005522 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523}
5524
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005525 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005526find_file_in_path_option(
5527 char_u *ptr, /* file name */
5528 int len, /* length of file name */
5529 int options,
5530 int first, /* use count'th matching file name */
5531 char_u *path_option, /* p_path or p_cdpath */
5532 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5533 char_u *rel_fname, /* file name we are looking relative to. */
5534 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 static int did_findfile_init = FALSE;
5538 char_u save_char;
5539 char_u *file_name = NULL;
5540 char_u *buf = NULL;
5541 int rel_to_curdir;
5542#ifdef AMIGA
5543 struct Process *proc = (struct Process *)FindTask(0L);
5544 APTR save_winptr = proc->pr_WindowPtr;
5545
5546 /* Avoid a requester here for a volume that doesn't exist. */
5547 proc->pr_WindowPtr = (APTR)-1L;
5548#endif
5549
5550 if (first == TRUE)
5551 {
5552 /* copy file name into NameBuff, expanding environment variables */
5553 save_char = ptr[len];
5554 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005555 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 ptr[len] = save_char;
5557
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005558 vim_free(ff_file_to_find);
5559 ff_file_to_find = vim_strsave(NameBuff);
5560 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 file_name = NULL;
5563 goto theend;
5564 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005565 if (options & FNAME_UNESC)
5566 {
5567 /* Change all "\ " to " ". */
5568 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5569 if (ptr[0] == '\\' && ptr[1] == ' ')
5570 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005574 rel_to_curdir = (ff_file_to_find[0] == '.'
5575 && (ff_file_to_find[1] == NUL
5576 || vim_ispathsep(ff_file_to_find[1])
5577 || (ff_file_to_find[1] == '.'
5578 && (ff_file_to_find[2] == NUL
5579 || vim_ispathsep(ff_file_to_find[2])))));
5580 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 /* "..", "../path", "." and "./path": don't use the path_option */
5582 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005583#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005585 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005586 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005587 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588#endif
5589#ifdef AMIGA
5590 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005591 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592#endif
5593 )
5594 {
5595 /*
5596 * Absolute path, no need to use "path_option".
5597 * If this is not a first call, return NULL. We already returned a
5598 * filename on the first call.
5599 */
5600 if (first == TRUE)
5601 {
5602 int l;
5603 int run;
5604
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005605 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005607 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 goto theend;
5609 }
5610
5611 /* When FNAME_REL flag given first use the directory of the file.
5612 * Otherwise or when this fails use the current directory. */
5613 for (run = 1; run <= 2; ++run)
5614 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005615 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 if (run == 1
5617 && rel_to_curdir
5618 && (options & FNAME_REL)
5619 && rel_fname != NULL
5620 && STRLEN(rel_fname) + l < MAXPATHL)
5621 {
5622 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005623 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 l = (int)STRLEN(NameBuff);
5625 }
5626 else
5627 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005628 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 run = 2;
5630 }
5631
5632 /* When the file doesn't exist, try adding parts of
5633 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005634 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 for (;;)
5636 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005637 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005638 && (find_what == FINDFILE_BOTH
5639 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005640 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 file_name = vim_strsave(NameBuff);
5643 goto theend;
5644 }
5645 if (*buf == NUL)
5646 break;
5647 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5648 }
5649 }
5650 }
5651 }
5652 else
5653 {
5654 /*
5655 * Loop over all paths in the 'path' or 'cdpath' option.
5656 * When "first" is set, first setup to the start of the option.
5657 * Otherwise continue to find the next match.
5658 */
5659 if (first == TRUE)
5660 {
5661 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005662 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 dir = path_option;
5664 did_findfile_init = FALSE;
5665 }
5666
5667 for (;;)
5668 {
5669 if (did_findfile_init)
5670 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005671 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 if (file_name != NULL)
5673 break;
5674
5675 did_findfile_init = FALSE;
5676 }
5677 else
5678 {
5679 char_u *r_ptr;
5680
5681 if (dir == NULL || *dir == NUL)
5682 {
5683 /* We searched all paths of the option, now we can
5684 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005685 vim_findfile_cleanup(fdip_search_ctx);
5686 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 break;
5688 }
5689
5690 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5691 break;
5692
5693 /* copy next path */
5694 buf[0] = 0;
5695 copy_option_part(&dir, buf, MAXPATHL, " ,");
5696
5697#ifdef FEAT_PATH_EXTRA
5698 /* get the stopdir string */
5699 r_ptr = vim_findfile_stopdir(buf);
5700#else
5701 r_ptr = NULL;
5702#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005703 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005704 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005705 fdip_search_ctx, FALSE, rel_fname);
5706 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 did_findfile_init = TRUE;
5708 vim_free(buf);
5709 }
5710 }
5711 }
5712 if (file_name == NULL && (options & FNAME_MESS))
5713 {
5714 if (first == TRUE)
5715 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005716 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005717 semsg(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005718 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005720 semsg(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005721 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 else
5724 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005725 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005726 semsg(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005727 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005729 semsg(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005730 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732 }
5733
5734theend:
5735#ifdef AMIGA
5736 proc->pr_WindowPtr = save_winptr;
5737#endif
5738 return file_name;
5739}
5740
5741#endif /* FEAT_SEARCHPATH */
5742
5743/*
5744 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5745 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5746 */
5747 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005748vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750#ifndef FEAT_SEARCHPATH
5751 return mch_chdir((char *)new_dir);
5752#else
5753 char_u *dir_name;
5754 int r;
5755
5756 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5757 FNAME_MESS, curbuf->b_ffname);
5758 if (dir_name == NULL)
5759 return -1;
5760 r = mch_chdir((char *)dir_name);
5761 vim_free(dir_name);
5762 return r;
5763#endif
5764}
5765
5766/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005767 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005769 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5770 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 * Returns OK or FAIL.
5772 */
5773 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005776 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 if (mch_get_user_name(buf, len) == FAIL)
5779 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005780 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
5782 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005783 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 return OK;
5785}
5786
5787#ifndef HAVE_QSORT
5788/*
5789 * Our own qsort(), for systems that don't have it.
5790 * It's simple and slow. From the K&R C book.
5791 */
5792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005793qsort(
5794 void *base,
5795 size_t elm_count,
5796 size_t elm_size,
5797 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 char_u *buf;
5800 char_u *p1;
5801 char_u *p2;
5802 int i, j;
5803 int gap;
5804
5805 buf = alloc((unsigned)elm_size);
5806 if (buf == NULL)
5807 return;
5808
5809 for (gap = elm_count / 2; gap > 0; gap /= 2)
5810 for (i = gap; i < elm_count; ++i)
5811 for (j = i - gap; j >= 0; j -= gap)
5812 {
5813 /* Compare the elements. */
5814 p1 = (char_u *)base + j * elm_size;
5815 p2 = (char_u *)base + (j + gap) * elm_size;
5816 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5817 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005818 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 mch_memmove(buf, p1, elm_size);
5820 mch_memmove(p1, p2, elm_size);
5821 mch_memmove(p2, buf, elm_size);
5822 }
5823
5824 vim_free(buf);
5825}
5826#endif
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828/*
5829 * Sort an array of strings.
5830 */
5831static int
5832#ifdef __BORLANDC__
5833_RTLENTRYF
5834#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005835sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
5837 static int
5838#ifdef __BORLANDC__
5839_RTLENTRYF
5840#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005841sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 return STRCMP(*(char **)s1, *(char **)s2);
5844}
5845
5846 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005847sort_strings(
5848 char_u **files,
5849 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854#if !defined(NO_EXPANDPATH) || defined(PROTO)
5855/*
5856 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005857 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 * Return value like strcmp(p, q), but consider path separators.
5859 */
5860 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005861pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005863 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005864 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005865 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005867 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005869 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005870 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005871
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005873 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005875 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 return 0;
5877 s = q;
Bram Moolenaar15675582018-02-09 12:29:56 +01005878 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 break;
5880 }
5881
5882 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005883 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
5885 s = p;
5886 break;
5887 }
5888
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005889 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#ifdef BACKSLASH_IN_FILENAME
5891 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005892 && !((c1 == '/' && c2 == '\\')
5893 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894#endif
5895 )
5896 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005897 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005899 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005901 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5902 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005904
5905 i += MB_PTR2LEN((char_u *)p + i);
5906 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005908 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005909 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005911 c1 = PTR2CHAR((char_u *)s + i);
5912 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005914 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005915 && i > 0
5916 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005918 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005920 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005922 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 return 0; /* match with trailing slash */
5924 if (s == q)
5925 return -1; /* no match */
5926 return 1;
5927}
5928#endif
5929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930/*
5931 * The putenv() implementation below comes from the "screen" program.
5932 * Included with permission from Juergen Weigert.
5933 * See pty.c for the copyright notice.
5934 */
5935
5936/*
5937 * putenv -- put value into environment
5938 *
5939 * Usage: i = putenv (string)
5940 * int i;
5941 * char *string;
5942 *
5943 * where string is of the form <name>=<value>.
5944 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5945 *
5946 * Putenv may need to add a new name into the environment, or to
5947 * associate a value longer than the current value with a particular
5948 * name. So, to make life simpler, putenv() copies your entire
5949 * environment into the heap (i.e. malloc()) from the stack
5950 * (i.e. where it resides when your process is initiated) the first
5951 * time you call it.
5952 *
5953 * (history removed, not very interesting. See the "screen" sources.)
5954 */
5955
5956#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5957
5958#define EXTRASIZE 5 /* increment to add to env. size */
5959
5960static int envsize = -1; /* current size of environment */
Bram Moolenaard0573012017-10-28 21:11:06 +02005961extern char **environ; /* the global which is your env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005963static int findenv(char *name); /* look for a name in the env. */
5964static int newenv(void); /* copy env. from stack to heap */
5965static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
5967 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005968putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 int i;
5971 char *p;
5972
5973 if (envsize < 0)
5974 { /* first time putenv called */
5975 if (newenv() < 0) /* copy env. to heap */
5976 return -1;
5977 }
5978
5979 i = findenv((char *)string); /* look for name in environment */
5980
5981 if (i < 0)
5982 { /* name must be added */
5983 for (i = 0; environ[i]; i++);
5984 if (i >= (envsize - 1))
5985 { /* need new slot */
5986 if (moreenv() < 0)
5987 return -1;
5988 }
5989 p = (char *)alloc((unsigned)(strlen(string) + 1));
5990 if (p == NULL) /* not enough core */
5991 return -1;
5992 environ[i + 1] = 0; /* new end of env. */
5993 }
5994 else
5995 { /* name already in env. */
5996 p = vim_realloc(environ[i], strlen(string) + 1);
5997 if (p == NULL)
5998 return -1;
5999 }
6000 sprintf(p, "%s", string); /* copy into env. */
6001 environ[i] = p;
6002
6003 return 0;
6004}
6005
6006 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006007findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 char *namechar, *envchar;
6010 int i, found;
6011
6012 found = 0;
6013 for (i = 0; environ[i] && !found; i++)
6014 {
6015 envchar = environ[i];
6016 namechar = name;
6017 while (*namechar && *namechar != '=' && (*namechar == *envchar))
6018 {
6019 namechar++;
6020 envchar++;
6021 }
6022 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6023 }
6024 return found ? i - 1 : -1;
6025}
6026
6027 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006028newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 char **env, *elem;
6031 int i, esize;
6032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 for (i = 0; environ[i]; i++)
6034 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02006035
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 esize = i + EXTRASIZE + 1;
6037 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6038 if (env == NULL)
6039 return -1;
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 for (i = 0; environ[i]; i++)
6042 {
6043 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6044 if (elem == NULL)
6045 return -1;
6046 env[i] = elem;
6047 strcpy(elem, environ[i]);
6048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 env[i] = 0;
6051 environ = env;
6052 envsize = esize;
6053 return 0;
6054}
6055
6056 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006057moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 int esize;
6060 char **env;
6061
6062 esize = envsize + EXTRASIZE;
6063 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6064 if (env == 0)
6065 return -1;
6066 environ = env;
6067 envsize = esize;
6068 return 0;
6069}
6070
6071# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02006072/*
6073 * Used for mch_getenv() for Mac.
6074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006076vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 int i;
6079 char_u *p;
6080
6081 if (envsize < 0)
6082 return NULL;
6083
6084 i = findenv((char *)string);
6085
6086 if (i < 0)
6087 return NULL;
6088
6089 p = vim_strchr((char_u *)environ[i], '=');
6090 return (p + 1);
6091}
6092# endif
6093
6094#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006095
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006096#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006097/*
6098 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6099 * rights to write into.
6100 */
6101 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006102filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006103{
6104 int retval = 0;
6105#if defined(UNIX) || defined(VMS)
6106 int perm = 0;
6107#endif
6108
6109#if defined(UNIX) || defined(VMS)
6110 perm = mch_getperm(fname);
6111#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006112 if (
6113# ifdef WIN3264
6114 mch_writable(fname) &&
6115# else
6116# if defined(UNIX) || defined(VMS)
6117 (perm & 0222) &&
6118# endif
6119# endif
6120 mch_access((char *)fname, W_OK) == 0
6121 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006122 {
6123 ++retval;
6124 if (mch_isdir(fname))
6125 ++retval;
6126 }
6127 return retval;
6128}
6129#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006130
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006131#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6132/*
6133 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006134 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006135 */
6136 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006137get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006138{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006139 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006140
6141 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006142 if (n == EOF) return -1;
6143 c = getc(fd);
6144 if (c == EOF) return -1;
6145 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006146}
6147
6148/*
6149 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006150 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006151 */
6152 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006153get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006154{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006155 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006156
6157 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006158 if (n == EOF) return -1;
6159 c = getc(fd);
6160 if (c == EOF) return -1;
6161 n = (n << 8) + c;
6162 c = getc(fd);
6163 if (c == EOF) return -1;
6164 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006165}
6166
6167/*
6168 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006169 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006170 */
6171 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006172get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006173{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006174 int c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006175 /* Use unsigned rather than int otherwise result is undefined
6176 * when left-shift sets the MSB. */
6177 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006178
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006179 c = getc(fd);
6180 if (c == EOF) return -1;
6181 n = (unsigned)c;
6182 c = getc(fd);
6183 if (c == EOF) return -1;
6184 n = (n << 8) + (unsigned)c;
6185 c = getc(fd);
6186 if (c == EOF) return -1;
6187 n = (n << 8) + (unsigned)c;
6188 c = getc(fd);
6189 if (c == EOF) return -1;
6190 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006191 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006192}
6193
6194/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006195 * Read 8 bytes from "fd" and turn them into a time_T, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006196 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006197 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006198 time_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006199get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006200{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006201 int c;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006202 time_T n = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006203 int i;
6204
6205 for (i = 0; i < 8; ++i)
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006206 {
6207 c = getc(fd);
6208 if (c == EOF) return -1;
6209 n = (n << 8) + c;
6210 }
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006211 return n;
6212}
6213
6214/*
6215 * Read a string of length "cnt" from "fd" into allocated memory.
6216 * Returns NULL when out of memory or unable to read that many bytes.
6217 */
6218 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006219read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006220{
6221 char_u *str;
6222 int i;
6223 int c;
6224
6225 /* allocate memory */
6226 str = alloc((unsigned)cnt + 1);
6227 if (str != NULL)
6228 {
6229 /* Read the string. Quit when running into the EOF. */
6230 for (i = 0; i < cnt; ++i)
6231 {
6232 c = getc(fd);
6233 if (c == EOF)
6234 {
6235 vim_free(str);
6236 return NULL;
6237 }
6238 str[i] = c;
6239 }
6240 str[i] = NUL;
6241 }
6242 return str;
6243}
6244
6245/*
6246 * Write a number to file "fd", MSB first, in "len" bytes.
6247 */
6248 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006249put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006250{
6251 int i;
6252
6253 for (i = len - 1; i >= 0; --i)
6254 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6255 return FAIL;
6256 return OK;
6257}
6258
6259#ifdef _MSC_VER
6260# if (_MSC_VER <= 1200)
6261/* This line is required for VC6 without the service pack. Also see the
6262 * matching #pragma below. */
6263 # pragma optimize("", off)
6264# endif
6265#endif
6266
6267/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006268 * Write time_T to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006269 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006270 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006271 int
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006272put_time(FILE *fd, time_T the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006273{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006274 char_u buf[8];
6275
6276 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006277 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006278}
6279
6280/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006281 * Write time_T to "buf[8]".
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006282 */
6283 void
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006284time_to_bytes(time_T the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006285{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006286 int c;
6287 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006288 int bi = 0;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006289 time_T wtime = the_time;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006290
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006291 /* time_T can be up to 8 bytes in size, more than long_u, thus we
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006292 * can't use put_bytes() here.
6293 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006294 * sign. This happens for large values of wtime. A cast to long_u may
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006295 * truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes,
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006296 * it's safe to assume that long_u is 4 bytes or more and when using 8
6297 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006298 for (i = 7; i >= 0; --i)
6299 {
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006300 if (i + 1 > (int)sizeof(time_T))
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006301 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006302 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006303 else
6304 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006305#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006306 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006307#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006308 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006309#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006310 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006311 }
6312 }
6313}
6314
6315#ifdef _MSC_VER
6316# if (_MSC_VER <= 1200)
6317 # pragma optimize("", on)
6318# endif
6319#endif
6320
6321#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006322
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006323#if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006324/*
6325 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6326 * When "s" is NULL FALSE is returned.
6327 */
6328 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006329has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006330{
6331 char_u *p;
6332
6333 if (s != NULL)
6334 for (p = s; *p != NUL; ++p)
6335 if (*p >= 128)
6336 return TRUE;
6337 return FALSE;
6338}
6339#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006340
6341#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006342# define MAX_REPEAT_PARSE 8
6343
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006344/*
6345 * Process messages that have been queued for netbeans or clientserver.
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006346 * Also check if any jobs have ended.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006347 * These functions can call arbitrary vimscript and should only be called when
6348 * it is safe to do so.
6349 */
6350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006351parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006352{
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006353 win_T *old_curwin = curwin;
6354 int i;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006355
Bram Moolenaar94f01952018-09-01 15:30:03 +02006356 // Do not handle messages while redrawing, because it may cause buffers to
6357 // change or be wiped while they are being redrawn.
6358 if (updating_screen)
6359 return;
6360
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006361 // Loop when a job ended, but don't keep looping forever.
6362 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
6363 {
6364 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006365# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006366 channel_handle_events(FALSE);
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006367# endif
6368
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006369# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006370 // Process the queued netbeans messages.
6371 netbeans_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006372# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006373# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006374 // Write any buffer lines still to be written.
6375 channel_write_any_lines();
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006376
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006377 // Process the messages queued on channels.
6378 channel_parse_messages();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006379# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006380# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006381 // Process the queued clientserver messages.
6382 server_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006383# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006384# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006385 // Check if any jobs have ended. If so, repeat the above to handle
6386 // changes, e.g. stdin may have been closed.
6387 if (job_check_ended())
6388 continue;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006389# endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01006390# ifdef FEAT_TERMINAL
6391 free_unused_terminals();
6392# endif
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006393 break;
6394 }
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006395
Bram Moolenaar94f01952018-09-01 15:30:03 +02006396 // If the current window changed we need to bail out of the waiting loop.
6397 // E.g. when a job exit callback closes the terminal window.
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006398 if (curwin != old_curwin)
6399 ins_char_typebuf(K_IGNORE);
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006400}
6401#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006402
Bram Moolenaar51628222016-12-01 23:03:28 +01006403#ifndef PROTO /* proto is defined in vim.h */
6404# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006405/*
6406 * Return time in msec since "start_tv".
6407 */
6408 long
6409elapsed(struct timeval *start_tv)
6410{
6411 struct timeval now_tv;
6412
6413 gettimeofday(&now_tv, NULL);
6414 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
6415 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
6416}
Bram Moolenaar51628222016-12-01 23:03:28 +01006417# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006418
Bram Moolenaar51628222016-12-01 23:03:28 +01006419# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006420/*
6421 * Return time in msec since "start_tick".
6422 */
6423 long
6424elapsed(DWORD start_tick)
6425{
6426 DWORD now = GetTickCount();
6427
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006428 return (long)now - (long)start_tick;
6429}
Bram Moolenaar51628222016-12-01 23:03:28 +01006430# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006431#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006432
6433#if defined(FEAT_JOB_CHANNEL) \
6434 || (defined(UNIX) && (!defined(USE_SYSTEM) \
6435 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
6436 || defined(PROTO)
6437/*
6438 * Parse "cmd" and put the white-separated parts in "argv".
6439 * "argv" is an allocated array with "argc" entries and room for 4 more.
6440 * Returns FAIL when out of memory.
6441 */
6442 int
6443mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
6444{
6445 int i;
6446 char_u *p, *d;
6447 int inquote;
6448
6449 /*
6450 * Do this loop twice:
6451 * 1: find number of arguments
6452 * 2: separate them and build argv[]
6453 */
6454 for (i = 0; i < 2; ++i)
6455 {
6456 p = skipwhite(cmd);
6457 inquote = FALSE;
6458 *argc = 0;
6459 for (;;)
6460 {
6461 if (i == 1)
6462 (*argv)[*argc] = (char *)p;
6463 ++*argc;
6464 d = p;
6465 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
6466 {
6467 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006468 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02006469 inquote = !inquote;
6470 else
6471 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006472 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02006473 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006474 // First pass: skip over "\ " and "\"".
6475 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02006476 ++p;
6477 }
6478 if (i == 1)
6479 *d++ = *p;
6480 }
6481 ++p;
6482 }
6483 if (*p == NUL)
6484 {
6485 if (i == 1)
6486 *d++ = NUL;
6487 break;
6488 }
6489 if (i == 1)
6490 *d++ = NUL;
6491 p = skipwhite(p + 1);
6492 }
6493 if (*argv == NULL)
6494 {
6495 if (use_shcf)
6496 {
6497 /* Account for possible multiple args in p_shcf. */
6498 p = p_shcf;
6499 for (;;)
6500 {
6501 p = skiptowhite(p);
6502 if (*p == NUL)
6503 break;
6504 ++*argc;
6505 p = skipwhite(p);
6506 }
6507 }
6508
6509 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
6510 if (*argv == NULL) /* out of memory */
6511 return FAIL;
6512 }
6513 }
6514 return OK;
6515}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006516
6517# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6518/*
6519 * Build "argv[argc]" from the string "cmd".
6520 * "argv[argc]" is set to NULL;
6521 * Return FAIL when out of memory.
6522 */
6523 int
6524build_argv_from_string(char_u *cmd, char ***argv, int *argc)
6525{
6526 char_u *cmd_copy;
6527 int i;
6528
6529 /* Make a copy, parsing will modify "cmd". */
6530 cmd_copy = vim_strsave(cmd);
6531 if (cmd_copy == NULL
6532 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
6533 {
6534 vim_free(cmd_copy);
6535 return FAIL;
6536 }
6537 for (i = 0; i < *argc; i++)
6538 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
6539 (*argv)[*argc] = NULL;
6540 vim_free(cmd_copy);
6541 return OK;
6542}
6543
6544/*
6545 * Build "argv[argc]" from the list "l".
6546 * "argv[argc]" is set to NULL;
6547 * Return FAIL when out of memory.
6548 */
6549 int
6550build_argv_from_list(list_T *l, char ***argv, int *argc)
6551{
6552 listitem_T *li;
6553 char_u *s;
6554
6555 /* Pass argv[] to mch_call_shell(). */
6556 *argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
6557 if (*argv == NULL)
6558 return FAIL;
6559 *argc = 0;
6560 for (li = l->lv_first; li != NULL; li = li->li_next)
6561 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006562 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006563 if (s == NULL)
6564 {
6565 int i;
6566
6567 for (i = 0; i < *argc; ++i)
6568 vim_free((*argv)[i]);
6569 return FAIL;
6570 }
6571 (*argv)[*argc] = (char *)vim_strsave(s);
6572 *argc += 1;
6573 }
6574 (*argv)[*argc] = NULL;
6575 return OK;
6576}
6577# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006578#endif