blob: fd432cdba3e05e4a4825d6b5a4b5b2e8a44e751f [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/*
51 * Get the screen position of character col with a coladd in the cursor line.
52 */
53 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010054getviscol2(colnr_T col, colnr_T coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055{
56 colnr_T x;
57 pos_T pos;
58
59 pos.lnum = curwin->w_cursor.lnum;
60 pos.col = col;
61 pos.coladd = coladd;
62 getvvcol(curwin, &pos, &x, NULL, NULL);
63 return (int)x;
64}
65
66/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000067 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 * cursor in that column.
69 * The caller must have saved the cursor line for undo!
70 */
71 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010072coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
74 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
75
76 if (wcol == MAXCOL)
77 curwin->w_valid &= ~VALID_VIRTCOL;
78 else
79 {
80 /* Virtcol is valid */
81 curwin->w_valid |= VALID_VIRTCOL;
82 curwin->w_virtcol = wcol;
83 }
84 return rc;
85}
86#endif
87
88/*
89 * Try to advance the Cursor to the specified screen column.
90 * If virtual editing: fine tune the cursor position.
91 * Note that all virtual positions off the end of a line should share
92 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
93 * beginning at coladd 0.
94 *
95 * return OK if desired column is reached, FAIL if not
96 */
97 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010098coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099{
100 int rc = getvpos(&curwin->w_cursor, wcol);
101
102 if (wcol == MAXCOL || rc == FAIL)
103 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000104 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000106 /* Virtcol is valid when not on a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 curwin->w_valid |= VALID_VIRTCOL;
108 curwin->w_virtcol = wcol;
109 }
110 return rc;
111}
112
113/*
114 * Return in "pos" the position of the cursor advanced to screen column "wcol".
115 * return OK if desired column is reached, FAIL if not
116 */
117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100118getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120#ifdef FEAT_VIRTUALEDIT
121 return coladvance2(pos, FALSE, virtual_active(), wcol);
122}
123
124 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100125coladvance2(
126 pos_T *pos,
127 int addspaces, /* change the text to achieve our goal? */
128 int finetune, /* change char offset for the exact column */
129 colnr_T wcol) /* column to move to */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
131#endif
132 int idx;
133 char_u *ptr;
134 char_u *line;
135 colnr_T col = 0;
136 int csize = 0;
137 int one_more;
138#ifdef FEAT_LINEBREAK
139 int head = 0;
140#endif
141
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000142 one_more = (State & INSERT)
143 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000144 || (VIsual_active && *p_sel != 'o')
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000145#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000146 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000147#endif
148 ;
Bram Moolenaara1381de2009-11-03 15:44:21 +0000149 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151 if (wcol >= MAXCOL)
152 {
153 idx = (int)STRLEN(line) - 1 + one_more;
154 col = wcol;
155
156#ifdef FEAT_VIRTUALEDIT
157 if ((addspaces || finetune) && !VIsual_active)
158 {
159 curwin->w_curswant = linetabsize(line) + one_more;
160 if (curwin->w_curswant > 0)
161 --curwin->w_curswant;
162 }
163#endif
164 }
165 else
166 {
167#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar02631462017-09-22 15:20:32 +0200168 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
Bram Moolenaarebefac62005-12-28 22:39:57 +0000170 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 && curwin->w_width != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 && wcol >= (colnr_T)width)
174 {
175 csize = linetabsize(line);
176 if (csize > 0)
177 csize--;
178
Bram Moolenaarebefac62005-12-28 22:39:57 +0000179 if (wcol / width > (colnr_T)csize / width
180 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 {
182 /* In case of line wrapping don't move the cursor beyond the
Bram Moolenaarebefac62005-12-28 22:39:57 +0000183 * right screen edge. In Insert mode allow going just beyond
184 * the last character (like what happens when typing and
185 * reaching the right window edge). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 wcol = (csize / width + 1) * width - 1;
187 }
188 }
189#endif
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 ptr = line;
192 while (col <= wcol && *ptr != NUL)
193 {
194 /* Count a tab for what it's worth (if list mode not on) */
195#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200196 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100197 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200199 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#endif
201 col += csize;
202 }
203 idx = (int)(ptr - line);
204 /*
205 * Handle all the special cases. The virtual_active() check
206 * is needed to ensure that a virtual position off the end of
207 * a line has the correct indexing. The one_more comparison
208 * replaces an explicit add of one_more later on.
209 */
210 if (col > wcol || (!virtual_active() && one_more == 0))
211 {
212 idx -= 1;
213# ifdef FEAT_LINEBREAK
214 /* Don't count the chars from 'showbreak'. */
215 csize -= head;
216# endif
217 col -= csize;
218 }
219
220#ifdef FEAT_VIRTUALEDIT
221 if (virtual_active()
222 && addspaces
223 && ((col != wcol && col != wcol + 1) || csize > 1))
224 {
225 /* 'virtualedit' is set: The difference between wcol and col is
226 * filled with spaces. */
227
228 if (line[idx] == NUL)
229 {
230 /* Append spaces */
231 int correct = wcol - col;
232 char_u *newline = alloc(idx + correct + 1);
233 int t;
234
235 if (newline == NULL)
236 return FAIL;
237
238 for (t = 0; t < idx; ++t)
239 newline[t] = line[t];
240
241 for (t = 0; t < correct; ++t)
242 newline[t + idx] = ' ';
243
244 newline[idx + correct] = NUL;
245
246 ml_replace(pos->lnum, newline, FALSE);
247 changed_bytes(pos->lnum, (colnr_T)idx);
248 idx += correct;
249 col = wcol;
250 }
251 else
252 {
253 /* Break a tab */
254 int linelen = (int)STRLEN(line);
255 int correct = wcol - col - csize + 1; /* negative!! */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000256 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 int t, s = 0;
258 int v;
259
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000260 if (-correct > csize)
261 return FAIL;
262
263 newline = alloc(linelen + csize);
264 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 return FAIL;
266
267 for (t = 0; t < linelen; t++)
268 {
269 if (t != idx)
270 newline[s++] = line[t];
271 else
272 for (v = 0; v < csize; v++)
273 newline[s++] = ' ';
274 }
275
276 newline[linelen + csize - 1] = NUL;
277
278 ml_replace(pos->lnum, newline, FALSE);
279 changed_bytes(pos->lnum, idx);
280 idx += (csize - 1 + correct);
281 col += correct;
282 }
283 }
284#endif
285 }
286
287 if (idx < 0)
288 pos->col = 0;
289 else
290 pos->col = idx;
291
292#ifdef FEAT_VIRTUALEDIT
293 pos->coladd = 0;
294
295 if (finetune)
296 {
297 if (wcol == MAXCOL)
298 {
299 /* The width of the last character is used to set coladd. */
300 if (!one_more)
301 {
302 colnr_T scol, ecol;
303
304 getvcol(curwin, pos, &scol, NULL, &ecol);
305 pos->coladd = ecol - scol;
306 }
307 }
308 else
309 {
310 int b = (int)wcol - (int)col;
311
312 /* The difference between wcol and col is used to set coladd. */
Bram Moolenaar02631462017-09-22 15:20:32 +0200313 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 pos->coladd = b;
315
316 col += b;
317 }
318 }
319#endif
320
321#ifdef FEAT_MBYTE
Bram Moolenaara1381de2009-11-03 15:44:21 +0000322 /* prevent from moving onto a trail byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200324 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#endif
326
327 if (col < wcol)
328 return FAIL;
329 return OK;
330}
331
332/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000333 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100336inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
338 return inc(&curwin->w_cursor);
339}
340
Bram Moolenaar446cb832008-06-24 21:56:24 +0000341/*
342 * Increment the line pointer "lp" crossing line boundaries as necessary.
343 * Return 1 when going to the next line.
344 * Return 2 when moving forward onto a NUL at the end of the line).
345 * Return -1 when at the end of file.
346 * Return 0 otherwise.
347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100349inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 /* when searching position may be set to end of a line */
354 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100356 p = ml_get_pos(lp);
357 if (*p != NUL) /* still within line, move to next char (may be NUL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100359#ifdef FEAT_MBYTE
360 if (has_mbyte)
361 {
362 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100364 lp->col += l;
365 return ((p[l] != NUL) ? 0 : 2);
366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#endif
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100368 lp->col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100370 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#endif
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100372 return ((p[1] != NUL) ? 0 : 2);
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
376 {
377 lp->col = 0;
378 lp->lnum++;
379#ifdef FEAT_VIRTUALEDIT
380 lp->coladd = 0;
381#endif
382 return 1;
383 }
384 return -1;
385}
386
387/*
388 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
389 */
390 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100391incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392{
393 int r;
394
395 if ((r = inc(lp)) >= 1 && lp->col)
396 r = inc(lp);
397 return r;
398}
399
400/*
401 * dec(p)
402 *
403 * Decrement the line pointer 'p' crossing line boundaries as necessary.
404 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
405 */
406 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100407dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100409 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410}
411
412 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100413dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 char_u *p;
416
417#ifdef FEAT_VIRTUALEDIT
418 lp->coladd = 0;
419#endif
420 if (lp->col > 0) /* still within line */
421 {
422 lp->col--;
423#ifdef FEAT_MBYTE
424 if (has_mbyte)
425 {
426 p = ml_get(lp->lnum);
427 lp->col -= (*mb_head_off)(p, p + lp->col);
428 }
429#endif
430 return 0;
431 }
432 if (lp->lnum > 1) /* there is a prior line */
433 {
434 lp->lnum--;
435 p = ml_get(lp->lnum);
436 lp->col = (colnr_T)STRLEN(p);
437#ifdef FEAT_MBYTE
438 if (has_mbyte)
439 lp->col -= (*mb_head_off)(p, p + lp->col);
440#endif
441 return 1;
442 }
443 return -1; /* at start of file */
444}
445
446/*
447 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
448 */
449 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100450decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 int r;
453
454 if ((r = dec(lp)) == 1 && lp->col)
455 r = dec(lp);
456 return r;
457}
458
459/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200460 * Get the line number relative to the current cursor position, i.e. the
461 * difference between line number and cursor position. Only look for lines that
462 * can be visible, folded lines don't count.
463 */
464 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100465get_cursor_rel_lnum(
466 win_T *wp,
467 linenr_T lnum) /* line number to get the result for */
Bram Moolenaar64486672010-05-16 15:46:46 +0200468{
469 linenr_T cursor = wp->w_cursor.lnum;
470 linenr_T retval = 0;
471
472#ifdef FEAT_FOLDING
473 if (hasAnyFolding(wp))
474 {
475 if (lnum > cursor)
476 {
477 while (lnum > cursor)
478 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100479 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200480 /* if lnum and cursor are in the same fold,
481 * now lnum <= cursor */
482 if (lnum > cursor)
483 retval++;
484 lnum--;
485 }
486 }
487 else if (lnum < cursor)
488 {
489 while (lnum < cursor)
490 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100491 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200492 /* if lnum and cursor are in the same fold,
493 * now lnum >= cursor */
494 if (lnum < cursor)
495 retval--;
496 lnum++;
497 }
498 }
499 /* else if (lnum == cursor)
500 * retval = 0;
501 */
502 }
503 else
504#endif
505 retval = lnum - cursor;
506
507 return retval;
508}
509
510/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200511 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
512 * This allows for the col to be on the NUL byte.
513 */
514 void
515check_pos(buf_T *buf, pos_T *pos)
516{
517 char_u *line;
518 colnr_T len;
519
520 if (pos->lnum > buf->b_ml.ml_line_count)
521 pos->lnum = buf->b_ml.ml_line_count;
522
523 if (pos->col > 0)
524 {
525 line = ml_get_buf(buf, pos->lnum, FALSE);
526 len = (colnr_T)STRLEN(line);
527 if (pos->col > len)
528 pos->col = len;
529 }
530}
531
532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 * Make sure curwin->w_cursor.lnum is valid.
534 */
535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
539 {
540#ifdef FEAT_FOLDING
541 /* If there is a closed fold at the end of the file, put the cursor in
542 * its first line. Otherwise in the last line. */
543 if (!hasFolding(curbuf->b_ml.ml_line_count,
544 &curwin->w_cursor.lnum, NULL))
545#endif
546 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
547 }
548 if (curwin->w_cursor.lnum <= 0)
549 curwin->w_cursor.lnum = 1;
550}
551
552/*
553 * Make sure curwin->w_cursor.col is valid.
554 */
555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100556check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200558 check_cursor_col_win(curwin);
559}
560
561/*
562 * Make sure win->w_cursor.col is valid.
563 */
564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100565check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200566{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 colnr_T len;
568#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200569 colnr_T oldcol = win->w_cursor.col;
570 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571#endif
572
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200573 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200575 win->w_cursor.col = 0;
576 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000578 /* Allow cursor past end-of-line when:
579 * - in Insert mode or restarting Insert mode
580 * - in Visual mode and 'selection' isn't "old"
581 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000582 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000584#ifdef FEAT_VIRTUALEDIT
585 || (ve_flags & VE_ONEMORE)
586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000590 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200591 win->w_cursor.col = len - 1;
Bram Moolenaar87c19962007-04-26 08:54:21 +0000592#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200593 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000594 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200595 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000596#endif
597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200599 else if (win->w_cursor.col < 0)
600 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602#ifdef FEAT_VIRTUALEDIT
603 /* If virtual editing is on, we can leave the cursor on the old position,
604 * only we must set it to virtual. But don't do it when at the end of the
605 * line. */
606 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200607 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000609 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200610 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200611 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200612 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200613
614 /* Make sure that coladd is not more than the char width.
615 * Not for the last character, coladd is then used when the cursor
616 * is actually after the last character. */
617 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200618 {
619 int cs, ce;
620
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200621 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
622 if (win->w_cursor.coladd > ce - cs)
623 win->w_cursor.coladd = ce - cs;
624 }
625 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000626 else
627 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200628 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#endif
631}
632
633/*
634 * make sure curwin->w_cursor in on a valid character
635 */
636 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100637check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
639 check_cursor_lnum();
640 check_cursor_col();
641}
642
643#if defined(FEAT_TEXTOBJ) || defined(PROTO)
644/*
645 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
646 * Allow it when in Visual mode and 'selection' is not "old".
647 */
648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100649adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 && gchar_cursor() == NUL)
654 --curwin->w_cursor.col;
655}
656#endif
657
658/*
659 * When curwin->w_leftcol has changed, adjust the cursor position.
660 * Return TRUE if the cursor was moved.
661 */
662 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100663leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
665 long lastcol;
666 colnr_T s, e;
667 int retval = FALSE;
668
669 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200670 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 validate_virtcol();
672
673 /*
674 * If the cursor is right or left of the screen, move it to last or first
675 * character.
676 */
677 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
678 {
679 retval = TRUE;
680 coladvance((colnr_T)(lastcol - p_siso));
681 }
682 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
683 {
684 retval = TRUE;
685 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
686 }
687
688 /*
689 * If the start of the character under the cursor is not on the screen,
690 * advance the cursor one more char. If this fails (last char of the
691 * line) adjust the scrolling.
692 */
693 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
694 if (e > (colnr_T)lastcol)
695 {
696 retval = TRUE;
697 coladvance(s - 1);
698 }
699 else if (s < curwin->w_leftcol)
700 {
701 retval = TRUE;
702 if (coladvance(e + 1) == FAIL) /* there isn't another character */
703 {
704 curwin->w_leftcol = s; /* adjust w_leftcol instead */
705 changed_cline_bef_curs();
706 }
707 }
708
709 if (retval)
710 curwin->w_set_curswant = TRUE;
711 redraw_later(NOT_VALID);
712 return retval;
713}
714
715/**********************************************************************
716 * Various routines dealing with allocation and deallocation of memory.
717 */
718
719#if defined(MEM_PROFILE) || defined(PROTO)
720
721# define MEM_SIZES 8200
722static long_u mem_allocs[MEM_SIZES];
723static long_u mem_frees[MEM_SIZES];
724static long_u mem_allocated;
725static long_u mem_freed;
726static long_u mem_peak;
727static long_u num_alloc;
728static long_u num_freed;
729
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100730static void mem_pre_alloc_s(size_t *sizep);
731static void mem_pre_alloc_l(long_u *sizep);
732static void mem_post_alloc(void **pp, size_t size);
733static void mem_pre_free(void **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100736mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738 *sizep += sizeof(size_t);
739}
740
741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100742mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 *sizep += sizeof(size_t);
745}
746
747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100748mem_post_alloc(
749 void **pp,
750 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 if (*pp == NULL)
753 return;
754 size -= sizeof(size_t);
755 *(long_u *)*pp = size;
756 if (size <= MEM_SIZES-1)
757 mem_allocs[size-1]++;
758 else
759 mem_allocs[MEM_SIZES-1]++;
760 mem_allocated += size;
761 if (mem_allocated - mem_freed > mem_peak)
762 mem_peak = mem_allocated - mem_freed;
763 num_alloc++;
764 *pp = (void *)((char *)*pp + sizeof(size_t));
765}
766
767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100768mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 long_u size;
771
772 *pp = (void *)((char *)*pp - sizeof(size_t));
773 size = *(size_t *)*pp;
774 if (size <= MEM_SIZES-1)
775 mem_frees[size-1]++;
776 else
777 mem_frees[MEM_SIZES-1]++;
778 mem_freed += size;
779 num_freed++;
780}
781
782/*
783 * called on exit via atexit()
784 */
785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100786vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 int i, j;
789
790 printf("\r\n");
791 j = 0;
792 for (i = 0; i < MEM_SIZES - 1; i++)
793 {
794 if (mem_allocs[i] || mem_frees[i])
795 {
796 if (mem_frees[i] > mem_allocs[i])
797 printf("\r\n%s", _("ERROR: "));
798 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
799 j++;
800 if (j > 3)
801 {
802 j = 0;
803 printf("\r\n");
804 }
805 }
806 }
807
808 i = MEM_SIZES - 1;
809 if (mem_allocs[i])
810 {
811 printf("\r\n");
812 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200813 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
815 }
816
817 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
818 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
819 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
820 num_alloc, num_freed);
821}
822
823#endif /* MEM_PROFILE */
824
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100825#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100826static int alloc_does_fail(long_u size);
Bram Moolenaara260b872016-01-15 20:48:22 +0100827
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100828 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100829alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100830{
831 if (alloc_fail_countdown == 0)
832 {
833 if (--alloc_fail_repeat <= 0)
834 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100835 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100836 return TRUE;
837 }
838 --alloc_fail_countdown;
839 return FALSE;
840}
841#endif
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843/*
844 * Some memory is reserved for error messages and for being able to
845 * call mf_release_all(), which needs some memory for mf_trans_add().
846 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100847#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200848#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000851 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 * Use lalloc for larger blocks.
853 */
854 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100855alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856{
857 return (lalloc((long_u)size, TRUE));
858}
859
860/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100861 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100862 */
863 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100864alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100865{
866#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100867 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100868 return NULL;
869#endif
870 return (lalloc((long_u)size, TRUE));
871}
872
873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 * Allocate memory and set all bytes to zero.
875 */
876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 char_u *p;
880
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200881 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (p != NULL)
883 (void)vim_memset(p, 0, (size_t)size);
884 return p;
885}
886
887/*
888 * alloc() with check for maximum line length
889 */
890 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100891alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200893#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (sizeof(int) == 2 && size > 0x7fff)
895 {
896 /* Don't hide this message */
897 emsg_silent = 0;
898 EMSG(_("E340: Line is becoming too long"));
899 return NULL;
900 }
901#endif
902 return (lalloc((long_u)size, TRUE));
903}
904
905/*
906 * Allocate memory like lalloc() and set all bytes to zero.
907 */
908 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100909lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 char_u *p;
912
913 p = (lalloc(size, message));
914 if (p != NULL)
915 (void)vim_memset(p, 0, (size_t)size);
916 return p;
917}
918
919/*
920 * Low level memory allocation function.
921 * This is used often, KEEP IT FAST!
922 */
923 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100924lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
926 char_u *p; /* pointer to new storage space */
927 static int releasing = FALSE; /* don't do mf_release_all() recursive */
928 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100929#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 static long_u allocated = 0; /* allocated since last avail check */
931#endif
932
933 /* Safety check for allocating zero bytes */
934 if (size == 0)
935 {
936 /* Don't hide this message */
937 emsg_silent = 0;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100938 IEMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 return NULL;
940 }
941
942#ifdef MEM_PROFILE
943 mem_pre_alloc_l(&size);
944#endif
945
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 /*
947 * Loop when out of memory: Try to release some memfile blocks and
948 * if some blocks are released call malloc again.
949 */
950 for (;;)
951 {
952 /*
953 * Handle three kind of systems:
954 * 1. No check for available memory: Just return.
955 * 2. Slow check for available memory: call mch_avail_mem() after
956 * allocating KEEP_ROOM amount of memory.
957 * 3. Strict check for available memory: call mch_avail_mem()
958 */
959 if ((p = (char_u *)malloc((size_t)size)) != NULL)
960 {
961#ifndef HAVE_AVAIL_MEM
962 /* 1. No check for available memory: Just return. */
963 goto theend;
964#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 /* 2. Slow check for available memory: call mch_avail_mem() after
966 * allocating (KEEP_ROOM / 2) amount of memory. */
967 allocated += size;
968 if (allocated < KEEP_ROOM / 2)
969 goto theend;
970 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200973 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000975 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 p = NULL;
977 }
978 else
979 goto theend;
980#endif
981 }
982 /*
983 * Remember that mf_release_all() is being called to avoid an endless
984 * loop, because mf_release_all() may call alloc() recursively.
985 */
986 if (releasing)
987 break;
988 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000989
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100990 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000991 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 releasing = FALSE;
994 if (!try_again)
995 break;
996 }
997
998 if (message && p == NULL)
999 do_outofmem_msg(size);
1000
1001theend:
1002#ifdef MEM_PROFILE
1003 mem_post_alloc((void **)&p, (size_t)size);
1004#endif
1005 return p;
1006}
1007
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001008/*
1009 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001010 */
1011 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001012lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001013{
1014#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001015 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001016 return NULL;
1017#endif
1018 return (lalloc((long_u)size, message));
1019}
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#if defined(MEM_PROFILE) || defined(PROTO)
1022/*
1023 * realloc() with memory profiling.
1024 */
1025 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001026mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 void *p;
1029
1030 mem_pre_free(&ptr);
1031 mem_pre_alloc_s(&size);
1032
1033 p = realloc(ptr, size);
1034
1035 mem_post_alloc(&p, size);
1036
1037 return p;
1038}
1039#endif
1040
1041/*
1042* Avoid repeating the error message many times (they take 1 second each).
1043* Did_outofmem_msg is reset when a character is read.
1044*/
1045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001046do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 if (!did_outofmem_msg)
1049 {
1050 /* Don't hide this message */
1051 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001052
1053 /* Must come first to avoid coming back here when printing the error
1054 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001056
1057 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059}
1060
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001061#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001062
1063# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001064static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001065# endif
1066
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001067/*
1068 * Free everything that we allocated.
1069 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001070 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1071 * surprised if Vim crashes...
1072 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001073 */
1074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001076{
1077 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001078
1079 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1080 * Don't try freeing everything again. */
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001081 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001082 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001083 entered_free_all_mem = TRUE;
Bram Moolenaara9673212016-06-01 22:21:06 +02001084
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001085# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001086 /* Don't want to trigger autocommands from here on. */
1087 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001088# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001089
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001090 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1091 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001092 if (first_tabpage->tp_next != NULL)
1093 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001094 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001095 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001096
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001097# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001098 /* Free all spell info. */
1099 spell_free_all();
1100# endif
1101
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001102# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001103 /* Clear user commands (before deleting buffers). */
1104 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001105# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001106
1107# ifdef FEAT_MENU
1108 /* Clear menus. */
1109 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001110# ifdef FEAT_MULTI_LANG
1111 do_cmdline_cmd((char_u *)"menutranslate clear");
1112# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001113# endif
1114
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001115 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001116 do_cmdline_cmd((char_u *)"lmapclear");
1117 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001118 do_cmdline_cmd((char_u *)"mapclear");
1119 do_cmdline_cmd((char_u *)"mapclear!");
1120 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001121# if defined(FEAT_EVAL)
1122 do_cmdline_cmd((char_u *)"breakdel *");
1123# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001124# if defined(FEAT_PROFILE)
1125 do_cmdline_cmd((char_u *)"profdel *");
1126# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001127# if defined(FEAT_KEYMAP)
1128 do_cmdline_cmd((char_u *)"set keymap=");
1129#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001130
1131# ifdef FEAT_TITLE
1132 free_titles();
1133# endif
1134# if defined(FEAT_SEARCHPATH)
1135 free_findfile();
1136# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001137
1138 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001139# if defined(FEAT_AUTOCMD)
1140 free_all_autocmds();
1141# endif
1142 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001143 free_all_marks();
1144 alist_clear(&global_alist);
1145 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001146# if defined(FEAT_CMDL_COMPL)
1147 free_users();
1148# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001149 free_search_patterns();
1150 free_old_sub();
1151 free_last_insert();
1152 free_prev_shellcmd();
1153 free_regexp_stuff();
1154 free_tag_stuff();
1155 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001156# ifdef FEAT_SIGNS
1157 free_signs();
1158# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001159# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001160 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001161# endif
1162# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001163 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001164# endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001165 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001166
1167 /* Free some global vars. */
1168 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001169# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001170 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001171# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001172 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001173# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001174 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001175# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001176 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001177 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001178
1179 /* Clear cmdline history. */
1180 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001181# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001182 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001183# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001184
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001185#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001186 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001187 win_T *win;
1188 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001189
1190 qf_free_all(NULL);
1191 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001192 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001193 qf_free_all(win);
1194 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001195#endif
1196
1197 /* Close all script inputs. */
1198 close_all_scripts();
1199
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001200 /* Destroy all windows. Must come before freeing buffers. */
1201 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001202
Bram Moolenaar4f198282017-10-23 21:53:30 +02001203 /* Free all option values. Must come after closing windows. */
1204 free_all_options();
1205
Bram Moolenaar2a329742008-04-01 12:53:43 +00001206 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1207 * were freed already. */
1208#ifdef FEAT_AUTOCHDIR
1209 p_acd = FALSE;
1210#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001211 for (buf = firstbuf; buf != NULL; )
1212 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001213 bufref_T bufref;
1214
1215 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001216 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001217 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001218 if (bufref_valid(&bufref))
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001219 buf = nextbuf; /* didn't work, try next one */
1220 else
1221 buf = firstbuf;
1222 }
1223
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001224#ifdef FEAT_ARABIC
1225 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001226#endif
1227
1228 /* Clear registers. */
1229 clear_registers();
1230 ResetRedobuff();
1231 ResetRedobuff();
1232
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001233#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001234 vim_free(serverDelayedStartName);
1235#endif
1236
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001237 /* highlight info */
1238 free_highlight();
1239
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001240 reset_last_sourcing();
1241
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001242 free_tabpage(first_tabpage);
1243 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001244
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001245# ifdef UNIX
1246 /* Machine-specific free. */
1247 mch_free_mem();
1248# endif
1249
1250 /* message history */
1251 for (;;)
1252 if (delete_first_msg() == FAIL)
1253 break;
1254
Bram Moolenaar655da312016-05-28 22:22:34 +02001255# ifdef FEAT_JOB_CHANNEL
1256 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001257# endif
Bram Moolenaar623e2632016-07-30 22:47:56 +02001258#ifdef FEAT_TIMERS
1259 timer_free_all();
1260#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001261# ifdef FEAT_EVAL
1262 /* must be after channel_free_all() with unrefs partials */
1263 eval_clear();
1264# endif
1265# ifdef FEAT_JOB_CHANNEL
1266 /* must be after eval_clear() with unrefs jobs */
1267 job_free_all();
1268# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001269
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001270 free_termoptions();
1271
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001272 /* screenlines (can't display anything now!) */
1273 free_screenlines();
1274
1275#if defined(USE_XSMP)
1276 xsmp_close();
1277#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001278#ifdef FEAT_GUI_GTK
1279 gui_mch_free_all();
1280#endif
1281 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001282
1283 vim_free(IObuff);
1284 vim_free(NameBuff);
1285}
1286#endif
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001289 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
1291 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 char_u *p;
1295 unsigned len;
1296
1297 len = (unsigned)STRLEN(string) + 1;
1298 p = alloc(len);
1299 if (p != NULL)
1300 mch_memmove(p, string, (size_t)len);
1301 return p;
1302}
1303
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001304/*
1305 * Copy up to "len" bytes of "string" into newly allocated memory and
1306 * terminate with a NUL.
1307 * The allocated memory always has size "len + 1", also when "string" is
1308 * shorter.
1309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001311vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 char_u *p;
1314
1315 p = alloc((unsigned)(len + 1));
1316 if (p != NULL)
1317 {
1318 STRNCPY(p, string, len);
1319 p[len] = NUL;
1320 }
1321 return p;
1322}
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324/*
1325 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1326 * by a backslash.
1327 */
1328 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001329vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001331 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332}
1333
1334/*
1335 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1336 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001337 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 */
1339 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001340vim_strsave_escaped_ext(
1341 char_u *string,
1342 char_u *esc_chars,
1343 int cc,
1344 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346 char_u *p;
1347 char_u *p2;
1348 char_u *escaped_string;
1349 unsigned length;
1350#ifdef FEAT_MBYTE
1351 int l;
1352#endif
1353
1354 /*
1355 * First count the number of backslashes required.
1356 * Then allocate the memory and insert them.
1357 */
1358 length = 1; /* count the trailing NUL */
1359 for (p = string; *p; p++)
1360 {
1361#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001362 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
1364 length += l; /* count a multibyte char */
1365 p += l - 1;
1366 continue;
1367 }
1368#endif
1369 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1370 ++length; /* count a backslash */
1371 ++length; /* count an ordinary char */
1372 }
1373 escaped_string = alloc(length);
1374 if (escaped_string != NULL)
1375 {
1376 p2 = escaped_string;
1377 for (p = string; *p; p++)
1378 {
1379#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001380 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
1382 mch_memmove(p2, p, (size_t)l);
1383 p2 += l;
1384 p += l - 1; /* skip multibyte char */
1385 continue;
1386 }
1387#endif
1388 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001389 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 *p2++ = *p;
1391 }
1392 *p2 = NUL;
1393 }
1394 return escaped_string;
1395}
1396
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001397/*
1398 * Return TRUE when 'shell' has "csh" in the tail.
1399 */
1400 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001401csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001402{
1403 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1404}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001405
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001406/*
1407 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001408 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001409 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001410 * Escape a newline, depending on the 'shell' option.
1411 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1412 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001413 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001414 * Returns the result in allocated memory, NULL if we have run out.
1415 */
1416 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001417vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001418{
1419 unsigned length;
1420 char_u *p;
1421 char_u *d;
1422 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001423 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001424 int csh_like;
1425
1426 /* Only csh and similar shells expand '!' within single quotes. For sh and
1427 * the like we must not put a backslash before it, it will be taken
1428 * literally. If do_special is set the '!' will be escaped twice.
1429 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001430 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001431
1432 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001433 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001434 for (p = string; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001435 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001436# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001437 if (!p_ssl)
1438 {
1439 if (*p == '"')
1440 ++length; /* " -> "" */
1441 }
1442 else
1443# endif
1444 if (*p == '\'')
1445 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001446 if ((*p == '\n' && (csh_like || do_newline))
1447 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001448 {
1449 ++length; /* insert backslash */
1450 if (csh_like && do_special)
1451 ++length; /* insert backslash */
1452 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001453 if (do_special && find_cmdline_var(p, &l) >= 0)
1454 {
1455 ++length; /* insert backslash */
1456 p += l - 1;
1457 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001458 }
1459
1460 /* Allocate memory for the result and fill it. */
1461 escaped_string = alloc(length);
1462 if (escaped_string != NULL)
1463 {
1464 d = escaped_string;
1465
1466 /* add opening quote */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001467# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001468 if (!p_ssl)
1469 *d++ = '"';
1470 else
1471# endif
1472 *d++ = '\'';
1473
1474 for (p = string; *p != NUL; )
1475 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001476# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001477 if (!p_ssl)
1478 {
1479 if (*p == '"')
1480 {
1481 *d++ = '"';
1482 *d++ = '"';
1483 ++p;
1484 continue;
1485 }
1486 }
1487 else
1488# endif
1489 if (*p == '\'')
1490 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001491 *d++ = '\'';
1492 *d++ = '\\';
1493 *d++ = '\'';
1494 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001495 ++p;
1496 continue;
1497 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001498 if ((*p == '\n' && (csh_like || do_newline))
1499 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001500 {
1501 *d++ = '\\';
1502 if (csh_like && do_special)
1503 *d++ = '\\';
1504 *d++ = *p++;
1505 continue;
1506 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001507 if (do_special && find_cmdline_var(p, &l) >= 0)
1508 {
1509 *d++ = '\\'; /* insert backslash */
1510 while (--l >= 0) /* copy the var */
1511 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001512 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001513 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001514
1515 MB_COPY_CHAR(p, d);
1516 }
1517
1518 /* add terminating quote and finish with a NUL */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001519# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001520 if (!p_ssl)
1521 *d++ = '"';
1522 else
1523# endif
1524 *d++ = '\'';
1525 *d = NUL;
1526 }
1527
1528 return escaped_string;
1529}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531/*
1532 * Like vim_strsave(), but make all characters uppercase.
1533 * This uses ASCII lower-to-upper case translation, language independent.
1534 */
1535 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 char_u *p1;
1539
1540 p1 = vim_strsave(string);
1541 vim_strup(p1);
1542 return p1;
1543}
1544
1545/*
1546 * Like vim_strnsave(), but make all characters uppercase.
1547 * This uses ASCII lower-to-upper case translation, language independent.
1548 */
1549 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001550vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 char_u *p1;
1553
1554 p1 = vim_strnsave(string, len);
1555 vim_strup(p1);
1556 return p1;
1557}
1558
1559/*
1560 * ASCII lower-to-upper case translation, language independent.
1561 */
1562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001563vim_strup(
1564 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 char_u *p2;
1567 int c;
1568
1569 if (p != NULL)
1570 {
1571 p2 = p;
1572 while ((c = *p2) != NUL)
1573#ifdef EBCDIC
1574 *p2++ = isalpha(c) ? toupper(c) : c;
1575#else
1576 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1577#endif
1578 }
1579}
1580
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001581#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001582/*
1583 * Make string "s" all upper-case and return it in allocated memory.
1584 * Handles multi-byte characters as well as possible.
1585 * Returns NULL when out of memory.
1586 */
1587 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001588strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001589{
1590 char_u *p;
1591 char_u *res;
1592
1593 res = p = vim_strsave(orig);
1594
1595 if (res != NULL)
1596 while (*p != NUL)
1597 {
1598# ifdef FEAT_MBYTE
1599 int l;
1600
1601 if (enc_utf8)
1602 {
1603 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001604 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001605 char_u *s;
1606
1607 c = utf_ptr2char(p);
1608 uc = utf_toupper(c);
1609
1610 /* Reallocate string when byte count changes. This is rare,
1611 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001612 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001613 newl = utf_char2len(uc);
1614 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001615 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001616 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001617 if (s == NULL)
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001618 {
1619 vim_free(res);
1620 return NULL;
1621 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001622 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001623 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001624 p = s + (p - res);
1625 vim_free(res);
1626 res = s;
1627 }
1628
1629 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001630 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001631 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001632 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001633 p += l; /* skip multi-byte character */
1634 else
1635# endif
1636 {
1637 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1638 p++;
1639 }
1640 }
1641
1642 return res;
1643}
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001644
1645/*
1646 * Make string "s" all lower-case and return it in allocated memory.
1647 * Handles multi-byte characters as well as possible.
1648 * Returns NULL when out of memory.
1649 */
1650 char_u *
1651strlow_save(char_u *orig)
1652{
1653 char_u *p;
1654 char_u *res;
1655
1656 res = p = vim_strsave(orig);
1657
1658 if (res != NULL)
1659 while (*p != NUL)
1660 {
1661# ifdef FEAT_MBYTE
1662 int l;
1663
1664 if (enc_utf8)
1665 {
1666 int c, lc;
1667 int newl;
1668 char_u *s;
1669
1670 c = utf_ptr2char(p);
1671 lc = utf_tolower(c);
1672
1673 /* Reallocate string when byte count changes. This is rare,
1674 * thus it's OK to do another malloc()/free(). */
1675 l = utf_ptr2len(p);
1676 newl = utf_char2len(lc);
1677 if (newl != l)
1678 {
1679 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
1680 if (s == NULL)
1681 {
1682 vim_free(res);
1683 return NULL;
1684 }
1685 mch_memmove(s, res, p - res);
1686 STRCPY(s + (p - res) + newl, p + l);
1687 p = s + (p - res);
1688 vim_free(res);
1689 res = s;
1690 }
1691
1692 utf_char2bytes(lc, p);
1693 p += newl;
1694 }
1695 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1696 p += l; /* skip multi-byte character */
1697 else
1698# endif
1699 {
1700 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
1701 p++;
1702 }
1703 }
1704
1705 return res;
1706}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001707#endif
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 * delete spaces at the end of a string
1711 */
1712 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001713del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 char_u *q;
1716
1717 q = ptr + STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001718 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 *q = NUL;
1720}
1721
1722/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001723 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001724 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 */
1726 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001727vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 STRNCPY(to, from, len);
1730 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731}
1732
1733/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001734 * Like strcat(), but make sure the result fits in "tosize" bytes and is
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001735 * always NUL terminated. "from" and "to" may overlap.
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001736 */
1737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001738vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001739{
1740 size_t tolen = STRLEN(to);
1741 size_t fromlen = STRLEN(from);
1742
1743 if (tolen + fromlen + 1 > tosize)
1744 {
1745 mch_memmove(to + tolen, from, tosize - tolen - 1);
1746 to[tosize - 1] = NUL;
1747 }
1748 else
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001749 mch_memmove(to + tolen, from, fromlen + 1);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001750}
1751
1752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 * Isolate one part of a string option where parts are separated with
1754 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001755 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 * "*option" is advanced to the next part.
1757 * The length is returned.
1758 */
1759 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760copy_option_part(
1761 char_u **option,
1762 char_u *buf,
1763 int maxlen,
1764 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 int len = 0;
1767 char_u *p = *option;
1768
1769 /* skip '.' at start of option part, for 'suffixes' */
1770 if (*p == '.')
1771 buf[len++] = *p++;
1772 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1773 {
1774 /*
1775 * Skip backslash before a separator character and space.
1776 */
1777 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1778 ++p;
1779 if (len < maxlen - 1)
1780 buf[len++] = *p;
1781 ++p;
1782 }
1783 buf[len] = NUL;
1784
1785 if (*p != NUL && *p != ',') /* skip non-standard separator */
1786 ++p;
1787 p = skip_to_option_part(p); /* p points to next file name */
1788
1789 *option = p;
1790 return len;
1791}
1792
1793/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001794 * Replacement for free() that ignores NULL pointers.
1795 * Also skip free() when exiting for sure, this helps when we caught a deadly
1796 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 */
1798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001801 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803#ifdef MEM_PROFILE
1804 mem_pre_free(&x);
1805#endif
1806 free(x);
1807 }
1808}
1809
1810#ifndef HAVE_MEMSET
1811 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001812vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814 char *p = ptr;
1815
1816 while (size-- > 0)
1817 *p++ = c;
1818 return ptr;
1819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1823/*
1824 * Compare two strings, ignoring case, using current locale.
1825 * Doesn't work for multi-byte characters.
1826 * return 0 for match, < 0 for smaller, > 0 for bigger
1827 */
1828 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001829vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 int i;
1832
1833 for (;;)
1834 {
1835 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1836 if (i != 0)
1837 return i; /* this character different */
1838 if (*s1 == NUL)
1839 break; /* strings match until NUL */
1840 ++s1;
1841 ++s2;
1842 }
1843 return 0; /* strings match */
1844}
1845#endif
1846
1847#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1848/*
1849 * Compare two strings, for length "len", ignoring case, using current locale.
1850 * Doesn't work for multi-byte characters.
1851 * return 0 for match, < 0 for smaller, > 0 for bigger
1852 */
1853 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001854vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 int i;
1857
1858 while (len > 0)
1859 {
1860 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1861 if (i != 0)
1862 return i; /* this character different */
1863 if (*s1 == NUL)
1864 break; /* strings match until NUL */
1865 ++s1;
1866 ++s2;
1867 --len;
1868 }
1869 return 0; /* strings match */
1870}
1871#endif
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873/*
1874 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001875 * with characters from 128 to 255 correctly. It also doesn't return a
1876 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 */
1878 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001879vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 char_u *p;
1882 int b;
1883
1884 p = string;
1885#ifdef FEAT_MBYTE
1886 if (enc_utf8 && c >= 0x80)
1887 {
1888 while (*p != NUL)
1889 {
Bram Moolenaarace95982017-03-29 17:30:27 +02001890 int l = utfc_ptr2len(p);
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001891
1892 /* Avoid matching an illegal byte here. */
1893 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001895 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 return NULL;
1898 }
1899 if (enc_dbcs != 0 && c > 255)
1900 {
1901 int n2 = c & 0xff;
1902
1903 c = ((unsigned)c >> 8) & 0xff;
1904 while ((b = *p) != NUL)
1905 {
1906 if (b == c && p[1] == n2)
1907 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001908 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 return NULL;
1911 }
1912 if (has_mbyte)
1913 {
1914 while ((b = *p) != NUL)
1915 {
1916 if (b == c)
1917 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001918 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 return NULL;
1921 }
1922#endif
1923 while ((b = *p) != NUL)
1924 {
1925 if (b == c)
1926 return p;
1927 ++p;
1928 }
1929 return NULL;
1930}
1931
1932/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001933 * Version of strchr() that only works for bytes and handles unsigned char
1934 * strings with characters above 128 correctly. It also doesn't return a
1935 * pointer to the NUL at the end of the string.
1936 */
1937 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001938vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001939{
1940 char_u *p = string;
1941
1942 while (*p != NUL)
1943 {
1944 if (*p == c)
1945 return p;
1946 ++p;
1947 }
1948 return NULL;
1949}
1950
1951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001953 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
1956 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001957vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001960 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
Bram Moolenaar05159a02005-02-26 23:04:13 +00001962 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001964 if (*p == c)
1965 retval = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001966 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 return retval;
1969}
1970
1971/*
1972 * Vim's version of strpbrk(), in case it's missing.
1973 * Don't generate a prototype for this, causes problems when it's not used.
1974 */
1975#ifndef PROTO
1976# ifndef HAVE_STRPBRK
1977# ifdef vim_strpbrk
1978# undef vim_strpbrk
1979# endif
1980 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001981vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
1983 while (*s)
1984 {
1985 if (vim_strchr(charset, *s) != NULL)
1986 return s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001987 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
1989 return NULL;
1990}
1991# endif
1992#endif
1993
1994/*
1995 * Vim has its own isspace() function, because on some machines isspace()
1996 * can't handle characters above 128.
1997 */
1998 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001999vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 return ((x >= 9 && x <= 13) || x == ' ');
2002}
2003
2004/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002005 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
2007
2008/*
2009 * Clear an allocated growing array.
2010 */
2011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 vim_free(gap->ga_data);
2015 ga_init(gap);
2016}
2017
2018/*
2019 * Clear a growing array that contains a list of strings.
2020 */
2021 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002022ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
2024 int i;
2025
2026 for (i = 0; i < gap->ga_len; ++i)
2027 vim_free(((char_u **)(gap->ga_data))[i]);
2028 ga_clear(gap);
2029}
2030
2031/*
2032 * Initialize a growing array. Don't forget to set ga_itemsize and
2033 * ga_growsize! Or use ga_init2().
2034 */
2035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002036ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
2038 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002039 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 gap->ga_len = 0;
2041}
2042
2043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 ga_init(gap);
2047 gap->ga_itemsize = itemsize;
2048 gap->ga_growsize = growsize;
2049}
2050
2051/*
2052 * Make room in growing array "gap" for at least "n" items.
2053 * Return FAIL for failure, OK otherwise.
2054 */
2055 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002056ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002058 size_t old_len;
2059 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 char_u *pp;
2061
Bram Moolenaar86b68352004-12-27 21:59:20 +00002062 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064 if (n < gap->ga_growsize)
2065 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002066 new_len = gap->ga_itemsize * (gap->ga_len + n);
2067 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002068 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if (pp == NULL)
2070 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002071 old_len = gap->ga_itemsize * gap->ga_maxlen;
2072 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002073 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 gap->ga_data = pp;
2075 }
2076 return OK;
2077}
2078
2079/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002080 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002081 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002082 * Returns NULL when out of memory.
2083 */
2084 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002085ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002086{
2087 int i;
2088 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002089 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002090 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002091 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002092
2093 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002094 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002095
2096 s = alloc(len + 1);
2097 if (s != NULL)
2098 {
2099 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002100 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002101 for (i = 0; i < gap->ga_len; ++i)
2102 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002103 if (p != s)
2104 {
2105 STRCPY(p, sep);
2106 p += sep_len;
2107 }
2108 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2109 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002110 }
2111 }
2112 return s;
2113}
2114
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01002115#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002116/*
2117 * Make a copy of string "p" and add it to "gap".
2118 * When out of memory nothing changes.
2119 */
2120 void
2121ga_add_string(garray_T *gap, char_u *p)
2122{
2123 char_u *cp = vim_strsave(p);
2124
2125 if (cp != NULL)
2126 {
2127 if (ga_grow(gap, 1) == OK)
2128 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2129 else
2130 vim_free(cp);
2131 }
2132}
2133#endif
2134
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002137 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 * Note: Does NOT copy the NUL at the end!
2139 */
2140 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002141ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
Bram Moolenaar43345542015-11-29 17:35:35 +01002143 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
Bram Moolenaar478af672017-04-10 22:22:42 +02002145 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01002146 return;
2147 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (ga_grow(gap, len) == OK)
2149 {
2150 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2151 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153}
2154
2155/*
2156 * Append one byte to a growarray which contains bytes.
2157 */
2158 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002159ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 if (ga_grow(gap, 1) == OK)
2162 {
2163 *((char *)gap->ga_data + gap->ga_len) = c;
2164 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166}
2167
Bram Moolenaar597a4222014-06-25 14:39:50 +02002168#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2169 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002170/*
2171 * Append the text in "gap" below the cursor line and clear "gap".
2172 */
2173 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002174append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002175{
2176 /* Remove trailing CR. */
2177 if (gap->ga_len > 0
2178 && !curbuf->b_p_bin
2179 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2180 --gap->ga_len;
2181 ga_append(gap, NUL);
2182 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2183 gap->ga_len = 0;
2184}
2185#endif
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187/************************************************************************
2188 * functions that use lookup tables for various things, generally to do with
2189 * special key codes.
2190 */
2191
2192/*
2193 * Some useful tables.
2194 */
2195
2196static struct modmasktable
2197{
2198 short mod_mask; /* Bit-mask for particular key modifier */
2199 short mod_flag; /* Bit(s) for particular key modifier */
2200 char_u name; /* Single letter name of modifier */
2201} mod_mask_table[] =
2202{
2203 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002204 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2206 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2207 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2208 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2209 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02002210#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2212#endif
2213 /* 'A' must be the last one */
2214 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2215 {0, 0, NUL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002216 /* NOTE: when adding an entry, update MAX_KEY_NAME_LEN! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217};
2218
2219/*
2220 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002221 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 */
2223#define MOD_KEYS_ENTRY_SIZE 5
2224
2225static char_u modifier_keys_table[] =
2226{
2227/* mod mask with modifier without modifier */
2228 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2229 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2230 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2231 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2232 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2233 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2234 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2235 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2236 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2237 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2238 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2239 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2240 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2241 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2242 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2243 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2244 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2245 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2246 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2247 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2248 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2249 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2250 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2251 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2252 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2253 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2254 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2255 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2256 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2257 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2258 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2259 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2260 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2261
2262 /* vt100 F1 */
2263 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2264 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2265 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2266 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2267
2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2269 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2270 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2273 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2278
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2289
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2300
2301 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2306 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2307 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2308
2309 /* TAB pseudo code*/
2310 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2311
2312 NUL
2313};
2314
2315static struct key_name_entry
2316{
2317 int key; /* Special key code or ascii value */
2318 char_u *name; /* Name of key */
2319} key_names_table[] =
2320{
2321 {' ', (char_u *)"Space"},
2322 {TAB, (char_u *)"Tab"},
2323 {K_TAB, (char_u *)"Tab"},
2324 {NL, (char_u *)"NL"},
2325 {NL, (char_u *)"NewLine"}, /* Alternative name */
2326 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2327 {NL, (char_u *)"LF"}, /* Alternative name */
2328 {CAR, (char_u *)"CR"},
2329 {CAR, (char_u *)"Return"}, /* Alternative name */
2330 {CAR, (char_u *)"Enter"}, /* Alternative name */
2331 {K_BS, (char_u *)"BS"},
2332 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2333 {ESC, (char_u *)"Esc"},
2334 {CSI, (char_u *)"CSI"},
2335 {K_CSI, (char_u *)"xCSI"},
2336 {'|', (char_u *)"Bar"},
2337 {'\\', (char_u *)"Bslash"},
2338 {K_DEL, (char_u *)"Del"},
2339 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2340 {K_KDEL, (char_u *)"kDel"},
2341 {K_UP, (char_u *)"Up"},
2342 {K_DOWN, (char_u *)"Down"},
2343 {K_LEFT, (char_u *)"Left"},
2344 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002345 {K_XUP, (char_u *)"xUp"},
2346 {K_XDOWN, (char_u *)"xDown"},
2347 {K_XLEFT, (char_u *)"xLeft"},
2348 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01002349 {K_PS, (char_u *)"PasteStart"},
2350 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 {K_F1, (char_u *)"F1"},
2353 {K_F2, (char_u *)"F2"},
2354 {K_F3, (char_u *)"F3"},
2355 {K_F4, (char_u *)"F4"},
2356 {K_F5, (char_u *)"F5"},
2357 {K_F6, (char_u *)"F6"},
2358 {K_F7, (char_u *)"F7"},
2359 {K_F8, (char_u *)"F8"},
2360 {K_F9, (char_u *)"F9"},
2361 {K_F10, (char_u *)"F10"},
2362
2363 {K_F11, (char_u *)"F11"},
2364 {K_F12, (char_u *)"F12"},
2365 {K_F13, (char_u *)"F13"},
2366 {K_F14, (char_u *)"F14"},
2367 {K_F15, (char_u *)"F15"},
2368 {K_F16, (char_u *)"F16"},
2369 {K_F17, (char_u *)"F17"},
2370 {K_F18, (char_u *)"F18"},
2371 {K_F19, (char_u *)"F19"},
2372 {K_F20, (char_u *)"F20"},
2373
2374 {K_F21, (char_u *)"F21"},
2375 {K_F22, (char_u *)"F22"},
2376 {K_F23, (char_u *)"F23"},
2377 {K_F24, (char_u *)"F24"},
2378 {K_F25, (char_u *)"F25"},
2379 {K_F26, (char_u *)"F26"},
2380 {K_F27, (char_u *)"F27"},
2381 {K_F28, (char_u *)"F28"},
2382 {K_F29, (char_u *)"F29"},
2383 {K_F30, (char_u *)"F30"},
2384
2385 {K_F31, (char_u *)"F31"},
2386 {K_F32, (char_u *)"F32"},
2387 {K_F33, (char_u *)"F33"},
2388 {K_F34, (char_u *)"F34"},
2389 {K_F35, (char_u *)"F35"},
2390 {K_F36, (char_u *)"F36"},
2391 {K_F37, (char_u *)"F37"},
2392
2393 {K_XF1, (char_u *)"xF1"},
2394 {K_XF2, (char_u *)"xF2"},
2395 {K_XF3, (char_u *)"xF3"},
2396 {K_XF4, (char_u *)"xF4"},
2397
2398 {K_HELP, (char_u *)"Help"},
2399 {K_UNDO, (char_u *)"Undo"},
2400 {K_INS, (char_u *)"Insert"},
2401 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2402 {K_KINS, (char_u *)"kInsert"},
2403 {K_HOME, (char_u *)"Home"},
2404 {K_KHOME, (char_u *)"kHome"},
2405 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002406 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {K_END, (char_u *)"End"},
2408 {K_KEND, (char_u *)"kEnd"},
2409 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002410 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {K_PAGEUP, (char_u *)"PageUp"},
2412 {K_PAGEDOWN, (char_u *)"PageDown"},
2413 {K_KPAGEUP, (char_u *)"kPageUp"},
2414 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2415
2416 {K_KPLUS, (char_u *)"kPlus"},
2417 {K_KMINUS, (char_u *)"kMinus"},
2418 {K_KDIVIDE, (char_u *)"kDivide"},
2419 {K_KMULTIPLY, (char_u *)"kMultiply"},
2420 {K_KENTER, (char_u *)"kEnter"},
2421 {K_KPOINT, (char_u *)"kPoint"},
2422
2423 {K_K0, (char_u *)"k0"},
2424 {K_K1, (char_u *)"k1"},
2425 {K_K2, (char_u *)"k2"},
2426 {K_K3, (char_u *)"k3"},
2427 {K_K4, (char_u *)"k4"},
2428 {K_K5, (char_u *)"k5"},
2429 {K_K6, (char_u *)"k6"},
2430 {K_K7, (char_u *)"k7"},
2431 {K_K8, (char_u *)"k8"},
2432 {K_K9, (char_u *)"k9"},
2433
2434 {'<', (char_u *)"lt"},
2435
2436 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002437#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002439#endif
2440#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002442#endif
2443#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002445#endif
2446#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002448#endif
2449#ifdef FEAT_MOUSE_URXVT
2450 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2451#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002452#ifdef FEAT_MOUSE_SGR
2453 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaara529ce02017-06-22 22:37:57 +02002454 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2457 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2458 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2459 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2460 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002461 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2463 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2464 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2465 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2466 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2467 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002468 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2469 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2470 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2471 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2472 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2473 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {K_X1MOUSE, (char_u *)"X1Mouse"},
2475 {K_X1DRAG, (char_u *)"X1Drag"},
2476 {K_X1RELEASE, (char_u *)"X1Release"},
2477 {K_X2MOUSE, (char_u *)"X2Mouse"},
2478 {K_X2DRAG, (char_u *)"X2Drag"},
2479 {K_X2RELEASE, (char_u *)"X2Release"},
2480 {K_DROP, (char_u *)"Drop"},
2481 {K_ZERO, (char_u *)"Nul"},
2482#ifdef FEAT_EVAL
2483 {K_SNR, (char_u *)"SNR"},
2484#endif
2485 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002486 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {0, NULL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002488 /* NOTE: When adding a long name update MAX_KEY_NAME_LEN. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489};
2490
2491#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2492
2493#ifdef FEAT_MOUSE
2494static struct mousetable
2495{
2496 int pseudo_code; /* Code for pseudo mouse event */
2497 int button; /* Which mouse button is it? */
2498 int is_click; /* Is it a mouse button click event? */
2499 int is_drag; /* Is it a mouse drag event? */
2500} mouse_table[] =
2501{
2502 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2503#ifdef FEAT_GUI
2504 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2505#endif
2506 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2507 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2508#ifdef FEAT_GUI
2509 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2510#endif
2511 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2512 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2513 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2514 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2515 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2516 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2517 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2518 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2519 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2520 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2521 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2522 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2523 /* DRAG without CLICK */
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002524 {(int)KE_MOUSEMOVE, MOUSE_RELEASE, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /* RELEASE without CLICK */
2526 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2527 {0, 0, 0, 0},
2528};
2529#endif /* FEAT_MOUSE */
2530
2531/*
2532 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2533 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2534 */
2535 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002536name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538 int i;
2539
2540 c = TOUPPER_ASC(c);
2541 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2542 if (c == mod_mask_table[i].name)
2543 return mod_mask_table[i].mod_flag;
2544 return 0;
2545}
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547/*
2548 * Check if if there is a special key code for "key" that includes the
2549 * modifiers specified.
2550 */
2551 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002552simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 int i;
2555 int key0;
2556 int key1;
2557
2558 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2559 {
2560 /* TAB is a special case */
2561 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2562 {
2563 *modifiers &= ~MOD_MASK_SHIFT;
2564 return K_S_TAB;
2565 }
2566 key0 = KEY2TERMCAP0(key);
2567 key1 = KEY2TERMCAP1(key);
2568 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2569 if (key0 == modifier_keys_table[i + 3]
2570 && key1 == modifier_keys_table[i + 4]
2571 && (*modifiers & modifier_keys_table[i]))
2572 {
2573 *modifiers &= ~modifier_keys_table[i];
2574 return TERMCAP2KEY(modifier_keys_table[i + 1],
2575 modifier_keys_table[i + 2]);
2576 }
2577 }
2578 return key;
2579}
2580
2581/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002582 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002583 */
2584 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002585handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002586{
2587 switch (key)
2588 {
2589 case K_XUP: return K_UP;
2590 case K_XDOWN: return K_DOWN;
2591 case K_XLEFT: return K_LEFT;
2592 case K_XRIGHT: return K_RIGHT;
2593 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002594 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002595 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002596 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002597 case K_XF1: return K_F1;
2598 case K_XF2: return K_F2;
2599 case K_XF3: return K_F3;
2600 case K_XF4: return K_F4;
2601 case K_S_XF1: return K_S_F1;
2602 case K_S_XF2: return K_S_F2;
2603 case K_S_XF3: return K_S_F3;
2604 case K_S_XF4: return K_S_F4;
2605 }
2606 return key;
2607}
2608
2609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 * Return a string which contains the name of the given key when the given
2611 * modifiers are down.
2612 */
2613 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002614get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 static char_u string[MAX_KEY_NAME_LEN + 1];
2617
2618 int i, idx;
2619 int table_idx;
2620 char_u *s;
2621
2622 string[0] = '<';
2623 idx = 1;
2624
2625 /* Key that stands for a normal character. */
2626 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2627 c = KEY2TERMCAP1(c);
2628
2629 /*
2630 * Translate shifted special keys into unshifted keys and set modifier.
2631 * Same for CTRL and ALT modifiers.
2632 */
2633 if (IS_SPECIAL(c))
2634 {
2635 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2636 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2637 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2638 {
2639 modifiers |= modifier_keys_table[i];
2640 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2641 modifier_keys_table[i + 4]);
2642 break;
2643 }
2644 }
2645
2646 /* try to find the key in the special key table */
2647 table_idx = find_special_key_in_table(c);
2648
2649 /*
2650 * When not a known special key, and not a printable character, try to
2651 * extract modifiers.
2652 */
2653 if (c > 0
2654#ifdef FEAT_MBYTE
2655 && (*mb_char2len)(c) == 1
2656#endif
2657 )
2658 {
2659 if (table_idx < 0
2660 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2661 && (c & 0x80))
2662 {
2663 c &= 0x7f;
2664 modifiers |= MOD_MASK_ALT;
2665 /* try again, to find the un-alted key in the special key table */
2666 table_idx = find_special_key_in_table(c);
2667 }
2668 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2669 {
2670#ifdef EBCDIC
2671 c = CtrlChar(c);
2672#else
2673 c += '@';
2674#endif
2675 modifiers |= MOD_MASK_CTRL;
2676 }
2677 }
2678
2679 /* translate the modifier into a string */
2680 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2681 if ((modifiers & mod_mask_table[i].mod_mask)
2682 == mod_mask_table[i].mod_flag)
2683 {
2684 string[idx++] = mod_mask_table[i].name;
2685 string[idx++] = (char_u)'-';
2686 }
2687
2688 if (table_idx < 0) /* unknown special key, may output t_xx */
2689 {
2690 if (IS_SPECIAL(c))
2691 {
2692 string[idx++] = 't';
2693 string[idx++] = '_';
2694 string[idx++] = KEY2TERMCAP0(c);
2695 string[idx++] = KEY2TERMCAP1(c);
2696 }
2697 /* Not a special key, only modifiers, output directly */
2698 else
2699 {
2700#ifdef FEAT_MBYTE
2701 if (has_mbyte && (*mb_char2len)(c) > 1)
2702 idx += (*mb_char2bytes)(c, string + idx);
2703 else
2704#endif
2705 if (vim_isprintc(c))
2706 string[idx++] = c;
2707 else
2708 {
2709 s = transchar(c);
2710 while (*s)
2711 string[idx++] = *s++;
2712 }
2713 }
2714 }
2715 else /* use name of special key */
2716 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002717 size_t len = STRLEN(key_names_table[table_idx].name);
2718
2719 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2720 {
2721 STRCPY(string + idx, key_names_table[table_idx].name);
2722 idx += (int)len;
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 string[idx++] = '>';
2726 string[idx] = NUL;
2727 return string;
2728}
2729
2730/*
2731 * Try translating a <> name at (*srcp)[] to dst[].
2732 * Return the number of characters added to dst[], zero for no match.
2733 * If there is a match, srcp is advanced to after the <> name.
2734 * dst[] must be big enough to hold the result (up to six characters)!
2735 */
2736 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002737trans_special(
2738 char_u **srcp,
2739 char_u *dst,
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002740 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2741 int in_string) /* TRUE when inside a double quoted string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 int modifiers = 0;
2744 int key;
2745 int dlen = 0;
2746
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002747 key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (key == 0)
2749 return 0;
2750
2751 /* Put the appropriate modifier in a string */
2752 if (modifiers != 0)
2753 {
2754 dst[dlen++] = K_SPECIAL;
2755 dst[dlen++] = KS_MODIFIER;
2756 dst[dlen++] = modifiers;
2757 }
2758
2759 if (IS_SPECIAL(key))
2760 {
2761 dst[dlen++] = K_SPECIAL;
2762 dst[dlen++] = KEY2TERMCAP0(key);
2763 dst[dlen++] = KEY2TERMCAP1(key);
2764 }
2765#ifdef FEAT_MBYTE
2766 else if (has_mbyte && !keycode)
2767 dlen += (*mb_char2bytes)(key, dst + dlen);
2768#endif
2769 else if (keycode)
2770 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2771 else
2772 dst[dlen++] = key;
2773
2774 return dlen;
2775}
2776
2777/*
2778 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2779 * srcp is advanced to after the <> name.
2780 * returns 0 if there is no match.
2781 */
2782 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002783find_special_key(
2784 char_u **srcp,
2785 int *modp,
2786 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002787 int keep_x_key, /* don't translate xHome to Home key */
2788 int in_string) /* TRUE in string, double quote is escaped */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 char_u *last_dash;
2791 char_u *end_of_name;
2792 char_u *src;
2793 char_u *bp;
2794 int modifiers;
2795 int bit;
2796 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002797 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002798 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 src = *srcp;
2801 if (src[0] != '<')
2802 return 0;
2803
2804 /* Find end of modifier list */
2805 last_dash = src;
2806 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2807 {
2808 if (*bp == '-')
2809 {
2810 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002811 if (bp[1] != NUL)
2812 {
2813#ifdef FEAT_MBYTE
2814 if (has_mbyte)
2815 l = mb_ptr2len(bp + 1);
2816 else
2817#endif
2818 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002819 /* Anything accepted, like <C-?>.
2820 * <C-"> or <M-"> are not special in strings as " is
2821 * the string delimiter. With a backslash it works: <M-\"> */
2822 if (!(in_string && bp[1] == '"') && bp[2] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002823 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002824 else if (in_string && bp[1] == '\\' && bp[2] == '"'
2825 && bp[3] == '>')
2826 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2830 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002831 else if (STRNICMP(bp, "char-", 5) == 0)
2832 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002833 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002834 bp += l + 5;
2835 break;
2836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838
2839 if (*bp == '>') /* found matching '>' */
2840 {
2841 end_of_name = bp + 1;
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 /* Which modifiers are given? */
2844 modifiers = 0x0;
2845 for (bp = src + 1; bp < last_dash; bp++)
2846 {
2847 if (*bp != '-')
2848 {
2849 bit = name_to_mod_mask(*bp);
2850 if (bit == 0x0)
2851 break; /* Illegal modifier name */
2852 modifiers |= bit;
2853 }
2854 }
2855
2856 /*
2857 * Legal modifier name.
2858 */
2859 if (bp >= last_dash)
2860 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002861 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2862 && VIM_ISDIGIT(last_dash[6]))
2863 {
2864 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002865 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002866 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002869 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002870 int off = 1;
2871
2872 /* Modifier with single letter, or special key name. */
2873 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2874 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002875#ifdef FEAT_MBYTE
2876 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002877 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002878 else
2879#endif
2880 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002881 if (modifiers != 0 && last_dash[l + off] == '>')
2882 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002883 else
2884 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002885 key = get_special_key_code(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002886 if (!keep_x_key)
2887 key = handle_x_keys(key);
2888 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
2891 /*
2892 * get_special_key_code() may return NUL for invalid
2893 * special key name.
2894 */
2895 if (key != NUL)
2896 {
2897 /*
2898 * Only use a modifier when there is no special key code that
2899 * includes the modifier.
2900 */
2901 key = simplify_key(key, &modifiers);
2902
2903 if (!keycode)
2904 {
2905 /* don't want keycode, use single byte code */
2906 if (key == K_BS)
2907 key = BS;
2908 else if (key == K_DEL || key == K_KDEL)
2909 key = DEL;
2910 }
2911
2912 /*
2913 * Normal Key with modifier: Try to make a single byte code.
2914 */
2915 if (!IS_SPECIAL(key))
2916 key = extract_modifiers(key, &modifiers);
2917
2918 *modp = modifiers;
2919 *srcp = end_of_name;
2920 return key;
2921 }
2922 }
2923 }
2924 return 0;
2925}
2926
2927/*
2928 * Try to include modifiers in the key.
2929 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2930 */
2931 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002932extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 int modifiers = *modp;
2935
Bram Moolenaard0573012017-10-28 21:11:06 +02002936#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002937 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (!(modifiers & MOD_MASK_CMD))
2939#endif
2940 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2941 {
2942 key = TOUPPER_ASC(key);
2943 modifiers &= ~MOD_MASK_SHIFT;
2944 }
2945 if ((modifiers & MOD_MASK_CTRL)
2946#ifdef EBCDIC
2947 /* * TODO: EBCDIC Better use:
2948 * && (Ctrl_chr(key) || key == '?')
2949 * ??? */
2950 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2951 != NULL
2952#else
2953 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2954#endif
2955 )
2956 {
2957 key = Ctrl_chr(key);
2958 modifiers &= ~MOD_MASK_CTRL;
2959 /* <C-@> is <Nul> */
2960 if (key == 0)
2961 key = K_ZERO;
2962 }
Bram Moolenaard0573012017-10-28 21:11:06 +02002963#ifdef MACOS_X
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002964 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (!(modifiers & MOD_MASK_CMD))
2966#endif
2967 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2968#ifdef FEAT_MBYTE
2969 && !enc_dbcs /* avoid creating a lead byte */
2970#endif
2971 )
2972 {
2973 key |= 0x80;
2974 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2975 }
2976
2977 *modp = modifiers;
2978 return key;
2979}
2980
2981/*
2982 * Try to find key "c" in the special key table.
2983 * Return the index when found, -1 when not found.
2984 */
2985 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002986find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 int i;
2989
2990 for (i = 0; key_names_table[i].name != NULL; i++)
2991 if (c == key_names_table[i].key)
2992 break;
2993 if (key_names_table[i].name == NULL)
2994 i = -1;
2995 return i;
2996}
2997
2998/*
2999 * Find the special key with the given name (the given string does not have to
3000 * end with NUL, the name is assumed to end before the first non-idchar).
3001 * If the name starts with "t_" the next two characters are interpreted as a
3002 * termcap name.
3003 * Return the key code, or 0 if not found.
3004 */
3005 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003006get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
3008 char_u *table_name;
3009 char_u string[3];
3010 int i, j;
3011
3012 /*
3013 * If it's <t_xx> we get the code for xx from the termcap
3014 */
3015 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3016 {
3017 string[0] = name[2];
3018 string[1] = name[3];
3019 string[2] = NUL;
3020 if (add_termcap_entry(string, FALSE) == OK)
3021 return TERMCAP2KEY(name[2], name[3]);
3022 }
3023 else
3024 for (i = 0; key_names_table[i].name != NULL; i++)
3025 {
3026 table_name = key_names_table[i].name;
3027 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3028 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3029 break;
3030 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3031 return key_names_table[i].key;
3032 }
3033 return 0;
3034}
3035
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003036#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003038get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003040 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 return NULL;
3042 return key_names_table[i].name;
3043}
3044#endif
3045
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003046#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047/*
3048 * Look up the given mouse code to return the relevant information in the other
3049 * arguments. Return which button is down or was released.
3050 */
3051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 int i;
3055
3056 for (i = 0; mouse_table[i].pseudo_code; i++)
3057 if (code == mouse_table[i].pseudo_code)
3058 {
3059 *is_click = mouse_table[i].is_click;
3060 *is_drag = mouse_table[i].is_drag;
3061 return mouse_table[i].button;
3062 }
3063 return 0; /* Shouldn't get here */
3064}
3065
3066/*
3067 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3068 * the given information about which mouse button is down, and whether the
3069 * mouse was clicked, dragged or released.
3070 */
3071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003072get_pseudo_mouse_code(
3073 int button, /* eg MOUSE_LEFT */
3074 int is_click,
3075 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 int i;
3078
3079 for (i = 0; mouse_table[i].pseudo_code; i++)
3080 if (button == mouse_table[i].button
3081 && is_click == mouse_table[i].is_click
3082 && is_drag == mouse_table[i].is_drag)
3083 {
3084#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003085 /* Trick: a non mappable left click and release has mouse_col -1
3086 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3087 * gui_mouse_moved() */
3088 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003090 if (mouse_col < 0)
3091 mouse_col = 0;
3092 else
3093 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3095 return (int)KE_LEFTMOUSE_NM;
3096 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3097 return (int)KE_LEFTRELEASE_NM;
3098 }
3099#endif
3100 return mouse_table[i].pseudo_code;
3101 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003102 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104#endif /* FEAT_MOUSE */
3105
3106/*
3107 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3108 */
3109 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003110get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int c = *buf->b_p_ff;
3113
3114 if (buf->b_p_bin || c == 'u')
3115 return EOL_UNIX;
3116 if (c == 'm')
3117 return EOL_MAC;
3118 return EOL_DOS;
3119}
3120
3121/*
3122 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3123 * argument.
3124 */
3125 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003126get_fileformat_force(
3127 buf_T *buf,
3128 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 int c;
3131
3132 if (eap != NULL && eap->force_ff != 0)
3133 c = eap->cmd[eap->force_ff];
3134 else
3135 {
3136 if ((eap != NULL && eap->force_bin != 0)
3137 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3138 return EOL_UNIX;
3139 c = *buf->b_p_ff;
3140 }
3141 if (c == 'u')
3142 return EOL_UNIX;
3143 if (c == 'm')
3144 return EOL_MAC;
3145 return EOL_DOS;
3146}
3147
3148/*
3149 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3150 * Sets both 'textmode' and 'fileformat'.
3151 * Note: Does _not_ set global value of 'textmode'!
3152 */
3153 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003154set_fileformat(
3155 int t,
3156 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 char *p = NULL;
3159
3160 switch (t)
3161 {
3162 case EOL_DOS:
3163 p = FF_DOS;
3164 curbuf->b_p_tx = TRUE;
3165 break;
3166 case EOL_UNIX:
3167 p = FF_UNIX;
3168 curbuf->b_p_tx = FALSE;
3169 break;
3170 case EOL_MAC:
3171 p = FF_MAC;
3172 curbuf->b_p_tx = FALSE;
3173 break;
3174 }
3175 if (p != NULL)
3176 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003177 OPT_FREE | opt_flags, 0);
3178
Bram Moolenaarf740b292006-02-16 22:11:02 +00003179 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003181 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#ifdef FEAT_TITLE
3183 need_maketitle = TRUE; /* set window title later */
3184#endif
3185}
3186
3187/*
3188 * Return the default fileformat from 'fileformats'.
3189 */
3190 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003191default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
3193 switch (*p_ffs)
3194 {
3195 case 'm': return EOL_MAC;
3196 case 'd': return EOL_DOS;
3197 }
3198 return EOL_UNIX;
3199}
3200
3201/*
3202 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3203 */
3204 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003205call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 char_u *ncmd;
3208 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003209#ifdef FEAT_PROFILE
3210 proftime_T wait_time;
3211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213 if (p_verbose > 3)
3214 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003215 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003216 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 cmd == NULL ? p_sh : cmd);
3218 out_char('\n');
3219 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003220 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
3222
Bram Moolenaar05159a02005-02-26 23:04:13 +00003223#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003224 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003225 prof_child_enter(&wait_time);
3226#endif
3227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (*p_sh == NUL)
3229 {
3230 EMSG(_(e_shellempty));
3231 retval = -1;
3232 }
3233 else
3234 {
3235#ifdef FEAT_GUI_MSWIN
3236 /* Don't hide the pointer while executing a shell command. */
3237 gui_mch_mousehide(FALSE);
3238#endif
3239#ifdef FEAT_GUI
3240 ++hold_gui_events;
3241#endif
3242 /* The external command may update a tags file, clear cached tags. */
3243 tag_freematch();
3244
3245 if (cmd == NULL || *p_sxq == NUL)
3246 retval = mch_call_shell(cmd, opt);
3247 else
3248 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003249 char_u *ecmd = cmd;
3250
3251 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3252 {
3253 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3254 if (ecmd == NULL)
3255 ecmd = cmd;
3256 }
3257 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (ncmd != NULL)
3259 {
3260 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003261 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003262 /* When 'shellxquote' is ( append ).
3263 * When 'shellxquote' is "( append )". */
3264 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3265 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3266 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 retval = mch_call_shell(ncmd, opt);
3268 vim_free(ncmd);
3269 }
3270 else
3271 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003272 if (ecmd != cmd)
3273 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275#ifdef FEAT_GUI
3276 --hold_gui_events;
3277#endif
3278 /*
3279 * Check the window size, in case it changed while executing the
3280 * external command.
3281 */
3282 shell_resized_check();
3283 }
3284
3285#ifdef FEAT_EVAL
3286 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003287# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003288 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003289 prof_child_exit(&wait_time);
3290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291#endif
3292
3293 return retval;
3294}
3295
3296/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003297 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3298 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 */
3300 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003301get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 if (State & NORMAL)
3304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003306 {
3307 if (VIsual_select)
3308 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003310 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003311 else if (finish_op)
3312 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 return State;
3315}
3316
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003317#if defined(FEAT_MBYTE) || defined(PROTO)
3318/*
3319 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003320 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003321 * "b" must point to the start of the file name
3322 */
3323 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003324after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003325{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003326 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003327 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3328}
3329#endif
3330
3331/*
3332 * Return TRUE if file names "f1" and "f2" are in the same directory.
3333 * "f1" may be a short name, "f2" must be a full path.
3334 */
3335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003336same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003337{
3338 char_u ffname[MAXPATHL];
3339 char_u *t1;
3340 char_u *t2;
3341
3342 /* safety check */
3343 if (f1 == NULL || f2 == NULL)
3344 return FALSE;
3345
3346 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3347 t1 = gettail_sep(ffname);
3348 t2 = gettail_sep(f2);
3349 return (t1 - ffname == t2 - f2
3350 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3351}
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar4033c552017-09-16 20:54:51 +02003354 || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3356 || defined(PROTO)
3357/*
3358 * Change to a file's directory.
3359 * Caller must call shorten_fnames()!
3360 * Return OK or FAIL.
3361 */
3362 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003363vim_chdirfile(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003365 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003367 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003368 *gettail_sep(dir) = NUL;
3369 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370}
3371#endif
3372
3373#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3374/*
3375 * Check if "name" ends in a slash and is not a directory.
3376 * Used for systems where stat() ignores a trailing slash on a file name.
3377 * The Vim code assumes a trailing slash is only ignored for a directory.
3378 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003379 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01003380illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 if (name[0] == NUL)
3383 return FALSE; /* no file name is not illegal */
3384 if (name[strlen(name) - 1] != '/')
3385 return FALSE; /* no trailing slash */
3386 if (mch_isdir((char_u *)name))
3387 return FALSE; /* trailing slash for a directory */
3388 return TRUE;
3389}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003390
3391/*
3392 * Special implementation of mch_stat() for Solaris.
3393 */
3394 int
3395vim_stat(const char *name, stat_T *stp)
3396{
3397 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
3398 * the name ends in "/" and it's not a directory. */
Bram Moolenaard8492792017-03-16 12:22:38 +01003399 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003400}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#endif
3402
3403#if defined(CURSOR_SHAPE) || defined(PROTO)
3404
3405/*
3406 * Handling of cursor and mouse pointer shapes in various modes.
3407 */
3408
3409cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3410{
3411 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3412 * defaults when Vim starts.
3413 * Adjust the SHAPE_IDX_ defines when making changes! */
3414 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3415 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3416 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3417 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3418 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3419 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3420 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3421 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3422 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3423 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3424 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3425 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3426 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3427 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3428 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3429 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3430 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3431};
3432
3433#ifdef FEAT_MOUSESHAPE
3434/*
3435 * Table with names for mouse shapes. Keep in sync with all the tables for
3436 * mch_set_mouse_shape()!.
3437 */
3438static char * mshape_names[] =
3439{
3440 "arrow", /* default, must be the first one */
3441 "blank", /* hidden */
3442 "beam",
3443 "updown",
3444 "udsizing",
3445 "leftright",
3446 "lrsizing",
3447 "busy",
3448 "no",
3449 "crosshair",
3450 "hand1",
3451 "hand2",
3452 "pencil",
3453 "question",
3454 "rightup-arrow",
3455 "up-arrow",
3456 NULL
3457};
3458#endif
3459
3460/*
3461 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3462 * ("what" is SHAPE_MOUSE).
3463 * Returns error message for an illegal option, NULL otherwise.
3464 */
3465 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003466parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 char_u *modep;
3469 char_u *colonp;
3470 char_u *commap;
3471 char_u *slashp;
3472 char_u *p, *endp;
3473 int idx = 0; /* init for GCC */
3474 int all_idx;
3475 int len;
3476 int i;
3477 long n;
3478 int found_ve = FALSE; /* found "ve" flag */
3479 int round;
3480
3481 /*
3482 * First round: check for errors; second round: do it for real.
3483 */
3484 for (round = 1; round <= 2; ++round)
3485 {
3486 /*
3487 * Repeat for all comma separated parts.
3488 */
3489#ifdef FEAT_MOUSESHAPE
3490 if (what == SHAPE_MOUSE)
3491 modep = p_mouseshape;
3492 else
3493#endif
3494 modep = p_guicursor;
3495 while (*modep != NUL)
3496 {
3497 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01003498 commap = vim_strchr(modep, ',');
3499
3500 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 return (char_u *)N_("E545: Missing colon");
3502 if (colonp == modep)
3503 return (char_u *)N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 /*
3506 * Repeat for all mode's before the colon.
3507 * For the 'a' mode, we loop to handle all the modes.
3508 */
3509 all_idx = -1;
3510 while (modep < colonp || all_idx >= 0)
3511 {
3512 if (all_idx < 0)
3513 {
3514 /* Find the mode. */
3515 if (modep[1] == '-' || modep[1] == ':')
3516 len = 1;
3517 else
3518 len = 2;
3519 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3520 all_idx = SHAPE_IDX_COUNT - 1;
3521 else
3522 {
3523 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3524 if (STRNICMP(modep, shape_table[idx].name, len)
3525 == 0)
3526 break;
3527 if (idx == SHAPE_IDX_COUNT
3528 || (shape_table[idx].used_for & what) == 0)
3529 return (char_u *)N_("E546: Illegal mode");
3530 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3531 found_ve = TRUE;
3532 }
3533 modep += len + 1;
3534 }
3535
3536 if (all_idx >= 0)
3537 idx = all_idx--;
3538 else if (round == 2)
3539 {
3540#ifdef FEAT_MOUSESHAPE
3541 if (what == SHAPE_MOUSE)
3542 {
3543 /* Set the default, for the missing parts */
3544 shape_table[idx].mshape = 0;
3545 }
3546 else
3547#endif
3548 {
3549 /* Set the defaults, for the missing parts */
3550 shape_table[idx].shape = SHAPE_BLOCK;
3551 shape_table[idx].blinkwait = 700L;
3552 shape_table[idx].blinkon = 400L;
3553 shape_table[idx].blinkoff = 250L;
3554 }
3555 }
3556
3557 /* Parse the part after the colon */
3558 for (p = colonp + 1; *p && *p != ','; )
3559 {
3560#ifdef FEAT_MOUSESHAPE
3561 if (what == SHAPE_MOUSE)
3562 {
3563 for (i = 0; ; ++i)
3564 {
3565 if (mshape_names[i] == NULL)
3566 {
3567 if (!VIM_ISDIGIT(*p))
3568 return (char_u *)N_("E547: Illegal mouseshape");
3569 if (round == 2)
3570 shape_table[idx].mshape =
3571 getdigits(&p) + MSHAPE_NUMBERED;
3572 else
3573 (void)getdigits(&p);
3574 break;
3575 }
3576 len = (int)STRLEN(mshape_names[i]);
3577 if (STRNICMP(p, mshape_names[i], len) == 0)
3578 {
3579 if (round == 2)
3580 shape_table[idx].mshape = i;
3581 p += len;
3582 break;
3583 }
3584 }
3585 }
3586 else /* if (what == SHAPE_MOUSE) */
3587#endif
3588 {
3589 /*
3590 * First handle the ones with a number argument.
3591 */
3592 i = *p;
3593 len = 0;
3594 if (STRNICMP(p, "ver", 3) == 0)
3595 len = 3;
3596 else if (STRNICMP(p, "hor", 3) == 0)
3597 len = 3;
3598 else if (STRNICMP(p, "blinkwait", 9) == 0)
3599 len = 9;
3600 else if (STRNICMP(p, "blinkon", 7) == 0)
3601 len = 7;
3602 else if (STRNICMP(p, "blinkoff", 8) == 0)
3603 len = 8;
3604 if (len != 0)
3605 {
3606 p += len;
3607 if (!VIM_ISDIGIT(*p))
3608 return (char_u *)N_("E548: digit expected");
3609 n = getdigits(&p);
3610 if (len == 3) /* "ver" or "hor" */
3611 {
3612 if (n == 0)
3613 return (char_u *)N_("E549: Illegal percentage");
3614 if (round == 2)
3615 {
3616 if (TOLOWER_ASC(i) == 'v')
3617 shape_table[idx].shape = SHAPE_VER;
3618 else
3619 shape_table[idx].shape = SHAPE_HOR;
3620 shape_table[idx].percentage = n;
3621 }
3622 }
3623 else if (round == 2)
3624 {
3625 if (len == 9)
3626 shape_table[idx].blinkwait = n;
3627 else if (len == 7)
3628 shape_table[idx].blinkon = n;
3629 else
3630 shape_table[idx].blinkoff = n;
3631 }
3632 }
3633 else if (STRNICMP(p, "block", 5) == 0)
3634 {
3635 if (round == 2)
3636 shape_table[idx].shape = SHAPE_BLOCK;
3637 p += 5;
3638 }
3639 else /* must be a highlight group name then */
3640 {
3641 endp = vim_strchr(p, '-');
3642 if (commap == NULL) /* last part */
3643 {
3644 if (endp == NULL)
3645 endp = p + STRLEN(p); /* find end of part */
3646 }
3647 else if (endp > commap || endp == NULL)
3648 endp = commap;
3649 slashp = vim_strchr(p, '/');
3650 if (slashp != NULL && slashp < endp)
3651 {
3652 /* "group/langmap_group" */
3653 i = syn_check_group(p, (int)(slashp - p));
3654 p = slashp + 1;
3655 }
3656 if (round == 2)
3657 {
3658 shape_table[idx].id = syn_check_group(p,
3659 (int)(endp - p));
3660 shape_table[idx].id_lm = shape_table[idx].id;
3661 if (slashp != NULL && slashp < endp)
3662 shape_table[idx].id = i;
3663 }
3664 p = endp;
3665 }
3666 } /* if (what != SHAPE_MOUSE) */
3667
3668 if (*p == '-')
3669 ++p;
3670 }
3671 }
3672 modep = p;
3673 if (*modep == ',')
3674 ++modep;
3675 }
3676 }
3677
3678 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3679 if (!found_ve)
3680 {
3681#ifdef FEAT_MOUSESHAPE
3682 if (what == SHAPE_MOUSE)
3683 {
3684 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3685 }
3686 else
3687#endif
3688 {
3689 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3690 shape_table[SHAPE_IDX_VE].percentage =
3691 shape_table[SHAPE_IDX_V].percentage;
3692 shape_table[SHAPE_IDX_VE].blinkwait =
3693 shape_table[SHAPE_IDX_V].blinkwait;
3694 shape_table[SHAPE_IDX_VE].blinkon =
3695 shape_table[SHAPE_IDX_V].blinkon;
3696 shape_table[SHAPE_IDX_VE].blinkoff =
3697 shape_table[SHAPE_IDX_V].blinkoff;
3698 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3699 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3700 }
3701 }
3702
3703 return NULL;
3704}
3705
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003706# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3707 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708/*
3709 * Return the index into shape_table[] for the current mode.
3710 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3711 */
3712 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003713get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
3715#ifdef FEAT_MOUSESHAPE
3716 if (mouse && (State == HITRETURN || State == ASKMORE))
3717 {
3718# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003719 int x, y;
3720 gui_mch_getmouse(&x, &y);
3721 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 return SHAPE_IDX_MOREL;
3723# endif
3724 return SHAPE_IDX_MORE;
3725 }
3726 if (mouse && drag_status_line)
3727 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (mouse && drag_sep_line)
3729 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730#endif
3731 if (!mouse && State == SHOWMATCH)
3732 return SHAPE_IDX_SM;
3733#ifdef FEAT_VREPLACE
3734 if (State & VREPLACE_FLAG)
3735 return SHAPE_IDX_R;
3736#endif
3737 if (State & REPLACE_FLAG)
3738 return SHAPE_IDX_R;
3739 if (State & INSERT)
3740 return SHAPE_IDX_I;
3741 if (State & CMDLINE)
3742 {
3743 if (cmdline_at_end())
3744 return SHAPE_IDX_C;
3745 if (cmdline_overstrike())
3746 return SHAPE_IDX_CR;
3747 return SHAPE_IDX_CI;
3748 }
3749 if (finish_op)
3750 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (VIsual_active)
3752 {
3753 if (*p_sel == 'e')
3754 return SHAPE_IDX_VE;
3755 else
3756 return SHAPE_IDX_V;
3757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 return SHAPE_IDX_N;
3759}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3763static int old_mouse_shape = 0;
3764
3765/*
3766 * Set the mouse shape:
3767 * If "shape" is -1, use shape depending on the current mode,
3768 * depending on the current state.
3769 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3770 * when the mouse moves off the status or command line).
3771 */
3772 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003773update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 int new_mouse_shape;
3776
3777 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003778 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 return;
3780
3781 /* Postpone the updating when more is to come. Speeds up executing of
3782 * mappings. */
3783 if (shape_idx == -1 && char_avail())
3784 {
3785 postponed_mouseshape = TRUE;
3786 return;
3787 }
3788
Bram Moolenaar14716812006-05-04 21:54:08 +00003789 /* When ignoring the mouse don't change shape on the statusline. */
3790 if (*p_mouse == NUL
3791 && (shape_idx == SHAPE_IDX_CLINE
3792 || shape_idx == SHAPE_IDX_STATUS
3793 || shape_idx == SHAPE_IDX_VSEP))
3794 shape_idx = -2;
3795
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (shape_idx == -2
3797 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3798 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3799 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3800 return;
3801 if (shape_idx < 0)
3802 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3803 else
3804 new_mouse_shape = shape_table[shape_idx].mshape;
3805 if (new_mouse_shape != old_mouse_shape)
3806 {
3807 mch_set_mouse_shape(new_mouse_shape);
3808 old_mouse_shape = new_mouse_shape;
3809 }
3810 postponed_mouseshape = FALSE;
3811}
3812# endif
3813
3814#endif /* CURSOR_SHAPE */
3815
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817/* TODO: make some #ifdef for this */
3818/*--------[ file searching ]-------------------------------------------------*/
3819/*
3820 * File searching functions for 'path', 'tags' and 'cdpath' options.
3821 * External visible functions:
3822 * vim_findfile_init() creates/initialises the search context
3823 * vim_findfile_free_visited() free list of visited files/dirs of search
3824 * context
3825 * vim_findfile() find a file in the search context
3826 * vim_findfile_cleanup() cleanup/free search context created by
3827 * vim_findfile_init()
3828 *
3829 * All static functions and variables start with 'ff_'
3830 *
3831 * In general it works like this:
3832 * First you create yourself a search context by calling vim_findfile_init().
3833 * It is possible to give a search context from a previous call to
3834 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3835 * until you are satisfied with the result or it returns NULL. On every call it
3836 * returns the next file which matches the conditions given to
3837 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3838 *
3839 * It is possible to call vim_findfile_init() again to reinitialise your search
3840 * with some new parameters. Don't forget to pass your old search context to
3841 * it, so it can reuse it and especially reuse the list of already visited
3842 * directories. If you want to delete the list of already visited directories
3843 * simply call vim_findfile_free_visited().
3844 *
3845 * When you are done call vim_findfile_cleanup() to free the search context.
3846 *
3847 * The function vim_findfile_init() has a long comment, which describes the
3848 * needed parameters.
3849 *
3850 *
3851 *
3852 * ATTENTION:
3853 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003854 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 * thread-safe!!!!!
3856 *
3857 * To minimize parameter passing (or because I'm to lazy), only the
3858 * external visible functions get a search context as a parameter. This is
3859 * then assigned to a static global, which is used throughout the local
3860 * functions.
3861 */
3862
3863/*
3864 * type for the directory search stack
3865 */
3866typedef struct ff_stack
3867{
3868 struct ff_stack *ffs_prev;
3869
3870 /* the fix part (no wildcards) and the part containing the wildcards
3871 * of the search path
3872 */
3873 char_u *ffs_fix_path;
3874#ifdef FEAT_PATH_EXTRA
3875 char_u *ffs_wc_path;
3876#endif
3877
3878 /* files/dirs found in the above directory, matched by the first wildcard
3879 * of wc_part
3880 */
3881 char_u **ffs_filearray;
3882 int ffs_filearray_size;
3883 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3884
3885 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003886 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 int ffs_stage;
3890
3891 /* How deep are we in the directory tree?
3892 * Counts backward from value of level parameter to vim_findfile_init
3893 */
3894 int ffs_level;
3895
3896 /* Did we already expand '**' to an empty string? */
3897 int ffs_star_star_empty;
3898} ff_stack_T;
3899
3900/*
3901 * type for already visited directories or files.
3902 */
3903typedef struct ff_visited
3904{
3905 struct ff_visited *ffv_next;
3906
3907#ifdef FEAT_PATH_EXTRA
3908 /* Visited directories are different if the wildcard string are
3909 * different. So we have to save it.
3910 */
3911 char_u *ffv_wc_path;
3912#endif
3913 /* for unix use inode etc for comparison (needed because of links), else
3914 * use filename.
3915 */
3916#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003917 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3918 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 ino_t ffv_ino; /* inode number */
3920#endif
3921 /* The memory for this struct is allocated according to the length of
3922 * ffv_fname.
3923 */
3924 char_u ffv_fname[1]; /* actually longer */
3925} ff_visited_T;
3926
3927/*
3928 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003929 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3931 * So we have to do 3 searches:
3932 * 1) search from the current files directory downward for the file "tags"
3933 * 2) search from the current files directory downward for the file "TAGS"
3934 * 3) search from Vims current directory downwards for the file "tags"
3935 * As you can see, the first and the third search are for the same file, so for
3936 * the third search we can use the visited list of the first search. For the
3937 * second search we must start from a empty visited list.
3938 * The struct ff_visited_list_hdr is used to manage a linked list of already
3939 * visited lists.
3940 */
3941typedef struct ff_visited_list_hdr
3942{
3943 struct ff_visited_list_hdr *ffvl_next;
3944
3945 /* the filename the attached visited list is for */
3946 char_u *ffvl_filename;
3947
3948 ff_visited_T *ffvl_visited_list;
3949
3950} ff_visited_list_hdr_T;
3951
3952
3953/*
3954 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003955 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
3957#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959/*
3960 * The search context:
3961 * ffsc_stack_ptr: the stack for the dirs to search
3962 * ffsc_visited_list: the currently active visited list
3963 * ffsc_dir_visited_list: the currently active visited list for search dirs
3964 * ffsc_visited_lists_list: the list of all visited lists
3965 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3966 * ffsc_file_to_search: the file to search for
3967 * ffsc_start_dir: the starting directory, if search path was relative
3968 * ffsc_fix_path: the fix part of the given path (without wildcards)
3969 * Needed for upward search.
3970 * ffsc_wc_path: the part of the given path containing wildcards
3971 * ffsc_level: how many levels of dirs to search downwards
3972 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003973 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003974 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
3976typedef struct ff_search_ctx_T
3977{
3978 ff_stack_T *ffsc_stack_ptr;
3979 ff_visited_list_hdr_T *ffsc_visited_list;
3980 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3981 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3982 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3983 char_u *ffsc_file_to_search;
3984 char_u *ffsc_start_dir;
3985 char_u *ffsc_fix_path;
3986#ifdef FEAT_PATH_EXTRA
3987 char_u *ffsc_wc_path;
3988 int ffsc_level;
3989 char_u **ffsc_stopdirs_v;
3990#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003991 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003992 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003993} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995/* locally needed functions */
3996#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003997static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003999static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004001static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
4002static void ff_free_visited_list(ff_visited_T *vl);
4003static 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 +00004004#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004005static int ff_wc_equal(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#endif
4007
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004008static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
4009static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
4010static void ff_clear(ff_search_ctx_T *search_ctx);
4011static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004013static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004015static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#endif
4017#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004018static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#endif
4020
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004021static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#if 0
4024/*
4025 * if someone likes findfirst/findnext, here are the functions
4026 * NOT TESTED!!
4027 */
4028
4029static void *ff_fn_search_context = NULL;
4030
4031 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004032vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033{
4034 ff_fn_search_context =
4035 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4036 ff_fn_search_context, rel_fname);
4037 if (NULL == ff_fn_search_context)
4038 return NULL;
4039 else
4040 return vim_findnext()
4041}
4042
4043 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004044vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 char_u *ret = vim_findfile(ff_fn_search_context);
4047
4048 if (NULL == ret)
4049 {
4050 vim_findfile_cleanup(ff_fn_search_context);
4051 ff_fn_search_context = NULL;
4052 }
4053 return ret;
4054}
4055#endif
4056
4057/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004058 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004060 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 *
4062 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4063 * with the search context.
4064 *
4065 * Find the file 'filename' in the directory 'path'.
4066 * The parameter 'path' may contain wildcards. If so only search 'level'
4067 * directories deep. The parameter 'level' is the absolute maximum and is
4068 * not related to restricts given to the '**' wildcard. If 'level' is 100
4069 * and you use '**200' vim_findfile() will stop after 100 levels.
4070 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004071 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4072 * escape special characters.
4073 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4075 * restarted on the next higher directory level. This is repeated until the
4076 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4077 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4078 *
4079 * If the 'path' is relative, the starting dir for the search is either VIM's
4080 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004081 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 * the first wildcard.
4083 *
4084 * Upward search is only done on the starting dir.
4085 *
4086 * If 'free_visited' is TRUE the list of already visited files/directories is
4087 * cleared. Set this to FALSE if you just want to search from another
4088 * directory, but want to be sure that no directory from a previous search is
4089 * searched again. This is useful if you search for a file at different places.
4090 * The list of visited files/dirs can also be cleared with the function
4091 * vim_findfile_free_visited().
4092 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004093 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4094 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 *
4096 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004097 * passed in the parameter "search_ctx_arg". This context is reused and
4098 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004100 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4101 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004103 * If you don't have a search context from a previous call "search_ctx_arg"
4104 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 *
4106 * This function silently ignores a few errors, vim_findfile() will have
4107 * limited functionality then.
4108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004110vim_findfile_init(
4111 char_u *path,
4112 char_u *filename,
4113 char_u *stopdirs UNUSED,
4114 int level,
4115 int free_visited,
4116 int find_what,
4117 void *search_ctx_arg,
4118 int tagfile, /* expanding names of tags files */
4119 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004122 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004124 ff_stack_T *sptr;
4125 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 /* If a search context is given by the caller, reuse it, else allocate a
4128 * new one.
4129 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004130 if (search_ctx_arg != NULL)
4131 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 else
4133 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004134 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4135 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004137 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004139 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004140 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004143 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /* clear visited list if wanted */
4146 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004147 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 else
4149 {
4150 /* Reuse old visited lists. Get the visited list for the given
4151 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004152 * one. */
4153 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4154 &search_ctx->ffsc_visited_lists_list);
4155 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004157 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4158 &search_ctx->ffsc_dir_visited_lists_list);
4159 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 goto error_return;
4161 }
4162
4163 if (ff_expand_buffer == NULL)
4164 {
4165 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4166 if (ff_expand_buffer == NULL)
4167 goto error_return;
4168 }
4169
4170 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004171 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (path[0] == '.'
4173 && (vim_ispathsep(path[1]) || path[1] == NUL)
4174 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4175 && rel_fname != NULL)
4176 {
4177 int len = (int)(gettail(rel_fname) - rel_fname);
4178
4179 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4180 {
4181 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004182 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004183 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004186 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4187 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 goto error_return;
4189 if (*++path != NUL)
4190 ++path;
4191 }
4192 else if (*path == NUL || !vim_isAbsName(path))
4193 {
4194#ifdef BACKSLASH_IN_FILENAME
4195 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4196 if (*path != NUL && path[1] == ':')
4197 {
4198 char_u drive[3];
4199
4200 drive[0] = path[0];
4201 drive[1] = ':';
4202 drive[2] = NUL;
4203 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4204 goto error_return;
4205 path += 2;
4206 }
4207 else
4208#endif
4209 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4210 goto error_return;
4211
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004212 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4213 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 goto error_return;
4215
4216#ifdef BACKSLASH_IN_FILENAME
4217 /* A path that starts with "/dir" is relative to the drive, not to the
4218 * directory (but not for "//machine/dir"). Only use the drive name. */
4219 if ((*path == '/' || *path == '\\')
4220 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004221 && search_ctx->ffsc_start_dir[1] == ':')
4222 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#endif
4224 }
4225
4226#ifdef FEAT_PATH_EXTRA
4227 /*
4228 * If stopdirs are given, split them into an array of pointers.
4229 * If this fails (mem allocation), there is no upward search at all or a
4230 * stop directory is not recognized -> continue silently.
4231 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004232 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 * is handled as unlimited upward search. See function
4234 * ff_path_in_stoplist() for details.
4235 */
4236 if (stopdirs != NULL)
4237 {
4238 char_u *walker = stopdirs;
4239 int dircount;
4240
4241 while (*walker == ';')
4242 walker++;
4243
4244 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004245 search_ctx->ffsc_stopdirs_v =
4246 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004248 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 do
4251 {
4252 char_u *helper;
4253 void *ptr;
4254
4255 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004256 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 (dircount + 1) * sizeof(char_u *));
4258 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004259 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 else
4261 /* ignore, keep what we have and continue */
4262 break;
4263 walker = vim_strchr(walker, ';');
4264 if (walker)
4265 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004266 search_ctx->ffsc_stopdirs_v[dircount-1] =
4267 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 walker++;
4269 }
4270 else
4271 /* this might be "", which means ascent till top
4272 * of directory tree.
4273 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004274 search_ctx->ffsc_stopdirs_v[dircount-1] =
4275 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 dircount++;
4278
4279 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004280 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283#endif
4284
4285#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004286 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 /* split into:
4289 * -fix path
4290 * -wildcard_stuff (might be NULL)
4291 */
4292 wc_part = vim_strchr(path, '*');
4293 if (wc_part != NULL)
4294 {
4295 int llevel;
4296 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004297 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004300 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
4302 /*
4303 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004304 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4306 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4307 * For EBCDIC you get different character values.
4308 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004309 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * string.
4311 */
4312 len = 0;
4313 while (*wc_part != NUL)
4314 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004315 if (len + 5 >= MAXPATHL)
4316 {
4317 EMSG(_(e_pathtoolong));
4318 break;
4319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if (STRNCMP(wc_part, "**", 2) == 0)
4321 {
4322 ff_expand_buffer[len++] = *wc_part++;
4323 ff_expand_buffer[len++] = *wc_part++;
4324
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004325 llevel = strtol((char *)wc_part, &errpt, 10);
4326 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004328 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 /* restrict is 0 -> remove already added '**' */
4330 len -= 2;
4331 else
4332 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004333 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004334 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
4336 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4337 goto error_return;
4338 }
4339 }
4340 else
4341 ff_expand_buffer[len++] = *wc_part++;
4342 }
4343 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004344 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004346 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 goto error_return;
4348 }
4349 else
4350#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004351 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004353 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 /* store the fix part as startdir.
4356 * This is needed if the parameter path is fully qualified.
4357 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004358 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004359 if (search_ctx->ffsc_start_dir == NULL)
4360 goto error_return;
4361 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363
4364 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004365 if (STRLEN(search_ctx->ffsc_start_dir)
4366 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4367 {
4368 EMSG(_(e_pathtoolong));
4369 goto error_return;
4370 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004371 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004373 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004374 int eb_len = (int)STRLEN(ff_expand_buffer);
4375 char_u *buf = alloc(eb_len
4376 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004377
4378 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004379 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004380 if (mch_isdir(buf))
4381 {
4382 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4383 add_pathsep(ff_expand_buffer);
4384 }
4385#ifdef FEAT_PATH_EXTRA
4386 else
4387 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004388 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004389 char_u *wc_path = NULL;
4390 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004391 int len = 0;
4392
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004393 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004394 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004395 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004396 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4397 add_pathsep(ff_expand_buffer);
4398 }
4399 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004400 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004401
4402 if (search_ctx->ffsc_wc_path != NULL)
4403 {
4404 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004405 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004406 + STRLEN(search_ctx->ffsc_fix_path + len)
4407 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004408 if (temp == NULL || wc_path == NULL)
4409 {
4410 vim_free(buf);
4411 vim_free(temp);
4412 vim_free(wc_path);
4413 goto error_return;
4414 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004415
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004416 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4417 STRCAT(temp, search_ctx->ffsc_wc_path);
4418 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004419 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004420 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004421 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004422 }
4423#endif
4424 vim_free(buf);
4425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 sptr = ff_create_stack_element(ff_expand_buffer,
4428#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004429 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#endif
4431 level, 0);
4432
4433 if (sptr == NULL)
4434 goto error_return;
4435
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004436 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004438 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4439 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 goto error_return;
4441
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004442 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444error_return:
4445 /*
4446 * We clear the search context now!
4447 * Even when the caller gave us a (perhaps valid) context we free it here,
4448 * as we might have already destroyed it.
4449 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004450 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 return NULL;
4452}
4453
4454#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4455/*
4456 * Get the stopdir string. Check that ';' is not escaped.
4457 */
4458 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004459vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460{
4461 char_u *r_ptr = buf;
4462
4463 while (*r_ptr != NUL && *r_ptr != ';')
4464 {
4465 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4466 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004467 /* Overwrite the escape char,
4468 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004469 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 r_ptr++;
4471 }
4472 r_ptr++;
4473 }
4474 if (*r_ptr == ';')
4475 {
4476 *r_ptr = 0;
4477 r_ptr++;
4478 }
4479 else if (*r_ptr == NUL)
4480 r_ptr = NULL;
4481 return r_ptr;
4482}
4483#endif
4484
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004485/*
4486 * Clean up the given search context. Can handle a NULL pointer.
4487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004489vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004491 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 return;
4493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004495 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497}
4498
4499/*
4500 * Find a file in a search context.
4501 * The search context was created with vim_findfile_init() above.
4502 * Return a pointer to an allocated file name or NULL if nothing found.
4503 * To get all matching files call this function until you get NULL.
4504 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004505 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 *
4507 * The search algorithm is depth first. To change this replace the
4508 * stack with a list (don't forget to leave partly searched directories on the
4509 * top of the list).
4510 */
4511 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004512vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513{
4514 char_u *file_path;
4515#ifdef FEAT_PATH_EXTRA
4516 char_u *rest_of_wildcards;
4517 char_u *path_end = NULL;
4518#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004519 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4521 int len;
4522#endif
4523 int i;
4524 char_u *p;
4525#ifdef FEAT_SEARCHPATH
4526 char_u *suf;
4527#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004528 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004530 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return NULL;
4532
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004533 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 /*
4536 * filepath is used as buffer for various actions and as the storage to
4537 * return a found filename.
4538 */
4539 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4540 return NULL;
4541
4542#ifdef FEAT_PATH_EXTRA
4543 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004544 if (search_ctx->ffsc_start_dir != NULL)
4545 path_end = &search_ctx->ffsc_start_dir[
4546 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547#endif
4548
4549#ifdef FEAT_PATH_EXTRA
4550 /* upward search loop */
4551 for (;;)
4552 {
4553#endif
4554 /* downward search loop */
4555 for (;;)
4556 {
4557 /* check if user user wants to stop the search*/
4558 ui_breakcheck();
4559 if (got_int)
4560 break;
4561
4562 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004563 stackp = ff_pop(search_ctx);
4564 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 break;
4566
4567 /*
4568 * TODO: decide if we leave this test in
4569 *
4570 * GOOD: don't search a directory(-tree) twice.
4571 * BAD: - check linked list for every new directory entered.
4572 * - check for double files also done below
4573 *
4574 * Here we check if we already searched this directory.
4575 * We already searched a directory if:
4576 * 1) The directory is the same.
4577 * 2) We would use the same wildcard string.
4578 *
4579 * Good if you have links on same directory via several ways
4580 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4581 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4582 *
4583 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004584 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004586 if (stackp->ffs_filearray == NULL
4587 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004589 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004591 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#endif
4593 ) == FAIL)
4594 {
4595#ifdef FF_VERBOSE
4596 if (p_verbose >= 5)
4597 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004598 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004600 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 /* don't overwrite this either */
4602 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004603 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004606 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 continue;
4608 }
4609#ifdef FF_VERBOSE
4610 else if (p_verbose >= 5)
4611 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004612 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004613 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004614 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 /* don't overwrite this either */
4616 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004617 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619#endif
4620
4621 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004622 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004624 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 continue;
4626 }
4627
4628 file_path[0] = NUL;
4629
4630 /*
4631 * If no filearray till now expand wildcards
4632 * The function expand_wildcards() can handle an array of paths
4633 * and all possible expands are returned in one array. We use this
4634 * to handle the expansion of '**' into an empty string.
4635 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004636 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 char_u *dirptrs[2];
4639
4640 /* we use filepath to build the path expand_wildcards() should
4641 * expand.
4642 */
4643 dirptrs[0] = file_path;
4644 dirptrs[1] = NULL;
4645
4646 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004647 if (!vim_isAbsName(stackp->ffs_fix_path)
4648 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004650 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4651 {
4652 STRCPY(file_path, search_ctx->ffsc_start_dir);
4653 add_pathsep(file_path);
4654 }
4655 else
4656 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
4658
4659 /* append the fix part of the search path */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004660 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4661 {
4662 STRCAT(file_path, stackp->ffs_fix_path);
4663 add_pathsep(file_path);
4664 }
4665 else
4666 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004669 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (*rest_of_wildcards != NUL)
4671 {
4672 len = (int)STRLEN(file_path);
4673 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4674 {
4675 /* pointer to the restrict byte
4676 * The restrict byte is not a character!
4677 */
4678 p = rest_of_wildcards + 2;
4679
4680 if (*p > 0)
4681 {
4682 (*p)--;
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004683 if (len + 1 < MAXPATHL)
4684 file_path[len++] = '*';
4685 else
4686 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688
4689 if (*p == 0)
4690 {
4691 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004692 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 else
4695 rest_of_wildcards += 3;
4696
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004697 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
4699 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004700 stackp->ffs_star_star_empty = 1;
4701 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 }
4704
4705 /*
4706 * Here we copy until the next path separator or the end of
4707 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004708 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 * pushing every directory returned from expand_wildcards()
4710 * on the stack again for further search.
4711 */
4712 while (*rest_of_wildcards
4713 && !vim_ispathsep(*rest_of_wildcards))
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004714 if (len + 1 < MAXPATHL)
4715 file_path[len++] = *rest_of_wildcards++;
4716 else
4717 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
4719 file_path[len] = NUL;
4720 if (vim_ispathsep(*rest_of_wildcards))
4721 rest_of_wildcards++;
4722 }
4723#endif
4724
4725 /*
4726 * Expand wildcards like "*" and "$VAR".
4727 * If the path is a URL don't try this.
4728 */
4729 if (path_with_url(dirptrs[0]))
4730 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004731 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004733 if (stackp->ffs_filearray != NULL
4734 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004736 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004738 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004741 /* Add EW_NOTWILD because the expanded path may contain
4742 * wildcard characters that are to be taken literally.
4743 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004745 &stackp->ffs_filearray_size,
4746 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004747 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004749 stackp->ffs_filearray_cur = 0;
4750 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752#ifdef FEAT_PATH_EXTRA
4753 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004754 rest_of_wildcards = &stackp->ffs_wc_path[
4755 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756#endif
4757
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004758 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
4760 /* this is the first time we work on this directory */
4761#ifdef FEAT_PATH_EXTRA
4762 if (*rest_of_wildcards == NUL)
4763#endif
4764 {
4765 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004766 * We don't have further wildcards to expand, so we have to
4767 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004769 for (i = stackp->ffs_filearray_cur;
4770 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004772 if (!path_with_url(stackp->ffs_filearray[i])
4773 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 continue; /* not a directory */
4775
Bram Moolenaar21160b92009-11-11 15:56:10 +00004776 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 * below */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004778 if (STRLEN(stackp->ffs_filearray[i]) + 1
4779 + STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4780 {
4781 STRCPY(file_path, stackp->ffs_filearray[i]);
4782 add_pathsep(file_path);
4783 STRCAT(file_path, search_ctx->ffsc_file_to_search);
4784 }
4785 else
4786 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 /*
4789 * Try without extra suffix and then with suffixes
4790 * from 'suffixesadd'.
4791 */
4792#ifdef FEAT_SEARCHPATH
4793 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004794 if (search_ctx->ffsc_tagfile)
4795 suf = (char_u *)"";
4796 else
4797 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 for (;;)
4799#endif
4800 {
4801 /* if file exists and we didn't already find it */
4802 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004803 || (mch_getperm(file_path) >= 0
4804 && (search_ctx->ffsc_find_what
4805 == FINDFILE_BOTH
4806 || ((search_ctx->ffsc_find_what
4807 == FINDFILE_DIR)
4808 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809#ifndef FF_VERBOSE
4810 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004811 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 file_path
4813#ifdef FEAT_PATH_EXTRA
4814 , (char_u *)""
4815#endif
4816 ) == OK)
4817#endif
4818 )
4819 {
4820#ifdef FF_VERBOSE
4821 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004822 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 file_path
4824#ifdef FEAT_PATH_EXTRA
4825 , (char_u *)""
4826#endif
4827 ) == FAIL)
4828 {
4829 if (p_verbose >= 5)
4830 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004831 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004832 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 file_path);
4834 /* don't overwrite this either */
4835 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004836 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 continue;
4839 }
4840#endif
4841
4842 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004843 stackp->ffs_filearray_cur = i + 1;
4844 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004846 if (!path_with_url(file_path))
4847 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4849 == OK)
4850 {
4851 p = shorten_fname(file_path,
4852 ff_expand_buffer);
4853 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004854 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856#ifdef FF_VERBOSE
4857 if (p_verbose >= 5)
4858 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004859 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004860 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 /* don't overwrite this either */
4862 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004863 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865#endif
4866 return file_path;
4867 }
4868
4869#ifdef FEAT_SEARCHPATH
4870 /* Not found or found already, try next suffix. */
4871 if (*suf == NUL)
4872 break;
4873 copy_option_part(&suf, file_path + len,
4874 MAXPATHL - len, ",");
4875#endif
4876 }
4877 }
4878 }
4879#ifdef FEAT_PATH_EXTRA
4880 else
4881 {
4882 /*
4883 * still wildcards left, push the directories for further
4884 * search
4885 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004886 for (i = stackp->ffs_filearray_cur;
4887 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004889 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 continue; /* not a directory */
4891
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004892 ff_push(search_ctx,
4893 ff_create_stack_element(
4894 stackp->ffs_filearray[i],
4895 rest_of_wildcards,
4896 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898 }
4899#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004900 stackp->ffs_filearray_cur = 0;
4901 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903
4904#ifdef FEAT_PATH_EXTRA
4905 /*
4906 * if wildcards contains '**' we have to descent till we reach the
4907 * leaves of the directory tree.
4908 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004909 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004911 for (i = stackp->ffs_filearray_cur;
4912 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004914 if (fnamecmp(stackp->ffs_filearray[i],
4915 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004917 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004919 ff_push(search_ctx,
4920 ff_create_stack_element(stackp->ffs_filearray[i],
4921 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 }
4924#endif
4925
4926 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004927 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929 }
4930
4931#ifdef FEAT_PATH_EXTRA
4932 /* If we reached this, we didn't find anything downwards.
4933 * Let's check if we should do an upward search.
4934 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004935 if (search_ctx->ffsc_start_dir
4936 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 ff_stack_T *sptr;
4939
4940 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004941 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4942 (int)(path_end - search_ctx->ffsc_start_dir),
4943 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 break;
4945
4946 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004947 while (path_end > search_ctx->ffsc_start_dir
4948 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004950 while (path_end > search_ctx->ffsc_start_dir
4951 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 path_end--;
4953 *path_end = 0;
4954 path_end--;
4955
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004956 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 break;
4958
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004959 if (STRLEN(search_ctx->ffsc_start_dir) + 1
4960 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
4961 {
4962 STRCPY(file_path, search_ctx->ffsc_start_dir);
4963 add_pathsep(file_path);
4964 STRCAT(file_path, search_ctx->ffsc_fix_path);
4965 }
4966 else
4967 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 /* create a new stack entry */
4970 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004971 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (sptr == NULL)
4973 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004974 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 else
4977 break;
4978 }
4979#endif
4980
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004981fail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 vim_free(file_path);
4983 return NULL;
4984}
4985
4986/*
4987 * Free the list of lists of visited files and directories
4988 * Can handle it if the passed search_context is NULL;
4989 */
4990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004991vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004993 ff_search_ctx_T *search_ctx;
4994
4995 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 return;
4997
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004998 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
4999 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
5000 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001}
5002
5003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005004vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006 ff_visited_list_hdr_T *vp;
5007
5008 while (*list_headp != NULL)
5009 {
5010 vp = (*list_headp)->ffvl_next;
5011 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5012
5013 vim_free((*list_headp)->ffvl_filename);
5014 vim_free(*list_headp);
5015 *list_headp = vp;
5016 }
5017 *list_headp = NULL;
5018}
5019
5020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005021ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
5023 ff_visited_T *vp;
5024
5025 while (vl != NULL)
5026 {
5027 vp = vl->ffv_next;
5028#ifdef FEAT_PATH_EXTRA
5029 vim_free(vl->ffv_wc_path);
5030#endif
5031 vim_free(vl);
5032 vl = vp;
5033 }
5034 vl = NULL;
5035}
5036
5037/*
5038 * Returns the already visited list for the given filename. If none is found it
5039 * allocates a new one.
5040 */
5041 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01005042ff_get_visited_list(
5043 char_u *filename,
5044 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
5046 ff_visited_list_hdr_T *retptr = NULL;
5047
5048 /* check if a visited list for the given filename exists */
5049 if (*list_headp != NULL)
5050 {
5051 retptr = *list_headp;
5052 while (retptr != NULL)
5053 {
5054 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5055 {
5056#ifdef FF_VERBOSE
5057 if (p_verbose >= 5)
5058 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005059 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005060 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 filename);
5062 /* don't overwrite this either */
5063 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005064 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066#endif
5067 return retptr;
5068 }
5069 retptr = retptr->ffvl_next;
5070 }
5071 }
5072
5073#ifdef FF_VERBOSE
5074 if (p_verbose >= 5)
5075 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005076 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005077 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 /* don't overwrite this either */
5079 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005080 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082#endif
5083
5084 /*
5085 * if we reach this we didn't find a list and we have to allocate new list
5086 */
5087 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5088 if (retptr == NULL)
5089 return NULL;
5090
5091 retptr->ffvl_visited_list = NULL;
5092 retptr->ffvl_filename = vim_strsave(filename);
5093 if (retptr->ffvl_filename == NULL)
5094 {
5095 vim_free(retptr);
5096 return NULL;
5097 }
5098 retptr->ffvl_next = *list_headp;
5099 *list_headp = retptr;
5100
5101 return retptr;
5102}
5103
5104#ifdef FEAT_PATH_EXTRA
5105/*
5106 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5107 * They are equal if:
5108 * - both paths are NULL
5109 * - they have the same length
5110 * - char by char comparison is OK
5111 * - the only differences are in the counters behind a '**', so
5112 * '**\20' is equal to '**\24'
5113 */
5114 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005115ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005117 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005118 int c1 = NUL;
5119 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005120 int prev1 = NUL;
5121 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 if (s1 == s2)
5124 return TRUE;
5125
5126 if (s1 == NULL || s2 == NULL)
5127 return FALSE;
5128
Bram Moolenaard43f0952015-08-27 22:30:47 +02005129 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005131 c1 = PTR2CHAR(s1 + i);
5132 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005133
5134 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5135 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005136 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005137 prev2 = prev1;
5138 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005139
5140 i += MB_PTR2LEN(s1 + i);
5141 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005143 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144}
5145#endif
5146
5147/*
5148 * maintains the list of already visited files and dirs
5149 * returns FAIL if the given file/dir is already in the list
5150 * returns OK if it is newly added
5151 *
5152 * TODO: What to do on memory allocation problems?
5153 * -> return TRUE - Better the file is found several times instead of
5154 * never.
5155 */
5156 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005157ff_check_visited(
5158 ff_visited_T **visited_list,
5159 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005161 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005163 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
5165 ff_visited_T *vp;
5166#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02005167 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 int url = FALSE;
5169#endif
5170
5171 /* For an URL we only compare the name, otherwise we compare the
5172 * device/inode (unix) or the full path name (not Unix). */
5173 if (path_with_url(fname))
5174 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005175 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#ifdef UNIX
5177 url = TRUE;
5178#endif
5179 }
5180 else
5181 {
5182 ff_expand_buffer[0] = NUL;
5183#ifdef UNIX
5184 if (mch_stat((char *)fname, &st) < 0)
5185#else
5186 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5187#endif
5188 return FAIL;
5189 }
5190
5191 /* check against list of already visited files */
5192 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5193 {
5194 if (
5195#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005196 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5197 && vp->ffv_ino == st.st_ino)
5198 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199#endif
5200 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5201 )
5202 {
5203#ifdef FEAT_PATH_EXTRA
5204 /* are the wildcard parts equal */
5205 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5206#endif
5207 /* already visited */
5208 return FAIL;
5209 }
5210 }
5211
5212 /*
5213 * New file/dir. Add it to the list of visited files/dirs.
5214 */
5215 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5216 + STRLEN(ff_expand_buffer)));
5217
5218 if (vp != NULL)
5219 {
5220#ifdef UNIX
5221 if (!url)
5222 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005223 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 vp->ffv_ino = st.st_ino;
5225 vp->ffv_dev = st.st_dev;
5226 vp->ffv_fname[0] = NUL;
5227 }
5228 else
5229 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005230 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231#endif
5232 STRCPY(vp->ffv_fname, ff_expand_buffer);
5233#ifdef UNIX
5234 }
5235#endif
5236#ifdef FEAT_PATH_EXTRA
5237 if (wc_path != NULL)
5238 vp->ffv_wc_path = vim_strsave(wc_path);
5239 else
5240 vp->ffv_wc_path = NULL;
5241#endif
5242
5243 vp->ffv_next = *visited_list;
5244 *visited_list = vp;
5245 }
5246
5247 return OK;
5248}
5249
5250/*
5251 * create stack element from given path pieces
5252 */
5253 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005254ff_create_stack_element(
5255 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005257 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005259 int level,
5260 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 ff_stack_T *new;
5263
5264 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5265 if (new == NULL)
5266 return NULL;
5267
5268 new->ffs_prev = NULL;
5269 new->ffs_filearray = NULL;
5270 new->ffs_filearray_size = 0;
5271 new->ffs_filearray_cur = 0;
5272 new->ffs_stage = 0;
5273 new->ffs_level = level;
Bram Moolenaar945ec092016-06-08 21:17:43 +02005274 new->ffs_star_star_empty = star_star_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 /* the following saves NULL pointer checks in vim_findfile */
5277 if (fix_part == NULL)
5278 fix_part = (char_u *)"";
5279 new->ffs_fix_path = vim_strsave(fix_part);
5280
5281#ifdef FEAT_PATH_EXTRA
5282 if (wc_part == NULL)
5283 wc_part = (char_u *)"";
5284 new->ffs_wc_path = vim_strsave(wc_part);
5285#endif
5286
5287 if (new->ffs_fix_path == NULL
5288#ifdef FEAT_PATH_EXTRA
5289 || new->ffs_wc_path == NULL
5290#endif
5291 )
5292 {
5293 ff_free_stack_element(new);
5294 new = NULL;
5295 }
5296
5297 return new;
5298}
5299
5300/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005301 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 */
5303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005304ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
5306 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005307 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005308 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005310 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5311 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313}
5314
5315/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005316 * Pop a dir from the directory stack.
5317 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 */
5319 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005320ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322 ff_stack_T *sptr;
5323
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005324 sptr = search_ctx->ffsc_stack_ptr;
5325 if (search_ctx->ffsc_stack_ptr != NULL)
5326 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 return sptr;
5329}
5330
5331/*
5332 * free the given stack element
5333 */
5334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005335ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005338 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005340 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#endif
5342
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005343 if (stack_ptr->ffs_filearray != NULL)
5344 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005346 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347}
5348
5349/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005350 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 */
5352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005353ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 ff_stack_T *sptr;
5356
5357 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005358 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 ff_free_stack_element(sptr);
5360
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005361 vim_free(search_ctx->ffsc_file_to_search);
5362 vim_free(search_ctx->ffsc_start_dir);
5363 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005365 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366#endif
5367
5368#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005369 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
5371 int i = 0;
5372
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005373 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005375 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 i++;
5377 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005378 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005380 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381#endif
5382
5383 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005384 search_ctx->ffsc_file_to_search = NULL;
5385 search_ctx->ffsc_start_dir = NULL;
5386 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005388 search_ctx->ffsc_wc_path = NULL;
5389 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#endif
5391}
5392
5393#ifdef FEAT_PATH_EXTRA
5394/*
5395 * check if the given path is in the stopdirs
5396 * returns TRUE if yes else FALSE
5397 */
5398 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005399ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 int i = 0;
5402
5403 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005404 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 path_len--;
5406
5407 /* if no path consider it as match */
5408 if (path_len == 0)
5409 return TRUE;
5410
5411 for (i = 0; stopdirs_v[i] != NULL; i++)
5412 {
5413 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5414 {
5415 /* match for parent directory. So '/home' also matches
5416 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5417 * '/home/r' would also match '/home/rks'
5418 */
5419 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005420 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 return TRUE;
5422 }
5423 else
5424 {
5425 if (fnamecmp(stopdirs_v[i], path) == 0)
5426 return TRUE;
5427 }
5428 }
5429 return FALSE;
5430}
5431#endif
5432
5433#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5434/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005435 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 *
5437 * On the first call set the parameter 'first' to TRUE to initialize
5438 * the search. For repeating calls to FALSE.
5439 *
5440 * Repeating calls will return other files called 'ptr[len]' from the path.
5441 *
5442 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5443 * don't need valid values.
5444 *
5445 * If nothing found on the first call the option FNAME_MESS will issue the
5446 * message:
5447 * 'Can't find file "<file>" in path'
5448 * On repeating calls:
5449 * 'No more file "<file>" found in path'
5450 *
5451 * options:
5452 * FNAME_MESS give error message when not found
5453 *
5454 * Uses NameBuff[]!
5455 *
5456 * Returns an allocated string for the file name. NULL for error.
5457 *
5458 */
5459 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005460find_file_in_path(
5461 char_u *ptr, /* file name */
5462 int len, /* length of file name */
5463 int options,
5464 int first, /* use count'th matching file name */
5465 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 return find_file_in_path_option(ptr, len, options, first,
5468 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005469 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470}
5471
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005472static char_u *ff_file_to_find = NULL;
5473static void *fdip_search_ctx = NULL;
5474
5475#if defined(EXITFREE)
5476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005477free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005478{
5479 vim_free(ff_file_to_find);
5480 vim_findfile_cleanup(fdip_search_ctx);
5481}
5482#endif
5483
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484/*
5485 * Find the directory name "ptr[len]" in the path.
5486 *
5487 * options:
5488 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005489 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 *
5491 * Uses NameBuff[]!
5492 *
5493 * Returns an allocated string for the file name. NULL for error.
5494 */
5495 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005496find_directory_in_path(
5497 char_u *ptr, /* file name */
5498 int len, /* length of file name */
5499 int options,
5500 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005503 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504}
5505
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005506 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005507find_file_in_path_option(
5508 char_u *ptr, /* file name */
5509 int len, /* length of file name */
5510 int options,
5511 int first, /* use count'th matching file name */
5512 char_u *path_option, /* p_path or p_cdpath */
5513 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5514 char_u *rel_fname, /* file name we are looking relative to. */
5515 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 static int did_findfile_init = FALSE;
5519 char_u save_char;
5520 char_u *file_name = NULL;
5521 char_u *buf = NULL;
5522 int rel_to_curdir;
5523#ifdef AMIGA
5524 struct Process *proc = (struct Process *)FindTask(0L);
5525 APTR save_winptr = proc->pr_WindowPtr;
5526
5527 /* Avoid a requester here for a volume that doesn't exist. */
5528 proc->pr_WindowPtr = (APTR)-1L;
5529#endif
5530
5531 if (first == TRUE)
5532 {
5533 /* copy file name into NameBuff, expanding environment variables */
5534 save_char = ptr[len];
5535 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005536 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 ptr[len] = save_char;
5538
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005539 vim_free(ff_file_to_find);
5540 ff_file_to_find = vim_strsave(NameBuff);
5541 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
5543 file_name = NULL;
5544 goto theend;
5545 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005546 if (options & FNAME_UNESC)
5547 {
5548 /* Change all "\ " to " ". */
5549 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5550 if (ptr[0] == '\\' && ptr[1] == ' ')
5551 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005555 rel_to_curdir = (ff_file_to_find[0] == '.'
5556 && (ff_file_to_find[1] == NUL
5557 || vim_ispathsep(ff_file_to_find[1])
5558 || (ff_file_to_find[1] == '.'
5559 && (ff_file_to_find[2] == NUL
5560 || vim_ispathsep(ff_file_to_find[2])))));
5561 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 /* "..", "../path", "." and "./path": don't use the path_option */
5563 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005564#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005566 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005567 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005568 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569#endif
5570#ifdef AMIGA
5571 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005572 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#endif
5574 )
5575 {
5576 /*
5577 * Absolute path, no need to use "path_option".
5578 * If this is not a first call, return NULL. We already returned a
5579 * filename on the first call.
5580 */
5581 if (first == TRUE)
5582 {
5583 int l;
5584 int run;
5585
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005586 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005588 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 goto theend;
5590 }
5591
5592 /* When FNAME_REL flag given first use the directory of the file.
5593 * Otherwise or when this fails use the current directory. */
5594 for (run = 1; run <= 2; ++run)
5595 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005596 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (run == 1
5598 && rel_to_curdir
5599 && (options & FNAME_REL)
5600 && rel_fname != NULL
5601 && STRLEN(rel_fname) + l < MAXPATHL)
5602 {
5603 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005604 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 l = (int)STRLEN(NameBuff);
5606 }
5607 else
5608 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005609 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 run = 2;
5611 }
5612
5613 /* When the file doesn't exist, try adding parts of
5614 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005615 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 for (;;)
5617 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005618 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005619 && (find_what == FINDFILE_BOTH
5620 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005621 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 {
5623 file_name = vim_strsave(NameBuff);
5624 goto theend;
5625 }
5626 if (*buf == NUL)
5627 break;
5628 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5629 }
5630 }
5631 }
5632 }
5633 else
5634 {
5635 /*
5636 * Loop over all paths in the 'path' or 'cdpath' option.
5637 * When "first" is set, first setup to the start of the option.
5638 * Otherwise continue to find the next match.
5639 */
5640 if (first == TRUE)
5641 {
5642 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005643 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 dir = path_option;
5645 did_findfile_init = FALSE;
5646 }
5647
5648 for (;;)
5649 {
5650 if (did_findfile_init)
5651 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005652 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (file_name != NULL)
5654 break;
5655
5656 did_findfile_init = FALSE;
5657 }
5658 else
5659 {
5660 char_u *r_ptr;
5661
5662 if (dir == NULL || *dir == NUL)
5663 {
5664 /* We searched all paths of the option, now we can
5665 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005666 vim_findfile_cleanup(fdip_search_ctx);
5667 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 break;
5669 }
5670
5671 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5672 break;
5673
5674 /* copy next path */
5675 buf[0] = 0;
5676 copy_option_part(&dir, buf, MAXPATHL, " ,");
5677
5678#ifdef FEAT_PATH_EXTRA
5679 /* get the stopdir string */
5680 r_ptr = vim_findfile_stopdir(buf);
5681#else
5682 r_ptr = NULL;
5683#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005684 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005685 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005686 fdip_search_ctx, FALSE, rel_fname);
5687 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 did_findfile_init = TRUE;
5689 vim_free(buf);
5690 }
5691 }
5692 }
5693 if (file_name == NULL && (options & FNAME_MESS))
5694 {
5695 if (first == TRUE)
5696 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005697 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005699 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 else
5701 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005702 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else
5705 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005706 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005708 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 else
5710 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005711 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 }
5714
5715theend:
5716#ifdef AMIGA
5717 proc->pr_WindowPtr = save_winptr;
5718#endif
5719 return file_name;
5720}
5721
5722#endif /* FEAT_SEARCHPATH */
5723
5724/*
5725 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5726 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5727 */
5728 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005729vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731#ifndef FEAT_SEARCHPATH
5732 return mch_chdir((char *)new_dir);
5733#else
5734 char_u *dir_name;
5735 int r;
5736
5737 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5738 FNAME_MESS, curbuf->b_ffname);
5739 if (dir_name == NULL)
5740 return -1;
5741 r = mch_chdir((char *)dir_name);
5742 vim_free(dir_name);
5743 return r;
5744#endif
5745}
5746
5747/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005748 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005750 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5751 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 * Returns OK or FAIL.
5753 */
5754 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005755get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005757 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
5759 if (mch_get_user_name(buf, len) == FAIL)
5760 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005761 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
5763 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005764 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 return OK;
5766}
5767
5768#ifndef HAVE_QSORT
5769/*
5770 * Our own qsort(), for systems that don't have it.
5771 * It's simple and slow. From the K&R C book.
5772 */
5773 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774qsort(
5775 void *base,
5776 size_t elm_count,
5777 size_t elm_size,
5778 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 char_u *buf;
5781 char_u *p1;
5782 char_u *p2;
5783 int i, j;
5784 int gap;
5785
5786 buf = alloc((unsigned)elm_size);
5787 if (buf == NULL)
5788 return;
5789
5790 for (gap = elm_count / 2; gap > 0; gap /= 2)
5791 for (i = gap; i < elm_count; ++i)
5792 for (j = i - gap; j >= 0; j -= gap)
5793 {
5794 /* Compare the elements. */
5795 p1 = (char_u *)base + j * elm_size;
5796 p2 = (char_u *)base + (j + gap) * elm_size;
5797 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5798 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005799 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 mch_memmove(buf, p1, elm_size);
5801 mch_memmove(p1, p2, elm_size);
5802 mch_memmove(p2, buf, elm_size);
5803 }
5804
5805 vim_free(buf);
5806}
5807#endif
5808
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809/*
5810 * Sort an array of strings.
5811 */
5812static int
5813#ifdef __BORLANDC__
5814_RTLENTRYF
5815#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005816sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 static int
5819#ifdef __BORLANDC__
5820_RTLENTRYF
5821#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005822sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 return STRCMP(*(char **)s1, *(char **)s2);
5825}
5826
5827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005828sort_strings(
5829 char_u **files,
5830 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835#if !defined(NO_EXPANDPATH) || defined(PROTO)
5836/*
5837 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005838 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * Return value like strcmp(p, q), but consider path separators.
5840 */
5841 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005842pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005844 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005845 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005846 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005848 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005850 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005851 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005854 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005856 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 return 0;
5858 s = q;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005859 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 break;
5861 }
5862
5863 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005864 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {
5866 s = p;
5867 break;
5868 }
5869
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005870 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#ifdef BACKSLASH_IN_FILENAME
5872 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005873 && !((c1 == '/' && c2 == '\\')
5874 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875#endif
5876 )
5877 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005878 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005880 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005882 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5883 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005885
5886 i += MB_PTR2LEN((char_u *)p + i);
5887 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005889 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005890 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005892 c1 = PTR2CHAR((char_u *)s + i);
5893 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005895 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005896 && i > 0
5897 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005899 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005901 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005903 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 return 0; /* match with trailing slash */
5905 if (s == q)
5906 return -1; /* no match */
5907 return 1;
5908}
5909#endif
5910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911/*
5912 * The putenv() implementation below comes from the "screen" program.
5913 * Included with permission from Juergen Weigert.
5914 * See pty.c for the copyright notice.
5915 */
5916
5917/*
5918 * putenv -- put value into environment
5919 *
5920 * Usage: i = putenv (string)
5921 * int i;
5922 * char *string;
5923 *
5924 * where string is of the form <name>=<value>.
5925 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5926 *
5927 * Putenv may need to add a new name into the environment, or to
5928 * associate a value longer than the current value with a particular
5929 * name. So, to make life simpler, putenv() copies your entire
5930 * environment into the heap (i.e. malloc()) from the stack
5931 * (i.e. where it resides when your process is initiated) the first
5932 * time you call it.
5933 *
5934 * (history removed, not very interesting. See the "screen" sources.)
5935 */
5936
5937#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5938
5939#define EXTRASIZE 5 /* increment to add to env. size */
5940
5941static int envsize = -1; /* current size of environment */
Bram Moolenaard0573012017-10-28 21:11:06 +02005942extern char **environ; /* the global which is your env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005944static int findenv(char *name); /* look for a name in the env. */
5945static int newenv(void); /* copy env. from stack to heap */
5946static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005949putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 int i;
5952 char *p;
5953
5954 if (envsize < 0)
5955 { /* first time putenv called */
5956 if (newenv() < 0) /* copy env. to heap */
5957 return -1;
5958 }
5959
5960 i = findenv((char *)string); /* look for name in environment */
5961
5962 if (i < 0)
5963 { /* name must be added */
5964 for (i = 0; environ[i]; i++);
5965 if (i >= (envsize - 1))
5966 { /* need new slot */
5967 if (moreenv() < 0)
5968 return -1;
5969 }
5970 p = (char *)alloc((unsigned)(strlen(string) + 1));
5971 if (p == NULL) /* not enough core */
5972 return -1;
5973 environ[i + 1] = 0; /* new end of env. */
5974 }
5975 else
5976 { /* name already in env. */
5977 p = vim_realloc(environ[i], strlen(string) + 1);
5978 if (p == NULL)
5979 return -1;
5980 }
5981 sprintf(p, "%s", string); /* copy into env. */
5982 environ[i] = p;
5983
5984 return 0;
5985}
5986
5987 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005988findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 char *namechar, *envchar;
5991 int i, found;
5992
5993 found = 0;
5994 for (i = 0; environ[i] && !found; i++)
5995 {
5996 envchar = environ[i];
5997 namechar = name;
5998 while (*namechar && *namechar != '=' && (*namechar == *envchar))
5999 {
6000 namechar++;
6001 envchar++;
6002 }
6003 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6004 }
6005 return found ? i - 1 : -1;
6006}
6007
6008 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006009newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 char **env, *elem;
6012 int i, esize;
6013
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 for (i = 0; environ[i]; i++)
6015 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02006016
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 esize = i + EXTRASIZE + 1;
6018 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6019 if (env == NULL)
6020 return -1;
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 for (i = 0; environ[i]; i++)
6023 {
6024 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6025 if (elem == NULL)
6026 return -1;
6027 env[i] = elem;
6028 strcpy(elem, environ[i]);
6029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
6031 env[i] = 0;
6032 environ = env;
6033 envsize = esize;
6034 return 0;
6035}
6036
6037 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006038moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 int esize;
6041 char **env;
6042
6043 esize = envsize + EXTRASIZE;
6044 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6045 if (env == 0)
6046 return -1;
6047 environ = env;
6048 envsize = esize;
6049 return 0;
6050}
6051
6052# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02006053/*
6054 * Used for mch_getenv() for Mac.
6055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006057vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 int i;
6060 char_u *p;
6061
6062 if (envsize < 0)
6063 return NULL;
6064
6065 i = findenv((char *)string);
6066
6067 if (i < 0)
6068 return NULL;
6069
6070 p = vim_strchr((char_u *)environ[i], '=');
6071 return (p + 1);
6072}
6073# endif
6074
6075#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006076
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006077#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006078/*
6079 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6080 * rights to write into.
6081 */
6082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006083filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006084{
6085 int retval = 0;
6086#if defined(UNIX) || defined(VMS)
6087 int perm = 0;
6088#endif
6089
6090#if defined(UNIX) || defined(VMS)
6091 perm = mch_getperm(fname);
6092#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006093 if (
6094# ifdef WIN3264
6095 mch_writable(fname) &&
6096# else
6097# if defined(UNIX) || defined(VMS)
6098 (perm & 0222) &&
6099# endif
6100# endif
6101 mch_access((char *)fname, W_OK) == 0
6102 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006103 {
6104 ++retval;
6105 if (mch_isdir(fname))
6106 ++retval;
6107 }
6108 return retval;
6109}
6110#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006111
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006112#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6113/*
6114 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6115 */
6116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006117get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006118{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006119 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006120
6121 n = getc(fd);
6122 n = (n << 8) + getc(fd);
6123 return n;
6124}
6125
6126/*
6127 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6128 */
6129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006130get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006131{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006132 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006133
6134 n = getc(fd);
6135 n = (n << 8) + getc(fd);
6136 n = (n << 8) + getc(fd);
6137 return n;
6138}
6139
6140/*
6141 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6142 */
6143 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006144get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006145{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006146 /* Use unsigned rather than int otherwise result is undefined
6147 * when left-shift sets the MSB. */
6148 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006149
Bram Moolenaar95235e62013-09-08 16:07:07 +02006150 n = (unsigned)getc(fd);
6151 n = (n << 8) + (unsigned)getc(fd);
6152 n = (n << 8) + (unsigned)getc(fd);
6153 n = (n << 8) + (unsigned)getc(fd);
6154 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006155}
6156
6157/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006158 * Read 8 bytes from "fd" and turn them into a time_T, MSB first.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006159 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006160 time_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006161get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006162{
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006163 time_T n = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006164 int i;
6165
6166 for (i = 0; i < 8; ++i)
6167 n = (n << 8) + getc(fd);
6168 return n;
6169}
6170
6171/*
6172 * Read a string of length "cnt" from "fd" into allocated memory.
6173 * Returns NULL when out of memory or unable to read that many bytes.
6174 */
6175 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006176read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006177{
6178 char_u *str;
6179 int i;
6180 int c;
6181
6182 /* allocate memory */
6183 str = alloc((unsigned)cnt + 1);
6184 if (str != NULL)
6185 {
6186 /* Read the string. Quit when running into the EOF. */
6187 for (i = 0; i < cnt; ++i)
6188 {
6189 c = getc(fd);
6190 if (c == EOF)
6191 {
6192 vim_free(str);
6193 return NULL;
6194 }
6195 str[i] = c;
6196 }
6197 str[i] = NUL;
6198 }
6199 return str;
6200}
6201
6202/*
6203 * Write a number to file "fd", MSB first, in "len" bytes.
6204 */
6205 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006206put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006207{
6208 int i;
6209
6210 for (i = len - 1; i >= 0; --i)
6211 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6212 return FAIL;
6213 return OK;
6214}
6215
6216#ifdef _MSC_VER
6217# if (_MSC_VER <= 1200)
6218/* This line is required for VC6 without the service pack. Also see the
6219 * matching #pragma below. */
6220 # pragma optimize("", off)
6221# endif
6222#endif
6223
6224/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006225 * Write time_T to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006226 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006227 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006228 int
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006229put_time(FILE *fd, time_T the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006230{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006231 char_u buf[8];
6232
6233 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006234 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006235}
6236
6237/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006238 * Write time_T to "buf[8]".
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006239 */
6240 void
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006241time_to_bytes(time_T the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006242{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006243 int c;
6244 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006245 int bi = 0;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006246 time_T wtime = the_time;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006247
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006248 /* time_T can be up to 8 bytes in size, more than long_u, thus we
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006249 * can't use put_bytes() here.
6250 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006251 * sign. This happens for large values of wtime. A cast to long_u may
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006252 * truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes,
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006253 * it's safe to assume that long_u is 4 bytes or more and when using 8
6254 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006255 for (i = 7; i >= 0; --i)
6256 {
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006257 if (i + 1 > (int)sizeof(time_T))
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006258 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006259 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006260 else
6261 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006262#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006263 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006264#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006265 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006266#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006267 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006268 }
6269 }
6270}
6271
6272#ifdef _MSC_VER
6273# if (_MSC_VER <= 1200)
6274 # pragma optimize("", on)
6275# endif
6276#endif
6277
6278#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006279
6280#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6281 || defined(FEAT_SPELL) || defined(PROTO)
6282/*
6283 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6284 * When "s" is NULL FALSE is returned.
6285 */
6286 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006287has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006288{
6289 char_u *p;
6290
6291 if (s != NULL)
6292 for (p = s; *p != NUL; ++p)
6293 if (*p >= 128)
6294 return TRUE;
6295 return FALSE;
6296}
6297#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006298
6299#if defined(MESSAGE_QUEUE) || defined(PROTO)
6300/*
6301 * Process messages that have been queued for netbeans or clientserver.
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006302 * Also check if any jobs have ended.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006303 * These functions can call arbitrary vimscript and should only be called when
6304 * it is safe to do so.
6305 */
6306 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006307parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006308{
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006309 win_T *old_curwin = curwin;
6310
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006311 /* For Win32 mch_breakcheck() does not check for input, do it here. */
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006312# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006313 channel_handle_events(FALSE);
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006314# endif
6315
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006316# ifdef FEAT_NETBEANS_INTG
6317 /* Process the queued netbeans messages. */
6318 netbeans_parse_messages();
6319# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006320# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006321 /* Write any buffer lines still to be written. */
6322 channel_write_any_lines();
6323
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006324 /* Process the messages queued on channels. */
6325 channel_parse_messages();
6326# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006327# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006328 /* Process the queued clientserver messages. */
6329 server_parse_messages();
6330# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006331# ifdef FEAT_JOB_CHANNEL
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006332 /* Check if any jobs have ended. */
6333 job_check_ended();
6334# endif
Bram Moolenaara3f7e582017-11-09 13:21:58 +01006335
6336 /* If the current window changed we need to bail out of the waiting loop.
6337 * E.g. when a job exit callback closes the terminal window. */
6338 if (curwin != old_curwin)
6339 ins_char_typebuf(K_IGNORE);
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006340}
6341#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006342
Bram Moolenaar51628222016-12-01 23:03:28 +01006343#ifndef PROTO /* proto is defined in vim.h */
6344# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006345/*
6346 * Return time in msec since "start_tv".
6347 */
6348 long
6349elapsed(struct timeval *start_tv)
6350{
6351 struct timeval now_tv;
6352
6353 gettimeofday(&now_tv, NULL);
6354 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
6355 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
6356}
Bram Moolenaar51628222016-12-01 23:03:28 +01006357# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006358
Bram Moolenaar51628222016-12-01 23:03:28 +01006359# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006360/*
6361 * Return time in msec since "start_tick".
6362 */
6363 long
6364elapsed(DWORD start_tick)
6365{
6366 DWORD now = GetTickCount();
6367
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006368 return (long)now - (long)start_tick;
6369}
Bram Moolenaar51628222016-12-01 23:03:28 +01006370# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006371#endif