blob: 0ee57fc4a69a8c2f2aafaf55889c88d936f312d0 [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)
20static int coladvance2 __ARGS((pos_T *pos, int addspaces, int finetune, colnr_T wcol));
21
22/*
23 * Return TRUE if in the current mode we need to use virtual.
24 */
25 int
26virtual_active()
27{
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
42getviscol()
43{
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
54getviscol2(col, coladd)
55 colnr_T col;
56 colnr_T coladd;
57{
58 colnr_T x;
59 pos_T pos;
60
61 pos.lnum = curwin->w_cursor.lnum;
62 pos.col = col;
63 pos.coladd = coladd;
64 getvvcol(curwin, &pos, &x, NULL, NULL);
65 return (int)x;
66}
67
68/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000069 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 * cursor in that column.
71 * The caller must have saved the cursor line for undo!
72 */
73 int
74coladvance_force(wcol)
75 colnr_T wcol;
76{
77 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
78
79 if (wcol == MAXCOL)
80 curwin->w_valid &= ~VALID_VIRTCOL;
81 else
82 {
83 /* Virtcol is valid */
84 curwin->w_valid |= VALID_VIRTCOL;
85 curwin->w_virtcol = wcol;
86 }
87 return rc;
88}
89#endif
90
91/*
92 * Try to advance the Cursor to the specified screen column.
93 * If virtual editing: fine tune the cursor position.
94 * Note that all virtual positions off the end of a line should share
95 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
96 * beginning at coladd 0.
97 *
98 * return OK if desired column is reached, FAIL if not
99 */
100 int
101coladvance(wcol)
102 colnr_T wcol;
103{
104 int rc = getvpos(&curwin->w_cursor, wcol);
105
106 if (wcol == MAXCOL || rc == FAIL)
107 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000108 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000110 /* Virtcol is valid when not on a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 curwin->w_valid |= VALID_VIRTCOL;
112 curwin->w_virtcol = wcol;
113 }
114 return rc;
115}
116
117/*
118 * Return in "pos" the position of the cursor advanced to screen column "wcol".
119 * return OK if desired column is reached, FAIL if not
120 */
121 int
122getvpos(pos, wcol)
123 pos_T *pos;
124 colnr_T wcol;
125{
126#ifdef FEAT_VIRTUALEDIT
127 return coladvance2(pos, FALSE, virtual_active(), wcol);
128}
129
130 static int
131coladvance2(pos, addspaces, finetune, wcol)
132 pos_T *pos;
133 int addspaces; /* change the text to achieve our goal? */
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +0000134 int finetune; /* change char offset for the exact column */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 colnr_T wcol; /* column to move to */
136{
137#endif
138 int idx;
139 char_u *ptr;
140 char_u *line;
141 colnr_T col = 0;
142 int csize = 0;
143 int one_more;
144#ifdef FEAT_LINEBREAK
145 int head = 0;
146#endif
147
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000148 one_more = (State & INSERT)
149 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000150 || (VIsual_active && *p_sel != 'o')
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000151#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000152 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000153#endif
154 ;
Bram Moolenaara1381de2009-11-03 15:44:21 +0000155 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
157 if (wcol >= MAXCOL)
158 {
159 idx = (int)STRLEN(line) - 1 + one_more;
160 col = wcol;
161
162#ifdef FEAT_VIRTUALEDIT
163 if ((addspaces || finetune) && !VIsual_active)
164 {
165 curwin->w_curswant = linetabsize(line) + one_more;
166 if (curwin->w_curswant > 0)
167 --curwin->w_curswant;
168 }
169#endif
170 }
171 else
172 {
173#ifdef FEAT_VIRTUALEDIT
174 int width = W_WIDTH(curwin) - win_col_off(curwin);
175
Bram Moolenaarebefac62005-12-28 22:39:57 +0000176 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 && curwin->w_p_wrap
178# ifdef FEAT_VERTSPLIT
179 && curwin->w_width != 0
180# endif
181 && wcol >= (colnr_T)width)
182 {
183 csize = linetabsize(line);
184 if (csize > 0)
185 csize--;
186
Bram Moolenaarebefac62005-12-28 22:39:57 +0000187 if (wcol / width > (colnr_T)csize / width
188 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {
190 /* In case of line wrapping don't move the cursor beyond the
Bram Moolenaarebefac62005-12-28 22:39:57 +0000191 * right screen edge. In Insert mode allow going just beyond
192 * the last character (like what happens when typing and
193 * reaching the right window edge). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 wcol = (csize / width + 1) * width - 1;
195 }
196 }
197#endif
198
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 ptr = line;
200 while (col <= wcol && *ptr != NUL)
201 {
202 /* Count a tab for what it's worth (if list mode not on) */
203#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200204 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000205 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200207 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#endif
209 col += csize;
210 }
211 idx = (int)(ptr - line);
212 /*
213 * Handle all the special cases. The virtual_active() check
214 * is needed to ensure that a virtual position off the end of
215 * a line has the correct indexing. The one_more comparison
216 * replaces an explicit add of one_more later on.
217 */
218 if (col > wcol || (!virtual_active() && one_more == 0))
219 {
220 idx -= 1;
221# ifdef FEAT_LINEBREAK
222 /* Don't count the chars from 'showbreak'. */
223 csize -= head;
224# endif
225 col -= csize;
226 }
227
228#ifdef FEAT_VIRTUALEDIT
229 if (virtual_active()
230 && addspaces
231 && ((col != wcol && col != wcol + 1) || csize > 1))
232 {
233 /* 'virtualedit' is set: The difference between wcol and col is
234 * filled with spaces. */
235
236 if (line[idx] == NUL)
237 {
238 /* Append spaces */
239 int correct = wcol - col;
240 char_u *newline = alloc(idx + correct + 1);
241 int t;
242
243 if (newline == NULL)
244 return FAIL;
245
246 for (t = 0; t < idx; ++t)
247 newline[t] = line[t];
248
249 for (t = 0; t < correct; ++t)
250 newline[t + idx] = ' ';
251
252 newline[idx + correct] = NUL;
253
254 ml_replace(pos->lnum, newline, FALSE);
255 changed_bytes(pos->lnum, (colnr_T)idx);
256 idx += correct;
257 col = wcol;
258 }
259 else
260 {
261 /* Break a tab */
262 int linelen = (int)STRLEN(line);
263 int correct = wcol - col - csize + 1; /* negative!! */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000264 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 int t, s = 0;
266 int v;
267
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000268 if (-correct > csize)
269 return FAIL;
270
271 newline = alloc(linelen + csize);
272 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 return FAIL;
274
275 for (t = 0; t < linelen; t++)
276 {
277 if (t != idx)
278 newline[s++] = line[t];
279 else
280 for (v = 0; v < csize; v++)
281 newline[s++] = ' ';
282 }
283
284 newline[linelen + csize - 1] = NUL;
285
286 ml_replace(pos->lnum, newline, FALSE);
287 changed_bytes(pos->lnum, idx);
288 idx += (csize - 1 + correct);
289 col += correct;
290 }
291 }
292#endif
293 }
294
295 if (idx < 0)
296 pos->col = 0;
297 else
298 pos->col = idx;
299
300#ifdef FEAT_VIRTUALEDIT
301 pos->coladd = 0;
302
303 if (finetune)
304 {
305 if (wcol == MAXCOL)
306 {
307 /* The width of the last character is used to set coladd. */
308 if (!one_more)
309 {
310 colnr_T scol, ecol;
311
312 getvcol(curwin, pos, &scol, NULL, &ecol);
313 pos->coladd = ecol - scol;
314 }
315 }
316 else
317 {
318 int b = (int)wcol - (int)col;
319
320 /* The difference between wcol and col is used to set coladd. */
321 if (b > 0 && b < (MAXCOL - 2 * W_WIDTH(curwin)))
322 pos->coladd = b;
323
324 col += b;
325 }
326 }
327#endif
328
329#ifdef FEAT_MBYTE
Bram Moolenaara1381de2009-11-03 15:44:21 +0000330 /* prevent from moving onto a trail byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200332 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#endif
334
335 if (col < wcol)
336 return FAIL;
337 return OK;
338}
339
340/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000341 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 */
343 int
344inc_cursor()
345{
346 return inc(&curwin->w_cursor);
347}
348
Bram Moolenaar446cb832008-06-24 21:56:24 +0000349/*
350 * Increment the line pointer "lp" crossing line boundaries as necessary.
351 * Return 1 when going to the next line.
352 * Return 2 when moving forward onto a NUL at the end of the line).
353 * Return -1 when at the end of file.
354 * Return 0 otherwise.
355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 int
357inc(lp)
358 pos_T *lp;
359{
360 char_u *p = ml_get_pos(lp);
361
362 if (*p != NUL) /* still within line, move to next char (may be NUL) */
363 {
364#ifdef FEAT_MBYTE
365 if (has_mbyte)
366 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000367 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
369 lp->col += l;
370 return ((p[l] != NUL) ? 0 : 2);
371 }
372#endif
373 lp->col++;
374#ifdef FEAT_VIRTUALEDIT
375 lp->coladd = 0;
376#endif
377 return ((p[1] != NUL) ? 0 : 2);
378 }
379 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
380 {
381 lp->col = 0;
382 lp->lnum++;
383#ifdef FEAT_VIRTUALEDIT
384 lp->coladd = 0;
385#endif
386 return 1;
387 }
388 return -1;
389}
390
391/*
392 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
393 */
394 int
395incl(lp)
396 pos_T *lp;
397{
398 int r;
399
400 if ((r = inc(lp)) >= 1 && lp->col)
401 r = inc(lp);
402 return r;
403}
404
405/*
406 * dec(p)
407 *
408 * Decrement the line pointer 'p' crossing line boundaries as necessary.
409 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
410 */
411 int
412dec_cursor()
413{
414 return dec(&curwin->w_cursor);
415}
416
417 int
418dec(lp)
419 pos_T *lp;
420{
421 char_u *p;
422
423#ifdef FEAT_VIRTUALEDIT
424 lp->coladd = 0;
425#endif
426 if (lp->col > 0) /* still within line */
427 {
428 lp->col--;
429#ifdef FEAT_MBYTE
430 if (has_mbyte)
431 {
432 p = ml_get(lp->lnum);
433 lp->col -= (*mb_head_off)(p, p + lp->col);
434 }
435#endif
436 return 0;
437 }
438 if (lp->lnum > 1) /* there is a prior line */
439 {
440 lp->lnum--;
441 p = ml_get(lp->lnum);
442 lp->col = (colnr_T)STRLEN(p);
443#ifdef FEAT_MBYTE
444 if (has_mbyte)
445 lp->col -= (*mb_head_off)(p, p + lp->col);
446#endif
447 return 1;
448 }
449 return -1; /* at start of file */
450}
451
452/*
453 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
454 */
455 int
456decl(lp)
457 pos_T *lp;
458{
459 int r;
460
461 if ((r = dec(lp)) == 1 && lp->col)
462 r = dec(lp);
463 return r;
464}
465
466/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200467 * Get the line number relative to the current cursor position, i.e. the
468 * difference between line number and cursor position. Only look for lines that
469 * can be visible, folded lines don't count.
470 */
471 linenr_T
472get_cursor_rel_lnum(wp, lnum)
473 win_T *wp;
474 linenr_T lnum; /* line number to get the result for */
475{
476 linenr_T cursor = wp->w_cursor.lnum;
477 linenr_T retval = 0;
478
479#ifdef FEAT_FOLDING
480 if (hasAnyFolding(wp))
481 {
482 if (lnum > cursor)
483 {
484 while (lnum > cursor)
485 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100486 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200487 /* if lnum and cursor are in the same fold,
488 * now lnum <= cursor */
489 if (lnum > cursor)
490 retval++;
491 lnum--;
492 }
493 }
494 else if (lnum < cursor)
495 {
496 while (lnum < cursor)
497 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100498 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar64486672010-05-16 15:46:46 +0200499 /* if lnum and cursor are in the same fold,
500 * now lnum >= cursor */
501 if (lnum < cursor)
502 retval--;
503 lnum++;
504 }
505 }
506 /* else if (lnum == cursor)
507 * retval = 0;
508 */
509 }
510 else
511#endif
512 retval = lnum - cursor;
513
514 return retval;
515}
516
517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * Make sure curwin->w_cursor.lnum is valid.
519 */
520 void
521check_cursor_lnum()
522{
523 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
524 {
525#ifdef FEAT_FOLDING
526 /* If there is a closed fold at the end of the file, put the cursor in
527 * its first line. Otherwise in the last line. */
528 if (!hasFolding(curbuf->b_ml.ml_line_count,
529 &curwin->w_cursor.lnum, NULL))
530#endif
531 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
532 }
533 if (curwin->w_cursor.lnum <= 0)
534 curwin->w_cursor.lnum = 1;
535}
536
537/*
538 * Make sure curwin->w_cursor.col is valid.
539 */
540 void
541check_cursor_col()
542{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200543 check_cursor_col_win(curwin);
544}
545
546/*
547 * Make sure win->w_cursor.col is valid.
548 */
549 void
550check_cursor_col_win(win)
551 win_T *win;
552{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 colnr_T len;
554#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200555 colnr_T oldcol = win->w_cursor.col;
556 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200559 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200561 win->w_cursor.col = 0;
562 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {
Bram Moolenaar33f54432008-01-04 20:25:44 +0000564 /* Allow cursor past end-of-line when:
565 * - in Insert mode or restarting Insert mode
566 * - in Visual mode and 'selection' isn't "old"
567 * - 'virtualedit' is set */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000568 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000570#ifdef FEAT_VIRTUALEDIT
571 || (ve_flags & VE_ONEMORE)
572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200574 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000576 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200577 win->w_cursor.col = len - 1;
Bram Moolenaar87c19962007-04-26 08:54:21 +0000578#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200579 /* Move the cursor to the head byte. */
Bram Moolenaar87c19962007-04-26 08:54:21 +0000580 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200581 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000582#endif
583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200585 else if (win->w_cursor.col < 0)
586 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588#ifdef FEAT_VIRTUALEDIT
589 /* If virtual editing is on, we can leave the cursor on the old position,
590 * only we must set it to virtual. But don't do it when at the end of the
591 * line. */
592 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200593 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000595 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200596 if (oldcoladd > win->w_cursor.col)
597 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000598 else
599 /* avoid weird number when there is a miscalculation or overflow */
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200600 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#endif
603}
604
605/*
606 * make sure curwin->w_cursor in on a valid character
607 */
608 void
609check_cursor()
610{
611 check_cursor_lnum();
612 check_cursor_col();
613}
614
615#if defined(FEAT_TEXTOBJ) || defined(PROTO)
616/*
617 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
618 * Allow it when in Visual mode and 'selection' is not "old".
619 */
620 void
621adjust_cursor_col()
622{
623 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 && gchar_cursor() == NUL)
626 --curwin->w_cursor.col;
627}
628#endif
629
630/*
631 * When curwin->w_leftcol has changed, adjust the cursor position.
632 * Return TRUE if the cursor was moved.
633 */
634 int
635leftcol_changed()
636{
637 long lastcol;
638 colnr_T s, e;
639 int retval = FALSE;
640
641 changed_cline_bef_curs();
642 lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1;
643 validate_virtcol();
644
645 /*
646 * If the cursor is right or left of the screen, move it to last or first
647 * character.
648 */
649 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
650 {
651 retval = TRUE;
652 coladvance((colnr_T)(lastcol - p_siso));
653 }
654 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
655 {
656 retval = TRUE;
657 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
658 }
659
660 /*
661 * If the start of the character under the cursor is not on the screen,
662 * advance the cursor one more char. If this fails (last char of the
663 * line) adjust the scrolling.
664 */
665 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
666 if (e > (colnr_T)lastcol)
667 {
668 retval = TRUE;
669 coladvance(s - 1);
670 }
671 else if (s < curwin->w_leftcol)
672 {
673 retval = TRUE;
674 if (coladvance(e + 1) == FAIL) /* there isn't another character */
675 {
676 curwin->w_leftcol = s; /* adjust w_leftcol instead */
677 changed_cline_bef_curs();
678 }
679 }
680
681 if (retval)
682 curwin->w_set_curswant = TRUE;
683 redraw_later(NOT_VALID);
684 return retval;
685}
686
687/**********************************************************************
688 * Various routines dealing with allocation and deallocation of memory.
689 */
690
691#if defined(MEM_PROFILE) || defined(PROTO)
692
693# define MEM_SIZES 8200
694static long_u mem_allocs[MEM_SIZES];
695static long_u mem_frees[MEM_SIZES];
696static long_u mem_allocated;
697static long_u mem_freed;
698static long_u mem_peak;
699static long_u num_alloc;
700static long_u num_freed;
701
702static void mem_pre_alloc_s __ARGS((size_t *sizep));
703static void mem_pre_alloc_l __ARGS((long_u *sizep));
704static void mem_post_alloc __ARGS((void **pp, size_t size));
705static void mem_pre_free __ARGS((void **pp));
706
707 static void
708mem_pre_alloc_s(sizep)
709 size_t *sizep;
710{
711 *sizep += sizeof(size_t);
712}
713
714 static void
715mem_pre_alloc_l(sizep)
716 long_u *sizep;
717{
718 *sizep += sizeof(size_t);
719}
720
721 static void
722mem_post_alloc(pp, size)
723 void **pp;
724 size_t size;
725{
726 if (*pp == NULL)
727 return;
728 size -= sizeof(size_t);
729 *(long_u *)*pp = size;
730 if (size <= MEM_SIZES-1)
731 mem_allocs[size-1]++;
732 else
733 mem_allocs[MEM_SIZES-1]++;
734 mem_allocated += size;
735 if (mem_allocated - mem_freed > mem_peak)
736 mem_peak = mem_allocated - mem_freed;
737 num_alloc++;
738 *pp = (void *)((char *)*pp + sizeof(size_t));
739}
740
741 static void
742mem_pre_free(pp)
743 void **pp;
744{
745 long_u size;
746
747 *pp = (void *)((char *)*pp - sizeof(size_t));
748 size = *(size_t *)*pp;
749 if (size <= MEM_SIZES-1)
750 mem_frees[size-1]++;
751 else
752 mem_frees[MEM_SIZES-1]++;
753 mem_freed += size;
754 num_freed++;
755}
756
757/*
758 * called on exit via atexit()
759 */
760 void
761vim_mem_profile_dump()
762{
763 int i, j;
764
765 printf("\r\n");
766 j = 0;
767 for (i = 0; i < MEM_SIZES - 1; i++)
768 {
769 if (mem_allocs[i] || mem_frees[i])
770 {
771 if (mem_frees[i] > mem_allocs[i])
772 printf("\r\n%s", _("ERROR: "));
773 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
774 j++;
775 if (j > 3)
776 {
777 j = 0;
778 printf("\r\n");
779 }
780 }
781 }
782
783 i = MEM_SIZES - 1;
784 if (mem_allocs[i])
785 {
786 printf("\r\n");
787 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200788 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
790 }
791
792 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
793 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
794 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
795 num_alloc, num_freed);
796}
797
798#endif /* MEM_PROFILE */
799
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100800#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100801static int alloc_does_fail __ARGS((long_u size));
802
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100803 static int
Bram Moolenaara260b872016-01-15 20:48:22 +0100804alloc_does_fail(size)
805 long_u size;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100806{
807 if (alloc_fail_countdown == 0)
808 {
809 if (--alloc_fail_repeat <= 0)
810 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100811 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100812 return TRUE;
813 }
814 --alloc_fail_countdown;
815 return FALSE;
816}
817#endif
818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819/*
820 * Some memory is reserved for error messages and for being able to
821 * call mf_release_all(), which needs some memory for mf_trans_add().
822 */
823#if defined(MSDOS) && !defined(DJGPP)
824# define SMALL_MEM
825# define KEEP_ROOM 8192L
826#else
827# define KEEP_ROOM (2 * 8192L)
828#endif
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200829#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000832 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Use lalloc for larger blocks.
834 */
835 char_u *
836alloc(size)
837 unsigned size;
838{
839 return (lalloc((long_u)size, TRUE));
840}
841
842/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100843 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100844 */
845 char_u *
846alloc_id(size, id)
847 unsigned size;
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100848 alloc_id_T id UNUSED;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100849{
850#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100851 if (alloc_fail_id == id && alloc_does_fail((long_u)size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100852 return NULL;
853#endif
854 return (lalloc((long_u)size, TRUE));
855}
856
857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 * Allocate memory and set all bytes to zero.
859 */
860 char_u *
861alloc_clear(size)
862 unsigned size;
863{
864 char_u *p;
865
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200866 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (p != NULL)
868 (void)vim_memset(p, 0, (size_t)size);
869 return p;
870}
871
872/*
873 * alloc() with check for maximum line length
874 */
875 char_u *
876alloc_check(size)
877 unsigned size;
878{
879#if !defined(UNIX) && !defined(__EMX__)
880 if (sizeof(int) == 2 && size > 0x7fff)
881 {
882 /* Don't hide this message */
883 emsg_silent = 0;
884 EMSG(_("E340: Line is becoming too long"));
885 return NULL;
886 }
887#endif
888 return (lalloc((long_u)size, TRUE));
889}
890
891/*
892 * Allocate memory like lalloc() and set all bytes to zero.
893 */
894 char_u *
895lalloc_clear(size, message)
896 long_u size;
897 int message;
898{
899 char_u *p;
900
901 p = (lalloc(size, message));
902 if (p != NULL)
903 (void)vim_memset(p, 0, (size_t)size);
904 return p;
905}
906
907/*
908 * Low level memory allocation function.
909 * This is used often, KEEP IT FAST!
910 */
911 char_u *
912lalloc(size, message)
913 long_u size;
914 int message;
915{
916 char_u *p; /* pointer to new storage space */
917 static int releasing = FALSE; /* don't do mf_release_all() recursive */
918 int try_again;
919#if defined(HAVE_AVAIL_MEM) && !defined(SMALL_MEM)
920 static long_u allocated = 0; /* allocated since last avail check */
921#endif
922
923 /* Safety check for allocating zero bytes */
924 if (size == 0)
925 {
926 /* Don't hide this message */
927 emsg_silent = 0;
928 EMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
929 return NULL;
930 }
931
932#ifdef MEM_PROFILE
933 mem_pre_alloc_l(&size);
934#endif
935
936#if defined(MSDOS) && !defined(DJGPP)
937 if (size >= 0xfff0) /* in MSDOS we can't deal with >64K blocks */
938 p = NULL;
939 else
940#endif
941
942 /*
943 * Loop when out of memory: Try to release some memfile blocks and
944 * if some blocks are released call malloc again.
945 */
946 for (;;)
947 {
948 /*
949 * Handle three kind of systems:
950 * 1. No check for available memory: Just return.
951 * 2. Slow check for available memory: call mch_avail_mem() after
952 * allocating KEEP_ROOM amount of memory.
953 * 3. Strict check for available memory: call mch_avail_mem()
954 */
955 if ((p = (char_u *)malloc((size_t)size)) != NULL)
956 {
957#ifndef HAVE_AVAIL_MEM
958 /* 1. No check for available memory: Just return. */
959 goto theend;
960#else
961# ifndef SMALL_MEM
962 /* 2. Slow check for available memory: call mch_avail_mem() after
963 * allocating (KEEP_ROOM / 2) amount of memory. */
964 allocated += size;
965 if (allocated < KEEP_ROOM / 2)
966 goto theend;
967 allocated = 0;
968# endif
969 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200970 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000972 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 p = NULL;
974 }
975 else
976 goto theend;
977#endif
978 }
979 /*
980 * Remember that mf_release_all() is being called to avoid an endless
981 * loop, because mf_release_all() may call alloc() recursively.
982 */
983 if (releasing)
984 break;
985 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000986
987 clear_sb_text(); /* free any scrollback text */
988 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 releasing = FALSE;
991 if (!try_again)
992 break;
993 }
994
995 if (message && p == NULL)
996 do_outofmem_msg(size);
997
998theend:
999#ifdef MEM_PROFILE
1000 mem_post_alloc((void **)&p, (size_t)size);
1001#endif
1002 return p;
1003}
1004
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001005/*
1006 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001007 */
1008 char_u *
1009lalloc_id(size, message, id)
1010 long_u size;
1011 int message;
Bram Moolenaar28fb79d2016-01-09 22:28:33 +01001012 alloc_id_T id UNUSED;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001013{
1014#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +01001015 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001016 return NULL;
1017#endif
1018 return (lalloc((long_u)size, message));
1019}
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#if defined(MEM_PROFILE) || defined(PROTO)
1022/*
1023 * realloc() with memory profiling.
1024 */
1025 void *
1026mem_realloc(ptr, size)
1027 void *ptr;
1028 size_t size;
1029{
1030 void *p;
1031
1032 mem_pre_free(&ptr);
1033 mem_pre_alloc_s(&size);
1034
1035 p = realloc(ptr, size);
1036
1037 mem_post_alloc(&p, size);
1038
1039 return p;
1040}
1041#endif
1042
1043/*
1044* Avoid repeating the error message many times (they take 1 second each).
1045* Did_outofmem_msg is reset when a character is read.
1046*/
1047 void
1048do_outofmem_msg(size)
1049 long_u size;
1050{
1051 if (!did_outofmem_msg)
1052 {
1053 /* Don't hide this message */
1054 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001055
1056 /* Must come first to avoid coming back here when printing the error
1057 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001059
1060 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062}
1063
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001064#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001065
1066# if defined(FEAT_SEARCHPATH)
1067static void free_findfile __ARGS((void));
1068# endif
1069
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001070/*
1071 * Free everything that we allocated.
1072 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001073 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1074 * surprised if Vim crashes...
1075 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001076 */
1077 void
1078free_all_mem()
1079{
1080 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001081 static int entered = FALSE;
1082
1083 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1084 * Don't try freeing everything again. */
1085 if (entered)
1086 return;
1087 entered = TRUE;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001088
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001089# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001090 /* Don't want to trigger autocommands from here on. */
1091 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001092# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001093
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001094# ifdef FEAT_WINDOWS
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001095 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1096 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001097 if (first_tabpage->tp_next != NULL)
1098 do_cmdline_cmd((char_u *)"tabonly!");
1099 if (firstwin != lastwin)
1100 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001101# endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001102
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001103# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001104 /* Free all spell info. */
1105 spell_free_all();
1106# endif
1107
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001108# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001109 /* Clear user commands (before deleting buffers). */
1110 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001111# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001112
1113# ifdef FEAT_MENU
1114 /* Clear menus. */
1115 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001116# ifdef FEAT_MULTI_LANG
1117 do_cmdline_cmd((char_u *)"menutranslate clear");
1118# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001119# endif
1120
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001121 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001122 do_cmdline_cmd((char_u *)"lmapclear");
1123 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001124 do_cmdline_cmd((char_u *)"mapclear");
1125 do_cmdline_cmd((char_u *)"mapclear!");
1126 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001127# if defined(FEAT_EVAL)
1128 do_cmdline_cmd((char_u *)"breakdel *");
1129# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001130# if defined(FEAT_PROFILE)
1131 do_cmdline_cmd((char_u *)"profdel *");
1132# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001133# if defined(FEAT_KEYMAP)
1134 do_cmdline_cmd((char_u *)"set keymap=");
1135#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001136
1137# ifdef FEAT_TITLE
1138 free_titles();
1139# endif
1140# if defined(FEAT_SEARCHPATH)
1141 free_findfile();
1142# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001143
1144 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001145# if defined(FEAT_AUTOCMD)
1146 free_all_autocmds();
1147# endif
1148 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001149 free_all_options();
1150 free_all_marks();
1151 alist_clear(&global_alist);
1152 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001153# if defined(FEAT_CMDL_COMPL)
1154 free_users();
1155# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001156 free_search_patterns();
1157 free_old_sub();
1158 free_last_insert();
1159 free_prev_shellcmd();
1160 free_regexp_stuff();
1161 free_tag_stuff();
1162 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001163# ifdef FEAT_SIGNS
1164 free_signs();
1165# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001166# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001167 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001168# endif
1169# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001170 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001171# endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001172 clear_sb_text(); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001173
1174 /* Free some global vars. */
1175 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001176# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001177 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001178# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001179 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001180# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001181 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001182# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001183 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001184 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001185
1186 /* Clear cmdline history. */
1187 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001188# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001189 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001190# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001191
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001192#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001193 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001194 win_T *win;
1195 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001196
1197 qf_free_all(NULL);
1198 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001199 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001200 qf_free_all(win);
1201 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001202#endif
1203
1204 /* Close all script inputs. */
1205 close_all_scripts();
1206
1207#if defined(FEAT_WINDOWS)
1208 /* Destroy all windows. Must come before freeing buffers. */
1209 win_free_all();
1210#endif
1211
Bram Moolenaar2a329742008-04-01 12:53:43 +00001212 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1213 * were freed already. */
1214#ifdef FEAT_AUTOCHDIR
1215 p_acd = FALSE;
1216#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001217 for (buf = firstbuf; buf != NULL; )
1218 {
1219 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001220 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001221 if (buf_valid(buf))
1222 buf = nextbuf; /* didn't work, try next one */
1223 else
1224 buf = firstbuf;
1225 }
1226
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001227#ifdef FEAT_ARABIC
1228 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001229#endif
1230
1231 /* Clear registers. */
1232 clear_registers();
1233 ResetRedobuff();
1234 ResetRedobuff();
1235
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001236#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001237 vim_free(serverDelayedStartName);
1238#endif
1239
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001240 /* highlight info */
1241 free_highlight();
1242
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001243 reset_last_sourcing();
1244
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001245#ifdef FEAT_WINDOWS
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001246 free_tabpage(first_tabpage);
1247 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001248#endif
1249
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001250# ifdef UNIX
1251 /* Machine-specific free. */
1252 mch_free_mem();
1253# endif
1254
1255 /* message history */
1256 for (;;)
1257 if (delete_first_msg() == FAIL)
1258 break;
1259
1260# ifdef FEAT_EVAL
1261 eval_clear();
1262# endif
1263
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001264 free_termoptions();
1265
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001266 /* screenlines (can't display anything now!) */
1267 free_screenlines();
1268
1269#if defined(USE_XSMP)
1270 xsmp_close();
1271#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001272#ifdef FEAT_GUI_GTK
1273 gui_mch_free_all();
1274#endif
1275 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001276
1277 vim_free(IObuff);
1278 vim_free(NameBuff);
1279}
1280#endif
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001283 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 */
1285 char_u *
1286vim_strsave(string)
1287 char_u *string;
1288{
1289 char_u *p;
1290 unsigned len;
1291
1292 len = (unsigned)STRLEN(string) + 1;
1293 p = alloc(len);
1294 if (p != NULL)
1295 mch_memmove(p, string, (size_t)len);
1296 return p;
1297}
1298
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001299/*
1300 * Copy up to "len" bytes of "string" into newly allocated memory and
1301 * terminate with a NUL.
1302 * The allocated memory always has size "len + 1", also when "string" is
1303 * shorter.
1304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *
1306vim_strnsave(string, len)
1307 char_u *string;
1308 int len;
1309{
1310 char_u *p;
1311
1312 p = alloc((unsigned)(len + 1));
1313 if (p != NULL)
1314 {
1315 STRNCPY(p, string, len);
1316 p[len] = NUL;
1317 }
1318 return p;
1319}
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321/*
1322 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1323 * by a backslash.
1324 */
1325 char_u *
1326vim_strsave_escaped(string, esc_chars)
1327 char_u *string;
1328 char_u *esc_chars;
1329{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001330 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331}
1332
1333/*
1334 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1335 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001336 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 */
1338 char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001339vim_strsave_escaped_ext(string, esc_chars, cc, bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *string;
1341 char_u *esc_chars;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001342 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int bsl;
1344{
1345 char_u *p;
1346 char_u *p2;
1347 char_u *escaped_string;
1348 unsigned length;
1349#ifdef FEAT_MBYTE
1350 int l;
1351#endif
1352
1353 /*
1354 * First count the number of backslashes required.
1355 * Then allocate the memory and insert them.
1356 */
1357 length = 1; /* count the trailing NUL */
1358 for (p = string; *p; p++)
1359 {
1360#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001361 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
1363 length += l; /* count a multibyte char */
1364 p += l - 1;
1365 continue;
1366 }
1367#endif
1368 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1369 ++length; /* count a backslash */
1370 ++length; /* count an ordinary char */
1371 }
1372 escaped_string = alloc(length);
1373 if (escaped_string != NULL)
1374 {
1375 p2 = escaped_string;
1376 for (p = string; *p; p++)
1377 {
1378#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001379 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
1381 mch_memmove(p2, p, (size_t)l);
1382 p2 += l;
1383 p += l - 1; /* skip multibyte char */
1384 continue;
1385 }
1386#endif
1387 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001388 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 *p2++ = *p;
1390 }
1391 *p2 = NUL;
1392 }
1393 return escaped_string;
1394}
1395
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001396/*
1397 * Return TRUE when 'shell' has "csh" in the tail.
1398 */
1399 int
1400csh_like_shell()
1401{
1402 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1403}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001404
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001405/*
1406 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001407 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001408 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001409 * Escape a newline, depending on the 'shell' option.
1410 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1411 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001412 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001413 * Returns the result in allocated memory, NULL if we have run out.
1414 */
1415 char_u *
Bram Moolenaar26df0922014-02-23 23:39:13 +01001416vim_strsave_shellescape(string, do_special, do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001417 char_u *string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001418 int do_special;
Bram Moolenaar26df0922014-02-23 23:39:13 +01001419 int do_newline;
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001420{
1421 unsigned length;
1422 char_u *p;
1423 char_u *d;
1424 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001425 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001426 int csh_like;
1427
1428 /* Only csh and similar shells expand '!' within single quotes. For sh and
1429 * the like we must not put a backslash before it, it will be taken
1430 * literally. If do_special is set the '!' will be escaped twice.
1431 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001432 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001433
1434 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001435 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001436 for (p = string; *p != NUL; mb_ptr_adv(p))
1437 {
1438# if defined(WIN32) || defined(WIN16) || defined(DOS)
1439 if (!p_ssl)
1440 {
1441 if (*p == '"')
1442 ++length; /* " -> "" */
1443 }
1444 else
1445# endif
1446 if (*p == '\'')
1447 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001448 if ((*p == '\n' && (csh_like || do_newline))
1449 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001450 {
1451 ++length; /* insert backslash */
1452 if (csh_like && do_special)
1453 ++length; /* insert backslash */
1454 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001455 if (do_special && find_cmdline_var(p, &l) >= 0)
1456 {
1457 ++length; /* insert backslash */
1458 p += l - 1;
1459 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001460 }
1461
1462 /* Allocate memory for the result and fill it. */
1463 escaped_string = alloc(length);
1464 if (escaped_string != NULL)
1465 {
1466 d = escaped_string;
1467
1468 /* add opening quote */
1469# if defined(WIN32) || defined(WIN16) || defined(DOS)
1470 if (!p_ssl)
1471 *d++ = '"';
1472 else
1473# endif
1474 *d++ = '\'';
1475
1476 for (p = string; *p != NUL; )
1477 {
1478# if defined(WIN32) || defined(WIN16) || defined(DOS)
1479 if (!p_ssl)
1480 {
1481 if (*p == '"')
1482 {
1483 *d++ = '"';
1484 *d++ = '"';
1485 ++p;
1486 continue;
1487 }
1488 }
1489 else
1490# endif
1491 if (*p == '\'')
1492 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001493 *d++ = '\'';
1494 *d++ = '\\';
1495 *d++ = '\'';
1496 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001497 ++p;
1498 continue;
1499 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001500 if ((*p == '\n' && (csh_like || do_newline))
1501 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001502 {
1503 *d++ = '\\';
1504 if (csh_like && do_special)
1505 *d++ = '\\';
1506 *d++ = *p++;
1507 continue;
1508 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001509 if (do_special && find_cmdline_var(p, &l) >= 0)
1510 {
1511 *d++ = '\\'; /* insert backslash */
1512 while (--l >= 0) /* copy the var */
1513 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001514 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001515 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001516
1517 MB_COPY_CHAR(p, d);
1518 }
1519
1520 /* add terminating quote and finish with a NUL */
1521# if defined(WIN32) || defined(WIN16) || defined(DOS)
1522 if (!p_ssl)
1523 *d++ = '"';
1524 else
1525# endif
1526 *d++ = '\'';
1527 *d = NUL;
1528 }
1529
1530 return escaped_string;
1531}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533/*
1534 * Like vim_strsave(), but make all characters uppercase.
1535 * This uses ASCII lower-to-upper case translation, language independent.
1536 */
1537 char_u *
1538vim_strsave_up(string)
1539 char_u *string;
1540{
1541 char_u *p1;
1542
1543 p1 = vim_strsave(string);
1544 vim_strup(p1);
1545 return p1;
1546}
1547
1548/*
1549 * Like vim_strnsave(), but make all characters uppercase.
1550 * This uses ASCII lower-to-upper case translation, language independent.
1551 */
1552 char_u *
1553vim_strnsave_up(string, len)
1554 char_u *string;
1555 int len;
1556{
1557 char_u *p1;
1558
1559 p1 = vim_strnsave(string, len);
1560 vim_strup(p1);
1561 return p1;
1562}
1563
1564/*
1565 * ASCII lower-to-upper case translation, language independent.
1566 */
1567 void
1568vim_strup(p)
1569 char_u *p;
1570{
1571 char_u *p2;
1572 int c;
1573
1574 if (p != NULL)
1575 {
1576 p2 = p;
1577 while ((c = *p2) != NUL)
1578#ifdef EBCDIC
1579 *p2++ = isalpha(c) ? toupper(c) : c;
1580#else
1581 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1582#endif
1583 }
1584}
1585
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001586#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001587/*
1588 * Make string "s" all upper-case and return it in allocated memory.
1589 * Handles multi-byte characters as well as possible.
1590 * Returns NULL when out of memory.
1591 */
1592 char_u *
1593strup_save(orig)
1594 char_u *orig;
1595{
1596 char_u *p;
1597 char_u *res;
1598
1599 res = p = vim_strsave(orig);
1600
1601 if (res != NULL)
1602 while (*p != NUL)
1603 {
1604# ifdef FEAT_MBYTE
1605 int l;
1606
1607 if (enc_utf8)
1608 {
1609 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001610 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001611 char_u *s;
1612
1613 c = utf_ptr2char(p);
1614 uc = utf_toupper(c);
1615
1616 /* Reallocate string when byte count changes. This is rare,
1617 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001618 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001619 newl = utf_char2len(uc);
1620 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001621 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001622 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001623 if (s == NULL)
1624 break;
1625 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001626 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001627 p = s + (p - res);
1628 vim_free(res);
1629 res = s;
1630 }
1631
1632 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001633 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001634 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001635 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001636 p += l; /* skip multi-byte character */
1637 else
1638# endif
1639 {
1640 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1641 p++;
1642 }
1643 }
1644
1645 return res;
1646}
1647#endif
1648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * delete spaces at the end of a string
1651 */
1652 void
1653del_trailing_spaces(ptr)
1654 char_u *ptr;
1655{
1656 char_u *q;
1657
1658 q = ptr + STRLEN(ptr);
1659 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
1660 *q = NUL;
1661}
1662
1663/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001664 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001665 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 */
1667 void
1668vim_strncpy(to, from, len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001669 char_u *to;
1670 char_u *from;
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001671 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001673 STRNCPY(to, from, len);
1674 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675}
1676
1677/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001678 * Like strcat(), but make sure the result fits in "tosize" bytes and is
1679 * always NUL terminated.
1680 */
1681 void
1682vim_strcat(to, from, tosize)
1683 char_u *to;
1684 char_u *from;
1685 size_t tosize;
1686{
1687 size_t tolen = STRLEN(to);
1688 size_t fromlen = STRLEN(from);
1689
1690 if (tolen + fromlen + 1 > tosize)
1691 {
1692 mch_memmove(to + tolen, from, tosize - tolen - 1);
1693 to[tosize - 1] = NUL;
1694 }
1695 else
1696 STRCPY(to + tolen, from);
1697}
1698
1699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 * Isolate one part of a string option where parts are separated with
1701 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001702 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 * "*option" is advanced to the next part.
1704 * The length is returned.
1705 */
1706 int
1707copy_option_part(option, buf, maxlen, sep_chars)
1708 char_u **option;
1709 char_u *buf;
1710 int maxlen;
1711 char *sep_chars;
1712{
1713 int len = 0;
1714 char_u *p = *option;
1715
1716 /* skip '.' at start of option part, for 'suffixes' */
1717 if (*p == '.')
1718 buf[len++] = *p++;
1719 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1720 {
1721 /*
1722 * Skip backslash before a separator character and space.
1723 */
1724 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1725 ++p;
1726 if (len < maxlen - 1)
1727 buf[len++] = *p;
1728 ++p;
1729 }
1730 buf[len] = NUL;
1731
1732 if (*p != NUL && *p != ',') /* skip non-standard separator */
1733 ++p;
1734 p = skip_to_option_part(p); /* p points to next file name */
1735
1736 *option = p;
1737 return len;
1738}
1739
1740/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001741 * Replacement for free() that ignores NULL pointers.
1742 * Also skip free() when exiting for sure, this helps when we caught a deadly
1743 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 */
1745 void
1746vim_free(x)
1747 void *x;
1748{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001749 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 {
1751#ifdef MEM_PROFILE
1752 mem_pre_free(&x);
1753#endif
1754 free(x);
1755 }
1756}
1757
1758#ifndef HAVE_MEMSET
1759 void *
1760vim_memset(ptr, c, size)
1761 void *ptr;
1762 int c;
1763 size_t size;
1764{
1765 char *p = ptr;
1766
1767 while (size-- > 0)
1768 *p++ = c;
1769 return ptr;
1770}
1771#endif
1772
1773#ifdef VIM_MEMCMP
1774/*
1775 * Return zero when "b1" and "b2" are the same for "len" bytes.
1776 * Return non-zero otherwise.
1777 */
1778 int
1779vim_memcmp(b1, b2, len)
1780 void *b1;
1781 void *b2;
1782 size_t len;
1783{
1784 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2;
1785
1786 for ( ; len > 0; --len)
1787 {
1788 if (*p1 != *p2)
1789 return 1;
1790 ++p1;
1791 ++p2;
1792 }
1793 return 0;
1794}
1795#endif
1796
1797#ifdef VIM_MEMMOVE
1798/*
1799 * Version of memmove() that handles overlapping source and destination.
1800 * For systems that don't have a function that is guaranteed to do that (SYSV).
1801 */
1802 void
1803mch_memmove(dst_arg, src_arg, len)
1804 void *src_arg, *dst_arg;
1805 size_t len;
1806{
1807 /*
1808 * A void doesn't have a size, we use char pointers.
1809 */
1810 char *dst = dst_arg, *src = src_arg;
1811
1812 /* overlap, copy backwards */
1813 if (dst > src && dst < src + len)
1814 {
1815 src += len;
1816 dst += len;
1817 while (len-- > 0)
1818 *--dst = *--src;
1819 }
1820 else /* copy forwards */
1821 while (len-- > 0)
1822 *dst++ = *src++;
1823}
1824#endif
1825
1826#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1827/*
1828 * Compare two strings, ignoring case, using current locale.
1829 * Doesn't work for multi-byte characters.
1830 * return 0 for match, < 0 for smaller, > 0 for bigger
1831 */
1832 int
1833vim_stricmp(s1, s2)
1834 char *s1;
1835 char *s2;
1836{
1837 int i;
1838
1839 for (;;)
1840 {
1841 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1842 if (i != 0)
1843 return i; /* this character different */
1844 if (*s1 == NUL)
1845 break; /* strings match until NUL */
1846 ++s1;
1847 ++s2;
1848 }
1849 return 0; /* strings match */
1850}
1851#endif
1852
1853#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1854/*
1855 * Compare two strings, for length "len", ignoring case, using current locale.
1856 * Doesn't work for multi-byte characters.
1857 * return 0 for match, < 0 for smaller, > 0 for bigger
1858 */
1859 int
1860vim_strnicmp(s1, s2, len)
1861 char *s1;
1862 char *s2;
1863 size_t len;
1864{
1865 int i;
1866
1867 while (len > 0)
1868 {
1869 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1870 if (i != 0)
1871 return i; /* this character different */
1872 if (*s1 == NUL)
1873 break; /* strings match until NUL */
1874 ++s1;
1875 ++s2;
1876 --len;
1877 }
1878 return 0; /* strings match */
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
1883 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001884 * with characters from 128 to 255 correctly. It also doesn't return a
1885 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 */
1887 char_u *
1888vim_strchr(string, c)
1889 char_u *string;
1890 int c;
1891{
1892 char_u *p;
1893 int b;
1894
1895 p = string;
1896#ifdef FEAT_MBYTE
1897 if (enc_utf8 && c >= 0x80)
1898 {
1899 while (*p != NUL)
1900 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001901 int l = (*mb_ptr2len)(p);
1902
1903 /* Avoid matching an illegal byte here. */
1904 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001906 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 return NULL;
1909 }
1910 if (enc_dbcs != 0 && c > 255)
1911 {
1912 int n2 = c & 0xff;
1913
1914 c = ((unsigned)c >> 8) & 0xff;
1915 while ((b = *p) != NUL)
1916 {
1917 if (b == c && p[1] == n2)
1918 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001919 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 return NULL;
1922 }
1923 if (has_mbyte)
1924 {
1925 while ((b = *p) != NUL)
1926 {
1927 if (b == c)
1928 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001929 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 return NULL;
1932 }
1933#endif
1934 while ((b = *p) != NUL)
1935 {
1936 if (b == c)
1937 return p;
1938 ++p;
1939 }
1940 return NULL;
1941}
1942
1943/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001944 * Version of strchr() that only works for bytes and handles unsigned char
1945 * strings with characters above 128 correctly. It also doesn't return a
1946 * pointer to the NUL at the end of the string.
1947 */
1948 char_u *
1949vim_strbyte(string, c)
1950 char_u *string;
1951 int c;
1952{
1953 char_u *p = string;
1954
1955 while (*p != NUL)
1956 {
1957 if (*p == c)
1958 return p;
1959 ++p;
1960 }
1961 return NULL;
1962}
1963
1964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001966 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001967 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 */
1969 char_u *
1970vim_strrchr(string, c)
1971 char_u *string;
1972 int c;
1973{
1974 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001975 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
Bram Moolenaar05159a02005-02-26 23:04:13 +00001977 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001979 if (*p == c)
1980 retval = p;
1981 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983 return retval;
1984}
1985
1986/*
1987 * Vim's version of strpbrk(), in case it's missing.
1988 * Don't generate a prototype for this, causes problems when it's not used.
1989 */
1990#ifndef PROTO
1991# ifndef HAVE_STRPBRK
1992# ifdef vim_strpbrk
1993# undef vim_strpbrk
1994# endif
1995 char_u *
1996vim_strpbrk(s, charset)
1997 char_u *s;
1998 char_u *charset;
1999{
2000 while (*s)
2001 {
2002 if (vim_strchr(charset, *s) != NULL)
2003 return s;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002004 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 return NULL;
2007}
2008# endif
2009#endif
2010
2011/*
2012 * Vim has its own isspace() function, because on some machines isspace()
2013 * can't handle characters above 128.
2014 */
2015 int
2016vim_isspace(x)
2017 int x;
2018{
2019 return ((x >= 9 && x <= 13) || x == ' ');
2020}
2021
2022/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002023 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 */
2025
2026/*
2027 * Clear an allocated growing array.
2028 */
2029 void
2030ga_clear(gap)
2031 garray_T *gap;
2032{
2033 vim_free(gap->ga_data);
2034 ga_init(gap);
2035}
2036
2037/*
2038 * Clear a growing array that contains a list of strings.
2039 */
2040 void
2041ga_clear_strings(gap)
2042 garray_T *gap;
2043{
2044 int i;
2045
2046 for (i = 0; i < gap->ga_len; ++i)
2047 vim_free(((char_u **)(gap->ga_data))[i]);
2048 ga_clear(gap);
2049}
2050
2051/*
2052 * Initialize a growing array. Don't forget to set ga_itemsize and
2053 * ga_growsize! Or use ga_init2().
2054 */
2055 void
2056ga_init(gap)
2057 garray_T *gap;
2058{
2059 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002060 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 gap->ga_len = 0;
2062}
2063
2064 void
2065ga_init2(gap, itemsize, growsize)
2066 garray_T *gap;
2067 int itemsize;
2068 int growsize;
2069{
2070 ga_init(gap);
2071 gap->ga_itemsize = itemsize;
2072 gap->ga_growsize = growsize;
2073}
2074
2075/*
2076 * Make room in growing array "gap" for at least "n" items.
2077 * Return FAIL for failure, OK otherwise.
2078 */
2079 int
2080ga_grow(gap, n)
2081 garray_T *gap;
2082 int n;
2083{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002084 size_t old_len;
2085 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 char_u *pp;
2087
Bram Moolenaar86b68352004-12-27 21:59:20 +00002088 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 if (n < gap->ga_growsize)
2091 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002092 new_len = gap->ga_itemsize * (gap->ga_len + n);
2093 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002094 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 if (pp == NULL)
2096 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002097 old_len = gap->ga_itemsize * gap->ga_maxlen;
2098 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002099 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 gap->ga_data = pp;
2101 }
2102 return OK;
2103}
2104
2105/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002106 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002107 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002108 * Returns NULL when out of memory.
2109 */
2110 char_u *
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002111ga_concat_strings(gap, sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002112 garray_T *gap;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002113 char *sep;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002114{
2115 int i;
2116 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002117 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002118 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002119 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002120
2121 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002122 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002123
2124 s = alloc(len + 1);
2125 if (s != NULL)
2126 {
2127 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002128 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002129 for (i = 0; i < gap->ga_len; ++i)
2130 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002131 if (p != s)
2132 {
2133 STRCPY(p, sep);
2134 p += sep_len;
2135 }
2136 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2137 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002138 }
2139 }
2140 return s;
2141}
2142
2143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002145 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 * Note: Does NOT copy the NUL at the end!
2147 */
2148 void
2149ga_concat(gap, s)
2150 garray_T *gap;
2151 char_u *s;
2152{
Bram Moolenaar43345542015-11-29 17:35:35 +01002153 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaar43345542015-11-29 17:35:35 +01002155 if (s == NULL)
2156 return;
2157 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (ga_grow(gap, len) == OK)
2159 {
2160 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2161 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163}
2164
2165/*
2166 * Append one byte to a growarray which contains bytes.
2167 */
2168 void
2169ga_append(gap, c)
2170 garray_T *gap;
2171 int c;
2172{
2173 if (ga_grow(gap, 1) == OK)
2174 {
2175 *((char *)gap->ga_data + gap->ga_len) = c;
2176 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178}
2179
Bram Moolenaar597a4222014-06-25 14:39:50 +02002180#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2181 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002182/*
2183 * Append the text in "gap" below the cursor line and clear "gap".
2184 */
2185 void
2186append_ga_line(gap)
2187 garray_T *gap;
2188{
2189 /* Remove trailing CR. */
2190 if (gap->ga_len > 0
2191 && !curbuf->b_p_bin
2192 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2193 --gap->ga_len;
2194 ga_append(gap, NUL);
2195 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2196 gap->ga_len = 0;
2197}
2198#endif
2199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200/************************************************************************
2201 * functions that use lookup tables for various things, generally to do with
2202 * special key codes.
2203 */
2204
2205/*
2206 * Some useful tables.
2207 */
2208
2209static struct modmasktable
2210{
2211 short mod_mask; /* Bit-mask for particular key modifier */
2212 short mod_flag; /* Bit(s) for particular key modifier */
2213 char_u name; /* Single letter name of modifier */
2214} mod_mask_table[] =
2215{
2216 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002217 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2219 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2220 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2221 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2222 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
2223#ifdef MACOS
2224 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2225#endif
2226 /* 'A' must be the last one */
2227 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2228 {0, 0, NUL}
2229};
2230
2231/*
2232 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002233 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
2235#define MOD_KEYS_ENTRY_SIZE 5
2236
2237static char_u modifier_keys_table[] =
2238{
2239/* mod mask with modifier without modifier */
2240 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2241 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2242 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2243 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2244 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2245 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2246 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2247 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2248 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2249 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2250 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2251 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2252 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2253 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2254 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2255 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2256 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2257 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2258 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2259 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2260 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2261 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2262 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2263 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2264 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2265 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2266 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2267 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2268 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2269 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2270 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2273
2274 /* vt100 F1 */
2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2279
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2290
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2300 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2301
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2306 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2307 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2308 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2309 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2310 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2311 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2312
2313 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2314 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2315 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2316 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2317 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2318 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2319 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2320
2321 /* TAB pseudo code*/
2322 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2323
2324 NUL
2325};
2326
2327static struct key_name_entry
2328{
2329 int key; /* Special key code or ascii value */
2330 char_u *name; /* Name of key */
2331} key_names_table[] =
2332{
2333 {' ', (char_u *)"Space"},
2334 {TAB, (char_u *)"Tab"},
2335 {K_TAB, (char_u *)"Tab"},
2336 {NL, (char_u *)"NL"},
2337 {NL, (char_u *)"NewLine"}, /* Alternative name */
2338 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2339 {NL, (char_u *)"LF"}, /* Alternative name */
2340 {CAR, (char_u *)"CR"},
2341 {CAR, (char_u *)"Return"}, /* Alternative name */
2342 {CAR, (char_u *)"Enter"}, /* Alternative name */
2343 {K_BS, (char_u *)"BS"},
2344 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2345 {ESC, (char_u *)"Esc"},
2346 {CSI, (char_u *)"CSI"},
2347 {K_CSI, (char_u *)"xCSI"},
2348 {'|', (char_u *)"Bar"},
2349 {'\\', (char_u *)"Bslash"},
2350 {K_DEL, (char_u *)"Del"},
2351 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2352 {K_KDEL, (char_u *)"kDel"},
2353 {K_UP, (char_u *)"Up"},
2354 {K_DOWN, (char_u *)"Down"},
2355 {K_LEFT, (char_u *)"Left"},
2356 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002357 {K_XUP, (char_u *)"xUp"},
2358 {K_XDOWN, (char_u *)"xDown"},
2359 {K_XLEFT, (char_u *)"xLeft"},
2360 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 {K_F1, (char_u *)"F1"},
2363 {K_F2, (char_u *)"F2"},
2364 {K_F3, (char_u *)"F3"},
2365 {K_F4, (char_u *)"F4"},
2366 {K_F5, (char_u *)"F5"},
2367 {K_F6, (char_u *)"F6"},
2368 {K_F7, (char_u *)"F7"},
2369 {K_F8, (char_u *)"F8"},
2370 {K_F9, (char_u *)"F9"},
2371 {K_F10, (char_u *)"F10"},
2372
2373 {K_F11, (char_u *)"F11"},
2374 {K_F12, (char_u *)"F12"},
2375 {K_F13, (char_u *)"F13"},
2376 {K_F14, (char_u *)"F14"},
2377 {K_F15, (char_u *)"F15"},
2378 {K_F16, (char_u *)"F16"},
2379 {K_F17, (char_u *)"F17"},
2380 {K_F18, (char_u *)"F18"},
2381 {K_F19, (char_u *)"F19"},
2382 {K_F20, (char_u *)"F20"},
2383
2384 {K_F21, (char_u *)"F21"},
2385 {K_F22, (char_u *)"F22"},
2386 {K_F23, (char_u *)"F23"},
2387 {K_F24, (char_u *)"F24"},
2388 {K_F25, (char_u *)"F25"},
2389 {K_F26, (char_u *)"F26"},
2390 {K_F27, (char_u *)"F27"},
2391 {K_F28, (char_u *)"F28"},
2392 {K_F29, (char_u *)"F29"},
2393 {K_F30, (char_u *)"F30"},
2394
2395 {K_F31, (char_u *)"F31"},
2396 {K_F32, (char_u *)"F32"},
2397 {K_F33, (char_u *)"F33"},
2398 {K_F34, (char_u *)"F34"},
2399 {K_F35, (char_u *)"F35"},
2400 {K_F36, (char_u *)"F36"},
2401 {K_F37, (char_u *)"F37"},
2402
2403 {K_XF1, (char_u *)"xF1"},
2404 {K_XF2, (char_u *)"xF2"},
2405 {K_XF3, (char_u *)"xF3"},
2406 {K_XF4, (char_u *)"xF4"},
2407
2408 {K_HELP, (char_u *)"Help"},
2409 {K_UNDO, (char_u *)"Undo"},
2410 {K_INS, (char_u *)"Insert"},
2411 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2412 {K_KINS, (char_u *)"kInsert"},
2413 {K_HOME, (char_u *)"Home"},
2414 {K_KHOME, (char_u *)"kHome"},
2415 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002416 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {K_END, (char_u *)"End"},
2418 {K_KEND, (char_u *)"kEnd"},
2419 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002420 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {K_PAGEUP, (char_u *)"PageUp"},
2422 {K_PAGEDOWN, (char_u *)"PageDown"},
2423 {K_KPAGEUP, (char_u *)"kPageUp"},
2424 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2425
2426 {K_KPLUS, (char_u *)"kPlus"},
2427 {K_KMINUS, (char_u *)"kMinus"},
2428 {K_KDIVIDE, (char_u *)"kDivide"},
2429 {K_KMULTIPLY, (char_u *)"kMultiply"},
2430 {K_KENTER, (char_u *)"kEnter"},
2431 {K_KPOINT, (char_u *)"kPoint"},
2432
2433 {K_K0, (char_u *)"k0"},
2434 {K_K1, (char_u *)"k1"},
2435 {K_K2, (char_u *)"k2"},
2436 {K_K3, (char_u *)"k3"},
2437 {K_K4, (char_u *)"k4"},
2438 {K_K5, (char_u *)"k5"},
2439 {K_K6, (char_u *)"k6"},
2440 {K_K7, (char_u *)"k7"},
2441 {K_K8, (char_u *)"k8"},
2442 {K_K9, (char_u *)"k9"},
2443
2444 {'<', (char_u *)"lt"},
2445
2446 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002447#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002449#endif
2450#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002452#endif
2453#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002455#endif
2456#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002458#endif
2459#ifdef FEAT_MOUSE_URXVT
2460 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2461#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002462#ifdef FEAT_MOUSE_SGR
2463 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
2464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2466 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2467 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2468 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2469 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2470 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2471 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2472 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2473 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2474 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2475 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002476 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2477 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2478 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2479 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2480 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2481 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {K_X1MOUSE, (char_u *)"X1Mouse"},
2483 {K_X1DRAG, (char_u *)"X1Drag"},
2484 {K_X1RELEASE, (char_u *)"X1Release"},
2485 {K_X2MOUSE, (char_u *)"X2Mouse"},
2486 {K_X2DRAG, (char_u *)"X2Drag"},
2487 {K_X2RELEASE, (char_u *)"X2Release"},
2488 {K_DROP, (char_u *)"Drop"},
2489 {K_ZERO, (char_u *)"Nul"},
2490#ifdef FEAT_EVAL
2491 {K_SNR, (char_u *)"SNR"},
2492#endif
2493 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002494 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {0, NULL}
2496};
2497
2498#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2499
2500#ifdef FEAT_MOUSE
2501static struct mousetable
2502{
2503 int pseudo_code; /* Code for pseudo mouse event */
2504 int button; /* Which mouse button is it? */
2505 int is_click; /* Is it a mouse button click event? */
2506 int is_drag; /* Is it a mouse drag event? */
2507} mouse_table[] =
2508{
2509 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2510#ifdef FEAT_GUI
2511 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2512#endif
2513 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2514 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2515#ifdef FEAT_GUI
2516 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2517#endif
2518 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2519 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2520 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2521 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2522 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2523 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2524 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2525 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2526 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2527 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2528 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2529 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2530 /* DRAG without CLICK */
2531 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2532 /* RELEASE without CLICK */
2533 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2534 {0, 0, 0, 0},
2535};
2536#endif /* FEAT_MOUSE */
2537
2538/*
2539 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2540 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2541 */
2542 int
2543name_to_mod_mask(c)
2544 int c;
2545{
2546 int i;
2547
2548 c = TOUPPER_ASC(c);
2549 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2550 if (c == mod_mask_table[i].name)
2551 return mod_mask_table[i].mod_flag;
2552 return 0;
2553}
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555/*
2556 * Check if if there is a special key code for "key" that includes the
2557 * modifiers specified.
2558 */
2559 int
2560simplify_key(key, modifiers)
2561 int key;
2562 int *modifiers;
2563{
2564 int i;
2565 int key0;
2566 int key1;
2567
2568 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2569 {
2570 /* TAB is a special case */
2571 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2572 {
2573 *modifiers &= ~MOD_MASK_SHIFT;
2574 return K_S_TAB;
2575 }
2576 key0 = KEY2TERMCAP0(key);
2577 key1 = KEY2TERMCAP1(key);
2578 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2579 if (key0 == modifier_keys_table[i + 3]
2580 && key1 == modifier_keys_table[i + 4]
2581 && (*modifiers & modifier_keys_table[i]))
2582 {
2583 *modifiers &= ~modifier_keys_table[i];
2584 return TERMCAP2KEY(modifier_keys_table[i + 1],
2585 modifier_keys_table[i + 2]);
2586 }
2587 }
2588 return key;
2589}
2590
2591/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002592 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002593 */
2594 int
2595handle_x_keys(key)
2596 int key;
2597{
2598 switch (key)
2599 {
2600 case K_XUP: return K_UP;
2601 case K_XDOWN: return K_DOWN;
2602 case K_XLEFT: return K_LEFT;
2603 case K_XRIGHT: return K_RIGHT;
2604 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002605 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002606 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002607 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002608 case K_XF1: return K_F1;
2609 case K_XF2: return K_F2;
2610 case K_XF3: return K_F3;
2611 case K_XF4: return K_F4;
2612 case K_S_XF1: return K_S_F1;
2613 case K_S_XF2: return K_S_F2;
2614 case K_S_XF3: return K_S_F3;
2615 case K_S_XF4: return K_S_F4;
2616 }
2617 return key;
2618}
2619
2620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 * Return a string which contains the name of the given key when the given
2622 * modifiers are down.
2623 */
2624 char_u *
2625get_special_key_name(c, modifiers)
2626 int c;
2627 int modifiers;
2628{
2629 static char_u string[MAX_KEY_NAME_LEN + 1];
2630
2631 int i, idx;
2632 int table_idx;
2633 char_u *s;
2634
2635 string[0] = '<';
2636 idx = 1;
2637
2638 /* Key that stands for a normal character. */
2639 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2640 c = KEY2TERMCAP1(c);
2641
2642 /*
2643 * Translate shifted special keys into unshifted keys and set modifier.
2644 * Same for CTRL and ALT modifiers.
2645 */
2646 if (IS_SPECIAL(c))
2647 {
2648 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2649 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2650 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2651 {
2652 modifiers |= modifier_keys_table[i];
2653 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2654 modifier_keys_table[i + 4]);
2655 break;
2656 }
2657 }
2658
2659 /* try to find the key in the special key table */
2660 table_idx = find_special_key_in_table(c);
2661
2662 /*
2663 * When not a known special key, and not a printable character, try to
2664 * extract modifiers.
2665 */
2666 if (c > 0
2667#ifdef FEAT_MBYTE
2668 && (*mb_char2len)(c) == 1
2669#endif
2670 )
2671 {
2672 if (table_idx < 0
2673 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2674 && (c & 0x80))
2675 {
2676 c &= 0x7f;
2677 modifiers |= MOD_MASK_ALT;
2678 /* try again, to find the un-alted key in the special key table */
2679 table_idx = find_special_key_in_table(c);
2680 }
2681 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2682 {
2683#ifdef EBCDIC
2684 c = CtrlChar(c);
2685#else
2686 c += '@';
2687#endif
2688 modifiers |= MOD_MASK_CTRL;
2689 }
2690 }
2691
2692 /* translate the modifier into a string */
2693 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2694 if ((modifiers & mod_mask_table[i].mod_mask)
2695 == mod_mask_table[i].mod_flag)
2696 {
2697 string[idx++] = mod_mask_table[i].name;
2698 string[idx++] = (char_u)'-';
2699 }
2700
2701 if (table_idx < 0) /* unknown special key, may output t_xx */
2702 {
2703 if (IS_SPECIAL(c))
2704 {
2705 string[idx++] = 't';
2706 string[idx++] = '_';
2707 string[idx++] = KEY2TERMCAP0(c);
2708 string[idx++] = KEY2TERMCAP1(c);
2709 }
2710 /* Not a special key, only modifiers, output directly */
2711 else
2712 {
2713#ifdef FEAT_MBYTE
2714 if (has_mbyte && (*mb_char2len)(c) > 1)
2715 idx += (*mb_char2bytes)(c, string + idx);
2716 else
2717#endif
2718 if (vim_isprintc(c))
2719 string[idx++] = c;
2720 else
2721 {
2722 s = transchar(c);
2723 while (*s)
2724 string[idx++] = *s++;
2725 }
2726 }
2727 }
2728 else /* use name of special key */
2729 {
2730 STRCPY(string + idx, key_names_table[table_idx].name);
2731 idx = (int)STRLEN(string);
2732 }
2733 string[idx++] = '>';
2734 string[idx] = NUL;
2735 return string;
2736}
2737
2738/*
2739 * Try translating a <> name at (*srcp)[] to dst[].
2740 * Return the number of characters added to dst[], zero for no match.
2741 * If there is a match, srcp is advanced to after the <> name.
2742 * dst[] must be big enough to hold the result (up to six characters)!
2743 */
2744 int
2745trans_special(srcp, dst, keycode)
2746 char_u **srcp;
2747 char_u *dst;
2748 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2749{
2750 int modifiers = 0;
2751 int key;
2752 int dlen = 0;
2753
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002754 key = find_special_key(srcp, &modifiers, keycode, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (key == 0)
2756 return 0;
2757
2758 /* Put the appropriate modifier in a string */
2759 if (modifiers != 0)
2760 {
2761 dst[dlen++] = K_SPECIAL;
2762 dst[dlen++] = KS_MODIFIER;
2763 dst[dlen++] = modifiers;
2764 }
2765
2766 if (IS_SPECIAL(key))
2767 {
2768 dst[dlen++] = K_SPECIAL;
2769 dst[dlen++] = KEY2TERMCAP0(key);
2770 dst[dlen++] = KEY2TERMCAP1(key);
2771 }
2772#ifdef FEAT_MBYTE
2773 else if (has_mbyte && !keycode)
2774 dlen += (*mb_char2bytes)(key, dst + dlen);
2775#endif
2776 else if (keycode)
2777 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2778 else
2779 dst[dlen++] = key;
2780
2781 return dlen;
2782}
2783
2784/*
2785 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2786 * srcp is advanced to after the <> name.
2787 * returns 0 if there is no match.
2788 */
2789 int
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002790find_special_key(srcp, modp, keycode, keep_x_key)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 char_u **srcp;
2792 int *modp;
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002793 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2794 int keep_x_key; /* don't translate xHome to Home key */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 char_u *last_dash;
2797 char_u *end_of_name;
2798 char_u *src;
2799 char_u *bp;
2800 int modifiers;
2801 int bit;
2802 int key;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002803 unsigned long n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002804 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806 src = *srcp;
2807 if (src[0] != '<')
2808 return 0;
2809
2810 /* Find end of modifier list */
2811 last_dash = src;
2812 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2813 {
2814 if (*bp == '-')
2815 {
2816 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002817 if (bp[1] != NUL)
2818 {
2819#ifdef FEAT_MBYTE
2820 if (has_mbyte)
2821 l = mb_ptr2len(bp + 1);
2822 else
2823#endif
2824 l = 1;
2825 if (bp[l + 1] == '>')
2826 bp += l; /* anything accepted, like <C-?> */
2827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2830 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002831 else if (STRNICMP(bp, "char-", 5) == 0)
2832 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002833 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002834 bp += l + 5;
2835 break;
2836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838
2839 if (*bp == '>') /* found matching '>' */
2840 {
2841 end_of_name = bp + 1;
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 /* Which modifiers are given? */
2844 modifiers = 0x0;
2845 for (bp = src + 1; bp < last_dash; bp++)
2846 {
2847 if (*bp != '-')
2848 {
2849 bit = name_to_mod_mask(*bp);
2850 if (bit == 0x0)
2851 break; /* Illegal modifier name */
2852 modifiers |= bit;
2853 }
2854 }
2855
2856 /*
2857 * Legal modifier name.
2858 */
2859 if (bp >= last_dash)
2860 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002861 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2862 && VIM_ISDIGIT(last_dash[6]))
2863 {
2864 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002865 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002866 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002869 {
Bram Moolenaar792826c2011-08-19 22:29:02 +02002870 /*
2871 * Modifier with single letter, or special key name.
2872 */
2873#ifdef FEAT_MBYTE
2874 if (has_mbyte)
2875 l = mb_ptr2len(last_dash + 1);
2876 else
2877#endif
2878 l = 1;
2879 if (modifiers != 0 && last_dash[l + 1] == '>')
2880 key = PTR2CHAR(last_dash + 1);
2881 else
2882 {
2883 key = get_special_key_code(last_dash + 1);
2884 if (!keep_x_key)
2885 key = handle_x_keys(key);
2886 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
2889 /*
2890 * get_special_key_code() may return NUL for invalid
2891 * special key name.
2892 */
2893 if (key != NUL)
2894 {
2895 /*
2896 * Only use a modifier when there is no special key code that
2897 * includes the modifier.
2898 */
2899 key = simplify_key(key, &modifiers);
2900
2901 if (!keycode)
2902 {
2903 /* don't want keycode, use single byte code */
2904 if (key == K_BS)
2905 key = BS;
2906 else if (key == K_DEL || key == K_KDEL)
2907 key = DEL;
2908 }
2909
2910 /*
2911 * Normal Key with modifier: Try to make a single byte code.
2912 */
2913 if (!IS_SPECIAL(key))
2914 key = extract_modifiers(key, &modifiers);
2915
2916 *modp = modifiers;
2917 *srcp = end_of_name;
2918 return key;
2919 }
2920 }
2921 }
2922 return 0;
2923}
2924
2925/*
2926 * Try to include modifiers in the key.
2927 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2928 */
2929 int
2930extract_modifiers(key, modp)
2931 int key;
2932 int *modp;
2933{
2934 int modifiers = *modp;
2935
2936#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002937 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (!(modifiers & MOD_MASK_CMD))
2939#endif
2940 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2941 {
2942 key = TOUPPER_ASC(key);
2943 modifiers &= ~MOD_MASK_SHIFT;
2944 }
2945 if ((modifiers & MOD_MASK_CTRL)
2946#ifdef EBCDIC
2947 /* * TODO: EBCDIC Better use:
2948 * && (Ctrl_chr(key) || key == '?')
2949 * ??? */
2950 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2951 != NULL
2952#else
2953 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2954#endif
2955 )
2956 {
2957 key = Ctrl_chr(key);
2958 modifiers &= ~MOD_MASK_CTRL;
2959 /* <C-@> is <Nul> */
2960 if (key == 0)
2961 key = K_ZERO;
2962 }
2963#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002964 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (!(modifiers & MOD_MASK_CMD))
2966#endif
2967 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2968#ifdef FEAT_MBYTE
2969 && !enc_dbcs /* avoid creating a lead byte */
2970#endif
2971 )
2972 {
2973 key |= 0x80;
2974 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2975 }
2976
2977 *modp = modifiers;
2978 return key;
2979}
2980
2981/*
2982 * Try to find key "c" in the special key table.
2983 * Return the index when found, -1 when not found.
2984 */
2985 int
2986find_special_key_in_table(c)
2987 int c;
2988{
2989 int i;
2990
2991 for (i = 0; key_names_table[i].name != NULL; i++)
2992 if (c == key_names_table[i].key)
2993 break;
2994 if (key_names_table[i].name == NULL)
2995 i = -1;
2996 return i;
2997}
2998
2999/*
3000 * Find the special key with the given name (the given string does not have to
3001 * end with NUL, the name is assumed to end before the first non-idchar).
3002 * If the name starts with "t_" the next two characters are interpreted as a
3003 * termcap name.
3004 * Return the key code, or 0 if not found.
3005 */
3006 int
3007get_special_key_code(name)
3008 char_u *name;
3009{
3010 char_u *table_name;
3011 char_u string[3];
3012 int i, j;
3013
3014 /*
3015 * If it's <t_xx> we get the code for xx from the termcap
3016 */
3017 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3018 {
3019 string[0] = name[2];
3020 string[1] = name[3];
3021 string[2] = NUL;
3022 if (add_termcap_entry(string, FALSE) == OK)
3023 return TERMCAP2KEY(name[2], name[3]);
3024 }
3025 else
3026 for (i = 0; key_names_table[i].name != NULL; i++)
3027 {
3028 table_name = key_names_table[i].name;
3029 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3030 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3031 break;
3032 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3033 return key_names_table[i].key;
3034 }
3035 return 0;
3036}
3037
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003038#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 char_u *
3040get_key_name(i)
3041 int i;
3042{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003043 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 return NULL;
3045 return key_names_table[i].name;
3046}
3047#endif
3048
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003049#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050/*
3051 * Look up the given mouse code to return the relevant information in the other
3052 * arguments. Return which button is down or was released.
3053 */
3054 int
3055get_mouse_button(code, is_click, is_drag)
3056 int code;
3057 int *is_click;
3058 int *is_drag;
3059{
3060 int i;
3061
3062 for (i = 0; mouse_table[i].pseudo_code; i++)
3063 if (code == mouse_table[i].pseudo_code)
3064 {
3065 *is_click = mouse_table[i].is_click;
3066 *is_drag = mouse_table[i].is_drag;
3067 return mouse_table[i].button;
3068 }
3069 return 0; /* Shouldn't get here */
3070}
3071
3072/*
3073 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3074 * the given information about which mouse button is down, and whether the
3075 * mouse was clicked, dragged or released.
3076 */
3077 int
3078get_pseudo_mouse_code(button, is_click, is_drag)
3079 int button; /* eg MOUSE_LEFT */
3080 int is_click;
3081 int is_drag;
3082{
3083 int i;
3084
3085 for (i = 0; mouse_table[i].pseudo_code; i++)
3086 if (button == mouse_table[i].button
3087 && is_click == mouse_table[i].is_click
3088 && is_drag == mouse_table[i].is_drag)
3089 {
3090#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003091 /* Trick: a non mappable left click and release has mouse_col -1
3092 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3093 * gui_mouse_moved() */
3094 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003096 if (mouse_col < 0)
3097 mouse_col = 0;
3098 else
3099 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3101 return (int)KE_LEFTMOUSE_NM;
3102 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3103 return (int)KE_LEFTRELEASE_NM;
3104 }
3105#endif
3106 return mouse_table[i].pseudo_code;
3107 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003108 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109}
3110#endif /* FEAT_MOUSE */
3111
3112/*
3113 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3114 */
3115 int
3116get_fileformat(buf)
3117 buf_T *buf;
3118{
3119 int c = *buf->b_p_ff;
3120
3121 if (buf->b_p_bin || c == 'u')
3122 return EOL_UNIX;
3123 if (c == 'm')
3124 return EOL_MAC;
3125 return EOL_DOS;
3126}
3127
3128/*
3129 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3130 * argument.
3131 */
3132 int
3133get_fileformat_force(buf, eap)
3134 buf_T *buf;
3135 exarg_T *eap; /* can be NULL! */
3136{
3137 int c;
3138
3139 if (eap != NULL && eap->force_ff != 0)
3140 c = eap->cmd[eap->force_ff];
3141 else
3142 {
3143 if ((eap != NULL && eap->force_bin != 0)
3144 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3145 return EOL_UNIX;
3146 c = *buf->b_p_ff;
3147 }
3148 if (c == 'u')
3149 return EOL_UNIX;
3150 if (c == 'm')
3151 return EOL_MAC;
3152 return EOL_DOS;
3153}
3154
3155/*
3156 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3157 * Sets both 'textmode' and 'fileformat'.
3158 * Note: Does _not_ set global value of 'textmode'!
3159 */
3160 void
3161set_fileformat(t, opt_flags)
3162 int t;
3163 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */
3164{
3165 char *p = NULL;
3166
3167 switch (t)
3168 {
3169 case EOL_DOS:
3170 p = FF_DOS;
3171 curbuf->b_p_tx = TRUE;
3172 break;
3173 case EOL_UNIX:
3174 p = FF_UNIX;
3175 curbuf->b_p_tx = FALSE;
3176 break;
3177 case EOL_MAC:
3178 p = FF_MAC;
3179 curbuf->b_p_tx = FALSE;
3180 break;
3181 }
3182 if (p != NULL)
3183 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003184 OPT_FREE | opt_flags, 0);
3185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00003187 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003189 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#endif
3191#ifdef FEAT_TITLE
3192 need_maketitle = TRUE; /* set window title later */
3193#endif
3194}
3195
3196/*
3197 * Return the default fileformat from 'fileformats'.
3198 */
3199 int
3200default_fileformat()
3201{
3202 switch (*p_ffs)
3203 {
3204 case 'm': return EOL_MAC;
3205 case 'd': return EOL_DOS;
3206 }
3207 return EOL_UNIX;
3208}
3209
3210/*
3211 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3212 */
3213 int
3214call_shell(cmd, opt)
3215 char_u *cmd;
3216 int opt;
3217{
3218 char_u *ncmd;
3219 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003220#ifdef FEAT_PROFILE
3221 proftime_T wait_time;
3222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224 if (p_verbose > 3)
3225 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003226 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003227 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 cmd == NULL ? p_sh : cmd);
3229 out_char('\n');
3230 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003231 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233
Bram Moolenaar05159a02005-02-26 23:04:13 +00003234#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003235 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003236 prof_child_enter(&wait_time);
3237#endif
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (*p_sh == NUL)
3240 {
3241 EMSG(_(e_shellempty));
3242 retval = -1;
3243 }
3244 else
3245 {
3246#ifdef FEAT_GUI_MSWIN
3247 /* Don't hide the pointer while executing a shell command. */
3248 gui_mch_mousehide(FALSE);
3249#endif
3250#ifdef FEAT_GUI
3251 ++hold_gui_events;
3252#endif
3253 /* The external command may update a tags file, clear cached tags. */
3254 tag_freematch();
3255
3256 if (cmd == NULL || *p_sxq == NUL)
3257 retval = mch_call_shell(cmd, opt);
3258 else
3259 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003260 char_u *ecmd = cmd;
3261
3262 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3263 {
3264 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3265 if (ecmd == NULL)
3266 ecmd = cmd;
3267 }
3268 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (ncmd != NULL)
3270 {
3271 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003272 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003273 /* When 'shellxquote' is ( append ).
3274 * When 'shellxquote' is "( append )". */
3275 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3276 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3277 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 retval = mch_call_shell(ncmd, opt);
3279 vim_free(ncmd);
3280 }
3281 else
3282 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003283 if (ecmd != cmd)
3284 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286#ifdef FEAT_GUI
3287 --hold_gui_events;
3288#endif
3289 /*
3290 * Check the window size, in case it changed while executing the
3291 * external command.
3292 */
3293 shell_resized_check();
3294 }
3295
3296#ifdef FEAT_EVAL
3297 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003298# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003299 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003300 prof_child_exit(&wait_time);
3301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#endif
3303
3304 return retval;
3305}
3306
3307/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003308 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3309 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 */
3311 int
3312get_real_state()
3313{
3314 if (State & NORMAL)
3315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003317 {
3318 if (VIsual_select)
3319 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003321 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003322 else if (finish_op)
3323 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325 return State;
3326}
3327
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003328#if defined(FEAT_MBYTE) || defined(PROTO)
3329/*
3330 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003331 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003332 * "b" must point to the start of the file name
3333 */
3334 int
3335after_pathsep(b, p)
3336 char_u *b;
3337 char_u *p;
3338{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003339 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003340 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3341}
3342#endif
3343
3344/*
3345 * Return TRUE if file names "f1" and "f2" are in the same directory.
3346 * "f1" may be a short name, "f2" must be a full path.
3347 */
3348 int
3349same_directory(f1, f2)
3350 char_u *f1;
3351 char_u *f2;
3352{
3353 char_u ffname[MAXPATHL];
3354 char_u *t1;
3355 char_u *t2;
3356
3357 /* safety check */
3358 if (f1 == NULL || f2 == NULL)
3359 return FALSE;
3360
3361 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3362 t1 = gettail_sep(ffname);
3363 t2 = gettail_sep(f2);
3364 return (t1 - ffname == t2 - f2
3365 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3366}
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00003369 || ((defined(FEAT_GUI_GTK)) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00003370 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3372 || defined(PROTO)
3373/*
3374 * Change to a file's directory.
3375 * Caller must call shorten_fnames()!
3376 * Return OK or FAIL.
3377 */
3378 int
3379vim_chdirfile(fname)
3380 char_u *fname;
3381{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003382 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003384 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003385 *gettail_sep(dir) = NUL;
3386 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387}
3388#endif
3389
3390#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3391/*
3392 * Check if "name" ends in a slash and is not a directory.
3393 * Used for systems where stat() ignores a trailing slash on a file name.
3394 * The Vim code assumes a trailing slash is only ignored for a directory.
3395 */
3396 int
3397illegal_slash(name)
3398 char *name;
3399{
3400 if (name[0] == NUL)
3401 return FALSE; /* no file name is not illegal */
3402 if (name[strlen(name) - 1] != '/')
3403 return FALSE; /* no trailing slash */
3404 if (mch_isdir((char_u *)name))
3405 return FALSE; /* trailing slash for a directory */
3406 return TRUE;
3407}
3408#endif
3409
3410#if defined(CURSOR_SHAPE) || defined(PROTO)
3411
3412/*
3413 * Handling of cursor and mouse pointer shapes in various modes.
3414 */
3415
3416cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3417{
3418 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3419 * defaults when Vim starts.
3420 * Adjust the SHAPE_IDX_ defines when making changes! */
3421 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3422 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3423 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3424 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3425 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3426 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3427 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3428 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3429 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3430 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3431 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3432 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3433 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3434 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3435 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3436 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3437 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3438};
3439
3440#ifdef FEAT_MOUSESHAPE
3441/*
3442 * Table with names for mouse shapes. Keep in sync with all the tables for
3443 * mch_set_mouse_shape()!.
3444 */
3445static char * mshape_names[] =
3446{
3447 "arrow", /* default, must be the first one */
3448 "blank", /* hidden */
3449 "beam",
3450 "updown",
3451 "udsizing",
3452 "leftright",
3453 "lrsizing",
3454 "busy",
3455 "no",
3456 "crosshair",
3457 "hand1",
3458 "hand2",
3459 "pencil",
3460 "question",
3461 "rightup-arrow",
3462 "up-arrow",
3463 NULL
3464};
3465#endif
3466
3467/*
3468 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3469 * ("what" is SHAPE_MOUSE).
3470 * Returns error message for an illegal option, NULL otherwise.
3471 */
3472 char_u *
3473parse_shape_opt(what)
3474 int what;
3475{
3476 char_u *modep;
3477 char_u *colonp;
3478 char_u *commap;
3479 char_u *slashp;
3480 char_u *p, *endp;
3481 int idx = 0; /* init for GCC */
3482 int all_idx;
3483 int len;
3484 int i;
3485 long n;
3486 int found_ve = FALSE; /* found "ve" flag */
3487 int round;
3488
3489 /*
3490 * First round: check for errors; second round: do it for real.
3491 */
3492 for (round = 1; round <= 2; ++round)
3493 {
3494 /*
3495 * Repeat for all comma separated parts.
3496 */
3497#ifdef FEAT_MOUSESHAPE
3498 if (what == SHAPE_MOUSE)
3499 modep = p_mouseshape;
3500 else
3501#endif
3502 modep = p_guicursor;
3503 while (*modep != NUL)
3504 {
3505 colonp = vim_strchr(modep, ':');
3506 if (colonp == NULL)
3507 return (char_u *)N_("E545: Missing colon");
3508 if (colonp == modep)
3509 return (char_u *)N_("E546: Illegal mode");
3510 commap = vim_strchr(modep, ',');
3511
3512 /*
3513 * Repeat for all mode's before the colon.
3514 * For the 'a' mode, we loop to handle all the modes.
3515 */
3516 all_idx = -1;
3517 while (modep < colonp || all_idx >= 0)
3518 {
3519 if (all_idx < 0)
3520 {
3521 /* Find the mode. */
3522 if (modep[1] == '-' || modep[1] == ':')
3523 len = 1;
3524 else
3525 len = 2;
3526 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3527 all_idx = SHAPE_IDX_COUNT - 1;
3528 else
3529 {
3530 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3531 if (STRNICMP(modep, shape_table[idx].name, len)
3532 == 0)
3533 break;
3534 if (idx == SHAPE_IDX_COUNT
3535 || (shape_table[idx].used_for & what) == 0)
3536 return (char_u *)N_("E546: Illegal mode");
3537 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3538 found_ve = TRUE;
3539 }
3540 modep += len + 1;
3541 }
3542
3543 if (all_idx >= 0)
3544 idx = all_idx--;
3545 else if (round == 2)
3546 {
3547#ifdef FEAT_MOUSESHAPE
3548 if (what == SHAPE_MOUSE)
3549 {
3550 /* Set the default, for the missing parts */
3551 shape_table[idx].mshape = 0;
3552 }
3553 else
3554#endif
3555 {
3556 /* Set the defaults, for the missing parts */
3557 shape_table[idx].shape = SHAPE_BLOCK;
3558 shape_table[idx].blinkwait = 700L;
3559 shape_table[idx].blinkon = 400L;
3560 shape_table[idx].blinkoff = 250L;
3561 }
3562 }
3563
3564 /* Parse the part after the colon */
3565 for (p = colonp + 1; *p && *p != ','; )
3566 {
3567#ifdef FEAT_MOUSESHAPE
3568 if (what == SHAPE_MOUSE)
3569 {
3570 for (i = 0; ; ++i)
3571 {
3572 if (mshape_names[i] == NULL)
3573 {
3574 if (!VIM_ISDIGIT(*p))
3575 return (char_u *)N_("E547: Illegal mouseshape");
3576 if (round == 2)
3577 shape_table[idx].mshape =
3578 getdigits(&p) + MSHAPE_NUMBERED;
3579 else
3580 (void)getdigits(&p);
3581 break;
3582 }
3583 len = (int)STRLEN(mshape_names[i]);
3584 if (STRNICMP(p, mshape_names[i], len) == 0)
3585 {
3586 if (round == 2)
3587 shape_table[idx].mshape = i;
3588 p += len;
3589 break;
3590 }
3591 }
3592 }
3593 else /* if (what == SHAPE_MOUSE) */
3594#endif
3595 {
3596 /*
3597 * First handle the ones with a number argument.
3598 */
3599 i = *p;
3600 len = 0;
3601 if (STRNICMP(p, "ver", 3) == 0)
3602 len = 3;
3603 else if (STRNICMP(p, "hor", 3) == 0)
3604 len = 3;
3605 else if (STRNICMP(p, "blinkwait", 9) == 0)
3606 len = 9;
3607 else if (STRNICMP(p, "blinkon", 7) == 0)
3608 len = 7;
3609 else if (STRNICMP(p, "blinkoff", 8) == 0)
3610 len = 8;
3611 if (len != 0)
3612 {
3613 p += len;
3614 if (!VIM_ISDIGIT(*p))
3615 return (char_u *)N_("E548: digit expected");
3616 n = getdigits(&p);
3617 if (len == 3) /* "ver" or "hor" */
3618 {
3619 if (n == 0)
3620 return (char_u *)N_("E549: Illegal percentage");
3621 if (round == 2)
3622 {
3623 if (TOLOWER_ASC(i) == 'v')
3624 shape_table[idx].shape = SHAPE_VER;
3625 else
3626 shape_table[idx].shape = SHAPE_HOR;
3627 shape_table[idx].percentage = n;
3628 }
3629 }
3630 else if (round == 2)
3631 {
3632 if (len == 9)
3633 shape_table[idx].blinkwait = n;
3634 else if (len == 7)
3635 shape_table[idx].blinkon = n;
3636 else
3637 shape_table[idx].blinkoff = n;
3638 }
3639 }
3640 else if (STRNICMP(p, "block", 5) == 0)
3641 {
3642 if (round == 2)
3643 shape_table[idx].shape = SHAPE_BLOCK;
3644 p += 5;
3645 }
3646 else /* must be a highlight group name then */
3647 {
3648 endp = vim_strchr(p, '-');
3649 if (commap == NULL) /* last part */
3650 {
3651 if (endp == NULL)
3652 endp = p + STRLEN(p); /* find end of part */
3653 }
3654 else if (endp > commap || endp == NULL)
3655 endp = commap;
3656 slashp = vim_strchr(p, '/');
3657 if (slashp != NULL && slashp < endp)
3658 {
3659 /* "group/langmap_group" */
3660 i = syn_check_group(p, (int)(slashp - p));
3661 p = slashp + 1;
3662 }
3663 if (round == 2)
3664 {
3665 shape_table[idx].id = syn_check_group(p,
3666 (int)(endp - p));
3667 shape_table[idx].id_lm = shape_table[idx].id;
3668 if (slashp != NULL && slashp < endp)
3669 shape_table[idx].id = i;
3670 }
3671 p = endp;
3672 }
3673 } /* if (what != SHAPE_MOUSE) */
3674
3675 if (*p == '-')
3676 ++p;
3677 }
3678 }
3679 modep = p;
3680 if (*modep == ',')
3681 ++modep;
3682 }
3683 }
3684
3685 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3686 if (!found_ve)
3687 {
3688#ifdef FEAT_MOUSESHAPE
3689 if (what == SHAPE_MOUSE)
3690 {
3691 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3692 }
3693 else
3694#endif
3695 {
3696 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3697 shape_table[SHAPE_IDX_VE].percentage =
3698 shape_table[SHAPE_IDX_V].percentage;
3699 shape_table[SHAPE_IDX_VE].blinkwait =
3700 shape_table[SHAPE_IDX_V].blinkwait;
3701 shape_table[SHAPE_IDX_VE].blinkon =
3702 shape_table[SHAPE_IDX_V].blinkon;
3703 shape_table[SHAPE_IDX_VE].blinkoff =
3704 shape_table[SHAPE_IDX_V].blinkoff;
3705 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3706 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3707 }
3708 }
3709
3710 return NULL;
3711}
3712
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003713# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3714 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715/*
3716 * Return the index into shape_table[] for the current mode.
3717 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3718 */
3719 int
3720get_shape_idx(mouse)
3721 int mouse;
3722{
3723#ifdef FEAT_MOUSESHAPE
3724 if (mouse && (State == HITRETURN || State == ASKMORE))
3725 {
3726# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003727 int x, y;
3728 gui_mch_getmouse(&x, &y);
3729 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return SHAPE_IDX_MOREL;
3731# endif
3732 return SHAPE_IDX_MORE;
3733 }
3734 if (mouse && drag_status_line)
3735 return SHAPE_IDX_SDRAG;
3736# ifdef FEAT_VERTSPLIT
3737 if (mouse && drag_sep_line)
3738 return SHAPE_IDX_VDRAG;
3739# endif
3740#endif
3741 if (!mouse && State == SHOWMATCH)
3742 return SHAPE_IDX_SM;
3743#ifdef FEAT_VREPLACE
3744 if (State & VREPLACE_FLAG)
3745 return SHAPE_IDX_R;
3746#endif
3747 if (State & REPLACE_FLAG)
3748 return SHAPE_IDX_R;
3749 if (State & INSERT)
3750 return SHAPE_IDX_I;
3751 if (State & CMDLINE)
3752 {
3753 if (cmdline_at_end())
3754 return SHAPE_IDX_C;
3755 if (cmdline_overstrike())
3756 return SHAPE_IDX_CR;
3757 return SHAPE_IDX_CI;
3758 }
3759 if (finish_op)
3760 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (VIsual_active)
3762 {
3763 if (*p_sel == 'e')
3764 return SHAPE_IDX_VE;
3765 else
3766 return SHAPE_IDX_V;
3767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return SHAPE_IDX_N;
3769}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3773static int old_mouse_shape = 0;
3774
3775/*
3776 * Set the mouse shape:
3777 * If "shape" is -1, use shape depending on the current mode,
3778 * depending on the current state.
3779 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3780 * when the mouse moves off the status or command line).
3781 */
3782 void
3783update_mouseshape(shape_idx)
3784 int shape_idx;
3785{
3786 int new_mouse_shape;
3787
3788 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003789 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 return;
3791
3792 /* Postpone the updating when more is to come. Speeds up executing of
3793 * mappings. */
3794 if (shape_idx == -1 && char_avail())
3795 {
3796 postponed_mouseshape = TRUE;
3797 return;
3798 }
3799
Bram Moolenaar14716812006-05-04 21:54:08 +00003800 /* When ignoring the mouse don't change shape on the statusline. */
3801 if (*p_mouse == NUL
3802 && (shape_idx == SHAPE_IDX_CLINE
3803 || shape_idx == SHAPE_IDX_STATUS
3804 || shape_idx == SHAPE_IDX_VSEP))
3805 shape_idx = -2;
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (shape_idx == -2
3808 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3809 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3810 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3811 return;
3812 if (shape_idx < 0)
3813 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3814 else
3815 new_mouse_shape = shape_table[shape_idx].mshape;
3816 if (new_mouse_shape != old_mouse_shape)
3817 {
3818 mch_set_mouse_shape(new_mouse_shape);
3819 old_mouse_shape = new_mouse_shape;
3820 }
3821 postponed_mouseshape = FALSE;
3822}
3823# endif
3824
3825#endif /* CURSOR_SHAPE */
3826
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828/* TODO: make some #ifdef for this */
3829/*--------[ file searching ]-------------------------------------------------*/
3830/*
3831 * File searching functions for 'path', 'tags' and 'cdpath' options.
3832 * External visible functions:
3833 * vim_findfile_init() creates/initialises the search context
3834 * vim_findfile_free_visited() free list of visited files/dirs of search
3835 * context
3836 * vim_findfile() find a file in the search context
3837 * vim_findfile_cleanup() cleanup/free search context created by
3838 * vim_findfile_init()
3839 *
3840 * All static functions and variables start with 'ff_'
3841 *
3842 * In general it works like this:
3843 * First you create yourself a search context by calling vim_findfile_init().
3844 * It is possible to give a search context from a previous call to
3845 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3846 * until you are satisfied with the result or it returns NULL. On every call it
3847 * returns the next file which matches the conditions given to
3848 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3849 *
3850 * It is possible to call vim_findfile_init() again to reinitialise your search
3851 * with some new parameters. Don't forget to pass your old search context to
3852 * it, so it can reuse it and especially reuse the list of already visited
3853 * directories. If you want to delete the list of already visited directories
3854 * simply call vim_findfile_free_visited().
3855 *
3856 * When you are done call vim_findfile_cleanup() to free the search context.
3857 *
3858 * The function vim_findfile_init() has a long comment, which describes the
3859 * needed parameters.
3860 *
3861 *
3862 *
3863 * ATTENTION:
3864 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003865 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * thread-safe!!!!!
3867 *
3868 * To minimize parameter passing (or because I'm to lazy), only the
3869 * external visible functions get a search context as a parameter. This is
3870 * then assigned to a static global, which is used throughout the local
3871 * functions.
3872 */
3873
3874/*
3875 * type for the directory search stack
3876 */
3877typedef struct ff_stack
3878{
3879 struct ff_stack *ffs_prev;
3880
3881 /* the fix part (no wildcards) and the part containing the wildcards
3882 * of the search path
3883 */
3884 char_u *ffs_fix_path;
3885#ifdef FEAT_PATH_EXTRA
3886 char_u *ffs_wc_path;
3887#endif
3888
3889 /* files/dirs found in the above directory, matched by the first wildcard
3890 * of wc_part
3891 */
3892 char_u **ffs_filearray;
3893 int ffs_filearray_size;
3894 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3895
3896 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003897 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 int ffs_stage;
3901
3902 /* How deep are we in the directory tree?
3903 * Counts backward from value of level parameter to vim_findfile_init
3904 */
3905 int ffs_level;
3906
3907 /* Did we already expand '**' to an empty string? */
3908 int ffs_star_star_empty;
3909} ff_stack_T;
3910
3911/*
3912 * type for already visited directories or files.
3913 */
3914typedef struct ff_visited
3915{
3916 struct ff_visited *ffv_next;
3917
3918#ifdef FEAT_PATH_EXTRA
3919 /* Visited directories are different if the wildcard string are
3920 * different. So we have to save it.
3921 */
3922 char_u *ffv_wc_path;
3923#endif
3924 /* for unix use inode etc for comparison (needed because of links), else
3925 * use filename.
3926 */
3927#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003928 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3929 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 ino_t ffv_ino; /* inode number */
3931#endif
3932 /* The memory for this struct is allocated according to the length of
3933 * ffv_fname.
3934 */
3935 char_u ffv_fname[1]; /* actually longer */
3936} ff_visited_T;
3937
3938/*
3939 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003940 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3942 * So we have to do 3 searches:
3943 * 1) search from the current files directory downward for the file "tags"
3944 * 2) search from the current files directory downward for the file "TAGS"
3945 * 3) search from Vims current directory downwards for the file "tags"
3946 * As you can see, the first and the third search are for the same file, so for
3947 * the third search we can use the visited list of the first search. For the
3948 * second search we must start from a empty visited list.
3949 * The struct ff_visited_list_hdr is used to manage a linked list of already
3950 * visited lists.
3951 */
3952typedef struct ff_visited_list_hdr
3953{
3954 struct ff_visited_list_hdr *ffvl_next;
3955
3956 /* the filename the attached visited list is for */
3957 char_u *ffvl_filename;
3958
3959 ff_visited_T *ffvl_visited_list;
3960
3961} ff_visited_list_hdr_T;
3962
3963
3964/*
3965 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003966 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
3968#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970/*
3971 * The search context:
3972 * ffsc_stack_ptr: the stack for the dirs to search
3973 * ffsc_visited_list: the currently active visited list
3974 * ffsc_dir_visited_list: the currently active visited list for search dirs
3975 * ffsc_visited_lists_list: the list of all visited lists
3976 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3977 * ffsc_file_to_search: the file to search for
3978 * ffsc_start_dir: the starting directory, if search path was relative
3979 * ffsc_fix_path: the fix part of the given path (without wildcards)
3980 * Needed for upward search.
3981 * ffsc_wc_path: the part of the given path containing wildcards
3982 * ffsc_level: how many levels of dirs to search downwards
3983 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003984 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003985 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 */
3987typedef struct ff_search_ctx_T
3988{
3989 ff_stack_T *ffsc_stack_ptr;
3990 ff_visited_list_hdr_T *ffsc_visited_list;
3991 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3992 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3993 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3994 char_u *ffsc_file_to_search;
3995 char_u *ffsc_start_dir;
3996 char_u *ffsc_fix_path;
3997#ifdef FEAT_PATH_EXTRA
3998 char_u *ffsc_wc_path;
3999 int ffsc_level;
4000 char_u **ffsc_stopdirs_v;
4001#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004002 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004003 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004004} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006/* locally needed functions */
4007#ifdef FEAT_PATH_EXTRA
4008static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *));
4009#else
4010static int ff_check_visited __ARGS((ff_visited_T **, char_u *));
4011#endif
4012static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp));
4013static void ff_free_visited_list __ARGS((ff_visited_T *vl));
4014static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp));
4015#ifdef FEAT_PATH_EXTRA
4016static int ff_wc_equal __ARGS((char_u *s1, char_u *s2));
4017#endif
4018
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004019static void ff_push __ARGS((ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr));
4020static ff_stack_T *ff_pop __ARGS((ff_search_ctx_T *search_ctx));
4021static void ff_clear __ARGS((ff_search_ctx_T *search_ctx));
4022static void ff_free_stack_element __ARGS((ff_stack_T *stack_ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#ifdef FEAT_PATH_EXTRA
4024static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int));
4025#else
4026static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int));
4027#endif
4028#ifdef FEAT_PATH_EXTRA
4029static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **));
4030#endif
4031
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004032static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034#if 0
4035/*
4036 * if someone likes findfirst/findnext, here are the functions
4037 * NOT TESTED!!
4038 */
4039
4040static void *ff_fn_search_context = NULL;
4041
4042 char_u *
4043vim_findfirst(path, filename, level)
4044 char_u *path;
4045 char_u *filename;
4046 int level;
4047{
4048 ff_fn_search_context =
4049 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4050 ff_fn_search_context, rel_fname);
4051 if (NULL == ff_fn_search_context)
4052 return NULL;
4053 else
4054 return vim_findnext()
4055}
4056
4057 char_u *
4058vim_findnext()
4059{
4060 char_u *ret = vim_findfile(ff_fn_search_context);
4061
4062 if (NULL == ret)
4063 {
4064 vim_findfile_cleanup(ff_fn_search_context);
4065 ff_fn_search_context = NULL;
4066 }
4067 return ret;
4068}
4069#endif
4070
4071/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004072 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004074 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 *
4076 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4077 * with the search context.
4078 *
4079 * Find the file 'filename' in the directory 'path'.
4080 * The parameter 'path' may contain wildcards. If so only search 'level'
4081 * directories deep. The parameter 'level' is the absolute maximum and is
4082 * not related to restricts given to the '**' wildcard. If 'level' is 100
4083 * and you use '**200' vim_findfile() will stop after 100 levels.
4084 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004085 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4086 * escape special characters.
4087 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4089 * restarted on the next higher directory level. This is repeated until the
4090 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4091 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4092 *
4093 * If the 'path' is relative, the starting dir for the search is either VIM's
4094 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004095 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 * the first wildcard.
4097 *
4098 * Upward search is only done on the starting dir.
4099 *
4100 * If 'free_visited' is TRUE the list of already visited files/directories is
4101 * cleared. Set this to FALSE if you just want to search from another
4102 * directory, but want to be sure that no directory from a previous search is
4103 * searched again. This is useful if you search for a file at different places.
4104 * The list of visited files/dirs can also be cleared with the function
4105 * vim_findfile_free_visited().
4106 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004107 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4108 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 *
4110 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004111 * passed in the parameter "search_ctx_arg". This context is reused and
4112 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004114 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4115 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004117 * If you don't have a search context from a previous call "search_ctx_arg"
4118 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * This function silently ignores a few errors, vim_findfile() will have
4121 * limited functionality then.
4122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 void *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004124vim_findfile_init(path, filename, stopdirs, level, free_visited, find_what,
4125 search_ctx_arg, tagfile, rel_fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 char_u *path;
4127 char_u *filename;
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00004128 char_u *stopdirs UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 int level;
4130 int free_visited;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004131 int find_what;
4132 void *search_ctx_arg;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004133 int tagfile; /* expanding names of tags files */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 char_u *rel_fname; /* file name to use for "." */
4135{
4136#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004137 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004139 ff_stack_T *sptr;
4140 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /* If a search context is given by the caller, reuse it, else allocate a
4143 * new one.
4144 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004145 if (search_ctx_arg != NULL)
4146 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 else
4148 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004149 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4150 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004152 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004154 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004155 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004158 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 /* clear visited list if wanted */
4161 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004162 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 else
4164 {
4165 /* Reuse old visited lists. Get the visited list for the given
4166 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004167 * one. */
4168 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4169 &search_ctx->ffsc_visited_lists_list);
4170 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004172 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4173 &search_ctx->ffsc_dir_visited_lists_list);
4174 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 goto error_return;
4176 }
4177
4178 if (ff_expand_buffer == NULL)
4179 {
4180 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4181 if (ff_expand_buffer == NULL)
4182 goto error_return;
4183 }
4184
4185 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004186 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (path[0] == '.'
4188 && (vim_ispathsep(path[1]) || path[1] == NUL)
4189 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4190 && rel_fname != NULL)
4191 {
4192 int len = (int)(gettail(rel_fname) - rel_fname);
4193
4194 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4195 {
4196 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004197 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004198 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004201 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4202 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 goto error_return;
4204 if (*++path != NUL)
4205 ++path;
4206 }
4207 else if (*path == NUL || !vim_isAbsName(path))
4208 {
4209#ifdef BACKSLASH_IN_FILENAME
4210 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4211 if (*path != NUL && path[1] == ':')
4212 {
4213 char_u drive[3];
4214
4215 drive[0] = path[0];
4216 drive[1] = ':';
4217 drive[2] = NUL;
4218 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4219 goto error_return;
4220 path += 2;
4221 }
4222 else
4223#endif
4224 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4225 goto error_return;
4226
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004227 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4228 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 goto error_return;
4230
4231#ifdef BACKSLASH_IN_FILENAME
4232 /* A path that starts with "/dir" is relative to the drive, not to the
4233 * directory (but not for "//machine/dir"). Only use the drive name. */
4234 if ((*path == '/' || *path == '\\')
4235 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004236 && search_ctx->ffsc_start_dir[1] == ':')
4237 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#endif
4239 }
4240
4241#ifdef FEAT_PATH_EXTRA
4242 /*
4243 * If stopdirs are given, split them into an array of pointers.
4244 * If this fails (mem allocation), there is no upward search at all or a
4245 * stop directory is not recognized -> continue silently.
4246 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004247 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 * is handled as unlimited upward search. See function
4249 * ff_path_in_stoplist() for details.
4250 */
4251 if (stopdirs != NULL)
4252 {
4253 char_u *walker = stopdirs;
4254 int dircount;
4255
4256 while (*walker == ';')
4257 walker++;
4258
4259 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004260 search_ctx->ffsc_stopdirs_v =
4261 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004263 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
4265 do
4266 {
4267 char_u *helper;
4268 void *ptr;
4269
4270 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004271 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 (dircount + 1) * sizeof(char_u *));
4273 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004274 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 else
4276 /* ignore, keep what we have and continue */
4277 break;
4278 walker = vim_strchr(walker, ';');
4279 if (walker)
4280 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004281 search_ctx->ffsc_stopdirs_v[dircount-1] =
4282 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 walker++;
4284 }
4285 else
4286 /* this might be "", which means ascent till top
4287 * of directory tree.
4288 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004289 search_ctx->ffsc_stopdirs_v[dircount-1] =
4290 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 dircount++;
4293
4294 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004295 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298#endif
4299
4300#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004301 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /* split into:
4304 * -fix path
4305 * -wildcard_stuff (might be NULL)
4306 */
4307 wc_part = vim_strchr(path, '*');
4308 if (wc_part != NULL)
4309 {
4310 int llevel;
4311 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004312 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004315 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 /*
4318 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004319 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4321 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4322 * For EBCDIC you get different character values.
4323 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004324 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 * string.
4326 */
4327 len = 0;
4328 while (*wc_part != NUL)
4329 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004330 if (len + 5 >= MAXPATHL)
4331 {
4332 EMSG(_(e_pathtoolong));
4333 break;
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (STRNCMP(wc_part, "**", 2) == 0)
4336 {
4337 ff_expand_buffer[len++] = *wc_part++;
4338 ff_expand_buffer[len++] = *wc_part++;
4339
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004340 llevel = strtol((char *)wc_part, &errpt, 10);
4341 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004343 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 /* restrict is 0 -> remove already added '**' */
4345 len -= 2;
4346 else
4347 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004348 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004349 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
4351 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4352 goto error_return;
4353 }
4354 }
4355 else
4356 ff_expand_buffer[len++] = *wc_part++;
4357 }
4358 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004359 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004361 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 goto error_return;
4363 }
4364 else
4365#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004366 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004368 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
4370 /* store the fix part as startdir.
4371 * This is needed if the parameter path is fully qualified.
4372 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004373 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004374 if (search_ctx->ffsc_start_dir == NULL)
4375 goto error_return;
4376 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378
4379 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004380 if (STRLEN(search_ctx->ffsc_start_dir)
4381 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4382 {
4383 EMSG(_(e_pathtoolong));
4384 goto error_return;
4385 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004386 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004388 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004389 int eb_len = (int)STRLEN(ff_expand_buffer);
4390 char_u *buf = alloc(eb_len
4391 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004392
4393 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004394 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004395 if (mch_isdir(buf))
4396 {
4397 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4398 add_pathsep(ff_expand_buffer);
4399 }
4400#ifdef FEAT_PATH_EXTRA
4401 else
4402 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004403 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004404 char_u *wc_path = NULL;
4405 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004406 int len = 0;
4407
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004408 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004409 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004410 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004411 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4412 add_pathsep(ff_expand_buffer);
4413 }
4414 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004415 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004416
4417 if (search_ctx->ffsc_wc_path != NULL)
4418 {
4419 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004420 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004421 + STRLEN(search_ctx->ffsc_fix_path + len)
4422 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004423 if (temp == NULL || wc_path == NULL)
4424 {
4425 vim_free(buf);
4426 vim_free(temp);
4427 vim_free(wc_path);
4428 goto error_return;
4429 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004430
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004431 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4432 STRCAT(temp, search_ctx->ffsc_wc_path);
4433 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004434 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004435 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004436 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004437 }
4438#endif
4439 vim_free(buf);
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 sptr = ff_create_stack_element(ff_expand_buffer,
4443#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004444 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445#endif
4446 level, 0);
4447
4448 if (sptr == NULL)
4449 goto error_return;
4450
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004451 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004453 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4454 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 goto error_return;
4456
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004457 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
4459error_return:
4460 /*
4461 * We clear the search context now!
4462 * Even when the caller gave us a (perhaps valid) context we free it here,
4463 * as we might have already destroyed it.
4464 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004465 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 return NULL;
4467}
4468
4469#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4470/*
4471 * Get the stopdir string. Check that ';' is not escaped.
4472 */
4473 char_u *
4474vim_findfile_stopdir(buf)
4475 char_u *buf;
4476{
4477 char_u *r_ptr = buf;
4478
4479 while (*r_ptr != NUL && *r_ptr != ';')
4480 {
4481 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4482 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004483 /* Overwrite the escape char,
4484 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004485 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 r_ptr++;
4487 }
4488 r_ptr++;
4489 }
4490 if (*r_ptr == ';')
4491 {
4492 *r_ptr = 0;
4493 r_ptr++;
4494 }
4495 else if (*r_ptr == NUL)
4496 r_ptr = NULL;
4497 return r_ptr;
4498}
4499#endif
4500
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004501/*
4502 * Clean up the given search context. Can handle a NULL pointer.
4503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 void
4505vim_findfile_cleanup(ctx)
4506 void *ctx;
4507{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004508 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 return;
4510
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004512 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514}
4515
4516/*
4517 * Find a file in a search context.
4518 * The search context was created with vim_findfile_init() above.
4519 * Return a pointer to an allocated file name or NULL if nothing found.
4520 * To get all matching files call this function until you get NULL.
4521 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004522 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 *
4524 * The search algorithm is depth first. To change this replace the
4525 * stack with a list (don't forget to leave partly searched directories on the
4526 * top of the list).
4527 */
4528 char_u *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004529vim_findfile(search_ctx_arg)
4530 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
4532 char_u *file_path;
4533#ifdef FEAT_PATH_EXTRA
4534 char_u *rest_of_wildcards;
4535 char_u *path_end = NULL;
4536#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004537 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4539 int len;
4540#endif
4541 int i;
4542 char_u *p;
4543#ifdef FEAT_SEARCHPATH
4544 char_u *suf;
4545#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004546 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004548 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 return NULL;
4550
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004551 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 /*
4554 * filepath is used as buffer for various actions and as the storage to
4555 * return a found filename.
4556 */
4557 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4558 return NULL;
4559
4560#ifdef FEAT_PATH_EXTRA
4561 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004562 if (search_ctx->ffsc_start_dir != NULL)
4563 path_end = &search_ctx->ffsc_start_dir[
4564 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565#endif
4566
4567#ifdef FEAT_PATH_EXTRA
4568 /* upward search loop */
4569 for (;;)
4570 {
4571#endif
4572 /* downward search loop */
4573 for (;;)
4574 {
4575 /* check if user user wants to stop the search*/
4576 ui_breakcheck();
4577 if (got_int)
4578 break;
4579
4580 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004581 stackp = ff_pop(search_ctx);
4582 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 break;
4584
4585 /*
4586 * TODO: decide if we leave this test in
4587 *
4588 * GOOD: don't search a directory(-tree) twice.
4589 * BAD: - check linked list for every new directory entered.
4590 * - check for double files also done below
4591 *
4592 * Here we check if we already searched this directory.
4593 * We already searched a directory if:
4594 * 1) The directory is the same.
4595 * 2) We would use the same wildcard string.
4596 *
4597 * Good if you have links on same directory via several ways
4598 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4599 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4600 *
4601 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004602 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004604 if (stackp->ffs_filearray == NULL
4605 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004607 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004609 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#endif
4611 ) == FAIL)
4612 {
4613#ifdef FF_VERBOSE
4614 if (p_verbose >= 5)
4615 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004616 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004618 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 /* don't overwrite this either */
4620 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004621 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004624 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 continue;
4626 }
4627#ifdef FF_VERBOSE
4628 else if (p_verbose >= 5)
4629 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004630 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004631 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004632 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 /* don't overwrite this either */
4634 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004635 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637#endif
4638
4639 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004640 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004642 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 continue;
4644 }
4645
4646 file_path[0] = NUL;
4647
4648 /*
4649 * If no filearray till now expand wildcards
4650 * The function expand_wildcards() can handle an array of paths
4651 * and all possible expands are returned in one array. We use this
4652 * to handle the expansion of '**' into an empty string.
4653 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004654 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
4656 char_u *dirptrs[2];
4657
4658 /* we use filepath to build the path expand_wildcards() should
4659 * expand.
4660 */
4661 dirptrs[0] = file_path;
4662 dirptrs[1] = NULL;
4663
4664 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004665 if (!vim_isAbsName(stackp->ffs_fix_path)
4666 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004668 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 add_pathsep(file_path);
4670 }
4671
4672 /* append the fix part of the search path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004673 STRCAT(file_path, stackp->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 add_pathsep(file_path);
4675
4676#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004677 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (*rest_of_wildcards != NUL)
4679 {
4680 len = (int)STRLEN(file_path);
4681 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4682 {
4683 /* pointer to the restrict byte
4684 * The restrict byte is not a character!
4685 */
4686 p = rest_of_wildcards + 2;
4687
4688 if (*p > 0)
4689 {
4690 (*p)--;
4691 file_path[len++] = '*';
4692 }
4693
4694 if (*p == 0)
4695 {
4696 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004697 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
4699 else
4700 rest_of_wildcards += 3;
4701
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004702 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004705 stackp->ffs_star_star_empty = 1;
4706 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708 }
4709
4710 /*
4711 * Here we copy until the next path separator or the end of
4712 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004713 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 * pushing every directory returned from expand_wildcards()
4715 * on the stack again for further search.
4716 */
4717 while (*rest_of_wildcards
4718 && !vim_ispathsep(*rest_of_wildcards))
4719 file_path[len++] = *rest_of_wildcards++;
4720
4721 file_path[len] = NUL;
4722 if (vim_ispathsep(*rest_of_wildcards))
4723 rest_of_wildcards++;
4724 }
4725#endif
4726
4727 /*
4728 * Expand wildcards like "*" and "$VAR".
4729 * If the path is a URL don't try this.
4730 */
4731 if (path_with_url(dirptrs[0]))
4732 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004733 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004735 if (stackp->ffs_filearray != NULL
4736 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004738 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004740 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
4742 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004743 /* Add EW_NOTWILD because the expanded path may contain
4744 * wildcard characters that are to be taken literally.
4745 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004747 &stackp->ffs_filearray_size,
4748 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004749 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004751 stackp->ffs_filearray_cur = 0;
4752 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754#ifdef FEAT_PATH_EXTRA
4755 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004756 rest_of_wildcards = &stackp->ffs_wc_path[
4757 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004760 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762 /* this is the first time we work on this directory */
4763#ifdef FEAT_PATH_EXTRA
4764 if (*rest_of_wildcards == NUL)
4765#endif
4766 {
4767 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004768 * We don't have further wildcards to expand, so we have to
4769 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004771 for (i = stackp->ffs_filearray_cur;
4772 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004774 if (!path_with_url(stackp->ffs_filearray[i])
4775 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 continue; /* not a directory */
4777
Bram Moolenaar21160b92009-11-11 15:56:10 +00004778 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * below */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004780 STRCPY(file_path, stackp->ffs_filearray[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004782 STRCAT(file_path, search_ctx->ffsc_file_to_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 /*
4785 * Try without extra suffix and then with suffixes
4786 * from 'suffixesadd'.
4787 */
4788#ifdef FEAT_SEARCHPATH
4789 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004790 if (search_ctx->ffsc_tagfile)
4791 suf = (char_u *)"";
4792 else
4793 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 for (;;)
4795#endif
4796 {
4797 /* if file exists and we didn't already find it */
4798 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004799 || (mch_getperm(file_path) >= 0
4800 && (search_ctx->ffsc_find_what
4801 == FINDFILE_BOTH
4802 || ((search_ctx->ffsc_find_what
4803 == FINDFILE_DIR)
4804 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#ifndef FF_VERBOSE
4806 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004807 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 file_path
4809#ifdef FEAT_PATH_EXTRA
4810 , (char_u *)""
4811#endif
4812 ) == OK)
4813#endif
4814 )
4815 {
4816#ifdef FF_VERBOSE
4817 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004818 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 file_path
4820#ifdef FEAT_PATH_EXTRA
4821 , (char_u *)""
4822#endif
4823 ) == FAIL)
4824 {
4825 if (p_verbose >= 5)
4826 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004827 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004828 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 file_path);
4830 /* don't overwrite this either */
4831 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004832 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 continue;
4835 }
4836#endif
4837
4838 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004839 stackp->ffs_filearray_cur = i + 1;
4840 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004842 if (!path_with_url(file_path))
4843 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4845 == OK)
4846 {
4847 p = shorten_fname(file_path,
4848 ff_expand_buffer);
4849 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004850 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852#ifdef FF_VERBOSE
4853 if (p_verbose >= 5)
4854 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004855 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004856 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 /* don't overwrite this either */
4858 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004859 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861#endif
4862 return file_path;
4863 }
4864
4865#ifdef FEAT_SEARCHPATH
4866 /* Not found or found already, try next suffix. */
4867 if (*suf == NUL)
4868 break;
4869 copy_option_part(&suf, file_path + len,
4870 MAXPATHL - len, ",");
4871#endif
4872 }
4873 }
4874 }
4875#ifdef FEAT_PATH_EXTRA
4876 else
4877 {
4878 /*
4879 * still wildcards left, push the directories for further
4880 * search
4881 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004882 for (i = stackp->ffs_filearray_cur;
4883 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004885 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 continue; /* not a directory */
4887
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004888 ff_push(search_ctx,
4889 ff_create_stack_element(
4890 stackp->ffs_filearray[i],
4891 rest_of_wildcards,
4892 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 }
4895#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004896 stackp->ffs_filearray_cur = 0;
4897 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899
4900#ifdef FEAT_PATH_EXTRA
4901 /*
4902 * if wildcards contains '**' we have to descent till we reach the
4903 * leaves of the directory tree.
4904 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004905 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004907 for (i = stackp->ffs_filearray_cur;
4908 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004910 if (fnamecmp(stackp->ffs_filearray[i],
4911 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004913 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004915 ff_push(search_ctx,
4916 ff_create_stack_element(stackp->ffs_filearray[i],
4917 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 }
4920#endif
4921
4922 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004923 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 }
4926
4927#ifdef FEAT_PATH_EXTRA
4928 /* If we reached this, we didn't find anything downwards.
4929 * Let's check if we should do an upward search.
4930 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004931 if (search_ctx->ffsc_start_dir
4932 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 ff_stack_T *sptr;
4935
4936 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004937 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4938 (int)(path_end - search_ctx->ffsc_start_dir),
4939 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 break;
4941
4942 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004943 while (path_end > search_ctx->ffsc_start_dir
4944 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004946 while (path_end > search_ctx->ffsc_start_dir
4947 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 path_end--;
4949 *path_end = 0;
4950 path_end--;
4951
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004952 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 break;
4954
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004955 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004957 STRCAT(file_path, search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958
4959 /* create a new stack entry */
4960 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004961 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (sptr == NULL)
4963 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004964 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966 else
4967 break;
4968 }
4969#endif
4970
4971 vim_free(file_path);
4972 return NULL;
4973}
4974
4975/*
4976 * Free the list of lists of visited files and directories
4977 * Can handle it if the passed search_context is NULL;
4978 */
4979 void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004980vim_findfile_free_visited(search_ctx_arg)
4981 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004983 ff_search_ctx_T *search_ctx;
4984
4985 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 return;
4987
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004988 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
4989 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
4990 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991}
4992
4993 static void
4994vim_findfile_free_visited_list(list_headp)
4995 ff_visited_list_hdr_T **list_headp;
4996{
4997 ff_visited_list_hdr_T *vp;
4998
4999 while (*list_headp != NULL)
5000 {
5001 vp = (*list_headp)->ffvl_next;
5002 ff_free_visited_list((*list_headp)->ffvl_visited_list);
5003
5004 vim_free((*list_headp)->ffvl_filename);
5005 vim_free(*list_headp);
5006 *list_headp = vp;
5007 }
5008 *list_headp = NULL;
5009}
5010
5011 static void
5012ff_free_visited_list(vl)
5013 ff_visited_T *vl;
5014{
5015 ff_visited_T *vp;
5016
5017 while (vl != NULL)
5018 {
5019 vp = vl->ffv_next;
5020#ifdef FEAT_PATH_EXTRA
5021 vim_free(vl->ffv_wc_path);
5022#endif
5023 vim_free(vl);
5024 vl = vp;
5025 }
5026 vl = NULL;
5027}
5028
5029/*
5030 * Returns the already visited list for the given filename. If none is found it
5031 * allocates a new one.
5032 */
5033 static ff_visited_list_hdr_T*
5034ff_get_visited_list(filename, list_headp)
5035 char_u *filename;
5036 ff_visited_list_hdr_T **list_headp;
5037{
5038 ff_visited_list_hdr_T *retptr = NULL;
5039
5040 /* check if a visited list for the given filename exists */
5041 if (*list_headp != NULL)
5042 {
5043 retptr = *list_headp;
5044 while (retptr != NULL)
5045 {
5046 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5047 {
5048#ifdef FF_VERBOSE
5049 if (p_verbose >= 5)
5050 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005051 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005052 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 filename);
5054 /* don't overwrite this either */
5055 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005056 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058#endif
5059 return retptr;
5060 }
5061 retptr = retptr->ffvl_next;
5062 }
5063 }
5064
5065#ifdef FF_VERBOSE
5066 if (p_verbose >= 5)
5067 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005068 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005069 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 /* don't overwrite this either */
5071 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005072 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074#endif
5075
5076 /*
5077 * if we reach this we didn't find a list and we have to allocate new list
5078 */
5079 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5080 if (retptr == NULL)
5081 return NULL;
5082
5083 retptr->ffvl_visited_list = NULL;
5084 retptr->ffvl_filename = vim_strsave(filename);
5085 if (retptr->ffvl_filename == NULL)
5086 {
5087 vim_free(retptr);
5088 return NULL;
5089 }
5090 retptr->ffvl_next = *list_headp;
5091 *list_headp = retptr;
5092
5093 return retptr;
5094}
5095
5096#ifdef FEAT_PATH_EXTRA
5097/*
5098 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5099 * They are equal if:
5100 * - both paths are NULL
5101 * - they have the same length
5102 * - char by char comparison is OK
5103 * - the only differences are in the counters behind a '**', so
5104 * '**\20' is equal to '**\24'
5105 */
5106 static int
5107ff_wc_equal(s1, s2)
5108 char_u *s1;
5109 char_u *s2;
5110{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005111 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005112 int c1 = NUL;
5113 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005114 int prev1 = NUL;
5115 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116
5117 if (s1 == s2)
5118 return TRUE;
5119
5120 if (s1 == NULL || s2 == NULL)
5121 return FALSE;
5122
Bram Moolenaard43f0952015-08-27 22:30:47 +02005123 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005125 c1 = PTR2CHAR(s1 + i);
5126 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005127
5128 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5129 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005130 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005131 prev2 = prev1;
5132 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005133
5134 i += MB_PTR2LEN(s1 + i);
5135 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005137 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138}
5139#endif
5140
5141/*
5142 * maintains the list of already visited files and dirs
5143 * returns FAIL if the given file/dir is already in the list
5144 * returns OK if it is newly added
5145 *
5146 * TODO: What to do on memory allocation problems?
5147 * -> return TRUE - Better the file is found several times instead of
5148 * never.
5149 */
5150 static int
5151ff_check_visited(visited_list, fname
5152#ifdef FEAT_PATH_EXTRA
5153 , wc_path
5154#endif
5155 )
5156 ff_visited_T **visited_list;
5157 char_u *fname;
5158#ifdef FEAT_PATH_EXTRA
5159 char_u *wc_path;
5160#endif
5161{
5162 ff_visited_T *vp;
5163#ifdef UNIX
5164 struct stat st;
5165 int url = FALSE;
5166#endif
5167
5168 /* For an URL we only compare the name, otherwise we compare the
5169 * device/inode (unix) or the full path name (not Unix). */
5170 if (path_with_url(fname))
5171 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005172 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#ifdef UNIX
5174 url = TRUE;
5175#endif
5176 }
5177 else
5178 {
5179 ff_expand_buffer[0] = NUL;
5180#ifdef UNIX
5181 if (mch_stat((char *)fname, &st) < 0)
5182#else
5183 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5184#endif
5185 return FAIL;
5186 }
5187
5188 /* check against list of already visited files */
5189 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5190 {
5191 if (
5192#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005193 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5194 && vp->ffv_ino == st.st_ino)
5195 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#endif
5197 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5198 )
5199 {
5200#ifdef FEAT_PATH_EXTRA
5201 /* are the wildcard parts equal */
5202 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5203#endif
5204 /* already visited */
5205 return FAIL;
5206 }
5207 }
5208
5209 /*
5210 * New file/dir. Add it to the list of visited files/dirs.
5211 */
5212 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5213 + STRLEN(ff_expand_buffer)));
5214
5215 if (vp != NULL)
5216 {
5217#ifdef UNIX
5218 if (!url)
5219 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005220 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 vp->ffv_ino = st.st_ino;
5222 vp->ffv_dev = st.st_dev;
5223 vp->ffv_fname[0] = NUL;
5224 }
5225 else
5226 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005227 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#endif
5229 STRCPY(vp->ffv_fname, ff_expand_buffer);
5230#ifdef UNIX
5231 }
5232#endif
5233#ifdef FEAT_PATH_EXTRA
5234 if (wc_path != NULL)
5235 vp->ffv_wc_path = vim_strsave(wc_path);
5236 else
5237 vp->ffv_wc_path = NULL;
5238#endif
5239
5240 vp->ffv_next = *visited_list;
5241 *visited_list = vp;
5242 }
5243
5244 return OK;
5245}
5246
5247/*
5248 * create stack element from given path pieces
5249 */
5250 static ff_stack_T *
5251ff_create_stack_element(fix_part,
5252#ifdef FEAT_PATH_EXTRA
5253 wc_part,
5254#endif
5255 level, star_star_empty)
5256 char_u *fix_part;
5257#ifdef FEAT_PATH_EXTRA
5258 char_u *wc_part;
5259#endif
5260 int level;
5261 int star_star_empty;
5262{
5263 ff_stack_T *new;
5264
5265 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5266 if (new == NULL)
5267 return NULL;
5268
5269 new->ffs_prev = NULL;
5270 new->ffs_filearray = NULL;
5271 new->ffs_filearray_size = 0;
5272 new->ffs_filearray_cur = 0;
5273 new->ffs_stage = 0;
5274 new->ffs_level = level;
5275 new->ffs_star_star_empty = star_star_empty;;
5276
5277 /* the following saves NULL pointer checks in vim_findfile */
5278 if (fix_part == NULL)
5279 fix_part = (char_u *)"";
5280 new->ffs_fix_path = vim_strsave(fix_part);
5281
5282#ifdef FEAT_PATH_EXTRA
5283 if (wc_part == NULL)
5284 wc_part = (char_u *)"";
5285 new->ffs_wc_path = vim_strsave(wc_part);
5286#endif
5287
5288 if (new->ffs_fix_path == NULL
5289#ifdef FEAT_PATH_EXTRA
5290 || new->ffs_wc_path == NULL
5291#endif
5292 )
5293 {
5294 ff_free_stack_element(new);
5295 new = NULL;
5296 }
5297
5298 return new;
5299}
5300
5301/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005302 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 */
5304 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005305ff_push(search_ctx, stack_ptr)
5306 ff_search_ctx_T *search_ctx;
5307 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005310 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005311 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005313 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5314 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316}
5317
5318/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005319 * Pop a dir from the directory stack.
5320 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 */
5322 static ff_stack_T *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005323ff_pop(search_ctx)
5324 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 ff_stack_T *sptr;
5327
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005328 sptr = search_ctx->ffsc_stack_ptr;
5329 if (search_ctx->ffsc_stack_ptr != NULL)
5330 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332 return sptr;
5333}
5334
5335/*
5336 * free the given stack element
5337 */
5338 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005339ff_free_stack_element(stack_ptr)
5340 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005343 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005345 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#endif
5347
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005348 if (stack_ptr->ffs_filearray != NULL)
5349 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005351 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352}
5353
5354/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005355 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 */
5357 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005358ff_clear(search_ctx)
5359 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 ff_stack_T *sptr;
5362
5363 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005364 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 ff_free_stack_element(sptr);
5366
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005367 vim_free(search_ctx->ffsc_file_to_search);
5368 vim_free(search_ctx->ffsc_start_dir);
5369 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005371 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#endif
5373
5374#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005375 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 int i = 0;
5378
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005379 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005381 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 i++;
5383 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005384 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005386 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#endif
5388
5389 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005390 search_ctx->ffsc_file_to_search = NULL;
5391 search_ctx->ffsc_start_dir = NULL;
5392 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005394 search_ctx->ffsc_wc_path = NULL;
5395 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#endif
5397}
5398
5399#ifdef FEAT_PATH_EXTRA
5400/*
5401 * check if the given path is in the stopdirs
5402 * returns TRUE if yes else FALSE
5403 */
5404 static int
5405ff_path_in_stoplist(path, path_len, stopdirs_v)
5406 char_u *path;
5407 int path_len;
5408 char_u **stopdirs_v;
5409{
5410 int i = 0;
5411
5412 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005413 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 path_len--;
5415
5416 /* if no path consider it as match */
5417 if (path_len == 0)
5418 return TRUE;
5419
5420 for (i = 0; stopdirs_v[i] != NULL; i++)
5421 {
5422 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5423 {
5424 /* match for parent directory. So '/home' also matches
5425 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5426 * '/home/r' would also match '/home/rks'
5427 */
5428 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005429 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 return TRUE;
5431 }
5432 else
5433 {
5434 if (fnamecmp(stopdirs_v[i], path) == 0)
5435 return TRUE;
5436 }
5437 }
5438 return FALSE;
5439}
5440#endif
5441
5442#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5443/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005444 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 *
5446 * On the first call set the parameter 'first' to TRUE to initialize
5447 * the search. For repeating calls to FALSE.
5448 *
5449 * Repeating calls will return other files called 'ptr[len]' from the path.
5450 *
5451 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5452 * don't need valid values.
5453 *
5454 * If nothing found on the first call the option FNAME_MESS will issue the
5455 * message:
5456 * 'Can't find file "<file>" in path'
5457 * On repeating calls:
5458 * 'No more file "<file>" found in path'
5459 *
5460 * options:
5461 * FNAME_MESS give error message when not found
5462 *
5463 * Uses NameBuff[]!
5464 *
5465 * Returns an allocated string for the file name. NULL for error.
5466 *
5467 */
5468 char_u *
5469find_file_in_path(ptr, len, options, first, rel_fname)
5470 char_u *ptr; /* file name */
5471 int len; /* length of file name */
5472 int options;
5473 int first; /* use count'th matching file name */
5474 char_u *rel_fname; /* file name searching relative to */
5475{
5476 return find_file_in_path_option(ptr, len, options, first,
5477 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005478 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479}
5480
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005481static char_u *ff_file_to_find = NULL;
5482static void *fdip_search_ctx = NULL;
5483
5484#if defined(EXITFREE)
5485 static void
5486free_findfile()
5487{
5488 vim_free(ff_file_to_find);
5489 vim_findfile_cleanup(fdip_search_ctx);
5490}
5491#endif
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493/*
5494 * Find the directory name "ptr[len]" in the path.
5495 *
5496 * options:
5497 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005498 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 *
5500 * Uses NameBuff[]!
5501 *
5502 * Returns an allocated string for the file name. NULL for error.
5503 */
5504 char_u *
5505find_directory_in_path(ptr, len, options, rel_fname)
5506 char_u *ptr; /* file name */
5507 int len; /* length of file name */
5508 int options;
5509 char_u *rel_fname; /* file name searching relative to */
5510{
5511 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005512 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513}
5514
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005515 char_u *
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005516find_file_in_path_option(ptr, len, options, first, path_option,
5517 find_what, rel_fname, suffixes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u *ptr; /* file name */
5519 int len; /* length of file name */
5520 int options;
5521 int first; /* use count'th matching file name */
5522 char_u *path_option; /* p_path or p_cdpath */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005523 int find_what; /* FINDFILE_FILE, _DIR or _BOTH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 char_u *rel_fname; /* file name we are looking relative to. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005525 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 static int did_findfile_init = FALSE;
5529 char_u save_char;
5530 char_u *file_name = NULL;
5531 char_u *buf = NULL;
5532 int rel_to_curdir;
5533#ifdef AMIGA
5534 struct Process *proc = (struct Process *)FindTask(0L);
5535 APTR save_winptr = proc->pr_WindowPtr;
5536
5537 /* Avoid a requester here for a volume that doesn't exist. */
5538 proc->pr_WindowPtr = (APTR)-1L;
5539#endif
5540
5541 if (first == TRUE)
5542 {
5543 /* copy file name into NameBuff, expanding environment variables */
5544 save_char = ptr[len];
5545 ptr[len] = NUL;
5546 expand_env(ptr, NameBuff, MAXPATHL);
5547 ptr[len] = save_char;
5548
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005549 vim_free(ff_file_to_find);
5550 ff_file_to_find = vim_strsave(NameBuff);
5551 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 {
5553 file_name = NULL;
5554 goto theend;
5555 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005556 if (options & FNAME_UNESC)
5557 {
5558 /* Change all "\ " to " ". */
5559 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5560 if (ptr[0] == '\\' && ptr[1] == ' ')
5561 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005565 rel_to_curdir = (ff_file_to_find[0] == '.'
5566 && (ff_file_to_find[1] == NUL
5567 || vim_ispathsep(ff_file_to_find[1])
5568 || (ff_file_to_find[1] == '.'
5569 && (ff_file_to_find[2] == NUL
5570 || vim_ispathsep(ff_file_to_find[2])))));
5571 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 /* "..", "../path", "." and "./path": don't use the path_option */
5573 || rel_to_curdir
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005574#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005576 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005577 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005578 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579#endif
5580#ifdef AMIGA
5581 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005582 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583#endif
5584 )
5585 {
5586 /*
5587 * Absolute path, no need to use "path_option".
5588 * If this is not a first call, return NULL. We already returned a
5589 * filename on the first call.
5590 */
5591 if (first == TRUE)
5592 {
5593 int l;
5594 int run;
5595
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005596 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005598 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 goto theend;
5600 }
5601
5602 /* When FNAME_REL flag given first use the directory of the file.
5603 * Otherwise or when this fails use the current directory. */
5604 for (run = 1; run <= 2; ++run)
5605 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005606 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if (run == 1
5608 && rel_to_curdir
5609 && (options & FNAME_REL)
5610 && rel_fname != NULL
5611 && STRLEN(rel_fname) + l < MAXPATHL)
5612 {
5613 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005614 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 l = (int)STRLEN(NameBuff);
5616 }
5617 else
5618 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005619 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 run = 2;
5621 }
5622
5623 /* When the file doesn't exist, try adding parts of
5624 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005625 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 for (;;)
5627 {
5628 if (
5629#ifdef DJGPP
5630 /* "C:" by itself will fail for mch_getperm(),
5631 * assume it's always valid. */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005632 (find_what != FINDFILE_FILE && NameBuff[0] != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 && NameBuff[1] == ':'
5634 && NameBuff[2] == NUL) ||
5635#endif
5636 (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005637 && (find_what == FINDFILE_BOTH
5638 || ((find_what == FINDFILE_DIR)
5639 == mch_isdir(NameBuff)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
5641 file_name = vim_strsave(NameBuff);
5642 goto theend;
5643 }
5644 if (*buf == NUL)
5645 break;
5646 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5647 }
5648 }
5649 }
5650 }
5651 else
5652 {
5653 /*
5654 * Loop over all paths in the 'path' or 'cdpath' option.
5655 * When "first" is set, first setup to the start of the option.
5656 * Otherwise continue to find the next match.
5657 */
5658 if (first == TRUE)
5659 {
5660 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005661 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 dir = path_option;
5663 did_findfile_init = FALSE;
5664 }
5665
5666 for (;;)
5667 {
5668 if (did_findfile_init)
5669 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005670 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (file_name != NULL)
5672 break;
5673
5674 did_findfile_init = FALSE;
5675 }
5676 else
5677 {
5678 char_u *r_ptr;
5679
5680 if (dir == NULL || *dir == NUL)
5681 {
5682 /* We searched all paths of the option, now we can
5683 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005684 vim_findfile_cleanup(fdip_search_ctx);
5685 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 break;
5687 }
5688
5689 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5690 break;
5691
5692 /* copy next path */
5693 buf[0] = 0;
5694 copy_option_part(&dir, buf, MAXPATHL, " ,");
5695
5696#ifdef FEAT_PATH_EXTRA
5697 /* get the stopdir string */
5698 r_ptr = vim_findfile_stopdir(buf);
5699#else
5700 r_ptr = NULL;
5701#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005702 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005703 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005704 fdip_search_ctx, FALSE, rel_fname);
5705 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 did_findfile_init = TRUE;
5707 vim_free(buf);
5708 }
5709 }
5710 }
5711 if (file_name == NULL && (options & FNAME_MESS))
5712 {
5713 if (first == TRUE)
5714 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005715 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005717 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 else
5719 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005720 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 else
5723 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005724 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005726 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 else
5728 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005729 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731 }
5732
5733theend:
5734#ifdef AMIGA
5735 proc->pr_WindowPtr = save_winptr;
5736#endif
5737 return file_name;
5738}
5739
5740#endif /* FEAT_SEARCHPATH */
5741
5742/*
5743 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5744 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5745 */
5746 int
5747vim_chdir(new_dir)
5748 char_u *new_dir;
5749{
5750#ifndef FEAT_SEARCHPATH
5751 return mch_chdir((char *)new_dir);
5752#else
5753 char_u *dir_name;
5754 int r;
5755
5756 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5757 FNAME_MESS, curbuf->b_ffname);
5758 if (dir_name == NULL)
5759 return -1;
5760 r = mch_chdir((char *)dir_name);
5761 vim_free(dir_name);
5762 return r;
5763#endif
5764}
5765
5766/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005767 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005769 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5770 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 * Returns OK or FAIL.
5772 */
5773 int
5774get_user_name(buf, len)
5775 char_u *buf;
5776 int len;
5777{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005778 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 if (mch_get_user_name(buf, len) == FAIL)
5781 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005782 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005785 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 return OK;
5787}
5788
5789#ifndef HAVE_QSORT
5790/*
5791 * Our own qsort(), for systems that don't have it.
5792 * It's simple and slow. From the K&R C book.
5793 */
5794 void
5795qsort(base, elm_count, elm_size, cmp)
5796 void *base;
5797 size_t elm_count;
5798 size_t elm_size;
5799 int (*cmp) __ARGS((const void *, const void *));
5800{
5801 char_u *buf;
5802 char_u *p1;
5803 char_u *p2;
5804 int i, j;
5805 int gap;
5806
5807 buf = alloc((unsigned)elm_size);
5808 if (buf == NULL)
5809 return;
5810
5811 for (gap = elm_count / 2; gap > 0; gap /= 2)
5812 for (i = gap; i < elm_count; ++i)
5813 for (j = i - gap; j >= 0; j -= gap)
5814 {
5815 /* Compare the elements. */
5816 p1 = (char_u *)base + j * elm_size;
5817 p2 = (char_u *)base + (j + gap) * elm_size;
5818 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5819 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005820 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 mch_memmove(buf, p1, elm_size);
5822 mch_memmove(p1, p2, elm_size);
5823 mch_memmove(p2, buf, elm_size);
5824 }
5825
5826 vim_free(buf);
5827}
5828#endif
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830/*
5831 * Sort an array of strings.
5832 */
5833static int
5834#ifdef __BORLANDC__
5835_RTLENTRYF
5836#endif
5837sort_compare __ARGS((const void *s1, const void *s2));
5838
5839 static int
5840#ifdef __BORLANDC__
5841_RTLENTRYF
5842#endif
5843sort_compare(s1, s2)
5844 const void *s1;
5845 const void *s2;
5846{
5847 return STRCMP(*(char **)s1, *(char **)s2);
5848}
5849
5850 void
5851sort_strings(files, count)
5852 char_u **files;
5853 int count;
5854{
5855 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858#if !defined(NO_EXPANDPATH) || defined(PROTO)
5859/*
5860 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005861 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 * Return value like strcmp(p, q), but consider path separators.
5863 */
5864 int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005865pathcmp(p, q, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 const char *p, *q;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005867 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005869 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005870 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005871 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005873 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005875 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005876 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005879 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005881 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 return 0;
5883 s = q;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005884 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 break;
5886 }
5887
5888 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005889 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 s = p;
5892 break;
5893 }
5894
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005895 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896#ifdef BACKSLASH_IN_FILENAME
5897 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005898 && !((c1 == '/' && c2 == '\\')
5899 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#endif
5901 )
5902 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005903 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005905 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005907 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5908 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005910
5911 i += MB_PTR2LEN((char_u *)p + i);
5912 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005914 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005915 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005917 c1 = PTR2CHAR((char_u *)s + i);
5918 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005920 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005921 && i > 0
5922 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005924 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005926 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005928 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 return 0; /* match with trailing slash */
5930 if (s == q)
5931 return -1; /* no match */
5932 return 1;
5933}
5934#endif
5935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936/*
5937 * The putenv() implementation below comes from the "screen" program.
5938 * Included with permission from Juergen Weigert.
5939 * See pty.c for the copyright notice.
5940 */
5941
5942/*
5943 * putenv -- put value into environment
5944 *
5945 * Usage: i = putenv (string)
5946 * int i;
5947 * char *string;
5948 *
5949 * where string is of the form <name>=<value>.
5950 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5951 *
5952 * Putenv may need to add a new name into the environment, or to
5953 * associate a value longer than the current value with a particular
5954 * name. So, to make life simpler, putenv() copies your entire
5955 * environment into the heap (i.e. malloc()) from the stack
5956 * (i.e. where it resides when your process is initiated) the first
5957 * time you call it.
5958 *
5959 * (history removed, not very interesting. See the "screen" sources.)
5960 */
5961
5962#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5963
5964#define EXTRASIZE 5 /* increment to add to env. size */
5965
5966static int envsize = -1; /* current size of environment */
5967#ifndef MACOS_CLASSIC
5968extern
5969#endif
5970 char **environ; /* the global which is your env. */
5971
5972static int findenv __ARGS((char *name)); /* look for a name in the env. */
5973static int newenv __ARGS((void)); /* copy env. from stack to heap */
5974static int moreenv __ARGS((void)); /* incr. size of env. */
5975
5976 int
5977putenv(string)
5978 const char *string;
5979{
5980 int i;
5981 char *p;
5982
5983 if (envsize < 0)
5984 { /* first time putenv called */
5985 if (newenv() < 0) /* copy env. to heap */
5986 return -1;
5987 }
5988
5989 i = findenv((char *)string); /* look for name in environment */
5990
5991 if (i < 0)
5992 { /* name must be added */
5993 for (i = 0; environ[i]; i++);
5994 if (i >= (envsize - 1))
5995 { /* need new slot */
5996 if (moreenv() < 0)
5997 return -1;
5998 }
5999 p = (char *)alloc((unsigned)(strlen(string) + 1));
6000 if (p == NULL) /* not enough core */
6001 return -1;
6002 environ[i + 1] = 0; /* new end of env. */
6003 }
6004 else
6005 { /* name already in env. */
6006 p = vim_realloc(environ[i], strlen(string) + 1);
6007 if (p == NULL)
6008 return -1;
6009 }
6010 sprintf(p, "%s", string); /* copy into env. */
6011 environ[i] = p;
6012
6013 return 0;
6014}
6015
6016 static int
6017findenv(name)
6018 char *name;
6019{
6020 char *namechar, *envchar;
6021 int i, found;
6022
6023 found = 0;
6024 for (i = 0; environ[i] && !found; i++)
6025 {
6026 envchar = environ[i];
6027 namechar = name;
6028 while (*namechar && *namechar != '=' && (*namechar == *envchar))
6029 {
6030 namechar++;
6031 envchar++;
6032 }
6033 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6034 }
6035 return found ? i - 1 : -1;
6036}
6037
6038 static int
6039newenv()
6040{
6041 char **env, *elem;
6042 int i, esize;
6043
6044#ifdef MACOS
6045 /* for Mac a new, empty environment is created */
6046 i = 0;
6047#else
6048 for (i = 0; environ[i]; i++)
6049 ;
6050#endif
6051 esize = i + EXTRASIZE + 1;
6052 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6053 if (env == NULL)
6054 return -1;
6055
6056#ifndef MACOS
6057 for (i = 0; environ[i]; i++)
6058 {
6059 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6060 if (elem == NULL)
6061 return -1;
6062 env[i] = elem;
6063 strcpy(elem, environ[i]);
6064 }
6065#endif
6066
6067 env[i] = 0;
6068 environ = env;
6069 envsize = esize;
6070 return 0;
6071}
6072
6073 static int
6074moreenv()
6075{
6076 int esize;
6077 char **env;
6078
6079 esize = envsize + EXTRASIZE;
6080 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6081 if (env == 0)
6082 return -1;
6083 environ = env;
6084 envsize = esize;
6085 return 0;
6086}
6087
6088# ifdef USE_VIMPTY_GETENV
6089 char_u *
6090vimpty_getenv(string)
6091 const char_u *string;
6092{
6093 int i;
6094 char_u *p;
6095
6096 if (envsize < 0)
6097 return NULL;
6098
6099 i = findenv((char *)string);
6100
6101 if (i < 0)
6102 return NULL;
6103
6104 p = vim_strchr((char_u *)environ[i], '=');
6105 return (p + 1);
6106}
6107# endif
6108
6109#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006110
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006111#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006112/*
6113 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6114 * rights to write into.
6115 */
6116 int
6117filewritable(fname)
6118 char_u *fname;
6119{
6120 int retval = 0;
6121#if defined(UNIX) || defined(VMS)
6122 int perm = 0;
6123#endif
6124
6125#if defined(UNIX) || defined(VMS)
6126 perm = mch_getperm(fname);
6127#endif
6128#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6129 if (
6130# ifdef WIN3264
6131 mch_writable(fname) &&
6132# else
6133# if defined(UNIX) || defined(VMS)
6134 (perm & 0222) &&
6135# endif
6136# endif
6137 mch_access((char *)fname, W_OK) == 0
6138 )
6139#endif
6140 {
6141 ++retval;
6142 if (mch_isdir(fname))
6143 ++retval;
6144 }
6145 return retval;
6146}
6147#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006148
6149/*
6150 * Print an error message with one or two "%s" and one or two string arguments.
6151 * This is not in message.c to avoid a warning for prototypes.
6152 */
6153 int
6154emsg3(s, a1, a2)
6155 char_u *s, *a1, *a2;
6156{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006157 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006158 return TRUE; /* no error messages at the moment */
Bram Moolenaar6f192452007-11-08 19:49:02 +00006159#ifdef HAVE_STDARG_H
6160 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
6161#else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006162 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2);
Bram Moolenaar6f192452007-11-08 19:49:02 +00006163#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006164 return emsg(IObuff);
6165}
6166
6167/*
6168 * Print an error message with one "%ld" and one long int argument.
6169 * This is not in message.c to avoid a warning for prototypes.
6170 */
6171 int
6172emsgn(s, n)
6173 char_u *s;
6174 long n;
6175{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006176 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006177 return TRUE; /* no error messages at the moment */
6178 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
6179 return emsg(IObuff);
6180}
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006181
6182#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6183/*
6184 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6185 */
6186 int
6187get2c(fd)
6188 FILE *fd;
6189{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006190 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006191
6192 n = getc(fd);
6193 n = (n << 8) + getc(fd);
6194 return n;
6195}
6196
6197/*
6198 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6199 */
6200 int
6201get3c(fd)
6202 FILE *fd;
6203{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006204 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006205
6206 n = getc(fd);
6207 n = (n << 8) + getc(fd);
6208 n = (n << 8) + getc(fd);
6209 return n;
6210}
6211
6212/*
6213 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6214 */
6215 int
6216get4c(fd)
6217 FILE *fd;
6218{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006219 /* Use unsigned rather than int otherwise result is undefined
6220 * when left-shift sets the MSB. */
6221 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006222
Bram Moolenaar95235e62013-09-08 16:07:07 +02006223 n = (unsigned)getc(fd);
6224 n = (n << 8) + (unsigned)getc(fd);
6225 n = (n << 8) + (unsigned)getc(fd);
6226 n = (n << 8) + (unsigned)getc(fd);
6227 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006228}
6229
6230/*
6231 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
6232 */
6233 time_t
6234get8ctime(fd)
6235 FILE *fd;
6236{
6237 time_t n = 0;
6238 int i;
6239
6240 for (i = 0; i < 8; ++i)
6241 n = (n << 8) + getc(fd);
6242 return n;
6243}
6244
6245/*
6246 * Read a string of length "cnt" from "fd" into allocated memory.
6247 * Returns NULL when out of memory or unable to read that many bytes.
6248 */
6249 char_u *
6250read_string(fd, cnt)
6251 FILE *fd;
6252 int cnt;
6253{
6254 char_u *str;
6255 int i;
6256 int c;
6257
6258 /* allocate memory */
6259 str = alloc((unsigned)cnt + 1);
6260 if (str != NULL)
6261 {
6262 /* Read the string. Quit when running into the EOF. */
6263 for (i = 0; i < cnt; ++i)
6264 {
6265 c = getc(fd);
6266 if (c == EOF)
6267 {
6268 vim_free(str);
6269 return NULL;
6270 }
6271 str[i] = c;
6272 }
6273 str[i] = NUL;
6274 }
6275 return str;
6276}
6277
6278/*
6279 * Write a number to file "fd", MSB first, in "len" bytes.
6280 */
6281 int
6282put_bytes(fd, nr, len)
6283 FILE *fd;
6284 long_u nr;
6285 int len;
6286{
6287 int i;
6288
6289 for (i = len - 1; i >= 0; --i)
6290 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6291 return FAIL;
6292 return OK;
6293}
6294
6295#ifdef _MSC_VER
6296# if (_MSC_VER <= 1200)
6297/* This line is required for VC6 without the service pack. Also see the
6298 * matching #pragma below. */
6299 # pragma optimize("", off)
6300# endif
6301#endif
6302
6303/*
6304 * Write time_t to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006305 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006306 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006307 int
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006308put_time(fd, the_time)
6309 FILE *fd;
6310 time_t the_time;
6311{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006312 char_u buf[8];
6313
6314 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006315 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006316}
6317
6318/*
6319 * Write time_t to "buf[8]".
6320 */
6321 void
6322time_to_bytes(the_time, buf)
6323 time_t the_time;
6324 char_u *buf;
6325{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006326 int c;
6327 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006328 int bi = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006329 time_t wtime = the_time;
6330
6331 /* time_t can be up to 8 bytes in size, more than long_u, thus we
6332 * can't use put_bytes() here.
6333 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006334 * sign. This happens for large values of wtime. A cast to long_u may
6335 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes,
6336 * it's safe to assume that long_u is 4 bytes or more and when using 8
6337 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006338 for (i = 7; i >= 0; --i)
6339 {
6340 if (i + 1 > (int)sizeof(time_t))
6341 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006342 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006343 else
6344 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006345#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006346 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006347#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006348 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006349#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006350 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006351 }
6352 }
6353}
6354
6355#ifdef _MSC_VER
6356# if (_MSC_VER <= 1200)
6357 # pragma optimize("", on)
6358# endif
6359#endif
6360
6361#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006362
6363#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6364 || defined(FEAT_SPELL) || defined(PROTO)
6365/*
6366 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6367 * When "s" is NULL FALSE is returned.
6368 */
6369 int
6370has_non_ascii(s)
6371 char_u *s;
6372{
6373 char_u *p;
6374
6375 if (s != NULL)
6376 for (p = s; *p != NUL; ++p)
6377 if (*p >= 128)
6378 return TRUE;
6379 return FALSE;
6380}
6381#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006382
6383#if defined(MESSAGE_QUEUE) || defined(PROTO)
6384/*
6385 * Process messages that have been queued for netbeans or clientserver.
6386 * These functions can call arbitrary vimscript and should only be called when
6387 * it is safe to do so.
6388 */
6389 void
6390parse_queued_messages()
6391{
6392# ifdef FEAT_NETBEANS_INTG
6393 /* Process the queued netbeans messages. */
6394 netbeans_parse_messages();
6395# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006396# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006397 /* Process the queued clientserver messages. */
6398 server_parse_messages();
6399# endif
6400}
6401#endif