blob: 3f1568d101e92757e94318efa1d6b73e13fe65ac [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
800/*
801 * Some memory is reserved for error messages and for being able to
802 * call mf_release_all(), which needs some memory for mf_trans_add().
803 */
804#if defined(MSDOS) && !defined(DJGPP)
805# define SMALL_MEM
806# define KEEP_ROOM 8192L
807#else
808# define KEEP_ROOM (2 * 8192L)
809#endif
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200810#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812/*
Bram Moolenaar2a329742008-04-01 12:53:43 +0000813 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 * Use lalloc for larger blocks.
815 */
816 char_u *
817alloc(size)
818 unsigned size;
819{
820 return (lalloc((long_u)size, TRUE));
821}
822
823/*
824 * Allocate memory and set all bytes to zero.
825 */
826 char_u *
827alloc_clear(size)
828 unsigned size;
829{
830 char_u *p;
831
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200832 p = lalloc((long_u)size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (p != NULL)
834 (void)vim_memset(p, 0, (size_t)size);
835 return p;
836}
837
838/*
839 * alloc() with check for maximum line length
840 */
841 char_u *
842alloc_check(size)
843 unsigned size;
844{
845#if !defined(UNIX) && !defined(__EMX__)
846 if (sizeof(int) == 2 && size > 0x7fff)
847 {
848 /* Don't hide this message */
849 emsg_silent = 0;
850 EMSG(_("E340: Line is becoming too long"));
851 return NULL;
852 }
853#endif
854 return (lalloc((long_u)size, TRUE));
855}
856
857/*
858 * Allocate memory like lalloc() and set all bytes to zero.
859 */
860 char_u *
861lalloc_clear(size, message)
862 long_u size;
863 int message;
864{
865 char_u *p;
866
867 p = (lalloc(size, message));
868 if (p != NULL)
869 (void)vim_memset(p, 0, (size_t)size);
870 return p;
871}
872
873/*
874 * Low level memory allocation function.
875 * This is used often, KEEP IT FAST!
876 */
877 char_u *
878lalloc(size, message)
879 long_u size;
880 int message;
881{
882 char_u *p; /* pointer to new storage space */
883 static int releasing = FALSE; /* don't do mf_release_all() recursive */
884 int try_again;
885#if defined(HAVE_AVAIL_MEM) && !defined(SMALL_MEM)
886 static long_u allocated = 0; /* allocated since last avail check */
887#endif
888
889 /* Safety check for allocating zero bytes */
890 if (size == 0)
891 {
892 /* Don't hide this message */
893 emsg_silent = 0;
894 EMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
895 return NULL;
896 }
897
898#ifdef MEM_PROFILE
899 mem_pre_alloc_l(&size);
900#endif
901
902#if defined(MSDOS) && !defined(DJGPP)
903 if (size >= 0xfff0) /* in MSDOS we can't deal with >64K blocks */
904 p = NULL;
905 else
906#endif
907
908 /*
909 * Loop when out of memory: Try to release some memfile blocks and
910 * if some blocks are released call malloc again.
911 */
912 for (;;)
913 {
914 /*
915 * Handle three kind of systems:
916 * 1. No check for available memory: Just return.
917 * 2. Slow check for available memory: call mch_avail_mem() after
918 * allocating KEEP_ROOM amount of memory.
919 * 3. Strict check for available memory: call mch_avail_mem()
920 */
921 if ((p = (char_u *)malloc((size_t)size)) != NULL)
922 {
923#ifndef HAVE_AVAIL_MEM
924 /* 1. No check for available memory: Just return. */
925 goto theend;
926#else
927# ifndef SMALL_MEM
928 /* 2. Slow check for available memory: call mch_avail_mem() after
929 * allocating (KEEP_ROOM / 2) amount of memory. */
930 allocated += size;
931 if (allocated < KEEP_ROOM / 2)
932 goto theend;
933 allocated = 0;
934# endif
935 /* 3. check for available memory: call mch_avail_mem() */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200936 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaar5a221812008-11-12 12:08:45 +0000938 free((char *)p); /* System is low... no go! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 p = NULL;
940 }
941 else
942 goto theend;
943#endif
944 }
945 /*
946 * Remember that mf_release_all() is being called to avoid an endless
947 * loop, because mf_release_all() may call alloc() recursively.
948 */
949 if (releasing)
950 break;
951 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000952
953 clear_sb_text(); /* free any scrollback text */
954 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar661b1822005-07-28 22:36:45 +0000955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 releasing = FALSE;
957 if (!try_again)
958 break;
959 }
960
961 if (message && p == NULL)
962 do_outofmem_msg(size);
963
964theend:
965#ifdef MEM_PROFILE
966 mem_post_alloc((void **)&p, (size_t)size);
967#endif
968 return p;
969}
970
971#if defined(MEM_PROFILE) || defined(PROTO)
972/*
973 * realloc() with memory profiling.
974 */
975 void *
976mem_realloc(ptr, size)
977 void *ptr;
978 size_t size;
979{
980 void *p;
981
982 mem_pre_free(&ptr);
983 mem_pre_alloc_s(&size);
984
985 p = realloc(ptr, size);
986
987 mem_post_alloc(&p, size);
988
989 return p;
990}
991#endif
992
993/*
994* Avoid repeating the error message many times (they take 1 second each).
995* Did_outofmem_msg is reset when a character is read.
996*/
997 void
998do_outofmem_msg(size)
999 long_u size;
1000{
1001 if (!did_outofmem_msg)
1002 {
1003 /* Don't hide this message */
1004 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001005
1006 /* Must come first to avoid coming back here when printing the error
1007 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001009
1010 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012}
1013
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001014#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001015
1016# if defined(FEAT_SEARCHPATH)
1017static void free_findfile __ARGS((void));
1018# endif
1019
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001020/*
1021 * Free everything that we allocated.
1022 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001023 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1024 * surprised if Vim crashes...
1025 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001026 */
1027 void
1028free_all_mem()
1029{
1030 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001031 static int entered = FALSE;
1032
1033 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1034 * Don't try freeing everything again. */
1035 if (entered)
1036 return;
1037 entered = TRUE;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001038
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001039# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001040 /* Don't want to trigger autocommands from here on. */
1041 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001042# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001043
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001044# ifdef FEAT_WINDOWS
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001045 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1046 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001047 if (first_tabpage->tp_next != NULL)
1048 do_cmdline_cmd((char_u *)"tabonly!");
1049 if (firstwin != lastwin)
1050 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001051# endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001052
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001053# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001054 /* Free all spell info. */
1055 spell_free_all();
1056# endif
1057
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001058# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001059 /* Clear user commands (before deleting buffers). */
1060 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001061# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001062
1063# ifdef FEAT_MENU
1064 /* Clear menus. */
1065 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001066# ifdef FEAT_MULTI_LANG
1067 do_cmdline_cmd((char_u *)"menutranslate clear");
1068# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001069# endif
1070
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001071 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001072 do_cmdline_cmd((char_u *)"lmapclear");
1073 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001074 do_cmdline_cmd((char_u *)"mapclear");
1075 do_cmdline_cmd((char_u *)"mapclear!");
1076 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001077# if defined(FEAT_EVAL)
1078 do_cmdline_cmd((char_u *)"breakdel *");
1079# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001080# if defined(FEAT_PROFILE)
1081 do_cmdline_cmd((char_u *)"profdel *");
1082# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001083# if defined(FEAT_KEYMAP)
1084 do_cmdline_cmd((char_u *)"set keymap=");
1085#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001086
1087# ifdef FEAT_TITLE
1088 free_titles();
1089# endif
1090# if defined(FEAT_SEARCHPATH)
1091 free_findfile();
1092# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001093
1094 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001095# if defined(FEAT_AUTOCMD)
1096 free_all_autocmds();
1097# endif
1098 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001099 free_all_options();
1100 free_all_marks();
1101 alist_clear(&global_alist);
1102 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001103# if defined(FEAT_CMDL_COMPL)
1104 free_users();
1105# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001106 free_search_patterns();
1107 free_old_sub();
1108 free_last_insert();
1109 free_prev_shellcmd();
1110 free_regexp_stuff();
1111 free_tag_stuff();
1112 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001113# ifdef FEAT_SIGNS
1114 free_signs();
1115# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001116# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001117 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001118# endif
1119# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001120 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001121# endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001122 clear_sb_text(); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001123
1124 /* Free some global vars. */
1125 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001126# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001127 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001128# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001129 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001130# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001131 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001132# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001133 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001134 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001135
1136 /* Clear cmdline history. */
1137 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001138# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001139 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001140# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001141
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001142#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001143 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001144 win_T *win;
1145 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001146
1147 qf_free_all(NULL);
1148 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001149 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001150 qf_free_all(win);
1151 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001152#endif
1153
1154 /* Close all script inputs. */
1155 close_all_scripts();
1156
1157#if defined(FEAT_WINDOWS)
1158 /* Destroy all windows. Must come before freeing buffers. */
1159 win_free_all();
1160#endif
1161
Bram Moolenaar2a329742008-04-01 12:53:43 +00001162 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1163 * were freed already. */
1164#ifdef FEAT_AUTOCHDIR
1165 p_acd = FALSE;
1166#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001167 for (buf = firstbuf; buf != NULL; )
1168 {
1169 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001170 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001171 if (buf_valid(buf))
1172 buf = nextbuf; /* didn't work, try next one */
1173 else
1174 buf = firstbuf;
1175 }
1176
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001177#ifdef FEAT_ARABIC
1178 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001179#endif
1180
1181 /* Clear registers. */
1182 clear_registers();
1183 ResetRedobuff();
1184 ResetRedobuff();
1185
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001186#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001187 vim_free(serverDelayedStartName);
1188#endif
1189
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001190 /* highlight info */
1191 free_highlight();
1192
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001193 reset_last_sourcing();
1194
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001195#ifdef FEAT_WINDOWS
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001196 free_tabpage(first_tabpage);
1197 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001198#endif
1199
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001200# ifdef UNIX
1201 /* Machine-specific free. */
1202 mch_free_mem();
1203# endif
1204
1205 /* message history */
1206 for (;;)
1207 if (delete_first_msg() == FAIL)
1208 break;
1209
1210# ifdef FEAT_EVAL
1211 eval_clear();
1212# endif
1213
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001214 free_termoptions();
1215
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001216 /* screenlines (can't display anything now!) */
1217 free_screenlines();
1218
1219#if defined(USE_XSMP)
1220 xsmp_close();
1221#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001222#ifdef FEAT_GUI_GTK
1223 gui_mch_free_all();
1224#endif
1225 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001226
1227 vim_free(IObuff);
1228 vim_free(NameBuff);
1229}
1230#endif
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001233 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 */
1235 char_u *
1236vim_strsave(string)
1237 char_u *string;
1238{
1239 char_u *p;
1240 unsigned len;
1241
1242 len = (unsigned)STRLEN(string) + 1;
1243 p = alloc(len);
1244 if (p != NULL)
1245 mch_memmove(p, string, (size_t)len);
1246 return p;
1247}
1248
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001249/*
1250 * Copy up to "len" bytes of "string" into newly allocated memory and
1251 * terminate with a NUL.
1252 * The allocated memory always has size "len + 1", also when "string" is
1253 * shorter.
1254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 char_u *
1256vim_strnsave(string, len)
1257 char_u *string;
1258 int len;
1259{
1260 char_u *p;
1261
1262 p = alloc((unsigned)(len + 1));
1263 if (p != NULL)
1264 {
1265 STRNCPY(p, string, len);
1266 p[len] = NUL;
1267 }
1268 return p;
1269}
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271/*
1272 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1273 * by a backslash.
1274 */
1275 char_u *
1276vim_strsave_escaped(string, esc_chars)
1277 char_u *string;
1278 char_u *esc_chars;
1279{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001280 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281}
1282
1283/*
1284 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1285 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001286 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 */
1288 char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001289vim_strsave_escaped_ext(string, esc_chars, cc, bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 char_u *string;
1291 char_u *esc_chars;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001292 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int bsl;
1294{
1295 char_u *p;
1296 char_u *p2;
1297 char_u *escaped_string;
1298 unsigned length;
1299#ifdef FEAT_MBYTE
1300 int l;
1301#endif
1302
1303 /*
1304 * First count the number of backslashes required.
1305 * Then allocate the memory and insert them.
1306 */
1307 length = 1; /* count the trailing NUL */
1308 for (p = string; *p; p++)
1309 {
1310#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001311 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 length += l; /* count a multibyte char */
1314 p += l - 1;
1315 continue;
1316 }
1317#endif
1318 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1319 ++length; /* count a backslash */
1320 ++length; /* count an ordinary char */
1321 }
1322 escaped_string = alloc(length);
1323 if (escaped_string != NULL)
1324 {
1325 p2 = escaped_string;
1326 for (p = string; *p; p++)
1327 {
1328#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001329 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
1331 mch_memmove(p2, p, (size_t)l);
1332 p2 += l;
1333 p += l - 1; /* skip multibyte char */
1334 continue;
1335 }
1336#endif
1337 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001338 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 *p2++ = *p;
1340 }
1341 *p2 = NUL;
1342 }
1343 return escaped_string;
1344}
1345
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001346/*
1347 * Return TRUE when 'shell' has "csh" in the tail.
1348 */
1349 int
1350csh_like_shell()
1351{
1352 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1353}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001354
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001355/*
1356 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001357 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001358 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001359 * Escape a newline, depending on the 'shell' option.
1360 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1361 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001362 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001363 * Returns the result in allocated memory, NULL if we have run out.
1364 */
1365 char_u *
Bram Moolenaar26df0922014-02-23 23:39:13 +01001366vim_strsave_shellescape(string, do_special, do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001367 char_u *string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001368 int do_special;
Bram Moolenaar26df0922014-02-23 23:39:13 +01001369 int do_newline;
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001370{
1371 unsigned length;
1372 char_u *p;
1373 char_u *d;
1374 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001375 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001376 int csh_like;
1377
1378 /* Only csh and similar shells expand '!' within single quotes. For sh and
1379 * the like we must not put a backslash before it, it will be taken
1380 * literally. If do_special is set the '!' will be escaped twice.
1381 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001382 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001383
1384 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001385 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001386 for (p = string; *p != NUL; mb_ptr_adv(p))
1387 {
1388# if defined(WIN32) || defined(WIN16) || defined(DOS)
1389 if (!p_ssl)
1390 {
1391 if (*p == '"')
1392 ++length; /* " -> "" */
1393 }
1394 else
1395# endif
1396 if (*p == '\'')
1397 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001398 if ((*p == '\n' && (csh_like || do_newline))
1399 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001400 {
1401 ++length; /* insert backslash */
1402 if (csh_like && do_special)
1403 ++length; /* insert backslash */
1404 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001405 if (do_special && find_cmdline_var(p, &l) >= 0)
1406 {
1407 ++length; /* insert backslash */
1408 p += l - 1;
1409 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001410 }
1411
1412 /* Allocate memory for the result and fill it. */
1413 escaped_string = alloc(length);
1414 if (escaped_string != NULL)
1415 {
1416 d = escaped_string;
1417
1418 /* add opening quote */
1419# if defined(WIN32) || defined(WIN16) || defined(DOS)
1420 if (!p_ssl)
1421 *d++ = '"';
1422 else
1423# endif
1424 *d++ = '\'';
1425
1426 for (p = string; *p != NUL; )
1427 {
1428# if defined(WIN32) || defined(WIN16) || defined(DOS)
1429 if (!p_ssl)
1430 {
1431 if (*p == '"')
1432 {
1433 *d++ = '"';
1434 *d++ = '"';
1435 ++p;
1436 continue;
1437 }
1438 }
1439 else
1440# endif
1441 if (*p == '\'')
1442 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001443 *d++ = '\'';
1444 *d++ = '\\';
1445 *d++ = '\'';
1446 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001447 ++p;
1448 continue;
1449 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001450 if ((*p == '\n' && (csh_like || do_newline))
1451 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001452 {
1453 *d++ = '\\';
1454 if (csh_like && do_special)
1455 *d++ = '\\';
1456 *d++ = *p++;
1457 continue;
1458 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001459 if (do_special && find_cmdline_var(p, &l) >= 0)
1460 {
1461 *d++ = '\\'; /* insert backslash */
1462 while (--l >= 0) /* copy the var */
1463 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001464 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001465 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001466
1467 MB_COPY_CHAR(p, d);
1468 }
1469
1470 /* add terminating quote and finish with a NUL */
1471# if defined(WIN32) || defined(WIN16) || defined(DOS)
1472 if (!p_ssl)
1473 *d++ = '"';
1474 else
1475# endif
1476 *d++ = '\'';
1477 *d = NUL;
1478 }
1479
1480 return escaped_string;
1481}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * Like vim_strsave(), but make all characters uppercase.
1485 * This uses ASCII lower-to-upper case translation, language independent.
1486 */
1487 char_u *
1488vim_strsave_up(string)
1489 char_u *string;
1490{
1491 char_u *p1;
1492
1493 p1 = vim_strsave(string);
1494 vim_strup(p1);
1495 return p1;
1496}
1497
1498/*
1499 * Like vim_strnsave(), but make all characters uppercase.
1500 * This uses ASCII lower-to-upper case translation, language independent.
1501 */
1502 char_u *
1503vim_strnsave_up(string, len)
1504 char_u *string;
1505 int len;
1506{
1507 char_u *p1;
1508
1509 p1 = vim_strnsave(string, len);
1510 vim_strup(p1);
1511 return p1;
1512}
1513
1514/*
1515 * ASCII lower-to-upper case translation, language independent.
1516 */
1517 void
1518vim_strup(p)
1519 char_u *p;
1520{
1521 char_u *p2;
1522 int c;
1523
1524 if (p != NULL)
1525 {
1526 p2 = p;
1527 while ((c = *p2) != NUL)
1528#ifdef EBCDIC
1529 *p2++ = isalpha(c) ? toupper(c) : c;
1530#else
1531 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1532#endif
1533 }
1534}
1535
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001536#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001537/*
1538 * Make string "s" all upper-case and return it in allocated memory.
1539 * Handles multi-byte characters as well as possible.
1540 * Returns NULL when out of memory.
1541 */
1542 char_u *
1543strup_save(orig)
1544 char_u *orig;
1545{
1546 char_u *p;
1547 char_u *res;
1548
1549 res = p = vim_strsave(orig);
1550
1551 if (res != NULL)
1552 while (*p != NUL)
1553 {
1554# ifdef FEAT_MBYTE
1555 int l;
1556
1557 if (enc_utf8)
1558 {
1559 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001560 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001561 char_u *s;
1562
1563 c = utf_ptr2char(p);
1564 uc = utf_toupper(c);
1565
1566 /* Reallocate string when byte count changes. This is rare,
1567 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001568 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001569 newl = utf_char2len(uc);
1570 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001571 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001572 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001573 if (s == NULL)
1574 break;
1575 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001576 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001577 p = s + (p - res);
1578 vim_free(res);
1579 res = s;
1580 }
1581
1582 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001583 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001584 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001585 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001586 p += l; /* skip multi-byte character */
1587 else
1588# endif
1589 {
1590 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1591 p++;
1592 }
1593 }
1594
1595 return res;
1596}
1597#endif
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 * delete spaces at the end of a string
1601 */
1602 void
1603del_trailing_spaces(ptr)
1604 char_u *ptr;
1605{
1606 char_u *q;
1607
1608 q = ptr + STRLEN(ptr);
1609 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
1610 *q = NUL;
1611}
1612
1613/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001614 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001615 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
1617 void
1618vim_strncpy(to, from, len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001619 char_u *to;
1620 char_u *from;
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001621 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001623 STRNCPY(to, from, len);
1624 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625}
1626
1627/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001628 * Like strcat(), but make sure the result fits in "tosize" bytes and is
1629 * always NUL terminated.
1630 */
1631 void
1632vim_strcat(to, from, tosize)
1633 char_u *to;
1634 char_u *from;
1635 size_t tosize;
1636{
1637 size_t tolen = STRLEN(to);
1638 size_t fromlen = STRLEN(from);
1639
1640 if (tolen + fromlen + 1 > tosize)
1641 {
1642 mch_memmove(to + tolen, from, tosize - tolen - 1);
1643 to[tosize - 1] = NUL;
1644 }
1645 else
1646 STRCPY(to + tolen, from);
1647}
1648
1649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * Isolate one part of a string option where parts are separated with
1651 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001652 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 * "*option" is advanced to the next part.
1654 * The length is returned.
1655 */
1656 int
1657copy_option_part(option, buf, maxlen, sep_chars)
1658 char_u **option;
1659 char_u *buf;
1660 int maxlen;
1661 char *sep_chars;
1662{
1663 int len = 0;
1664 char_u *p = *option;
1665
1666 /* skip '.' at start of option part, for 'suffixes' */
1667 if (*p == '.')
1668 buf[len++] = *p++;
1669 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1670 {
1671 /*
1672 * Skip backslash before a separator character and space.
1673 */
1674 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1675 ++p;
1676 if (len < maxlen - 1)
1677 buf[len++] = *p;
1678 ++p;
1679 }
1680 buf[len] = NUL;
1681
1682 if (*p != NUL && *p != ',') /* skip non-standard separator */
1683 ++p;
1684 p = skip_to_option_part(p); /* p points to next file name */
1685
1686 *option = p;
1687 return len;
1688}
1689
1690/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001691 * Replacement for free() that ignores NULL pointers.
1692 * Also skip free() when exiting for sure, this helps when we caught a deadly
1693 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 */
1695 void
1696vim_free(x)
1697 void *x;
1698{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001699 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 {
1701#ifdef MEM_PROFILE
1702 mem_pre_free(&x);
1703#endif
1704 free(x);
1705 }
1706}
1707
1708#ifndef HAVE_MEMSET
1709 void *
1710vim_memset(ptr, c, size)
1711 void *ptr;
1712 int c;
1713 size_t size;
1714{
1715 char *p = ptr;
1716
1717 while (size-- > 0)
1718 *p++ = c;
1719 return ptr;
1720}
1721#endif
1722
1723#ifdef VIM_MEMCMP
1724/*
1725 * Return zero when "b1" and "b2" are the same for "len" bytes.
1726 * Return non-zero otherwise.
1727 */
1728 int
1729vim_memcmp(b1, b2, len)
1730 void *b1;
1731 void *b2;
1732 size_t len;
1733{
1734 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2;
1735
1736 for ( ; len > 0; --len)
1737 {
1738 if (*p1 != *p2)
1739 return 1;
1740 ++p1;
1741 ++p2;
1742 }
1743 return 0;
1744}
1745#endif
1746
1747#ifdef VIM_MEMMOVE
1748/*
1749 * Version of memmove() that handles overlapping source and destination.
1750 * For systems that don't have a function that is guaranteed to do that (SYSV).
1751 */
1752 void
1753mch_memmove(dst_arg, src_arg, len)
1754 void *src_arg, *dst_arg;
1755 size_t len;
1756{
1757 /*
1758 * A void doesn't have a size, we use char pointers.
1759 */
1760 char *dst = dst_arg, *src = src_arg;
1761
1762 /* overlap, copy backwards */
1763 if (dst > src && dst < src + len)
1764 {
1765 src += len;
1766 dst += len;
1767 while (len-- > 0)
1768 *--dst = *--src;
1769 }
1770 else /* copy forwards */
1771 while (len-- > 0)
1772 *dst++ = *src++;
1773}
1774#endif
1775
1776#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1777/*
1778 * Compare two strings, ignoring case, using current locale.
1779 * Doesn't work for multi-byte characters.
1780 * return 0 for match, < 0 for smaller, > 0 for bigger
1781 */
1782 int
1783vim_stricmp(s1, s2)
1784 char *s1;
1785 char *s2;
1786{
1787 int i;
1788
1789 for (;;)
1790 {
1791 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1792 if (i != 0)
1793 return i; /* this character different */
1794 if (*s1 == NUL)
1795 break; /* strings match until NUL */
1796 ++s1;
1797 ++s2;
1798 }
1799 return 0; /* strings match */
1800}
1801#endif
1802
1803#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1804/*
1805 * Compare two strings, for length "len", ignoring case, using current locale.
1806 * Doesn't work for multi-byte characters.
1807 * return 0 for match, < 0 for smaller, > 0 for bigger
1808 */
1809 int
1810vim_strnicmp(s1, s2, len)
1811 char *s1;
1812 char *s2;
1813 size_t len;
1814{
1815 int i;
1816
1817 while (len > 0)
1818 {
1819 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1820 if (i != 0)
1821 return i; /* this character different */
1822 if (*s1 == NUL)
1823 break; /* strings match until NUL */
1824 ++s1;
1825 ++s2;
1826 --len;
1827 }
1828 return 0; /* strings match */
1829}
1830#endif
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832/*
1833 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001834 * with characters from 128 to 255 correctly. It also doesn't return a
1835 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 char_u *
1838vim_strchr(string, c)
1839 char_u *string;
1840 int c;
1841{
1842 char_u *p;
1843 int b;
1844
1845 p = string;
1846#ifdef FEAT_MBYTE
1847 if (enc_utf8 && c >= 0x80)
1848 {
1849 while (*p != NUL)
1850 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001851 int l = (*mb_ptr2len)(p);
1852
1853 /* Avoid matching an illegal byte here. */
1854 if (utf_ptr2char(p) == c && l > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 return p;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001856 p += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 return NULL;
1859 }
1860 if (enc_dbcs != 0 && c > 255)
1861 {
1862 int n2 = c & 0xff;
1863
1864 c = ((unsigned)c >> 8) & 0xff;
1865 while ((b = *p) != NUL)
1866 {
1867 if (b == c && p[1] == n2)
1868 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001869 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871 return NULL;
1872 }
1873 if (has_mbyte)
1874 {
1875 while ((b = *p) != NUL)
1876 {
1877 if (b == c)
1878 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001879 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
1881 return NULL;
1882 }
1883#endif
1884 while ((b = *p) != NUL)
1885 {
1886 if (b == c)
1887 return p;
1888 ++p;
1889 }
1890 return NULL;
1891}
1892
1893/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001894 * Version of strchr() that only works for bytes and handles unsigned char
1895 * strings with characters above 128 correctly. It also doesn't return a
1896 * pointer to the NUL at the end of the string.
1897 */
1898 char_u *
1899vim_strbyte(string, c)
1900 char_u *string;
1901 int c;
1902{
1903 char_u *p = string;
1904
1905 while (*p != NUL)
1906 {
1907 if (*p == c)
1908 return p;
1909 ++p;
1910 }
1911 return NULL;
1912}
1913
1914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001916 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001917 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 */
1919 char_u *
1920vim_strrchr(string, c)
1921 char_u *string;
1922 int c;
1923{
1924 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001925 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar05159a02005-02-26 23:04:13 +00001927 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001929 if (*p == c)
1930 retval = p;
1931 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
1933 return retval;
1934}
1935
1936/*
1937 * Vim's version of strpbrk(), in case it's missing.
1938 * Don't generate a prototype for this, causes problems when it's not used.
1939 */
1940#ifndef PROTO
1941# ifndef HAVE_STRPBRK
1942# ifdef vim_strpbrk
1943# undef vim_strpbrk
1944# endif
1945 char_u *
1946vim_strpbrk(s, charset)
1947 char_u *s;
1948 char_u *charset;
1949{
1950 while (*s)
1951 {
1952 if (vim_strchr(charset, *s) != NULL)
1953 return s;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001954 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956 return NULL;
1957}
1958# endif
1959#endif
1960
1961/*
1962 * Vim has its own isspace() function, because on some machines isspace()
1963 * can't handle characters above 128.
1964 */
1965 int
1966vim_isspace(x)
1967 int x;
1968{
1969 return ((x >= 9 && x <= 13) || x == ' ');
1970}
1971
1972/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001973 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 */
1975
1976/*
1977 * Clear an allocated growing array.
1978 */
1979 void
1980ga_clear(gap)
1981 garray_T *gap;
1982{
1983 vim_free(gap->ga_data);
1984 ga_init(gap);
1985}
1986
1987/*
1988 * Clear a growing array that contains a list of strings.
1989 */
1990 void
1991ga_clear_strings(gap)
1992 garray_T *gap;
1993{
1994 int i;
1995
1996 for (i = 0; i < gap->ga_len; ++i)
1997 vim_free(((char_u **)(gap->ga_data))[i]);
1998 ga_clear(gap);
1999}
2000
2001/*
2002 * Initialize a growing array. Don't forget to set ga_itemsize and
2003 * ga_growsize! Or use ga_init2().
2004 */
2005 void
2006ga_init(gap)
2007 garray_T *gap;
2008{
2009 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002010 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 gap->ga_len = 0;
2012}
2013
2014 void
2015ga_init2(gap, itemsize, growsize)
2016 garray_T *gap;
2017 int itemsize;
2018 int growsize;
2019{
2020 ga_init(gap);
2021 gap->ga_itemsize = itemsize;
2022 gap->ga_growsize = growsize;
2023}
2024
2025/*
2026 * Make room in growing array "gap" for at least "n" items.
2027 * Return FAIL for failure, OK otherwise.
2028 */
2029 int
2030ga_grow(gap, n)
2031 garray_T *gap;
2032 int n;
2033{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002034 size_t old_len;
2035 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 char_u *pp;
2037
Bram Moolenaar86b68352004-12-27 21:59:20 +00002038 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 if (n < gap->ga_growsize)
2041 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002042 new_len = gap->ga_itemsize * (gap->ga_len + n);
2043 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002044 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (pp == NULL)
2046 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002047 old_len = gap->ga_itemsize * gap->ga_maxlen;
2048 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002049 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 gap->ga_data = pp;
2051 }
2052 return OK;
2053}
2054
2055/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002056 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002057 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002058 * Returns NULL when out of memory.
2059 */
2060 char_u *
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002061ga_concat_strings(gap, sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002062 garray_T *gap;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002063 char *sep;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002064{
2065 int i;
2066 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002067 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002068 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002069 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002070
2071 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002072 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002073
2074 s = alloc(len + 1);
2075 if (s != NULL)
2076 {
2077 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002078 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002079 for (i = 0; i < gap->ga_len; ++i)
2080 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002081 if (p != s)
2082 {
2083 STRCPY(p, sep);
2084 p += sep_len;
2085 }
2086 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2087 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002088 }
2089 }
2090 return s;
2091}
2092
2093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 * Concatenate a string to a growarray which contains characters.
2095 * Note: Does NOT copy the NUL at the end!
2096 */
2097 void
2098ga_concat(gap, s)
2099 garray_T *gap;
2100 char_u *s;
2101{
2102 int len = (int)STRLEN(s);
2103
2104 if (ga_grow(gap, len) == OK)
2105 {
2106 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2107 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
2109}
2110
2111/*
2112 * Append one byte to a growarray which contains bytes.
2113 */
2114 void
2115ga_append(gap, c)
2116 garray_T *gap;
2117 int c;
2118{
2119 if (ga_grow(gap, 1) == OK)
2120 {
2121 *((char *)gap->ga_data + gap->ga_len) = c;
2122 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124}
2125
Bram Moolenaar597a4222014-06-25 14:39:50 +02002126#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2127 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002128/*
2129 * Append the text in "gap" below the cursor line and clear "gap".
2130 */
2131 void
2132append_ga_line(gap)
2133 garray_T *gap;
2134{
2135 /* Remove trailing CR. */
2136 if (gap->ga_len > 0
2137 && !curbuf->b_p_bin
2138 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2139 --gap->ga_len;
2140 ga_append(gap, NUL);
2141 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2142 gap->ga_len = 0;
2143}
2144#endif
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146/************************************************************************
2147 * functions that use lookup tables for various things, generally to do with
2148 * special key codes.
2149 */
2150
2151/*
2152 * Some useful tables.
2153 */
2154
2155static struct modmasktable
2156{
2157 short mod_mask; /* Bit-mask for particular key modifier */
2158 short mod_flag; /* Bit(s) for particular key modifier */
2159 char_u name; /* Single letter name of modifier */
2160} mod_mask_table[] =
2161{
2162 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002163 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2165 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2166 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2167 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2168 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
2169#ifdef MACOS
2170 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2171#endif
2172 /* 'A' must be the last one */
2173 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2174 {0, 0, NUL}
2175};
2176
2177/*
2178 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002179 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
2181#define MOD_KEYS_ENTRY_SIZE 5
2182
2183static char_u modifier_keys_table[] =
2184{
2185/* mod mask with modifier without modifier */
2186 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2187 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2188 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2189 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2190 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2191 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2192 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2193 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2194 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2195 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2196 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2197 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2198 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2199 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2200 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2201 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2202 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2203 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2204 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2205 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2206 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2207 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2208 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2209 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2210 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2211 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2212 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2213 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2214 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2215 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2216 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2217 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2218 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2219
2220 /* vt100 F1 */
2221 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2222 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2223 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2224 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2225
2226 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2227 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2228 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2229 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2230 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2231 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2232 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2233 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2234 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2235 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2236
2237 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2238 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2239 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2240 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2241 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2242 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2243 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2244 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2245 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2246 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2247
2248 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2249 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2250 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2251 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2252 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2253 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2254 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2255 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2256 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2257 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2258
2259 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2260 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2261 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2262 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2263 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2264 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2265 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2266
2267 /* TAB pseudo code*/
2268 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2269
2270 NUL
2271};
2272
2273static struct key_name_entry
2274{
2275 int key; /* Special key code or ascii value */
2276 char_u *name; /* Name of key */
2277} key_names_table[] =
2278{
2279 {' ', (char_u *)"Space"},
2280 {TAB, (char_u *)"Tab"},
2281 {K_TAB, (char_u *)"Tab"},
2282 {NL, (char_u *)"NL"},
2283 {NL, (char_u *)"NewLine"}, /* Alternative name */
2284 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2285 {NL, (char_u *)"LF"}, /* Alternative name */
2286 {CAR, (char_u *)"CR"},
2287 {CAR, (char_u *)"Return"}, /* Alternative name */
2288 {CAR, (char_u *)"Enter"}, /* Alternative name */
2289 {K_BS, (char_u *)"BS"},
2290 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2291 {ESC, (char_u *)"Esc"},
2292 {CSI, (char_u *)"CSI"},
2293 {K_CSI, (char_u *)"xCSI"},
2294 {'|', (char_u *)"Bar"},
2295 {'\\', (char_u *)"Bslash"},
2296 {K_DEL, (char_u *)"Del"},
2297 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2298 {K_KDEL, (char_u *)"kDel"},
2299 {K_UP, (char_u *)"Up"},
2300 {K_DOWN, (char_u *)"Down"},
2301 {K_LEFT, (char_u *)"Left"},
2302 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002303 {K_XUP, (char_u *)"xUp"},
2304 {K_XDOWN, (char_u *)"xDown"},
2305 {K_XLEFT, (char_u *)"xLeft"},
2306 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 {K_F1, (char_u *)"F1"},
2309 {K_F2, (char_u *)"F2"},
2310 {K_F3, (char_u *)"F3"},
2311 {K_F4, (char_u *)"F4"},
2312 {K_F5, (char_u *)"F5"},
2313 {K_F6, (char_u *)"F6"},
2314 {K_F7, (char_u *)"F7"},
2315 {K_F8, (char_u *)"F8"},
2316 {K_F9, (char_u *)"F9"},
2317 {K_F10, (char_u *)"F10"},
2318
2319 {K_F11, (char_u *)"F11"},
2320 {K_F12, (char_u *)"F12"},
2321 {K_F13, (char_u *)"F13"},
2322 {K_F14, (char_u *)"F14"},
2323 {K_F15, (char_u *)"F15"},
2324 {K_F16, (char_u *)"F16"},
2325 {K_F17, (char_u *)"F17"},
2326 {K_F18, (char_u *)"F18"},
2327 {K_F19, (char_u *)"F19"},
2328 {K_F20, (char_u *)"F20"},
2329
2330 {K_F21, (char_u *)"F21"},
2331 {K_F22, (char_u *)"F22"},
2332 {K_F23, (char_u *)"F23"},
2333 {K_F24, (char_u *)"F24"},
2334 {K_F25, (char_u *)"F25"},
2335 {K_F26, (char_u *)"F26"},
2336 {K_F27, (char_u *)"F27"},
2337 {K_F28, (char_u *)"F28"},
2338 {K_F29, (char_u *)"F29"},
2339 {K_F30, (char_u *)"F30"},
2340
2341 {K_F31, (char_u *)"F31"},
2342 {K_F32, (char_u *)"F32"},
2343 {K_F33, (char_u *)"F33"},
2344 {K_F34, (char_u *)"F34"},
2345 {K_F35, (char_u *)"F35"},
2346 {K_F36, (char_u *)"F36"},
2347 {K_F37, (char_u *)"F37"},
2348
2349 {K_XF1, (char_u *)"xF1"},
2350 {K_XF2, (char_u *)"xF2"},
2351 {K_XF3, (char_u *)"xF3"},
2352 {K_XF4, (char_u *)"xF4"},
2353
2354 {K_HELP, (char_u *)"Help"},
2355 {K_UNDO, (char_u *)"Undo"},
2356 {K_INS, (char_u *)"Insert"},
2357 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2358 {K_KINS, (char_u *)"kInsert"},
2359 {K_HOME, (char_u *)"Home"},
2360 {K_KHOME, (char_u *)"kHome"},
2361 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002362 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {K_END, (char_u *)"End"},
2364 {K_KEND, (char_u *)"kEnd"},
2365 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002366 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {K_PAGEUP, (char_u *)"PageUp"},
2368 {K_PAGEDOWN, (char_u *)"PageDown"},
2369 {K_KPAGEUP, (char_u *)"kPageUp"},
2370 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2371
2372 {K_KPLUS, (char_u *)"kPlus"},
2373 {K_KMINUS, (char_u *)"kMinus"},
2374 {K_KDIVIDE, (char_u *)"kDivide"},
2375 {K_KMULTIPLY, (char_u *)"kMultiply"},
2376 {K_KENTER, (char_u *)"kEnter"},
2377 {K_KPOINT, (char_u *)"kPoint"},
2378
2379 {K_K0, (char_u *)"k0"},
2380 {K_K1, (char_u *)"k1"},
2381 {K_K2, (char_u *)"k2"},
2382 {K_K3, (char_u *)"k3"},
2383 {K_K4, (char_u *)"k4"},
2384 {K_K5, (char_u *)"k5"},
2385 {K_K6, (char_u *)"k6"},
2386 {K_K7, (char_u *)"k7"},
2387 {K_K8, (char_u *)"k8"},
2388 {K_K9, (char_u *)"k9"},
2389
2390 {'<', (char_u *)"lt"},
2391
2392 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002393#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002395#endif
2396#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002398#endif
2399#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002401#endif
2402#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002404#endif
2405#ifdef FEAT_MOUSE_URXVT
2406 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2407#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002408#ifdef FEAT_MOUSE_SGR
2409 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
2410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2412 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2413 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2414 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2415 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2416 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2417 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2418 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2419 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2420 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2421 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002422 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2423 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2424 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2425 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2426 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2427 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {K_X1MOUSE, (char_u *)"X1Mouse"},
2429 {K_X1DRAG, (char_u *)"X1Drag"},
2430 {K_X1RELEASE, (char_u *)"X1Release"},
2431 {K_X2MOUSE, (char_u *)"X2Mouse"},
2432 {K_X2DRAG, (char_u *)"X2Drag"},
2433 {K_X2RELEASE, (char_u *)"X2Release"},
2434 {K_DROP, (char_u *)"Drop"},
2435 {K_ZERO, (char_u *)"Nul"},
2436#ifdef FEAT_EVAL
2437 {K_SNR, (char_u *)"SNR"},
2438#endif
2439 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02002440 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {0, NULL}
2442};
2443
2444#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2445
2446#ifdef FEAT_MOUSE
2447static struct mousetable
2448{
2449 int pseudo_code; /* Code for pseudo mouse event */
2450 int button; /* Which mouse button is it? */
2451 int is_click; /* Is it a mouse button click event? */
2452 int is_drag; /* Is it a mouse drag event? */
2453} mouse_table[] =
2454{
2455 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2456#ifdef FEAT_GUI
2457 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2458#endif
2459 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2460 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2461#ifdef FEAT_GUI
2462 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2463#endif
2464 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2465 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2466 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2467 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2468 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2469 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2470 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2471 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2472 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2473 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2474 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2475 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2476 /* DRAG without CLICK */
2477 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2478 /* RELEASE without CLICK */
2479 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2480 {0, 0, 0, 0},
2481};
2482#endif /* FEAT_MOUSE */
2483
2484/*
2485 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2486 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2487 */
2488 int
2489name_to_mod_mask(c)
2490 int c;
2491{
2492 int i;
2493
2494 c = TOUPPER_ASC(c);
2495 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2496 if (c == mod_mask_table[i].name)
2497 return mod_mask_table[i].mod_flag;
2498 return 0;
2499}
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
2502 * Check if if there is a special key code for "key" that includes the
2503 * modifiers specified.
2504 */
2505 int
2506simplify_key(key, modifiers)
2507 int key;
2508 int *modifiers;
2509{
2510 int i;
2511 int key0;
2512 int key1;
2513
2514 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2515 {
2516 /* TAB is a special case */
2517 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2518 {
2519 *modifiers &= ~MOD_MASK_SHIFT;
2520 return K_S_TAB;
2521 }
2522 key0 = KEY2TERMCAP0(key);
2523 key1 = KEY2TERMCAP1(key);
2524 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2525 if (key0 == modifier_keys_table[i + 3]
2526 && key1 == modifier_keys_table[i + 4]
2527 && (*modifiers & modifier_keys_table[i]))
2528 {
2529 *modifiers &= ~modifier_keys_table[i];
2530 return TERMCAP2KEY(modifier_keys_table[i + 1],
2531 modifier_keys_table[i + 2]);
2532 }
2533 }
2534 return key;
2535}
2536
2537/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002538 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002539 */
2540 int
2541handle_x_keys(key)
2542 int key;
2543{
2544 switch (key)
2545 {
2546 case K_XUP: return K_UP;
2547 case K_XDOWN: return K_DOWN;
2548 case K_XLEFT: return K_LEFT;
2549 case K_XRIGHT: return K_RIGHT;
2550 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002551 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002552 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002553 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002554 case K_XF1: return K_F1;
2555 case K_XF2: return K_F2;
2556 case K_XF3: return K_F3;
2557 case K_XF4: return K_F4;
2558 case K_S_XF1: return K_S_F1;
2559 case K_S_XF2: return K_S_F2;
2560 case K_S_XF3: return K_S_F3;
2561 case K_S_XF4: return K_S_F4;
2562 }
2563 return key;
2564}
2565
2566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 * Return a string which contains the name of the given key when the given
2568 * modifiers are down.
2569 */
2570 char_u *
2571get_special_key_name(c, modifiers)
2572 int c;
2573 int modifiers;
2574{
2575 static char_u string[MAX_KEY_NAME_LEN + 1];
2576
2577 int i, idx;
2578 int table_idx;
2579 char_u *s;
2580
2581 string[0] = '<';
2582 idx = 1;
2583
2584 /* Key that stands for a normal character. */
2585 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2586 c = KEY2TERMCAP1(c);
2587
2588 /*
2589 * Translate shifted special keys into unshifted keys and set modifier.
2590 * Same for CTRL and ALT modifiers.
2591 */
2592 if (IS_SPECIAL(c))
2593 {
2594 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2595 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2596 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2597 {
2598 modifiers |= modifier_keys_table[i];
2599 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2600 modifier_keys_table[i + 4]);
2601 break;
2602 }
2603 }
2604
2605 /* try to find the key in the special key table */
2606 table_idx = find_special_key_in_table(c);
2607
2608 /*
2609 * When not a known special key, and not a printable character, try to
2610 * extract modifiers.
2611 */
2612 if (c > 0
2613#ifdef FEAT_MBYTE
2614 && (*mb_char2len)(c) == 1
2615#endif
2616 )
2617 {
2618 if (table_idx < 0
2619 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2620 && (c & 0x80))
2621 {
2622 c &= 0x7f;
2623 modifiers |= MOD_MASK_ALT;
2624 /* try again, to find the un-alted key in the special key table */
2625 table_idx = find_special_key_in_table(c);
2626 }
2627 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2628 {
2629#ifdef EBCDIC
2630 c = CtrlChar(c);
2631#else
2632 c += '@';
2633#endif
2634 modifiers |= MOD_MASK_CTRL;
2635 }
2636 }
2637
2638 /* translate the modifier into a string */
2639 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2640 if ((modifiers & mod_mask_table[i].mod_mask)
2641 == mod_mask_table[i].mod_flag)
2642 {
2643 string[idx++] = mod_mask_table[i].name;
2644 string[idx++] = (char_u)'-';
2645 }
2646
2647 if (table_idx < 0) /* unknown special key, may output t_xx */
2648 {
2649 if (IS_SPECIAL(c))
2650 {
2651 string[idx++] = 't';
2652 string[idx++] = '_';
2653 string[idx++] = KEY2TERMCAP0(c);
2654 string[idx++] = KEY2TERMCAP1(c);
2655 }
2656 /* Not a special key, only modifiers, output directly */
2657 else
2658 {
2659#ifdef FEAT_MBYTE
2660 if (has_mbyte && (*mb_char2len)(c) > 1)
2661 idx += (*mb_char2bytes)(c, string + idx);
2662 else
2663#endif
2664 if (vim_isprintc(c))
2665 string[idx++] = c;
2666 else
2667 {
2668 s = transchar(c);
2669 while (*s)
2670 string[idx++] = *s++;
2671 }
2672 }
2673 }
2674 else /* use name of special key */
2675 {
2676 STRCPY(string + idx, key_names_table[table_idx].name);
2677 idx = (int)STRLEN(string);
2678 }
2679 string[idx++] = '>';
2680 string[idx] = NUL;
2681 return string;
2682}
2683
2684/*
2685 * Try translating a <> name at (*srcp)[] to dst[].
2686 * Return the number of characters added to dst[], zero for no match.
2687 * If there is a match, srcp is advanced to after the <> name.
2688 * dst[] must be big enough to hold the result (up to six characters)!
2689 */
2690 int
2691trans_special(srcp, dst, keycode)
2692 char_u **srcp;
2693 char_u *dst;
2694 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2695{
2696 int modifiers = 0;
2697 int key;
2698 int dlen = 0;
2699
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002700 key = find_special_key(srcp, &modifiers, keycode, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (key == 0)
2702 return 0;
2703
2704 /* Put the appropriate modifier in a string */
2705 if (modifiers != 0)
2706 {
2707 dst[dlen++] = K_SPECIAL;
2708 dst[dlen++] = KS_MODIFIER;
2709 dst[dlen++] = modifiers;
2710 }
2711
2712 if (IS_SPECIAL(key))
2713 {
2714 dst[dlen++] = K_SPECIAL;
2715 dst[dlen++] = KEY2TERMCAP0(key);
2716 dst[dlen++] = KEY2TERMCAP1(key);
2717 }
2718#ifdef FEAT_MBYTE
2719 else if (has_mbyte && !keycode)
2720 dlen += (*mb_char2bytes)(key, dst + dlen);
2721#endif
2722 else if (keycode)
2723 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2724 else
2725 dst[dlen++] = key;
2726
2727 return dlen;
2728}
2729
2730/*
2731 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2732 * srcp is advanced to after the <> name.
2733 * returns 0 if there is no match.
2734 */
2735 int
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002736find_special_key(srcp, modp, keycode, keep_x_key)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 char_u **srcp;
2738 int *modp;
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002739 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2740 int keep_x_key; /* don't translate xHome to Home key */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 char_u *last_dash;
2743 char_u *end_of_name;
2744 char_u *src;
2745 char_u *bp;
2746 int modifiers;
2747 int bit;
2748 int key;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002749 unsigned long n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002750 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752 src = *srcp;
2753 if (src[0] != '<')
2754 return 0;
2755
2756 /* Find end of modifier list */
2757 last_dash = src;
2758 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2759 {
2760 if (*bp == '-')
2761 {
2762 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002763 if (bp[1] != NUL)
2764 {
2765#ifdef FEAT_MBYTE
2766 if (has_mbyte)
2767 l = mb_ptr2len(bp + 1);
2768 else
2769#endif
2770 l = 1;
2771 if (bp[l + 1] == '>')
2772 bp += l; /* anything accepted, like <C-?> */
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2776 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002777 else if (STRNICMP(bp, "char-", 5) == 0)
2778 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002779 vim_str2nr(bp + 5, NULL, &l, TRUE, TRUE, NULL, NULL, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002780 bp += l + 5;
2781 break;
2782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
2784
2785 if (*bp == '>') /* found matching '>' */
2786 {
2787 end_of_name = bp + 1;
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 /* Which modifiers are given? */
2790 modifiers = 0x0;
2791 for (bp = src + 1; bp < last_dash; bp++)
2792 {
2793 if (*bp != '-')
2794 {
2795 bit = name_to_mod_mask(*bp);
2796 if (bit == 0x0)
2797 break; /* Illegal modifier name */
2798 modifiers |= bit;
2799 }
2800 }
2801
2802 /*
2803 * Legal modifier name.
2804 */
2805 if (bp >= last_dash)
2806 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002807 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2808 && VIM_ISDIGIT(last_dash[6]))
2809 {
2810 /* <Char-123> or <Char-033> or <Char-0x33> */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002811 vim_str2nr(last_dash + 6, NULL, NULL, TRUE, TRUE, NULL, &n, 0);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002812 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002815 {
Bram Moolenaar792826c2011-08-19 22:29:02 +02002816 /*
2817 * Modifier with single letter, or special key name.
2818 */
2819#ifdef FEAT_MBYTE
2820 if (has_mbyte)
2821 l = mb_ptr2len(last_dash + 1);
2822 else
2823#endif
2824 l = 1;
2825 if (modifiers != 0 && last_dash[l + 1] == '>')
2826 key = PTR2CHAR(last_dash + 1);
2827 else
2828 {
2829 key = get_special_key_code(last_dash + 1);
2830 if (!keep_x_key)
2831 key = handle_x_keys(key);
2832 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /*
2836 * get_special_key_code() may return NUL for invalid
2837 * special key name.
2838 */
2839 if (key != NUL)
2840 {
2841 /*
2842 * Only use a modifier when there is no special key code that
2843 * includes the modifier.
2844 */
2845 key = simplify_key(key, &modifiers);
2846
2847 if (!keycode)
2848 {
2849 /* don't want keycode, use single byte code */
2850 if (key == K_BS)
2851 key = BS;
2852 else if (key == K_DEL || key == K_KDEL)
2853 key = DEL;
2854 }
2855
2856 /*
2857 * Normal Key with modifier: Try to make a single byte code.
2858 */
2859 if (!IS_SPECIAL(key))
2860 key = extract_modifiers(key, &modifiers);
2861
2862 *modp = modifiers;
2863 *srcp = end_of_name;
2864 return key;
2865 }
2866 }
2867 }
2868 return 0;
2869}
2870
2871/*
2872 * Try to include modifiers in the key.
2873 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2874 */
2875 int
2876extract_modifiers(key, modp)
2877 int key;
2878 int *modp;
2879{
2880 int modifiers = *modp;
2881
2882#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002883 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (!(modifiers & MOD_MASK_CMD))
2885#endif
2886 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2887 {
2888 key = TOUPPER_ASC(key);
2889 modifiers &= ~MOD_MASK_SHIFT;
2890 }
2891 if ((modifiers & MOD_MASK_CTRL)
2892#ifdef EBCDIC
2893 /* * TODO: EBCDIC Better use:
2894 * && (Ctrl_chr(key) || key == '?')
2895 * ??? */
2896 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2897 != NULL
2898#else
2899 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2900#endif
2901 )
2902 {
2903 key = Ctrl_chr(key);
2904 modifiers &= ~MOD_MASK_CTRL;
2905 /* <C-@> is <Nul> */
2906 if (key == 0)
2907 key = K_ZERO;
2908 }
2909#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002910 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 if (!(modifiers & MOD_MASK_CMD))
2912#endif
2913 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2914#ifdef FEAT_MBYTE
2915 && !enc_dbcs /* avoid creating a lead byte */
2916#endif
2917 )
2918 {
2919 key |= 0x80;
2920 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2921 }
2922
2923 *modp = modifiers;
2924 return key;
2925}
2926
2927/*
2928 * Try to find key "c" in the special key table.
2929 * Return the index when found, -1 when not found.
2930 */
2931 int
2932find_special_key_in_table(c)
2933 int c;
2934{
2935 int i;
2936
2937 for (i = 0; key_names_table[i].name != NULL; i++)
2938 if (c == key_names_table[i].key)
2939 break;
2940 if (key_names_table[i].name == NULL)
2941 i = -1;
2942 return i;
2943}
2944
2945/*
2946 * Find the special key with the given name (the given string does not have to
2947 * end with NUL, the name is assumed to end before the first non-idchar).
2948 * If the name starts with "t_" the next two characters are interpreted as a
2949 * termcap name.
2950 * Return the key code, or 0 if not found.
2951 */
2952 int
2953get_special_key_code(name)
2954 char_u *name;
2955{
2956 char_u *table_name;
2957 char_u string[3];
2958 int i, j;
2959
2960 /*
2961 * If it's <t_xx> we get the code for xx from the termcap
2962 */
2963 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
2964 {
2965 string[0] = name[2];
2966 string[1] = name[3];
2967 string[2] = NUL;
2968 if (add_termcap_entry(string, FALSE) == OK)
2969 return TERMCAP2KEY(name[2], name[3]);
2970 }
2971 else
2972 for (i = 0; key_names_table[i].name != NULL; i++)
2973 {
2974 table_name = key_names_table[i].name;
2975 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
2976 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
2977 break;
2978 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
2979 return key_names_table[i].key;
2980 }
2981 return 0;
2982}
2983
Bram Moolenaar05bb9532008-07-04 09:44:11 +00002984#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 char_u *
2986get_key_name(i)
2987 int i;
2988{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00002989 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 return NULL;
2991 return key_names_table[i].name;
2992}
2993#endif
2994
Bram Moolenaar05bb9532008-07-04 09:44:11 +00002995#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996/*
2997 * Look up the given mouse code to return the relevant information in the other
2998 * arguments. Return which button is down or was released.
2999 */
3000 int
3001get_mouse_button(code, is_click, is_drag)
3002 int code;
3003 int *is_click;
3004 int *is_drag;
3005{
3006 int i;
3007
3008 for (i = 0; mouse_table[i].pseudo_code; i++)
3009 if (code == mouse_table[i].pseudo_code)
3010 {
3011 *is_click = mouse_table[i].is_click;
3012 *is_drag = mouse_table[i].is_drag;
3013 return mouse_table[i].button;
3014 }
3015 return 0; /* Shouldn't get here */
3016}
3017
3018/*
3019 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3020 * the given information about which mouse button is down, and whether the
3021 * mouse was clicked, dragged or released.
3022 */
3023 int
3024get_pseudo_mouse_code(button, is_click, is_drag)
3025 int button; /* eg MOUSE_LEFT */
3026 int is_click;
3027 int is_drag;
3028{
3029 int i;
3030
3031 for (i = 0; mouse_table[i].pseudo_code; i++)
3032 if (button == mouse_table[i].button
3033 && is_click == mouse_table[i].is_click
3034 && is_drag == mouse_table[i].is_drag)
3035 {
3036#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003037 /* Trick: a non mappable left click and release has mouse_col -1
3038 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3039 * gui_mouse_moved() */
3040 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003042 if (mouse_col < 0)
3043 mouse_col = 0;
3044 else
3045 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3047 return (int)KE_LEFTMOUSE_NM;
3048 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3049 return (int)KE_LEFTRELEASE_NM;
3050 }
3051#endif
3052 return mouse_table[i].pseudo_code;
3053 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003054 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055}
3056#endif /* FEAT_MOUSE */
3057
3058/*
3059 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3060 */
3061 int
3062get_fileformat(buf)
3063 buf_T *buf;
3064{
3065 int c = *buf->b_p_ff;
3066
3067 if (buf->b_p_bin || c == 'u')
3068 return EOL_UNIX;
3069 if (c == 'm')
3070 return EOL_MAC;
3071 return EOL_DOS;
3072}
3073
3074/*
3075 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3076 * argument.
3077 */
3078 int
3079get_fileformat_force(buf, eap)
3080 buf_T *buf;
3081 exarg_T *eap; /* can be NULL! */
3082{
3083 int c;
3084
3085 if (eap != NULL && eap->force_ff != 0)
3086 c = eap->cmd[eap->force_ff];
3087 else
3088 {
3089 if ((eap != NULL && eap->force_bin != 0)
3090 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3091 return EOL_UNIX;
3092 c = *buf->b_p_ff;
3093 }
3094 if (c == 'u')
3095 return EOL_UNIX;
3096 if (c == 'm')
3097 return EOL_MAC;
3098 return EOL_DOS;
3099}
3100
3101/*
3102 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3103 * Sets both 'textmode' and 'fileformat'.
3104 * Note: Does _not_ set global value of 'textmode'!
3105 */
3106 void
3107set_fileformat(t, opt_flags)
3108 int t;
3109 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */
3110{
3111 char *p = NULL;
3112
3113 switch (t)
3114 {
3115 case EOL_DOS:
3116 p = FF_DOS;
3117 curbuf->b_p_tx = TRUE;
3118 break;
3119 case EOL_UNIX:
3120 p = FF_UNIX;
3121 curbuf->b_p_tx = FALSE;
3122 break;
3123 case EOL_MAC:
3124 p = FF_MAC;
3125 curbuf->b_p_tx = FALSE;
3126 break;
3127 }
3128 if (p != NULL)
3129 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003130 OPT_FREE | opt_flags, 0);
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00003133 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003135 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136#endif
3137#ifdef FEAT_TITLE
3138 need_maketitle = TRUE; /* set window title later */
3139#endif
3140}
3141
3142/*
3143 * Return the default fileformat from 'fileformats'.
3144 */
3145 int
3146default_fileformat()
3147{
3148 switch (*p_ffs)
3149 {
3150 case 'm': return EOL_MAC;
3151 case 'd': return EOL_DOS;
3152 }
3153 return EOL_UNIX;
3154}
3155
3156/*
3157 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3158 */
3159 int
3160call_shell(cmd, opt)
3161 char_u *cmd;
3162 int opt;
3163{
3164 char_u *ncmd;
3165 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003166#ifdef FEAT_PROFILE
3167 proftime_T wait_time;
3168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 if (p_verbose > 3)
3171 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003172 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003173 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 cmd == NULL ? p_sh : cmd);
3175 out_char('\n');
3176 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003177 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179
Bram Moolenaar05159a02005-02-26 23:04:13 +00003180#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003181 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003182 prof_child_enter(&wait_time);
3183#endif
3184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (*p_sh == NUL)
3186 {
3187 EMSG(_(e_shellempty));
3188 retval = -1;
3189 }
3190 else
3191 {
3192#ifdef FEAT_GUI_MSWIN
3193 /* Don't hide the pointer while executing a shell command. */
3194 gui_mch_mousehide(FALSE);
3195#endif
3196#ifdef FEAT_GUI
3197 ++hold_gui_events;
3198#endif
3199 /* The external command may update a tags file, clear cached tags. */
3200 tag_freematch();
3201
3202 if (cmd == NULL || *p_sxq == NUL)
3203 retval = mch_call_shell(cmd, opt);
3204 else
3205 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003206 char_u *ecmd = cmd;
3207
3208 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3209 {
3210 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3211 if (ecmd == NULL)
3212 ecmd = cmd;
3213 }
3214 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (ncmd != NULL)
3216 {
3217 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003218 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003219 /* When 'shellxquote' is ( append ).
3220 * When 'shellxquote' is "( append )". */
3221 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3222 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3223 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 retval = mch_call_shell(ncmd, opt);
3225 vim_free(ncmd);
3226 }
3227 else
3228 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003229 if (ecmd != cmd)
3230 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232#ifdef FEAT_GUI
3233 --hold_gui_events;
3234#endif
3235 /*
3236 * Check the window size, in case it changed while executing the
3237 * external command.
3238 */
3239 shell_resized_check();
3240 }
3241
3242#ifdef FEAT_EVAL
3243 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003244# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003245 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003246 prof_child_exit(&wait_time);
3247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248#endif
3249
3250 return retval;
3251}
3252
3253/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003254 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3255 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 */
3257 int
3258get_real_state()
3259{
3260 if (State & NORMAL)
3261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003263 {
3264 if (VIsual_select)
3265 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003268 else if (finish_op)
3269 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271 return State;
3272}
3273
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003274#if defined(FEAT_MBYTE) || defined(PROTO)
3275/*
3276 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003277 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003278 * "b" must point to the start of the file name
3279 */
3280 int
3281after_pathsep(b, p)
3282 char_u *b;
3283 char_u *p;
3284{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003285 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003286 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3287}
3288#endif
3289
3290/*
3291 * Return TRUE if file names "f1" and "f2" are in the same directory.
3292 * "f1" may be a short name, "f2" must be a full path.
3293 */
3294 int
3295same_directory(f1, f2)
3296 char_u *f1;
3297 char_u *f2;
3298{
3299 char_u ffname[MAXPATHL];
3300 char_u *t1;
3301 char_u *t2;
3302
3303 /* safety check */
3304 if (f1 == NULL || f2 == NULL)
3305 return FALSE;
3306
3307 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3308 t1 = gettail_sep(ffname);
3309 t2 = gettail_sep(f2);
3310 return (t1 - ffname == t2 - f2
3311 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00003315 || ((defined(FEAT_GUI_GTK)) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00003316 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3318 || defined(PROTO)
3319/*
3320 * Change to a file's directory.
3321 * Caller must call shorten_fnames()!
3322 * Return OK or FAIL.
3323 */
3324 int
3325vim_chdirfile(fname)
3326 char_u *fname;
3327{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003328 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003330 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003331 *gettail_sep(dir) = NUL;
3332 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333}
3334#endif
3335
3336#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3337/*
3338 * Check if "name" ends in a slash and is not a directory.
3339 * Used for systems where stat() ignores a trailing slash on a file name.
3340 * The Vim code assumes a trailing slash is only ignored for a directory.
3341 */
3342 int
3343illegal_slash(name)
3344 char *name;
3345{
3346 if (name[0] == NUL)
3347 return FALSE; /* no file name is not illegal */
3348 if (name[strlen(name) - 1] != '/')
3349 return FALSE; /* no trailing slash */
3350 if (mch_isdir((char_u *)name))
3351 return FALSE; /* trailing slash for a directory */
3352 return TRUE;
3353}
3354#endif
3355
3356#if defined(CURSOR_SHAPE) || defined(PROTO)
3357
3358/*
3359 * Handling of cursor and mouse pointer shapes in various modes.
3360 */
3361
3362cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3363{
3364 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3365 * defaults when Vim starts.
3366 * Adjust the SHAPE_IDX_ defines when making changes! */
3367 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3368 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3369 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3370 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3371 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3372 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3373 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3374 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3375 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3376 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3377 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3378 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3379 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3380 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3381 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3382 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3383 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3384};
3385
3386#ifdef FEAT_MOUSESHAPE
3387/*
3388 * Table with names for mouse shapes. Keep in sync with all the tables for
3389 * mch_set_mouse_shape()!.
3390 */
3391static char * mshape_names[] =
3392{
3393 "arrow", /* default, must be the first one */
3394 "blank", /* hidden */
3395 "beam",
3396 "updown",
3397 "udsizing",
3398 "leftright",
3399 "lrsizing",
3400 "busy",
3401 "no",
3402 "crosshair",
3403 "hand1",
3404 "hand2",
3405 "pencil",
3406 "question",
3407 "rightup-arrow",
3408 "up-arrow",
3409 NULL
3410};
3411#endif
3412
3413/*
3414 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3415 * ("what" is SHAPE_MOUSE).
3416 * Returns error message for an illegal option, NULL otherwise.
3417 */
3418 char_u *
3419parse_shape_opt(what)
3420 int what;
3421{
3422 char_u *modep;
3423 char_u *colonp;
3424 char_u *commap;
3425 char_u *slashp;
3426 char_u *p, *endp;
3427 int idx = 0; /* init for GCC */
3428 int all_idx;
3429 int len;
3430 int i;
3431 long n;
3432 int found_ve = FALSE; /* found "ve" flag */
3433 int round;
3434
3435 /*
3436 * First round: check for errors; second round: do it for real.
3437 */
3438 for (round = 1; round <= 2; ++round)
3439 {
3440 /*
3441 * Repeat for all comma separated parts.
3442 */
3443#ifdef FEAT_MOUSESHAPE
3444 if (what == SHAPE_MOUSE)
3445 modep = p_mouseshape;
3446 else
3447#endif
3448 modep = p_guicursor;
3449 while (*modep != NUL)
3450 {
3451 colonp = vim_strchr(modep, ':');
3452 if (colonp == NULL)
3453 return (char_u *)N_("E545: Missing colon");
3454 if (colonp == modep)
3455 return (char_u *)N_("E546: Illegal mode");
3456 commap = vim_strchr(modep, ',');
3457
3458 /*
3459 * Repeat for all mode's before the colon.
3460 * For the 'a' mode, we loop to handle all the modes.
3461 */
3462 all_idx = -1;
3463 while (modep < colonp || all_idx >= 0)
3464 {
3465 if (all_idx < 0)
3466 {
3467 /* Find the mode. */
3468 if (modep[1] == '-' || modep[1] == ':')
3469 len = 1;
3470 else
3471 len = 2;
3472 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3473 all_idx = SHAPE_IDX_COUNT - 1;
3474 else
3475 {
3476 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3477 if (STRNICMP(modep, shape_table[idx].name, len)
3478 == 0)
3479 break;
3480 if (idx == SHAPE_IDX_COUNT
3481 || (shape_table[idx].used_for & what) == 0)
3482 return (char_u *)N_("E546: Illegal mode");
3483 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3484 found_ve = TRUE;
3485 }
3486 modep += len + 1;
3487 }
3488
3489 if (all_idx >= 0)
3490 idx = all_idx--;
3491 else if (round == 2)
3492 {
3493#ifdef FEAT_MOUSESHAPE
3494 if (what == SHAPE_MOUSE)
3495 {
3496 /* Set the default, for the missing parts */
3497 shape_table[idx].mshape = 0;
3498 }
3499 else
3500#endif
3501 {
3502 /* Set the defaults, for the missing parts */
3503 shape_table[idx].shape = SHAPE_BLOCK;
3504 shape_table[idx].blinkwait = 700L;
3505 shape_table[idx].blinkon = 400L;
3506 shape_table[idx].blinkoff = 250L;
3507 }
3508 }
3509
3510 /* Parse the part after the colon */
3511 for (p = colonp + 1; *p && *p != ','; )
3512 {
3513#ifdef FEAT_MOUSESHAPE
3514 if (what == SHAPE_MOUSE)
3515 {
3516 for (i = 0; ; ++i)
3517 {
3518 if (mshape_names[i] == NULL)
3519 {
3520 if (!VIM_ISDIGIT(*p))
3521 return (char_u *)N_("E547: Illegal mouseshape");
3522 if (round == 2)
3523 shape_table[idx].mshape =
3524 getdigits(&p) + MSHAPE_NUMBERED;
3525 else
3526 (void)getdigits(&p);
3527 break;
3528 }
3529 len = (int)STRLEN(mshape_names[i]);
3530 if (STRNICMP(p, mshape_names[i], len) == 0)
3531 {
3532 if (round == 2)
3533 shape_table[idx].mshape = i;
3534 p += len;
3535 break;
3536 }
3537 }
3538 }
3539 else /* if (what == SHAPE_MOUSE) */
3540#endif
3541 {
3542 /*
3543 * First handle the ones with a number argument.
3544 */
3545 i = *p;
3546 len = 0;
3547 if (STRNICMP(p, "ver", 3) == 0)
3548 len = 3;
3549 else if (STRNICMP(p, "hor", 3) == 0)
3550 len = 3;
3551 else if (STRNICMP(p, "blinkwait", 9) == 0)
3552 len = 9;
3553 else if (STRNICMP(p, "blinkon", 7) == 0)
3554 len = 7;
3555 else if (STRNICMP(p, "blinkoff", 8) == 0)
3556 len = 8;
3557 if (len != 0)
3558 {
3559 p += len;
3560 if (!VIM_ISDIGIT(*p))
3561 return (char_u *)N_("E548: digit expected");
3562 n = getdigits(&p);
3563 if (len == 3) /* "ver" or "hor" */
3564 {
3565 if (n == 0)
3566 return (char_u *)N_("E549: Illegal percentage");
3567 if (round == 2)
3568 {
3569 if (TOLOWER_ASC(i) == 'v')
3570 shape_table[idx].shape = SHAPE_VER;
3571 else
3572 shape_table[idx].shape = SHAPE_HOR;
3573 shape_table[idx].percentage = n;
3574 }
3575 }
3576 else if (round == 2)
3577 {
3578 if (len == 9)
3579 shape_table[idx].blinkwait = n;
3580 else if (len == 7)
3581 shape_table[idx].blinkon = n;
3582 else
3583 shape_table[idx].blinkoff = n;
3584 }
3585 }
3586 else if (STRNICMP(p, "block", 5) == 0)
3587 {
3588 if (round == 2)
3589 shape_table[idx].shape = SHAPE_BLOCK;
3590 p += 5;
3591 }
3592 else /* must be a highlight group name then */
3593 {
3594 endp = vim_strchr(p, '-');
3595 if (commap == NULL) /* last part */
3596 {
3597 if (endp == NULL)
3598 endp = p + STRLEN(p); /* find end of part */
3599 }
3600 else if (endp > commap || endp == NULL)
3601 endp = commap;
3602 slashp = vim_strchr(p, '/');
3603 if (slashp != NULL && slashp < endp)
3604 {
3605 /* "group/langmap_group" */
3606 i = syn_check_group(p, (int)(slashp - p));
3607 p = slashp + 1;
3608 }
3609 if (round == 2)
3610 {
3611 shape_table[idx].id = syn_check_group(p,
3612 (int)(endp - p));
3613 shape_table[idx].id_lm = shape_table[idx].id;
3614 if (slashp != NULL && slashp < endp)
3615 shape_table[idx].id = i;
3616 }
3617 p = endp;
3618 }
3619 } /* if (what != SHAPE_MOUSE) */
3620
3621 if (*p == '-')
3622 ++p;
3623 }
3624 }
3625 modep = p;
3626 if (*modep == ',')
3627 ++modep;
3628 }
3629 }
3630
3631 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3632 if (!found_ve)
3633 {
3634#ifdef FEAT_MOUSESHAPE
3635 if (what == SHAPE_MOUSE)
3636 {
3637 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3638 }
3639 else
3640#endif
3641 {
3642 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3643 shape_table[SHAPE_IDX_VE].percentage =
3644 shape_table[SHAPE_IDX_V].percentage;
3645 shape_table[SHAPE_IDX_VE].blinkwait =
3646 shape_table[SHAPE_IDX_V].blinkwait;
3647 shape_table[SHAPE_IDX_VE].blinkon =
3648 shape_table[SHAPE_IDX_V].blinkon;
3649 shape_table[SHAPE_IDX_VE].blinkoff =
3650 shape_table[SHAPE_IDX_V].blinkoff;
3651 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3652 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3653 }
3654 }
3655
3656 return NULL;
3657}
3658
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003659# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3660 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661/*
3662 * Return the index into shape_table[] for the current mode.
3663 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3664 */
3665 int
3666get_shape_idx(mouse)
3667 int mouse;
3668{
3669#ifdef FEAT_MOUSESHAPE
3670 if (mouse && (State == HITRETURN || State == ASKMORE))
3671 {
3672# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003673 int x, y;
3674 gui_mch_getmouse(&x, &y);
3675 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return SHAPE_IDX_MOREL;
3677# endif
3678 return SHAPE_IDX_MORE;
3679 }
3680 if (mouse && drag_status_line)
3681 return SHAPE_IDX_SDRAG;
3682# ifdef FEAT_VERTSPLIT
3683 if (mouse && drag_sep_line)
3684 return SHAPE_IDX_VDRAG;
3685# endif
3686#endif
3687 if (!mouse && State == SHOWMATCH)
3688 return SHAPE_IDX_SM;
3689#ifdef FEAT_VREPLACE
3690 if (State & VREPLACE_FLAG)
3691 return SHAPE_IDX_R;
3692#endif
3693 if (State & REPLACE_FLAG)
3694 return SHAPE_IDX_R;
3695 if (State & INSERT)
3696 return SHAPE_IDX_I;
3697 if (State & CMDLINE)
3698 {
3699 if (cmdline_at_end())
3700 return SHAPE_IDX_C;
3701 if (cmdline_overstrike())
3702 return SHAPE_IDX_CR;
3703 return SHAPE_IDX_CI;
3704 }
3705 if (finish_op)
3706 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 if (VIsual_active)
3708 {
3709 if (*p_sel == 'e')
3710 return SHAPE_IDX_VE;
3711 else
3712 return SHAPE_IDX_V;
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return SHAPE_IDX_N;
3715}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3719static int old_mouse_shape = 0;
3720
3721/*
3722 * Set the mouse shape:
3723 * If "shape" is -1, use shape depending on the current mode,
3724 * depending on the current state.
3725 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3726 * when the mouse moves off the status or command line).
3727 */
3728 void
3729update_mouseshape(shape_idx)
3730 int shape_idx;
3731{
3732 int new_mouse_shape;
3733
3734 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003735 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 return;
3737
3738 /* Postpone the updating when more is to come. Speeds up executing of
3739 * mappings. */
3740 if (shape_idx == -1 && char_avail())
3741 {
3742 postponed_mouseshape = TRUE;
3743 return;
3744 }
3745
Bram Moolenaar14716812006-05-04 21:54:08 +00003746 /* When ignoring the mouse don't change shape on the statusline. */
3747 if (*p_mouse == NUL
3748 && (shape_idx == SHAPE_IDX_CLINE
3749 || shape_idx == SHAPE_IDX_STATUS
3750 || shape_idx == SHAPE_IDX_VSEP))
3751 shape_idx = -2;
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (shape_idx == -2
3754 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3755 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3756 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3757 return;
3758 if (shape_idx < 0)
3759 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3760 else
3761 new_mouse_shape = shape_table[shape_idx].mshape;
3762 if (new_mouse_shape != old_mouse_shape)
3763 {
3764 mch_set_mouse_shape(new_mouse_shape);
3765 old_mouse_shape = new_mouse_shape;
3766 }
3767 postponed_mouseshape = FALSE;
3768}
3769# endif
3770
3771#endif /* CURSOR_SHAPE */
3772
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774/* TODO: make some #ifdef for this */
3775/*--------[ file searching ]-------------------------------------------------*/
3776/*
3777 * File searching functions for 'path', 'tags' and 'cdpath' options.
3778 * External visible functions:
3779 * vim_findfile_init() creates/initialises the search context
3780 * vim_findfile_free_visited() free list of visited files/dirs of search
3781 * context
3782 * vim_findfile() find a file in the search context
3783 * vim_findfile_cleanup() cleanup/free search context created by
3784 * vim_findfile_init()
3785 *
3786 * All static functions and variables start with 'ff_'
3787 *
3788 * In general it works like this:
3789 * First you create yourself a search context by calling vim_findfile_init().
3790 * It is possible to give a search context from a previous call to
3791 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3792 * until you are satisfied with the result or it returns NULL. On every call it
3793 * returns the next file which matches the conditions given to
3794 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3795 *
3796 * It is possible to call vim_findfile_init() again to reinitialise your search
3797 * with some new parameters. Don't forget to pass your old search context to
3798 * it, so it can reuse it and especially reuse the list of already visited
3799 * directories. If you want to delete the list of already visited directories
3800 * simply call vim_findfile_free_visited().
3801 *
3802 * When you are done call vim_findfile_cleanup() to free the search context.
3803 *
3804 * The function vim_findfile_init() has a long comment, which describes the
3805 * needed parameters.
3806 *
3807 *
3808 *
3809 * ATTENTION:
3810 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003811 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 * thread-safe!!!!!
3813 *
3814 * To minimize parameter passing (or because I'm to lazy), only the
3815 * external visible functions get a search context as a parameter. This is
3816 * then assigned to a static global, which is used throughout the local
3817 * functions.
3818 */
3819
3820/*
3821 * type for the directory search stack
3822 */
3823typedef struct ff_stack
3824{
3825 struct ff_stack *ffs_prev;
3826
3827 /* the fix part (no wildcards) and the part containing the wildcards
3828 * of the search path
3829 */
3830 char_u *ffs_fix_path;
3831#ifdef FEAT_PATH_EXTRA
3832 char_u *ffs_wc_path;
3833#endif
3834
3835 /* files/dirs found in the above directory, matched by the first wildcard
3836 * of wc_part
3837 */
3838 char_u **ffs_filearray;
3839 int ffs_filearray_size;
3840 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3841
3842 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003843 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 int ffs_stage;
3847
3848 /* How deep are we in the directory tree?
3849 * Counts backward from value of level parameter to vim_findfile_init
3850 */
3851 int ffs_level;
3852
3853 /* Did we already expand '**' to an empty string? */
3854 int ffs_star_star_empty;
3855} ff_stack_T;
3856
3857/*
3858 * type for already visited directories or files.
3859 */
3860typedef struct ff_visited
3861{
3862 struct ff_visited *ffv_next;
3863
3864#ifdef FEAT_PATH_EXTRA
3865 /* Visited directories are different if the wildcard string are
3866 * different. So we have to save it.
3867 */
3868 char_u *ffv_wc_path;
3869#endif
3870 /* for unix use inode etc for comparison (needed because of links), else
3871 * use filename.
3872 */
3873#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003874 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3875 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 ino_t ffv_ino; /* inode number */
3877#endif
3878 /* The memory for this struct is allocated according to the length of
3879 * ffv_fname.
3880 */
3881 char_u ffv_fname[1]; /* actually longer */
3882} ff_visited_T;
3883
3884/*
3885 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003886 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3888 * So we have to do 3 searches:
3889 * 1) search from the current files directory downward for the file "tags"
3890 * 2) search from the current files directory downward for the file "TAGS"
3891 * 3) search from Vims current directory downwards for the file "tags"
3892 * As you can see, the first and the third search are for the same file, so for
3893 * the third search we can use the visited list of the first search. For the
3894 * second search we must start from a empty visited list.
3895 * The struct ff_visited_list_hdr is used to manage a linked list of already
3896 * visited lists.
3897 */
3898typedef struct ff_visited_list_hdr
3899{
3900 struct ff_visited_list_hdr *ffvl_next;
3901
3902 /* the filename the attached visited list is for */
3903 char_u *ffvl_filename;
3904
3905 ff_visited_T *ffvl_visited_list;
3906
3907} ff_visited_list_hdr_T;
3908
3909
3910/*
3911 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003912 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 */
3914#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916/*
3917 * The search context:
3918 * ffsc_stack_ptr: the stack for the dirs to search
3919 * ffsc_visited_list: the currently active visited list
3920 * ffsc_dir_visited_list: the currently active visited list for search dirs
3921 * ffsc_visited_lists_list: the list of all visited lists
3922 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3923 * ffsc_file_to_search: the file to search for
3924 * ffsc_start_dir: the starting directory, if search path was relative
3925 * ffsc_fix_path: the fix part of the given path (without wildcards)
3926 * Needed for upward search.
3927 * ffsc_wc_path: the part of the given path containing wildcards
3928 * ffsc_level: how many levels of dirs to search downwards
3929 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003930 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003931 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 */
3933typedef struct ff_search_ctx_T
3934{
3935 ff_stack_T *ffsc_stack_ptr;
3936 ff_visited_list_hdr_T *ffsc_visited_list;
3937 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3938 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3939 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3940 char_u *ffsc_file_to_search;
3941 char_u *ffsc_start_dir;
3942 char_u *ffsc_fix_path;
3943#ifdef FEAT_PATH_EXTRA
3944 char_u *ffsc_wc_path;
3945 int ffsc_level;
3946 char_u **ffsc_stopdirs_v;
3947#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003948 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003949 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003950} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952/* locally needed functions */
3953#ifdef FEAT_PATH_EXTRA
3954static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *));
3955#else
3956static int ff_check_visited __ARGS((ff_visited_T **, char_u *));
3957#endif
3958static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp));
3959static void ff_free_visited_list __ARGS((ff_visited_T *vl));
3960static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp));
3961#ifdef FEAT_PATH_EXTRA
3962static int ff_wc_equal __ARGS((char_u *s1, char_u *s2));
3963#endif
3964
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003965static void ff_push __ARGS((ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr));
3966static ff_stack_T *ff_pop __ARGS((ff_search_ctx_T *search_ctx));
3967static void ff_clear __ARGS((ff_search_ctx_T *search_ctx));
3968static void ff_free_stack_element __ARGS((ff_stack_T *stack_ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969#ifdef FEAT_PATH_EXTRA
3970static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int));
3971#else
3972static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int));
3973#endif
3974#ifdef FEAT_PATH_EXTRA
3975static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **));
3976#endif
3977
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01003978static char_u e_pathtoolong[] = N_("E854: path too long for completion");
3979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980#if 0
3981/*
3982 * if someone likes findfirst/findnext, here are the functions
3983 * NOT TESTED!!
3984 */
3985
3986static void *ff_fn_search_context = NULL;
3987
3988 char_u *
3989vim_findfirst(path, filename, level)
3990 char_u *path;
3991 char_u *filename;
3992 int level;
3993{
3994 ff_fn_search_context =
3995 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
3996 ff_fn_search_context, rel_fname);
3997 if (NULL == ff_fn_search_context)
3998 return NULL;
3999 else
4000 return vim_findnext()
4001}
4002
4003 char_u *
4004vim_findnext()
4005{
4006 char_u *ret = vim_findfile(ff_fn_search_context);
4007
4008 if (NULL == ret)
4009 {
4010 vim_findfile_cleanup(ff_fn_search_context);
4011 ff_fn_search_context = NULL;
4012 }
4013 return ret;
4014}
4015#endif
4016
4017/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004018 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004020 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 *
4022 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4023 * with the search context.
4024 *
4025 * Find the file 'filename' in the directory 'path'.
4026 * The parameter 'path' may contain wildcards. If so only search 'level'
4027 * directories deep. The parameter 'level' is the absolute maximum and is
4028 * not related to restricts given to the '**' wildcard. If 'level' is 100
4029 * and you use '**200' vim_findfile() will stop after 100 levels.
4030 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004031 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4032 * escape special characters.
4033 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4035 * restarted on the next higher directory level. This is repeated until the
4036 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4037 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4038 *
4039 * If the 'path' is relative, the starting dir for the search is either VIM's
4040 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004041 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * the first wildcard.
4043 *
4044 * Upward search is only done on the starting dir.
4045 *
4046 * If 'free_visited' is TRUE the list of already visited files/directories is
4047 * cleared. Set this to FALSE if you just want to search from another
4048 * directory, but want to be sure that no directory from a previous search is
4049 * searched again. This is useful if you search for a file at different places.
4050 * The list of visited files/dirs can also be cleared with the function
4051 * vim_findfile_free_visited().
4052 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004053 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4054 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 *
4056 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004057 * passed in the parameter "search_ctx_arg". This context is reused and
4058 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004060 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4061 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004063 * If you don't have a search context from a previous call "search_ctx_arg"
4064 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 *
4066 * This function silently ignores a few errors, vim_findfile() will have
4067 * limited functionality then.
4068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 void *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004070vim_findfile_init(path, filename, stopdirs, level, free_visited, find_what,
4071 search_ctx_arg, tagfile, rel_fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 char_u *path;
4073 char_u *filename;
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00004074 char_u *stopdirs UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 int level;
4076 int free_visited;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004077 int find_what;
4078 void *search_ctx_arg;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004079 int tagfile; /* expanding names of tags files */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 char_u *rel_fname; /* file name to use for "." */
4081{
4082#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004083 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004085 ff_stack_T *sptr;
4086 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 /* If a search context is given by the caller, reuse it, else allocate a
4089 * new one.
4090 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004091 if (search_ctx_arg != NULL)
4092 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 else
4094 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004095 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4096 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004098 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004100 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004101 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004104 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105
4106 /* clear visited list if wanted */
4107 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004108 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 else
4110 {
4111 /* Reuse old visited lists. Get the visited list for the given
4112 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004113 * one. */
4114 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4115 &search_ctx->ffsc_visited_lists_list);
4116 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004118 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4119 &search_ctx->ffsc_dir_visited_lists_list);
4120 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 goto error_return;
4122 }
4123
4124 if (ff_expand_buffer == NULL)
4125 {
4126 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4127 if (ff_expand_buffer == NULL)
4128 goto error_return;
4129 }
4130
4131 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004132 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (path[0] == '.'
4134 && (vim_ispathsep(path[1]) || path[1] == NUL)
4135 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4136 && rel_fname != NULL)
4137 {
4138 int len = (int)(gettail(rel_fname) - rel_fname);
4139
4140 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4141 {
4142 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004143 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004144 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004147 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4148 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 goto error_return;
4150 if (*++path != NUL)
4151 ++path;
4152 }
4153 else if (*path == NUL || !vim_isAbsName(path))
4154 {
4155#ifdef BACKSLASH_IN_FILENAME
4156 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4157 if (*path != NUL && path[1] == ':')
4158 {
4159 char_u drive[3];
4160
4161 drive[0] = path[0];
4162 drive[1] = ':';
4163 drive[2] = NUL;
4164 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4165 goto error_return;
4166 path += 2;
4167 }
4168 else
4169#endif
4170 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4171 goto error_return;
4172
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004173 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4174 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 goto error_return;
4176
4177#ifdef BACKSLASH_IN_FILENAME
4178 /* A path that starts with "/dir" is relative to the drive, not to the
4179 * directory (but not for "//machine/dir"). Only use the drive name. */
4180 if ((*path == '/' || *path == '\\')
4181 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004182 && search_ctx->ffsc_start_dir[1] == ':')
4183 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#endif
4185 }
4186
4187#ifdef FEAT_PATH_EXTRA
4188 /*
4189 * If stopdirs are given, split them into an array of pointers.
4190 * If this fails (mem allocation), there is no upward search at all or a
4191 * stop directory is not recognized -> continue silently.
4192 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004193 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 * is handled as unlimited upward search. See function
4195 * ff_path_in_stoplist() for details.
4196 */
4197 if (stopdirs != NULL)
4198 {
4199 char_u *walker = stopdirs;
4200 int dircount;
4201
4202 while (*walker == ';')
4203 walker++;
4204
4205 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004206 search_ctx->ffsc_stopdirs_v =
4207 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004209 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
4211 do
4212 {
4213 char_u *helper;
4214 void *ptr;
4215
4216 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004217 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 (dircount + 1) * sizeof(char_u *));
4219 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004220 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 else
4222 /* ignore, keep what we have and continue */
4223 break;
4224 walker = vim_strchr(walker, ';');
4225 if (walker)
4226 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004227 search_ctx->ffsc_stopdirs_v[dircount-1] =
4228 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 walker++;
4230 }
4231 else
4232 /* this might be "", which means ascent till top
4233 * of directory tree.
4234 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004235 search_ctx->ffsc_stopdirs_v[dircount-1] =
4236 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 dircount++;
4239
4240 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004241 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244#endif
4245
4246#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004247 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /* split into:
4250 * -fix path
4251 * -wildcard_stuff (might be NULL)
4252 */
4253 wc_part = vim_strchr(path, '*');
4254 if (wc_part != NULL)
4255 {
4256 int llevel;
4257 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004258 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004261 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /*
4264 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004265 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4267 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4268 * For EBCDIC you get different character values.
4269 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004270 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 * string.
4272 */
4273 len = 0;
4274 while (*wc_part != NUL)
4275 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004276 if (len + 5 >= MAXPATHL)
4277 {
4278 EMSG(_(e_pathtoolong));
4279 break;
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (STRNCMP(wc_part, "**", 2) == 0)
4282 {
4283 ff_expand_buffer[len++] = *wc_part++;
4284 ff_expand_buffer[len++] = *wc_part++;
4285
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004286 llevel = strtol((char *)wc_part, &errpt, 10);
4287 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004289 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 /* restrict is 0 -> remove already added '**' */
4291 len -= 2;
4292 else
4293 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004294 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004295 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 {
4297 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4298 goto error_return;
4299 }
4300 }
4301 else
4302 ff_expand_buffer[len++] = *wc_part++;
4303 }
4304 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004305 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004307 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 goto error_return;
4309 }
4310 else
4311#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004312 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004314 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 /* store the fix part as startdir.
4317 * This is needed if the parameter path is fully qualified.
4318 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004319 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004320 if (search_ctx->ffsc_start_dir == NULL)
4321 goto error_return;
4322 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324
4325 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004326 if (STRLEN(search_ctx->ffsc_start_dir)
4327 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4328 {
4329 EMSG(_(e_pathtoolong));
4330 goto error_return;
4331 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004332 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004334 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004335 int eb_len = (int)STRLEN(ff_expand_buffer);
4336 char_u *buf = alloc(eb_len
4337 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004338
4339 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004340 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004341 if (mch_isdir(buf))
4342 {
4343 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4344 add_pathsep(ff_expand_buffer);
4345 }
4346#ifdef FEAT_PATH_EXTRA
4347 else
4348 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004349 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004350 char_u *wc_path = NULL;
4351 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004352 int len = 0;
4353
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004354 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004355 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004356 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004357 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4358 add_pathsep(ff_expand_buffer);
4359 }
4360 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004361 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004362
4363 if (search_ctx->ffsc_wc_path != NULL)
4364 {
4365 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004366 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004367 + STRLEN(search_ctx->ffsc_fix_path + len)
4368 + 1));
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004369 if (temp == NULL || wc_path == NULL)
4370 {
4371 vim_free(buf);
4372 vim_free(temp);
4373 vim_free(wc_path);
4374 goto error_return;
4375 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004376
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004377 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4378 STRCAT(temp, search_ctx->ffsc_wc_path);
4379 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004380 vim_free(wc_path);
Bram Moolenaarc79a5452015-09-29 12:08:42 +02004381 search_ctx->ffsc_wc_path = temp;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004382 }
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004383 }
4384#endif
4385 vim_free(buf);
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 sptr = ff_create_stack_element(ff_expand_buffer,
4389#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004390 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#endif
4392 level, 0);
4393
4394 if (sptr == NULL)
4395 goto error_return;
4396
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004397 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004399 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4400 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 goto error_return;
4402
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004403 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405error_return:
4406 /*
4407 * We clear the search context now!
4408 * Even when the caller gave us a (perhaps valid) context we free it here,
4409 * as we might have already destroyed it.
4410 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004411 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 return NULL;
4413}
4414
4415#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4416/*
4417 * Get the stopdir string. Check that ';' is not escaped.
4418 */
4419 char_u *
4420vim_findfile_stopdir(buf)
4421 char_u *buf;
4422{
4423 char_u *r_ptr = buf;
4424
4425 while (*r_ptr != NUL && *r_ptr != ';')
4426 {
4427 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4428 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004429 /* Overwrite the escape char,
4430 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004431 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 r_ptr++;
4433 }
4434 r_ptr++;
4435 }
4436 if (*r_ptr == ';')
4437 {
4438 *r_ptr = 0;
4439 r_ptr++;
4440 }
4441 else if (*r_ptr == NUL)
4442 r_ptr = NULL;
4443 return r_ptr;
4444}
4445#endif
4446
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004447/*
4448 * Clean up the given search context. Can handle a NULL pointer.
4449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 void
4451vim_findfile_cleanup(ctx)
4452 void *ctx;
4453{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004454 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return;
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004458 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460}
4461
4462/*
4463 * Find a file in a search context.
4464 * The search context was created with vim_findfile_init() above.
4465 * Return a pointer to an allocated file name or NULL if nothing found.
4466 * To get all matching files call this function until you get NULL.
4467 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004468 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 *
4470 * The search algorithm is depth first. To change this replace the
4471 * stack with a list (don't forget to leave partly searched directories on the
4472 * top of the list).
4473 */
4474 char_u *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004475vim_findfile(search_ctx_arg)
4476 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
4478 char_u *file_path;
4479#ifdef FEAT_PATH_EXTRA
4480 char_u *rest_of_wildcards;
4481 char_u *path_end = NULL;
4482#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004483 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4485 int len;
4486#endif
4487 int i;
4488 char_u *p;
4489#ifdef FEAT_SEARCHPATH
4490 char_u *suf;
4491#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004492 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004494 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 return NULL;
4496
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004497 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 /*
4500 * filepath is used as buffer for various actions and as the storage to
4501 * return a found filename.
4502 */
4503 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4504 return NULL;
4505
4506#ifdef FEAT_PATH_EXTRA
4507 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004508 if (search_ctx->ffsc_start_dir != NULL)
4509 path_end = &search_ctx->ffsc_start_dir[
4510 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#endif
4512
4513#ifdef FEAT_PATH_EXTRA
4514 /* upward search loop */
4515 for (;;)
4516 {
4517#endif
4518 /* downward search loop */
4519 for (;;)
4520 {
4521 /* check if user user wants to stop the search*/
4522 ui_breakcheck();
4523 if (got_int)
4524 break;
4525
4526 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004527 stackp = ff_pop(search_ctx);
4528 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530
4531 /*
4532 * TODO: decide if we leave this test in
4533 *
4534 * GOOD: don't search a directory(-tree) twice.
4535 * BAD: - check linked list for every new directory entered.
4536 * - check for double files also done below
4537 *
4538 * Here we check if we already searched this directory.
4539 * We already searched a directory if:
4540 * 1) The directory is the same.
4541 * 2) We would use the same wildcard string.
4542 *
4543 * Good if you have links on same directory via several ways
4544 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4545 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4546 *
4547 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004548 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004550 if (stackp->ffs_filearray == NULL
4551 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004553 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004555 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#endif
4557 ) == FAIL)
4558 {
4559#ifdef FF_VERBOSE
4560 if (p_verbose >= 5)
4561 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004562 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004564 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 /* don't overwrite this either */
4566 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004567 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004570 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 continue;
4572 }
4573#ifdef FF_VERBOSE
4574 else if (p_verbose >= 5)
4575 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004576 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004577 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004578 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 /* don't overwrite this either */
4580 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004581 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583#endif
4584
4585 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004586 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004588 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 continue;
4590 }
4591
4592 file_path[0] = NUL;
4593
4594 /*
4595 * If no filearray till now expand wildcards
4596 * The function expand_wildcards() can handle an array of paths
4597 * and all possible expands are returned in one array. We use this
4598 * to handle the expansion of '**' into an empty string.
4599 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004600 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
4602 char_u *dirptrs[2];
4603
4604 /* we use filepath to build the path expand_wildcards() should
4605 * expand.
4606 */
4607 dirptrs[0] = file_path;
4608 dirptrs[1] = NULL;
4609
4610 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004611 if (!vim_isAbsName(stackp->ffs_fix_path)
4612 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004614 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 add_pathsep(file_path);
4616 }
4617
4618 /* append the fix part of the search path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004619 STRCAT(file_path, stackp->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 add_pathsep(file_path);
4621
4622#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004623 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (*rest_of_wildcards != NUL)
4625 {
4626 len = (int)STRLEN(file_path);
4627 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4628 {
4629 /* pointer to the restrict byte
4630 * The restrict byte is not a character!
4631 */
4632 p = rest_of_wildcards + 2;
4633
4634 if (*p > 0)
4635 {
4636 (*p)--;
4637 file_path[len++] = '*';
4638 }
4639
4640 if (*p == 0)
4641 {
4642 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004643 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 else
4646 rest_of_wildcards += 3;
4647
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004648 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
4650 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004651 stackp->ffs_star_star_empty = 1;
4652 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 }
4655
4656 /*
4657 * Here we copy until the next path separator or the end of
4658 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004659 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 * pushing every directory returned from expand_wildcards()
4661 * on the stack again for further search.
4662 */
4663 while (*rest_of_wildcards
4664 && !vim_ispathsep(*rest_of_wildcards))
4665 file_path[len++] = *rest_of_wildcards++;
4666
4667 file_path[len] = NUL;
4668 if (vim_ispathsep(*rest_of_wildcards))
4669 rest_of_wildcards++;
4670 }
4671#endif
4672
4673 /*
4674 * Expand wildcards like "*" and "$VAR".
4675 * If the path is a URL don't try this.
4676 */
4677 if (path_with_url(dirptrs[0]))
4678 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004679 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004681 if (stackp->ffs_filearray != NULL
4682 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004684 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004686 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004689 /* Add EW_NOTWILD because the expanded path may contain
4690 * wildcard characters that are to be taken literally.
4691 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004693 &stackp->ffs_filearray_size,
4694 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004695 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004697 stackp->ffs_filearray_cur = 0;
4698 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700#ifdef FEAT_PATH_EXTRA
4701 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004702 rest_of_wildcards = &stackp->ffs_wc_path[
4703 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704#endif
4705
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004706 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
4708 /* this is the first time we work on this directory */
4709#ifdef FEAT_PATH_EXTRA
4710 if (*rest_of_wildcards == NUL)
4711#endif
4712 {
4713 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004714 * We don't have further wildcards to expand, so we have to
4715 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004717 for (i = stackp->ffs_filearray_cur;
4718 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004720 if (!path_with_url(stackp->ffs_filearray[i])
4721 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 continue; /* not a directory */
4723
Bram Moolenaar21160b92009-11-11 15:56:10 +00004724 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 * below */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004726 STRCPY(file_path, stackp->ffs_filearray[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004728 STRCAT(file_path, search_ctx->ffsc_file_to_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 /*
4731 * Try without extra suffix and then with suffixes
4732 * from 'suffixesadd'.
4733 */
4734#ifdef FEAT_SEARCHPATH
4735 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004736 if (search_ctx->ffsc_tagfile)
4737 suf = (char_u *)"";
4738 else
4739 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 for (;;)
4741#endif
4742 {
4743 /* if file exists and we didn't already find it */
4744 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004745 || (mch_getperm(file_path) >= 0
4746 && (search_ctx->ffsc_find_what
4747 == FINDFILE_BOTH
4748 || ((search_ctx->ffsc_find_what
4749 == FINDFILE_DIR)
4750 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#ifndef FF_VERBOSE
4752 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004753 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 file_path
4755#ifdef FEAT_PATH_EXTRA
4756 , (char_u *)""
4757#endif
4758 ) == OK)
4759#endif
4760 )
4761 {
4762#ifdef FF_VERBOSE
4763 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004764 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 file_path
4766#ifdef FEAT_PATH_EXTRA
4767 , (char_u *)""
4768#endif
4769 ) == FAIL)
4770 {
4771 if (p_verbose >= 5)
4772 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004773 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004774 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 file_path);
4776 /* don't overwrite this either */
4777 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004778 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 continue;
4781 }
4782#endif
4783
4784 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004785 stackp->ffs_filearray_cur = i + 1;
4786 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004788 if (!path_with_url(file_path))
4789 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4791 == OK)
4792 {
4793 p = shorten_fname(file_path,
4794 ff_expand_buffer);
4795 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004796 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798#ifdef FF_VERBOSE
4799 if (p_verbose >= 5)
4800 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004801 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004802 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 /* don't overwrite this either */
4804 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004805 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807#endif
4808 return file_path;
4809 }
4810
4811#ifdef FEAT_SEARCHPATH
4812 /* Not found or found already, try next suffix. */
4813 if (*suf == NUL)
4814 break;
4815 copy_option_part(&suf, file_path + len,
4816 MAXPATHL - len, ",");
4817#endif
4818 }
4819 }
4820 }
4821#ifdef FEAT_PATH_EXTRA
4822 else
4823 {
4824 /*
4825 * still wildcards left, push the directories for further
4826 * search
4827 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004828 for (i = stackp->ffs_filearray_cur;
4829 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004831 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 continue; /* not a directory */
4833
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004834 ff_push(search_ctx,
4835 ff_create_stack_element(
4836 stackp->ffs_filearray[i],
4837 rest_of_wildcards,
4838 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 }
4841#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004842 stackp->ffs_filearray_cur = 0;
4843 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845
4846#ifdef FEAT_PATH_EXTRA
4847 /*
4848 * if wildcards contains '**' we have to descent till we reach the
4849 * leaves of the directory tree.
4850 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004851 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004853 for (i = stackp->ffs_filearray_cur;
4854 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004856 if (fnamecmp(stackp->ffs_filearray[i],
4857 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004859 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004861 ff_push(search_ctx,
4862 ff_create_stack_element(stackp->ffs_filearray[i],
4863 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
4866#endif
4867
4868 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004869 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 }
4872
4873#ifdef FEAT_PATH_EXTRA
4874 /* If we reached this, we didn't find anything downwards.
4875 * Let's check if we should do an upward search.
4876 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004877 if (search_ctx->ffsc_start_dir
4878 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 ff_stack_T *sptr;
4881
4882 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004883 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4884 (int)(path_end - search_ctx->ffsc_start_dir),
4885 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 break;
4887
4888 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004889 while (path_end > search_ctx->ffsc_start_dir
4890 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004892 while (path_end > search_ctx->ffsc_start_dir
4893 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 path_end--;
4895 *path_end = 0;
4896 path_end--;
4897
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004898 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 break;
4900
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004901 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004903 STRCAT(file_path, search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904
4905 /* create a new stack entry */
4906 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004907 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (sptr == NULL)
4909 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004910 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else
4913 break;
4914 }
4915#endif
4916
4917 vim_free(file_path);
4918 return NULL;
4919}
4920
4921/*
4922 * Free the list of lists of visited files and directories
4923 * Can handle it if the passed search_context is NULL;
4924 */
4925 void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004926vim_findfile_free_visited(search_ctx_arg)
4927 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004929 ff_search_ctx_T *search_ctx;
4930
4931 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return;
4933
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004934 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
4935 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
4936 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937}
4938
4939 static void
4940vim_findfile_free_visited_list(list_headp)
4941 ff_visited_list_hdr_T **list_headp;
4942{
4943 ff_visited_list_hdr_T *vp;
4944
4945 while (*list_headp != NULL)
4946 {
4947 vp = (*list_headp)->ffvl_next;
4948 ff_free_visited_list((*list_headp)->ffvl_visited_list);
4949
4950 vim_free((*list_headp)->ffvl_filename);
4951 vim_free(*list_headp);
4952 *list_headp = vp;
4953 }
4954 *list_headp = NULL;
4955}
4956
4957 static void
4958ff_free_visited_list(vl)
4959 ff_visited_T *vl;
4960{
4961 ff_visited_T *vp;
4962
4963 while (vl != NULL)
4964 {
4965 vp = vl->ffv_next;
4966#ifdef FEAT_PATH_EXTRA
4967 vim_free(vl->ffv_wc_path);
4968#endif
4969 vim_free(vl);
4970 vl = vp;
4971 }
4972 vl = NULL;
4973}
4974
4975/*
4976 * Returns the already visited list for the given filename. If none is found it
4977 * allocates a new one.
4978 */
4979 static ff_visited_list_hdr_T*
4980ff_get_visited_list(filename, list_headp)
4981 char_u *filename;
4982 ff_visited_list_hdr_T **list_headp;
4983{
4984 ff_visited_list_hdr_T *retptr = NULL;
4985
4986 /* check if a visited list for the given filename exists */
4987 if (*list_headp != NULL)
4988 {
4989 retptr = *list_headp;
4990 while (retptr != NULL)
4991 {
4992 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
4993 {
4994#ifdef FF_VERBOSE
4995 if (p_verbose >= 5)
4996 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004997 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004998 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 filename);
5000 /* don't overwrite this either */
5001 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005002 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
5004#endif
5005 return retptr;
5006 }
5007 retptr = retptr->ffvl_next;
5008 }
5009 }
5010
5011#ifdef FF_VERBOSE
5012 if (p_verbose >= 5)
5013 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005014 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005015 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 /* don't overwrite this either */
5017 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005018 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020#endif
5021
5022 /*
5023 * if we reach this we didn't find a list and we have to allocate new list
5024 */
5025 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5026 if (retptr == NULL)
5027 return NULL;
5028
5029 retptr->ffvl_visited_list = NULL;
5030 retptr->ffvl_filename = vim_strsave(filename);
5031 if (retptr->ffvl_filename == NULL)
5032 {
5033 vim_free(retptr);
5034 return NULL;
5035 }
5036 retptr->ffvl_next = *list_headp;
5037 *list_headp = retptr;
5038
5039 return retptr;
5040}
5041
5042#ifdef FEAT_PATH_EXTRA
5043/*
5044 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5045 * They are equal if:
5046 * - both paths are NULL
5047 * - they have the same length
5048 * - char by char comparison is OK
5049 * - the only differences are in the counters behind a '**', so
5050 * '**\20' is equal to '**\24'
5051 */
5052 static int
5053ff_wc_equal(s1, s2)
5054 char_u *s1;
5055 char_u *s2;
5056{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005057 int i, j;
Bram Moolenaard43f0952015-08-27 22:30:47 +02005058 int c1 = NUL;
5059 int c2 = NUL;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005060 int prev1 = NUL;
5061 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 if (s1 == s2)
5064 return TRUE;
5065
5066 if (s1 == NULL || s2 == NULL)
5067 return FALSE;
5068
Bram Moolenaard43f0952015-08-27 22:30:47 +02005069 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaard43f0952015-08-27 22:30:47 +02005071 c1 = PTR2CHAR(s1 + i);
5072 c2 = PTR2CHAR(s2 + j);
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005073
5074 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5075 && (prev1 != '*' || prev2 != '*'))
Bram Moolenaard43f0952015-08-27 22:30:47 +02005076 return FALSE;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005077 prev2 = prev1;
5078 prev1 = c1;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005079
5080 i += MB_PTR2LEN(s1 + i);
5081 j += MB_PTR2LEN(s2 + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
Bram Moolenaar4d0c7bc2015-09-25 16:38:01 +02005083 return s1[i] == s2[j];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084}
5085#endif
5086
5087/*
5088 * maintains the list of already visited files and dirs
5089 * returns FAIL if the given file/dir is already in the list
5090 * returns OK if it is newly added
5091 *
5092 * TODO: What to do on memory allocation problems?
5093 * -> return TRUE - Better the file is found several times instead of
5094 * never.
5095 */
5096 static int
5097ff_check_visited(visited_list, fname
5098#ifdef FEAT_PATH_EXTRA
5099 , wc_path
5100#endif
5101 )
5102 ff_visited_T **visited_list;
5103 char_u *fname;
5104#ifdef FEAT_PATH_EXTRA
5105 char_u *wc_path;
5106#endif
5107{
5108 ff_visited_T *vp;
5109#ifdef UNIX
5110 struct stat st;
5111 int url = FALSE;
5112#endif
5113
5114 /* For an URL we only compare the name, otherwise we compare the
5115 * device/inode (unix) or the full path name (not Unix). */
5116 if (path_with_url(fname))
5117 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005118 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119#ifdef UNIX
5120 url = TRUE;
5121#endif
5122 }
5123 else
5124 {
5125 ff_expand_buffer[0] = NUL;
5126#ifdef UNIX
5127 if (mch_stat((char *)fname, &st) < 0)
5128#else
5129 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5130#endif
5131 return FAIL;
5132 }
5133
5134 /* check against list of already visited files */
5135 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5136 {
5137 if (
5138#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005139 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5140 && vp->ffv_ino == st.st_ino)
5141 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142#endif
5143 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5144 )
5145 {
5146#ifdef FEAT_PATH_EXTRA
5147 /* are the wildcard parts equal */
5148 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5149#endif
5150 /* already visited */
5151 return FAIL;
5152 }
5153 }
5154
5155 /*
5156 * New file/dir. Add it to the list of visited files/dirs.
5157 */
5158 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5159 + STRLEN(ff_expand_buffer)));
5160
5161 if (vp != NULL)
5162 {
5163#ifdef UNIX
5164 if (!url)
5165 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005166 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 vp->ffv_ino = st.st_ino;
5168 vp->ffv_dev = st.st_dev;
5169 vp->ffv_fname[0] = NUL;
5170 }
5171 else
5172 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005173 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#endif
5175 STRCPY(vp->ffv_fname, ff_expand_buffer);
5176#ifdef UNIX
5177 }
5178#endif
5179#ifdef FEAT_PATH_EXTRA
5180 if (wc_path != NULL)
5181 vp->ffv_wc_path = vim_strsave(wc_path);
5182 else
5183 vp->ffv_wc_path = NULL;
5184#endif
5185
5186 vp->ffv_next = *visited_list;
5187 *visited_list = vp;
5188 }
5189
5190 return OK;
5191}
5192
5193/*
5194 * create stack element from given path pieces
5195 */
5196 static ff_stack_T *
5197ff_create_stack_element(fix_part,
5198#ifdef FEAT_PATH_EXTRA
5199 wc_part,
5200#endif
5201 level, star_star_empty)
5202 char_u *fix_part;
5203#ifdef FEAT_PATH_EXTRA
5204 char_u *wc_part;
5205#endif
5206 int level;
5207 int star_star_empty;
5208{
5209 ff_stack_T *new;
5210
5211 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5212 if (new == NULL)
5213 return NULL;
5214
5215 new->ffs_prev = NULL;
5216 new->ffs_filearray = NULL;
5217 new->ffs_filearray_size = 0;
5218 new->ffs_filearray_cur = 0;
5219 new->ffs_stage = 0;
5220 new->ffs_level = level;
5221 new->ffs_star_star_empty = star_star_empty;;
5222
5223 /* the following saves NULL pointer checks in vim_findfile */
5224 if (fix_part == NULL)
5225 fix_part = (char_u *)"";
5226 new->ffs_fix_path = vim_strsave(fix_part);
5227
5228#ifdef FEAT_PATH_EXTRA
5229 if (wc_part == NULL)
5230 wc_part = (char_u *)"";
5231 new->ffs_wc_path = vim_strsave(wc_part);
5232#endif
5233
5234 if (new->ffs_fix_path == NULL
5235#ifdef FEAT_PATH_EXTRA
5236 || new->ffs_wc_path == NULL
5237#endif
5238 )
5239 {
5240 ff_free_stack_element(new);
5241 new = NULL;
5242 }
5243
5244 return new;
5245}
5246
5247/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005248 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
5250 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005251ff_push(search_ctx, stack_ptr)
5252 ff_search_ctx_T *search_ctx;
5253 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005256 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005257 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005259 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5260 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262}
5263
5264/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005265 * Pop a dir from the directory stack.
5266 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 */
5268 static ff_stack_T *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005269ff_pop(search_ctx)
5270 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 ff_stack_T *sptr;
5273
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005274 sptr = search_ctx->ffsc_stack_ptr;
5275 if (search_ctx->ffsc_stack_ptr != NULL)
5276 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 return sptr;
5279}
5280
5281/*
5282 * free the given stack element
5283 */
5284 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005285ff_free_stack_element(stack_ptr)
5286 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005289 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005291 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#endif
5293
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005294 if (stack_ptr->ffs_filearray != NULL)
5295 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005297 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298}
5299
5300/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005301 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 */
5303 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005304ff_clear(search_ctx)
5305 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307 ff_stack_T *sptr;
5308
5309 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005310 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 ff_free_stack_element(sptr);
5312
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005313 vim_free(search_ctx->ffsc_file_to_search);
5314 vim_free(search_ctx->ffsc_start_dir);
5315 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005317 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#endif
5319
5320#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005321 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 int i = 0;
5324
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005325 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005327 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 i++;
5329 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005330 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005332 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333#endif
5334
5335 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005336 search_ctx->ffsc_file_to_search = NULL;
5337 search_ctx->ffsc_start_dir = NULL;
5338 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005340 search_ctx->ffsc_wc_path = NULL;
5341 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342#endif
5343}
5344
5345#ifdef FEAT_PATH_EXTRA
5346/*
5347 * check if the given path is in the stopdirs
5348 * returns TRUE if yes else FALSE
5349 */
5350 static int
5351ff_path_in_stoplist(path, path_len, stopdirs_v)
5352 char_u *path;
5353 int path_len;
5354 char_u **stopdirs_v;
5355{
5356 int i = 0;
5357
5358 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005359 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 path_len--;
5361
5362 /* if no path consider it as match */
5363 if (path_len == 0)
5364 return TRUE;
5365
5366 for (i = 0; stopdirs_v[i] != NULL; i++)
5367 {
5368 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5369 {
5370 /* match for parent directory. So '/home' also matches
5371 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5372 * '/home/r' would also match '/home/rks'
5373 */
5374 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005375 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 return TRUE;
5377 }
5378 else
5379 {
5380 if (fnamecmp(stopdirs_v[i], path) == 0)
5381 return TRUE;
5382 }
5383 }
5384 return FALSE;
5385}
5386#endif
5387
5388#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5389/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005390 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 *
5392 * On the first call set the parameter 'first' to TRUE to initialize
5393 * the search. For repeating calls to FALSE.
5394 *
5395 * Repeating calls will return other files called 'ptr[len]' from the path.
5396 *
5397 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5398 * don't need valid values.
5399 *
5400 * If nothing found on the first call the option FNAME_MESS will issue the
5401 * message:
5402 * 'Can't find file "<file>" in path'
5403 * On repeating calls:
5404 * 'No more file "<file>" found in path'
5405 *
5406 * options:
5407 * FNAME_MESS give error message when not found
5408 *
5409 * Uses NameBuff[]!
5410 *
5411 * Returns an allocated string for the file name. NULL for error.
5412 *
5413 */
5414 char_u *
5415find_file_in_path(ptr, len, options, first, rel_fname)
5416 char_u *ptr; /* file name */
5417 int len; /* length of file name */
5418 int options;
5419 int first; /* use count'th matching file name */
5420 char_u *rel_fname; /* file name searching relative to */
5421{
5422 return find_file_in_path_option(ptr, len, options, first,
5423 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005424 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425}
5426
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005427static char_u *ff_file_to_find = NULL;
5428static void *fdip_search_ctx = NULL;
5429
5430#if defined(EXITFREE)
5431 static void
5432free_findfile()
5433{
5434 vim_free(ff_file_to_find);
5435 vim_findfile_cleanup(fdip_search_ctx);
5436}
5437#endif
5438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439/*
5440 * Find the directory name "ptr[len]" in the path.
5441 *
5442 * options:
5443 * FNAME_MESS give error message when not found
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005444 * FNAME_UNESC unescape backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 *
5446 * Uses NameBuff[]!
5447 *
5448 * Returns an allocated string for the file name. NULL for error.
5449 */
5450 char_u *
5451find_directory_in_path(ptr, len, options, rel_fname)
5452 char_u *ptr; /* file name */
5453 int len; /* length of file name */
5454 int options;
5455 char_u *rel_fname; /* file name searching relative to */
5456{
5457 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005458 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005461 char_u *
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005462find_file_in_path_option(ptr, len, options, first, path_option,
5463 find_what, rel_fname, suffixes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 char_u *ptr; /* file name */
5465 int len; /* length of file name */
5466 int options;
5467 int first; /* use count'th matching file name */
5468 char_u *path_option; /* p_path or p_cdpath */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005469 int find_what; /* FINDFILE_FILE, _DIR or _BOTH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 char_u *rel_fname; /* file name we are looking relative to. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005471 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 static int did_findfile_init = FALSE;
5475 char_u save_char;
5476 char_u *file_name = NULL;
5477 char_u *buf = NULL;
5478 int rel_to_curdir;
5479#ifdef AMIGA
5480 struct Process *proc = (struct Process *)FindTask(0L);
5481 APTR save_winptr = proc->pr_WindowPtr;
5482
5483 /* Avoid a requester here for a volume that doesn't exist. */
5484 proc->pr_WindowPtr = (APTR)-1L;
5485#endif
5486
5487 if (first == TRUE)
5488 {
5489 /* copy file name into NameBuff, expanding environment variables */
5490 save_char = ptr[len];
5491 ptr[len] = NUL;
5492 expand_env(ptr, NameBuff, MAXPATHL);
5493 ptr[len] = save_char;
5494
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005495 vim_free(ff_file_to_find);
5496 ff_file_to_find = vim_strsave(NameBuff);
5497 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
5499 file_name = NULL;
5500 goto theend;
5501 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005502 if (options & FNAME_UNESC)
5503 {
5504 /* Change all "\ " to " ". */
5505 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
5506 if (ptr[0] == '\\' && ptr[1] == ' ')
5507 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
5508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005511 rel_to_curdir = (ff_file_to_find[0] == '.'
5512 && (ff_file_to_find[1] == NUL
5513 || vim_ispathsep(ff_file_to_find[1])
5514 || (ff_file_to_find[1] == '.'
5515 && (ff_file_to_find[2] == NUL
5516 || vim_ispathsep(ff_file_to_find[2])))));
5517 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 /* "..", "../path", "." and "./path": don't use the path_option */
5519 || rel_to_curdir
5520#if defined(MSWIN) || defined(MSDOS) || defined(OS2)
5521 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005522 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005523 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005524 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#endif
5526#ifdef AMIGA
5527 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005528 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#endif
5530 )
5531 {
5532 /*
5533 * Absolute path, no need to use "path_option".
5534 * If this is not a first call, return NULL. We already returned a
5535 * filename on the first call.
5536 */
5537 if (first == TRUE)
5538 {
5539 int l;
5540 int run;
5541
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005542 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005544 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 goto theend;
5546 }
5547
5548 /* When FNAME_REL flag given first use the directory of the file.
5549 * Otherwise or when this fails use the current directory. */
5550 for (run = 1; run <= 2; ++run)
5551 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005552 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 if (run == 1
5554 && rel_to_curdir
5555 && (options & FNAME_REL)
5556 && rel_fname != NULL
5557 && STRLEN(rel_fname) + l < MAXPATHL)
5558 {
5559 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005560 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 l = (int)STRLEN(NameBuff);
5562 }
5563 else
5564 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005565 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 run = 2;
5567 }
5568
5569 /* When the file doesn't exist, try adding parts of
5570 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005571 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 for (;;)
5573 {
5574 if (
5575#ifdef DJGPP
5576 /* "C:" by itself will fail for mch_getperm(),
5577 * assume it's always valid. */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005578 (find_what != FINDFILE_FILE && NameBuff[0] != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 && NameBuff[1] == ':'
5580 && NameBuff[2] == NUL) ||
5581#endif
5582 (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005583 && (find_what == FINDFILE_BOTH
5584 || ((find_what == FINDFILE_DIR)
5585 == mch_isdir(NameBuff)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 file_name = vim_strsave(NameBuff);
5588 goto theend;
5589 }
5590 if (*buf == NUL)
5591 break;
5592 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5593 }
5594 }
5595 }
5596 }
5597 else
5598 {
5599 /*
5600 * Loop over all paths in the 'path' or 'cdpath' option.
5601 * When "first" is set, first setup to the start of the option.
5602 * Otherwise continue to find the next match.
5603 */
5604 if (first == TRUE)
5605 {
5606 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005607 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 dir = path_option;
5609 did_findfile_init = FALSE;
5610 }
5611
5612 for (;;)
5613 {
5614 if (did_findfile_init)
5615 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005616 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (file_name != NULL)
5618 break;
5619
5620 did_findfile_init = FALSE;
5621 }
5622 else
5623 {
5624 char_u *r_ptr;
5625
5626 if (dir == NULL || *dir == NUL)
5627 {
5628 /* We searched all paths of the option, now we can
5629 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005630 vim_findfile_cleanup(fdip_search_ctx);
5631 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 break;
5633 }
5634
5635 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5636 break;
5637
5638 /* copy next path */
5639 buf[0] = 0;
5640 copy_option_part(&dir, buf, MAXPATHL, " ,");
5641
5642#ifdef FEAT_PATH_EXTRA
5643 /* get the stopdir string */
5644 r_ptr = vim_findfile_stopdir(buf);
5645#else
5646 r_ptr = NULL;
5647#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005648 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005649 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005650 fdip_search_ctx, FALSE, rel_fname);
5651 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 did_findfile_init = TRUE;
5653 vim_free(buf);
5654 }
5655 }
5656 }
5657 if (file_name == NULL && (options & FNAME_MESS))
5658 {
5659 if (first == TRUE)
5660 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005661 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005663 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 else
5665 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005666 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else
5669 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005670 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005672 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 else
5674 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005675 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 }
5678
5679theend:
5680#ifdef AMIGA
5681 proc->pr_WindowPtr = save_winptr;
5682#endif
5683 return file_name;
5684}
5685
5686#endif /* FEAT_SEARCHPATH */
5687
5688/*
5689 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5690 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5691 */
5692 int
5693vim_chdir(new_dir)
5694 char_u *new_dir;
5695{
5696#ifndef FEAT_SEARCHPATH
5697 return mch_chdir((char *)new_dir);
5698#else
5699 char_u *dir_name;
5700 int r;
5701
5702 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5703 FNAME_MESS, curbuf->b_ffname);
5704 if (dir_name == NULL)
5705 return -1;
5706 r = mch_chdir((char *)dir_name);
5707 vim_free(dir_name);
5708 return r;
5709#endif
5710}
5711
5712/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005713 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005715 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5716 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 * Returns OK or FAIL.
5718 */
5719 int
5720get_user_name(buf, len)
5721 char_u *buf;
5722 int len;
5723{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005724 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 if (mch_get_user_name(buf, len) == FAIL)
5727 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005728 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005731 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 return OK;
5733}
5734
5735#ifndef HAVE_QSORT
5736/*
5737 * Our own qsort(), for systems that don't have it.
5738 * It's simple and slow. From the K&R C book.
5739 */
5740 void
5741qsort(base, elm_count, elm_size, cmp)
5742 void *base;
5743 size_t elm_count;
5744 size_t elm_size;
5745 int (*cmp) __ARGS((const void *, const void *));
5746{
5747 char_u *buf;
5748 char_u *p1;
5749 char_u *p2;
5750 int i, j;
5751 int gap;
5752
5753 buf = alloc((unsigned)elm_size);
5754 if (buf == NULL)
5755 return;
5756
5757 for (gap = elm_count / 2; gap > 0; gap /= 2)
5758 for (i = gap; i < elm_count; ++i)
5759 for (j = i - gap; j >= 0; j -= gap)
5760 {
5761 /* Compare the elements. */
5762 p1 = (char_u *)base + j * elm_size;
5763 p2 = (char_u *)base + (j + gap) * elm_size;
5764 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5765 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005766 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 mch_memmove(buf, p1, elm_size);
5768 mch_memmove(p1, p2, elm_size);
5769 mch_memmove(p2, buf, elm_size);
5770 }
5771
5772 vim_free(buf);
5773}
5774#endif
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776/*
5777 * Sort an array of strings.
5778 */
5779static int
5780#ifdef __BORLANDC__
5781_RTLENTRYF
5782#endif
5783sort_compare __ARGS((const void *s1, const void *s2));
5784
5785 static int
5786#ifdef __BORLANDC__
5787_RTLENTRYF
5788#endif
5789sort_compare(s1, s2)
5790 const void *s1;
5791 const void *s2;
5792{
5793 return STRCMP(*(char **)s1, *(char **)s2);
5794}
5795
5796 void
5797sort_strings(files, count)
5798 char_u **files;
5799 int count;
5800{
5801 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
5804#if !defined(NO_EXPANDPATH) || defined(PROTO)
5805/*
5806 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005807 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 * Return value like strcmp(p, q), but consider path separators.
5809 */
5810 int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005811pathcmp(p, q, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 const char *p, *q;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005813 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005815 int i, j;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005816 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005817 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005819 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005821 c1 = PTR2CHAR((char_u *)p + i);
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005822 c2 = PTR2CHAR((char_u *)q + j);
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005825 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005827 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 return 0;
5829 s = q;
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005830 i = j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 break;
5832 }
5833
5834 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005835 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 {
5837 s = p;
5838 break;
5839 }
5840
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005841 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#ifdef BACKSLASH_IN_FILENAME
5843 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005844 && !((c1 == '/' && c2 == '\\')
5845 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846#endif
5847 )
5848 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005849 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005851 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005853 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5854 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005856
5857 i += MB_PTR2LEN((char_u *)p + i);
5858 j += MB_PTR2LEN((char_u *)q + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 }
Bram Moolenaarf6470c22015-08-25 16:31:40 +02005860 if (s == NULL) /* "i" or "j" ran into "maxlen" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005861 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005863 c1 = PTR2CHAR((char_u *)s + i);
5864 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005866 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005867 && i > 0
5868 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005870 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005872 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005874 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 return 0; /* match with trailing slash */
5876 if (s == q)
5877 return -1; /* no match */
5878 return 1;
5879}
5880#endif
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882/*
5883 * The putenv() implementation below comes from the "screen" program.
5884 * Included with permission from Juergen Weigert.
5885 * See pty.c for the copyright notice.
5886 */
5887
5888/*
5889 * putenv -- put value into environment
5890 *
5891 * Usage: i = putenv (string)
5892 * int i;
5893 * char *string;
5894 *
5895 * where string is of the form <name>=<value>.
5896 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5897 *
5898 * Putenv may need to add a new name into the environment, or to
5899 * associate a value longer than the current value with a particular
5900 * name. So, to make life simpler, putenv() copies your entire
5901 * environment into the heap (i.e. malloc()) from the stack
5902 * (i.e. where it resides when your process is initiated) the first
5903 * time you call it.
5904 *
5905 * (history removed, not very interesting. See the "screen" sources.)
5906 */
5907
5908#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5909
5910#define EXTRASIZE 5 /* increment to add to env. size */
5911
5912static int envsize = -1; /* current size of environment */
5913#ifndef MACOS_CLASSIC
5914extern
5915#endif
5916 char **environ; /* the global which is your env. */
5917
5918static int findenv __ARGS((char *name)); /* look for a name in the env. */
5919static int newenv __ARGS((void)); /* copy env. from stack to heap */
5920static int moreenv __ARGS((void)); /* incr. size of env. */
5921
5922 int
5923putenv(string)
5924 const char *string;
5925{
5926 int i;
5927 char *p;
5928
5929 if (envsize < 0)
5930 { /* first time putenv called */
5931 if (newenv() < 0) /* copy env. to heap */
5932 return -1;
5933 }
5934
5935 i = findenv((char *)string); /* look for name in environment */
5936
5937 if (i < 0)
5938 { /* name must be added */
5939 for (i = 0; environ[i]; i++);
5940 if (i >= (envsize - 1))
5941 { /* need new slot */
5942 if (moreenv() < 0)
5943 return -1;
5944 }
5945 p = (char *)alloc((unsigned)(strlen(string) + 1));
5946 if (p == NULL) /* not enough core */
5947 return -1;
5948 environ[i + 1] = 0; /* new end of env. */
5949 }
5950 else
5951 { /* name already in env. */
5952 p = vim_realloc(environ[i], strlen(string) + 1);
5953 if (p == NULL)
5954 return -1;
5955 }
5956 sprintf(p, "%s", string); /* copy into env. */
5957 environ[i] = p;
5958
5959 return 0;
5960}
5961
5962 static int
5963findenv(name)
5964 char *name;
5965{
5966 char *namechar, *envchar;
5967 int i, found;
5968
5969 found = 0;
5970 for (i = 0; environ[i] && !found; i++)
5971 {
5972 envchar = environ[i];
5973 namechar = name;
5974 while (*namechar && *namechar != '=' && (*namechar == *envchar))
5975 {
5976 namechar++;
5977 envchar++;
5978 }
5979 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
5980 }
5981 return found ? i - 1 : -1;
5982}
5983
5984 static int
5985newenv()
5986{
5987 char **env, *elem;
5988 int i, esize;
5989
5990#ifdef MACOS
5991 /* for Mac a new, empty environment is created */
5992 i = 0;
5993#else
5994 for (i = 0; environ[i]; i++)
5995 ;
5996#endif
5997 esize = i + EXTRASIZE + 1;
5998 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
5999 if (env == NULL)
6000 return -1;
6001
6002#ifndef MACOS
6003 for (i = 0; environ[i]; i++)
6004 {
6005 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6006 if (elem == NULL)
6007 return -1;
6008 env[i] = elem;
6009 strcpy(elem, environ[i]);
6010 }
6011#endif
6012
6013 env[i] = 0;
6014 environ = env;
6015 envsize = esize;
6016 return 0;
6017}
6018
6019 static int
6020moreenv()
6021{
6022 int esize;
6023 char **env;
6024
6025 esize = envsize + EXTRASIZE;
6026 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6027 if (env == 0)
6028 return -1;
6029 environ = env;
6030 envsize = esize;
6031 return 0;
6032}
6033
6034# ifdef USE_VIMPTY_GETENV
6035 char_u *
6036vimpty_getenv(string)
6037 const char_u *string;
6038{
6039 int i;
6040 char_u *p;
6041
6042 if (envsize < 0)
6043 return NULL;
6044
6045 i = findenv((char *)string);
6046
6047 if (i < 0)
6048 return NULL;
6049
6050 p = vim_strchr((char_u *)environ[i], '=');
6051 return (p + 1);
6052}
6053# endif
6054
6055#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006056
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006057#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006058/*
6059 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6060 * rights to write into.
6061 */
6062 int
6063filewritable(fname)
6064 char_u *fname;
6065{
6066 int retval = 0;
6067#if defined(UNIX) || defined(VMS)
6068 int perm = 0;
6069#endif
6070
6071#if defined(UNIX) || defined(VMS)
6072 perm = mch_getperm(fname);
6073#endif
6074#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6075 if (
6076# ifdef WIN3264
6077 mch_writable(fname) &&
6078# else
6079# if defined(UNIX) || defined(VMS)
6080 (perm & 0222) &&
6081# endif
6082# endif
6083 mch_access((char *)fname, W_OK) == 0
6084 )
6085#endif
6086 {
6087 ++retval;
6088 if (mch_isdir(fname))
6089 ++retval;
6090 }
6091 return retval;
6092}
6093#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006094
6095/*
6096 * Print an error message with one or two "%s" and one or two string arguments.
6097 * This is not in message.c to avoid a warning for prototypes.
6098 */
6099 int
6100emsg3(s, a1, a2)
6101 char_u *s, *a1, *a2;
6102{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006103 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006104 return TRUE; /* no error messages at the moment */
Bram Moolenaar6f192452007-11-08 19:49:02 +00006105#ifdef HAVE_STDARG_H
6106 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
6107#else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006108 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2);
Bram Moolenaar6f192452007-11-08 19:49:02 +00006109#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006110 return emsg(IObuff);
6111}
6112
6113/*
6114 * Print an error message with one "%ld" and one long int argument.
6115 * This is not in message.c to avoid a warning for prototypes.
6116 */
6117 int
6118emsgn(s, n)
6119 char_u *s;
6120 long n;
6121{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006122 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006123 return TRUE; /* no error messages at the moment */
6124 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
6125 return emsg(IObuff);
6126}
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006127
6128#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6129/*
6130 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6131 */
6132 int
6133get2c(fd)
6134 FILE *fd;
6135{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006136 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006137
6138 n = getc(fd);
6139 n = (n << 8) + getc(fd);
6140 return n;
6141}
6142
6143/*
6144 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6145 */
6146 int
6147get3c(fd)
6148 FILE *fd;
6149{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006150 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006151
6152 n = getc(fd);
6153 n = (n << 8) + getc(fd);
6154 n = (n << 8) + getc(fd);
6155 return n;
6156}
6157
6158/*
6159 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6160 */
6161 int
6162get4c(fd)
6163 FILE *fd;
6164{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006165 /* Use unsigned rather than int otherwise result is undefined
6166 * when left-shift sets the MSB. */
6167 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006168
Bram Moolenaar95235e62013-09-08 16:07:07 +02006169 n = (unsigned)getc(fd);
6170 n = (n << 8) + (unsigned)getc(fd);
6171 n = (n << 8) + (unsigned)getc(fd);
6172 n = (n << 8) + (unsigned)getc(fd);
6173 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006174}
6175
6176/*
6177 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
6178 */
6179 time_t
6180get8ctime(fd)
6181 FILE *fd;
6182{
6183 time_t n = 0;
6184 int i;
6185
6186 for (i = 0; i < 8; ++i)
6187 n = (n << 8) + getc(fd);
6188 return n;
6189}
6190
6191/*
6192 * Read a string of length "cnt" from "fd" into allocated memory.
6193 * Returns NULL when out of memory or unable to read that many bytes.
6194 */
6195 char_u *
6196read_string(fd, cnt)
6197 FILE *fd;
6198 int cnt;
6199{
6200 char_u *str;
6201 int i;
6202 int c;
6203
6204 /* allocate memory */
6205 str = alloc((unsigned)cnt + 1);
6206 if (str != NULL)
6207 {
6208 /* Read the string. Quit when running into the EOF. */
6209 for (i = 0; i < cnt; ++i)
6210 {
6211 c = getc(fd);
6212 if (c == EOF)
6213 {
6214 vim_free(str);
6215 return NULL;
6216 }
6217 str[i] = c;
6218 }
6219 str[i] = NUL;
6220 }
6221 return str;
6222}
6223
6224/*
6225 * Write a number to file "fd", MSB first, in "len" bytes.
6226 */
6227 int
6228put_bytes(fd, nr, len)
6229 FILE *fd;
6230 long_u nr;
6231 int len;
6232{
6233 int i;
6234
6235 for (i = len - 1; i >= 0; --i)
6236 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6237 return FAIL;
6238 return OK;
6239}
6240
6241#ifdef _MSC_VER
6242# if (_MSC_VER <= 1200)
6243/* This line is required for VC6 without the service pack. Also see the
6244 * matching #pragma below. */
6245 # pragma optimize("", off)
6246# endif
6247#endif
6248
6249/*
6250 * Write time_t to file "fd" in 8 bytes.
6251 */
6252 void
6253put_time(fd, the_time)
6254 FILE *fd;
6255 time_t the_time;
6256{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006257 char_u buf[8];
6258
6259 time_to_bytes(the_time, buf);
Bram Moolenaarcf487672015-03-05 13:36:00 +01006260 (void)fwrite(buf, (size_t)8, (size_t)1, fd);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006261}
6262
6263/*
6264 * Write time_t to "buf[8]".
6265 */
6266 void
6267time_to_bytes(the_time, buf)
6268 time_t the_time;
6269 char_u *buf;
6270{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006271 int c;
6272 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006273 int bi = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006274 time_t wtime = the_time;
6275
6276 /* time_t can be up to 8 bytes in size, more than long_u, thus we
6277 * can't use put_bytes() here.
6278 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006279 * sign. This happens for large values of wtime. A cast to long_u may
6280 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes,
6281 * it's safe to assume that long_u is 4 bytes or more and when using 8
6282 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006283 for (i = 7; i >= 0; --i)
6284 {
6285 if (i + 1 > (int)sizeof(time_t))
6286 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006287 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006288 else
6289 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006290#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006291 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006292#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006293 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006294#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006295 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006296 }
6297 }
6298}
6299
6300#ifdef _MSC_VER
6301# if (_MSC_VER <= 1200)
6302 # pragma optimize("", on)
6303# endif
6304#endif
6305
6306#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006307
6308#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6309 || defined(FEAT_SPELL) || defined(PROTO)
6310/*
6311 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6312 * When "s" is NULL FALSE is returned.
6313 */
6314 int
6315has_non_ascii(s)
6316 char_u *s;
6317{
6318 char_u *p;
6319
6320 if (s != NULL)
6321 for (p = s; *p != NUL; ++p)
6322 if (*p >= 128)
6323 return TRUE;
6324 return FALSE;
6325}
6326#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006327
6328#if defined(MESSAGE_QUEUE) || defined(PROTO)
6329/*
6330 * Process messages that have been queued for netbeans or clientserver.
6331 * These functions can call arbitrary vimscript and should only be called when
6332 * it is safe to do so.
6333 */
6334 void
6335parse_queued_messages()
6336{
6337# ifdef FEAT_NETBEANS_INTG
6338 /* Process the queued netbeans messages. */
6339 netbeans_parse_messages();
6340# endif
Bram Moolenaar95346802015-09-15 15:57:29 +02006341# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006342 /* Process the queued clientserver messages. */
6343 server_parse_messages();
6344# endif
6345}
6346#endif