blob: b1cf8b5e909191bf380066556adb55df82f43d9a [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
76getviscol2(colnr_T col, colnr_T coladd)
77{
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
323#ifdef FEAT_MBYTE
Bram Moolenaara1381de2009-11-03 15:44:21 +0000324 /* prevent from moving onto a trail byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200326 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#endif
328
329 if (col < wcol)
330 return FAIL;
331 return OK;
332}
333
334/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000335 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 */
337 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 return inc(&curwin->w_cursor);
341}
342
Bram Moolenaar446cb832008-06-24 21:56:24 +0000343/*
344 * Increment the line pointer "lp" crossing line boundaries as necessary.
345 * Return 1 when going to the next line.
346 * Return 2 when moving forward onto a NUL at the end of the line).
347 * Return -1 when at the end of file.
348 * Return 0 otherwise.
349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100351inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100355 /* when searching position may be set to end of a line */
356 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100358 p = ml_get_pos(lp);
359 if (*p != NUL) /* still within line, move to next char (may be NUL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100361#ifdef FEAT_MBYTE
362 if (has_mbyte)
363 {
364 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100366 lp->col += l;
367 return ((p[l] != NUL) ? 0 : 2);
368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369#endif
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100370 lp->col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100372 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#endif
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100374 return ((p[1] != NUL) ? 0 : 2);
375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
377 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
378 {
379 lp->col = 0;
380 lp->lnum++;
381#ifdef FEAT_VIRTUALEDIT
382 lp->coladd = 0;
383#endif
384 return 1;
385 }
386 return -1;
387}
388
389/*
390 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
391 */
392 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100393incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
395 int r;
396
397 if ((r = inc(lp)) >= 1 && lp->col)
398 r = inc(lp);
399 return r;
400}
401
402/*
403 * dec(p)
404 *
405 * Decrement the line pointer 'p' crossing line boundaries as necessary.
406 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
407 */
408 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100409dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100411 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412}
413
414 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100415dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
417 char_u *p;
418
419#ifdef FEAT_VIRTUALEDIT
420 lp->coladd = 0;
421#endif
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100422 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100424 /* past end of line */
425 p = ml_get(lp->lnum);
426 lp->col = (colnr_T)STRLEN(p);
427#ifdef FEAT_MBYTE
428 if (has_mbyte)
429 lp->col -= (*mb_head_off)(p, p + lp->col);
430#endif
431 return 0;
432 }
433
434 if (lp->col > 0)
435 {
436 /* still within line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 lp->col--;
438#ifdef FEAT_MBYTE
439 if (has_mbyte)
440 {
441 p = ml_get(lp->lnum);
442 lp->col -= (*mb_head_off)(p, p + lp->col);
443 }
444#endif
445 return 0;
446 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100447
448 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100450 /* there is a prior line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 lp->lnum--;
452 p = ml_get(lp->lnum);
453 lp->col = (colnr_T)STRLEN(p);
454#ifdef FEAT_MBYTE
455 if (has_mbyte)
456 lp->col -= (*mb_head_off)(p, p + lp->col);
457#endif
458 return 1;
459 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100460
461 /* at start of file */
462 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463}
464
465/*
466 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
467 */
468 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100469decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int r;
472
473 if ((r = dec(lp)) == 1 && lp->col)
474 r = dec(lp);
475 return r;
476}
477
478/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200479 * Get the line number relative to the current cursor position, i.e. the
480 * difference between line number and cursor position. Only look for lines that
481 * can be visible, folded lines don't count.
482 */
483 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484get_cursor_rel_lnum(
485 win_T *wp,
486 linenr_T lnum) /* line number to get the result for */
Bram Moolenaar64486672010-05-16 15:46:46 +0200487{
488 linenr_T cursor = wp->w_cursor.lnum;
489 linenr_T retval = 0;
490
491#ifdef FEAT_FOLDING
492 if (hasAnyFolding(wp))
493 {
494 if (lnum > cursor)
495 {
496 while (lnum > cursor)
497 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100498 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200499 /* if lnum and cursor are in the same fold,
500 * now lnum <= cursor */
501 if (lnum > cursor)
502 retval++;
503 lnum--;
504 }
505 }
506 else if (lnum < cursor)
507 {
508 while (lnum < cursor)
509 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100510 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200511 /* if lnum and cursor are in the same fold,
512 * now lnum >= cursor */
513 if (lnum < cursor)
514 retval--;
515 lnum++;
516 }
517 }
518 /* else if (lnum == cursor)
519 * retval = 0;
520 */
521 }
522 else
523#endif
524 retval = lnum - cursor;
525
526 return retval;
527}
528
529/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200530 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
531 * This allows for the col to be on the NUL byte.
532 */
533 void
534check_pos(buf_T *buf, pos_T *pos)
535{
536 char_u *line;
537 colnr_T len;
538
539 if (pos->lnum > buf->b_ml.ml_line_count)
540 pos->lnum = buf->b_ml.ml_line_count;
541
542 if (pos->col > 0)
543 {
544 line = ml_get_buf(buf, pos->lnum, FALSE);
545 len = (colnr_T)STRLEN(line);
546 if (pos->col > len)
547 pos->col = len;
548 }
549}
550
551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 * Make sure curwin->w_cursor.lnum is valid.
553 */
554 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100555check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
558 {
559#ifdef FEAT_FOLDING
560 /* If there is a closed fold at the end of the file, put the cursor in
561 * its first line. Otherwise in the last line. */
562 if (!hasFolding(curbuf->b_ml.ml_line_count,
563 &curwin->w_cursor.lnum, NULL))
564#endif
565 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
566 }
567 if (curwin->w_cursor.lnum <= 0)
568 curwin->w_cursor.lnum = 1;
569}
570
571/*
572 * Make sure curwin->w_cursor.col is valid.
573 */
574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100575check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200577 check_cursor_col_win(curwin);
578}
579
580/*
581 * Make sure win->w_cursor.col is valid.
582 */
583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100584check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200585{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 colnr_T len;
587#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 colnr_T oldcol = win->w_cursor.col;
589 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#endif
591
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200592 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200594 win->w_cursor.col = 0;
595 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000597 /* Allow cursor past end-of-line when:
598 * - in Insert mode or restarting Insert mode
599 * - in Visual mode and 'selection' isn't "old"
600 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000601 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000603#ifdef FEAT_VIRTUALEDIT
604 || (ve_flags & VE_ONEMORE)
605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200607 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000609 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200610 win->w_cursor.col = len - 1;
Bram Moolenaar87c19962007-04-26 08:54:21 +0000611#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200612 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000613 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200614 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000615#endif
616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200618 else if (win->w_cursor.col < 0)
619 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621#ifdef FEAT_VIRTUALEDIT
622 /* If virtual editing is on, we can leave the cursor on the old position,
623 * only we must set it to virtual. But don't do it when at the end of the
624 * line. */
625 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200626 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000628 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200629 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200630 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200631 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200632
633 /* Make sure that coladd is not more than the char width.
634 * Not for the last character, coladd is then used when the cursor
635 * is actually after the last character. */
636 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200637 {
638 int cs, ce;
639
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200640 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
641 if (win->w_cursor.coladd > ce - cs)
642 win->w_cursor.coladd = ce - cs;
643 }
644 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000645 else
646 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200647 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#endif
650}
651
652/*
653 * make sure curwin->w_cursor in on a valid character
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 check_cursor_lnum();
659 check_cursor_col();
660}
661
662#if defined(FEAT_TEXTOBJ) || defined(PROTO)
663/*
664 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
665 * Allow it when in Visual mode and 'selection' is not "old".
666 */
667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 && gchar_cursor() == NUL)
673 --curwin->w_cursor.col;
674}
675#endif
676
677/*
678 * When curwin->w_leftcol has changed, adjust the cursor position.
679 * Return TRUE if the cursor was moved.
680 */
681 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100682leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 long lastcol;
685 colnr_T s, e;
686 int retval = FALSE;
687
688 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200689 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 validate_virtcol();
691
692 /*
693 * If the cursor is right or left of the screen, move it to last or first
694 * character.
695 */
696 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
697 {
698 retval = TRUE;
699 coladvance((colnr_T)(lastcol - p_siso));
700 }
701 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
702 {
703 retval = TRUE;
704 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
705 }
706
707 /*
708 * If the start of the character under the cursor is not on the screen,
709 * advance the cursor one more char. If this fails (last char of the
710 * line) adjust the scrolling.
711 */
712 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
713 if (e > (colnr_T)lastcol)
714 {
715 retval = TRUE;
716 coladvance(s - 1);
717 }
718 else if (s < curwin->w_leftcol)
719 {
720 retval = TRUE;
721 if (coladvance(e + 1) == FAIL) /* there isn't another character */
722 {
723 curwin->w_leftcol = s; /* adjust w_leftcol instead */
724 changed_cline_bef_curs();
725 }
726 }
727
728 if (retval)
729 curwin->w_set_curswant = TRUE;
730 redraw_later(NOT_VALID);
731 return retval;
732}
733
734/**********************************************************************
735 * Various routines dealing with allocation and deallocation of memory.
736 */
737
738#if defined(MEM_PROFILE) || defined(PROTO)
739
740# define MEM_SIZES 8200
741static long_u mem_allocs[MEM_SIZES];
742static long_u mem_frees[MEM_SIZES];
743static long_u mem_allocated;
744static long_u mem_freed;
745static long_u mem_peak;
746static long_u num_alloc;
747static long_u num_freed;
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100750mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 *sizep += sizeof(size_t);
753}
754
755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100756mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757{
758 *sizep += sizeof(size_t);
759}
760
761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100762mem_post_alloc(
763 void **pp,
764 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 if (*pp == NULL)
767 return;
768 size -= sizeof(size_t);
769 *(long_u *)*pp = size;
770 if (size <= MEM_SIZES-1)
771 mem_allocs[size-1]++;
772 else
773 mem_allocs[MEM_SIZES-1]++;
774 mem_allocated += size;
775 if (mem_allocated - mem_freed > mem_peak)
776 mem_peak = mem_allocated - mem_freed;
777 num_alloc++;
778 *pp = (void *)((char *)*pp + sizeof(size_t));
779}
780
781 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100782mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
784 long_u size;
785
786 *pp = (void *)((char *)*pp - sizeof(size_t));
787 size = *(size_t *)*pp;
788 if (size <= MEM_SIZES-1)
789 mem_frees[size-1]++;
790 else
791 mem_frees[MEM_SIZES-1]++;
792 mem_freed += size;
793 num_freed++;
794}
795
796/*
797 * called on exit via atexit()
798 */
799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100800vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 int i, j;
803
804 printf("\r\n");
805 j = 0;
806 for (i = 0; i < MEM_SIZES - 1; i++)
807 {
808 if (mem_allocs[i] || mem_frees[i])
809 {
810 if (mem_frees[i] > mem_allocs[i])
811 printf("\r\n%s", _("ERROR: "));
812 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
813 j++;
814 if (j > 3)
815 {
816 j = 0;
817 printf("\r\n");
818 }
819 }
820 }
821
822 i = MEM_SIZES - 1;
823 if (mem_allocs[i])
824 {
825 printf("\r\n");
826 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200827 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
829 }
830
831 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
832 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
833 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
834 num_alloc, num_freed);
835}
836
837#endif /* MEM_PROFILE */
838
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100839#ifdef FEAT_EVAL
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100840 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100841alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100842{
843 if (alloc_fail_countdown == 0)
844 {
845 if (--alloc_fail_repeat <= 0)
846 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100847 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100848 return TRUE;
849 }
850 --alloc_fail_countdown;
851 return FALSE;
852}
853#endif
854
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855/*
856 * Some memory is reserved for error messages and for being able to
857 * call mf_release_all(), which needs some memory for mf_trans_add().
858 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100859#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200860#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000863 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * Use lalloc for larger blocks.
865 */
866 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100867alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868{
869 return (lalloc((long_u)size, TRUE));
870}
871
872/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100873 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100874 */
875 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100876alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100877{
878#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100879 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100880 return NULL;
881#endif
882 return (lalloc((long_u)size, TRUE));
883}
884
885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * Allocate memory and set all bytes to zero.
887 */
888 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100889alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 char_u *p;
892
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200893 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (p != NULL)
895 (void)vim_memset(p, 0, (size_t)size);
896 return p;
897}
898
899/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100900 * Same as alloc_clear() but with allocation id for testing
901 */
902 char_u *
903alloc_clear_id(unsigned size, alloc_id_T id UNUSED)
904{
905#ifdef FEAT_EVAL
906 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
907 return NULL;
908#endif
909 return alloc_clear(size);
910}
911
912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * alloc() with check for maximum line length
914 */
915 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100916alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200918#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (sizeof(int) == 2 && size > 0x7fff)
920 {
921 /* Don't hide this message */
922 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100923 emsg(_("E340: Line is becoming too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 return NULL;
925 }
926#endif
927 return (lalloc((long_u)size, TRUE));
928}
929
930/*
931 * Allocate memory like lalloc() and set all bytes to zero.
932 */
933 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100934lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 char_u *p;
937
938 p = (lalloc(size, message));
939 if (p != NULL)
940 (void)vim_memset(p, 0, (size_t)size);
941 return p;
942}
943
944/*
945 * Low level memory allocation function.
946 * This is used often, KEEP IT FAST!
947 */
948 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100949lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 char_u *p; /* pointer to new storage space */
952 static int releasing = FALSE; /* don't do mf_release_all() recursive */
953 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100954#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 static long_u allocated = 0; /* allocated since last avail check */
956#endif
957
958 /* Safety check for allocating zero bytes */
959 if (size == 0)
960 {
961 /* Don't hide this message */
962 emsg_silent = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100963 siemsg(_("E341: Internal error: lalloc(%ld, )"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 return NULL;
965 }
966
967#ifdef MEM_PROFILE
968 mem_pre_alloc_l(&size);
969#endif
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 /*
972 * Loop when out of memory: Try to release some memfile blocks and
973 * if some blocks are released call malloc again.
974 */
975 for (;;)
976 {
977 /*
978 * Handle three kind of systems:
979 * 1. No check for available memory: Just return.
980 * 2. Slow check for available memory: call mch_avail_mem() after
981 * allocating KEEP_ROOM amount of memory.
982 * 3. Strict check for available memory: call mch_avail_mem()
983 */
984 if ((p = (char_u *)malloc((size_t)size)) != NULL)
985 {
986#ifndef HAVE_AVAIL_MEM
987 /* 1. No check for available memory: Just return. */
988 goto theend;
989#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 /* 2. Slow check for available memory: call mch_avail_mem() after
991 * allocating (KEEP_ROOM / 2) amount of memory. */
992 allocated += size;
993 if (allocated < KEEP_ROOM / 2)
994 goto theend;
995 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200998 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
Bram Moolenaar5a221812008-11-12 12:08:45 +00001000 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 p = NULL;
1002 }
1003 else
1004 goto theend;
1005#endif
1006 }
1007 /*
1008 * Remember that mf_release_all() is being called to avoid an endless
1009 * loop, because mf_release_all() may call alloc() recursively.
1010 */
1011 if (releasing)
1012 break;
1013 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +00001014
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001015 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001016 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 releasing = FALSE;
1019 if (!try_again)
1020 break;
1021 }
1022
1023 if (message && p == NULL)
1024 do_outofmem_msg(size);
1025
1026theend:
1027#ifdef MEM_PROFILE
1028 mem_post_alloc((void **)&p, (size_t)size);
1029#endif
1030 return p;
1031}
1032
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001033/*
1034 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001035 */
Bram Moolenaar113e1072019-01-20 15:30:40 +01001036#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001037 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001038lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001039{
1040#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001041 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001042 return NULL;
1043#endif
1044 return (lalloc((long_u)size, message));
1045}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001046#endif
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#if defined(MEM_PROFILE) || defined(PROTO)
1049/*
1050 * realloc() with memory profiling.
1051 */
1052 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001053mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 void *p;
1056
1057 mem_pre_free(&ptr);
1058 mem_pre_alloc_s(&size);
1059
1060 p = realloc(ptr, size);
1061
1062 mem_post_alloc(&p, size);
1063
1064 return p;
1065}
1066#endif
1067
1068/*
1069* Avoid repeating the error message many times (they take 1 second each).
1070* Did_outofmem_msg is reset when a character is read.
1071*/
1072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001073do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075 if (!did_outofmem_msg)
1076 {
1077 /* Don't hide this message */
1078 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001079
1080 /* Must come first to avoid coming back here when printing the error
1081 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001083
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 semsg(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
1086}
1087
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001088#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001089
1090# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001091static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001092# endif
1093
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001094/*
1095 * Free everything that we allocated.
1096 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001097 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1098 * surprised if Vim crashes...
1099 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001100 */
1101 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001102free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001103{
1104 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001105
1106 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1107 * Don't try freeing everything again. */
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001108 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001109 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001110 entered_free_all_mem = TRUE;
Bram Moolenaara9673212016-06-01 22:21:06 +02001111
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001112 /* Don't want to trigger autocommands from here on. */
1113 block_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001114
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001115 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1116 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001117 if (first_tabpage->tp_next != NULL)
1118 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001119 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001120 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001121
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001122# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001123 /* Free all spell info. */
1124 spell_free_all();
1125# endif
1126
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001127#if defined(FEAT_INS_EXPAND) && defined(FEAT_BEVAL_TERM)
1128 ui_remove_balloon();
1129# endif
1130
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001131# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001132 /* Clear user commands (before deleting buffers). */
1133 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001134# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001135
1136# ifdef FEAT_MENU
1137 /* Clear menus. */
1138 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001139# ifdef FEAT_MULTI_LANG
1140 do_cmdline_cmd((char_u *)"menutranslate clear");
1141# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001142# endif
1143
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001144 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001145 do_cmdline_cmd((char_u *)"lmapclear");
1146 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001147 do_cmdline_cmd((char_u *)"mapclear");
1148 do_cmdline_cmd((char_u *)"mapclear!");
1149 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001150# if defined(FEAT_EVAL)
1151 do_cmdline_cmd((char_u *)"breakdel *");
1152# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001153# if defined(FEAT_PROFILE)
1154 do_cmdline_cmd((char_u *)"profdel *");
1155# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001156# if defined(FEAT_KEYMAP)
1157 do_cmdline_cmd((char_u *)"set keymap=");
1158#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001159
1160# ifdef FEAT_TITLE
1161 free_titles();
1162# endif
1163# if defined(FEAT_SEARCHPATH)
1164 free_findfile();
1165# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001166
1167 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001168 free_all_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001169 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001170 free_all_marks();
1171 alist_clear(&global_alist);
1172 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001173# if defined(FEAT_CMDL_COMPL)
1174 free_users();
1175# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001176 free_search_patterns();
1177 free_old_sub();
1178 free_last_insert();
1179 free_prev_shellcmd();
1180 free_regexp_stuff();
1181 free_tag_stuff();
1182 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001183# ifdef FEAT_SIGNS
1184 free_signs();
1185# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001186# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001187 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001188# endif
1189# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001190 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001191# endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001192 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001193
1194 /* Free some global vars. */
1195 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001196# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001197 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001198# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001199 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001200# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001201 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001202# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001203 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001204 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001205
1206 /* Clear cmdline history. */
1207 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001208# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001209 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001210# endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001211#ifdef FEAT_TEXT_PROP
1212 clear_global_prop_types();
1213#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001214
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001215#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001216 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001217 win_T *win;
1218 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001219
1220 qf_free_all(NULL);
1221 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001222 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001223 qf_free_all(win);
1224 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001225#endif
1226
1227 /* Close all script inputs. */
1228 close_all_scripts();
1229
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001230 /* Destroy all windows. Must come before freeing buffers. */
1231 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001232
Bram Moolenaar4f198282017-10-23 21:53:30 +02001233 /* Free all option values. Must come after closing windows. */
1234 free_all_options();
1235
Bram Moolenaar2a329742008-04-01 12:53:43 +00001236 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1237 * were freed already. */
1238#ifdef FEAT_AUTOCHDIR
1239 p_acd = FALSE;
1240#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001241 for (buf = firstbuf; buf != NULL; )
1242 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001243 bufref_T bufref;
1244
1245 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001246 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001247 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001248 if (bufref_valid(&bufref))
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001249 buf = nextbuf; /* didn't work, try next one */
1250 else
1251 buf = firstbuf;
1252 }
1253
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001254# ifdef FEAT_ARABIC
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001255 free_cmdline_buf();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001256# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001257
1258 /* Clear registers. */
1259 clear_registers();
1260 ResetRedobuff();
1261 ResetRedobuff();
1262
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001263# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001264 vim_free(serverDelayedStartName);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001265# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001266
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001267 /* highlight info */
1268 free_highlight();
1269
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001270 reset_last_sourcing();
1271
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001272 free_tabpage(first_tabpage);
1273 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001274
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001275# ifdef UNIX
1276 /* Machine-specific free. */
1277 mch_free_mem();
1278# endif
1279
1280 /* message history */
1281 for (;;)
1282 if (delete_first_msg() == FAIL)
1283 break;
1284
Bram Moolenaar655da312016-05-28 22:22:34 +02001285# ifdef FEAT_JOB_CHANNEL
1286 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001287# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001288# ifdef FEAT_TIMERS
Bram Moolenaar623e2632016-07-30 22:47:56 +02001289 timer_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001290# endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001291# ifdef FEAT_EVAL
1292 /* must be after channel_free_all() with unrefs partials */
1293 eval_clear();
1294# endif
1295# ifdef FEAT_JOB_CHANNEL
1296 /* must be after eval_clear() with unrefs jobs */
1297 job_free_all();
1298# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001299
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001300 free_termoptions();
1301
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001302 /* screenlines (can't display anything now!) */
1303 free_screenlines();
1304
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001305# if defined(USE_XSMP)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001306 xsmp_close();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001307# endif
1308# ifdef FEAT_GUI_GTK
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001309 gui_mch_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001310# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001311 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001312
1313 vim_free(IObuff);
1314 vim_free(NameBuff);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001315# ifdef FEAT_QUICKFIX
1316 check_quickfix_busy();
1317# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001318}
1319#endif
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001322 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 */
1324 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001325vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326{
1327 char_u *p;
1328 unsigned len;
1329
1330 len = (unsigned)STRLEN(string) + 1;
1331 p = alloc(len);
1332 if (p != NULL)
1333 mch_memmove(p, string, (size_t)len);
1334 return p;
1335}
1336
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001337/*
1338 * Copy up to "len" bytes of "string" into newly allocated memory and
1339 * terminate with a NUL.
1340 * The allocated memory always has size "len + 1", also when "string" is
1341 * shorter.
1342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001344vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346 char_u *p;
1347
1348 p = alloc((unsigned)(len + 1));
1349 if (p != NULL)
1350 {
1351 STRNCPY(p, string, len);
1352 p[len] = NUL;
1353 }
1354 return p;
1355}
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001358 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1359 * Returns NULL when out of memory.
1360 */
1361 char_u *
1362vim_memsave(char_u *p, int len)
1363{
1364 char_u *ret = alloc((unsigned)len);
1365
1366 if (ret != NULL)
1367 mch_memmove(ret, p, (size_t)len);
1368 return ret;
1369}
1370
1371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1373 * by a backslash.
1374 */
1375 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001376vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001378 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379}
1380
1381/*
1382 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1383 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001384 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 */
1386 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001387vim_strsave_escaped_ext(
1388 char_u *string,
1389 char_u *esc_chars,
1390 int cc,
1391 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 char_u *p;
1394 char_u *p2;
1395 char_u *escaped_string;
1396 unsigned length;
1397#ifdef FEAT_MBYTE
1398 int l;
1399#endif
1400
1401 /*
1402 * First count the number of backslashes required.
1403 * Then allocate the memory and insert them.
1404 */
1405 length = 1; /* count the trailing NUL */
1406 for (p = string; *p; p++)
1407 {
1408#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001409 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
1411 length += l; /* count a multibyte char */
1412 p += l - 1;
1413 continue;
1414 }
1415#endif
1416 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1417 ++length; /* count a backslash */
1418 ++length; /* count an ordinary char */
1419 }
1420 escaped_string = alloc(length);
1421 if (escaped_string != NULL)
1422 {
1423 p2 = escaped_string;
1424 for (p = string; *p; p++)
1425 {
1426#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001427 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
1429 mch_memmove(p2, p, (size_t)l);
1430 p2 += l;
1431 p += l - 1; /* skip multibyte char */
1432 continue;
1433 }
1434#endif
1435 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001436 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 *p2++ = *p;
1438 }
1439 *p2 = NUL;
1440 }
1441 return escaped_string;
1442}
1443
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001444/*
1445 * Return TRUE when 'shell' has "csh" in the tail.
1446 */
1447 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001448csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001449{
1450 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1451}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001452
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001453/*
1454 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001455 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001456 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001457 * Escape a newline, depending on the 'shell' option.
1458 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1459 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001460 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001461 * Returns the result in allocated memory, NULL if we have run out.
1462 */
1463 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001464vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001465{
1466 unsigned length;
1467 char_u *p;
1468 char_u *d;
1469 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001470 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001471 int csh_like;
1472
1473 /* Only csh and similar shells expand '!' within single quotes. For sh and
1474 * the like we must not put a backslash before it, it will be taken
1475 * literally. If do_special is set the '!' will be escaped twice.
1476 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001477 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001478
1479 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001480 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001481 for (p = string; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001482 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001483# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001484 if (!p_ssl)
1485 {
1486 if (*p == '"')
1487 ++length; /* " -> "" */
1488 }
1489 else
1490# endif
1491 if (*p == '\'')
1492 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001493 if ((*p == '\n' && (csh_like || do_newline))
1494 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001495 {
1496 ++length; /* insert backslash */
1497 if (csh_like && do_special)
1498 ++length; /* insert backslash */
1499 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001500 if (do_special && find_cmdline_var(p, &l) >= 0)
1501 {
1502 ++length; /* insert backslash */
1503 p += l - 1;
1504 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001505 }
1506
1507 /* Allocate memory for the result and fill it. */
1508 escaped_string = alloc(length);
1509 if (escaped_string != NULL)
1510 {
1511 d = escaped_string;
1512
1513 /* add opening quote */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001514# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001515 if (!p_ssl)
1516 *d++ = '"';
1517 else
1518# endif
1519 *d++ = '\'';
1520
1521 for (p = string; *p != NUL; )
1522 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001523# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001524 if (!p_ssl)
1525 {
1526 if (*p == '"')
1527 {
1528 *d++ = '"';
1529 *d++ = '"';
1530 ++p;
1531 continue;
1532 }
1533 }
1534 else
1535# endif
1536 if (*p == '\'')
1537 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001538 *d++ = '\'';
1539 *d++ = '\\';
1540 *d++ = '\'';
1541 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001542 ++p;
1543 continue;
1544 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001545 if ((*p == '\n' && (csh_like || do_newline))
1546 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001547 {
1548 *d++ = '\\';
1549 if (csh_like && do_special)
1550 *d++ = '\\';
1551 *d++ = *p++;
1552 continue;
1553 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001554 if (do_special && find_cmdline_var(p, &l) >= 0)
1555 {
1556 *d++ = '\\'; /* insert backslash */
1557 while (--l >= 0) /* copy the var */
1558 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001559 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001560 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001561
1562 MB_COPY_CHAR(p, d);
1563 }
1564
1565 /* add terminating quote and finish with a NUL */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001566# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001567 if (!p_ssl)
1568 *d++ = '"';
1569 else
1570# endif
1571 *d++ = '\'';
1572 *d = NUL;
1573 }
1574
1575 return escaped_string;
1576}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578/*
1579 * Like vim_strsave(), but make all characters uppercase.
1580 * This uses ASCII lower-to-upper case translation, language independent.
1581 */
1582 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001583vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 char_u *p1;
1586
1587 p1 = vim_strsave(string);
1588 vim_strup(p1);
1589 return p1;
1590}
1591
1592/*
1593 * Like vim_strnsave(), but make all characters uppercase.
1594 * This uses ASCII lower-to-upper case translation, language independent.
1595 */
1596 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001597vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 char_u *p1;
1600
1601 p1 = vim_strnsave(string, len);
1602 vim_strup(p1);
1603 return p1;
1604}
1605
1606/*
1607 * ASCII lower-to-upper case translation, language independent.
1608 */
1609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001610vim_strup(
1611 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 char_u *p2;
1614 int c;
1615
1616 if (p != NULL)
1617 {
1618 p2 = p;
1619 while ((c = *p2) != NUL)
1620#ifdef EBCDIC
1621 *p2++ = isalpha(c) ? toupper(c) : c;
1622#else
1623 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1624#endif
1625 }
1626}
1627
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001628#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001629/*
1630 * Make string "s" all upper-case and return it in allocated memory.
1631 * Handles multi-byte characters as well as possible.
1632 * Returns NULL when out of memory.
1633 */
1634 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001635strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001636{
1637 char_u *p;
1638 char_u *res;
1639
1640 res = p = vim_strsave(orig);
1641
1642 if (res != NULL)
1643 while (*p != NUL)
1644 {
1645# ifdef FEAT_MBYTE
1646 int l;
1647
1648 if (enc_utf8)
1649 {
1650 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001651 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001652 char_u *s;
1653
1654 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001655 l = utf_ptr2len(p);
1656 if (c == 0)
1657 {
1658 /* overlong sequence, use only the first byte */
1659 c = *p;
1660 l = 1;
1661 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001662 uc = utf_toupper(c);
1663
1664 /* Reallocate string when byte count changes. This is rare,
1665 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001666 newl = utf_char2len(uc);
1667 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001668 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001669 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001670 if (s == NULL)
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001671 {
1672 vim_free(res);
1673 return NULL;
1674 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001675 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001676 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001677 p = s + (p - res);
1678 vim_free(res);
1679 res = s;
1680 }
1681
1682 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001683 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001684 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001685 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001686 p += l; /* skip multi-byte character */
1687 else
1688# endif
1689 {
1690 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1691 p++;
1692 }
1693 }
1694
1695 return res;
1696}
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001697
1698/*
1699 * Make string "s" all lower-case and return it in allocated memory.
1700 * Handles multi-byte characters as well as possible.
1701 * Returns NULL when out of memory.
1702 */
1703 char_u *
1704strlow_save(char_u *orig)
1705{
1706 char_u *p;
1707 char_u *res;
1708
1709 res = p = vim_strsave(orig);
1710
1711 if (res != NULL)
1712 while (*p != NUL)
1713 {
1714# ifdef FEAT_MBYTE
1715 int l;
1716
1717 if (enc_utf8)
1718 {
1719 int c, lc;
1720 int newl;
1721 char_u *s;
1722
1723 c = utf_ptr2char(p);
Bram Moolenaare6640ad2017-12-22 21:06:56 +01001724 l = utf_ptr2len(p);
1725 if (c == 0)
1726 {
1727 /* overlong sequence, use only the first byte */
1728 c = *p;
1729 l = 1;
1730 }
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001731 lc = utf_tolower(c);
1732
1733 /* Reallocate string when byte count changes. This is rare,
1734 * thus it's OK to do another malloc()/free(). */
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001735 newl = utf_char2len(lc);
1736 if (newl != l)
1737 {
1738 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
1739 if (s == NULL)
1740 {
1741 vim_free(res);
1742 return NULL;
1743 }
1744 mch_memmove(s, res, p - res);
1745 STRCPY(s + (p - res) + newl, p + l);
1746 p = s + (p - res);
1747 vim_free(res);
1748 res = s;
1749 }
1750
1751 utf_char2bytes(lc, p);
1752 p += newl;
1753 }
1754 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1755 p += l; /* skip multi-byte character */
1756 else
1757# endif
1758 {
1759 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
1760 p++;
1761 }
1762 }
1763
1764 return res;
1765}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766#endif
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * delete spaces at the end of a string
1770 */
1771 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001772del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 char_u *q;
1775
1776 q = ptr + STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001777 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 *q = NUL;
1779}
1780
1781/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001782 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001783 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 */
1785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001786vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001788 STRNCPY(to, from, len);
1789 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790}
1791
1792/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001793 * Like strcat(), but make sure the result fits in "tosize" bytes and is
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001794 * always NUL terminated. "from" and "to" may overlap.
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001795 */
1796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001798{
1799 size_t tolen = STRLEN(to);
1800 size_t fromlen = STRLEN(from);
1801
1802 if (tolen + fromlen + 1 > tosize)
1803 {
1804 mch_memmove(to + tolen, from, tosize - tolen - 1);
1805 to[tosize - 1] = NUL;
1806 }
1807 else
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001808 mch_memmove(to + tolen, from, fromlen + 1);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001809}
1810
1811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 * Isolate one part of a string option where parts are separated with
1813 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001814 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 * "*option" is advanced to the next part.
1816 * The length is returned.
1817 */
1818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001819copy_option_part(
1820 char_u **option,
1821 char_u *buf,
1822 int maxlen,
1823 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 int len = 0;
1826 char_u *p = *option;
1827
1828 /* skip '.' at start of option part, for 'suffixes' */
1829 if (*p == '.')
1830 buf[len++] = *p++;
1831 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1832 {
1833 /*
1834 * Skip backslash before a separator character and space.
1835 */
1836 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1837 ++p;
1838 if (len < maxlen - 1)
1839 buf[len++] = *p;
1840 ++p;
1841 }
1842 buf[len] = NUL;
1843
1844 if (*p != NUL && *p != ',') /* skip non-standard separator */
1845 ++p;
1846 p = skip_to_option_part(p); /* p points to next file name */
1847
1848 *option = p;
1849 return len;
1850}
1851
1852/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001853 * Replacement for free() that ignores NULL pointers.
1854 * Also skip free() when exiting for sure, this helps when we caught a deadly
1855 * signal that was caused by a crash in free().
Bram Moolenaard23a8232018-02-10 18:45:26 +01001856 * If you want to set NULL after calling this function, you should use
1857 * VIM_CLEAR() instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 */
1859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001860vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001862 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864#ifdef MEM_PROFILE
1865 mem_pre_free(&x);
1866#endif
1867 free(x);
1868 }
1869}
1870
1871#ifndef HAVE_MEMSET
1872 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001873vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
1875 char *p = ptr;
1876
1877 while (size-- > 0)
1878 *p++ = c;
1879 return ptr;
1880}
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1884/*
1885 * Compare two strings, ignoring case, using current locale.
1886 * Doesn't work for multi-byte characters.
1887 * return 0 for match, < 0 for smaller, > 0 for bigger
1888 */
1889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001890vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 int i;
1893
1894 for (;;)
1895 {
1896 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1897 if (i != 0)
1898 return i; /* this character different */
1899 if (*s1 == NUL)
1900 break; /* strings match until NUL */
1901 ++s1;
1902 ++s2;
1903 }
1904 return 0; /* strings match */
1905}
1906#endif
1907
1908#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1909/*
1910 * Compare two strings, for length "len", ignoring case, using current locale.
1911 * Doesn't work for multi-byte characters.
1912 * return 0 for match, < 0 for smaller, > 0 for bigger
1913 */
1914 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001915vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 int i;
1918
1919 while (len > 0)
1920 {
1921 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1922 if (i != 0)
1923 return i; /* this character different */
1924 if (*s1 == NUL)
1925 break; /* strings match until NUL */
1926 ++s1;
1927 ++s2;
1928 --len;
1929 }
1930 return 0; /* strings match */
1931}
1932#endif
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934/*
1935 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001936 * with characters from 128 to 255 correctly. It also doesn't return a
1937 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
1939 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001940vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 char_u *p;
1943 int b;
1944
1945 p = string;
1946#ifdef FEAT_MBYTE
1947 if (enc_utf8 && c >= 0x80)
1948 {
1949 while (*p != NUL)
1950 {
Bram Moolenaarace95982017-03-29 17:30:27 +02001951 int l = utfc_ptr2len(p);
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001952
1953 /* Avoid matching an illegal byte here. */
1954 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001956 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 return NULL;
1959 }
1960 if (enc_dbcs != 0 && c > 255)
1961 {
1962 int n2 = c & 0xff;
1963
1964 c = ((unsigned)c >> 8) & 0xff;
1965 while ((b = *p) != NUL)
1966 {
1967 if (b == c && p[1] == n2)
1968 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001969 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 return NULL;
1972 }
1973 if (has_mbyte)
1974 {
1975 while ((b = *p) != NUL)
1976 {
1977 if (b == c)
1978 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001979 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 }
1981 return NULL;
1982 }
1983#endif
1984 while ((b = *p) != NUL)
1985 {
1986 if (b == c)
1987 return p;
1988 ++p;
1989 }
1990 return NULL;
1991}
1992
1993/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001994 * Version of strchr() that only works for bytes and handles unsigned char
1995 * strings with characters above 128 correctly. It also doesn't return a
1996 * pointer to the NUL at the end of the string.
1997 */
1998 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001999vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002000{
2001 char_u *p = string;
2002
2003 while (*p != NUL)
2004 {
2005 if (*p == c)
2006 return p;
2007 ++p;
2008 }
2009 return NULL;
2010}
2011
2012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002014 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00002015 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 */
2017 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002018vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
Bram Moolenaar05159a02005-02-26 23:04:13 +00002023 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002025 if (*p == c)
2026 retval = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002027 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 return retval;
2030}
2031
2032/*
2033 * Vim's version of strpbrk(), in case it's missing.
2034 * Don't generate a prototype for this, causes problems when it's not used.
2035 */
2036#ifndef PROTO
2037# ifndef HAVE_STRPBRK
2038# ifdef vim_strpbrk
2039# undef vim_strpbrk
2040# endif
2041 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002042vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 while (*s)
2045 {
2046 if (vim_strchr(charset, *s) != NULL)
2047 return s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002048 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050 return NULL;
2051}
2052# endif
2053#endif
2054
2055/*
2056 * Vim has its own isspace() function, because on some machines isspace()
2057 * can't handle characters above 128.
2058 */
2059 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002060vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
2062 return ((x >= 9 && x <= 13) || x == ' ');
2063}
2064
2065/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002066 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 */
2068
2069/*
2070 * Clear an allocated growing array.
2071 */
2072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002073ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 vim_free(gap->ga_data);
2076 ga_init(gap);
2077}
2078
2079/*
2080 * Clear a growing array that contains a list of strings.
2081 */
2082 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002083ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 int i;
2086
2087 for (i = 0; i < gap->ga_len; ++i)
2088 vim_free(((char_u **)(gap->ga_data))[i]);
2089 ga_clear(gap);
2090}
2091
2092/*
2093 * Initialize a growing array. Don't forget to set ga_itemsize and
2094 * ga_growsize! Or use ga_init2().
2095 */
2096 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002097ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002100 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 gap->ga_len = 0;
2102}
2103
2104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002105ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
2107 ga_init(gap);
2108 gap->ga_itemsize = itemsize;
2109 gap->ga_growsize = growsize;
2110}
2111
2112/*
2113 * Make room in growing array "gap" for at least "n" items.
2114 * Return FAIL for failure, OK otherwise.
2115 */
2116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002117ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002119 size_t old_len;
2120 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 char_u *pp;
2122
Bram Moolenaar86b68352004-12-27 21:59:20 +00002123 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
2125 if (n < gap->ga_growsize)
2126 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002127 new_len = gap->ga_itemsize * (gap->ga_len + n);
2128 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002129 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if (pp == NULL)
2131 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002132 old_len = gap->ga_itemsize * gap->ga_maxlen;
2133 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002134 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 gap->ga_data = pp;
2136 }
2137 return OK;
2138}
2139
Bram Moolenaar113e1072019-01-20 15:30:40 +01002140#if defined(FEAT_EVAL) || defined(FEAT_SEARCHPATH) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002142 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002143 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002144 * Returns NULL when out of memory.
2145 */
2146 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002148{
2149 int i;
2150 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002151 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002152 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002153 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002154
2155 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002156 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002157
2158 s = alloc(len + 1);
2159 if (s != NULL)
2160 {
2161 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002162 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002163 for (i = 0; i < gap->ga_len; ++i)
2164 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002165 if (p != s)
2166 {
2167 STRCPY(p, sep);
2168 p += sep_len;
2169 }
2170 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2171 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002172 }
2173 }
2174 return s;
2175}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002176#endif
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002177
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01002178#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002179/*
2180 * Make a copy of string "p" and add it to "gap".
2181 * When out of memory nothing changes.
2182 */
2183 void
2184ga_add_string(garray_T *gap, char_u *p)
2185{
2186 char_u *cp = vim_strsave(p);
2187
2188 if (cp != NULL)
2189 {
2190 if (ga_grow(gap, 1) == OK)
2191 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2192 else
2193 vim_free(cp);
2194 }
2195}
2196#endif
2197
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002200 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 * Note: Does NOT copy the NUL at the end!
2202 */
2203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002204ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
Bram Moolenaar43345542015-11-29 17:35:35 +01002206 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
Bram Moolenaar478af672017-04-10 22:22:42 +02002208 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01002209 return;
2210 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (ga_grow(gap, len) == OK)
2212 {
2213 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2214 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216}
2217
2218/*
2219 * Append one byte to a growarray which contains bytes.
2220 */
2221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002222ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 if (ga_grow(gap, 1) == OK)
2225 {
2226 *((char *)gap->ga_data + gap->ga_len) = c;
2227 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229}
2230
Bram Moolenaar597a4222014-06-25 14:39:50 +02002231#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2232 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002233/*
2234 * Append the text in "gap" below the cursor line and clear "gap".
2235 */
2236 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002237append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002238{
2239 /* Remove trailing CR. */
2240 if (gap->ga_len > 0
2241 && !curbuf->b_p_bin
2242 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2243 --gap->ga_len;
2244 ga_append(gap, NUL);
2245 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2246 gap->ga_len = 0;
2247}
2248#endif
2249
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250/************************************************************************
2251 * functions that use lookup tables for various things, generally to do with
2252 * special key codes.
2253 */
2254
2255/*
2256 * Some useful tables.
2257 */
2258
2259static struct modmasktable
2260{
2261 short mod_mask; /* Bit-mask for particular key modifier */
2262 short mod_flag; /* Bit(s) for particular key modifier */
2263 char_u name; /* Single letter name of modifier */
2264} mod_mask_table[] =
2265{
2266 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002267 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2269 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2270 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2271 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2272 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02002273#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2275#endif
2276 /* 'A' must be the last one */
2277 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2278 {0, 0, NUL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002279 /* NOTE: when adding an entry, update MAX_KEY_NAME_LEN! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280};
2281
2282/*
2283 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002284 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 */
2286#define MOD_KEYS_ENTRY_SIZE 5
2287
2288static char_u modifier_keys_table[] =
2289{
2290/* mod mask with modifier without modifier */
2291 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2292 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2293 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2294 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2295 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2296 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2297 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2298 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2299 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2300 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2301 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2302 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2303 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2304 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2305 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2306 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2307 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2308 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2309 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2310 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2311 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2312 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2313 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2314 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2315 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2316 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2317 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2318 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2319 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2320 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2321 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2322 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2323 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2324
2325 /* vt100 F1 */
2326 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2327 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2328 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2329 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2330
2331 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2332 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2333 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2334 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2335 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2336 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2337 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2338 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2339 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2340 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2341
2342 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2343 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2344 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2345 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2346 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2347 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2348 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2349 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2350 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2351 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2352
2353 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2354 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2355 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2356 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2357 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2358 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2359 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2360 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2361 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2362 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2363
2364 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2365 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2366 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2367 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2368 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2369 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2370 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2371
2372 /* TAB pseudo code*/
2373 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2374
2375 NUL
2376};
2377
2378static struct key_name_entry
2379{
2380 int key; /* Special key code or ascii value */
2381 char_u *name; /* Name of key */
2382} key_names_table[] =
2383{
2384 {' ', (char_u *)"Space"},
2385 {TAB, (char_u *)"Tab"},
2386 {K_TAB, (char_u *)"Tab"},
2387 {NL, (char_u *)"NL"},
2388 {NL, (char_u *)"NewLine"}, /* Alternative name */
2389 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2390 {NL, (char_u *)"LF"}, /* Alternative name */
2391 {CAR, (char_u *)"CR"},
2392 {CAR, (char_u *)"Return"}, /* Alternative name */
2393 {CAR, (char_u *)"Enter"}, /* Alternative name */
2394 {K_BS, (char_u *)"BS"},
2395 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2396 {ESC, (char_u *)"Esc"},
2397 {CSI, (char_u *)"CSI"},
2398 {K_CSI, (char_u *)"xCSI"},
2399 {'|', (char_u *)"Bar"},
2400 {'\\', (char_u *)"Bslash"},
2401 {K_DEL, (char_u *)"Del"},
2402 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2403 {K_KDEL, (char_u *)"kDel"},
2404 {K_UP, (char_u *)"Up"},
2405 {K_DOWN, (char_u *)"Down"},
2406 {K_LEFT, (char_u *)"Left"},
2407 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002408 {K_XUP, (char_u *)"xUp"},
2409 {K_XDOWN, (char_u *)"xDown"},
2410 {K_XLEFT, (char_u *)"xLeft"},
2411 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01002412 {K_PS, (char_u *)"PasteStart"},
2413 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 {K_F1, (char_u *)"F1"},
2416 {K_F2, (char_u *)"F2"},
2417 {K_F3, (char_u *)"F3"},
2418 {K_F4, (char_u *)"F4"},
2419 {K_F5, (char_u *)"F5"},
2420 {K_F6, (char_u *)"F6"},
2421 {K_F7, (char_u *)"F7"},
2422 {K_F8, (char_u *)"F8"},
2423 {K_F9, (char_u *)"F9"},
2424 {K_F10, (char_u *)"F10"},
2425
2426 {K_F11, (char_u *)"F11"},
2427 {K_F12, (char_u *)"F12"},
2428 {K_F13, (char_u *)"F13"},
2429 {K_F14, (char_u *)"F14"},
2430 {K_F15, (char_u *)"F15"},
2431 {K_F16, (char_u *)"F16"},
2432 {K_F17, (char_u *)"F17"},
2433 {K_F18, (char_u *)"F18"},
2434 {K_F19, (char_u *)"F19"},
2435 {K_F20, (char_u *)"F20"},
2436
2437 {K_F21, (char_u *)"F21"},
2438 {K_F22, (char_u *)"F22"},
2439 {K_F23, (char_u *)"F23"},
2440 {K_F24, (char_u *)"F24"},
2441 {K_F25, (char_u *)"F25"},
2442 {K_F26, (char_u *)"F26"},
2443 {K_F27, (char_u *)"F27"},
2444 {K_F28, (char_u *)"F28"},
2445 {K_F29, (char_u *)"F29"},
2446 {K_F30, (char_u *)"F30"},
2447
2448 {K_F31, (char_u *)"F31"},
2449 {K_F32, (char_u *)"F32"},
2450 {K_F33, (char_u *)"F33"},
2451 {K_F34, (char_u *)"F34"},
2452 {K_F35, (char_u *)"F35"},
2453 {K_F36, (char_u *)"F36"},
2454 {K_F37, (char_u *)"F37"},
2455
2456 {K_XF1, (char_u *)"xF1"},
2457 {K_XF2, (char_u *)"xF2"},
2458 {K_XF3, (char_u *)"xF3"},
2459 {K_XF4, (char_u *)"xF4"},
2460
2461 {K_HELP, (char_u *)"Help"},
2462 {K_UNDO, (char_u *)"Undo"},
2463 {K_INS, (char_u *)"Insert"},
2464 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2465 {K_KINS, (char_u *)"kInsert"},
2466 {K_HOME, (char_u *)"Home"},
2467 {K_KHOME, (char_u *)"kHome"},
2468 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002469 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {K_END, (char_u *)"End"},
2471 {K_KEND, (char_u *)"kEnd"},
2472 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002473 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {K_PAGEUP, (char_u *)"PageUp"},
2475 {K_PAGEDOWN, (char_u *)"PageDown"},
2476 {K_KPAGEUP, (char_u *)"kPageUp"},
2477 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2478
2479 {K_KPLUS, (char_u *)"kPlus"},
2480 {K_KMINUS, (char_u *)"kMinus"},
2481 {K_KDIVIDE, (char_u *)"kDivide"},
2482 {K_KMULTIPLY, (char_u *)"kMultiply"},
2483 {K_KENTER, (char_u *)"kEnter"},
2484 {K_KPOINT, (char_u *)"kPoint"},
2485
2486 {K_K0, (char_u *)"k0"},
2487 {K_K1, (char_u *)"k1"},
2488 {K_K2, (char_u *)"k2"},
2489 {K_K3, (char_u *)"k3"},
2490 {K_K4, (char_u *)"k4"},
2491 {K_K5, (char_u *)"k5"},
2492 {K_K6, (char_u *)"k6"},
2493 {K_K7, (char_u *)"k7"},
2494 {K_K8, (char_u *)"k8"},
2495 {K_K9, (char_u *)"k9"},
2496
2497 {'<', (char_u *)"lt"},
2498
2499 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002500#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002502#endif
2503#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002505#endif
2506#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002508#endif
2509#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002511#endif
2512#ifdef FEAT_MOUSE_URXVT
2513 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2514#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002515#ifdef FEAT_MOUSE_SGR
2516 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaara529ce02017-06-22 22:37:57 +02002517 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2520 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2521 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2522 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2523 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002524 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2526 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2527 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2528 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2529 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2530 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002531 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2532 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2533 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2534 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2535 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2536 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {K_X1MOUSE, (char_u *)"X1Mouse"},
2538 {K_X1DRAG, (char_u *)"X1Drag"},
2539 {K_X1RELEASE, (char_u *)"X1Release"},
2540 {K_X2MOUSE, (char_u *)"X2Mouse"},
2541 {K_X2DRAG, (char_u *)"X2Drag"},
2542 {K_X2RELEASE, (char_u *)"X2Release"},
2543 {K_DROP, (char_u *)"Drop"},
2544 {K_ZERO, (char_u *)"Nul"},
2545#ifdef FEAT_EVAL
2546 {K_SNR, (char_u *)"SNR"},
2547#endif
2548 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002549 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {0, NULL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002551 /* NOTE: When adding a long name update MAX_KEY_NAME_LEN. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552};
2553
2554#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2555
2556#ifdef FEAT_MOUSE
2557static struct mousetable
2558{
2559 int pseudo_code; /* Code for pseudo mouse event */
2560 int button; /* Which mouse button is it? */
2561 int is_click; /* Is it a mouse button click event? */
2562 int is_drag; /* Is it a mouse drag event? */
2563} mouse_table[] =
2564{
2565 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2566#ifdef FEAT_GUI
2567 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2568#endif
2569 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2570 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2571#ifdef FEAT_GUI
2572 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2573#endif
2574 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2575 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2576 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2577 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2578 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2579 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2580 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2581 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2582 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2583 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2584 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2585 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2586 /* DRAG without CLICK */
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002587 {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 /* RELEASE without CLICK */
2589 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2590 {0, 0, 0, 0},
2591};
2592#endif /* FEAT_MOUSE */
2593
2594/*
2595 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2596 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2597 */
2598 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002599name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600{
2601 int i;
2602
2603 c = TOUPPER_ASC(c);
2604 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2605 if (c == mod_mask_table[i].name)
2606 return mod_mask_table[i].mod_flag;
2607 return 0;
2608}
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610/*
2611 * Check if if there is a special key code for "key" that includes the
2612 * modifiers specified.
2613 */
2614 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002615simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 int i;
2618 int key0;
2619 int key1;
2620
2621 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2622 {
2623 /* TAB is a special case */
2624 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2625 {
2626 *modifiers &= ~MOD_MASK_SHIFT;
2627 return K_S_TAB;
2628 }
2629 key0 = KEY2TERMCAP0(key);
2630 key1 = KEY2TERMCAP1(key);
2631 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2632 if (key0 == modifier_keys_table[i + 3]
2633 && key1 == modifier_keys_table[i + 4]
2634 && (*modifiers & modifier_keys_table[i]))
2635 {
2636 *modifiers &= ~modifier_keys_table[i];
2637 return TERMCAP2KEY(modifier_keys_table[i + 1],
2638 modifier_keys_table[i + 2]);
2639 }
2640 }
2641 return key;
2642}
2643
2644/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002645 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002646 */
2647 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002648handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002649{
2650 switch (key)
2651 {
2652 case K_XUP: return K_UP;
2653 case K_XDOWN: return K_DOWN;
2654 case K_XLEFT: return K_LEFT;
2655 case K_XRIGHT: return K_RIGHT;
2656 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002657 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002658 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002659 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002660 case K_XF1: return K_F1;
2661 case K_XF2: return K_F2;
2662 case K_XF3: return K_F3;
2663 case K_XF4: return K_F4;
2664 case K_S_XF1: return K_S_F1;
2665 case K_S_XF2: return K_S_F2;
2666 case K_S_XF3: return K_S_F3;
2667 case K_S_XF4: return K_S_F4;
2668 }
2669 return key;
2670}
2671
2672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 * Return a string which contains the name of the given key when the given
2674 * modifiers are down.
2675 */
2676 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002677get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 static char_u string[MAX_KEY_NAME_LEN + 1];
2680
2681 int i, idx;
2682 int table_idx;
2683 char_u *s;
2684
2685 string[0] = '<';
2686 idx = 1;
2687
2688 /* Key that stands for a normal character. */
2689 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2690 c = KEY2TERMCAP1(c);
2691
2692 /*
2693 * Translate shifted special keys into unshifted keys and set modifier.
2694 * Same for CTRL and ALT modifiers.
2695 */
2696 if (IS_SPECIAL(c))
2697 {
2698 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2699 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2700 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2701 {
2702 modifiers |= modifier_keys_table[i];
2703 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2704 modifier_keys_table[i + 4]);
2705 break;
2706 }
2707 }
2708
2709 /* try to find the key in the special key table */
2710 table_idx = find_special_key_in_table(c);
2711
2712 /*
2713 * When not a known special key, and not a printable character, try to
2714 * extract modifiers.
2715 */
2716 if (c > 0
2717#ifdef FEAT_MBYTE
2718 && (*mb_char2len)(c) == 1
2719#endif
2720 )
2721 {
2722 if (table_idx < 0
2723 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2724 && (c & 0x80))
2725 {
2726 c &= 0x7f;
2727 modifiers |= MOD_MASK_ALT;
2728 /* try again, to find the un-alted key in the special key table */
2729 table_idx = find_special_key_in_table(c);
2730 }
2731 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2732 {
2733#ifdef EBCDIC
2734 c = CtrlChar(c);
2735#else
2736 c += '@';
2737#endif
2738 modifiers |= MOD_MASK_CTRL;
2739 }
2740 }
2741
2742 /* translate the modifier into a string */
2743 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2744 if ((modifiers & mod_mask_table[i].mod_mask)
2745 == mod_mask_table[i].mod_flag)
2746 {
2747 string[idx++] = mod_mask_table[i].name;
2748 string[idx++] = (char_u)'-';
2749 }
2750
2751 if (table_idx < 0) /* unknown special key, may output t_xx */
2752 {
2753 if (IS_SPECIAL(c))
2754 {
2755 string[idx++] = 't';
2756 string[idx++] = '_';
2757 string[idx++] = KEY2TERMCAP0(c);
2758 string[idx++] = KEY2TERMCAP1(c);
2759 }
2760 /* Not a special key, only modifiers, output directly */
2761 else
2762 {
2763#ifdef FEAT_MBYTE
2764 if (has_mbyte && (*mb_char2len)(c) > 1)
2765 idx += (*mb_char2bytes)(c, string + idx);
2766 else
2767#endif
2768 if (vim_isprintc(c))
2769 string[idx++] = c;
2770 else
2771 {
2772 s = transchar(c);
2773 while (*s)
2774 string[idx++] = *s++;
2775 }
2776 }
2777 }
2778 else /* use name of special key */
2779 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002780 size_t len = STRLEN(key_names_table[table_idx].name);
2781
2782 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2783 {
2784 STRCPY(string + idx, key_names_table[table_idx].name);
2785 idx += (int)len;
2786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 }
2788 string[idx++] = '>';
2789 string[idx] = NUL;
2790 return string;
2791}
2792
2793/*
2794 * Try translating a <> name at (*srcp)[] to dst[].
2795 * Return the number of characters added to dst[], zero for no match.
2796 * If there is a match, srcp is advanced to after the <> name.
2797 * dst[] must be big enough to hold the result (up to six characters)!
2798 */
2799 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002800trans_special(
2801 char_u **srcp,
2802 char_u *dst,
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002803 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2804 int in_string) /* TRUE when inside a double quoted string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
2806 int modifiers = 0;
2807 int key;
2808 int dlen = 0;
2809
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002810 key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (key == 0)
2812 return 0;
2813
2814 /* Put the appropriate modifier in a string */
2815 if (modifiers != 0)
2816 {
2817 dst[dlen++] = K_SPECIAL;
2818 dst[dlen++] = KS_MODIFIER;
2819 dst[dlen++] = modifiers;
2820 }
2821
2822 if (IS_SPECIAL(key))
2823 {
2824 dst[dlen++] = K_SPECIAL;
2825 dst[dlen++] = KEY2TERMCAP0(key);
2826 dst[dlen++] = KEY2TERMCAP1(key);
2827 }
2828#ifdef FEAT_MBYTE
2829 else if (has_mbyte && !keycode)
2830 dlen += (*mb_char2bytes)(key, dst + dlen);
2831#endif
2832 else if (keycode)
2833 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2834 else
2835 dst[dlen++] = key;
2836
2837 return dlen;
2838}
2839
2840/*
2841 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2842 * srcp is advanced to after the <> name.
2843 * returns 0 if there is no match.
2844 */
2845 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002846find_special_key(
2847 char_u **srcp,
2848 int *modp,
2849 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002850 int keep_x_key, /* don't translate xHome to Home key */
2851 int in_string) /* TRUE in string, double quote is escaped */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 char_u *last_dash;
2854 char_u *end_of_name;
2855 char_u *src;
2856 char_u *bp;
2857 int modifiers;
2858 int bit;
2859 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002860 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002861 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 src = *srcp;
2864 if (src[0] != '<')
2865 return 0;
2866
2867 /* Find end of modifier list */
2868 last_dash = src;
2869 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2870 {
2871 if (*bp == '-')
2872 {
2873 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002874 if (bp[1] != NUL)
2875 {
2876#ifdef FEAT_MBYTE
2877 if (has_mbyte)
2878 l = mb_ptr2len(bp + 1);
2879 else
2880#endif
2881 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002882 /* Anything accepted, like <C-?>.
2883 * <C-"> or <M-"> are not special in strings as " is
2884 * the string delimiter. With a backslash it works: <M-\"> */
2885 if (!(in_string && bp[1] == '"') && bp[2] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002886 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002887 else if (in_string && bp[1] == '\\' && bp[2] == '"'
2888 && bp[3] == '>')
2889 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2893 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002894 else if (STRNICMP(bp, "char-", 5) == 0)
2895 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002896 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002897 bp += l + 5;
2898 break;
2899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
2901
2902 if (*bp == '>') /* found matching '>' */
2903 {
2904 end_of_name = bp + 1;
2905
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 /* Which modifiers are given? */
2907 modifiers = 0x0;
2908 for (bp = src + 1; bp < last_dash; bp++)
2909 {
2910 if (*bp != '-')
2911 {
2912 bit = name_to_mod_mask(*bp);
2913 if (bit == 0x0)
2914 break; /* Illegal modifier name */
2915 modifiers |= bit;
2916 }
2917 }
2918
2919 /*
2920 * Legal modifier name.
2921 */
2922 if (bp >= last_dash)
2923 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002924 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2925 && VIM_ISDIGIT(last_dash[6]))
2926 {
2927 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002928 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002929 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002932 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002933 int off = 1;
2934
2935 /* Modifier with single letter, or special key name. */
2936 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2937 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002938#ifdef FEAT_MBYTE
2939 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002940 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002941 else
2942#endif
2943 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002944 if (modifiers != 0 && last_dash[l + off] == '>')
2945 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002946 else
2947 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002948 key = get_special_key_code(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002949 if (!keep_x_key)
2950 key = handle_x_keys(key);
2951 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 /*
2955 * get_special_key_code() may return NUL for invalid
2956 * special key name.
2957 */
2958 if (key != NUL)
2959 {
2960 /*
2961 * Only use a modifier when there is no special key code that
2962 * includes the modifier.
2963 */
2964 key = simplify_key(key, &modifiers);
2965
2966 if (!keycode)
2967 {
2968 /* don't want keycode, use single byte code */
2969 if (key == K_BS)
2970 key = BS;
2971 else if (key == K_DEL || key == K_KDEL)
2972 key = DEL;
2973 }
2974
2975 /*
2976 * Normal Key with modifier: Try to make a single byte code.
2977 */
2978 if (!IS_SPECIAL(key))
2979 key = extract_modifiers(key, &modifiers);
2980
2981 *modp = modifiers;
2982 *srcp = end_of_name;
2983 return key;
2984 }
2985 }
2986 }
2987 return 0;
2988}
2989
2990/*
2991 * Try to include modifiers in the key.
2992 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2993 */
2994 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002995extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 int modifiers = *modp;
2998
Bram Moolenaard0573012017-10-28 21:11:06 +02002999#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003000 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (!(modifiers & MOD_MASK_CMD))
3002#endif
3003 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
3004 {
3005 key = TOUPPER_ASC(key);
3006 modifiers &= ~MOD_MASK_SHIFT;
3007 }
3008 if ((modifiers & MOD_MASK_CTRL)
3009#ifdef EBCDIC
3010 /* * TODO: EBCDIC Better use:
3011 * && (Ctrl_chr(key) || key == '?')
3012 * ??? */
3013 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
3014 != NULL
3015#else
3016 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
3017#endif
3018 )
3019 {
3020 key = Ctrl_chr(key);
3021 modifiers &= ~MOD_MASK_CTRL;
3022 /* <C-@> is <Nul> */
3023 if (key == 0)
3024 key = K_ZERO;
3025 }
Bram Moolenaard0573012017-10-28 21:11:06 +02003026#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003027 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (!(modifiers & MOD_MASK_CMD))
3029#endif
3030 if ((modifiers & MOD_MASK_ALT) && key < 0x80
3031#ifdef FEAT_MBYTE
3032 && !enc_dbcs /* avoid creating a lead byte */
3033#endif
3034 )
3035 {
3036 key |= 0x80;
3037 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
3038 }
3039
3040 *modp = modifiers;
3041 return key;
3042}
3043
3044/*
3045 * Try to find key "c" in the special key table.
3046 * Return the index when found, -1 when not found.
3047 */
3048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003049find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 int i;
3052
3053 for (i = 0; key_names_table[i].name != NULL; i++)
3054 if (c == key_names_table[i].key)
3055 break;
3056 if (key_names_table[i].name == NULL)
3057 i = -1;
3058 return i;
3059}
3060
3061/*
3062 * Find the special key with the given name (the given string does not have to
3063 * end with NUL, the name is assumed to end before the first non-idchar).
3064 * If the name starts with "t_" the next two characters are interpreted as a
3065 * termcap name.
3066 * Return the key code, or 0 if not found.
3067 */
3068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003069get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 char_u *table_name;
3072 char_u string[3];
3073 int i, j;
3074
3075 /*
3076 * If it's <t_xx> we get the code for xx from the termcap
3077 */
3078 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3079 {
3080 string[0] = name[2];
3081 string[1] = name[3];
3082 string[2] = NUL;
3083 if (add_termcap_entry(string, FALSE) == OK)
3084 return TERMCAP2KEY(name[2], name[3]);
3085 }
3086 else
3087 for (i = 0; key_names_table[i].name != NULL; i++)
3088 {
3089 table_name = key_names_table[i].name;
3090 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3091 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3092 break;
3093 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3094 return key_names_table[i].key;
3095 }
3096 return 0;
3097}
3098
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003099#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003101get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003103 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 return NULL;
3105 return key_names_table[i].name;
3106}
3107#endif
3108
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003109#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110/*
3111 * Look up the given mouse code to return the relevant information in the other
3112 * arguments. Return which button is down or was released.
3113 */
3114 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003115get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 int i;
3118
3119 for (i = 0; mouse_table[i].pseudo_code; i++)
3120 if (code == mouse_table[i].pseudo_code)
3121 {
3122 *is_click = mouse_table[i].is_click;
3123 *is_drag = mouse_table[i].is_drag;
3124 return mouse_table[i].button;
3125 }
3126 return 0; /* Shouldn't get here */
3127}
3128
3129/*
3130 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3131 * the given information about which mouse button is down, and whether the
3132 * mouse was clicked, dragged or released.
3133 */
3134 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003135get_pseudo_mouse_code(
3136 int button, /* eg MOUSE_LEFT */
3137 int is_click,
3138 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 int i;
3141
3142 for (i = 0; mouse_table[i].pseudo_code; i++)
3143 if (button == mouse_table[i].button
3144 && is_click == mouse_table[i].is_click
3145 && is_drag == mouse_table[i].is_drag)
3146 {
3147#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003148 /* Trick: a non mappable left click and release has mouse_col -1
3149 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3150 * gui_mouse_moved() */
3151 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003153 if (mouse_col < 0)
3154 mouse_col = 0;
3155 else
3156 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3158 return (int)KE_LEFTMOUSE_NM;
3159 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3160 return (int)KE_LEFTRELEASE_NM;
3161 }
3162#endif
3163 return mouse_table[i].pseudo_code;
3164 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003165 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166}
3167#endif /* FEAT_MOUSE */
3168
3169/*
3170 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3171 */
3172 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003173get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
3175 int c = *buf->b_p_ff;
3176
3177 if (buf->b_p_bin || c == 'u')
3178 return EOL_UNIX;
3179 if (c == 'm')
3180 return EOL_MAC;
3181 return EOL_DOS;
3182}
3183
3184/*
3185 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3186 * argument.
3187 */
3188 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003189get_fileformat_force(
3190 buf_T *buf,
3191 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
3193 int c;
3194
3195 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003196 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 else
3198 {
3199 if ((eap != NULL && eap->force_bin != 0)
3200 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3201 return EOL_UNIX;
3202 c = *buf->b_p_ff;
3203 }
3204 if (c == 'u')
3205 return EOL_UNIX;
3206 if (c == 'm')
3207 return EOL_MAC;
3208 return EOL_DOS;
3209}
3210
3211/*
3212 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3213 * Sets both 'textmode' and 'fileformat'.
3214 * Note: Does _not_ set global value of 'textmode'!
3215 */
3216 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003217set_fileformat(
3218 int t,
3219 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221 char *p = NULL;
3222
3223 switch (t)
3224 {
3225 case EOL_DOS:
3226 p = FF_DOS;
3227 curbuf->b_p_tx = TRUE;
3228 break;
3229 case EOL_UNIX:
3230 p = FF_UNIX;
3231 curbuf->b_p_tx = FALSE;
3232 break;
3233 case EOL_MAC:
3234 p = FF_MAC;
3235 curbuf->b_p_tx = FALSE;
3236 break;
3237 }
3238 if (p != NULL)
3239 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003240 OPT_FREE | opt_flags, 0);
3241
Bram Moolenaarf740b292006-02-16 22:11:02 +00003242 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003244 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#ifdef FEAT_TITLE
3246 need_maketitle = TRUE; /* set window title later */
3247#endif
3248}
3249
3250/*
3251 * Return the default fileformat from 'fileformats'.
3252 */
3253 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 switch (*p_ffs)
3257 {
3258 case 'm': return EOL_MAC;
3259 case 'd': return EOL_DOS;
3260 }
3261 return EOL_UNIX;
3262}
3263
3264/*
3265 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3266 */
3267 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003268call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 char_u *ncmd;
3271 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003272#ifdef FEAT_PROFILE
3273 proftime_T wait_time;
3274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 if (p_verbose > 3)
3277 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003278 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003279 smsg(_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 cmd == NULL ? p_sh : cmd);
3281 out_char('\n');
3282 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003283 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285
Bram Moolenaar05159a02005-02-26 23:04:13 +00003286#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003287 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003288 prof_child_enter(&wait_time);
3289#endif
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (*p_sh == NUL)
3292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003293 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 retval = -1;
3295 }
3296 else
3297 {
3298#ifdef FEAT_GUI_MSWIN
3299 /* Don't hide the pointer while executing a shell command. */
3300 gui_mch_mousehide(FALSE);
3301#endif
3302#ifdef FEAT_GUI
3303 ++hold_gui_events;
3304#endif
3305 /* The external command may update a tags file, clear cached tags. */
3306 tag_freematch();
3307
3308 if (cmd == NULL || *p_sxq == NUL)
3309 retval = mch_call_shell(cmd, opt);
3310 else
3311 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003312 char_u *ecmd = cmd;
3313
3314 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3315 {
3316 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3317 if (ecmd == NULL)
3318 ecmd = cmd;
3319 }
3320 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (ncmd != NULL)
3322 {
3323 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003324 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003325 /* When 'shellxquote' is ( append ).
3326 * When 'shellxquote' is "( append )". */
3327 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3328 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3329 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 retval = mch_call_shell(ncmd, opt);
3331 vim_free(ncmd);
3332 }
3333 else
3334 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003335 if (ecmd != cmd)
3336 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338#ifdef FEAT_GUI
3339 --hold_gui_events;
3340#endif
3341 /*
3342 * Check the window size, in case it changed while executing the
3343 * external command.
3344 */
3345 shell_resized_check();
3346 }
3347
3348#ifdef FEAT_EVAL
3349 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003350# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003351 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003352 prof_child_exit(&wait_time);
3353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
3355
3356 return retval;
3357}
3358
3359/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003360 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3361 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
3363 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003364get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 if (State & NORMAL)
3367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003369 {
3370 if (VIsual_select)
3371 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003373 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003374 else if (finish_op)
3375 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 return State;
3378}
3379
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003380#if defined(FEAT_MBYTE) || defined(PROTO)
3381/*
3382 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003383 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003384 * "b" must point to the start of the file name
3385 */
3386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003387after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003388{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003389 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003390 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3391}
3392#endif
3393
3394/*
3395 * Return TRUE if file names "f1" and "f2" are in the same directory.
3396 * "f1" may be a short name, "f2" must be a full path.
3397 */
3398 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003399same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003400{
3401 char_u ffname[MAXPATHL];
3402 char_u *t1;
3403 char_u *t2;
3404
3405 /* safety check */
3406 if (f1 == NULL || f2 == NULL)
3407 return FALSE;
3408
3409 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3410 t1 = gettail_sep(ffname);
3411 t2 = gettail_sep(f2);
3412 return (t1 - ffname == t2 - f2
3413 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3414}
3415
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02003416#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
3417 || defined(MSWIN) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01003418 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 || defined(PROTO)
3420/*
3421 * Change to a file's directory.
3422 * Caller must call shorten_fnames()!
3423 * Return OK or FAIL.
3424 */
3425 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003426vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003428 char_u old_dir[MAXPATHL];
3429 char_u new_dir[MAXPATHL];
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003430 int res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003432 if (mch_dirname(old_dir, MAXPATHL) != OK)
3433 *old_dir = NUL;
3434
3435 vim_strncpy(new_dir, fname, MAXPATHL - 1);
3436 *gettail_sep(new_dir) = NUL;
3437
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01003438 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01003439 // nothing to do
3440 res = OK;
3441 else
3442 {
3443 res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
3444
3445 if (res == OK && trigger_autocmd != NULL)
3446 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
3447 new_dir, FALSE, curbuf);
3448 }
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01003449 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450}
3451#endif
3452
3453#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3454/*
3455 * Check if "name" ends in a slash and is not a directory.
3456 * Used for systems where stat() ignores a trailing slash on a file name.
3457 * The Vim code assumes a trailing slash is only ignored for a directory.
3458 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003459 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01003460illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 if (name[0] == NUL)
3463 return FALSE; /* no file name is not illegal */
3464 if (name[strlen(name) - 1] != '/')
3465 return FALSE; /* no trailing slash */
3466 if (mch_isdir((char_u *)name))
3467 return FALSE; /* trailing slash for a directory */
3468 return TRUE;
3469}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003470
3471/*
3472 * Special implementation of mch_stat() for Solaris.
3473 */
3474 int
3475vim_stat(const char *name, stat_T *stp)
3476{
3477 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
3478 * the name ends in "/" and it's not a directory. */
Bram Moolenaard8492792017-03-16 12:22:38 +01003479 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
3482
3483#if defined(CURSOR_SHAPE) || defined(PROTO)
3484
3485/*
3486 * Handling of cursor and mouse pointer shapes in various modes.
3487 */
3488
3489cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3490{
3491 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3492 * defaults when Vim starts.
3493 * Adjust the SHAPE_IDX_ defines when making changes! */
3494 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3495 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3496 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3497 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3498 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3499 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3500 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3501 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3502 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3503 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3504 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3505 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3506 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3507 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3508 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3509 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3510 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3511};
3512
3513#ifdef FEAT_MOUSESHAPE
3514/*
3515 * Table with names for mouse shapes. Keep in sync with all the tables for
3516 * mch_set_mouse_shape()!.
3517 */
3518static char * mshape_names[] =
3519{
3520 "arrow", /* default, must be the first one */
3521 "blank", /* hidden */
3522 "beam",
3523 "updown",
3524 "udsizing",
3525 "leftright",
3526 "lrsizing",
3527 "busy",
3528 "no",
3529 "crosshair",
3530 "hand1",
3531 "hand2",
3532 "pencil",
3533 "question",
3534 "rightup-arrow",
3535 "up-arrow",
3536 NULL
3537};
3538#endif
3539
3540/*
3541 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3542 * ("what" is SHAPE_MOUSE).
3543 * Returns error message for an illegal option, NULL otherwise.
3544 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003545 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003546parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
3548 char_u *modep;
3549 char_u *colonp;
3550 char_u *commap;
3551 char_u *slashp;
3552 char_u *p, *endp;
3553 int idx = 0; /* init for GCC */
3554 int all_idx;
3555 int len;
3556 int i;
3557 long n;
3558 int found_ve = FALSE; /* found "ve" flag */
3559 int round;
3560
3561 /*
3562 * First round: check for errors; second round: do it for real.
3563 */
3564 for (round = 1; round <= 2; ++round)
3565 {
3566 /*
3567 * Repeat for all comma separated parts.
3568 */
3569#ifdef FEAT_MOUSESHAPE
3570 if (what == SHAPE_MOUSE)
3571 modep = p_mouseshape;
3572 else
3573#endif
3574 modep = p_guicursor;
3575 while (*modep != NUL)
3576 {
3577 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01003578 commap = vim_strchr(modep, ',');
3579
3580 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003581 return N_("E545: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (colonp == modep)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003583 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
3585 /*
3586 * Repeat for all mode's before the colon.
3587 * For the 'a' mode, we loop to handle all the modes.
3588 */
3589 all_idx = -1;
3590 while (modep < colonp || all_idx >= 0)
3591 {
3592 if (all_idx < 0)
3593 {
3594 /* Find the mode. */
3595 if (modep[1] == '-' || modep[1] == ':')
3596 len = 1;
3597 else
3598 len = 2;
3599 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3600 all_idx = SHAPE_IDX_COUNT - 1;
3601 else
3602 {
3603 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3604 if (STRNICMP(modep, shape_table[idx].name, len)
3605 == 0)
3606 break;
3607 if (idx == SHAPE_IDX_COUNT
3608 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003609 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3611 found_ve = TRUE;
3612 }
3613 modep += len + 1;
3614 }
3615
3616 if (all_idx >= 0)
3617 idx = all_idx--;
3618 else if (round == 2)
3619 {
3620#ifdef FEAT_MOUSESHAPE
3621 if (what == SHAPE_MOUSE)
3622 {
3623 /* Set the default, for the missing parts */
3624 shape_table[idx].mshape = 0;
3625 }
3626 else
3627#endif
3628 {
3629 /* Set the defaults, for the missing parts */
3630 shape_table[idx].shape = SHAPE_BLOCK;
3631 shape_table[idx].blinkwait = 700L;
3632 shape_table[idx].blinkon = 400L;
3633 shape_table[idx].blinkoff = 250L;
3634 }
3635 }
3636
3637 /* Parse the part after the colon */
3638 for (p = colonp + 1; *p && *p != ','; )
3639 {
3640#ifdef FEAT_MOUSESHAPE
3641 if (what == SHAPE_MOUSE)
3642 {
3643 for (i = 0; ; ++i)
3644 {
3645 if (mshape_names[i] == NULL)
3646 {
3647 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 return N_("E547: Illegal mouseshape");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (round == 2)
3650 shape_table[idx].mshape =
3651 getdigits(&p) + MSHAPE_NUMBERED;
3652 else
3653 (void)getdigits(&p);
3654 break;
3655 }
3656 len = (int)STRLEN(mshape_names[i]);
3657 if (STRNICMP(p, mshape_names[i], len) == 0)
3658 {
3659 if (round == 2)
3660 shape_table[idx].mshape = i;
3661 p += len;
3662 break;
3663 }
3664 }
3665 }
3666 else /* if (what == SHAPE_MOUSE) */
3667#endif
3668 {
3669 /*
3670 * First handle the ones with a number argument.
3671 */
3672 i = *p;
3673 len = 0;
3674 if (STRNICMP(p, "ver", 3) == 0)
3675 len = 3;
3676 else if (STRNICMP(p, "hor", 3) == 0)
3677 len = 3;
3678 else if (STRNICMP(p, "blinkwait", 9) == 0)
3679 len = 9;
3680 else if (STRNICMP(p, "blinkon", 7) == 0)
3681 len = 7;
3682 else if (STRNICMP(p, "blinkoff", 8) == 0)
3683 len = 8;
3684 if (len != 0)
3685 {
3686 p += len;
3687 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003688 return N_("E548: digit expected");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 n = getdigits(&p);
3690 if (len == 3) /* "ver" or "hor" */
3691 {
3692 if (n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003693 return N_("E549: Illegal percentage");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 if (round == 2)
3695 {
3696 if (TOLOWER_ASC(i) == 'v')
3697 shape_table[idx].shape = SHAPE_VER;
3698 else
3699 shape_table[idx].shape = SHAPE_HOR;
3700 shape_table[idx].percentage = n;
3701 }
3702 }
3703 else if (round == 2)
3704 {
3705 if (len == 9)
3706 shape_table[idx].blinkwait = n;
3707 else if (len == 7)
3708 shape_table[idx].blinkon = n;
3709 else
3710 shape_table[idx].blinkoff = n;
3711 }
3712 }
3713 else if (STRNICMP(p, "block", 5) == 0)
3714 {
3715 if (round == 2)
3716 shape_table[idx].shape = SHAPE_BLOCK;
3717 p += 5;
3718 }
3719 else /* must be a highlight group name then */
3720 {
3721 endp = vim_strchr(p, '-');
3722 if (commap == NULL) /* last part */
3723 {
3724 if (endp == NULL)
3725 endp = p + STRLEN(p); /* find end of part */
3726 }
3727 else if (endp > commap || endp == NULL)
3728 endp = commap;
3729 slashp = vim_strchr(p, '/');
3730 if (slashp != NULL && slashp < endp)
3731 {
3732 /* "group/langmap_group" */
3733 i = syn_check_group(p, (int)(slashp - p));
3734 p = slashp + 1;
3735 }
3736 if (round == 2)
3737 {
3738 shape_table[idx].id = syn_check_group(p,
3739 (int)(endp - p));
3740 shape_table[idx].id_lm = shape_table[idx].id;
3741 if (slashp != NULL && slashp < endp)
3742 shape_table[idx].id = i;
3743 }
3744 p = endp;
3745 }
3746 } /* if (what != SHAPE_MOUSE) */
3747
3748 if (*p == '-')
3749 ++p;
3750 }
3751 }
3752 modep = p;
3753 if (*modep == ',')
3754 ++modep;
3755 }
3756 }
3757
3758 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3759 if (!found_ve)
3760 {
3761#ifdef FEAT_MOUSESHAPE
3762 if (what == SHAPE_MOUSE)
3763 {
3764 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3765 }
3766 else
3767#endif
3768 {
3769 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3770 shape_table[SHAPE_IDX_VE].percentage =
3771 shape_table[SHAPE_IDX_V].percentage;
3772 shape_table[SHAPE_IDX_VE].blinkwait =
3773 shape_table[SHAPE_IDX_V].blinkwait;
3774 shape_table[SHAPE_IDX_VE].blinkon =
3775 shape_table[SHAPE_IDX_V].blinkon;
3776 shape_table[SHAPE_IDX_VE].blinkoff =
3777 shape_table[SHAPE_IDX_V].blinkoff;
3778 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3779 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3780 }
3781 }
3782
3783 return NULL;
3784}
3785
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003786# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3787 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788/*
3789 * Return the index into shape_table[] for the current mode.
3790 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3791 */
3792 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003793get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
3795#ifdef FEAT_MOUSESHAPE
3796 if (mouse && (State == HITRETURN || State == ASKMORE))
3797 {
3798# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003799 int x, y;
3800 gui_mch_getmouse(&x, &y);
3801 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return SHAPE_IDX_MOREL;
3803# endif
3804 return SHAPE_IDX_MORE;
3805 }
3806 if (mouse && drag_status_line)
3807 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (mouse && drag_sep_line)
3809 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810#endif
3811 if (!mouse && State == SHOWMATCH)
3812 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (State & VREPLACE_FLAG)
3814 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (State & REPLACE_FLAG)
3816 return SHAPE_IDX_R;
3817 if (State & INSERT)
3818 return SHAPE_IDX_I;
3819 if (State & CMDLINE)
3820 {
3821 if (cmdline_at_end())
3822 return SHAPE_IDX_C;
3823 if (cmdline_overstrike())
3824 return SHAPE_IDX_CR;
3825 return SHAPE_IDX_CI;
3826 }
3827 if (finish_op)
3828 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (VIsual_active)
3830 {
3831 if (*p_sel == 'e')
3832 return SHAPE_IDX_VE;
3833 else
3834 return SHAPE_IDX_V;
3835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return SHAPE_IDX_N;
3837}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
3840# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3841static int old_mouse_shape = 0;
3842
3843/*
3844 * Set the mouse shape:
3845 * If "shape" is -1, use shape depending on the current mode,
3846 * depending on the current state.
3847 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3848 * when the mouse moves off the status or command line).
3849 */
3850 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003851update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
3853 int new_mouse_shape;
3854
3855 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003856 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return;
3858
3859 /* Postpone the updating when more is to come. Speeds up executing of
3860 * mappings. */
3861 if (shape_idx == -1 && char_avail())
3862 {
3863 postponed_mouseshape = TRUE;
3864 return;
3865 }
3866
Bram Moolenaar14716812006-05-04 21:54:08 +00003867 /* When ignoring the mouse don't change shape on the statusline. */
3868 if (*p_mouse == NUL
3869 && (shape_idx == SHAPE_IDX_CLINE
3870 || shape_idx == SHAPE_IDX_STATUS
3871 || shape_idx == SHAPE_IDX_VSEP))
3872 shape_idx = -2;
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (shape_idx == -2
3875 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3876 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3877 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3878 return;
3879 if (shape_idx < 0)
3880 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3881 else
3882 new_mouse_shape = shape_table[shape_idx].mshape;
3883 if (new_mouse_shape != old_mouse_shape)
3884 {
3885 mch_set_mouse_shape(new_mouse_shape);
3886 old_mouse_shape = new_mouse_shape;
3887 }
3888 postponed_mouseshape = FALSE;
3889}
3890# endif
3891
3892#endif /* CURSOR_SHAPE */
3893
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895/* TODO: make some #ifdef for this */
3896/*--------[ file searching ]-------------------------------------------------*/
3897/*
3898 * File searching functions for 'path', 'tags' and 'cdpath' options.
3899 * External visible functions:
3900 * vim_findfile_init() creates/initialises the search context
3901 * vim_findfile_free_visited() free list of visited files/dirs of search
3902 * context
3903 * vim_findfile() find a file in the search context
3904 * vim_findfile_cleanup() cleanup/free search context created by
3905 * vim_findfile_init()
3906 *
3907 * All static functions and variables start with 'ff_'
3908 *
3909 * In general it works like this:
3910 * First you create yourself a search context by calling vim_findfile_init().
3911 * It is possible to give a search context from a previous call to
3912 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3913 * until you are satisfied with the result or it returns NULL. On every call it
3914 * returns the next file which matches the conditions given to
3915 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3916 *
3917 * It is possible to call vim_findfile_init() again to reinitialise your search
3918 * with some new parameters. Don't forget to pass your old search context to
3919 * it, so it can reuse it and especially reuse the list of already visited
3920 * directories. If you want to delete the list of already visited directories
3921 * simply call vim_findfile_free_visited().
3922 *
3923 * When you are done call vim_findfile_cleanup() to free the search context.
3924 *
3925 * The function vim_findfile_init() has a long comment, which describes the
3926 * needed parameters.
3927 *
3928 *
3929 *
3930 * ATTENTION:
3931 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003932 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * thread-safe!!!!!
3934 *
3935 * To minimize parameter passing (or because I'm to lazy), only the
3936 * external visible functions get a search context as a parameter. This is
3937 * then assigned to a static global, which is used throughout the local
3938 * functions.
3939 */
3940
3941/*
3942 * type for the directory search stack
3943 */
3944typedef struct ff_stack
3945{
3946 struct ff_stack *ffs_prev;
3947
3948 /* the fix part (no wildcards) and the part containing the wildcards
3949 * of the search path
3950 */
3951 char_u *ffs_fix_path;
3952#ifdef FEAT_PATH_EXTRA
3953 char_u *ffs_wc_path;
3954#endif
3955
3956 /* files/dirs found in the above directory, matched by the first wildcard
3957 * of wc_part
3958 */
3959 char_u **ffs_filearray;
3960 int ffs_filearray_size;
3961 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3962
3963 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003964 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 int ffs_stage;
3968
3969 /* How deep are we in the directory tree?
3970 * Counts backward from value of level parameter to vim_findfile_init
3971 */
3972 int ffs_level;
3973
3974 /* Did we already expand '**' to an empty string? */
3975 int ffs_star_star_empty;
3976} ff_stack_T;
3977
3978/*
3979 * type for already visited directories or files.
3980 */
3981typedef struct ff_visited
3982{
3983 struct ff_visited *ffv_next;
3984
3985#ifdef FEAT_PATH_EXTRA
3986 /* Visited directories are different if the wildcard string are
3987 * different. So we have to save it.
3988 */
3989 char_u *ffv_wc_path;
3990#endif
3991 /* for unix use inode etc for comparison (needed because of links), else
3992 * use filename.
3993 */
3994#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003995 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3996 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 ino_t ffv_ino; /* inode number */
3998#endif
3999 /* The memory for this struct is allocated according to the length of
4000 * ffv_fname.
4001 */
4002 char_u ffv_fname[1]; /* actually longer */
4003} ff_visited_T;
4004
4005/*
4006 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004007 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
4009 * So we have to do 3 searches:
4010 * 1) search from the current files directory downward for the file "tags"
4011 * 2) search from the current files directory downward for the file "TAGS"
4012 * 3) search from Vims current directory downwards for the file "tags"
4013 * As you can see, the first and the third search are for the same file, so for
4014 * the third search we can use the visited list of the first search. For the
4015 * second search we must start from a empty visited list.
4016 * The struct ff_visited_list_hdr is used to manage a linked list of already
4017 * visited lists.
4018 */
4019typedef struct ff_visited_list_hdr
4020{
4021 struct ff_visited_list_hdr *ffvl_next;
4022
4023 /* the filename the attached visited list is for */
4024 char_u *ffvl_filename;
4025
4026 ff_visited_T *ffvl_visited_list;
4027
4028} ff_visited_list_hdr_T;
4029
4030
4031/*
4032 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004033 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 */
4035#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004036
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037/*
4038 * The search context:
4039 * ffsc_stack_ptr: the stack for the dirs to search
4040 * ffsc_visited_list: the currently active visited list
4041 * ffsc_dir_visited_list: the currently active visited list for search dirs
4042 * ffsc_visited_lists_list: the list of all visited lists
4043 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
4044 * ffsc_file_to_search: the file to search for
4045 * ffsc_start_dir: the starting directory, if search path was relative
4046 * ffsc_fix_path: the fix part of the given path (without wildcards)
4047 * Needed for upward search.
4048 * ffsc_wc_path: the part of the given path containing wildcards
4049 * ffsc_level: how many levels of dirs to search downwards
4050 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004051 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02004052 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
4054typedef struct ff_search_ctx_T
4055{
4056 ff_stack_T *ffsc_stack_ptr;
4057 ff_visited_list_hdr_T *ffsc_visited_list;
4058 ff_visited_list_hdr_T *ffsc_dir_visited_list;
4059 ff_visited_list_hdr_T *ffsc_visited_lists_list;
4060 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
4061 char_u *ffsc_file_to_search;
4062 char_u *ffsc_start_dir;
4063 char_u *ffsc_fix_path;
4064#ifdef FEAT_PATH_EXTRA
4065 char_u *ffsc_wc_path;
4066 int ffsc_level;
4067 char_u **ffsc_stopdirs_v;
4068#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004069 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004070 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004071} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073/* locally needed functions */
4074#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004075static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004077static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004079static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
4080static void ff_free_visited_list(ff_visited_T *vl);
4081static 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 +00004082
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004083static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
4084static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
4085static void ff_clear(ff_search_ctx_T *search_ctx);
4086static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004088static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004090static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#endif
4092#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004093static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094#endif
4095
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004096static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098#if 0
4099/*
4100 * if someone likes findfirst/findnext, here are the functions
4101 * NOT TESTED!!
4102 */
4103
4104static void *ff_fn_search_context = NULL;
4105
4106 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004107vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 ff_fn_search_context =
4110 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4111 ff_fn_search_context, rel_fname);
4112 if (NULL == ff_fn_search_context)
4113 return NULL;
4114 else
4115 return vim_findnext()
4116}
4117
4118 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004119vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121 char_u *ret = vim_findfile(ff_fn_search_context);
4122
4123 if (NULL == ret)
4124 {
4125 vim_findfile_cleanup(ff_fn_search_context);
4126 ff_fn_search_context = NULL;
4127 }
4128 return ret;
4129}
4130#endif
4131
4132/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004133 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004135 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 *
4137 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4138 * with the search context.
4139 *
4140 * Find the file 'filename' in the directory 'path'.
4141 * The parameter 'path' may contain wildcards. If so only search 'level'
4142 * directories deep. The parameter 'level' is the absolute maximum and is
4143 * not related to restricts given to the '**' wildcard. If 'level' is 100
4144 * and you use '**200' vim_findfile() will stop after 100 levels.
4145 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004146 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4147 * escape special characters.
4148 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4150 * restarted on the next higher directory level. This is repeated until the
4151 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4152 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4153 *
4154 * If the 'path' is relative, the starting dir for the search is either VIM's
4155 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004156 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 * the first wildcard.
4158 *
4159 * Upward search is only done on the starting dir.
4160 *
4161 * If 'free_visited' is TRUE the list of already visited files/directories is
4162 * cleared. Set this to FALSE if you just want to search from another
4163 * directory, but want to be sure that no directory from a previous search is
4164 * searched again. This is useful if you search for a file at different places.
4165 * The list of visited files/dirs can also be cleared with the function
4166 * vim_findfile_free_visited().
4167 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004168 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4169 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 *
4171 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004172 * passed in the parameter "search_ctx_arg". This context is reused and
4173 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004175 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4176 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004178 * If you don't have a search context from a previous call "search_ctx_arg"
4179 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 *
4181 * This function silently ignores a few errors, vim_findfile() will have
4182 * limited functionality then.
4183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185vim_findfile_init(
4186 char_u *path,
4187 char_u *filename,
4188 char_u *stopdirs UNUSED,
4189 int level,
4190 int free_visited,
4191 int find_what,
4192 void *search_ctx_arg,
4193 int tagfile, /* expanding names of tags files */
4194 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
4196#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004197 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004199 ff_stack_T *sptr;
4200 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 /* If a search context is given by the caller, reuse it, else allocate a
4203 * new one.
4204 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004205 if (search_ctx_arg != NULL)
4206 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 else
4208 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004209 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4210 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004212 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004214 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004215 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004218 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 /* clear visited list if wanted */
4221 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004222 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 else
4224 {
4225 /* Reuse old visited lists. Get the visited list for the given
4226 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004227 * one. */
4228 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4229 &search_ctx->ffsc_visited_lists_list);
4230 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004232 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4233 &search_ctx->ffsc_dir_visited_lists_list);
4234 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 goto error_return;
4236 }
4237
4238 if (ff_expand_buffer == NULL)
4239 {
4240 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4241 if (ff_expand_buffer == NULL)
4242 goto error_return;
4243 }
4244
4245 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004246 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 if (path[0] == '.'
4248 && (vim_ispathsep(path[1]) || path[1] == NUL)
4249 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4250 && rel_fname != NULL)
4251 {
4252 int len = (int)(gettail(rel_fname) - rel_fname);
4253
4254 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4255 {
4256 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004257 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004258 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004261 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4262 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 goto error_return;
4264 if (*++path != NUL)
4265 ++path;
4266 }
4267 else if (*path == NUL || !vim_isAbsName(path))
4268 {
4269#ifdef BACKSLASH_IN_FILENAME
4270 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4271 if (*path != NUL && path[1] == ':')
4272 {
4273 char_u drive[3];
4274
4275 drive[0] = path[0];
4276 drive[1] = ':';
4277 drive[2] = NUL;
4278 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4279 goto error_return;
4280 path += 2;
4281 }
4282 else
4283#endif
4284 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4285 goto error_return;
4286
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004287 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4288 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 goto error_return;
4290
4291#ifdef BACKSLASH_IN_FILENAME
4292 /* A path that starts with "/dir" is relative to the drive, not to the
4293 * directory (but not for "//machine/dir"). Only use the drive name. */
4294 if ((*path == '/' || *path == '\\')
4295 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004296 && search_ctx->ffsc_start_dir[1] == ':')
4297 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298#endif
4299 }
4300
4301#ifdef FEAT_PATH_EXTRA
4302 /*
4303 * If stopdirs are given, split them into an array of pointers.
4304 * If this fails (mem allocation), there is no upward search at all or a
4305 * stop directory is not recognized -> continue silently.
4306 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004307 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 * is handled as unlimited upward search. See function
4309 * ff_path_in_stoplist() for details.
4310 */
4311 if (stopdirs != NULL)
4312 {
4313 char_u *walker = stopdirs;
4314 int dircount;
4315
4316 while (*walker == ';')
4317 walker++;
4318
4319 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004320 search_ctx->ffsc_stopdirs_v =
4321 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004323 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
4325 do
4326 {
4327 char_u *helper;
4328 void *ptr;
4329
4330 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004331 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 (dircount + 1) * sizeof(char_u *));
4333 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004334 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 else
4336 /* ignore, keep what we have and continue */
4337 break;
4338 walker = vim_strchr(walker, ';');
4339 if (walker)
4340 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004341 search_ctx->ffsc_stopdirs_v[dircount-1] =
4342 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 walker++;
4344 }
4345 else
4346 /* this might be "", which means ascent till top
4347 * of directory tree.
4348 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004349 search_ctx->ffsc_stopdirs_v[dircount-1] =
4350 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351
4352 dircount++;
4353
4354 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004355 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358#endif
4359
4360#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004361 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 /* split into:
4364 * -fix path
4365 * -wildcard_stuff (might be NULL)
4366 */
4367 wc_part = vim_strchr(path, '*');
4368 if (wc_part != NULL)
4369 {
4370 int llevel;
4371 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004372 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004375 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 /*
4378 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004379 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4381 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4382 * For EBCDIC you get different character values.
4383 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004384 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 * string.
4386 */
4387 len = 0;
4388 while (*wc_part != NUL)
4389 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004390 if (len + 5 >= MAXPATHL)
4391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004392 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004393 break;
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (STRNCMP(wc_part, "**", 2) == 0)
4396 {
4397 ff_expand_buffer[len++] = *wc_part++;
4398 ff_expand_buffer[len++] = *wc_part++;
4399
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004400 llevel = strtol((char *)wc_part, &errpt, 10);
4401 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004403 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 /* restrict is 0 -> remove already added '**' */
4405 len -= 2;
4406 else
4407 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004408 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004409 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004411 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 +00004412 goto error_return;
4413 }
4414 }
4415 else
4416 ff_expand_buffer[len++] = *wc_part++;
4417 }
4418 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004419 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004421 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 goto error_return;
4423 }
4424 else
4425#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004426 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004428 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
4430 /* store the fix part as startdir.
4431 * This is needed if the parameter path is fully qualified.
4432 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004433 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004434 if (search_ctx->ffsc_start_dir == NULL)
4435 goto error_return;
4436 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438
4439 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004440 if (STRLEN(search_ctx->ffsc_start_dir)
4441 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004443 emsg(_(e_pathtoolong));
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004444 goto error_return;
4445 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004446 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004448 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004449 int eb_len = (int)STRLEN(ff_expand_buffer);
4450 char_u *buf = alloc(eb_len
4451 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004452
4453 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004454 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004455 if (mch_isdir(buf))
4456 {
4457 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4458 add_pathsep(ff_expand_buffer);
4459 }
4460#ifdef FEAT_PATH_EXTRA
4461 else
4462 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004463 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004464 char_u *wc_path = NULL;
4465 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004466 int len = 0;
4467
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004468 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004469 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004470 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004471 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4472 add_pathsep(ff_expand_buffer);
4473 }
4474 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004475 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004476
4477 if (search_ctx->ffsc_wc_path != NULL)
4478 {
4479 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004480 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004481 + STRLEN(search_ctx->ffsc_fix_path + len)
4482 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004483 if (temp == NULL || wc_path == NULL)
4484 {
4485 vim_free(buf);
4486 vim_free(temp);
4487 vim_free(wc_path);
4488 goto error_return;
4489 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004490
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004491 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4492 STRCAT(temp, search_ctx->ffsc_wc_path);
4493 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004494 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004495 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004496 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004497 }
4498#endif
4499 vim_free(buf);
4500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 sptr = ff_create_stack_element(ff_expand_buffer,
4503#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004504 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#endif
4506 level, 0);
4507
4508 if (sptr == NULL)
4509 goto error_return;
4510
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004511 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004513 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4514 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 goto error_return;
4516
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004517 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
4519error_return:
4520 /*
4521 * We clear the search context now!
4522 * Even when the caller gave us a (perhaps valid) context we free it here,
4523 * as we might have already destroyed it.
4524 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004525 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 return NULL;
4527}
4528
4529#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4530/*
4531 * Get the stopdir string. Check that ';' is not escaped.
4532 */
4533 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004534vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
4536 char_u *r_ptr = buf;
4537
4538 while (*r_ptr != NUL && *r_ptr != ';')
4539 {
4540 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4541 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004542 /* Overwrite the escape char,
4543 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004544 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 r_ptr++;
4546 }
4547 r_ptr++;
4548 }
4549 if (*r_ptr == ';')
4550 {
4551 *r_ptr = 0;
4552 r_ptr++;
4553 }
4554 else if (*r_ptr == NUL)
4555 r_ptr = NULL;
4556 return r_ptr;
4557}
4558#endif
4559
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004560/*
4561 * Clean up the given search context. Can handle a NULL pointer.
4562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004564vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004566 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 return;
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004570 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572}
4573
4574/*
4575 * Find a file in a search context.
4576 * The search context was created with vim_findfile_init() above.
4577 * Return a pointer to an allocated file name or NULL if nothing found.
4578 * To get all matching files call this function until you get NULL.
4579 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004580 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 *
4582 * The search algorithm is depth first. To change this replace the
4583 * stack with a list (don't forget to leave partly searched directories on the
4584 * top of the list).
4585 */
4586 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004587vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588{
4589 char_u *file_path;
4590#ifdef FEAT_PATH_EXTRA
4591 char_u *rest_of_wildcards;
4592 char_u *path_end = NULL;
4593#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004594 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4596 int len;
4597#endif
4598 int i;
4599 char_u *p;
4600#ifdef FEAT_SEARCHPATH
4601 char_u *suf;
4602#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004603 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004605 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 return NULL;
4607
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004608 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
4610 /*
4611 * filepath is used as buffer for various actions and as the storage to
4612 * return a found filename.
4613 */
4614 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4615 return NULL;
4616
4617#ifdef FEAT_PATH_EXTRA
4618 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004619 if (search_ctx->ffsc_start_dir != NULL)
4620 path_end = &search_ctx->ffsc_start_dir[
4621 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#endif
4623
4624#ifdef FEAT_PATH_EXTRA
4625 /* upward search loop */
4626 for (;;)
4627 {
4628#endif
4629 /* downward search loop */
4630 for (;;)
4631 {
4632 /* check if user user wants to stop the search*/
4633 ui_breakcheck();
4634 if (got_int)
4635 break;
4636
4637 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004638 stackp = ff_pop(search_ctx);
4639 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 break;
4641
4642 /*
4643 * TODO: decide if we leave this test in
4644 *
4645 * GOOD: don't search a directory(-tree) twice.
4646 * BAD: - check linked list for every new directory entered.
4647 * - check for double files also done below
4648 *
4649 * Here we check if we already searched this directory.
4650 * We already searched a directory if:
4651 * 1) The directory is the same.
4652 * 2) We would use the same wildcard string.
4653 *
4654 * Good if you have links on same directory via several ways
4655 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4656 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4657 *
4658 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004659 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004661 if (stackp->ffs_filearray == NULL
4662 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004664 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004666 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#endif
4668 ) == FAIL)
4669 {
4670#ifdef FF_VERBOSE
4671 if (p_verbose >= 5)
4672 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004673 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004674 smsg("Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004675 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004677 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004678 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004681 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 continue;
4683 }
4684#ifdef FF_VERBOSE
4685 else if (p_verbose >= 5)
4686 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004687 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004688 smsg("Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004689 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004691 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004692 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694#endif
4695
4696 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004697 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004699 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 continue;
4701 }
4702
4703 file_path[0] = NUL;
4704
4705 /*
4706 * If no filearray till now expand wildcards
4707 * The function expand_wildcards() can handle an array of paths
4708 * and all possible expands are returned in one array. We use this
4709 * to handle the expansion of '**' into an empty string.
4710 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004711 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
4713 char_u *dirptrs[2];
4714
4715 /* we use filepath to build the path expand_wildcards() should
4716 * expand.
4717 */
4718 dirptrs[0] = file_path;
4719 dirptrs[1] = NULL;
4720
4721 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004722 if (!vim_isAbsName(stackp->ffs_fix_path)
4723 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004725 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4726 {
4727 STRCPY(file_path, search_ctx->ffsc_start_dir);
4728 add_pathsep(file_path);
4729 }
4730 else
4731 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
4733
4734 /* append the fix part of the search path */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004735 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4736 {
4737 STRCAT(file_path, stackp->ffs_fix_path);
4738 add_pathsep(file_path);
4739 }
4740 else
4741 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
4743#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004744 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if (*rest_of_wildcards != NUL)
4746 {
4747 len = (int)STRLEN(file_path);
4748 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4749 {
4750 /* pointer to the restrict byte
4751 * The restrict byte is not a character!
4752 */
4753 p = rest_of_wildcards + 2;
4754
4755 if (*p > 0)
4756 {
4757 (*p)--;
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004758 if (len + 1 < MAXPATHL)
4759 file_path[len++] = '*';
4760 else
4761 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763
4764 if (*p == 0)
4765 {
4766 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004767 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769 else
4770 rest_of_wildcards += 3;
4771
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004772 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004775 stackp->ffs_star_star_empty = 1;
4776 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 }
4779
4780 /*
4781 * Here we copy until the next path separator or the end of
4782 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004783 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 * pushing every directory returned from expand_wildcards()
4785 * on the stack again for further search.
4786 */
4787 while (*rest_of_wildcards
4788 && !vim_ispathsep(*rest_of_wildcards))
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004789 if (len + 1 < MAXPATHL)
4790 file_path[len++] = *rest_of_wildcards++;
4791 else
4792 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 file_path[len] = NUL;
4795 if (vim_ispathsep(*rest_of_wildcards))
4796 rest_of_wildcards++;
4797 }
4798#endif
4799
4800 /*
4801 * Expand wildcards like "*" and "$VAR".
4802 * If the path is a URL don't try this.
4803 */
4804 if (path_with_url(dirptrs[0]))
4805 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004806 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004808 if (stackp->ffs_filearray != NULL
4809 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004811 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004813 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004816 /* Add EW_NOTWILD because the expanded path may contain
4817 * wildcard characters that are to be taken literally.
4818 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004820 &stackp->ffs_filearray_size,
4821 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004822 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004824 stackp->ffs_filearray_cur = 0;
4825 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827#ifdef FEAT_PATH_EXTRA
4828 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004829 rest_of_wildcards = &stackp->ffs_wc_path[
4830 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#endif
4832
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004833 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
4835 /* this is the first time we work on this directory */
4836#ifdef FEAT_PATH_EXTRA
4837 if (*rest_of_wildcards == NUL)
4838#endif
4839 {
4840 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004841 * We don't have further wildcards to expand, so we have to
4842 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004844 for (i = stackp->ffs_filearray_cur;
4845 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004847 if (!path_with_url(stackp->ffs_filearray[i])
4848 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 continue; /* not a directory */
4850
Bram Moolenaar21160b92009-11-11 15:56:10 +00004851 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 * below */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004853 if (STRLEN(stackp->ffs_filearray[i]) + 1
4854 + STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4855 {
4856 STRCPY(file_path, stackp->ffs_filearray[i]);
4857 add_pathsep(file_path);
4858 STRCAT(file_path, search_ctx->ffsc_file_to_search);
4859 }
4860 else
4861 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
4863 /*
4864 * Try without extra suffix and then with suffixes
4865 * from 'suffixesadd'.
4866 */
4867#ifdef FEAT_SEARCHPATH
4868 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004869 if (search_ctx->ffsc_tagfile)
4870 suf = (char_u *)"";
4871 else
4872 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 for (;;)
4874#endif
4875 {
4876 /* if file exists and we didn't already find it */
4877 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004878 || (mch_getperm(file_path) >= 0
4879 && (search_ctx->ffsc_find_what
4880 == FINDFILE_BOTH
4881 || ((search_ctx->ffsc_find_what
4882 == FINDFILE_DIR)
4883 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#ifndef FF_VERBOSE
4885 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004886 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 file_path
4888#ifdef FEAT_PATH_EXTRA
4889 , (char_u *)""
4890#endif
4891 ) == OK)
4892#endif
4893 )
4894 {
4895#ifdef FF_VERBOSE
4896 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004897 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 file_path
4899#ifdef FEAT_PATH_EXTRA
4900 , (char_u *)""
4901#endif
4902 ) == FAIL)
4903 {
4904 if (p_verbose >= 5)
4905 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004906 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004907 smsg("Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 file_path);
4909 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004910 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004911 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 continue;
4914 }
4915#endif
4916
4917 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004918 stackp->ffs_filearray_cur = i + 1;
4919 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004921 if (!path_with_url(file_path))
4922 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4924 == OK)
4925 {
4926 p = shorten_fname(file_path,
4927 ff_expand_buffer);
4928 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004929 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931#ifdef FF_VERBOSE
4932 if (p_verbose >= 5)
4933 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004934 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004935 smsg("HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004937 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004938 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940#endif
4941 return file_path;
4942 }
4943
4944#ifdef FEAT_SEARCHPATH
4945 /* Not found or found already, try next suffix. */
4946 if (*suf == NUL)
4947 break;
4948 copy_option_part(&suf, file_path + len,
4949 MAXPATHL - len, ",");
4950#endif
4951 }
4952 }
4953 }
4954#ifdef FEAT_PATH_EXTRA
4955 else
4956 {
4957 /*
4958 * still wildcards left, push the directories for further
4959 * search
4960 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004961 for (i = stackp->ffs_filearray_cur;
4962 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004964 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 continue; /* not a directory */
4966
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004967 ff_push(search_ctx,
4968 ff_create_stack_element(
4969 stackp->ffs_filearray[i],
4970 rest_of_wildcards,
4971 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 }
4974#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004975 stackp->ffs_filearray_cur = 0;
4976 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978
4979#ifdef FEAT_PATH_EXTRA
4980 /*
4981 * if wildcards contains '**' we have to descent till we reach the
4982 * leaves of the directory tree.
4983 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004984 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004986 for (i = stackp->ffs_filearray_cur;
4987 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004989 if (fnamecmp(stackp->ffs_filearray[i],
4990 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004992 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004994 ff_push(search_ctx,
4995 ff_create_stack_element(stackp->ffs_filearray[i],
4996 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 }
4999#endif
5000
5001 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005002 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 }
5005
5006#ifdef FEAT_PATH_EXTRA
5007 /* If we reached this, we didn't find anything downwards.
5008 * Let's check if we should do an upward search.
5009 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005010 if (search_ctx->ffsc_start_dir
5011 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 ff_stack_T *sptr;
5014
5015 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005016 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
5017 (int)(path_end - search_ctx->ffsc_start_dir),
5018 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 break;
5020
5021 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005022 while (path_end > search_ctx->ffsc_start_dir
5023 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005025 while (path_end > search_ctx->ffsc_start_dir
5026 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 path_end--;
5028 *path_end = 0;
5029 path_end--;
5030
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005031 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 break;
5033
Bram Moolenaar15618fa2017-03-19 21:37:13 +01005034 if (STRLEN(search_ctx->ffsc_start_dir) + 1
5035 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
5036 {
5037 STRCPY(file_path, search_ctx->ffsc_start_dir);
5038 add_pathsep(file_path);
5039 STRCAT(file_path, search_ctx->ffsc_fix_path);
5040 }
5041 else
5042 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 /* create a new stack entry */
5045 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005046 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (sptr == NULL)
5048 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005049 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051 else
5052 break;
5053 }
5054#endif
5055
Bram Moolenaar15618fa2017-03-19 21:37:13 +01005056fail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 vim_free(file_path);
5058 return NULL;
5059}
5060
5061/*
5062 * Free the list of lists of visited files and directories
5063 * Can handle it if the passed search_context is NULL;
5064 */
5065 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005066vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005068 ff_search_ctx_T *search_ctx;
5069
5070 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 return;
5072
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005073 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
5074 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
5075 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076}
5077
5078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005079vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 ff_visited_list_hdr_T *vp;
5082
5083 while (*list_headp != NULL)
5084 {
5085 vp = (*list_headp)->ffvl_next;
5086 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5087
5088 vim_free((*list_headp)->ffvl_filename);
5089 vim_free(*list_headp);
5090 *list_headp = vp;
5091 }
5092 *list_headp = NULL;
5093}
5094
5095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005096ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
5098 ff_visited_T *vp;
5099
5100 while (vl != NULL)
5101 {
5102 vp = vl->ffv_next;
5103#ifdef FEAT_PATH_EXTRA
5104 vim_free(vl->ffv_wc_path);
5105#endif
5106 vim_free(vl);
5107 vl = vp;
5108 }
5109 vl = NULL;
5110}
5111
5112/*
5113 * Returns the already visited list for the given filename. If none is found it
5114 * allocates a new one.
5115 */
5116 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01005117ff_get_visited_list(
5118 char_u *filename,
5119 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
5121 ff_visited_list_hdr_T *retptr = NULL;
5122
5123 /* check if a visited list for the given filename exists */
5124 if (*list_headp != NULL)
5125 {
5126 retptr = *list_headp;
5127 while (retptr != NULL)
5128 {
5129 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5130 {
5131#ifdef FF_VERBOSE
5132 if (p_verbose >= 5)
5133 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005134 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005135 smsg("ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 filename);
5137 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005138 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005139 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141#endif
5142 return retptr;
5143 }
5144 retptr = retptr->ffvl_next;
5145 }
5146 }
5147
5148#ifdef FF_VERBOSE
5149 if (p_verbose >= 5)
5150 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005151 verbose_enter_scroll();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005152 smsg("ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 /* don't overwrite this either */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005154 msg_puts("\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005155 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157#endif
5158
5159 /*
5160 * if we reach this we didn't find a list and we have to allocate new list
5161 */
5162 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5163 if (retptr == NULL)
5164 return NULL;
5165
5166 retptr->ffvl_visited_list = NULL;
5167 retptr->ffvl_filename = vim_strsave(filename);
5168 if (retptr->ffvl_filename == NULL)
5169 {
5170 vim_free(retptr);
5171 return NULL;
5172 }
5173 retptr->ffvl_next = *list_headp;
5174 *list_headp = retptr;
5175
5176 return retptr;
5177}
5178
5179#ifdef FEAT_PATH_EXTRA
5180/*
5181 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5182 * They are equal if:
5183 * - both paths are NULL
5184 * - they have the same length
5185 * - char by char comparison is OK
5186 * - the only differences are in the counters behind a '**', so
5187 * '**\20' is equal to '**\24'
5188 */
5189 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005190ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005192 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005193 int c1 = NUL;
5194 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005195 int prev1 = NUL;
5196 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 if (s1 == s2)
5199 return TRUE;
5200
5201 if (s1 == NULL || s2 == NULL)
5202 return FALSE;
5203
Bram Moolenaard43f0952015-08-27 22:30:47 +02005204 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005206 c1 = PTR2CHAR(s1 + i);
5207 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005208
5209 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5210 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005211 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005212 prev2 = prev1;
5213 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005214
Bram Moolenaar15675582018-02-09 12:29:56 +01005215 i += MB_PTR2LEN(s1 + i);
5216 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005218 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219}
5220#endif
5221
5222/*
5223 * maintains the list of already visited files and dirs
5224 * returns FAIL if the given file/dir is already in the list
5225 * returns OK if it is newly added
5226 *
5227 * TODO: What to do on memory allocation problems?
5228 * -> return TRUE - Better the file is found several times instead of
5229 * never.
5230 */
5231 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005232ff_check_visited(
5233 ff_visited_T **visited_list,
5234 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005236 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 ff_visited_T *vp;
5241#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02005242 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 int url = FALSE;
5244#endif
5245
5246 /* For an URL we only compare the name, otherwise we compare the
5247 * device/inode (unix) or the full path name (not Unix). */
5248 if (path_with_url(fname))
5249 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005250 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#ifdef UNIX
5252 url = TRUE;
5253#endif
5254 }
5255 else
5256 {
5257 ff_expand_buffer[0] = NUL;
5258#ifdef UNIX
5259 if (mch_stat((char *)fname, &st) < 0)
5260#else
5261 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5262#endif
5263 return FAIL;
5264 }
5265
5266 /* check against list of already visited files */
5267 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5268 {
5269 if (
5270#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005271 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5272 && vp->ffv_ino == st.st_ino)
5273 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274#endif
5275 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5276 )
5277 {
5278#ifdef FEAT_PATH_EXTRA
5279 /* are the wildcard parts equal */
5280 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5281#endif
5282 /* already visited */
5283 return FAIL;
5284 }
5285 }
5286
5287 /*
5288 * New file/dir. Add it to the list of visited files/dirs.
5289 */
5290 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5291 + STRLEN(ff_expand_buffer)));
5292
5293 if (vp != NULL)
5294 {
5295#ifdef UNIX
5296 if (!url)
5297 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005298 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 vp->ffv_ino = st.st_ino;
5300 vp->ffv_dev = st.st_dev;
5301 vp->ffv_fname[0] = NUL;
5302 }
5303 else
5304 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005305 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306#endif
5307 STRCPY(vp->ffv_fname, ff_expand_buffer);
5308#ifdef UNIX
5309 }
5310#endif
5311#ifdef FEAT_PATH_EXTRA
5312 if (wc_path != NULL)
5313 vp->ffv_wc_path = vim_strsave(wc_path);
5314 else
5315 vp->ffv_wc_path = NULL;
5316#endif
5317
5318 vp->ffv_next = *visited_list;
5319 *visited_list = vp;
5320 }
5321
5322 return OK;
5323}
5324
5325/*
5326 * create stack element from given path pieces
5327 */
5328 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005329ff_create_stack_element(
5330 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005332 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005334 int level,
5335 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 ff_stack_T *new;
5338
5339 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5340 if (new == NULL)
5341 return NULL;
5342
5343 new->ffs_prev = NULL;
5344 new->ffs_filearray = NULL;
5345 new->ffs_filearray_size = 0;
5346 new->ffs_filearray_cur = 0;
5347 new->ffs_stage = 0;
5348 new->ffs_level = level;
Bram Moolenaar945ec092016-06-08 21:17:43 +02005349 new->ffs_star_star_empty = star_star_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351 /* the following saves NULL pointer checks in vim_findfile */
5352 if (fix_part == NULL)
5353 fix_part = (char_u *)"";
5354 new->ffs_fix_path = vim_strsave(fix_part);
5355
5356#ifdef FEAT_PATH_EXTRA
5357 if (wc_part == NULL)
5358 wc_part = (char_u *)"";
5359 new->ffs_wc_path = vim_strsave(wc_part);
5360#endif
5361
5362 if (new->ffs_fix_path == NULL
5363#ifdef FEAT_PATH_EXTRA
5364 || new->ffs_wc_path == NULL
5365#endif
5366 )
5367 {
5368 ff_free_stack_element(new);
5369 new = NULL;
5370 }
5371
5372 return new;
5373}
5374
5375/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005376 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 */
5378 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005379ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005382 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005383 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005385 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5386 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388}
5389
5390/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005391 * Pop a dir from the directory stack.
5392 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 */
5394 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005395ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
5397 ff_stack_T *sptr;
5398
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005399 sptr = search_ctx->ffsc_stack_ptr;
5400 if (search_ctx->ffsc_stack_ptr != NULL)
5401 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403 return sptr;
5404}
5405
5406/*
5407 * free the given stack element
5408 */
5409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005410ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005413 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005415 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#endif
5417
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005418 if (stack_ptr->ffs_filearray != NULL)
5419 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005421 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422}
5423
5424/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005425 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 */
5427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 ff_stack_T *sptr;
5431
5432 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005433 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 ff_free_stack_element(sptr);
5435
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005436 vim_free(search_ctx->ffsc_file_to_search);
5437 vim_free(search_ctx->ffsc_start_dir);
5438 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005440 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441#endif
5442
5443#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005444 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
5446 int i = 0;
5447
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005448 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005450 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 i++;
5452 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005453 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005455 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#endif
5457
5458 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005459 search_ctx->ffsc_file_to_search = NULL;
5460 search_ctx->ffsc_start_dir = NULL;
5461 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005463 search_ctx->ffsc_wc_path = NULL;
5464 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465#endif
5466}
5467
5468#ifdef FEAT_PATH_EXTRA
5469/*
5470 * check if the given path is in the stopdirs
5471 * returns TRUE if yes else FALSE
5472 */
5473 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 int i = 0;
5477
5478 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005479 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 path_len--;
5481
5482 /* if no path consider it as match */
5483 if (path_len == 0)
5484 return TRUE;
5485
5486 for (i = 0; stopdirs_v[i] != NULL; i++)
5487 {
5488 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5489 {
5490 /* match for parent directory. So '/home' also matches
5491 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5492 * '/home/r' would also match '/home/rks'
5493 */
5494 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005495 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return TRUE;
5497 }
5498 else
5499 {
5500 if (fnamecmp(stopdirs_v[i], path) == 0)
5501 return TRUE;
5502 }
5503 }
5504 return FALSE;
5505}
5506#endif
5507
5508#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5509/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005510 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 *
5512 * On the first call set the parameter 'first' to TRUE to initialize
5513 * the search. For repeating calls to FALSE.
5514 *
5515 * Repeating calls will return other files called 'ptr[len]' from the path.
5516 *
5517 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5518 * don't need valid values.
5519 *
5520 * If nothing found on the first call the option FNAME_MESS will issue the
5521 * message:
5522 * 'Can't find file "<file>" in path'
5523 * On repeating calls:
5524 * 'No more file "<file>" found in path'
5525 *
5526 * options:
5527 * FNAME_MESS give error message when not found
5528 *
5529 * Uses NameBuff[]!
5530 *
5531 * Returns an allocated string for the file name. NULL for error.
5532 *
5533 */
5534 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005535find_file_in_path(
5536 char_u *ptr, /* file name */
5537 int len, /* length of file name */
5538 int options,
5539 int first, /* use count'th matching file name */
5540 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 return find_file_in_path_option(ptr, len, options, first,
5543 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005544 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545}
5546
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005547static char_u *ff_file_to_find = NULL;
5548static void *fdip_search_ctx = NULL;
5549
5550#if defined(EXITFREE)
5551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005552free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005553{
5554 vim_free(ff_file_to_find);
5555 vim_findfile_cleanup(fdip_search_ctx);
5556}
5557#endif
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559/*
5560 * Find the directory name "ptr[len]" in the path.
5561 *
5562 * options:
5563 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005564 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 *
5566 * Uses NameBuff[]!
5567 *
5568 * Returns an allocated string for the file name. NULL for error.
5569 */
5570 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005571find_directory_in_path(
5572 char_u *ptr, /* file name */
5573 int len, /* length of file name */
5574 int options,
5575 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
5577 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005578 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579}
5580
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005581 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005582find_file_in_path_option(
5583 char_u *ptr, /* file name */
5584 int len, /* length of file name */
5585 int options,
5586 int first, /* use count'th matching file name */
5587 char_u *path_option, /* p_path or p_cdpath */
5588 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5589 char_u *rel_fname, /* file name we are looking relative to. */
5590 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 static int did_findfile_init = FALSE;
5594 char_u save_char;
5595 char_u *file_name = NULL;
5596 char_u *buf = NULL;
5597 int rel_to_curdir;
5598#ifdef AMIGA
5599 struct Process *proc = (struct Process *)FindTask(0L);
5600 APTR save_winptr = proc->pr_WindowPtr;
5601
5602 /* Avoid a requester here for a volume that doesn't exist. */
5603 proc->pr_WindowPtr = (APTR)-1L;
5604#endif
5605
5606 if (first == TRUE)
5607 {
5608 /* copy file name into NameBuff, expanding environment variables */
5609 save_char = ptr[len];
5610 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005611 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 ptr[len] = save_char;
5613
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005614 vim_free(ff_file_to_find);
5615 ff_file_to_find = vim_strsave(NameBuff);
5616 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
5618 file_name = NULL;
5619 goto theend;
5620 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005621 if (options & FNAME_UNESC)
5622 {
5623 /* Change all "\ " to " ". */
5624 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5625 if (ptr[0] == '\\' && ptr[1] == ' ')
5626 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
5629
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005630 rel_to_curdir = (ff_file_to_find[0] == '.'
5631 && (ff_file_to_find[1] == NUL
5632 || vim_ispathsep(ff_file_to_find[1])
5633 || (ff_file_to_find[1] == '.'
5634 && (ff_file_to_find[2] == NUL
5635 || vim_ispathsep(ff_file_to_find[2])))));
5636 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 /* "..", "../path", "." and "./path": don't use the path_option */
5638 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005639#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005641 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005642 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005643 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644#endif
5645#ifdef AMIGA
5646 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005647 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#endif
5649 )
5650 {
5651 /*
5652 * Absolute path, no need to use "path_option".
5653 * If this is not a first call, return NULL. We already returned a
5654 * filename on the first call.
5655 */
5656 if (first == TRUE)
5657 {
5658 int l;
5659 int run;
5660
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005661 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005663 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 goto theend;
5665 }
5666
5667 /* When FNAME_REL flag given first use the directory of the file.
5668 * Otherwise or when this fails use the current directory. */
5669 for (run = 1; run <= 2; ++run)
5670 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005671 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 if (run == 1
5673 && rel_to_curdir
5674 && (options & FNAME_REL)
5675 && rel_fname != NULL
5676 && STRLEN(rel_fname) + l < MAXPATHL)
5677 {
5678 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005679 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 l = (int)STRLEN(NameBuff);
5681 }
5682 else
5683 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005684 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 run = 2;
5686 }
5687
5688 /* When the file doesn't exist, try adding parts of
5689 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005690 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 for (;;)
5692 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005693 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005694 && (find_what == FINDFILE_BOTH
5695 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005696 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 file_name = vim_strsave(NameBuff);
5699 goto theend;
5700 }
5701 if (*buf == NUL)
5702 break;
5703 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5704 }
5705 }
5706 }
5707 }
5708 else
5709 {
5710 /*
5711 * Loop over all paths in the 'path' or 'cdpath' option.
5712 * When "first" is set, first setup to the start of the option.
5713 * Otherwise continue to find the next match.
5714 */
5715 if (first == TRUE)
5716 {
5717 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005718 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 dir = path_option;
5720 did_findfile_init = FALSE;
5721 }
5722
5723 for (;;)
5724 {
5725 if (did_findfile_init)
5726 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005727 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 if (file_name != NULL)
5729 break;
5730
5731 did_findfile_init = FALSE;
5732 }
5733 else
5734 {
5735 char_u *r_ptr;
5736
5737 if (dir == NULL || *dir == NUL)
5738 {
5739 /* We searched all paths of the option, now we can
5740 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005741 vim_findfile_cleanup(fdip_search_ctx);
5742 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 break;
5744 }
5745
5746 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5747 break;
5748
5749 /* copy next path */
5750 buf[0] = 0;
5751 copy_option_part(&dir, buf, MAXPATHL, " ,");
5752
5753#ifdef FEAT_PATH_EXTRA
5754 /* get the stopdir string */
5755 r_ptr = vim_findfile_stopdir(buf);
5756#else
5757 r_ptr = NULL;
5758#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005759 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005760 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005761 fdip_search_ctx, FALSE, rel_fname);
5762 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 did_findfile_init = TRUE;
5764 vim_free(buf);
5765 }
5766 }
5767 }
5768 if (file_name == NULL && (options & FNAME_MESS))
5769 {
5770 if (first == TRUE)
5771 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005772 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005773 semsg(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005774 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005776 semsg(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005777 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779 else
5780 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005781 if (find_what == FINDFILE_DIR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005782 semsg(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005783 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005785 semsg(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005786 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
5788 }
5789
5790theend:
5791#ifdef AMIGA
5792 proc->pr_WindowPtr = save_winptr;
5793#endif
5794 return file_name;
5795}
5796
5797#endif /* FEAT_SEARCHPATH */
5798
5799/*
5800 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5801 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5802 */
5803 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005804vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806#ifndef FEAT_SEARCHPATH
5807 return mch_chdir((char *)new_dir);
5808#else
5809 char_u *dir_name;
5810 int r;
5811
5812 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5813 FNAME_MESS, curbuf->b_ffname);
5814 if (dir_name == NULL)
5815 return -1;
5816 r = mch_chdir((char *)dir_name);
5817 vim_free(dir_name);
5818 return r;
5819#endif
5820}
5821
5822/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005823 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005825 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5826 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 * Returns OK or FAIL.
5828 */
5829 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005830get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005832 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
5834 if (mch_get_user_name(buf, len) == FAIL)
5835 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005836 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 }
5838 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005839 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return OK;
5841}
5842
5843#ifndef HAVE_QSORT
5844/*
5845 * Our own qsort(), for systems that don't have it.
5846 * It's simple and slow. From the K&R C book.
5847 */
5848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005849qsort(
5850 void *base,
5851 size_t elm_count,
5852 size_t elm_size,
5853 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 char_u *buf;
5856 char_u *p1;
5857 char_u *p2;
5858 int i, j;
5859 int gap;
5860
5861 buf = alloc((unsigned)elm_size);
5862 if (buf == NULL)
5863 return;
5864
5865 for (gap = elm_count / 2; gap > 0; gap /= 2)
5866 for (i = gap; i < elm_count; ++i)
5867 for (j = i - gap; j >= 0; j -= gap)
5868 {
5869 /* Compare the elements. */
5870 p1 = (char_u *)base + j * elm_size;
5871 p2 = (char_u *)base + (j + gap) * elm_size;
5872 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5873 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005874 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 mch_memmove(buf, p1, elm_size);
5876 mch_memmove(p1, p2, elm_size);
5877 mch_memmove(p2, buf, elm_size);
5878 }
5879
5880 vim_free(buf);
5881}
5882#endif
5883
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884/*
5885 * Sort an array of strings.
5886 */
5887static int
5888#ifdef __BORLANDC__
5889_RTLENTRYF
5890#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005891sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
5893 static int
5894#ifdef __BORLANDC__
5895_RTLENTRYF
5896#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005897sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 return STRCMP(*(char **)s1, *(char **)s2);
5900}
5901
5902 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005903sort_strings(
5904 char_u **files,
5905 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5908}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
5910#if !defined(NO_EXPANDPATH) || defined(PROTO)
5911/*
5912 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005913 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 * Return value like strcmp(p, q), but consider path separators.
5915 */
5916 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005917pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005919 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005920 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005921 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005923 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005925 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005926 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005929 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005931 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 return 0;
5933 s = q;
Bram Moolenaar15675582018-02-09 12:29:56 +01005934 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 break;
5936 }
5937
5938 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005939 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
5941 s = p;
5942 break;
5943 }
5944
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005945 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#ifdef BACKSLASH_IN_FILENAME
5947 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005948 && !((c1 == '/' && c2 == '\\')
5949 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#endif
5951 )
5952 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005953 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005955 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005957 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5958 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005960
5961 i += MB_PTR2LEN((char_u *)p + i);
5962 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005964 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005965 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005967 c1 = PTR2CHAR((char_u *)s + i);
5968 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005970 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005971 && i > 0
5972 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005974 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005976 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005978 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 return 0; /* match with trailing slash */
5980 if (s == q)
5981 return -1; /* no match */
5982 return 1;
5983}
5984#endif
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986/*
5987 * The putenv() implementation below comes from the "screen" program.
5988 * Included with permission from Juergen Weigert.
5989 * See pty.c for the copyright notice.
5990 */
5991
5992/*
5993 * putenv -- put value into environment
5994 *
5995 * Usage: i = putenv (string)
5996 * int i;
5997 * char *string;
5998 *
5999 * where string is of the form <name>=<value>.
6000 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
6001 *
6002 * Putenv may need to add a new name into the environment, or to
6003 * associate a value longer than the current value with a particular
6004 * name. So, to make life simpler, putenv() copies your entire
6005 * environment into the heap (i.e. malloc()) from the stack
6006 * (i.e. where it resides when your process is initiated) the first
6007 * time you call it.
6008 *
6009 * (history removed, not very interesting. See the "screen" sources.)
6010 */
6011
6012#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
6013
6014#define EXTRASIZE 5 /* increment to add to env. size */
6015
6016static int envsize = -1; /* current size of environment */
Bram Moolenaard0573012017-10-28 21:11:06 +02006017extern char **environ; /* the global which is your env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01006019static int findenv(char *name); /* look for a name in the env. */
6020static int newenv(void); /* copy env. from stack to heap */
6021static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
6023 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006024putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 int i;
6027 char *p;
6028
6029 if (envsize < 0)
6030 { /* first time putenv called */
6031 if (newenv() < 0) /* copy env. to heap */
6032 return -1;
6033 }
6034
6035 i = findenv((char *)string); /* look for name in environment */
6036
6037 if (i < 0)
6038 { /* name must be added */
6039 for (i = 0; environ[i]; i++);
6040 if (i >= (envsize - 1))
6041 { /* need new slot */
6042 if (moreenv() < 0)
6043 return -1;
6044 }
6045 p = (char *)alloc((unsigned)(strlen(string) + 1));
6046 if (p == NULL) /* not enough core */
6047 return -1;
6048 environ[i + 1] = 0; /* new end of env. */
6049 }
6050 else
6051 { /* name already in env. */
6052 p = vim_realloc(environ[i], strlen(string) + 1);
6053 if (p == NULL)
6054 return -1;
6055 }
6056 sprintf(p, "%s", string); /* copy into env. */
6057 environ[i] = p;
6058
6059 return 0;
6060}
6061
6062 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006063findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 char *namechar, *envchar;
6066 int i, found;
6067
6068 found = 0;
6069 for (i = 0; environ[i] && !found; i++)
6070 {
6071 envchar = environ[i];
6072 namechar = name;
6073 while (*namechar && *namechar != '=' && (*namechar == *envchar))
6074 {
6075 namechar++;
6076 envchar++;
6077 }
6078 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6079 }
6080 return found ? i - 1 : -1;
6081}
6082
6083 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006084newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 char **env, *elem;
6087 int i, esize;
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 for (i = 0; environ[i]; i++)
6090 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02006091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 esize = i + EXTRASIZE + 1;
6093 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6094 if (env == NULL)
6095 return -1;
6096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 for (i = 0; environ[i]; i++)
6098 {
6099 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6100 if (elem == NULL)
6101 return -1;
6102 env[i] = elem;
6103 strcpy(elem, environ[i]);
6104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 env[i] = 0;
6107 environ = env;
6108 envsize = esize;
6109 return 0;
6110}
6111
6112 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006113moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
6115 int esize;
6116 char **env;
6117
6118 esize = envsize + EXTRASIZE;
6119 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6120 if (env == 0)
6121 return -1;
6122 environ = env;
6123 envsize = esize;
6124 return 0;
6125}
6126
6127# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02006128/*
6129 * Used for mch_getenv() for Mac.
6130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006132vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 int i;
6135 char_u *p;
6136
6137 if (envsize < 0)
6138 return NULL;
6139
6140 i = findenv((char *)string);
6141
6142 if (i < 0)
6143 return NULL;
6144
6145 p = vim_strchr((char_u *)environ[i], '=');
6146 return (p + 1);
6147}
6148# endif
6149
6150#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006151
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006152#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006153/*
6154 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6155 * rights to write into.
6156 */
6157 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006158filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006159{
6160 int retval = 0;
6161#if defined(UNIX) || defined(VMS)
6162 int perm = 0;
6163#endif
6164
6165#if defined(UNIX) || defined(VMS)
6166 perm = mch_getperm(fname);
6167#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006168 if (
6169# ifdef WIN3264
6170 mch_writable(fname) &&
6171# else
6172# if defined(UNIX) || defined(VMS)
6173 (perm & 0222) &&
6174# endif
6175# endif
6176 mch_access((char *)fname, W_OK) == 0
6177 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006178 {
6179 ++retval;
6180 if (mch_isdir(fname))
6181 ++retval;
6182 }
6183 return retval;
6184}
6185#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006186
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006187#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6188/*
6189 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006190 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006191 */
6192 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006193get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006194{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006195 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006196
6197 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006198 if (n == EOF) return -1;
6199 c = getc(fd);
6200 if (c == EOF) return -1;
6201 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006202}
6203
6204/*
6205 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006206 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006207 */
6208 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006209get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006210{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006211 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006212
6213 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006214 if (n == EOF) return -1;
6215 c = getc(fd);
6216 if (c == EOF) return -1;
6217 n = (n << 8) + c;
6218 c = getc(fd);
6219 if (c == EOF) return -1;
6220 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006221}
6222
6223/*
6224 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006225 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006226 */
6227 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006228get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006229{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006230 int c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006231 /* Use unsigned rather than int otherwise result is undefined
6232 * when left-shift sets the MSB. */
6233 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006234
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006235 c = getc(fd);
6236 if (c == EOF) return -1;
6237 n = (unsigned)c;
6238 c = getc(fd);
6239 if (c == EOF) return -1;
6240 n = (n << 8) + (unsigned)c;
6241 c = getc(fd);
6242 if (c == EOF) return -1;
6243 n = (n << 8) + (unsigned)c;
6244 c = getc(fd);
6245 if (c == EOF) return -1;
6246 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02006247 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006248}
6249
6250/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006251 * Read 8 bytes from "fd" and turn them into a time_T, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006252 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006253 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006254 time_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006255get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006256{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006257 int c;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006258 time_T n = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006259 int i;
6260
6261 for (i = 0; i < 8; ++i)
Bram Moolenaare26e0d22018-03-20 12:34:04 +01006262 {
6263 c = getc(fd);
6264 if (c == EOF) return -1;
6265 n = (n << 8) + c;
6266 }
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006267 return n;
6268}
6269
6270/*
6271 * Read a string of length "cnt" from "fd" into allocated memory.
6272 * Returns NULL when out of memory or unable to read that many bytes.
6273 */
6274 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006275read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006276{
6277 char_u *str;
6278 int i;
6279 int c;
6280
6281 /* allocate memory */
6282 str = alloc((unsigned)cnt + 1);
6283 if (str != NULL)
6284 {
6285 /* Read the string. Quit when running into the EOF. */
6286 for (i = 0; i < cnt; ++i)
6287 {
6288 c = getc(fd);
6289 if (c == EOF)
6290 {
6291 vim_free(str);
6292 return NULL;
6293 }
6294 str[i] = c;
6295 }
6296 str[i] = NUL;
6297 }
6298 return str;
6299}
6300
6301/*
6302 * Write a number to file "fd", MSB first, in "len" bytes.
6303 */
6304 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006305put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006306{
6307 int i;
6308
6309 for (i = len - 1; i >= 0; --i)
6310 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6311 return FAIL;
6312 return OK;
6313}
6314
6315#ifdef _MSC_VER
6316# if (_MSC_VER <= 1200)
6317/* This line is required for VC6 without the service pack. Also see the
6318 * matching #pragma below. */
6319 # pragma optimize("", off)
6320# endif
6321#endif
6322
6323/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006324 * Write time_T to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006325 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006326 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006327 int
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006328put_time(FILE *fd, time_T the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006329{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006330 char_u buf[8];
6331
6332 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006333 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006334}
6335
6336/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006337 * Write time_T to "buf[8]".
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006338 */
6339 void
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006340time_to_bytes(time_T the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006341{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006342 int c;
6343 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006344 int bi = 0;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006345 time_T wtime = the_time;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006346
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006347 /* time_T can be up to 8 bytes in size, more than long_u, thus we
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006348 * can't use put_bytes() here.
6349 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006350 * sign. This happens for large values of wtime. A cast to long_u may
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006351 * truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes,
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006352 * it's safe to assume that long_u is 4 bytes or more and when using 8
6353 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006354 for (i = 7; i >= 0; --i)
6355 {
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006356 if (i + 1 > (int)sizeof(time_T))
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006357 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006358 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006359 else
6360 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006361#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006362 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006363#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006364 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006365#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006366 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006367 }
6368 }
6369}
6370
6371#ifdef _MSC_VER
6372# if (_MSC_VER <= 1200)
6373 # pragma optimize("", on)
6374# endif
6375#endif
6376
6377#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006378
6379#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6380 || defined(FEAT_SPELL) || defined(PROTO)
6381/*
6382 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6383 * When "s" is NULL FALSE is returned.
6384 */
6385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006386has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006387{
6388 char_u *p;
6389
6390 if (s != NULL)
6391 for (p = s; *p != NUL; ++p)
6392 if (*p >= 128)
6393 return TRUE;
6394 return FALSE;
6395}
6396#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006397
6398#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006399# define MAX_REPEAT_PARSE 8
6400
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006401/*
6402 * Process messages that have been queued for netbeans or clientserver.
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006403 * Also check if any jobs have ended.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006404 * These functions can call arbitrary vimscript and should only be called when
6405 * it is safe to do so.
6406 */
6407 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006408parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006409{
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006410 win_T *old_curwin = curwin;
6411 int i;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006412
Bram Moolenaar94f01952018-09-01 15:30:03 +02006413 // Do not handle messages while redrawing, because it may cause buffers to
6414 // change or be wiped while they are being redrawn.
6415 if (updating_screen)
6416 return;
6417
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006418 // Loop when a job ended, but don't keep looping forever.
6419 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
6420 {
6421 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006422# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006423 channel_handle_events(FALSE);
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006424# endif
6425
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006426# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006427 // Process the queued netbeans messages.
6428 netbeans_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006429# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006430# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006431 // Write any buffer lines still to be written.
6432 channel_write_any_lines();
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006433
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006434 // Process the messages queued on channels.
6435 channel_parse_messages();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006436# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006437# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006438 // Process the queued clientserver messages.
6439 server_parse_messages();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006440# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006441# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006442 // Check if any jobs have ended. If so, repeat the above to handle
6443 // changes, e.g. stdin may have been closed.
6444 if (job_check_ended())
6445 continue;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006446# endif
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01006447 break;
6448 }
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006449
Bram Moolenaar94f01952018-09-01 15:30:03 +02006450 // If the current window changed we need to bail out of the waiting loop.
6451 // E.g. when a job exit callback closes the terminal window.
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006452 if (curwin != old_curwin)
6453 ins_char_typebuf(K_IGNORE);
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006454}
6455#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006456
Bram Moolenaar51628222016-12-01 23:03:28 +01006457#ifndef PROTO /* proto is defined in vim.h */
6458# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006459/*
6460 * Return time in msec since "start_tv".
6461 */
6462 long
6463elapsed(struct timeval *start_tv)
6464{
6465 struct timeval now_tv;
6466
6467 gettimeofday(&now_tv, NULL);
6468 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
6469 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
6470}
Bram Moolenaar51628222016-12-01 23:03:28 +01006471# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006472
Bram Moolenaar51628222016-12-01 23:03:28 +01006473# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006474/*
6475 * Return time in msec since "start_tick".
6476 */
6477 long
6478elapsed(DWORD start_tick)
6479{
6480 DWORD now = GetTickCount();
6481
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006482 return (long)now - (long)start_tick;
6483}
Bram Moolenaar51628222016-12-01 23:03:28 +01006484# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006485#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006486
6487#if defined(FEAT_JOB_CHANNEL) \
6488 || (defined(UNIX) && (!defined(USE_SYSTEM) \
6489 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
6490 || defined(PROTO)
6491/*
6492 * Parse "cmd" and put the white-separated parts in "argv".
6493 * "argv" is an allocated array with "argc" entries and room for 4 more.
6494 * Returns FAIL when out of memory.
6495 */
6496 int
6497mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
6498{
6499 int i;
6500 char_u *p, *d;
6501 int inquote;
6502
6503 /*
6504 * Do this loop twice:
6505 * 1: find number of arguments
6506 * 2: separate them and build argv[]
6507 */
6508 for (i = 0; i < 2; ++i)
6509 {
6510 p = skipwhite(cmd);
6511 inquote = FALSE;
6512 *argc = 0;
6513 for (;;)
6514 {
6515 if (i == 1)
6516 (*argv)[*argc] = (char *)p;
6517 ++*argc;
6518 d = p;
6519 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
6520 {
6521 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006522 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02006523 inquote = !inquote;
6524 else
6525 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006526 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02006527 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02006528 // First pass: skip over "\ " and "\"".
6529 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02006530 ++p;
6531 }
6532 if (i == 1)
6533 *d++ = *p;
6534 }
6535 ++p;
6536 }
6537 if (*p == NUL)
6538 {
6539 if (i == 1)
6540 *d++ = NUL;
6541 break;
6542 }
6543 if (i == 1)
6544 *d++ = NUL;
6545 p = skipwhite(p + 1);
6546 }
6547 if (*argv == NULL)
6548 {
6549 if (use_shcf)
6550 {
6551 /* Account for possible multiple args in p_shcf. */
6552 p = p_shcf;
6553 for (;;)
6554 {
6555 p = skiptowhite(p);
6556 if (*p == NUL)
6557 break;
6558 ++*argc;
6559 p = skipwhite(p);
6560 }
6561 }
6562
6563 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
6564 if (*argv == NULL) /* out of memory */
6565 return FAIL;
6566 }
6567 }
6568 return OK;
6569}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006570
6571# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6572/*
6573 * Build "argv[argc]" from the string "cmd".
6574 * "argv[argc]" is set to NULL;
6575 * Return FAIL when out of memory.
6576 */
6577 int
6578build_argv_from_string(char_u *cmd, char ***argv, int *argc)
6579{
6580 char_u *cmd_copy;
6581 int i;
6582
6583 /* Make a copy, parsing will modify "cmd". */
6584 cmd_copy = vim_strsave(cmd);
6585 if (cmd_copy == NULL
6586 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
6587 {
6588 vim_free(cmd_copy);
6589 return FAIL;
6590 }
6591 for (i = 0; i < *argc; i++)
6592 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
6593 (*argv)[*argc] = NULL;
6594 vim_free(cmd_copy);
6595 return OK;
6596}
6597
6598/*
6599 * Build "argv[argc]" from the list "l".
6600 * "argv[argc]" is set to NULL;
6601 * Return FAIL when out of memory.
6602 */
6603 int
6604build_argv_from_list(list_T *l, char ***argv, int *argc)
6605{
6606 listitem_T *li;
6607 char_u *s;
6608
6609 /* Pass argv[] to mch_call_shell(). */
6610 *argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
6611 if (*argv == NULL)
6612 return FAIL;
6613 *argc = 0;
6614 for (li = l->lv_first; li != NULL; li = li->li_next)
6615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006616 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006617 if (s == NULL)
6618 {
6619 int i;
6620
6621 for (i = 0; i < *argc; ++i)
6622 vim_free((*argv)[i]);
6623 return FAIL;
6624 }
6625 (*argv)[*argc] = (char *)vim_strsave(s);
6626 *argc += 1;
6627 }
6628 (*argv)[*argc] = NULL;
6629 return OK;
6630}
6631# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02006632#endif