blob: 1f5f75269046c6d7d801a1927c900cdfd198060d [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 Moolenaar071d4272004-06-13 20:20:40 +000019#if defined(FEAT_VIRTUALEDIT) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22/*
23 * Return TRUE if in the current mode we need to use virtual.
24 */
25 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010026virtual_active(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027{
28 /* While an operator is being executed we return "virtual_op", because
29 * VIsual_active has already been reset, thus we can't check for "block"
30 * being used. */
31 if (virtual_op != MAYBE)
32 return virtual_op;
33 return (ve_flags == VE_ALL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 || ((ve_flags & VE_INSERT) && (State & INSERT)));
36}
37
38/*
39 * Get the screen position of the cursor.
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
44 colnr_T x;
45
46 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
47 return (int)x;
48}
49
50/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000051 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 * cursor in that column.
53 * The caller must have saved the cursor line for undo!
54 */
55 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010056coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000057{
58 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
59
60 if (wcol == MAXCOL)
61 curwin->w_valid &= ~VALID_VIRTCOL;
62 else
63 {
64 /* Virtcol is valid */
65 curwin->w_valid |= VALID_VIRTCOL;
66 curwin->w_virtcol = wcol;
67 }
68 return rc;
69}
70#endif
71
72/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010073 * Get the screen position of character col with a coladd in the cursor line.
74 */
75 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010076getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010077{
78 colnr_T x;
79 pos_T pos;
80
81 pos.lnum = curwin->w_cursor.lnum;
82 pos.col = col;
83#ifdef FEAT_VIRTUALEDIT
84 pos.coladd = coladd;
85#endif
86 getvvcol(curwin, &pos, &x, NULL, NULL);
87 return (int)x;
88}
89
90/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 * Try to advance the Cursor to the specified screen column.
92 * If virtual editing: fine tune the cursor position.
93 * Note that all virtual positions off the end of a line should share
94 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
95 * beginning at coladd 0.
96 *
97 * return OK if desired column is reached, FAIL if not
98 */
99 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100100coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
102 int rc = getvpos(&curwin->w_cursor, wcol);
103
104 if (wcol == MAXCOL || rc == FAIL)
105 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000106 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000108 /* Virtcol is valid when not on a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 curwin->w_valid |= VALID_VIRTCOL;
110 curwin->w_virtcol = wcol;
111 }
112 return rc;
113}
114
115/*
116 * Return in "pos" the position of the cursor advanced to screen column "wcol".
117 * return OK if desired column is reached, FAIL if not
118 */
119 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100120getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121{
122#ifdef FEAT_VIRTUALEDIT
123 return coladvance2(pos, FALSE, virtual_active(), wcol);
124}
125
126 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100127coladvance2(
128 pos_T *pos,
129 int addspaces, /* change the text to achieve our goal? */
130 int finetune, /* change char offset for the exact column */
131 colnr_T wcol) /* column to move to */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133#endif
134 int idx;
135 char_u *ptr;
136 char_u *line;
137 colnr_T col = 0;
138 int csize = 0;
139 int one_more;
140#ifdef FEAT_LINEBREAK
141 int head = 0;
142#endif
143
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000144 one_more = (State & INSERT)
145 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000146 || (VIsual_active && *p_sel != 'o')
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000147#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000148 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000149#endif
150 ;
Bram Moolenaara1381de2009-11-03 15:44:21 +0000151 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153 if (wcol >= MAXCOL)
154 {
155 idx = (int)STRLEN(line) - 1 + one_more;
156 col = wcol;
157
158#ifdef FEAT_VIRTUALEDIT
159 if ((addspaces || finetune) && !VIsual_active)
160 {
161 curwin->w_curswant = linetabsize(line) + one_more;
162 if (curwin->w_curswant > 0)
163 --curwin->w_curswant;
164 }
165#endif
166 }
167 else
168 {
169#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar02631462017-09-22 15:20:32 +0200170 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaarebefac62005-12-28 22:39:57 +0000172 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 && curwin->w_width != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 && wcol >= (colnr_T)width)
176 {
177 csize = linetabsize(line);
178 if (csize > 0)
179 csize--;
180
Bram Moolenaarebefac62005-12-28 22:39:57 +0000181 if (wcol / width > (colnr_T)csize / width
182 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
184 /* In case of line wrapping don't move the cursor beyond the
Bram Moolenaarebefac62005-12-28 22:39:57 +0000185 * right screen edge. In Insert mode allow going just beyond
186 * the last character (like what happens when typing and
187 * reaching the right window edge). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 wcol = (csize / width + 1) * width - 1;
189 }
190 }
191#endif
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 ptr = line;
194 while (col <= wcol && *ptr != NUL)
195 {
196 /* Count a tab for what it's worth (if list mode not on) */
197#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200198 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100199 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200201 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203 col += csize;
204 }
205 idx = (int)(ptr - line);
206 /*
207 * Handle all the special cases. The virtual_active() check
208 * is needed to ensure that a virtual position off the end of
209 * a line has the correct indexing. The one_more comparison
210 * replaces an explicit add of one_more later on.
211 */
212 if (col > wcol || (!virtual_active() && one_more == 0))
213 {
214 idx -= 1;
215# ifdef FEAT_LINEBREAK
216 /* Don't count the chars from 'showbreak'. */
217 csize -= head;
218# endif
219 col -= csize;
220 }
221
222#ifdef FEAT_VIRTUALEDIT
223 if (virtual_active()
224 && addspaces
225 && ((col != wcol && col != wcol + 1) || csize > 1))
226 {
227 /* 'virtualedit' is set: The difference between wcol and col is
228 * filled with spaces. */
229
230 if (line[idx] == NUL)
231 {
232 /* Append spaces */
233 int correct = wcol - col;
234 char_u *newline = alloc(idx + correct + 1);
235 int t;
236
237 if (newline == NULL)
238 return FAIL;
239
240 for (t = 0; t < idx; ++t)
241 newline[t] = line[t];
242
243 for (t = 0; t < correct; ++t)
244 newline[t + idx] = ' ';
245
246 newline[idx + correct] = NUL;
247
248 ml_replace(pos->lnum, newline, FALSE);
249 changed_bytes(pos->lnum, (colnr_T)idx);
250 idx += correct;
251 col = wcol;
252 }
253 else
254 {
255 /* Break a tab */
256 int linelen = (int)STRLEN(line);
257 int correct = wcol - col - csize + 1; /* negative!! */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000258 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 int t, s = 0;
260 int v;
261
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000262 if (-correct > csize)
263 return FAIL;
264
265 newline = alloc(linelen + csize);
266 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 return FAIL;
268
269 for (t = 0; t < linelen; t++)
270 {
271 if (t != idx)
272 newline[s++] = line[t];
273 else
274 for (v = 0; v < csize; v++)
275 newline[s++] = ' ';
276 }
277
278 newline[linelen + csize - 1] = NUL;
279
280 ml_replace(pos->lnum, newline, FALSE);
281 changed_bytes(pos->lnum, idx);
282 idx += (csize - 1 + correct);
283 col += correct;
284 }
285 }
286#endif
287 }
288
289 if (idx < 0)
290 pos->col = 0;
291 else
292 pos->col = idx;
293
294#ifdef FEAT_VIRTUALEDIT
295 pos->coladd = 0;
296
297 if (finetune)
298 {
299 if (wcol == MAXCOL)
300 {
301 /* The width of the last character is used to set coladd. */
302 if (!one_more)
303 {
304 colnr_T scol, ecol;
305
306 getvcol(curwin, pos, &scol, NULL, &ecol);
307 pos->coladd = ecol - scol;
308 }
309 }
310 else
311 {
312 int b = (int)wcol - (int)col;
313
314 /* The difference between wcol and col is used to set coladd. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200315 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 pos->coladd = b;
317
318 col += b;
319 }
320 }
321#endif
322
Bram Moolenaara1381de2009-11-03 15:44:21 +0000323 /* prevent from moving onto a trail byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200325 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
327 if (col < wcol)
328 return FAIL;
329 return OK;
330}
331
332/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000333 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100336inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
338 return inc(&curwin->w_cursor);
339}
340
Bram Moolenaar446cb832008-06-24 21:56:24 +0000341/*
342 * Increment the line pointer "lp" crossing line boundaries as necessary.
343 * Return 1 when going to the next line.
344 * Return 2 when moving forward onto a NUL at the end of the line).
345 * Return -1 when at the end of file.
346 * Return 0 otherwise.
347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100349inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 /* when searching position may be set to end of a line */
354 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100356 p = ml_get_pos(lp);
357 if (*p != NUL) /* still within line, move to next char (may be NUL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100359 if (has_mbyte)
360 {
361 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100363 lp->col += l;
364 return ((p[l] != NUL) ? 0 : 2);
365 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100366 lp->col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100368 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369#endif
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100370 return ((p[1] != NUL) ? 0 : 2);
371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 }
373 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
374 {
375 lp->col = 0;
376 lp->lnum++;
377#ifdef FEAT_VIRTUALEDIT
378 lp->coladd = 0;
379#endif
380 return 1;
381 }
382 return -1;
383}
384
385/*
386 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
387 */
388 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100389incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390{
391 int r;
392
393 if ((r = inc(lp)) >= 1 && lp->col)
394 r = inc(lp);
395 return r;
396}
397
398/*
399 * dec(p)
400 *
401 * Decrement the line pointer 'p' crossing line boundaries as necessary.
402 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
403 */
404 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100405dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100407 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408}
409
410 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100411dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
413 char_u *p;
414
415#ifdef FEAT_VIRTUALEDIT
416 lp->coladd = 0;
417#endif
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100418 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100420 /* past end of line */
421 p = ml_get(lp->lnum);
422 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100423 if (has_mbyte)
424 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100425 return 0;
426 }
427
428 if (lp->col > 0)
429 {
430 /* still within line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 if (has_mbyte)
433 {
434 p = ml_get(lp->lnum);
435 lp->col -= (*mb_head_off)(p, p + lp->col);
436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 return 0;
438 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100439
440 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100442 /* there is a prior line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 lp->lnum--;
444 p = ml_get(lp->lnum);
445 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 if (has_mbyte)
447 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 return 1;
449 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100450
451 /* at start of file */
452 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453}
454
455/*
456 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
457 */
458 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100459decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460{
461 int r;
462
463 if ((r = dec(lp)) == 1 && lp->col)
464 r = dec(lp);
465 return r;
466}
467
468/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200469 * Get the line number relative to the current cursor position, i.e. the
470 * difference between line number and cursor position. Only look for lines that
471 * can be visible, folded lines don't count.
472 */
473 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100474get_cursor_rel_lnum(
475 win_T *wp,
476 linenr_T lnum) /* line number to get the result for */
Bram Moolenaar64486672010-05-16 15:46:46 +0200477{
478 linenr_T cursor = wp->w_cursor.lnum;
479 linenr_T retval = 0;
480
481#ifdef FEAT_FOLDING
482 if (hasAnyFolding(wp))
483 {
484 if (lnum > cursor)
485 {
486 while (lnum > cursor)
487 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100488 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200489 /* if lnum and cursor are in the same fold,
490 * now lnum <= cursor */
491 if (lnum > cursor)
492 retval++;
493 lnum--;
494 }
495 }
496 else if (lnum < cursor)
497 {
498 while (lnum < cursor)
499 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100500 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200501 /* if lnum and cursor are in the same fold,
502 * now lnum >= cursor */
503 if (lnum < cursor)
504 retval--;
505 lnum++;
506 }
507 }
508 /* else if (lnum == cursor)
509 * retval = 0;
510 */
511 }
512 else
513#endif
514 retval = lnum - cursor;
515
516 return retval;
517}
518
519/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200520 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
521 * This allows for the col to be on the NUL byte.
522 */
523 void
524check_pos(buf_T *buf, pos_T *pos)
525{
526 char_u *line;
527 colnr_T len;
528
529 if (pos->lnum > buf->b_ml.ml_line_count)
530 pos->lnum = buf->b_ml.ml_line_count;
531
532 if (pos->col > 0)
533 {
534 line = ml_get_buf(buf, pos->lnum, FALSE);
535 len = (colnr_T)STRLEN(line);
536 if (pos->col > len)
537 pos->col = len;
538 }
539}
540
541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * Make sure curwin->w_cursor.lnum is valid.
543 */
544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100545check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
548 {
549#ifdef FEAT_FOLDING
550 /* If there is a closed fold at the end of the file, put the cursor in
551 * its first line. Otherwise in the last line. */
552 if (!hasFolding(curbuf->b_ml.ml_line_count,
553 &curwin->w_cursor.lnum, NULL))
554#endif
555 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
556 }
557 if (curwin->w_cursor.lnum <= 0)
558 curwin->w_cursor.lnum = 1;
559}
560
561/*
562 * Make sure curwin->w_cursor.col is valid.
563 */
564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100565check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200567 check_cursor_col_win(curwin);
568}
569
570/*
571 * Make sure win->w_cursor.col is valid.
572 */
573 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100574check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200575{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 colnr_T len;
577#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200578 colnr_T oldcol = win->w_cursor.col;
579 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580#endif
581
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200582 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200584 win->w_cursor.col = 0;
585 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000587 /* Allow cursor past end-of-line when:
588 * - in Insert mode or restarting Insert mode
589 * - in Visual mode and 'selection' isn't "old"
590 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000591 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000593#ifdef FEAT_VIRTUALEDIT
594 || (ve_flags & VE_ONEMORE)
595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200597 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000599 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200600 win->w_cursor.col = len - 1;
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200601 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000602 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200603 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200606 else if (win->w_cursor.col < 0)
607 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#ifdef FEAT_VIRTUALEDIT
610 /* If virtual editing is on, we can leave the cursor on the old position,
611 * only we must set it to virtual. But don't do it when at the end of the
612 * line. */
613 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200614 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000616 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200617 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200618 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200619 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200620
621 /* Make sure that coladd is not more than the char width.
622 * Not for the last character, coladd is then used when the cursor
623 * is actually after the last character. */
624 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200625 {
626 int cs, ce;
627
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200628 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
629 if (win->w_cursor.coladd > ce - cs)
630 win->w_cursor.coladd = ce - cs;
631 }
632 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000633 else
634 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200635 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637#endif
638}
639
640/*
641 * make sure curwin->w_cursor in on a valid character
642 */
643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 check_cursor_lnum();
647 check_cursor_col();
648}
649
650#if defined(FEAT_TEXTOBJ) || defined(PROTO)
651/*
652 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
653 * Allow it when in Visual mode and 'selection' is not "old".
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 && gchar_cursor() == NUL)
661 --curwin->w_cursor.col;
662}
663#endif
664
665/*
666 * When curwin->w_leftcol has changed, adjust the cursor position.
667 * Return TRUE if the cursor was moved.
668 */
669 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100670leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 long lastcol;
673 colnr_T s, e;
674 int retval = FALSE;
675
676 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200677 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 validate_virtcol();
679
680 /*
681 * If the cursor is right or left of the screen, move it to last or first
682 * character.
683 */
684 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
685 {
686 retval = TRUE;
687 coladvance((colnr_T)(lastcol - p_siso));
688 }
689 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
690 {
691 retval = TRUE;
692 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
693 }
694
695 /*
696 * If the start of the character under the cursor is not on the screen,
697 * advance the cursor one more char. If this fails (last char of the
698 * line) adjust the scrolling.
699 */
700 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
701 if (e > (colnr_T)lastcol)
702 {
703 retval = TRUE;
704 coladvance(s - 1);
705 }
706 else if (s < curwin->w_leftcol)
707 {
708 retval = TRUE;
709 if (coladvance(e + 1) == FAIL) /* there isn't another character */
710 {
711 curwin->w_leftcol = s; /* adjust w_leftcol instead */
712 changed_cline_bef_curs();
713 }
714 }
715
716 if (retval)
717 curwin->w_set_curswant = TRUE;
718 redraw_later(NOT_VALID);
719 return retval;
720}
721
722/**********************************************************************
723 * Various routines dealing with allocation and deallocation of memory.
724 */
725
726#if defined(MEM_PROFILE) || defined(PROTO)
727
728# define MEM_SIZES 8200
729static long_u mem_allocs[MEM_SIZES];
730static long_u mem_frees[MEM_SIZES];
731static long_u mem_allocated;
732static long_u mem_freed;
733static long_u mem_peak;
734static long_u num_alloc;
735static long_u num_freed;
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100738mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 *sizep += sizeof(size_t);
741}
742
743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100744mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 *sizep += sizeof(size_t);
747}
748
749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750mem_post_alloc(
751 void **pp,
752 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 if (*pp == NULL)
755 return;
756 size -= sizeof(size_t);
757 *(long_u *)*pp = size;
758 if (size <= MEM_SIZES-1)
759 mem_allocs[size-1]++;
760 else
761 mem_allocs[MEM_SIZES-1]++;
762 mem_allocated += size;
763 if (mem_allocated - mem_freed > mem_peak)
764 mem_peak = mem_allocated - mem_freed;
765 num_alloc++;
766 *pp = (void *)((char *)*pp + sizeof(size_t));
767}
768
769 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100770mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771{
772 long_u size;
773
774 *pp = (void *)((char *)*pp - sizeof(size_t));
775 size = *(size_t *)*pp;
776 if (size <= MEM_SIZES-1)
777 mem_frees[size-1]++;
778 else
779 mem_frees[MEM_SIZES-1]++;
780 mem_freed += size;
781 num_freed++;
782}
783
784/*
785 * called on exit via atexit()
786 */
787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100788vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
790 int i, j;
791
792 printf("\r\n");
793 j = 0;
794 for (i = 0; i < MEM_SIZES - 1; i++)
795 {
796 if (mem_allocs[i] || mem_frees[i])
797 {
798 if (mem_frees[i] > mem_allocs[i])
799 printf("\r\n%s", _("ERROR: "));
800 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
801 j++;
802 if (j > 3)
803 {
804 j = 0;
805 printf("\r\n");
806 }
807 }
808 }
809
810 i = MEM_SIZES - 1;
811 if (mem_allocs[i])
812 {
813 printf("\r\n");
814 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200815 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
817 }
818
819 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
820 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
821 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
822 num_alloc, num_freed);
823}
824
825#endif /* MEM_PROFILE */
826
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100827#ifdef FEAT_EVAL
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100828 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100829alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100830{
831 if (alloc_fail_countdown == 0)
832 {
833 if (--alloc_fail_repeat <= 0)
834 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100835 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100836 return TRUE;
837 }
838 --alloc_fail_countdown;
839 return FALSE;
840}
841#endif
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843/*
844 * Some memory is reserved for error messages and for being able to
845 * call mf_release_all(), which needs some memory for mf_trans_add().
846 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100847#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200848#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000851 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 * Use lalloc for larger blocks.
853 */
854 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100855alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856{
857 return (lalloc((long_u)size, TRUE));
858}
859
860/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100861 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100862 */
863 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100864alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100865{
866#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100867 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100868 return NULL;
869#endif
870 return (lalloc((long_u)size, TRUE));
871}
872
873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 * Allocate memory and set all bytes to zero.
875 */
876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 char_u *p;
880
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200881 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (p != NULL)
883 (void)vim_memset(p, 0, (size_t)size);
884 return p;
885}
886
887/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100888 * Same as alloc_clear() but with allocation id for testing
889 */
890 char_u *
891alloc_clear_id(unsigned size, alloc_id_T id UNUSED)
892{
893#ifdef FEAT_EVAL
894 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
895 return NULL;
896#endif
897 return alloc_clear(size);
898}
899
900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * alloc() with check for maximum line length
902 */
903 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100904alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200906#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (sizeof(int) == 2 && size > 0x7fff)
908 {
909 /* Don't hide this message */
910 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100911 emsg(_("E340: Line is becoming too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 return NULL;
913 }
914#endif
915 return (lalloc((long_u)size, TRUE));
916}
917
918/*
919 * Allocate memory like lalloc() and set all bytes to zero.
920 */
921 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100922lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 char_u *p;
925
926 p = (lalloc(size, message));
927 if (p != NULL)
928 (void)vim_memset(p, 0, (size_t)size);
929 return p;
930}
931
932/*
933 * Low level memory allocation function.
934 * This is used often, KEEP IT FAST!
935 */
936 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100937lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 char_u *p; /* pointer to new storage space */
940 static int releasing = FALSE; /* don't do mf_release_all() recursive */
941 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100942#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 static long_u allocated = 0; /* allocated since last avail check */
944#endif
945
946 /* Safety check for allocating zero bytes */
947 if (size == 0)
948 {
949 /* Don't hide this message */
950 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 siemsg(_("E341: Internal error: lalloc(%ld, )"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 return NULL;
953 }
954
955#ifdef MEM_PROFILE
956 mem_pre_alloc_l(&size);
957#endif
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 /*
960 * Loop when out of memory: Try to release some memfile blocks and
961 * if some blocks are released call malloc again.
962 */
963 for (;;)
964 {
965 /*
966 * Handle three kind of systems:
967 * 1. No check for available memory: Just return.
968 * 2. Slow check for available memory: call mch_avail_mem() after
969 * allocating KEEP_ROOM amount of memory.
970 * 3. Strict check for available memory: call mch_avail_mem()
971 */
972 if ((p = (char_u *)malloc((size_t)size)) != NULL)
973 {
974#ifndef HAVE_AVAIL_MEM
975 /* 1. No check for available memory: Just return. */
976 goto theend;
977#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 /* 2. Slow check for available memory: call mch_avail_mem() after
979 * allocating (KEEP_ROOM / 2) amount of memory. */
980 allocated += size;
981 if (allocated < KEEP_ROOM / 2)
982 goto theend;
983 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200986 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000988 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 p = NULL;
990 }
991 else
992 goto theend;
993#endif
994 }
995 /*
996 * Remember that mf_release_all() is being called to avoid an endless
997 * loop, because mf_release_all() may call alloc() recursively.
998 */
999 if (releasing)
1000 break;
1001 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +00001002
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001003 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001004 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 releasing = FALSE;
1007 if (!try_again)
1008 break;
1009 }
1010
1011 if (message && p == NULL)
1012 do_outofmem_msg(size);
1013
1014theend:
1015#ifdef MEM_PROFILE
1016 mem_post_alloc((void **)&p, (size_t)size);
1017#endif
1018 return p;
1019}
1020
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001021/*
1022 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001023 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001024#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001025 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001026lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001027{
1028#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001029 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001030 return NULL;
1031#endif
1032 return (lalloc((long_u)size, message));
1033}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001034#endif
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#if defined(MEM_PROFILE) || defined(PROTO)
1037/*
1038 * realloc() with memory profiling.
1039 */
1040 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001041mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 void *p;
1044
1045 mem_pre_free(&ptr);
1046 mem_pre_alloc_s(&size);
1047
1048 p = realloc(ptr, size);
1049
1050 mem_post_alloc(&p, size);
1051
1052 return p;
1053}
1054#endif
1055
1056/*
1057* Avoid repeating the error message many times (they take 1 second each).
1058* Did_outofmem_msg is reset when a character is read.
1059*/
1060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001061do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063 if (!did_outofmem_msg)
1064 {
1065 /* Don't hide this message */
1066 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001067
1068 /* Must come first to avoid coming back here when printing the error
1069 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001071
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001072 semsg(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
1074}
1075
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001076#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001077
1078# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001079static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001080# endif
1081
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001082/*
1083 * Free everything that we allocated.
1084 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001085 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1086 * surprised if Vim crashes...
1087 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001088 */
1089 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001090free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001091{
1092 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001093
1094 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1095 * Don't try freeing everything again. */
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001096 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001097 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001098 entered_free_all_mem = TRUE;
Bram Moolenaara9673212016-06-01 22:21:06 +02001099
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001100 /* Don't want to trigger autocommands from here on. */
1101 block_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001102
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001103 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1104 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001105 if (first_tabpage->tp_next != NULL)
1106 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001107 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001108 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001109
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001110# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001111 /* Free all spell info. */
1112 spell_free_all();
1113# endif
1114
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001115#if defined(FEAT_INS_EXPAND) && defined(FEAT_BEVAL_TERM)
1116 ui_remove_balloon();
1117# endif
1118
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001119# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001120 /* Clear user commands (before deleting buffers). */
1121 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001122# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001123
1124# ifdef FEAT_MENU
1125 /* Clear menus. */
1126 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001127# ifdef FEAT_MULTI_LANG
1128 do_cmdline_cmd((char_u *)"menutranslate clear");
1129# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001130# endif
1131
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001132 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001133 do_cmdline_cmd((char_u *)"lmapclear");
1134 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001135 do_cmdline_cmd((char_u *)"mapclear");
1136 do_cmdline_cmd((char_u *)"mapclear!");
1137 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001138# if defined(FEAT_EVAL)
1139 do_cmdline_cmd((char_u *)"breakdel *");
1140# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001141# if defined(FEAT_PROFILE)
1142 do_cmdline_cmd((char_u *)"profdel *");
1143# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001144# if defined(FEAT_KEYMAP)
1145 do_cmdline_cmd((char_u *)"set keymap=");
1146#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001147
1148# ifdef FEAT_TITLE
1149 free_titles();
1150# endif
1151# if defined(FEAT_SEARCHPATH)
1152 free_findfile();
1153# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001154
1155 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001156 free_all_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001157 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001158 free_all_marks();
1159 alist_clear(&global_alist);
1160 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001161# if defined(FEAT_CMDL_COMPL)
1162 free_users();
1163# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001164 free_search_patterns();
1165 free_old_sub();
1166 free_last_insert();
1167 free_prev_shellcmd();
1168 free_regexp_stuff();
1169 free_tag_stuff();
1170 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001171# ifdef FEAT_SIGNS
1172 free_signs();
1173# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001174# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001175 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001176# endif
1177# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001178 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001179# endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001180 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001181
1182 /* Free some global vars. */
1183 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001184# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001185 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001186# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001187 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001188# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001189 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001190# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001191 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001192 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001193
1194 /* Clear cmdline history. */
1195 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001196# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001197 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001198# endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001199#ifdef FEAT_TEXT_PROP
1200 clear_global_prop_types();
1201#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001202
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001203#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001204 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001205 win_T *win;
1206 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001207
1208 qf_free_all(NULL);
1209 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001210 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001211 qf_free_all(win);
1212 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001213#endif
1214
1215 /* Close all script inputs. */
1216 close_all_scripts();
1217
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001218 /* Destroy all windows. Must come before freeing buffers. */
1219 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001220
Bram Moolenaar4f198282017-10-23 21:53:30 +02001221 /* Free all option values. Must come after closing windows. */
1222 free_all_options();
1223
Bram Moolenaar2a329742008-04-01 12:53:43 +00001224 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1225 * were freed already. */
1226#ifdef FEAT_AUTOCHDIR
1227 p_acd = FALSE;
1228#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001229 for (buf = firstbuf; buf != NULL; )
1230 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001231 bufref_T bufref;
1232
1233 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001234 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001235 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001236 if (bufref_valid(&bufref))
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001237 buf = nextbuf; /* didn't work, try next one */
1238 else
1239 buf = firstbuf;
1240 }
1241
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001242# ifdef FEAT_ARABIC
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001243 free_cmdline_buf();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001244# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001245
1246 /* Clear registers. */
1247 clear_registers();
1248 ResetRedobuff();
1249 ResetRedobuff();
1250
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001251# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001252 vim_free(serverDelayedStartName);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001253# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001254
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001255 /* highlight info */
1256 free_highlight();
1257
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001258 reset_last_sourcing();
1259
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001260 free_tabpage(first_tabpage);
1261 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001262
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001263# ifdef UNIX
1264 /* Machine-specific free. */
1265 mch_free_mem();
1266# endif
1267
1268 /* message history */
1269 for (;;)
1270 if (delete_first_msg() == FAIL)
1271 break;
1272
Bram Moolenaar655da312016-05-28 22:22:34 +02001273# ifdef FEAT_JOB_CHANNEL
1274 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001275# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001276# ifdef FEAT_TIMERS
Bram Moolenaar623e2632016-07-30 22:47:56 +02001277 timer_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001278# endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001279# ifdef FEAT_EVAL
1280 /* must be after channel_free_all() with unrefs partials */
1281 eval_clear();
1282# endif
1283# ifdef FEAT_JOB_CHANNEL
1284 /* must be after eval_clear() with unrefs jobs */
1285 job_free_all();
1286# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001287
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001288 free_termoptions();
1289
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001290 /* screenlines (can't display anything now!) */
1291 free_screenlines();
1292
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001293# if defined(USE_XSMP)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001294 xsmp_close();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001295# endif
1296# ifdef FEAT_GUI_GTK
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001297 gui_mch_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001298# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001299 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001300
1301 vim_free(IObuff);
1302 vim_free(NameBuff);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001303# ifdef FEAT_QUICKFIX
1304 check_quickfix_busy();
1305# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001306}
1307#endif
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001310 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 */
1312 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001313vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 char_u *p;
1316 unsigned len;
1317
1318 len = (unsigned)STRLEN(string) + 1;
1319 p = alloc(len);
1320 if (p != NULL)
1321 mch_memmove(p, string, (size_t)len);
1322 return p;
1323}
1324
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001325/*
1326 * Copy up to "len" bytes of "string" into newly allocated memory and
1327 * terminate with a NUL.
1328 * The allocated memory always has size "len + 1", also when "string" is
1329 * shorter.
1330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001332vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 char_u *p;
1335
1336 p = alloc((unsigned)(len + 1));
1337 if (p != NULL)
1338 {
1339 STRNCPY(p, string, len);
1340 p[len] = NUL;
1341 }
1342 return p;
1343}
1344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001346 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1347 * Returns NULL when out of memory.
1348 */
1349 char_u *
1350vim_memsave(char_u *p, int len)
1351{
1352 char_u *ret = alloc((unsigned)len);
1353
1354 if (ret != NULL)
1355 mch_memmove(ret, p, (size_t)len);
1356 return ret;
1357}
1358
1359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1361 * by a backslash.
1362 */
1363 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001364vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001366 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367}
1368
1369/*
1370 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1371 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001372 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 */
1374 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001375vim_strsave_escaped_ext(
1376 char_u *string,
1377 char_u *esc_chars,
1378 int cc,
1379 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380{
1381 char_u *p;
1382 char_u *p2;
1383 char_u *escaped_string;
1384 unsigned length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 /*
1388 * First count the number of backslashes required.
1389 * Then allocate the memory and insert them.
1390 */
1391 length = 1; /* count the trailing NUL */
1392 for (p = string; *p; p++)
1393 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001394 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 length += l; /* count a multibyte char */
1397 p += l - 1;
1398 continue;
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1401 ++length; /* count a backslash */
1402 ++length; /* count an ordinary char */
1403 }
1404 escaped_string = alloc(length);
1405 if (escaped_string != NULL)
1406 {
1407 p2 = escaped_string;
1408 for (p = string; *p; p++)
1409 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001410 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
1412 mch_memmove(p2, p, (size_t)l);
1413 p2 += l;
1414 p += l - 1; /* skip multibyte char */
1415 continue;
1416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001418 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 *p2++ = *p;
1420 }
1421 *p2 = NUL;
1422 }
1423 return escaped_string;
1424}
1425
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001426/*
1427 * Return TRUE when 'shell' has "csh" in the tail.
1428 */
1429 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001430csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001431{
1432 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1433}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001434
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001435/*
1436 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001437 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001438 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001439 * Escape a newline, depending on the 'shell' option.
1440 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1441 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001442 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001443 * Returns the result in allocated memory, NULL if we have run out.
1444 */
1445 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001446vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001447{
1448 unsigned length;
1449 char_u *p;
1450 char_u *d;
1451 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001452 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001453 int csh_like;
1454
1455 /* Only csh and similar shells expand '!' within single quotes. For sh and
1456 * the like we must not put a backslash before it, it will be taken
1457 * literally. If do_special is set the '!' will be escaped twice.
1458 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001459 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001460
1461 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001462 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001463 for (p = string; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001464 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001465# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001466 if (!p_ssl)
1467 {
1468 if (*p == '"')
1469 ++length; /* " -> "" */
1470 }
1471 else
1472# endif
1473 if (*p == '\'')
1474 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001475 if ((*p == '\n' && (csh_like || do_newline))
1476 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001477 {
1478 ++length; /* insert backslash */
1479 if (csh_like && do_special)
1480 ++length; /* insert backslash */
1481 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001482 if (do_special && find_cmdline_var(p, &l) >= 0)
1483 {
1484 ++length; /* insert backslash */
1485 p += l - 1;
1486 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001487 }
1488
1489 /* Allocate memory for the result and fill it. */
1490 escaped_string = alloc(length);
1491 if (escaped_string != NULL)
1492 {
1493 d = escaped_string;
1494
1495 /* add opening quote */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001496# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001497 if (!p_ssl)
1498 *d++ = '"';
1499 else
1500# endif
1501 *d++ = '\'';
1502
1503 for (p = string; *p != NUL; )
1504 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001505# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001506 if (!p_ssl)
1507 {
1508 if (*p == '"')
1509 {
1510 *d++ = '"';
1511 *d++ = '"';
1512 ++p;
1513 continue;
1514 }
1515 }
1516 else
1517# endif
1518 if (*p == '\'')
1519 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001520 *d++ = '\'';
1521 *d++ = '\\';
1522 *d++ = '\'';
1523 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001524 ++p;
1525 continue;
1526 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001527 if ((*p == '\n' && (csh_like || do_newline))
1528 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001529 {
1530 *d++ = '\\';
1531 if (csh_like && do_special)
1532 *d++ = '\\';
1533 *d++ = *p++;
1534 continue;
1535 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001536 if (do_special && find_cmdline_var(p, &l) >= 0)
1537 {
1538 *d++ = '\\'; /* insert backslash */
1539 while (--l >= 0) /* copy the var */
1540 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001541 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001542 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001543
1544 MB_COPY_CHAR(p, d);
1545 }
1546
1547 /* add terminating quote and finish with a NUL */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001548# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001549 if (!p_ssl)
1550 *d++ = '"';
1551 else
1552# endif
1553 *d++ = '\'';
1554 *d = NUL;
1555 }
1556
1557 return escaped_string;
1558}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560/*
1561 * Like vim_strsave(), but make all characters uppercase.
1562 * This uses ASCII lower-to-upper case translation, language independent.
1563 */
1564 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
1567 char_u *p1;
1568
1569 p1 = vim_strsave(string);
1570 vim_strup(p1);
1571 return p1;
1572}
1573
1574/*
1575 * Like vim_strnsave(), but make all characters uppercase.
1576 * This uses ASCII lower-to-upper case translation, language independent.
1577 */
1578 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001579vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 char_u *p1;
1582
1583 p1 = vim_strnsave(string, len);
1584 vim_strup(p1);
1585 return p1;
1586}
1587
1588/*
1589 * ASCII lower-to-upper case translation, language independent.
1590 */
1591 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001592vim_strup(
1593 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
1595 char_u *p2;
1596 int c;
1597
1598 if (p != NULL)
1599 {
1600 p2 = p;
1601 while ((c = *p2) != NUL)
1602#ifdef EBCDIC
1603 *p2++ = isalpha(c) ? toupper(c) : c;
1604#else
1605 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1606#endif
1607 }
1608}
1609
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001610#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001611/*
1612 * Make string "s" all upper-case and return it in allocated memory.
1613 * Handles multi-byte characters as well as possible.
1614 * Returns NULL when out of memory.
1615 */
1616 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001617strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001618{
1619 char_u *p;
1620 char_u *res;
1621
1622 res = p = vim_strsave(orig);
1623
1624 if (res != NULL)
1625 while (*p != NUL)
1626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001627 int l;
1628
1629 if (enc_utf8)
1630 {
1631 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001632 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001633 char_u *s;
1634
1635 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001636 l = utf_ptr2len(p);
1637 if (c == 0)
1638 {
1639 /* overlong sequence, use only the first byte */
1640 c = *p;
1641 l = 1;
1642 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001643 uc = utf_toupper(c);
1644
1645 /* Reallocate string when byte count changes. This is rare,
1646 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001647 newl = utf_char2len(uc);
1648 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001649 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001650 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001651 if (s == NULL)
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001652 {
1653 vim_free(res);
1654 return NULL;
1655 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001656 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001657 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001658 p = s + (p - res);
1659 vim_free(res);
1660 res = s;
1661 }
1662
1663 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001664 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001665 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001666 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001667 p += l; /* skip multi-byte character */
1668 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001669 {
1670 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1671 p++;
1672 }
1673 }
1674
1675 return res;
1676}
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001677
1678/*
1679 * Make string "s" all lower-case and return it in allocated memory.
1680 * Handles multi-byte characters as well as possible.
1681 * Returns NULL when out of memory.
1682 */
1683 char_u *
1684strlow_save(char_u *orig)
1685{
1686 char_u *p;
1687 char_u *res;
1688
1689 res = p = vim_strsave(orig);
1690
1691 if (res != NULL)
1692 while (*p != NUL)
1693 {
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001694 int l;
1695
1696 if (enc_utf8)
1697 {
1698 int c, lc;
1699 int newl;
1700 char_u *s;
1701
1702 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001703 l = utf_ptr2len(p);
1704 if (c == 0)
1705 {
1706 /* overlong sequence, use only the first byte */
1707 c = *p;
1708 l = 1;
1709 }
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001710 lc = utf_tolower(c);
1711
1712 /* Reallocate string when byte count changes. This is rare,
1713 * thus it's OK to do another malloc()/free(). */
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001714 newl = utf_char2len(lc);
1715 if (newl != l)
1716 {
1717 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
1718 if (s == NULL)
1719 {
1720 vim_free(res);
1721 return NULL;
1722 }
1723 mch_memmove(s, res, p - res);
1724 STRCPY(s + (p - res) + newl, p + l);
1725 p = s + (p - res);
1726 vim_free(res);
1727 res = s;
1728 }
1729
1730 utf_char2bytes(lc, p);
1731 p += newl;
1732 }
1733 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1734 p += l; /* skip multi-byte character */
1735 else
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001736 {
1737 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
1738 p++;
1739 }
1740 }
1741
1742 return res;
1743}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001744#endif
1745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 * delete spaces at the end of a string
1748 */
1749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 char_u *q;
1753
1754 q = ptr + STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001755 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 *q = NUL;
1757}
1758
1759/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001760 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001761 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 */
1763 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001764vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766 STRNCPY(to, from, len);
1767 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
1770/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001771 * Like strcat(), but make sure the result fits in "tosize" bytes and is
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001772 * always NUL terminated. "from" and "to" may overlap.
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001773 */
1774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001776{
1777 size_t tolen = STRLEN(to);
1778 size_t fromlen = STRLEN(from);
1779
1780 if (tolen + fromlen + 1 > tosize)
1781 {
1782 mch_memmove(to + tolen, from, tosize - tolen - 1);
1783 to[tosize - 1] = NUL;
1784 }
1785 else
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001786 mch_memmove(to + tolen, from, fromlen + 1);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001787}
1788
1789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 * Isolate one part of a string option where parts are separated with
1791 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001792 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 * "*option" is advanced to the next part.
1794 * The length is returned.
1795 */
1796 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797copy_option_part(
1798 char_u **option,
1799 char_u *buf,
1800 int maxlen,
1801 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
1803 int len = 0;
1804 char_u *p = *option;
1805
1806 /* skip '.' at start of option part, for 'suffixes' */
1807 if (*p == '.')
1808 buf[len++] = *p++;
1809 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1810 {
1811 /*
1812 * Skip backslash before a separator character and space.
1813 */
1814 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1815 ++p;
1816 if (len < maxlen - 1)
1817 buf[len++] = *p;
1818 ++p;
1819 }
1820 buf[len] = NUL;
1821
1822 if (*p != NUL && *p != ',') /* skip non-standard separator */
1823 ++p;
1824 p = skip_to_option_part(p); /* p points to next file name */
1825
1826 *option = p;
1827 return len;
1828}
1829
1830/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001831 * Replacement for free() that ignores NULL pointers.
1832 * Also skip free() when exiting for sure, this helps when we caught a deadly
1833 * signal that was caused by a crash in free().
Bram Moolenaard23a8232018-02-10 18:45:26 +01001834 * If you want to set NULL after calling this function, you should use
1835 * VIM_CLEAR() instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001838vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001840 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
1842#ifdef MEM_PROFILE
1843 mem_pre_free(&x);
1844#endif
1845 free(x);
1846 }
1847}
1848
1849#ifndef HAVE_MEMSET
1850 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001851vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 char *p = ptr;
1854
1855 while (size-- > 0)
1856 *p++ = c;
1857 return ptr;
1858}
1859#endif
1860
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1862/*
1863 * Compare two strings, ignoring case, using current locale.
1864 * Doesn't work for multi-byte characters.
1865 * return 0 for match, < 0 for smaller, > 0 for bigger
1866 */
1867 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001868vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869{
1870 int i;
1871
1872 for (;;)
1873 {
1874 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1875 if (i != 0)
1876 return i; /* this character different */
1877 if (*s1 == NUL)
1878 break; /* strings match until NUL */
1879 ++s1;
1880 ++s2;
1881 }
1882 return 0; /* strings match */
1883}
1884#endif
1885
1886#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1887/*
1888 * Compare two strings, for length "len", ignoring case, using current locale.
1889 * Doesn't work for multi-byte characters.
1890 * return 0 for match, < 0 for smaller, > 0 for bigger
1891 */
1892 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001893vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 int i;
1896
1897 while (len > 0)
1898 {
1899 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1900 if (i != 0)
1901 return i; /* this character different */
1902 if (*s1 == NUL)
1903 break; /* strings match until NUL */
1904 ++s1;
1905 ++s2;
1906 --len;
1907 }
1908 return 0; /* strings match */
1909}
1910#endif
1911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912/*
1913 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001914 * with characters from 128 to 255 correctly. It also doesn't return a
1915 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 */
1917 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 char_u *p;
1921 int b;
1922
1923 p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (enc_utf8 && c >= 0x80)
1925 {
1926 while (*p != NUL)
1927 {
Bram Moolenaarace95982017-03-29 17:30:27 +02001928 int l = utfc_ptr2len(p);
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001929
1930 /* Avoid matching an illegal byte here. */
1931 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001933 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
1935 return NULL;
1936 }
1937 if (enc_dbcs != 0 && c > 255)
1938 {
1939 int n2 = c & 0xff;
1940
1941 c = ((unsigned)c >> 8) & 0xff;
1942 while ((b = *p) != NUL)
1943 {
1944 if (b == c && p[1] == n2)
1945 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001946 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 return NULL;
1949 }
1950 if (has_mbyte)
1951 {
1952 while ((b = *p) != NUL)
1953 {
1954 if (b == c)
1955 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001956 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 return NULL;
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 while ((b = *p) != NUL)
1961 {
1962 if (b == c)
1963 return p;
1964 ++p;
1965 }
1966 return NULL;
1967}
1968
1969/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001970 * Version of strchr() that only works for bytes and handles unsigned char
1971 * strings with characters above 128 correctly. It also doesn't return a
1972 * pointer to the NUL at the end of the string.
1973 */
1974 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001975vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001976{
1977 char_u *p = string;
1978
1979 while (*p != NUL)
1980 {
1981 if (*p == c)
1982 return p;
1983 ++p;
1984 }
1985 return NULL;
1986}
1987
1988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001990 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001991 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 */
1993 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001997 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
Bram Moolenaar05159a02005-02-26 23:04:13 +00001999 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002001 if (*p == c)
2002 retval = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002003 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
2005 return retval;
2006}
2007
2008/*
2009 * Vim's version of strpbrk(), in case it's missing.
2010 * Don't generate a prototype for this, causes problems when it's not used.
2011 */
2012#ifndef PROTO
2013# ifndef HAVE_STRPBRK
2014# ifdef vim_strpbrk
2015# undef vim_strpbrk
2016# endif
2017 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002018vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020 while (*s)
2021 {
2022 if (vim_strchr(charset, *s) != NULL)
2023 return s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002024 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 return NULL;
2027}
2028# endif
2029#endif
2030
2031/*
2032 * Vim has its own isspace() function, because on some machines isspace()
2033 * can't handle characters above 128.
2034 */
2035 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002036vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
2038 return ((x >= 9 && x <= 13) || x == ' ');
2039}
2040
2041/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002042 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 */
2044
2045/*
2046 * Clear an allocated growing array.
2047 */
2048 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002049ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 vim_free(gap->ga_data);
2052 ga_init(gap);
2053}
2054
2055/*
2056 * Clear a growing array that contains a list of strings.
2057 */
2058 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002059ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 int i;
2062
2063 for (i = 0; i < gap->ga_len; ++i)
2064 vim_free(((char_u **)(gap->ga_data))[i]);
2065 ga_clear(gap);
2066}
2067
2068/*
2069 * Initialize a growing array. Don't forget to set ga_itemsize and
2070 * ga_growsize! Or use ga_init2().
2071 */
2072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002073ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002076 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 gap->ga_len = 0;
2078}
2079
2080 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002081ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 ga_init(gap);
2084 gap->ga_itemsize = itemsize;
2085 gap->ga_growsize = growsize;
2086}
2087
2088/*
2089 * Make room in growing array "gap" for at least "n" items.
2090 * Return FAIL for failure, OK otherwise.
2091 */
2092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002093ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002095 size_t old_len;
2096 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 char_u *pp;
2098
Bram Moolenaar86b68352004-12-27 21:59:20 +00002099 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
2101 if (n < gap->ga_growsize)
2102 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002103 new_len = gap->ga_itemsize * (gap->ga_len + n);
2104 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002105 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (pp == NULL)
2107 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002108 old_len = gap->ga_itemsize * gap->ga_maxlen;
2109 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002110 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 gap->ga_data = pp;
2112 }
2113 return OK;
2114}
2115
Bram Moolenaar113e1072019-01-20 15:30:40 +01002116#if defined(FEAT_EVAL) || defined(FEAT_SEARCHPATH) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002118 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002119 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002120 * Returns NULL when out of memory.
2121 */
2122 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002124{
2125 int i;
2126 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002127 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002128 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002129 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002130
2131 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002132 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002133
2134 s = alloc(len + 1);
2135 if (s != NULL)
2136 {
2137 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002138 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002139 for (i = 0; i < gap->ga_len; ++i)
2140 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002141 if (p != s)
2142 {
2143 STRCPY(p, sep);
2144 p += sep_len;
2145 }
2146 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2147 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002148 }
2149 }
2150 return s;
2151}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002152#endif
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002153
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01002154#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002155/*
2156 * Make a copy of string "p" and add it to "gap".
2157 * When out of memory nothing changes.
2158 */
2159 void
2160ga_add_string(garray_T *gap, char_u *p)
2161{
2162 char_u *cp = vim_strsave(p);
2163
2164 if (cp != NULL)
2165 {
2166 if (ga_grow(gap, 1) == OK)
2167 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2168 else
2169 vim_free(cp);
2170 }
2171}
2172#endif
2173
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002176 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 * Note: Does NOT copy the NUL at the end!
2178 */
2179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002180ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
Bram Moolenaar43345542015-11-29 17:35:35 +01002182 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
Bram Moolenaar478af672017-04-10 22:22:42 +02002184 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01002185 return;
2186 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (ga_grow(gap, len) == OK)
2188 {
2189 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2190 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 }
2192}
2193
2194/*
2195 * Append one byte to a growarray which contains bytes.
2196 */
2197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002198ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 if (ga_grow(gap, 1) == OK)
2201 {
2202 *((char *)gap->ga_data + gap->ga_len) = c;
2203 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205}
2206
Bram Moolenaar597a4222014-06-25 14:39:50 +02002207#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2208 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002209/*
2210 * Append the text in "gap" below the cursor line and clear "gap".
2211 */
2212 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002213append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002214{
2215 /* Remove trailing CR. */
2216 if (gap->ga_len > 0
2217 && !curbuf->b_p_bin
2218 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2219 --gap->ga_len;
2220 ga_append(gap, NUL);
2221 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2222 gap->ga_len = 0;
2223}
2224#endif
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226/************************************************************************
2227 * functions that use lookup tables for various things, generally to do with
2228 * special key codes.
2229 */
2230
2231/*
2232 * Some useful tables.
2233 */
2234
2235static struct modmasktable
2236{
2237 short mod_mask; /* Bit-mask for particular key modifier */
2238 short mod_flag; /* Bit(s) for particular key modifier */
2239 char_u name; /* Single letter name of modifier */
2240} mod_mask_table[] =
2241{
2242 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002243 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2245 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2246 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2247 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2248 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02002249#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2251#endif
2252 /* 'A' must be the last one */
2253 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2254 {0, 0, NUL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002255 /* NOTE: when adding an entry, update MAX_KEY_NAME_LEN! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256};
2257
2258/*
2259 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002260 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
2262#define MOD_KEYS_ENTRY_SIZE 5
2263
2264static char_u modifier_keys_table[] =
2265{
2266/* mod mask with modifier without modifier */
2267 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2268 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2269 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2270 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2271 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2272 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2273 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2274 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2275 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2276 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2277 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2278 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2279 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2280 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2281 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2282 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2283 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2284 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2285 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2286 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2287 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2288 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2289 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2290 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2291 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2292 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2293 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2294 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2295 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2296 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2297 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2300
2301 /* vt100 F1 */
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2306
2307 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2308 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2309 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2310 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2311 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2312 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2313 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2314 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2315 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2316 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2317
2318 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2319 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2320 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2321 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2322 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2323 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2324 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2325 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2326 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2327 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2328
2329 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2330 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2331 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2332 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2333 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2334 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2335 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2336 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2337 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2338 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2339
2340 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2341 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2342 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2343 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2344 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2345 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2346 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2347
2348 /* TAB pseudo code*/
2349 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2350
2351 NUL
2352};
2353
2354static struct key_name_entry
2355{
2356 int key; /* Special key code or ascii value */
2357 char_u *name; /* Name of key */
2358} key_names_table[] =
2359{
2360 {' ', (char_u *)"Space"},
2361 {TAB, (char_u *)"Tab"},
2362 {K_TAB, (char_u *)"Tab"},
2363 {NL, (char_u *)"NL"},
2364 {NL, (char_u *)"NewLine"}, /* Alternative name */
2365 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2366 {NL, (char_u *)"LF"}, /* Alternative name */
2367 {CAR, (char_u *)"CR"},
2368 {CAR, (char_u *)"Return"}, /* Alternative name */
2369 {CAR, (char_u *)"Enter"}, /* Alternative name */
2370 {K_BS, (char_u *)"BS"},
2371 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2372 {ESC, (char_u *)"Esc"},
2373 {CSI, (char_u *)"CSI"},
2374 {K_CSI, (char_u *)"xCSI"},
2375 {'|', (char_u *)"Bar"},
2376 {'\\', (char_u *)"Bslash"},
2377 {K_DEL, (char_u *)"Del"},
2378 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2379 {K_KDEL, (char_u *)"kDel"},
2380 {K_UP, (char_u *)"Up"},
2381 {K_DOWN, (char_u *)"Down"},
2382 {K_LEFT, (char_u *)"Left"},
2383 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002384 {K_XUP, (char_u *)"xUp"},
2385 {K_XDOWN, (char_u *)"xDown"},
2386 {K_XLEFT, (char_u *)"xLeft"},
2387 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01002388 {K_PS, (char_u *)"PasteStart"},
2389 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 {K_F1, (char_u *)"F1"},
2392 {K_F2, (char_u *)"F2"},
2393 {K_F3, (char_u *)"F3"},
2394 {K_F4, (char_u *)"F4"},
2395 {K_F5, (char_u *)"F5"},
2396 {K_F6, (char_u *)"F6"},
2397 {K_F7, (char_u *)"F7"},
2398 {K_F8, (char_u *)"F8"},
2399 {K_F9, (char_u *)"F9"},
2400 {K_F10, (char_u *)"F10"},
2401
2402 {K_F11, (char_u *)"F11"},
2403 {K_F12, (char_u *)"F12"},
2404 {K_F13, (char_u *)"F13"},
2405 {K_F14, (char_u *)"F14"},
2406 {K_F15, (char_u *)"F15"},
2407 {K_F16, (char_u *)"F16"},
2408 {K_F17, (char_u *)"F17"},
2409 {K_F18, (char_u *)"F18"},
2410 {K_F19, (char_u *)"F19"},
2411 {K_F20, (char_u *)"F20"},
2412
2413 {K_F21, (char_u *)"F21"},
2414 {K_F22, (char_u *)"F22"},
2415 {K_F23, (char_u *)"F23"},
2416 {K_F24, (char_u *)"F24"},
2417 {K_F25, (char_u *)"F25"},
2418 {K_F26, (char_u *)"F26"},
2419 {K_F27, (char_u *)"F27"},
2420 {K_F28, (char_u *)"F28"},
2421 {K_F29, (char_u *)"F29"},
2422 {K_F30, (char_u *)"F30"},
2423
2424 {K_F31, (char_u *)"F31"},
2425 {K_F32, (char_u *)"F32"},
2426 {K_F33, (char_u *)"F33"},
2427 {K_F34, (char_u *)"F34"},
2428 {K_F35, (char_u *)"F35"},
2429 {K_F36, (char_u *)"F36"},
2430 {K_F37, (char_u *)"F37"},
2431
2432 {K_XF1, (char_u *)"xF1"},
2433 {K_XF2, (char_u *)"xF2"},
2434 {K_XF3, (char_u *)"xF3"},
2435 {K_XF4, (char_u *)"xF4"},
2436
2437 {K_HELP, (char_u *)"Help"},
2438 {K_UNDO, (char_u *)"Undo"},
2439 {K_INS, (char_u *)"Insert"},
2440 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2441 {K_KINS, (char_u *)"kInsert"},
2442 {K_HOME, (char_u *)"Home"},
2443 {K_KHOME, (char_u *)"kHome"},
2444 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002445 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {K_END, (char_u *)"End"},
2447 {K_KEND, (char_u *)"kEnd"},
2448 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002449 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {K_PAGEUP, (char_u *)"PageUp"},
2451 {K_PAGEDOWN, (char_u *)"PageDown"},
2452 {K_KPAGEUP, (char_u *)"kPageUp"},
2453 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2454
2455 {K_KPLUS, (char_u *)"kPlus"},
2456 {K_KMINUS, (char_u *)"kMinus"},
2457 {K_KDIVIDE, (char_u *)"kDivide"},
2458 {K_KMULTIPLY, (char_u *)"kMultiply"},
2459 {K_KENTER, (char_u *)"kEnter"},
2460 {K_KPOINT, (char_u *)"kPoint"},
2461
2462 {K_K0, (char_u *)"k0"},
2463 {K_K1, (char_u *)"k1"},
2464 {K_K2, (char_u *)"k2"},
2465 {K_K3, (char_u *)"k3"},
2466 {K_K4, (char_u *)"k4"},
2467 {K_K5, (char_u *)"k5"},
2468 {K_K6, (char_u *)"k6"},
2469 {K_K7, (char_u *)"k7"},
2470 {K_K8, (char_u *)"k8"},
2471 {K_K9, (char_u *)"k9"},
2472
2473 {'<', (char_u *)"lt"},
2474
2475 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002476#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002478#endif
2479#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002481#endif
2482#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002484#endif
2485#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002487#endif
2488#ifdef FEAT_MOUSE_URXVT
2489 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2490#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002491#ifdef FEAT_MOUSE_SGR
2492 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaara529ce02017-06-22 22:37:57 +02002493 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2496 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2497 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2498 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2499 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002500 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2502 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2503 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2504 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2505 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2506 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002507 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2508 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2509 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2510 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2511 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2512 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {K_X1MOUSE, (char_u *)"X1Mouse"},
2514 {K_X1DRAG, (char_u *)"X1Drag"},
2515 {K_X1RELEASE, (char_u *)"X1Release"},
2516 {K_X2MOUSE, (char_u *)"X2Mouse"},
2517 {K_X2DRAG, (char_u *)"X2Drag"},
2518 {K_X2RELEASE, (char_u *)"X2Release"},
2519 {K_DROP, (char_u *)"Drop"},
2520 {K_ZERO, (char_u *)"Nul"},
2521#ifdef FEAT_EVAL
2522 {K_SNR, (char_u *)"SNR"},
2523#endif
2524 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002525 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {0, NULL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002527 /* NOTE: When adding a long name update MAX_KEY_NAME_LEN. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528};
2529
2530#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2531
2532#ifdef FEAT_MOUSE
2533static struct mousetable
2534{
2535 int pseudo_code; /* Code for pseudo mouse event */
2536 int button; /* Which mouse button is it? */
2537 int is_click; /* Is it a mouse button click event? */
2538 int is_drag; /* Is it a mouse drag event? */
2539} mouse_table[] =
2540{
2541 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2542#ifdef FEAT_GUI
2543 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2544#endif
2545 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2546 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2547#ifdef FEAT_GUI
2548 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2549#endif
2550 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2551 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2552 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2553 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2554 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2555 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2556 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2557 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2558 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2559 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2560 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2561 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2562 /* DRAG without CLICK */
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002563 {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 /* RELEASE without CLICK */
2565 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2566 {0, 0, 0, 0},
2567};
2568#endif /* FEAT_MOUSE */
2569
2570/*
2571 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2572 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2573 */
2574 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002575name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
2577 int i;
2578
2579 c = TOUPPER_ASC(c);
2580 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2581 if (c == mod_mask_table[i].name)
2582 return mod_mask_table[i].mod_flag;
2583 return 0;
2584}
2585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586/*
2587 * Check if if there is a special key code for "key" that includes the
2588 * modifiers specified.
2589 */
2590 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002591simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 int i;
2594 int key0;
2595 int key1;
2596
2597 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2598 {
2599 /* TAB is a special case */
2600 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2601 {
2602 *modifiers &= ~MOD_MASK_SHIFT;
2603 return K_S_TAB;
2604 }
2605 key0 = KEY2TERMCAP0(key);
2606 key1 = KEY2TERMCAP1(key);
2607 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2608 if (key0 == modifier_keys_table[i + 3]
2609 && key1 == modifier_keys_table[i + 4]
2610 && (*modifiers & modifier_keys_table[i]))
2611 {
2612 *modifiers &= ~modifier_keys_table[i];
2613 return TERMCAP2KEY(modifier_keys_table[i + 1],
2614 modifier_keys_table[i + 2]);
2615 }
2616 }
2617 return key;
2618}
2619
2620/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002621 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002622 */
2623 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002624handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002625{
2626 switch (key)
2627 {
2628 case K_XUP: return K_UP;
2629 case K_XDOWN: return K_DOWN;
2630 case K_XLEFT: return K_LEFT;
2631 case K_XRIGHT: return K_RIGHT;
2632 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002633 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002634 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002635 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002636 case K_XF1: return K_F1;
2637 case K_XF2: return K_F2;
2638 case K_XF3: return K_F3;
2639 case K_XF4: return K_F4;
2640 case K_S_XF1: return K_S_F1;
2641 case K_S_XF2: return K_S_F2;
2642 case K_S_XF3: return K_S_F3;
2643 case K_S_XF4: return K_S_F4;
2644 }
2645 return key;
2646}
2647
2648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 * Return a string which contains the name of the given key when the given
2650 * modifiers are down.
2651 */
2652 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002653get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
2655 static char_u string[MAX_KEY_NAME_LEN + 1];
2656
2657 int i, idx;
2658 int table_idx;
2659 char_u *s;
2660
2661 string[0] = '<';
2662 idx = 1;
2663
2664 /* Key that stands for a normal character. */
2665 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2666 c = KEY2TERMCAP1(c);
2667
2668 /*
2669 * Translate shifted special keys into unshifted keys and set modifier.
2670 * Same for CTRL and ALT modifiers.
2671 */
2672 if (IS_SPECIAL(c))
2673 {
2674 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2675 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2676 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2677 {
2678 modifiers |= modifier_keys_table[i];
2679 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2680 modifier_keys_table[i + 4]);
2681 break;
2682 }
2683 }
2684
2685 /* try to find the key in the special key table */
2686 table_idx = find_special_key_in_table(c);
2687
2688 /*
2689 * When not a known special key, and not a printable character, try to
2690 * extract modifiers.
2691 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002692 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
2694 if (table_idx < 0
2695 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2696 && (c & 0x80))
2697 {
2698 c &= 0x7f;
2699 modifiers |= MOD_MASK_ALT;
2700 /* try again, to find the un-alted key in the special key table */
2701 table_idx = find_special_key_in_table(c);
2702 }
2703 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2704 {
2705#ifdef EBCDIC
2706 c = CtrlChar(c);
2707#else
2708 c += '@';
2709#endif
2710 modifiers |= MOD_MASK_CTRL;
2711 }
2712 }
2713
2714 /* translate the modifier into a string */
2715 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2716 if ((modifiers & mod_mask_table[i].mod_mask)
2717 == mod_mask_table[i].mod_flag)
2718 {
2719 string[idx++] = mod_mask_table[i].name;
2720 string[idx++] = (char_u)'-';
2721 }
2722
2723 if (table_idx < 0) /* unknown special key, may output t_xx */
2724 {
2725 if (IS_SPECIAL(c))
2726 {
2727 string[idx++] = 't';
2728 string[idx++] = '_';
2729 string[idx++] = KEY2TERMCAP0(c);
2730 string[idx++] = KEY2TERMCAP1(c);
2731 }
2732 /* Not a special key, only modifiers, output directly */
2733 else
2734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (has_mbyte && (*mb_char2len)(c) > 1)
2736 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002737 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 string[idx++] = c;
2739 else
2740 {
2741 s = transchar(c);
2742 while (*s)
2743 string[idx++] = *s++;
2744 }
2745 }
2746 }
2747 else /* use name of special key */
2748 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002749 size_t len = STRLEN(key_names_table[table_idx].name);
2750
2751 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2752 {
2753 STRCPY(string + idx, key_names_table[table_idx].name);
2754 idx += (int)len;
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 string[idx++] = '>';
2758 string[idx] = NUL;
2759 return string;
2760}
2761
2762/*
2763 * Try translating a <> name at (*srcp)[] to dst[].
2764 * Return the number of characters added to dst[], zero for no match.
2765 * If there is a match, srcp is advanced to after the <> name.
2766 * dst[] must be big enough to hold the result (up to six characters)!
2767 */
2768 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002769trans_special(
2770 char_u **srcp,
2771 char_u *dst,
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002772 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2773 int in_string) /* TRUE when inside a double quoted string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 int modifiers = 0;
2776 int key;
2777 int dlen = 0;
2778
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002779 key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (key == 0)
2781 return 0;
2782
2783 /* Put the appropriate modifier in a string */
2784 if (modifiers != 0)
2785 {
2786 dst[dlen++] = K_SPECIAL;
2787 dst[dlen++] = KS_MODIFIER;
2788 dst[dlen++] = modifiers;
2789 }
2790
2791 if (IS_SPECIAL(key))
2792 {
2793 dst[dlen++] = K_SPECIAL;
2794 dst[dlen++] = KEY2TERMCAP0(key);
2795 dst[dlen++] = KEY2TERMCAP1(key);
2796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 else if (has_mbyte && !keycode)
2798 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 else if (keycode)
2800 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2801 else
2802 dst[dlen++] = key;
2803
2804 return dlen;
2805}
2806
2807/*
2808 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2809 * srcp is advanced to after the <> name.
2810 * returns 0 if there is no match.
2811 */
2812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002813find_special_key(
2814 char_u **srcp,
2815 int *modp,
2816 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002817 int keep_x_key, /* don't translate xHome to Home key */
2818 int in_string) /* TRUE in string, double quote is escaped */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 char_u *last_dash;
2821 char_u *end_of_name;
2822 char_u *src;
2823 char_u *bp;
2824 int modifiers;
2825 int bit;
2826 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002827 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002828 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 src = *srcp;
2831 if (src[0] != '<')
2832 return 0;
2833
2834 /* Find end of modifier list */
2835 last_dash = src;
2836 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2837 {
2838 if (*bp == '-')
2839 {
2840 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002841 if (bp[1] != NUL)
2842 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002843 if (has_mbyte)
2844 l = mb_ptr2len(bp + 1);
2845 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002846 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002847 /* Anything accepted, like <C-?>.
2848 * <C-"> or <M-"> are not special in strings as " is
2849 * the string delimiter. With a backslash it works: <M-\"> */
2850 if (!(in_string && bp[1] == '"') && bp[2] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002851 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002852 else if (in_string && bp[1] == '\\' && bp[2] == '"'
2853 && bp[3] == '>')
2854 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2858 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002859 else if (STRNICMP(bp, "char-", 5) == 0)
2860 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002861 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002862 bp += l + 5;
2863 break;
2864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866
2867 if (*bp == '>') /* found matching '>' */
2868 {
2869 end_of_name = bp + 1;
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 /* Which modifiers are given? */
2872 modifiers = 0x0;
2873 for (bp = src + 1; bp < last_dash; bp++)
2874 {
2875 if (*bp != '-')
2876 {
2877 bit = name_to_mod_mask(*bp);
2878 if (bit == 0x0)
2879 break; /* Illegal modifier name */
2880 modifiers |= bit;
2881 }
2882 }
2883
2884 /*
2885 * Legal modifier name.
2886 */
2887 if (bp >= last_dash)
2888 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002889 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2890 && VIM_ISDIGIT(last_dash[6]))
2891 {
2892 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002893 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002894 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002897 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002898 int off = 1;
2899
2900 /* Modifier with single letter, or special key name. */
2901 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2902 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002903 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002904 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002905 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02002906 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002907 if (modifiers != 0 && last_dash[l + off] == '>')
2908 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002909 else
2910 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002911 key = get_special_key_code(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002912 if (!keep_x_key)
2913 key = handle_x_keys(key);
2914 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
2917 /*
2918 * get_special_key_code() may return NUL for invalid
2919 * special key name.
2920 */
2921 if (key != NUL)
2922 {
2923 /*
2924 * Only use a modifier when there is no special key code that
2925 * includes the modifier.
2926 */
2927 key = simplify_key(key, &modifiers);
2928
2929 if (!keycode)
2930 {
2931 /* don't want keycode, use single byte code */
2932 if (key == K_BS)
2933 key = BS;
2934 else if (key == K_DEL || key == K_KDEL)
2935 key = DEL;
2936 }
2937
2938 /*
2939 * Normal Key with modifier: Try to make a single byte code.
2940 */
2941 if (!IS_SPECIAL(key))
2942 key = extract_modifiers(key, &modifiers);
2943
2944 *modp = modifiers;
2945 *srcp = end_of_name;
2946 return key;
2947 }
2948 }
2949 }
2950 return 0;
2951}
2952
2953/*
2954 * Try to include modifiers in the key.
2955 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2956 */
2957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002958extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 int modifiers = *modp;
2961
Bram Moolenaard0573012017-10-28 21:11:06 +02002962#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002963 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (!(modifiers & MOD_MASK_CMD))
2965#endif
2966 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2967 {
2968 key = TOUPPER_ASC(key);
2969 modifiers &= ~MOD_MASK_SHIFT;
2970 }
2971 if ((modifiers & MOD_MASK_CTRL)
2972#ifdef EBCDIC
2973 /* * TODO: EBCDIC Better use:
2974 * && (Ctrl_chr(key) || key == '?')
2975 * ??? */
2976 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2977 != NULL
2978#else
2979 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2980#endif
2981 )
2982 {
2983 key = Ctrl_chr(key);
2984 modifiers &= ~MOD_MASK_CTRL;
2985 /* <C-@> is <Nul> */
2986 if (key == 0)
2987 key = K_ZERO;
2988 }
Bram Moolenaard0573012017-10-28 21:11:06 +02002989#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002990 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (!(modifiers & MOD_MASK_CMD))
2992#endif
2993 if ((modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002994 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
2996 key |= 0x80;
2997 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2998 }
2999
3000 *modp = modifiers;
3001 return key;
3002}
3003
3004/*
3005 * Try to find key "c" in the special key table.
3006 * Return the index when found, -1 when not found.
3007 */
3008 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003009find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 int i;
3012
3013 for (i = 0; key_names_table[i].name != NULL; i++)
3014 if (c == key_names_table[i].key)
3015 break;
3016 if (key_names_table[i].name == NULL)
3017 i = -1;
3018 return i;
3019}
3020
3021/*
3022 * Find the special key with the given name (the given string does not have to
3023 * end with NUL, the name is assumed to end before the first non-idchar).
3024 * If the name starts with "t_" the next two characters are interpreted as a
3025 * termcap name.
3026 * Return the key code, or 0 if not found.
3027 */
3028 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003029get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 char_u *table_name;
3032 char_u string[3];
3033 int i, j;
3034
3035 /*
3036 * If it's <t_xx> we get the code for xx from the termcap
3037 */
3038 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3039 {
3040 string[0] = name[2];
3041 string[1] = name[3];
3042 string[2] = NUL;
3043 if (add_termcap_entry(string, FALSE) == OK)
3044 return TERMCAP2KEY(name[2], name[3]);
3045 }
3046 else
3047 for (i = 0; key_names_table[i].name != NULL; i++)
3048 {
3049 table_name = key_names_table[i].name;
3050 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3051 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3052 break;
3053 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3054 return key_names_table[i].key;
3055 }
3056 return 0;
3057}
3058
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003059#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003061get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003063 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 return NULL;
3065 return key_names_table[i].name;
3066}
3067#endif
3068
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003069#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070/*
3071 * Look up the given mouse code to return the relevant information in the other
3072 * arguments. Return which button is down or was released.
3073 */
3074 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003075get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 int i;
3078
3079 for (i = 0; mouse_table[i].pseudo_code; i++)
3080 if (code == mouse_table[i].pseudo_code)
3081 {
3082 *is_click = mouse_table[i].is_click;
3083 *is_drag = mouse_table[i].is_drag;
3084 return mouse_table[i].button;
3085 }
3086 return 0; /* Shouldn't get here */
3087}
3088
3089/*
3090 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3091 * the given information about which mouse button is down, and whether the
3092 * mouse was clicked, dragged or released.
3093 */
3094 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003095get_pseudo_mouse_code(
3096 int button, /* eg MOUSE_LEFT */
3097 int is_click,
3098 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 int i;
3101
3102 for (i = 0; mouse_table[i].pseudo_code; i++)
3103 if (button == mouse_table[i].button
3104 && is_click == mouse_table[i].is_click
3105 && is_drag == mouse_table[i].is_drag)
3106 {
3107#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003108 /* Trick: a non mappable left click and release has mouse_col -1
3109 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3110 * gui_mouse_moved() */
3111 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003113 if (mouse_col < 0)
3114 mouse_col = 0;
3115 else
3116 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3118 return (int)KE_LEFTMOUSE_NM;
3119 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3120 return (int)KE_LEFTRELEASE_NM;
3121 }
3122#endif
3123 return mouse_table[i].pseudo_code;
3124 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003125 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126}
3127#endif /* FEAT_MOUSE */
3128
3129/*
3130 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3131 */
3132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003133get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 int c = *buf->b_p_ff;
3136
3137 if (buf->b_p_bin || c == 'u')
3138 return EOL_UNIX;
3139 if (c == 'm')
3140 return EOL_MAC;
3141 return EOL_DOS;
3142}
3143
3144/*
3145 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3146 * argument.
3147 */
3148 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003149get_fileformat_force(
3150 buf_T *buf,
3151 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 int c;
3154
3155 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003156 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 else
3158 {
3159 if ((eap != NULL && eap->force_bin != 0)
3160 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3161 return EOL_UNIX;
3162 c = *buf->b_p_ff;
3163 }
3164 if (c == 'u')
3165 return EOL_UNIX;
3166 if (c == 'm')
3167 return EOL_MAC;
3168 return EOL_DOS;
3169}
3170
3171/*
3172 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3173 * Sets both 'textmode' and 'fileformat'.
3174 * Note: Does _not_ set global value of 'textmode'!
3175 */
3176 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003177set_fileformat(
3178 int t,
3179 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 char *p = NULL;
3182
3183 switch (t)
3184 {
3185 case EOL_DOS:
3186 p = FF_DOS;
3187 curbuf->b_p_tx = TRUE;
3188 break;
3189 case EOL_UNIX:
3190 p = FF_UNIX;
3191 curbuf->b_p_tx = FALSE;
3192 break;
3193 case EOL_MAC:
3194 p = FF_MAC;
3195 curbuf->b_p_tx = FALSE;
3196 break;
3197 }
3198 if (p != NULL)
3199 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003200 OPT_FREE | opt_flags, 0);
3201
Bram Moolenaarf740b292006-02-16 22:11:02 +00003202 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003204 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#ifdef FEAT_TITLE
3206 need_maketitle = TRUE; /* set window title later */
3207#endif
3208}
3209
3210/*
3211 * Return the default fileformat from 'fileformats'.
3212 */
3213 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003214default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 switch (*p_ffs)
3217 {
3218 case 'm': return EOL_MAC;
3219 case 'd': return EOL_DOS;
3220 }
3221 return EOL_UNIX;
3222}
3223
3224/*
3225 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3226 */
3227 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003228call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 char_u *ncmd;
3231 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003232#ifdef FEAT_PROFILE
3233 proftime_T wait_time;
3234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 if (p_verbose > 3)
3237 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003238 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003239 smsg(_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 cmd == NULL ? p_sh : cmd);
3241 out_char('\n');
3242 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003243 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245
Bram Moolenaar05159a02005-02-26 23:04:13 +00003246#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003247 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003248 prof_child_enter(&wait_time);
3249#endif
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (*p_sh == NUL)
3252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003253 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 retval = -1;
3255 }
3256 else
3257 {
3258#ifdef FEAT_GUI_MSWIN
3259 /* Don't hide the pointer while executing a shell command. */
3260 gui_mch_mousehide(FALSE);
3261#endif
3262#ifdef FEAT_GUI
3263 ++hold_gui_events;
3264#endif
3265 /* The external command may update a tags file, clear cached tags. */
3266 tag_freematch();
3267
3268 if (cmd == NULL || *p_sxq == NUL)
3269 retval = mch_call_shell(cmd, opt);
3270 else
3271 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003272 char_u *ecmd = cmd;
3273
3274 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3275 {
3276 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3277 if (ecmd == NULL)
3278 ecmd = cmd;
3279 }
3280 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (ncmd != NULL)
3282 {
3283 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003284 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003285 /* When 'shellxquote' is ( append ).
3286 * When 'shellxquote' is "( append )". */
3287 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3288 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3289 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 retval = mch_call_shell(ncmd, opt);
3291 vim_free(ncmd);
3292 }
3293 else
3294 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003295 if (ecmd != cmd)
3296 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
3298#ifdef FEAT_GUI
3299 --hold_gui_events;
3300#endif
3301 /*
3302 * Check the window size, in case it changed while executing the
3303 * external command.
3304 */
3305 shell_resized_check();
3306 }
3307
3308#ifdef FEAT_EVAL
3309 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003310# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003311 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003312 prof_child_exit(&wait_time);
3313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#endif
3315
3316 return retval;
3317}
3318
3319/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003320 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3321 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
3323 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003324get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 if (State & NORMAL)
3327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003329 {
3330 if (VIsual_select)
3331 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003333 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003334 else if (finish_op)
3335 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 return State;
3338}
3339
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003340/*
3341 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003342 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003343 * "b" must point to the start of the file name
3344 */
3345 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003346after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003347{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003348 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003349 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3350}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003351
3352/*
3353 * Return TRUE if file names "f1" and "f2" are in the same directory.
3354 * "f1" may be a short name, "f2" must be a full path.
3355 */
3356 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003357same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003358{
3359 char_u ffname[MAXPATHL];
3360 char_u *t1;
3361 char_u *t2;
3362
3363 /* safety check */
3364 if (f1 == NULL || f2 == NULL)
3365 return FALSE;
3366
3367 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3368 t1 = gettail_sep(ffname);
3369 t2 = gettail_sep(f2);
3370 return (t1 - ffname == t2 - f2
3371 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3372}
3373
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02003374#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
3375 || defined(MSWIN) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01003376 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 || defined(PROTO)
3378/*
3379 * Change to a file's directory.
3380 * Caller must call shorten_fnames()!
3381 * Return OK or FAIL.
3382 */
3383 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003384vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003386 char_u old_dir[MAXPATHL];
3387 char_u new_dir[MAXPATHL];
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003388 int res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003390 if (mch_dirname(old_dir, MAXPATHL) != OK)
3391 *old_dir = NUL;
3392
3393 vim_strncpy(new_dir, fname, MAXPATHL - 1);
3394 *gettail_sep(new_dir) = NUL;
3395
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01003396 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003397 // nothing to do
3398 res = OK;
3399 else
3400 {
3401 res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
3402
3403 if (res == OK && trigger_autocmd != NULL)
3404 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
3405 new_dir, FALSE, curbuf);
3406 }
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003407 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408}
3409#endif
3410
3411#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3412/*
3413 * Check if "name" ends in a slash and is not a directory.
3414 * Used for systems where stat() ignores a trailing slash on a file name.
3415 * The Vim code assumes a trailing slash is only ignored for a directory.
3416 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003417 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01003418illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 if (name[0] == NUL)
3421 return FALSE; /* no file name is not illegal */
3422 if (name[strlen(name) - 1] != '/')
3423 return FALSE; /* no trailing slash */
3424 if (mch_isdir((char_u *)name))
3425 return FALSE; /* trailing slash for a directory */
3426 return TRUE;
3427}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003428
3429/*
3430 * Special implementation of mch_stat() for Solaris.
3431 */
3432 int
3433vim_stat(const char *name, stat_T *stp)
3434{
3435 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
3436 * the name ends in "/" and it's not a directory. */
Bram Moolenaard8492792017-03-16 12:22:38 +01003437 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003438}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#endif
3440
3441#if defined(CURSOR_SHAPE) || defined(PROTO)
3442
3443/*
3444 * Handling of cursor and mouse pointer shapes in various modes.
3445 */
3446
3447cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3448{
3449 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3450 * defaults when Vim starts.
3451 * Adjust the SHAPE_IDX_ defines when making changes! */
3452 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3453 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3454 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3455 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3456 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3457 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3458 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3459 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3460 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3461 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3462 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3463 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3464 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3465 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3466 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3467 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3468 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3469};
3470
3471#ifdef FEAT_MOUSESHAPE
3472/*
3473 * Table with names for mouse shapes. Keep in sync with all the tables for
3474 * mch_set_mouse_shape()!.
3475 */
3476static char * mshape_names[] =
3477{
3478 "arrow", /* default, must be the first one */
3479 "blank", /* hidden */
3480 "beam",
3481 "updown",
3482 "udsizing",
3483 "leftright",
3484 "lrsizing",
3485 "busy",
3486 "no",
3487 "crosshair",
3488 "hand1",
3489 "hand2",
3490 "pencil",
3491 "question",
3492 "rightup-arrow",
3493 "up-arrow",
3494 NULL
3495};
3496#endif
3497
3498/*
3499 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3500 * ("what" is SHAPE_MOUSE).
3501 * Returns error message for an illegal option, NULL otherwise.
3502 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003503 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003504parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506 char_u *modep;
3507 char_u *colonp;
3508 char_u *commap;
3509 char_u *slashp;
3510 char_u *p, *endp;
3511 int idx = 0; /* init for GCC */
3512 int all_idx;
3513 int len;
3514 int i;
3515 long n;
3516 int found_ve = FALSE; /* found "ve" flag */
3517 int round;
3518
3519 /*
3520 * First round: check for errors; second round: do it for real.
3521 */
3522 for (round = 1; round <= 2; ++round)
3523 {
3524 /*
3525 * Repeat for all comma separated parts.
3526 */
3527#ifdef FEAT_MOUSESHAPE
3528 if (what == SHAPE_MOUSE)
3529 modep = p_mouseshape;
3530 else
3531#endif
3532 modep = p_guicursor;
3533 while (*modep != NUL)
3534 {
3535 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01003536 commap = vim_strchr(modep, ',');
3537
3538 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003539 return N_("E545: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (colonp == modep)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003541 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
3543 /*
3544 * Repeat for all mode's before the colon.
3545 * For the 'a' mode, we loop to handle all the modes.
3546 */
3547 all_idx = -1;
3548 while (modep < colonp || all_idx >= 0)
3549 {
3550 if (all_idx < 0)
3551 {
3552 /* Find the mode. */
3553 if (modep[1] == '-' || modep[1] == ':')
3554 len = 1;
3555 else
3556 len = 2;
3557 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3558 all_idx = SHAPE_IDX_COUNT - 1;
3559 else
3560 {
3561 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3562 if (STRNICMP(modep, shape_table[idx].name, len)
3563 == 0)
3564 break;
3565 if (idx == SHAPE_IDX_COUNT
3566 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003567 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3569 found_ve = TRUE;
3570 }
3571 modep += len + 1;
3572 }
3573
3574 if (all_idx >= 0)
3575 idx = all_idx--;
3576 else if (round == 2)
3577 {
3578#ifdef FEAT_MOUSESHAPE
3579 if (what == SHAPE_MOUSE)
3580 {
3581 /* Set the default, for the missing parts */
3582 shape_table[idx].mshape = 0;
3583 }
3584 else
3585#endif
3586 {
3587 /* Set the defaults, for the missing parts */
3588 shape_table[idx].shape = SHAPE_BLOCK;
3589 shape_table[idx].blinkwait = 700L;
3590 shape_table[idx].blinkon = 400L;
3591 shape_table[idx].blinkoff = 250L;
3592 }
3593 }
3594
3595 /* Parse the part after the colon */
3596 for (p = colonp + 1; *p && *p != ','; )
3597 {
3598#ifdef FEAT_MOUSESHAPE
3599 if (what == SHAPE_MOUSE)
3600 {
3601 for (i = 0; ; ++i)
3602 {
3603 if (mshape_names[i] == NULL)
3604 {
3605 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003606 return N_("E547: Illegal mouseshape");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (round == 2)
3608 shape_table[idx].mshape =
3609 getdigits(&p) + MSHAPE_NUMBERED;
3610 else
3611 (void)getdigits(&p);
3612 break;
3613 }
3614 len = (int)STRLEN(mshape_names[i]);
3615 if (STRNICMP(p, mshape_names[i], len) == 0)
3616 {
3617 if (round == 2)
3618 shape_table[idx].mshape = i;
3619 p += len;
3620 break;
3621 }
3622 }
3623 }
3624 else /* if (what == SHAPE_MOUSE) */
3625#endif
3626 {
3627 /*
3628 * First handle the ones with a number argument.
3629 */
3630 i = *p;
3631 len = 0;
3632 if (STRNICMP(p, "ver", 3) == 0)
3633 len = 3;
3634 else if (STRNICMP(p, "hor", 3) == 0)
3635 len = 3;
3636 else if (STRNICMP(p, "blinkwait", 9) == 0)
3637 len = 9;
3638 else if (STRNICMP(p, "blinkon", 7) == 0)
3639 len = 7;
3640 else if (STRNICMP(p, "blinkoff", 8) == 0)
3641 len = 8;
3642 if (len != 0)
3643 {
3644 p += len;
3645 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003646 return N_("E548: digit expected");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 n = getdigits(&p);
3648 if (len == 3) /* "ver" or "hor" */
3649 {
3650 if (n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003651 return N_("E549: Illegal percentage");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (round == 2)
3653 {
3654 if (TOLOWER_ASC(i) == 'v')
3655 shape_table[idx].shape = SHAPE_VER;
3656 else
3657 shape_table[idx].shape = SHAPE_HOR;
3658 shape_table[idx].percentage = n;
3659 }
3660 }
3661 else if (round == 2)
3662 {
3663 if (len == 9)
3664 shape_table[idx].blinkwait = n;
3665 else if (len == 7)
3666 shape_table[idx].blinkon = n;
3667 else
3668 shape_table[idx].blinkoff = n;
3669 }
3670 }
3671 else if (STRNICMP(p, "block", 5) == 0)
3672 {
3673 if (round == 2)
3674 shape_table[idx].shape = SHAPE_BLOCK;
3675 p += 5;
3676 }
3677 else /* must be a highlight group name then */
3678 {
3679 endp = vim_strchr(p, '-');
3680 if (commap == NULL) /* last part */
3681 {
3682 if (endp == NULL)
3683 endp = p + STRLEN(p); /* find end of part */
3684 }
3685 else if (endp > commap || endp == NULL)
3686 endp = commap;
3687 slashp = vim_strchr(p, '/');
3688 if (slashp != NULL && slashp < endp)
3689 {
3690 /* "group/langmap_group" */
3691 i = syn_check_group(p, (int)(slashp - p));
3692 p = slashp + 1;
3693 }
3694 if (round == 2)
3695 {
3696 shape_table[idx].id = syn_check_group(p,
3697 (int)(endp - p));
3698 shape_table[idx].id_lm = shape_table[idx].id;
3699 if (slashp != NULL && slashp < endp)
3700 shape_table[idx].id = i;
3701 }
3702 p = endp;
3703 }
3704 } /* if (what != SHAPE_MOUSE) */
3705
3706 if (*p == '-')
3707 ++p;
3708 }
3709 }
3710 modep = p;
3711 if (*modep == ',')
3712 ++modep;
3713 }
3714 }
3715
3716 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3717 if (!found_ve)
3718 {
3719#ifdef FEAT_MOUSESHAPE
3720 if (what == SHAPE_MOUSE)
3721 {
3722 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3723 }
3724 else
3725#endif
3726 {
3727 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3728 shape_table[SHAPE_IDX_VE].percentage =
3729 shape_table[SHAPE_IDX_V].percentage;
3730 shape_table[SHAPE_IDX_VE].blinkwait =
3731 shape_table[SHAPE_IDX_V].blinkwait;
3732 shape_table[SHAPE_IDX_VE].blinkon =
3733 shape_table[SHAPE_IDX_V].blinkon;
3734 shape_table[SHAPE_IDX_VE].blinkoff =
3735 shape_table[SHAPE_IDX_V].blinkoff;
3736 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3737 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3738 }
3739 }
3740
3741 return NULL;
3742}
3743
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003744# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3745 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746/*
3747 * Return the index into shape_table[] for the current mode.
3748 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3749 */
3750 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003751get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
3753#ifdef FEAT_MOUSESHAPE
3754 if (mouse && (State == HITRETURN || State == ASKMORE))
3755 {
3756# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003757 int x, y;
3758 gui_mch_getmouse(&x, &y);
3759 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 return SHAPE_IDX_MOREL;
3761# endif
3762 return SHAPE_IDX_MORE;
3763 }
3764 if (mouse && drag_status_line)
3765 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (mouse && drag_sep_line)
3767 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768#endif
3769 if (!mouse && State == SHOWMATCH)
3770 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (State & VREPLACE_FLAG)
3772 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (State & REPLACE_FLAG)
3774 return SHAPE_IDX_R;
3775 if (State & INSERT)
3776 return SHAPE_IDX_I;
3777 if (State & CMDLINE)
3778 {
3779 if (cmdline_at_end())
3780 return SHAPE_IDX_C;
3781 if (cmdline_overstrike())
3782 return SHAPE_IDX_CR;
3783 return SHAPE_IDX_CI;
3784 }
3785 if (finish_op)
3786 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (VIsual_active)
3788 {
3789 if (*p_sel == 'e')
3790 return SHAPE_IDX_VE;
3791 else
3792 return SHAPE_IDX_V;
3793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return SHAPE_IDX_N;
3795}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3799static int old_mouse_shape = 0;
3800
3801/*
3802 * Set the mouse shape:
3803 * If "shape" is -1, use shape depending on the current mode,
3804 * depending on the current state.
3805 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3806 * when the mouse moves off the status or command line).
3807 */
3808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003809update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
3811 int new_mouse_shape;
3812
3813 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003814 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return;
3816
3817 /* Postpone the updating when more is to come. Speeds up executing of
3818 * mappings. */
3819 if (shape_idx == -1 && char_avail())
3820 {
3821 postponed_mouseshape = TRUE;
3822 return;
3823 }
3824
Bram Moolenaar14716812006-05-04 21:54:08 +00003825 /* When ignoring the mouse don't change shape on the statusline. */
3826 if (*p_mouse == NUL
3827 && (shape_idx == SHAPE_IDX_CLINE
3828 || shape_idx == SHAPE_IDX_STATUS
3829 || shape_idx == SHAPE_IDX_VSEP))
3830 shape_idx = -2;
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (shape_idx == -2
3833 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3834 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3835 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3836 return;
3837 if (shape_idx < 0)
3838 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3839 else
3840 new_mouse_shape = shape_table[shape_idx].mshape;
3841 if (new_mouse_shape != old_mouse_shape)
3842 {
3843 mch_set_mouse_shape(new_mouse_shape);
3844 old_mouse_shape = new_mouse_shape;
3845 }
3846 postponed_mouseshape = FALSE;
3847}
3848# endif
3849
3850#endif /* CURSOR_SHAPE */
3851
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853/* TODO: make some #ifdef for this */
3854/*--------[ file searching ]-------------------------------------------------*/
3855/*
3856 * File searching functions for 'path', 'tags' and 'cdpath' options.
3857 * External visible functions:
3858 * vim_findfile_init() creates/initialises the search context
3859 * vim_findfile_free_visited() free list of visited files/dirs of search
3860 * context
3861 * vim_findfile() find a file in the search context
3862 * vim_findfile_cleanup() cleanup/free search context created by
3863 * vim_findfile_init()
3864 *
3865 * All static functions and variables start with 'ff_'
3866 *
3867 * In general it works like this:
3868 * First you create yourself a search context by calling vim_findfile_init().
3869 * It is possible to give a search context from a previous call to
3870 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3871 * until you are satisfied with the result or it returns NULL. On every call it
3872 * returns the next file which matches the conditions given to
3873 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3874 *
3875 * It is possible to call vim_findfile_init() again to reinitialise your search
3876 * with some new parameters. Don't forget to pass your old search context to
3877 * it, so it can reuse it and especially reuse the list of already visited
3878 * directories. If you want to delete the list of already visited directories
3879 * simply call vim_findfile_free_visited().
3880 *
3881 * When you are done call vim_findfile_cleanup() to free the search context.
3882 *
3883 * The function vim_findfile_init() has a long comment, which describes the
3884 * needed parameters.
3885 *
3886 *
3887 *
3888 * ATTENTION:
3889 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003890 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 * thread-safe!!!!!
3892 *
3893 * To minimize parameter passing (or because I'm to lazy), only the
3894 * external visible functions get a search context as a parameter. This is
3895 * then assigned to a static global, which is used throughout the local
3896 * functions.
3897 */
3898
3899/*
3900 * type for the directory search stack
3901 */
3902typedef struct ff_stack
3903{
3904 struct ff_stack *ffs_prev;
3905
3906 /* the fix part (no wildcards) and the part containing the wildcards
3907 * of the search path
3908 */
3909 char_u *ffs_fix_path;
3910#ifdef FEAT_PATH_EXTRA
3911 char_u *ffs_wc_path;
3912#endif
3913
3914 /* files/dirs found in the above directory, matched by the first wildcard
3915 * of wc_part
3916 */
3917 char_u **ffs_filearray;
3918 int ffs_filearray_size;
3919 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3920
3921 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003922 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 int ffs_stage;
3926
3927 /* How deep are we in the directory tree?
3928 * Counts backward from value of level parameter to vim_findfile_init
3929 */
3930 int ffs_level;
3931
3932 /* Did we already expand '**' to an empty string? */
3933 int ffs_star_star_empty;
3934} ff_stack_T;
3935
3936/*
3937 * type for already visited directories or files.
3938 */
3939typedef struct ff_visited
3940{
3941 struct ff_visited *ffv_next;
3942
3943#ifdef FEAT_PATH_EXTRA
3944 /* Visited directories are different if the wildcard string are
3945 * different. So we have to save it.
3946 */
3947 char_u *ffv_wc_path;
3948#endif
3949 /* for unix use inode etc for comparison (needed because of links), else
3950 * use filename.
3951 */
3952#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003953 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3954 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 ino_t ffv_ino; /* inode number */
3956#endif
3957 /* The memory for this struct is allocated according to the length of
3958 * ffv_fname.
3959 */
3960 char_u ffv_fname[1]; /* actually longer */
3961} ff_visited_T;
3962
3963/*
3964 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003965 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3967 * So we have to do 3 searches:
3968 * 1) search from the current files directory downward for the file "tags"
3969 * 2) search from the current files directory downward for the file "TAGS"
3970 * 3) search from Vims current directory downwards for the file "tags"
3971 * As you can see, the first and the third search are for the same file, so for
3972 * the third search we can use the visited list of the first search. For the
3973 * second search we must start from a empty visited list.
3974 * The struct ff_visited_list_hdr is used to manage a linked list of already
3975 * visited lists.
3976 */
3977typedef struct ff_visited_list_hdr
3978{
3979 struct ff_visited_list_hdr *ffvl_next;
3980
3981 /* the filename the attached visited list is for */
3982 char_u *ffvl_filename;
3983
3984 ff_visited_T *ffvl_visited_list;
3985
3986} ff_visited_list_hdr_T;
3987
3988
3989/*
3990 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003991 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 */
3993#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995/*
3996 * The search context:
3997 * ffsc_stack_ptr: the stack for the dirs to search
3998 * ffsc_visited_list: the currently active visited list
3999 * ffsc_dir_visited_list: the currently active visited list for search dirs
4000 * ffsc_visited_lists_list: the list of all visited lists
4001 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
4002 * ffsc_file_to_search: the file to search for
4003 * ffsc_start_dir: the starting directory, if search path was relative
4004 * ffsc_fix_path: the fix part of the given path (without wildcards)
4005 * Needed for upward search.
4006 * ffsc_wc_path: the part of the given path containing wildcards
4007 * ffsc_level: how many levels of dirs to search downwards
4008 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004009 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02004010 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 */
4012typedef struct ff_search_ctx_T
4013{
4014 ff_stack_T *ffsc_stack_ptr;
4015 ff_visited_list_hdr_T *ffsc_visited_list;
4016 ff_visited_list_hdr_T *ffsc_dir_visited_list;
4017 ff_visited_list_hdr_T *ffsc_visited_lists_list;
4018 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
4019 char_u *ffsc_file_to_search;
4020 char_u *ffsc_start_dir;
4021 char_u *ffsc_fix_path;
4022#ifdef FEAT_PATH_EXTRA
4023 char_u *ffsc_wc_path;
4024 int ffsc_level;
4025 char_u **ffsc_stopdirs_v;
4026#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004027 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004028 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004029} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031/* locally needed functions */
4032#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004033static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004035static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004037static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
4038static void ff_free_visited_list(ff_visited_T *vl);
4039static 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 +00004040
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004041static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
4042static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
4043static void ff_clear(ff_search_ctx_T *search_ctx);
4044static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004046static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004048static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#endif
4050#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004051static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052#endif
4053
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004054static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#if 0
4057/*
4058 * if someone likes findfirst/findnext, here are the functions
4059 * NOT TESTED!!
4060 */
4061
4062static void *ff_fn_search_context = NULL;
4063
4064 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004065vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066{
4067 ff_fn_search_context =
4068 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4069 ff_fn_search_context, rel_fname);
4070 if (NULL == ff_fn_search_context)
4071 return NULL;
4072 else
4073 return vim_findnext()
4074}
4075
4076 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004077vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
4079 char_u *ret = vim_findfile(ff_fn_search_context);
4080
4081 if (NULL == ret)
4082 {
4083 vim_findfile_cleanup(ff_fn_search_context);
4084 ff_fn_search_context = NULL;
4085 }
4086 return ret;
4087}
4088#endif
4089
4090/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004091 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004093 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 *
4095 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4096 * with the search context.
4097 *
4098 * Find the file 'filename' in the directory 'path'.
4099 * The parameter 'path' may contain wildcards. If so only search 'level'
4100 * directories deep. The parameter 'level' is the absolute maximum and is
4101 * not related to restricts given to the '**' wildcard. If 'level' is 100
4102 * and you use '**200' vim_findfile() will stop after 100 levels.
4103 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004104 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4105 * escape special characters.
4106 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4108 * restarted on the next higher directory level. This is repeated until the
4109 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4110 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4111 *
4112 * If the 'path' is relative, the starting dir for the search is either VIM's
4113 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004114 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * the first wildcard.
4116 *
4117 * Upward search is only done on the starting dir.
4118 *
4119 * If 'free_visited' is TRUE the list of already visited files/directories is
4120 * cleared. Set this to FALSE if you just want to search from another
4121 * directory, but want to be sure that no directory from a previous search is
4122 * searched again. This is useful if you search for a file at different places.
4123 * The list of visited files/dirs can also be cleared with the function
4124 * vim_findfile_free_visited().
4125 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004126 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4127 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 *
4129 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004130 * passed in the parameter "search_ctx_arg". This context is reused and
4131 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004133 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4134 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004136 * If you don't have a search context from a previous call "search_ctx_arg"
4137 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 *
4139 * This function silently ignores a few errors, vim_findfile() will have
4140 * limited functionality then.
4141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004143vim_findfile_init(
4144 char_u *path,
4145 char_u *filename,
4146 char_u *stopdirs UNUSED,
4147 int level,
4148 int free_visited,
4149 int find_what,
4150 void *search_ctx_arg,
4151 int tagfile, /* expanding names of tags files */
4152 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
4154#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004155 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004157 ff_stack_T *sptr;
4158 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 /* If a search context is given by the caller, reuse it, else allocate a
4161 * new one.
4162 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004163 if (search_ctx_arg != NULL)
4164 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 else
4166 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004167 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4168 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004170 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004172 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004173 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174
4175 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004176 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 /* clear visited list if wanted */
4179 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004180 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else
4182 {
4183 /* Reuse old visited lists. Get the visited list for the given
4184 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004185 * one. */
4186 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4187 &search_ctx->ffsc_visited_lists_list);
4188 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004190 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4191 &search_ctx->ffsc_dir_visited_lists_list);
4192 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 goto error_return;
4194 }
4195
4196 if (ff_expand_buffer == NULL)
4197 {
4198 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4199 if (ff_expand_buffer == NULL)
4200 goto error_return;
4201 }
4202
4203 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004204 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (path[0] == '.'
4206 && (vim_ispathsep(path[1]) || path[1] == NUL)
4207 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4208 && rel_fname != NULL)
4209 {
4210 int len = (int)(gettail(rel_fname) - rel_fname);
4211
4212 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4213 {
4214 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004215 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004216 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004219 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4220 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 goto error_return;
4222 if (*++path != NUL)
4223 ++path;
4224 }
4225 else if (*path == NUL || !vim_isAbsName(path))
4226 {
4227#ifdef BACKSLASH_IN_FILENAME
4228 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4229 if (*path != NUL && path[1] == ':')
4230 {
4231 char_u drive[3];
4232
4233 drive[0] = path[0];
4234 drive[1] = ':';
4235 drive[2] = NUL;
4236 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4237 goto error_return;
4238 path += 2;
4239 }
4240 else
4241#endif
4242 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4243 goto error_return;
4244
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004245 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4246 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 goto error_return;
4248
4249#ifdef BACKSLASH_IN_FILENAME
4250 /* A path that starts with "/dir" is relative to the drive, not to the
4251 * directory (but not for "//machine/dir"). Only use the drive name. */
4252 if ((*path == '/' || *path == '\\')
4253 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004254 && search_ctx->ffsc_start_dir[1] == ':')
4255 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif
4257 }
4258
4259#ifdef FEAT_PATH_EXTRA
4260 /*
4261 * If stopdirs are given, split them into an array of pointers.
4262 * If this fails (mem allocation), there is no upward search at all or a
4263 * stop directory is not recognized -> continue silently.
4264 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004265 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * is handled as unlimited upward search. See function
4267 * ff_path_in_stoplist() for details.
4268 */
4269 if (stopdirs != NULL)
4270 {
4271 char_u *walker = stopdirs;
4272 int dircount;
4273
4274 while (*walker == ';')
4275 walker++;
4276
4277 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004278 search_ctx->ffsc_stopdirs_v =
4279 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004281 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
4283 do
4284 {
4285 char_u *helper;
4286 void *ptr;
4287
4288 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004289 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 (dircount + 1) * sizeof(char_u *));
4291 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004292 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 else
4294 /* ignore, keep what we have and continue */
4295 break;
4296 walker = vim_strchr(walker, ';');
4297 if (walker)
4298 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004299 search_ctx->ffsc_stopdirs_v[dircount-1] =
4300 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 walker++;
4302 }
4303 else
4304 /* this might be "", which means ascent till top
4305 * of directory tree.
4306 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004307 search_ctx->ffsc_stopdirs_v[dircount-1] =
4308 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 dircount++;
4311
4312 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004313 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 }
4316#endif
4317
4318#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004319 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
4321 /* split into:
4322 * -fix path
4323 * -wildcard_stuff (might be NULL)
4324 */
4325 wc_part = vim_strchr(path, '*');
4326 if (wc_part != NULL)
4327 {
4328 int llevel;
4329 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004330 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004333 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
4336 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004337 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4339 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4340 * For EBCDIC you get different character values.
4341 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004342 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 * string.
4344 */
4345 len = 0;
4346 while (*wc_part != NUL)
4347 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004348 if (len + 5 >= MAXPATHL)
4349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004350 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004351 break;
4352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (STRNCMP(wc_part, "**", 2) == 0)
4354 {
4355 ff_expand_buffer[len++] = *wc_part++;
4356 ff_expand_buffer[len++] = *wc_part++;
4357
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004358 llevel = strtol((char *)wc_part, &errpt, 10);
4359 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004361 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 /* restrict is 0 -> remove already added '**' */
4363 len -= 2;
4364 else
4365 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004366 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004367 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004369 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 +00004370 goto error_return;
4371 }
4372 }
4373 else
4374 ff_expand_buffer[len++] = *wc_part++;
4375 }
4376 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004377 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004379 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 goto error_return;
4381 }
4382 else
4383#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004384 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004386 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
4388 /* store the fix part as startdir.
4389 * This is needed if the parameter path is fully qualified.
4390 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004391 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004392 if (search_ctx->ffsc_start_dir == NULL)
4393 goto error_return;
4394 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396
4397 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004398 if (STRLEN(search_ctx->ffsc_start_dir)
4399 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004401 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004402 goto error_return;
4403 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004404 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004406 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004407 int eb_len = (int)STRLEN(ff_expand_buffer);
4408 char_u *buf = alloc(eb_len
4409 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004410
4411 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004412 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004413 if (mch_isdir(buf))
4414 {
4415 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4416 add_pathsep(ff_expand_buffer);
4417 }
4418#ifdef FEAT_PATH_EXTRA
4419 else
4420 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004421 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004422 char_u *wc_path = NULL;
4423 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004424 int len = 0;
4425
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004426 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004427 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004428 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004429 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4430 add_pathsep(ff_expand_buffer);
4431 }
4432 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004433 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004434
4435 if (search_ctx->ffsc_wc_path != NULL)
4436 {
4437 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004438 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004439 + STRLEN(search_ctx->ffsc_fix_path + len)
4440 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004441 if (temp == NULL || wc_path == NULL)
4442 {
4443 vim_free(buf);
4444 vim_free(temp);
4445 vim_free(wc_path);
4446 goto error_return;
4447 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004448
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004449 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4450 STRCAT(temp, search_ctx->ffsc_wc_path);
4451 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004452 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004453 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004454 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004455 }
4456#endif
4457 vim_free(buf);
4458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460 sptr = ff_create_stack_element(ff_expand_buffer,
4461#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004462 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463#endif
4464 level, 0);
4465
4466 if (sptr == NULL)
4467 goto error_return;
4468
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004469 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004471 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4472 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 goto error_return;
4474
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004475 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477error_return:
4478 /*
4479 * We clear the search context now!
4480 * Even when the caller gave us a (perhaps valid) context we free it here,
4481 * as we might have already destroyed it.
4482 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004483 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return NULL;
4485}
4486
4487#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4488/*
4489 * Get the stopdir string. Check that ';' is not escaped.
4490 */
4491 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004492vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494 char_u *r_ptr = buf;
4495
4496 while (*r_ptr != NUL && *r_ptr != ';')
4497 {
4498 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4499 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004500 /* Overwrite the escape char,
4501 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004502 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 r_ptr++;
4504 }
4505 r_ptr++;
4506 }
4507 if (*r_ptr == ';')
4508 {
4509 *r_ptr = 0;
4510 r_ptr++;
4511 }
4512 else if (*r_ptr == NUL)
4513 r_ptr = NULL;
4514 return r_ptr;
4515}
4516#endif
4517
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004518/*
4519 * Clean up the given search context. Can handle a NULL pointer.
4520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004522vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004524 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 return;
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004528 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530}
4531
4532/*
4533 * Find a file in a search context.
4534 * The search context was created with vim_findfile_init() above.
4535 * Return a pointer to an allocated file name or NULL if nothing found.
4536 * To get all matching files call this function until you get NULL.
4537 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004538 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 *
4540 * The search algorithm is depth first. To change this replace the
4541 * stack with a list (don't forget to leave partly searched directories on the
4542 * top of the list).
4543 */
4544 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004545vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 char_u *file_path;
4548#ifdef FEAT_PATH_EXTRA
4549 char_u *rest_of_wildcards;
4550 char_u *path_end = NULL;
4551#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004552 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4554 int len;
4555#endif
4556 int i;
4557 char_u *p;
4558#ifdef FEAT_SEARCHPATH
4559 char_u *suf;
4560#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004561 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004563 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 return NULL;
4565
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004566 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 /*
4569 * filepath is used as buffer for various actions and as the storage to
4570 * return a found filename.
4571 */
4572 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4573 return NULL;
4574
4575#ifdef FEAT_PATH_EXTRA
4576 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004577 if (search_ctx->ffsc_start_dir != NULL)
4578 path_end = &search_ctx->ffsc_start_dir[
4579 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#endif
4581
4582#ifdef FEAT_PATH_EXTRA
4583 /* upward search loop */
4584 for (;;)
4585 {
4586#endif
4587 /* downward search loop */
4588 for (;;)
4589 {
4590 /* check if user user wants to stop the search*/
4591 ui_breakcheck();
4592 if (got_int)
4593 break;
4594
4595 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004596 stackp = ff_pop(search_ctx);
4597 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 break;
4599
4600 /*
4601 * TODO: decide if we leave this test in
4602 *
4603 * GOOD: don't search a directory(-tree) twice.
4604 * BAD: - check linked list for every new directory entered.
4605 * - check for double files also done below
4606 *
4607 * Here we check if we already searched this directory.
4608 * We already searched a directory if:
4609 * 1) The directory is the same.
4610 * 2) We would use the same wildcard string.
4611 *
4612 * Good if you have links on same directory via several ways
4613 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4614 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4615 *
4616 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004617 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004619 if (stackp->ffs_filearray == NULL
4620 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004622 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004624 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625#endif
4626 ) == FAIL)
4627 {
4628#ifdef FF_VERBOSE
4629 if (p_verbose >= 5)
4630 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004631 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004632 smsg("Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004633 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004635 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004636 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004639 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 continue;
4641 }
4642#ifdef FF_VERBOSE
4643 else if (p_verbose >= 5)
4644 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004645 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004646 smsg("Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004647 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004649 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004650 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652#endif
4653
4654 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004655 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004657 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 continue;
4659 }
4660
4661 file_path[0] = NUL;
4662
4663 /*
4664 * If no filearray till now expand wildcards
4665 * The function expand_wildcards() can handle an array of paths
4666 * and all possible expands are returned in one array. We use this
4667 * to handle the expansion of '**' into an empty string.
4668 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004669 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
4671 char_u *dirptrs[2];
4672
4673 /* we use filepath to build the path expand_wildcards() should
4674 * expand.
4675 */
4676 dirptrs[0] = file_path;
4677 dirptrs[1] = NULL;
4678
4679 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004680 if (!vim_isAbsName(stackp->ffs_fix_path)
4681 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 {
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004683 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4684 {
4685 STRCPY(file_path, search_ctx->ffsc_start_dir);
4686 add_pathsep(file_path);
4687 }
4688 else
4689 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691
4692 /* append the fix part of the search path */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004693 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4694 {
4695 STRCAT(file_path, stackp->ffs_fix_path);
4696 add_pathsep(file_path);
4697 }
4698 else
4699 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004702 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (*rest_of_wildcards != NUL)
4704 {
4705 len = (int)STRLEN(file_path);
4706 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4707 {
4708 /* pointer to the restrict byte
4709 * The restrict byte is not a character!
4710 */
4711 p = rest_of_wildcards + 2;
4712
4713 if (*p > 0)
4714 {
4715 (*p)--;
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004716 if (len + 1 < MAXPATHL)
4717 file_path[len++] = '*';
4718 else
4719 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721
4722 if (*p == 0)
4723 {
4724 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004725 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727 else
4728 rest_of_wildcards += 3;
4729
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004730 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004733 stackp->ffs_star_star_empty = 1;
4734 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736 }
4737
4738 /*
4739 * Here we copy until the next path separator or the end of
4740 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004741 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 * pushing every directory returned from expand_wildcards()
4743 * on the stack again for further search.
4744 */
4745 while (*rest_of_wildcards
4746 && !vim_ispathsep(*rest_of_wildcards))
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004747 if (len + 1 < MAXPATHL)
4748 file_path[len++] = *rest_of_wildcards++;
4749 else
4750 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 file_path[len] = NUL;
4753 if (vim_ispathsep(*rest_of_wildcards))
4754 rest_of_wildcards++;
4755 }
4756#endif
4757
4758 /*
4759 * Expand wildcards like "*" and "$VAR".
4760 * If the path is a URL don't try this.
4761 */
4762 if (path_with_url(dirptrs[0]))
4763 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004764 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004766 if (stackp->ffs_filearray != NULL
4767 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004769 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004771 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004774 /* Add EW_NOTWILD because the expanded path may contain
4775 * wildcard characters that are to be taken literally.
4776 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004778 &stackp->ffs_filearray_size,
4779 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004780 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004782 stackp->ffs_filearray_cur = 0;
4783 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785#ifdef FEAT_PATH_EXTRA
4786 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004787 rest_of_wildcards = &stackp->ffs_wc_path[
4788 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789#endif
4790
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004791 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
4793 /* this is the first time we work on this directory */
4794#ifdef FEAT_PATH_EXTRA
4795 if (*rest_of_wildcards == NUL)
4796#endif
4797 {
4798 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004799 * We don't have further wildcards to expand, so we have to
4800 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004802 for (i = stackp->ffs_filearray_cur;
4803 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004805 if (!path_with_url(stackp->ffs_filearray[i])
4806 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 continue; /* not a directory */
4808
Bram Moolenaar21160b92009-11-11 15:56:10 +00004809 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 * below */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004811 if (STRLEN(stackp->ffs_filearray[i]) + 1
4812 + STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4813 {
4814 STRCPY(file_path, stackp->ffs_filearray[i]);
4815 add_pathsep(file_path);
4816 STRCAT(file_path, search_ctx->ffsc_file_to_search);
4817 }
4818 else
4819 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 /*
4822 * Try without extra suffix and then with suffixes
4823 * from 'suffixesadd'.
4824 */
4825#ifdef FEAT_SEARCHPATH
4826 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004827 if (search_ctx->ffsc_tagfile)
4828 suf = (char_u *)"";
4829 else
4830 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 for (;;)
4832#endif
4833 {
4834 /* if file exists and we didn't already find it */
4835 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004836 || (mch_getperm(file_path) >= 0
4837 && (search_ctx->ffsc_find_what
4838 == FINDFILE_BOTH
4839 || ((search_ctx->ffsc_find_what
4840 == FINDFILE_DIR)
4841 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842#ifndef FF_VERBOSE
4843 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004844 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 file_path
4846#ifdef FEAT_PATH_EXTRA
4847 , (char_u *)""
4848#endif
4849 ) == OK)
4850#endif
4851 )
4852 {
4853#ifdef FF_VERBOSE
4854 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004855 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 file_path
4857#ifdef FEAT_PATH_EXTRA
4858 , (char_u *)""
4859#endif
4860 ) == FAIL)
4861 {
4862 if (p_verbose >= 5)
4863 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004864 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004865 smsg("Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 file_path);
4867 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004868 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004869 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 continue;
4872 }
4873#endif
4874
4875 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004876 stackp->ffs_filearray_cur = i + 1;
4877 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004879 if (!path_with_url(file_path))
4880 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4882 == OK)
4883 {
4884 p = shorten_fname(file_path,
4885 ff_expand_buffer);
4886 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004887 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889#ifdef FF_VERBOSE
4890 if (p_verbose >= 5)
4891 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004892 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004893 smsg("HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004895 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004896 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898#endif
4899 return file_path;
4900 }
4901
4902#ifdef FEAT_SEARCHPATH
4903 /* Not found or found already, try next suffix. */
4904 if (*suf == NUL)
4905 break;
4906 copy_option_part(&suf, file_path + len,
4907 MAXPATHL - len, ",");
4908#endif
4909 }
4910 }
4911 }
4912#ifdef FEAT_PATH_EXTRA
4913 else
4914 {
4915 /*
4916 * still wildcards left, push the directories for further
4917 * search
4918 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004919 for (i = stackp->ffs_filearray_cur;
4920 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004922 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 continue; /* not a directory */
4924
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004925 ff_push(search_ctx,
4926 ff_create_stack_element(
4927 stackp->ffs_filearray[i],
4928 rest_of_wildcards,
4929 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931 }
4932#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004933 stackp->ffs_filearray_cur = 0;
4934 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936
4937#ifdef FEAT_PATH_EXTRA
4938 /*
4939 * if wildcards contains '**' we have to descent till we reach the
4940 * leaves of the directory tree.
4941 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004942 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004944 for (i = stackp->ffs_filearray_cur;
4945 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004947 if (fnamecmp(stackp->ffs_filearray[i],
4948 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004950 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004952 ff_push(search_ctx,
4953 ff_create_stack_element(stackp->ffs_filearray[i],
4954 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 }
4957#endif
4958
4959 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004960 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 }
4963
4964#ifdef FEAT_PATH_EXTRA
4965 /* If we reached this, we didn't find anything downwards.
4966 * Let's check if we should do an upward search.
4967 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004968 if (search_ctx->ffsc_start_dir
4969 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 ff_stack_T *sptr;
4972
4973 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004974 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4975 (int)(path_end - search_ctx->ffsc_start_dir),
4976 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 break;
4978
4979 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004980 while (path_end > search_ctx->ffsc_start_dir
4981 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004983 while (path_end > search_ctx->ffsc_start_dir
4984 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 path_end--;
4986 *path_end = 0;
4987 path_end--;
4988
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004989 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 break;
4991
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004992 if (STRLEN(search_ctx->ffsc_start_dir) + 1
4993 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
4994 {
4995 STRCPY(file_path, search_ctx->ffsc_start_dir);
4996 add_pathsep(file_path);
4997 STRCAT(file_path, search_ctx->ffsc_fix_path);
4998 }
4999 else
5000 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002 /* create a new stack entry */
5003 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005004 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 if (sptr == NULL)
5006 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005007 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 else
5010 break;
5011 }
5012#endif
5013
Bram Moolenaar15618fa2017-03-19 21:37:13 +01005014fail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 vim_free(file_path);
5016 return NULL;
5017}
5018
5019/*
5020 * Free the list of lists of visited files and directories
5021 * Can handle it if the passed search_context is NULL;
5022 */
5023 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005024vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005026 ff_search_ctx_T *search_ctx;
5027
5028 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 return;
5030
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005031 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
5032 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
5033 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034}
5035
5036 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005037vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 ff_visited_list_hdr_T *vp;
5040
5041 while (*list_headp != NULL)
5042 {
5043 vp = (*list_headp)->ffvl_next;
5044 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5045
5046 vim_free((*list_headp)->ffvl_filename);
5047 vim_free(*list_headp);
5048 *list_headp = vp;
5049 }
5050 *list_headp = NULL;
5051}
5052
5053 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005054ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 ff_visited_T *vp;
5057
5058 while (vl != NULL)
5059 {
5060 vp = vl->ffv_next;
5061#ifdef FEAT_PATH_EXTRA
5062 vim_free(vl->ffv_wc_path);
5063#endif
5064 vim_free(vl);
5065 vl = vp;
5066 }
5067 vl = NULL;
5068}
5069
5070/*
5071 * Returns the already visited list for the given filename. If none is found it
5072 * allocates a new one.
5073 */
5074 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01005075ff_get_visited_list(
5076 char_u *filename,
5077 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 ff_visited_list_hdr_T *retptr = NULL;
5080
5081 /* check if a visited list for the given filename exists */
5082 if (*list_headp != NULL)
5083 {
5084 retptr = *list_headp;
5085 while (retptr != NULL)
5086 {
5087 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5088 {
5089#ifdef FF_VERBOSE
5090 if (p_verbose >= 5)
5091 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005092 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005093 smsg("ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 filename);
5095 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005096 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005097 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099#endif
5100 return retptr;
5101 }
5102 retptr = retptr->ffvl_next;
5103 }
5104 }
5105
5106#ifdef FF_VERBOSE
5107 if (p_verbose >= 5)
5108 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005109 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005110 smsg("ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005112 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115#endif
5116
5117 /*
5118 * if we reach this we didn't find a list and we have to allocate new list
5119 */
5120 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5121 if (retptr == NULL)
5122 return NULL;
5123
5124 retptr->ffvl_visited_list = NULL;
5125 retptr->ffvl_filename = vim_strsave(filename);
5126 if (retptr->ffvl_filename == NULL)
5127 {
5128 vim_free(retptr);
5129 return NULL;
5130 }
5131 retptr->ffvl_next = *list_headp;
5132 *list_headp = retptr;
5133
5134 return retptr;
5135}
5136
5137#ifdef FEAT_PATH_EXTRA
5138/*
5139 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5140 * They are equal if:
5141 * - both paths are NULL
5142 * - they have the same length
5143 * - char by char comparison is OK
5144 * - the only differences are in the counters behind a '**', so
5145 * '**\20' is equal to '**\24'
5146 */
5147 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005148ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005150 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005151 int c1 = NUL;
5152 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005153 int prev1 = NUL;
5154 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 if (s1 == s2)
5157 return TRUE;
5158
5159 if (s1 == NULL || s2 == NULL)
5160 return FALSE;
5161
Bram Moolenaard43f0952015-08-27 22:30:47 +02005162 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005164 c1 = PTR2CHAR(s1 + i);
5165 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005166
5167 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5168 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005169 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005170 prev2 = prev1;
5171 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005172
Bram Moolenaar15675582018-02-09 12:29:56 +01005173 i += MB_PTR2LEN(s1 + i);
5174 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005176 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177}
5178#endif
5179
5180/*
5181 * maintains the list of already visited files and dirs
5182 * returns FAIL if the given file/dir is already in the list
5183 * returns OK if it is newly added
5184 *
5185 * TODO: What to do on memory allocation problems?
5186 * -> return TRUE - Better the file is found several times instead of
5187 * never.
5188 */
5189 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005190ff_check_visited(
5191 ff_visited_T **visited_list,
5192 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005194 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005196 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198 ff_visited_T *vp;
5199#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02005200 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 int url = FALSE;
5202#endif
5203
5204 /* For an URL we only compare the name, otherwise we compare the
5205 * device/inode (unix) or the full path name (not Unix). */
5206 if (path_with_url(fname))
5207 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005208 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209#ifdef UNIX
5210 url = TRUE;
5211#endif
5212 }
5213 else
5214 {
5215 ff_expand_buffer[0] = NUL;
5216#ifdef UNIX
5217 if (mch_stat((char *)fname, &st) < 0)
5218#else
5219 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5220#endif
5221 return FAIL;
5222 }
5223
5224 /* check against list of already visited files */
5225 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5226 {
5227 if (
5228#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005229 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5230 && vp->ffv_ino == st.st_ino)
5231 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#endif
5233 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5234 )
5235 {
5236#ifdef FEAT_PATH_EXTRA
5237 /* are the wildcard parts equal */
5238 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5239#endif
5240 /* already visited */
5241 return FAIL;
5242 }
5243 }
5244
5245 /*
5246 * New file/dir. Add it to the list of visited files/dirs.
5247 */
5248 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5249 + STRLEN(ff_expand_buffer)));
5250
5251 if (vp != NULL)
5252 {
5253#ifdef UNIX
5254 if (!url)
5255 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005256 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 vp->ffv_ino = st.st_ino;
5258 vp->ffv_dev = st.st_dev;
5259 vp->ffv_fname[0] = NUL;
5260 }
5261 else
5262 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005263 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264#endif
5265 STRCPY(vp->ffv_fname, ff_expand_buffer);
5266#ifdef UNIX
5267 }
5268#endif
5269#ifdef FEAT_PATH_EXTRA
5270 if (wc_path != NULL)
5271 vp->ffv_wc_path = vim_strsave(wc_path);
5272 else
5273 vp->ffv_wc_path = NULL;
5274#endif
5275
5276 vp->ffv_next = *visited_list;
5277 *visited_list = vp;
5278 }
5279
5280 return OK;
5281}
5282
5283/*
5284 * create stack element from given path pieces
5285 */
5286 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005287ff_create_stack_element(
5288 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005290 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292 int level,
5293 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
5295 ff_stack_T *new;
5296
5297 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5298 if (new == NULL)
5299 return NULL;
5300
5301 new->ffs_prev = NULL;
5302 new->ffs_filearray = NULL;
5303 new->ffs_filearray_size = 0;
5304 new->ffs_filearray_cur = 0;
5305 new->ffs_stage = 0;
5306 new->ffs_level = level;
Bram Moolenaar945ec092016-06-08 21:17:43 +02005307 new->ffs_star_star_empty = star_star_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309 /* the following saves NULL pointer checks in vim_findfile */
5310 if (fix_part == NULL)
5311 fix_part = (char_u *)"";
5312 new->ffs_fix_path = vim_strsave(fix_part);
5313
5314#ifdef FEAT_PATH_EXTRA
5315 if (wc_part == NULL)
5316 wc_part = (char_u *)"";
5317 new->ffs_wc_path = vim_strsave(wc_part);
5318#endif
5319
5320 if (new->ffs_fix_path == NULL
5321#ifdef FEAT_PATH_EXTRA
5322 || new->ffs_wc_path == NULL
5323#endif
5324 )
5325 {
5326 ff_free_stack_element(new);
5327 new = NULL;
5328 }
5329
5330 return new;
5331}
5332
5333/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005334 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 */
5336 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005337ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005340 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005341 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005343 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5344 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346}
5347
5348/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005349 * Pop a dir from the directory stack.
5350 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 */
5352 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005353ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 ff_stack_T *sptr;
5356
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005357 sptr = search_ctx->ffsc_stack_ptr;
5358 if (search_ctx->ffsc_stack_ptr != NULL)
5359 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361 return sptr;
5362}
5363
5364/*
5365 * free the given stack element
5366 */
5367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005368ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005371 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005373 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374#endif
5375
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005376 if (stack_ptr->ffs_filearray != NULL)
5377 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005379 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380}
5381
5382/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005383 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 */
5385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005386ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 ff_stack_T *sptr;
5389
5390 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005391 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 ff_free_stack_element(sptr);
5393
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005394 vim_free(search_ctx->ffsc_file_to_search);
5395 vim_free(search_ctx->ffsc_start_dir);
5396 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005398 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#endif
5400
5401#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005402 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
5404 int i = 0;
5405
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005406 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005408 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 i++;
5410 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005411 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005413 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414#endif
5415
5416 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005417 search_ctx->ffsc_file_to_search = NULL;
5418 search_ctx->ffsc_start_dir = NULL;
5419 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005421 search_ctx->ffsc_wc_path = NULL;
5422 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#endif
5424}
5425
5426#ifdef FEAT_PATH_EXTRA
5427/*
5428 * check if the given path is in the stopdirs
5429 * returns TRUE if yes else FALSE
5430 */
5431 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005432ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
5434 int i = 0;
5435
5436 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005437 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 path_len--;
5439
5440 /* if no path consider it as match */
5441 if (path_len == 0)
5442 return TRUE;
5443
5444 for (i = 0; stopdirs_v[i] != NULL; i++)
5445 {
5446 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5447 {
5448 /* match for parent directory. So '/home' also matches
5449 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5450 * '/home/r' would also match '/home/rks'
5451 */
5452 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005453 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 return TRUE;
5455 }
5456 else
5457 {
5458 if (fnamecmp(stopdirs_v[i], path) == 0)
5459 return TRUE;
5460 }
5461 }
5462 return FALSE;
5463}
5464#endif
5465
5466#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5467/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005468 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 *
5470 * On the first call set the parameter 'first' to TRUE to initialize
5471 * the search. For repeating calls to FALSE.
5472 *
5473 * Repeating calls will return other files called 'ptr[len]' from the path.
5474 *
5475 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5476 * don't need valid values.
5477 *
5478 * If nothing found on the first call the option FNAME_MESS will issue the
5479 * message:
5480 * 'Can't find file "<file>" in path'
5481 * On repeating calls:
5482 * 'No more file "<file>" found in path'
5483 *
5484 * options:
5485 * FNAME_MESS give error message when not found
5486 *
5487 * Uses NameBuff[]!
5488 *
5489 * Returns an allocated string for the file name. NULL for error.
5490 *
5491 */
5492 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005493find_file_in_path(
5494 char_u *ptr, /* file name */
5495 int len, /* length of file name */
5496 int options,
5497 int first, /* use count'th matching file name */
5498 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 return find_file_in_path_option(ptr, len, options, first,
5501 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005502 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503}
5504
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005505static char_u *ff_file_to_find = NULL;
5506static void *fdip_search_ctx = NULL;
5507
5508#if defined(EXITFREE)
5509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005510free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005511{
5512 vim_free(ff_file_to_find);
5513 vim_findfile_cleanup(fdip_search_ctx);
5514}
5515#endif
5516
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517/*
5518 * Find the directory name "ptr[len]" in the path.
5519 *
5520 * options:
5521 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005522 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 *
5524 * Uses NameBuff[]!
5525 *
5526 * Returns an allocated string for the file name. NULL for error.
5527 */
5528 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005529find_directory_in_path(
5530 char_u *ptr, /* file name */
5531 int len, /* length of file name */
5532 int options,
5533 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005536 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537}
5538
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005539 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005540find_file_in_path_option(
5541 char_u *ptr, /* file name */
5542 int len, /* length of file name */
5543 int options,
5544 int first, /* use count'th matching file name */
5545 char_u *path_option, /* p_path or p_cdpath */
5546 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5547 char_u *rel_fname, /* file name we are looking relative to. */
5548 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 static int did_findfile_init = FALSE;
5552 char_u save_char;
5553 char_u *file_name = NULL;
5554 char_u *buf = NULL;
5555 int rel_to_curdir;
5556#ifdef AMIGA
5557 struct Process *proc = (struct Process *)FindTask(0L);
5558 APTR save_winptr = proc->pr_WindowPtr;
5559
5560 /* Avoid a requester here for a volume that doesn't exist. */
5561 proc->pr_WindowPtr = (APTR)-1L;
5562#endif
5563
5564 if (first == TRUE)
5565 {
5566 /* copy file name into NameBuff, expanding environment variables */
5567 save_char = ptr[len];
5568 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005569 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 ptr[len] = save_char;
5571
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005572 vim_free(ff_file_to_find);
5573 ff_file_to_find = vim_strsave(NameBuff);
5574 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 file_name = NULL;
5577 goto theend;
5578 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005579 if (options & FNAME_UNESC)
5580 {
5581 /* Change all "\ " to " ". */
5582 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5583 if (ptr[0] == '\\' && ptr[1] == ' ')
5584 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005588 rel_to_curdir = (ff_file_to_find[0] == '.'
5589 && (ff_file_to_find[1] == NUL
5590 || vim_ispathsep(ff_file_to_find[1])
5591 || (ff_file_to_find[1] == '.'
5592 && (ff_file_to_find[2] == NUL
5593 || vim_ispathsep(ff_file_to_find[2])))));
5594 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 /* "..", "../path", "." and "./path": don't use the path_option */
5596 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005597#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005599 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005600 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005601 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602#endif
5603#ifdef AMIGA
5604 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005605 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606#endif
5607 )
5608 {
5609 /*
5610 * Absolute path, no need to use "path_option".
5611 * If this is not a first call, return NULL. We already returned a
5612 * filename on the first call.
5613 */
5614 if (first == TRUE)
5615 {
5616 int l;
5617 int run;
5618
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005619 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005621 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 goto theend;
5623 }
5624
5625 /* When FNAME_REL flag given first use the directory of the file.
5626 * Otherwise or when this fails use the current directory. */
5627 for (run = 1; run <= 2; ++run)
5628 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005629 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (run == 1
5631 && rel_to_curdir
5632 && (options & FNAME_REL)
5633 && rel_fname != NULL
5634 && STRLEN(rel_fname) + l < MAXPATHL)
5635 {
5636 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005637 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 l = (int)STRLEN(NameBuff);
5639 }
5640 else
5641 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005642 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 run = 2;
5644 }
5645
5646 /* When the file doesn't exist, try adding parts of
5647 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005648 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 for (;;)
5650 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005651 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005652 && (find_what == FINDFILE_BOTH
5653 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005654 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 file_name = vim_strsave(NameBuff);
5657 goto theend;
5658 }
5659 if (*buf == NUL)
5660 break;
5661 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5662 }
5663 }
5664 }
5665 }
5666 else
5667 {
5668 /*
5669 * Loop over all paths in the 'path' or 'cdpath' option.
5670 * When "first" is set, first setup to the start of the option.
5671 * Otherwise continue to find the next match.
5672 */
5673 if (first == TRUE)
5674 {
5675 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005676 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 dir = path_option;
5678 did_findfile_init = FALSE;
5679 }
5680
5681 for (;;)
5682 {
5683 if (did_findfile_init)
5684 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005685 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (file_name != NULL)
5687 break;
5688
5689 did_findfile_init = FALSE;
5690 }
5691 else
5692 {
5693 char_u *r_ptr;
5694
5695 if (dir == NULL || *dir == NUL)
5696 {
5697 /* We searched all paths of the option, now we can
5698 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005699 vim_findfile_cleanup(fdip_search_ctx);
5700 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 break;
5702 }
5703
5704 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5705 break;
5706
5707 /* copy next path */
5708 buf[0] = 0;
5709 copy_option_part(&dir, buf, MAXPATHL, " ,");
5710
5711#ifdef FEAT_PATH_EXTRA
5712 /* get the stopdir string */
5713 r_ptr = vim_findfile_stopdir(buf);
5714#else
5715 r_ptr = NULL;
5716#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005717 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005718 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005719 fdip_search_ctx, FALSE, rel_fname);
5720 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 did_findfile_init = TRUE;
5722 vim_free(buf);
5723 }
5724 }
5725 }
5726 if (file_name == NULL && (options & FNAME_MESS))
5727 {
5728 if (first == TRUE)
5729 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005730 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005731 semsg(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005732 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005734 semsg(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005735 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737 else
5738 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005739 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005740 semsg(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005741 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005743 semsg(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005744 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746 }
5747
5748theend:
5749#ifdef AMIGA
5750 proc->pr_WindowPtr = save_winptr;
5751#endif
5752 return file_name;
5753}
5754
5755#endif /* FEAT_SEARCHPATH */
5756
5757/*
5758 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5759 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5760 */
5761 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005762vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764#ifndef FEAT_SEARCHPATH
5765 return mch_chdir((char *)new_dir);
5766#else
5767 char_u *dir_name;
5768 int r;
5769
5770 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5771 FNAME_MESS, curbuf->b_ffname);
5772 if (dir_name == NULL)
5773 return -1;
5774 r = mch_chdir((char *)dir_name);
5775 vim_free(dir_name);
5776 return r;
5777#endif
5778}
5779
5780/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005781 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005783 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5784 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 * Returns OK or FAIL.
5786 */
5787 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005788get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005790 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
5792 if (mch_get_user_name(buf, len) == FAIL)
5793 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005794 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
5796 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005797 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 return OK;
5799}
5800
5801#ifndef HAVE_QSORT
5802/*
5803 * Our own qsort(), for systems that don't have it.
5804 * It's simple and slow. From the K&R C book.
5805 */
5806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005807qsort(
5808 void *base,
5809 size_t elm_count,
5810 size_t elm_size,
5811 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 char_u *buf;
5814 char_u *p1;
5815 char_u *p2;
5816 int i, j;
5817 int gap;
5818
5819 buf = alloc((unsigned)elm_size);
5820 if (buf == NULL)
5821 return;
5822
5823 for (gap = elm_count / 2; gap > 0; gap /= 2)
5824 for (i = gap; i < elm_count; ++i)
5825 for (j = i - gap; j >= 0; j -= gap)
5826 {
5827 /* Compare the elements. */
5828 p1 = (char_u *)base + j * elm_size;
5829 p2 = (char_u *)base + (j + gap) * elm_size;
5830 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5831 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005832 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 mch_memmove(buf, p1, elm_size);
5834 mch_memmove(p1, p2, elm_size);
5835 mch_memmove(p2, buf, elm_size);
5836 }
5837
5838 vim_free(buf);
5839}
5840#endif
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842/*
5843 * Sort an array of strings.
5844 */
5845static int
5846#ifdef __BORLANDC__
5847_RTLENTRYF
5848#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005849sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
5851 static int
5852#ifdef __BORLANDC__
5853_RTLENTRYF
5854#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005855sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857 return STRCMP(*(char **)s1, *(char **)s2);
5858}
5859
5860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005861sort_strings(
5862 char_u **files,
5863 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868#if !defined(NO_EXPANDPATH) || defined(PROTO)
5869/*
5870 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005871 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 * Return value like strcmp(p, q), but consider path separators.
5873 */
5874 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005875pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005877 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005878 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005879 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005881 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005883 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005884 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005887 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005889 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 return 0;
5891 s = q;
Bram Moolenaar15675582018-02-09 12:29:56 +01005892 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 break;
5894 }
5895
5896 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005897 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
5899 s = p;
5900 break;
5901 }
5902
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005903 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#ifdef BACKSLASH_IN_FILENAME
5905 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005906 && !((c1 == '/' && c2 == '\\')
5907 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#endif
5909 )
5910 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005911 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005913 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005915 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5916 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005918
5919 i += MB_PTR2LEN((char_u *)p + i);
5920 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005922 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005923 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005925 c1 = PTR2CHAR((char_u *)s + i);
5926 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005928 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005929 && i > 0
5930 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005932 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005934 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005936 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 return 0; /* match with trailing slash */
5938 if (s == q)
5939 return -1; /* no match */
5940 return 1;
5941}
5942#endif
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944/*
5945 * The putenv() implementation below comes from the "screen" program.
5946 * Included with permission from Juergen Weigert.
5947 * See pty.c for the copyright notice.
5948 */
5949
5950/*
5951 * putenv -- put value into environment
5952 *
5953 * Usage: i = putenv (string)
5954 * int i;
5955 * char *string;
5956 *
5957 * where string is of the form <name>=<value>.
5958 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5959 *
5960 * Putenv may need to add a new name into the environment, or to
5961 * associate a value longer than the current value with a particular
5962 * name. So, to make life simpler, putenv() copies your entire
5963 * environment into the heap (i.e. malloc()) from the stack
5964 * (i.e. where it resides when your process is initiated) the first
5965 * time you call it.
5966 *
5967 * (history removed, not very interesting. See the "screen" sources.)
5968 */
5969
5970#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5971
5972#define EXTRASIZE 5 /* increment to add to env. size */
5973
5974static int envsize = -1; /* current size of environment */
Bram Moolenaard0573012017-10-28 21:11:06 +02005975extern char **environ; /* the global which is your env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005977static int findenv(char *name); /* look for a name in the env. */
5978static int newenv(void); /* copy env. from stack to heap */
5979static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
5981 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005982putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 int i;
5985 char *p;
5986
5987 if (envsize < 0)
5988 { /* first time putenv called */
5989 if (newenv() < 0) /* copy env. to heap */
5990 return -1;
5991 }
5992
5993 i = findenv((char *)string); /* look for name in environment */
5994
5995 if (i < 0)
5996 { /* name must be added */
5997 for (i = 0; environ[i]; i++);
5998 if (i >= (envsize - 1))
5999 { /* need new slot */
6000 if (moreenv() < 0)
6001 return -1;
6002 }
6003 p = (char *)alloc((unsigned)(strlen(string) + 1));
6004 if (p == NULL) /* not enough core */
6005 return -1;
6006 environ[i + 1] = 0; /* new end of env. */
6007 }
6008 else
6009 { /* name already in env. */
6010 p = vim_realloc(environ[i], strlen(string) + 1);
6011 if (p == NULL)
6012 return -1;
6013 }
6014 sprintf(p, "%s", string); /* copy into env. */
6015 environ[i] = p;
6016
6017 return 0;
6018}
6019
6020 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006021findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 char *namechar, *envchar;
6024 int i, found;
6025
6026 found = 0;
6027 for (i = 0; environ[i] && !found; i++)
6028 {
6029 envchar = environ[i];
6030 namechar = name;
6031 while (*namechar && *namechar != '=' && (*namechar == *envchar))
6032 {
6033 namechar++;
6034 envchar++;
6035 }
6036 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6037 }
6038 return found ? i - 1 : -1;
6039}
6040
6041 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006042newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 char **env, *elem;
6045 int i, esize;
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 for (i = 0; environ[i]; i++)
6048 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02006049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 esize = i + EXTRASIZE + 1;
6051 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6052 if (env == NULL)
6053 return -1;
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 for (i = 0; environ[i]; i++)
6056 {
6057 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6058 if (elem == NULL)
6059 return -1;
6060 env[i] = elem;
6061 strcpy(elem, environ[i]);
6062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 env[i] = 0;
6065 environ = env;
6066 envsize = esize;
6067 return 0;
6068}
6069
6070 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006071moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 int esize;
6074 char **env;
6075
6076 esize = envsize + EXTRASIZE;
6077 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6078 if (env == 0)
6079 return -1;
6080 environ = env;
6081 envsize = esize;
6082 return 0;
6083}
6084
6085# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02006086/*
6087 * Used for mch_getenv() for Mac.
6088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006090vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 int i;
6093 char_u *p;
6094
6095 if (envsize < 0)
6096 return NULL;
6097
6098 i = findenv((char *)string);
6099
6100 if (i < 0)
6101 return NULL;
6102
6103 p = vim_strchr((char_u *)environ[i], '=');
6104 return (p + 1);
6105}
6106# endif
6107
6108#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006109
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006110#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006111/*
6112 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6113 * rights to write into.
6114 */
6115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006116filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006117{
6118 int retval = 0;
6119#if defined(UNIX) || defined(VMS)
6120 int perm = 0;
6121#endif
6122
6123#if defined(UNIX) || defined(VMS)
6124 perm = mch_getperm(fname);
6125#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006126 if (
6127# ifdef WIN3264
6128 mch_writable(fname) &&
6129# else
6130# if defined(UNIX) || defined(VMS)
6131 (perm & 0222) &&
6132# endif
6133# endif
6134 mch_access((char *)fname, W_OK) == 0
6135 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006136 {
6137 ++retval;
6138 if (mch_isdir(fname))
6139 ++retval;
6140 }
6141 return retval;
6142}
6143#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006144
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006145#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6146/*
6147 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006148 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006149 */
6150 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006151get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006152{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006153 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006154
6155 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006156 if (n == EOF) return -1;
6157 c = getc(fd);
6158 if (c == EOF) return -1;
6159 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006160}
6161
6162/*
6163 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006164 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006165 */
6166 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006167get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006168{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006169 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006170
6171 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006172 if (n == EOF) return -1;
6173 c = getc(fd);
6174 if (c == EOF) return -1;
6175 n = (n << 8) + c;
6176 c = getc(fd);
6177 if (c == EOF) return -1;
6178 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006179}
6180
6181/*
6182 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006183 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006184 */
6185 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006186get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006187{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006188 int c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006189 /* Use unsigned rather than int otherwise result is undefined
6190 * when left-shift sets the MSB. */
6191 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006192
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006193 c = getc(fd);
6194 if (c == EOF) return -1;
6195 n = (unsigned)c;
6196 c = getc(fd);
6197 if (c == EOF) return -1;
6198 n = (n << 8) + (unsigned)c;
6199 c = getc(fd);
6200 if (c == EOF) return -1;
6201 n = (n << 8) + (unsigned)c;
6202 c = getc(fd);
6203 if (c == EOF) return -1;
6204 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006205 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006206}
6207
6208/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006209 * Read 8 bytes from "fd" and turn them into a time_T, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006210 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006211 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006212 time_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006213get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006214{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006215 int c;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006216 time_T n = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006217 int i;
6218
6219 for (i = 0; i < 8; ++i)
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006220 {
6221 c = getc(fd);
6222 if (c == EOF) return -1;
6223 n = (n << 8) + c;
6224 }
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006225 return n;
6226}
6227
6228/*
6229 * Read a string of length "cnt" from "fd" into allocated memory.
6230 * Returns NULL when out of memory or unable to read that many bytes.
6231 */
6232 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006233read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006234{
6235 char_u *str;
6236 int i;
6237 int c;
6238
6239 /* allocate memory */
6240 str = alloc((unsigned)cnt + 1);
6241 if (str != NULL)
6242 {
6243 /* Read the string. Quit when running into the EOF. */
6244 for (i = 0; i < cnt; ++i)
6245 {
6246 c = getc(fd);
6247 if (c == EOF)
6248 {
6249 vim_free(str);
6250 return NULL;
6251 }
6252 str[i] = c;
6253 }
6254 str[i] = NUL;
6255 }
6256 return str;
6257}
6258
6259/*
6260 * Write a number to file "fd", MSB first, in "len" bytes.
6261 */
6262 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006263put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006264{
6265 int i;
6266
6267 for (i = len - 1; i >= 0; --i)
6268 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6269 return FAIL;
6270 return OK;
6271}
6272
6273#ifdef _MSC_VER
6274# if (_MSC_VER <= 1200)
6275/* This line is required for VC6 without the service pack. Also see the
6276 * matching #pragma below. */
6277 # pragma optimize("", off)
6278# endif
6279#endif
6280
6281/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006282 * Write time_T to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006283 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006284 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006285 int
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006286put_time(FILE *fd, time_T the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006287{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006288 char_u buf[8];
6289
6290 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006291 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006292}
6293
6294/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006295 * Write time_T to "buf[8]".
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006296 */
6297 void
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006298time_to_bytes(time_T the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006299{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006300 int c;
6301 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006302 int bi = 0;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006303 time_T wtime = the_time;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006304
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006305 /* time_T can be up to 8 bytes in size, more than long_u, thus we
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006306 * can't use put_bytes() here.
6307 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006308 * sign. This happens for large values of wtime. A cast to long_u may
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006309 * truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes,
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006310 * it's safe to assume that long_u is 4 bytes or more and when using 8
6311 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006312 for (i = 7; i >= 0; --i)
6313 {
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006314 if (i + 1 > (int)sizeof(time_T))
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006315 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006316 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006317 else
6318 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006319#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006320 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006321#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006322 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006323#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006324 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006325 }
6326 }
6327}
6328
6329#ifdef _MSC_VER
6330# if (_MSC_VER <= 1200)
6331 # pragma optimize("", on)
6332# endif
6333#endif
6334
6335#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006336
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006337#if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006338/*
6339 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6340 * When "s" is NULL FALSE is returned.
6341 */
6342 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006343has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006344{
6345 char_u *p;
6346
6347 if (s != NULL)
6348 for (p = s; *p != NUL; ++p)
6349 if (*p >= 128)
6350 return TRUE;
6351 return FALSE;
6352}
6353#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006354
6355#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006356# define MAX_REPEAT_PARSE 8
6357
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006358/*
6359 * Process messages that have been queued for netbeans or clientserver.
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006360 * Also check if any jobs have ended.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006361 * These functions can call arbitrary vimscript and should only be called when
6362 * it is safe to do so.
6363 */
6364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006366{
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006367 win_T *old_curwin = curwin;
6368 int i;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006369
Bram Moolenaar94f01952018-09-01 15:30:03 +02006370 // Do not handle messages while redrawing, because it may cause buffers to
6371 // change or be wiped while they are being redrawn.
6372 if (updating_screen)
6373 return;
6374
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006375 // Loop when a job ended, but don't keep looping forever.
6376 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
6377 {
6378 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006379# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006380 channel_handle_events(FALSE);
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006381# endif
6382
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006383# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006384 // Process the queued netbeans messages.
6385 netbeans_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006386# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006387# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006388 // Write any buffer lines still to be written.
6389 channel_write_any_lines();
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006390
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006391 // Process the messages queued on channels.
6392 channel_parse_messages();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006393# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006394# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006395 // Process the queued clientserver messages.
6396 server_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006397# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006398# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006399 // Check if any jobs have ended. If so, repeat the above to handle
6400 // changes, e.g. stdin may have been closed.
6401 if (job_check_ended())
6402 continue;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006403# endif
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006404 break;
6405 }
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006406
Bram Moolenaar94f01952018-09-01 15:30:03 +02006407 // If the current window changed we need to bail out of the waiting loop.
6408 // E.g. when a job exit callback closes the terminal window.
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006409 if (curwin != old_curwin)
6410 ins_char_typebuf(K_IGNORE);
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006411}
6412#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006413
Bram Moolenaar51628222016-12-01 23:03:28 +01006414#ifndef PROTO /* proto is defined in vim.h */
6415# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006416/*
6417 * Return time in msec since "start_tv".
6418 */
6419 long
6420elapsed(struct timeval *start_tv)
6421{
6422 struct timeval now_tv;
6423
6424 gettimeofday(&now_tv, NULL);
6425 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
6426 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
6427}
Bram Moolenaar51628222016-12-01 23:03:28 +01006428# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006429
Bram Moolenaar51628222016-12-01 23:03:28 +01006430# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006431/*
6432 * Return time in msec since "start_tick".
6433 */
6434 long
6435elapsed(DWORD start_tick)
6436{
6437 DWORD now = GetTickCount();
6438
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006439 return (long)now - (long)start_tick;
6440}
Bram Moolenaar51628222016-12-01 23:03:28 +01006441# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006442#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006443
6444#if defined(FEAT_JOB_CHANNEL) \
6445 || (defined(UNIX) && (!defined(USE_SYSTEM) \
6446 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
6447 || defined(PROTO)
6448/*
6449 * Parse "cmd" and put the white-separated parts in "argv".
6450 * "argv" is an allocated array with "argc" entries and room for 4 more.
6451 * Returns FAIL when out of memory.
6452 */
6453 int
6454mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
6455{
6456 int i;
6457 char_u *p, *d;
6458 int inquote;
6459
6460 /*
6461 * Do this loop twice:
6462 * 1: find number of arguments
6463 * 2: separate them and build argv[]
6464 */
6465 for (i = 0; i < 2; ++i)
6466 {
6467 p = skipwhite(cmd);
6468 inquote = FALSE;
6469 *argc = 0;
6470 for (;;)
6471 {
6472 if (i == 1)
6473 (*argv)[*argc] = (char *)p;
6474 ++*argc;
6475 d = p;
6476 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
6477 {
6478 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006479 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02006480 inquote = !inquote;
6481 else
6482 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006483 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02006484 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006485 // First pass: skip over "\ " and "\"".
6486 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02006487 ++p;
6488 }
6489 if (i == 1)
6490 *d++ = *p;
6491 }
6492 ++p;
6493 }
6494 if (*p == NUL)
6495 {
6496 if (i == 1)
6497 *d++ = NUL;
6498 break;
6499 }
6500 if (i == 1)
6501 *d++ = NUL;
6502 p = skipwhite(p + 1);
6503 }
6504 if (*argv == NULL)
6505 {
6506 if (use_shcf)
6507 {
6508 /* Account for possible multiple args in p_shcf. */
6509 p = p_shcf;
6510 for (;;)
6511 {
6512 p = skiptowhite(p);
6513 if (*p == NUL)
6514 break;
6515 ++*argc;
6516 p = skipwhite(p);
6517 }
6518 }
6519
6520 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
6521 if (*argv == NULL) /* out of memory */
6522 return FAIL;
6523 }
6524 }
6525 return OK;
6526}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006527
6528# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6529/*
6530 * Build "argv[argc]" from the string "cmd".
6531 * "argv[argc]" is set to NULL;
6532 * Return FAIL when out of memory.
6533 */
6534 int
6535build_argv_from_string(char_u *cmd, char ***argv, int *argc)
6536{
6537 char_u *cmd_copy;
6538 int i;
6539
6540 /* Make a copy, parsing will modify "cmd". */
6541 cmd_copy = vim_strsave(cmd);
6542 if (cmd_copy == NULL
6543 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
6544 {
6545 vim_free(cmd_copy);
6546 return FAIL;
6547 }
6548 for (i = 0; i < *argc; i++)
6549 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
6550 (*argv)[*argc] = NULL;
6551 vim_free(cmd_copy);
6552 return OK;
6553}
6554
6555/*
6556 * Build "argv[argc]" from the list "l".
6557 * "argv[argc]" is set to NULL;
6558 * Return FAIL when out of memory.
6559 */
6560 int
6561build_argv_from_list(list_T *l, char ***argv, int *argc)
6562{
6563 listitem_T *li;
6564 char_u *s;
6565
6566 /* Pass argv[] to mch_call_shell(). */
6567 *argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
6568 if (*argv == NULL)
6569 return FAIL;
6570 *argc = 0;
6571 for (li = l->lv_first; li != NULL; li = li->li_next)
6572 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006573 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006574 if (s == NULL)
6575 {
6576 int i;
6577
6578 for (i = 0; i < *argc; ++i)
6579 vim_free((*argv)[i]);
6580 return FAIL;
6581 }
6582 (*argv)[*argc] = (char *)vim_strsave(s);
6583 *argc += 1;
6584 }
6585 (*argv)[*argc] = NULL;
6586 return OK;
6587}
6588# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006589#endif