blob: fac44c737a30c43f9eece04f24c6f16fa9e204c1 [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{
351 char_u *p = ml_get_pos(lp);
352
353 if (*p != NUL) /* still within line, move to next char (may be NUL) */
354 {
355#ifdef FEAT_MBYTE
356 if (has_mbyte)
357 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000358 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360 lp->col += l;
361 return ((p[l] != NUL) ? 0 : 2);
362 }
363#endif
364 lp->col++;
365#ifdef FEAT_VIRTUALEDIT
366 lp->coladd = 0;
367#endif
368 return ((p[1] != NUL) ? 0 : 2);
369 }
370 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
371 {
372 lp->col = 0;
373 lp->lnum++;
374#ifdef FEAT_VIRTUALEDIT
375 lp->coladd = 0;
376#endif
377 return 1;
378 }
379 return -1;
380}
381
382/*
383 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
384 */
385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100386incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
388 int r;
389
390 if ((r = inc(lp)) >= 1 && lp->col)
391 r = inc(lp);
392 return r;
393}
394
395/*
396 * dec(p)
397 *
398 * Decrement the line pointer 'p' crossing line boundaries as necessary.
399 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
400 */
401 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100402dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100404 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405}
406
407 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100408dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
410 char_u *p;
411
412#ifdef FEAT_VIRTUALEDIT
413 lp->coladd = 0;
414#endif
415 if (lp->col > 0) /* still within line */
416 {
417 lp->col--;
418#ifdef FEAT_MBYTE
419 if (has_mbyte)
420 {
421 p = ml_get(lp->lnum);
422 lp->col -= (*mb_head_off)(p, p + lp->col);
423 }
424#endif
425 return 0;
426 }
427 if (lp->lnum > 1) /* there is a prior line */
428 {
429 lp->lnum--;
430 p = ml_get(lp->lnum);
431 lp->col = (colnr_T)STRLEN(p);
432#ifdef FEAT_MBYTE
433 if (has_mbyte)
434 lp->col -= (*mb_head_off)(p, p + lp->col);
435#endif
436 return 1;
437 }
438 return -1; /* at start of file */
439}
440
441/*
442 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
443 */
444 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100445decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446{
447 int r;
448
449 if ((r = dec(lp)) == 1 && lp->col)
450 r = dec(lp);
451 return r;
452}
453
454/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200455 * Get the line number relative to the current cursor position, i.e. the
456 * difference between line number and cursor position. Only look for lines that
457 * can be visible, folded lines don't count.
458 */
459 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100460get_cursor_rel_lnum(
461 win_T *wp,
462 linenr_T lnum) /* line number to get the result for */
Bram Moolenaar64486672010-05-16 15:46:46 +0200463{
464 linenr_T cursor = wp->w_cursor.lnum;
465 linenr_T retval = 0;
466
467#ifdef FEAT_FOLDING
468 if (hasAnyFolding(wp))
469 {
470 if (lnum > cursor)
471 {
472 while (lnum > cursor)
473 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100474 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200475 /* if lnum and cursor are in the same fold,
476 * now lnum <= cursor */
477 if (lnum > cursor)
478 retval++;
479 lnum--;
480 }
481 }
482 else if (lnum < cursor)
483 {
484 while (lnum < cursor)
485 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100486 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200487 /* if lnum and cursor are in the same fold,
488 * now lnum >= cursor */
489 if (lnum < cursor)
490 retval--;
491 lnum++;
492 }
493 }
494 /* else if (lnum == cursor)
495 * retval = 0;
496 */
497 }
498 else
499#endif
500 retval = lnum - cursor;
501
502 return retval;
503}
504
505/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200506 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
507 * This allows for the col to be on the NUL byte.
508 */
509 void
510check_pos(buf_T *buf, pos_T *pos)
511{
512 char_u *line;
513 colnr_T len;
514
515 if (pos->lnum > buf->b_ml.ml_line_count)
516 pos->lnum = buf->b_ml.ml_line_count;
517
518 if (pos->col > 0)
519 {
520 line = ml_get_buf(buf, pos->lnum, FALSE);
521 len = (colnr_T)STRLEN(line);
522 if (pos->col > len)
523 pos->col = len;
524 }
525}
526
527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Make sure curwin->w_cursor.lnum is valid.
529 */
530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100531check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
534 {
535#ifdef FEAT_FOLDING
536 /* If there is a closed fold at the end of the file, put the cursor in
537 * its first line. Otherwise in the last line. */
538 if (!hasFolding(curbuf->b_ml.ml_line_count,
539 &curwin->w_cursor.lnum, NULL))
540#endif
541 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
542 }
543 if (curwin->w_cursor.lnum <= 0)
544 curwin->w_cursor.lnum = 1;
545}
546
547/*
548 * Make sure curwin->w_cursor.col is valid.
549 */
550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100551check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200553 check_cursor_col_win(curwin);
554}
555
556/*
557 * Make sure win->w_cursor.col is valid.
558 */
559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100560check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200561{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 colnr_T len;
563#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200564 colnr_T oldcol = win->w_cursor.col;
565 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#endif
567
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200568 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200570 win->w_cursor.col = 0;
571 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000573 /* Allow cursor past end-of-line when:
574 * - in Insert mode or restarting Insert mode
575 * - in Visual mode and 'selection' isn't "old"
576 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000577 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000579#ifdef FEAT_VIRTUALEDIT
580 || (ve_flags & VE_ONEMORE)
581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200583 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000585 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200586 win->w_cursor.col = len - 1;
Bram Moolenaar87c19962007-04-26 08:54:21 +0000587#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000589 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200590 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000591#endif
592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200594 else if (win->w_cursor.col < 0)
595 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
597#ifdef FEAT_VIRTUALEDIT
598 /* If virtual editing is on, we can leave the cursor on the old position,
599 * only we must set it to virtual. But don't do it when at the end of the
600 * line. */
601 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200602 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000604 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200605 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200606 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200607 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200608
609 /* Make sure that coladd is not more than the char width.
610 * Not for the last character, coladd is then used when the cursor
611 * is actually after the last character. */
612 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200613 {
614 int cs, ce;
615
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200616 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
617 if (win->w_cursor.coladd > ce - cs)
618 win->w_cursor.coladd = ce - cs;
619 }
620 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000621 else
622 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200623 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#endif
626}
627
628/*
629 * make sure curwin->w_cursor in on a valid character
630 */
631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100632check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 check_cursor_lnum();
635 check_cursor_col();
636}
637
638#if defined(FEAT_TEXTOBJ) || defined(PROTO)
639/*
640 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
641 * Allow it when in Visual mode and 'selection' is not "old".
642 */
643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 && gchar_cursor() == NUL)
649 --curwin->w_cursor.col;
650}
651#endif
652
653/*
654 * When curwin->w_leftcol has changed, adjust the cursor position.
655 * Return TRUE if the cursor was moved.
656 */
657 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100658leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 long lastcol;
661 colnr_T s, e;
662 int retval = FALSE;
663
664 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200665 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 validate_virtcol();
667
668 /*
669 * If the cursor is right or left of the screen, move it to last or first
670 * character.
671 */
672 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
673 {
674 retval = TRUE;
675 coladvance((colnr_T)(lastcol - p_siso));
676 }
677 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
678 {
679 retval = TRUE;
680 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
681 }
682
683 /*
684 * If the start of the character under the cursor is not on the screen,
685 * advance the cursor one more char. If this fails (last char of the
686 * line) adjust the scrolling.
687 */
688 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
689 if (e > (colnr_T)lastcol)
690 {
691 retval = TRUE;
692 coladvance(s - 1);
693 }
694 else if (s < curwin->w_leftcol)
695 {
696 retval = TRUE;
697 if (coladvance(e + 1) == FAIL) /* there isn't another character */
698 {
699 curwin->w_leftcol = s; /* adjust w_leftcol instead */
700 changed_cline_bef_curs();
701 }
702 }
703
704 if (retval)
705 curwin->w_set_curswant = TRUE;
706 redraw_later(NOT_VALID);
707 return retval;
708}
709
710/**********************************************************************
711 * Various routines dealing with allocation and deallocation of memory.
712 */
713
714#if defined(MEM_PROFILE) || defined(PROTO)
715
716# define MEM_SIZES 8200
717static long_u mem_allocs[MEM_SIZES];
718static long_u mem_frees[MEM_SIZES];
719static long_u mem_allocated;
720static long_u mem_freed;
721static long_u mem_peak;
722static long_u num_alloc;
723static long_u num_freed;
724
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100725static void mem_pre_alloc_s(size_t *sizep);
726static void mem_pre_alloc_l(long_u *sizep);
727static void mem_post_alloc(void **pp, size_t size);
728static void mem_pre_free(void **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 *sizep += sizeof(size_t);
734}
735
736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100737mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738{
739 *sizep += sizeof(size_t);
740}
741
742 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743mem_post_alloc(
744 void **pp,
745 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 if (*pp == NULL)
748 return;
749 size -= sizeof(size_t);
750 *(long_u *)*pp = size;
751 if (size <= MEM_SIZES-1)
752 mem_allocs[size-1]++;
753 else
754 mem_allocs[MEM_SIZES-1]++;
755 mem_allocated += size;
756 if (mem_allocated - mem_freed > mem_peak)
757 mem_peak = mem_allocated - mem_freed;
758 num_alloc++;
759 *pp = (void *)((char *)*pp + sizeof(size_t));
760}
761
762 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100763mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 long_u size;
766
767 *pp = (void *)((char *)*pp - sizeof(size_t));
768 size = *(size_t *)*pp;
769 if (size <= MEM_SIZES-1)
770 mem_frees[size-1]++;
771 else
772 mem_frees[MEM_SIZES-1]++;
773 mem_freed += size;
774 num_freed++;
775}
776
777/*
778 * called on exit via atexit()
779 */
780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100781vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 int i, j;
784
785 printf("\r\n");
786 j = 0;
787 for (i = 0; i < MEM_SIZES - 1; i++)
788 {
789 if (mem_allocs[i] || mem_frees[i])
790 {
791 if (mem_frees[i] > mem_allocs[i])
792 printf("\r\n%s", _("ERROR: "));
793 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
794 j++;
795 if (j > 3)
796 {
797 j = 0;
798 printf("\r\n");
799 }
800 }
801 }
802
803 i = MEM_SIZES - 1;
804 if (mem_allocs[i])
805 {
806 printf("\r\n");
807 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200808 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
810 }
811
812 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
813 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
814 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
815 num_alloc, num_freed);
816}
817
818#endif /* MEM_PROFILE */
819
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100820#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100821static int alloc_does_fail(long_u size);
Bram Moolenaara260b872016-01-15 20:48:22 +0100822
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100823 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100824alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100825{
826 if (alloc_fail_countdown == 0)
827 {
828 if (--alloc_fail_repeat <= 0)
829 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100830 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100831 return TRUE;
832 }
833 --alloc_fail_countdown;
834 return FALSE;
835}
836#endif
837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838/*
839 * Some memory is reserved for error messages and for being able to
840 * call mf_release_all(), which needs some memory for mf_trans_add().
841 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100842#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200843#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000846 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 * Use lalloc for larger blocks.
848 */
849 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100850alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
852 return (lalloc((long_u)size, TRUE));
853}
854
855/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100856 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100857 */
858 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100859alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100860{
861#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100862 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100863 return NULL;
864#endif
865 return (lalloc((long_u)size, TRUE));
866}
867
868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * Allocate memory and set all bytes to zero.
870 */
871 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100872alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 char_u *p;
875
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200876 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (p != NULL)
878 (void)vim_memset(p, 0, (size_t)size);
879 return p;
880}
881
882/*
883 * alloc() with check for maximum line length
884 */
885 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100886alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200888#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (sizeof(int) == 2 && size > 0x7fff)
890 {
891 /* Don't hide this message */
892 emsg_silent = 0;
893 EMSG(_("E340: Line is becoming too long"));
894 return NULL;
895 }
896#endif
897 return (lalloc((long_u)size, TRUE));
898}
899
900/*
901 * Allocate memory like lalloc() and set all bytes to zero.
902 */
903 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100904lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 char_u *p;
907
908 p = (lalloc(size, message));
909 if (p != NULL)
910 (void)vim_memset(p, 0, (size_t)size);
911 return p;
912}
913
914/*
915 * Low level memory allocation function.
916 * This is used often, KEEP IT FAST!
917 */
918 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100919lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
921 char_u *p; /* pointer to new storage space */
922 static int releasing = FALSE; /* don't do mf_release_all() recursive */
923 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100924#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 static long_u allocated = 0; /* allocated since last avail check */
926#endif
927
928 /* Safety check for allocating zero bytes */
929 if (size == 0)
930 {
931 /* Don't hide this message */
932 emsg_silent = 0;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100933 IEMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 return NULL;
935 }
936
937#ifdef MEM_PROFILE
938 mem_pre_alloc_l(&size);
939#endif
940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 /*
942 * Loop when out of memory: Try to release some memfile blocks and
943 * if some blocks are released call malloc again.
944 */
945 for (;;)
946 {
947 /*
948 * Handle three kind of systems:
949 * 1. No check for available memory: Just return.
950 * 2. Slow check for available memory: call mch_avail_mem() after
951 * allocating KEEP_ROOM amount of memory.
952 * 3. Strict check for available memory: call mch_avail_mem()
953 */
954 if ((p = (char_u *)malloc((size_t)size)) != NULL)
955 {
956#ifndef HAVE_AVAIL_MEM
957 /* 1. No check for available memory: Just return. */
958 goto theend;
959#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 /* 2. Slow check for available memory: call mch_avail_mem() after
961 * allocating (KEEP_ROOM / 2) amount of memory. */
962 allocated += size;
963 if (allocated < KEEP_ROOM / 2)
964 goto theend;
965 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200968 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000970 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 p = NULL;
972 }
973 else
974 goto theend;
975#endif
976 }
977 /*
978 * Remember that mf_release_all() is being called to avoid an endless
979 * loop, because mf_release_all() may call alloc() recursively.
980 */
981 if (releasing)
982 break;
983 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000984
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100985 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000986 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 releasing = FALSE;
989 if (!try_again)
990 break;
991 }
992
993 if (message && p == NULL)
994 do_outofmem_msg(size);
995
996theend:
997#ifdef MEM_PROFILE
998 mem_post_alloc((void **)&p, (size_t)size);
999#endif
1000 return p;
1001}
1002
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001003/*
1004 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001005 */
1006 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001007lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001008{
1009#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001010 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001011 return NULL;
1012#endif
1013 return (lalloc((long_u)size, message));
1014}
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#if defined(MEM_PROFILE) || defined(PROTO)
1017/*
1018 * realloc() with memory profiling.
1019 */
1020 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001021mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 void *p;
1024
1025 mem_pre_free(&ptr);
1026 mem_pre_alloc_s(&size);
1027
1028 p = realloc(ptr, size);
1029
1030 mem_post_alloc(&p, size);
1031
1032 return p;
1033}
1034#endif
1035
1036/*
1037* Avoid repeating the error message many times (they take 1 second each).
1038* Did_outofmem_msg is reset when a character is read.
1039*/
1040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001041do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 if (!did_outofmem_msg)
1044 {
1045 /* Don't hide this message */
1046 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001047
1048 /* Must come first to avoid coming back here when printing the error
1049 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001051
1052 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
1054}
1055
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001056#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001057
1058# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001059static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001060# endif
1061
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001062/*
1063 * Free everything that we allocated.
1064 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001065 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1066 * surprised if Vim crashes...
1067 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001068 */
1069 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001070free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001071{
1072 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001073
1074 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1075 * Don't try freeing everything again. */
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001076 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001077 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001078 entered_free_all_mem = TRUE;
Bram Moolenaara9673212016-06-01 22:21:06 +02001079
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001080# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001081 /* Don't want to trigger autocommands from here on. */
1082 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001083# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001084
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001085 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1086 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001087 if (first_tabpage->tp_next != NULL)
1088 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001089 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001090 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001091
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001092# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001093 /* Free all spell info. */
1094 spell_free_all();
1095# endif
1096
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001097# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001098 /* Clear user commands (before deleting buffers). */
1099 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001100# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001101
1102# ifdef FEAT_MENU
1103 /* Clear menus. */
1104 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001105# ifdef FEAT_MULTI_LANG
1106 do_cmdline_cmd((char_u *)"menutranslate clear");
1107# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001108# endif
1109
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001110 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001111 do_cmdline_cmd((char_u *)"lmapclear");
1112 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001113 do_cmdline_cmd((char_u *)"mapclear");
1114 do_cmdline_cmd((char_u *)"mapclear!");
1115 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001116# if defined(FEAT_EVAL)
1117 do_cmdline_cmd((char_u *)"breakdel *");
1118# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001119# if defined(FEAT_PROFILE)
1120 do_cmdline_cmd((char_u *)"profdel *");
1121# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001122# if defined(FEAT_KEYMAP)
1123 do_cmdline_cmd((char_u *)"set keymap=");
1124#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001125
1126# ifdef FEAT_TITLE
1127 free_titles();
1128# endif
1129# if defined(FEAT_SEARCHPATH)
1130 free_findfile();
1131# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001132
1133 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001134# if defined(FEAT_AUTOCMD)
1135 free_all_autocmds();
1136# endif
1137 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001138 free_all_options();
1139 free_all_marks();
1140 alist_clear(&global_alist);
1141 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001142# if defined(FEAT_CMDL_COMPL)
1143 free_users();
1144# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001145 free_search_patterns();
1146 free_old_sub();
1147 free_last_insert();
1148 free_prev_shellcmd();
1149 free_regexp_stuff();
1150 free_tag_stuff();
1151 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001152# ifdef FEAT_SIGNS
1153 free_signs();
1154# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001155# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001156 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001157# endif
1158# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001159 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001160# endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001161 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001162
1163 /* Free some global vars. */
1164 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001165# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001166 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001167# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001168 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001169# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001170 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001171# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001172 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001173 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001174
1175 /* Clear cmdline history. */
1176 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001177# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001178 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001179# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001180
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001181#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001182 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001183 win_T *win;
1184 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001185
1186 qf_free_all(NULL);
1187 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001188 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001189 qf_free_all(win);
1190 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001191#endif
1192
1193 /* Close all script inputs. */
1194 close_all_scripts();
1195
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001196 /* Destroy all windows. Must come before freeing buffers. */
1197 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001198
Bram Moolenaar2a329742008-04-01 12:53:43 +00001199 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1200 * were freed already. */
1201#ifdef FEAT_AUTOCHDIR
1202 p_acd = FALSE;
1203#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001204 for (buf = firstbuf; buf != NULL; )
1205 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001206 bufref_T bufref;
1207
1208 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001209 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001210 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001211 if (bufref_valid(&bufref))
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001212 buf = nextbuf; /* didn't work, try next one */
1213 else
1214 buf = firstbuf;
1215 }
1216
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001217#ifdef FEAT_ARABIC
1218 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001219#endif
1220
1221 /* Clear registers. */
1222 clear_registers();
1223 ResetRedobuff();
1224 ResetRedobuff();
1225
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001226#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001227 vim_free(serverDelayedStartName);
1228#endif
1229
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001230 /* highlight info */
1231 free_highlight();
1232
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001233 reset_last_sourcing();
1234
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001235 free_tabpage(first_tabpage);
1236 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001237
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001238# ifdef UNIX
1239 /* Machine-specific free. */
1240 mch_free_mem();
1241# endif
1242
1243 /* message history */
1244 for (;;)
1245 if (delete_first_msg() == FAIL)
1246 break;
1247
Bram Moolenaar655da312016-05-28 22:22:34 +02001248# ifdef FEAT_JOB_CHANNEL
1249 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001250# endif
Bram Moolenaar623e2632016-07-30 22:47:56 +02001251#ifdef FEAT_TIMERS
1252 timer_free_all();
1253#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001254# ifdef FEAT_EVAL
1255 /* must be after channel_free_all() with unrefs partials */
1256 eval_clear();
1257# endif
1258# ifdef FEAT_JOB_CHANNEL
1259 /* must be after eval_clear() with unrefs jobs */
1260 job_free_all();
1261# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001262
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001263 free_termoptions();
1264
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001265 /* screenlines (can't display anything now!) */
1266 free_screenlines();
1267
1268#if defined(USE_XSMP)
1269 xsmp_close();
1270#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001271#ifdef FEAT_GUI_GTK
1272 gui_mch_free_all();
1273#endif
1274 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001275
1276 vim_free(IObuff);
1277 vim_free(NameBuff);
1278}
1279#endif
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001282 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 */
1284 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001285vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 char_u *p;
1288 unsigned len;
1289
1290 len = (unsigned)STRLEN(string) + 1;
1291 p = alloc(len);
1292 if (p != NULL)
1293 mch_memmove(p, string, (size_t)len);
1294 return p;
1295}
1296
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001297/*
1298 * Copy up to "len" bytes of "string" into newly allocated memory and
1299 * terminate with a NUL.
1300 * The allocated memory always has size "len + 1", also when "string" is
1301 * shorter.
1302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001304vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
1306 char_u *p;
1307
1308 p = alloc((unsigned)(len + 1));
1309 if (p != NULL)
1310 {
1311 STRNCPY(p, string, len);
1312 p[len] = NUL;
1313 }
1314 return p;
1315}
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317/*
1318 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1319 * by a backslash.
1320 */
1321 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001322vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001324 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325}
1326
1327/*
1328 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1329 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001330 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 */
1332 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001333vim_strsave_escaped_ext(
1334 char_u *string,
1335 char_u *esc_chars,
1336 int cc,
1337 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339 char_u *p;
1340 char_u *p2;
1341 char_u *escaped_string;
1342 unsigned length;
1343#ifdef FEAT_MBYTE
1344 int l;
1345#endif
1346
1347 /*
1348 * First count the number of backslashes required.
1349 * Then allocate the memory and insert them.
1350 */
1351 length = 1; /* count the trailing NUL */
1352 for (p = string; *p; p++)
1353 {
1354#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001355 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 length += l; /* count a multibyte char */
1358 p += l - 1;
1359 continue;
1360 }
1361#endif
1362 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1363 ++length; /* count a backslash */
1364 ++length; /* count an ordinary char */
1365 }
1366 escaped_string = alloc(length);
1367 if (escaped_string != NULL)
1368 {
1369 p2 = escaped_string;
1370 for (p = string; *p; p++)
1371 {
1372#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001373 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375 mch_memmove(p2, p, (size_t)l);
1376 p2 += l;
1377 p += l - 1; /* skip multibyte char */
1378 continue;
1379 }
1380#endif
1381 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001382 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 *p2++ = *p;
1384 }
1385 *p2 = NUL;
1386 }
1387 return escaped_string;
1388}
1389
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001390/*
1391 * Return TRUE when 'shell' has "csh" in the tail.
1392 */
1393 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001394csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001395{
1396 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1397}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001398
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001399/*
1400 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001401 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001402 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001403 * Escape a newline, depending on the 'shell' option.
1404 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1405 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001406 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001407 * Returns the result in allocated memory, NULL if we have run out.
1408 */
1409 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001410vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001411{
1412 unsigned length;
1413 char_u *p;
1414 char_u *d;
1415 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001416 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001417 int csh_like;
1418
1419 /* Only csh and similar shells expand '!' within single quotes. For sh and
1420 * the like we must not put a backslash before it, it will be taken
1421 * literally. If do_special is set the '!' will be escaped twice.
1422 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001423 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001424
1425 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001426 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001427 for (p = string; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001428 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001429# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001430 if (!p_ssl)
1431 {
1432 if (*p == '"')
1433 ++length; /* " -> "" */
1434 }
1435 else
1436# endif
1437 if (*p == '\'')
1438 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001439 if ((*p == '\n' && (csh_like || do_newline))
1440 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001441 {
1442 ++length; /* insert backslash */
1443 if (csh_like && do_special)
1444 ++length; /* insert backslash */
1445 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001446 if (do_special && find_cmdline_var(p, &l) >= 0)
1447 {
1448 ++length; /* insert backslash */
1449 p += l - 1;
1450 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001451 }
1452
1453 /* Allocate memory for the result and fill it. */
1454 escaped_string = alloc(length);
1455 if (escaped_string != NULL)
1456 {
1457 d = escaped_string;
1458
1459 /* add opening quote */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001460# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001461 if (!p_ssl)
1462 *d++ = '"';
1463 else
1464# endif
1465 *d++ = '\'';
1466
1467 for (p = string; *p != NUL; )
1468 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001469# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001470 if (!p_ssl)
1471 {
1472 if (*p == '"')
1473 {
1474 *d++ = '"';
1475 *d++ = '"';
1476 ++p;
1477 continue;
1478 }
1479 }
1480 else
1481# endif
1482 if (*p == '\'')
1483 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001484 *d++ = '\'';
1485 *d++ = '\\';
1486 *d++ = '\'';
1487 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001488 ++p;
1489 continue;
1490 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001491 if ((*p == '\n' && (csh_like || do_newline))
1492 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001493 {
1494 *d++ = '\\';
1495 if (csh_like && do_special)
1496 *d++ = '\\';
1497 *d++ = *p++;
1498 continue;
1499 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001500 if (do_special && find_cmdline_var(p, &l) >= 0)
1501 {
1502 *d++ = '\\'; /* insert backslash */
1503 while (--l >= 0) /* copy the var */
1504 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001505 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001506 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001507
1508 MB_COPY_CHAR(p, d);
1509 }
1510
1511 /* add terminating quote and finish with a NUL */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001512# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001513 if (!p_ssl)
1514 *d++ = '"';
1515 else
1516# endif
1517 *d++ = '\'';
1518 *d = NUL;
1519 }
1520
1521 return escaped_string;
1522}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524/*
1525 * Like vim_strsave(), but make all characters uppercase.
1526 * This uses ASCII lower-to-upper case translation, language independent.
1527 */
1528 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001529vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 char_u *p1;
1532
1533 p1 = vim_strsave(string);
1534 vim_strup(p1);
1535 return p1;
1536}
1537
1538/*
1539 * Like vim_strnsave(), but make all characters uppercase.
1540 * This uses ASCII lower-to-upper case translation, language independent.
1541 */
1542 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001543vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 char_u *p1;
1546
1547 p1 = vim_strnsave(string, len);
1548 vim_strup(p1);
1549 return p1;
1550}
1551
1552/*
1553 * ASCII lower-to-upper case translation, language independent.
1554 */
1555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001556vim_strup(
1557 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 char_u *p2;
1560 int c;
1561
1562 if (p != NULL)
1563 {
1564 p2 = p;
1565 while ((c = *p2) != NUL)
1566#ifdef EBCDIC
1567 *p2++ = isalpha(c) ? toupper(c) : c;
1568#else
1569 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1570#endif
1571 }
1572}
1573
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001574#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001575/*
1576 * Make string "s" all upper-case and return it in allocated memory.
1577 * Handles multi-byte characters as well as possible.
1578 * Returns NULL when out of memory.
1579 */
1580 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001581strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001582{
1583 char_u *p;
1584 char_u *res;
1585
1586 res = p = vim_strsave(orig);
1587
1588 if (res != NULL)
1589 while (*p != NUL)
1590 {
1591# ifdef FEAT_MBYTE
1592 int l;
1593
1594 if (enc_utf8)
1595 {
1596 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001597 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001598 char_u *s;
1599
1600 c = utf_ptr2char(p);
1601 uc = utf_toupper(c);
1602
1603 /* Reallocate string when byte count changes. This is rare,
1604 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001605 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001606 newl = utf_char2len(uc);
1607 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001608 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001609 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001610 if (s == NULL)
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001611 {
1612 vim_free(res);
1613 return NULL;
1614 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001615 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001616 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001617 p = s + (p - res);
1618 vim_free(res);
1619 res = s;
1620 }
1621
1622 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001623 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001624 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001625 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001626 p += l; /* skip multi-byte character */
1627 else
1628# endif
1629 {
1630 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1631 p++;
1632 }
1633 }
1634
1635 return res;
1636}
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001637
1638/*
1639 * Make string "s" all lower-case and return it in allocated memory.
1640 * Handles multi-byte characters as well as possible.
1641 * Returns NULL when out of memory.
1642 */
1643 char_u *
1644strlow_save(char_u *orig)
1645{
1646 char_u *p;
1647 char_u *res;
1648
1649 res = p = vim_strsave(orig);
1650
1651 if (res != NULL)
1652 while (*p != NUL)
1653 {
1654# ifdef FEAT_MBYTE
1655 int l;
1656
1657 if (enc_utf8)
1658 {
1659 int c, lc;
1660 int newl;
1661 char_u *s;
1662
1663 c = utf_ptr2char(p);
1664 lc = utf_tolower(c);
1665
1666 /* Reallocate string when byte count changes. This is rare,
1667 * thus it's OK to do another malloc()/free(). */
1668 l = utf_ptr2len(p);
1669 newl = utf_char2len(lc);
1670 if (newl != l)
1671 {
1672 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
1673 if (s == NULL)
1674 {
1675 vim_free(res);
1676 return NULL;
1677 }
1678 mch_memmove(s, res, p - res);
1679 STRCPY(s + (p - res) + newl, p + l);
1680 p = s + (p - res);
1681 vim_free(res);
1682 res = s;
1683 }
1684
1685 utf_char2bytes(lc, p);
1686 p += newl;
1687 }
1688 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1689 p += l; /* skip multi-byte character */
1690 else
1691# endif
1692 {
1693 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
1694 p++;
1695 }
1696 }
1697
1698 return res;
1699}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001700#endif
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 * delete spaces at the end of a string
1704 */
1705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001706del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 char_u *q;
1709
1710 q = ptr + STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001711 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *q = NUL;
1713}
1714
1715/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001716 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001717 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
1719 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001720vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001722 STRNCPY(to, from, len);
1723 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724}
1725
1726/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001727 * Like strcat(), but make sure the result fits in "tosize" bytes and is
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001728 * always NUL terminated. "from" and "to" may overlap.
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001729 */
1730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001731vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001732{
1733 size_t tolen = STRLEN(to);
1734 size_t fromlen = STRLEN(from);
1735
1736 if (tolen + fromlen + 1 > tosize)
1737 {
1738 mch_memmove(to + tolen, from, tosize - tolen - 1);
1739 to[tosize - 1] = NUL;
1740 }
1741 else
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001742 mch_memmove(to + tolen, from, fromlen + 1);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001743}
1744
1745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 * Isolate one part of a string option where parts are separated with
1747 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001748 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 * "*option" is advanced to the next part.
1750 * The length is returned.
1751 */
1752 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001753copy_option_part(
1754 char_u **option,
1755 char_u *buf,
1756 int maxlen,
1757 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 int len = 0;
1760 char_u *p = *option;
1761
1762 /* skip '.' at start of option part, for 'suffixes' */
1763 if (*p == '.')
1764 buf[len++] = *p++;
1765 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1766 {
1767 /*
1768 * Skip backslash before a separator character and space.
1769 */
1770 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1771 ++p;
1772 if (len < maxlen - 1)
1773 buf[len++] = *p;
1774 ++p;
1775 }
1776 buf[len] = NUL;
1777
1778 if (*p != NUL && *p != ',') /* skip non-standard separator */
1779 ++p;
1780 p = skip_to_option_part(p); /* p points to next file name */
1781
1782 *option = p;
1783 return len;
1784}
1785
1786/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001787 * Replacement for free() that ignores NULL pointers.
1788 * Also skip free() when exiting for sure, this helps when we caught a deadly
1789 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 */
1791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001792vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001794 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796#ifdef MEM_PROFILE
1797 mem_pre_free(&x);
1798#endif
1799 free(x);
1800 }
1801}
1802
1803#ifndef HAVE_MEMSET
1804 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001805vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
1807 char *p = ptr;
1808
1809 while (size-- > 0)
1810 *p++ = c;
1811 return ptr;
1812}
1813#endif
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1816/*
1817 * Compare two strings, ignoring case, using current locale.
1818 * Doesn't work for multi-byte characters.
1819 * return 0 for match, < 0 for smaller, > 0 for bigger
1820 */
1821 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001822vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int i;
1825
1826 for (;;)
1827 {
1828 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1829 if (i != 0)
1830 return i; /* this character different */
1831 if (*s1 == NUL)
1832 break; /* strings match until NUL */
1833 ++s1;
1834 ++s2;
1835 }
1836 return 0; /* strings match */
1837}
1838#endif
1839
1840#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1841/*
1842 * Compare two strings, for length "len", ignoring case, using current locale.
1843 * Doesn't work for multi-byte characters.
1844 * return 0 for match, < 0 for smaller, > 0 for bigger
1845 */
1846 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001847vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 int i;
1850
1851 while (len > 0)
1852 {
1853 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1854 if (i != 0)
1855 return i; /* this character different */
1856 if (*s1 == NUL)
1857 break; /* strings match until NUL */
1858 ++s1;
1859 ++s2;
1860 --len;
1861 }
1862 return 0; /* strings match */
1863}
1864#endif
1865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866/*
1867 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001868 * with characters from 128 to 255 correctly. It also doesn't return a
1869 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
1871 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001872vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
1874 char_u *p;
1875 int b;
1876
1877 p = string;
1878#ifdef FEAT_MBYTE
1879 if (enc_utf8 && c >= 0x80)
1880 {
1881 while (*p != NUL)
1882 {
Bram Moolenaarace95982017-03-29 17:30:27 +02001883 int l = utfc_ptr2len(p);
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001884
1885 /* Avoid matching an illegal byte here. */
1886 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001888 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890 return NULL;
1891 }
1892 if (enc_dbcs != 0 && c > 255)
1893 {
1894 int n2 = c & 0xff;
1895
1896 c = ((unsigned)c >> 8) & 0xff;
1897 while ((b = *p) != NUL)
1898 {
1899 if (b == c && p[1] == n2)
1900 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001901 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 return NULL;
1904 }
1905 if (has_mbyte)
1906 {
1907 while ((b = *p) != NUL)
1908 {
1909 if (b == c)
1910 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001911 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 return NULL;
1914 }
1915#endif
1916 while ((b = *p) != NUL)
1917 {
1918 if (b == c)
1919 return p;
1920 ++p;
1921 }
1922 return NULL;
1923}
1924
1925/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001926 * Version of strchr() that only works for bytes and handles unsigned char
1927 * strings with characters above 128 correctly. It also doesn't return a
1928 * pointer to the NUL at the end of the string.
1929 */
1930 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001931vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001932{
1933 char_u *p = string;
1934
1935 while (*p != NUL)
1936 {
1937 if (*p == c)
1938 return p;
1939 ++p;
1940 }
1941 return NULL;
1942}
1943
1944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001946 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001947 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 */
1949 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001950vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
1952 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001953 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
Bram Moolenaar05159a02005-02-26 23:04:13 +00001955 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001957 if (*p == c)
1958 retval = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001959 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 return retval;
1962}
1963
1964/*
1965 * Vim's version of strpbrk(), in case it's missing.
1966 * Don't generate a prototype for this, causes problems when it's not used.
1967 */
1968#ifndef PROTO
1969# ifndef HAVE_STRPBRK
1970# ifdef vim_strpbrk
1971# undef vim_strpbrk
1972# endif
1973 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001974vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 while (*s)
1977 {
1978 if (vim_strchr(charset, *s) != NULL)
1979 return s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001980 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982 return NULL;
1983}
1984# endif
1985#endif
1986
1987/*
1988 * Vim has its own isspace() function, because on some machines isspace()
1989 * can't handle characters above 128.
1990 */
1991 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001992vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 return ((x >= 9 && x <= 13) || x == ' ');
1995}
1996
1997/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001998 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 */
2000
2001/*
2002 * Clear an allocated growing array.
2003 */
2004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 vim_free(gap->ga_data);
2008 ga_init(gap);
2009}
2010
2011/*
2012 * Clear a growing array that contains a list of strings.
2013 */
2014 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002015ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 int i;
2018
2019 for (i = 0; i < gap->ga_len; ++i)
2020 vim_free(((char_u **)(gap->ga_data))[i]);
2021 ga_clear(gap);
2022}
2023
2024/*
2025 * Initialize a growing array. Don't forget to set ga_itemsize and
2026 * ga_growsize! Or use ga_init2().
2027 */
2028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002029ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030{
2031 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002032 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 gap->ga_len = 0;
2034}
2035
2036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002037ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 ga_init(gap);
2040 gap->ga_itemsize = itemsize;
2041 gap->ga_growsize = growsize;
2042}
2043
2044/*
2045 * Make room in growing array "gap" for at least "n" items.
2046 * Return FAIL for failure, OK otherwise.
2047 */
2048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002049ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002051 size_t old_len;
2052 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 char_u *pp;
2054
Bram Moolenaar86b68352004-12-27 21:59:20 +00002055 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 if (n < gap->ga_growsize)
2058 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002059 new_len = gap->ga_itemsize * (gap->ga_len + n);
2060 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002061 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (pp == NULL)
2063 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002064 old_len = gap->ga_itemsize * gap->ga_maxlen;
2065 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002066 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 gap->ga_data = pp;
2068 }
2069 return OK;
2070}
2071
2072/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002073 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002074 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002075 * Returns NULL when out of memory.
2076 */
2077 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002078ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002079{
2080 int i;
2081 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002082 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002083 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002084 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002085
2086 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002087 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002088
2089 s = alloc(len + 1);
2090 if (s != NULL)
2091 {
2092 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002093 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002094 for (i = 0; i < gap->ga_len; ++i)
2095 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002096 if (p != s)
2097 {
2098 STRCPY(p, sep);
2099 p += sep_len;
2100 }
2101 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2102 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002103 }
2104 }
2105 return s;
2106}
2107
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01002108#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002109/*
2110 * Make a copy of string "p" and add it to "gap".
2111 * When out of memory nothing changes.
2112 */
2113 void
2114ga_add_string(garray_T *gap, char_u *p)
2115{
2116 char_u *cp = vim_strsave(p);
2117
2118 if (cp != NULL)
2119 {
2120 if (ga_grow(gap, 1) == OK)
2121 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2122 else
2123 vim_free(cp);
2124 }
2125}
2126#endif
2127
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002130 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 * Note: Does NOT copy the NUL at the end!
2132 */
2133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
Bram Moolenaar43345542015-11-29 17:35:35 +01002136 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar478af672017-04-10 22:22:42 +02002138 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01002139 return;
2140 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (ga_grow(gap, len) == OK)
2142 {
2143 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2144 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146}
2147
2148/*
2149 * Append one byte to a growarray which contains bytes.
2150 */
2151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 if (ga_grow(gap, 1) == OK)
2155 {
2156 *((char *)gap->ga_data + gap->ga_len) = c;
2157 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159}
2160
Bram Moolenaar597a4222014-06-25 14:39:50 +02002161#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2162 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002163/*
2164 * Append the text in "gap" below the cursor line and clear "gap".
2165 */
2166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002167append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002168{
2169 /* Remove trailing CR. */
2170 if (gap->ga_len > 0
2171 && !curbuf->b_p_bin
2172 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2173 --gap->ga_len;
2174 ga_append(gap, NUL);
2175 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2176 gap->ga_len = 0;
2177}
2178#endif
2179
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180/************************************************************************
2181 * functions that use lookup tables for various things, generally to do with
2182 * special key codes.
2183 */
2184
2185/*
2186 * Some useful tables.
2187 */
2188
2189static struct modmasktable
2190{
2191 short mod_mask; /* Bit-mask for particular key modifier */
2192 short mod_flag; /* Bit(s) for particular key modifier */
2193 char_u name; /* Single letter name of modifier */
2194} mod_mask_table[] =
2195{
2196 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002197 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2199 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2200 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2201 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2202 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
2203#ifdef MACOS
2204 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2205#endif
2206 /* 'A' must be the last one */
2207 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2208 {0, 0, NUL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002209 /* NOTE: when adding an entry, update MAX_KEY_NAME_LEN! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210};
2211
2212/*
2213 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002214 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 */
2216#define MOD_KEYS_ENTRY_SIZE 5
2217
2218static char_u modifier_keys_table[] =
2219{
2220/* mod mask with modifier without modifier */
2221 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2222 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2223 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2224 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2225 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2226 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2227 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2228 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2229 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2230 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2231 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2232 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2233 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2234 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2235 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2236 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2237 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2238 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2239 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2240 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2241 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2242 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2243 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2244 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2245 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2246 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2247 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2248 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2249 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2250 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2251 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2252 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2253 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2254
2255 /* vt100 F1 */
2256 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2257 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2258 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2259 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2260
2261 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2262 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2263 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2264 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2265 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2266 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2267 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2269 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2270 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2271
2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2273 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2282
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2293
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2300 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2301
2302 /* TAB pseudo code*/
2303 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2304
2305 NUL
2306};
2307
2308static struct key_name_entry
2309{
2310 int key; /* Special key code or ascii value */
2311 char_u *name; /* Name of key */
2312} key_names_table[] =
2313{
2314 {' ', (char_u *)"Space"},
2315 {TAB, (char_u *)"Tab"},
2316 {K_TAB, (char_u *)"Tab"},
2317 {NL, (char_u *)"NL"},
2318 {NL, (char_u *)"NewLine"}, /* Alternative name */
2319 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2320 {NL, (char_u *)"LF"}, /* Alternative name */
2321 {CAR, (char_u *)"CR"},
2322 {CAR, (char_u *)"Return"}, /* Alternative name */
2323 {CAR, (char_u *)"Enter"}, /* Alternative name */
2324 {K_BS, (char_u *)"BS"},
2325 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2326 {ESC, (char_u *)"Esc"},
2327 {CSI, (char_u *)"CSI"},
2328 {K_CSI, (char_u *)"xCSI"},
2329 {'|', (char_u *)"Bar"},
2330 {'\\', (char_u *)"Bslash"},
2331 {K_DEL, (char_u *)"Del"},
2332 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2333 {K_KDEL, (char_u *)"kDel"},
2334 {K_UP, (char_u *)"Up"},
2335 {K_DOWN, (char_u *)"Down"},
2336 {K_LEFT, (char_u *)"Left"},
2337 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002338 {K_XUP, (char_u *)"xUp"},
2339 {K_XDOWN, (char_u *)"xDown"},
2340 {K_XLEFT, (char_u *)"xLeft"},
2341 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01002342 {K_PS, (char_u *)"PasteStart"},
2343 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 {K_F1, (char_u *)"F1"},
2346 {K_F2, (char_u *)"F2"},
2347 {K_F3, (char_u *)"F3"},
2348 {K_F4, (char_u *)"F4"},
2349 {K_F5, (char_u *)"F5"},
2350 {K_F6, (char_u *)"F6"},
2351 {K_F7, (char_u *)"F7"},
2352 {K_F8, (char_u *)"F8"},
2353 {K_F9, (char_u *)"F9"},
2354 {K_F10, (char_u *)"F10"},
2355
2356 {K_F11, (char_u *)"F11"},
2357 {K_F12, (char_u *)"F12"},
2358 {K_F13, (char_u *)"F13"},
2359 {K_F14, (char_u *)"F14"},
2360 {K_F15, (char_u *)"F15"},
2361 {K_F16, (char_u *)"F16"},
2362 {K_F17, (char_u *)"F17"},
2363 {K_F18, (char_u *)"F18"},
2364 {K_F19, (char_u *)"F19"},
2365 {K_F20, (char_u *)"F20"},
2366
2367 {K_F21, (char_u *)"F21"},
2368 {K_F22, (char_u *)"F22"},
2369 {K_F23, (char_u *)"F23"},
2370 {K_F24, (char_u *)"F24"},
2371 {K_F25, (char_u *)"F25"},
2372 {K_F26, (char_u *)"F26"},
2373 {K_F27, (char_u *)"F27"},
2374 {K_F28, (char_u *)"F28"},
2375 {K_F29, (char_u *)"F29"},
2376 {K_F30, (char_u *)"F30"},
2377
2378 {K_F31, (char_u *)"F31"},
2379 {K_F32, (char_u *)"F32"},
2380 {K_F33, (char_u *)"F33"},
2381 {K_F34, (char_u *)"F34"},
2382 {K_F35, (char_u *)"F35"},
2383 {K_F36, (char_u *)"F36"},
2384 {K_F37, (char_u *)"F37"},
2385
2386 {K_XF1, (char_u *)"xF1"},
2387 {K_XF2, (char_u *)"xF2"},
2388 {K_XF3, (char_u *)"xF3"},
2389 {K_XF4, (char_u *)"xF4"},
2390
2391 {K_HELP, (char_u *)"Help"},
2392 {K_UNDO, (char_u *)"Undo"},
2393 {K_INS, (char_u *)"Insert"},
2394 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2395 {K_KINS, (char_u *)"kInsert"},
2396 {K_HOME, (char_u *)"Home"},
2397 {K_KHOME, (char_u *)"kHome"},
2398 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002399 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {K_END, (char_u *)"End"},
2401 {K_KEND, (char_u *)"kEnd"},
2402 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002403 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {K_PAGEUP, (char_u *)"PageUp"},
2405 {K_PAGEDOWN, (char_u *)"PageDown"},
2406 {K_KPAGEUP, (char_u *)"kPageUp"},
2407 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2408
2409 {K_KPLUS, (char_u *)"kPlus"},
2410 {K_KMINUS, (char_u *)"kMinus"},
2411 {K_KDIVIDE, (char_u *)"kDivide"},
2412 {K_KMULTIPLY, (char_u *)"kMultiply"},
2413 {K_KENTER, (char_u *)"kEnter"},
2414 {K_KPOINT, (char_u *)"kPoint"},
2415
2416 {K_K0, (char_u *)"k0"},
2417 {K_K1, (char_u *)"k1"},
2418 {K_K2, (char_u *)"k2"},
2419 {K_K3, (char_u *)"k3"},
2420 {K_K4, (char_u *)"k4"},
2421 {K_K5, (char_u *)"k5"},
2422 {K_K6, (char_u *)"k6"},
2423 {K_K7, (char_u *)"k7"},
2424 {K_K8, (char_u *)"k8"},
2425 {K_K9, (char_u *)"k9"},
2426
2427 {'<', (char_u *)"lt"},
2428
2429 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002430#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002432#endif
2433#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002435#endif
2436#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002438#endif
2439#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002441#endif
2442#ifdef FEAT_MOUSE_URXVT
2443 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2444#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002445#ifdef FEAT_MOUSE_SGR
2446 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaara529ce02017-06-22 22:37:57 +02002447 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2450 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2451 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2452 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2453 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2454 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2455 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2456 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2457 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2458 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2459 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002460 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2461 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2462 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2463 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2464 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2465 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {K_X1MOUSE, (char_u *)"X1Mouse"},
2467 {K_X1DRAG, (char_u *)"X1Drag"},
2468 {K_X1RELEASE, (char_u *)"X1Release"},
2469 {K_X2MOUSE, (char_u *)"X2Mouse"},
2470 {K_X2DRAG, (char_u *)"X2Drag"},
2471 {K_X2RELEASE, (char_u *)"X2Release"},
2472 {K_DROP, (char_u *)"Drop"},
2473 {K_ZERO, (char_u *)"Nul"},
2474#ifdef FEAT_EVAL
2475 {K_SNR, (char_u *)"SNR"},
2476#endif
2477 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002478 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {0, NULL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002480 /* NOTE: When adding a long name update MAX_KEY_NAME_LEN. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481};
2482
2483#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2484
2485#ifdef FEAT_MOUSE
2486static struct mousetable
2487{
2488 int pseudo_code; /* Code for pseudo mouse event */
2489 int button; /* Which mouse button is it? */
2490 int is_click; /* Is it a mouse button click event? */
2491 int is_drag; /* Is it a mouse drag event? */
2492} mouse_table[] =
2493{
2494 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2495#ifdef FEAT_GUI
2496 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2497#endif
2498 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2499 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2500#ifdef FEAT_GUI
2501 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2502#endif
2503 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2504 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2505 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2506 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2507 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2508 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2509 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2510 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2511 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2512 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2513 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2514 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2515 /* DRAG without CLICK */
2516 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2517 /* RELEASE without CLICK */
2518 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2519 {0, 0, 0, 0},
2520};
2521#endif /* FEAT_MOUSE */
2522
2523/*
2524 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2525 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2526 */
2527 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002528name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530 int i;
2531
2532 c = TOUPPER_ASC(c);
2533 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2534 if (c == mod_mask_table[i].name)
2535 return mod_mask_table[i].mod_flag;
2536 return 0;
2537}
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539/*
2540 * Check if if there is a special key code for "key" that includes the
2541 * modifiers specified.
2542 */
2543 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002544simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 int i;
2547 int key0;
2548 int key1;
2549
2550 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2551 {
2552 /* TAB is a special case */
2553 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2554 {
2555 *modifiers &= ~MOD_MASK_SHIFT;
2556 return K_S_TAB;
2557 }
2558 key0 = KEY2TERMCAP0(key);
2559 key1 = KEY2TERMCAP1(key);
2560 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2561 if (key0 == modifier_keys_table[i + 3]
2562 && key1 == modifier_keys_table[i + 4]
2563 && (*modifiers & modifier_keys_table[i]))
2564 {
2565 *modifiers &= ~modifier_keys_table[i];
2566 return TERMCAP2KEY(modifier_keys_table[i + 1],
2567 modifier_keys_table[i + 2]);
2568 }
2569 }
2570 return key;
2571}
2572
2573/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002574 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002575 */
2576 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002577handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002578{
2579 switch (key)
2580 {
2581 case K_XUP: return K_UP;
2582 case K_XDOWN: return K_DOWN;
2583 case K_XLEFT: return K_LEFT;
2584 case K_XRIGHT: return K_RIGHT;
2585 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002586 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002587 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002588 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002589 case K_XF1: return K_F1;
2590 case K_XF2: return K_F2;
2591 case K_XF3: return K_F3;
2592 case K_XF4: return K_F4;
2593 case K_S_XF1: return K_S_F1;
2594 case K_S_XF2: return K_S_F2;
2595 case K_S_XF3: return K_S_F3;
2596 case K_S_XF4: return K_S_F4;
2597 }
2598 return key;
2599}
2600
2601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 * Return a string which contains the name of the given key when the given
2603 * modifiers are down.
2604 */
2605 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002606get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 static char_u string[MAX_KEY_NAME_LEN + 1];
2609
2610 int i, idx;
2611 int table_idx;
2612 char_u *s;
2613
2614 string[0] = '<';
2615 idx = 1;
2616
2617 /* Key that stands for a normal character. */
2618 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2619 c = KEY2TERMCAP1(c);
2620
2621 /*
2622 * Translate shifted special keys into unshifted keys and set modifier.
2623 * Same for CTRL and ALT modifiers.
2624 */
2625 if (IS_SPECIAL(c))
2626 {
2627 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2628 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2629 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2630 {
2631 modifiers |= modifier_keys_table[i];
2632 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2633 modifier_keys_table[i + 4]);
2634 break;
2635 }
2636 }
2637
2638 /* try to find the key in the special key table */
2639 table_idx = find_special_key_in_table(c);
2640
2641 /*
2642 * When not a known special key, and not a printable character, try to
2643 * extract modifiers.
2644 */
2645 if (c > 0
2646#ifdef FEAT_MBYTE
2647 && (*mb_char2len)(c) == 1
2648#endif
2649 )
2650 {
2651 if (table_idx < 0
2652 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2653 && (c & 0x80))
2654 {
2655 c &= 0x7f;
2656 modifiers |= MOD_MASK_ALT;
2657 /* try again, to find the un-alted key in the special key table */
2658 table_idx = find_special_key_in_table(c);
2659 }
2660 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2661 {
2662#ifdef EBCDIC
2663 c = CtrlChar(c);
2664#else
2665 c += '@';
2666#endif
2667 modifiers |= MOD_MASK_CTRL;
2668 }
2669 }
2670
2671 /* translate the modifier into a string */
2672 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2673 if ((modifiers & mod_mask_table[i].mod_mask)
2674 == mod_mask_table[i].mod_flag)
2675 {
2676 string[idx++] = mod_mask_table[i].name;
2677 string[idx++] = (char_u)'-';
2678 }
2679
2680 if (table_idx < 0) /* unknown special key, may output t_xx */
2681 {
2682 if (IS_SPECIAL(c))
2683 {
2684 string[idx++] = 't';
2685 string[idx++] = '_';
2686 string[idx++] = KEY2TERMCAP0(c);
2687 string[idx++] = KEY2TERMCAP1(c);
2688 }
2689 /* Not a special key, only modifiers, output directly */
2690 else
2691 {
2692#ifdef FEAT_MBYTE
2693 if (has_mbyte && (*mb_char2len)(c) > 1)
2694 idx += (*mb_char2bytes)(c, string + idx);
2695 else
2696#endif
2697 if (vim_isprintc(c))
2698 string[idx++] = c;
2699 else
2700 {
2701 s = transchar(c);
2702 while (*s)
2703 string[idx++] = *s++;
2704 }
2705 }
2706 }
2707 else /* use name of special key */
2708 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002709 size_t len = STRLEN(key_names_table[table_idx].name);
2710
2711 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2712 {
2713 STRCPY(string + idx, key_names_table[table_idx].name);
2714 idx += (int)len;
2715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717 string[idx++] = '>';
2718 string[idx] = NUL;
2719 return string;
2720}
2721
2722/*
2723 * Try translating a <> name at (*srcp)[] to dst[].
2724 * Return the number of characters added to dst[], zero for no match.
2725 * If there is a match, srcp is advanced to after the <> name.
2726 * dst[] must be big enough to hold the result (up to six characters)!
2727 */
2728 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002729trans_special(
2730 char_u **srcp,
2731 char_u *dst,
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002732 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2733 int in_string) /* TRUE when inside a double quoted string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
2735 int modifiers = 0;
2736 int key;
2737 int dlen = 0;
2738
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002739 key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (key == 0)
2741 return 0;
2742
2743 /* Put the appropriate modifier in a string */
2744 if (modifiers != 0)
2745 {
2746 dst[dlen++] = K_SPECIAL;
2747 dst[dlen++] = KS_MODIFIER;
2748 dst[dlen++] = modifiers;
2749 }
2750
2751 if (IS_SPECIAL(key))
2752 {
2753 dst[dlen++] = K_SPECIAL;
2754 dst[dlen++] = KEY2TERMCAP0(key);
2755 dst[dlen++] = KEY2TERMCAP1(key);
2756 }
2757#ifdef FEAT_MBYTE
2758 else if (has_mbyte && !keycode)
2759 dlen += (*mb_char2bytes)(key, dst + dlen);
2760#endif
2761 else if (keycode)
2762 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2763 else
2764 dst[dlen++] = key;
2765
2766 return dlen;
2767}
2768
2769/*
2770 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2771 * srcp is advanced to after the <> name.
2772 * returns 0 if there is no match.
2773 */
2774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002775find_special_key(
2776 char_u **srcp,
2777 int *modp,
2778 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002779 int keep_x_key, /* don't translate xHome to Home key */
2780 int in_string) /* TRUE in string, double quote is escaped */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 char_u *last_dash;
2783 char_u *end_of_name;
2784 char_u *src;
2785 char_u *bp;
2786 int modifiers;
2787 int bit;
2788 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002789 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002790 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
2792 src = *srcp;
2793 if (src[0] != '<')
2794 return 0;
2795
2796 /* Find end of modifier list */
2797 last_dash = src;
2798 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2799 {
2800 if (*bp == '-')
2801 {
2802 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002803 if (bp[1] != NUL)
2804 {
2805#ifdef FEAT_MBYTE
2806 if (has_mbyte)
2807 l = mb_ptr2len(bp + 1);
2808 else
2809#endif
2810 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002811 /* Anything accepted, like <C-?>.
2812 * <C-"> or <M-"> are not special in strings as " is
2813 * the string delimiter. With a backslash it works: <M-\"> */
2814 if (!(in_string && bp[1] == '"') && bp[2] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002815 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002816 else if (in_string && bp[1] == '\\' && bp[2] == '"'
2817 && bp[3] == '>')
2818 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
2821 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2822 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002823 else if (STRNICMP(bp, "char-", 5) == 0)
2824 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002825 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002826 bp += l + 5;
2827 break;
2828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830
2831 if (*bp == '>') /* found matching '>' */
2832 {
2833 end_of_name = bp + 1;
2834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /* Which modifiers are given? */
2836 modifiers = 0x0;
2837 for (bp = src + 1; bp < last_dash; bp++)
2838 {
2839 if (*bp != '-')
2840 {
2841 bit = name_to_mod_mask(*bp);
2842 if (bit == 0x0)
2843 break; /* Illegal modifier name */
2844 modifiers |= bit;
2845 }
2846 }
2847
2848 /*
2849 * Legal modifier name.
2850 */
2851 if (bp >= last_dash)
2852 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002853 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2854 && VIM_ISDIGIT(last_dash[6]))
2855 {
2856 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002857 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002858 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002861 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002862 int off = 1;
2863
2864 /* Modifier with single letter, or special key name. */
2865 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2866 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002867#ifdef FEAT_MBYTE
2868 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002869 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002870 else
2871#endif
2872 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002873 if (modifiers != 0 && last_dash[l + off] == '>')
2874 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002875 else
2876 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002877 key = get_special_key_code(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002878 if (!keep_x_key)
2879 key = handle_x_keys(key);
2880 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 /*
2884 * get_special_key_code() may return NUL for invalid
2885 * special key name.
2886 */
2887 if (key != NUL)
2888 {
2889 /*
2890 * Only use a modifier when there is no special key code that
2891 * includes the modifier.
2892 */
2893 key = simplify_key(key, &modifiers);
2894
2895 if (!keycode)
2896 {
2897 /* don't want keycode, use single byte code */
2898 if (key == K_BS)
2899 key = BS;
2900 else if (key == K_DEL || key == K_KDEL)
2901 key = DEL;
2902 }
2903
2904 /*
2905 * Normal Key with modifier: Try to make a single byte code.
2906 */
2907 if (!IS_SPECIAL(key))
2908 key = extract_modifiers(key, &modifiers);
2909
2910 *modp = modifiers;
2911 *srcp = end_of_name;
2912 return key;
2913 }
2914 }
2915 }
2916 return 0;
2917}
2918
2919/*
2920 * Try to include modifiers in the key.
2921 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2922 */
2923 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002924extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 int modifiers = *modp;
2927
2928#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002929 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (!(modifiers & MOD_MASK_CMD))
2931#endif
2932 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2933 {
2934 key = TOUPPER_ASC(key);
2935 modifiers &= ~MOD_MASK_SHIFT;
2936 }
2937 if ((modifiers & MOD_MASK_CTRL)
2938#ifdef EBCDIC
2939 /* * TODO: EBCDIC Better use:
2940 * && (Ctrl_chr(key) || key == '?')
2941 * ??? */
2942 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2943 != NULL
2944#else
2945 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2946#endif
2947 )
2948 {
2949 key = Ctrl_chr(key);
2950 modifiers &= ~MOD_MASK_CTRL;
2951 /* <C-@> is <Nul> */
2952 if (key == 0)
2953 key = K_ZERO;
2954 }
2955#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002956 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (!(modifiers & MOD_MASK_CMD))
2958#endif
2959 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2960#ifdef FEAT_MBYTE
2961 && !enc_dbcs /* avoid creating a lead byte */
2962#endif
2963 )
2964 {
2965 key |= 0x80;
2966 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2967 }
2968
2969 *modp = modifiers;
2970 return key;
2971}
2972
2973/*
2974 * Try to find key "c" in the special key table.
2975 * Return the index when found, -1 when not found.
2976 */
2977 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002978find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 int i;
2981
2982 for (i = 0; key_names_table[i].name != NULL; i++)
2983 if (c == key_names_table[i].key)
2984 break;
2985 if (key_names_table[i].name == NULL)
2986 i = -1;
2987 return i;
2988}
2989
2990/*
2991 * Find the special key with the given name (the given string does not have to
2992 * end with NUL, the name is assumed to end before the first non-idchar).
2993 * If the name starts with "t_" the next two characters are interpreted as a
2994 * termcap name.
2995 * Return the key code, or 0 if not found.
2996 */
2997 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002998get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 char_u *table_name;
3001 char_u string[3];
3002 int i, j;
3003
3004 /*
3005 * If it's <t_xx> we get the code for xx from the termcap
3006 */
3007 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3008 {
3009 string[0] = name[2];
3010 string[1] = name[3];
3011 string[2] = NUL;
3012 if (add_termcap_entry(string, FALSE) == OK)
3013 return TERMCAP2KEY(name[2], name[3]);
3014 }
3015 else
3016 for (i = 0; key_names_table[i].name != NULL; i++)
3017 {
3018 table_name = key_names_table[i].name;
3019 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3020 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3021 break;
3022 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3023 return key_names_table[i].key;
3024 }
3025 return 0;
3026}
3027
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003028#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003030get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003032 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 return NULL;
3034 return key_names_table[i].name;
3035}
3036#endif
3037
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003038#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039/*
3040 * Look up the given mouse code to return the relevant information in the other
3041 * arguments. Return which button is down or was released.
3042 */
3043 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003044get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 int i;
3047
3048 for (i = 0; mouse_table[i].pseudo_code; i++)
3049 if (code == mouse_table[i].pseudo_code)
3050 {
3051 *is_click = mouse_table[i].is_click;
3052 *is_drag = mouse_table[i].is_drag;
3053 return mouse_table[i].button;
3054 }
3055 return 0; /* Shouldn't get here */
3056}
3057
3058/*
3059 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3060 * the given information about which mouse button is down, and whether the
3061 * mouse was clicked, dragged or released.
3062 */
3063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003064get_pseudo_mouse_code(
3065 int button, /* eg MOUSE_LEFT */
3066 int is_click,
3067 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 int i;
3070
3071 for (i = 0; mouse_table[i].pseudo_code; i++)
3072 if (button == mouse_table[i].button
3073 && is_click == mouse_table[i].is_click
3074 && is_drag == mouse_table[i].is_drag)
3075 {
3076#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003077 /* Trick: a non mappable left click and release has mouse_col -1
3078 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3079 * gui_mouse_moved() */
3080 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003082 if (mouse_col < 0)
3083 mouse_col = 0;
3084 else
3085 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3087 return (int)KE_LEFTMOUSE_NM;
3088 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3089 return (int)KE_LEFTRELEASE_NM;
3090 }
3091#endif
3092 return mouse_table[i].pseudo_code;
3093 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003094 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
3096#endif /* FEAT_MOUSE */
3097
3098/*
3099 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3100 */
3101 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003102get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 int c = *buf->b_p_ff;
3105
3106 if (buf->b_p_bin || c == 'u')
3107 return EOL_UNIX;
3108 if (c == 'm')
3109 return EOL_MAC;
3110 return EOL_DOS;
3111}
3112
3113/*
3114 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3115 * argument.
3116 */
3117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003118get_fileformat_force(
3119 buf_T *buf,
3120 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 int c;
3123
3124 if (eap != NULL && eap->force_ff != 0)
3125 c = eap->cmd[eap->force_ff];
3126 else
3127 {
3128 if ((eap != NULL && eap->force_bin != 0)
3129 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3130 return EOL_UNIX;
3131 c = *buf->b_p_ff;
3132 }
3133 if (c == 'u')
3134 return EOL_UNIX;
3135 if (c == 'm')
3136 return EOL_MAC;
3137 return EOL_DOS;
3138}
3139
3140/*
3141 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3142 * Sets both 'textmode' and 'fileformat'.
3143 * Note: Does _not_ set global value of 'textmode'!
3144 */
3145 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003146set_fileformat(
3147 int t,
3148 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150 char *p = NULL;
3151
3152 switch (t)
3153 {
3154 case EOL_DOS:
3155 p = FF_DOS;
3156 curbuf->b_p_tx = TRUE;
3157 break;
3158 case EOL_UNIX:
3159 p = FF_UNIX;
3160 curbuf->b_p_tx = FALSE;
3161 break;
3162 case EOL_MAC:
3163 p = FF_MAC;
3164 curbuf->b_p_tx = FALSE;
3165 break;
3166 }
3167 if (p != NULL)
3168 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003169 OPT_FREE | opt_flags, 0);
3170
Bram Moolenaarf740b292006-02-16 22:11:02 +00003171 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003173 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174#ifdef FEAT_TITLE
3175 need_maketitle = TRUE; /* set window title later */
3176#endif
3177}
3178
3179/*
3180 * Return the default fileformat from 'fileformats'.
3181 */
3182 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003183default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 switch (*p_ffs)
3186 {
3187 case 'm': return EOL_MAC;
3188 case 'd': return EOL_DOS;
3189 }
3190 return EOL_UNIX;
3191}
3192
3193/*
3194 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3195 */
3196 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003197call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 char_u *ncmd;
3200 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003201#ifdef FEAT_PROFILE
3202 proftime_T wait_time;
3203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 if (p_verbose > 3)
3206 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003207 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003208 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 cmd == NULL ? p_sh : cmd);
3210 out_char('\n');
3211 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003212 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
3214
Bram Moolenaar05159a02005-02-26 23:04:13 +00003215#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003216 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003217 prof_child_enter(&wait_time);
3218#endif
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (*p_sh == NUL)
3221 {
3222 EMSG(_(e_shellempty));
3223 retval = -1;
3224 }
3225 else
3226 {
3227#ifdef FEAT_GUI_MSWIN
3228 /* Don't hide the pointer while executing a shell command. */
3229 gui_mch_mousehide(FALSE);
3230#endif
3231#ifdef FEAT_GUI
3232 ++hold_gui_events;
3233#endif
3234 /* The external command may update a tags file, clear cached tags. */
3235 tag_freematch();
3236
3237 if (cmd == NULL || *p_sxq == NUL)
3238 retval = mch_call_shell(cmd, opt);
3239 else
3240 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003241 char_u *ecmd = cmd;
3242
3243 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3244 {
3245 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3246 if (ecmd == NULL)
3247 ecmd = cmd;
3248 }
3249 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (ncmd != NULL)
3251 {
3252 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003253 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003254 /* When 'shellxquote' is ( append ).
3255 * When 'shellxquote' is "( append )". */
3256 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3257 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3258 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 retval = mch_call_shell(ncmd, opt);
3260 vim_free(ncmd);
3261 }
3262 else
3263 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003264 if (ecmd != cmd)
3265 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267#ifdef FEAT_GUI
3268 --hold_gui_events;
3269#endif
3270 /*
3271 * Check the window size, in case it changed while executing the
3272 * external command.
3273 */
3274 shell_resized_check();
3275 }
3276
3277#ifdef FEAT_EVAL
3278 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003279# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003280 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003281 prof_child_exit(&wait_time);
3282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#endif
3284
3285 return retval;
3286}
3287
3288/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003289 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3290 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 */
3292 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003293get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 if (State & NORMAL)
3296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003298 {
3299 if (VIsual_select)
3300 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003302 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003303 else if (finish_op)
3304 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306 return State;
3307}
3308
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003309#if defined(FEAT_MBYTE) || defined(PROTO)
3310/*
3311 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003312 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003313 * "b" must point to the start of the file name
3314 */
3315 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003316after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003317{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003318 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003319 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3320}
3321#endif
3322
3323/*
3324 * Return TRUE if file names "f1" and "f2" are in the same directory.
3325 * "f1" may be a short name, "f2" must be a full path.
3326 */
3327 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003328same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003329{
3330 char_u ffname[MAXPATHL];
3331 char_u *t1;
3332 char_u *t2;
3333
3334 /* safety check */
3335 if (f1 == NULL || f2 == NULL)
3336 return FALSE;
3337
3338 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3339 t1 = gettail_sep(ffname);
3340 t2 = gettail_sep(f2);
3341 return (t1 - ffname == t2 - f2
3342 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3343}
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar4033c552017-09-16 20:54:51 +02003346 || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3348 || defined(PROTO)
3349/*
3350 * Change to a file's directory.
3351 * Caller must call shorten_fnames()!
3352 * Return OK or FAIL.
3353 */
3354 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003355vim_chdirfile(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003357 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003359 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003360 *gettail_sep(dir) = NUL;
3361 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362}
3363#endif
3364
3365#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3366/*
3367 * Check if "name" ends in a slash and is not a directory.
3368 * Used for systems where stat() ignores a trailing slash on a file name.
3369 * The Vim code assumes a trailing slash is only ignored for a directory.
3370 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003371 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01003372illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 if (name[0] == NUL)
3375 return FALSE; /* no file name is not illegal */
3376 if (name[strlen(name) - 1] != '/')
3377 return FALSE; /* no trailing slash */
3378 if (mch_isdir((char_u *)name))
3379 return FALSE; /* trailing slash for a directory */
3380 return TRUE;
3381}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003382
3383/*
3384 * Special implementation of mch_stat() for Solaris.
3385 */
3386 int
3387vim_stat(const char *name, stat_T *stp)
3388{
3389 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
3390 * the name ends in "/" and it's not a directory. */
Bram Moolenaard8492792017-03-16 12:22:38 +01003391 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#endif
3394
3395#if defined(CURSOR_SHAPE) || defined(PROTO)
3396
3397/*
3398 * Handling of cursor and mouse pointer shapes in various modes.
3399 */
3400
3401cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3402{
3403 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3404 * defaults when Vim starts.
3405 * Adjust the SHAPE_IDX_ defines when making changes! */
3406 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3407 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3408 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3409 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3410 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3411 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3412 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3413 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3414 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3415 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3416 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3417 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3418 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3419 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3420 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3421 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3422 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3423};
3424
3425#ifdef FEAT_MOUSESHAPE
3426/*
3427 * Table with names for mouse shapes. Keep in sync with all the tables for
3428 * mch_set_mouse_shape()!.
3429 */
3430static char * mshape_names[] =
3431{
3432 "arrow", /* default, must be the first one */
3433 "blank", /* hidden */
3434 "beam",
3435 "updown",
3436 "udsizing",
3437 "leftright",
3438 "lrsizing",
3439 "busy",
3440 "no",
3441 "crosshair",
3442 "hand1",
3443 "hand2",
3444 "pencil",
3445 "question",
3446 "rightup-arrow",
3447 "up-arrow",
3448 NULL
3449};
3450#endif
3451
3452/*
3453 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3454 * ("what" is SHAPE_MOUSE).
3455 * Returns error message for an illegal option, NULL otherwise.
3456 */
3457 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003458parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 char_u *modep;
3461 char_u *colonp;
3462 char_u *commap;
3463 char_u *slashp;
3464 char_u *p, *endp;
3465 int idx = 0; /* init for GCC */
3466 int all_idx;
3467 int len;
3468 int i;
3469 long n;
3470 int found_ve = FALSE; /* found "ve" flag */
3471 int round;
3472
3473 /*
3474 * First round: check for errors; second round: do it for real.
3475 */
3476 for (round = 1; round <= 2; ++round)
3477 {
3478 /*
3479 * Repeat for all comma separated parts.
3480 */
3481#ifdef FEAT_MOUSESHAPE
3482 if (what == SHAPE_MOUSE)
3483 modep = p_mouseshape;
3484 else
3485#endif
3486 modep = p_guicursor;
3487 while (*modep != NUL)
3488 {
3489 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01003490 commap = vim_strchr(modep, ',');
3491
3492 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return (char_u *)N_("E545: Missing colon");
3494 if (colonp == modep)
3495 return (char_u *)N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 /*
3498 * Repeat for all mode's before the colon.
3499 * For the 'a' mode, we loop to handle all the modes.
3500 */
3501 all_idx = -1;
3502 while (modep < colonp || all_idx >= 0)
3503 {
3504 if (all_idx < 0)
3505 {
3506 /* Find the mode. */
3507 if (modep[1] == '-' || modep[1] == ':')
3508 len = 1;
3509 else
3510 len = 2;
3511 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3512 all_idx = SHAPE_IDX_COUNT - 1;
3513 else
3514 {
3515 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3516 if (STRNICMP(modep, shape_table[idx].name, len)
3517 == 0)
3518 break;
3519 if (idx == SHAPE_IDX_COUNT
3520 || (shape_table[idx].used_for & what) == 0)
3521 return (char_u *)N_("E546: Illegal mode");
3522 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3523 found_ve = TRUE;
3524 }
3525 modep += len + 1;
3526 }
3527
3528 if (all_idx >= 0)
3529 idx = all_idx--;
3530 else if (round == 2)
3531 {
3532#ifdef FEAT_MOUSESHAPE
3533 if (what == SHAPE_MOUSE)
3534 {
3535 /* Set the default, for the missing parts */
3536 shape_table[idx].mshape = 0;
3537 }
3538 else
3539#endif
3540 {
3541 /* Set the defaults, for the missing parts */
3542 shape_table[idx].shape = SHAPE_BLOCK;
3543 shape_table[idx].blinkwait = 700L;
3544 shape_table[idx].blinkon = 400L;
3545 shape_table[idx].blinkoff = 250L;
3546 }
3547 }
3548
3549 /* Parse the part after the colon */
3550 for (p = colonp + 1; *p && *p != ','; )
3551 {
3552#ifdef FEAT_MOUSESHAPE
3553 if (what == SHAPE_MOUSE)
3554 {
3555 for (i = 0; ; ++i)
3556 {
3557 if (mshape_names[i] == NULL)
3558 {
3559 if (!VIM_ISDIGIT(*p))
3560 return (char_u *)N_("E547: Illegal mouseshape");
3561 if (round == 2)
3562 shape_table[idx].mshape =
3563 getdigits(&p) + MSHAPE_NUMBERED;
3564 else
3565 (void)getdigits(&p);
3566 break;
3567 }
3568 len = (int)STRLEN(mshape_names[i]);
3569 if (STRNICMP(p, mshape_names[i], len) == 0)
3570 {
3571 if (round == 2)
3572 shape_table[idx].mshape = i;
3573 p += len;
3574 break;
3575 }
3576 }
3577 }
3578 else /* if (what == SHAPE_MOUSE) */
3579#endif
3580 {
3581 /*
3582 * First handle the ones with a number argument.
3583 */
3584 i = *p;
3585 len = 0;
3586 if (STRNICMP(p, "ver", 3) == 0)
3587 len = 3;
3588 else if (STRNICMP(p, "hor", 3) == 0)
3589 len = 3;
3590 else if (STRNICMP(p, "blinkwait", 9) == 0)
3591 len = 9;
3592 else if (STRNICMP(p, "blinkon", 7) == 0)
3593 len = 7;
3594 else if (STRNICMP(p, "blinkoff", 8) == 0)
3595 len = 8;
3596 if (len != 0)
3597 {
3598 p += len;
3599 if (!VIM_ISDIGIT(*p))
3600 return (char_u *)N_("E548: digit expected");
3601 n = getdigits(&p);
3602 if (len == 3) /* "ver" or "hor" */
3603 {
3604 if (n == 0)
3605 return (char_u *)N_("E549: Illegal percentage");
3606 if (round == 2)
3607 {
3608 if (TOLOWER_ASC(i) == 'v')
3609 shape_table[idx].shape = SHAPE_VER;
3610 else
3611 shape_table[idx].shape = SHAPE_HOR;
3612 shape_table[idx].percentage = n;
3613 }
3614 }
3615 else if (round == 2)
3616 {
3617 if (len == 9)
3618 shape_table[idx].blinkwait = n;
3619 else if (len == 7)
3620 shape_table[idx].blinkon = n;
3621 else
3622 shape_table[idx].blinkoff = n;
3623 }
3624 }
3625 else if (STRNICMP(p, "block", 5) == 0)
3626 {
3627 if (round == 2)
3628 shape_table[idx].shape = SHAPE_BLOCK;
3629 p += 5;
3630 }
3631 else /* must be a highlight group name then */
3632 {
3633 endp = vim_strchr(p, '-');
3634 if (commap == NULL) /* last part */
3635 {
3636 if (endp == NULL)
3637 endp = p + STRLEN(p); /* find end of part */
3638 }
3639 else if (endp > commap || endp == NULL)
3640 endp = commap;
3641 slashp = vim_strchr(p, '/');
3642 if (slashp != NULL && slashp < endp)
3643 {
3644 /* "group/langmap_group" */
3645 i = syn_check_group(p, (int)(slashp - p));
3646 p = slashp + 1;
3647 }
3648 if (round == 2)
3649 {
3650 shape_table[idx].id = syn_check_group(p,
3651 (int)(endp - p));
3652 shape_table[idx].id_lm = shape_table[idx].id;
3653 if (slashp != NULL && slashp < endp)
3654 shape_table[idx].id = i;
3655 }
3656 p = endp;
3657 }
3658 } /* if (what != SHAPE_MOUSE) */
3659
3660 if (*p == '-')
3661 ++p;
3662 }
3663 }
3664 modep = p;
3665 if (*modep == ',')
3666 ++modep;
3667 }
3668 }
3669
3670 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3671 if (!found_ve)
3672 {
3673#ifdef FEAT_MOUSESHAPE
3674 if (what == SHAPE_MOUSE)
3675 {
3676 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3677 }
3678 else
3679#endif
3680 {
3681 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3682 shape_table[SHAPE_IDX_VE].percentage =
3683 shape_table[SHAPE_IDX_V].percentage;
3684 shape_table[SHAPE_IDX_VE].blinkwait =
3685 shape_table[SHAPE_IDX_V].blinkwait;
3686 shape_table[SHAPE_IDX_VE].blinkon =
3687 shape_table[SHAPE_IDX_V].blinkon;
3688 shape_table[SHAPE_IDX_VE].blinkoff =
3689 shape_table[SHAPE_IDX_V].blinkoff;
3690 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3691 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3692 }
3693 }
3694
3695 return NULL;
3696}
3697
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003698# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3699 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * Return the index into shape_table[] for the current mode.
3702 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3703 */
3704 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003705get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707#ifdef FEAT_MOUSESHAPE
3708 if (mouse && (State == HITRETURN || State == ASKMORE))
3709 {
3710# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003711 int x, y;
3712 gui_mch_getmouse(&x, &y);
3713 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return SHAPE_IDX_MOREL;
3715# endif
3716 return SHAPE_IDX_MORE;
3717 }
3718 if (mouse && drag_status_line)
3719 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (mouse && drag_sep_line)
3721 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#endif
3723 if (!mouse && State == SHOWMATCH)
3724 return SHAPE_IDX_SM;
3725#ifdef FEAT_VREPLACE
3726 if (State & VREPLACE_FLAG)
3727 return SHAPE_IDX_R;
3728#endif
3729 if (State & REPLACE_FLAG)
3730 return SHAPE_IDX_R;
3731 if (State & INSERT)
3732 return SHAPE_IDX_I;
3733 if (State & CMDLINE)
3734 {
3735 if (cmdline_at_end())
3736 return SHAPE_IDX_C;
3737 if (cmdline_overstrike())
3738 return SHAPE_IDX_CR;
3739 return SHAPE_IDX_CI;
3740 }
3741 if (finish_op)
3742 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (VIsual_active)
3744 {
3745 if (*p_sel == 'e')
3746 return SHAPE_IDX_VE;
3747 else
3748 return SHAPE_IDX_V;
3749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 return SHAPE_IDX_N;
3751}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
3754# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3755static int old_mouse_shape = 0;
3756
3757/*
3758 * Set the mouse shape:
3759 * If "shape" is -1, use shape depending on the current mode,
3760 * depending on the current state.
3761 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3762 * when the mouse moves off the status or command line).
3763 */
3764 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003765update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
3767 int new_mouse_shape;
3768
3769 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003770 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return;
3772
3773 /* Postpone the updating when more is to come. Speeds up executing of
3774 * mappings. */
3775 if (shape_idx == -1 && char_avail())
3776 {
3777 postponed_mouseshape = TRUE;
3778 return;
3779 }
3780
Bram Moolenaar14716812006-05-04 21:54:08 +00003781 /* When ignoring the mouse don't change shape on the statusline. */
3782 if (*p_mouse == NUL
3783 && (shape_idx == SHAPE_IDX_CLINE
3784 || shape_idx == SHAPE_IDX_STATUS
3785 || shape_idx == SHAPE_IDX_VSEP))
3786 shape_idx = -2;
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (shape_idx == -2
3789 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3790 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3791 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3792 return;
3793 if (shape_idx < 0)
3794 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3795 else
3796 new_mouse_shape = shape_table[shape_idx].mshape;
3797 if (new_mouse_shape != old_mouse_shape)
3798 {
3799 mch_set_mouse_shape(new_mouse_shape);
3800 old_mouse_shape = new_mouse_shape;
3801 }
3802 postponed_mouseshape = FALSE;
3803}
3804# endif
3805
3806#endif /* CURSOR_SHAPE */
3807
3808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809/* TODO: make some #ifdef for this */
3810/*--------[ file searching ]-------------------------------------------------*/
3811/*
3812 * File searching functions for 'path', 'tags' and 'cdpath' options.
3813 * External visible functions:
3814 * vim_findfile_init() creates/initialises the search context
3815 * vim_findfile_free_visited() free list of visited files/dirs of search
3816 * context
3817 * vim_findfile() find a file in the search context
3818 * vim_findfile_cleanup() cleanup/free search context created by
3819 * vim_findfile_init()
3820 *
3821 * All static functions and variables start with 'ff_'
3822 *
3823 * In general it works like this:
3824 * First you create yourself a search context by calling vim_findfile_init().
3825 * It is possible to give a search context from a previous call to
3826 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3827 * until you are satisfied with the result or it returns NULL. On every call it
3828 * returns the next file which matches the conditions given to
3829 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3830 *
3831 * It is possible to call vim_findfile_init() again to reinitialise your search
3832 * with some new parameters. Don't forget to pass your old search context to
3833 * it, so it can reuse it and especially reuse the list of already visited
3834 * directories. If you want to delete the list of already visited directories
3835 * simply call vim_findfile_free_visited().
3836 *
3837 * When you are done call vim_findfile_cleanup() to free the search context.
3838 *
3839 * The function vim_findfile_init() has a long comment, which describes the
3840 * needed parameters.
3841 *
3842 *
3843 *
3844 * ATTENTION:
3845 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003846 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 * thread-safe!!!!!
3848 *
3849 * To minimize parameter passing (or because I'm to lazy), only the
3850 * external visible functions get a search context as a parameter. This is
3851 * then assigned to a static global, which is used throughout the local
3852 * functions.
3853 */
3854
3855/*
3856 * type for the directory search stack
3857 */
3858typedef struct ff_stack
3859{
3860 struct ff_stack *ffs_prev;
3861
3862 /* the fix part (no wildcards) and the part containing the wildcards
3863 * of the search path
3864 */
3865 char_u *ffs_fix_path;
3866#ifdef FEAT_PATH_EXTRA
3867 char_u *ffs_wc_path;
3868#endif
3869
3870 /* files/dirs found in the above directory, matched by the first wildcard
3871 * of wc_part
3872 */
3873 char_u **ffs_filearray;
3874 int ffs_filearray_size;
3875 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3876
3877 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003878 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 int ffs_stage;
3882
3883 /* How deep are we in the directory tree?
3884 * Counts backward from value of level parameter to vim_findfile_init
3885 */
3886 int ffs_level;
3887
3888 /* Did we already expand '**' to an empty string? */
3889 int ffs_star_star_empty;
3890} ff_stack_T;
3891
3892/*
3893 * type for already visited directories or files.
3894 */
3895typedef struct ff_visited
3896{
3897 struct ff_visited *ffv_next;
3898
3899#ifdef FEAT_PATH_EXTRA
3900 /* Visited directories are different if the wildcard string are
3901 * different. So we have to save it.
3902 */
3903 char_u *ffv_wc_path;
3904#endif
3905 /* for unix use inode etc for comparison (needed because of links), else
3906 * use filename.
3907 */
3908#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003909 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3910 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 ino_t ffv_ino; /* inode number */
3912#endif
3913 /* The memory for this struct is allocated according to the length of
3914 * ffv_fname.
3915 */
3916 char_u ffv_fname[1]; /* actually longer */
3917} ff_visited_T;
3918
3919/*
3920 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003921 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3923 * So we have to do 3 searches:
3924 * 1) search from the current files directory downward for the file "tags"
3925 * 2) search from the current files directory downward for the file "TAGS"
3926 * 3) search from Vims current directory downwards for the file "tags"
3927 * As you can see, the first and the third search are for the same file, so for
3928 * the third search we can use the visited list of the first search. For the
3929 * second search we must start from a empty visited list.
3930 * The struct ff_visited_list_hdr is used to manage a linked list of already
3931 * visited lists.
3932 */
3933typedef struct ff_visited_list_hdr
3934{
3935 struct ff_visited_list_hdr *ffvl_next;
3936
3937 /* the filename the attached visited list is for */
3938 char_u *ffvl_filename;
3939
3940 ff_visited_T *ffvl_visited_list;
3941
3942} ff_visited_list_hdr_T;
3943
3944
3945/*
3946 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003947 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 */
3949#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951/*
3952 * The search context:
3953 * ffsc_stack_ptr: the stack for the dirs to search
3954 * ffsc_visited_list: the currently active visited list
3955 * ffsc_dir_visited_list: the currently active visited list for search dirs
3956 * ffsc_visited_lists_list: the list of all visited lists
3957 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3958 * ffsc_file_to_search: the file to search for
3959 * ffsc_start_dir: the starting directory, if search path was relative
3960 * ffsc_fix_path: the fix part of the given path (without wildcards)
3961 * Needed for upward search.
3962 * ffsc_wc_path: the part of the given path containing wildcards
3963 * ffsc_level: how many levels of dirs to search downwards
3964 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003965 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003966 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
3968typedef struct ff_search_ctx_T
3969{
3970 ff_stack_T *ffsc_stack_ptr;
3971 ff_visited_list_hdr_T *ffsc_visited_list;
3972 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3973 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3974 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3975 char_u *ffsc_file_to_search;
3976 char_u *ffsc_start_dir;
3977 char_u *ffsc_fix_path;
3978#ifdef FEAT_PATH_EXTRA
3979 char_u *ffsc_wc_path;
3980 int ffsc_level;
3981 char_u **ffsc_stopdirs_v;
3982#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003983 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003984 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003985} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987/* locally needed functions */
3988#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003989static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003991static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003993static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
3994static void ff_free_visited_list(ff_visited_T *vl);
3995static 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 +00003996#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003997static int ff_wc_equal(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#endif
3999
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004000static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
4001static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
4002static void ff_clear(ff_search_ctx_T *search_ctx);
4003static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004005static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004007static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#endif
4009#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004010static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#endif
4012
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004013static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#if 0
4016/*
4017 * if someone likes findfirst/findnext, here are the functions
4018 * NOT TESTED!!
4019 */
4020
4021static void *ff_fn_search_context = NULL;
4022
4023 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004024vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 ff_fn_search_context =
4027 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4028 ff_fn_search_context, rel_fname);
4029 if (NULL == ff_fn_search_context)
4030 return NULL;
4031 else
4032 return vim_findnext()
4033}
4034
4035 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004036vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
4038 char_u *ret = vim_findfile(ff_fn_search_context);
4039
4040 if (NULL == ret)
4041 {
4042 vim_findfile_cleanup(ff_fn_search_context);
4043 ff_fn_search_context = NULL;
4044 }
4045 return ret;
4046}
4047#endif
4048
4049/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004050 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004052 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 *
4054 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4055 * with the search context.
4056 *
4057 * Find the file 'filename' in the directory 'path'.
4058 * The parameter 'path' may contain wildcards. If so only search 'level'
4059 * directories deep. The parameter 'level' is the absolute maximum and is
4060 * not related to restricts given to the '**' wildcard. If 'level' is 100
4061 * and you use '**200' vim_findfile() will stop after 100 levels.
4062 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004063 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4064 * escape special characters.
4065 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4067 * restarted on the next higher directory level. This is repeated until the
4068 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4069 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4070 *
4071 * If the 'path' is relative, the starting dir for the search is either VIM's
4072 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004073 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 * the first wildcard.
4075 *
4076 * Upward search is only done on the starting dir.
4077 *
4078 * If 'free_visited' is TRUE the list of already visited files/directories is
4079 * cleared. Set this to FALSE if you just want to search from another
4080 * directory, but want to be sure that no directory from a previous search is
4081 * searched again. This is useful if you search for a file at different places.
4082 * The list of visited files/dirs can also be cleared with the function
4083 * vim_findfile_free_visited().
4084 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004085 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4086 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 *
4088 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004089 * passed in the parameter "search_ctx_arg". This context is reused and
4090 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004092 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4093 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004095 * If you don't have a search context from a previous call "search_ctx_arg"
4096 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 *
4098 * This function silently ignores a few errors, vim_findfile() will have
4099 * limited functionality then.
4100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004102vim_findfile_init(
4103 char_u *path,
4104 char_u *filename,
4105 char_u *stopdirs UNUSED,
4106 int level,
4107 int free_visited,
4108 int find_what,
4109 void *search_ctx_arg,
4110 int tagfile, /* expanding names of tags files */
4111 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004114 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004116 ff_stack_T *sptr;
4117 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 /* If a search context is given by the caller, reuse it, else allocate a
4120 * new one.
4121 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004122 if (search_ctx_arg != NULL)
4123 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 else
4125 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004126 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4127 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004129 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004131 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004132 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004135 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
4137 /* clear visited list if wanted */
4138 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004139 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 else
4141 {
4142 /* Reuse old visited lists. Get the visited list for the given
4143 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004144 * one. */
4145 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4146 &search_ctx->ffsc_visited_lists_list);
4147 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004149 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4150 &search_ctx->ffsc_dir_visited_lists_list);
4151 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 goto error_return;
4153 }
4154
4155 if (ff_expand_buffer == NULL)
4156 {
4157 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4158 if (ff_expand_buffer == NULL)
4159 goto error_return;
4160 }
4161
4162 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004163 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 if (path[0] == '.'
4165 && (vim_ispathsep(path[1]) || path[1] == NUL)
4166 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4167 && rel_fname != NULL)
4168 {
4169 int len = (int)(gettail(rel_fname) - rel_fname);
4170
4171 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4172 {
4173 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004174 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004175 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004178 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4179 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 goto error_return;
4181 if (*++path != NUL)
4182 ++path;
4183 }
4184 else if (*path == NUL || !vim_isAbsName(path))
4185 {
4186#ifdef BACKSLASH_IN_FILENAME
4187 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4188 if (*path != NUL && path[1] == ':')
4189 {
4190 char_u drive[3];
4191
4192 drive[0] = path[0];
4193 drive[1] = ':';
4194 drive[2] = NUL;
4195 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4196 goto error_return;
4197 path += 2;
4198 }
4199 else
4200#endif
4201 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4202 goto error_return;
4203
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004204 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4205 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 goto error_return;
4207
4208#ifdef BACKSLASH_IN_FILENAME
4209 /* A path that starts with "/dir" is relative to the drive, not to the
4210 * directory (but not for "//machine/dir"). Only use the drive name. */
4211 if ((*path == '/' || *path == '\\')
4212 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004213 && search_ctx->ffsc_start_dir[1] == ':')
4214 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#endif
4216 }
4217
4218#ifdef FEAT_PATH_EXTRA
4219 /*
4220 * If stopdirs are given, split them into an array of pointers.
4221 * If this fails (mem allocation), there is no upward search at all or a
4222 * stop directory is not recognized -> continue silently.
4223 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004224 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 * is handled as unlimited upward search. See function
4226 * ff_path_in_stoplist() for details.
4227 */
4228 if (stopdirs != NULL)
4229 {
4230 char_u *walker = stopdirs;
4231 int dircount;
4232
4233 while (*walker == ';')
4234 walker++;
4235
4236 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004237 search_ctx->ffsc_stopdirs_v =
4238 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004240 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
4242 do
4243 {
4244 char_u *helper;
4245 void *ptr;
4246
4247 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004248 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 (dircount + 1) * sizeof(char_u *));
4250 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004251 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 else
4253 /* ignore, keep what we have and continue */
4254 break;
4255 walker = vim_strchr(walker, ';');
4256 if (walker)
4257 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004258 search_ctx->ffsc_stopdirs_v[dircount-1] =
4259 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 walker++;
4261 }
4262 else
4263 /* this might be "", which means ascent till top
4264 * of directory tree.
4265 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004266 search_ctx->ffsc_stopdirs_v[dircount-1] =
4267 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
4269 dircount++;
4270
4271 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004272 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275#endif
4276
4277#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004278 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 /* split into:
4281 * -fix path
4282 * -wildcard_stuff (might be NULL)
4283 */
4284 wc_part = vim_strchr(path, '*');
4285 if (wc_part != NULL)
4286 {
4287 int llevel;
4288 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004289 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004292 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294 /*
4295 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004296 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4298 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4299 * For EBCDIC you get different character values.
4300 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004301 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 * string.
4303 */
4304 len = 0;
4305 while (*wc_part != NUL)
4306 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004307 if (len + 5 >= MAXPATHL)
4308 {
4309 EMSG(_(e_pathtoolong));
4310 break;
4311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (STRNCMP(wc_part, "**", 2) == 0)
4313 {
4314 ff_expand_buffer[len++] = *wc_part++;
4315 ff_expand_buffer[len++] = *wc_part++;
4316
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004317 llevel = strtol((char *)wc_part, &errpt, 10);
4318 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004320 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 /* restrict is 0 -> remove already added '**' */
4322 len -= 2;
4323 else
4324 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004325 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004326 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
4328 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4329 goto error_return;
4330 }
4331 }
4332 else
4333 ff_expand_buffer[len++] = *wc_part++;
4334 }
4335 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004336 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004338 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 goto error_return;
4340 }
4341 else
4342#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004343 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004345 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
4347 /* store the fix part as startdir.
4348 * This is needed if the parameter path is fully qualified.
4349 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004350 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004351 if (search_ctx->ffsc_start_dir == NULL)
4352 goto error_return;
4353 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355
4356 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004357 if (STRLEN(search_ctx->ffsc_start_dir)
4358 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4359 {
4360 EMSG(_(e_pathtoolong));
4361 goto error_return;
4362 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004363 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004365 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004366 int eb_len = (int)STRLEN(ff_expand_buffer);
4367 char_u *buf = alloc(eb_len
4368 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004369
4370 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004371 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004372 if (mch_isdir(buf))
4373 {
4374 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4375 add_pathsep(ff_expand_buffer);
4376 }
4377#ifdef FEAT_PATH_EXTRA
4378 else
4379 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004380 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004381 char_u *wc_path = NULL;
4382 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004383 int len = 0;
4384
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004385 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004386 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004387 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004388 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4389 add_pathsep(ff_expand_buffer);
4390 }
4391 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004392 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004393
4394 if (search_ctx->ffsc_wc_path != NULL)
4395 {
4396 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004397 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004398 + STRLEN(search_ctx->ffsc_fix_path + len)
4399 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004400 if (temp == NULL || wc_path == NULL)
4401 {
4402 vim_free(buf);
4403 vim_free(temp);
4404 vim_free(wc_path);
4405 goto error_return;
4406 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004407
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004408 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4409 STRCAT(temp, search_ctx->ffsc_wc_path);
4410 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004411 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004412 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004413 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004414 }
4415#endif
4416 vim_free(buf);
4417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 sptr = ff_create_stack_element(ff_expand_buffer,
4420#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004421 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422#endif
4423 level, 0);
4424
4425 if (sptr == NULL)
4426 goto error_return;
4427
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004428 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004430 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4431 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 goto error_return;
4433
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004434 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436error_return:
4437 /*
4438 * We clear the search context now!
4439 * Even when the caller gave us a (perhaps valid) context we free it here,
4440 * as we might have already destroyed it.
4441 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004442 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 return NULL;
4444}
4445
4446#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4447/*
4448 * Get the stopdir string. Check that ';' is not escaped.
4449 */
4450 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004451vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
4453 char_u *r_ptr = buf;
4454
4455 while (*r_ptr != NUL && *r_ptr != ';')
4456 {
4457 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4458 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004459 /* Overwrite the escape char,
4460 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004461 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 r_ptr++;
4463 }
4464 r_ptr++;
4465 }
4466 if (*r_ptr == ';')
4467 {
4468 *r_ptr = 0;
4469 r_ptr++;
4470 }
4471 else if (*r_ptr == NUL)
4472 r_ptr = NULL;
4473 return r_ptr;
4474}
4475#endif
4476
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004477/*
4478 * Clean up the given search context. Can handle a NULL pointer.
4479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004481vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004483 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return;
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004487 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489}
4490
4491/*
4492 * Find a file in a search context.
4493 * The search context was created with vim_findfile_init() above.
4494 * Return a pointer to an allocated file name or NULL if nothing found.
4495 * To get all matching files call this function until you get NULL.
4496 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004497 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 *
4499 * The search algorithm is depth first. To change this replace the
4500 * stack with a list (don't forget to leave partly searched directories on the
4501 * top of the list).
4502 */
4503 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004504vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505{
4506 char_u *file_path;
4507#ifdef FEAT_PATH_EXTRA
4508 char_u *rest_of_wildcards;
4509 char_u *path_end = NULL;
4510#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004511 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4513 int len;
4514#endif
4515 int i;
4516 char_u *p;
4517#ifdef FEAT_SEARCHPATH
4518 char_u *suf;
4519#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004520 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004522 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 return NULL;
4524
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004525 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527 /*
4528 * filepath is used as buffer for various actions and as the storage to
4529 * return a found filename.
4530 */
4531 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4532 return NULL;
4533
4534#ifdef FEAT_PATH_EXTRA
4535 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004536 if (search_ctx->ffsc_start_dir != NULL)
4537 path_end = &search_ctx->ffsc_start_dir[
4538 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539#endif
4540
4541#ifdef FEAT_PATH_EXTRA
4542 /* upward search loop */
4543 for (;;)
4544 {
4545#endif
4546 /* downward search loop */
4547 for (;;)
4548 {
4549 /* check if user user wants to stop the search*/
4550 ui_breakcheck();
4551 if (got_int)
4552 break;
4553
4554 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004555 stackp = ff_pop(search_ctx);
4556 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 break;
4558
4559 /*
4560 * TODO: decide if we leave this test in
4561 *
4562 * GOOD: don't search a directory(-tree) twice.
4563 * BAD: - check linked list for every new directory entered.
4564 * - check for double files also done below
4565 *
4566 * Here we check if we already searched this directory.
4567 * We already searched a directory if:
4568 * 1) The directory is the same.
4569 * 2) We would use the same wildcard string.
4570 *
4571 * Good if you have links on same directory via several ways
4572 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4573 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4574 *
4575 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004576 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004578 if (stackp->ffs_filearray == NULL
4579 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004581 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004583 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#endif
4585 ) == FAIL)
4586 {
4587#ifdef FF_VERBOSE
4588 if (p_verbose >= 5)
4589 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004590 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004592 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 /* don't overwrite this either */
4594 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004595 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004598 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 continue;
4600 }
4601#ifdef FF_VERBOSE
4602 else if (p_verbose >= 5)
4603 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004604 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004605 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004606 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /* don't overwrite this either */
4608 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004609 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611#endif
4612
4613 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004614 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004616 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 continue;
4618 }
4619
4620 file_path[0] = NUL;
4621
4622 /*
4623 * If no filearray till now expand wildcards
4624 * The function expand_wildcards() can handle an array of paths
4625 * and all possible expands are returned in one array. We use this
4626 * to handle the expansion of '**' into an empty string.
4627 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004628 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
4630 char_u *dirptrs[2];
4631
4632 /* we use filepath to build the path expand_wildcards() should
4633 * expand.
4634 */
4635 dirptrs[0] = file_path;
4636 dirptrs[1] = NULL;
4637
4638 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004639 if (!vim_isAbsName(stackp->ffs_fix_path)
4640 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004642 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4643 {
4644 STRCPY(file_path, search_ctx->ffsc_start_dir);
4645 add_pathsep(file_path);
4646 }
4647 else
4648 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650
4651 /* append the fix part of the search path */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004652 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4653 {
4654 STRCAT(file_path, stackp->ffs_fix_path);
4655 add_pathsep(file_path);
4656 }
4657 else
4658 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
4660#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004661 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (*rest_of_wildcards != NUL)
4663 {
4664 len = (int)STRLEN(file_path);
4665 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4666 {
4667 /* pointer to the restrict byte
4668 * The restrict byte is not a character!
4669 */
4670 p = rest_of_wildcards + 2;
4671
4672 if (*p > 0)
4673 {
4674 (*p)--;
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004675 if (len + 1 < MAXPATHL)
4676 file_path[len++] = '*';
4677 else
4678 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680
4681 if (*p == 0)
4682 {
4683 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004684 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 else
4687 rest_of_wildcards += 3;
4688
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004689 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004692 stackp->ffs_star_star_empty = 1;
4693 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 }
4696
4697 /*
4698 * Here we copy until the next path separator or the end of
4699 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004700 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 * pushing every directory returned from expand_wildcards()
4702 * on the stack again for further search.
4703 */
4704 while (*rest_of_wildcards
4705 && !vim_ispathsep(*rest_of_wildcards))
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004706 if (len + 1 < MAXPATHL)
4707 file_path[len++] = *rest_of_wildcards++;
4708 else
4709 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
4711 file_path[len] = NUL;
4712 if (vim_ispathsep(*rest_of_wildcards))
4713 rest_of_wildcards++;
4714 }
4715#endif
4716
4717 /*
4718 * Expand wildcards like "*" and "$VAR".
4719 * If the path is a URL don't try this.
4720 */
4721 if (path_with_url(dirptrs[0]))
4722 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004723 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004725 if (stackp->ffs_filearray != NULL
4726 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004728 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004730 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004733 /* Add EW_NOTWILD because the expanded path may contain
4734 * wildcard characters that are to be taken literally.
4735 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004737 &stackp->ffs_filearray_size,
4738 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004739 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004741 stackp->ffs_filearray_cur = 0;
4742 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744#ifdef FEAT_PATH_EXTRA
4745 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004746 rest_of_wildcards = &stackp->ffs_wc_path[
4747 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748#endif
4749
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004750 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 /* this is the first time we work on this directory */
4753#ifdef FEAT_PATH_EXTRA
4754 if (*rest_of_wildcards == NUL)
4755#endif
4756 {
4757 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004758 * We don't have further wildcards to expand, so we have to
4759 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004761 for (i = stackp->ffs_filearray_cur;
4762 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004764 if (!path_with_url(stackp->ffs_filearray[i])
4765 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 continue; /* not a directory */
4767
Bram Moolenaar21160b92009-11-11 15:56:10 +00004768 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 * below */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004770 if (STRLEN(stackp->ffs_filearray[i]) + 1
4771 + STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4772 {
4773 STRCPY(file_path, stackp->ffs_filearray[i]);
4774 add_pathsep(file_path);
4775 STRCAT(file_path, search_ctx->ffsc_file_to_search);
4776 }
4777 else
4778 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 /*
4781 * Try without extra suffix and then with suffixes
4782 * from 'suffixesadd'.
4783 */
4784#ifdef FEAT_SEARCHPATH
4785 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004786 if (search_ctx->ffsc_tagfile)
4787 suf = (char_u *)"";
4788 else
4789 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 for (;;)
4791#endif
4792 {
4793 /* if file exists and we didn't already find it */
4794 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004795 || (mch_getperm(file_path) >= 0
4796 && (search_ctx->ffsc_find_what
4797 == FINDFILE_BOTH
4798 || ((search_ctx->ffsc_find_what
4799 == FINDFILE_DIR)
4800 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801#ifndef FF_VERBOSE
4802 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004803 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 file_path
4805#ifdef FEAT_PATH_EXTRA
4806 , (char_u *)""
4807#endif
4808 ) == OK)
4809#endif
4810 )
4811 {
4812#ifdef FF_VERBOSE
4813 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004814 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 file_path
4816#ifdef FEAT_PATH_EXTRA
4817 , (char_u *)""
4818#endif
4819 ) == FAIL)
4820 {
4821 if (p_verbose >= 5)
4822 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004823 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004824 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 file_path);
4826 /* don't overwrite this either */
4827 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004828 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 continue;
4831 }
4832#endif
4833
4834 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004835 stackp->ffs_filearray_cur = i + 1;
4836 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004838 if (!path_with_url(file_path))
4839 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4841 == OK)
4842 {
4843 p = shorten_fname(file_path,
4844 ff_expand_buffer);
4845 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004846 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848#ifdef FF_VERBOSE
4849 if (p_verbose >= 5)
4850 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004851 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004852 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 /* don't overwrite this either */
4854 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004855 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857#endif
4858 return file_path;
4859 }
4860
4861#ifdef FEAT_SEARCHPATH
4862 /* Not found or found already, try next suffix. */
4863 if (*suf == NUL)
4864 break;
4865 copy_option_part(&suf, file_path + len,
4866 MAXPATHL - len, ",");
4867#endif
4868 }
4869 }
4870 }
4871#ifdef FEAT_PATH_EXTRA
4872 else
4873 {
4874 /*
4875 * still wildcards left, push the directories for further
4876 * search
4877 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004878 for (i = stackp->ffs_filearray_cur;
4879 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004881 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 continue; /* not a directory */
4883
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004884 ff_push(search_ctx,
4885 ff_create_stack_element(
4886 stackp->ffs_filearray[i],
4887 rest_of_wildcards,
4888 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 }
4891#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004892 stackp->ffs_filearray_cur = 0;
4893 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895
4896#ifdef FEAT_PATH_EXTRA
4897 /*
4898 * if wildcards contains '**' we have to descent till we reach the
4899 * leaves of the directory tree.
4900 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004901 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004903 for (i = stackp->ffs_filearray_cur;
4904 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004906 if (fnamecmp(stackp->ffs_filearray[i],
4907 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004909 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004911 ff_push(search_ctx,
4912 ff_create_stack_element(stackp->ffs_filearray[i],
4913 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 }
4916#endif
4917
4918 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004919 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 }
4922
4923#ifdef FEAT_PATH_EXTRA
4924 /* If we reached this, we didn't find anything downwards.
4925 * Let's check if we should do an upward search.
4926 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004927 if (search_ctx->ffsc_start_dir
4928 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
4930 ff_stack_T *sptr;
4931
4932 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004933 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4934 (int)(path_end - search_ctx->ffsc_start_dir),
4935 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937
4938 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004939 while (path_end > search_ctx->ffsc_start_dir
4940 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004942 while (path_end > search_ctx->ffsc_start_dir
4943 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 path_end--;
4945 *path_end = 0;
4946 path_end--;
4947
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004948 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 break;
4950
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004951 if (STRLEN(search_ctx->ffsc_start_dir) + 1
4952 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
4953 {
4954 STRCPY(file_path, search_ctx->ffsc_start_dir);
4955 add_pathsep(file_path);
4956 STRCAT(file_path, search_ctx->ffsc_fix_path);
4957 }
4958 else
4959 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
4961 /* create a new stack entry */
4962 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004963 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (sptr == NULL)
4965 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004966 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else
4969 break;
4970 }
4971#endif
4972
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004973fail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 vim_free(file_path);
4975 return NULL;
4976}
4977
4978/*
4979 * Free the list of lists of visited files and directories
4980 * Can handle it if the passed search_context is NULL;
4981 */
4982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004983vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004985 ff_search_ctx_T *search_ctx;
4986
4987 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 return;
4989
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004990 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
4991 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
4992 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993}
4994
4995 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004996vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
4998 ff_visited_list_hdr_T *vp;
4999
5000 while (*list_headp != NULL)
5001 {
5002 vp = (*list_headp)->ffvl_next;
5003 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5004
5005 vim_free((*list_headp)->ffvl_filename);
5006 vim_free(*list_headp);
5007 *list_headp = vp;
5008 }
5009 *list_headp = NULL;
5010}
5011
5012 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005013ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 ff_visited_T *vp;
5016
5017 while (vl != NULL)
5018 {
5019 vp = vl->ffv_next;
5020#ifdef FEAT_PATH_EXTRA
5021 vim_free(vl->ffv_wc_path);
5022#endif
5023 vim_free(vl);
5024 vl = vp;
5025 }
5026 vl = NULL;
5027}
5028
5029/*
5030 * Returns the already visited list for the given filename. If none is found it
5031 * allocates a new one.
5032 */
5033 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01005034ff_get_visited_list(
5035 char_u *filename,
5036 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038 ff_visited_list_hdr_T *retptr = NULL;
5039
5040 /* check if a visited list for the given filename exists */
5041 if (*list_headp != NULL)
5042 {
5043 retptr = *list_headp;
5044 while (retptr != NULL)
5045 {
5046 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5047 {
5048#ifdef FF_VERBOSE
5049 if (p_verbose >= 5)
5050 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005051 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005052 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 filename);
5054 /* don't overwrite this either */
5055 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005056 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058#endif
5059 return retptr;
5060 }
5061 retptr = retptr->ffvl_next;
5062 }
5063 }
5064
5065#ifdef FF_VERBOSE
5066 if (p_verbose >= 5)
5067 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005068 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005069 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 /* don't overwrite this either */
5071 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005072 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074#endif
5075
5076 /*
5077 * if we reach this we didn't find a list and we have to allocate new list
5078 */
5079 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5080 if (retptr == NULL)
5081 return NULL;
5082
5083 retptr->ffvl_visited_list = NULL;
5084 retptr->ffvl_filename = vim_strsave(filename);
5085 if (retptr->ffvl_filename == NULL)
5086 {
5087 vim_free(retptr);
5088 return NULL;
5089 }
5090 retptr->ffvl_next = *list_headp;
5091 *list_headp = retptr;
5092
5093 return retptr;
5094}
5095
5096#ifdef FEAT_PATH_EXTRA
5097/*
5098 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5099 * They are equal if:
5100 * - both paths are NULL
5101 * - they have the same length
5102 * - char by char comparison is OK
5103 * - the only differences are in the counters behind a '**', so
5104 * '**\20' is equal to '**\24'
5105 */
5106 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005107ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005109 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005110 int c1 = NUL;
5111 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005112 int prev1 = NUL;
5113 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 if (s1 == s2)
5116 return TRUE;
5117
5118 if (s1 == NULL || s2 == NULL)
5119 return FALSE;
5120
Bram Moolenaard43f0952015-08-27 22:30:47 +02005121 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005123 c1 = PTR2CHAR(s1 + i);
5124 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005125
5126 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5127 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005128 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005129 prev2 = prev1;
5130 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005131
5132 i += MB_PTR2LEN(s1 + i);
5133 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005135 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136}
5137#endif
5138
5139/*
5140 * maintains the list of already visited files and dirs
5141 * returns FAIL if the given file/dir is already in the list
5142 * returns OK if it is newly added
5143 *
5144 * TODO: What to do on memory allocation problems?
5145 * -> return TRUE - Better the file is found several times instead of
5146 * never.
5147 */
5148 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005149ff_check_visited(
5150 ff_visited_T **visited_list,
5151 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005153 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005155 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
5157 ff_visited_T *vp;
5158#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02005159 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 int url = FALSE;
5161#endif
5162
5163 /* For an URL we only compare the name, otherwise we compare the
5164 * device/inode (unix) or the full path name (not Unix). */
5165 if (path_with_url(fname))
5166 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005167 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#ifdef UNIX
5169 url = TRUE;
5170#endif
5171 }
5172 else
5173 {
5174 ff_expand_buffer[0] = NUL;
5175#ifdef UNIX
5176 if (mch_stat((char *)fname, &st) < 0)
5177#else
5178 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5179#endif
5180 return FAIL;
5181 }
5182
5183 /* check against list of already visited files */
5184 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5185 {
5186 if (
5187#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005188 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5189 && vp->ffv_ino == st.st_ino)
5190 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191#endif
5192 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5193 )
5194 {
5195#ifdef FEAT_PATH_EXTRA
5196 /* are the wildcard parts equal */
5197 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5198#endif
5199 /* already visited */
5200 return FAIL;
5201 }
5202 }
5203
5204 /*
5205 * New file/dir. Add it to the list of visited files/dirs.
5206 */
5207 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5208 + STRLEN(ff_expand_buffer)));
5209
5210 if (vp != NULL)
5211 {
5212#ifdef UNIX
5213 if (!url)
5214 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005215 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 vp->ffv_ino = st.st_ino;
5217 vp->ffv_dev = st.st_dev;
5218 vp->ffv_fname[0] = NUL;
5219 }
5220 else
5221 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005222 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#endif
5224 STRCPY(vp->ffv_fname, ff_expand_buffer);
5225#ifdef UNIX
5226 }
5227#endif
5228#ifdef FEAT_PATH_EXTRA
5229 if (wc_path != NULL)
5230 vp->ffv_wc_path = vim_strsave(wc_path);
5231 else
5232 vp->ffv_wc_path = NULL;
5233#endif
5234
5235 vp->ffv_next = *visited_list;
5236 *visited_list = vp;
5237 }
5238
5239 return OK;
5240}
5241
5242/*
5243 * create stack element from given path pieces
5244 */
5245 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005246ff_create_stack_element(
5247 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005249 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251 int level,
5252 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 ff_stack_T *new;
5255
5256 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5257 if (new == NULL)
5258 return NULL;
5259
5260 new->ffs_prev = NULL;
5261 new->ffs_filearray = NULL;
5262 new->ffs_filearray_size = 0;
5263 new->ffs_filearray_cur = 0;
5264 new->ffs_stage = 0;
5265 new->ffs_level = level;
Bram Moolenaar945ec092016-06-08 21:17:43 +02005266 new->ffs_star_star_empty = star_star_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 /* the following saves NULL pointer checks in vim_findfile */
5269 if (fix_part == NULL)
5270 fix_part = (char_u *)"";
5271 new->ffs_fix_path = vim_strsave(fix_part);
5272
5273#ifdef FEAT_PATH_EXTRA
5274 if (wc_part == NULL)
5275 wc_part = (char_u *)"";
5276 new->ffs_wc_path = vim_strsave(wc_part);
5277#endif
5278
5279 if (new->ffs_fix_path == NULL
5280#ifdef FEAT_PATH_EXTRA
5281 || new->ffs_wc_path == NULL
5282#endif
5283 )
5284 {
5285 ff_free_stack_element(new);
5286 new = NULL;
5287 }
5288
5289 return new;
5290}
5291
5292/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005293 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 */
5295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005296ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
5298 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005299 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005300 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005302 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5303 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305}
5306
5307/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005308 * Pop a dir from the directory stack.
5309 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 */
5311 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005312ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314 ff_stack_T *sptr;
5315
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005316 sptr = search_ctx->ffsc_stack_ptr;
5317 if (search_ctx->ffsc_stack_ptr != NULL)
5318 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 return sptr;
5321}
5322
5323/*
5324 * free the given stack element
5325 */
5326 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005327ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
5329 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005330 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005332 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333#endif
5334
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005335 if (stack_ptr->ffs_filearray != NULL)
5336 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005338 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339}
5340
5341/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005342 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 */
5344 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005345ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 ff_stack_T *sptr;
5348
5349 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005350 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 ff_free_stack_element(sptr);
5352
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005353 vim_free(search_ctx->ffsc_file_to_search);
5354 vim_free(search_ctx->ffsc_start_dir);
5355 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005357 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358#endif
5359
5360#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005361 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 int i = 0;
5364
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005365 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005367 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 i++;
5369 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005370 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005372 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
5374
5375 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005376 search_ctx->ffsc_file_to_search = NULL;
5377 search_ctx->ffsc_start_dir = NULL;
5378 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005380 search_ctx->ffsc_wc_path = NULL;
5381 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382#endif
5383}
5384
5385#ifdef FEAT_PATH_EXTRA
5386/*
5387 * check if the given path is in the stopdirs
5388 * returns TRUE if yes else FALSE
5389 */
5390 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005391ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393 int i = 0;
5394
5395 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005396 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 path_len--;
5398
5399 /* if no path consider it as match */
5400 if (path_len == 0)
5401 return TRUE;
5402
5403 for (i = 0; stopdirs_v[i] != NULL; i++)
5404 {
5405 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5406 {
5407 /* match for parent directory. So '/home' also matches
5408 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5409 * '/home/r' would also match '/home/rks'
5410 */
5411 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005412 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 return TRUE;
5414 }
5415 else
5416 {
5417 if (fnamecmp(stopdirs_v[i], path) == 0)
5418 return TRUE;
5419 }
5420 }
5421 return FALSE;
5422}
5423#endif
5424
5425#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5426/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005427 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 *
5429 * On the first call set the parameter 'first' to TRUE to initialize
5430 * the search. For repeating calls to FALSE.
5431 *
5432 * Repeating calls will return other files called 'ptr[len]' from the path.
5433 *
5434 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5435 * don't need valid values.
5436 *
5437 * If nothing found on the first call the option FNAME_MESS will issue the
5438 * message:
5439 * 'Can't find file "<file>" in path'
5440 * On repeating calls:
5441 * 'No more file "<file>" found in path'
5442 *
5443 * options:
5444 * FNAME_MESS give error message when not found
5445 *
5446 * Uses NameBuff[]!
5447 *
5448 * Returns an allocated string for the file name. NULL for error.
5449 *
5450 */
5451 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005452find_file_in_path(
5453 char_u *ptr, /* file name */
5454 int len, /* length of file name */
5455 int options,
5456 int first, /* use count'th matching file name */
5457 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459 return find_file_in_path_option(ptr, len, options, first,
5460 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005461 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
5463
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005464static char_u *ff_file_to_find = NULL;
5465static void *fdip_search_ctx = NULL;
5466
5467#if defined(EXITFREE)
5468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005469free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005470{
5471 vim_free(ff_file_to_find);
5472 vim_findfile_cleanup(fdip_search_ctx);
5473}
5474#endif
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476/*
5477 * Find the directory name "ptr[len]" in the path.
5478 *
5479 * options:
5480 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005481 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 *
5483 * Uses NameBuff[]!
5484 *
5485 * Returns an allocated string for the file name. NULL for error.
5486 */
5487 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005488find_directory_in_path(
5489 char_u *ptr, /* file name */
5490 int len, /* length of file name */
5491 int options,
5492 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005495 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005498 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005499find_file_in_path_option(
5500 char_u *ptr, /* file name */
5501 int len, /* length of file name */
5502 int options,
5503 int first, /* use count'th matching file name */
5504 char_u *path_option, /* p_path or p_cdpath */
5505 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5506 char_u *rel_fname, /* file name we are looking relative to. */
5507 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 static int did_findfile_init = FALSE;
5511 char_u save_char;
5512 char_u *file_name = NULL;
5513 char_u *buf = NULL;
5514 int rel_to_curdir;
5515#ifdef AMIGA
5516 struct Process *proc = (struct Process *)FindTask(0L);
5517 APTR save_winptr = proc->pr_WindowPtr;
5518
5519 /* Avoid a requester here for a volume that doesn't exist. */
5520 proc->pr_WindowPtr = (APTR)-1L;
5521#endif
5522
5523 if (first == TRUE)
5524 {
5525 /* copy file name into NameBuff, expanding environment variables */
5526 save_char = ptr[len];
5527 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005528 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 ptr[len] = save_char;
5530
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005531 vim_free(ff_file_to_find);
5532 ff_file_to_find = vim_strsave(NameBuff);
5533 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
5535 file_name = NULL;
5536 goto theend;
5537 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005538 if (options & FNAME_UNESC)
5539 {
5540 /* Change all "\ " to " ". */
5541 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5542 if (ptr[0] == '\\' && ptr[1] == ' ')
5543 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005547 rel_to_curdir = (ff_file_to_find[0] == '.'
5548 && (ff_file_to_find[1] == NUL
5549 || vim_ispathsep(ff_file_to_find[1])
5550 || (ff_file_to_find[1] == '.'
5551 && (ff_file_to_find[2] == NUL
5552 || vim_ispathsep(ff_file_to_find[2])))));
5553 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 /* "..", "../path", "." and "./path": don't use the path_option */
5555 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005556#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005558 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005559 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005560 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#endif
5562#ifdef AMIGA
5563 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005564 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565#endif
5566 )
5567 {
5568 /*
5569 * Absolute path, no need to use "path_option".
5570 * If this is not a first call, return NULL. We already returned a
5571 * filename on the first call.
5572 */
5573 if (first == TRUE)
5574 {
5575 int l;
5576 int run;
5577
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005578 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005580 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 goto theend;
5582 }
5583
5584 /* When FNAME_REL flag given first use the directory of the file.
5585 * Otherwise or when this fails use the current directory. */
5586 for (run = 1; run <= 2; ++run)
5587 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005588 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 if (run == 1
5590 && rel_to_curdir
5591 && (options & FNAME_REL)
5592 && rel_fname != NULL
5593 && STRLEN(rel_fname) + l < MAXPATHL)
5594 {
5595 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005596 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 l = (int)STRLEN(NameBuff);
5598 }
5599 else
5600 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005601 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 run = 2;
5603 }
5604
5605 /* When the file doesn't exist, try adding parts of
5606 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005607 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 for (;;)
5609 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005610 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005611 && (find_what == FINDFILE_BOTH
5612 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005613 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 file_name = vim_strsave(NameBuff);
5616 goto theend;
5617 }
5618 if (*buf == NUL)
5619 break;
5620 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5621 }
5622 }
5623 }
5624 }
5625 else
5626 {
5627 /*
5628 * Loop over all paths in the 'path' or 'cdpath' option.
5629 * When "first" is set, first setup to the start of the option.
5630 * Otherwise continue to find the next match.
5631 */
5632 if (first == TRUE)
5633 {
5634 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005635 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 dir = path_option;
5637 did_findfile_init = FALSE;
5638 }
5639
5640 for (;;)
5641 {
5642 if (did_findfile_init)
5643 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005644 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (file_name != NULL)
5646 break;
5647
5648 did_findfile_init = FALSE;
5649 }
5650 else
5651 {
5652 char_u *r_ptr;
5653
5654 if (dir == NULL || *dir == NUL)
5655 {
5656 /* We searched all paths of the option, now we can
5657 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005658 vim_findfile_cleanup(fdip_search_ctx);
5659 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 break;
5661 }
5662
5663 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5664 break;
5665
5666 /* copy next path */
5667 buf[0] = 0;
5668 copy_option_part(&dir, buf, MAXPATHL, " ,");
5669
5670#ifdef FEAT_PATH_EXTRA
5671 /* get the stopdir string */
5672 r_ptr = vim_findfile_stopdir(buf);
5673#else
5674 r_ptr = NULL;
5675#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005676 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005677 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005678 fdip_search_ctx, FALSE, rel_fname);
5679 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 did_findfile_init = TRUE;
5681 vim_free(buf);
5682 }
5683 }
5684 }
5685 if (file_name == NULL && (options & FNAME_MESS))
5686 {
5687 if (first == TRUE)
5688 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005689 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005691 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
5693 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005694 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696 else
5697 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005698 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005700 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 else
5702 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005703 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
5705 }
5706
5707theend:
5708#ifdef AMIGA
5709 proc->pr_WindowPtr = save_winptr;
5710#endif
5711 return file_name;
5712}
5713
5714#endif /* FEAT_SEARCHPATH */
5715
5716/*
5717 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5718 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5719 */
5720 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005721vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723#ifndef FEAT_SEARCHPATH
5724 return mch_chdir((char *)new_dir);
5725#else
5726 char_u *dir_name;
5727 int r;
5728
5729 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5730 FNAME_MESS, curbuf->b_ffname);
5731 if (dir_name == NULL)
5732 return -1;
5733 r = mch_chdir((char *)dir_name);
5734 vim_free(dir_name);
5735 return r;
5736#endif
5737}
5738
5739/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005740 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005742 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5743 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 * Returns OK or FAIL.
5745 */
5746 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005747get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005749 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 if (mch_get_user_name(buf, len) == FAIL)
5752 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005753 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005756 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 return OK;
5758}
5759
5760#ifndef HAVE_QSORT
5761/*
5762 * Our own qsort(), for systems that don't have it.
5763 * It's simple and slow. From the K&R C book.
5764 */
5765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005766qsort(
5767 void *base,
5768 size_t elm_count,
5769 size_t elm_size,
5770 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 char_u *buf;
5773 char_u *p1;
5774 char_u *p2;
5775 int i, j;
5776 int gap;
5777
5778 buf = alloc((unsigned)elm_size);
5779 if (buf == NULL)
5780 return;
5781
5782 for (gap = elm_count / 2; gap > 0; gap /= 2)
5783 for (i = gap; i < elm_count; ++i)
5784 for (j = i - gap; j >= 0; j -= gap)
5785 {
5786 /* Compare the elements. */
5787 p1 = (char_u *)base + j * elm_size;
5788 p2 = (char_u *)base + (j + gap) * elm_size;
5789 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5790 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005791 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 mch_memmove(buf, p1, elm_size);
5793 mch_memmove(p1, p2, elm_size);
5794 mch_memmove(p2, buf, elm_size);
5795 }
5796
5797 vim_free(buf);
5798}
5799#endif
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801/*
5802 * Sort an array of strings.
5803 */
5804static int
5805#ifdef __BORLANDC__
5806_RTLENTRYF
5807#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005808sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
5810 static int
5811#ifdef __BORLANDC__
5812_RTLENTRYF
5813#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005814sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 return STRCMP(*(char **)s1, *(char **)s2);
5817}
5818
5819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005820sort_strings(
5821 char_u **files,
5822 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827#if !defined(NO_EXPANDPATH) || defined(PROTO)
5828/*
5829 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005830 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 * Return value like strcmp(p, q), but consider path separators.
5832 */
5833 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005834pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005836 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005837 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005838 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005840 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005842 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005843 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005846 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005848 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 return 0;
5850 s = q;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005851 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 break;
5853 }
5854
5855 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005856 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
5858 s = p;
5859 break;
5860 }
5861
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005862 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863#ifdef BACKSLASH_IN_FILENAME
5864 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005865 && !((c1 == '/' && c2 == '\\')
5866 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#endif
5868 )
5869 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005870 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005872 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005874 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5875 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005877
5878 i += MB_PTR2LEN((char_u *)p + i);
5879 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005881 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005882 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005884 c1 = PTR2CHAR((char_u *)s + i);
5885 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005887 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005888 && i > 0
5889 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005891 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005893 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005895 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 return 0; /* match with trailing slash */
5897 if (s == q)
5898 return -1; /* no match */
5899 return 1;
5900}
5901#endif
5902
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903/*
5904 * The putenv() implementation below comes from the "screen" program.
5905 * Included with permission from Juergen Weigert.
5906 * See pty.c for the copyright notice.
5907 */
5908
5909/*
5910 * putenv -- put value into environment
5911 *
5912 * Usage: i = putenv (string)
5913 * int i;
5914 * char *string;
5915 *
5916 * where string is of the form <name>=<value>.
5917 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5918 *
5919 * Putenv may need to add a new name into the environment, or to
5920 * associate a value longer than the current value with a particular
5921 * name. So, to make life simpler, putenv() copies your entire
5922 * environment into the heap (i.e. malloc()) from the stack
5923 * (i.e. where it resides when your process is initiated) the first
5924 * time you call it.
5925 *
5926 * (history removed, not very interesting. See the "screen" sources.)
5927 */
5928
5929#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5930
5931#define EXTRASIZE 5 /* increment to add to env. size */
5932
5933static int envsize = -1; /* current size of environment */
5934#ifndef MACOS_CLASSIC
5935extern
5936#endif
5937 char **environ; /* the global which is your env. */
5938
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005939static int findenv(char *name); /* look for a name in the env. */
5940static int newenv(void); /* copy env. from stack to heap */
5941static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942
5943 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005944putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
5946 int i;
5947 char *p;
5948
5949 if (envsize < 0)
5950 { /* first time putenv called */
5951 if (newenv() < 0) /* copy env. to heap */
5952 return -1;
5953 }
5954
5955 i = findenv((char *)string); /* look for name in environment */
5956
5957 if (i < 0)
5958 { /* name must be added */
5959 for (i = 0; environ[i]; i++);
5960 if (i >= (envsize - 1))
5961 { /* need new slot */
5962 if (moreenv() < 0)
5963 return -1;
5964 }
5965 p = (char *)alloc((unsigned)(strlen(string) + 1));
5966 if (p == NULL) /* not enough core */
5967 return -1;
5968 environ[i + 1] = 0; /* new end of env. */
5969 }
5970 else
5971 { /* name already in env. */
5972 p = vim_realloc(environ[i], strlen(string) + 1);
5973 if (p == NULL)
5974 return -1;
5975 }
5976 sprintf(p, "%s", string); /* copy into env. */
5977 environ[i] = p;
5978
5979 return 0;
5980}
5981
5982 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005983findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 char *namechar, *envchar;
5986 int i, found;
5987
5988 found = 0;
5989 for (i = 0; environ[i] && !found; i++)
5990 {
5991 envchar = environ[i];
5992 namechar = name;
5993 while (*namechar && *namechar != '=' && (*namechar == *envchar))
5994 {
5995 namechar++;
5996 envchar++;
5997 }
5998 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
5999 }
6000 return found ? i - 1 : -1;
6001}
6002
6003 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006004newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 char **env, *elem;
6007 int i, esize;
6008
6009#ifdef MACOS
6010 /* for Mac a new, empty environment is created */
6011 i = 0;
6012#else
6013 for (i = 0; environ[i]; i++)
6014 ;
6015#endif
6016 esize = i + EXTRASIZE + 1;
6017 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6018 if (env == NULL)
6019 return -1;
6020
6021#ifndef MACOS
6022 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 }
6030#endif
6031
6032 env[i] = 0;
6033 environ = env;
6034 envsize = esize;
6035 return 0;
6036}
6037
6038 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006039moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 int esize;
6042 char **env;
6043
6044 esize = envsize + EXTRASIZE;
6045 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6046 if (env == 0)
6047 return -1;
6048 environ = env;
6049 envsize = esize;
6050 return 0;
6051}
6052
6053# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02006054/*
6055 * Used for mch_getenv() for Mac.
6056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006058vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 int i;
6061 char_u *p;
6062
6063 if (envsize < 0)
6064 return NULL;
6065
6066 i = findenv((char *)string);
6067
6068 if (i < 0)
6069 return NULL;
6070
6071 p = vim_strchr((char_u *)environ[i], '=');
6072 return (p + 1);
6073}
6074# endif
6075
6076#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006077
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006078#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006079/*
6080 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6081 * rights to write into.
6082 */
6083 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006084filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006085{
6086 int retval = 0;
6087#if defined(UNIX) || defined(VMS)
6088 int perm = 0;
6089#endif
6090
6091#if defined(UNIX) || defined(VMS)
6092 perm = mch_getperm(fname);
6093#endif
6094#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6095 if (
6096# ifdef WIN3264
6097 mch_writable(fname) &&
6098# else
6099# if defined(UNIX) || defined(VMS)
6100 (perm & 0222) &&
6101# endif
6102# endif
6103 mch_access((char *)fname, W_OK) == 0
6104 )
6105#endif
6106 {
6107 ++retval;
6108 if (mch_isdir(fname))
6109 ++retval;
6110 }
6111 return retval;
6112}
6113#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006114
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006115#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6116/*
6117 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6118 */
6119 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006120get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006121{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006122 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006123
6124 n = getc(fd);
6125 n = (n << 8) + getc(fd);
6126 return n;
6127}
6128
6129/*
6130 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6131 */
6132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006133get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006134{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006135 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006136
6137 n = getc(fd);
6138 n = (n << 8) + getc(fd);
6139 n = (n << 8) + getc(fd);
6140 return n;
6141}
6142
6143/*
6144 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6145 */
6146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006147get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006148{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006149 /* Use unsigned rather than int otherwise result is undefined
6150 * when left-shift sets the MSB. */
6151 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006152
Bram Moolenaar95235e62013-09-08 16:07:07 +02006153 n = (unsigned)getc(fd);
6154 n = (n << 8) + (unsigned)getc(fd);
6155 n = (n << 8) + (unsigned)getc(fd);
6156 n = (n << 8) + (unsigned)getc(fd);
6157 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006158}
6159
6160/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006161 * Read 8 bytes from "fd" and turn them into a time_T, MSB first.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006162 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006163 time_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006164get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006165{
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006166 time_T n = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006167 int i;
6168
6169 for (i = 0; i < 8; ++i)
6170 n = (n << 8) + getc(fd);
6171 return n;
6172}
6173
6174/*
6175 * Read a string of length "cnt" from "fd" into allocated memory.
6176 * Returns NULL when out of memory or unable to read that many bytes.
6177 */
6178 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006179read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006180{
6181 char_u *str;
6182 int i;
6183 int c;
6184
6185 /* allocate memory */
6186 str = alloc((unsigned)cnt + 1);
6187 if (str != NULL)
6188 {
6189 /* Read the string. Quit when running into the EOF. */
6190 for (i = 0; i < cnt; ++i)
6191 {
6192 c = getc(fd);
6193 if (c == EOF)
6194 {
6195 vim_free(str);
6196 return NULL;
6197 }
6198 str[i] = c;
6199 }
6200 str[i] = NUL;
6201 }
6202 return str;
6203}
6204
6205/*
6206 * Write a number to file "fd", MSB first, in "len" bytes.
6207 */
6208 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006209put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006210{
6211 int i;
6212
6213 for (i = len - 1; i >= 0; --i)
6214 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6215 return FAIL;
6216 return OK;
6217}
6218
6219#ifdef _MSC_VER
6220# if (_MSC_VER <= 1200)
6221/* This line is required for VC6 without the service pack. Also see the
6222 * matching #pragma below. */
6223 # pragma optimize("", off)
6224# endif
6225#endif
6226
6227/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006228 * Write time_T to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006229 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006230 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006231 int
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006232put_time(FILE *fd, time_T the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006233{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006234 char_u buf[8];
6235
6236 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006237 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006238}
6239
6240/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006241 * Write time_T to "buf[8]".
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006242 */
6243 void
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006244time_to_bytes(time_T the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006245{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006246 int c;
6247 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006248 int bi = 0;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006249 time_T wtime = the_time;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006250
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006251 /* time_T can be up to 8 bytes in size, more than long_u, thus we
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006252 * can't use put_bytes() here.
6253 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006254 * sign. This happens for large values of wtime. A cast to long_u may
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006255 * truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes,
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006256 * it's safe to assume that long_u is 4 bytes or more and when using 8
6257 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006258 for (i = 7; i >= 0; --i)
6259 {
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006260 if (i + 1 > (int)sizeof(time_T))
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006261 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006262 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006263 else
6264 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006265#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006266 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006267#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006268 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006269#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006270 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006271 }
6272 }
6273}
6274
6275#ifdef _MSC_VER
6276# if (_MSC_VER <= 1200)
6277 # pragma optimize("", on)
6278# endif
6279#endif
6280
6281#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006282
6283#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6284 || defined(FEAT_SPELL) || defined(PROTO)
6285/*
6286 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6287 * When "s" is NULL FALSE is returned.
6288 */
6289 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006290has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006291{
6292 char_u *p;
6293
6294 if (s != NULL)
6295 for (p = s; *p != NUL; ++p)
6296 if (*p >= 128)
6297 return TRUE;
6298 return FALSE;
6299}
6300#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006301
6302#if defined(MESSAGE_QUEUE) || defined(PROTO)
6303/*
6304 * Process messages that have been queued for netbeans or clientserver.
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006305 * Also check if any jobs have ended.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006306 * These functions can call arbitrary vimscript and should only be called when
6307 * it is safe to do so.
6308 */
6309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006310parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006311{
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006312 /* For Win32 mch_breakcheck() does not check for input, do it here. */
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006313# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006314 channel_handle_events(FALSE);
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006315# endif
6316
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006317# ifdef FEAT_NETBEANS_INTG
6318 /* Process the queued netbeans messages. */
6319 netbeans_parse_messages();
6320# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006321# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006322 /* Write any buffer lines still to be written. */
6323 channel_write_any_lines();
6324
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006325 /* Process the messages queued on channels. */
6326 channel_parse_messages();
6327# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006328# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006329 /* Process the queued clientserver messages. */
6330 server_parse_messages();
6331# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006332# ifdef FEAT_JOB_CHANNEL
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006333 /* Check if any jobs have ended. */
6334 job_check_ended();
6335# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006336}
6337#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006338
Bram Moolenaar51628222016-12-01 23:03:28 +01006339#ifndef PROTO /* proto is defined in vim.h */
6340# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006341/*
6342 * Return time in msec since "start_tv".
6343 */
6344 long
6345elapsed(struct timeval *start_tv)
6346{
6347 struct timeval now_tv;
6348
6349 gettimeofday(&now_tv, NULL);
6350 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
6351 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
6352}
Bram Moolenaar51628222016-12-01 23:03:28 +01006353# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006354
Bram Moolenaar51628222016-12-01 23:03:28 +01006355# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006356/*
6357 * Return time in msec since "start_tick".
6358 */
6359 long
6360elapsed(DWORD start_tick)
6361{
6362 DWORD now = GetTickCount();
6363
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006364 return (long)now - (long)start_tick;
6365}
Bram Moolenaar51628222016-12-01 23:03:28 +01006366# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006367#endif