blob: 854021255467f726a5aa625811974c6899d054cf [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
801 static int
802alloc_does_fail()
803{
804 if (alloc_fail_countdown == 0)
805 {
806 if (--alloc_fail_repeat <= 0)
807 alloc_fail_id = 0;
808 return TRUE;
809 }
810 --alloc_fail_countdown;
811 return FALSE;
812}
813#endif
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Some memory is reserved for error messages and for being able to
817 * call mf_release_all(), which needs some memory for mf_trans_add().
818 */
819#if defined(MSDOS) && !defined(DJGPP)
820# define SMALL_MEM
821# define KEEP_ROOM 8192L
822#else
823# define KEEP_ROOM (2 * 8192L)
824#endif
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200825#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000828 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 * Use lalloc for larger blocks.
830 */
831 char_u *
832alloc(size)
833 unsigned size;
834{
835 return (lalloc((long_u)size, TRUE));
836}
837
838/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100839 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100840 */
841 char_u *
842alloc_id(size, id)
843 unsigned size;
Bram Moolenaar28fb79d2016-01-09 22:28:33 +0100844 alloc_id_T id UNUSED;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100845{
846#ifdef FEAT_EVAL
847 if (alloc_fail_id == id && alloc_does_fail())
848 return NULL;
849#endif
850 return (lalloc((long_u)size, TRUE));
851}
852
853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 * Allocate memory and set all bytes to zero.
855 */
856 char_u *
857alloc_clear(size)
858 unsigned size;
859{
860 char_u *p;
861
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200862 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 if (p != NULL)
864 (void)vim_memset(p, 0, (size_t)size);
865 return p;
866}
867
868/*
869 * alloc() with check for maximum line length
870 */
871 char_u *
872alloc_check(size)
873 unsigned size;
874{
875#if !defined(UNIX) && !defined(__EMX__)
876 if (sizeof(int) == 2 && size > 0x7fff)
877 {
878 /* Don't hide this message */
879 emsg_silent = 0;
880 EMSG(_("E340: Line is becoming too long"));
881 return NULL;
882 }
883#endif
884 return (lalloc((long_u)size, TRUE));
885}
886
887/*
888 * Allocate memory like lalloc() and set all bytes to zero.
889 */
890 char_u *
891lalloc_clear(size, message)
892 long_u size;
893 int message;
894{
895 char_u *p;
896
897 p = (lalloc(size, message));
898 if (p != NULL)
899 (void)vim_memset(p, 0, (size_t)size);
900 return p;
901}
902
903/*
904 * Low level memory allocation function.
905 * This is used often, KEEP IT FAST!
906 */
907 char_u *
908lalloc(size, message)
909 long_u size;
910 int message;
911{
912 char_u *p; /* pointer to new storage space */
913 static int releasing = FALSE; /* don't do mf_release_all() recursive */
914 int try_again;
915#if defined(HAVE_AVAIL_MEM) && !defined(SMALL_MEM)
916 static long_u allocated = 0; /* allocated since last avail check */
917#endif
918
919 /* Safety check for allocating zero bytes */
920 if (size == 0)
921 {
922 /* Don't hide this message */
923 emsg_silent = 0;
924 EMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
925 return NULL;
926 }
927
928#ifdef MEM_PROFILE
929 mem_pre_alloc_l(&size);
930#endif
931
932#if defined(MSDOS) && !defined(DJGPP)
933 if (size >= 0xfff0) /* in MSDOS we can't deal with >64K blocks */
934 p = NULL;
935 else
936#endif
937
938 /*
939 * Loop when out of memory: Try to release some memfile blocks and
940 * if some blocks are released call malloc again.
941 */
942 for (;;)
943 {
944 /*
945 * Handle three kind of systems:
946 * 1. No check for available memory: Just return.
947 * 2. Slow check for available memory: call mch_avail_mem() after
948 * allocating KEEP_ROOM amount of memory.
949 * 3. Strict check for available memory: call mch_avail_mem()
950 */
951 if ((p = (char_u *)malloc((size_t)size)) != NULL)
952 {
953#ifndef HAVE_AVAIL_MEM
954 /* 1. No check for available memory: Just return. */
955 goto theend;
956#else
957# ifndef SMALL_MEM
958 /* 2. Slow check for available memory: call mch_avail_mem() after
959 * allocating (KEEP_ROOM / 2) amount of memory. */
960 allocated += size;
961 if (allocated < KEEP_ROOM / 2)
962 goto theend;
963 allocated = 0;
964# endif
965 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200966 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000968 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 p = NULL;
970 }
971 else
972 goto theend;
973#endif
974 }
975 /*
976 * Remember that mf_release_all() is being called to avoid an endless
977 * loop, because mf_release_all() may call alloc() recursively.
978 */
979 if (releasing)
980 break;
981 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000982
983 clear_sb_text(); /* free any scrollback text */
984 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 releasing = FALSE;
987 if (!try_again)
988 break;
989 }
990
991 if (message && p == NULL)
992 do_outofmem_msg(size);
993
994theend:
995#ifdef MEM_PROFILE
996 mem_post_alloc((void **)&p, (size_t)size);
997#endif
998 return p;
999}
1000
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001001/*
1002 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001003 */
1004 char_u *
1005lalloc_id(size, message, id)
1006 long_u size;
1007 int message;
Bram Moolenaar28fb79d2016-01-09 22:28:33 +01001008 alloc_id_T id UNUSED;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01001009{
1010#ifdef FEAT_EVAL
1011 if (alloc_fail_id == id && alloc_does_fail())
1012 return NULL;
1013#endif
1014 return (lalloc((long_u)size, message));
1015}
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#if defined(MEM_PROFILE) || defined(PROTO)
1018/*
1019 * realloc() with memory profiling.
1020 */
1021 void *
1022mem_realloc(ptr, size)
1023 void *ptr;
1024 size_t size;
1025{
1026 void *p;
1027
1028 mem_pre_free(&ptr);
1029 mem_pre_alloc_s(&size);
1030
1031 p = realloc(ptr, size);
1032
1033 mem_post_alloc(&p, size);
1034
1035 return p;
1036}
1037#endif
1038
1039/*
1040* Avoid repeating the error message many times (they take 1 second each).
1041* Did_outofmem_msg is reset when a character is read.
1042*/
1043 void
1044do_outofmem_msg(size)
1045 long_u size;
1046{
1047 if (!did_outofmem_msg)
1048 {
1049 /* Don't hide this message */
1050 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001051
1052 /* Must come first to avoid coming back here when printing the error
1053 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001055
1056 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 }
1058}
1059
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001060#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001061
1062# if defined(FEAT_SEARCHPATH)
1063static void free_findfile __ARGS((void));
1064# endif
1065
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001066/*
1067 * Free everything that we allocated.
1068 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001069 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1070 * surprised if Vim crashes...
1071 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001072 */
1073 void
1074free_all_mem()
1075{
1076 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001077 static int entered = FALSE;
1078
1079 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1080 * Don't try freeing everything again. */
1081 if (entered)
1082 return;
1083 entered = TRUE;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001084
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001085# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001086 /* Don't want to trigger autocommands from here on. */
1087 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001088# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001089
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001090# ifdef FEAT_WINDOWS
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001091 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1092 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001093 if (first_tabpage->tp_next != NULL)
1094 do_cmdline_cmd((char_u *)"tabonly!");
1095 if (firstwin != lastwin)
1096 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001097# endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001098
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001099# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001100 /* Free all spell info. */
1101 spell_free_all();
1102# endif
1103
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001104# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001105 /* Clear user commands (before deleting buffers). */
1106 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001107# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001108
1109# ifdef FEAT_MENU
1110 /* Clear menus. */
1111 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001112# ifdef FEAT_MULTI_LANG
1113 do_cmdline_cmd((char_u *)"menutranslate clear");
1114# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001115# endif
1116
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001117 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001118 do_cmdline_cmd((char_u *)"lmapclear");
1119 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001120 do_cmdline_cmd((char_u *)"mapclear");
1121 do_cmdline_cmd((char_u *)"mapclear!");
1122 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001123# if defined(FEAT_EVAL)
1124 do_cmdline_cmd((char_u *)"breakdel *");
1125# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001126# if defined(FEAT_PROFILE)
1127 do_cmdline_cmd((char_u *)"profdel *");
1128# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001129# if defined(FEAT_KEYMAP)
1130 do_cmdline_cmd((char_u *)"set keymap=");
1131#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001132
1133# ifdef FEAT_TITLE
1134 free_titles();
1135# endif
1136# if defined(FEAT_SEARCHPATH)
1137 free_findfile();
1138# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001139
1140 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001141# if defined(FEAT_AUTOCMD)
1142 free_all_autocmds();
1143# endif
1144 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001145 free_all_options();
1146 free_all_marks();
1147 alist_clear(&global_alist);
1148 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001149# if defined(FEAT_CMDL_COMPL)
1150 free_users();
1151# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001152 free_search_patterns();
1153 free_old_sub();
1154 free_last_insert();
1155 free_prev_shellcmd();
1156 free_regexp_stuff();
1157 free_tag_stuff();
1158 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001159# ifdef FEAT_SIGNS
1160 free_signs();
1161# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001162# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001163 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001164# endif
1165# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001166 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001167# endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001168 clear_sb_text(); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001169
1170 /* Free some global vars. */
1171 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001172# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001173 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001174# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001175 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001176# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001177 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001178# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001179 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001180 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001181
1182 /* Clear cmdline history. */
1183 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001184# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001185 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001186# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001187
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001188#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001189 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001190 win_T *win;
1191 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001192
1193 qf_free_all(NULL);
1194 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001195 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001196 qf_free_all(win);
1197 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001198#endif
1199
1200 /* Close all script inputs. */
1201 close_all_scripts();
1202
1203#if defined(FEAT_WINDOWS)
1204 /* Destroy all windows. Must come before freeing buffers. */
1205 win_free_all();
1206#endif
1207
Bram Moolenaar2a329742008-04-01 12:53:43 +00001208 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1209 * were freed already. */
1210#ifdef FEAT_AUTOCHDIR
1211 p_acd = FALSE;
1212#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001213 for (buf = firstbuf; buf != NULL; )
1214 {
1215 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001216 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001217 if (buf_valid(buf))
1218 buf = nextbuf; /* didn't work, try next one */
1219 else
1220 buf = firstbuf;
1221 }
1222
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001223#ifdef FEAT_ARABIC
1224 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001225#endif
1226
1227 /* Clear registers. */
1228 clear_registers();
1229 ResetRedobuff();
1230 ResetRedobuff();
1231
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001232#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001233 vim_free(serverDelayedStartName);
1234#endif
1235
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001236 /* highlight info */
1237 free_highlight();
1238
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001239 reset_last_sourcing();
1240
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001241#ifdef FEAT_WINDOWS
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001242 free_tabpage(first_tabpage);
1243 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001244#endif
1245
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001246# ifdef UNIX
1247 /* Machine-specific free. */
1248 mch_free_mem();
1249# endif
1250
1251 /* message history */
1252 for (;;)
1253 if (delete_first_msg() == FAIL)
1254 break;
1255
1256# ifdef FEAT_EVAL
1257 eval_clear();
1258# endif
1259
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001260 free_termoptions();
1261
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001262 /* screenlines (can't display anything now!) */
1263 free_screenlines();
1264
1265#if defined(USE_XSMP)
1266 xsmp_close();
1267#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001268#ifdef FEAT_GUI_GTK
1269 gui_mch_free_all();
1270#endif
1271 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001272
1273 vim_free(IObuff);
1274 vim_free(NameBuff);
1275}
1276#endif
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001279 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 */
1281 char_u *
1282vim_strsave(string)
1283 char_u *string;
1284{
1285 char_u *p;
1286 unsigned len;
1287
1288 len = (unsigned)STRLEN(string) + 1;
1289 p = alloc(len);
1290 if (p != NULL)
1291 mch_memmove(p, string, (size_t)len);
1292 return p;
1293}
1294
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001295/*
1296 * Copy up to "len" bytes of "string" into newly allocated memory and
1297 * terminate with a NUL.
1298 * The allocated memory always has size "len + 1", also when "string" is
1299 * shorter.
1300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *
1302vim_strnsave(string, len)
1303 char_u *string;
1304 int len;
1305{
1306 char_u *p;
1307
1308 p = alloc((unsigned)(len + 1));
1309 if (p != NULL)
1310 {
1311 STRNCPY(p, string, len);
1312 p[len] = NUL;
1313 }
1314 return p;
1315}
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317/*
1318 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1319 * by a backslash.
1320 */
1321 char_u *
1322vim_strsave_escaped(string, esc_chars)
1323 char_u *string;
1324 char_u *esc_chars;
1325{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001326 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327}
1328
1329/*
1330 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1331 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001332 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 */
1334 char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001335vim_strsave_escaped_ext(string, esc_chars, cc, bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 char_u *string;
1337 char_u *esc_chars;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001338 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 int bsl;
1340{
1341 char_u *p;
1342 char_u *p2;
1343 char_u *escaped_string;
1344 unsigned length;
1345#ifdef FEAT_MBYTE
1346 int l;
1347#endif
1348
1349 /*
1350 * First count the number of backslashes required.
1351 * Then allocate the memory and insert them.
1352 */
1353 length = 1; /* count the trailing NUL */
1354 for (p = string; *p; p++)
1355 {
1356#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001357 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 length += l; /* count a multibyte char */
1360 p += l - 1;
1361 continue;
1362 }
1363#endif
1364 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1365 ++length; /* count a backslash */
1366 ++length; /* count an ordinary char */
1367 }
1368 escaped_string = alloc(length);
1369 if (escaped_string != NULL)
1370 {
1371 p2 = escaped_string;
1372 for (p = string; *p; p++)
1373 {
1374#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001375 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
1377 mch_memmove(p2, p, (size_t)l);
1378 p2 += l;
1379 p += l - 1; /* skip multibyte char */
1380 continue;
1381 }
1382#endif
1383 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001384 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 *p2++ = *p;
1386 }
1387 *p2 = NUL;
1388 }
1389 return escaped_string;
1390}
1391
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001392/*
1393 * Return TRUE when 'shell' has "csh" in the tail.
1394 */
1395 int
1396csh_like_shell()
1397{
1398 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1399}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001400
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001401/*
1402 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001403 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001404 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001405 * Escape a newline, depending on the 'shell' option.
1406 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1407 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001408 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001409 * Returns the result in allocated memory, NULL if we have run out.
1410 */
1411 char_u *
Bram Moolenaar26df0922014-02-23 23:39:13 +01001412vim_strsave_shellescape(string, do_special, do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001413 char_u *string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001414 int do_special;
Bram Moolenaar26df0922014-02-23 23:39:13 +01001415 int do_newline;
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001416{
1417 unsigned length;
1418 char_u *p;
1419 char_u *d;
1420 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001421 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001422 int csh_like;
1423
1424 /* Only csh and similar shells expand '!' within single quotes. For sh and
1425 * the like we must not put a backslash before it, it will be taken
1426 * literally. If do_special is set the '!' will be escaped twice.
1427 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001428 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001429
1430 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001431 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001432 for (p = string; *p != NUL; mb_ptr_adv(p))
1433 {
1434# if defined(WIN32) || defined(WIN16) || defined(DOS)
1435 if (!p_ssl)
1436 {
1437 if (*p == '"')
1438 ++length; /* " -> "" */
1439 }
1440 else
1441# endif
1442 if (*p == '\'')
1443 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001444 if ((*p == '\n' && (csh_like || do_newline))
1445 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001446 {
1447 ++length; /* insert backslash */
1448 if (csh_like && do_special)
1449 ++length; /* insert backslash */
1450 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001451 if (do_special && find_cmdline_var(p, &l) >= 0)
1452 {
1453 ++length; /* insert backslash */
1454 p += l - 1;
1455 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001456 }
1457
1458 /* Allocate memory for the result and fill it. */
1459 escaped_string = alloc(length);
1460 if (escaped_string != NULL)
1461 {
1462 d = escaped_string;
1463
1464 /* add opening quote */
1465# if defined(WIN32) || defined(WIN16) || defined(DOS)
1466 if (!p_ssl)
1467 *d++ = '"';
1468 else
1469# endif
1470 *d++ = '\'';
1471
1472 for (p = string; *p != NUL; )
1473 {
1474# if defined(WIN32) || defined(WIN16) || defined(DOS)
1475 if (!p_ssl)
1476 {
1477 if (*p == '"')
1478 {
1479 *d++ = '"';
1480 *d++ = '"';
1481 ++p;
1482 continue;
1483 }
1484 }
1485 else
1486# endif
1487 if (*p == '\'')
1488 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001489 *d++ = '\'';
1490 *d++ = '\\';
1491 *d++ = '\'';
1492 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001493 ++p;
1494 continue;
1495 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001496 if ((*p == '\n' && (csh_like || do_newline))
1497 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001498 {
1499 *d++ = '\\';
1500 if (csh_like && do_special)
1501 *d++ = '\\';
1502 *d++ = *p++;
1503 continue;
1504 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001505 if (do_special && find_cmdline_var(p, &l) >= 0)
1506 {
1507 *d++ = '\\'; /* insert backslash */
1508 while (--l >= 0) /* copy the var */
1509 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001510 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001511 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001512
1513 MB_COPY_CHAR(p, d);
1514 }
1515
1516 /* add terminating quote and finish with a NUL */
1517# if defined(WIN32) || defined(WIN16) || defined(DOS)
1518 if (!p_ssl)
1519 *d++ = '"';
1520 else
1521# endif
1522 *d++ = '\'';
1523 *d = NUL;
1524 }
1525
1526 return escaped_string;
1527}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529/*
1530 * Like vim_strsave(), but make all characters uppercase.
1531 * This uses ASCII lower-to-upper case translation, language independent.
1532 */
1533 char_u *
1534vim_strsave_up(string)
1535 char_u *string;
1536{
1537 char_u *p1;
1538
1539 p1 = vim_strsave(string);
1540 vim_strup(p1);
1541 return p1;
1542}
1543
1544/*
1545 * Like vim_strnsave(), but make all characters uppercase.
1546 * This uses ASCII lower-to-upper case translation, language independent.
1547 */
1548 char_u *
1549vim_strnsave_up(string, len)
1550 char_u *string;
1551 int len;
1552{
1553 char_u *p1;
1554
1555 p1 = vim_strnsave(string, len);
1556 vim_strup(p1);
1557 return p1;
1558}
1559
1560/*
1561 * ASCII lower-to-upper case translation, language independent.
1562 */
1563 void
1564vim_strup(p)
1565 char_u *p;
1566{
1567 char_u *p2;
1568 int c;
1569
1570 if (p != NULL)
1571 {
1572 p2 = p;
1573 while ((c = *p2) != NUL)
1574#ifdef EBCDIC
1575 *p2++ = isalpha(c) ? toupper(c) : c;
1576#else
1577 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1578#endif
1579 }
1580}
1581
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001582#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001583/*
1584 * Make string "s" all upper-case and return it in allocated memory.
1585 * Handles multi-byte characters as well as possible.
1586 * Returns NULL when out of memory.
1587 */
1588 char_u *
1589strup_save(orig)
1590 char_u *orig;
1591{
1592 char_u *p;
1593 char_u *res;
1594
1595 res = p = vim_strsave(orig);
1596
1597 if (res != NULL)
1598 while (*p != NUL)
1599 {
1600# ifdef FEAT_MBYTE
1601 int l;
1602
1603 if (enc_utf8)
1604 {
1605 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001606 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001607 char_u *s;
1608
1609 c = utf_ptr2char(p);
1610 uc = utf_toupper(c);
1611
1612 /* Reallocate string when byte count changes. This is rare,
1613 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001614 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001615 newl = utf_char2len(uc);
1616 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001617 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001618 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001619 if (s == NULL)
1620 break;
1621 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001622 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001623 p = s + (p - res);
1624 vim_free(res);
1625 res = s;
1626 }
1627
1628 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001629 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001630 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001631 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001632 p += l; /* skip multi-byte character */
1633 else
1634# endif
1635 {
1636 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1637 p++;
1638 }
1639 }
1640
1641 return res;
1642}
1643#endif
1644
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 * delete spaces at the end of a string
1647 */
1648 void
1649del_trailing_spaces(ptr)
1650 char_u *ptr;
1651{
1652 char_u *q;
1653
1654 q = ptr + STRLEN(ptr);
1655 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
1656 *q = NUL;
1657}
1658
1659/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001660 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001661 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 */
1663 void
1664vim_strncpy(to, from, len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001665 char_u *to;
1666 char_u *from;
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001667 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001669 STRNCPY(to, from, len);
1670 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671}
1672
1673/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001674 * Like strcat(), but make sure the result fits in "tosize" bytes and is
1675 * always NUL terminated.
1676 */
1677 void
1678vim_strcat(to, from, tosize)
1679 char_u *to;
1680 char_u *from;
1681 size_t tosize;
1682{
1683 size_t tolen = STRLEN(to);
1684 size_t fromlen = STRLEN(from);
1685
1686 if (tolen + fromlen + 1 > tosize)
1687 {
1688 mch_memmove(to + tolen, from, tosize - tolen - 1);
1689 to[tosize - 1] = NUL;
1690 }
1691 else
1692 STRCPY(to + tolen, from);
1693}
1694
1695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 * Isolate one part of a string option where parts are separated with
1697 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001698 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 * "*option" is advanced to the next part.
1700 * The length is returned.
1701 */
1702 int
1703copy_option_part(option, buf, maxlen, sep_chars)
1704 char_u **option;
1705 char_u *buf;
1706 int maxlen;
1707 char *sep_chars;
1708{
1709 int len = 0;
1710 char_u *p = *option;
1711
1712 /* skip '.' at start of option part, for 'suffixes' */
1713 if (*p == '.')
1714 buf[len++] = *p++;
1715 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1716 {
1717 /*
1718 * Skip backslash before a separator character and space.
1719 */
1720 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1721 ++p;
1722 if (len < maxlen - 1)
1723 buf[len++] = *p;
1724 ++p;
1725 }
1726 buf[len] = NUL;
1727
1728 if (*p != NUL && *p != ',') /* skip non-standard separator */
1729 ++p;
1730 p = skip_to_option_part(p); /* p points to next file name */
1731
1732 *option = p;
1733 return len;
1734}
1735
1736/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001737 * Replacement for free() that ignores NULL pointers.
1738 * Also skip free() when exiting for sure, this helps when we caught a deadly
1739 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 */
1741 void
1742vim_free(x)
1743 void *x;
1744{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001745 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
1747#ifdef MEM_PROFILE
1748 mem_pre_free(&x);
1749#endif
1750 free(x);
1751 }
1752}
1753
1754#ifndef HAVE_MEMSET
1755 void *
1756vim_memset(ptr, c, size)
1757 void *ptr;
1758 int c;
1759 size_t size;
1760{
1761 char *p = ptr;
1762
1763 while (size-- > 0)
1764 *p++ = c;
1765 return ptr;
1766}
1767#endif
1768
1769#ifdef VIM_MEMCMP
1770/*
1771 * Return zero when "b1" and "b2" are the same for "len" bytes.
1772 * Return non-zero otherwise.
1773 */
1774 int
1775vim_memcmp(b1, b2, len)
1776 void *b1;
1777 void *b2;
1778 size_t len;
1779{
1780 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2;
1781
1782 for ( ; len > 0; --len)
1783 {
1784 if (*p1 != *p2)
1785 return 1;
1786 ++p1;
1787 ++p2;
1788 }
1789 return 0;
1790}
1791#endif
1792
1793#ifdef VIM_MEMMOVE
1794/*
1795 * Version of memmove() that handles overlapping source and destination.
1796 * For systems that don't have a function that is guaranteed to do that (SYSV).
1797 */
1798 void
1799mch_memmove(dst_arg, src_arg, len)
1800 void *src_arg, *dst_arg;
1801 size_t len;
1802{
1803 /*
1804 * A void doesn't have a size, we use char pointers.
1805 */
1806 char *dst = dst_arg, *src = src_arg;
1807
1808 /* overlap, copy backwards */
1809 if (dst > src && dst < src + len)
1810 {
1811 src += len;
1812 dst += len;
1813 while (len-- > 0)
1814 *--dst = *--src;
1815 }
1816 else /* copy forwards */
1817 while (len-- > 0)
1818 *dst++ = *src++;
1819}
1820#endif
1821
1822#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1823/*
1824 * Compare two strings, ignoring case, using current locale.
1825 * Doesn't work for multi-byte characters.
1826 * return 0 for match, < 0 for smaller, > 0 for bigger
1827 */
1828 int
1829vim_stricmp(s1, s2)
1830 char *s1;
1831 char *s2;
1832{
1833 int i;
1834
1835 for (;;)
1836 {
1837 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1838 if (i != 0)
1839 return i; /* this character different */
1840 if (*s1 == NUL)
1841 break; /* strings match until NUL */
1842 ++s1;
1843 ++s2;
1844 }
1845 return 0; /* strings match */
1846}
1847#endif
1848
1849#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1850/*
1851 * Compare two strings, for length "len", ignoring case, using current locale.
1852 * Doesn't work for multi-byte characters.
1853 * return 0 for match, < 0 for smaller, > 0 for bigger
1854 */
1855 int
1856vim_strnicmp(s1, s2, len)
1857 char *s1;
1858 char *s2;
1859 size_t len;
1860{
1861 int i;
1862
1863 while (len > 0)
1864 {
1865 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1866 if (i != 0)
1867 return i; /* this character different */
1868 if (*s1 == NUL)
1869 break; /* strings match until NUL */
1870 ++s1;
1871 ++s2;
1872 --len;
1873 }
1874 return 0; /* strings match */
1875}
1876#endif
1877
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878/*
1879 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001880 * with characters from 128 to 255 correctly. It also doesn't return a
1881 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 */
1883 char_u *
1884vim_strchr(string, c)
1885 char_u *string;
1886 int c;
1887{
1888 char_u *p;
1889 int b;
1890
1891 p = string;
1892#ifdef FEAT_MBYTE
1893 if (enc_utf8 && c >= 0x80)
1894 {
1895 while (*p != NUL)
1896 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001897 int l = (*mb_ptr2len)(p);
1898
1899 /* Avoid matching an illegal byte here. */
1900 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001902 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 return NULL;
1905 }
1906 if (enc_dbcs != 0 && c > 255)
1907 {
1908 int n2 = c & 0xff;
1909
1910 c = ((unsigned)c >> 8) & 0xff;
1911 while ((b = *p) != NUL)
1912 {
1913 if (b == c && p[1] == n2)
1914 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001915 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 return NULL;
1918 }
1919 if (has_mbyte)
1920 {
1921 while ((b = *p) != NUL)
1922 {
1923 if (b == c)
1924 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001925 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 }
1927 return NULL;
1928 }
1929#endif
1930 while ((b = *p) != NUL)
1931 {
1932 if (b == c)
1933 return p;
1934 ++p;
1935 }
1936 return NULL;
1937}
1938
1939/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001940 * Version of strchr() that only works for bytes and handles unsigned char
1941 * strings with characters above 128 correctly. It also doesn't return a
1942 * pointer to the NUL at the end of the string.
1943 */
1944 char_u *
1945vim_strbyte(string, c)
1946 char_u *string;
1947 int c;
1948{
1949 char_u *p = string;
1950
1951 while (*p != NUL)
1952 {
1953 if (*p == c)
1954 return p;
1955 ++p;
1956 }
1957 return NULL;
1958}
1959
1960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001962 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001963 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
1965 char_u *
1966vim_strrchr(string, c)
1967 char_u *string;
1968 int c;
1969{
1970 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001971 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
Bram Moolenaar05159a02005-02-26 23:04:13 +00001973 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001975 if (*p == c)
1976 retval = p;
1977 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 return retval;
1980}
1981
1982/*
1983 * Vim's version of strpbrk(), in case it's missing.
1984 * Don't generate a prototype for this, causes problems when it's not used.
1985 */
1986#ifndef PROTO
1987# ifndef HAVE_STRPBRK
1988# ifdef vim_strpbrk
1989# undef vim_strpbrk
1990# endif
1991 char_u *
1992vim_strpbrk(s, charset)
1993 char_u *s;
1994 char_u *charset;
1995{
1996 while (*s)
1997 {
1998 if (vim_strchr(charset, *s) != NULL)
1999 return s;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002000 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 return NULL;
2003}
2004# endif
2005#endif
2006
2007/*
2008 * Vim has its own isspace() function, because on some machines isspace()
2009 * can't handle characters above 128.
2010 */
2011 int
2012vim_isspace(x)
2013 int x;
2014{
2015 return ((x >= 9 && x <= 13) || x == ' ');
2016}
2017
2018/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002019 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 */
2021
2022/*
2023 * Clear an allocated growing array.
2024 */
2025 void
2026ga_clear(gap)
2027 garray_T *gap;
2028{
2029 vim_free(gap->ga_data);
2030 ga_init(gap);
2031}
2032
2033/*
2034 * Clear a growing array that contains a list of strings.
2035 */
2036 void
2037ga_clear_strings(gap)
2038 garray_T *gap;
2039{
2040 int i;
2041
2042 for (i = 0; i < gap->ga_len; ++i)
2043 vim_free(((char_u **)(gap->ga_data))[i]);
2044 ga_clear(gap);
2045}
2046
2047/*
2048 * Initialize a growing array. Don't forget to set ga_itemsize and
2049 * ga_growsize! Or use ga_init2().
2050 */
2051 void
2052ga_init(gap)
2053 garray_T *gap;
2054{
2055 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002056 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 gap->ga_len = 0;
2058}
2059
2060 void
2061ga_init2(gap, itemsize, growsize)
2062 garray_T *gap;
2063 int itemsize;
2064 int growsize;
2065{
2066 ga_init(gap);
2067 gap->ga_itemsize = itemsize;
2068 gap->ga_growsize = growsize;
2069}
2070
2071/*
2072 * Make room in growing array "gap" for at least "n" items.
2073 * Return FAIL for failure, OK otherwise.
2074 */
2075 int
2076ga_grow(gap, n)
2077 garray_T *gap;
2078 int n;
2079{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002080 size_t old_len;
2081 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 char_u *pp;
2083
Bram Moolenaar86b68352004-12-27 21:59:20 +00002084 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 if (n < gap->ga_growsize)
2087 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002088 new_len = gap->ga_itemsize * (gap->ga_len + n);
2089 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002090 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 if (pp == NULL)
2092 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002093 old_len = gap->ga_itemsize * gap->ga_maxlen;
2094 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002095 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 gap->ga_data = pp;
2097 }
2098 return OK;
2099}
2100
2101/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002102 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002103 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002104 * Returns NULL when out of memory.
2105 */
2106 char_u *
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002107ga_concat_strings(gap, sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002108 garray_T *gap;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002109 char *sep;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002110{
2111 int i;
2112 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002113 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002114 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002115 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002116
2117 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002118 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002119
2120 s = alloc(len + 1);
2121 if (s != NULL)
2122 {
2123 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002124 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002125 for (i = 0; i < gap->ga_len; ++i)
2126 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002127 if (p != s)
2128 {
2129 STRCPY(p, sep);
2130 p += sep_len;
2131 }
2132 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2133 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002134 }
2135 }
2136 return s;
2137}
2138
2139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 * Concatenate a string to a growarray which contains characters.
Bram Moolenaar43345542015-11-29 17:35:35 +01002141 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 * Note: Does NOT copy the NUL at the end!
2143 */
2144 void
2145ga_concat(gap, s)
2146 garray_T *gap;
2147 char_u *s;
2148{
Bram Moolenaar43345542015-11-29 17:35:35 +01002149 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar43345542015-11-29 17:35:35 +01002151 if (s == NULL)
2152 return;
2153 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (ga_grow(gap, len) == OK)
2155 {
2156 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2157 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159}
2160
2161/*
2162 * Append one byte to a growarray which contains bytes.
2163 */
2164 void
2165ga_append(gap, c)
2166 garray_T *gap;
2167 int c;
2168{
2169 if (ga_grow(gap, 1) == OK)
2170 {
2171 *((char *)gap->ga_data + gap->ga_len) = c;
2172 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174}
2175
Bram Moolenaar597a4222014-06-25 14:39:50 +02002176#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2177 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002178/*
2179 * Append the text in "gap" below the cursor line and clear "gap".
2180 */
2181 void
2182append_ga_line(gap)
2183 garray_T *gap;
2184{
2185 /* Remove trailing CR. */
2186 if (gap->ga_len > 0
2187 && !curbuf->b_p_bin
2188 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2189 --gap->ga_len;
2190 ga_append(gap, NUL);
2191 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2192 gap->ga_len = 0;
2193}
2194#endif
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196/************************************************************************
2197 * functions that use lookup tables for various things, generally to do with
2198 * special key codes.
2199 */
2200
2201/*
2202 * Some useful tables.
2203 */
2204
2205static struct modmasktable
2206{
2207 short mod_mask; /* Bit-mask for particular key modifier */
2208 short mod_flag; /* Bit(s) for particular key modifier */
2209 char_u name; /* Single letter name of modifier */
2210} mod_mask_table[] =
2211{
2212 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002213 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2215 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2216 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2217 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2218 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
2219#ifdef MACOS
2220 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2221#endif
2222 /* 'A' must be the last one */
2223 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2224 {0, 0, NUL}
2225};
2226
2227/*
2228 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002229 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
2231#define MOD_KEYS_ENTRY_SIZE 5
2232
2233static char_u modifier_keys_table[] =
2234{
2235/* mod mask with modifier without modifier */
2236 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2237 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2238 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2239 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2240 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2241 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2242 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2243 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2244 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2245 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2246 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2247 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2248 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2249 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2250 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2251 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2252 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2253 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2254 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2255 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2256 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2257 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2258 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2259 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2260 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2261 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2262 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2263 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2264 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2265 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2266 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2267 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2269
2270 /* vt100 F1 */
2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2273 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2275
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2281 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2286
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2292 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2297
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2300 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2301 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2302 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2303 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2304 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2305 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2306 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2307 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2308
2309 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2310 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2311 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2312 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2313 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2314 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2315 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2316
2317 /* TAB pseudo code*/
2318 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2319
2320 NUL
2321};
2322
2323static struct key_name_entry
2324{
2325 int key; /* Special key code or ascii value */
2326 char_u *name; /* Name of key */
2327} key_names_table[] =
2328{
2329 {' ', (char_u *)"Space"},
2330 {TAB, (char_u *)"Tab"},
2331 {K_TAB, (char_u *)"Tab"},
2332 {NL, (char_u *)"NL"},
2333 {NL, (char_u *)"NewLine"}, /* Alternative name */
2334 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2335 {NL, (char_u *)"LF"}, /* Alternative name */
2336 {CAR, (char_u *)"CR"},
2337 {CAR, (char_u *)"Return"}, /* Alternative name */
2338 {CAR, (char_u *)"Enter"}, /* Alternative name */
2339 {K_BS, (char_u *)"BS"},
2340 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2341 {ESC, (char_u *)"Esc"},
2342 {CSI, (char_u *)"CSI"},
2343 {K_CSI, (char_u *)"xCSI"},
2344 {'|', (char_u *)"Bar"},
2345 {'\\', (char_u *)"Bslash"},
2346 {K_DEL, (char_u *)"Del"},
2347 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2348 {K_KDEL, (char_u *)"kDel"},
2349 {K_UP, (char_u *)"Up"},
2350 {K_DOWN, (char_u *)"Down"},
2351 {K_LEFT, (char_u *)"Left"},
2352 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002353 {K_XUP, (char_u *)"xUp"},
2354 {K_XDOWN, (char_u *)"xDown"},
2355 {K_XLEFT, (char_u *)"xLeft"},
2356 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 {K_F1, (char_u *)"F1"},
2359 {K_F2, (char_u *)"F2"},
2360 {K_F3, (char_u *)"F3"},
2361 {K_F4, (char_u *)"F4"},
2362 {K_F5, (char_u *)"F5"},
2363 {K_F6, (char_u *)"F6"},
2364 {K_F7, (char_u *)"F7"},
2365 {K_F8, (char_u *)"F8"},
2366 {K_F9, (char_u *)"F9"},
2367 {K_F10, (char_u *)"F10"},
2368
2369 {K_F11, (char_u *)"F11"},
2370 {K_F12, (char_u *)"F12"},
2371 {K_F13, (char_u *)"F13"},
2372 {K_F14, (char_u *)"F14"},
2373 {K_F15, (char_u *)"F15"},
2374 {K_F16, (char_u *)"F16"},
2375 {K_F17, (char_u *)"F17"},
2376 {K_F18, (char_u *)"F18"},
2377 {K_F19, (char_u *)"F19"},
2378 {K_F20, (char_u *)"F20"},
2379
2380 {K_F21, (char_u *)"F21"},
2381 {K_F22, (char_u *)"F22"},
2382 {K_F23, (char_u *)"F23"},
2383 {K_F24, (char_u *)"F24"},
2384 {K_F25, (char_u *)"F25"},
2385 {K_F26, (char_u *)"F26"},
2386 {K_F27, (char_u *)"F27"},
2387 {K_F28, (char_u *)"F28"},
2388 {K_F29, (char_u *)"F29"},
2389 {K_F30, (char_u *)"F30"},
2390
2391 {K_F31, (char_u *)"F31"},
2392 {K_F32, (char_u *)"F32"},
2393 {K_F33, (char_u *)"F33"},
2394 {K_F34, (char_u *)"F34"},
2395 {K_F35, (char_u *)"F35"},
2396 {K_F36, (char_u *)"F36"},
2397 {K_F37, (char_u *)"F37"},
2398
2399 {K_XF1, (char_u *)"xF1"},
2400 {K_XF2, (char_u *)"xF2"},
2401 {K_XF3, (char_u *)"xF3"},
2402 {K_XF4, (char_u *)"xF4"},
2403
2404 {K_HELP, (char_u *)"Help"},
2405 {K_UNDO, (char_u *)"Undo"},
2406 {K_INS, (char_u *)"Insert"},
2407 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2408 {K_KINS, (char_u *)"kInsert"},
2409 {K_HOME, (char_u *)"Home"},
2410 {K_KHOME, (char_u *)"kHome"},
2411 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002412 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {K_END, (char_u *)"End"},
2414 {K_KEND, (char_u *)"kEnd"},
2415 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002416 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {K_PAGEUP, (char_u *)"PageUp"},
2418 {K_PAGEDOWN, (char_u *)"PageDown"},
2419 {K_KPAGEUP, (char_u *)"kPageUp"},
2420 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2421
2422 {K_KPLUS, (char_u *)"kPlus"},
2423 {K_KMINUS, (char_u *)"kMinus"},
2424 {K_KDIVIDE, (char_u *)"kDivide"},
2425 {K_KMULTIPLY, (char_u *)"kMultiply"},
2426 {K_KENTER, (char_u *)"kEnter"},
2427 {K_KPOINT, (char_u *)"kPoint"},
2428
2429 {K_K0, (char_u *)"k0"},
2430 {K_K1, (char_u *)"k1"},
2431 {K_K2, (char_u *)"k2"},
2432 {K_K3, (char_u *)"k3"},
2433 {K_K4, (char_u *)"k4"},
2434 {K_K5, (char_u *)"k5"},
2435 {K_K6, (char_u *)"k6"},
2436 {K_K7, (char_u *)"k7"},
2437 {K_K8, (char_u *)"k8"},
2438 {K_K9, (char_u *)"k9"},
2439
2440 {'<', (char_u *)"lt"},
2441
2442 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002443#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002445#endif
2446#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002448#endif
2449#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002451#endif
2452#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002454#endif
2455#ifdef FEAT_MOUSE_URXVT
2456 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2457#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002458#ifdef FEAT_MOUSE_SGR
2459 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
2460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2462 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2463 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2464 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2465 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2466 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2467 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2468 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2469 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2470 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2471 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002472 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2473 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2474 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2475 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2476 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2477 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {K_X1MOUSE, (char_u *)"X1Mouse"},
2479 {K_X1DRAG, (char_u *)"X1Drag"},
2480 {K_X1RELEASE, (char_u *)"X1Release"},
2481 {K_X2MOUSE, (char_u *)"X2Mouse"},
2482 {K_X2DRAG, (char_u *)"X2Drag"},
2483 {K_X2RELEASE, (char_u *)"X2Release"},
2484 {K_DROP, (char_u *)"Drop"},
2485 {K_ZERO, (char_u *)"Nul"},
2486#ifdef FEAT_EVAL
2487 {K_SNR, (char_u *)"SNR"},
2488#endif
2489 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002490 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {0, NULL}
2492};
2493
2494#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2495
2496#ifdef FEAT_MOUSE
2497static struct mousetable
2498{
2499 int pseudo_code; /* Code for pseudo mouse event */
2500 int button; /* Which mouse button is it? */
2501 int is_click; /* Is it a mouse button click event? */
2502 int is_drag; /* Is it a mouse drag event? */
2503} mouse_table[] =
2504{
2505 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2506#ifdef FEAT_GUI
2507 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2508#endif
2509 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2510 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2511#ifdef FEAT_GUI
2512 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2513#endif
2514 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2515 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2516 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2517 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2518 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2519 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2520 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2521 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2522 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2523 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2524 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2525 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2526 /* DRAG without CLICK */
2527 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2528 /* RELEASE without CLICK */
2529 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2530 {0, 0, 0, 0},
2531};
2532#endif /* FEAT_MOUSE */
2533
2534/*
2535 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2536 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2537 */
2538 int
2539name_to_mod_mask(c)
2540 int c;
2541{
2542 int i;
2543
2544 c = TOUPPER_ASC(c);
2545 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2546 if (c == mod_mask_table[i].name)
2547 return mod_mask_table[i].mod_flag;
2548 return 0;
2549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
2552 * Check if if there is a special key code for "key" that includes the
2553 * modifiers specified.
2554 */
2555 int
2556simplify_key(key, modifiers)
2557 int key;
2558 int *modifiers;
2559{
2560 int i;
2561 int key0;
2562 int key1;
2563
2564 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2565 {
2566 /* TAB is a special case */
2567 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2568 {
2569 *modifiers &= ~MOD_MASK_SHIFT;
2570 return K_S_TAB;
2571 }
2572 key0 = KEY2TERMCAP0(key);
2573 key1 = KEY2TERMCAP1(key);
2574 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2575 if (key0 == modifier_keys_table[i + 3]
2576 && key1 == modifier_keys_table[i + 4]
2577 && (*modifiers & modifier_keys_table[i]))
2578 {
2579 *modifiers &= ~modifier_keys_table[i];
2580 return TERMCAP2KEY(modifier_keys_table[i + 1],
2581 modifier_keys_table[i + 2]);
2582 }
2583 }
2584 return key;
2585}
2586
2587/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002588 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002589 */
2590 int
2591handle_x_keys(key)
2592 int key;
2593{
2594 switch (key)
2595 {
2596 case K_XUP: return K_UP;
2597 case K_XDOWN: return K_DOWN;
2598 case K_XLEFT: return K_LEFT;
2599 case K_XRIGHT: return K_RIGHT;
2600 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002601 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002602 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002603 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002604 case K_XF1: return K_F1;
2605 case K_XF2: return K_F2;
2606 case K_XF3: return K_F3;
2607 case K_XF4: return K_F4;
2608 case K_S_XF1: return K_S_F1;
2609 case K_S_XF2: return K_S_F2;
2610 case K_S_XF3: return K_S_F3;
2611 case K_S_XF4: return K_S_F4;
2612 }
2613 return key;
2614}
2615
2616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * Return a string which contains the name of the given key when the given
2618 * modifiers are down.
2619 */
2620 char_u *
2621get_special_key_name(c, modifiers)
2622 int c;
2623 int modifiers;
2624{
2625 static char_u string[MAX_KEY_NAME_LEN + 1];
2626
2627 int i, idx;
2628 int table_idx;
2629 char_u *s;
2630
2631 string[0] = '<';
2632 idx = 1;
2633
2634 /* Key that stands for a normal character. */
2635 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2636 c = KEY2TERMCAP1(c);
2637
2638 /*
2639 * Translate shifted special keys into unshifted keys and set modifier.
2640 * Same for CTRL and ALT modifiers.
2641 */
2642 if (IS_SPECIAL(c))
2643 {
2644 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2645 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2646 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2647 {
2648 modifiers |= modifier_keys_table[i];
2649 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2650 modifier_keys_table[i + 4]);
2651 break;
2652 }
2653 }
2654
2655 /* try to find the key in the special key table */
2656 table_idx = find_special_key_in_table(c);
2657
2658 /*
2659 * When not a known special key, and not a printable character, try to
2660 * extract modifiers.
2661 */
2662 if (c > 0
2663#ifdef FEAT_MBYTE
2664 && (*mb_char2len)(c) == 1
2665#endif
2666 )
2667 {
2668 if (table_idx < 0
2669 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2670 && (c & 0x80))
2671 {
2672 c &= 0x7f;
2673 modifiers |= MOD_MASK_ALT;
2674 /* try again, to find the un-alted key in the special key table */
2675 table_idx = find_special_key_in_table(c);
2676 }
2677 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2678 {
2679#ifdef EBCDIC
2680 c = CtrlChar(c);
2681#else
2682 c += '@';
2683#endif
2684 modifiers |= MOD_MASK_CTRL;
2685 }
2686 }
2687
2688 /* translate the modifier into a string */
2689 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2690 if ((modifiers & mod_mask_table[i].mod_mask)
2691 == mod_mask_table[i].mod_flag)
2692 {
2693 string[idx++] = mod_mask_table[i].name;
2694 string[idx++] = (char_u)'-';
2695 }
2696
2697 if (table_idx < 0) /* unknown special key, may output t_xx */
2698 {
2699 if (IS_SPECIAL(c))
2700 {
2701 string[idx++] = 't';
2702 string[idx++] = '_';
2703 string[idx++] = KEY2TERMCAP0(c);
2704 string[idx++] = KEY2TERMCAP1(c);
2705 }
2706 /* Not a special key, only modifiers, output directly */
2707 else
2708 {
2709#ifdef FEAT_MBYTE
2710 if (has_mbyte && (*mb_char2len)(c) > 1)
2711 idx += (*mb_char2bytes)(c, string + idx);
2712 else
2713#endif
2714 if (vim_isprintc(c))
2715 string[idx++] = c;
2716 else
2717 {
2718 s = transchar(c);
2719 while (*s)
2720 string[idx++] = *s++;
2721 }
2722 }
2723 }
2724 else /* use name of special key */
2725 {
2726 STRCPY(string + idx, key_names_table[table_idx].name);
2727 idx = (int)STRLEN(string);
2728 }
2729 string[idx++] = '>';
2730 string[idx] = NUL;
2731 return string;
2732}
2733
2734/*
2735 * Try translating a <> name at (*srcp)[] to dst[].
2736 * Return the number of characters added to dst[], zero for no match.
2737 * If there is a match, srcp is advanced to after the <> name.
2738 * dst[] must be big enough to hold the result (up to six characters)!
2739 */
2740 int
2741trans_special(srcp, dst, keycode)
2742 char_u **srcp;
2743 char_u *dst;
2744 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2745{
2746 int modifiers = 0;
2747 int key;
2748 int dlen = 0;
2749
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002750 key = find_special_key(srcp, &modifiers, keycode, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (key == 0)
2752 return 0;
2753
2754 /* Put the appropriate modifier in a string */
2755 if (modifiers != 0)
2756 {
2757 dst[dlen++] = K_SPECIAL;
2758 dst[dlen++] = KS_MODIFIER;
2759 dst[dlen++] = modifiers;
2760 }
2761
2762 if (IS_SPECIAL(key))
2763 {
2764 dst[dlen++] = K_SPECIAL;
2765 dst[dlen++] = KEY2TERMCAP0(key);
2766 dst[dlen++] = KEY2TERMCAP1(key);
2767 }
2768#ifdef FEAT_MBYTE
2769 else if (has_mbyte && !keycode)
2770 dlen += (*mb_char2bytes)(key, dst + dlen);
2771#endif
2772 else if (keycode)
2773 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2774 else
2775 dst[dlen++] = key;
2776
2777 return dlen;
2778}
2779
2780/*
2781 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2782 * srcp is advanced to after the <> name.
2783 * returns 0 if there is no match.
2784 */
2785 int
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002786find_special_key(srcp, modp, keycode, keep_x_key)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 char_u **srcp;
2788 int *modp;
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002789 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2790 int keep_x_key; /* don't translate xHome to Home key */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 char_u *last_dash;
2793 char_u *end_of_name;
2794 char_u *src;
2795 char_u *bp;
2796 int modifiers;
2797 int bit;
2798 int key;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002799 unsigned long n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002800 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 src = *srcp;
2803 if (src[0] != '<')
2804 return 0;
2805
2806 /* Find end of modifier list */
2807 last_dash = src;
2808 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2809 {
2810 if (*bp == '-')
2811 {
2812 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002813 if (bp[1] != NUL)
2814 {
2815#ifdef FEAT_MBYTE
2816 if (has_mbyte)
2817 l = mb_ptr2len(bp + 1);
2818 else
2819#endif
2820 l = 1;
2821 if (bp[l + 1] == '>')
2822 bp += l; /* anything accepted, like <C-?> */
2823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2826 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002827 else if (STRNICMP(bp, "char-", 5) == 0)
2828 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002829 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002830 bp += l + 5;
2831 break;
2832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
2834
2835 if (*bp == '>') /* found matching '>' */
2836 {
2837 end_of_name = bp + 1;
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 /* Which modifiers are given? */
2840 modifiers = 0x0;
2841 for (bp = src + 1; bp < last_dash; bp++)
2842 {
2843 if (*bp != '-')
2844 {
2845 bit = name_to_mod_mask(*bp);
2846 if (bit == 0x0)
2847 break; /* Illegal modifier name */
2848 modifiers |= bit;
2849 }
2850 }
2851
2852 /*
2853 * Legal modifier name.
2854 */
2855 if (bp >= last_dash)
2856 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002857 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2858 && VIM_ISDIGIT(last_dash[6]))
2859 {
2860 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002861 vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002862 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002865 {
Bram Moolenaar792826c2011-08-19 22:29:02 +02002866 /*
2867 * Modifier with single letter, or special key name.
2868 */
2869#ifdef FEAT_MBYTE
2870 if (has_mbyte)
2871 l = mb_ptr2len(last_dash + 1);
2872 else
2873#endif
2874 l = 1;
2875 if (modifiers != 0 && last_dash[l + 1] == '>')
2876 key = PTR2CHAR(last_dash + 1);
2877 else
2878 {
2879 key = get_special_key_code(last_dash + 1);
2880 if (!keep_x_key)
2881 key = handle_x_keys(key);
2882 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
2885 /*
2886 * get_special_key_code() may return NUL for invalid
2887 * special key name.
2888 */
2889 if (key != NUL)
2890 {
2891 /*
2892 * Only use a modifier when there is no special key code that
2893 * includes the modifier.
2894 */
2895 key = simplify_key(key, &modifiers);
2896
2897 if (!keycode)
2898 {
2899 /* don't want keycode, use single byte code */
2900 if (key == K_BS)
2901 key = BS;
2902 else if (key == K_DEL || key == K_KDEL)
2903 key = DEL;
2904 }
2905
2906 /*
2907 * Normal Key with modifier: Try to make a single byte code.
2908 */
2909 if (!IS_SPECIAL(key))
2910 key = extract_modifiers(key, &modifiers);
2911
2912 *modp = modifiers;
2913 *srcp = end_of_name;
2914 return key;
2915 }
2916 }
2917 }
2918 return 0;
2919}
2920
2921/*
2922 * Try to include modifiers in the key.
2923 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2924 */
2925 int
2926extract_modifiers(key, modp)
2927 int key;
2928 int *modp;
2929{
2930 int modifiers = *modp;
2931
2932#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002933 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 if (!(modifiers & MOD_MASK_CMD))
2935#endif
2936 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2937 {
2938 key = TOUPPER_ASC(key);
2939 modifiers &= ~MOD_MASK_SHIFT;
2940 }
2941 if ((modifiers & MOD_MASK_CTRL)
2942#ifdef EBCDIC
2943 /* * TODO: EBCDIC Better use:
2944 * && (Ctrl_chr(key) || key == '?')
2945 * ??? */
2946 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2947 != NULL
2948#else
2949 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2950#endif
2951 )
2952 {
2953 key = Ctrl_chr(key);
2954 modifiers &= ~MOD_MASK_CTRL;
2955 /* <C-@> is <Nul> */
2956 if (key == 0)
2957 key = K_ZERO;
2958 }
2959#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002960 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (!(modifiers & MOD_MASK_CMD))
2962#endif
2963 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2964#ifdef FEAT_MBYTE
2965 && !enc_dbcs /* avoid creating a lead byte */
2966#endif
2967 )
2968 {
2969 key |= 0x80;
2970 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2971 }
2972
2973 *modp = modifiers;
2974 return key;
2975}
2976
2977/*
2978 * Try to find key "c" in the special key table.
2979 * Return the index when found, -1 when not found.
2980 */
2981 int
2982find_special_key_in_table(c)
2983 int c;
2984{
2985 int i;
2986
2987 for (i = 0; key_names_table[i].name != NULL; i++)
2988 if (c == key_names_table[i].key)
2989 break;
2990 if (key_names_table[i].name == NULL)
2991 i = -1;
2992 return i;
2993}
2994
2995/*
2996 * Find the special key with the given name (the given string does not have to
2997 * end with NUL, the name is assumed to end before the first non-idchar).
2998 * If the name starts with "t_" the next two characters are interpreted as a
2999 * termcap name.
3000 * Return the key code, or 0 if not found.
3001 */
3002 int
3003get_special_key_code(name)
3004 char_u *name;
3005{
3006 char_u *table_name;
3007 char_u string[3];
3008 int i, j;
3009
3010 /*
3011 * If it's <t_xx> we get the code for xx from the termcap
3012 */
3013 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
3014 {
3015 string[0] = name[2];
3016 string[1] = name[3];
3017 string[2] = NUL;
3018 if (add_termcap_entry(string, FALSE) == OK)
3019 return TERMCAP2KEY(name[2], name[3]);
3020 }
3021 else
3022 for (i = 0; key_names_table[i].name != NULL; i++)
3023 {
3024 table_name = key_names_table[i].name;
3025 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3026 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3027 break;
3028 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3029 return key_names_table[i].key;
3030 }
3031 return 0;
3032}
3033
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003034#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 char_u *
3036get_key_name(i)
3037 int i;
3038{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003039 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 return NULL;
3041 return key_names_table[i].name;
3042}
3043#endif
3044
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003045#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046/*
3047 * Look up the given mouse code to return the relevant information in the other
3048 * arguments. Return which button is down or was released.
3049 */
3050 int
3051get_mouse_button(code, is_click, is_drag)
3052 int code;
3053 int *is_click;
3054 int *is_drag;
3055{
3056 int i;
3057
3058 for (i = 0; mouse_table[i].pseudo_code; i++)
3059 if (code == mouse_table[i].pseudo_code)
3060 {
3061 *is_click = mouse_table[i].is_click;
3062 *is_drag = mouse_table[i].is_drag;
3063 return mouse_table[i].button;
3064 }
3065 return 0; /* Shouldn't get here */
3066}
3067
3068/*
3069 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3070 * the given information about which mouse button is down, and whether the
3071 * mouse was clicked, dragged or released.
3072 */
3073 int
3074get_pseudo_mouse_code(button, is_click, is_drag)
3075 int button; /* eg MOUSE_LEFT */
3076 int is_click;
3077 int is_drag;
3078{
3079 int i;
3080
3081 for (i = 0; mouse_table[i].pseudo_code; i++)
3082 if (button == mouse_table[i].button
3083 && is_click == mouse_table[i].is_click
3084 && is_drag == mouse_table[i].is_drag)
3085 {
3086#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003087 /* Trick: a non mappable left click and release has mouse_col -1
3088 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3089 * gui_mouse_moved() */
3090 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003092 if (mouse_col < 0)
3093 mouse_col = 0;
3094 else
3095 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3097 return (int)KE_LEFTMOUSE_NM;
3098 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3099 return (int)KE_LEFTRELEASE_NM;
3100 }
3101#endif
3102 return mouse_table[i].pseudo_code;
3103 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003104 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
3106#endif /* FEAT_MOUSE */
3107
3108/*
3109 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3110 */
3111 int
3112get_fileformat(buf)
3113 buf_T *buf;
3114{
3115 int c = *buf->b_p_ff;
3116
3117 if (buf->b_p_bin || c == 'u')
3118 return EOL_UNIX;
3119 if (c == 'm')
3120 return EOL_MAC;
3121 return EOL_DOS;
3122}
3123
3124/*
3125 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3126 * argument.
3127 */
3128 int
3129get_fileformat_force(buf, eap)
3130 buf_T *buf;
3131 exarg_T *eap; /* can be NULL! */
3132{
3133 int c;
3134
3135 if (eap != NULL && eap->force_ff != 0)
3136 c = eap->cmd[eap->force_ff];
3137 else
3138 {
3139 if ((eap != NULL && eap->force_bin != 0)
3140 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3141 return EOL_UNIX;
3142 c = *buf->b_p_ff;
3143 }
3144 if (c == 'u')
3145 return EOL_UNIX;
3146 if (c == 'm')
3147 return EOL_MAC;
3148 return EOL_DOS;
3149}
3150
3151/*
3152 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3153 * Sets both 'textmode' and 'fileformat'.
3154 * Note: Does _not_ set global value of 'textmode'!
3155 */
3156 void
3157set_fileformat(t, opt_flags)
3158 int t;
3159 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */
3160{
3161 char *p = NULL;
3162
3163 switch (t)
3164 {
3165 case EOL_DOS:
3166 p = FF_DOS;
3167 curbuf->b_p_tx = TRUE;
3168 break;
3169 case EOL_UNIX:
3170 p = FF_UNIX;
3171 curbuf->b_p_tx = FALSE;
3172 break;
3173 case EOL_MAC:
3174 p = FF_MAC;
3175 curbuf->b_p_tx = FALSE;
3176 break;
3177 }
3178 if (p != NULL)
3179 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003180 OPT_FREE | opt_flags, 0);
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00003183 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003185 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#endif
3187#ifdef FEAT_TITLE
3188 need_maketitle = TRUE; /* set window title later */
3189#endif
3190}
3191
3192/*
3193 * Return the default fileformat from 'fileformats'.
3194 */
3195 int
3196default_fileformat()
3197{
3198 switch (*p_ffs)
3199 {
3200 case 'm': return EOL_MAC;
3201 case 'd': return EOL_DOS;
3202 }
3203 return EOL_UNIX;
3204}
3205
3206/*
3207 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3208 */
3209 int
3210call_shell(cmd, opt)
3211 char_u *cmd;
3212 int opt;
3213{
3214 char_u *ncmd;
3215 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003216#ifdef FEAT_PROFILE
3217 proftime_T wait_time;
3218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 if (p_verbose > 3)
3221 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003222 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003223 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 cmd == NULL ? p_sh : cmd);
3225 out_char('\n');
3226 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003227 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229
Bram Moolenaar05159a02005-02-26 23:04:13 +00003230#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003231 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003232 prof_child_enter(&wait_time);
3233#endif
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (*p_sh == NUL)
3236 {
3237 EMSG(_(e_shellempty));
3238 retval = -1;
3239 }
3240 else
3241 {
3242#ifdef FEAT_GUI_MSWIN
3243 /* Don't hide the pointer while executing a shell command. */
3244 gui_mch_mousehide(FALSE);
3245#endif
3246#ifdef FEAT_GUI
3247 ++hold_gui_events;
3248#endif
3249 /* The external command may update a tags file, clear cached tags. */
3250 tag_freematch();
3251
3252 if (cmd == NULL || *p_sxq == NUL)
3253 retval = mch_call_shell(cmd, opt);
3254 else
3255 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003256 char_u *ecmd = cmd;
3257
3258 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3259 {
3260 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3261 if (ecmd == NULL)
3262 ecmd = cmd;
3263 }
3264 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (ncmd != NULL)
3266 {
3267 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003268 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003269 /* When 'shellxquote' is ( append ).
3270 * When 'shellxquote' is "( append )". */
3271 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3272 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3273 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 retval = mch_call_shell(ncmd, opt);
3275 vim_free(ncmd);
3276 }
3277 else
3278 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003279 if (ecmd != cmd)
3280 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282#ifdef FEAT_GUI
3283 --hold_gui_events;
3284#endif
3285 /*
3286 * Check the window size, in case it changed while executing the
3287 * external command.
3288 */
3289 shell_resized_check();
3290 }
3291
3292#ifdef FEAT_EVAL
3293 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003294# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003295 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003296 prof_child_exit(&wait_time);
3297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298#endif
3299
3300 return retval;
3301}
3302
3303/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003304 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3305 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 */
3307 int
3308get_real_state()
3309{
3310 if (State & NORMAL)
3311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003313 {
3314 if (VIsual_select)
3315 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003317 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003318 else if (finish_op)
3319 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321 return State;
3322}
3323
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003324#if defined(FEAT_MBYTE) || defined(PROTO)
3325/*
3326 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003327 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003328 * "b" must point to the start of the file name
3329 */
3330 int
3331after_pathsep(b, p)
3332 char_u *b;
3333 char_u *p;
3334{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003335 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003336 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3337}
3338#endif
3339
3340/*
3341 * Return TRUE if file names "f1" and "f2" are in the same directory.
3342 * "f1" may be a short name, "f2" must be a full path.
3343 */
3344 int
3345same_directory(f1, f2)
3346 char_u *f1;
3347 char_u *f2;
3348{
3349 char_u ffname[MAXPATHL];
3350 char_u *t1;
3351 char_u *t2;
3352
3353 /* safety check */
3354 if (f1 == NULL || f2 == NULL)
3355 return FALSE;
3356
3357 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3358 t1 = gettail_sep(ffname);
3359 t2 = gettail_sep(f2);
3360 return (t1 - ffname == t2 - f2
3361 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3362}
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00003365 || ((defined(FEAT_GUI_GTK)) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00003366 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3368 || defined(PROTO)
3369/*
3370 * Change to a file's directory.
3371 * Caller must call shorten_fnames()!
3372 * Return OK or FAIL.
3373 */
3374 int
3375vim_chdirfile(fname)
3376 char_u *fname;
3377{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003378 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003380 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003381 *gettail_sep(dir) = NUL;
3382 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384#endif
3385
3386#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3387/*
3388 * Check if "name" ends in a slash and is not a directory.
3389 * Used for systems where stat() ignores a trailing slash on a file name.
3390 * The Vim code assumes a trailing slash is only ignored for a directory.
3391 */
3392 int
3393illegal_slash(name)
3394 char *name;
3395{
3396 if (name[0] == NUL)
3397 return FALSE; /* no file name is not illegal */
3398 if (name[strlen(name) - 1] != '/')
3399 return FALSE; /* no trailing slash */
3400 if (mch_isdir((char_u *)name))
3401 return FALSE; /* trailing slash for a directory */
3402 return TRUE;
3403}
3404#endif
3405
3406#if defined(CURSOR_SHAPE) || defined(PROTO)
3407
3408/*
3409 * Handling of cursor and mouse pointer shapes in various modes.
3410 */
3411
3412cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3413{
3414 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3415 * defaults when Vim starts.
3416 * Adjust the SHAPE_IDX_ defines when making changes! */
3417 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3418 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3419 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3420 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3421 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3422 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3423 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3424 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3425 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3426 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3427 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3428 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3429 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3430 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3431 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3432 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3433 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3434};
3435
3436#ifdef FEAT_MOUSESHAPE
3437/*
3438 * Table with names for mouse shapes. Keep in sync with all the tables for
3439 * mch_set_mouse_shape()!.
3440 */
3441static char * mshape_names[] =
3442{
3443 "arrow", /* default, must be the first one */
3444 "blank", /* hidden */
3445 "beam",
3446 "updown",
3447 "udsizing",
3448 "leftright",
3449 "lrsizing",
3450 "busy",
3451 "no",
3452 "crosshair",
3453 "hand1",
3454 "hand2",
3455 "pencil",
3456 "question",
3457 "rightup-arrow",
3458 "up-arrow",
3459 NULL
3460};
3461#endif
3462
3463/*
3464 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3465 * ("what" is SHAPE_MOUSE).
3466 * Returns error message for an illegal option, NULL otherwise.
3467 */
3468 char_u *
3469parse_shape_opt(what)
3470 int what;
3471{
3472 char_u *modep;
3473 char_u *colonp;
3474 char_u *commap;
3475 char_u *slashp;
3476 char_u *p, *endp;
3477 int idx = 0; /* init for GCC */
3478 int all_idx;
3479 int len;
3480 int i;
3481 long n;
3482 int found_ve = FALSE; /* found "ve" flag */
3483 int round;
3484
3485 /*
3486 * First round: check for errors; second round: do it for real.
3487 */
3488 for (round = 1; round <= 2; ++round)
3489 {
3490 /*
3491 * Repeat for all comma separated parts.
3492 */
3493#ifdef FEAT_MOUSESHAPE
3494 if (what == SHAPE_MOUSE)
3495 modep = p_mouseshape;
3496 else
3497#endif
3498 modep = p_guicursor;
3499 while (*modep != NUL)
3500 {
3501 colonp = vim_strchr(modep, ':');
3502 if (colonp == NULL)
3503 return (char_u *)N_("E545: Missing colon");
3504 if (colonp == modep)
3505 return (char_u *)N_("E546: Illegal mode");
3506 commap = vim_strchr(modep, ',');
3507
3508 /*
3509 * Repeat for all mode's before the colon.
3510 * For the 'a' mode, we loop to handle all the modes.
3511 */
3512 all_idx = -1;
3513 while (modep < colonp || all_idx >= 0)
3514 {
3515 if (all_idx < 0)
3516 {
3517 /* Find the mode. */
3518 if (modep[1] == '-' || modep[1] == ':')
3519 len = 1;
3520 else
3521 len = 2;
3522 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3523 all_idx = SHAPE_IDX_COUNT - 1;
3524 else
3525 {
3526 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3527 if (STRNICMP(modep, shape_table[idx].name, len)
3528 == 0)
3529 break;
3530 if (idx == SHAPE_IDX_COUNT
3531 || (shape_table[idx].used_for & what) == 0)
3532 return (char_u *)N_("E546: Illegal mode");
3533 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3534 found_ve = TRUE;
3535 }
3536 modep += len + 1;
3537 }
3538
3539 if (all_idx >= 0)
3540 idx = all_idx--;
3541 else if (round == 2)
3542 {
3543#ifdef FEAT_MOUSESHAPE
3544 if (what == SHAPE_MOUSE)
3545 {
3546 /* Set the default, for the missing parts */
3547 shape_table[idx].mshape = 0;
3548 }
3549 else
3550#endif
3551 {
3552 /* Set the defaults, for the missing parts */
3553 shape_table[idx].shape = SHAPE_BLOCK;
3554 shape_table[idx].blinkwait = 700L;
3555 shape_table[idx].blinkon = 400L;
3556 shape_table[idx].blinkoff = 250L;
3557 }
3558 }
3559
3560 /* Parse the part after the colon */
3561 for (p = colonp + 1; *p && *p != ','; )
3562 {
3563#ifdef FEAT_MOUSESHAPE
3564 if (what == SHAPE_MOUSE)
3565 {
3566 for (i = 0; ; ++i)
3567 {
3568 if (mshape_names[i] == NULL)
3569 {
3570 if (!VIM_ISDIGIT(*p))
3571 return (char_u *)N_("E547: Illegal mouseshape");
3572 if (round == 2)
3573 shape_table[idx].mshape =
3574 getdigits(&p) + MSHAPE_NUMBERED;
3575 else
3576 (void)getdigits(&p);
3577 break;
3578 }
3579 len = (int)STRLEN(mshape_names[i]);
3580 if (STRNICMP(p, mshape_names[i], len) == 0)
3581 {
3582 if (round == 2)
3583 shape_table[idx].mshape = i;
3584 p += len;
3585 break;
3586 }
3587 }
3588 }
3589 else /* if (what == SHAPE_MOUSE) */
3590#endif
3591 {
3592 /*
3593 * First handle the ones with a number argument.
3594 */
3595 i = *p;
3596 len = 0;
3597 if (STRNICMP(p, "ver", 3) == 0)
3598 len = 3;
3599 else if (STRNICMP(p, "hor", 3) == 0)
3600 len = 3;
3601 else if (STRNICMP(p, "blinkwait", 9) == 0)
3602 len = 9;
3603 else if (STRNICMP(p, "blinkon", 7) == 0)
3604 len = 7;
3605 else if (STRNICMP(p, "blinkoff", 8) == 0)
3606 len = 8;
3607 if (len != 0)
3608 {
3609 p += len;
3610 if (!VIM_ISDIGIT(*p))
3611 return (char_u *)N_("E548: digit expected");
3612 n = getdigits(&p);
3613 if (len == 3) /* "ver" or "hor" */
3614 {
3615 if (n == 0)
3616 return (char_u *)N_("E549: Illegal percentage");
3617 if (round == 2)
3618 {
3619 if (TOLOWER_ASC(i) == 'v')
3620 shape_table[idx].shape = SHAPE_VER;
3621 else
3622 shape_table[idx].shape = SHAPE_HOR;
3623 shape_table[idx].percentage = n;
3624 }
3625 }
3626 else if (round == 2)
3627 {
3628 if (len == 9)
3629 shape_table[idx].blinkwait = n;
3630 else if (len == 7)
3631 shape_table[idx].blinkon = n;
3632 else
3633 shape_table[idx].blinkoff = n;
3634 }
3635 }
3636 else if (STRNICMP(p, "block", 5) == 0)
3637 {
3638 if (round == 2)
3639 shape_table[idx].shape = SHAPE_BLOCK;
3640 p += 5;
3641 }
3642 else /* must be a highlight group name then */
3643 {
3644 endp = vim_strchr(p, '-');
3645 if (commap == NULL) /* last part */
3646 {
3647 if (endp == NULL)
3648 endp = p + STRLEN(p); /* find end of part */
3649 }
3650 else if (endp > commap || endp == NULL)
3651 endp = commap;
3652 slashp = vim_strchr(p, '/');
3653 if (slashp != NULL && slashp < endp)
3654 {
3655 /* "group/langmap_group" */
3656 i = syn_check_group(p, (int)(slashp - p));
3657 p = slashp + 1;
3658 }
3659 if (round == 2)
3660 {
3661 shape_table[idx].id = syn_check_group(p,
3662 (int)(endp - p));
3663 shape_table[idx].id_lm = shape_table[idx].id;
3664 if (slashp != NULL && slashp < endp)
3665 shape_table[idx].id = i;
3666 }
3667 p = endp;
3668 }
3669 } /* if (what != SHAPE_MOUSE) */
3670
3671 if (*p == '-')
3672 ++p;
3673 }
3674 }
3675 modep = p;
3676 if (*modep == ',')
3677 ++modep;
3678 }
3679 }
3680
3681 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3682 if (!found_ve)
3683 {
3684#ifdef FEAT_MOUSESHAPE
3685 if (what == SHAPE_MOUSE)
3686 {
3687 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3688 }
3689 else
3690#endif
3691 {
3692 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3693 shape_table[SHAPE_IDX_VE].percentage =
3694 shape_table[SHAPE_IDX_V].percentage;
3695 shape_table[SHAPE_IDX_VE].blinkwait =
3696 shape_table[SHAPE_IDX_V].blinkwait;
3697 shape_table[SHAPE_IDX_VE].blinkon =
3698 shape_table[SHAPE_IDX_V].blinkon;
3699 shape_table[SHAPE_IDX_VE].blinkoff =
3700 shape_table[SHAPE_IDX_V].blinkoff;
3701 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3702 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3703 }
3704 }
3705
3706 return NULL;
3707}
3708
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003709# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3710 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711/*
3712 * Return the index into shape_table[] for the current mode.
3713 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3714 */
3715 int
3716get_shape_idx(mouse)
3717 int mouse;
3718{
3719#ifdef FEAT_MOUSESHAPE
3720 if (mouse && (State == HITRETURN || State == ASKMORE))
3721 {
3722# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003723 int x, y;
3724 gui_mch_getmouse(&x, &y);
3725 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 return SHAPE_IDX_MOREL;
3727# endif
3728 return SHAPE_IDX_MORE;
3729 }
3730 if (mouse && drag_status_line)
3731 return SHAPE_IDX_SDRAG;
3732# ifdef FEAT_VERTSPLIT
3733 if (mouse && drag_sep_line)
3734 return SHAPE_IDX_VDRAG;
3735# endif
3736#endif
3737 if (!mouse && State == SHOWMATCH)
3738 return SHAPE_IDX_SM;
3739#ifdef FEAT_VREPLACE
3740 if (State & VREPLACE_FLAG)
3741 return SHAPE_IDX_R;
3742#endif
3743 if (State & REPLACE_FLAG)
3744 return SHAPE_IDX_R;
3745 if (State & INSERT)
3746 return SHAPE_IDX_I;
3747 if (State & CMDLINE)
3748 {
3749 if (cmdline_at_end())
3750 return SHAPE_IDX_C;
3751 if (cmdline_overstrike())
3752 return SHAPE_IDX_CR;
3753 return SHAPE_IDX_CI;
3754 }
3755 if (finish_op)
3756 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (VIsual_active)
3758 {
3759 if (*p_sel == 'e')
3760 return SHAPE_IDX_VE;
3761 else
3762 return SHAPE_IDX_V;
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return SHAPE_IDX_N;
3765}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
3768# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3769static int old_mouse_shape = 0;
3770
3771/*
3772 * Set the mouse shape:
3773 * If "shape" is -1, use shape depending on the current mode,
3774 * depending on the current state.
3775 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3776 * when the mouse moves off the status or command line).
3777 */
3778 void
3779update_mouseshape(shape_idx)
3780 int shape_idx;
3781{
3782 int new_mouse_shape;
3783
3784 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003785 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return;
3787
3788 /* Postpone the updating when more is to come. Speeds up executing of
3789 * mappings. */
3790 if (shape_idx == -1 && char_avail())
3791 {
3792 postponed_mouseshape = TRUE;
3793 return;
3794 }
3795
Bram Moolenaar14716812006-05-04 21:54:08 +00003796 /* When ignoring the mouse don't change shape on the statusline. */
3797 if (*p_mouse == NUL
3798 && (shape_idx == SHAPE_IDX_CLINE
3799 || shape_idx == SHAPE_IDX_STATUS
3800 || shape_idx == SHAPE_IDX_VSEP))
3801 shape_idx = -2;
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (shape_idx == -2
3804 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3805 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3806 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3807 return;
3808 if (shape_idx < 0)
3809 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3810 else
3811 new_mouse_shape = shape_table[shape_idx].mshape;
3812 if (new_mouse_shape != old_mouse_shape)
3813 {
3814 mch_set_mouse_shape(new_mouse_shape);
3815 old_mouse_shape = new_mouse_shape;
3816 }
3817 postponed_mouseshape = FALSE;
3818}
3819# endif
3820
3821#endif /* CURSOR_SHAPE */
3822
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824/* TODO: make some #ifdef for this */
3825/*--------[ file searching ]-------------------------------------------------*/
3826/*
3827 * File searching functions for 'path', 'tags' and 'cdpath' options.
3828 * External visible functions:
3829 * vim_findfile_init() creates/initialises the search context
3830 * vim_findfile_free_visited() free list of visited files/dirs of search
3831 * context
3832 * vim_findfile() find a file in the search context
3833 * vim_findfile_cleanup() cleanup/free search context created by
3834 * vim_findfile_init()
3835 *
3836 * All static functions and variables start with 'ff_'
3837 *
3838 * In general it works like this:
3839 * First you create yourself a search context by calling vim_findfile_init().
3840 * It is possible to give a search context from a previous call to
3841 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3842 * until you are satisfied with the result or it returns NULL. On every call it
3843 * returns the next file which matches the conditions given to
3844 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3845 *
3846 * It is possible to call vim_findfile_init() again to reinitialise your search
3847 * with some new parameters. Don't forget to pass your old search context to
3848 * it, so it can reuse it and especially reuse the list of already visited
3849 * directories. If you want to delete the list of already visited directories
3850 * simply call vim_findfile_free_visited().
3851 *
3852 * When you are done call vim_findfile_cleanup() to free the search context.
3853 *
3854 * The function vim_findfile_init() has a long comment, which describes the
3855 * needed parameters.
3856 *
3857 *
3858 *
3859 * ATTENTION:
3860 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003861 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 * thread-safe!!!!!
3863 *
3864 * To minimize parameter passing (or because I'm to lazy), only the
3865 * external visible functions get a search context as a parameter. This is
3866 * then assigned to a static global, which is used throughout the local
3867 * functions.
3868 */
3869
3870/*
3871 * type for the directory search stack
3872 */
3873typedef struct ff_stack
3874{
3875 struct ff_stack *ffs_prev;
3876
3877 /* the fix part (no wildcards) and the part containing the wildcards
3878 * of the search path
3879 */
3880 char_u *ffs_fix_path;
3881#ifdef FEAT_PATH_EXTRA
3882 char_u *ffs_wc_path;
3883#endif
3884
3885 /* files/dirs found in the above directory, matched by the first wildcard
3886 * of wc_part
3887 */
3888 char_u **ffs_filearray;
3889 int ffs_filearray_size;
3890 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3891
3892 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003893 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 int ffs_stage;
3897
3898 /* How deep are we in the directory tree?
3899 * Counts backward from value of level parameter to vim_findfile_init
3900 */
3901 int ffs_level;
3902
3903 /* Did we already expand '**' to an empty string? */
3904 int ffs_star_star_empty;
3905} ff_stack_T;
3906
3907/*
3908 * type for already visited directories or files.
3909 */
3910typedef struct ff_visited
3911{
3912 struct ff_visited *ffv_next;
3913
3914#ifdef FEAT_PATH_EXTRA
3915 /* Visited directories are different if the wildcard string are
3916 * different. So we have to save it.
3917 */
3918 char_u *ffv_wc_path;
3919#endif
3920 /* for unix use inode etc for comparison (needed because of links), else
3921 * use filename.
3922 */
3923#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003924 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3925 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 ino_t ffv_ino; /* inode number */
3927#endif
3928 /* The memory for this struct is allocated according to the length of
3929 * ffv_fname.
3930 */
3931 char_u ffv_fname[1]; /* actually longer */
3932} ff_visited_T;
3933
3934/*
3935 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003936 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3938 * So we have to do 3 searches:
3939 * 1) search from the current files directory downward for the file "tags"
3940 * 2) search from the current files directory downward for the file "TAGS"
3941 * 3) search from Vims current directory downwards for the file "tags"
3942 * As you can see, the first and the third search are for the same file, so for
3943 * the third search we can use the visited list of the first search. For the
3944 * second search we must start from a empty visited list.
3945 * The struct ff_visited_list_hdr is used to manage a linked list of already
3946 * visited lists.
3947 */
3948typedef struct ff_visited_list_hdr
3949{
3950 struct ff_visited_list_hdr *ffvl_next;
3951
3952 /* the filename the attached visited list is for */
3953 char_u *ffvl_filename;
3954
3955 ff_visited_T *ffvl_visited_list;
3956
3957} ff_visited_list_hdr_T;
3958
3959
3960/*
3961 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003962 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 */
3964#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966/*
3967 * The search context:
3968 * ffsc_stack_ptr: the stack for the dirs to search
3969 * ffsc_visited_list: the currently active visited list
3970 * ffsc_dir_visited_list: the currently active visited list for search dirs
3971 * ffsc_visited_lists_list: the list of all visited lists
3972 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3973 * ffsc_file_to_search: the file to search for
3974 * ffsc_start_dir: the starting directory, if search path was relative
3975 * ffsc_fix_path: the fix part of the given path (without wildcards)
3976 * Needed for upward search.
3977 * ffsc_wc_path: the part of the given path containing wildcards
3978 * ffsc_level: how many levels of dirs to search downwards
3979 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003980 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003981 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 */
3983typedef struct ff_search_ctx_T
3984{
3985 ff_stack_T *ffsc_stack_ptr;
3986 ff_visited_list_hdr_T *ffsc_visited_list;
3987 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3988 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3989 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3990 char_u *ffsc_file_to_search;
3991 char_u *ffsc_start_dir;
3992 char_u *ffsc_fix_path;
3993#ifdef FEAT_PATH_EXTRA
3994 char_u *ffsc_wc_path;
3995 int ffsc_level;
3996 char_u **ffsc_stopdirs_v;
3997#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003998 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003999 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004000} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002/* locally needed functions */
4003#ifdef FEAT_PATH_EXTRA
4004static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *));
4005#else
4006static int ff_check_visited __ARGS((ff_visited_T **, char_u *));
4007#endif
4008static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp));
4009static void ff_free_visited_list __ARGS((ff_visited_T *vl));
4010static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp));
4011#ifdef FEAT_PATH_EXTRA
4012static int ff_wc_equal __ARGS((char_u *s1, char_u *s2));
4013#endif
4014
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004015static void ff_push __ARGS((ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr));
4016static ff_stack_T *ff_pop __ARGS((ff_search_ctx_T *search_ctx));
4017static void ff_clear __ARGS((ff_search_ctx_T *search_ctx));
4018static void ff_free_stack_element __ARGS((ff_stack_T *stack_ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#ifdef FEAT_PATH_EXTRA
4020static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int));
4021#else
4022static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int));
4023#endif
4024#ifdef FEAT_PATH_EXTRA
4025static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **));
4026#endif
4027
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004028static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#if 0
4031/*
4032 * if someone likes findfirst/findnext, here are the functions
4033 * NOT TESTED!!
4034 */
4035
4036static void *ff_fn_search_context = NULL;
4037
4038 char_u *
4039vim_findfirst(path, filename, level)
4040 char_u *path;
4041 char_u *filename;
4042 int level;
4043{
4044 ff_fn_search_context =
4045 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4046 ff_fn_search_context, rel_fname);
4047 if (NULL == ff_fn_search_context)
4048 return NULL;
4049 else
4050 return vim_findnext()
4051}
4052
4053 char_u *
4054vim_findnext()
4055{
4056 char_u *ret = vim_findfile(ff_fn_search_context);
4057
4058 if (NULL == ret)
4059 {
4060 vim_findfile_cleanup(ff_fn_search_context);
4061 ff_fn_search_context = NULL;
4062 }
4063 return ret;
4064}
4065#endif
4066
4067/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004068 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004070 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 *
4072 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4073 * with the search context.
4074 *
4075 * Find the file 'filename' in the directory 'path'.
4076 * The parameter 'path' may contain wildcards. If so only search 'level'
4077 * directories deep. The parameter 'level' is the absolute maximum and is
4078 * not related to restricts given to the '**' wildcard. If 'level' is 100
4079 * and you use '**200' vim_findfile() will stop after 100 levels.
4080 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004081 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4082 * escape special characters.
4083 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4085 * restarted on the next higher directory level. This is repeated until the
4086 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4087 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4088 *
4089 * If the 'path' is relative, the starting dir for the search is either VIM's
4090 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004091 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * the first wildcard.
4093 *
4094 * Upward search is only done on the starting dir.
4095 *
4096 * If 'free_visited' is TRUE the list of already visited files/directories is
4097 * cleared. Set this to FALSE if you just want to search from another
4098 * directory, but want to be sure that no directory from a previous search is
4099 * searched again. This is useful if you search for a file at different places.
4100 * The list of visited files/dirs can also be cleared with the function
4101 * vim_findfile_free_visited().
4102 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004103 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4104 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 *
4106 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004107 * passed in the parameter "search_ctx_arg". This context is reused and
4108 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004110 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4111 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004113 * If you don't have a search context from a previous call "search_ctx_arg"
4114 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 *
4116 * This function silently ignores a few errors, vim_findfile() will have
4117 * limited functionality then.
4118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 void *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004120vim_findfile_init(path, filename, stopdirs, level, free_visited, find_what,
4121 search_ctx_arg, tagfile, rel_fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 char_u *path;
4123 char_u *filename;
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00004124 char_u *stopdirs UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 int level;
4126 int free_visited;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004127 int find_what;
4128 void *search_ctx_arg;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004129 int tagfile; /* expanding names of tags files */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 char_u *rel_fname; /* file name to use for "." */
4131{
4132#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004133 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004135 ff_stack_T *sptr;
4136 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 /* If a search context is given by the caller, reuse it, else allocate a
4139 * new one.
4140 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004141 if (search_ctx_arg != NULL)
4142 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 else
4144 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004145 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4146 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004148 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004150 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004151 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004154 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /* clear visited list if wanted */
4157 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004158 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 else
4160 {
4161 /* Reuse old visited lists. Get the visited list for the given
4162 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004163 * one. */
4164 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4165 &search_ctx->ffsc_visited_lists_list);
4166 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004168 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4169 &search_ctx->ffsc_dir_visited_lists_list);
4170 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 goto error_return;
4172 }
4173
4174 if (ff_expand_buffer == NULL)
4175 {
4176 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4177 if (ff_expand_buffer == NULL)
4178 goto error_return;
4179 }
4180
4181 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004182 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (path[0] == '.'
4184 && (vim_ispathsep(path[1]) || path[1] == NUL)
4185 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4186 && rel_fname != NULL)
4187 {
4188 int len = (int)(gettail(rel_fname) - rel_fname);
4189
4190 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4191 {
4192 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004193 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004194 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004197 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4198 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 goto error_return;
4200 if (*++path != NUL)
4201 ++path;
4202 }
4203 else if (*path == NUL || !vim_isAbsName(path))
4204 {
4205#ifdef BACKSLASH_IN_FILENAME
4206 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4207 if (*path != NUL && path[1] == ':')
4208 {
4209 char_u drive[3];
4210
4211 drive[0] = path[0];
4212 drive[1] = ':';
4213 drive[2] = NUL;
4214 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4215 goto error_return;
4216 path += 2;
4217 }
4218 else
4219#endif
4220 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4221 goto error_return;
4222
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004223 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4224 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 goto error_return;
4226
4227#ifdef BACKSLASH_IN_FILENAME
4228 /* A path that starts with "/dir" is relative to the drive, not to the
4229 * directory (but not for "//machine/dir"). Only use the drive name. */
4230 if ((*path == '/' || *path == '\\')
4231 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004232 && search_ctx->ffsc_start_dir[1] == ':')
4233 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234#endif
4235 }
4236
4237#ifdef FEAT_PATH_EXTRA
4238 /*
4239 * If stopdirs are given, split them into an array of pointers.
4240 * If this fails (mem allocation), there is no upward search at all or a
4241 * stop directory is not recognized -> continue silently.
4242 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004243 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 * is handled as unlimited upward search. See function
4245 * ff_path_in_stoplist() for details.
4246 */
4247 if (stopdirs != NULL)
4248 {
4249 char_u *walker = stopdirs;
4250 int dircount;
4251
4252 while (*walker == ';')
4253 walker++;
4254
4255 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004256 search_ctx->ffsc_stopdirs_v =
4257 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004259 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
4261 do
4262 {
4263 char_u *helper;
4264 void *ptr;
4265
4266 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004267 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 (dircount + 1) * sizeof(char_u *));
4269 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004270 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 else
4272 /* ignore, keep what we have and continue */
4273 break;
4274 walker = vim_strchr(walker, ';');
4275 if (walker)
4276 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004277 search_ctx->ffsc_stopdirs_v[dircount-1] =
4278 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 walker++;
4280 }
4281 else
4282 /* this might be "", which means ascent till top
4283 * of directory tree.
4284 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004285 search_ctx->ffsc_stopdirs_v[dircount-1] =
4286 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 dircount++;
4289
4290 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004291 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 }
4294#endif
4295
4296#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004297 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 /* split into:
4300 * -fix path
4301 * -wildcard_stuff (might be NULL)
4302 */
4303 wc_part = vim_strchr(path, '*');
4304 if (wc_part != NULL)
4305 {
4306 int llevel;
4307 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004308 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004311 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /*
4314 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004315 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4317 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4318 * For EBCDIC you get different character values.
4319 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004320 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 * string.
4322 */
4323 len = 0;
4324 while (*wc_part != NUL)
4325 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004326 if (len + 5 >= MAXPATHL)
4327 {
4328 EMSG(_(e_pathtoolong));
4329 break;
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if (STRNCMP(wc_part, "**", 2) == 0)
4332 {
4333 ff_expand_buffer[len++] = *wc_part++;
4334 ff_expand_buffer[len++] = *wc_part++;
4335
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004336 llevel = strtol((char *)wc_part, &errpt, 10);
4337 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004339 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /* restrict is 0 -> remove already added '**' */
4341 len -= 2;
4342 else
4343 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004344 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004345 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
4347 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4348 goto error_return;
4349 }
4350 }
4351 else
4352 ff_expand_buffer[len++] = *wc_part++;
4353 }
4354 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004355 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004357 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 goto error_return;
4359 }
4360 else
4361#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004362 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004364 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 {
4366 /* store the fix part as startdir.
4367 * This is needed if the parameter path is fully qualified.
4368 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004369 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004370 if (search_ctx->ffsc_start_dir == NULL)
4371 goto error_return;
4372 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374
4375 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004376 if (STRLEN(search_ctx->ffsc_start_dir)
4377 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4378 {
4379 EMSG(_(e_pathtoolong));
4380 goto error_return;
4381 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004382 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004384 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004385 int eb_len = (int)STRLEN(ff_expand_buffer);
4386 char_u *buf = alloc(eb_len
4387 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004388
4389 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004390 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004391 if (mch_isdir(buf))
4392 {
4393 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4394 add_pathsep(ff_expand_buffer);
4395 }
4396#ifdef FEAT_PATH_EXTRA
4397 else
4398 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004399 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004400 char_u *wc_path = NULL;
4401 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004402 int len = 0;
4403
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004404 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004405 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004406 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004407 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4408 add_pathsep(ff_expand_buffer);
4409 }
4410 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004411 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004412
4413 if (search_ctx->ffsc_wc_path != NULL)
4414 {
4415 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004416 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004417 + STRLEN(search_ctx->ffsc_fix_path + len)
4418 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004419 if (temp == NULL || wc_path == NULL)
4420 {
4421 vim_free(buf);
4422 vim_free(temp);
4423 vim_free(wc_path);
4424 goto error_return;
4425 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004426
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004427 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4428 STRCAT(temp, search_ctx->ffsc_wc_path);
4429 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004430 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004431 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004432 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004433 }
4434#endif
4435 vim_free(buf);
4436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 sptr = ff_create_stack_element(ff_expand_buffer,
4439#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004440 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441#endif
4442 level, 0);
4443
4444 if (sptr == NULL)
4445 goto error_return;
4446
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004447 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004449 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4450 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 goto error_return;
4452
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004453 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
4455error_return:
4456 /*
4457 * We clear the search context now!
4458 * Even when the caller gave us a (perhaps valid) context we free it here,
4459 * as we might have already destroyed it.
4460 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004461 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 return NULL;
4463}
4464
4465#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4466/*
4467 * Get the stopdir string. Check that ';' is not escaped.
4468 */
4469 char_u *
4470vim_findfile_stopdir(buf)
4471 char_u *buf;
4472{
4473 char_u *r_ptr = buf;
4474
4475 while (*r_ptr != NUL && *r_ptr != ';')
4476 {
4477 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4478 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004479 /* Overwrite the escape char,
4480 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004481 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 r_ptr++;
4483 }
4484 r_ptr++;
4485 }
4486 if (*r_ptr == ';')
4487 {
4488 *r_ptr = 0;
4489 r_ptr++;
4490 }
4491 else if (*r_ptr == NUL)
4492 r_ptr = NULL;
4493 return r_ptr;
4494}
4495#endif
4496
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004497/*
4498 * Clean up the given search context. Can handle a NULL pointer.
4499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 void
4501vim_findfile_cleanup(ctx)
4502 void *ctx;
4503{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004504 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 return;
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004508 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510}
4511
4512/*
4513 * Find a file in a search context.
4514 * The search context was created with vim_findfile_init() above.
4515 * Return a pointer to an allocated file name or NULL if nothing found.
4516 * To get all matching files call this function until you get NULL.
4517 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004518 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 *
4520 * The search algorithm is depth first. To change this replace the
4521 * stack with a list (don't forget to leave partly searched directories on the
4522 * top of the list).
4523 */
4524 char_u *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004525vim_findfile(search_ctx_arg)
4526 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
4528 char_u *file_path;
4529#ifdef FEAT_PATH_EXTRA
4530 char_u *rest_of_wildcards;
4531 char_u *path_end = NULL;
4532#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004533 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4535 int len;
4536#endif
4537 int i;
4538 char_u *p;
4539#ifdef FEAT_SEARCHPATH
4540 char_u *suf;
4541#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004542 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004544 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 return NULL;
4546
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004547 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 /*
4550 * filepath is used as buffer for various actions and as the storage to
4551 * return a found filename.
4552 */
4553 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4554 return NULL;
4555
4556#ifdef FEAT_PATH_EXTRA
4557 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004558 if (search_ctx->ffsc_start_dir != NULL)
4559 path_end = &search_ctx->ffsc_start_dir[
4560 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561#endif
4562
4563#ifdef FEAT_PATH_EXTRA
4564 /* upward search loop */
4565 for (;;)
4566 {
4567#endif
4568 /* downward search loop */
4569 for (;;)
4570 {
4571 /* check if user user wants to stop the search*/
4572 ui_breakcheck();
4573 if (got_int)
4574 break;
4575
4576 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004577 stackp = ff_pop(search_ctx);
4578 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 break;
4580
4581 /*
4582 * TODO: decide if we leave this test in
4583 *
4584 * GOOD: don't search a directory(-tree) twice.
4585 * BAD: - check linked list for every new directory entered.
4586 * - check for double files also done below
4587 *
4588 * Here we check if we already searched this directory.
4589 * We already searched a directory if:
4590 * 1) The directory is the same.
4591 * 2) We would use the same wildcard string.
4592 *
4593 * Good if you have links on same directory via several ways
4594 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4595 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4596 *
4597 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004598 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004600 if (stackp->ffs_filearray == NULL
4601 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004603 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004605 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#endif
4607 ) == FAIL)
4608 {
4609#ifdef FF_VERBOSE
4610 if (p_verbose >= 5)
4611 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004612 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004614 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 /* don't overwrite this either */
4616 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004617 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004620 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 continue;
4622 }
4623#ifdef FF_VERBOSE
4624 else if (p_verbose >= 5)
4625 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004626 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004627 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004628 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 /* don't overwrite this either */
4630 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004631 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633#endif
4634
4635 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004636 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004638 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 continue;
4640 }
4641
4642 file_path[0] = NUL;
4643
4644 /*
4645 * If no filearray till now expand wildcards
4646 * The function expand_wildcards() can handle an array of paths
4647 * and all possible expands are returned in one array. We use this
4648 * to handle the expansion of '**' into an empty string.
4649 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004650 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
4652 char_u *dirptrs[2];
4653
4654 /* we use filepath to build the path expand_wildcards() should
4655 * expand.
4656 */
4657 dirptrs[0] = file_path;
4658 dirptrs[1] = NULL;
4659
4660 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004661 if (!vim_isAbsName(stackp->ffs_fix_path)
4662 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004664 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 add_pathsep(file_path);
4666 }
4667
4668 /* append the fix part of the search path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004669 STRCAT(file_path, stackp->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 add_pathsep(file_path);
4671
4672#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004673 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (*rest_of_wildcards != NUL)
4675 {
4676 len = (int)STRLEN(file_path);
4677 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4678 {
4679 /* pointer to the restrict byte
4680 * The restrict byte is not a character!
4681 */
4682 p = rest_of_wildcards + 2;
4683
4684 if (*p > 0)
4685 {
4686 (*p)--;
4687 file_path[len++] = '*';
4688 }
4689
4690 if (*p == 0)
4691 {
4692 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004693 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 else
4696 rest_of_wildcards += 3;
4697
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004698 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
4700 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004701 stackp->ffs_star_star_empty = 1;
4702 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 }
4705
4706 /*
4707 * Here we copy until the next path separator or the end of
4708 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004709 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 * pushing every directory returned from expand_wildcards()
4711 * on the stack again for further search.
4712 */
4713 while (*rest_of_wildcards
4714 && !vim_ispathsep(*rest_of_wildcards))
4715 file_path[len++] = *rest_of_wildcards++;
4716
4717 file_path[len] = NUL;
4718 if (vim_ispathsep(*rest_of_wildcards))
4719 rest_of_wildcards++;
4720 }
4721#endif
4722
4723 /*
4724 * Expand wildcards like "*" and "$VAR".
4725 * If the path is a URL don't try this.
4726 */
4727 if (path_with_url(dirptrs[0]))
4728 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004729 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004731 if (stackp->ffs_filearray != NULL
4732 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004734 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004736 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
4738 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004739 /* Add EW_NOTWILD because the expanded path may contain
4740 * wildcard characters that are to be taken literally.
4741 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004743 &stackp->ffs_filearray_size,
4744 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004745 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004747 stackp->ffs_filearray_cur = 0;
4748 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750#ifdef FEAT_PATH_EXTRA
4751 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004752 rest_of_wildcards = &stackp->ffs_wc_path[
4753 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#endif
4755
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004756 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 /* this is the first time we work on this directory */
4759#ifdef FEAT_PATH_EXTRA
4760 if (*rest_of_wildcards == NUL)
4761#endif
4762 {
4763 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004764 * We don't have further wildcards to expand, so we have to
4765 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004767 for (i = stackp->ffs_filearray_cur;
4768 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004770 if (!path_with_url(stackp->ffs_filearray[i])
4771 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 continue; /* not a directory */
4773
Bram Moolenaar21160b92009-11-11 15:56:10 +00004774 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 * below */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004776 STRCPY(file_path, stackp->ffs_filearray[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004778 STRCAT(file_path, search_ctx->ffsc_file_to_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 /*
4781 * Try without extra suffix and then with suffixes
4782 * from 'suffixesadd'.
4783 */
4784#ifdef FEAT_SEARCHPATH
4785 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004786 if (search_ctx->ffsc_tagfile)
4787 suf = (char_u *)"";
4788 else
4789 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 for (;;)
4791#endif
4792 {
4793 /* if file exists and we didn't already find it */
4794 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004795 || (mch_getperm(file_path) >= 0
4796 && (search_ctx->ffsc_find_what
4797 == FINDFILE_BOTH
4798 || ((search_ctx->ffsc_find_what
4799 == FINDFILE_DIR)
4800 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801#ifndef FF_VERBOSE
4802 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004803 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 file_path
4805#ifdef FEAT_PATH_EXTRA
4806 , (char_u *)""
4807#endif
4808 ) == OK)
4809#endif
4810 )
4811 {
4812#ifdef FF_VERBOSE
4813 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004814 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 file_path
4816#ifdef FEAT_PATH_EXTRA
4817 , (char_u *)""
4818#endif
4819 ) == FAIL)
4820 {
4821 if (p_verbose >= 5)
4822 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004823 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004824 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 file_path);
4826 /* don't overwrite this either */
4827 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004828 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 continue;
4831 }
4832#endif
4833
4834 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004835 stackp->ffs_filearray_cur = i + 1;
4836 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004838 if (!path_with_url(file_path))
4839 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4841 == OK)
4842 {
4843 p = shorten_fname(file_path,
4844 ff_expand_buffer);
4845 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004846 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848#ifdef FF_VERBOSE
4849 if (p_verbose >= 5)
4850 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004851 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004852 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 /* don't overwrite this either */
4854 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004855 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857#endif
4858 return file_path;
4859 }
4860
4861#ifdef FEAT_SEARCHPATH
4862 /* Not found or found already, try next suffix. */
4863 if (*suf == NUL)
4864 break;
4865 copy_option_part(&suf, file_path + len,
4866 MAXPATHL - len, ",");
4867#endif
4868 }
4869 }
4870 }
4871#ifdef FEAT_PATH_EXTRA
4872 else
4873 {
4874 /*
4875 * still wildcards left, push the directories for further
4876 * search
4877 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004878 for (i = stackp->ffs_filearray_cur;
4879 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004881 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 continue; /* not a directory */
4883
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004884 ff_push(search_ctx,
4885 ff_create_stack_element(
4886 stackp->ffs_filearray[i],
4887 rest_of_wildcards,
4888 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 }
4891#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004892 stackp->ffs_filearray_cur = 0;
4893 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895
4896#ifdef FEAT_PATH_EXTRA
4897 /*
4898 * if wildcards contains '**' we have to descent till we reach the
4899 * leaves of the directory tree.
4900 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004901 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004903 for (i = stackp->ffs_filearray_cur;
4904 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004906 if (fnamecmp(stackp->ffs_filearray[i],
4907 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004909 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004911 ff_push(search_ctx,
4912 ff_create_stack_element(stackp->ffs_filearray[i],
4913 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 }
4916#endif
4917
4918 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004919 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 }
4922
4923#ifdef FEAT_PATH_EXTRA
4924 /* If we reached this, we didn't find anything downwards.
4925 * Let's check if we should do an upward search.
4926 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004927 if (search_ctx->ffsc_start_dir
4928 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
4930 ff_stack_T *sptr;
4931
4932 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004933 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4934 (int)(path_end - search_ctx->ffsc_start_dir),
4935 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937
4938 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004939 while (path_end > search_ctx->ffsc_start_dir
4940 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004942 while (path_end > search_ctx->ffsc_start_dir
4943 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 path_end--;
4945 *path_end = 0;
4946 path_end--;
4947
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004948 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 break;
4950
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004951 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004953 STRCAT(file_path, search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
4955 /* create a new stack entry */
4956 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004957 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (sptr == NULL)
4959 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004960 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962 else
4963 break;
4964 }
4965#endif
4966
4967 vim_free(file_path);
4968 return NULL;
4969}
4970
4971/*
4972 * Free the list of lists of visited files and directories
4973 * Can handle it if the passed search_context is NULL;
4974 */
4975 void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004976vim_findfile_free_visited(search_ctx_arg)
4977 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004979 ff_search_ctx_T *search_ctx;
4980
4981 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 return;
4983
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004984 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
4985 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
4986 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987}
4988
4989 static void
4990vim_findfile_free_visited_list(list_headp)
4991 ff_visited_list_hdr_T **list_headp;
4992{
4993 ff_visited_list_hdr_T *vp;
4994
4995 while (*list_headp != NULL)
4996 {
4997 vp = (*list_headp)->ffvl_next;
4998 ff_free_visited_list((*list_headp)->ffvl_visited_list);
4999
5000 vim_free((*list_headp)->ffvl_filename);
5001 vim_free(*list_headp);
5002 *list_headp = vp;
5003 }
5004 *list_headp = NULL;
5005}
5006
5007 static void
5008ff_free_visited_list(vl)
5009 ff_visited_T *vl;
5010{
5011 ff_visited_T *vp;
5012
5013 while (vl != NULL)
5014 {
5015 vp = vl->ffv_next;
5016#ifdef FEAT_PATH_EXTRA
5017 vim_free(vl->ffv_wc_path);
5018#endif
5019 vim_free(vl);
5020 vl = vp;
5021 }
5022 vl = NULL;
5023}
5024
5025/*
5026 * Returns the already visited list for the given filename. If none is found it
5027 * allocates a new one.
5028 */
5029 static ff_visited_list_hdr_T*
5030ff_get_visited_list(filename, list_headp)
5031 char_u *filename;
5032 ff_visited_list_hdr_T **list_headp;
5033{
5034 ff_visited_list_hdr_T *retptr = NULL;
5035
5036 /* check if a visited list for the given filename exists */
5037 if (*list_headp != NULL)
5038 {
5039 retptr = *list_headp;
5040 while (retptr != NULL)
5041 {
5042 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5043 {
5044#ifdef FF_VERBOSE
5045 if (p_verbose >= 5)
5046 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005047 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005048 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 filename);
5050 /* don't overwrite this either */
5051 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005052 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054#endif
5055 return retptr;
5056 }
5057 retptr = retptr->ffvl_next;
5058 }
5059 }
5060
5061#ifdef FF_VERBOSE
5062 if (p_verbose >= 5)
5063 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005064 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005065 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 /* don't overwrite this either */
5067 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005068 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070#endif
5071
5072 /*
5073 * if we reach this we didn't find a list and we have to allocate new list
5074 */
5075 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5076 if (retptr == NULL)
5077 return NULL;
5078
5079 retptr->ffvl_visited_list = NULL;
5080 retptr->ffvl_filename = vim_strsave(filename);
5081 if (retptr->ffvl_filename == NULL)
5082 {
5083 vim_free(retptr);
5084 return NULL;
5085 }
5086 retptr->ffvl_next = *list_headp;
5087 *list_headp = retptr;
5088
5089 return retptr;
5090}
5091
5092#ifdef FEAT_PATH_EXTRA
5093/*
5094 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5095 * They are equal if:
5096 * - both paths are NULL
5097 * - they have the same length
5098 * - char by char comparison is OK
5099 * - the only differences are in the counters behind a '**', so
5100 * '**\20' is equal to '**\24'
5101 */
5102 static int
5103ff_wc_equal(s1, s2)
5104 char_u *s1;
5105 char_u *s2;
5106{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005107 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005108 int c1 = NUL;
5109 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005110 int prev1 = NUL;
5111 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113 if (s1 == s2)
5114 return TRUE;
5115
5116 if (s1 == NULL || s2 == NULL)
5117 return FALSE;
5118
Bram Moolenaard43f0952015-08-27 22:30:47 +02005119 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005121 c1 = PTR2CHAR(s1 + i);
5122 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005123
5124 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5125 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005126 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005127 prev2 = prev1;
5128 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005129
5130 i += MB_PTR2LEN(s1 + i);
5131 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005133 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134}
5135#endif
5136
5137/*
5138 * maintains the list of already visited files and dirs
5139 * returns FAIL if the given file/dir is already in the list
5140 * returns OK if it is newly added
5141 *
5142 * TODO: What to do on memory allocation problems?
5143 * -> return TRUE - Better the file is found several times instead of
5144 * never.
5145 */
5146 static int
5147ff_check_visited(visited_list, fname
5148#ifdef FEAT_PATH_EXTRA
5149 , wc_path
5150#endif
5151 )
5152 ff_visited_T **visited_list;
5153 char_u *fname;
5154#ifdef FEAT_PATH_EXTRA
5155 char_u *wc_path;
5156#endif
5157{
5158 ff_visited_T *vp;
5159#ifdef UNIX
5160 struct stat st;
5161 int url = FALSE;
5162#endif
5163
5164 /* For an URL we only compare the name, otherwise we compare the
5165 * device/inode (unix) or the full path name (not Unix). */
5166 if (path_with_url(fname))
5167 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005168 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169#ifdef UNIX
5170 url = TRUE;
5171#endif
5172 }
5173 else
5174 {
5175 ff_expand_buffer[0] = NUL;
5176#ifdef UNIX
5177 if (mch_stat((char *)fname, &st) < 0)
5178#else
5179 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5180#endif
5181 return FAIL;
5182 }
5183
5184 /* check against list of already visited files */
5185 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5186 {
5187 if (
5188#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005189 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5190 && vp->ffv_ino == st.st_ino)
5191 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#endif
5193 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5194 )
5195 {
5196#ifdef FEAT_PATH_EXTRA
5197 /* are the wildcard parts equal */
5198 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5199#endif
5200 /* already visited */
5201 return FAIL;
5202 }
5203 }
5204
5205 /*
5206 * New file/dir. Add it to the list of visited files/dirs.
5207 */
5208 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5209 + STRLEN(ff_expand_buffer)));
5210
5211 if (vp != NULL)
5212 {
5213#ifdef UNIX
5214 if (!url)
5215 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005216 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 vp->ffv_ino = st.st_ino;
5218 vp->ffv_dev = st.st_dev;
5219 vp->ffv_fname[0] = NUL;
5220 }
5221 else
5222 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005223 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#endif
5225 STRCPY(vp->ffv_fname, ff_expand_buffer);
5226#ifdef UNIX
5227 }
5228#endif
5229#ifdef FEAT_PATH_EXTRA
5230 if (wc_path != NULL)
5231 vp->ffv_wc_path = vim_strsave(wc_path);
5232 else
5233 vp->ffv_wc_path = NULL;
5234#endif
5235
5236 vp->ffv_next = *visited_list;
5237 *visited_list = vp;
5238 }
5239
5240 return OK;
5241}
5242
5243/*
5244 * create stack element from given path pieces
5245 */
5246 static ff_stack_T *
5247ff_create_stack_element(fix_part,
5248#ifdef FEAT_PATH_EXTRA
5249 wc_part,
5250#endif
5251 level, star_star_empty)
5252 char_u *fix_part;
5253#ifdef FEAT_PATH_EXTRA
5254 char_u *wc_part;
5255#endif
5256 int level;
5257 int star_star_empty;
5258{
5259 ff_stack_T *new;
5260
5261 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5262 if (new == NULL)
5263 return NULL;
5264
5265 new->ffs_prev = NULL;
5266 new->ffs_filearray = NULL;
5267 new->ffs_filearray_size = 0;
5268 new->ffs_filearray_cur = 0;
5269 new->ffs_stage = 0;
5270 new->ffs_level = level;
5271 new->ffs_star_star_empty = star_star_empty;;
5272
5273 /* the following saves NULL pointer checks in vim_findfile */
5274 if (fix_part == NULL)
5275 fix_part = (char_u *)"";
5276 new->ffs_fix_path = vim_strsave(fix_part);
5277
5278#ifdef FEAT_PATH_EXTRA
5279 if (wc_part == NULL)
5280 wc_part = (char_u *)"";
5281 new->ffs_wc_path = vim_strsave(wc_part);
5282#endif
5283
5284 if (new->ffs_fix_path == NULL
5285#ifdef FEAT_PATH_EXTRA
5286 || new->ffs_wc_path == NULL
5287#endif
5288 )
5289 {
5290 ff_free_stack_element(new);
5291 new = NULL;
5292 }
5293
5294 return new;
5295}
5296
5297/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005298 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 */
5300 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005301ff_push(search_ctx, stack_ptr)
5302 ff_search_ctx_T *search_ctx;
5303 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
5305 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005306 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005307 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005309 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5310 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
5312}
5313
5314/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005315 * Pop a dir from the directory stack.
5316 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 */
5318 static ff_stack_T *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005319ff_pop(search_ctx)
5320 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322 ff_stack_T *sptr;
5323
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005324 sptr = search_ctx->ffsc_stack_ptr;
5325 if (search_ctx->ffsc_stack_ptr != NULL)
5326 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 return sptr;
5329}
5330
5331/*
5332 * free the given stack element
5333 */
5334 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005335ff_free_stack_element(stack_ptr)
5336 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005339 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005341 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342#endif
5343
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005344 if (stack_ptr->ffs_filearray != NULL)
5345 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005347 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348}
5349
5350/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005351 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 */
5353 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005354ff_clear(search_ctx)
5355 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 ff_stack_T *sptr;
5358
5359 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005360 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 ff_free_stack_element(sptr);
5362
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005363 vim_free(search_ctx->ffsc_file_to_search);
5364 vim_free(search_ctx->ffsc_start_dir);
5365 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005367 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369
5370#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005371 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
5373 int i = 0;
5374
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005375 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005377 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 i++;
5379 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005380 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005382 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383#endif
5384
5385 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005386 search_ctx->ffsc_file_to_search = NULL;
5387 search_ctx->ffsc_start_dir = NULL;
5388 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005390 search_ctx->ffsc_wc_path = NULL;
5391 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#endif
5393}
5394
5395#ifdef FEAT_PATH_EXTRA
5396/*
5397 * check if the given path is in the stopdirs
5398 * returns TRUE if yes else FALSE
5399 */
5400 static int
5401ff_path_in_stoplist(path, path_len, stopdirs_v)
5402 char_u *path;
5403 int path_len;
5404 char_u **stopdirs_v;
5405{
5406 int i = 0;
5407
5408 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005409 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 path_len--;
5411
5412 /* if no path consider it as match */
5413 if (path_len == 0)
5414 return TRUE;
5415
5416 for (i = 0; stopdirs_v[i] != NULL; i++)
5417 {
5418 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5419 {
5420 /* match for parent directory. So '/home' also matches
5421 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5422 * '/home/r' would also match '/home/rks'
5423 */
5424 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005425 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return TRUE;
5427 }
5428 else
5429 {
5430 if (fnamecmp(stopdirs_v[i], path) == 0)
5431 return TRUE;
5432 }
5433 }
5434 return FALSE;
5435}
5436#endif
5437
5438#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5439/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005440 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 *
5442 * On the first call set the parameter 'first' to TRUE to initialize
5443 * the search. For repeating calls to FALSE.
5444 *
5445 * Repeating calls will return other files called 'ptr[len]' from the path.
5446 *
5447 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5448 * don't need valid values.
5449 *
5450 * If nothing found on the first call the option FNAME_MESS will issue the
5451 * message:
5452 * 'Can't find file "<file>" in path'
5453 * On repeating calls:
5454 * 'No more file "<file>" found in path'
5455 *
5456 * options:
5457 * FNAME_MESS give error message when not found
5458 *
5459 * Uses NameBuff[]!
5460 *
5461 * Returns an allocated string for the file name. NULL for error.
5462 *
5463 */
5464 char_u *
5465find_file_in_path(ptr, len, options, first, rel_fname)
5466 char_u *ptr; /* file name */
5467 int len; /* length of file name */
5468 int options;
5469 int first; /* use count'th matching file name */
5470 char_u *rel_fname; /* file name searching relative to */
5471{
5472 return find_file_in_path_option(ptr, len, options, first,
5473 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005474 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475}
5476
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005477static char_u *ff_file_to_find = NULL;
5478static void *fdip_search_ctx = NULL;
5479
5480#if defined(EXITFREE)
5481 static void
5482free_findfile()
5483{
5484 vim_free(ff_file_to_find);
5485 vim_findfile_cleanup(fdip_search_ctx);
5486}
5487#endif
5488
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489/*
5490 * Find the directory name "ptr[len]" in the path.
5491 *
5492 * options:
5493 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005494 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 *
5496 * Uses NameBuff[]!
5497 *
5498 * Returns an allocated string for the file name. NULL for error.
5499 */
5500 char_u *
5501find_directory_in_path(ptr, len, options, rel_fname)
5502 char_u *ptr; /* file name */
5503 int len; /* length of file name */
5504 int options;
5505 char_u *rel_fname; /* file name searching relative to */
5506{
5507 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005508 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509}
5510
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005511 char_u *
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005512find_file_in_path_option(ptr, len, options, first, path_option,
5513 find_what, rel_fname, suffixes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 char_u *ptr; /* file name */
5515 int len; /* length of file name */
5516 int options;
5517 int first; /* use count'th matching file name */
5518 char_u *path_option; /* p_path or p_cdpath */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005519 int find_what; /* FINDFILE_FILE, _DIR or _BOTH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 char_u *rel_fname; /* file name we are looking relative to. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005521 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 static int did_findfile_init = FALSE;
5525 char_u save_char;
5526 char_u *file_name = NULL;
5527 char_u *buf = NULL;
5528 int rel_to_curdir;
5529#ifdef AMIGA
5530 struct Process *proc = (struct Process *)FindTask(0L);
5531 APTR save_winptr = proc->pr_WindowPtr;
5532
5533 /* Avoid a requester here for a volume that doesn't exist. */
5534 proc->pr_WindowPtr = (APTR)-1L;
5535#endif
5536
5537 if (first == TRUE)
5538 {
5539 /* copy file name into NameBuff, expanding environment variables */
5540 save_char = ptr[len];
5541 ptr[len] = NUL;
5542 expand_env(ptr, NameBuff, MAXPATHL);
5543 ptr[len] = save_char;
5544
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005545 vim_free(ff_file_to_find);
5546 ff_file_to_find = vim_strsave(NameBuff);
5547 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
5549 file_name = NULL;
5550 goto theend;
5551 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005552 if (options & FNAME_UNESC)
5553 {
5554 /* Change all "\ " to " ". */
5555 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5556 if (ptr[0] == '\\' && ptr[1] == ' ')
5557 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005561 rel_to_curdir = (ff_file_to_find[0] == '.'
5562 && (ff_file_to_find[1] == NUL
5563 || vim_ispathsep(ff_file_to_find[1])
5564 || (ff_file_to_find[1] == '.'
5565 && (ff_file_to_find[2] == NUL
5566 || vim_ispathsep(ff_file_to_find[2])))));
5567 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 /* "..", "../path", "." and "./path": don't use the path_option */
5569 || rel_to_curdir
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005570#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005572 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005573 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005574 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575#endif
5576#ifdef AMIGA
5577 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005578 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579#endif
5580 )
5581 {
5582 /*
5583 * Absolute path, no need to use "path_option".
5584 * If this is not a first call, return NULL. We already returned a
5585 * filename on the first call.
5586 */
5587 if (first == TRUE)
5588 {
5589 int l;
5590 int run;
5591
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005592 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005594 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 goto theend;
5596 }
5597
5598 /* When FNAME_REL flag given first use the directory of the file.
5599 * Otherwise or when this fails use the current directory. */
5600 for (run = 1; run <= 2; ++run)
5601 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005602 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 if (run == 1
5604 && rel_to_curdir
5605 && (options & FNAME_REL)
5606 && rel_fname != NULL
5607 && STRLEN(rel_fname) + l < MAXPATHL)
5608 {
5609 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005610 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 l = (int)STRLEN(NameBuff);
5612 }
5613 else
5614 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005615 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 run = 2;
5617 }
5618
5619 /* When the file doesn't exist, try adding parts of
5620 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005621 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 for (;;)
5623 {
5624 if (
5625#ifdef DJGPP
5626 /* "C:" by itself will fail for mch_getperm(),
5627 * assume it's always valid. */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005628 (find_what != FINDFILE_FILE && NameBuff[0] != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 && NameBuff[1] == ':'
5630 && NameBuff[2] == NUL) ||
5631#endif
5632 (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005633 && (find_what == FINDFILE_BOTH
5634 || ((find_what == FINDFILE_DIR)
5635 == mch_isdir(NameBuff)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
5637 file_name = vim_strsave(NameBuff);
5638 goto theend;
5639 }
5640 if (*buf == NUL)
5641 break;
5642 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5643 }
5644 }
5645 }
5646 }
5647 else
5648 {
5649 /*
5650 * Loop over all paths in the 'path' or 'cdpath' option.
5651 * When "first" is set, first setup to the start of the option.
5652 * Otherwise continue to find the next match.
5653 */
5654 if (first == TRUE)
5655 {
5656 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005657 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 dir = path_option;
5659 did_findfile_init = FALSE;
5660 }
5661
5662 for (;;)
5663 {
5664 if (did_findfile_init)
5665 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005666 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 if (file_name != NULL)
5668 break;
5669
5670 did_findfile_init = FALSE;
5671 }
5672 else
5673 {
5674 char_u *r_ptr;
5675
5676 if (dir == NULL || *dir == NUL)
5677 {
5678 /* We searched all paths of the option, now we can
5679 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005680 vim_findfile_cleanup(fdip_search_ctx);
5681 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683 }
5684
5685 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5686 break;
5687
5688 /* copy next path */
5689 buf[0] = 0;
5690 copy_option_part(&dir, buf, MAXPATHL, " ,");
5691
5692#ifdef FEAT_PATH_EXTRA
5693 /* get the stopdir string */
5694 r_ptr = vim_findfile_stopdir(buf);
5695#else
5696 r_ptr = NULL;
5697#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005698 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005699 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005700 fdip_search_ctx, FALSE, rel_fname);
5701 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 did_findfile_init = TRUE;
5703 vim_free(buf);
5704 }
5705 }
5706 }
5707 if (file_name == NULL && (options & FNAME_MESS))
5708 {
5709 if (first == TRUE)
5710 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005711 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005713 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 else
5715 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005716 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 else
5719 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005720 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005722 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 else
5724 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005725 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727 }
5728
5729theend:
5730#ifdef AMIGA
5731 proc->pr_WindowPtr = save_winptr;
5732#endif
5733 return file_name;
5734}
5735
5736#endif /* FEAT_SEARCHPATH */
5737
5738/*
5739 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5740 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5741 */
5742 int
5743vim_chdir(new_dir)
5744 char_u *new_dir;
5745{
5746#ifndef FEAT_SEARCHPATH
5747 return mch_chdir((char *)new_dir);
5748#else
5749 char_u *dir_name;
5750 int r;
5751
5752 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5753 FNAME_MESS, curbuf->b_ffname);
5754 if (dir_name == NULL)
5755 return -1;
5756 r = mch_chdir((char *)dir_name);
5757 vim_free(dir_name);
5758 return r;
5759#endif
5760}
5761
5762/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005763 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005765 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5766 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 * Returns OK or FAIL.
5768 */
5769 int
5770get_user_name(buf, len)
5771 char_u *buf;
5772 int len;
5773{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005774 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 {
5776 if (mch_get_user_name(buf, len) == FAIL)
5777 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005778 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
5780 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005781 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 return OK;
5783}
5784
5785#ifndef HAVE_QSORT
5786/*
5787 * Our own qsort(), for systems that don't have it.
5788 * It's simple and slow. From the K&R C book.
5789 */
5790 void
5791qsort(base, elm_count, elm_size, cmp)
5792 void *base;
5793 size_t elm_count;
5794 size_t elm_size;
5795 int (*cmp) __ARGS((const void *, const void *));
5796{
5797 char_u *buf;
5798 char_u *p1;
5799 char_u *p2;
5800 int i, j;
5801 int gap;
5802
5803 buf = alloc((unsigned)elm_size);
5804 if (buf == NULL)
5805 return;
5806
5807 for (gap = elm_count / 2; gap > 0; gap /= 2)
5808 for (i = gap; i < elm_count; ++i)
5809 for (j = i - gap; j >= 0; j -= gap)
5810 {
5811 /* Compare the elements. */
5812 p1 = (char_u *)base + j * elm_size;
5813 p2 = (char_u *)base + (j + gap) * elm_size;
5814 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5815 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005816 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 mch_memmove(buf, p1, elm_size);
5818 mch_memmove(p1, p2, elm_size);
5819 mch_memmove(p2, buf, elm_size);
5820 }
5821
5822 vim_free(buf);
5823}
5824#endif
5825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826/*
5827 * Sort an array of strings.
5828 */
5829static int
5830#ifdef __BORLANDC__
5831_RTLENTRYF
5832#endif
5833sort_compare __ARGS((const void *s1, const void *s2));
5834
5835 static int
5836#ifdef __BORLANDC__
5837_RTLENTRYF
5838#endif
5839sort_compare(s1, s2)
5840 const void *s1;
5841 const void *s2;
5842{
5843 return STRCMP(*(char **)s1, *(char **)s2);
5844}
5845
5846 void
5847sort_strings(files, count)
5848 char_u **files;
5849 int count;
5850{
5851 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854#if !defined(NO_EXPANDPATH) || defined(PROTO)
5855/*
5856 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005857 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 * Return value like strcmp(p, q), but consider path separators.
5859 */
5860 int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005861pathcmp(p, q, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 const char *p, *q;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005863 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005865 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005866 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005867 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005869 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005871 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005872 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005875 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005877 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 return 0;
5879 s = q;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005880 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 break;
5882 }
5883
5884 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005885 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {
5887 s = p;
5888 break;
5889 }
5890
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005891 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#ifdef BACKSLASH_IN_FILENAME
5893 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005894 && !((c1 == '/' && c2 == '\\')
5895 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896#endif
5897 )
5898 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005899 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005901 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005903 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5904 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005906
5907 i += MB_PTR2LEN((char_u *)p + i);
5908 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005910 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005911 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005913 c1 = PTR2CHAR((char_u *)s + i);
5914 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005916 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005917 && i > 0
5918 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005920 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005922 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005924 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 return 0; /* match with trailing slash */
5926 if (s == q)
5927 return -1; /* no match */
5928 return 1;
5929}
5930#endif
5931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932/*
5933 * The putenv() implementation below comes from the "screen" program.
5934 * Included with permission from Juergen Weigert.
5935 * See pty.c for the copyright notice.
5936 */
5937
5938/*
5939 * putenv -- put value into environment
5940 *
5941 * Usage: i = putenv (string)
5942 * int i;
5943 * char *string;
5944 *
5945 * where string is of the form <name>=<value>.
5946 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5947 *
5948 * Putenv may need to add a new name into the environment, or to
5949 * associate a value longer than the current value with a particular
5950 * name. So, to make life simpler, putenv() copies your entire
5951 * environment into the heap (i.e. malloc()) from the stack
5952 * (i.e. where it resides when your process is initiated) the first
5953 * time you call it.
5954 *
5955 * (history removed, not very interesting. See the "screen" sources.)
5956 */
5957
5958#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5959
5960#define EXTRASIZE 5 /* increment to add to env. size */
5961
5962static int envsize = -1; /* current size of environment */
5963#ifndef MACOS_CLASSIC
5964extern
5965#endif
5966 char **environ; /* the global which is your env. */
5967
5968static int findenv __ARGS((char *name)); /* look for a name in the env. */
5969static int newenv __ARGS((void)); /* copy env. from stack to heap */
5970static int moreenv __ARGS((void)); /* incr. size of env. */
5971
5972 int
5973putenv(string)
5974 const char *string;
5975{
5976 int i;
5977 char *p;
5978
5979 if (envsize < 0)
5980 { /* first time putenv called */
5981 if (newenv() < 0) /* copy env. to heap */
5982 return -1;
5983 }
5984
5985 i = findenv((char *)string); /* look for name in environment */
5986
5987 if (i < 0)
5988 { /* name must be added */
5989 for (i = 0; environ[i]; i++);
5990 if (i >= (envsize - 1))
5991 { /* need new slot */
5992 if (moreenv() < 0)
5993 return -1;
5994 }
5995 p = (char *)alloc((unsigned)(strlen(string) + 1));
5996 if (p == NULL) /* not enough core */
5997 return -1;
5998 environ[i + 1] = 0; /* new end of env. */
5999 }
6000 else
6001 { /* name already in env. */
6002 p = vim_realloc(environ[i], strlen(string) + 1);
6003 if (p == NULL)
6004 return -1;
6005 }
6006 sprintf(p, "%s", string); /* copy into env. */
6007 environ[i] = p;
6008
6009 return 0;
6010}
6011
6012 static int
6013findenv(name)
6014 char *name;
6015{
6016 char *namechar, *envchar;
6017 int i, found;
6018
6019 found = 0;
6020 for (i = 0; environ[i] && !found; i++)
6021 {
6022 envchar = environ[i];
6023 namechar = name;
6024 while (*namechar && *namechar != '=' && (*namechar == *envchar))
6025 {
6026 namechar++;
6027 envchar++;
6028 }
6029 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
6030 }
6031 return found ? i - 1 : -1;
6032}
6033
6034 static int
6035newenv()
6036{
6037 char **env, *elem;
6038 int i, esize;
6039
6040#ifdef MACOS
6041 /* for Mac a new, empty environment is created */
6042 i = 0;
6043#else
6044 for (i = 0; environ[i]; i++)
6045 ;
6046#endif
6047 esize = i + EXTRASIZE + 1;
6048 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6049 if (env == NULL)
6050 return -1;
6051
6052#ifndef MACOS
6053 for (i = 0; environ[i]; i++)
6054 {
6055 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6056 if (elem == NULL)
6057 return -1;
6058 env[i] = elem;
6059 strcpy(elem, environ[i]);
6060 }
6061#endif
6062
6063 env[i] = 0;
6064 environ = env;
6065 envsize = esize;
6066 return 0;
6067}
6068
6069 static int
6070moreenv()
6071{
6072 int esize;
6073 char **env;
6074
6075 esize = envsize + EXTRASIZE;
6076 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6077 if (env == 0)
6078 return -1;
6079 environ = env;
6080 envsize = esize;
6081 return 0;
6082}
6083
6084# ifdef USE_VIMPTY_GETENV
6085 char_u *
6086vimpty_getenv(string)
6087 const char_u *string;
6088{
6089 int i;
6090 char_u *p;
6091
6092 if (envsize < 0)
6093 return NULL;
6094
6095 i = findenv((char *)string);
6096
6097 if (i < 0)
6098 return NULL;
6099
6100 p = vim_strchr((char_u *)environ[i], '=');
6101 return (p + 1);
6102}
6103# endif
6104
6105#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006106
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006107#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006108/*
6109 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6110 * rights to write into.
6111 */
6112 int
6113filewritable(fname)
6114 char_u *fname;
6115{
6116 int retval = 0;
6117#if defined(UNIX) || defined(VMS)
6118 int perm = 0;
6119#endif
6120
6121#if defined(UNIX) || defined(VMS)
6122 perm = mch_getperm(fname);
6123#endif
6124#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6125 if (
6126# ifdef WIN3264
6127 mch_writable(fname) &&
6128# else
6129# if defined(UNIX) || defined(VMS)
6130 (perm & 0222) &&
6131# endif
6132# endif
6133 mch_access((char *)fname, W_OK) == 0
6134 )
6135#endif
6136 {
6137 ++retval;
6138 if (mch_isdir(fname))
6139 ++retval;
6140 }
6141 return retval;
6142}
6143#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006144
6145/*
6146 * Print an error message with one or two "%s" and one or two string arguments.
6147 * This is not in message.c to avoid a warning for prototypes.
6148 */
6149 int
6150emsg3(s, a1, a2)
6151 char_u *s, *a1, *a2;
6152{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006153 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006154 return TRUE; /* no error messages at the moment */
Bram Moolenaar6f192452007-11-08 19:49:02 +00006155#ifdef HAVE_STDARG_H
6156 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
6157#else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006158 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2);
Bram Moolenaar6f192452007-11-08 19:49:02 +00006159#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006160 return emsg(IObuff);
6161}
6162
6163/*
6164 * Print an error message with one "%ld" and one long int argument.
6165 * This is not in message.c to avoid a warning for prototypes.
6166 */
6167 int
6168emsgn(s, n)
6169 char_u *s;
6170 long n;
6171{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006172 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006173 return TRUE; /* no error messages at the moment */
6174 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
6175 return emsg(IObuff);
6176}
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006177
6178#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6179/*
6180 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6181 */
6182 int
6183get2c(fd)
6184 FILE *fd;
6185{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006186 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006187
6188 n = getc(fd);
6189 n = (n << 8) + getc(fd);
6190 return n;
6191}
6192
6193/*
6194 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6195 */
6196 int
6197get3c(fd)
6198 FILE *fd;
6199{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006200 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006201
6202 n = getc(fd);
6203 n = (n << 8) + getc(fd);
6204 n = (n << 8) + getc(fd);
6205 return n;
6206}
6207
6208/*
6209 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6210 */
6211 int
6212get4c(fd)
6213 FILE *fd;
6214{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006215 /* Use unsigned rather than int otherwise result is undefined
6216 * when left-shift sets the MSB. */
6217 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006218
Bram Moolenaar95235e62013-09-08 16:07:07 +02006219 n = (unsigned)getc(fd);
6220 n = (n << 8) + (unsigned)getc(fd);
6221 n = (n << 8) + (unsigned)getc(fd);
6222 n = (n << 8) + (unsigned)getc(fd);
6223 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006224}
6225
6226/*
6227 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
6228 */
6229 time_t
6230get8ctime(fd)
6231 FILE *fd;
6232{
6233 time_t n = 0;
6234 int i;
6235
6236 for (i = 0; i < 8; ++i)
6237 n = (n << 8) + getc(fd);
6238 return n;
6239}
6240
6241/*
6242 * Read a string of length "cnt" from "fd" into allocated memory.
6243 * Returns NULL when out of memory or unable to read that many bytes.
6244 */
6245 char_u *
6246read_string(fd, cnt)
6247 FILE *fd;
6248 int cnt;
6249{
6250 char_u *str;
6251 int i;
6252 int c;
6253
6254 /* allocate memory */
6255 str = alloc((unsigned)cnt + 1);
6256 if (str != NULL)
6257 {
6258 /* Read the string. Quit when running into the EOF. */
6259 for (i = 0; i < cnt; ++i)
6260 {
6261 c = getc(fd);
6262 if (c == EOF)
6263 {
6264 vim_free(str);
6265 return NULL;
6266 }
6267 str[i] = c;
6268 }
6269 str[i] = NUL;
6270 }
6271 return str;
6272}
6273
6274/*
6275 * Write a number to file "fd", MSB first, in "len" bytes.
6276 */
6277 int
6278put_bytes(fd, nr, len)
6279 FILE *fd;
6280 long_u nr;
6281 int len;
6282{
6283 int i;
6284
6285 for (i = len - 1; i >= 0; --i)
6286 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6287 return FAIL;
6288 return OK;
6289}
6290
6291#ifdef _MSC_VER
6292# if (_MSC_VER <= 1200)
6293/* This line is required for VC6 without the service pack. Also see the
6294 * matching #pragma below. */
6295 # pragma optimize("", off)
6296# endif
6297#endif
6298
6299/*
6300 * Write time_t to file "fd" in 8 bytes.
Bram Moolenaar285bf842016-01-07 22:34:01 +01006301 * Returns FAIL when the write failed.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006302 */
Bram Moolenaar285bf842016-01-07 22:34:01 +01006303 int
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006304put_time(fd, the_time)
6305 FILE *fd;
6306 time_t the_time;
6307{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006308 char_u buf[8];
6309
6310 time_to_bytes(the_time, buf);
Bram Moolenaar285bf842016-01-07 22:34:01 +01006311 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006312}
6313
6314/*
6315 * Write time_t to "buf[8]".
6316 */
6317 void
6318time_to_bytes(the_time, buf)
6319 time_t the_time;
6320 char_u *buf;
6321{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006322 int c;
6323 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006324 int bi = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006325 time_t wtime = the_time;
6326
6327 /* time_t can be up to 8 bytes in size, more than long_u, thus we
6328 * can't use put_bytes() here.
6329 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006330 * sign. This happens for large values of wtime. A cast to long_u may
6331 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes,
6332 * it's safe to assume that long_u is 4 bytes or more and when using 8
6333 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006334 for (i = 7; i >= 0; --i)
6335 {
6336 if (i + 1 > (int)sizeof(time_t))
6337 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006338 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006339 else
6340 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006341#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006342 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006343#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006344 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006345#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006346 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006347 }
6348 }
6349}
6350
6351#ifdef _MSC_VER
6352# if (_MSC_VER <= 1200)
6353 # pragma optimize("", on)
6354# endif
6355#endif
6356
6357#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006358
6359#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6360 || defined(FEAT_SPELL) || defined(PROTO)
6361/*
6362 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6363 * When "s" is NULL FALSE is returned.
6364 */
6365 int
6366has_non_ascii(s)
6367 char_u *s;
6368{
6369 char_u *p;
6370
6371 if (s != NULL)
6372 for (p = s; *p != NUL; ++p)
6373 if (*p >= 128)
6374 return TRUE;
6375 return FALSE;
6376}
6377#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006378
6379#if defined(MESSAGE_QUEUE) || defined(PROTO)
6380/*
6381 * Process messages that have been queued for netbeans or clientserver.
6382 * These functions can call arbitrary vimscript and should only be called when
6383 * it is safe to do so.
6384 */
6385 void
6386parse_queued_messages()
6387{
6388# ifdef FEAT_NETBEANS_INTG
6389 /* Process the queued netbeans messages. */
6390 netbeans_parse_messages();
6391# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006392# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006393 /* Process the queued clientserver messages. */
6394 server_parse_messages();
6395# endif
6396}
6397#endif