blob: d431a94280143f785b9896fcd92edb502ba274bd [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
168 int width = W_WIDTH(curwin) - win_col_off(curwin);
169
Bram Moolenaarebefac62005-12-28 22:39:57 +0000170 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 && curwin->w_p_wrap
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100172# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 && curwin->w_width != 0
174# endif
175 && wcol >= (colnr_T)width)
176 {
177 csize = linetabsize(line);
178 if (csize > 0)
179 csize--;
180
Bram Moolenaarebefac62005-12-28 22:39:57 +0000181 if (wcol / width > (colnr_T)csize / width
182 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
184 /* In case of line wrapping don't move the cursor beyond the
Bram Moolenaarebefac62005-12-28 22:39:57 +0000185 * right screen edge. In Insert mode allow going just beyond
186 * the last character (like what happens when typing and
187 * reaching the right window edge). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 wcol = (csize / width + 1) * width - 1;
189 }
190 }
191#endif
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 ptr = line;
194 while (col <= wcol && *ptr != NUL)
195 {
196 /* Count a tab for what it's worth (if list mode not on) */
197#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200198 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100199 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200201 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
203 col += csize;
204 }
205 idx = (int)(ptr - line);
206 /*
207 * Handle all the special cases. The virtual_active() check
208 * is needed to ensure that a virtual position off the end of
209 * a line has the correct indexing. The one_more comparison
210 * replaces an explicit add of one_more later on.
211 */
212 if (col > wcol || (!virtual_active() && one_more == 0))
213 {
214 idx -= 1;
215# ifdef FEAT_LINEBREAK
216 /* Don't count the chars from 'showbreak'. */
217 csize -= head;
218# endif
219 col -= csize;
220 }
221
222#ifdef FEAT_VIRTUALEDIT
223 if (virtual_active()
224 && addspaces
225 && ((col != wcol && col != wcol + 1) || csize > 1))
226 {
227 /* 'virtualedit' is set: The difference between wcol and col is
228 * filled with spaces. */
229
230 if (line[idx] == NUL)
231 {
232 /* Append spaces */
233 int correct = wcol - col;
234 char_u *newline = alloc(idx + correct + 1);
235 int t;
236
237 if (newline == NULL)
238 return FAIL;
239
240 for (t = 0; t < idx; ++t)
241 newline[t] = line[t];
242
243 for (t = 0; t < correct; ++t)
244 newline[t + idx] = ' ';
245
246 newline[idx + correct] = NUL;
247
248 ml_replace(pos->lnum, newline, FALSE);
249 changed_bytes(pos->lnum, (colnr_T)idx);
250 idx += correct;
251 col = wcol;
252 }
253 else
254 {
255 /* Break a tab */
256 int linelen = (int)STRLEN(line);
257 int correct = wcol - col - csize + 1; /* negative!! */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000258 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 int t, s = 0;
260 int v;
261
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000262 if (-correct > csize)
263 return FAIL;
264
265 newline = alloc(linelen + csize);
266 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 return FAIL;
268
269 for (t = 0; t < linelen; t++)
270 {
271 if (t != idx)
272 newline[s++] = line[t];
273 else
274 for (v = 0; v < csize; v++)
275 newline[s++] = ' ';
276 }
277
278 newline[linelen + csize - 1] = NUL;
279
280 ml_replace(pos->lnum, newline, FALSE);
281 changed_bytes(pos->lnum, idx);
282 idx += (csize - 1 + correct);
283 col += correct;
284 }
285 }
286#endif
287 }
288
289 if (idx < 0)
290 pos->col = 0;
291 else
292 pos->col = idx;
293
294#ifdef FEAT_VIRTUALEDIT
295 pos->coladd = 0;
296
297 if (finetune)
298 {
299 if (wcol == MAXCOL)
300 {
301 /* The width of the last character is used to set coladd. */
302 if (!one_more)
303 {
304 colnr_T scol, ecol;
305
306 getvcol(curwin, pos, &scol, NULL, &ecol);
307 pos->coladd = ecol - scol;
308 }
309 }
310 else
311 {
312 int b = (int)wcol - (int)col;
313
314 /* The difference between wcol and col is used to set coladd. */
315 if (b > 0 && b < (MAXCOL - 2 * W_WIDTH(curwin)))
316 pos->coladd = b;
317
318 col += b;
319 }
320 }
321#endif
322
323#ifdef FEAT_MBYTE
Bram Moolenaara1381de2009-11-03 15:44:21 +0000324 /* prevent from moving onto a trail byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200326 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#endif
328
329 if (col < wcol)
330 return FAIL;
331 return OK;
332}
333
334/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000335 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 */
337 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 return inc(&curwin->w_cursor);
341}
342
Bram Moolenaar446cb832008-06-24 21:56:24 +0000343/*
344 * Increment the line pointer "lp" crossing line boundaries as necessary.
345 * Return 1 when going to the next line.
346 * Return 2 when moving forward onto a NUL at the end of the line).
347 * Return -1 when at the end of file.
348 * Return 0 otherwise.
349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100351inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
353 char_u *p = ml_get_pos(lp);
354
355 if (*p != NUL) /* still within line, move to next char (may be NUL) */
356 {
357#ifdef FEAT_MBYTE
358 if (has_mbyte)
359 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000360 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 lp->col += l;
363 return ((p[l] != NUL) ? 0 : 2);
364 }
365#endif
366 lp->col++;
367#ifdef FEAT_VIRTUALEDIT
368 lp->coladd = 0;
369#endif
370 return ((p[1] != NUL) ? 0 : 2);
371 }
372 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
373 {
374 lp->col = 0;
375 lp->lnum++;
376#ifdef FEAT_VIRTUALEDIT
377 lp->coladd = 0;
378#endif
379 return 1;
380 }
381 return -1;
382}
383
384/*
385 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
386 */
387 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100388incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 int r;
391
392 if ((r = inc(lp)) >= 1 && lp->col)
393 r = inc(lp);
394 return r;
395}
396
397/*
398 * dec(p)
399 *
400 * Decrement the line pointer 'p' crossing line boundaries as necessary.
401 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
402 */
403 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100404dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100406 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407}
408
409 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100410dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
412 char_u *p;
413
414#ifdef FEAT_VIRTUALEDIT
415 lp->coladd = 0;
416#endif
417 if (lp->col > 0) /* still within line */
418 {
419 lp->col--;
420#ifdef FEAT_MBYTE
421 if (has_mbyte)
422 {
423 p = ml_get(lp->lnum);
424 lp->col -= (*mb_head_off)(p, p + lp->col);
425 }
426#endif
427 return 0;
428 }
429 if (lp->lnum > 1) /* there is a prior line */
430 {
431 lp->lnum--;
432 p = ml_get(lp->lnum);
433 lp->col = (colnr_T)STRLEN(p);
434#ifdef FEAT_MBYTE
435 if (has_mbyte)
436 lp->col -= (*mb_head_off)(p, p + lp->col);
437#endif
438 return 1;
439 }
440 return -1; /* at start of file */
441}
442
443/*
444 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
445 */
446 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100447decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 int r;
450
451 if ((r = dec(lp)) == 1 && lp->col)
452 r = dec(lp);
453 return r;
454}
455
456/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200457 * Get the line number relative to the current cursor position, i.e. the
458 * difference between line number and cursor position. Only look for lines that
459 * can be visible, folded lines don't count.
460 */
461 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100462get_cursor_rel_lnum(
463 win_T *wp,
464 linenr_T lnum) /* line number to get the result for */
Bram Moolenaar64486672010-05-16 15:46:46 +0200465{
466 linenr_T cursor = wp->w_cursor.lnum;
467 linenr_T retval = 0;
468
469#ifdef FEAT_FOLDING
470 if (hasAnyFolding(wp))
471 {
472 if (lnum > cursor)
473 {
474 while (lnum > cursor)
475 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100476 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200477 /* if lnum and cursor are in the same fold,
478 * now lnum <= cursor */
479 if (lnum > cursor)
480 retval++;
481 lnum--;
482 }
483 }
484 else if (lnum < cursor)
485 {
486 while (lnum < cursor)
487 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100488 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200489 /* if lnum and cursor are in the same fold,
490 * now lnum >= cursor */
491 if (lnum < cursor)
492 retval--;
493 lnum++;
494 }
495 }
496 /* else if (lnum == cursor)
497 * retval = 0;
498 */
499 }
500 else
501#endif
502 retval = lnum - cursor;
503
504 return retval;
505}
506
507/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200508 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
509 * This allows for the col to be on the NUL byte.
510 */
511 void
512check_pos(buf_T *buf, pos_T *pos)
513{
514 char_u *line;
515 colnr_T len;
516
517 if (pos->lnum > buf->b_ml.ml_line_count)
518 pos->lnum = buf->b_ml.ml_line_count;
519
520 if (pos->col > 0)
521 {
522 line = ml_get_buf(buf, pos->lnum, FALSE);
523 len = (colnr_T)STRLEN(line);
524 if (pos->col > len)
525 pos->col = len;
526 }
527}
528
529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * Make sure curwin->w_cursor.lnum is valid.
531 */
532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100533check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
536 {
537#ifdef FEAT_FOLDING
538 /* If there is a closed fold at the end of the file, put the cursor in
539 * its first line. Otherwise in the last line. */
540 if (!hasFolding(curbuf->b_ml.ml_line_count,
541 &curwin->w_cursor.lnum, NULL))
542#endif
543 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
544 }
545 if (curwin->w_cursor.lnum <= 0)
546 curwin->w_cursor.lnum = 1;
547}
548
549/*
550 * Make sure curwin->w_cursor.col is valid.
551 */
552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100553check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200555 check_cursor_col_win(curwin);
556}
557
558/*
559 * Make sure win->w_cursor.col is valid.
560 */
561 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100562check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200563{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 colnr_T len;
565#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200566 colnr_T oldcol = win->w_cursor.col;
567 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#endif
569
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200570 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200572 win->w_cursor.col = 0;
573 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000575 /* Allow cursor past end-of-line when:
576 * - in Insert mode or restarting Insert mode
577 * - in Visual mode and 'selection' isn't "old"
578 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000579 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000581#ifdef FEAT_VIRTUALEDIT
582 || (ve_flags & VE_ONEMORE)
583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200585 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000587 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 win->w_cursor.col = len - 1;
Bram Moolenaar87c19962007-04-26 08:54:21 +0000589#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200590 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000591 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200592 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000593#endif
594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200596 else if (win->w_cursor.col < 0)
597 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599#ifdef FEAT_VIRTUALEDIT
600 /* If virtual editing is on, we can leave the cursor on the old position,
601 * only we must set it to virtual. But don't do it when at the end of the
602 * line. */
603 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200604 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000606 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200607 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200608 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200609 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200610 if (win->w_cursor.col < len && win->w_cursor.coladd > 0)
611 {
612 int cs, ce;
613
614 /* check that coladd is not more than the char width */
615 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
616 if (win->w_cursor.coladd > ce - cs)
617 win->w_cursor.coladd = ce - cs;
618 }
619 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000620 else
621 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200622 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#endif
625}
626
627/*
628 * make sure curwin->w_cursor in on a valid character
629 */
630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100631check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 check_cursor_lnum();
634 check_cursor_col();
635}
636
637#if defined(FEAT_TEXTOBJ) || defined(PROTO)
638/*
639 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
640 * Allow it when in Visual mode and 'selection' is not "old".
641 */
642 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100643adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 && gchar_cursor() == NUL)
648 --curwin->w_cursor.col;
649}
650#endif
651
652/*
653 * When curwin->w_leftcol has changed, adjust the cursor position.
654 * Return TRUE if the cursor was moved.
655 */
656 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 long lastcol;
660 colnr_T s, e;
661 int retval = FALSE;
662
663 changed_cline_bef_curs();
664 lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1;
665 validate_virtcol();
666
667 /*
668 * If the cursor is right or left of the screen, move it to last or first
669 * character.
670 */
671 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
672 {
673 retval = TRUE;
674 coladvance((colnr_T)(lastcol - p_siso));
675 }
676 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
677 {
678 retval = TRUE;
679 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
680 }
681
682 /*
683 * If the start of the character under the cursor is not on the screen,
684 * advance the cursor one more char. If this fails (last char of the
685 * line) adjust the scrolling.
686 */
687 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
688 if (e > (colnr_T)lastcol)
689 {
690 retval = TRUE;
691 coladvance(s - 1);
692 }
693 else if (s < curwin->w_leftcol)
694 {
695 retval = TRUE;
696 if (coladvance(e + 1) == FAIL) /* there isn't another character */
697 {
698 curwin->w_leftcol = s; /* adjust w_leftcol instead */
699 changed_cline_bef_curs();
700 }
701 }
702
703 if (retval)
704 curwin->w_set_curswant = TRUE;
705 redraw_later(NOT_VALID);
706 return retval;
707}
708
709/**********************************************************************
710 * Various routines dealing with allocation and deallocation of memory.
711 */
712
713#if defined(MEM_PROFILE) || defined(PROTO)
714
715# define MEM_SIZES 8200
716static long_u mem_allocs[MEM_SIZES];
717static long_u mem_frees[MEM_SIZES];
718static long_u mem_allocated;
719static long_u mem_freed;
720static long_u mem_peak;
721static long_u num_alloc;
722static long_u num_freed;
723
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100724static void mem_pre_alloc_s(size_t *sizep);
725static void mem_pre_alloc_l(long_u *sizep);
726static void mem_post_alloc(void **pp, size_t size);
727static void mem_pre_free(void **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100730mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 *sizep += sizeof(size_t);
733}
734
735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100736mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738 *sizep += sizeof(size_t);
739}
740
741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100742mem_post_alloc(
743 void **pp,
744 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 if (*pp == NULL)
747 return;
748 size -= sizeof(size_t);
749 *(long_u *)*pp = size;
750 if (size <= MEM_SIZES-1)
751 mem_allocs[size-1]++;
752 else
753 mem_allocs[MEM_SIZES-1]++;
754 mem_allocated += size;
755 if (mem_allocated - mem_freed > mem_peak)
756 mem_peak = mem_allocated - mem_freed;
757 num_alloc++;
758 *pp = (void *)((char *)*pp + sizeof(size_t));
759}
760
761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100762mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 long_u size;
765
766 *pp = (void *)((char *)*pp - sizeof(size_t));
767 size = *(size_t *)*pp;
768 if (size <= MEM_SIZES-1)
769 mem_frees[size-1]++;
770 else
771 mem_frees[MEM_SIZES-1]++;
772 mem_freed += size;
773 num_freed++;
774}
775
776/*
777 * called on exit via atexit()
778 */
779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100780vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 int i, j;
783
784 printf("\r\n");
785 j = 0;
786 for (i = 0; i < MEM_SIZES - 1; i++)
787 {
788 if (mem_allocs[i] || mem_frees[i])
789 {
790 if (mem_frees[i] > mem_allocs[i])
791 printf("\r\n%s", _("ERROR: "));
792 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
793 j++;
794 if (j > 3)
795 {
796 j = 0;
797 printf("\r\n");
798 }
799 }
800 }
801
802 i = MEM_SIZES - 1;
803 if (mem_allocs[i])
804 {
805 printf("\r\n");
806 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200807 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
809 }
810
811 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
812 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
813 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
814 num_alloc, num_freed);
815}
816
817#endif /* MEM_PROFILE */
818
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100819#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100820static int alloc_does_fail(long_u size);
Bram Moolenaara260b872016-01-15 20:48:22 +0100821
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100822 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100824{
825 if (alloc_fail_countdown == 0)
826 {
827 if (--alloc_fail_repeat <= 0)
828 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100829 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100830 return TRUE;
831 }
832 --alloc_fail_countdown;
833 return FALSE;
834}
835#endif
836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837/*
838 * Some memory is reserved for error messages and for being able to
839 * call mf_release_all(), which needs some memory for mf_trans_add().
840 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100841#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200842#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000845 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 * Use lalloc for larger blocks.
847 */
848 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100849alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 return (lalloc((long_u)size, TRUE));
852}
853
854/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100855 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100856 */
857 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100858alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100859{
860#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100861 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100862 return NULL;
863#endif
864 return (lalloc((long_u)size, TRUE));
865}
866
867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 * Allocate memory and set all bytes to zero.
869 */
870 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100871alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 char_u *p;
874
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200875 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (p != NULL)
877 (void)vim_memset(p, 0, (size_t)size);
878 return p;
879}
880
881/*
882 * alloc() with check for maximum line length
883 */
884 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100885alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200887#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (sizeof(int) == 2 && size > 0x7fff)
889 {
890 /* Don't hide this message */
891 emsg_silent = 0;
892 EMSG(_("E340: Line is becoming too long"));
893 return NULL;
894 }
895#endif
896 return (lalloc((long_u)size, TRUE));
897}
898
899/*
900 * Allocate memory like lalloc() and set all bytes to zero.
901 */
902 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100903lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 char_u *p;
906
907 p = (lalloc(size, message));
908 if (p != NULL)
909 (void)vim_memset(p, 0, (size_t)size);
910 return p;
911}
912
913/*
914 * Low level memory allocation function.
915 * This is used often, KEEP IT FAST!
916 */
917 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100918lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919{
920 char_u *p; /* pointer to new storage space */
921 static int releasing = FALSE; /* don't do mf_release_all() recursive */
922 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100923#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 static long_u allocated = 0; /* allocated since last avail check */
925#endif
926
927 /* Safety check for allocating zero bytes */
928 if (size == 0)
929 {
930 /* Don't hide this message */
931 emsg_silent = 0;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100932 IEMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 return NULL;
934 }
935
936#ifdef MEM_PROFILE
937 mem_pre_alloc_l(&size);
938#endif
939
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 /*
941 * Loop when out of memory: Try to release some memfile blocks and
942 * if some blocks are released call malloc again.
943 */
944 for (;;)
945 {
946 /*
947 * Handle three kind of systems:
948 * 1. No check for available memory: Just return.
949 * 2. Slow check for available memory: call mch_avail_mem() after
950 * allocating KEEP_ROOM amount of memory.
951 * 3. Strict check for available memory: call mch_avail_mem()
952 */
953 if ((p = (char_u *)malloc((size_t)size)) != NULL)
954 {
955#ifndef HAVE_AVAIL_MEM
956 /* 1. No check for available memory: Just return. */
957 goto theend;
958#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 /* 2. Slow check for available memory: call mch_avail_mem() after
960 * allocating (KEEP_ROOM / 2) amount of memory. */
961 allocated += size;
962 if (allocated < KEEP_ROOM / 2)
963 goto theend;
964 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200967 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000969 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 p = NULL;
971 }
972 else
973 goto theend;
974#endif
975 }
976 /*
977 * Remember that mf_release_all() is being called to avoid an endless
978 * loop, because mf_release_all() may call alloc() recursively.
979 */
980 if (releasing)
981 break;
982 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000983
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100984 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000985 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 releasing = FALSE;
988 if (!try_again)
989 break;
990 }
991
992 if (message && p == NULL)
993 do_outofmem_msg(size);
994
995theend:
996#ifdef MEM_PROFILE
997 mem_post_alloc((void **)&p, (size_t)size);
998#endif
999 return p;
1000}
1001
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001002/*
1003 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001004 */
1005 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001006lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001007{
1008#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001009 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001010 return NULL;
1011#endif
1012 return (lalloc((long_u)size, message));
1013}
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#if defined(MEM_PROFILE) || defined(PROTO)
1016/*
1017 * realloc() with memory profiling.
1018 */
1019 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001020mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 void *p;
1023
1024 mem_pre_free(&ptr);
1025 mem_pre_alloc_s(&size);
1026
1027 p = realloc(ptr, size);
1028
1029 mem_post_alloc(&p, size);
1030
1031 return p;
1032}
1033#endif
1034
1035/*
1036* Avoid repeating the error message many times (they take 1 second each).
1037* Did_outofmem_msg is reset when a character is read.
1038*/
1039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001040do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 if (!did_outofmem_msg)
1043 {
1044 /* Don't hide this message */
1045 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001046
1047 /* Must come first to avoid coming back here when printing the error
1048 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001050
1051 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 }
1053}
1054
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001055#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001056
1057# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001058static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001059# endif
1060
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001061/*
1062 * Free everything that we allocated.
1063 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001064 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1065 * surprised if Vim crashes...
1066 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001067 */
1068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001069free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001070{
1071 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001072
1073 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1074 * Don't try freeing everything again. */
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001075 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001076 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001077 entered_free_all_mem = TRUE;
Bram Moolenaara9673212016-06-01 22:21:06 +02001078
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001079# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001080 /* Don't want to trigger autocommands from here on. */
1081 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001082# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001083
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001084# ifdef FEAT_WINDOWS
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 Moolenaare0ca7b22007-11-24 20:28:24 +00001091# endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001092
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001093# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001094 /* Free all spell info. */
1095 spell_free_all();
1096# endif
1097
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001098# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001099 /* Clear user commands (before deleting buffers). */
1100 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001101# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001102
1103# ifdef FEAT_MENU
1104 /* Clear menus. */
1105 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001106# ifdef FEAT_MULTI_LANG
1107 do_cmdline_cmd((char_u *)"menutranslate clear");
1108# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001109# endif
1110
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001111 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001112 do_cmdline_cmd((char_u *)"lmapclear");
1113 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001114 do_cmdline_cmd((char_u *)"mapclear");
1115 do_cmdline_cmd((char_u *)"mapclear!");
1116 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001117# if defined(FEAT_EVAL)
1118 do_cmdline_cmd((char_u *)"breakdel *");
1119# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001120# if defined(FEAT_PROFILE)
1121 do_cmdline_cmd((char_u *)"profdel *");
1122# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001123# if defined(FEAT_KEYMAP)
1124 do_cmdline_cmd((char_u *)"set keymap=");
1125#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001126
1127# ifdef FEAT_TITLE
1128 free_titles();
1129# endif
1130# if defined(FEAT_SEARCHPATH)
1131 free_findfile();
1132# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001133
1134 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001135# if defined(FEAT_AUTOCMD)
1136 free_all_autocmds();
1137# endif
1138 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001139 free_all_options();
1140 free_all_marks();
1141 alist_clear(&global_alist);
1142 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001143# if defined(FEAT_CMDL_COMPL)
1144 free_users();
1145# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001146 free_search_patterns();
1147 free_old_sub();
1148 free_last_insert();
1149 free_prev_shellcmd();
1150 free_regexp_stuff();
1151 free_tag_stuff();
1152 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001153# ifdef FEAT_SIGNS
1154 free_signs();
1155# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001156# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001157 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001158# endif
1159# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001160 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001161# endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01001162 clear_sb_text(TRUE); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001163
1164 /* Free some global vars. */
1165 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001166# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001167 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001168# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001169 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001170# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001171 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001172# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001173 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001174 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001175
1176 /* Clear cmdline history. */
1177 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001178# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001179 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001180# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001181
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001182#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001183 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001184 win_T *win;
1185 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001186
1187 qf_free_all(NULL);
1188 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001189 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001190 qf_free_all(win);
1191 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001192#endif
1193
1194 /* Close all script inputs. */
1195 close_all_scripts();
1196
1197#if defined(FEAT_WINDOWS)
1198 /* Destroy all windows. Must come before freeing buffers. */
1199 win_free_all();
1200#endif
1201
Bram Moolenaar2a329742008-04-01 12:53:43 +00001202 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1203 * were freed already. */
1204#ifdef FEAT_AUTOCHDIR
1205 p_acd = FALSE;
1206#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001207 for (buf = firstbuf; buf != NULL; )
1208 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001209 bufref_T bufref;
1210
1211 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001212 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001213 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001214 if (bufref_valid(&bufref))
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001215 buf = nextbuf; /* didn't work, try next one */
1216 else
1217 buf = firstbuf;
1218 }
1219
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001220#ifdef FEAT_ARABIC
1221 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001222#endif
1223
1224 /* Clear registers. */
1225 clear_registers();
1226 ResetRedobuff();
1227 ResetRedobuff();
1228
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001229#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001230 vim_free(serverDelayedStartName);
1231#endif
1232
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001233 /* highlight info */
1234 free_highlight();
1235
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001236 reset_last_sourcing();
1237
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001238#ifdef FEAT_WINDOWS
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001239 free_tabpage(first_tabpage);
1240 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001241#endif
1242
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001243# ifdef UNIX
1244 /* Machine-specific free. */
1245 mch_free_mem();
1246# endif
1247
1248 /* message history */
1249 for (;;)
1250 if (delete_first_msg() == FAIL)
1251 break;
1252
Bram Moolenaar655da312016-05-28 22:22:34 +02001253# ifdef FEAT_JOB_CHANNEL
1254 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001255# endif
Bram Moolenaar623e2632016-07-30 22:47:56 +02001256#ifdef FEAT_TIMERS
1257 timer_free_all();
1258#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001259# ifdef FEAT_EVAL
1260 /* must be after channel_free_all() with unrefs partials */
1261 eval_clear();
1262# endif
1263# ifdef FEAT_JOB_CHANNEL
1264 /* must be after eval_clear() with unrefs jobs */
1265 job_free_all();
1266# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001267
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001268 free_termoptions();
1269
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001270 /* screenlines (can't display anything now!) */
1271 free_screenlines();
1272
1273#if defined(USE_XSMP)
1274 xsmp_close();
1275#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001276#ifdef FEAT_GUI_GTK
1277 gui_mch_free_all();
1278#endif
1279 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001280
1281 vim_free(IObuff);
1282 vim_free(NameBuff);
1283}
1284#endif
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001287 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 */
1289 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001290vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 char_u *p;
1293 unsigned len;
1294
1295 len = (unsigned)STRLEN(string) + 1;
1296 p = alloc(len);
1297 if (p != NULL)
1298 mch_memmove(p, string, (size_t)len);
1299 return p;
1300}
1301
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001302/*
1303 * Copy up to "len" bytes of "string" into newly allocated memory and
1304 * terminate with a NUL.
1305 * The allocated memory always has size "len + 1", also when "string" is
1306 * shorter.
1307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001309vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 char_u *p;
1312
1313 p = alloc((unsigned)(len + 1));
1314 if (p != NULL)
1315 {
1316 STRNCPY(p, string, len);
1317 p[len] = NUL;
1318 }
1319 return p;
1320}
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322/*
1323 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1324 * by a backslash.
1325 */
1326 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001327vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001329 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330}
1331
1332/*
1333 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1334 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001335 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001338vim_strsave_escaped_ext(
1339 char_u *string,
1340 char_u *esc_chars,
1341 int cc,
1342 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
1344 char_u *p;
1345 char_u *p2;
1346 char_u *escaped_string;
1347 unsigned length;
1348#ifdef FEAT_MBYTE
1349 int l;
1350#endif
1351
1352 /*
1353 * First count the number of backslashes required.
1354 * Then allocate the memory and insert them.
1355 */
1356 length = 1; /* count the trailing NUL */
1357 for (p = string; *p; p++)
1358 {
1359#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001360 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
1362 length += l; /* count a multibyte char */
1363 p += l - 1;
1364 continue;
1365 }
1366#endif
1367 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1368 ++length; /* count a backslash */
1369 ++length; /* count an ordinary char */
1370 }
1371 escaped_string = alloc(length);
1372 if (escaped_string != NULL)
1373 {
1374 p2 = escaped_string;
1375 for (p = string; *p; p++)
1376 {
1377#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001378 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 mch_memmove(p2, p, (size_t)l);
1381 p2 += l;
1382 p += l - 1; /* skip multibyte char */
1383 continue;
1384 }
1385#endif
1386 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001387 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 *p2++ = *p;
1389 }
1390 *p2 = NUL;
1391 }
1392 return escaped_string;
1393}
1394
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001395/*
1396 * Return TRUE when 'shell' has "csh" in the tail.
1397 */
1398 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001399csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001400{
1401 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1402}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001403
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001404/*
1405 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001406 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001407 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001408 * Escape a newline, depending on the 'shell' option.
1409 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1410 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001411 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001412 * Returns the result in allocated memory, NULL if we have run out.
1413 */
1414 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001415vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001416{
1417 unsigned length;
1418 char_u *p;
1419 char_u *d;
1420 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001421 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001422 int csh_like;
1423
1424 /* Only csh and similar shells expand '!' within single quotes. For sh and
1425 * the like we must not put a backslash before it, it will be taken
1426 * literally. If do_special is set the '!' will be escaped twice.
1427 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001428 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001429
1430 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001431 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001432 for (p = string; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001433 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001434# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001435 if (!p_ssl)
1436 {
1437 if (*p == '"')
1438 ++length; /* " -> "" */
1439 }
1440 else
1441# endif
1442 if (*p == '\'')
1443 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001444 if ((*p == '\n' && (csh_like || do_newline))
1445 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001446 {
1447 ++length; /* insert backslash */
1448 if (csh_like && do_special)
1449 ++length; /* insert backslash */
1450 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001451 if (do_special && find_cmdline_var(p, &l) >= 0)
1452 {
1453 ++length; /* insert backslash */
1454 p += l - 1;
1455 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001456 }
1457
1458 /* Allocate memory for the result and fill it. */
1459 escaped_string = alloc(length);
1460 if (escaped_string != NULL)
1461 {
1462 d = escaped_string;
1463
1464 /* add opening quote */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001465# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001466 if (!p_ssl)
1467 *d++ = '"';
1468 else
1469# endif
1470 *d++ = '\'';
1471
1472 for (p = string; *p != NUL; )
1473 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001474# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001475 if (!p_ssl)
1476 {
1477 if (*p == '"')
1478 {
1479 *d++ = '"';
1480 *d++ = '"';
1481 ++p;
1482 continue;
1483 }
1484 }
1485 else
1486# endif
1487 if (*p == '\'')
1488 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001489 *d++ = '\'';
1490 *d++ = '\\';
1491 *d++ = '\'';
1492 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001493 ++p;
1494 continue;
1495 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001496 if ((*p == '\n' && (csh_like || do_newline))
1497 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001498 {
1499 *d++ = '\\';
1500 if (csh_like && do_special)
1501 *d++ = '\\';
1502 *d++ = *p++;
1503 continue;
1504 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001505 if (do_special && find_cmdline_var(p, &l) >= 0)
1506 {
1507 *d++ = '\\'; /* insert backslash */
1508 while (--l >= 0) /* copy the var */
1509 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001510 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001511 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001512
1513 MB_COPY_CHAR(p, d);
1514 }
1515
1516 /* add terminating quote and finish with a NUL */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001517# ifdef WIN32
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001518 if (!p_ssl)
1519 *d++ = '"';
1520 else
1521# endif
1522 *d++ = '\'';
1523 *d = NUL;
1524 }
1525
1526 return escaped_string;
1527}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529/*
1530 * Like vim_strsave(), but make all characters uppercase.
1531 * This uses ASCII lower-to-upper case translation, language independent.
1532 */
1533 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001534vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
1536 char_u *p1;
1537
1538 p1 = vim_strsave(string);
1539 vim_strup(p1);
1540 return p1;
1541}
1542
1543/*
1544 * Like vim_strnsave(), but make all characters uppercase.
1545 * This uses ASCII lower-to-upper case translation, language independent.
1546 */
1547 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001548vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 char_u *p1;
1551
1552 p1 = vim_strnsave(string, len);
1553 vim_strup(p1);
1554 return p1;
1555}
1556
1557/*
1558 * ASCII lower-to-upper case translation, language independent.
1559 */
1560 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001561vim_strup(
1562 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 char_u *p2;
1565 int c;
1566
1567 if (p != NULL)
1568 {
1569 p2 = p;
1570 while ((c = *p2) != NUL)
1571#ifdef EBCDIC
1572 *p2++ = isalpha(c) ? toupper(c) : c;
1573#else
1574 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1575#endif
1576 }
1577}
1578
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001579#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001580/*
1581 * Make string "s" all upper-case and return it in allocated memory.
1582 * Handles multi-byte characters as well as possible.
1583 * Returns NULL when out of memory.
1584 */
1585 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001586strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001587{
1588 char_u *p;
1589 char_u *res;
1590
1591 res = p = vim_strsave(orig);
1592
1593 if (res != NULL)
1594 while (*p != NUL)
1595 {
1596# ifdef FEAT_MBYTE
1597 int l;
1598
1599 if (enc_utf8)
1600 {
1601 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001602 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001603 char_u *s;
1604
1605 c = utf_ptr2char(p);
1606 uc = utf_toupper(c);
1607
1608 /* Reallocate string when byte count changes. This is rare,
1609 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001610 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001611 newl = utf_char2len(uc);
1612 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001613 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001614 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001615 if (s == NULL)
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001616 {
1617 vim_free(res);
1618 return NULL;
1619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001620 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001621 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001622 p = s + (p - res);
1623 vim_free(res);
1624 res = s;
1625 }
1626
1627 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001628 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001629 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001630 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001631 p += l; /* skip multi-byte character */
1632 else
1633# endif
1634 {
1635 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1636 p++;
1637 }
1638 }
1639
1640 return res;
1641}
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +01001642
1643/*
1644 * Make string "s" all lower-case and return it in allocated memory.
1645 * Handles multi-byte characters as well as possible.
1646 * Returns NULL when out of memory.
1647 */
1648 char_u *
1649strlow_save(char_u *orig)
1650{
1651 char_u *p;
1652 char_u *res;
1653
1654 res = p = vim_strsave(orig);
1655
1656 if (res != NULL)
1657 while (*p != NUL)
1658 {
1659# ifdef FEAT_MBYTE
1660 int l;
1661
1662 if (enc_utf8)
1663 {
1664 int c, lc;
1665 int newl;
1666 char_u *s;
1667
1668 c = utf_ptr2char(p);
1669 lc = utf_tolower(c);
1670
1671 /* Reallocate string when byte count changes. This is rare,
1672 * thus it's OK to do another malloc()/free(). */
1673 l = utf_ptr2len(p);
1674 newl = utf_char2len(lc);
1675 if (newl != l)
1676 {
1677 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
1678 if (s == NULL)
1679 {
1680 vim_free(res);
1681 return NULL;
1682 }
1683 mch_memmove(s, res, p - res);
1684 STRCPY(s + (p - res) + newl, p + l);
1685 p = s + (p - res);
1686 vim_free(res);
1687 res = s;
1688 }
1689
1690 utf_char2bytes(lc, p);
1691 p += newl;
1692 }
1693 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
1694 p += l; /* skip multi-byte character */
1695 else
1696# endif
1697 {
1698 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
1699 p++;
1700 }
1701 }
1702
1703 return res;
1704}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001705#endif
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 * delete spaces at the end of a string
1709 */
1710 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001711del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 char_u *q;
1714
1715 q = ptr + STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001716 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 *q = NUL;
1718}
1719
1720/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001721 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001722 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 */
1724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001725vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001727 STRNCPY(to, from, len);
1728 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729}
1730
1731/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001732 * Like strcat(), but make sure the result fits in "tosize" bytes and is
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001733 * always NUL terminated. "from" and "to" may overlap.
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001734 */
1735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001736vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001737{
1738 size_t tolen = STRLEN(to);
1739 size_t fromlen = STRLEN(from);
1740
1741 if (tolen + fromlen + 1 > tosize)
1742 {
1743 mch_memmove(to + tolen, from, tosize - tolen - 1);
1744 to[tosize - 1] = NUL;
1745 }
1746 else
Bram Moolenaar45600ce2017-01-27 21:54:07 +01001747 mch_memmove(to + tolen, from, fromlen + 1);
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001748}
1749
1750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * Isolate one part of a string option where parts are separated with
1752 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001753 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 * "*option" is advanced to the next part.
1755 * The length is returned.
1756 */
1757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001758copy_option_part(
1759 char_u **option,
1760 char_u *buf,
1761 int maxlen,
1762 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 int len = 0;
1765 char_u *p = *option;
1766
1767 /* skip '.' at start of option part, for 'suffixes' */
1768 if (*p == '.')
1769 buf[len++] = *p++;
1770 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1771 {
1772 /*
1773 * Skip backslash before a separator character and space.
1774 */
1775 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1776 ++p;
1777 if (len < maxlen - 1)
1778 buf[len++] = *p;
1779 ++p;
1780 }
1781 buf[len] = NUL;
1782
1783 if (*p != NUL && *p != ',') /* skip non-standard separator */
1784 ++p;
1785 p = skip_to_option_part(p); /* p points to next file name */
1786
1787 *option = p;
1788 return len;
1789}
1790
1791/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001792 * Replacement for free() that ignores NULL pointers.
1793 * Also skip free() when exiting for sure, this helps when we caught a deadly
1794 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 */
1796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001799 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
1801#ifdef MEM_PROFILE
1802 mem_pre_free(&x);
1803#endif
1804 free(x);
1805 }
1806}
1807
1808#ifndef HAVE_MEMSET
1809 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001810vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 char *p = ptr;
1813
1814 while (size-- > 0)
1815 *p++ = c;
1816 return ptr;
1817}
1818#endif
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1821/*
1822 * Compare two strings, ignoring case, using current locale.
1823 * Doesn't work for multi-byte characters.
1824 * return 0 for match, < 0 for smaller, > 0 for bigger
1825 */
1826 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001827vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 int i;
1830
1831 for (;;)
1832 {
1833 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1834 if (i != 0)
1835 return i; /* this character different */
1836 if (*s1 == NUL)
1837 break; /* strings match until NUL */
1838 ++s1;
1839 ++s2;
1840 }
1841 return 0; /* strings match */
1842}
1843#endif
1844
1845#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1846/*
1847 * Compare two strings, for length "len", ignoring case, using current locale.
1848 * Doesn't work for multi-byte characters.
1849 * return 0 for match, < 0 for smaller, > 0 for bigger
1850 */
1851 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001852vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 int i;
1855
1856 while (len > 0)
1857 {
1858 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1859 if (i != 0)
1860 return i; /* this character different */
1861 if (*s1 == NUL)
1862 break; /* strings match until NUL */
1863 ++s1;
1864 ++s2;
1865 --len;
1866 }
1867 return 0; /* strings match */
1868}
1869#endif
1870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871/*
1872 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001873 * with characters from 128 to 255 correctly. It also doesn't return a
1874 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 */
1876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001877vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
1879 char_u *p;
1880 int b;
1881
1882 p = string;
1883#ifdef FEAT_MBYTE
1884 if (enc_utf8 && c >= 0x80)
1885 {
1886 while (*p != NUL)
1887 {
Bram Moolenaarace95982017-03-29 17:30:27 +02001888 int l = utfc_ptr2len(p);
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001889
1890 /* Avoid matching an illegal byte here. */
1891 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001893 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895 return NULL;
1896 }
1897 if (enc_dbcs != 0 && c > 255)
1898 {
1899 int n2 = c & 0xff;
1900
1901 c = ((unsigned)c >> 8) & 0xff;
1902 while ((b = *p) != NUL)
1903 {
1904 if (b == c && p[1] == n2)
1905 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001906 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 return NULL;
1909 }
1910 if (has_mbyte)
1911 {
1912 while ((b = *p) != NUL)
1913 {
1914 if (b == c)
1915 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001916 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
1918 return NULL;
1919 }
1920#endif
1921 while ((b = *p) != NUL)
1922 {
1923 if (b == c)
1924 return p;
1925 ++p;
1926 }
1927 return NULL;
1928}
1929
1930/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001931 * Version of strchr() that only works for bytes and handles unsigned char
1932 * strings with characters above 128 correctly. It also doesn't return a
1933 * pointer to the NUL at the end of the string.
1934 */
1935 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001936vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001937{
1938 char_u *p = string;
1939
1940 while (*p != NUL)
1941 {
1942 if (*p == c)
1943 return p;
1944 ++p;
1945 }
1946 return NULL;
1947}
1948
1949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001951 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001952 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 */
1954 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001955vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
1957 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001958 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
Bram Moolenaar05159a02005-02-26 23:04:13 +00001960 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001962 if (*p == c)
1963 retval = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001964 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966 return retval;
1967}
1968
1969/*
1970 * Vim's version of strpbrk(), in case it's missing.
1971 * Don't generate a prototype for this, causes problems when it's not used.
1972 */
1973#ifndef PROTO
1974# ifndef HAVE_STRPBRK
1975# ifdef vim_strpbrk
1976# undef vim_strpbrk
1977# endif
1978 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001979vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 while (*s)
1982 {
1983 if (vim_strchr(charset, *s) != NULL)
1984 return s;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001985 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 return NULL;
1988}
1989# endif
1990#endif
1991
1992/*
1993 * Vim has its own isspace() function, because on some machines isspace()
1994 * can't handle characters above 128.
1995 */
1996 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001997vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999 return ((x >= 9 && x <= 13) || x == ' ');
2000}
2001
2002/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002003 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
2005
2006/*
2007 * Clear an allocated growing array.
2008 */
2009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002010ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 vim_free(gap->ga_data);
2013 ga_init(gap);
2014}
2015
2016/*
2017 * Clear a growing array that contains a list of strings.
2018 */
2019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002020ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
2022 int i;
2023
2024 for (i = 0; i < gap->ga_len; ++i)
2025 vim_free(((char_u **)(gap->ga_data))[i]);
2026 ga_clear(gap);
2027}
2028
2029/*
2030 * Initialize a growing array. Don't forget to set ga_itemsize and
2031 * ga_growsize! Or use ga_init2().
2032 */
2033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002037 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 gap->ga_len = 0;
2039}
2040
2041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002042ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 ga_init(gap);
2045 gap->ga_itemsize = itemsize;
2046 gap->ga_growsize = growsize;
2047}
2048
2049/*
2050 * Make room in growing array "gap" for at least "n" items.
2051 * Return FAIL for failure, OK otherwise.
2052 */
2053 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002054ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002056 size_t old_len;
2057 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 char_u *pp;
2059
Bram Moolenaar86b68352004-12-27 21:59:20 +00002060 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 if (n < gap->ga_growsize)
2063 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002064 new_len = gap->ga_itemsize * (gap->ga_len + n);
2065 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002066 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (pp == NULL)
2068 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002069 old_len = gap->ga_itemsize * gap->ga_maxlen;
2070 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002071 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 gap->ga_data = pp;
2073 }
2074 return OK;
2075}
2076
2077/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002078 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002079 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002080 * Returns NULL when out of memory.
2081 */
2082 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002083ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002084{
2085 int i;
2086 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002087 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002088 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002089 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002090
2091 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002092 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002093
2094 s = alloc(len + 1);
2095 if (s != NULL)
2096 {
2097 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002098 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002099 for (i = 0; i < gap->ga_len; ++i)
2100 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002101 if (p != s)
2102 {
2103 STRCPY(p, sep);
2104 p += sep_len;
2105 }
2106 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2107 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002108 }
2109 }
2110 return s;
2111}
2112
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01002113#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002114/*
2115 * Make a copy of string "p" and add it to "gap".
2116 * When out of memory nothing changes.
2117 */
2118 void
2119ga_add_string(garray_T *gap, char_u *p)
2120{
2121 char_u *cp = vim_strsave(p);
2122
2123 if (cp != NULL)
2124 {
2125 if (ga_grow(gap, 1) == OK)
2126 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2127 else
2128 vim_free(cp);
2129 }
2130}
2131#endif
2132
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002135 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 * Note: Does NOT copy the NUL at the end!
2137 */
2138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
Bram Moolenaar43345542015-11-29 17:35:35 +01002141 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
Bram Moolenaar478af672017-04-10 22:22:42 +02002143 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01002144 return;
2145 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (ga_grow(gap, len) == OK)
2147 {
2148 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2149 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151}
2152
2153/*
2154 * Append one byte to a growarray which contains bytes.
2155 */
2156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002157ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
2159 if (ga_grow(gap, 1) == OK)
2160 {
2161 *((char *)gap->ga_data + gap->ga_len) = c;
2162 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164}
2165
Bram Moolenaar597a4222014-06-25 14:39:50 +02002166#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2167 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002168/*
2169 * Append the text in "gap" below the cursor line and clear "gap".
2170 */
2171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002172append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002173{
2174 /* Remove trailing CR. */
2175 if (gap->ga_len > 0
2176 && !curbuf->b_p_bin
2177 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2178 --gap->ga_len;
2179 ga_append(gap, NUL);
2180 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2181 gap->ga_len = 0;
2182}
2183#endif
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185/************************************************************************
2186 * functions that use lookup tables for various things, generally to do with
2187 * special key codes.
2188 */
2189
2190/*
2191 * Some useful tables.
2192 */
2193
2194static struct modmasktable
2195{
2196 short mod_mask; /* Bit-mask for particular key modifier */
2197 short mod_flag; /* Bit(s) for particular key modifier */
2198 char_u name; /* Single letter name of modifier */
2199} mod_mask_table[] =
2200{
2201 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002202 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2204 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2205 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2206 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2207 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
2208#ifdef MACOS
2209 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2210#endif
2211 /* 'A' must be the last one */
2212 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2213 {0, 0, NUL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002214 /* NOTE: when adding an entry, update MAX_KEY_NAME_LEN! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215};
2216
2217/*
2218 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002219 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 */
2221#define MOD_KEYS_ENTRY_SIZE 5
2222
2223static char_u modifier_keys_table[] =
2224{
2225/* mod mask with modifier without modifier */
2226 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2227 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2228 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2229 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2230 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2231 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2232 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2233 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2234 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2235 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2236 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2237 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2238 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2239 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2240 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2241 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2242 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2243 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2244 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2245 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2246 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2247 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2248 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2249 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2250 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2251 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2252 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2253 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2254 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2255 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2256 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2257 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2258 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2259
2260 /* vt100 F1 */
2261 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2262 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2263 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2264 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2265
2266 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2267 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2269 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2270 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2273 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2276
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2287
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2298
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2300 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2301 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2306
2307 /* TAB pseudo code*/
2308 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2309
2310 NUL
2311};
2312
2313static struct key_name_entry
2314{
2315 int key; /* Special key code or ascii value */
2316 char_u *name; /* Name of key */
2317} key_names_table[] =
2318{
2319 {' ', (char_u *)"Space"},
2320 {TAB, (char_u *)"Tab"},
2321 {K_TAB, (char_u *)"Tab"},
2322 {NL, (char_u *)"NL"},
2323 {NL, (char_u *)"NewLine"}, /* Alternative name */
2324 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2325 {NL, (char_u *)"LF"}, /* Alternative name */
2326 {CAR, (char_u *)"CR"},
2327 {CAR, (char_u *)"Return"}, /* Alternative name */
2328 {CAR, (char_u *)"Enter"}, /* Alternative name */
2329 {K_BS, (char_u *)"BS"},
2330 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2331 {ESC, (char_u *)"Esc"},
2332 {CSI, (char_u *)"CSI"},
2333 {K_CSI, (char_u *)"xCSI"},
2334 {'|', (char_u *)"Bar"},
2335 {'\\', (char_u *)"Bslash"},
2336 {K_DEL, (char_u *)"Del"},
2337 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2338 {K_KDEL, (char_u *)"kDel"},
2339 {K_UP, (char_u *)"Up"},
2340 {K_DOWN, (char_u *)"Down"},
2341 {K_LEFT, (char_u *)"Left"},
2342 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002343 {K_XUP, (char_u *)"xUp"},
2344 {K_XDOWN, (char_u *)"xDown"},
2345 {K_XLEFT, (char_u *)"xLeft"},
2346 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01002347 {K_PS, (char_u *)"PasteStart"},
2348 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 {K_F1, (char_u *)"F1"},
2351 {K_F2, (char_u *)"F2"},
2352 {K_F3, (char_u *)"F3"},
2353 {K_F4, (char_u *)"F4"},
2354 {K_F5, (char_u *)"F5"},
2355 {K_F6, (char_u *)"F6"},
2356 {K_F7, (char_u *)"F7"},
2357 {K_F8, (char_u *)"F8"},
2358 {K_F9, (char_u *)"F9"},
2359 {K_F10, (char_u *)"F10"},
2360
2361 {K_F11, (char_u *)"F11"},
2362 {K_F12, (char_u *)"F12"},
2363 {K_F13, (char_u *)"F13"},
2364 {K_F14, (char_u *)"F14"},
2365 {K_F15, (char_u *)"F15"},
2366 {K_F16, (char_u *)"F16"},
2367 {K_F17, (char_u *)"F17"},
2368 {K_F18, (char_u *)"F18"},
2369 {K_F19, (char_u *)"F19"},
2370 {K_F20, (char_u *)"F20"},
2371
2372 {K_F21, (char_u *)"F21"},
2373 {K_F22, (char_u *)"F22"},
2374 {K_F23, (char_u *)"F23"},
2375 {K_F24, (char_u *)"F24"},
2376 {K_F25, (char_u *)"F25"},
2377 {K_F26, (char_u *)"F26"},
2378 {K_F27, (char_u *)"F27"},
2379 {K_F28, (char_u *)"F28"},
2380 {K_F29, (char_u *)"F29"},
2381 {K_F30, (char_u *)"F30"},
2382
2383 {K_F31, (char_u *)"F31"},
2384 {K_F32, (char_u *)"F32"},
2385 {K_F33, (char_u *)"F33"},
2386 {K_F34, (char_u *)"F34"},
2387 {K_F35, (char_u *)"F35"},
2388 {K_F36, (char_u *)"F36"},
2389 {K_F37, (char_u *)"F37"},
2390
2391 {K_XF1, (char_u *)"xF1"},
2392 {K_XF2, (char_u *)"xF2"},
2393 {K_XF3, (char_u *)"xF3"},
2394 {K_XF4, (char_u *)"xF4"},
2395
2396 {K_HELP, (char_u *)"Help"},
2397 {K_UNDO, (char_u *)"Undo"},
2398 {K_INS, (char_u *)"Insert"},
2399 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2400 {K_KINS, (char_u *)"kInsert"},
2401 {K_HOME, (char_u *)"Home"},
2402 {K_KHOME, (char_u *)"kHome"},
2403 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002404 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {K_END, (char_u *)"End"},
2406 {K_KEND, (char_u *)"kEnd"},
2407 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002408 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {K_PAGEUP, (char_u *)"PageUp"},
2410 {K_PAGEDOWN, (char_u *)"PageDown"},
2411 {K_KPAGEUP, (char_u *)"kPageUp"},
2412 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2413
2414 {K_KPLUS, (char_u *)"kPlus"},
2415 {K_KMINUS, (char_u *)"kMinus"},
2416 {K_KDIVIDE, (char_u *)"kDivide"},
2417 {K_KMULTIPLY, (char_u *)"kMultiply"},
2418 {K_KENTER, (char_u *)"kEnter"},
2419 {K_KPOINT, (char_u *)"kPoint"},
2420
2421 {K_K0, (char_u *)"k0"},
2422 {K_K1, (char_u *)"k1"},
2423 {K_K2, (char_u *)"k2"},
2424 {K_K3, (char_u *)"k3"},
2425 {K_K4, (char_u *)"k4"},
2426 {K_K5, (char_u *)"k5"},
2427 {K_K6, (char_u *)"k6"},
2428 {K_K7, (char_u *)"k7"},
2429 {K_K8, (char_u *)"k8"},
2430 {K_K9, (char_u *)"k9"},
2431
2432 {'<', (char_u *)"lt"},
2433
2434 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002435#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002437#endif
2438#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002440#endif
2441#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002443#endif
2444#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002446#endif
2447#ifdef FEAT_MOUSE_URXVT
2448 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2449#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002450#ifdef FEAT_MOUSE_SGR
2451 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaara529ce02017-06-22 22:37:57 +02002452 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2455 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2456 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2457 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2458 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2459 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2460 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2461 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2462 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2463 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2464 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002465 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2466 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2467 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2468 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2469 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2470 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {K_X1MOUSE, (char_u *)"X1Mouse"},
2472 {K_X1DRAG, (char_u *)"X1Drag"},
2473 {K_X1RELEASE, (char_u *)"X1Release"},
2474 {K_X2MOUSE, (char_u *)"X2Mouse"},
2475 {K_X2DRAG, (char_u *)"X2Drag"},
2476 {K_X2RELEASE, (char_u *)"X2Release"},
2477 {K_DROP, (char_u *)"Drop"},
2478 {K_ZERO, (char_u *)"Nul"},
2479#ifdef FEAT_EVAL
2480 {K_SNR, (char_u *)"SNR"},
2481#endif
2482 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002483 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {0, NULL}
Bram Moolenaar423977d2017-01-22 15:05:12 +01002485 /* NOTE: When adding a long name update MAX_KEY_NAME_LEN. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486};
2487
2488#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2489
2490#ifdef FEAT_MOUSE
2491static struct mousetable
2492{
2493 int pseudo_code; /* Code for pseudo mouse event */
2494 int button; /* Which mouse button is it? */
2495 int is_click; /* Is it a mouse button click event? */
2496 int is_drag; /* Is it a mouse drag event? */
2497} mouse_table[] =
2498{
2499 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2500#ifdef FEAT_GUI
2501 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2502#endif
2503 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2504 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2505#ifdef FEAT_GUI
2506 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2507#endif
2508 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2509 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2510 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2511 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2512 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2513 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2514 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2515 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2516 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2517 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2518 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2519 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2520 /* DRAG without CLICK */
2521 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2522 /* RELEASE without CLICK */
2523 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2524 {0, 0, 0, 0},
2525};
2526#endif /* FEAT_MOUSE */
2527
2528/*
2529 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2530 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2531 */
2532 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002533name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 int i;
2536
2537 c = TOUPPER_ASC(c);
2538 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2539 if (c == mod_mask_table[i].name)
2540 return mod_mask_table[i].mod_flag;
2541 return 0;
2542}
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
2545 * Check if if there is a special key code for "key" that includes the
2546 * modifiers specified.
2547 */
2548 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002549simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 int i;
2552 int key0;
2553 int key1;
2554
2555 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2556 {
2557 /* TAB is a special case */
2558 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2559 {
2560 *modifiers &= ~MOD_MASK_SHIFT;
2561 return K_S_TAB;
2562 }
2563 key0 = KEY2TERMCAP0(key);
2564 key1 = KEY2TERMCAP1(key);
2565 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2566 if (key0 == modifier_keys_table[i + 3]
2567 && key1 == modifier_keys_table[i + 4]
2568 && (*modifiers & modifier_keys_table[i]))
2569 {
2570 *modifiers &= ~modifier_keys_table[i];
2571 return TERMCAP2KEY(modifier_keys_table[i + 1],
2572 modifier_keys_table[i + 2]);
2573 }
2574 }
2575 return key;
2576}
2577
2578/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002579 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002580 */
2581 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002582handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002583{
2584 switch (key)
2585 {
2586 case K_XUP: return K_UP;
2587 case K_XDOWN: return K_DOWN;
2588 case K_XLEFT: return K_LEFT;
2589 case K_XRIGHT: return K_RIGHT;
2590 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002591 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002592 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002593 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002594 case K_XF1: return K_F1;
2595 case K_XF2: return K_F2;
2596 case K_XF3: return K_F3;
2597 case K_XF4: return K_F4;
2598 case K_S_XF1: return K_S_F1;
2599 case K_S_XF2: return K_S_F2;
2600 case K_S_XF3: return K_S_F3;
2601 case K_S_XF4: return K_S_F4;
2602 }
2603 return key;
2604}
2605
2606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 * Return a string which contains the name of the given key when the given
2608 * modifiers are down.
2609 */
2610 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002611get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
2613 static char_u string[MAX_KEY_NAME_LEN + 1];
2614
2615 int i, idx;
2616 int table_idx;
2617 char_u *s;
2618
2619 string[0] = '<';
2620 idx = 1;
2621
2622 /* Key that stands for a normal character. */
2623 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2624 c = KEY2TERMCAP1(c);
2625
2626 /*
2627 * Translate shifted special keys into unshifted keys and set modifier.
2628 * Same for CTRL and ALT modifiers.
2629 */
2630 if (IS_SPECIAL(c))
2631 {
2632 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2633 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2634 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2635 {
2636 modifiers |= modifier_keys_table[i];
2637 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2638 modifier_keys_table[i + 4]);
2639 break;
2640 }
2641 }
2642
2643 /* try to find the key in the special key table */
2644 table_idx = find_special_key_in_table(c);
2645
2646 /*
2647 * When not a known special key, and not a printable character, try to
2648 * extract modifiers.
2649 */
2650 if (c > 0
2651#ifdef FEAT_MBYTE
2652 && (*mb_char2len)(c) == 1
2653#endif
2654 )
2655 {
2656 if (table_idx < 0
2657 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2658 && (c & 0x80))
2659 {
2660 c &= 0x7f;
2661 modifiers |= MOD_MASK_ALT;
2662 /* try again, to find the un-alted key in the special key table */
2663 table_idx = find_special_key_in_table(c);
2664 }
2665 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2666 {
2667#ifdef EBCDIC
2668 c = CtrlChar(c);
2669#else
2670 c += '@';
2671#endif
2672 modifiers |= MOD_MASK_CTRL;
2673 }
2674 }
2675
2676 /* translate the modifier into a string */
2677 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2678 if ((modifiers & mod_mask_table[i].mod_mask)
2679 == mod_mask_table[i].mod_flag)
2680 {
2681 string[idx++] = mod_mask_table[i].name;
2682 string[idx++] = (char_u)'-';
2683 }
2684
2685 if (table_idx < 0) /* unknown special key, may output t_xx */
2686 {
2687 if (IS_SPECIAL(c))
2688 {
2689 string[idx++] = 't';
2690 string[idx++] = '_';
2691 string[idx++] = KEY2TERMCAP0(c);
2692 string[idx++] = KEY2TERMCAP1(c);
2693 }
2694 /* Not a special key, only modifiers, output directly */
2695 else
2696 {
2697#ifdef FEAT_MBYTE
2698 if (has_mbyte && (*mb_char2len)(c) > 1)
2699 idx += (*mb_char2bytes)(c, string + idx);
2700 else
2701#endif
2702 if (vim_isprintc(c))
2703 string[idx++] = c;
2704 else
2705 {
2706 s = transchar(c);
2707 while (*s)
2708 string[idx++] = *s++;
2709 }
2710 }
2711 }
2712 else /* use name of special key */
2713 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002714 size_t len = STRLEN(key_names_table[table_idx].name);
2715
2716 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2717 {
2718 STRCPY(string + idx, key_names_table[table_idx].name);
2719 idx += (int)len;
2720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 string[idx++] = '>';
2723 string[idx] = NUL;
2724 return string;
2725}
2726
2727/*
2728 * Try translating a <> name at (*srcp)[] to dst[].
2729 * Return the number of characters added to dst[], zero for no match.
2730 * If there is a match, srcp is advanced to after the <> name.
2731 * dst[] must be big enough to hold the result (up to six characters)!
2732 */
2733 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002734trans_special(
2735 char_u **srcp,
2736 char_u *dst,
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002737 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2738 int in_string) /* TRUE when inside a double quoted string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 int modifiers = 0;
2741 int key;
2742 int dlen = 0;
2743
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002744 key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (key == 0)
2746 return 0;
2747
2748 /* Put the appropriate modifier in a string */
2749 if (modifiers != 0)
2750 {
2751 dst[dlen++] = K_SPECIAL;
2752 dst[dlen++] = KS_MODIFIER;
2753 dst[dlen++] = modifiers;
2754 }
2755
2756 if (IS_SPECIAL(key))
2757 {
2758 dst[dlen++] = K_SPECIAL;
2759 dst[dlen++] = KEY2TERMCAP0(key);
2760 dst[dlen++] = KEY2TERMCAP1(key);
2761 }
2762#ifdef FEAT_MBYTE
2763 else if (has_mbyte && !keycode)
2764 dlen += (*mb_char2bytes)(key, dst + dlen);
2765#endif
2766 else if (keycode)
2767 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2768 else
2769 dst[dlen++] = key;
2770
2771 return dlen;
2772}
2773
2774/*
2775 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2776 * srcp is advanced to after the <> name.
2777 * returns 0 if there is no match.
2778 */
2779 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002780find_special_key(
2781 char_u **srcp,
2782 int *modp,
2783 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002784 int keep_x_key, /* don't translate xHome to Home key */
2785 int in_string) /* TRUE in string, double quote is escaped */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787 char_u *last_dash;
2788 char_u *end_of_name;
2789 char_u *src;
2790 char_u *bp;
2791 int modifiers;
2792 int bit;
2793 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002794 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002795 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797 src = *srcp;
2798 if (src[0] != '<')
2799 return 0;
2800
2801 /* Find end of modifier list */
2802 last_dash = src;
2803 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2804 {
2805 if (*bp == '-')
2806 {
2807 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002808 if (bp[1] != NUL)
2809 {
2810#ifdef FEAT_MBYTE
2811 if (has_mbyte)
2812 l = mb_ptr2len(bp + 1);
2813 else
2814#endif
2815 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002816 /* Anything accepted, like <C-?>.
2817 * <C-"> or <M-"> are not special in strings as " is
2818 * the string delimiter. With a backslash it works: <M-\"> */
2819 if (!(in_string && bp[1] == '"') && bp[2] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002820 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002821 else if (in_string && bp[1] == '\\' && bp[2] == '"'
2822 && bp[3] == '>')
2823 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2827 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002828 else if (STRNICMP(bp, "char-", 5) == 0)
2829 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002830 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002831 bp += l + 5;
2832 break;
2833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
2835
2836 if (*bp == '>') /* found matching '>' */
2837 {
2838 end_of_name = bp + 1;
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 /* Which modifiers are given? */
2841 modifiers = 0x0;
2842 for (bp = src + 1; bp < last_dash; bp++)
2843 {
2844 if (*bp != '-')
2845 {
2846 bit = name_to_mod_mask(*bp);
2847 if (bit == 0x0)
2848 break; /* Illegal modifier name */
2849 modifiers |= bit;
2850 }
2851 }
2852
2853 /*
2854 * Legal modifier name.
2855 */
2856 if (bp >= last_dash)
2857 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002858 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2859 && VIM_ISDIGIT(last_dash[6]))
2860 {
2861 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002862 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002863 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002866 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002867 int off = 1;
2868
2869 /* Modifier with single letter, or special key name. */
2870 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2871 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002872#ifdef FEAT_MBYTE
2873 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002874 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002875 else
2876#endif
2877 l = 1;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002878 if (modifiers != 0 && last_dash[l + off] == '>')
2879 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002880 else
2881 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002882 key = get_special_key_code(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002883 if (!keep_x_key)
2884 key = handle_x_keys(key);
2885 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
2888 /*
2889 * get_special_key_code() may return NUL for invalid
2890 * special key name.
2891 */
2892 if (key != NUL)
2893 {
2894 /*
2895 * Only use a modifier when there is no special key code that
2896 * includes the modifier.
2897 */
2898 key = simplify_key(key, &modifiers);
2899
2900 if (!keycode)
2901 {
2902 /* don't want keycode, use single byte code */
2903 if (key == K_BS)
2904 key = BS;
2905 else if (key == K_DEL || key == K_KDEL)
2906 key = DEL;
2907 }
2908
2909 /*
2910 * Normal Key with modifier: Try to make a single byte code.
2911 */
2912 if (!IS_SPECIAL(key))
2913 key = extract_modifiers(key, &modifiers);
2914
2915 *modp = modifiers;
2916 *srcp = end_of_name;
2917 return key;
2918 }
2919 }
2920 }
2921 return 0;
2922}
2923
2924/*
2925 * Try to include modifiers in the key.
2926 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2927 */
2928 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002929extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 int modifiers = *modp;
2932
2933#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002934 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (!(modifiers & MOD_MASK_CMD))
2936#endif
2937 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2938 {
2939 key = TOUPPER_ASC(key);
2940 modifiers &= ~MOD_MASK_SHIFT;
2941 }
2942 if ((modifiers & MOD_MASK_CTRL)
2943#ifdef EBCDIC
2944 /* * TODO: EBCDIC Better use:
2945 * && (Ctrl_chr(key) || key == '?')
2946 * ??? */
2947 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2948 != NULL
2949#else
2950 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2951#endif
2952 )
2953 {
2954 key = Ctrl_chr(key);
2955 modifiers &= ~MOD_MASK_CTRL;
2956 /* <C-@> is <Nul> */
2957 if (key == 0)
2958 key = K_ZERO;
2959 }
2960#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002961 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if (!(modifiers & MOD_MASK_CMD))
2963#endif
2964 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2965#ifdef FEAT_MBYTE
2966 && !enc_dbcs /* avoid creating a lead byte */
2967#endif
2968 )
2969 {
2970 key |= 0x80;
2971 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2972 }
2973
2974 *modp = modifiers;
2975 return key;
2976}
2977
2978/*
2979 * Try to find key "c" in the special key table.
2980 * Return the index when found, -1 when not found.
2981 */
2982 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002983find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 int i;
2986
2987 for (i = 0; key_names_table[i].name != NULL; i++)
2988 if (c == key_names_table[i].key)
2989 break;
2990 if (key_names_table[i].name == NULL)
2991 i = -1;
2992 return i;
2993}
2994
2995/*
2996 * Find the special key with the given name (the given string does not have to
2997 * end with NUL, the name is assumed to end before the first non-idchar).
2998 * If the name starts with "t_" the next two characters are interpreted as a
2999 * termcap name.
3000 * Return the key code, or 0 if not found.
3001 */
3002 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003003get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 char_u *table_name;
3006 char_u string[3];
3007 int i, j;
3008
3009 /*
3010 * If it's <t_xx> we get the code for xx from the termcap
3011 */
3012 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3013 {
3014 string[0] = name[2];
3015 string[1] = name[3];
3016 string[2] = NUL;
3017 if (add_termcap_entry(string, FALSE) == OK)
3018 return TERMCAP2KEY(name[2], name[3]);
3019 }
3020 else
3021 for (i = 0; key_names_table[i].name != NULL; i++)
3022 {
3023 table_name = key_names_table[i].name;
3024 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3025 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3026 break;
3027 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3028 return key_names_table[i].key;
3029 }
3030 return 0;
3031}
3032
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003033#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003035get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003037 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 return NULL;
3039 return key_names_table[i].name;
3040}
3041#endif
3042
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003043#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * Look up the given mouse code to return the relevant information in the other
3046 * arguments. Return which button is down or was released.
3047 */
3048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003049get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 int i;
3052
3053 for (i = 0; mouse_table[i].pseudo_code; i++)
3054 if (code == mouse_table[i].pseudo_code)
3055 {
3056 *is_click = mouse_table[i].is_click;
3057 *is_drag = mouse_table[i].is_drag;
3058 return mouse_table[i].button;
3059 }
3060 return 0; /* Shouldn't get here */
3061}
3062
3063/*
3064 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3065 * the given information about which mouse button is down, and whether the
3066 * mouse was clicked, dragged or released.
3067 */
3068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003069get_pseudo_mouse_code(
3070 int button, /* eg MOUSE_LEFT */
3071 int is_click,
3072 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 int i;
3075
3076 for (i = 0; mouse_table[i].pseudo_code; i++)
3077 if (button == mouse_table[i].button
3078 && is_click == mouse_table[i].is_click
3079 && is_drag == mouse_table[i].is_drag)
3080 {
3081#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003082 /* Trick: a non mappable left click and release has mouse_col -1
3083 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3084 * gui_mouse_moved() */
3085 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003087 if (mouse_col < 0)
3088 mouse_col = 0;
3089 else
3090 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3092 return (int)KE_LEFTMOUSE_NM;
3093 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3094 return (int)KE_LEFTRELEASE_NM;
3095 }
3096#endif
3097 return mouse_table[i].pseudo_code;
3098 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003099 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100}
3101#endif /* FEAT_MOUSE */
3102
3103/*
3104 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3105 */
3106 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003107get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 int c = *buf->b_p_ff;
3110
3111 if (buf->b_p_bin || c == 'u')
3112 return EOL_UNIX;
3113 if (c == 'm')
3114 return EOL_MAC;
3115 return EOL_DOS;
3116}
3117
3118/*
3119 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3120 * argument.
3121 */
3122 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003123get_fileformat_force(
3124 buf_T *buf,
3125 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 int c;
3128
3129 if (eap != NULL && eap->force_ff != 0)
3130 c = eap->cmd[eap->force_ff];
3131 else
3132 {
3133 if ((eap != NULL && eap->force_bin != 0)
3134 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3135 return EOL_UNIX;
3136 c = *buf->b_p_ff;
3137 }
3138 if (c == 'u')
3139 return EOL_UNIX;
3140 if (c == 'm')
3141 return EOL_MAC;
3142 return EOL_DOS;
3143}
3144
3145/*
3146 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3147 * Sets both 'textmode' and 'fileformat'.
3148 * Note: Does _not_ set global value of 'textmode'!
3149 */
3150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003151set_fileformat(
3152 int t,
3153 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 char *p = NULL;
3156
3157 switch (t)
3158 {
3159 case EOL_DOS:
3160 p = FF_DOS;
3161 curbuf->b_p_tx = TRUE;
3162 break;
3163 case EOL_UNIX:
3164 p = FF_UNIX;
3165 curbuf->b_p_tx = FALSE;
3166 break;
3167 case EOL_MAC:
3168 p = FF_MAC;
3169 curbuf->b_p_tx = FALSE;
3170 break;
3171 }
3172 if (p != NULL)
3173 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003174 OPT_FREE | opt_flags, 0);
3175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00003177 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003179 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#endif
3181#ifdef FEAT_TITLE
3182 need_maketitle = TRUE; /* set window title later */
3183#endif
3184}
3185
3186/*
3187 * Return the default fileformat from 'fileformats'.
3188 */
3189 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003190default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 switch (*p_ffs)
3193 {
3194 case 'm': return EOL_MAC;
3195 case 'd': return EOL_DOS;
3196 }
3197 return EOL_UNIX;
3198}
3199
3200/*
3201 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3202 */
3203 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003204call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
3206 char_u *ncmd;
3207 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003208#ifdef FEAT_PROFILE
3209 proftime_T wait_time;
3210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
3212 if (p_verbose > 3)
3213 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003214 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003215 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 cmd == NULL ? p_sh : cmd);
3217 out_char('\n');
3218 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003219 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
3221
Bram Moolenaar05159a02005-02-26 23:04:13 +00003222#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003223 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003224 prof_child_enter(&wait_time);
3225#endif
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (*p_sh == NUL)
3228 {
3229 EMSG(_(e_shellempty));
3230 retval = -1;
3231 }
3232 else
3233 {
3234#ifdef FEAT_GUI_MSWIN
3235 /* Don't hide the pointer while executing a shell command. */
3236 gui_mch_mousehide(FALSE);
3237#endif
3238#ifdef FEAT_GUI
3239 ++hold_gui_events;
3240#endif
3241 /* The external command may update a tags file, clear cached tags. */
3242 tag_freematch();
3243
3244 if (cmd == NULL || *p_sxq == NUL)
3245 retval = mch_call_shell(cmd, opt);
3246 else
3247 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003248 char_u *ecmd = cmd;
3249
3250 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3251 {
3252 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3253 if (ecmd == NULL)
3254 ecmd = cmd;
3255 }
3256 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (ncmd != NULL)
3258 {
3259 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003260 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003261 /* When 'shellxquote' is ( append ).
3262 * When 'shellxquote' is "( append )". */
3263 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3264 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3265 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 retval = mch_call_shell(ncmd, opt);
3267 vim_free(ncmd);
3268 }
3269 else
3270 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003271 if (ecmd != cmd)
3272 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274#ifdef FEAT_GUI
3275 --hold_gui_events;
3276#endif
3277 /*
3278 * Check the window size, in case it changed while executing the
3279 * external command.
3280 */
3281 shell_resized_check();
3282 }
3283
3284#ifdef FEAT_EVAL
3285 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003286# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003287 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003288 prof_child_exit(&wait_time);
3289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#endif
3291
3292 return retval;
3293}
3294
3295/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003296 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3297 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
3299 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003300get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 if (State & NORMAL)
3303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003305 {
3306 if (VIsual_select)
3307 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003309 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003310 else if (finish_op)
3311 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 return State;
3314}
3315
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003316#if defined(FEAT_MBYTE) || defined(PROTO)
3317/*
3318 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003319 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003320 * "b" must point to the start of the file name
3321 */
3322 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003323after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003324{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003325 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003326 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3327}
3328#endif
3329
3330/*
3331 * Return TRUE if file names "f1" and "f2" are in the same directory.
3332 * "f1" may be a short name, "f2" must be a full path.
3333 */
3334 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003335same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003336{
3337 char_u ffname[MAXPATHL];
3338 char_u *t1;
3339 char_u *t2;
3340
3341 /* safety check */
3342 if (f1 == NULL || f2 == NULL)
3343 return FALSE;
3344
3345 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3346 t1 = gettail_sep(ffname);
3347 t2 = gettail_sep(f2);
3348 return (t1 - ffname == t2 - f2
3349 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3350}
3351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00003353 || ((defined(FEAT_GUI_GTK)) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00003354 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3356 || defined(PROTO)
3357/*
3358 * Change to a file's directory.
3359 * Caller must call shorten_fnames()!
3360 * Return OK or FAIL.
3361 */
3362 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003363vim_chdirfile(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003365 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003367 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003368 *gettail_sep(dir) = NUL;
3369 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370}
3371#endif
3372
3373#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3374/*
3375 * Check if "name" ends in a slash and is not a directory.
3376 * Used for systems where stat() ignores a trailing slash on a file name.
3377 * The Vim code assumes a trailing slash is only ignored for a directory.
3378 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003379 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01003380illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 if (name[0] == NUL)
3383 return FALSE; /* no file name is not illegal */
3384 if (name[strlen(name) - 1] != '/')
3385 return FALSE; /* no trailing slash */
3386 if (mch_isdir((char_u *)name))
3387 return FALSE; /* trailing slash for a directory */
3388 return TRUE;
3389}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003390
3391/*
3392 * Special implementation of mch_stat() for Solaris.
3393 */
3394 int
3395vim_stat(const char *name, stat_T *stp)
3396{
3397 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
3398 * the name ends in "/" and it's not a directory. */
Bram Moolenaard8492792017-03-16 12:22:38 +01003399 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003400}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#endif
3402
3403#if defined(CURSOR_SHAPE) || defined(PROTO)
3404
3405/*
3406 * Handling of cursor and mouse pointer shapes in various modes.
3407 */
3408
3409cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3410{
3411 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3412 * defaults when Vim starts.
3413 * Adjust the SHAPE_IDX_ defines when making changes! */
3414 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3415 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3416 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3417 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3418 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3419 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3420 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3421 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3422 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3423 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3424 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3425 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3426 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3427 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3428 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3429 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3430 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3431};
3432
3433#ifdef FEAT_MOUSESHAPE
3434/*
3435 * Table with names for mouse shapes. Keep in sync with all the tables for
3436 * mch_set_mouse_shape()!.
3437 */
3438static char * mshape_names[] =
3439{
3440 "arrow", /* default, must be the first one */
3441 "blank", /* hidden */
3442 "beam",
3443 "updown",
3444 "udsizing",
3445 "leftright",
3446 "lrsizing",
3447 "busy",
3448 "no",
3449 "crosshair",
3450 "hand1",
3451 "hand2",
3452 "pencil",
3453 "question",
3454 "rightup-arrow",
3455 "up-arrow",
3456 NULL
3457};
3458#endif
3459
3460/*
3461 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3462 * ("what" is SHAPE_MOUSE).
3463 * Returns error message for an illegal option, NULL otherwise.
3464 */
3465 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003466parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 char_u *modep;
3469 char_u *colonp;
3470 char_u *commap;
3471 char_u *slashp;
3472 char_u *p, *endp;
3473 int idx = 0; /* init for GCC */
3474 int all_idx;
3475 int len;
3476 int i;
3477 long n;
3478 int found_ve = FALSE; /* found "ve" flag */
3479 int round;
3480
3481 /*
3482 * First round: check for errors; second round: do it for real.
3483 */
3484 for (round = 1; round <= 2; ++round)
3485 {
3486 /*
3487 * Repeat for all comma separated parts.
3488 */
3489#ifdef FEAT_MOUSESHAPE
3490 if (what == SHAPE_MOUSE)
3491 modep = p_mouseshape;
3492 else
3493#endif
3494 modep = p_guicursor;
3495 while (*modep != NUL)
3496 {
3497 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01003498 commap = vim_strchr(modep, ',');
3499
3500 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 return (char_u *)N_("E545: Missing colon");
3502 if (colonp == modep)
3503 return (char_u *)N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 /*
3506 * Repeat for all mode's before the colon.
3507 * For the 'a' mode, we loop to handle all the modes.
3508 */
3509 all_idx = -1;
3510 while (modep < colonp || all_idx >= 0)
3511 {
3512 if (all_idx < 0)
3513 {
3514 /* Find the mode. */
3515 if (modep[1] == '-' || modep[1] == ':')
3516 len = 1;
3517 else
3518 len = 2;
3519 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3520 all_idx = SHAPE_IDX_COUNT - 1;
3521 else
3522 {
3523 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3524 if (STRNICMP(modep, shape_table[idx].name, len)
3525 == 0)
3526 break;
3527 if (idx == SHAPE_IDX_COUNT
3528 || (shape_table[idx].used_for & what) == 0)
3529 return (char_u *)N_("E546: Illegal mode");
3530 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3531 found_ve = TRUE;
3532 }
3533 modep += len + 1;
3534 }
3535
3536 if (all_idx >= 0)
3537 idx = all_idx--;
3538 else if (round == 2)
3539 {
3540#ifdef FEAT_MOUSESHAPE
3541 if (what == SHAPE_MOUSE)
3542 {
3543 /* Set the default, for the missing parts */
3544 shape_table[idx].mshape = 0;
3545 }
3546 else
3547#endif
3548 {
3549 /* Set the defaults, for the missing parts */
3550 shape_table[idx].shape = SHAPE_BLOCK;
3551 shape_table[idx].blinkwait = 700L;
3552 shape_table[idx].blinkon = 400L;
3553 shape_table[idx].blinkoff = 250L;
3554 }
3555 }
3556
3557 /* Parse the part after the colon */
3558 for (p = colonp + 1; *p && *p != ','; )
3559 {
3560#ifdef FEAT_MOUSESHAPE
3561 if (what == SHAPE_MOUSE)
3562 {
3563 for (i = 0; ; ++i)
3564 {
3565 if (mshape_names[i] == NULL)
3566 {
3567 if (!VIM_ISDIGIT(*p))
3568 return (char_u *)N_("E547: Illegal mouseshape");
3569 if (round == 2)
3570 shape_table[idx].mshape =
3571 getdigits(&p) + MSHAPE_NUMBERED;
3572 else
3573 (void)getdigits(&p);
3574 break;
3575 }
3576 len = (int)STRLEN(mshape_names[i]);
3577 if (STRNICMP(p, mshape_names[i], len) == 0)
3578 {
3579 if (round == 2)
3580 shape_table[idx].mshape = i;
3581 p += len;
3582 break;
3583 }
3584 }
3585 }
3586 else /* if (what == SHAPE_MOUSE) */
3587#endif
3588 {
3589 /*
3590 * First handle the ones with a number argument.
3591 */
3592 i = *p;
3593 len = 0;
3594 if (STRNICMP(p, "ver", 3) == 0)
3595 len = 3;
3596 else if (STRNICMP(p, "hor", 3) == 0)
3597 len = 3;
3598 else if (STRNICMP(p, "blinkwait", 9) == 0)
3599 len = 9;
3600 else if (STRNICMP(p, "blinkon", 7) == 0)
3601 len = 7;
3602 else if (STRNICMP(p, "blinkoff", 8) == 0)
3603 len = 8;
3604 if (len != 0)
3605 {
3606 p += len;
3607 if (!VIM_ISDIGIT(*p))
3608 return (char_u *)N_("E548: digit expected");
3609 n = getdigits(&p);
3610 if (len == 3) /* "ver" or "hor" */
3611 {
3612 if (n == 0)
3613 return (char_u *)N_("E549: Illegal percentage");
3614 if (round == 2)
3615 {
3616 if (TOLOWER_ASC(i) == 'v')
3617 shape_table[idx].shape = SHAPE_VER;
3618 else
3619 shape_table[idx].shape = SHAPE_HOR;
3620 shape_table[idx].percentage = n;
3621 }
3622 }
3623 else if (round == 2)
3624 {
3625 if (len == 9)
3626 shape_table[idx].blinkwait = n;
3627 else if (len == 7)
3628 shape_table[idx].blinkon = n;
3629 else
3630 shape_table[idx].blinkoff = n;
3631 }
3632 }
3633 else if (STRNICMP(p, "block", 5) == 0)
3634 {
3635 if (round == 2)
3636 shape_table[idx].shape = SHAPE_BLOCK;
3637 p += 5;
3638 }
3639 else /* must be a highlight group name then */
3640 {
3641 endp = vim_strchr(p, '-');
3642 if (commap == NULL) /* last part */
3643 {
3644 if (endp == NULL)
3645 endp = p + STRLEN(p); /* find end of part */
3646 }
3647 else if (endp > commap || endp == NULL)
3648 endp = commap;
3649 slashp = vim_strchr(p, '/');
3650 if (slashp != NULL && slashp < endp)
3651 {
3652 /* "group/langmap_group" */
3653 i = syn_check_group(p, (int)(slashp - p));
3654 p = slashp + 1;
3655 }
3656 if (round == 2)
3657 {
3658 shape_table[idx].id = syn_check_group(p,
3659 (int)(endp - p));
3660 shape_table[idx].id_lm = shape_table[idx].id;
3661 if (slashp != NULL && slashp < endp)
3662 shape_table[idx].id = i;
3663 }
3664 p = endp;
3665 }
3666 } /* if (what != SHAPE_MOUSE) */
3667
3668 if (*p == '-')
3669 ++p;
3670 }
3671 }
3672 modep = p;
3673 if (*modep == ',')
3674 ++modep;
3675 }
3676 }
3677
3678 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3679 if (!found_ve)
3680 {
3681#ifdef FEAT_MOUSESHAPE
3682 if (what == SHAPE_MOUSE)
3683 {
3684 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3685 }
3686 else
3687#endif
3688 {
3689 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3690 shape_table[SHAPE_IDX_VE].percentage =
3691 shape_table[SHAPE_IDX_V].percentage;
3692 shape_table[SHAPE_IDX_VE].blinkwait =
3693 shape_table[SHAPE_IDX_V].blinkwait;
3694 shape_table[SHAPE_IDX_VE].blinkon =
3695 shape_table[SHAPE_IDX_V].blinkon;
3696 shape_table[SHAPE_IDX_VE].blinkoff =
3697 shape_table[SHAPE_IDX_V].blinkoff;
3698 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3699 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3700 }
3701 }
3702
3703 return NULL;
3704}
3705
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003706# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3707 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708/*
3709 * Return the index into shape_table[] for the current mode.
3710 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3711 */
3712 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003713get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
3715#ifdef FEAT_MOUSESHAPE
3716 if (mouse && (State == HITRETURN || State == ASKMORE))
3717 {
3718# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003719 int x, y;
3720 gui_mch_getmouse(&x, &y);
3721 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 return SHAPE_IDX_MOREL;
3723# endif
3724 return SHAPE_IDX_MORE;
3725 }
3726 if (mouse && drag_status_line)
3727 return SHAPE_IDX_SDRAG;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003728# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (mouse && drag_sep_line)
3730 return SHAPE_IDX_VDRAG;
3731# endif
3732#endif
3733 if (!mouse && State == SHOWMATCH)
3734 return SHAPE_IDX_SM;
3735#ifdef FEAT_VREPLACE
3736 if (State & VREPLACE_FLAG)
3737 return SHAPE_IDX_R;
3738#endif
3739 if (State & REPLACE_FLAG)
3740 return SHAPE_IDX_R;
3741 if (State & INSERT)
3742 return SHAPE_IDX_I;
3743 if (State & CMDLINE)
3744 {
3745 if (cmdline_at_end())
3746 return SHAPE_IDX_C;
3747 if (cmdline_overstrike())
3748 return SHAPE_IDX_CR;
3749 return SHAPE_IDX_CI;
3750 }
3751 if (finish_op)
3752 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (VIsual_active)
3754 {
3755 if (*p_sel == 'e')
3756 return SHAPE_IDX_VE;
3757 else
3758 return SHAPE_IDX_V;
3759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 return SHAPE_IDX_N;
3761}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3765static int old_mouse_shape = 0;
3766
3767/*
3768 * Set the mouse shape:
3769 * If "shape" is -1, use shape depending on the current mode,
3770 * depending on the current state.
3771 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3772 * when the mouse moves off the status or command line).
3773 */
3774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003775update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
3777 int new_mouse_shape;
3778
3779 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003780 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 return;
3782
3783 /* Postpone the updating when more is to come. Speeds up executing of
3784 * mappings. */
3785 if (shape_idx == -1 && char_avail())
3786 {
3787 postponed_mouseshape = TRUE;
3788 return;
3789 }
3790
Bram Moolenaar14716812006-05-04 21:54:08 +00003791 /* When ignoring the mouse don't change shape on the statusline. */
3792 if (*p_mouse == NUL
3793 && (shape_idx == SHAPE_IDX_CLINE
3794 || shape_idx == SHAPE_IDX_STATUS
3795 || shape_idx == SHAPE_IDX_VSEP))
3796 shape_idx = -2;
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (shape_idx == -2
3799 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3800 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3801 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3802 return;
3803 if (shape_idx < 0)
3804 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3805 else
3806 new_mouse_shape = shape_table[shape_idx].mshape;
3807 if (new_mouse_shape != old_mouse_shape)
3808 {
3809 mch_set_mouse_shape(new_mouse_shape);
3810 old_mouse_shape = new_mouse_shape;
3811 }
3812 postponed_mouseshape = FALSE;
3813}
3814# endif
3815
3816#endif /* CURSOR_SHAPE */
3817
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819/* TODO: make some #ifdef for this */
3820/*--------[ file searching ]-------------------------------------------------*/
3821/*
3822 * File searching functions for 'path', 'tags' and 'cdpath' options.
3823 * External visible functions:
3824 * vim_findfile_init() creates/initialises the search context
3825 * vim_findfile_free_visited() free list of visited files/dirs of search
3826 * context
3827 * vim_findfile() find a file in the search context
3828 * vim_findfile_cleanup() cleanup/free search context created by
3829 * vim_findfile_init()
3830 *
3831 * All static functions and variables start with 'ff_'
3832 *
3833 * In general it works like this:
3834 * First you create yourself a search context by calling vim_findfile_init().
3835 * It is possible to give a search context from a previous call to
3836 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3837 * until you are satisfied with the result or it returns NULL. On every call it
3838 * returns the next file which matches the conditions given to
3839 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3840 *
3841 * It is possible to call vim_findfile_init() again to reinitialise your search
3842 * with some new parameters. Don't forget to pass your old search context to
3843 * it, so it can reuse it and especially reuse the list of already visited
3844 * directories. If you want to delete the list of already visited directories
3845 * simply call vim_findfile_free_visited().
3846 *
3847 * When you are done call vim_findfile_cleanup() to free the search context.
3848 *
3849 * The function vim_findfile_init() has a long comment, which describes the
3850 * needed parameters.
3851 *
3852 *
3853 *
3854 * ATTENTION:
3855 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003856 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 * thread-safe!!!!!
3858 *
3859 * To minimize parameter passing (or because I'm to lazy), only the
3860 * external visible functions get a search context as a parameter. This is
3861 * then assigned to a static global, which is used throughout the local
3862 * functions.
3863 */
3864
3865/*
3866 * type for the directory search stack
3867 */
3868typedef struct ff_stack
3869{
3870 struct ff_stack *ffs_prev;
3871
3872 /* the fix part (no wildcards) and the part containing the wildcards
3873 * of the search path
3874 */
3875 char_u *ffs_fix_path;
3876#ifdef FEAT_PATH_EXTRA
3877 char_u *ffs_wc_path;
3878#endif
3879
3880 /* files/dirs found in the above directory, matched by the first wildcard
3881 * of wc_part
3882 */
3883 char_u **ffs_filearray;
3884 int ffs_filearray_size;
3885 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3886
3887 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003888 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 int ffs_stage;
3892
3893 /* How deep are we in the directory tree?
3894 * Counts backward from value of level parameter to vim_findfile_init
3895 */
3896 int ffs_level;
3897
3898 /* Did we already expand '**' to an empty string? */
3899 int ffs_star_star_empty;
3900} ff_stack_T;
3901
3902/*
3903 * type for already visited directories or files.
3904 */
3905typedef struct ff_visited
3906{
3907 struct ff_visited *ffv_next;
3908
3909#ifdef FEAT_PATH_EXTRA
3910 /* Visited directories are different if the wildcard string are
3911 * different. So we have to save it.
3912 */
3913 char_u *ffv_wc_path;
3914#endif
3915 /* for unix use inode etc for comparison (needed because of links), else
3916 * use filename.
3917 */
3918#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003919 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3920 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 ino_t ffv_ino; /* inode number */
3922#endif
3923 /* The memory for this struct is allocated according to the length of
3924 * ffv_fname.
3925 */
3926 char_u ffv_fname[1]; /* actually longer */
3927} ff_visited_T;
3928
3929/*
3930 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003931 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3933 * So we have to do 3 searches:
3934 * 1) search from the current files directory downward for the file "tags"
3935 * 2) search from the current files directory downward for the file "TAGS"
3936 * 3) search from Vims current directory downwards for the file "tags"
3937 * As you can see, the first and the third search are for the same file, so for
3938 * the third search we can use the visited list of the first search. For the
3939 * second search we must start from a empty visited list.
3940 * The struct ff_visited_list_hdr is used to manage a linked list of already
3941 * visited lists.
3942 */
3943typedef struct ff_visited_list_hdr
3944{
3945 struct ff_visited_list_hdr *ffvl_next;
3946
3947 /* the filename the attached visited list is for */
3948 char_u *ffvl_filename;
3949
3950 ff_visited_T *ffvl_visited_list;
3951
3952} ff_visited_list_hdr_T;
3953
3954
3955/*
3956 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003957 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 */
3959#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003960
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961/*
3962 * The search context:
3963 * ffsc_stack_ptr: the stack for the dirs to search
3964 * ffsc_visited_list: the currently active visited list
3965 * ffsc_dir_visited_list: the currently active visited list for search dirs
3966 * ffsc_visited_lists_list: the list of all visited lists
3967 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3968 * ffsc_file_to_search: the file to search for
3969 * ffsc_start_dir: the starting directory, if search path was relative
3970 * ffsc_fix_path: the fix part of the given path (without wildcards)
3971 * Needed for upward search.
3972 * ffsc_wc_path: the part of the given path containing wildcards
3973 * ffsc_level: how many levels of dirs to search downwards
3974 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003975 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003976 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 */
3978typedef struct ff_search_ctx_T
3979{
3980 ff_stack_T *ffsc_stack_ptr;
3981 ff_visited_list_hdr_T *ffsc_visited_list;
3982 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3983 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3984 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3985 char_u *ffsc_file_to_search;
3986 char_u *ffsc_start_dir;
3987 char_u *ffsc_fix_path;
3988#ifdef FEAT_PATH_EXTRA
3989 char_u *ffsc_wc_path;
3990 int ffsc_level;
3991 char_u **ffsc_stopdirs_v;
3992#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003993 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003994 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003995} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997/* locally needed functions */
3998#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003999static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004001static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004003static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
4004static void ff_free_visited_list(ff_visited_T *vl);
4005static 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 +00004006#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004007static int ff_wc_equal(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#endif
4009
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004010static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
4011static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
4012static void ff_clear(ff_search_ctx_T *search_ctx);
4013static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004015static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004017static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#endif
4019#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004020static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#endif
4022
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004023static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#if 0
4026/*
4027 * if someone likes findfirst/findnext, here are the functions
4028 * NOT TESTED!!
4029 */
4030
4031static void *ff_fn_search_context = NULL;
4032
4033 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004034vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036 ff_fn_search_context =
4037 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4038 ff_fn_search_context, rel_fname);
4039 if (NULL == ff_fn_search_context)
4040 return NULL;
4041 else
4042 return vim_findnext()
4043}
4044
4045 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004046vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
4048 char_u *ret = vim_findfile(ff_fn_search_context);
4049
4050 if (NULL == ret)
4051 {
4052 vim_findfile_cleanup(ff_fn_search_context);
4053 ff_fn_search_context = NULL;
4054 }
4055 return ret;
4056}
4057#endif
4058
4059/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004060 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004062 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 *
4064 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4065 * with the search context.
4066 *
4067 * Find the file 'filename' in the directory 'path'.
4068 * The parameter 'path' may contain wildcards. If so only search 'level'
4069 * directories deep. The parameter 'level' is the absolute maximum and is
4070 * not related to restricts given to the '**' wildcard. If 'level' is 100
4071 * and you use '**200' vim_findfile() will stop after 100 levels.
4072 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004073 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4074 * escape special characters.
4075 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4077 * restarted on the next higher directory level. This is repeated until the
4078 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4079 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4080 *
4081 * If the 'path' is relative, the starting dir for the search is either VIM's
4082 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004083 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * the first wildcard.
4085 *
4086 * Upward search is only done on the starting dir.
4087 *
4088 * If 'free_visited' is TRUE the list of already visited files/directories is
4089 * cleared. Set this to FALSE if you just want to search from another
4090 * directory, but want to be sure that no directory from a previous search is
4091 * searched again. This is useful if you search for a file at different places.
4092 * The list of visited files/dirs can also be cleared with the function
4093 * vim_findfile_free_visited().
4094 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004095 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4096 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 *
4098 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004099 * passed in the parameter "search_ctx_arg". This context is reused and
4100 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004102 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4103 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004105 * If you don't have a search context from a previous call "search_ctx_arg"
4106 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 *
4108 * This function silently ignores a few errors, vim_findfile() will have
4109 * limited functionality then.
4110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004112vim_findfile_init(
4113 char_u *path,
4114 char_u *filename,
4115 char_u *stopdirs UNUSED,
4116 int level,
4117 int free_visited,
4118 int find_what,
4119 void *search_ctx_arg,
4120 int tagfile, /* expanding names of tags files */
4121 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004124 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004126 ff_stack_T *sptr;
4127 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 /* If a search context is given by the caller, reuse it, else allocate a
4130 * new one.
4131 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004132 if (search_ctx_arg != NULL)
4133 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 else
4135 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004136 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4137 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004139 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004141 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004142 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004145 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 /* clear visited list if wanted */
4148 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004149 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 else
4151 {
4152 /* Reuse old visited lists. Get the visited list for the given
4153 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004154 * one. */
4155 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4156 &search_ctx->ffsc_visited_lists_list);
4157 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004159 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4160 &search_ctx->ffsc_dir_visited_lists_list);
4161 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 goto error_return;
4163 }
4164
4165 if (ff_expand_buffer == NULL)
4166 {
4167 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4168 if (ff_expand_buffer == NULL)
4169 goto error_return;
4170 }
4171
4172 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004173 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 if (path[0] == '.'
4175 && (vim_ispathsep(path[1]) || path[1] == NUL)
4176 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4177 && rel_fname != NULL)
4178 {
4179 int len = (int)(gettail(rel_fname) - rel_fname);
4180
4181 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4182 {
4183 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004184 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004185 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004188 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4189 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 goto error_return;
4191 if (*++path != NUL)
4192 ++path;
4193 }
4194 else if (*path == NUL || !vim_isAbsName(path))
4195 {
4196#ifdef BACKSLASH_IN_FILENAME
4197 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4198 if (*path != NUL && path[1] == ':')
4199 {
4200 char_u drive[3];
4201
4202 drive[0] = path[0];
4203 drive[1] = ':';
4204 drive[2] = NUL;
4205 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4206 goto error_return;
4207 path += 2;
4208 }
4209 else
4210#endif
4211 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4212 goto error_return;
4213
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004214 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4215 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 goto error_return;
4217
4218#ifdef BACKSLASH_IN_FILENAME
4219 /* A path that starts with "/dir" is relative to the drive, not to the
4220 * directory (but not for "//machine/dir"). Only use the drive name. */
4221 if ((*path == '/' || *path == '\\')
4222 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004223 && search_ctx->ffsc_start_dir[1] == ':')
4224 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225#endif
4226 }
4227
4228#ifdef FEAT_PATH_EXTRA
4229 /*
4230 * If stopdirs are given, split them into an array of pointers.
4231 * If this fails (mem allocation), there is no upward search at all or a
4232 * stop directory is not recognized -> continue silently.
4233 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004234 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 * is handled as unlimited upward search. See function
4236 * ff_path_in_stoplist() for details.
4237 */
4238 if (stopdirs != NULL)
4239 {
4240 char_u *walker = stopdirs;
4241 int dircount;
4242
4243 while (*walker == ';')
4244 walker++;
4245
4246 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004247 search_ctx->ffsc_stopdirs_v =
4248 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004250 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 do
4253 {
4254 char_u *helper;
4255 void *ptr;
4256
4257 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004258 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 (dircount + 1) * sizeof(char_u *));
4260 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004261 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 else
4263 /* ignore, keep what we have and continue */
4264 break;
4265 walker = vim_strchr(walker, ';');
4266 if (walker)
4267 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004268 search_ctx->ffsc_stopdirs_v[dircount-1] =
4269 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 walker++;
4271 }
4272 else
4273 /* this might be "", which means ascent till top
4274 * of directory tree.
4275 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004276 search_ctx->ffsc_stopdirs_v[dircount-1] =
4277 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 dircount++;
4280
4281 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004282 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285#endif
4286
4287#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004288 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
4290 /* split into:
4291 * -fix path
4292 * -wildcard_stuff (might be NULL)
4293 */
4294 wc_part = vim_strchr(path, '*');
4295 if (wc_part != NULL)
4296 {
4297 int llevel;
4298 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004299 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
4301 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004302 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /*
4305 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004306 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4308 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4309 * For EBCDIC you get different character values.
4310 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004311 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * string.
4313 */
4314 len = 0;
4315 while (*wc_part != NUL)
4316 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004317 if (len + 5 >= MAXPATHL)
4318 {
4319 EMSG(_(e_pathtoolong));
4320 break;
4321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (STRNCMP(wc_part, "**", 2) == 0)
4323 {
4324 ff_expand_buffer[len++] = *wc_part++;
4325 ff_expand_buffer[len++] = *wc_part++;
4326
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004327 llevel = strtol((char *)wc_part, &errpt, 10);
4328 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004330 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 /* restrict is 0 -> remove already added '**' */
4332 len -= 2;
4333 else
4334 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004335 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004336 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
4338 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4339 goto error_return;
4340 }
4341 }
4342 else
4343 ff_expand_buffer[len++] = *wc_part++;
4344 }
4345 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004346 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004348 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 goto error_return;
4350 }
4351 else
4352#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004353 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004355 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
4357 /* store the fix part as startdir.
4358 * This is needed if the parameter path is fully qualified.
4359 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004360 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004361 if (search_ctx->ffsc_start_dir == NULL)
4362 goto error_return;
4363 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365
4366 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004367 if (STRLEN(search_ctx->ffsc_start_dir)
4368 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4369 {
4370 EMSG(_(e_pathtoolong));
4371 goto error_return;
4372 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004373 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004375 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004376 int eb_len = (int)STRLEN(ff_expand_buffer);
4377 char_u *buf = alloc(eb_len
4378 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004379
4380 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004381 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004382 if (mch_isdir(buf))
4383 {
4384 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4385 add_pathsep(ff_expand_buffer);
4386 }
4387#ifdef FEAT_PATH_EXTRA
4388 else
4389 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004390 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004391 char_u *wc_path = NULL;
4392 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004393 int len = 0;
4394
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004395 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004396 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004397 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004398 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4399 add_pathsep(ff_expand_buffer);
4400 }
4401 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004402 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004403
4404 if (search_ctx->ffsc_wc_path != NULL)
4405 {
4406 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004407 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004408 + STRLEN(search_ctx->ffsc_fix_path + len)
4409 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004410 if (temp == NULL || wc_path == NULL)
4411 {
4412 vim_free(buf);
4413 vim_free(temp);
4414 vim_free(wc_path);
4415 goto error_return;
4416 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004417
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004418 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4419 STRCAT(temp, search_ctx->ffsc_wc_path);
4420 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004421 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004422 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004423 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004424 }
4425#endif
4426 vim_free(buf);
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 sptr = ff_create_stack_element(ff_expand_buffer,
4430#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004431 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432#endif
4433 level, 0);
4434
4435 if (sptr == NULL)
4436 goto error_return;
4437
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004438 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004440 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4441 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 goto error_return;
4443
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004444 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446error_return:
4447 /*
4448 * We clear the search context now!
4449 * Even when the caller gave us a (perhaps valid) context we free it here,
4450 * as we might have already destroyed it.
4451 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004452 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 return NULL;
4454}
4455
4456#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4457/*
4458 * Get the stopdir string. Check that ';' is not escaped.
4459 */
4460 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004461vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 char_u *r_ptr = buf;
4464
4465 while (*r_ptr != NUL && *r_ptr != ';')
4466 {
4467 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4468 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004469 /* Overwrite the escape char,
4470 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004471 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 r_ptr++;
4473 }
4474 r_ptr++;
4475 }
4476 if (*r_ptr == ';')
4477 {
4478 *r_ptr = 0;
4479 r_ptr++;
4480 }
4481 else if (*r_ptr == NUL)
4482 r_ptr = NULL;
4483 return r_ptr;
4484}
4485#endif
4486
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004487/*
4488 * Clean up the given search context. Can handle a NULL pointer.
4489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004491vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004493 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 return;
4495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004497 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499}
4500
4501/*
4502 * Find a file in a search context.
4503 * The search context was created with vim_findfile_init() above.
4504 * Return a pointer to an allocated file name or NULL if nothing found.
4505 * To get all matching files call this function until you get NULL.
4506 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004507 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 *
4509 * The search algorithm is depth first. To change this replace the
4510 * stack with a list (don't forget to leave partly searched directories on the
4511 * top of the list).
4512 */
4513 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004514vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
4516 char_u *file_path;
4517#ifdef FEAT_PATH_EXTRA
4518 char_u *rest_of_wildcards;
4519 char_u *path_end = NULL;
4520#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004521 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4523 int len;
4524#endif
4525 int i;
4526 char_u *p;
4527#ifdef FEAT_SEARCHPATH
4528 char_u *suf;
4529#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004530 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004532 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 return NULL;
4534
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004535 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 /*
4538 * filepath is used as buffer for various actions and as the storage to
4539 * return a found filename.
4540 */
4541 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4542 return NULL;
4543
4544#ifdef FEAT_PATH_EXTRA
4545 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004546 if (search_ctx->ffsc_start_dir != NULL)
4547 path_end = &search_ctx->ffsc_start_dir[
4548 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549#endif
4550
4551#ifdef FEAT_PATH_EXTRA
4552 /* upward search loop */
4553 for (;;)
4554 {
4555#endif
4556 /* downward search loop */
4557 for (;;)
4558 {
4559 /* check if user user wants to stop the search*/
4560 ui_breakcheck();
4561 if (got_int)
4562 break;
4563
4564 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004565 stackp = ff_pop(search_ctx);
4566 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 break;
4568
4569 /*
4570 * TODO: decide if we leave this test in
4571 *
4572 * GOOD: don't search a directory(-tree) twice.
4573 * BAD: - check linked list for every new directory entered.
4574 * - check for double files also done below
4575 *
4576 * Here we check if we already searched this directory.
4577 * We already searched a directory if:
4578 * 1) The directory is the same.
4579 * 2) We would use the same wildcard string.
4580 *
4581 * Good if you have links on same directory via several ways
4582 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4583 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4584 *
4585 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004586 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004588 if (stackp->ffs_filearray == NULL
4589 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004591 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004593 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#endif
4595 ) == FAIL)
4596 {
4597#ifdef FF_VERBOSE
4598 if (p_verbose >= 5)
4599 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004600 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004602 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 /* don't overwrite this either */
4604 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004605 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004608 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 continue;
4610 }
4611#ifdef FF_VERBOSE
4612 else if (p_verbose >= 5)
4613 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004614 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004615 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004616 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 /* don't overwrite this either */
4618 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004619 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621#endif
4622
4623 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004624 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004626 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 continue;
4628 }
4629
4630 file_path[0] = NUL;
4631
4632 /*
4633 * If no filearray till now expand wildcards
4634 * The function expand_wildcards() can handle an array of paths
4635 * and all possible expands are returned in one array. We use this
4636 * to handle the expansion of '**' into an empty string.
4637 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004638 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
4640 char_u *dirptrs[2];
4641
4642 /* we use filepath to build the path expand_wildcards() should
4643 * expand.
4644 */
4645 dirptrs[0] = file_path;
4646 dirptrs[1] = NULL;
4647
4648 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004649 if (!vim_isAbsName(stackp->ffs_fix_path)
4650 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004652 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4653 {
4654 STRCPY(file_path, search_ctx->ffsc_start_dir);
4655 add_pathsep(file_path);
4656 }
4657 else
4658 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660
4661 /* append the fix part of the search path */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004662 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4663 {
4664 STRCAT(file_path, stackp->ffs_fix_path);
4665 add_pathsep(file_path);
4666 }
4667 else
4668 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004671 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (*rest_of_wildcards != NUL)
4673 {
4674 len = (int)STRLEN(file_path);
4675 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4676 {
4677 /* pointer to the restrict byte
4678 * The restrict byte is not a character!
4679 */
4680 p = rest_of_wildcards + 2;
4681
4682 if (*p > 0)
4683 {
4684 (*p)--;
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004685 if (len + 1 < MAXPATHL)
4686 file_path[len++] = '*';
4687 else
4688 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690
4691 if (*p == 0)
4692 {
4693 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004694 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 else
4697 rest_of_wildcards += 3;
4698
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004699 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004702 stackp->ffs_star_star_empty = 1;
4703 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 }
4706
4707 /*
4708 * Here we copy until the next path separator or the end of
4709 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004710 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 * pushing every directory returned from expand_wildcards()
4712 * on the stack again for further search.
4713 */
4714 while (*rest_of_wildcards
4715 && !vim_ispathsep(*rest_of_wildcards))
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004716 if (len + 1 < MAXPATHL)
4717 file_path[len++] = *rest_of_wildcards++;
4718 else
4719 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
4721 file_path[len] = NUL;
4722 if (vim_ispathsep(*rest_of_wildcards))
4723 rest_of_wildcards++;
4724 }
4725#endif
4726
4727 /*
4728 * Expand wildcards like "*" and "$VAR".
4729 * If the path is a URL don't try this.
4730 */
4731 if (path_with_url(dirptrs[0]))
4732 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004733 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004735 if (stackp->ffs_filearray != NULL
4736 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004738 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004740 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
4742 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004743 /* Add EW_NOTWILD because the expanded path may contain
4744 * wildcard characters that are to be taken literally.
4745 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004747 &stackp->ffs_filearray_size,
4748 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004749 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004751 stackp->ffs_filearray_cur = 0;
4752 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754#ifdef FEAT_PATH_EXTRA
4755 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004756 rest_of_wildcards = &stackp->ffs_wc_path[
4757 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004760 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762 /* this is the first time we work on this directory */
4763#ifdef FEAT_PATH_EXTRA
4764 if (*rest_of_wildcards == NUL)
4765#endif
4766 {
4767 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004768 * We don't have further wildcards to expand, so we have to
4769 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004771 for (i = stackp->ffs_filearray_cur;
4772 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004774 if (!path_with_url(stackp->ffs_filearray[i])
4775 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 continue; /* not a directory */
4777
Bram Moolenaar21160b92009-11-11 15:56:10 +00004778 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * below */
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004780 if (STRLEN(stackp->ffs_filearray[i]) + 1
4781 + STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4782 {
4783 STRCPY(file_path, stackp->ffs_filearray[i]);
4784 add_pathsep(file_path);
4785 STRCAT(file_path, search_ctx->ffsc_file_to_search);
4786 }
4787 else
4788 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
4790 /*
4791 * Try without extra suffix and then with suffixes
4792 * from 'suffixesadd'.
4793 */
4794#ifdef FEAT_SEARCHPATH
4795 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004796 if (search_ctx->ffsc_tagfile)
4797 suf = (char_u *)"";
4798 else
4799 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 for (;;)
4801#endif
4802 {
4803 /* if file exists and we didn't already find it */
4804 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004805 || (mch_getperm(file_path) >= 0
4806 && (search_ctx->ffsc_find_what
4807 == FINDFILE_BOTH
4808 || ((search_ctx->ffsc_find_what
4809 == FINDFILE_DIR)
4810 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#ifndef FF_VERBOSE
4812 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004813 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 file_path
4815#ifdef FEAT_PATH_EXTRA
4816 , (char_u *)""
4817#endif
4818 ) == OK)
4819#endif
4820 )
4821 {
4822#ifdef FF_VERBOSE
4823 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004824 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 file_path
4826#ifdef FEAT_PATH_EXTRA
4827 , (char_u *)""
4828#endif
4829 ) == FAIL)
4830 {
4831 if (p_verbose >= 5)
4832 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004833 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004834 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 file_path);
4836 /* don't overwrite this either */
4837 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004838 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 continue;
4841 }
4842#endif
4843
4844 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004845 stackp->ffs_filearray_cur = i + 1;
4846 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004848 if (!path_with_url(file_path))
4849 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4851 == OK)
4852 {
4853 p = shorten_fname(file_path,
4854 ff_expand_buffer);
4855 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004856 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858#ifdef FF_VERBOSE
4859 if (p_verbose >= 5)
4860 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004861 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004862 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 /* don't overwrite this either */
4864 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004865 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867#endif
4868 return file_path;
4869 }
4870
4871#ifdef FEAT_SEARCHPATH
4872 /* Not found or found already, try next suffix. */
4873 if (*suf == NUL)
4874 break;
4875 copy_option_part(&suf, file_path + len,
4876 MAXPATHL - len, ",");
4877#endif
4878 }
4879 }
4880 }
4881#ifdef FEAT_PATH_EXTRA
4882 else
4883 {
4884 /*
4885 * still wildcards left, push the directories for further
4886 * search
4887 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004888 for (i = stackp->ffs_filearray_cur;
4889 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004891 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 continue; /* not a directory */
4893
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004894 ff_push(search_ctx,
4895 ff_create_stack_element(
4896 stackp->ffs_filearray[i],
4897 rest_of_wildcards,
4898 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 }
4901#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004902 stackp->ffs_filearray_cur = 0;
4903 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905
4906#ifdef FEAT_PATH_EXTRA
4907 /*
4908 * if wildcards contains '**' we have to descent till we reach the
4909 * leaves of the directory tree.
4910 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004911 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004913 for (i = stackp->ffs_filearray_cur;
4914 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004916 if (fnamecmp(stackp->ffs_filearray[i],
4917 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004919 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004921 ff_push(search_ctx,
4922 ff_create_stack_element(stackp->ffs_filearray[i],
4923 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925 }
4926#endif
4927
4928 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004929 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 }
4932
4933#ifdef FEAT_PATH_EXTRA
4934 /* If we reached this, we didn't find anything downwards.
4935 * Let's check if we should do an upward search.
4936 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004937 if (search_ctx->ffsc_start_dir
4938 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
4940 ff_stack_T *sptr;
4941
4942 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004943 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4944 (int)(path_end - search_ctx->ffsc_start_dir),
4945 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 break;
4947
4948 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004949 while (path_end > search_ctx->ffsc_start_dir
4950 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004952 while (path_end > search_ctx->ffsc_start_dir
4953 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 path_end--;
4955 *path_end = 0;
4956 path_end--;
4957
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004958 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 break;
4960
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004961 if (STRLEN(search_ctx->ffsc_start_dir) + 1
4962 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
4963 {
4964 STRCPY(file_path, search_ctx->ffsc_start_dir);
4965 add_pathsep(file_path);
4966 STRCAT(file_path, search_ctx->ffsc_fix_path);
4967 }
4968 else
4969 goto fail;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 /* create a new stack entry */
4972 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004973 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (sptr == NULL)
4975 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004976 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 else
4979 break;
4980 }
4981#endif
4982
Bram Moolenaar15618fa2017-03-19 21:37:13 +01004983fail:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 vim_free(file_path);
4985 return NULL;
4986}
4987
4988/*
4989 * Free the list of lists of visited files and directories
4990 * Can handle it if the passed search_context is NULL;
4991 */
4992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004993vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004995 ff_search_ctx_T *search_ctx;
4996
4997 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return;
4999
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005000 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
5001 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
5002 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003}
5004
5005 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005006vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007{
5008 ff_visited_list_hdr_T *vp;
5009
5010 while (*list_headp != NULL)
5011 {
5012 vp = (*list_headp)->ffvl_next;
5013 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5014
5015 vim_free((*list_headp)->ffvl_filename);
5016 vim_free(*list_headp);
5017 *list_headp = vp;
5018 }
5019 *list_headp = NULL;
5020}
5021
5022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005023ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 ff_visited_T *vp;
5026
5027 while (vl != NULL)
5028 {
5029 vp = vl->ffv_next;
5030#ifdef FEAT_PATH_EXTRA
5031 vim_free(vl->ffv_wc_path);
5032#endif
5033 vim_free(vl);
5034 vl = vp;
5035 }
5036 vl = NULL;
5037}
5038
5039/*
5040 * Returns the already visited list for the given filename. If none is found it
5041 * allocates a new one.
5042 */
5043 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01005044ff_get_visited_list(
5045 char_u *filename,
5046 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
5048 ff_visited_list_hdr_T *retptr = NULL;
5049
5050 /* check if a visited list for the given filename exists */
5051 if (*list_headp != NULL)
5052 {
5053 retptr = *list_headp;
5054 while (retptr != NULL)
5055 {
5056 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5057 {
5058#ifdef FF_VERBOSE
5059 if (p_verbose >= 5)
5060 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005061 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005062 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 filename);
5064 /* don't overwrite this either */
5065 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005066 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068#endif
5069 return retptr;
5070 }
5071 retptr = retptr->ffvl_next;
5072 }
5073 }
5074
5075#ifdef FF_VERBOSE
5076 if (p_verbose >= 5)
5077 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005078 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005079 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 /* don't overwrite this either */
5081 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005082 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084#endif
5085
5086 /*
5087 * if we reach this we didn't find a list and we have to allocate new list
5088 */
5089 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5090 if (retptr == NULL)
5091 return NULL;
5092
5093 retptr->ffvl_visited_list = NULL;
5094 retptr->ffvl_filename = vim_strsave(filename);
5095 if (retptr->ffvl_filename == NULL)
5096 {
5097 vim_free(retptr);
5098 return NULL;
5099 }
5100 retptr->ffvl_next = *list_headp;
5101 *list_headp = retptr;
5102
5103 return retptr;
5104}
5105
5106#ifdef FEAT_PATH_EXTRA
5107/*
5108 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5109 * They are equal if:
5110 * - both paths are NULL
5111 * - they have the same length
5112 * - char by char comparison is OK
5113 * - the only differences are in the counters behind a '**', so
5114 * '**\20' is equal to '**\24'
5115 */
5116 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005117ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005119 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005120 int c1 = NUL;
5121 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005122 int prev1 = NUL;
5123 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
5125 if (s1 == s2)
5126 return TRUE;
5127
5128 if (s1 == NULL || s2 == NULL)
5129 return FALSE;
5130
Bram Moolenaard43f0952015-08-27 22:30:47 +02005131 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005133 c1 = PTR2CHAR(s1 + i);
5134 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005135
5136 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5137 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005138 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005139 prev2 = prev1;
5140 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005141
5142 i += MB_PTR2LEN(s1 + i);
5143 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005145 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146}
5147#endif
5148
5149/*
5150 * maintains the list of already visited files and dirs
5151 * returns FAIL if the given file/dir is already in the list
5152 * returns OK if it is newly added
5153 *
5154 * TODO: What to do on memory allocation problems?
5155 * -> return TRUE - Better the file is found several times instead of
5156 * never.
5157 */
5158 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005159ff_check_visited(
5160 ff_visited_T **visited_list,
5161 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005163 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005165 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 ff_visited_T *vp;
5168#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02005169 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 int url = FALSE;
5171#endif
5172
5173 /* For an URL we only compare the name, otherwise we compare the
5174 * device/inode (unix) or the full path name (not Unix). */
5175 if (path_with_url(fname))
5176 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005177 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#ifdef UNIX
5179 url = TRUE;
5180#endif
5181 }
5182 else
5183 {
5184 ff_expand_buffer[0] = NUL;
5185#ifdef UNIX
5186 if (mch_stat((char *)fname, &st) < 0)
5187#else
5188 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5189#endif
5190 return FAIL;
5191 }
5192
5193 /* check against list of already visited files */
5194 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5195 {
5196 if (
5197#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005198 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5199 && vp->ffv_ino == st.st_ino)
5200 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
5202 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5203 )
5204 {
5205#ifdef FEAT_PATH_EXTRA
5206 /* are the wildcard parts equal */
5207 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5208#endif
5209 /* already visited */
5210 return FAIL;
5211 }
5212 }
5213
5214 /*
5215 * New file/dir. Add it to the list of visited files/dirs.
5216 */
5217 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5218 + STRLEN(ff_expand_buffer)));
5219
5220 if (vp != NULL)
5221 {
5222#ifdef UNIX
5223 if (!url)
5224 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005225 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 vp->ffv_ino = st.st_ino;
5227 vp->ffv_dev = st.st_dev;
5228 vp->ffv_fname[0] = NUL;
5229 }
5230 else
5231 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005232 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#endif
5234 STRCPY(vp->ffv_fname, ff_expand_buffer);
5235#ifdef UNIX
5236 }
5237#endif
5238#ifdef FEAT_PATH_EXTRA
5239 if (wc_path != NULL)
5240 vp->ffv_wc_path = vim_strsave(wc_path);
5241 else
5242 vp->ffv_wc_path = NULL;
5243#endif
5244
5245 vp->ffv_next = *visited_list;
5246 *visited_list = vp;
5247 }
5248
5249 return OK;
5250}
5251
5252/*
5253 * create stack element from given path pieces
5254 */
5255 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005256ff_create_stack_element(
5257 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005259 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005261 int level,
5262 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 ff_stack_T *new;
5265
5266 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5267 if (new == NULL)
5268 return NULL;
5269
5270 new->ffs_prev = NULL;
5271 new->ffs_filearray = NULL;
5272 new->ffs_filearray_size = 0;
5273 new->ffs_filearray_cur = 0;
5274 new->ffs_stage = 0;
5275 new->ffs_level = level;
Bram Moolenaar945ec092016-06-08 21:17:43 +02005276 new->ffs_star_star_empty = star_star_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /* the following saves NULL pointer checks in vim_findfile */
5279 if (fix_part == NULL)
5280 fix_part = (char_u *)"";
5281 new->ffs_fix_path = vim_strsave(fix_part);
5282
5283#ifdef FEAT_PATH_EXTRA
5284 if (wc_part == NULL)
5285 wc_part = (char_u *)"";
5286 new->ffs_wc_path = vim_strsave(wc_part);
5287#endif
5288
5289 if (new->ffs_fix_path == NULL
5290#ifdef FEAT_PATH_EXTRA
5291 || new->ffs_wc_path == NULL
5292#endif
5293 )
5294 {
5295 ff_free_stack_element(new);
5296 new = NULL;
5297 }
5298
5299 return new;
5300}
5301
5302/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005303 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 */
5305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005306ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
5308 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005309 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005310 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005312 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5313 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315}
5316
5317/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005318 * Pop a dir from the directory stack.
5319 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 */
5321 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005322ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323{
5324 ff_stack_T *sptr;
5325
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005326 sptr = search_ctx->ffsc_stack_ptr;
5327 if (search_ctx->ffsc_stack_ptr != NULL)
5328 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 return sptr;
5331}
5332
5333/*
5334 * free the given stack element
5335 */
5336 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005337ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005340 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005342 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#endif
5344
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005345 if (stack_ptr->ffs_filearray != NULL)
5346 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005348 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349}
5350
5351/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005352 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 */
5354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005355ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 ff_stack_T *sptr;
5358
5359 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005360 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 ff_free_stack_element(sptr);
5362
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005363 vim_free(search_ctx->ffsc_file_to_search);
5364 vim_free(search_ctx->ffsc_start_dir);
5365 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005367 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369
5370#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005371 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
5373 int i = 0;
5374
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005375 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005377 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 i++;
5379 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005380 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005382 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383#endif
5384
5385 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005386 search_ctx->ffsc_file_to_search = NULL;
5387 search_ctx->ffsc_start_dir = NULL;
5388 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005390 search_ctx->ffsc_wc_path = NULL;
5391 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#endif
5393}
5394
5395#ifdef FEAT_PATH_EXTRA
5396/*
5397 * check if the given path is in the stopdirs
5398 * returns TRUE if yes else FALSE
5399 */
5400 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005401ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 int i = 0;
5404
5405 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005406 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 path_len--;
5408
5409 /* if no path consider it as match */
5410 if (path_len == 0)
5411 return TRUE;
5412
5413 for (i = 0; stopdirs_v[i] != NULL; i++)
5414 {
5415 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5416 {
5417 /* match for parent directory. So '/home' also matches
5418 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5419 * '/home/r' would also match '/home/rks'
5420 */
5421 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005422 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 return TRUE;
5424 }
5425 else
5426 {
5427 if (fnamecmp(stopdirs_v[i], path) == 0)
5428 return TRUE;
5429 }
5430 }
5431 return FALSE;
5432}
5433#endif
5434
5435#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5436/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005437 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 *
5439 * On the first call set the parameter 'first' to TRUE to initialize
5440 * the search. For repeating calls to FALSE.
5441 *
5442 * Repeating calls will return other files called 'ptr[len]' from the path.
5443 *
5444 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5445 * don't need valid values.
5446 *
5447 * If nothing found on the first call the option FNAME_MESS will issue the
5448 * message:
5449 * 'Can't find file "<file>" in path'
5450 * On repeating calls:
5451 * 'No more file "<file>" found in path'
5452 *
5453 * options:
5454 * FNAME_MESS give error message when not found
5455 *
5456 * Uses NameBuff[]!
5457 *
5458 * Returns an allocated string for the file name. NULL for error.
5459 *
5460 */
5461 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005462find_file_in_path(
5463 char_u *ptr, /* file name */
5464 int len, /* length of file name */
5465 int options,
5466 int first, /* use count'th matching file name */
5467 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 return find_file_in_path_option(ptr, len, options, first,
5470 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005471 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472}
5473
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005474static char_u *ff_file_to_find = NULL;
5475static void *fdip_search_ctx = NULL;
5476
5477#if defined(EXITFREE)
5478 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005479free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005480{
5481 vim_free(ff_file_to_find);
5482 vim_findfile_cleanup(fdip_search_ctx);
5483}
5484#endif
5485
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486/*
5487 * Find the directory name "ptr[len]" in the path.
5488 *
5489 * options:
5490 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005491 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 *
5493 * Uses NameBuff[]!
5494 *
5495 * Returns an allocated string for the file name. NULL for error.
5496 */
5497 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005498find_directory_in_path(
5499 char_u *ptr, /* file name */
5500 int len, /* length of file name */
5501 int options,
5502 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005505 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506}
5507
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005508 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005509find_file_in_path_option(
5510 char_u *ptr, /* file name */
5511 int len, /* length of file name */
5512 int options,
5513 int first, /* use count'th matching file name */
5514 char_u *path_option, /* p_path or p_cdpath */
5515 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5516 char_u *rel_fname, /* file name we are looking relative to. */
5517 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 static int did_findfile_init = FALSE;
5521 char_u save_char;
5522 char_u *file_name = NULL;
5523 char_u *buf = NULL;
5524 int rel_to_curdir;
5525#ifdef AMIGA
5526 struct Process *proc = (struct Process *)FindTask(0L);
5527 APTR save_winptr = proc->pr_WindowPtr;
5528
5529 /* Avoid a requester here for a volume that doesn't exist. */
5530 proc->pr_WindowPtr = (APTR)-1L;
5531#endif
5532
5533 if (first == TRUE)
5534 {
5535 /* copy file name into NameBuff, expanding environment variables */
5536 save_char = ptr[len];
5537 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005538 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 ptr[len] = save_char;
5540
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005541 vim_free(ff_file_to_find);
5542 ff_file_to_find = vim_strsave(NameBuff);
5543 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
5545 file_name = NULL;
5546 goto theend;
5547 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005548 if (options & FNAME_UNESC)
5549 {
5550 /* Change all "\ " to " ". */
5551 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5552 if (ptr[0] == '\\' && ptr[1] == ' ')
5553 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005557 rel_to_curdir = (ff_file_to_find[0] == '.'
5558 && (ff_file_to_find[1] == NUL
5559 || vim_ispathsep(ff_file_to_find[1])
5560 || (ff_file_to_find[1] == '.'
5561 && (ff_file_to_find[2] == NUL
5562 || vim_ispathsep(ff_file_to_find[2])))));
5563 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 /* "..", "../path", "." and "./path": don't use the path_option */
5565 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005566#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005568 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005569 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005570 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571#endif
5572#ifdef AMIGA
5573 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005574 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575#endif
5576 )
5577 {
5578 /*
5579 * Absolute path, no need to use "path_option".
5580 * If this is not a first call, return NULL. We already returned a
5581 * filename on the first call.
5582 */
5583 if (first == TRUE)
5584 {
5585 int l;
5586 int run;
5587
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005588 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005590 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 goto theend;
5592 }
5593
5594 /* When FNAME_REL flag given first use the directory of the file.
5595 * Otherwise or when this fails use the current directory. */
5596 for (run = 1; run <= 2; ++run)
5597 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005598 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (run == 1
5600 && rel_to_curdir
5601 && (options & FNAME_REL)
5602 && rel_fname != NULL
5603 && STRLEN(rel_fname) + l < MAXPATHL)
5604 {
5605 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005606 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 l = (int)STRLEN(NameBuff);
5608 }
5609 else
5610 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005611 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 run = 2;
5613 }
5614
5615 /* When the file doesn't exist, try adding parts of
5616 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005617 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 for (;;)
5619 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005620 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005621 && (find_what == FINDFILE_BOTH
5622 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005623 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
5625 file_name = vim_strsave(NameBuff);
5626 goto theend;
5627 }
5628 if (*buf == NUL)
5629 break;
5630 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5631 }
5632 }
5633 }
5634 }
5635 else
5636 {
5637 /*
5638 * Loop over all paths in the 'path' or 'cdpath' option.
5639 * When "first" is set, first setup to the start of the option.
5640 * Otherwise continue to find the next match.
5641 */
5642 if (first == TRUE)
5643 {
5644 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005645 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 dir = path_option;
5647 did_findfile_init = FALSE;
5648 }
5649
5650 for (;;)
5651 {
5652 if (did_findfile_init)
5653 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005654 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (file_name != NULL)
5656 break;
5657
5658 did_findfile_init = FALSE;
5659 }
5660 else
5661 {
5662 char_u *r_ptr;
5663
5664 if (dir == NULL || *dir == NUL)
5665 {
5666 /* We searched all paths of the option, now we can
5667 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005668 vim_findfile_cleanup(fdip_search_ctx);
5669 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 break;
5671 }
5672
5673 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5674 break;
5675
5676 /* copy next path */
5677 buf[0] = 0;
5678 copy_option_part(&dir, buf, MAXPATHL, " ,");
5679
5680#ifdef FEAT_PATH_EXTRA
5681 /* get the stopdir string */
5682 r_ptr = vim_findfile_stopdir(buf);
5683#else
5684 r_ptr = NULL;
5685#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005686 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005687 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005688 fdip_search_ctx, FALSE, rel_fname);
5689 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 did_findfile_init = TRUE;
5691 vim_free(buf);
5692 }
5693 }
5694 }
5695 if (file_name == NULL && (options & FNAME_MESS))
5696 {
5697 if (first == TRUE)
5698 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005699 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005701 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else
5703 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005704 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706 else
5707 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005708 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005710 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 else
5712 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005713 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715 }
5716
5717theend:
5718#ifdef AMIGA
5719 proc->pr_WindowPtr = save_winptr;
5720#endif
5721 return file_name;
5722}
5723
5724#endif /* FEAT_SEARCHPATH */
5725
5726/*
5727 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5728 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5729 */
5730 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005731vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
5733#ifndef FEAT_SEARCHPATH
5734 return mch_chdir((char *)new_dir);
5735#else
5736 char_u *dir_name;
5737 int r;
5738
5739 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5740 FNAME_MESS, curbuf->b_ffname);
5741 if (dir_name == NULL)
5742 return -1;
5743 r = mch_chdir((char *)dir_name);
5744 vim_free(dir_name);
5745 return r;
5746#endif
5747}
5748
5749/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005750 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005752 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5753 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 * Returns OK or FAIL.
5755 */
5756 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005757get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005759 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
5761 if (mch_get_user_name(buf, len) == FAIL)
5762 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005763 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005766 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 return OK;
5768}
5769
5770#ifndef HAVE_QSORT
5771/*
5772 * Our own qsort(), for systems that don't have it.
5773 * It's simple and slow. From the K&R C book.
5774 */
5775 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005776qsort(
5777 void *base,
5778 size_t elm_count,
5779 size_t elm_size,
5780 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 char_u *buf;
5783 char_u *p1;
5784 char_u *p2;
5785 int i, j;
5786 int gap;
5787
5788 buf = alloc((unsigned)elm_size);
5789 if (buf == NULL)
5790 return;
5791
5792 for (gap = elm_count / 2; gap > 0; gap /= 2)
5793 for (i = gap; i < elm_count; ++i)
5794 for (j = i - gap; j >= 0; j -= gap)
5795 {
5796 /* Compare the elements. */
5797 p1 = (char_u *)base + j * elm_size;
5798 p2 = (char_u *)base + (j + gap) * elm_size;
5799 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5800 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005801 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 mch_memmove(buf, p1, elm_size);
5803 mch_memmove(p1, p2, elm_size);
5804 mch_memmove(p2, buf, elm_size);
5805 }
5806
5807 vim_free(buf);
5808}
5809#endif
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811/*
5812 * Sort an array of strings.
5813 */
5814static int
5815#ifdef __BORLANDC__
5816_RTLENTRYF
5817#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005818sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 static int
5821#ifdef __BORLANDC__
5822_RTLENTRYF
5823#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005824sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 return STRCMP(*(char **)s1, *(char **)s2);
5827}
5828
5829 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005830sort_strings(
5831 char_u **files,
5832 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
5834 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5835}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
5837#if !defined(NO_EXPANDPATH) || defined(PROTO)
5838/*
5839 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005840 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Return value like strcmp(p, q), but consider path separators.
5842 */
5843 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005844pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005846 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005847 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005848 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005850 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005852 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005853 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005856 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005858 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 return 0;
5860 s = q;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005861 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 break;
5863 }
5864
5865 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005866 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
5868 s = p;
5869 break;
5870 }
5871
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005872 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873#ifdef BACKSLASH_IN_FILENAME
5874 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005875 && !((c1 == '/' && c2 == '\\')
5876 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#endif
5878 )
5879 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005880 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005882 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005884 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5885 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005887
5888 i += MB_PTR2LEN((char_u *)p + i);
5889 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005891 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005892 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005894 c1 = PTR2CHAR((char_u *)s + i);
5895 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005897 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005898 && i > 0
5899 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005901 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005903 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 return 0; /* match with trailing slash */
5907 if (s == q)
5908 return -1; /* no match */
5909 return 1;
5910}
5911#endif
5912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913/*
5914 * The putenv() implementation below comes from the "screen" program.
5915 * Included with permission from Juergen Weigert.
5916 * See pty.c for the copyright notice.
5917 */
5918
5919/*
5920 * putenv -- put value into environment
5921 *
5922 * Usage: i = putenv (string)
5923 * int i;
5924 * char *string;
5925 *
5926 * where string is of the form <name>=<value>.
5927 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5928 *
5929 * Putenv may need to add a new name into the environment, or to
5930 * associate a value longer than the current value with a particular
5931 * name. So, to make life simpler, putenv() copies your entire
5932 * environment into the heap (i.e. malloc()) from the stack
5933 * (i.e. where it resides when your process is initiated) the first
5934 * time you call it.
5935 *
5936 * (history removed, not very interesting. See the "screen" sources.)
5937 */
5938
5939#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5940
5941#define EXTRASIZE 5 /* increment to add to env. size */
5942
5943static int envsize = -1; /* current size of environment */
5944#ifndef MACOS_CLASSIC
5945extern
5946#endif
5947 char **environ; /* the global which is your env. */
5948
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005949static int findenv(char *name); /* look for a name in the env. */
5950static int newenv(void); /* copy env. from stack to heap */
5951static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
5953 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005954putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 int i;
5957 char *p;
5958
5959 if (envsize < 0)
5960 { /* first time putenv called */
5961 if (newenv() < 0) /* copy env. to heap */
5962 return -1;
5963 }
5964
5965 i = findenv((char *)string); /* look for name in environment */
5966
5967 if (i < 0)
5968 { /* name must be added */
5969 for (i = 0; environ[i]; i++);
5970 if (i >= (envsize - 1))
5971 { /* need new slot */
5972 if (moreenv() < 0)
5973 return -1;
5974 }
5975 p = (char *)alloc((unsigned)(strlen(string) + 1));
5976 if (p == NULL) /* not enough core */
5977 return -1;
5978 environ[i + 1] = 0; /* new end of env. */
5979 }
5980 else
5981 { /* name already in env. */
5982 p = vim_realloc(environ[i], strlen(string) + 1);
5983 if (p == NULL)
5984 return -1;
5985 }
5986 sprintf(p, "%s", string); /* copy into env. */
5987 environ[i] = p;
5988
5989 return 0;
5990}
5991
5992 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005993findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 char *namechar, *envchar;
5996 int i, found;
5997
5998 found = 0;
5999 for (i = 0; environ[i] && !found; i++)
6000 {
6001 envchar = environ[i];
6002 namechar = name;
6003 while (*namechar && *namechar != '=' && (*namechar == *envchar))
6004 {
6005 namechar++;
6006 envchar++;
6007 }
6008 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6009 }
6010 return found ? i - 1 : -1;
6011}
6012
6013 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006014newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 char **env, *elem;
6017 int i, esize;
6018
6019#ifdef MACOS
6020 /* for Mac a new, empty environment is created */
6021 i = 0;
6022#else
6023 for (i = 0; environ[i]; i++)
6024 ;
6025#endif
6026 esize = i + EXTRASIZE + 1;
6027 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6028 if (env == NULL)
6029 return -1;
6030
6031#ifndef MACOS
6032 for (i = 0; environ[i]; i++)
6033 {
6034 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6035 if (elem == NULL)
6036 return -1;
6037 env[i] = elem;
6038 strcpy(elem, environ[i]);
6039 }
6040#endif
6041
6042 env[i] = 0;
6043 environ = env;
6044 envsize = esize;
6045 return 0;
6046}
6047
6048 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006049moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 int esize;
6052 char **env;
6053
6054 esize = envsize + EXTRASIZE;
6055 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6056 if (env == 0)
6057 return -1;
6058 environ = env;
6059 envsize = esize;
6060 return 0;
6061}
6062
6063# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02006064/*
6065 * Used for mch_getenv() for Mac.
6066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006068vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
6070 int i;
6071 char_u *p;
6072
6073 if (envsize < 0)
6074 return NULL;
6075
6076 i = findenv((char *)string);
6077
6078 if (i < 0)
6079 return NULL;
6080
6081 p = vim_strchr((char_u *)environ[i], '=');
6082 return (p + 1);
6083}
6084# endif
6085
6086#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006087
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006088#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006089/*
6090 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6091 * rights to write into.
6092 */
6093 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006094filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006095{
6096 int retval = 0;
6097#if defined(UNIX) || defined(VMS)
6098 int perm = 0;
6099#endif
6100
6101#if defined(UNIX) || defined(VMS)
6102 perm = mch_getperm(fname);
6103#endif
6104#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6105 if (
6106# ifdef WIN3264
6107 mch_writable(fname) &&
6108# else
6109# if defined(UNIX) || defined(VMS)
6110 (perm & 0222) &&
6111# endif
6112# endif
6113 mch_access((char *)fname, W_OK) == 0
6114 )
6115#endif
6116 {
6117 ++retval;
6118 if (mch_isdir(fname))
6119 ++retval;
6120 }
6121 return retval;
6122}
6123#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006124
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006125#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6126/*
6127 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6128 */
6129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006130get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006131{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006132 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006133
6134 n = getc(fd);
6135 n = (n << 8) + getc(fd);
6136 return n;
6137}
6138
6139/*
6140 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6141 */
6142 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006143get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006144{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006145 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006146
6147 n = getc(fd);
6148 n = (n << 8) + getc(fd);
6149 n = (n << 8) + getc(fd);
6150 return n;
6151}
6152
6153/*
6154 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6155 */
6156 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006157get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006158{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006159 /* Use unsigned rather than int otherwise result is undefined
6160 * when left-shift sets the MSB. */
6161 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006162
Bram Moolenaar95235e62013-09-08 16:07:07 +02006163 n = (unsigned)getc(fd);
6164 n = (n << 8) + (unsigned)getc(fd);
6165 n = (n << 8) + (unsigned)getc(fd);
6166 n = (n << 8) + (unsigned)getc(fd);
6167 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006168}
6169
6170/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006171 * Read 8 bytes from "fd" and turn them into a time_T, MSB first.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006172 */
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006173 time_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006174get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006175{
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006176 time_T n = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006177 int i;
6178
6179 for (i = 0; i < 8; ++i)
6180 n = (n << 8) + getc(fd);
6181 return n;
6182}
6183
6184/*
6185 * Read a string of length "cnt" from "fd" into allocated memory.
6186 * Returns NULL when out of memory or unable to read that many bytes.
6187 */
6188 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006189read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006190{
6191 char_u *str;
6192 int i;
6193 int c;
6194
6195 /* allocate memory */
6196 str = alloc((unsigned)cnt + 1);
6197 if (str != NULL)
6198 {
6199 /* Read the string. Quit when running into the EOF. */
6200 for (i = 0; i < cnt; ++i)
6201 {
6202 c = getc(fd);
6203 if (c == EOF)
6204 {
6205 vim_free(str);
6206 return NULL;
6207 }
6208 str[i] = c;
6209 }
6210 str[i] = NUL;
6211 }
6212 return str;
6213}
6214
6215/*
6216 * Write a number to file "fd", MSB first, in "len" bytes.
6217 */
6218 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006219put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006220{
6221 int i;
6222
6223 for (i = len - 1; i >= 0; --i)
6224 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6225 return FAIL;
6226 return OK;
6227}
6228
6229#ifdef _MSC_VER
6230# if (_MSC_VER <= 1200)
6231/* This line is required for VC6 without the service pack. Also see the
6232 * matching #pragma below. */
6233 # pragma optimize("", off)
6234# endif
6235#endif
6236
6237/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006238 * Write time_T to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006239 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006240 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006241 int
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006242put_time(FILE *fd, time_T the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006243{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006244 char_u buf[8];
6245
6246 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006247 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006248}
6249
6250/*
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006251 * Write time_T to "buf[8]".
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006252 */
6253 void
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006254time_to_bytes(time_T the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006255{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006256 int c;
6257 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006258 int bi = 0;
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006259 time_T wtime = the_time;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006260
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006261 /* time_T can be up to 8 bytes in size, more than long_u, thus we
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006262 * can't use put_bytes() here.
6263 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006264 * sign. This happens for large values of wtime. A cast to long_u may
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006265 * truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes,
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006266 * it's safe to assume that long_u is 4 bytes or more and when using 8
6267 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006268 for (i = 7; i >= 0; --i)
6269 {
Bram Moolenaarf4fba6d2016-06-26 16:44:24 +02006270 if (i + 1 > (int)sizeof(time_T))
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006271 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006272 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006273 else
6274 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006275#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006276 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006277#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006278 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006279#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006280 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006281 }
6282 }
6283}
6284
6285#ifdef _MSC_VER
6286# if (_MSC_VER <= 1200)
6287 # pragma optimize("", on)
6288# endif
6289#endif
6290
6291#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006292
6293#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6294 || defined(FEAT_SPELL) || defined(PROTO)
6295/*
6296 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6297 * When "s" is NULL FALSE is returned.
6298 */
6299 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006300has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006301{
6302 char_u *p;
6303
6304 if (s != NULL)
6305 for (p = s; *p != NUL; ++p)
6306 if (*p >= 128)
6307 return TRUE;
6308 return FALSE;
6309}
6310#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006311
6312#if defined(MESSAGE_QUEUE) || defined(PROTO)
6313/*
6314 * Process messages that have been queued for netbeans or clientserver.
Bram Moolenaar3a117e12016-10-30 21:57:52 +01006315 * Also check if any jobs have ended.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006316 * These functions can call arbitrary vimscript and should only be called when
6317 * it is safe to do so.
6318 */
6319 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006320parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006321{
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006322 /* For Win32 mch_breakcheck() does not check for input, do it here. */
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006323# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02006324 channel_handle_events(FALSE);
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006325# endif
6326
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006327# ifdef FEAT_NETBEANS_INTG
6328 /* Process the queued netbeans messages. */
6329 netbeans_parse_messages();
6330# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006331# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02006332 /* Write any buffer lines still to be written. */
6333 channel_write_any_lines();
6334
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006335 /* Process the messages queued on channels. */
6336 channel_parse_messages();
6337# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006338# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006339 /* Process the queued clientserver messages. */
6340 server_parse_messages();
6341# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006342# ifdef FEAT_JOB_CHANNEL
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006343 /* Check if any jobs have ended. */
6344 job_check_ended();
6345# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006346}
6347#endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006348
Bram Moolenaar51628222016-12-01 23:03:28 +01006349#ifndef PROTO /* proto is defined in vim.h */
6350# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006351/*
6352 * Return time in msec since "start_tv".
6353 */
6354 long
6355elapsed(struct timeval *start_tv)
6356{
6357 struct timeval now_tv;
6358
6359 gettimeofday(&now_tv, NULL);
6360 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
6361 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
6362}
Bram Moolenaar51628222016-12-01 23:03:28 +01006363# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006364
Bram Moolenaar51628222016-12-01 23:03:28 +01006365# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006366/*
6367 * Return time in msec since "start_tick".
6368 */
6369 long
6370elapsed(DWORD start_tick)
6371{
6372 DWORD now = GetTickCount();
6373
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006374 return (long)now - (long)start_tick;
6375}
Bram Moolenaar51628222016-12-01 23:03:28 +01006376# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01006377#endif