blob: c1519ef915b3d4acacd439c2bdf171a4afa386c3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
172# ifdef FEAT_VERTSPLIT
173 && 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 Moolenaar1cd871b2004-12-19 22:46:22 +0000199 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 Moolenaar9b578142016-01-30 19:39:49 +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 Moolenaar071d4272004-06-13 20:20:40 +0000508 * Make sure curwin->w_cursor.lnum is valid.
509 */
510 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100511check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512{
513 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
514 {
515#ifdef FEAT_FOLDING
516 /* If there is a closed fold at the end of the file, put the cursor in
517 * its first line. Otherwise in the last line. */
518 if (!hasFolding(curbuf->b_ml.ml_line_count,
519 &curwin->w_cursor.lnum, NULL))
520#endif
521 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
522 }
523 if (curwin->w_cursor.lnum <= 0)
524 curwin->w_cursor.lnum = 1;
525}
526
527/*
528 * Make sure curwin->w_cursor.col is valid.
529 */
530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100531check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200533 check_cursor_col_win(curwin);
534}
535
536/*
537 * Make sure win->w_cursor.col is valid.
538 */
539 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100540check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200541{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 colnr_T len;
543#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200544 colnr_T oldcol = win->w_cursor.col;
545 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546#endif
547
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200548 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200550 win->w_cursor.col = 0;
551 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000553 /* Allow cursor past end-of-line when:
554 * - in Insert mode or restarting Insert mode
555 * - in Visual mode and 'selection' isn't "old"
556 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000557 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000559#ifdef FEAT_VIRTUALEDIT
560 || (ve_flags & VE_ONEMORE)
561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200563 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000565 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200566 win->w_cursor.col = len - 1;
Bram Moolenaar87c19962007-04-26 08:54:21 +0000567#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200568 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000569 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200570 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000571#endif
572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200574 else if (win->w_cursor.col < 0)
575 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577#ifdef FEAT_VIRTUALEDIT
578 /* If virtual editing is on, we can leave the cursor on the old position,
579 * only we must set it to virtual. But don't do it when at the end of the
580 * line. */
581 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200582 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000584 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200585 if (oldcoladd > win->w_cursor.col)
586 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000587 else
588 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200589 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#endif
592}
593
594/*
595 * make sure curwin->w_cursor in on a valid character
596 */
597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 check_cursor_lnum();
601 check_cursor_col();
602}
603
604#if defined(FEAT_TEXTOBJ) || defined(PROTO)
605/*
606 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
607 * Allow it when in Visual mode and 'selection' is not "old".
608 */
609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100610adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 && gchar_cursor() == NUL)
615 --curwin->w_cursor.col;
616}
617#endif
618
619/*
620 * When curwin->w_leftcol has changed, adjust the cursor position.
621 * Return TRUE if the cursor was moved.
622 */
623 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100624leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 long lastcol;
627 colnr_T s, e;
628 int retval = FALSE;
629
630 changed_cline_bef_curs();
631 lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1;
632 validate_virtcol();
633
634 /*
635 * If the cursor is right or left of the screen, move it to last or first
636 * character.
637 */
638 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
639 {
640 retval = TRUE;
641 coladvance((colnr_T)(lastcol - p_siso));
642 }
643 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
644 {
645 retval = TRUE;
646 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
647 }
648
649 /*
650 * If the start of the character under the cursor is not on the screen,
651 * advance the cursor one more char. If this fails (last char of the
652 * line) adjust the scrolling.
653 */
654 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
655 if (e > (colnr_T)lastcol)
656 {
657 retval = TRUE;
658 coladvance(s - 1);
659 }
660 else if (s < curwin->w_leftcol)
661 {
662 retval = TRUE;
663 if (coladvance(e + 1) == FAIL) /* there isn't another character */
664 {
665 curwin->w_leftcol = s; /* adjust w_leftcol instead */
666 changed_cline_bef_curs();
667 }
668 }
669
670 if (retval)
671 curwin->w_set_curswant = TRUE;
672 redraw_later(NOT_VALID);
673 return retval;
674}
675
676/**********************************************************************
677 * Various routines dealing with allocation and deallocation of memory.
678 */
679
680#if defined(MEM_PROFILE) || defined(PROTO)
681
682# define MEM_SIZES 8200
683static long_u mem_allocs[MEM_SIZES];
684static long_u mem_frees[MEM_SIZES];
685static long_u mem_allocated;
686static long_u mem_freed;
687static long_u mem_peak;
688static long_u num_alloc;
689static long_u num_freed;
690
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100691static void mem_pre_alloc_s(size_t *sizep);
692static void mem_pre_alloc_l(long_u *sizep);
693static void mem_post_alloc(void **pp, size_t size);
694static void mem_pre_free(void **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100697mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 *sizep += sizeof(size_t);
700}
701
702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100703mem_pre_alloc_l(long_u *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 *sizep += sizeof(size_t);
706}
707
708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100709mem_post_alloc(
710 void **pp,
711 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 if (*pp == NULL)
714 return;
715 size -= sizeof(size_t);
716 *(long_u *)*pp = size;
717 if (size <= MEM_SIZES-1)
718 mem_allocs[size-1]++;
719 else
720 mem_allocs[MEM_SIZES-1]++;
721 mem_allocated += size;
722 if (mem_allocated - mem_freed > mem_peak)
723 mem_peak = mem_allocated - mem_freed;
724 num_alloc++;
725 *pp = (void *)((char *)*pp + sizeof(size_t));
726}
727
728 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100729mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
731 long_u size;
732
733 *pp = (void *)((char *)*pp - sizeof(size_t));
734 size = *(size_t *)*pp;
735 if (size <= MEM_SIZES-1)
736 mem_frees[size-1]++;
737 else
738 mem_frees[MEM_SIZES-1]++;
739 mem_freed += size;
740 num_freed++;
741}
742
743/*
744 * called on exit via atexit()
745 */
746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100747vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 int i, j;
750
751 printf("\r\n");
752 j = 0;
753 for (i = 0; i < MEM_SIZES - 1; i++)
754 {
755 if (mem_allocs[i] || mem_frees[i])
756 {
757 if (mem_frees[i] > mem_allocs[i])
758 printf("\r\n%s", _("ERROR: "));
759 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
760 j++;
761 if (j > 3)
762 {
763 j = 0;
764 printf("\r\n");
765 }
766 }
767 }
768
769 i = MEM_SIZES - 1;
770 if (mem_allocs[i])
771 {
772 printf("\r\n");
773 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200774 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
776 }
777
778 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
779 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
780 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
781 num_alloc, num_freed);
782}
783
784#endif /* MEM_PROFILE */
785
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100786#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100787static int alloc_does_fail(long_u size);
Bram Moolenaara260b872016-01-15 20:48:22 +0100788
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100789 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100790alloc_does_fail(long_u size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100791{
792 if (alloc_fail_countdown == 0)
793 {
794 if (--alloc_fail_repeat <= 0)
795 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100796 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100797 return TRUE;
798 }
799 --alloc_fail_countdown;
800 return FALSE;
801}
802#endif
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804/*
805 * Some memory is reserved for error messages and for being able to
806 * call mf_release_all(), which needs some memory for mf_trans_add().
807 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100808#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200809#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000812 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 * Use lalloc for larger blocks.
814 */
815 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100816alloc(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 return (lalloc((long_u)size, TRUE));
819}
820
821/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100822 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100823 */
824 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100825alloc_id(unsigned size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100826{
827#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100828 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100829 return NULL;
830#endif
831 return (lalloc((long_u)size, TRUE));
832}
833
834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * Allocate memory and set all bytes to zero.
836 */
837 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100838alloc_clear(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 char_u *p;
841
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200842 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (p != NULL)
844 (void)vim_memset(p, 0, (size_t)size);
845 return p;
846}
847
848/*
849 * alloc() with check for maximum line length
850 */
851 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100852alloc_check(unsigned size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854#if !defined(UNIX) && !defined(__EMX__)
855 if (sizeof(int) == 2 && size > 0x7fff)
856 {
857 /* Don't hide this message */
858 emsg_silent = 0;
859 EMSG(_("E340: Line is becoming too long"));
860 return NULL;
861 }
862#endif
863 return (lalloc((long_u)size, TRUE));
864}
865
866/*
867 * Allocate memory like lalloc() and set all bytes to zero.
868 */
869 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100870lalloc_clear(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871{
872 char_u *p;
873
874 p = (lalloc(size, message));
875 if (p != NULL)
876 (void)vim_memset(p, 0, (size_t)size);
877 return p;
878}
879
880/*
881 * Low level memory allocation function.
882 * This is used often, KEEP IT FAST!
883 */
884 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100885lalloc(long_u size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
887 char_u *p; /* pointer to new storage space */
888 static int releasing = FALSE; /* don't do mf_release_all() recursive */
889 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100890#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 static long_u allocated = 0; /* allocated since last avail check */
892#endif
893
894 /* Safety check for allocating zero bytes */
895 if (size == 0)
896 {
897 /* Don't hide this message */
898 emsg_silent = 0;
899 EMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
900 return NULL;
901 }
902
903#ifdef MEM_PROFILE
904 mem_pre_alloc_l(&size);
905#endif
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 /*
908 * Loop when out of memory: Try to release some memfile blocks and
909 * if some blocks are released call malloc again.
910 */
911 for (;;)
912 {
913 /*
914 * Handle three kind of systems:
915 * 1. No check for available memory: Just return.
916 * 2. Slow check for available memory: call mch_avail_mem() after
917 * allocating KEEP_ROOM amount of memory.
918 * 3. Strict check for available memory: call mch_avail_mem()
919 */
920 if ((p = (char_u *)malloc((size_t)size)) != NULL)
921 {
922#ifndef HAVE_AVAIL_MEM
923 /* 1. No check for available memory: Just return. */
924 goto theend;
925#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 /* 2. Slow check for available memory: call mch_avail_mem() after
927 * allocating (KEEP_ROOM / 2) amount of memory. */
928 allocated += size;
929 if (allocated < KEEP_ROOM / 2)
930 goto theend;
931 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200934 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000936 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 p = NULL;
938 }
939 else
940 goto theend;
941#endif
942 }
943 /*
944 * Remember that mf_release_all() is being called to avoid an endless
945 * loop, because mf_release_all() may call alloc() recursively.
946 */
947 if (releasing)
948 break;
949 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000950
951 clear_sb_text(); /* free any scrollback text */
952 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 releasing = FALSE;
955 if (!try_again)
956 break;
957 }
958
959 if (message && p == NULL)
960 do_outofmem_msg(size);
961
962theend:
963#ifdef MEM_PROFILE
964 mem_post_alloc((void **)&p, (size_t)size);
965#endif
966 return p;
967}
968
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100969/*
970 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100971 */
972 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100973lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100974{
975#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100976 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100977 return NULL;
978#endif
979 return (lalloc((long_u)size, message));
980}
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#if defined(MEM_PROFILE) || defined(PROTO)
983/*
984 * realloc() with memory profiling.
985 */
986 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100987mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 void *p;
990
991 mem_pre_free(&ptr);
992 mem_pre_alloc_s(&size);
993
994 p = realloc(ptr, size);
995
996 mem_post_alloc(&p, size);
997
998 return p;
999}
1000#endif
1001
1002/*
1003* Avoid repeating the error message many times (they take 1 second each).
1004* Did_outofmem_msg is reset when a character is read.
1005*/
1006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001007do_outofmem_msg(long_u size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 if (!did_outofmem_msg)
1010 {
1011 /* Don't hide this message */
1012 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001013
1014 /* Must come first to avoid coming back here when printing the error
1015 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001017
1018 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020}
1021
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001022#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001023
1024# if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001025static void free_findfile(void);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001026# endif
1027
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001028/*
1029 * Free everything that we allocated.
1030 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001031 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1032 * surprised if Vim crashes...
1033 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001034 */
1035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001036free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001037{
1038 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001039 static int entered = FALSE;
1040
1041 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1042 * Don't try freeing everything again. */
1043 if (entered)
1044 return;
1045 entered = TRUE;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001046
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001047# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001048 /* Don't want to trigger autocommands from here on. */
1049 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001050# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001051
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001052# ifdef FEAT_WINDOWS
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001053 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1054 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001055 if (first_tabpage->tp_next != NULL)
1056 do_cmdline_cmd((char_u *)"tabonly!");
1057 if (firstwin != lastwin)
1058 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001059# endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001060
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001061# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001062 /* Free all spell info. */
1063 spell_free_all();
1064# endif
1065
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001066# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001067 /* Clear user commands (before deleting buffers). */
1068 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001069# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001070
1071# ifdef FEAT_MENU
1072 /* Clear menus. */
1073 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001074# ifdef FEAT_MULTI_LANG
1075 do_cmdline_cmd((char_u *)"menutranslate clear");
1076# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001077# endif
1078
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001079 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001080 do_cmdline_cmd((char_u *)"lmapclear");
1081 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001082 do_cmdline_cmd((char_u *)"mapclear");
1083 do_cmdline_cmd((char_u *)"mapclear!");
1084 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001085# if defined(FEAT_EVAL)
1086 do_cmdline_cmd((char_u *)"breakdel *");
1087# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001088# if defined(FEAT_PROFILE)
1089 do_cmdline_cmd((char_u *)"profdel *");
1090# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001091# if defined(FEAT_KEYMAP)
1092 do_cmdline_cmd((char_u *)"set keymap=");
1093#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001094
1095# ifdef FEAT_TITLE
1096 free_titles();
1097# endif
1098# if defined(FEAT_SEARCHPATH)
1099 free_findfile();
1100# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001101
1102 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001103# if defined(FEAT_AUTOCMD)
1104 free_all_autocmds();
1105# endif
1106 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001107 free_all_options();
1108 free_all_marks();
1109 alist_clear(&global_alist);
1110 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001111# if defined(FEAT_CMDL_COMPL)
1112 free_users();
1113# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001114 free_search_patterns();
1115 free_old_sub();
1116 free_last_insert();
1117 free_prev_shellcmd();
1118 free_regexp_stuff();
1119 free_tag_stuff();
1120 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001121# ifdef FEAT_SIGNS
1122 free_signs();
1123# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001124# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001125 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001126# endif
1127# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001128 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001129# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01001130# ifdef FEAT_CHANNEL
1131 channel_free_all();
1132# endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001133 clear_sb_text(); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001134
1135 /* Free some global vars. */
1136 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001137# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001138 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001139# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001140 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001141# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001142 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001143# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001144 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001145 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001146
1147 /* Clear cmdline history. */
1148 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001149# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001150 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001151# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001152
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001153#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001154 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001155 win_T *win;
1156 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001157
1158 qf_free_all(NULL);
1159 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001160 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001161 qf_free_all(win);
1162 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001163#endif
1164
1165 /* Close all script inputs. */
1166 close_all_scripts();
1167
1168#if defined(FEAT_WINDOWS)
1169 /* Destroy all windows. Must come before freeing buffers. */
1170 win_free_all();
1171#endif
1172
Bram Moolenaar2a329742008-04-01 12:53:43 +00001173 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1174 * were freed already. */
1175#ifdef FEAT_AUTOCHDIR
1176 p_acd = FALSE;
1177#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001178 for (buf = firstbuf; buf != NULL; )
1179 {
1180 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001181 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001182 if (buf_valid(buf))
1183 buf = nextbuf; /* didn't work, try next one */
1184 else
1185 buf = firstbuf;
1186 }
1187
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001188#ifdef FEAT_ARABIC
1189 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001190#endif
1191
1192 /* Clear registers. */
1193 clear_registers();
1194 ResetRedobuff();
1195 ResetRedobuff();
1196
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001197#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001198 vim_free(serverDelayedStartName);
1199#endif
1200
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001201 /* highlight info */
1202 free_highlight();
1203
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001204 reset_last_sourcing();
1205
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001206#ifdef FEAT_WINDOWS
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001207 free_tabpage(first_tabpage);
1208 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001209#endif
1210
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001211# ifdef UNIX
1212 /* Machine-specific free. */
1213 mch_free_mem();
1214# endif
1215
1216 /* message history */
1217 for (;;)
1218 if (delete_first_msg() == FAIL)
1219 break;
1220
1221# ifdef FEAT_EVAL
1222 eval_clear();
1223# endif
1224
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001225 free_termoptions();
1226
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001227 /* screenlines (can't display anything now!) */
1228 free_screenlines();
1229
1230#if defined(USE_XSMP)
1231 xsmp_close();
1232#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001233#ifdef FEAT_GUI_GTK
1234 gui_mch_free_all();
1235#endif
1236 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001237
1238 vim_free(IObuff);
1239 vim_free(NameBuff);
1240}
1241#endif
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001244 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 */
1246 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001247vim_strsave(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 char_u *p;
1250 unsigned len;
1251
1252 len = (unsigned)STRLEN(string) + 1;
1253 p = alloc(len);
1254 if (p != NULL)
1255 mch_memmove(p, string, (size_t)len);
1256 return p;
1257}
1258
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001259/*
1260 * Copy up to "len" bytes of "string" into newly allocated memory and
1261 * terminate with a NUL.
1262 * The allocated memory always has size "len + 1", also when "string" is
1263 * shorter.
1264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001266vim_strnsave(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
1268 char_u *p;
1269
1270 p = alloc((unsigned)(len + 1));
1271 if (p != NULL)
1272 {
1273 STRNCPY(p, string, len);
1274 p[len] = NUL;
1275 }
1276 return p;
1277}
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279/*
1280 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1281 * by a backslash.
1282 */
1283 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001284vim_strsave_escaped(char_u *string, char_u *esc_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001286 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287}
1288
1289/*
1290 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1291 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001292 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 */
1294 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001295vim_strsave_escaped_ext(
1296 char_u *string,
1297 char_u *esc_chars,
1298 int cc,
1299 int bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 char_u *p;
1302 char_u *p2;
1303 char_u *escaped_string;
1304 unsigned length;
1305#ifdef FEAT_MBYTE
1306 int l;
1307#endif
1308
1309 /*
1310 * First count the number of backslashes required.
1311 * Then allocate the memory and insert them.
1312 */
1313 length = 1; /* count the trailing NUL */
1314 for (p = string; *p; p++)
1315 {
1316#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001317 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {
1319 length += l; /* count a multibyte char */
1320 p += l - 1;
1321 continue;
1322 }
1323#endif
1324 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1325 ++length; /* count a backslash */
1326 ++length; /* count an ordinary char */
1327 }
1328 escaped_string = alloc(length);
1329 if (escaped_string != NULL)
1330 {
1331 p2 = escaped_string;
1332 for (p = string; *p; p++)
1333 {
1334#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001335 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337 mch_memmove(p2, p, (size_t)l);
1338 p2 += l;
1339 p += l - 1; /* skip multibyte char */
1340 continue;
1341 }
1342#endif
1343 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001344 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 *p2++ = *p;
1346 }
1347 *p2 = NUL;
1348 }
1349 return escaped_string;
1350}
1351
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001352/*
1353 * Return TRUE when 'shell' has "csh" in the tail.
1354 */
1355 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001356csh_like_shell(void)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001357{
1358 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1359}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001360
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001361/*
1362 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001363 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001364 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001365 * Escape a newline, depending on the 'shell' option.
1366 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1367 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001368 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001369 * Returns the result in allocated memory, NULL if we have run out.
1370 */
1371 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001372vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001373{
1374 unsigned length;
1375 char_u *p;
1376 char_u *d;
1377 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001378 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001379 int csh_like;
1380
1381 /* Only csh and similar shells expand '!' within single quotes. For sh and
1382 * the like we must not put a backslash before it, it will be taken
1383 * literally. If do_special is set the '!' will be escaped twice.
1384 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001385 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001386
1387 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001388 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001389 for (p = string; *p != NUL; mb_ptr_adv(p))
1390 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001391# if defined(WIN32) || defined(DOS)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001392 if (!p_ssl)
1393 {
1394 if (*p == '"')
1395 ++length; /* " -> "" */
1396 }
1397 else
1398# endif
1399 if (*p == '\'')
1400 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001401 if ((*p == '\n' && (csh_like || do_newline))
1402 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001403 {
1404 ++length; /* insert backslash */
1405 if (csh_like && do_special)
1406 ++length; /* insert backslash */
1407 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001408 if (do_special && find_cmdline_var(p, &l) >= 0)
1409 {
1410 ++length; /* insert backslash */
1411 p += l - 1;
1412 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001413 }
1414
1415 /* Allocate memory for the result and fill it. */
1416 escaped_string = alloc(length);
1417 if (escaped_string != NULL)
1418 {
1419 d = escaped_string;
1420
1421 /* add opening quote */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001422# if defined(WIN32) || defined(DOS)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001423 if (!p_ssl)
1424 *d++ = '"';
1425 else
1426# endif
1427 *d++ = '\'';
1428
1429 for (p = string; *p != NUL; )
1430 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001431# if defined(WIN32) || defined(DOS)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001432 if (!p_ssl)
1433 {
1434 if (*p == '"')
1435 {
1436 *d++ = '"';
1437 *d++ = '"';
1438 ++p;
1439 continue;
1440 }
1441 }
1442 else
1443# endif
1444 if (*p == '\'')
1445 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001446 *d++ = '\'';
1447 *d++ = '\\';
1448 *d++ = '\'';
1449 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001450 ++p;
1451 continue;
1452 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001453 if ((*p == '\n' && (csh_like || do_newline))
1454 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001455 {
1456 *d++ = '\\';
1457 if (csh_like && do_special)
1458 *d++ = '\\';
1459 *d++ = *p++;
1460 continue;
1461 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001462 if (do_special && find_cmdline_var(p, &l) >= 0)
1463 {
1464 *d++ = '\\'; /* insert backslash */
1465 while (--l >= 0) /* copy the var */
1466 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001467 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001468 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001469
1470 MB_COPY_CHAR(p, d);
1471 }
1472
1473 /* add terminating quote and finish with a NUL */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001474# if defined(WIN32) || defined(DOS)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001475 if (!p_ssl)
1476 *d++ = '"';
1477 else
1478# endif
1479 *d++ = '\'';
1480 *d = NUL;
1481 }
1482
1483 return escaped_string;
1484}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486/*
1487 * Like vim_strsave(), but make all characters uppercase.
1488 * This uses ASCII lower-to-upper case translation, language independent.
1489 */
1490 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001491vim_strsave_up(char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 char_u *p1;
1494
1495 p1 = vim_strsave(string);
1496 vim_strup(p1);
1497 return p1;
1498}
1499
1500/*
1501 * Like vim_strnsave(), but make all characters uppercase.
1502 * This uses ASCII lower-to-upper case translation, language independent.
1503 */
1504 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001505vim_strnsave_up(char_u *string, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 char_u *p1;
1508
1509 p1 = vim_strnsave(string, len);
1510 vim_strup(p1);
1511 return p1;
1512}
1513
1514/*
1515 * ASCII lower-to-upper case translation, language independent.
1516 */
1517 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001518vim_strup(
1519 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 char_u *p2;
1522 int c;
1523
1524 if (p != NULL)
1525 {
1526 p2 = p;
1527 while ((c = *p2) != NUL)
1528#ifdef EBCDIC
1529 *p2++ = isalpha(c) ? toupper(c) : c;
1530#else
1531 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1532#endif
1533 }
1534}
1535
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001536#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001537/*
1538 * Make string "s" all upper-case and return it in allocated memory.
1539 * Handles multi-byte characters as well as possible.
1540 * Returns NULL when out of memory.
1541 */
1542 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001543strup_save(char_u *orig)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001544{
1545 char_u *p;
1546 char_u *res;
1547
1548 res = p = vim_strsave(orig);
1549
1550 if (res != NULL)
1551 while (*p != NUL)
1552 {
1553# ifdef FEAT_MBYTE
1554 int l;
1555
1556 if (enc_utf8)
1557 {
1558 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001559 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001560 char_u *s;
1561
1562 c = utf_ptr2char(p);
1563 uc = utf_toupper(c);
1564
1565 /* Reallocate string when byte count changes. This is rare,
1566 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001567 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001568 newl = utf_char2len(uc);
1569 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001570 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001571 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001572 if (s == NULL)
1573 break;
1574 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001575 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001576 p = s + (p - res);
1577 vim_free(res);
1578 res = s;
1579 }
1580
1581 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001582 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001583 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001584 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001585 p += l; /* skip multi-byte character */
1586 else
1587# endif
1588 {
1589 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1590 p++;
1591 }
1592 }
1593
1594 return res;
1595}
1596#endif
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 * delete spaces at the end of a string
1600 */
1601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602del_trailing_spaces(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 char_u *q;
1605
1606 q = ptr + STRLEN(ptr);
1607 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
1608 *q = NUL;
1609}
1610
1611/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001612 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001613 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 */
1615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001616vim_strncpy(char_u *to, char_u *from, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001618 STRNCPY(to, from, len);
1619 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620}
1621
1622/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001623 * Like strcat(), but make sure the result fits in "tosize" bytes and is
1624 * always NUL terminated.
1625 */
1626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001627vim_strcat(char_u *to, char_u *from, size_t tosize)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001628{
1629 size_t tolen = STRLEN(to);
1630 size_t fromlen = STRLEN(from);
1631
1632 if (tolen + fromlen + 1 > tosize)
1633 {
1634 mch_memmove(to + tolen, from, tosize - tolen - 1);
1635 to[tosize - 1] = NUL;
1636 }
1637 else
1638 STRCPY(to + tolen, from);
1639}
1640
1641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 * Isolate one part of a string option where parts are separated with
1643 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001644 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 * "*option" is advanced to the next part.
1646 * The length is returned.
1647 */
1648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001649copy_option_part(
1650 char_u **option,
1651 char_u *buf,
1652 int maxlen,
1653 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
1655 int len = 0;
1656 char_u *p = *option;
1657
1658 /* skip '.' at start of option part, for 'suffixes' */
1659 if (*p == '.')
1660 buf[len++] = *p++;
1661 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1662 {
1663 /*
1664 * Skip backslash before a separator character and space.
1665 */
1666 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1667 ++p;
1668 if (len < maxlen - 1)
1669 buf[len++] = *p;
1670 ++p;
1671 }
1672 buf[len] = NUL;
1673
1674 if (*p != NUL && *p != ',') /* skip non-standard separator */
1675 ++p;
1676 p = skip_to_option_part(p); /* p points to next file name */
1677
1678 *option = p;
1679 return len;
1680}
1681
1682/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001683 * Replacement for free() that ignores NULL pointers.
1684 * Also skip free() when exiting for sure, this helps when we caught a deadly
1685 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 */
1687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001690 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692#ifdef MEM_PROFILE
1693 mem_pre_free(&x);
1694#endif
1695 free(x);
1696 }
1697}
1698
1699#ifndef HAVE_MEMSET
1700 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001701vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 char *p = ptr;
1704
1705 while (size-- > 0)
1706 *p++ = c;
1707 return ptr;
1708}
1709#endif
1710
1711#ifdef VIM_MEMCMP
1712/*
1713 * Return zero when "b1" and "b2" are the same for "len" bytes.
1714 * Return non-zero otherwise.
1715 */
1716 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001717vim_memcmp(void *b1, void *b2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2;
1720
1721 for ( ; len > 0; --len)
1722 {
1723 if (*p1 != *p2)
1724 return 1;
1725 ++p1;
1726 ++p2;
1727 }
1728 return 0;
1729}
1730#endif
1731
1732#ifdef VIM_MEMMOVE
1733/*
1734 * Version of memmove() that handles overlapping source and destination.
1735 * For systems that don't have a function that is guaranteed to do that (SYSV).
1736 */
1737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001738mch_memmove(void *src_arg, *dst_arg, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 /*
1741 * A void doesn't have a size, we use char pointers.
1742 */
1743 char *dst = dst_arg, *src = src_arg;
1744
1745 /* overlap, copy backwards */
1746 if (dst > src && dst < src + len)
1747 {
1748 src += len;
1749 dst += len;
1750 while (len-- > 0)
1751 *--dst = *--src;
1752 }
1753 else /* copy forwards */
1754 while (len-- > 0)
1755 *dst++ = *src++;
1756}
1757#endif
1758
1759#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1760/*
1761 * Compare two strings, ignoring case, using current locale.
1762 * Doesn't work for multi-byte characters.
1763 * return 0 for match, < 0 for smaller, > 0 for bigger
1764 */
1765 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766vim_stricmp(char *s1, char *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 int i;
1769
1770 for (;;)
1771 {
1772 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1773 if (i != 0)
1774 return i; /* this character different */
1775 if (*s1 == NUL)
1776 break; /* strings match until NUL */
1777 ++s1;
1778 ++s2;
1779 }
1780 return 0; /* strings match */
1781}
1782#endif
1783
1784#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1785/*
1786 * Compare two strings, for length "len", ignoring case, using current locale.
1787 * Doesn't work for multi-byte characters.
1788 * return 0 for match, < 0 for smaller, > 0 for bigger
1789 */
1790 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001791vim_strnicmp(char *s1, char *s2, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 int i;
1794
1795 while (len > 0)
1796 {
1797 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1798 if (i != 0)
1799 return i; /* this character different */
1800 if (*s1 == NUL)
1801 break; /* strings match until NUL */
1802 ++s1;
1803 ++s2;
1804 --len;
1805 }
1806 return 0; /* strings match */
1807}
1808#endif
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810/*
1811 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001812 * with characters from 128 to 255 correctly. It also doesn't return a
1813 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
1815 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001816vim_strchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 char_u *p;
1819 int b;
1820
1821 p = string;
1822#ifdef FEAT_MBYTE
1823 if (enc_utf8 && c >= 0x80)
1824 {
1825 while (*p != NUL)
1826 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001827 int l = (*mb_ptr2len)(p);
1828
1829 /* Avoid matching an illegal byte here. */
1830 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001832 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 return NULL;
1835 }
1836 if (enc_dbcs != 0 && c > 255)
1837 {
1838 int n2 = c & 0xff;
1839
1840 c = ((unsigned)c >> 8) & 0xff;
1841 while ((b = *p) != NUL)
1842 {
1843 if (b == c && p[1] == n2)
1844 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001845 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 return NULL;
1848 }
1849 if (has_mbyte)
1850 {
1851 while ((b = *p) != NUL)
1852 {
1853 if (b == c)
1854 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001855 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 }
1857 return NULL;
1858 }
1859#endif
1860 while ((b = *p) != NUL)
1861 {
1862 if (b == c)
1863 return p;
1864 ++p;
1865 }
1866 return NULL;
1867}
1868
1869/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001870 * Version of strchr() that only works for bytes and handles unsigned char
1871 * strings with characters above 128 correctly. It also doesn't return a
1872 * pointer to the NUL at the end of the string.
1873 */
1874 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001875vim_strbyte(char_u *string, int c)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001876{
1877 char_u *p = string;
1878
1879 while (*p != NUL)
1880 {
1881 if (*p == c)
1882 return p;
1883 ++p;
1884 }
1885 return NULL;
1886}
1887
1888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001890 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001891 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 */
1893 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001894vim_strrchr(char_u *string, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001897 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaar05159a02005-02-26 23:04:13 +00001899 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001901 if (*p == c)
1902 retval = p;
1903 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 return retval;
1906}
1907
1908/*
1909 * Vim's version of strpbrk(), in case it's missing.
1910 * Don't generate a prototype for this, causes problems when it's not used.
1911 */
1912#ifndef PROTO
1913# ifndef HAVE_STRPBRK
1914# ifdef vim_strpbrk
1915# undef vim_strpbrk
1916# endif
1917 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918vim_strpbrk(char_u *s, char_u *charset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 while (*s)
1921 {
1922 if (vim_strchr(charset, *s) != NULL)
1923 return s;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001924 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 return NULL;
1927}
1928# endif
1929#endif
1930
1931/*
1932 * Vim has its own isspace() function, because on some machines isspace()
1933 * can't handle characters above 128.
1934 */
1935 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001936vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
1938 return ((x >= 9 && x <= 13) || x == ' ');
1939}
1940
1941/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001942 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944
1945/*
1946 * Clear an allocated growing array.
1947 */
1948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001949ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
1951 vim_free(gap->ga_data);
1952 ga_init(gap);
1953}
1954
1955/*
1956 * Clear a growing array that contains a list of strings.
1957 */
1958 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001959ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960{
1961 int i;
1962
1963 for (i = 0; i < gap->ga_len; ++i)
1964 vim_free(((char_u **)(gap->ga_data))[i]);
1965 ga_clear(gap);
1966}
1967
1968/*
1969 * Initialize a growing array. Don't forget to set ga_itemsize and
1970 * ga_growsize! Or use ga_init2().
1971 */
1972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001973ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00001976 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 gap->ga_len = 0;
1978}
1979
1980 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001981ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
1983 ga_init(gap);
1984 gap->ga_itemsize = itemsize;
1985 gap->ga_growsize = growsize;
1986}
1987
1988/*
1989 * Make room in growing array "gap" for at least "n" items.
1990 * Return FAIL for failure, OK otherwise.
1991 */
1992 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001993ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01001995 size_t old_len;
1996 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 char_u *pp;
1998
Bram Moolenaar86b68352004-12-27 21:59:20 +00001999 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
2001 if (n < gap->ga_growsize)
2002 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002003 new_len = gap->ga_itemsize * (gap->ga_len + n);
2004 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002005 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (pp == NULL)
2007 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002008 old_len = gap->ga_itemsize * gap->ga_maxlen;
2009 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002010 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 gap->ga_data = pp;
2012 }
2013 return OK;
2014}
2015
2016/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002017 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002018 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002019 * Returns NULL when out of memory.
2020 */
2021 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002022ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002023{
2024 int i;
2025 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002026 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002027 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002028 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002029
2030 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002031 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002032
2033 s = alloc(len + 1);
2034 if (s != NULL)
2035 {
2036 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002037 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002038 for (i = 0; i < gap->ga_len; ++i)
2039 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002040 if (p != s)
2041 {
2042 STRCPY(p, sep);
2043 p += sep_len;
2044 }
2045 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2046 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002047 }
2048 }
2049 return s;
2050}
2051
Bram Moolenaarb20e3342016-01-18 23:29:01 +01002052#if defined(FEAT_VIMINFO) || defined(PROTO)
2053/*
2054 * Make a copy of string "p" and add it to "gap".
2055 * When out of memory nothing changes.
2056 */
2057 void
2058ga_add_string(garray_T *gap, char_u *p)
2059{
2060 char_u *cp = vim_strsave(p);
2061
2062 if (cp != NULL)
2063 {
2064 if (ga_grow(gap, 1) == OK)
2065 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
2066 else
2067 vim_free(cp);
2068 }
2069}
2070#endif
2071
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002074 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 * Note: Does NOT copy the NUL at the end!
2076 */
2077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002078ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079{
Bram Moolenaar43345542015-11-29 17:35:35 +01002080 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
Bram Moolenaar43345542015-11-29 17:35:35 +01002082 if (s == NULL)
2083 return;
2084 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (ga_grow(gap, len) == OK)
2086 {
2087 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2088 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090}
2091
2092/*
2093 * Append one byte to a growarray which contains bytes.
2094 */
2095 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 if (ga_grow(gap, 1) == OK)
2099 {
2100 *((char *)gap->ga_data + gap->ga_len) = c;
2101 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103}
2104
Bram Moolenaar597a4222014-06-25 14:39:50 +02002105#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2106 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002107/*
2108 * Append the text in "gap" below the cursor line and clear "gap".
2109 */
2110 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002111append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002112{
2113 /* Remove trailing CR. */
2114 if (gap->ga_len > 0
2115 && !curbuf->b_p_bin
2116 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2117 --gap->ga_len;
2118 ga_append(gap, NUL);
2119 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2120 gap->ga_len = 0;
2121}
2122#endif
2123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124/************************************************************************
2125 * functions that use lookup tables for various things, generally to do with
2126 * special key codes.
2127 */
2128
2129/*
2130 * Some useful tables.
2131 */
2132
2133static struct modmasktable
2134{
2135 short mod_mask; /* Bit-mask for particular key modifier */
2136 short mod_flag; /* Bit(s) for particular key modifier */
2137 char_u name; /* Single letter name of modifier */
2138} mod_mask_table[] =
2139{
2140 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002141 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2143 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2144 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2145 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2146 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
2147#ifdef MACOS
2148 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2149#endif
2150 /* 'A' must be the last one */
2151 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2152 {0, 0, NUL}
2153};
2154
2155/*
2156 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002157 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 */
2159#define MOD_KEYS_ENTRY_SIZE 5
2160
2161static char_u modifier_keys_table[] =
2162{
2163/* mod mask with modifier without modifier */
2164 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2165 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2166 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2167 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2168 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2169 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2170 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2171 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2172 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2173 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2174 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2175 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2176 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2177 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2178 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2179 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2180 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2181 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2182 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2183 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2184 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2185 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2186 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2187 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2188 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2189 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2190 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2191 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2192 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2193 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2194 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2195 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2196 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2197
2198 /* vt100 F1 */
2199 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2200 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2201 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2202 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2203
2204 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2205 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2206 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2207 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2208 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2209 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2210 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2211 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2212 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2213 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2214
2215 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2216 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2217 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2218 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2219 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2220 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2221 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2222 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2223 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2224 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2225
2226 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2227 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2228 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2229 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2230 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2231 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2232 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2233 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2234 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2235 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2236
2237 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2238 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2239 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2240 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2241 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2242 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2243 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2244
2245 /* TAB pseudo code*/
2246 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2247
2248 NUL
2249};
2250
2251static struct key_name_entry
2252{
2253 int key; /* Special key code or ascii value */
2254 char_u *name; /* Name of key */
2255} key_names_table[] =
2256{
2257 {' ', (char_u *)"Space"},
2258 {TAB, (char_u *)"Tab"},
2259 {K_TAB, (char_u *)"Tab"},
2260 {NL, (char_u *)"NL"},
2261 {NL, (char_u *)"NewLine"}, /* Alternative name */
2262 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2263 {NL, (char_u *)"LF"}, /* Alternative name */
2264 {CAR, (char_u *)"CR"},
2265 {CAR, (char_u *)"Return"}, /* Alternative name */
2266 {CAR, (char_u *)"Enter"}, /* Alternative name */
2267 {K_BS, (char_u *)"BS"},
2268 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2269 {ESC, (char_u *)"Esc"},
2270 {CSI, (char_u *)"CSI"},
2271 {K_CSI, (char_u *)"xCSI"},
2272 {'|', (char_u *)"Bar"},
2273 {'\\', (char_u *)"Bslash"},
2274 {K_DEL, (char_u *)"Del"},
2275 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2276 {K_KDEL, (char_u *)"kDel"},
2277 {K_UP, (char_u *)"Up"},
2278 {K_DOWN, (char_u *)"Down"},
2279 {K_LEFT, (char_u *)"Left"},
2280 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002281 {K_XUP, (char_u *)"xUp"},
2282 {K_XDOWN, (char_u *)"xDown"},
2283 {K_XLEFT, (char_u *)"xLeft"},
2284 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 {K_F1, (char_u *)"F1"},
2287 {K_F2, (char_u *)"F2"},
2288 {K_F3, (char_u *)"F3"},
2289 {K_F4, (char_u *)"F4"},
2290 {K_F5, (char_u *)"F5"},
2291 {K_F6, (char_u *)"F6"},
2292 {K_F7, (char_u *)"F7"},
2293 {K_F8, (char_u *)"F8"},
2294 {K_F9, (char_u *)"F9"},
2295 {K_F10, (char_u *)"F10"},
2296
2297 {K_F11, (char_u *)"F11"},
2298 {K_F12, (char_u *)"F12"},
2299 {K_F13, (char_u *)"F13"},
2300 {K_F14, (char_u *)"F14"},
2301 {K_F15, (char_u *)"F15"},
2302 {K_F16, (char_u *)"F16"},
2303 {K_F17, (char_u *)"F17"},
2304 {K_F18, (char_u *)"F18"},
2305 {K_F19, (char_u *)"F19"},
2306 {K_F20, (char_u *)"F20"},
2307
2308 {K_F21, (char_u *)"F21"},
2309 {K_F22, (char_u *)"F22"},
2310 {K_F23, (char_u *)"F23"},
2311 {K_F24, (char_u *)"F24"},
2312 {K_F25, (char_u *)"F25"},
2313 {K_F26, (char_u *)"F26"},
2314 {K_F27, (char_u *)"F27"},
2315 {K_F28, (char_u *)"F28"},
2316 {K_F29, (char_u *)"F29"},
2317 {K_F30, (char_u *)"F30"},
2318
2319 {K_F31, (char_u *)"F31"},
2320 {K_F32, (char_u *)"F32"},
2321 {K_F33, (char_u *)"F33"},
2322 {K_F34, (char_u *)"F34"},
2323 {K_F35, (char_u *)"F35"},
2324 {K_F36, (char_u *)"F36"},
2325 {K_F37, (char_u *)"F37"},
2326
2327 {K_XF1, (char_u *)"xF1"},
2328 {K_XF2, (char_u *)"xF2"},
2329 {K_XF3, (char_u *)"xF3"},
2330 {K_XF4, (char_u *)"xF4"},
2331
2332 {K_HELP, (char_u *)"Help"},
2333 {K_UNDO, (char_u *)"Undo"},
2334 {K_INS, (char_u *)"Insert"},
2335 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2336 {K_KINS, (char_u *)"kInsert"},
2337 {K_HOME, (char_u *)"Home"},
2338 {K_KHOME, (char_u *)"kHome"},
2339 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002340 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {K_END, (char_u *)"End"},
2342 {K_KEND, (char_u *)"kEnd"},
2343 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002344 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {K_PAGEUP, (char_u *)"PageUp"},
2346 {K_PAGEDOWN, (char_u *)"PageDown"},
2347 {K_KPAGEUP, (char_u *)"kPageUp"},
2348 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2349
2350 {K_KPLUS, (char_u *)"kPlus"},
2351 {K_KMINUS, (char_u *)"kMinus"},
2352 {K_KDIVIDE, (char_u *)"kDivide"},
2353 {K_KMULTIPLY, (char_u *)"kMultiply"},
2354 {K_KENTER, (char_u *)"kEnter"},
2355 {K_KPOINT, (char_u *)"kPoint"},
2356
2357 {K_K0, (char_u *)"k0"},
2358 {K_K1, (char_u *)"k1"},
2359 {K_K2, (char_u *)"k2"},
2360 {K_K3, (char_u *)"k3"},
2361 {K_K4, (char_u *)"k4"},
2362 {K_K5, (char_u *)"k5"},
2363 {K_K6, (char_u *)"k6"},
2364 {K_K7, (char_u *)"k7"},
2365 {K_K8, (char_u *)"k8"},
2366 {K_K9, (char_u *)"k9"},
2367
2368 {'<', (char_u *)"lt"},
2369
2370 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002371#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002373#endif
2374#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002376#endif
2377#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002379#endif
2380#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002382#endif
2383#ifdef FEAT_MOUSE_URXVT
2384 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2385#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002386#ifdef FEAT_MOUSE_SGR
2387 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
2388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2390 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2391 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2392 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2393 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2394 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2395 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2396 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2397 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2398 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2399 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002400 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2401 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2402 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2403 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2404 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2405 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {K_X1MOUSE, (char_u *)"X1Mouse"},
2407 {K_X1DRAG, (char_u *)"X1Drag"},
2408 {K_X1RELEASE, (char_u *)"X1Release"},
2409 {K_X2MOUSE, (char_u *)"X2Mouse"},
2410 {K_X2DRAG, (char_u *)"X2Drag"},
2411 {K_X2RELEASE, (char_u *)"X2Release"},
2412 {K_DROP, (char_u *)"Drop"},
2413 {K_ZERO, (char_u *)"Nul"},
2414#ifdef FEAT_EVAL
2415 {K_SNR, (char_u *)"SNR"},
2416#endif
2417 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002418 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {0, NULL}
2420};
2421
2422#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2423
2424#ifdef FEAT_MOUSE
2425static struct mousetable
2426{
2427 int pseudo_code; /* Code for pseudo mouse event */
2428 int button; /* Which mouse button is it? */
2429 int is_click; /* Is it a mouse button click event? */
2430 int is_drag; /* Is it a mouse drag event? */
2431} mouse_table[] =
2432{
2433 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2434#ifdef FEAT_GUI
2435 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2436#endif
2437 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2438 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2439#ifdef FEAT_GUI
2440 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2441#endif
2442 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2443 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2444 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2445 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2446 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2447 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2448 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2449 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2450 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2451 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2452 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2453 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2454 /* DRAG without CLICK */
2455 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2456 /* RELEASE without CLICK */
2457 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2458 {0, 0, 0, 0},
2459};
2460#endif /* FEAT_MOUSE */
2461
2462/*
2463 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2464 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2465 */
2466 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002467name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 int i;
2470
2471 c = TOUPPER_ASC(c);
2472 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2473 if (c == mod_mask_table[i].name)
2474 return mod_mask_table[i].mod_flag;
2475 return 0;
2476}
2477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478/*
2479 * Check if if there is a special key code for "key" that includes the
2480 * modifiers specified.
2481 */
2482 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002483simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
2485 int i;
2486 int key0;
2487 int key1;
2488
2489 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2490 {
2491 /* TAB is a special case */
2492 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2493 {
2494 *modifiers &= ~MOD_MASK_SHIFT;
2495 return K_S_TAB;
2496 }
2497 key0 = KEY2TERMCAP0(key);
2498 key1 = KEY2TERMCAP1(key);
2499 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2500 if (key0 == modifier_keys_table[i + 3]
2501 && key1 == modifier_keys_table[i + 4]
2502 && (*modifiers & modifier_keys_table[i]))
2503 {
2504 *modifiers &= ~modifier_keys_table[i];
2505 return TERMCAP2KEY(modifier_keys_table[i + 1],
2506 modifier_keys_table[i + 2]);
2507 }
2508 }
2509 return key;
2510}
2511
2512/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002513 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002514 */
2515 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002516handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002517{
2518 switch (key)
2519 {
2520 case K_XUP: return K_UP;
2521 case K_XDOWN: return K_DOWN;
2522 case K_XLEFT: return K_LEFT;
2523 case K_XRIGHT: return K_RIGHT;
2524 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002525 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002526 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002527 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002528 case K_XF1: return K_F1;
2529 case K_XF2: return K_F2;
2530 case K_XF3: return K_F3;
2531 case K_XF4: return K_F4;
2532 case K_S_XF1: return K_S_F1;
2533 case K_S_XF2: return K_S_F2;
2534 case K_S_XF3: return K_S_F3;
2535 case K_S_XF4: return K_S_F4;
2536 }
2537 return key;
2538}
2539
2540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 * Return a string which contains the name of the given key when the given
2542 * modifiers are down.
2543 */
2544 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002545get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 static char_u string[MAX_KEY_NAME_LEN + 1];
2548
2549 int i, idx;
2550 int table_idx;
2551 char_u *s;
2552
2553 string[0] = '<';
2554 idx = 1;
2555
2556 /* Key that stands for a normal character. */
2557 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2558 c = KEY2TERMCAP1(c);
2559
2560 /*
2561 * Translate shifted special keys into unshifted keys and set modifier.
2562 * Same for CTRL and ALT modifiers.
2563 */
2564 if (IS_SPECIAL(c))
2565 {
2566 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2567 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2568 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2569 {
2570 modifiers |= modifier_keys_table[i];
2571 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2572 modifier_keys_table[i + 4]);
2573 break;
2574 }
2575 }
2576
2577 /* try to find the key in the special key table */
2578 table_idx = find_special_key_in_table(c);
2579
2580 /*
2581 * When not a known special key, and not a printable character, try to
2582 * extract modifiers.
2583 */
2584 if (c > 0
2585#ifdef FEAT_MBYTE
2586 && (*mb_char2len)(c) == 1
2587#endif
2588 )
2589 {
2590 if (table_idx < 0
2591 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2592 && (c & 0x80))
2593 {
2594 c &= 0x7f;
2595 modifiers |= MOD_MASK_ALT;
2596 /* try again, to find the un-alted key in the special key table */
2597 table_idx = find_special_key_in_table(c);
2598 }
2599 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2600 {
2601#ifdef EBCDIC
2602 c = CtrlChar(c);
2603#else
2604 c += '@';
2605#endif
2606 modifiers |= MOD_MASK_CTRL;
2607 }
2608 }
2609
2610 /* translate the modifier into a string */
2611 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2612 if ((modifiers & mod_mask_table[i].mod_mask)
2613 == mod_mask_table[i].mod_flag)
2614 {
2615 string[idx++] = mod_mask_table[i].name;
2616 string[idx++] = (char_u)'-';
2617 }
2618
2619 if (table_idx < 0) /* unknown special key, may output t_xx */
2620 {
2621 if (IS_SPECIAL(c))
2622 {
2623 string[idx++] = 't';
2624 string[idx++] = '_';
2625 string[idx++] = KEY2TERMCAP0(c);
2626 string[idx++] = KEY2TERMCAP1(c);
2627 }
2628 /* Not a special key, only modifiers, output directly */
2629 else
2630 {
2631#ifdef FEAT_MBYTE
2632 if (has_mbyte && (*mb_char2len)(c) > 1)
2633 idx += (*mb_char2bytes)(c, string + idx);
2634 else
2635#endif
2636 if (vim_isprintc(c))
2637 string[idx++] = c;
2638 else
2639 {
2640 s = transchar(c);
2641 while (*s)
2642 string[idx++] = *s++;
2643 }
2644 }
2645 }
2646 else /* use name of special key */
2647 {
2648 STRCPY(string + idx, key_names_table[table_idx].name);
2649 idx = (int)STRLEN(string);
2650 }
2651 string[idx++] = '>';
2652 string[idx] = NUL;
2653 return string;
2654}
2655
2656/*
2657 * Try translating a <> name at (*srcp)[] to dst[].
2658 * Return the number of characters added to dst[], zero for no match.
2659 * If there is a match, srcp is advanced to after the <> name.
2660 * dst[] must be big enough to hold the result (up to six characters)!
2661 */
2662 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002663trans_special(
2664 char_u **srcp,
2665 char_u *dst,
2666 int keycode) /* prefer key code, e.g. K_DEL instead of DEL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668 int modifiers = 0;
2669 int key;
2670 int dlen = 0;
2671
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002672 key = find_special_key(srcp, &modifiers, keycode, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (key == 0)
2674 return 0;
2675
2676 /* Put the appropriate modifier in a string */
2677 if (modifiers != 0)
2678 {
2679 dst[dlen++] = K_SPECIAL;
2680 dst[dlen++] = KS_MODIFIER;
2681 dst[dlen++] = modifiers;
2682 }
2683
2684 if (IS_SPECIAL(key))
2685 {
2686 dst[dlen++] = K_SPECIAL;
2687 dst[dlen++] = KEY2TERMCAP0(key);
2688 dst[dlen++] = KEY2TERMCAP1(key);
2689 }
2690#ifdef FEAT_MBYTE
2691 else if (has_mbyte && !keycode)
2692 dlen += (*mb_char2bytes)(key, dst + dlen);
2693#endif
2694 else if (keycode)
2695 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2696 else
2697 dst[dlen++] = key;
2698
2699 return dlen;
2700}
2701
2702/*
2703 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2704 * srcp is advanced to after the <> name.
2705 * returns 0 if there is no match.
2706 */
2707 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002708find_special_key(
2709 char_u **srcp,
2710 int *modp,
2711 int keycode, /* prefer key code, e.g. K_DEL instead of DEL */
2712 int keep_x_key) /* don't translate xHome to Home key */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 char_u *last_dash;
2715 char_u *end_of_name;
2716 char_u *src;
2717 char_u *bp;
2718 int modifiers;
2719 int bit;
2720 int key;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002721 unsigned long n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002722 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
2724 src = *srcp;
2725 if (src[0] != '<')
2726 return 0;
2727
2728 /* Find end of modifier list */
2729 last_dash = src;
2730 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2731 {
2732 if (*bp == '-')
2733 {
2734 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002735 if (bp[1] != NUL)
2736 {
2737#ifdef FEAT_MBYTE
2738 if (has_mbyte)
2739 l = mb_ptr2len(bp + 1);
2740 else
2741#endif
2742 l = 1;
2743 if (bp[l + 1] == '>')
2744 bp += l; /* anything accepted, like <C-?> */
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
2747 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2748 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002749 else if (STRNICMP(bp, "char-", 5) == 0)
2750 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002751 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002752 bp += l + 5;
2753 break;
2754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756
2757 if (*bp == '>') /* found matching '>' */
2758 {
2759 end_of_name = bp + 1;
2760
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 /* Which modifiers are given? */
2762 modifiers = 0x0;
2763 for (bp = src + 1; bp < last_dash; bp++)
2764 {
2765 if (*bp != '-')
2766 {
2767 bit = name_to_mod_mask(*bp);
2768 if (bit == 0x0)
2769 break; /* Illegal modifier name */
2770 modifiers |= bit;
2771 }
2772 }
2773
2774 /*
2775 * Legal modifier name.
2776 */
2777 if (bp >= last_dash)
2778 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002779 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2780 && VIM_ISDIGIT(last_dash[6]))
2781 {
2782 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002783 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002784 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002787 {
Bram Moolenaar792826c2011-08-19 22:29:02 +02002788 /*
2789 * Modifier with single letter, or special key name.
2790 */
2791#ifdef FEAT_MBYTE
2792 if (has_mbyte)
2793 l = mb_ptr2len(last_dash + 1);
2794 else
2795#endif
2796 l = 1;
2797 if (modifiers != 0 && last_dash[l + 1] == '>')
2798 key = PTR2CHAR(last_dash + 1);
2799 else
2800 {
2801 key = get_special_key_code(last_dash + 1);
2802 if (!keep_x_key)
2803 key = handle_x_keys(key);
2804 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
2807 /*
2808 * get_special_key_code() may return NUL for invalid
2809 * special key name.
2810 */
2811 if (key != NUL)
2812 {
2813 /*
2814 * Only use a modifier when there is no special key code that
2815 * includes the modifier.
2816 */
2817 key = simplify_key(key, &modifiers);
2818
2819 if (!keycode)
2820 {
2821 /* don't want keycode, use single byte code */
2822 if (key == K_BS)
2823 key = BS;
2824 else if (key == K_DEL || key == K_KDEL)
2825 key = DEL;
2826 }
2827
2828 /*
2829 * Normal Key with modifier: Try to make a single byte code.
2830 */
2831 if (!IS_SPECIAL(key))
2832 key = extract_modifiers(key, &modifiers);
2833
2834 *modp = modifiers;
2835 *srcp = end_of_name;
2836 return key;
2837 }
2838 }
2839 }
2840 return 0;
2841}
2842
2843/*
2844 * Try to include modifiers in the key.
2845 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2846 */
2847 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002848extract_modifiers(int key, int *modp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 int modifiers = *modp;
2851
2852#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002853 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 if (!(modifiers & MOD_MASK_CMD))
2855#endif
2856 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2857 {
2858 key = TOUPPER_ASC(key);
2859 modifiers &= ~MOD_MASK_SHIFT;
2860 }
2861 if ((modifiers & MOD_MASK_CTRL)
2862#ifdef EBCDIC
2863 /* * TODO: EBCDIC Better use:
2864 * && (Ctrl_chr(key) || key == '?')
2865 * ??? */
2866 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2867 != NULL
2868#else
2869 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2870#endif
2871 )
2872 {
2873 key = Ctrl_chr(key);
2874 modifiers &= ~MOD_MASK_CTRL;
2875 /* <C-@> is <Nul> */
2876 if (key == 0)
2877 key = K_ZERO;
2878 }
2879#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002880 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (!(modifiers & MOD_MASK_CMD))
2882#endif
2883 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2884#ifdef FEAT_MBYTE
2885 && !enc_dbcs /* avoid creating a lead byte */
2886#endif
2887 )
2888 {
2889 key |= 0x80;
2890 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2891 }
2892
2893 *modp = modifiers;
2894 return key;
2895}
2896
2897/*
2898 * Try to find key "c" in the special key table.
2899 * Return the index when found, -1 when not found.
2900 */
2901 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002902find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 int i;
2905
2906 for (i = 0; key_names_table[i].name != NULL; i++)
2907 if (c == key_names_table[i].key)
2908 break;
2909 if (key_names_table[i].name == NULL)
2910 i = -1;
2911 return i;
2912}
2913
2914/*
2915 * Find the special key with the given name (the given string does not have to
2916 * end with NUL, the name is assumed to end before the first non-idchar).
2917 * If the name starts with "t_" the next two characters are interpreted as a
2918 * termcap name.
2919 * Return the key code, or 0 if not found.
2920 */
2921 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002922get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 char_u *table_name;
2925 char_u string[3];
2926 int i, j;
2927
2928 /*
2929 * If it's <t_xx> we get the code for xx from the termcap
2930 */
2931 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
2932 {
2933 string[0] = name[2];
2934 string[1] = name[3];
2935 string[2] = NUL;
2936 if (add_termcap_entry(string, FALSE) == OK)
2937 return TERMCAP2KEY(name[2], name[3]);
2938 }
2939 else
2940 for (i = 0; key_names_table[i].name != NULL; i++)
2941 {
2942 table_name = key_names_table[i].name;
2943 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
2944 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
2945 break;
2946 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
2947 return key_names_table[i].key;
2948 }
2949 return 0;
2950}
2951
Bram Moolenaar05bb9532008-07-04 09:44:11 +00002952#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002954get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00002956 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 return NULL;
2958 return key_names_table[i].name;
2959}
2960#endif
2961
Bram Moolenaar05bb9532008-07-04 09:44:11 +00002962#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963/*
2964 * Look up the given mouse code to return the relevant information in the other
2965 * arguments. Return which button is down or was released.
2966 */
2967 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002968get_mouse_button(int code, int *is_click, int *is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 int i;
2971
2972 for (i = 0; mouse_table[i].pseudo_code; i++)
2973 if (code == mouse_table[i].pseudo_code)
2974 {
2975 *is_click = mouse_table[i].is_click;
2976 *is_drag = mouse_table[i].is_drag;
2977 return mouse_table[i].button;
2978 }
2979 return 0; /* Shouldn't get here */
2980}
2981
2982/*
2983 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
2984 * the given information about which mouse button is down, and whether the
2985 * mouse was clicked, dragged or released.
2986 */
2987 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002988get_pseudo_mouse_code(
2989 int button, /* eg MOUSE_LEFT */
2990 int is_click,
2991 int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 int i;
2994
2995 for (i = 0; mouse_table[i].pseudo_code; i++)
2996 if (button == mouse_table[i].button
2997 && is_click == mouse_table[i].is_click
2998 && is_drag == mouse_table[i].is_drag)
2999 {
3000#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003001 /* Trick: a non mappable left click and release has mouse_col -1
3002 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3003 * gui_mouse_moved() */
3004 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003006 if (mouse_col < 0)
3007 mouse_col = 0;
3008 else
3009 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3011 return (int)KE_LEFTMOUSE_NM;
3012 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3013 return (int)KE_LEFTRELEASE_NM;
3014 }
3015#endif
3016 return mouse_table[i].pseudo_code;
3017 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003018 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020#endif /* FEAT_MOUSE */
3021
3022/*
3023 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3024 */
3025 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003026get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 int c = *buf->b_p_ff;
3029
3030 if (buf->b_p_bin || c == 'u')
3031 return EOL_UNIX;
3032 if (c == 'm')
3033 return EOL_MAC;
3034 return EOL_DOS;
3035}
3036
3037/*
3038 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3039 * argument.
3040 */
3041 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003042get_fileformat_force(
3043 buf_T *buf,
3044 exarg_T *eap) /* can be NULL! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 int c;
3047
3048 if (eap != NULL && eap->force_ff != 0)
3049 c = eap->cmd[eap->force_ff];
3050 else
3051 {
3052 if ((eap != NULL && eap->force_bin != 0)
3053 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3054 return EOL_UNIX;
3055 c = *buf->b_p_ff;
3056 }
3057 if (c == 'u')
3058 return EOL_UNIX;
3059 if (c == 'm')
3060 return EOL_MAC;
3061 return EOL_DOS;
3062}
3063
3064/*
3065 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3066 * Sets both 'textmode' and 'fileformat'.
3067 * Note: Does _not_ set global value of 'textmode'!
3068 */
3069 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070set_fileformat(
3071 int t,
3072 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 char *p = NULL;
3075
3076 switch (t)
3077 {
3078 case EOL_DOS:
3079 p = FF_DOS;
3080 curbuf->b_p_tx = TRUE;
3081 break;
3082 case EOL_UNIX:
3083 p = FF_UNIX;
3084 curbuf->b_p_tx = FALSE;
3085 break;
3086 case EOL_MAC:
3087 p = FF_MAC;
3088 curbuf->b_p_tx = FALSE;
3089 break;
3090 }
3091 if (p != NULL)
3092 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003093 OPT_FREE | opt_flags, 0);
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00003096 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003098 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#endif
3100#ifdef FEAT_TITLE
3101 need_maketitle = TRUE; /* set window title later */
3102#endif
3103}
3104
3105/*
3106 * Return the default fileformat from 'fileformats'.
3107 */
3108 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003109default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 switch (*p_ffs)
3112 {
3113 case 'm': return EOL_MAC;
3114 case 'd': return EOL_DOS;
3115 }
3116 return EOL_UNIX;
3117}
3118
3119/*
3120 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3121 */
3122 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003123call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 char_u *ncmd;
3126 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003127#ifdef FEAT_PROFILE
3128 proftime_T wait_time;
3129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 if (p_verbose > 3)
3132 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003133 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003134 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 cmd == NULL ? p_sh : cmd);
3136 out_char('\n');
3137 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003138 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
3140
Bram Moolenaar05159a02005-02-26 23:04:13 +00003141#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003142 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003143 prof_child_enter(&wait_time);
3144#endif
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (*p_sh == NUL)
3147 {
3148 EMSG(_(e_shellempty));
3149 retval = -1;
3150 }
3151 else
3152 {
3153#ifdef FEAT_GUI_MSWIN
3154 /* Don't hide the pointer while executing a shell command. */
3155 gui_mch_mousehide(FALSE);
3156#endif
3157#ifdef FEAT_GUI
3158 ++hold_gui_events;
3159#endif
3160 /* The external command may update a tags file, clear cached tags. */
3161 tag_freematch();
3162
3163 if (cmd == NULL || *p_sxq == NUL)
3164 retval = mch_call_shell(cmd, opt);
3165 else
3166 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003167 char_u *ecmd = cmd;
3168
3169 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3170 {
3171 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3172 if (ecmd == NULL)
3173 ecmd = cmd;
3174 }
3175 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (ncmd != NULL)
3177 {
3178 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003179 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003180 /* When 'shellxquote' is ( append ).
3181 * When 'shellxquote' is "( append )". */
3182 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3183 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3184 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 retval = mch_call_shell(ncmd, opt);
3186 vim_free(ncmd);
3187 }
3188 else
3189 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003190 if (ecmd != cmd)
3191 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193#ifdef FEAT_GUI
3194 --hold_gui_events;
3195#endif
3196 /*
3197 * Check the window size, in case it changed while executing the
3198 * external command.
3199 */
3200 shell_resized_check();
3201 }
3202
3203#ifdef FEAT_EVAL
3204 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003205# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003206 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003207 prof_child_exit(&wait_time);
3208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#endif
3210
3211 return retval;
3212}
3213
3214/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003215 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3216 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 */
3218 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003219get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221 if (State & NORMAL)
3222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003224 {
3225 if (VIsual_select)
3226 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003228 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003229 else if (finish_op)
3230 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232 return State;
3233}
3234
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003235#if defined(FEAT_MBYTE) || defined(PROTO)
3236/*
3237 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003238 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003239 * "b" must point to the start of the file name
3240 */
3241 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003242after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003243{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003244 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003245 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3246}
3247#endif
3248
3249/*
3250 * Return TRUE if file names "f1" and "f2" are in the same directory.
3251 * "f1" may be a short name, "f2" must be a full path.
3252 */
3253 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003255{
3256 char_u ffname[MAXPATHL];
3257 char_u *t1;
3258 char_u *t2;
3259
3260 /* safety check */
3261 if (f1 == NULL || f2 == NULL)
3262 return FALSE;
3263
3264 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3265 t1 = gettail_sep(ffname);
3266 t2 = gettail_sep(f2);
3267 return (t1 - ffname == t2 - f2
3268 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3269}
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00003272 || ((defined(FEAT_GUI_GTK)) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00003273 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3275 || defined(PROTO)
3276/*
3277 * Change to a file's directory.
3278 * Caller must call shorten_fnames()!
3279 * Return OK or FAIL.
3280 */
3281 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003282vim_chdirfile(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003284 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003286 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003287 *gettail_sep(dir) = NUL;
3288 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289}
3290#endif
3291
3292#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3293/*
3294 * Check if "name" ends in a slash and is not a directory.
3295 * Used for systems where stat() ignores a trailing slash on a file name.
3296 * The Vim code assumes a trailing slash is only ignored for a directory.
3297 */
3298 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003299illegal_slash(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 if (name[0] == NUL)
3302 return FALSE; /* no file name is not illegal */
3303 if (name[strlen(name) - 1] != '/')
3304 return FALSE; /* no trailing slash */
3305 if (mch_isdir((char_u *)name))
3306 return FALSE; /* trailing slash for a directory */
3307 return TRUE;
3308}
3309#endif
3310
3311#if defined(CURSOR_SHAPE) || defined(PROTO)
3312
3313/*
3314 * Handling of cursor and mouse pointer shapes in various modes.
3315 */
3316
3317cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3318{
3319 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3320 * defaults when Vim starts.
3321 * Adjust the SHAPE_IDX_ defines when making changes! */
3322 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3323 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3324 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3325 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3326 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3327 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3328 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3329 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3330 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3331 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3332 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3333 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3334 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3335 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3336 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3337 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3338 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3339};
3340
3341#ifdef FEAT_MOUSESHAPE
3342/*
3343 * Table with names for mouse shapes. Keep in sync with all the tables for
3344 * mch_set_mouse_shape()!.
3345 */
3346static char * mshape_names[] =
3347{
3348 "arrow", /* default, must be the first one */
3349 "blank", /* hidden */
3350 "beam",
3351 "updown",
3352 "udsizing",
3353 "leftright",
3354 "lrsizing",
3355 "busy",
3356 "no",
3357 "crosshair",
3358 "hand1",
3359 "hand2",
3360 "pencil",
3361 "question",
3362 "rightup-arrow",
3363 "up-arrow",
3364 NULL
3365};
3366#endif
3367
3368/*
3369 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3370 * ("what" is SHAPE_MOUSE).
3371 * Returns error message for an illegal option, NULL otherwise.
3372 */
3373 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003374parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 char_u *modep;
3377 char_u *colonp;
3378 char_u *commap;
3379 char_u *slashp;
3380 char_u *p, *endp;
3381 int idx = 0; /* init for GCC */
3382 int all_idx;
3383 int len;
3384 int i;
3385 long n;
3386 int found_ve = FALSE; /* found "ve" flag */
3387 int round;
3388
3389 /*
3390 * First round: check for errors; second round: do it for real.
3391 */
3392 for (round = 1; round <= 2; ++round)
3393 {
3394 /*
3395 * Repeat for all comma separated parts.
3396 */
3397#ifdef FEAT_MOUSESHAPE
3398 if (what == SHAPE_MOUSE)
3399 modep = p_mouseshape;
3400 else
3401#endif
3402 modep = p_guicursor;
3403 while (*modep != NUL)
3404 {
3405 colonp = vim_strchr(modep, ':');
3406 if (colonp == NULL)
3407 return (char_u *)N_("E545: Missing colon");
3408 if (colonp == modep)
3409 return (char_u *)N_("E546: Illegal mode");
3410 commap = vim_strchr(modep, ',');
3411
3412 /*
3413 * Repeat for all mode's before the colon.
3414 * For the 'a' mode, we loop to handle all the modes.
3415 */
3416 all_idx = -1;
3417 while (modep < colonp || all_idx >= 0)
3418 {
3419 if (all_idx < 0)
3420 {
3421 /* Find the mode. */
3422 if (modep[1] == '-' || modep[1] == ':')
3423 len = 1;
3424 else
3425 len = 2;
3426 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3427 all_idx = SHAPE_IDX_COUNT - 1;
3428 else
3429 {
3430 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3431 if (STRNICMP(modep, shape_table[idx].name, len)
3432 == 0)
3433 break;
3434 if (idx == SHAPE_IDX_COUNT
3435 || (shape_table[idx].used_for & what) == 0)
3436 return (char_u *)N_("E546: Illegal mode");
3437 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3438 found_ve = TRUE;
3439 }
3440 modep += len + 1;
3441 }
3442
3443 if (all_idx >= 0)
3444 idx = all_idx--;
3445 else if (round == 2)
3446 {
3447#ifdef FEAT_MOUSESHAPE
3448 if (what == SHAPE_MOUSE)
3449 {
3450 /* Set the default, for the missing parts */
3451 shape_table[idx].mshape = 0;
3452 }
3453 else
3454#endif
3455 {
3456 /* Set the defaults, for the missing parts */
3457 shape_table[idx].shape = SHAPE_BLOCK;
3458 shape_table[idx].blinkwait = 700L;
3459 shape_table[idx].blinkon = 400L;
3460 shape_table[idx].blinkoff = 250L;
3461 }
3462 }
3463
3464 /* Parse the part after the colon */
3465 for (p = colonp + 1; *p && *p != ','; )
3466 {
3467#ifdef FEAT_MOUSESHAPE
3468 if (what == SHAPE_MOUSE)
3469 {
3470 for (i = 0; ; ++i)
3471 {
3472 if (mshape_names[i] == NULL)
3473 {
3474 if (!VIM_ISDIGIT(*p))
3475 return (char_u *)N_("E547: Illegal mouseshape");
3476 if (round == 2)
3477 shape_table[idx].mshape =
3478 getdigits(&p) + MSHAPE_NUMBERED;
3479 else
3480 (void)getdigits(&p);
3481 break;
3482 }
3483 len = (int)STRLEN(mshape_names[i]);
3484 if (STRNICMP(p, mshape_names[i], len) == 0)
3485 {
3486 if (round == 2)
3487 shape_table[idx].mshape = i;
3488 p += len;
3489 break;
3490 }
3491 }
3492 }
3493 else /* if (what == SHAPE_MOUSE) */
3494#endif
3495 {
3496 /*
3497 * First handle the ones with a number argument.
3498 */
3499 i = *p;
3500 len = 0;
3501 if (STRNICMP(p, "ver", 3) == 0)
3502 len = 3;
3503 else if (STRNICMP(p, "hor", 3) == 0)
3504 len = 3;
3505 else if (STRNICMP(p, "blinkwait", 9) == 0)
3506 len = 9;
3507 else if (STRNICMP(p, "blinkon", 7) == 0)
3508 len = 7;
3509 else if (STRNICMP(p, "blinkoff", 8) == 0)
3510 len = 8;
3511 if (len != 0)
3512 {
3513 p += len;
3514 if (!VIM_ISDIGIT(*p))
3515 return (char_u *)N_("E548: digit expected");
3516 n = getdigits(&p);
3517 if (len == 3) /* "ver" or "hor" */
3518 {
3519 if (n == 0)
3520 return (char_u *)N_("E549: Illegal percentage");
3521 if (round == 2)
3522 {
3523 if (TOLOWER_ASC(i) == 'v')
3524 shape_table[idx].shape = SHAPE_VER;
3525 else
3526 shape_table[idx].shape = SHAPE_HOR;
3527 shape_table[idx].percentage = n;
3528 }
3529 }
3530 else if (round == 2)
3531 {
3532 if (len == 9)
3533 shape_table[idx].blinkwait = n;
3534 else if (len == 7)
3535 shape_table[idx].blinkon = n;
3536 else
3537 shape_table[idx].blinkoff = n;
3538 }
3539 }
3540 else if (STRNICMP(p, "block", 5) == 0)
3541 {
3542 if (round == 2)
3543 shape_table[idx].shape = SHAPE_BLOCK;
3544 p += 5;
3545 }
3546 else /* must be a highlight group name then */
3547 {
3548 endp = vim_strchr(p, '-');
3549 if (commap == NULL) /* last part */
3550 {
3551 if (endp == NULL)
3552 endp = p + STRLEN(p); /* find end of part */
3553 }
3554 else if (endp > commap || endp == NULL)
3555 endp = commap;
3556 slashp = vim_strchr(p, '/');
3557 if (slashp != NULL && slashp < endp)
3558 {
3559 /* "group/langmap_group" */
3560 i = syn_check_group(p, (int)(slashp - p));
3561 p = slashp + 1;
3562 }
3563 if (round == 2)
3564 {
3565 shape_table[idx].id = syn_check_group(p,
3566 (int)(endp - p));
3567 shape_table[idx].id_lm = shape_table[idx].id;
3568 if (slashp != NULL && slashp < endp)
3569 shape_table[idx].id = i;
3570 }
3571 p = endp;
3572 }
3573 } /* if (what != SHAPE_MOUSE) */
3574
3575 if (*p == '-')
3576 ++p;
3577 }
3578 }
3579 modep = p;
3580 if (*modep == ',')
3581 ++modep;
3582 }
3583 }
3584
3585 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3586 if (!found_ve)
3587 {
3588#ifdef FEAT_MOUSESHAPE
3589 if (what == SHAPE_MOUSE)
3590 {
3591 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3592 }
3593 else
3594#endif
3595 {
3596 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3597 shape_table[SHAPE_IDX_VE].percentage =
3598 shape_table[SHAPE_IDX_V].percentage;
3599 shape_table[SHAPE_IDX_VE].blinkwait =
3600 shape_table[SHAPE_IDX_V].blinkwait;
3601 shape_table[SHAPE_IDX_VE].blinkon =
3602 shape_table[SHAPE_IDX_V].blinkon;
3603 shape_table[SHAPE_IDX_VE].blinkoff =
3604 shape_table[SHAPE_IDX_V].blinkoff;
3605 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3606 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3607 }
3608 }
3609
3610 return NULL;
3611}
3612
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003613# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3614 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615/*
3616 * Return the index into shape_table[] for the current mode.
3617 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3618 */
3619 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003620get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621{
3622#ifdef FEAT_MOUSESHAPE
3623 if (mouse && (State == HITRETURN || State == ASKMORE))
3624 {
3625# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003626 int x, y;
3627 gui_mch_getmouse(&x, &y);
3628 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 return SHAPE_IDX_MOREL;
3630# endif
3631 return SHAPE_IDX_MORE;
3632 }
3633 if (mouse && drag_status_line)
3634 return SHAPE_IDX_SDRAG;
3635# ifdef FEAT_VERTSPLIT
3636 if (mouse && drag_sep_line)
3637 return SHAPE_IDX_VDRAG;
3638# endif
3639#endif
3640 if (!mouse && State == SHOWMATCH)
3641 return SHAPE_IDX_SM;
3642#ifdef FEAT_VREPLACE
3643 if (State & VREPLACE_FLAG)
3644 return SHAPE_IDX_R;
3645#endif
3646 if (State & REPLACE_FLAG)
3647 return SHAPE_IDX_R;
3648 if (State & INSERT)
3649 return SHAPE_IDX_I;
3650 if (State & CMDLINE)
3651 {
3652 if (cmdline_at_end())
3653 return SHAPE_IDX_C;
3654 if (cmdline_overstrike())
3655 return SHAPE_IDX_CR;
3656 return SHAPE_IDX_CI;
3657 }
3658 if (finish_op)
3659 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (VIsual_active)
3661 {
3662 if (*p_sel == 'e')
3663 return SHAPE_IDX_VE;
3664 else
3665 return SHAPE_IDX_V;
3666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return SHAPE_IDX_N;
3668}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3672static int old_mouse_shape = 0;
3673
3674/*
3675 * Set the mouse shape:
3676 * If "shape" is -1, use shape depending on the current mode,
3677 * depending on the current state.
3678 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3679 * when the mouse moves off the status or command line).
3680 */
3681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003682update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 int new_mouse_shape;
3685
3686 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003687 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return;
3689
3690 /* Postpone the updating when more is to come. Speeds up executing of
3691 * mappings. */
3692 if (shape_idx == -1 && char_avail())
3693 {
3694 postponed_mouseshape = TRUE;
3695 return;
3696 }
3697
Bram Moolenaar14716812006-05-04 21:54:08 +00003698 /* When ignoring the mouse don't change shape on the statusline. */
3699 if (*p_mouse == NUL
3700 && (shape_idx == SHAPE_IDX_CLINE
3701 || shape_idx == SHAPE_IDX_STATUS
3702 || shape_idx == SHAPE_IDX_VSEP))
3703 shape_idx = -2;
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (shape_idx == -2
3706 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3707 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3708 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3709 return;
3710 if (shape_idx < 0)
3711 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3712 else
3713 new_mouse_shape = shape_table[shape_idx].mshape;
3714 if (new_mouse_shape != old_mouse_shape)
3715 {
3716 mch_set_mouse_shape(new_mouse_shape);
3717 old_mouse_shape = new_mouse_shape;
3718 }
3719 postponed_mouseshape = FALSE;
3720}
3721# endif
3722
3723#endif /* CURSOR_SHAPE */
3724
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726/* TODO: make some #ifdef for this */
3727/*--------[ file searching ]-------------------------------------------------*/
3728/*
3729 * File searching functions for 'path', 'tags' and 'cdpath' options.
3730 * External visible functions:
3731 * vim_findfile_init() creates/initialises the search context
3732 * vim_findfile_free_visited() free list of visited files/dirs of search
3733 * context
3734 * vim_findfile() find a file in the search context
3735 * vim_findfile_cleanup() cleanup/free search context created by
3736 * vim_findfile_init()
3737 *
3738 * All static functions and variables start with 'ff_'
3739 *
3740 * In general it works like this:
3741 * First you create yourself a search context by calling vim_findfile_init().
3742 * It is possible to give a search context from a previous call to
3743 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3744 * until you are satisfied with the result or it returns NULL. On every call it
3745 * returns the next file which matches the conditions given to
3746 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3747 *
3748 * It is possible to call vim_findfile_init() again to reinitialise your search
3749 * with some new parameters. Don't forget to pass your old search context to
3750 * it, so it can reuse it and especially reuse the list of already visited
3751 * directories. If you want to delete the list of already visited directories
3752 * simply call vim_findfile_free_visited().
3753 *
3754 * When you are done call vim_findfile_cleanup() to free the search context.
3755 *
3756 * The function vim_findfile_init() has a long comment, which describes the
3757 * needed parameters.
3758 *
3759 *
3760 *
3761 * ATTENTION:
3762 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003763 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 * thread-safe!!!!!
3765 *
3766 * To minimize parameter passing (or because I'm to lazy), only the
3767 * external visible functions get a search context as a parameter. This is
3768 * then assigned to a static global, which is used throughout the local
3769 * functions.
3770 */
3771
3772/*
3773 * type for the directory search stack
3774 */
3775typedef struct ff_stack
3776{
3777 struct ff_stack *ffs_prev;
3778
3779 /* the fix part (no wildcards) and the part containing the wildcards
3780 * of the search path
3781 */
3782 char_u *ffs_fix_path;
3783#ifdef FEAT_PATH_EXTRA
3784 char_u *ffs_wc_path;
3785#endif
3786
3787 /* files/dirs found in the above directory, matched by the first wildcard
3788 * of wc_part
3789 */
3790 char_u **ffs_filearray;
3791 int ffs_filearray_size;
3792 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3793
3794 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003795 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 int ffs_stage;
3799
3800 /* How deep are we in the directory tree?
3801 * Counts backward from value of level parameter to vim_findfile_init
3802 */
3803 int ffs_level;
3804
3805 /* Did we already expand '**' to an empty string? */
3806 int ffs_star_star_empty;
3807} ff_stack_T;
3808
3809/*
3810 * type for already visited directories or files.
3811 */
3812typedef struct ff_visited
3813{
3814 struct ff_visited *ffv_next;
3815
3816#ifdef FEAT_PATH_EXTRA
3817 /* Visited directories are different if the wildcard string are
3818 * different. So we have to save it.
3819 */
3820 char_u *ffv_wc_path;
3821#endif
3822 /* for unix use inode etc for comparison (needed because of links), else
3823 * use filename.
3824 */
3825#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003826 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3827 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 ino_t ffv_ino; /* inode number */
3829#endif
3830 /* The memory for this struct is allocated according to the length of
3831 * ffv_fname.
3832 */
3833 char_u ffv_fname[1]; /* actually longer */
3834} ff_visited_T;
3835
3836/*
3837 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003838 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3840 * So we have to do 3 searches:
3841 * 1) search from the current files directory downward for the file "tags"
3842 * 2) search from the current files directory downward for the file "TAGS"
3843 * 3) search from Vims current directory downwards for the file "tags"
3844 * As you can see, the first and the third search are for the same file, so for
3845 * the third search we can use the visited list of the first search. For the
3846 * second search we must start from a empty visited list.
3847 * The struct ff_visited_list_hdr is used to manage a linked list of already
3848 * visited lists.
3849 */
3850typedef struct ff_visited_list_hdr
3851{
3852 struct ff_visited_list_hdr *ffvl_next;
3853
3854 /* the filename the attached visited list is for */
3855 char_u *ffvl_filename;
3856
3857 ff_visited_T *ffvl_visited_list;
3858
3859} ff_visited_list_hdr_T;
3860
3861
3862/*
3863 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003864 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 */
3866#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868/*
3869 * The search context:
3870 * ffsc_stack_ptr: the stack for the dirs to search
3871 * ffsc_visited_list: the currently active visited list
3872 * ffsc_dir_visited_list: the currently active visited list for search dirs
3873 * ffsc_visited_lists_list: the list of all visited lists
3874 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3875 * ffsc_file_to_search: the file to search for
3876 * ffsc_start_dir: the starting directory, if search path was relative
3877 * ffsc_fix_path: the fix part of the given path (without wildcards)
3878 * Needed for upward search.
3879 * ffsc_wc_path: the part of the given path containing wildcards
3880 * ffsc_level: how many levels of dirs to search downwards
3881 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003882 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003883 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 */
3885typedef struct ff_search_ctx_T
3886{
3887 ff_stack_T *ffsc_stack_ptr;
3888 ff_visited_list_hdr_T *ffsc_visited_list;
3889 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3890 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3891 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3892 char_u *ffsc_file_to_search;
3893 char_u *ffsc_start_dir;
3894 char_u *ffsc_fix_path;
3895#ifdef FEAT_PATH_EXTRA
3896 char_u *ffsc_wc_path;
3897 int ffsc_level;
3898 char_u **ffsc_stopdirs_v;
3899#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003900 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003901 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003902} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904/* locally needed functions */
3905#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003906static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003908static int ff_check_visited(ff_visited_T **, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003910static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
3911static void ff_free_visited_list(ff_visited_T *vl);
3912static 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 +00003913#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003914static int ff_wc_equal(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#endif
3916
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003917static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
3918static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
3919static void ff_clear(ff_search_ctx_T *search_ctx);
3920static void ff_free_stack_element(ff_stack_T *stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003922static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923#else
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003924static ff_stack_T *ff_create_stack_element(char_u *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#endif
3926#ifdef FEAT_PATH_EXTRA
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003927static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#endif
3929
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01003930static char_u e_pathtoolong[] = N_("E854: path too long for completion");
3931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#if 0
3933/*
3934 * if someone likes findfirst/findnext, here are the functions
3935 * NOT TESTED!!
3936 */
3937
3938static void *ff_fn_search_context = NULL;
3939
3940 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003941vim_findfirst(char_u *path, char_u *filename, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 ff_fn_search_context =
3944 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
3945 ff_fn_search_context, rel_fname);
3946 if (NULL == ff_fn_search_context)
3947 return NULL;
3948 else
3949 return vim_findnext()
3950}
3951
3952 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003953vim_findnext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 char_u *ret = vim_findfile(ff_fn_search_context);
3956
3957 if (NULL == ret)
3958 {
3959 vim_findfile_cleanup(ff_fn_search_context);
3960 ff_fn_search_context = NULL;
3961 }
3962 return ret;
3963}
3964#endif
3965
3966/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02003967 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02003969 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 *
3971 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
3972 * with the search context.
3973 *
3974 * Find the file 'filename' in the directory 'path'.
3975 * The parameter 'path' may contain wildcards. If so only search 'level'
3976 * directories deep. The parameter 'level' is the absolute maximum and is
3977 * not related to restricts given to the '**' wildcard. If 'level' is 100
3978 * and you use '**200' vim_findfile() will stop after 100 levels.
3979 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003980 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
3981 * escape special characters.
3982 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 * If 'stopdirs' is not NULL and nothing is found downward, the search is
3984 * restarted on the next higher directory level. This is repeated until the
3985 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
3986 * format ";*<dirname>*\(;<dirname>\)*;\=$".
3987 *
3988 * If the 'path' is relative, the starting dir for the search is either VIM's
3989 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02003990 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 * the first wildcard.
3992 *
3993 * Upward search is only done on the starting dir.
3994 *
3995 * If 'free_visited' is TRUE the list of already visited files/directories is
3996 * cleared. Set this to FALSE if you just want to search from another
3997 * directory, but want to be sure that no directory from a previous search is
3998 * searched again. This is useful if you search for a file at different places.
3999 * The list of visited files/dirs can also be cleared with the function
4000 * vim_findfile_free_visited().
4001 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004002 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4003 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 *
4005 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004006 * passed in the parameter "search_ctx_arg". This context is reused and
4007 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004009 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4010 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004012 * If you don't have a search context from a previous call "search_ctx_arg"
4013 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 *
4015 * This function silently ignores a few errors, vim_findfile() will have
4016 * limited functionality then.
4017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004019vim_findfile_init(
4020 char_u *path,
4021 char_u *filename,
4022 char_u *stopdirs UNUSED,
4023 int level,
4024 int free_visited,
4025 int find_what,
4026 void *search_ctx_arg,
4027 int tagfile, /* expanding names of tags files */
4028 char_u *rel_fname) /* file name to use for "." */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
4030#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004031 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004033 ff_stack_T *sptr;
4034 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
4036 /* If a search context is given by the caller, reuse it, else allocate a
4037 * new one.
4038 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004039 if (search_ctx_arg != NULL)
4040 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 else
4042 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004043 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4044 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004046 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004048 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004049 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004052 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 /* clear visited list if wanted */
4055 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004056 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 else
4058 {
4059 /* Reuse old visited lists. Get the visited list for the given
4060 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004061 * one. */
4062 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4063 &search_ctx->ffsc_visited_lists_list);
4064 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004066 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4067 &search_ctx->ffsc_dir_visited_lists_list);
4068 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 goto error_return;
4070 }
4071
4072 if (ff_expand_buffer == NULL)
4073 {
4074 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4075 if (ff_expand_buffer == NULL)
4076 goto error_return;
4077 }
4078
4079 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004080 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 if (path[0] == '.'
4082 && (vim_ispathsep(path[1]) || path[1] == NUL)
4083 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4084 && rel_fname != NULL)
4085 {
4086 int len = (int)(gettail(rel_fname) - rel_fname);
4087
4088 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4089 {
4090 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004091 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004092 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004095 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4096 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 goto error_return;
4098 if (*++path != NUL)
4099 ++path;
4100 }
4101 else if (*path == NUL || !vim_isAbsName(path))
4102 {
4103#ifdef BACKSLASH_IN_FILENAME
4104 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4105 if (*path != NUL && path[1] == ':')
4106 {
4107 char_u drive[3];
4108
4109 drive[0] = path[0];
4110 drive[1] = ':';
4111 drive[2] = NUL;
4112 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4113 goto error_return;
4114 path += 2;
4115 }
4116 else
4117#endif
4118 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4119 goto error_return;
4120
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004121 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4122 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 goto error_return;
4124
4125#ifdef BACKSLASH_IN_FILENAME
4126 /* A path that starts with "/dir" is relative to the drive, not to the
4127 * directory (but not for "//machine/dir"). Only use the drive name. */
4128 if ((*path == '/' || *path == '\\')
4129 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004130 && search_ctx->ffsc_start_dir[1] == ':')
4131 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#endif
4133 }
4134
4135#ifdef FEAT_PATH_EXTRA
4136 /*
4137 * If stopdirs are given, split them into an array of pointers.
4138 * If this fails (mem allocation), there is no upward search at all or a
4139 * stop directory is not recognized -> continue silently.
4140 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004141 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 * is handled as unlimited upward search. See function
4143 * ff_path_in_stoplist() for details.
4144 */
4145 if (stopdirs != NULL)
4146 {
4147 char_u *walker = stopdirs;
4148 int dircount;
4149
4150 while (*walker == ';')
4151 walker++;
4152
4153 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004154 search_ctx->ffsc_stopdirs_v =
4155 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004157 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
4159 do
4160 {
4161 char_u *helper;
4162 void *ptr;
4163
4164 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004165 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 (dircount + 1) * sizeof(char_u *));
4167 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004168 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 else
4170 /* ignore, keep what we have and continue */
4171 break;
4172 walker = vim_strchr(walker, ';');
4173 if (walker)
4174 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004175 search_ctx->ffsc_stopdirs_v[dircount-1] =
4176 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 walker++;
4178 }
4179 else
4180 /* this might be "", which means ascent till top
4181 * of directory tree.
4182 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004183 search_ctx->ffsc_stopdirs_v[dircount-1] =
4184 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186 dircount++;
4187
4188 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004189 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 }
4192#endif
4193
4194#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004195 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197 /* split into:
4198 * -fix path
4199 * -wildcard_stuff (might be NULL)
4200 */
4201 wc_part = vim_strchr(path, '*');
4202 if (wc_part != NULL)
4203 {
4204 int llevel;
4205 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004206 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004209 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 /*
4212 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004213 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4215 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4216 * For EBCDIC you get different character values.
4217 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004218 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 * string.
4220 */
4221 len = 0;
4222 while (*wc_part != NUL)
4223 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004224 if (len + 5 >= MAXPATHL)
4225 {
4226 EMSG(_(e_pathtoolong));
4227 break;
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (STRNCMP(wc_part, "**", 2) == 0)
4230 {
4231 ff_expand_buffer[len++] = *wc_part++;
4232 ff_expand_buffer[len++] = *wc_part++;
4233
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004234 llevel = strtol((char *)wc_part, &errpt, 10);
4235 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004237 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 /* restrict is 0 -> remove already added '**' */
4239 len -= 2;
4240 else
4241 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004242 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004243 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4246 goto error_return;
4247 }
4248 }
4249 else
4250 ff_expand_buffer[len++] = *wc_part++;
4251 }
4252 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004253 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004255 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 goto error_return;
4257 }
4258 else
4259#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004260 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004262 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
4264 /* store the fix part as startdir.
4265 * This is needed if the parameter path is fully qualified.
4266 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004267 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004268 if (search_ctx->ffsc_start_dir == NULL)
4269 goto error_return;
4270 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272
4273 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004274 if (STRLEN(search_ctx->ffsc_start_dir)
4275 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4276 {
4277 EMSG(_(e_pathtoolong));
4278 goto error_return;
4279 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004280 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004282 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004283 int eb_len = (int)STRLEN(ff_expand_buffer);
4284 char_u *buf = alloc(eb_len
4285 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004286
4287 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004288 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004289 if (mch_isdir(buf))
4290 {
4291 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4292 add_pathsep(ff_expand_buffer);
4293 }
4294#ifdef FEAT_PATH_EXTRA
4295 else
4296 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004297 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004298 char_u *wc_path = NULL;
4299 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004300 int len = 0;
4301
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004302 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004303 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004304 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004305 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4306 add_pathsep(ff_expand_buffer);
4307 }
4308 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004309 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004310
4311 if (search_ctx->ffsc_wc_path != NULL)
4312 {
4313 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004314 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004315 + STRLEN(search_ctx->ffsc_fix_path + len)
4316 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004317 if (temp == NULL || wc_path == NULL)
4318 {
4319 vim_free(buf);
4320 vim_free(temp);
4321 vim_free(wc_path);
4322 goto error_return;
4323 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004324
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004325 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4326 STRCAT(temp, search_ctx->ffsc_wc_path);
4327 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004328 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004329 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004330 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004331 }
4332#endif
4333 vim_free(buf);
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335
4336 sptr = ff_create_stack_element(ff_expand_buffer,
4337#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004338 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339#endif
4340 level, 0);
4341
4342 if (sptr == NULL)
4343 goto error_return;
4344
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004345 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004347 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4348 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 goto error_return;
4350
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004351 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
4353error_return:
4354 /*
4355 * We clear the search context now!
4356 * Even when the caller gave us a (perhaps valid) context we free it here,
4357 * as we might have already destroyed it.
4358 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004359 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 return NULL;
4361}
4362
4363#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4364/*
4365 * Get the stopdir string. Check that ';' is not escaped.
4366 */
4367 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004368vim_findfile_stopdir(char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 char_u *r_ptr = buf;
4371
4372 while (*r_ptr != NUL && *r_ptr != ';')
4373 {
4374 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4375 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004376 /* Overwrite the escape char,
4377 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004378 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 r_ptr++;
4380 }
4381 r_ptr++;
4382 }
4383 if (*r_ptr == ';')
4384 {
4385 *r_ptr = 0;
4386 r_ptr++;
4387 }
4388 else if (*r_ptr == NUL)
4389 r_ptr = NULL;
4390 return r_ptr;
4391}
4392#endif
4393
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004394/*
4395 * Clean up the given search context. Can handle a NULL pointer.
4396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004398vim_findfile_cleanup(void *ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004400 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 return;
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004404 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406}
4407
4408/*
4409 * Find a file in a search context.
4410 * The search context was created with vim_findfile_init() above.
4411 * Return a pointer to an allocated file name or NULL if nothing found.
4412 * To get all matching files call this function until you get NULL.
4413 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004414 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 *
4416 * The search algorithm is depth first. To change this replace the
4417 * stack with a list (don't forget to leave partly searched directories on the
4418 * top of the list).
4419 */
4420 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004421vim_findfile(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 char_u *file_path;
4424#ifdef FEAT_PATH_EXTRA
4425 char_u *rest_of_wildcards;
4426 char_u *path_end = NULL;
4427#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004428 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4430 int len;
4431#endif
4432 int i;
4433 char_u *p;
4434#ifdef FEAT_SEARCHPATH
4435 char_u *suf;
4436#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004437 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004439 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 return NULL;
4441
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004442 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 /*
4445 * filepath is used as buffer for various actions and as the storage to
4446 * return a found filename.
4447 */
4448 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4449 return NULL;
4450
4451#ifdef FEAT_PATH_EXTRA
4452 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004453 if (search_ctx->ffsc_start_dir != NULL)
4454 path_end = &search_ctx->ffsc_start_dir[
4455 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#endif
4457
4458#ifdef FEAT_PATH_EXTRA
4459 /* upward search loop */
4460 for (;;)
4461 {
4462#endif
4463 /* downward search loop */
4464 for (;;)
4465 {
4466 /* check if user user wants to stop the search*/
4467 ui_breakcheck();
4468 if (got_int)
4469 break;
4470
4471 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004472 stackp = ff_pop(search_ctx);
4473 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 break;
4475
4476 /*
4477 * TODO: decide if we leave this test in
4478 *
4479 * GOOD: don't search a directory(-tree) twice.
4480 * BAD: - check linked list for every new directory entered.
4481 * - check for double files also done below
4482 *
4483 * Here we check if we already searched this directory.
4484 * We already searched a directory if:
4485 * 1) The directory is the same.
4486 * 2) We would use the same wildcard string.
4487 *
4488 * Good if you have links on same directory via several ways
4489 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4490 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4491 *
4492 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004493 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004495 if (stackp->ffs_filearray == NULL
4496 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004498 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004500 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501#endif
4502 ) == FAIL)
4503 {
4504#ifdef FF_VERBOSE
4505 if (p_verbose >= 5)
4506 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004507 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004509 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 /* don't overwrite this either */
4511 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004512 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004515 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 continue;
4517 }
4518#ifdef FF_VERBOSE
4519 else if (p_verbose >= 5)
4520 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004521 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004522 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004523 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /* don't overwrite this either */
4525 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004526 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528#endif
4529
4530 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004531 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004533 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 continue;
4535 }
4536
4537 file_path[0] = NUL;
4538
4539 /*
4540 * If no filearray till now expand wildcards
4541 * The function expand_wildcards() can handle an array of paths
4542 * and all possible expands are returned in one array. We use this
4543 * to handle the expansion of '**' into an empty string.
4544 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004545 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 char_u *dirptrs[2];
4548
4549 /* we use filepath to build the path expand_wildcards() should
4550 * expand.
4551 */
4552 dirptrs[0] = file_path;
4553 dirptrs[1] = NULL;
4554
4555 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004556 if (!vim_isAbsName(stackp->ffs_fix_path)
4557 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004559 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 add_pathsep(file_path);
4561 }
4562
4563 /* append the fix part of the search path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004564 STRCAT(file_path, stackp->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 add_pathsep(file_path);
4566
4567#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004568 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (*rest_of_wildcards != NUL)
4570 {
4571 len = (int)STRLEN(file_path);
4572 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4573 {
4574 /* pointer to the restrict byte
4575 * The restrict byte is not a character!
4576 */
4577 p = rest_of_wildcards + 2;
4578
4579 if (*p > 0)
4580 {
4581 (*p)--;
4582 file_path[len++] = '*';
4583 }
4584
4585 if (*p == 0)
4586 {
4587 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004588 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 else
4591 rest_of_wildcards += 3;
4592
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004593 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
4595 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004596 stackp->ffs_star_star_empty = 1;
4597 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599 }
4600
4601 /*
4602 * Here we copy until the next path separator or the end of
4603 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004604 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 * pushing every directory returned from expand_wildcards()
4606 * on the stack again for further search.
4607 */
4608 while (*rest_of_wildcards
4609 && !vim_ispathsep(*rest_of_wildcards))
4610 file_path[len++] = *rest_of_wildcards++;
4611
4612 file_path[len] = NUL;
4613 if (vim_ispathsep(*rest_of_wildcards))
4614 rest_of_wildcards++;
4615 }
4616#endif
4617
4618 /*
4619 * Expand wildcards like "*" and "$VAR".
4620 * If the path is a URL don't try this.
4621 */
4622 if (path_with_url(dirptrs[0]))
4623 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004624 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004626 if (stackp->ffs_filearray != NULL
4627 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004629 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004631 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004634 /* Add EW_NOTWILD because the expanded path may contain
4635 * wildcard characters that are to be taken literally.
4636 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004638 &stackp->ffs_filearray_size,
4639 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004640 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004642 stackp->ffs_filearray_cur = 0;
4643 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645#ifdef FEAT_PATH_EXTRA
4646 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004647 rest_of_wildcards = &stackp->ffs_wc_path[
4648 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#endif
4650
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004651 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 /* this is the first time we work on this directory */
4654#ifdef FEAT_PATH_EXTRA
4655 if (*rest_of_wildcards == NUL)
4656#endif
4657 {
4658 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004659 * We don't have further wildcards to expand, so we have to
4660 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004662 for (i = stackp->ffs_filearray_cur;
4663 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004665 if (!path_with_url(stackp->ffs_filearray[i])
4666 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 continue; /* not a directory */
4668
Bram Moolenaar21160b92009-11-11 15:56:10 +00004669 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 * below */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004671 STRCPY(file_path, stackp->ffs_filearray[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004673 STRCAT(file_path, search_ctx->ffsc_file_to_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675 /*
4676 * Try without extra suffix and then with suffixes
4677 * from 'suffixesadd'.
4678 */
4679#ifdef FEAT_SEARCHPATH
4680 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004681 if (search_ctx->ffsc_tagfile)
4682 suf = (char_u *)"";
4683 else
4684 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 for (;;)
4686#endif
4687 {
4688 /* if file exists and we didn't already find it */
4689 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004690 || (mch_getperm(file_path) >= 0
4691 && (search_ctx->ffsc_find_what
4692 == FINDFILE_BOTH
4693 || ((search_ctx->ffsc_find_what
4694 == FINDFILE_DIR)
4695 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696#ifndef FF_VERBOSE
4697 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004698 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 file_path
4700#ifdef FEAT_PATH_EXTRA
4701 , (char_u *)""
4702#endif
4703 ) == OK)
4704#endif
4705 )
4706 {
4707#ifdef FF_VERBOSE
4708 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004709 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 file_path
4711#ifdef FEAT_PATH_EXTRA
4712 , (char_u *)""
4713#endif
4714 ) == FAIL)
4715 {
4716 if (p_verbose >= 5)
4717 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004718 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004719 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 file_path);
4721 /* don't overwrite this either */
4722 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004723 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725 continue;
4726 }
4727#endif
4728
4729 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004730 stackp->ffs_filearray_cur = i + 1;
4731 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004733 if (!path_with_url(file_path))
4734 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4736 == OK)
4737 {
4738 p = shorten_fname(file_path,
4739 ff_expand_buffer);
4740 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004741 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743#ifdef FF_VERBOSE
4744 if (p_verbose >= 5)
4745 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004746 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004747 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 /* don't overwrite this either */
4749 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004750 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752#endif
4753 return file_path;
4754 }
4755
4756#ifdef FEAT_SEARCHPATH
4757 /* Not found or found already, try next suffix. */
4758 if (*suf == NUL)
4759 break;
4760 copy_option_part(&suf, file_path + len,
4761 MAXPATHL - len, ",");
4762#endif
4763 }
4764 }
4765 }
4766#ifdef FEAT_PATH_EXTRA
4767 else
4768 {
4769 /*
4770 * still wildcards left, push the directories for further
4771 * search
4772 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004773 for (i = stackp->ffs_filearray_cur;
4774 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004776 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 continue; /* not a directory */
4778
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004779 ff_push(search_ctx,
4780 ff_create_stack_element(
4781 stackp->ffs_filearray[i],
4782 rest_of_wildcards,
4783 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 }
4786#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004787 stackp->ffs_filearray_cur = 0;
4788 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790
4791#ifdef FEAT_PATH_EXTRA
4792 /*
4793 * if wildcards contains '**' we have to descent till we reach the
4794 * leaves of the directory tree.
4795 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004796 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004798 for (i = stackp->ffs_filearray_cur;
4799 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004801 if (fnamecmp(stackp->ffs_filearray[i],
4802 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004804 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004806 ff_push(search_ctx,
4807 ff_create_stack_element(stackp->ffs_filearray[i],
4808 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810 }
4811#endif
4812
4813 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004814 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816 }
4817
4818#ifdef FEAT_PATH_EXTRA
4819 /* If we reached this, we didn't find anything downwards.
4820 * Let's check if we should do an upward search.
4821 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004822 if (search_ctx->ffsc_start_dir
4823 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
4825 ff_stack_T *sptr;
4826
4827 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004828 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4829 (int)(path_end - search_ctx->ffsc_start_dir),
4830 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 break;
4832
4833 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004834 while (path_end > search_ctx->ffsc_start_dir
4835 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004837 while (path_end > search_ctx->ffsc_start_dir
4838 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 path_end--;
4840 *path_end = 0;
4841 path_end--;
4842
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004843 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 break;
4845
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004846 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004848 STRCAT(file_path, search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /* create a new stack entry */
4851 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004852 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (sptr == NULL)
4854 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004855 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 else
4858 break;
4859 }
4860#endif
4861
4862 vim_free(file_path);
4863 return NULL;
4864}
4865
4866/*
4867 * Free the list of lists of visited files and directories
4868 * Can handle it if the passed search_context is NULL;
4869 */
4870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004871vim_findfile_free_visited(void *search_ctx_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004873 ff_search_ctx_T *search_ctx;
4874
4875 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 return;
4877
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004878 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
4879 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
4880 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881}
4882
4883 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004884vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 ff_visited_list_hdr_T *vp;
4887
4888 while (*list_headp != NULL)
4889 {
4890 vp = (*list_headp)->ffvl_next;
4891 ff_free_visited_list((*list_headp)->ffvl_visited_list);
4892
4893 vim_free((*list_headp)->ffvl_filename);
4894 vim_free(*list_headp);
4895 *list_headp = vp;
4896 }
4897 *list_headp = NULL;
4898}
4899
4900 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004901ff_free_visited_list(ff_visited_T *vl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 ff_visited_T *vp;
4904
4905 while (vl != NULL)
4906 {
4907 vp = vl->ffv_next;
4908#ifdef FEAT_PATH_EXTRA
4909 vim_free(vl->ffv_wc_path);
4910#endif
4911 vim_free(vl);
4912 vl = vp;
4913 }
4914 vl = NULL;
4915}
4916
4917/*
4918 * Returns the already visited list for the given filename. If none is found it
4919 * allocates a new one.
4920 */
4921 static ff_visited_list_hdr_T*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004922ff_get_visited_list(
4923 char_u *filename,
4924 ff_visited_list_hdr_T **list_headp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 ff_visited_list_hdr_T *retptr = NULL;
4927
4928 /* check if a visited list for the given filename exists */
4929 if (*list_headp != NULL)
4930 {
4931 retptr = *list_headp;
4932 while (retptr != NULL)
4933 {
4934 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
4935 {
4936#ifdef FF_VERBOSE
4937 if (p_verbose >= 5)
4938 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004939 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004940 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 filename);
4942 /* don't overwrite this either */
4943 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004944 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946#endif
4947 return retptr;
4948 }
4949 retptr = retptr->ffvl_next;
4950 }
4951 }
4952
4953#ifdef FF_VERBOSE
4954 if (p_verbose >= 5)
4955 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004956 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004957 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 /* don't overwrite this either */
4959 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004960 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962#endif
4963
4964 /*
4965 * if we reach this we didn't find a list and we have to allocate new list
4966 */
4967 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
4968 if (retptr == NULL)
4969 return NULL;
4970
4971 retptr->ffvl_visited_list = NULL;
4972 retptr->ffvl_filename = vim_strsave(filename);
4973 if (retptr->ffvl_filename == NULL)
4974 {
4975 vim_free(retptr);
4976 return NULL;
4977 }
4978 retptr->ffvl_next = *list_headp;
4979 *list_headp = retptr;
4980
4981 return retptr;
4982}
4983
4984#ifdef FEAT_PATH_EXTRA
4985/*
4986 * check if two wildcard paths are equal. Returns TRUE or FALSE.
4987 * They are equal if:
4988 * - both paths are NULL
4989 * - they have the same length
4990 * - char by char comparison is OK
4991 * - the only differences are in the counters behind a '**', so
4992 * '**\20' is equal to '**\24'
4993 */
4994 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004995ff_wc_equal(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02004997 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02004998 int c1 = NUL;
4999 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005000 int prev1 = NUL;
5001 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003 if (s1 == s2)
5004 return TRUE;
5005
5006 if (s1 == NULL || s2 == NULL)
5007 return FALSE;
5008
Bram Moolenaard43f0952015-08-27 22:30:47 +02005009 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005011 c1 = PTR2CHAR(s1 + i);
5012 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005013
5014 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5015 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005016 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005017 prev2 = prev1;
5018 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005019
5020 i += MB_PTR2LEN(s1 + i);
5021 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005023 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024}
5025#endif
5026
5027/*
5028 * maintains the list of already visited files and dirs
5029 * returns FAIL if the given file/dir is already in the list
5030 * returns OK if it is newly added
5031 *
5032 * TODO: What to do on memory allocation problems?
5033 * -> return TRUE - Better the file is found several times instead of
5034 * never.
5035 */
5036 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005037ff_check_visited(
5038 ff_visited_T **visited_list,
5039 char_u *fname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005041 , char_u *wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005043 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
5045 ff_visited_T *vp;
5046#ifdef UNIX
5047 struct stat st;
5048 int url = FALSE;
5049#endif
5050
5051 /* For an URL we only compare the name, otherwise we compare the
5052 * device/inode (unix) or the full path name (not Unix). */
5053 if (path_with_url(fname))
5054 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005055 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056#ifdef UNIX
5057 url = TRUE;
5058#endif
5059 }
5060 else
5061 {
5062 ff_expand_buffer[0] = NUL;
5063#ifdef UNIX
5064 if (mch_stat((char *)fname, &st) < 0)
5065#else
5066 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5067#endif
5068 return FAIL;
5069 }
5070
5071 /* check against list of already visited files */
5072 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5073 {
5074 if (
5075#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005076 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5077 && vp->ffv_ino == st.st_ino)
5078 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079#endif
5080 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5081 )
5082 {
5083#ifdef FEAT_PATH_EXTRA
5084 /* are the wildcard parts equal */
5085 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5086#endif
5087 /* already visited */
5088 return FAIL;
5089 }
5090 }
5091
5092 /*
5093 * New file/dir. Add it to the list of visited files/dirs.
5094 */
5095 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5096 + STRLEN(ff_expand_buffer)));
5097
5098 if (vp != NULL)
5099 {
5100#ifdef UNIX
5101 if (!url)
5102 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005103 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 vp->ffv_ino = st.st_ino;
5105 vp->ffv_dev = st.st_dev;
5106 vp->ffv_fname[0] = NUL;
5107 }
5108 else
5109 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005110 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111#endif
5112 STRCPY(vp->ffv_fname, ff_expand_buffer);
5113#ifdef UNIX
5114 }
5115#endif
5116#ifdef FEAT_PATH_EXTRA
5117 if (wc_path != NULL)
5118 vp->ffv_wc_path = vim_strsave(wc_path);
5119 else
5120 vp->ffv_wc_path = NULL;
5121#endif
5122
5123 vp->ffv_next = *visited_list;
5124 *visited_list = vp;
5125 }
5126
5127 return OK;
5128}
5129
5130/*
5131 * create stack element from given path pieces
5132 */
5133 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005134ff_create_stack_element(
5135 char_u *fix_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136#ifdef FEAT_PATH_EXTRA
Bram Moolenaar9b578142016-01-30 19:39:49 +01005137 char_u *wc_part,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005139 int level,
5140 int star_star_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 ff_stack_T *new;
5143
5144 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5145 if (new == NULL)
5146 return NULL;
5147
5148 new->ffs_prev = NULL;
5149 new->ffs_filearray = NULL;
5150 new->ffs_filearray_size = 0;
5151 new->ffs_filearray_cur = 0;
5152 new->ffs_stage = 0;
5153 new->ffs_level = level;
5154 new->ffs_star_star_empty = star_star_empty;;
5155
5156 /* the following saves NULL pointer checks in vim_findfile */
5157 if (fix_part == NULL)
5158 fix_part = (char_u *)"";
5159 new->ffs_fix_path = vim_strsave(fix_part);
5160
5161#ifdef FEAT_PATH_EXTRA
5162 if (wc_part == NULL)
5163 wc_part = (char_u *)"";
5164 new->ffs_wc_path = vim_strsave(wc_part);
5165#endif
5166
5167 if (new->ffs_fix_path == NULL
5168#ifdef FEAT_PATH_EXTRA
5169 || new->ffs_wc_path == NULL
5170#endif
5171 )
5172 {
5173 ff_free_stack_element(new);
5174 new = NULL;
5175 }
5176
5177 return new;
5178}
5179
5180/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005181 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
5183 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005184ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005187 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005188 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005190 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5191 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193}
5194
5195/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005196 * Pop a dir from the directory stack.
5197 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 */
5199 static ff_stack_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005200ff_pop(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
5202 ff_stack_T *sptr;
5203
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005204 sptr = search_ctx->ffsc_stack_ptr;
5205 if (search_ctx->ffsc_stack_ptr != NULL)
5206 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
5208 return sptr;
5209}
5210
5211/*
5212 * free the given stack element
5213 */
5214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005215ff_free_stack_element(ff_stack_T *stack_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005218 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005220 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#endif
5222
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005223 if (stack_ptr->ffs_filearray != NULL)
5224 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005226 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227}
5228
5229/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005230 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 */
5232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005233ff_clear(ff_search_ctx_T *search_ctx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234{
5235 ff_stack_T *sptr;
5236
5237 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005238 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 ff_free_stack_element(sptr);
5240
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005241 vim_free(search_ctx->ffsc_file_to_search);
5242 vim_free(search_ctx->ffsc_start_dir);
5243 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005245 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#endif
5247
5248#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005249 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 int i = 0;
5252
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005253 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005255 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 i++;
5257 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005258 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005260 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261#endif
5262
5263 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005264 search_ctx->ffsc_file_to_search = NULL;
5265 search_ctx->ffsc_start_dir = NULL;
5266 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005268 search_ctx->ffsc_wc_path = NULL;
5269 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#endif
5271}
5272
5273#ifdef FEAT_PATH_EXTRA
5274/*
5275 * check if the given path is in the stopdirs
5276 * returns TRUE if yes else FALSE
5277 */
5278 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 int i = 0;
5282
5283 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005284 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 path_len--;
5286
5287 /* if no path consider it as match */
5288 if (path_len == 0)
5289 return TRUE;
5290
5291 for (i = 0; stopdirs_v[i] != NULL; i++)
5292 {
5293 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5294 {
5295 /* match for parent directory. So '/home' also matches
5296 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5297 * '/home/r' would also match '/home/rks'
5298 */
5299 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005300 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 return TRUE;
5302 }
5303 else
5304 {
5305 if (fnamecmp(stopdirs_v[i], path) == 0)
5306 return TRUE;
5307 }
5308 }
5309 return FALSE;
5310}
5311#endif
5312
5313#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5314/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005315 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 *
5317 * On the first call set the parameter 'first' to TRUE to initialize
5318 * the search. For repeating calls to FALSE.
5319 *
5320 * Repeating calls will return other files called 'ptr[len]' from the path.
5321 *
5322 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5323 * don't need valid values.
5324 *
5325 * If nothing found on the first call the option FNAME_MESS will issue the
5326 * message:
5327 * 'Can't find file "<file>" in path'
5328 * On repeating calls:
5329 * 'No more file "<file>" found in path'
5330 *
5331 * options:
5332 * FNAME_MESS give error message when not found
5333 *
5334 * Uses NameBuff[]!
5335 *
5336 * Returns an allocated string for the file name. NULL for error.
5337 *
5338 */
5339 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005340find_file_in_path(
5341 char_u *ptr, /* file name */
5342 int len, /* length of file name */
5343 int options,
5344 int first, /* use count'th matching file name */
5345 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 return find_file_in_path_option(ptr, len, options, first,
5348 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005349 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350}
5351
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005352static char_u *ff_file_to_find = NULL;
5353static void *fdip_search_ctx = NULL;
5354
5355#if defined(EXITFREE)
5356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005357free_findfile(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005358{
5359 vim_free(ff_file_to_find);
5360 vim_findfile_cleanup(fdip_search_ctx);
5361}
5362#endif
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364/*
5365 * Find the directory name "ptr[len]" in the path.
5366 *
5367 * options:
5368 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005369 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 *
5371 * Uses NameBuff[]!
5372 *
5373 * Returns an allocated string for the file name. NULL for error.
5374 */
5375 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005376find_directory_in_path(
5377 char_u *ptr, /* file name */
5378 int len, /* length of file name */
5379 int options,
5380 char_u *rel_fname) /* file name searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
5382 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005383 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384}
5385
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005386 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387find_file_in_path_option(
5388 char_u *ptr, /* file name */
5389 int len, /* length of file name */
5390 int options,
5391 int first, /* use count'th matching file name */
5392 char_u *path_option, /* p_path or p_cdpath */
5393 int find_what, /* FINDFILE_FILE, _DIR or _BOTH */
5394 char_u *rel_fname, /* file name we are looking relative to. */
5395 char_u *suffixes) /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 static int did_findfile_init = FALSE;
5399 char_u save_char;
5400 char_u *file_name = NULL;
5401 char_u *buf = NULL;
5402 int rel_to_curdir;
5403#ifdef AMIGA
5404 struct Process *proc = (struct Process *)FindTask(0L);
5405 APTR save_winptr = proc->pr_WindowPtr;
5406
5407 /* Avoid a requester here for a volume that doesn't exist. */
5408 proc->pr_WindowPtr = (APTR)-1L;
5409#endif
5410
5411 if (first == TRUE)
5412 {
5413 /* copy file name into NameBuff, expanding environment variables */
5414 save_char = ptr[len];
5415 ptr[len] = NUL;
Bram Moolenaar58adb142016-01-16 21:50:51 +01005416 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 ptr[len] = save_char;
5418
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005419 vim_free(ff_file_to_find);
5420 ff_file_to_find = vim_strsave(NameBuff);
5421 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
5423 file_name = NULL;
5424 goto theend;
5425 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005426 if (options & FNAME_UNESC)
5427 {
5428 /* Change all "\ " to " ". */
5429 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5430 if (ptr[0] == '\\' && ptr[1] == ' ')
5431 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005435 rel_to_curdir = (ff_file_to_find[0] == '.'
5436 && (ff_file_to_find[1] == NUL
5437 || vim_ispathsep(ff_file_to_find[1])
5438 || (ff_file_to_find[1] == '.'
5439 && (ff_file_to_find[2] == NUL
5440 || vim_ispathsep(ff_file_to_find[2])))));
5441 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 /* "..", "../path", "." and "./path": don't use the path_option */
5443 || rel_to_curdir
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005444#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005446 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005447 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005448 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449#endif
5450#ifdef AMIGA
5451 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005452 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453#endif
5454 )
5455 {
5456 /*
5457 * Absolute path, no need to use "path_option".
5458 * If this is not a first call, return NULL. We already returned a
5459 * filename on the first call.
5460 */
5461 if (first == TRUE)
5462 {
5463 int l;
5464 int run;
5465
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005466 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005468 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 goto theend;
5470 }
5471
5472 /* When FNAME_REL flag given first use the directory of the file.
5473 * Otherwise or when this fails use the current directory. */
5474 for (run = 1; run <= 2; ++run)
5475 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005476 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (run == 1
5478 && rel_to_curdir
5479 && (options & FNAME_REL)
5480 && rel_fname != NULL
5481 && STRLEN(rel_fname) + l < MAXPATHL)
5482 {
5483 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005484 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 l = (int)STRLEN(NameBuff);
5486 }
5487 else
5488 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005489 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 run = 2;
5491 }
5492
5493 /* When the file doesn't exist, try adding parts of
5494 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005495 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 for (;;)
5497 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005498 if (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005499 && (find_what == FINDFILE_BOTH
5500 || ((find_what == FINDFILE_DIR)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005501 == mch_isdir(NameBuff))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 file_name = vim_strsave(NameBuff);
5504 goto theend;
5505 }
5506 if (*buf == NUL)
5507 break;
5508 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5509 }
5510 }
5511 }
5512 }
5513 else
5514 {
5515 /*
5516 * Loop over all paths in the 'path' or 'cdpath' option.
5517 * When "first" is set, first setup to the start of the option.
5518 * Otherwise continue to find the next match.
5519 */
5520 if (first == TRUE)
5521 {
5522 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005523 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 dir = path_option;
5525 did_findfile_init = FALSE;
5526 }
5527
5528 for (;;)
5529 {
5530 if (did_findfile_init)
5531 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005532 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (file_name != NULL)
5534 break;
5535
5536 did_findfile_init = FALSE;
5537 }
5538 else
5539 {
5540 char_u *r_ptr;
5541
5542 if (dir == NULL || *dir == NUL)
5543 {
5544 /* We searched all paths of the option, now we can
5545 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005546 vim_findfile_cleanup(fdip_search_ctx);
5547 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 break;
5549 }
5550
5551 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5552 break;
5553
5554 /* copy next path */
5555 buf[0] = 0;
5556 copy_option_part(&dir, buf, MAXPATHL, " ,");
5557
5558#ifdef FEAT_PATH_EXTRA
5559 /* get the stopdir string */
5560 r_ptr = vim_findfile_stopdir(buf);
5561#else
5562 r_ptr = NULL;
5563#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005564 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005565 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005566 fdip_search_ctx, FALSE, rel_fname);
5567 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 did_findfile_init = TRUE;
5569 vim_free(buf);
5570 }
5571 }
5572 }
5573 if (file_name == NULL && (options & FNAME_MESS))
5574 {
5575 if (first == TRUE)
5576 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005577 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005579 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 else
5581 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005582 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584 else
5585 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005586 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005588 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 else
5590 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005591 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593 }
5594
5595theend:
5596#ifdef AMIGA
5597 proc->pr_WindowPtr = save_winptr;
5598#endif
5599 return file_name;
5600}
5601
5602#endif /* FEAT_SEARCHPATH */
5603
5604/*
5605 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5606 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5607 */
5608 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005609vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
5611#ifndef FEAT_SEARCHPATH
5612 return mch_chdir((char *)new_dir);
5613#else
5614 char_u *dir_name;
5615 int r;
5616
5617 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5618 FNAME_MESS, curbuf->b_ffname);
5619 if (dir_name == NULL)
5620 return -1;
5621 r = mch_chdir((char *)dir_name);
5622 vim_free(dir_name);
5623 return r;
5624#endif
5625}
5626
5627/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005628 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005630 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5631 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 * Returns OK or FAIL.
5633 */
5634 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005635get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005637 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 if (mch_get_user_name(buf, len) == FAIL)
5640 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005641 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
5643 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005644 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 return OK;
5646}
5647
5648#ifndef HAVE_QSORT
5649/*
5650 * Our own qsort(), for systems that don't have it.
5651 * It's simple and slow. From the K&R C book.
5652 */
5653 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005654qsort(
5655 void *base,
5656 size_t elm_count,
5657 size_t elm_size,
5658 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 char_u *buf;
5661 char_u *p1;
5662 char_u *p2;
5663 int i, j;
5664 int gap;
5665
5666 buf = alloc((unsigned)elm_size);
5667 if (buf == NULL)
5668 return;
5669
5670 for (gap = elm_count / 2; gap > 0; gap /= 2)
5671 for (i = gap; i < elm_count; ++i)
5672 for (j = i - gap; j >= 0; j -= gap)
5673 {
5674 /* Compare the elements. */
5675 p1 = (char_u *)base + j * elm_size;
5676 p2 = (char_u *)base + (j + gap) * elm_size;
5677 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5678 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005679 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 mch_memmove(buf, p1, elm_size);
5681 mch_memmove(p1, p2, elm_size);
5682 mch_memmove(p2, buf, elm_size);
5683 }
5684
5685 vim_free(buf);
5686}
5687#endif
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689/*
5690 * Sort an array of strings.
5691 */
5692static int
5693#ifdef __BORLANDC__
5694_RTLENTRYF
5695#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005696sort_compare(const void *s1, const void *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 static int
5699#ifdef __BORLANDC__
5700_RTLENTRYF
5701#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +01005702sort_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 return STRCMP(*(char **)s1, *(char **)s2);
5705}
5706
5707 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005708sort_strings(
5709 char_u **files,
5710 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715#if !defined(NO_EXPANDPATH) || defined(PROTO)
5716/*
5717 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005718 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 * Return value like strcmp(p, q), but consider path separators.
5720 */
5721 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005722pathcmp(const char *p, const char *q, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005724 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005725 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005726 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005728 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005730 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005731 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005734 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005736 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 return 0;
5738 s = q;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005739 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 break;
5741 }
5742
5743 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005744 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746 s = p;
5747 break;
5748 }
5749
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005750 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#ifdef BACKSLASH_IN_FILENAME
5752 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005753 && !((c1 == '/' && c2 == '\\')
5754 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#endif
5756 )
5757 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005758 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005760 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005762 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5763 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005765
5766 i += MB_PTR2LEN((char_u *)p + i);
5767 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005769 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005770 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005772 c1 = PTR2CHAR((char_u *)s + i);
5773 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005775 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005776 && i > 0
5777 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005779 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005781 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005783 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 return 0; /* match with trailing slash */
5785 if (s == q)
5786 return -1; /* no match */
5787 return 1;
5788}
5789#endif
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791/*
5792 * The putenv() implementation below comes from the "screen" program.
5793 * Included with permission from Juergen Weigert.
5794 * See pty.c for the copyright notice.
5795 */
5796
5797/*
5798 * putenv -- put value into environment
5799 *
5800 * Usage: i = putenv (string)
5801 * int i;
5802 * char *string;
5803 *
5804 * where string is of the form <name>=<value>.
5805 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5806 *
5807 * Putenv may need to add a new name into the environment, or to
5808 * associate a value longer than the current value with a particular
5809 * name. So, to make life simpler, putenv() copies your entire
5810 * environment into the heap (i.e. malloc()) from the stack
5811 * (i.e. where it resides when your process is initiated) the first
5812 * time you call it.
5813 *
5814 * (history removed, not very interesting. See the "screen" sources.)
5815 */
5816
5817#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5818
5819#define EXTRASIZE 5 /* increment to add to env. size */
5820
5821static int envsize = -1; /* current size of environment */
5822#ifndef MACOS_CLASSIC
5823extern
5824#endif
5825 char **environ; /* the global which is your env. */
5826
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005827static int findenv(char *name); /* look for a name in the env. */
5828static int newenv(void); /* copy env. from stack to heap */
5829static int moreenv(void); /* incr. size of env. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
5831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005832putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
5834 int i;
5835 char *p;
5836
5837 if (envsize < 0)
5838 { /* first time putenv called */
5839 if (newenv() < 0) /* copy env. to heap */
5840 return -1;
5841 }
5842
5843 i = findenv((char *)string); /* look for name in environment */
5844
5845 if (i < 0)
5846 { /* name must be added */
5847 for (i = 0; environ[i]; i++);
5848 if (i >= (envsize - 1))
5849 { /* need new slot */
5850 if (moreenv() < 0)
5851 return -1;
5852 }
5853 p = (char *)alloc((unsigned)(strlen(string) + 1));
5854 if (p == NULL) /* not enough core */
5855 return -1;
5856 environ[i + 1] = 0; /* new end of env. */
5857 }
5858 else
5859 { /* name already in env. */
5860 p = vim_realloc(environ[i], strlen(string) + 1);
5861 if (p == NULL)
5862 return -1;
5863 }
5864 sprintf(p, "%s", string); /* copy into env. */
5865 environ[i] = p;
5866
5867 return 0;
5868}
5869
5870 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005871findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 char *namechar, *envchar;
5874 int i, found;
5875
5876 found = 0;
5877 for (i = 0; environ[i] && !found; i++)
5878 {
5879 envchar = environ[i];
5880 namechar = name;
5881 while (*namechar && *namechar != '=' && (*namechar == *envchar))
5882 {
5883 namechar++;
5884 envchar++;
5885 }
5886 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
5887 }
5888 return found ? i - 1 : -1;
5889}
5890
5891 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005892newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 char **env, *elem;
5895 int i, esize;
5896
5897#ifdef MACOS
5898 /* for Mac a new, empty environment is created */
5899 i = 0;
5900#else
5901 for (i = 0; environ[i]; i++)
5902 ;
5903#endif
5904 esize = i + EXTRASIZE + 1;
5905 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
5906 if (env == NULL)
5907 return -1;
5908
5909#ifndef MACOS
5910 for (i = 0; environ[i]; i++)
5911 {
5912 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
5913 if (elem == NULL)
5914 return -1;
5915 env[i] = elem;
5916 strcpy(elem, environ[i]);
5917 }
5918#endif
5919
5920 env[i] = 0;
5921 environ = env;
5922 envsize = esize;
5923 return 0;
5924}
5925
5926 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005927moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
5929 int esize;
5930 char **env;
5931
5932 esize = envsize + EXTRASIZE;
5933 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
5934 if (env == 0)
5935 return -1;
5936 environ = env;
5937 envsize = esize;
5938 return 0;
5939}
5940
5941# ifdef USE_VIMPTY_GETENV
5942 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005943vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 int i;
5946 char_u *p;
5947
5948 if (envsize < 0)
5949 return NULL;
5950
5951 i = findenv((char *)string);
5952
5953 if (i < 0)
5954 return NULL;
5955
5956 p = vim_strchr((char_u *)environ[i], '=');
5957 return (p + 1);
5958}
5959# endif
5960
5961#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00005962
Bram Moolenaarc4956c82006-03-12 21:58:43 +00005963#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00005964/*
5965 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
5966 * rights to write into.
5967 */
5968 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005969filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00005970{
5971 int retval = 0;
5972#if defined(UNIX) || defined(VMS)
5973 int perm = 0;
5974#endif
5975
5976#if defined(UNIX) || defined(VMS)
5977 perm = mch_getperm(fname);
5978#endif
5979#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
5980 if (
5981# ifdef WIN3264
5982 mch_writable(fname) &&
5983# else
5984# if defined(UNIX) || defined(VMS)
5985 (perm & 0222) &&
5986# endif
5987# endif
5988 mch_access((char *)fname, W_OK) == 0
5989 )
5990#endif
5991 {
5992 ++retval;
5993 if (mch_isdir(fname))
5994 ++retval;
5995 }
5996 return retval;
5997}
5998#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005999
6000/*
6001 * Print an error message with one or two "%s" and one or two string arguments.
6002 * This is not in message.c to avoid a warning for prototypes.
6003 */
6004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006005emsg3(char_u *s, char_u *a1, char_u *a2)
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006006{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006007 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006008 return TRUE; /* no error messages at the moment */
Bram Moolenaar6f192452007-11-08 19:49:02 +00006009 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006010 return emsg(IObuff);
6011}
6012
6013/*
6014 * Print an error message with one "%ld" and one long int argument.
6015 * This is not in message.c to avoid a warning for prototypes.
6016 */
6017 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006018emsgn(char_u *s, long n)
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006019{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006020 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006021 return TRUE; /* no error messages at the moment */
6022 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
6023 return emsg(IObuff);
6024}
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006025
6026#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6027/*
6028 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6029 */
6030 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006031get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006032{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006033 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006034
6035 n = getc(fd);
6036 n = (n << 8) + getc(fd);
6037 return n;
6038}
6039
6040/*
6041 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6042 */
6043 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006044get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006045{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006046 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006047
6048 n = getc(fd);
6049 n = (n << 8) + getc(fd);
6050 n = (n << 8) + getc(fd);
6051 return n;
6052}
6053
6054/*
6055 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6056 */
6057 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006058get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006059{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006060 /* Use unsigned rather than int otherwise result is undefined
6061 * when left-shift sets the MSB. */
6062 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006063
Bram Moolenaar95235e62013-09-08 16:07:07 +02006064 n = (unsigned)getc(fd);
6065 n = (n << 8) + (unsigned)getc(fd);
6066 n = (n << 8) + (unsigned)getc(fd);
6067 n = (n << 8) + (unsigned)getc(fd);
6068 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006069}
6070
6071/*
6072 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
6073 */
6074 time_t
Bram Moolenaar9b578142016-01-30 19:39:49 +01006075get8ctime(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006076{
6077 time_t n = 0;
6078 int i;
6079
6080 for (i = 0; i < 8; ++i)
6081 n = (n << 8) + getc(fd);
6082 return n;
6083}
6084
6085/*
6086 * Read a string of length "cnt" from "fd" into allocated memory.
6087 * Returns NULL when out of memory or unable to read that many bytes.
6088 */
6089 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006090read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006091{
6092 char_u *str;
6093 int i;
6094 int c;
6095
6096 /* allocate memory */
6097 str = alloc((unsigned)cnt + 1);
6098 if (str != NULL)
6099 {
6100 /* Read the string. Quit when running into the EOF. */
6101 for (i = 0; i < cnt; ++i)
6102 {
6103 c = getc(fd);
6104 if (c == EOF)
6105 {
6106 vim_free(str);
6107 return NULL;
6108 }
6109 str[i] = c;
6110 }
6111 str[i] = NUL;
6112 }
6113 return str;
6114}
6115
6116/*
6117 * Write a number to file "fd", MSB first, in "len" bytes.
6118 */
6119 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006120put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006121{
6122 int i;
6123
6124 for (i = len - 1; i >= 0; --i)
6125 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6126 return FAIL;
6127 return OK;
6128}
6129
6130#ifdef _MSC_VER
6131# if (_MSC_VER <= 1200)
6132/* This line is required for VC6 without the service pack. Also see the
6133 * matching #pragma below. */
6134 # pragma optimize("", off)
6135# endif
6136#endif
6137
6138/*
6139 * Write time_t to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006140 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006141 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006142 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006143put_time(FILE *fd, time_t the_time)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006144{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006145 char_u buf[8];
6146
6147 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006148 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006149}
6150
6151/*
6152 * Write time_t to "buf[8]".
6153 */
6154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006155time_to_bytes(time_t the_time, char_u *buf)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006156{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006157 int c;
6158 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006159 int bi = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006160 time_t wtime = the_time;
6161
6162 /* time_t can be up to 8 bytes in size, more than long_u, thus we
6163 * can't use put_bytes() here.
6164 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006165 * sign. This happens for large values of wtime. A cast to long_u may
6166 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes,
6167 * it's safe to assume that long_u is 4 bytes or more and when using 8
6168 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006169 for (i = 7; i >= 0; --i)
6170 {
6171 if (i + 1 > (int)sizeof(time_t))
6172 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006173 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006174 else
6175 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006176#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006177 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006178#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006179 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006180#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006181 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006182 }
6183 }
6184}
6185
6186#ifdef _MSC_VER
6187# if (_MSC_VER <= 1200)
6188 # pragma optimize("", on)
6189# endif
6190#endif
6191
6192#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006193
6194#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6195 || defined(FEAT_SPELL) || defined(PROTO)
6196/*
6197 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6198 * When "s" is NULL FALSE is returned.
6199 */
6200 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006201has_non_ascii(char_u *s)
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006202{
6203 char_u *p;
6204
6205 if (s != NULL)
6206 for (p = s; *p != NUL; ++p)
6207 if (*p >= 128)
6208 return TRUE;
6209 return FALSE;
6210}
6211#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006212
6213#if defined(MESSAGE_QUEUE) || defined(PROTO)
6214/*
6215 * Process messages that have been queued for netbeans or clientserver.
6216 * These functions can call arbitrary vimscript and should only be called when
6217 * it is safe to do so.
6218 */
6219 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006220parse_queued_messages(void)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006221{
Bram Moolenaarb7522a22016-02-21 17:20:55 +01006222 /* For Win32 mch_breakcheck() does not check for input, do it here. */
6223# if defined(WIN32) && defined(FEAT_CHANNEL)
6224 channel_handle_events();
6225# endif
6226
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006227# ifdef FEAT_NETBEANS_INTG
6228 /* Process the queued netbeans messages. */
6229 netbeans_parse_messages();
6230# endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01006231# ifdef FEAT_CHANNEL
6232 /* Process the messages queued on channels. */
6233 channel_parse_messages();
6234# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006235# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006236 /* Process the queued clientserver messages. */
6237 server_parse_messages();
6238# endif
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006239# ifdef FEAT_JOB
6240 /* Check if any jobs have ended. */
6241 job_check_ended();
6242# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006243}
6244#endif