blob: 1fe0e87cf1c37c15d39582b5ddcc7ea83b71e425 [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 Moolenaar39a58ca2005-06-27 22:42:44 +0000955#ifdef FEAT_EVAL
Bram Moolenaar661b1822005-07-28 22:36:45 +0000956 try_again |= garbage_collect(); /* cleanup recursive lists/dicts */
Bram Moolenaar39a58ca2005-06-27 22:42:44 +0000957#endif
Bram Moolenaar661b1822005-07-28 22:36:45 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 releasing = FALSE;
960 if (!try_again)
961 break;
962 }
963
964 if (message && p == NULL)
965 do_outofmem_msg(size);
966
967theend:
968#ifdef MEM_PROFILE
969 mem_post_alloc((void **)&p, (size_t)size);
970#endif
971 return p;
972}
973
974#if defined(MEM_PROFILE) || defined(PROTO)
975/*
976 * realloc() with memory profiling.
977 */
978 void *
979mem_realloc(ptr, size)
980 void *ptr;
981 size_t size;
982{
983 void *p;
984
985 mem_pre_free(&ptr);
986 mem_pre_alloc_s(&size);
987
988 p = realloc(ptr, size);
989
990 mem_post_alloc(&p, size);
991
992 return p;
993}
994#endif
995
996/*
997* Avoid repeating the error message many times (they take 1 second each).
998* Did_outofmem_msg is reset when a character is read.
999*/
1000 void
1001do_outofmem_msg(size)
1002 long_u size;
1003{
1004 if (!did_outofmem_msg)
1005 {
1006 /* Don't hide this message */
1007 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001008
1009 /* Must come first to avoid coming back here when printing the error
1010 * message fails, e.g. when setting v:errmsg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001012
1013 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015}
1016
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001017#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001018
1019# if defined(FEAT_SEARCHPATH)
1020static void free_findfile __ARGS((void));
1021# endif
1022
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001023/*
1024 * Free everything that we allocated.
1025 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001026 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1027 * surprised if Vim crashes...
1028 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001029 */
1030 void
1031free_all_mem()
1032{
1033 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001034 static int entered = FALSE;
1035
1036 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
1037 * Don't try freeing everything again. */
1038 if (entered)
1039 return;
1040 entered = TRUE;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001041
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001042# ifdef FEAT_AUTOCMD
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001043 /* Don't want to trigger autocommands from here on. */
1044 block_autocmds();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001045# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001046
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001047# ifdef FEAT_WINDOWS
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001048 /* Close all tabs and windows. Reset 'equalalways' to avoid redraws. */
1049 p_ea = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001050 if (first_tabpage->tp_next != NULL)
1051 do_cmdline_cmd((char_u *)"tabonly!");
1052 if (firstwin != lastwin)
1053 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001054# endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001055
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001056# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001057 /* Free all spell info. */
1058 spell_free_all();
1059# endif
1060
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001061# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001062 /* Clear user commands (before deleting buffers). */
1063 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001064# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001065
1066# ifdef FEAT_MENU
1067 /* Clear menus. */
1068 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001069# ifdef FEAT_MULTI_LANG
1070 do_cmdline_cmd((char_u *)"menutranslate clear");
1071# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001072# endif
1073
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001074 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar21160b92009-11-11 15:56:10 +00001075 do_cmdline_cmd((char_u *)"lmapclear");
1076 do_cmdline_cmd((char_u *)"xmapclear");
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001077 do_cmdline_cmd((char_u *)"mapclear");
1078 do_cmdline_cmd((char_u *)"mapclear!");
1079 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001080# if defined(FEAT_EVAL)
1081 do_cmdline_cmd((char_u *)"breakdel *");
1082# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001083# if defined(FEAT_PROFILE)
1084 do_cmdline_cmd((char_u *)"profdel *");
1085# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001086# if defined(FEAT_KEYMAP)
1087 do_cmdline_cmd((char_u *)"set keymap=");
1088#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001089
1090# ifdef FEAT_TITLE
1091 free_titles();
1092# endif
1093# if defined(FEAT_SEARCHPATH)
1094 free_findfile();
1095# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001096
1097 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001098# if defined(FEAT_AUTOCMD)
1099 free_all_autocmds();
1100# endif
1101 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001102 free_all_options();
1103 free_all_marks();
1104 alist_clear(&global_alist);
1105 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001106# if defined(FEAT_CMDL_COMPL)
1107 free_users();
1108# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001109 free_search_patterns();
1110 free_old_sub();
1111 free_last_insert();
1112 free_prev_shellcmd();
1113 free_regexp_stuff();
1114 free_tag_stuff();
1115 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001116# ifdef FEAT_SIGNS
1117 free_signs();
1118# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001119# ifdef FEAT_EVAL
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001120 set_expr_line(NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001121# endif
1122# ifdef FEAT_DIFF
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001123 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001124# endif
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001125 clear_sb_text(); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001126
1127 /* Free some global vars. */
1128 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001129# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001130 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001131# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001132 vim_free(last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001133# ifdef FEAT_CMDHIST
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001134 vim_free(new_last_cmdline);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001135# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001136 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001137 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001138
1139 /* Clear cmdline history. */
1140 p_hi = 0;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001141# ifdef FEAT_CMDHIST
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001142 init_history();
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001143# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001144
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001145#ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001146 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001147 win_T *win;
1148 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001149
1150 qf_free_all(NULL);
1151 /* Free all location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001152 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001153 qf_free_all(win);
1154 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001155#endif
1156
1157 /* Close all script inputs. */
1158 close_all_scripts();
1159
1160#if defined(FEAT_WINDOWS)
1161 /* Destroy all windows. Must come before freeing buffers. */
1162 win_free_all();
1163#endif
1164
Bram Moolenaar2a329742008-04-01 12:53:43 +00001165 /* Free all buffers. Reset 'autochdir' to avoid accessing things that
1166 * were freed already. */
1167#ifdef FEAT_AUTOCHDIR
1168 p_acd = FALSE;
1169#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001170 for (buf = firstbuf; buf != NULL; )
1171 {
1172 nextbuf = buf->b_next;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001173 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001174 if (buf_valid(buf))
1175 buf = nextbuf; /* didn't work, try next one */
1176 else
1177 buf = firstbuf;
1178 }
1179
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001180#ifdef FEAT_ARABIC
1181 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001182#endif
1183
1184 /* Clear registers. */
1185 clear_registers();
1186 ResetRedobuff();
1187 ResetRedobuff();
1188
Bram Moolenaar85c79d32007-02-20 01:59:20 +00001189#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001190 vim_free(serverDelayedStartName);
1191#endif
1192
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001193 /* highlight info */
1194 free_highlight();
1195
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001196 reset_last_sourcing();
1197
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001198#ifdef FEAT_WINDOWS
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001199 free_tabpage(first_tabpage);
1200 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001201#endif
1202
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001203# ifdef UNIX
1204 /* Machine-specific free. */
1205 mch_free_mem();
1206# endif
1207
1208 /* message history */
1209 for (;;)
1210 if (delete_first_msg() == FAIL)
1211 break;
1212
1213# ifdef FEAT_EVAL
1214 eval_clear();
1215# endif
1216
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001217 free_termoptions();
1218
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001219 /* screenlines (can't display anything now!) */
1220 free_screenlines();
1221
1222#if defined(USE_XSMP)
1223 xsmp_close();
1224#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001225#ifdef FEAT_GUI_GTK
1226 gui_mch_free_all();
1227#endif
1228 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001229
1230 vim_free(IObuff);
1231 vim_free(NameBuff);
1232}
1233#endif
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001236 * Copy "string" into newly allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
1238 char_u *
1239vim_strsave(string)
1240 char_u *string;
1241{
1242 char_u *p;
1243 unsigned len;
1244
1245 len = (unsigned)STRLEN(string) + 1;
1246 p = alloc(len);
1247 if (p != NULL)
1248 mch_memmove(p, string, (size_t)len);
1249 return p;
1250}
1251
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001252/*
1253 * Copy up to "len" bytes of "string" into newly allocated memory and
1254 * terminate with a NUL.
1255 * The allocated memory always has size "len + 1", also when "string" is
1256 * shorter.
1257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 char_u *
1259vim_strnsave(string, len)
1260 char_u *string;
1261 int len;
1262{
1263 char_u *p;
1264
1265 p = alloc((unsigned)(len + 1));
1266 if (p != NULL)
1267 {
1268 STRNCPY(p, string, len);
1269 p[len] = NUL;
1270 }
1271 return p;
1272}
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274/*
1275 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1276 * by a backslash.
1277 */
1278 char_u *
1279vim_strsave_escaped(string, esc_chars)
1280 char_u *string;
1281 char_u *esc_chars;
1282{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001283 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284}
1285
1286/*
1287 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1288 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001289 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 */
1291 char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001292vim_strsave_escaped_ext(string, esc_chars, cc, bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 char_u *string;
1294 char_u *esc_chars;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001295 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 int bsl;
1297{
1298 char_u *p;
1299 char_u *p2;
1300 char_u *escaped_string;
1301 unsigned length;
1302#ifdef FEAT_MBYTE
1303 int l;
1304#endif
1305
1306 /*
1307 * First count the number of backslashes required.
1308 * Then allocate the memory and insert them.
1309 */
1310 length = 1; /* count the trailing NUL */
1311 for (p = string; *p; p++)
1312 {
1313#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001314 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {
1316 length += l; /* count a multibyte char */
1317 p += l - 1;
1318 continue;
1319 }
1320#endif
1321 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1322 ++length; /* count a backslash */
1323 ++length; /* count an ordinary char */
1324 }
1325 escaped_string = alloc(length);
1326 if (escaped_string != NULL)
1327 {
1328 p2 = escaped_string;
1329 for (p = string; *p; p++)
1330 {
1331#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001332 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 mch_memmove(p2, p, (size_t)l);
1335 p2 += l;
1336 p += l - 1; /* skip multibyte char */
1337 continue;
1338 }
1339#endif
1340 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001341 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 *p2++ = *p;
1343 }
1344 *p2 = NUL;
1345 }
1346 return escaped_string;
1347}
1348
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001349/*
1350 * Return TRUE when 'shell' has "csh" in the tail.
1351 */
1352 int
1353csh_like_shell()
1354{
1355 return (strstr((char *)gettail(p_sh), "csh") != NULL);
1356}
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001357
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001358/*
1359 * Escape "string" for use as a shell argument with system().
Bram Moolenaar21160b92009-11-11 15:56:10 +00001360 * This uses single quotes, except when we know we need to use double quotes
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001361 * (MS-DOS and MS-Windows without 'shellslash' set).
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001362 * Escape a newline, depending on the 'shell' option.
1363 * When "do_special" is TRUE also replace "!", "%", "#" and things starting
1364 * with "<" like "<cfile>".
Bram Moolenaar26df0922014-02-23 23:39:13 +01001365 * When "do_newline" is FALSE do not escape newline unless it is csh shell.
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001366 * Returns the result in allocated memory, NULL if we have run out.
1367 */
1368 char_u *
Bram Moolenaar26df0922014-02-23 23:39:13 +01001369vim_strsave_shellescape(string, do_special, do_newline)
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001370 char_u *string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001371 int do_special;
Bram Moolenaar26df0922014-02-23 23:39:13 +01001372 int do_newline;
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001373{
1374 unsigned length;
1375 char_u *p;
1376 char_u *d;
1377 char_u *escaped_string;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001378 int l;
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001379 int csh_like;
1380
1381 /* Only csh and similar shells expand '!' within single quotes. For sh and
1382 * the like we must not put a backslash before it, it will be taken
1383 * literally. If do_special is set the '!' will be escaped twice.
1384 * Csh also needs to have "\n" escaped twice when do_special is set. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001385 csh_like = csh_like_shell();
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001386
1387 /* First count the number of extra bytes required. */
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001388 length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001389 for (p = string; *p != NUL; mb_ptr_adv(p))
1390 {
1391# if defined(WIN32) || defined(WIN16) || defined(DOS)
1392 if (!p_ssl)
1393 {
1394 if (*p == '"')
1395 ++length; /* " -> "" */
1396 }
1397 else
1398# endif
1399 if (*p == '\'')
1400 length += 3; /* ' => '\'' */
Bram Moolenaar26df0922014-02-23 23:39:13 +01001401 if ((*p == '\n' && (csh_like || do_newline))
1402 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001403 {
1404 ++length; /* insert backslash */
1405 if (csh_like && do_special)
1406 ++length; /* insert backslash */
1407 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001408 if (do_special && find_cmdline_var(p, &l) >= 0)
1409 {
1410 ++length; /* insert backslash */
1411 p += l - 1;
1412 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001413 }
1414
1415 /* Allocate memory for the result and fill it. */
1416 escaped_string = alloc(length);
1417 if (escaped_string != NULL)
1418 {
1419 d = escaped_string;
1420
1421 /* add opening quote */
1422# if defined(WIN32) || defined(WIN16) || defined(DOS)
1423 if (!p_ssl)
1424 *d++ = '"';
1425 else
1426# endif
1427 *d++ = '\'';
1428
1429 for (p = string; *p != NUL; )
1430 {
1431# if defined(WIN32) || defined(WIN16) || defined(DOS)
1432 if (!p_ssl)
1433 {
1434 if (*p == '"')
1435 {
1436 *d++ = '"';
1437 *d++ = '"';
1438 ++p;
1439 continue;
1440 }
1441 }
1442 else
1443# endif
1444 if (*p == '\'')
1445 {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001446 *d++ = '\'';
1447 *d++ = '\\';
1448 *d++ = '\'';
1449 *d++ = '\'';
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001450 ++p;
1451 continue;
1452 }
Bram Moolenaar26df0922014-02-23 23:39:13 +01001453 if ((*p == '\n' && (csh_like || do_newline))
1454 || (*p == '!' && (csh_like || do_special)))
Bram Moolenaarffd66c42008-07-16 20:43:37 +00001455 {
1456 *d++ = '\\';
1457 if (csh_like && do_special)
1458 *d++ = '\\';
1459 *d++ = *p++;
1460 continue;
1461 }
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001462 if (do_special && find_cmdline_var(p, &l) >= 0)
1463 {
1464 *d++ = '\\'; /* insert backslash */
1465 while (--l >= 0) /* copy the var */
1466 *d++ = *p++;
Bram Moolenaar099d01d2009-11-25 16:14:45 +00001467 continue;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00001468 }
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001469
1470 MB_COPY_CHAR(p, d);
1471 }
1472
1473 /* add terminating quote and finish with a NUL */
1474# if defined(WIN32) || defined(WIN16) || defined(DOS)
1475 if (!p_ssl)
1476 *d++ = '"';
1477 else
1478# endif
1479 *d++ = '\'';
1480 *d = NUL;
1481 }
1482
1483 return escaped_string;
1484}
Bram Moolenaar60a495f2006-10-03 12:44:42 +00001485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486/*
1487 * Like vim_strsave(), but make all characters uppercase.
1488 * This uses ASCII lower-to-upper case translation, language independent.
1489 */
1490 char_u *
1491vim_strsave_up(string)
1492 char_u *string;
1493{
1494 char_u *p1;
1495
1496 p1 = vim_strsave(string);
1497 vim_strup(p1);
1498 return p1;
1499}
1500
1501/*
1502 * Like vim_strnsave(), but make all characters uppercase.
1503 * This uses ASCII lower-to-upper case translation, language independent.
1504 */
1505 char_u *
1506vim_strnsave_up(string, len)
1507 char_u *string;
1508 int len;
1509{
1510 char_u *p1;
1511
1512 p1 = vim_strnsave(string, len);
1513 vim_strup(p1);
1514 return p1;
1515}
1516
1517/*
1518 * ASCII lower-to-upper case translation, language independent.
1519 */
1520 void
1521vim_strup(p)
1522 char_u *p;
1523{
1524 char_u *p2;
1525 int c;
1526
1527 if (p != NULL)
1528 {
1529 p2 = p;
1530 while ((c = *p2) != NUL)
1531#ifdef EBCDIC
1532 *p2++ = isalpha(c) ? toupper(c) : c;
1533#else
1534 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1535#endif
1536 }
1537}
1538
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001539#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001540/*
1541 * Make string "s" all upper-case and return it in allocated memory.
1542 * Handles multi-byte characters as well as possible.
1543 * Returns NULL when out of memory.
1544 */
1545 char_u *
1546strup_save(orig)
1547 char_u *orig;
1548{
1549 char_u *p;
1550 char_u *res;
1551
1552 res = p = vim_strsave(orig);
1553
1554 if (res != NULL)
1555 while (*p != NUL)
1556 {
1557# ifdef FEAT_MBYTE
1558 int l;
1559
1560 if (enc_utf8)
1561 {
1562 int c, uc;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001563 int newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001564 char_u *s;
1565
1566 c = utf_ptr2char(p);
1567 uc = utf_toupper(c);
1568
1569 /* Reallocate string when byte count changes. This is rare,
1570 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001571 l = utf_ptr2len(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001572 newl = utf_char2len(uc);
1573 if (newl != l)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001574 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001575 s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001576 if (s == NULL)
1577 break;
1578 mch_memmove(s, res, p - res);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001579 STRCPY(s + (p - res) + newl, p + l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001580 p = s + (p - res);
1581 vim_free(res);
1582 res = s;
1583 }
1584
1585 utf_char2bytes(uc, p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001586 p += newl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001587 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001588 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001589 p += l; /* skip multi-byte character */
1590 else
1591# endif
1592 {
1593 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1594 p++;
1595 }
1596 }
1597
1598 return res;
1599}
1600#endif
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602/*
1603 * copy a space a number of times
1604 */
1605 void
1606copy_spaces(ptr, count)
1607 char_u *ptr;
1608 size_t count;
1609{
1610 size_t i = count;
1611 char_u *p = ptr;
1612
1613 while (i--)
1614 *p++ = ' ';
1615}
1616
1617#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1618/*
1619 * Copy a character a number of times.
Bram Moolenaar21160b92009-11-11 15:56:10 +00001620 * Does not work for multi-byte characters!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
1622 void
1623copy_chars(ptr, count, c)
1624 char_u *ptr;
1625 size_t count;
1626 int c;
1627{
1628 size_t i = count;
1629 char_u *p = ptr;
1630
1631 while (i--)
1632 *p++ = c;
1633}
1634#endif
1635
1636/*
1637 * delete spaces at the end of a string
1638 */
1639 void
1640del_trailing_spaces(ptr)
1641 char_u *ptr;
1642{
1643 char_u *q;
1644
1645 q = ptr + STRLEN(ptr);
1646 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
1647 *q = NUL;
1648}
1649
1650/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001651 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001652 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 */
1654 void
1655vim_strncpy(to, from, len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001656 char_u *to;
1657 char_u *from;
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001658 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001660 STRNCPY(to, from, len);
1661 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662}
1663
1664/*
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02001665 * Like strcat(), but make sure the result fits in "tosize" bytes and is
1666 * always NUL terminated.
1667 */
1668 void
1669vim_strcat(to, from, tosize)
1670 char_u *to;
1671 char_u *from;
1672 size_t tosize;
1673{
1674 size_t tolen = STRLEN(to);
1675 size_t fromlen = STRLEN(from);
1676
1677 if (tolen + fromlen + 1 > tosize)
1678 {
1679 mch_memmove(to + tolen, from, tosize - tolen - 1);
1680 to[tosize - 1] = NUL;
1681 }
1682 else
1683 STRCPY(to + tolen, from);
1684}
1685
1686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 * Isolate one part of a string option where parts are separated with
1688 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001689 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 * "*option" is advanced to the next part.
1691 * The length is returned.
1692 */
1693 int
1694copy_option_part(option, buf, maxlen, sep_chars)
1695 char_u **option;
1696 char_u *buf;
1697 int maxlen;
1698 char *sep_chars;
1699{
1700 int len = 0;
1701 char_u *p = *option;
1702
1703 /* skip '.' at start of option part, for 'suffixes' */
1704 if (*p == '.')
1705 buf[len++] = *p++;
1706 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1707 {
1708 /*
1709 * Skip backslash before a separator character and space.
1710 */
1711 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1712 ++p;
1713 if (len < maxlen - 1)
1714 buf[len++] = *p;
1715 ++p;
1716 }
1717 buf[len] = NUL;
1718
1719 if (*p != NUL && *p != ',') /* skip non-standard separator */
1720 ++p;
1721 p = skip_to_option_part(p); /* p points to next file name */
1722
1723 *option = p;
1724 return len;
1725}
1726
1727/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001728 * Replacement for free() that ignores NULL pointers.
1729 * Also skip free() when exiting for sure, this helps when we caught a deadly
1730 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 */
1732 void
1733vim_free(x)
1734 void *x;
1735{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001736 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
1738#ifdef MEM_PROFILE
1739 mem_pre_free(&x);
1740#endif
1741 free(x);
1742 }
1743}
1744
1745#ifndef HAVE_MEMSET
1746 void *
1747vim_memset(ptr, c, size)
1748 void *ptr;
1749 int c;
1750 size_t size;
1751{
1752 char *p = ptr;
1753
1754 while (size-- > 0)
1755 *p++ = c;
1756 return ptr;
1757}
1758#endif
1759
1760#ifdef VIM_MEMCMP
1761/*
1762 * Return zero when "b1" and "b2" are the same for "len" bytes.
1763 * Return non-zero otherwise.
1764 */
1765 int
1766vim_memcmp(b1, b2, len)
1767 void *b1;
1768 void *b2;
1769 size_t len;
1770{
1771 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2;
1772
1773 for ( ; len > 0; --len)
1774 {
1775 if (*p1 != *p2)
1776 return 1;
1777 ++p1;
1778 ++p2;
1779 }
1780 return 0;
1781}
1782#endif
1783
1784#ifdef VIM_MEMMOVE
1785/*
1786 * Version of memmove() that handles overlapping source and destination.
1787 * For systems that don't have a function that is guaranteed to do that (SYSV).
1788 */
1789 void
1790mch_memmove(dst_arg, src_arg, len)
1791 void *src_arg, *dst_arg;
1792 size_t len;
1793{
1794 /*
1795 * A void doesn't have a size, we use char pointers.
1796 */
1797 char *dst = dst_arg, *src = src_arg;
1798
1799 /* overlap, copy backwards */
1800 if (dst > src && dst < src + len)
1801 {
1802 src += len;
1803 dst += len;
1804 while (len-- > 0)
1805 *--dst = *--src;
1806 }
1807 else /* copy forwards */
1808 while (len-- > 0)
1809 *dst++ = *src++;
1810}
1811#endif
1812
1813#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1814/*
1815 * Compare two strings, ignoring case, using current locale.
1816 * Doesn't work for multi-byte characters.
1817 * return 0 for match, < 0 for smaller, > 0 for bigger
1818 */
1819 int
1820vim_stricmp(s1, s2)
1821 char *s1;
1822 char *s2;
1823{
1824 int i;
1825
1826 for (;;)
1827 {
1828 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1829 if (i != 0)
1830 return i; /* this character different */
1831 if (*s1 == NUL)
1832 break; /* strings match until NUL */
1833 ++s1;
1834 ++s2;
1835 }
1836 return 0; /* strings match */
1837}
1838#endif
1839
1840#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1841/*
1842 * Compare two strings, for length "len", ignoring case, using current locale.
1843 * Doesn't work for multi-byte characters.
1844 * return 0 for match, < 0 for smaller, > 0 for bigger
1845 */
1846 int
1847vim_strnicmp(s1, s2, len)
1848 char *s1;
1849 char *s2;
1850 size_t len;
1851{
1852 int i;
1853
1854 while (len > 0)
1855 {
1856 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1857 if (i != 0)
1858 return i; /* this character different */
1859 if (*s1 == NUL)
1860 break; /* strings match until NUL */
1861 ++s1;
1862 ++s2;
1863 --len;
1864 }
1865 return 0; /* strings match */
1866}
1867#endif
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869/*
1870 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001871 * with characters from 128 to 255 correctly. It also doesn't return a
1872 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 */
1874 char_u *
1875vim_strchr(string, c)
1876 char_u *string;
1877 int c;
1878{
1879 char_u *p;
1880 int b;
1881
1882 p = string;
1883#ifdef FEAT_MBYTE
1884 if (enc_utf8 && c >= 0x80)
1885 {
1886 while (*p != NUL)
1887 {
1888 if (utf_ptr2char(p) == c)
1889 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001890 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
1892 return NULL;
1893 }
1894 if (enc_dbcs != 0 && c > 255)
1895 {
1896 int n2 = c & 0xff;
1897
1898 c = ((unsigned)c >> 8) & 0xff;
1899 while ((b = *p) != NUL)
1900 {
1901 if (b == c && p[1] == n2)
1902 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001903 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 return NULL;
1906 }
1907 if (has_mbyte)
1908 {
1909 while ((b = *p) != NUL)
1910 {
1911 if (b == c)
1912 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001913 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915 return NULL;
1916 }
1917#endif
1918 while ((b = *p) != NUL)
1919 {
1920 if (b == c)
1921 return p;
1922 ++p;
1923 }
1924 return NULL;
1925}
1926
1927/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001928 * Version of strchr() that only works for bytes and handles unsigned char
1929 * strings with characters above 128 correctly. It also doesn't return a
1930 * pointer to the NUL at the end of the string.
1931 */
1932 char_u *
1933vim_strbyte(string, c)
1934 char_u *string;
1935 int c;
1936{
1937 char_u *p = string;
1938
1939 while (*p != NUL)
1940 {
1941 if (*p == c)
1942 return p;
1943 ++p;
1944 }
1945 return NULL;
1946}
1947
1948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001950 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001951 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 */
1953 char_u *
1954vim_strrchr(string, c)
1955 char_u *string;
1956 int c;
1957{
1958 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
Bram Moolenaar05159a02005-02-26 23:04:13 +00001961 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001963 if (*p == c)
1964 retval = p;
1965 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967 return retval;
1968}
1969
1970/*
1971 * Vim's version of strpbrk(), in case it's missing.
1972 * Don't generate a prototype for this, causes problems when it's not used.
1973 */
1974#ifndef PROTO
1975# ifndef HAVE_STRPBRK
1976# ifdef vim_strpbrk
1977# undef vim_strpbrk
1978# endif
1979 char_u *
1980vim_strpbrk(s, charset)
1981 char_u *s;
1982 char_u *charset;
1983{
1984 while (*s)
1985 {
1986 if (vim_strchr(charset, *s) != NULL)
1987 return s;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001988 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 return NULL;
1991}
1992# endif
1993#endif
1994
1995/*
1996 * Vim has its own isspace() function, because on some machines isspace()
1997 * can't handle characters above 128.
1998 */
1999 int
2000vim_isspace(x)
2001 int x;
2002{
2003 return ((x >= 9 && x <= 13) || x == ' ');
2004}
2005
2006/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002007 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 */
2009
2010/*
2011 * Clear an allocated growing array.
2012 */
2013 void
2014ga_clear(gap)
2015 garray_T *gap;
2016{
2017 vim_free(gap->ga_data);
2018 ga_init(gap);
2019}
2020
2021/*
2022 * Clear a growing array that contains a list of strings.
2023 */
2024 void
2025ga_clear_strings(gap)
2026 garray_T *gap;
2027{
2028 int i;
2029
2030 for (i = 0; i < gap->ga_len; ++i)
2031 vim_free(((char_u **)(gap->ga_data))[i]);
2032 ga_clear(gap);
2033}
2034
2035/*
2036 * Initialize a growing array. Don't forget to set ga_itemsize and
2037 * ga_growsize! Or use ga_init2().
2038 */
2039 void
2040ga_init(gap)
2041 garray_T *gap;
2042{
2043 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00002044 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 gap->ga_len = 0;
2046}
2047
2048 void
2049ga_init2(gap, itemsize, growsize)
2050 garray_T *gap;
2051 int itemsize;
2052 int growsize;
2053{
2054 ga_init(gap);
2055 gap->ga_itemsize = itemsize;
2056 gap->ga_growsize = growsize;
2057}
2058
2059/*
2060 * Make room in growing array "gap" for at least "n" items.
2061 * Return FAIL for failure, OK otherwise.
2062 */
2063 int
2064ga_grow(gap, n)
2065 garray_T *gap;
2066 int n;
2067{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002068 size_t old_len;
2069 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 char_u *pp;
2071
Bram Moolenaar86b68352004-12-27 21:59:20 +00002072 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
2074 if (n < gap->ga_growsize)
2075 n = gap->ga_growsize;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002076 new_len = gap->ga_itemsize * (gap->ga_len + n);
2077 pp = (gap->ga_data == NULL)
Bram Moolenaar4336cdf2012-02-29 13:58:47 +01002078 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (pp == NULL)
2080 return FAIL;
Bram Moolenaar7282bc32012-02-22 18:12:32 +01002081 old_len = gap->ga_itemsize * gap->ga_maxlen;
2082 vim_memset(pp + old_len, 0, new_len - old_len);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002083 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 gap->ga_data = pp;
2085 }
2086 return OK;
2087}
2088
2089/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002090 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002091 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002092 * Returns NULL when out of memory.
2093 */
2094 char_u *
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002095ga_concat_strings(gap, sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002096 garray_T *gap;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002097 char *sep;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002098{
2099 int i;
2100 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002101 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002102 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002103 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002104
2105 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002106 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002107
2108 s = alloc(len + 1);
2109 if (s != NULL)
2110 {
2111 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002112 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002113 for (i = 0; i < gap->ga_len; ++i)
2114 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02002115 if (p != s)
2116 {
2117 STRCPY(p, sep);
2118 p += sep_len;
2119 }
2120 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
2121 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02002122 }
2123 }
2124 return s;
2125}
2126
2127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 * Concatenate a string to a growarray which contains characters.
2129 * Note: Does NOT copy the NUL at the end!
2130 */
2131 void
2132ga_concat(gap, s)
2133 garray_T *gap;
2134 char_u *s;
2135{
2136 int len = (int)STRLEN(s);
2137
2138 if (ga_grow(gap, len) == OK)
2139 {
2140 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
2141 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143}
2144
2145/*
2146 * Append one byte to a growarray which contains bytes.
2147 */
2148 void
2149ga_append(gap, c)
2150 garray_T *gap;
2151 int c;
2152{
2153 if (ga_grow(gap, 1) == OK)
2154 {
2155 *((char *)gap->ga_data + gap->ga_len) = c;
2156 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158}
2159
Bram Moolenaar597a4222014-06-25 14:39:50 +02002160#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
2161 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02002162/*
2163 * Append the text in "gap" below the cursor line and clear "gap".
2164 */
2165 void
2166append_ga_line(gap)
2167 garray_T *gap;
2168{
2169 /* Remove trailing CR. */
2170 if (gap->ga_len > 0
2171 && !curbuf->b_p_bin
2172 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2173 --gap->ga_len;
2174 ga_append(gap, NUL);
2175 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2176 gap->ga_len = 0;
2177}
2178#endif
2179
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180/************************************************************************
2181 * functions that use lookup tables for various things, generally to do with
2182 * special key codes.
2183 */
2184
2185/*
2186 * Some useful tables.
2187 */
2188
2189static struct modmasktable
2190{
2191 short mod_mask; /* Bit-mask for particular key modifier */
2192 short mod_flag; /* Bit(s) for particular key modifier */
2193 char_u name; /* Single letter name of modifier */
2194} mod_mask_table[] =
2195{
2196 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002197 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
2199 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
2200 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
2201 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
2202 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
2203#ifdef MACOS
2204 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
2205#endif
2206 /* 'A' must be the last one */
2207 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
2208 {0, 0, NUL}
2209};
2210
2211/*
2212 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00002213 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 */
2215#define MOD_KEYS_ENTRY_SIZE 5
2216
2217static char_u modifier_keys_table[] =
2218{
2219/* mod mask with modifier without modifier */
2220 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
2221 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
2222 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
2223 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
2224 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
2225 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
2226 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
2227 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
2228 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
2229 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
2230 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
2231 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
2232 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
2233 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
2234 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
2235 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
2236 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
2237 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
2238 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
2239 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
2240 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
2241 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
2242 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
2243 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
2244 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
2245 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
2246 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
2247 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
2248 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
2249 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
2250 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
2251 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
2252 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
2253
2254 /* vt100 F1 */
2255 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
2256 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
2257 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
2258 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
2259
2260 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
2261 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
2262 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
2263 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
2264 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
2265 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
2266 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
2267 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
2268 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
2269 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
2270
2271 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
2272 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
2273 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
2274 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
2275 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
2276 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
2277 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
2278 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
2279 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
2280 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
2281
2282 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
2283 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
2284 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
2285 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
2286 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
2287 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
2288 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
2289 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
2290 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
2291 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
2292
2293 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
2294 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
2295 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
2296 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
2297 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
2298 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
2299 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
2300
2301 /* TAB pseudo code*/
2302 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
2303
2304 NUL
2305};
2306
2307static struct key_name_entry
2308{
2309 int key; /* Special key code or ascii value */
2310 char_u *name; /* Name of key */
2311} key_names_table[] =
2312{
2313 {' ', (char_u *)"Space"},
2314 {TAB, (char_u *)"Tab"},
2315 {K_TAB, (char_u *)"Tab"},
2316 {NL, (char_u *)"NL"},
2317 {NL, (char_u *)"NewLine"}, /* Alternative name */
2318 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2319 {NL, (char_u *)"LF"}, /* Alternative name */
2320 {CAR, (char_u *)"CR"},
2321 {CAR, (char_u *)"Return"}, /* Alternative name */
2322 {CAR, (char_u *)"Enter"}, /* Alternative name */
2323 {K_BS, (char_u *)"BS"},
2324 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2325 {ESC, (char_u *)"Esc"},
2326 {CSI, (char_u *)"CSI"},
2327 {K_CSI, (char_u *)"xCSI"},
2328 {'|', (char_u *)"Bar"},
2329 {'\\', (char_u *)"Bslash"},
2330 {K_DEL, (char_u *)"Del"},
2331 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2332 {K_KDEL, (char_u *)"kDel"},
2333 {K_UP, (char_u *)"Up"},
2334 {K_DOWN, (char_u *)"Down"},
2335 {K_LEFT, (char_u *)"Left"},
2336 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002337 {K_XUP, (char_u *)"xUp"},
2338 {K_XDOWN, (char_u *)"xDown"},
2339 {K_XLEFT, (char_u *)"xLeft"},
2340 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342 {K_F1, (char_u *)"F1"},
2343 {K_F2, (char_u *)"F2"},
2344 {K_F3, (char_u *)"F3"},
2345 {K_F4, (char_u *)"F4"},
2346 {K_F5, (char_u *)"F5"},
2347 {K_F6, (char_u *)"F6"},
2348 {K_F7, (char_u *)"F7"},
2349 {K_F8, (char_u *)"F8"},
2350 {K_F9, (char_u *)"F9"},
2351 {K_F10, (char_u *)"F10"},
2352
2353 {K_F11, (char_u *)"F11"},
2354 {K_F12, (char_u *)"F12"},
2355 {K_F13, (char_u *)"F13"},
2356 {K_F14, (char_u *)"F14"},
2357 {K_F15, (char_u *)"F15"},
2358 {K_F16, (char_u *)"F16"},
2359 {K_F17, (char_u *)"F17"},
2360 {K_F18, (char_u *)"F18"},
2361 {K_F19, (char_u *)"F19"},
2362 {K_F20, (char_u *)"F20"},
2363
2364 {K_F21, (char_u *)"F21"},
2365 {K_F22, (char_u *)"F22"},
2366 {K_F23, (char_u *)"F23"},
2367 {K_F24, (char_u *)"F24"},
2368 {K_F25, (char_u *)"F25"},
2369 {K_F26, (char_u *)"F26"},
2370 {K_F27, (char_u *)"F27"},
2371 {K_F28, (char_u *)"F28"},
2372 {K_F29, (char_u *)"F29"},
2373 {K_F30, (char_u *)"F30"},
2374
2375 {K_F31, (char_u *)"F31"},
2376 {K_F32, (char_u *)"F32"},
2377 {K_F33, (char_u *)"F33"},
2378 {K_F34, (char_u *)"F34"},
2379 {K_F35, (char_u *)"F35"},
2380 {K_F36, (char_u *)"F36"},
2381 {K_F37, (char_u *)"F37"},
2382
2383 {K_XF1, (char_u *)"xF1"},
2384 {K_XF2, (char_u *)"xF2"},
2385 {K_XF3, (char_u *)"xF3"},
2386 {K_XF4, (char_u *)"xF4"},
2387
2388 {K_HELP, (char_u *)"Help"},
2389 {K_UNDO, (char_u *)"Undo"},
2390 {K_INS, (char_u *)"Insert"},
2391 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2392 {K_KINS, (char_u *)"kInsert"},
2393 {K_HOME, (char_u *)"Home"},
2394 {K_KHOME, (char_u *)"kHome"},
2395 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002396 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {K_END, (char_u *)"End"},
2398 {K_KEND, (char_u *)"kEnd"},
2399 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002400 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {K_PAGEUP, (char_u *)"PageUp"},
2402 {K_PAGEDOWN, (char_u *)"PageDown"},
2403 {K_KPAGEUP, (char_u *)"kPageUp"},
2404 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2405
2406 {K_KPLUS, (char_u *)"kPlus"},
2407 {K_KMINUS, (char_u *)"kMinus"},
2408 {K_KDIVIDE, (char_u *)"kDivide"},
2409 {K_KMULTIPLY, (char_u *)"kMultiply"},
2410 {K_KENTER, (char_u *)"kEnter"},
2411 {K_KPOINT, (char_u *)"kPoint"},
2412
2413 {K_K0, (char_u *)"k0"},
2414 {K_K1, (char_u *)"k1"},
2415 {K_K2, (char_u *)"k2"},
2416 {K_K3, (char_u *)"k3"},
2417 {K_K4, (char_u *)"k4"},
2418 {K_K5, (char_u *)"k5"},
2419 {K_K6, (char_u *)"k6"},
2420 {K_K7, (char_u *)"k7"},
2421 {K_K8, (char_u *)"k8"},
2422 {K_K9, (char_u *)"k9"},
2423
2424 {'<', (char_u *)"lt"},
2425
2426 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002427#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002429#endif
2430#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002432#endif
2433#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002435#endif
2436#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01002438#endif
2439#ifdef FEAT_MOUSE_URXVT
2440 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
2441#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002442#ifdef FEAT_MOUSE_SGR
2443 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
2444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2446 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2447 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2448 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2449 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2450 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2451 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2452 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2453 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2454 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2455 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002456 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
2457 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
2458 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
2459 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
2460 {K_MOUSEDOWN, (char_u *)"MouseDown"}, /* OBSOLETE: Use */
2461 {K_MOUSEUP, (char_u *)"MouseUp"}, /* ScrollWheelXXX instead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {K_X1MOUSE, (char_u *)"X1Mouse"},
2463 {K_X1DRAG, (char_u *)"X1Drag"},
2464 {K_X1RELEASE, (char_u *)"X1Release"},
2465 {K_X2MOUSE, (char_u *)"X2Mouse"},
2466 {K_X2DRAG, (char_u *)"X2Drag"},
2467 {K_X2RELEASE, (char_u *)"X2Release"},
2468 {K_DROP, (char_u *)"Drop"},
2469 {K_ZERO, (char_u *)"Nul"},
2470#ifdef FEAT_EVAL
2471 {K_SNR, (char_u *)"SNR"},
2472#endif
2473 {K_PLUG, (char_u *)"Plug"},
2474 {0, NULL}
2475};
2476
2477#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2478
2479#ifdef FEAT_MOUSE
2480static struct mousetable
2481{
2482 int pseudo_code; /* Code for pseudo mouse event */
2483 int button; /* Which mouse button is it? */
2484 int is_click; /* Is it a mouse button click event? */
2485 int is_drag; /* Is it a mouse drag event? */
2486} mouse_table[] =
2487{
2488 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2489#ifdef FEAT_GUI
2490 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2491#endif
2492 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2493 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2494#ifdef FEAT_GUI
2495 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2496#endif
2497 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2498 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2499 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2500 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2501 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2502 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2503 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2504 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2505 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2506 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2507 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2508 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2509 /* DRAG without CLICK */
2510 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2511 /* RELEASE without CLICK */
2512 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2513 {0, 0, 0, 0},
2514};
2515#endif /* FEAT_MOUSE */
2516
2517/*
2518 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2519 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2520 */
2521 int
2522name_to_mod_mask(c)
2523 int c;
2524{
2525 int i;
2526
2527 c = TOUPPER_ASC(c);
2528 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2529 if (c == mod_mask_table[i].name)
2530 return mod_mask_table[i].mod_flag;
2531 return 0;
2532}
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
2535 * Check if if there is a special key code for "key" that includes the
2536 * modifiers specified.
2537 */
2538 int
2539simplify_key(key, modifiers)
2540 int key;
2541 int *modifiers;
2542{
2543 int i;
2544 int key0;
2545 int key1;
2546
2547 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2548 {
2549 /* TAB is a special case */
2550 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2551 {
2552 *modifiers &= ~MOD_MASK_SHIFT;
2553 return K_S_TAB;
2554 }
2555 key0 = KEY2TERMCAP0(key);
2556 key1 = KEY2TERMCAP1(key);
2557 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2558 if (key0 == modifier_keys_table[i + 3]
2559 && key1 == modifier_keys_table[i + 4]
2560 && (*modifiers & modifier_keys_table[i]))
2561 {
2562 *modifiers &= ~modifier_keys_table[i];
2563 return TERMCAP2KEY(modifier_keys_table[i + 1],
2564 modifier_keys_table[i + 2]);
2565 }
2566 }
2567 return key;
2568}
2569
2570/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002571 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002572 */
2573 int
2574handle_x_keys(key)
2575 int key;
2576{
2577 switch (key)
2578 {
2579 case K_XUP: return K_UP;
2580 case K_XDOWN: return K_DOWN;
2581 case K_XLEFT: return K_LEFT;
2582 case K_XRIGHT: return K_RIGHT;
2583 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002584 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002585 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002586 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002587 case K_XF1: return K_F1;
2588 case K_XF2: return K_F2;
2589 case K_XF3: return K_F3;
2590 case K_XF4: return K_F4;
2591 case K_S_XF1: return K_S_F1;
2592 case K_S_XF2: return K_S_F2;
2593 case K_S_XF3: return K_S_F3;
2594 case K_S_XF4: return K_S_F4;
2595 }
2596 return key;
2597}
2598
2599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 * Return a string which contains the name of the given key when the given
2601 * modifiers are down.
2602 */
2603 char_u *
2604get_special_key_name(c, modifiers)
2605 int c;
2606 int modifiers;
2607{
2608 static char_u string[MAX_KEY_NAME_LEN + 1];
2609
2610 int i, idx;
2611 int table_idx;
2612 char_u *s;
2613
2614 string[0] = '<';
2615 idx = 1;
2616
2617 /* Key that stands for a normal character. */
2618 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2619 c = KEY2TERMCAP1(c);
2620
2621 /*
2622 * Translate shifted special keys into unshifted keys and set modifier.
2623 * Same for CTRL and ALT modifiers.
2624 */
2625 if (IS_SPECIAL(c))
2626 {
2627 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2628 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2629 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2630 {
2631 modifiers |= modifier_keys_table[i];
2632 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2633 modifier_keys_table[i + 4]);
2634 break;
2635 }
2636 }
2637
2638 /* try to find the key in the special key table */
2639 table_idx = find_special_key_in_table(c);
2640
2641 /*
2642 * When not a known special key, and not a printable character, try to
2643 * extract modifiers.
2644 */
2645 if (c > 0
2646#ifdef FEAT_MBYTE
2647 && (*mb_char2len)(c) == 1
2648#endif
2649 )
2650 {
2651 if (table_idx < 0
2652 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2653 && (c & 0x80))
2654 {
2655 c &= 0x7f;
2656 modifiers |= MOD_MASK_ALT;
2657 /* try again, to find the un-alted key in the special key table */
2658 table_idx = find_special_key_in_table(c);
2659 }
2660 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2661 {
2662#ifdef EBCDIC
2663 c = CtrlChar(c);
2664#else
2665 c += '@';
2666#endif
2667 modifiers |= MOD_MASK_CTRL;
2668 }
2669 }
2670
2671 /* translate the modifier into a string */
2672 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2673 if ((modifiers & mod_mask_table[i].mod_mask)
2674 == mod_mask_table[i].mod_flag)
2675 {
2676 string[idx++] = mod_mask_table[i].name;
2677 string[idx++] = (char_u)'-';
2678 }
2679
2680 if (table_idx < 0) /* unknown special key, may output t_xx */
2681 {
2682 if (IS_SPECIAL(c))
2683 {
2684 string[idx++] = 't';
2685 string[idx++] = '_';
2686 string[idx++] = KEY2TERMCAP0(c);
2687 string[idx++] = KEY2TERMCAP1(c);
2688 }
2689 /* Not a special key, only modifiers, output directly */
2690 else
2691 {
2692#ifdef FEAT_MBYTE
2693 if (has_mbyte && (*mb_char2len)(c) > 1)
2694 idx += (*mb_char2bytes)(c, string + idx);
2695 else
2696#endif
2697 if (vim_isprintc(c))
2698 string[idx++] = c;
2699 else
2700 {
2701 s = transchar(c);
2702 while (*s)
2703 string[idx++] = *s++;
2704 }
2705 }
2706 }
2707 else /* use name of special key */
2708 {
2709 STRCPY(string + idx, key_names_table[table_idx].name);
2710 idx = (int)STRLEN(string);
2711 }
2712 string[idx++] = '>';
2713 string[idx] = NUL;
2714 return string;
2715}
2716
2717/*
2718 * Try translating a <> name at (*srcp)[] to dst[].
2719 * Return the number of characters added to dst[], zero for no match.
2720 * If there is a match, srcp is advanced to after the <> name.
2721 * dst[] must be big enough to hold the result (up to six characters)!
2722 */
2723 int
2724trans_special(srcp, dst, keycode)
2725 char_u **srcp;
2726 char_u *dst;
2727 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2728{
2729 int modifiers = 0;
2730 int key;
2731 int dlen = 0;
2732
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002733 key = find_special_key(srcp, &modifiers, keycode, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (key == 0)
2735 return 0;
2736
2737 /* Put the appropriate modifier in a string */
2738 if (modifiers != 0)
2739 {
2740 dst[dlen++] = K_SPECIAL;
2741 dst[dlen++] = KS_MODIFIER;
2742 dst[dlen++] = modifiers;
2743 }
2744
2745 if (IS_SPECIAL(key))
2746 {
2747 dst[dlen++] = K_SPECIAL;
2748 dst[dlen++] = KEY2TERMCAP0(key);
2749 dst[dlen++] = KEY2TERMCAP1(key);
2750 }
2751#ifdef FEAT_MBYTE
2752 else if (has_mbyte && !keycode)
2753 dlen += (*mb_char2bytes)(key, dst + dlen);
2754#endif
2755 else if (keycode)
2756 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2757 else
2758 dst[dlen++] = key;
2759
2760 return dlen;
2761}
2762
2763/*
2764 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2765 * srcp is advanced to after the <> name.
2766 * returns 0 if there is no match.
2767 */
2768 int
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002769find_special_key(srcp, modp, keycode, keep_x_key)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 char_u **srcp;
2771 int *modp;
Bram Moolenaar2a8ced02008-12-24 11:54:31 +00002772 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2773 int keep_x_key; /* don't translate xHome to Home key */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 char_u *last_dash;
2776 char_u *end_of_name;
2777 char_u *src;
2778 char_u *bp;
2779 int modifiers;
2780 int bit;
2781 int key;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002782 unsigned long n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002783 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 src = *srcp;
2786 if (src[0] != '<')
2787 return 0;
2788
2789 /* Find end of modifier list */
2790 last_dash = src;
2791 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2792 {
2793 if (*bp == '-')
2794 {
2795 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002796 if (bp[1] != NUL)
2797 {
2798#ifdef FEAT_MBYTE
2799 if (has_mbyte)
2800 l = mb_ptr2len(bp + 1);
2801 else
2802#endif
2803 l = 1;
2804 if (bp[l + 1] == '>')
2805 bp += l; /* anything accepted, like <C-?> */
2806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2809 bp += 3; /* skip t_xx, xx may be '-' or '>' */
Bram Moolenaar792826c2011-08-19 22:29:02 +02002810 else if (STRNICMP(bp, "char-", 5) == 0)
2811 {
2812 vim_str2nr(bp + 5, NULL, &l, TRUE, TRUE, NULL, NULL);
2813 bp += l + 5;
2814 break;
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817
2818 if (*bp == '>') /* found matching '>' */
2819 {
2820 end_of_name = bp + 1;
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 /* Which modifiers are given? */
2823 modifiers = 0x0;
2824 for (bp = src + 1; bp < last_dash; bp++)
2825 {
2826 if (*bp != '-')
2827 {
2828 bit = name_to_mod_mask(*bp);
2829 if (bit == 0x0)
2830 break; /* Illegal modifier name */
2831 modifiers |= bit;
2832 }
2833 }
2834
2835 /*
2836 * Legal modifier name.
2837 */
2838 if (bp >= last_dash)
2839 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002840 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2841 && VIM_ISDIGIT(last_dash[6]))
2842 {
2843 /* <Char-123> or <Char-033> or <Char-0x33> */
2844 vim_str2nr(last_dash + 6, NULL, NULL, TRUE, TRUE, NULL, &n);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002845 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002848 {
Bram Moolenaar792826c2011-08-19 22:29:02 +02002849 /*
2850 * Modifier with single letter, or special key name.
2851 */
2852#ifdef FEAT_MBYTE
2853 if (has_mbyte)
2854 l = mb_ptr2len(last_dash + 1);
2855 else
2856#endif
2857 l = 1;
2858 if (modifiers != 0 && last_dash[l + 1] == '>')
2859 key = PTR2CHAR(last_dash + 1);
2860 else
2861 {
2862 key = get_special_key_code(last_dash + 1);
2863 if (!keep_x_key)
2864 key = handle_x_keys(key);
2865 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
2868 /*
2869 * get_special_key_code() may return NUL for invalid
2870 * special key name.
2871 */
2872 if (key != NUL)
2873 {
2874 /*
2875 * Only use a modifier when there is no special key code that
2876 * includes the modifier.
2877 */
2878 key = simplify_key(key, &modifiers);
2879
2880 if (!keycode)
2881 {
2882 /* don't want keycode, use single byte code */
2883 if (key == K_BS)
2884 key = BS;
2885 else if (key == K_DEL || key == K_KDEL)
2886 key = DEL;
2887 }
2888
2889 /*
2890 * Normal Key with modifier: Try to make a single byte code.
2891 */
2892 if (!IS_SPECIAL(key))
2893 key = extract_modifiers(key, &modifiers);
2894
2895 *modp = modifiers;
2896 *srcp = end_of_name;
2897 return key;
2898 }
2899 }
2900 }
2901 return 0;
2902}
2903
2904/*
2905 * Try to include modifiers in the key.
2906 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2907 */
2908 int
2909extract_modifiers(key, modp)
2910 int key;
2911 int *modp;
2912{
2913 int modifiers = *modp;
2914
2915#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002916 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 if (!(modifiers & MOD_MASK_CMD))
2918#endif
2919 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2920 {
2921 key = TOUPPER_ASC(key);
2922 modifiers &= ~MOD_MASK_SHIFT;
2923 }
2924 if ((modifiers & MOD_MASK_CTRL)
2925#ifdef EBCDIC
2926 /* * TODO: EBCDIC Better use:
2927 * && (Ctrl_chr(key) || key == '?')
2928 * ??? */
2929 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2930 != NULL
2931#else
2932 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2933#endif
2934 )
2935 {
2936 key = Ctrl_chr(key);
2937 modifiers &= ~MOD_MASK_CTRL;
2938 /* <C-@> is <Nul> */
2939 if (key == 0)
2940 key = K_ZERO;
2941 }
2942#ifdef MACOS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002943 /* Command-key really special, no fancynest */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if (!(modifiers & MOD_MASK_CMD))
2945#endif
2946 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2947#ifdef FEAT_MBYTE
2948 && !enc_dbcs /* avoid creating a lead byte */
2949#endif
2950 )
2951 {
2952 key |= 0x80;
2953 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2954 }
2955
2956 *modp = modifiers;
2957 return key;
2958}
2959
2960/*
2961 * Try to find key "c" in the special key table.
2962 * Return the index when found, -1 when not found.
2963 */
2964 int
2965find_special_key_in_table(c)
2966 int c;
2967{
2968 int i;
2969
2970 for (i = 0; key_names_table[i].name != NULL; i++)
2971 if (c == key_names_table[i].key)
2972 break;
2973 if (key_names_table[i].name == NULL)
2974 i = -1;
2975 return i;
2976}
2977
2978/*
2979 * Find the special key with the given name (the given string does not have to
2980 * end with NUL, the name is assumed to end before the first non-idchar).
2981 * If the name starts with "t_" the next two characters are interpreted as a
2982 * termcap name.
2983 * Return the key code, or 0 if not found.
2984 */
2985 int
2986get_special_key_code(name)
2987 char_u *name;
2988{
2989 char_u *table_name;
2990 char_u string[3];
2991 int i, j;
2992
2993 /*
2994 * If it's <t_xx> we get the code for xx from the termcap
2995 */
2996 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
2997 {
2998 string[0] = name[2];
2999 string[1] = name[3];
3000 string[2] = NUL;
3001 if (add_termcap_entry(string, FALSE) == OK)
3002 return TERMCAP2KEY(name[2], name[3]);
3003 }
3004 else
3005 for (i = 0; key_names_table[i].name != NULL; i++)
3006 {
3007 table_name = key_names_table[i].name;
3008 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
3009 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
3010 break;
3011 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
3012 return key_names_table[i].key;
3013 }
3014 return 0;
3015}
3016
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003017#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 char_u *
3019get_key_name(i)
3020 int i;
3021{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003022 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 return NULL;
3024 return key_names_table[i].name;
3025}
3026#endif
3027
Bram Moolenaar05bb9532008-07-04 09:44:11 +00003028#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029/*
3030 * Look up the given mouse code to return the relevant information in the other
3031 * arguments. Return which button is down or was released.
3032 */
3033 int
3034get_mouse_button(code, is_click, is_drag)
3035 int code;
3036 int *is_click;
3037 int *is_drag;
3038{
3039 int i;
3040
3041 for (i = 0; mouse_table[i].pseudo_code; i++)
3042 if (code == mouse_table[i].pseudo_code)
3043 {
3044 *is_click = mouse_table[i].is_click;
3045 *is_drag = mouse_table[i].is_drag;
3046 return mouse_table[i].button;
3047 }
3048 return 0; /* Shouldn't get here */
3049}
3050
3051/*
3052 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
3053 * the given information about which mouse button is down, and whether the
3054 * mouse was clicked, dragged or released.
3055 */
3056 int
3057get_pseudo_mouse_code(button, is_click, is_drag)
3058 int button; /* eg MOUSE_LEFT */
3059 int is_click;
3060 int is_drag;
3061{
3062 int i;
3063
3064 for (i = 0; mouse_table[i].pseudo_code; i++)
3065 if (button == mouse_table[i].button
3066 && is_click == mouse_table[i].is_click
3067 && is_drag == mouse_table[i].is_drag)
3068 {
3069#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003070 /* Trick: a non mappable left click and release has mouse_col -1
3071 * or added MOUSE_COLOFF. Used for 'mousefocus' in
3072 * gui_mouse_moved() */
3073 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00003075 if (mouse_col < 0)
3076 mouse_col = 0;
3077 else
3078 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
3080 return (int)KE_LEFTMOUSE_NM;
3081 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
3082 return (int)KE_LEFTRELEASE_NM;
3083 }
3084#endif
3085 return mouse_table[i].pseudo_code;
3086 }
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003087 return (int)KE_IGNORE; /* not recognized, ignore it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089#endif /* FEAT_MOUSE */
3090
3091/*
3092 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
3093 */
3094 int
3095get_fileformat(buf)
3096 buf_T *buf;
3097{
3098 int c = *buf->b_p_ff;
3099
3100 if (buf->b_p_bin || c == 'u')
3101 return EOL_UNIX;
3102 if (c == 'm')
3103 return EOL_MAC;
3104 return EOL_DOS;
3105}
3106
3107/*
3108 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
3109 * argument.
3110 */
3111 int
3112get_fileformat_force(buf, eap)
3113 buf_T *buf;
3114 exarg_T *eap; /* can be NULL! */
3115{
3116 int c;
3117
3118 if (eap != NULL && eap->force_ff != 0)
3119 c = eap->cmd[eap->force_ff];
3120 else
3121 {
3122 if ((eap != NULL && eap->force_bin != 0)
3123 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
3124 return EOL_UNIX;
3125 c = *buf->b_p_ff;
3126 }
3127 if (c == 'u')
3128 return EOL_UNIX;
3129 if (c == 'm')
3130 return EOL_MAC;
3131 return EOL_DOS;
3132}
3133
3134/*
3135 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
3136 * Sets both 'textmode' and 'fileformat'.
3137 * Note: Does _not_ set global value of 'textmode'!
3138 */
3139 void
3140set_fileformat(t, opt_flags)
3141 int t;
3142 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */
3143{
3144 char *p = NULL;
3145
3146 switch (t)
3147 {
3148 case EOL_DOS:
3149 p = FF_DOS;
3150 curbuf->b_p_tx = TRUE;
3151 break;
3152 case EOL_UNIX:
3153 p = FF_UNIX;
3154 curbuf->b_p_tx = FALSE;
3155 break;
3156 case EOL_MAC:
3157 p = FF_MAC;
3158 curbuf->b_p_tx = FALSE;
3159 break;
3160 }
3161 if (p != NULL)
3162 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003163 OPT_FREE | opt_flags, 0);
3164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00003166 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003168 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
3170#ifdef FEAT_TITLE
3171 need_maketitle = TRUE; /* set window title later */
3172#endif
3173}
3174
3175/*
3176 * Return the default fileformat from 'fileformats'.
3177 */
3178 int
3179default_fileformat()
3180{
3181 switch (*p_ffs)
3182 {
3183 case 'm': return EOL_MAC;
3184 case 'd': return EOL_DOS;
3185 }
3186 return EOL_UNIX;
3187}
3188
3189/*
3190 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
3191 */
3192 int
3193call_shell(cmd, opt)
3194 char_u *cmd;
3195 int opt;
3196{
3197 char_u *ncmd;
3198 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003199#ifdef FEAT_PROFILE
3200 proftime_T wait_time;
3201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203 if (p_verbose > 3)
3204 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003205 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00003206 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 cmd == NULL ? p_sh : cmd);
3208 out_char('\n');
3209 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00003210 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212
Bram Moolenaar05159a02005-02-26 23:04:13 +00003213#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003214 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003215 prof_child_enter(&wait_time);
3216#endif
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (*p_sh == NUL)
3219 {
3220 EMSG(_(e_shellempty));
3221 retval = -1;
3222 }
3223 else
3224 {
3225#ifdef FEAT_GUI_MSWIN
3226 /* Don't hide the pointer while executing a shell command. */
3227 gui_mch_mousehide(FALSE);
3228#endif
3229#ifdef FEAT_GUI
3230 ++hold_gui_events;
3231#endif
3232 /* The external command may update a tags file, clear cached tags. */
3233 tag_freematch();
3234
3235 if (cmd == NULL || *p_sxq == NUL)
3236 retval = mch_call_shell(cmd, opt);
3237 else
3238 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003239 char_u *ecmd = cmd;
3240
3241 if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
3242 {
3243 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3244 if (ecmd == NULL)
3245 ecmd = cmd;
3246 }
3247 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (ncmd != NULL)
3249 {
3250 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003251 STRCAT(ncmd, ecmd);
Bram Moolenaar034b1152012-02-19 18:19:30 +01003252 /* When 'shellxquote' is ( append ).
3253 * When 'shellxquote' is "( append )". */
3254 STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
3255 : STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
3256 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 retval = mch_call_shell(ncmd, opt);
3258 vim_free(ncmd);
3259 }
3260 else
3261 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01003262 if (ecmd != cmd)
3263 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265#ifdef FEAT_GUI
3266 --hold_gui_events;
3267#endif
3268 /*
3269 * Check the window size, in case it changed while executing the
3270 * external command.
3271 */
3272 shell_resized_check();
3273 }
3274
3275#ifdef FEAT_EVAL
3276 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003277# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00003278 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003279 prof_child_exit(&wait_time);
3280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#endif
3282
3283 return retval;
3284}
3285
3286/*
Bram Moolenaar01265852006-03-20 21:50:15 +00003287 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
3288 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 */
3290 int
3291get_real_state()
3292{
3293 if (State & NORMAL)
3294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00003296 {
3297 if (VIsual_select)
3298 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00003300 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003301 else if (finish_op)
3302 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304 return State;
3305}
3306
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003307#if defined(FEAT_MBYTE) || defined(PROTO)
3308/*
3309 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003310 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003311 * "b" must point to the start of the file name
3312 */
3313 int
3314after_pathsep(b, p)
3315 char_u *b;
3316 char_u *p;
3317{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02003318 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003319 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
3320}
3321#endif
3322
3323/*
3324 * Return TRUE if file names "f1" and "f2" are in the same directory.
3325 * "f1" may be a short name, "f2" must be a full path.
3326 */
3327 int
3328same_directory(f1, f2)
3329 char_u *f1;
3330 char_u *f2;
3331{
3332 char_u ffname[MAXPATHL];
3333 char_u *t1;
3334 char_u *t2;
3335
3336 /* safety check */
3337 if (f1 == NULL || f2 == NULL)
3338 return FALSE;
3339
3340 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
3341 t1 = gettail_sep(ffname);
3342 t2 = gettail_sep(f2);
3343 return (t1 - ffname == t2 - f2
3344 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
3345}
3346
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00003348 || ((defined(FEAT_GUI_GTK)) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00003349 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
3351 || defined(PROTO)
3352/*
3353 * Change to a file's directory.
3354 * Caller must call shorten_fnames()!
3355 * Return OK or FAIL.
3356 */
3357 int
3358vim_chdirfile(fname)
3359 char_u *fname;
3360{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003361 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003363 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003364 *gettail_sep(dir) = NUL;
3365 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366}
3367#endif
3368
3369#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3370/*
3371 * Check if "name" ends in a slash and is not a directory.
3372 * Used for systems where stat() ignores a trailing slash on a file name.
3373 * The Vim code assumes a trailing slash is only ignored for a directory.
3374 */
3375 int
3376illegal_slash(name)
3377 char *name;
3378{
3379 if (name[0] == NUL)
3380 return FALSE; /* no file name is not illegal */
3381 if (name[strlen(name) - 1] != '/')
3382 return FALSE; /* no trailing slash */
3383 if (mch_isdir((char_u *)name))
3384 return FALSE; /* trailing slash for a directory */
3385 return TRUE;
3386}
3387#endif
3388
3389#if defined(CURSOR_SHAPE) || defined(PROTO)
3390
3391/*
3392 * Handling of cursor and mouse pointer shapes in various modes.
3393 */
3394
3395cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3396{
3397 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3398 * defaults when Vim starts.
3399 * Adjust the SHAPE_IDX_ defines when making changes! */
3400 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3401 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3402 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3403 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3404 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3405 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3406 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3407 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3408 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3409 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3410 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3411 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3412 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3413 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3414 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3415 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3416 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3417};
3418
3419#ifdef FEAT_MOUSESHAPE
3420/*
3421 * Table with names for mouse shapes. Keep in sync with all the tables for
3422 * mch_set_mouse_shape()!.
3423 */
3424static char * mshape_names[] =
3425{
3426 "arrow", /* default, must be the first one */
3427 "blank", /* hidden */
3428 "beam",
3429 "updown",
3430 "udsizing",
3431 "leftright",
3432 "lrsizing",
3433 "busy",
3434 "no",
3435 "crosshair",
3436 "hand1",
3437 "hand2",
3438 "pencil",
3439 "question",
3440 "rightup-arrow",
3441 "up-arrow",
3442 NULL
3443};
3444#endif
3445
3446/*
3447 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3448 * ("what" is SHAPE_MOUSE).
3449 * Returns error message for an illegal option, NULL otherwise.
3450 */
3451 char_u *
3452parse_shape_opt(what)
3453 int what;
3454{
3455 char_u *modep;
3456 char_u *colonp;
3457 char_u *commap;
3458 char_u *slashp;
3459 char_u *p, *endp;
3460 int idx = 0; /* init for GCC */
3461 int all_idx;
3462 int len;
3463 int i;
3464 long n;
3465 int found_ve = FALSE; /* found "ve" flag */
3466 int round;
3467
3468 /*
3469 * First round: check for errors; second round: do it for real.
3470 */
3471 for (round = 1; round <= 2; ++round)
3472 {
3473 /*
3474 * Repeat for all comma separated parts.
3475 */
3476#ifdef FEAT_MOUSESHAPE
3477 if (what == SHAPE_MOUSE)
3478 modep = p_mouseshape;
3479 else
3480#endif
3481 modep = p_guicursor;
3482 while (*modep != NUL)
3483 {
3484 colonp = vim_strchr(modep, ':');
3485 if (colonp == NULL)
3486 return (char_u *)N_("E545: Missing colon");
3487 if (colonp == modep)
3488 return (char_u *)N_("E546: Illegal mode");
3489 commap = vim_strchr(modep, ',');
3490
3491 /*
3492 * Repeat for all mode's before the colon.
3493 * For the 'a' mode, we loop to handle all the modes.
3494 */
3495 all_idx = -1;
3496 while (modep < colonp || all_idx >= 0)
3497 {
3498 if (all_idx < 0)
3499 {
3500 /* Find the mode. */
3501 if (modep[1] == '-' || modep[1] == ':')
3502 len = 1;
3503 else
3504 len = 2;
3505 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3506 all_idx = SHAPE_IDX_COUNT - 1;
3507 else
3508 {
3509 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3510 if (STRNICMP(modep, shape_table[idx].name, len)
3511 == 0)
3512 break;
3513 if (idx == SHAPE_IDX_COUNT
3514 || (shape_table[idx].used_for & what) == 0)
3515 return (char_u *)N_("E546: Illegal mode");
3516 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3517 found_ve = TRUE;
3518 }
3519 modep += len + 1;
3520 }
3521
3522 if (all_idx >= 0)
3523 idx = all_idx--;
3524 else if (round == 2)
3525 {
3526#ifdef FEAT_MOUSESHAPE
3527 if (what == SHAPE_MOUSE)
3528 {
3529 /* Set the default, for the missing parts */
3530 shape_table[idx].mshape = 0;
3531 }
3532 else
3533#endif
3534 {
3535 /* Set the defaults, for the missing parts */
3536 shape_table[idx].shape = SHAPE_BLOCK;
3537 shape_table[idx].blinkwait = 700L;
3538 shape_table[idx].blinkon = 400L;
3539 shape_table[idx].blinkoff = 250L;
3540 }
3541 }
3542
3543 /* Parse the part after the colon */
3544 for (p = colonp + 1; *p && *p != ','; )
3545 {
3546#ifdef FEAT_MOUSESHAPE
3547 if (what == SHAPE_MOUSE)
3548 {
3549 for (i = 0; ; ++i)
3550 {
3551 if (mshape_names[i] == NULL)
3552 {
3553 if (!VIM_ISDIGIT(*p))
3554 return (char_u *)N_("E547: Illegal mouseshape");
3555 if (round == 2)
3556 shape_table[idx].mshape =
3557 getdigits(&p) + MSHAPE_NUMBERED;
3558 else
3559 (void)getdigits(&p);
3560 break;
3561 }
3562 len = (int)STRLEN(mshape_names[i]);
3563 if (STRNICMP(p, mshape_names[i], len) == 0)
3564 {
3565 if (round == 2)
3566 shape_table[idx].mshape = i;
3567 p += len;
3568 break;
3569 }
3570 }
3571 }
3572 else /* if (what == SHAPE_MOUSE) */
3573#endif
3574 {
3575 /*
3576 * First handle the ones with a number argument.
3577 */
3578 i = *p;
3579 len = 0;
3580 if (STRNICMP(p, "ver", 3) == 0)
3581 len = 3;
3582 else if (STRNICMP(p, "hor", 3) == 0)
3583 len = 3;
3584 else if (STRNICMP(p, "blinkwait", 9) == 0)
3585 len = 9;
3586 else if (STRNICMP(p, "blinkon", 7) == 0)
3587 len = 7;
3588 else if (STRNICMP(p, "blinkoff", 8) == 0)
3589 len = 8;
3590 if (len != 0)
3591 {
3592 p += len;
3593 if (!VIM_ISDIGIT(*p))
3594 return (char_u *)N_("E548: digit expected");
3595 n = getdigits(&p);
3596 if (len == 3) /* "ver" or "hor" */
3597 {
3598 if (n == 0)
3599 return (char_u *)N_("E549: Illegal percentage");
3600 if (round == 2)
3601 {
3602 if (TOLOWER_ASC(i) == 'v')
3603 shape_table[idx].shape = SHAPE_VER;
3604 else
3605 shape_table[idx].shape = SHAPE_HOR;
3606 shape_table[idx].percentage = n;
3607 }
3608 }
3609 else if (round == 2)
3610 {
3611 if (len == 9)
3612 shape_table[idx].blinkwait = n;
3613 else if (len == 7)
3614 shape_table[idx].blinkon = n;
3615 else
3616 shape_table[idx].blinkoff = n;
3617 }
3618 }
3619 else if (STRNICMP(p, "block", 5) == 0)
3620 {
3621 if (round == 2)
3622 shape_table[idx].shape = SHAPE_BLOCK;
3623 p += 5;
3624 }
3625 else /* must be a highlight group name then */
3626 {
3627 endp = vim_strchr(p, '-');
3628 if (commap == NULL) /* last part */
3629 {
3630 if (endp == NULL)
3631 endp = p + STRLEN(p); /* find end of part */
3632 }
3633 else if (endp > commap || endp == NULL)
3634 endp = commap;
3635 slashp = vim_strchr(p, '/');
3636 if (slashp != NULL && slashp < endp)
3637 {
3638 /* "group/langmap_group" */
3639 i = syn_check_group(p, (int)(slashp - p));
3640 p = slashp + 1;
3641 }
3642 if (round == 2)
3643 {
3644 shape_table[idx].id = syn_check_group(p,
3645 (int)(endp - p));
3646 shape_table[idx].id_lm = shape_table[idx].id;
3647 if (slashp != NULL && slashp < endp)
3648 shape_table[idx].id = i;
3649 }
3650 p = endp;
3651 }
3652 } /* if (what != SHAPE_MOUSE) */
3653
3654 if (*p == '-')
3655 ++p;
3656 }
3657 }
3658 modep = p;
3659 if (*modep == ',')
3660 ++modep;
3661 }
3662 }
3663
3664 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3665 if (!found_ve)
3666 {
3667#ifdef FEAT_MOUSESHAPE
3668 if (what == SHAPE_MOUSE)
3669 {
3670 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3671 }
3672 else
3673#endif
3674 {
3675 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3676 shape_table[SHAPE_IDX_VE].percentage =
3677 shape_table[SHAPE_IDX_V].percentage;
3678 shape_table[SHAPE_IDX_VE].blinkwait =
3679 shape_table[SHAPE_IDX_V].blinkwait;
3680 shape_table[SHAPE_IDX_VE].blinkon =
3681 shape_table[SHAPE_IDX_V].blinkon;
3682 shape_table[SHAPE_IDX_VE].blinkoff =
3683 shape_table[SHAPE_IDX_V].blinkoff;
3684 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3685 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3686 }
3687 }
3688
3689 return NULL;
3690}
3691
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003692# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3693 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
3695 * Return the index into shape_table[] for the current mode.
3696 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3697 */
3698 int
3699get_shape_idx(mouse)
3700 int mouse;
3701{
3702#ifdef FEAT_MOUSESHAPE
3703 if (mouse && (State == HITRETURN || State == ASKMORE))
3704 {
3705# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003706 int x, y;
3707 gui_mch_getmouse(&x, &y);
3708 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return SHAPE_IDX_MOREL;
3710# endif
3711 return SHAPE_IDX_MORE;
3712 }
3713 if (mouse && drag_status_line)
3714 return SHAPE_IDX_SDRAG;
3715# ifdef FEAT_VERTSPLIT
3716 if (mouse && drag_sep_line)
3717 return SHAPE_IDX_VDRAG;
3718# endif
3719#endif
3720 if (!mouse && State == SHOWMATCH)
3721 return SHAPE_IDX_SM;
3722#ifdef FEAT_VREPLACE
3723 if (State & VREPLACE_FLAG)
3724 return SHAPE_IDX_R;
3725#endif
3726 if (State & REPLACE_FLAG)
3727 return SHAPE_IDX_R;
3728 if (State & INSERT)
3729 return SHAPE_IDX_I;
3730 if (State & CMDLINE)
3731 {
3732 if (cmdline_at_end())
3733 return SHAPE_IDX_C;
3734 if (cmdline_overstrike())
3735 return SHAPE_IDX_CR;
3736 return SHAPE_IDX_CI;
3737 }
3738 if (finish_op)
3739 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (VIsual_active)
3741 {
3742 if (*p_sel == 'e')
3743 return SHAPE_IDX_VE;
3744 else
3745 return SHAPE_IDX_V;
3746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 return SHAPE_IDX_N;
3748}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
3751# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3752static int old_mouse_shape = 0;
3753
3754/*
3755 * Set the mouse shape:
3756 * If "shape" is -1, use shape depending on the current mode,
3757 * depending on the current state.
3758 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3759 * when the mouse moves off the status or command line).
3760 */
3761 void
3762update_mouseshape(shape_idx)
3763 int shape_idx;
3764{
3765 int new_mouse_shape;
3766
3767 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003768 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return;
3770
3771 /* Postpone the updating when more is to come. Speeds up executing of
3772 * mappings. */
3773 if (shape_idx == -1 && char_avail())
3774 {
3775 postponed_mouseshape = TRUE;
3776 return;
3777 }
3778
Bram Moolenaar14716812006-05-04 21:54:08 +00003779 /* When ignoring the mouse don't change shape on the statusline. */
3780 if (*p_mouse == NUL
3781 && (shape_idx == SHAPE_IDX_CLINE
3782 || shape_idx == SHAPE_IDX_STATUS
3783 || shape_idx == SHAPE_IDX_VSEP))
3784 shape_idx = -2;
3785
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (shape_idx == -2
3787 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3788 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3789 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3790 return;
3791 if (shape_idx < 0)
3792 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3793 else
3794 new_mouse_shape = shape_table[shape_idx].mshape;
3795 if (new_mouse_shape != old_mouse_shape)
3796 {
3797 mch_set_mouse_shape(new_mouse_shape);
3798 old_mouse_shape = new_mouse_shape;
3799 }
3800 postponed_mouseshape = FALSE;
3801}
3802# endif
3803
3804#endif /* CURSOR_SHAPE */
3805
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807/* TODO: make some #ifdef for this */
3808/*--------[ file searching ]-------------------------------------------------*/
3809/*
3810 * File searching functions for 'path', 'tags' and 'cdpath' options.
3811 * External visible functions:
3812 * vim_findfile_init() creates/initialises the search context
3813 * vim_findfile_free_visited() free list of visited files/dirs of search
3814 * context
3815 * vim_findfile() find a file in the search context
3816 * vim_findfile_cleanup() cleanup/free search context created by
3817 * vim_findfile_init()
3818 *
3819 * All static functions and variables start with 'ff_'
3820 *
3821 * In general it works like this:
3822 * First you create yourself a search context by calling vim_findfile_init().
3823 * It is possible to give a search context from a previous call to
3824 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3825 * until you are satisfied with the result or it returns NULL. On every call it
3826 * returns the next file which matches the conditions given to
3827 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3828 *
3829 * It is possible to call vim_findfile_init() again to reinitialise your search
3830 * with some new parameters. Don't forget to pass your old search context to
3831 * it, so it can reuse it and especially reuse the list of already visited
3832 * directories. If you want to delete the list of already visited directories
3833 * simply call vim_findfile_free_visited().
3834 *
3835 * When you are done call vim_findfile_cleanup() to free the search context.
3836 *
3837 * The function vim_findfile_init() has a long comment, which describes the
3838 * needed parameters.
3839 *
3840 *
3841 *
3842 * ATTENTION:
3843 * ==========
Bram Moolenaar77f66d62007-02-20 02:16:18 +00003844 * Also we use an allocated search context here, this functions are NOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 * thread-safe!!!!!
3846 *
3847 * To minimize parameter passing (or because I'm to lazy), only the
3848 * external visible functions get a search context as a parameter. This is
3849 * then assigned to a static global, which is used throughout the local
3850 * functions.
3851 */
3852
3853/*
3854 * type for the directory search stack
3855 */
3856typedef struct ff_stack
3857{
3858 struct ff_stack *ffs_prev;
3859
3860 /* the fix part (no wildcards) and the part containing the wildcards
3861 * of the search path
3862 */
3863 char_u *ffs_fix_path;
3864#ifdef FEAT_PATH_EXTRA
3865 char_u *ffs_wc_path;
3866#endif
3867
3868 /* files/dirs found in the above directory, matched by the first wildcard
3869 * of wc_part
3870 */
3871 char_u **ffs_filearray;
3872 int ffs_filearray_size;
3873 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3874
3875 /* to store status of partly handled directories
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003876 * 0: we work on this directory for the first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 * 1: this directory was partly searched in an earlier step
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 int ffs_stage;
3880
3881 /* How deep are we in the directory tree?
3882 * Counts backward from value of level parameter to vim_findfile_init
3883 */
3884 int ffs_level;
3885
3886 /* Did we already expand '**' to an empty string? */
3887 int ffs_star_star_empty;
3888} ff_stack_T;
3889
3890/*
3891 * type for already visited directories or files.
3892 */
3893typedef struct ff_visited
3894{
3895 struct ff_visited *ffv_next;
3896
3897#ifdef FEAT_PATH_EXTRA
3898 /* Visited directories are different if the wildcard string are
3899 * different. So we have to save it.
3900 */
3901 char_u *ffv_wc_path;
3902#endif
3903 /* for unix use inode etc for comparison (needed because of links), else
3904 * use filename.
3905 */
3906#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00003907 int ffv_dev_valid; /* ffv_dev and ffv_ino were set */
3908 dev_t ffv_dev; /* device number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 ino_t ffv_ino; /* inode number */
3910#endif
3911 /* The memory for this struct is allocated according to the length of
3912 * ffv_fname.
3913 */
3914 char_u ffv_fname[1]; /* actually longer */
3915} ff_visited_T;
3916
3917/*
3918 * We might have to manage several visited lists during a search.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003919 * This is especially needed for the tags option. If tags is set to:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3921 * So we have to do 3 searches:
3922 * 1) search from the current files directory downward for the file "tags"
3923 * 2) search from the current files directory downward for the file "TAGS"
3924 * 3) search from Vims current directory downwards for the file "tags"
3925 * As you can see, the first and the third search are for the same file, so for
3926 * the third search we can use the visited list of the first search. For the
3927 * second search we must start from a empty visited list.
3928 * The struct ff_visited_list_hdr is used to manage a linked list of already
3929 * visited lists.
3930 */
3931typedef struct ff_visited_list_hdr
3932{
3933 struct ff_visited_list_hdr *ffvl_next;
3934
3935 /* the filename the attached visited list is for */
3936 char_u *ffvl_filename;
3937
3938 ff_visited_T *ffvl_visited_list;
3939
3940} ff_visited_list_hdr_T;
3941
3942
3943/*
3944 * '**' can be expanded to several directory levels.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003945 * Set the default maximum depth.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 */
3947#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949/*
3950 * The search context:
3951 * ffsc_stack_ptr: the stack for the dirs to search
3952 * ffsc_visited_list: the currently active visited list
3953 * ffsc_dir_visited_list: the currently active visited list for search dirs
3954 * ffsc_visited_lists_list: the list of all visited lists
3955 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3956 * ffsc_file_to_search: the file to search for
3957 * ffsc_start_dir: the starting directory, if search path was relative
3958 * ffsc_fix_path: the fix part of the given path (without wildcards)
3959 * Needed for upward search.
3960 * ffsc_wc_path: the part of the given path containing wildcards
3961 * ffsc_level: how many levels of dirs to search downwards
3962 * ffsc_stopdirs_v: array of stop directories for upward search
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003963 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
Bram Moolenaar463ee342010-08-08 18:17:52 +02003964 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 */
3966typedef struct ff_search_ctx_T
3967{
3968 ff_stack_T *ffsc_stack_ptr;
3969 ff_visited_list_hdr_T *ffsc_visited_list;
3970 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3971 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3972 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3973 char_u *ffsc_file_to_search;
3974 char_u *ffsc_start_dir;
3975 char_u *ffsc_fix_path;
3976#ifdef FEAT_PATH_EXTRA
3977 char_u *ffsc_wc_path;
3978 int ffsc_level;
3979 char_u **ffsc_stopdirs_v;
3980#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003981 int ffsc_find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02003982 int ffsc_tagfile;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003983} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985/* locally needed functions */
3986#ifdef FEAT_PATH_EXTRA
3987static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *));
3988#else
3989static int ff_check_visited __ARGS((ff_visited_T **, char_u *));
3990#endif
3991static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp));
3992static void ff_free_visited_list __ARGS((ff_visited_T *vl));
3993static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp));
3994#ifdef FEAT_PATH_EXTRA
3995static int ff_wc_equal __ARGS((char_u *s1, char_u *s2));
3996#endif
3997
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00003998static void ff_push __ARGS((ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr));
3999static ff_stack_T *ff_pop __ARGS((ff_search_ctx_T *search_ctx));
4000static void ff_clear __ARGS((ff_search_ctx_T *search_ctx));
4001static void ff_free_stack_element __ARGS((ff_stack_T *stack_ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#ifdef FEAT_PATH_EXTRA
4003static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int));
4004#else
4005static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int));
4006#endif
4007#ifdef FEAT_PATH_EXTRA
4008static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **));
4009#endif
4010
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004011static char_u e_pathtoolong[] = N_("E854: path too long for completion");
4012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#if 0
4014/*
4015 * if someone likes findfirst/findnext, here are the functions
4016 * NOT TESTED!!
4017 */
4018
4019static void *ff_fn_search_context = NULL;
4020
4021 char_u *
4022vim_findfirst(path, filename, level)
4023 char_u *path;
4024 char_u *filename;
4025 int level;
4026{
4027 ff_fn_search_context =
4028 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
4029 ff_fn_search_context, rel_fname);
4030 if (NULL == ff_fn_search_context)
4031 return NULL;
4032 else
4033 return vim_findnext()
4034}
4035
4036 char_u *
4037vim_findnext()
4038{
4039 char_u *ret = vim_findfile(ff_fn_search_context);
4040
4041 if (NULL == ret)
4042 {
4043 vim_findfile_cleanup(ff_fn_search_context);
4044 ff_fn_search_context = NULL;
4045 }
4046 return ret;
4047}
4048#endif
4049
4050/*
Bram Moolenaare2b590e2010-08-08 18:29:48 +02004051 * Initialization routine for vim_findfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 *
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004053 * Returns the newly allocated search context or NULL if an error occurred.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 *
4055 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
4056 * with the search context.
4057 *
4058 * Find the file 'filename' in the directory 'path'.
4059 * The parameter 'path' may contain wildcards. If so only search 'level'
4060 * directories deep. The parameter 'level' is the absolute maximum and is
4061 * not related to restricts given to the '**' wildcard. If 'level' is 100
4062 * and you use '**200' vim_findfile() will stop after 100 levels.
4063 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004064 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
4065 * escape special characters.
4066 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * If 'stopdirs' is not NULL and nothing is found downward, the search is
4068 * restarted on the next higher directory level. This is repeated until the
4069 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
4070 * format ";*<dirname>*\(;<dirname>\)*;\=$".
4071 *
4072 * If the 'path' is relative, the starting dir for the search is either VIM's
4073 * current dir or if the path starts with "./" the current files dir.
Bram Moolenaarbe18d102010-05-22 21:37:53 +02004074 * If the 'path' is absolute, the starting dir is that part of the path before
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * the first wildcard.
4076 *
4077 * Upward search is only done on the starting dir.
4078 *
4079 * If 'free_visited' is TRUE the list of already visited files/directories is
4080 * cleared. Set this to FALSE if you just want to search from another
4081 * directory, but want to be sure that no directory from a previous search is
4082 * searched again. This is useful if you search for a file at different places.
4083 * The list of visited files/dirs can also be cleared with the function
4084 * vim_findfile_free_visited().
4085 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004086 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
4087 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 *
4089 * A search context returned by a previous call to vim_findfile_init() can be
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004090 * passed in the parameter "search_ctx_arg". This context is reused and
4091 * reinitialized with the new parameters. The list of already visited
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * directories from this context is only deleted if the parameter
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004093 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
4094 * if the reinitialization fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004096 * If you don't have a search context from a previous call "search_ctx_arg"
4097 * must be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 *
4099 * This function silently ignores a few errors, vim_findfile() will have
4100 * limited functionality then.
4101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 void *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004103vim_findfile_init(path, filename, stopdirs, level, free_visited, find_what,
4104 search_ctx_arg, tagfile, rel_fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 char_u *path;
4106 char_u *filename;
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00004107 char_u *stopdirs UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int level;
4109 int free_visited;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004110 int find_what;
4111 void *search_ctx_arg;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004112 int tagfile; /* expanding names of tags files */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 char_u *rel_fname; /* file name to use for "." */
4114{
4115#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004116 char_u *wc_part;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004118 ff_stack_T *sptr;
4119 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 /* If a search context is given by the caller, reuse it, else allocate a
4122 * new one.
4123 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004124 if (search_ctx_arg != NULL)
4125 search_ctx = search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
4127 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004128 search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
4129 if (search_ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 goto error_return;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004131 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004133 search_ctx->ffsc_find_what = find_what;
Bram Moolenaar463ee342010-08-08 18:17:52 +02004134 search_ctx->ffsc_tagfile = tagfile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 /* clear the search context, but NOT the visited lists */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004137 ff_clear(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 /* clear visited list if wanted */
4140 if (free_visited == TRUE)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004141 vim_findfile_free_visited(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 else
4143 {
4144 /* Reuse old visited lists. Get the visited list for the given
4145 * filename. If no list for the current filename exists, creates a new
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004146 * one. */
4147 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
4148 &search_ctx->ffsc_visited_lists_list);
4149 if (search_ctx->ffsc_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 goto error_return;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004151 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
4152 &search_ctx->ffsc_dir_visited_lists_list);
4153 if (search_ctx->ffsc_dir_visited_list == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 goto error_return;
4155 }
4156
4157 if (ff_expand_buffer == NULL)
4158 {
4159 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
4160 if (ff_expand_buffer == NULL)
4161 goto error_return;
4162 }
4163
4164 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004165 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (path[0] == '.'
4167 && (vim_ispathsep(path[1]) || path[1] == NUL)
4168 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
4169 && rel_fname != NULL)
4170 {
4171 int len = (int)(gettail(rel_fname) - rel_fname);
4172
4173 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
4174 {
4175 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004176 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004177 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004180 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
4181 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 goto error_return;
4183 if (*++path != NUL)
4184 ++path;
4185 }
4186 else if (*path == NUL || !vim_isAbsName(path))
4187 {
4188#ifdef BACKSLASH_IN_FILENAME
4189 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
4190 if (*path != NUL && path[1] == ':')
4191 {
4192 char_u drive[3];
4193
4194 drive[0] = path[0];
4195 drive[1] = ':';
4196 drive[2] = NUL;
4197 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4198 goto error_return;
4199 path += 2;
4200 }
4201 else
4202#endif
4203 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
4204 goto error_return;
4205
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004206 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
4207 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 goto error_return;
4209
4210#ifdef BACKSLASH_IN_FILENAME
4211 /* A path that starts with "/dir" is relative to the drive, not to the
4212 * directory (but not for "//machine/dir"). Only use the drive name. */
4213 if ((*path == '/' || *path == '\\')
4214 && path[1] != path[0]
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004215 && search_ctx->ffsc_start_dir[1] == ':')
4216 search_ctx->ffsc_start_dir[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#endif
4218 }
4219
4220#ifdef FEAT_PATH_EXTRA
4221 /*
4222 * If stopdirs are given, split them into an array of pointers.
4223 * If this fails (mem allocation), there is no upward search at all or a
4224 * stop directory is not recognized -> continue silently.
4225 * If stopdirs just contains a ";" or is empty,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004226 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 * is handled as unlimited upward search. See function
4228 * ff_path_in_stoplist() for details.
4229 */
4230 if (stopdirs != NULL)
4231 {
4232 char_u *walker = stopdirs;
4233 int dircount;
4234
4235 while (*walker == ';')
4236 walker++;
4237
4238 dircount = 1;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004239 search_ctx->ffsc_stopdirs_v =
4240 (char_u **)alloc((unsigned)sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004242 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244 do
4245 {
4246 char_u *helper;
4247 void *ptr;
4248
4249 helper = walker;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004250 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 (dircount + 1) * sizeof(char_u *));
4252 if (ptr)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004253 search_ctx->ffsc_stopdirs_v = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 else
4255 /* ignore, keep what we have and continue */
4256 break;
4257 walker = vim_strchr(walker, ';');
4258 if (walker)
4259 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004260 search_ctx->ffsc_stopdirs_v[dircount-1] =
4261 vim_strnsave(helper, (int)(walker - helper));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 walker++;
4263 }
4264 else
4265 /* this might be "", which means ascent till top
4266 * of directory tree.
4267 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004268 search_ctx->ffsc_stopdirs_v[dircount-1] =
4269 vim_strsave(helper);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271 dircount++;
4272
4273 } while (walker != NULL);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004274 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 }
4277#endif
4278
4279#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004280 search_ctx->ffsc_level = level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 /* split into:
4283 * -fix path
4284 * -wildcard_stuff (might be NULL)
4285 */
4286 wc_part = vim_strchr(path, '*');
4287 if (wc_part != NULL)
4288 {
4289 int llevel;
4290 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004291 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 /* save the fix part of the path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004294 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /*
4297 * copy wc_path and add restricts to the '**' wildcard.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004298 * The octet after a '**' is used as a (binary) counter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4300 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4301 * For EBCDIC you get different character values.
4302 * If no restrict is given after '**' the default is used.
Bram Moolenaar21160b92009-11-11 15:56:10 +00004303 * Due to this technique the path looks awful if you print it as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 * string.
4305 */
4306 len = 0;
4307 while (*wc_part != NUL)
4308 {
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004309 if (len + 5 >= MAXPATHL)
4310 {
4311 EMSG(_(e_pathtoolong));
4312 break;
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if (STRNCMP(wc_part, "**", 2) == 0)
4315 {
4316 ff_expand_buffer[len++] = *wc_part++;
4317 ff_expand_buffer[len++] = *wc_part++;
4318
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004319 llevel = strtol((char *)wc_part, &errpt, 10);
4320 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004322 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 /* restrict is 0 -> remove already added '**' */
4324 len -= 2;
4325 else
4326 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004327 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004328 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4331 goto error_return;
4332 }
4333 }
4334 else
4335 ff_expand_buffer[len++] = *wc_part++;
4336 }
4337 ff_expand_buffer[len] = NUL;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004338 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004340 if (search_ctx->ffsc_wc_path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 goto error_return;
4342 }
4343 else
4344#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004345 search_ctx->ffsc_fix_path = vim_strsave(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004347 if (search_ctx->ffsc_start_dir == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 /* store the fix part as startdir.
4350 * This is needed if the parameter path is fully qualified.
4351 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004352 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004353 if (search_ctx->ffsc_start_dir == NULL)
4354 goto error_return;
4355 search_ctx->ffsc_fix_path[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357
4358 /* create an absolute path */
Bram Moolenaarb9ba4032011-12-08 17:49:35 +01004359 if (STRLEN(search_ctx->ffsc_start_dir)
4360 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
4361 {
4362 EMSG(_(e_pathtoolong));
4363 goto error_return;
4364 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004365 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 add_pathsep(ff_expand_buffer);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004367 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004368 int eb_len = (int)STRLEN(ff_expand_buffer);
4369 char_u *buf = alloc(eb_len
4370 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004371
4372 STRCPY(buf, ff_expand_buffer);
Bram Moolenaar0f5a5ed2013-07-03 17:51:17 +02004373 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004374 if (mch_isdir(buf))
4375 {
4376 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
4377 add_pathsep(ff_expand_buffer);
4378 }
4379#ifdef FEAT_PATH_EXTRA
4380 else
4381 {
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004382 char_u *p = gettail(search_ctx->ffsc_fix_path);
Bram Moolenaar5f4c8402014-01-06 06:19:11 +01004383 char_u *wc_path = NULL;
4384 char_u *temp = NULL;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004385 int len = 0;
4386
Bram Moolenaaree0ee2a2013-07-03 21:19:07 +02004387 if (p > search_ctx->ffsc_fix_path)
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004388 {
Bram Moolenaar61214042013-07-04 21:19:33 +02004389 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004390 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
4391 add_pathsep(ff_expand_buffer);
4392 }
4393 else
Bram Moolenaar61214042013-07-04 21:19:33 +02004394 len = (int)STRLEN(search_ctx->ffsc_fix_path);
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004395
4396 if (search_ctx->ffsc_wc_path != NULL)
4397 {
4398 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar61214042013-07-04 21:19:33 +02004399 temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaare8785f22013-07-07 16:15:35 +02004400 + STRLEN(search_ctx->ffsc_fix_path + len)
4401 + 1));
Bram Moolenaarf4c5fcb2013-07-03 17:14:00 +02004402 }
4403
4404 if (temp == NULL || wc_path == NULL)
4405 {
4406 vim_free(buf);
4407 vim_free(temp);
4408 vim_free(wc_path);
4409 goto error_return;
4410 }
4411
4412 STRCPY(temp, search_ctx->ffsc_fix_path + len);
4413 STRCAT(temp, search_ctx->ffsc_wc_path);
4414 vim_free(search_ctx->ffsc_wc_path);
4415 vim_free(wc_path);
4416 search_ctx->ffsc_wc_path = temp;
4417 }
4418#endif
4419 vim_free(buf);
4420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 sptr = ff_create_stack_element(ff_expand_buffer,
4423#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004424 search_ctx->ffsc_wc_path,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425#endif
4426 level, 0);
4427
4428 if (sptr == NULL)
4429 goto error_return;
4430
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004431 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004433 search_ctx->ffsc_file_to_search = vim_strsave(filename);
4434 if (search_ctx->ffsc_file_to_search == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 goto error_return;
4436
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004437 return search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439error_return:
4440 /*
4441 * We clear the search context now!
4442 * Even when the caller gave us a (perhaps valid) context we free it here,
4443 * as we might have already destroyed it.
4444 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004445 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 return NULL;
4447}
4448
4449#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4450/*
4451 * Get the stopdir string. Check that ';' is not escaped.
4452 */
4453 char_u *
4454vim_findfile_stopdir(buf)
4455 char_u *buf;
4456{
4457 char_u *r_ptr = buf;
4458
4459 while (*r_ptr != NUL && *r_ptr != ';')
4460 {
4461 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4462 {
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004463 /* Overwrite the escape char,
4464 * use STRLEN(r_ptr) to move the trailing '\0'. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004465 STRMOVE(r_ptr, r_ptr + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 r_ptr++;
4467 }
4468 r_ptr++;
4469 }
4470 if (*r_ptr == ';')
4471 {
4472 *r_ptr = 0;
4473 r_ptr++;
4474 }
4475 else if (*r_ptr == NUL)
4476 r_ptr = NULL;
4477 return r_ptr;
4478}
4479#endif
4480
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004481/*
4482 * Clean up the given search context. Can handle a NULL pointer.
4483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 void
4485vim_findfile_cleanup(ctx)
4486 void *ctx;
4487{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004488 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 return;
4490
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 vim_findfile_free_visited(ctx);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004492 ff_clear(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 vim_free(ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494}
4495
4496/*
4497 * Find a file in a search context.
4498 * The search context was created with vim_findfile_init() above.
4499 * Return a pointer to an allocated file name or NULL if nothing found.
4500 * To get all matching files call this function until you get NULL.
4501 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004502 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 *
4504 * The search algorithm is depth first. To change this replace the
4505 * stack with a list (don't forget to leave partly searched directories on the
4506 * top of the list).
4507 */
4508 char_u *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004509vim_findfile(search_ctx_arg)
4510 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
4512 char_u *file_path;
4513#ifdef FEAT_PATH_EXTRA
4514 char_u *rest_of_wildcards;
4515 char_u *path_end = NULL;
4516#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004517 ff_stack_T *stackp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4519 int len;
4520#endif
4521 int i;
4522 char_u *p;
4523#ifdef FEAT_SEARCHPATH
4524 char_u *suf;
4525#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004526 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004528 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 return NULL;
4530
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004531 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 /*
4534 * filepath is used as buffer for various actions and as the storage to
4535 * return a found filename.
4536 */
4537 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4538 return NULL;
4539
4540#ifdef FEAT_PATH_EXTRA
4541 /* store the end of the start dir -- needed for upward search */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004542 if (search_ctx->ffsc_start_dir != NULL)
4543 path_end = &search_ctx->ffsc_start_dir[
4544 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#endif
4546
4547#ifdef FEAT_PATH_EXTRA
4548 /* upward search loop */
4549 for (;;)
4550 {
4551#endif
4552 /* downward search loop */
4553 for (;;)
4554 {
4555 /* check if user user wants to stop the search*/
4556 ui_breakcheck();
4557 if (got_int)
4558 break;
4559
4560 /* get directory to work on from stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004561 stackp = ff_pop(search_ctx);
4562 if (stackp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 break;
4564
4565 /*
4566 * TODO: decide if we leave this test in
4567 *
4568 * GOOD: don't search a directory(-tree) twice.
4569 * BAD: - check linked list for every new directory entered.
4570 * - check for double files also done below
4571 *
4572 * Here we check if we already searched this directory.
4573 * We already searched a directory if:
4574 * 1) The directory is the same.
4575 * 2) We would use the same wildcard string.
4576 *
4577 * Good if you have links on same directory via several ways
4578 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4579 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4580 *
4581 * This check is only needed for directories we work on for the
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004582 * first time (hence stackp->ff_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004584 if (stackp->ffs_filearray == NULL
4585 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 ->ffvl_visited_list,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004587 stackp->ffs_fix_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004589 , stackp->ffs_wc_path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590#endif
4591 ) == FAIL)
4592 {
4593#ifdef FF_VERBOSE
4594 if (p_verbose >= 5)
4595 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004596 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 smsg((char_u *)"Already Searched: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004598 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 /* don't overwrite this either */
4600 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004601 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004604 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 continue;
4606 }
4607#ifdef FF_VERBOSE
4608 else if (p_verbose >= 5)
4609 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004610 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004611 smsg((char_u *)"Searching: %s (%s)",
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004612 stackp->ffs_fix_path, stackp->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /* don't overwrite this either */
4614 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004615 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617#endif
4618
4619 /* check depth */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004620 if (stackp->ffs_level <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004622 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 continue;
4624 }
4625
4626 file_path[0] = NUL;
4627
4628 /*
4629 * If no filearray till now expand wildcards
4630 * The function expand_wildcards() can handle an array of paths
4631 * and all possible expands are returned in one array. We use this
4632 * to handle the expansion of '**' into an empty string.
4633 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004634 if (stackp->ffs_filearray == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
4636 char_u *dirptrs[2];
4637
4638 /* we use filepath to build the path expand_wildcards() should
4639 * expand.
4640 */
4641 dirptrs[0] = file_path;
4642 dirptrs[1] = NULL;
4643
4644 /* if we have a start dir copy it in */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004645 if (!vim_isAbsName(stackp->ffs_fix_path)
4646 && search_ctx->ffsc_start_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004648 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 add_pathsep(file_path);
4650 }
4651
4652 /* append the fix part of the search path */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004653 STRCAT(file_path, stackp->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 add_pathsep(file_path);
4655
4656#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004657 rest_of_wildcards = stackp->ffs_wc_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (*rest_of_wildcards != NUL)
4659 {
4660 len = (int)STRLEN(file_path);
4661 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4662 {
4663 /* pointer to the restrict byte
4664 * The restrict byte is not a character!
4665 */
4666 p = rest_of_wildcards + 2;
4667
4668 if (*p > 0)
4669 {
4670 (*p)--;
4671 file_path[len++] = '*';
4672 }
4673
4674 if (*p == 0)
4675 {
4676 /* remove '**<numb> from wildcards */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004677 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 else
4680 rest_of_wildcards += 3;
4681
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004682 if (stackp->ffs_star_star_empty == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
4684 /* if not done before, expand '**' to empty */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004685 stackp->ffs_star_star_empty = 1;
4686 dirptrs[1] = stackp->ffs_fix_path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688 }
4689
4690 /*
4691 * Here we copy until the next path separator or the end of
4692 * the path. If we stop at a path separator, there is
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00004693 * still something else left. This is handled below by
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 * pushing every directory returned from expand_wildcards()
4695 * on the stack again for further search.
4696 */
4697 while (*rest_of_wildcards
4698 && !vim_ispathsep(*rest_of_wildcards))
4699 file_path[len++] = *rest_of_wildcards++;
4700
4701 file_path[len] = NUL;
4702 if (vim_ispathsep(*rest_of_wildcards))
4703 rest_of_wildcards++;
4704 }
4705#endif
4706
4707 /*
4708 * Expand wildcards like "*" and "$VAR".
4709 * If the path is a URL don't try this.
4710 */
4711 if (path_with_url(dirptrs[0]))
4712 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004713 stackp->ffs_filearray = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 alloc((unsigned)sizeof(char *));
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004715 if (stackp->ffs_filearray != NULL
4716 && (stackp->ffs_filearray[0]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 = vim_strsave(dirptrs[0])) != NULL)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004718 stackp->ffs_filearray_size = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004720 stackp->ffs_filearray_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722 else
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004723 /* Add EW_NOTWILD because the expanded path may contain
4724 * wildcard characters that are to be taken literally.
4725 * This is a bit of a hack. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004727 &stackp->ffs_filearray_size,
4728 &stackp->ffs_filearray,
Bram Moolenaar0b573a52011-07-27 17:31:47 +02004729 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004731 stackp->ffs_filearray_cur = 0;
4732 stackp->ffs_stage = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734#ifdef FEAT_PATH_EXTRA
4735 else
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004736 rest_of_wildcards = &stackp->ffs_wc_path[
4737 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738#endif
4739
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004740 if (stackp->ffs_stage == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
4742 /* this is the first time we work on this directory */
4743#ifdef FEAT_PATH_EXTRA
4744 if (*rest_of_wildcards == NUL)
4745#endif
4746 {
4747 /*
Bram Moolenaar473de612013-06-08 18:19:48 +02004748 * We don't have further wildcards to expand, so we have to
4749 * check for the final file now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004751 for (i = stackp->ffs_filearray_cur;
4752 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004754 if (!path_with_url(stackp->ffs_filearray[i])
4755 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 continue; /* not a directory */
4757
Bram Moolenaar21160b92009-11-11 15:56:10 +00004758 /* prepare the filename to be checked for existence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 * below */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004760 STRCPY(file_path, stackp->ffs_filearray[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004762 STRCAT(file_path, search_ctx->ffsc_file_to_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 /*
4765 * Try without extra suffix and then with suffixes
4766 * from 'suffixesadd'.
4767 */
4768#ifdef FEAT_SEARCHPATH
4769 len = (int)STRLEN(file_path);
Bram Moolenaar463ee342010-08-08 18:17:52 +02004770 if (search_ctx->ffsc_tagfile)
4771 suf = (char_u *)"";
4772 else
4773 suf = curbuf->b_p_sua;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 for (;;)
4775#endif
4776 {
4777 /* if file exists and we didn't already find it */
4778 if ((path_with_url(file_path)
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004779 || (mch_getperm(file_path) >= 0
4780 && (search_ctx->ffsc_find_what
4781 == FINDFILE_BOTH
4782 || ((search_ctx->ffsc_find_what
4783 == FINDFILE_DIR)
4784 == mch_isdir(file_path)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785#ifndef FF_VERBOSE
4786 && (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004787 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 file_path
4789#ifdef FEAT_PATH_EXTRA
4790 , (char_u *)""
4791#endif
4792 ) == OK)
4793#endif
4794 )
4795 {
4796#ifdef FF_VERBOSE
4797 if (ff_check_visited(
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004798 &search_ctx->ffsc_visited_list->ffvl_visited_list,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 file_path
4800#ifdef FEAT_PATH_EXTRA
4801 , (char_u *)""
4802#endif
4803 ) == FAIL)
4804 {
4805 if (p_verbose >= 5)
4806 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004807 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004808 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 file_path);
4810 /* don't overwrite this either */
4811 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004812 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
4814 continue;
4815 }
4816#endif
4817
4818 /* push dir to examine rest of subdirs later */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004819 stackp->ffs_filearray_cur = i + 1;
4820 ff_push(search_ctx, stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
Bram Moolenaar6bab9fa2009-01-22 20:32:12 +00004822 if (!path_with_url(file_path))
4823 simplify_filename(file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4825 == OK)
4826 {
4827 p = shorten_fname(file_path,
4828 ff_expand_buffer);
4829 if (p != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004830 STRMOVE(file_path, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832#ifdef FF_VERBOSE
4833 if (p_verbose >= 5)
4834 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004835 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004836 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 /* don't overwrite this either */
4838 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004839 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841#endif
4842 return file_path;
4843 }
4844
4845#ifdef FEAT_SEARCHPATH
4846 /* Not found or found already, try next suffix. */
4847 if (*suf == NUL)
4848 break;
4849 copy_option_part(&suf, file_path + len,
4850 MAXPATHL - len, ",");
4851#endif
4852 }
4853 }
4854 }
4855#ifdef FEAT_PATH_EXTRA
4856 else
4857 {
4858 /*
4859 * still wildcards left, push the directories for further
4860 * search
4861 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004862 for (i = stackp->ffs_filearray_cur;
4863 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004865 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 continue; /* not a directory */
4867
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004868 ff_push(search_ctx,
4869 ff_create_stack_element(
4870 stackp->ffs_filearray[i],
4871 rest_of_wildcards,
4872 stackp->ffs_level - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 }
4875#endif
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004876 stackp->ffs_filearray_cur = 0;
4877 stackp->ffs_stage = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
4880#ifdef FEAT_PATH_EXTRA
4881 /*
4882 * if wildcards contains '**' we have to descent till we reach the
4883 * leaves of the directory tree.
4884 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004885 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004887 for (i = stackp->ffs_filearray_cur;
4888 i < stackp->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004890 if (fnamecmp(stackp->ffs_filearray[i],
4891 stackp->ffs_fix_path) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 continue; /* don't repush same directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004893 if (!mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 continue; /* not a directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004895 ff_push(search_ctx,
4896 ff_create_stack_element(stackp->ffs_filearray[i],
4897 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 }
4900#endif
4901
4902 /* we are done with the current directory */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004903 ff_free_stack_element(stackp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904
4905 }
4906
4907#ifdef FEAT_PATH_EXTRA
4908 /* If we reached this, we didn't find anything downwards.
4909 * Let's check if we should do an upward search.
4910 */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004911 if (search_ctx->ffsc_start_dir
4912 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
4914 ff_stack_T *sptr;
4915
4916 /* is the last starting directory in the stop list? */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004917 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
4918 (int)(path_end - search_ctx->ffsc_start_dir),
4919 search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 break;
4921
4922 /* cut of last dir */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004923 while (path_end > search_ctx->ffsc_start_dir
4924 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 path_end--;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004926 while (path_end > search_ctx->ffsc_start_dir
4927 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 path_end--;
4929 *path_end = 0;
4930 path_end--;
4931
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004932 if (*search_ctx->ffsc_start_dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 break;
4934
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004935 STRCPY(file_path, search_ctx->ffsc_start_dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 add_pathsep(file_path);
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004937 STRCAT(file_path, search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 /* create a new stack entry */
4940 sptr = ff_create_stack_element(file_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004941 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (sptr == NULL)
4943 break;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004944 ff_push(search_ctx, sptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 else
4947 break;
4948 }
4949#endif
4950
4951 vim_free(file_path);
4952 return NULL;
4953}
4954
4955/*
4956 * Free the list of lists of visited files and directories
4957 * Can handle it if the passed search_context is NULL;
4958 */
4959 void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004960vim_findfile_free_visited(search_ctx_arg)
4961 void *search_ctx_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004963 ff_search_ctx_T *search_ctx;
4964
4965 if (search_ctx_arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 return;
4967
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00004968 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
4969 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
4970 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971}
4972
4973 static void
4974vim_findfile_free_visited_list(list_headp)
4975 ff_visited_list_hdr_T **list_headp;
4976{
4977 ff_visited_list_hdr_T *vp;
4978
4979 while (*list_headp != NULL)
4980 {
4981 vp = (*list_headp)->ffvl_next;
4982 ff_free_visited_list((*list_headp)->ffvl_visited_list);
4983
4984 vim_free((*list_headp)->ffvl_filename);
4985 vim_free(*list_headp);
4986 *list_headp = vp;
4987 }
4988 *list_headp = NULL;
4989}
4990
4991 static void
4992ff_free_visited_list(vl)
4993 ff_visited_T *vl;
4994{
4995 ff_visited_T *vp;
4996
4997 while (vl != NULL)
4998 {
4999 vp = vl->ffv_next;
5000#ifdef FEAT_PATH_EXTRA
5001 vim_free(vl->ffv_wc_path);
5002#endif
5003 vim_free(vl);
5004 vl = vp;
5005 }
5006 vl = NULL;
5007}
5008
5009/*
5010 * Returns the already visited list for the given filename. If none is found it
5011 * allocates a new one.
5012 */
5013 static ff_visited_list_hdr_T*
5014ff_get_visited_list(filename, list_headp)
5015 char_u *filename;
5016 ff_visited_list_hdr_T **list_headp;
5017{
5018 ff_visited_list_hdr_T *retptr = NULL;
5019
5020 /* check if a visited list for the given filename exists */
5021 if (*list_headp != NULL)
5022 {
5023 retptr = *list_headp;
5024 while (retptr != NULL)
5025 {
5026 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
5027 {
5028#ifdef FF_VERBOSE
5029 if (p_verbose >= 5)
5030 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005031 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005032 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 filename);
5034 /* don't overwrite this either */
5035 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005036 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038#endif
5039 return retptr;
5040 }
5041 retptr = retptr->ffvl_next;
5042 }
5043 }
5044
5045#ifdef FF_VERBOSE
5046 if (p_verbose >= 5)
5047 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005048 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00005049 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /* don't overwrite this either */
5051 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00005052 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054#endif
5055
5056 /*
5057 * if we reach this we didn't find a list and we have to allocate new list
5058 */
5059 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
5060 if (retptr == NULL)
5061 return NULL;
5062
5063 retptr->ffvl_visited_list = NULL;
5064 retptr->ffvl_filename = vim_strsave(filename);
5065 if (retptr->ffvl_filename == NULL)
5066 {
5067 vim_free(retptr);
5068 return NULL;
5069 }
5070 retptr->ffvl_next = *list_headp;
5071 *list_headp = retptr;
5072
5073 return retptr;
5074}
5075
5076#ifdef FEAT_PATH_EXTRA
5077/*
5078 * check if two wildcard paths are equal. Returns TRUE or FALSE.
5079 * They are equal if:
5080 * - both paths are NULL
5081 * - they have the same length
5082 * - char by char comparison is OK
5083 * - the only differences are in the counters behind a '**', so
5084 * '**\20' is equal to '**\24'
5085 */
5086 static int
5087ff_wc_equal(s1, s2)
5088 char_u *s1;
5089 char_u *s2;
5090{
5091 int i;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005092 int prev1 = NUL;
5093 int prev2 = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 if (s1 == s2)
5096 return TRUE;
5097
5098 if (s1 == NULL || s2 == NULL)
5099 return FALSE;
5100
5101 if (STRLEN(s1) != STRLEN(s2))
5102 return FAIL;
5103
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005104 for (i = 0; s1[i] != NUL && s2[i] != NUL; i += MB_PTR2LEN(s1 + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005106 int c1 = PTR2CHAR(s1 + i);
5107 int c2 = PTR2CHAR(s2 + i);
5108
5109 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
5110 && (prev1 != '*' || prev2 != '*'))
5111 return FAIL;
5112 prev2 = prev1;
5113 prev1 = c1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 return TRUE;
5116}
5117#endif
5118
5119/*
5120 * maintains the list of already visited files and dirs
5121 * returns FAIL if the given file/dir is already in the list
5122 * returns OK if it is newly added
5123 *
5124 * TODO: What to do on memory allocation problems?
5125 * -> return TRUE - Better the file is found several times instead of
5126 * never.
5127 */
5128 static int
5129ff_check_visited(visited_list, fname
5130#ifdef FEAT_PATH_EXTRA
5131 , wc_path
5132#endif
5133 )
5134 ff_visited_T **visited_list;
5135 char_u *fname;
5136#ifdef FEAT_PATH_EXTRA
5137 char_u *wc_path;
5138#endif
5139{
5140 ff_visited_T *vp;
5141#ifdef UNIX
5142 struct stat st;
5143 int url = FALSE;
5144#endif
5145
5146 /* For an URL we only compare the name, otherwise we compare the
5147 * device/inode (unix) or the full path name (not Unix). */
5148 if (path_with_url(fname))
5149 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005150 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151#ifdef UNIX
5152 url = TRUE;
5153#endif
5154 }
5155 else
5156 {
5157 ff_expand_buffer[0] = NUL;
5158#ifdef UNIX
5159 if (mch_stat((char *)fname, &st) < 0)
5160#else
5161 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
5162#endif
5163 return FAIL;
5164 }
5165
5166 /* check against list of already visited files */
5167 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
5168 {
5169 if (
5170#ifdef UNIX
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005171 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
5172 && vp->ffv_ino == st.st_ino)
5173 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#endif
5175 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
5176 )
5177 {
5178#ifdef FEAT_PATH_EXTRA
5179 /* are the wildcard parts equal */
5180 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
5181#endif
5182 /* already visited */
5183 return FAIL;
5184 }
5185 }
5186
5187 /*
5188 * New file/dir. Add it to the list of visited files/dirs.
5189 */
5190 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
5191 + STRLEN(ff_expand_buffer)));
5192
5193 if (vp != NULL)
5194 {
5195#ifdef UNIX
5196 if (!url)
5197 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005198 vp->ffv_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 vp->ffv_ino = st.st_ino;
5200 vp->ffv_dev = st.st_dev;
5201 vp->ffv_fname[0] = NUL;
5202 }
5203 else
5204 {
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00005205 vp->ffv_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206#endif
5207 STRCPY(vp->ffv_fname, ff_expand_buffer);
5208#ifdef UNIX
5209 }
5210#endif
5211#ifdef FEAT_PATH_EXTRA
5212 if (wc_path != NULL)
5213 vp->ffv_wc_path = vim_strsave(wc_path);
5214 else
5215 vp->ffv_wc_path = NULL;
5216#endif
5217
5218 vp->ffv_next = *visited_list;
5219 *visited_list = vp;
5220 }
5221
5222 return OK;
5223}
5224
5225/*
5226 * create stack element from given path pieces
5227 */
5228 static ff_stack_T *
5229ff_create_stack_element(fix_part,
5230#ifdef FEAT_PATH_EXTRA
5231 wc_part,
5232#endif
5233 level, star_star_empty)
5234 char_u *fix_part;
5235#ifdef FEAT_PATH_EXTRA
5236 char_u *wc_part;
5237#endif
5238 int level;
5239 int star_star_empty;
5240{
5241 ff_stack_T *new;
5242
5243 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
5244 if (new == NULL)
5245 return NULL;
5246
5247 new->ffs_prev = NULL;
5248 new->ffs_filearray = NULL;
5249 new->ffs_filearray_size = 0;
5250 new->ffs_filearray_cur = 0;
5251 new->ffs_stage = 0;
5252 new->ffs_level = level;
5253 new->ffs_star_star_empty = star_star_empty;;
5254
5255 /* the following saves NULL pointer checks in vim_findfile */
5256 if (fix_part == NULL)
5257 fix_part = (char_u *)"";
5258 new->ffs_fix_path = vim_strsave(fix_part);
5259
5260#ifdef FEAT_PATH_EXTRA
5261 if (wc_part == NULL)
5262 wc_part = (char_u *)"";
5263 new->ffs_wc_path = vim_strsave(wc_part);
5264#endif
5265
5266 if (new->ffs_fix_path == NULL
5267#ifdef FEAT_PATH_EXTRA
5268 || new->ffs_wc_path == NULL
5269#endif
5270 )
5271 {
5272 ff_free_stack_element(new);
5273 new = NULL;
5274 }
5275
5276 return new;
5277}
5278
5279/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005280 * Push a dir on the directory stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 */
5282 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005283ff_push(search_ctx, stack_ptr)
5284 ff_search_ctx_T *search_ctx;
5285 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
5287 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005288 * to prevent a crash */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005289 if (stack_ptr != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005291 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
5292 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294}
5295
5296/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005297 * Pop a dir from the directory stack.
5298 * Returns NULL if stack is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 */
5300 static ff_stack_T *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005301ff_pop(search_ctx)
5302 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
5304 ff_stack_T *sptr;
5305
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005306 sptr = search_ctx->ffsc_stack_ptr;
5307 if (search_ctx->ffsc_stack_ptr != NULL)
5308 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
5310 return sptr;
5311}
5312
5313/*
5314 * free the given stack element
5315 */
5316 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005317ff_free_stack_element(stack_ptr)
5318 ff_stack_T *stack_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 /* vim_free handles possible NULL pointers */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005321 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005323 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#endif
5325
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005326 if (stack_ptr->ffs_filearray != NULL)
5327 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005329 vim_free(stack_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330}
5331
5332/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005333 * Clear the search context, but NOT the visited list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 */
5335 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005336ff_clear(search_ctx)
5337 ff_search_ctx_T *search_ctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 ff_stack_T *sptr;
5340
5341 /* clear up stack */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005342 while ((sptr = ff_pop(search_ctx)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 ff_free_stack_element(sptr);
5344
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005345 vim_free(search_ctx->ffsc_file_to_search);
5346 vim_free(search_ctx->ffsc_start_dir);
5347 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005349 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350#endif
5351
5352#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005353 if (search_ctx->ffsc_stopdirs_v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 int i = 0;
5356
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005357 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005359 vim_free(search_ctx->ffsc_stopdirs_v[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 i++;
5361 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005362 vim_free(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005364 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365#endif
5366
5367 /* reset everything */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005368 search_ctx->ffsc_file_to_search = NULL;
5369 search_ctx->ffsc_start_dir = NULL;
5370 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef FEAT_PATH_EXTRA
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005372 search_ctx->ffsc_wc_path = NULL;
5373 search_ctx->ffsc_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374#endif
5375}
5376
5377#ifdef FEAT_PATH_EXTRA
5378/*
5379 * check if the given path is in the stopdirs
5380 * returns TRUE if yes else FALSE
5381 */
5382 static int
5383ff_path_in_stoplist(path, path_len, stopdirs_v)
5384 char_u *path;
5385 int path_len;
5386 char_u **stopdirs_v;
5387{
5388 int i = 0;
5389
5390 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005391 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 path_len--;
5393
5394 /* if no path consider it as match */
5395 if (path_len == 0)
5396 return TRUE;
5397
5398 for (i = 0; stopdirs_v[i] != NULL; i++)
5399 {
5400 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5401 {
5402 /* match for parent directory. So '/home' also matches
5403 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5404 * '/home/r' would also match '/home/rks'
5405 */
5406 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005407 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 return TRUE;
5409 }
5410 else
5411 {
5412 if (fnamecmp(stopdirs_v[i], path) == 0)
5413 return TRUE;
5414 }
5415 }
5416 return FALSE;
5417}
5418#endif
5419
5420#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5421/*
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005422 * Find the file name "ptr[len]" in the path. Also finds directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 *
5424 * On the first call set the parameter 'first' to TRUE to initialize
5425 * the search. For repeating calls to FALSE.
5426 *
5427 * Repeating calls will return other files called 'ptr[len]' from the path.
5428 *
5429 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5430 * don't need valid values.
5431 *
5432 * If nothing found on the first call the option FNAME_MESS will issue the
5433 * message:
5434 * 'Can't find file "<file>" in path'
5435 * On repeating calls:
5436 * 'No more file "<file>" found in path'
5437 *
5438 * options:
5439 * FNAME_MESS give error message when not found
5440 *
5441 * Uses NameBuff[]!
5442 *
5443 * Returns an allocated string for the file name. NULL for error.
5444 *
5445 */
5446 char_u *
5447find_file_in_path(ptr, len, options, first, rel_fname)
5448 char_u *ptr; /* file name */
5449 int len; /* length of file name */
5450 int options;
5451 int first; /* use count'th matching file name */
5452 char_u *rel_fname; /* file name searching relative to */
5453{
5454 return find_file_in_path_option(ptr, len, options, first,
5455 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005456 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457}
5458
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005459static char_u *ff_file_to_find = NULL;
5460static void *fdip_search_ctx = NULL;
5461
5462#if defined(EXITFREE)
5463 static void
5464free_findfile()
5465{
5466 vim_free(ff_file_to_find);
5467 vim_findfile_cleanup(fdip_search_ctx);
5468}
5469#endif
5470
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471/*
5472 * Find the directory name "ptr[len]" in the path.
5473 *
5474 * options:
5475 * FNAME_MESS give error message when not found
5476 *
5477 * Uses NameBuff[]!
5478 *
5479 * Returns an allocated string for the file name. NULL for error.
5480 */
5481 char_u *
5482find_directory_in_path(ptr, len, options, rel_fname)
5483 char_u *ptr; /* file name */
5484 int len; /* length of file name */
5485 int options;
5486 char_u *rel_fname; /* file name searching relative to */
5487{
5488 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005489 FINDFILE_DIR, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490}
5491
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005492 char_u *
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005493find_file_in_path_option(ptr, len, options, first, path_option, find_what, rel_fname, suffixes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 char_u *ptr; /* file name */
5495 int len; /* length of file name */
5496 int options;
5497 int first; /* use count'th matching file name */
5498 char_u *path_option; /* p_path or p_cdpath */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005499 int find_what; /* FINDFILE_FILE, _DIR or _BOTH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 char_u *rel_fname; /* file name we are looking relative to. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005501 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 static int did_findfile_init = FALSE;
5505 char_u save_char;
5506 char_u *file_name = NULL;
5507 char_u *buf = NULL;
5508 int rel_to_curdir;
5509#ifdef AMIGA
5510 struct Process *proc = (struct Process *)FindTask(0L);
5511 APTR save_winptr = proc->pr_WindowPtr;
5512
5513 /* Avoid a requester here for a volume that doesn't exist. */
5514 proc->pr_WindowPtr = (APTR)-1L;
5515#endif
5516
5517 if (first == TRUE)
5518 {
5519 /* copy file name into NameBuff, expanding environment variables */
5520 save_char = ptr[len];
5521 ptr[len] = NUL;
5522 expand_env(ptr, NameBuff, MAXPATHL);
5523 ptr[len] = save_char;
5524
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005525 vim_free(ff_file_to_find);
5526 ff_file_to_find = vim_strsave(NameBuff);
5527 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
5529 file_name = NULL;
5530 goto theend;
5531 }
5532 }
5533
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005534 rel_to_curdir = (ff_file_to_find[0] == '.'
5535 && (ff_file_to_find[1] == NUL
5536 || vim_ispathsep(ff_file_to_find[1])
5537 || (ff_file_to_find[1] == '.'
5538 && (ff_file_to_find[2] == NUL
5539 || vim_ispathsep(ff_file_to_find[2])))));
5540 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 /* "..", "../path", "." and "./path": don't use the path_option */
5542 || rel_to_curdir
5543#if defined(MSWIN) || defined(MSDOS) || defined(OS2)
5544 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005545 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar21160b92009-11-11 15:56:10 +00005546 /* handle "c:name" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005547 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548#endif
5549#ifdef AMIGA
5550 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005551 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552#endif
5553 )
5554 {
5555 /*
5556 * Absolute path, no need to use "path_option".
5557 * If this is not a first call, return NULL. We already returned a
5558 * filename on the first call.
5559 */
5560 if (first == TRUE)
5561 {
5562 int l;
5563 int run;
5564
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005565 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005567 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 goto theend;
5569 }
5570
5571 /* When FNAME_REL flag given first use the directory of the file.
5572 * Otherwise or when this fails use the current directory. */
5573 for (run = 1; run <= 2; ++run)
5574 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005575 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 if (run == 1
5577 && rel_to_curdir
5578 && (options & FNAME_REL)
5579 && rel_fname != NULL
5580 && STRLEN(rel_fname) + l < MAXPATHL)
5581 {
5582 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005583 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 l = (int)STRLEN(NameBuff);
5585 }
5586 else
5587 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005588 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 run = 2;
5590 }
5591
5592 /* When the file doesn't exist, try adding parts of
5593 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005594 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 for (;;)
5596 {
5597 if (
5598#ifdef DJGPP
5599 /* "C:" by itself will fail for mch_getperm(),
5600 * assume it's always valid. */
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005601 (find_what != FINDFILE_FILE && NameBuff[0] != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 && NameBuff[1] == ':'
5603 && NameBuff[2] == NUL) ||
5604#endif
5605 (mch_getperm(NameBuff) >= 0
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005606 && (find_what == FINDFILE_BOTH
5607 || ((find_what == FINDFILE_DIR)
5608 == mch_isdir(NameBuff)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 file_name = vim_strsave(NameBuff);
5611 goto theend;
5612 }
5613 if (*buf == NUL)
5614 break;
5615 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5616 }
5617 }
5618 }
5619 }
5620 else
5621 {
5622 /*
5623 * Loop over all paths in the 'path' or 'cdpath' option.
5624 * When "first" is set, first setup to the start of the option.
5625 * Otherwise continue to find the next match.
5626 */
5627 if (first == TRUE)
5628 {
5629 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005630 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 dir = path_option;
5632 did_findfile_init = FALSE;
5633 }
5634
5635 for (;;)
5636 {
5637 if (did_findfile_init)
5638 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005639 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (file_name != NULL)
5641 break;
5642
5643 did_findfile_init = FALSE;
5644 }
5645 else
5646 {
5647 char_u *r_ptr;
5648
5649 if (dir == NULL || *dir == NUL)
5650 {
5651 /* We searched all paths of the option, now we can
5652 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005653 vim_findfile_cleanup(fdip_search_ctx);
5654 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 break;
5656 }
5657
5658 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5659 break;
5660
5661 /* copy next path */
5662 buf[0] = 0;
5663 copy_option_part(&dir, buf, MAXPATHL, " ,");
5664
5665#ifdef FEAT_PATH_EXTRA
5666 /* get the stopdir string */
5667 r_ptr = vim_findfile_stopdir(buf);
5668#else
5669 r_ptr = NULL;
5670#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005671 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005672 r_ptr, 100, FALSE, find_what,
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005673 fdip_search_ctx, FALSE, rel_fname);
5674 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 did_findfile_init = TRUE;
5676 vim_free(buf);
5677 }
5678 }
5679 }
5680 if (file_name == NULL && (options & FNAME_MESS))
5681 {
5682 if (first == TRUE)
5683 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005684 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005686 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 else
5688 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005689 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else
5692 {
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00005693 if (find_what == FINDFILE_DIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005695 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 else
5697 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005698 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 }
5701
5702theend:
5703#ifdef AMIGA
5704 proc->pr_WindowPtr = save_winptr;
5705#endif
5706 return file_name;
5707}
5708
5709#endif /* FEAT_SEARCHPATH */
5710
5711/*
5712 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5713 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5714 */
5715 int
5716vim_chdir(new_dir)
5717 char_u *new_dir;
5718{
5719#ifndef FEAT_SEARCHPATH
5720 return mch_chdir((char *)new_dir);
5721#else
5722 char_u *dir_name;
5723 int r;
5724
5725 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5726 FNAME_MESS, curbuf->b_ffname);
5727 if (dir_name == NULL)
5728 return -1;
5729 r = mch_chdir((char *)dir_name);
5730 vim_free(dir_name);
5731 return r;
5732#endif
5733}
5734
5735/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005736 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005738 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5739 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 * Returns OK or FAIL.
5741 */
5742 int
5743get_user_name(buf, len)
5744 char_u *buf;
5745 int len;
5746{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005747 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 if (mch_get_user_name(buf, len) == FAIL)
5750 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005751 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
5753 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005754 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 return OK;
5756}
5757
5758#ifndef HAVE_QSORT
5759/*
5760 * Our own qsort(), for systems that don't have it.
5761 * It's simple and slow. From the K&R C book.
5762 */
5763 void
5764qsort(base, elm_count, elm_size, cmp)
5765 void *base;
5766 size_t elm_count;
5767 size_t elm_size;
5768 int (*cmp) __ARGS((const void *, const void *));
5769{
5770 char_u *buf;
5771 char_u *p1;
5772 char_u *p2;
5773 int i, j;
5774 int gap;
5775
5776 buf = alloc((unsigned)elm_size);
5777 if (buf == NULL)
5778 return;
5779
5780 for (gap = elm_count / 2; gap > 0; gap /= 2)
5781 for (i = gap; i < elm_count; ++i)
5782 for (j = i - gap; j >= 0; j -= gap)
5783 {
5784 /* Compare the elements. */
5785 p1 = (char_u *)base + j * elm_size;
5786 p2 = (char_u *)base + (j + gap) * elm_size;
5787 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5788 break;
Bram Moolenaar21160b92009-11-11 15:56:10 +00005789 /* Exchange the elements. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 mch_memmove(buf, p1, elm_size);
5791 mch_memmove(p1, p2, elm_size);
5792 mch_memmove(p2, buf, elm_size);
5793 }
5794
5795 vim_free(buf);
5796}
5797#endif
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799/*
5800 * Sort an array of strings.
5801 */
5802static int
5803#ifdef __BORLANDC__
5804_RTLENTRYF
5805#endif
5806sort_compare __ARGS((const void *s1, const void *s2));
5807
5808 static int
5809#ifdef __BORLANDC__
5810_RTLENTRYF
5811#endif
5812sort_compare(s1, s2)
5813 const void *s1;
5814 const void *s2;
5815{
5816 return STRCMP(*(char **)s1, *(char **)s2);
5817}
5818
5819 void
5820sort_strings(files, count)
5821 char_u **files;
5822 int count;
5823{
5824 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827#if !defined(NO_EXPANDPATH) || defined(PROTO)
5828/*
5829 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005830 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 * Return value like strcmp(p, q), but consider path separators.
5832 */
5833 int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005834pathcmp(p, q, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 const char *p, *q;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005836 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838 int i;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005839 int c1, c2;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005840 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005842 for (i = 0; maxlen < 0 || i < maxlen; i += MB_PTR2LEN((char_u *)p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005844 c1 = PTR2CHAR((char_u *)p + i);
5845 c2 = PTR2CHAR((char_u *)q + i);
5846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 /* End of "p": check if "q" also ends or just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005848 if (c1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005850 if (c2 == NUL) /* full match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 return 0;
5852 s = q;
5853 break;
5854 }
5855
5856 /* End of "q": check if "p" just has a slash. */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005857 if (c2 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {
5859 s = p;
5860 break;
5861 }
5862
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005863 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#ifdef BACKSLASH_IN_FILENAME
5865 /* consider '/' and '\\' to be equal */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005866 && !((c1 == '/' && c2 == '\\')
5867 || (c1 == '\\' && c2 == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868#endif
5869 )
5870 {
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005871 if (vim_ispathsep(c1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 return -1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005873 if (vim_ispathsep(c2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 return 1;
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005875 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
5876 : c1 - c2; /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 }
5878 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005879 if (s == NULL) /* "i" ran into "maxlen" */
5880 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005882 c1 = PTR2CHAR((char_u *)s + i);
5883 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005885 if (c2 == NUL
Bram Moolenaar86b68352004-12-27 21:59:20 +00005886 && i > 0
5887 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005889 && (c1 == '/' || c1 == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#else
Bram Moolenaar38ec50b2013-04-12 14:42:39 +02005891 && c1 == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005893 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 return 0; /* match with trailing slash */
5895 if (s == q)
5896 return -1; /* no match */
5897 return 1;
5898}
5899#endif
5900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901/*
5902 * The putenv() implementation below comes from the "screen" program.
5903 * Included with permission from Juergen Weigert.
5904 * See pty.c for the copyright notice.
5905 */
5906
5907/*
5908 * putenv -- put value into environment
5909 *
5910 * Usage: i = putenv (string)
5911 * int i;
5912 * char *string;
5913 *
5914 * where string is of the form <name>=<value>.
5915 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5916 *
5917 * Putenv may need to add a new name into the environment, or to
5918 * associate a value longer than the current value with a particular
5919 * name. So, to make life simpler, putenv() copies your entire
5920 * environment into the heap (i.e. malloc()) from the stack
5921 * (i.e. where it resides when your process is initiated) the first
5922 * time you call it.
5923 *
5924 * (history removed, not very interesting. See the "screen" sources.)
5925 */
5926
5927#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5928
5929#define EXTRASIZE 5 /* increment to add to env. size */
5930
5931static int envsize = -1; /* current size of environment */
5932#ifndef MACOS_CLASSIC
5933extern
5934#endif
5935 char **environ; /* the global which is your env. */
5936
5937static int findenv __ARGS((char *name)); /* look for a name in the env. */
5938static int newenv __ARGS((void)); /* copy env. from stack to heap */
5939static int moreenv __ARGS((void)); /* incr. size of env. */
5940
5941 int
5942putenv(string)
5943 const char *string;
5944{
5945 int i;
5946 char *p;
5947
5948 if (envsize < 0)
5949 { /* first time putenv called */
5950 if (newenv() < 0) /* copy env. to heap */
5951 return -1;
5952 }
5953
5954 i = findenv((char *)string); /* look for name in environment */
5955
5956 if (i < 0)
5957 { /* name must be added */
5958 for (i = 0; environ[i]; i++);
5959 if (i >= (envsize - 1))
5960 { /* need new slot */
5961 if (moreenv() < 0)
5962 return -1;
5963 }
5964 p = (char *)alloc((unsigned)(strlen(string) + 1));
5965 if (p == NULL) /* not enough core */
5966 return -1;
5967 environ[i + 1] = 0; /* new end of env. */
5968 }
5969 else
5970 { /* name already in env. */
5971 p = vim_realloc(environ[i], strlen(string) + 1);
5972 if (p == NULL)
5973 return -1;
5974 }
5975 sprintf(p, "%s", string); /* copy into env. */
5976 environ[i] = p;
5977
5978 return 0;
5979}
5980
5981 static int
5982findenv(name)
5983 char *name;
5984{
5985 char *namechar, *envchar;
5986 int i, found;
5987
5988 found = 0;
5989 for (i = 0; environ[i] && !found; i++)
5990 {
5991 envchar = environ[i];
5992 namechar = name;
5993 while (*namechar && *namechar != '=' && (*namechar == *envchar))
5994 {
5995 namechar++;
5996 envchar++;
5997 }
5998 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
5999 }
6000 return found ? i - 1 : -1;
6001}
6002
6003 static int
6004newenv()
6005{
6006 char **env, *elem;
6007 int i, esize;
6008
6009#ifdef MACOS
6010 /* for Mac a new, empty environment is created */
6011 i = 0;
6012#else
6013 for (i = 0; environ[i]; i++)
6014 ;
6015#endif
6016 esize = i + EXTRASIZE + 1;
6017 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
6018 if (env == NULL)
6019 return -1;
6020
6021#ifndef MACOS
6022 for (i = 0; environ[i]; i++)
6023 {
6024 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
6025 if (elem == NULL)
6026 return -1;
6027 env[i] = elem;
6028 strcpy(elem, environ[i]);
6029 }
6030#endif
6031
6032 env[i] = 0;
6033 environ = env;
6034 envsize = esize;
6035 return 0;
6036}
6037
6038 static int
6039moreenv()
6040{
6041 int esize;
6042 char **env;
6043
6044 esize = envsize + EXTRASIZE;
6045 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
6046 if (env == 0)
6047 return -1;
6048 environ = env;
6049 envsize = esize;
6050 return 0;
6051}
6052
6053# ifdef USE_VIMPTY_GETENV
6054 char_u *
6055vimpty_getenv(string)
6056 const char_u *string;
6057{
6058 int i;
6059 char_u *p;
6060
6061 if (envsize < 0)
6062 return NULL;
6063
6064 i = findenv((char *)string);
6065
6066 if (i < 0)
6067 return NULL;
6068
6069 p = vim_strchr((char_u *)environ[i], '=');
6070 return (p + 1);
6071}
6072# endif
6073
6074#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006075
Bram Moolenaarc4956c82006-03-12 21:58:43 +00006076#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00006077/*
6078 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
6079 * rights to write into.
6080 */
6081 int
6082filewritable(fname)
6083 char_u *fname;
6084{
6085 int retval = 0;
6086#if defined(UNIX) || defined(VMS)
6087 int perm = 0;
6088#endif
6089
6090#if defined(UNIX) || defined(VMS)
6091 perm = mch_getperm(fname);
6092#endif
6093#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6094 if (
6095# ifdef WIN3264
6096 mch_writable(fname) &&
6097# else
6098# if defined(UNIX) || defined(VMS)
6099 (perm & 0222) &&
6100# endif
6101# endif
6102 mch_access((char *)fname, W_OK) == 0
6103 )
6104#endif
6105 {
6106 ++retval;
6107 if (mch_isdir(fname))
6108 ++retval;
6109 }
6110 return retval;
6111}
6112#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006113
6114/*
6115 * Print an error message with one or two "%s" and one or two string arguments.
6116 * This is not in message.c to avoid a warning for prototypes.
6117 */
6118 int
6119emsg3(s, a1, a2)
6120 char_u *s, *a1, *a2;
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 */
Bram Moolenaar6f192452007-11-08 19:49:02 +00006124#ifdef HAVE_STDARG_H
6125 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, a1, a2);
6126#else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006127 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2);
Bram Moolenaar6f192452007-11-08 19:49:02 +00006128#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006129 return emsg(IObuff);
6130}
6131
6132/*
6133 * Print an error message with one "%ld" and one long int argument.
6134 * This is not in message.c to avoid a warning for prototypes.
6135 */
6136 int
6137emsgn(s, n)
6138 char_u *s;
6139 long n;
6140{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006141 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00006142 return TRUE; /* no error messages at the moment */
6143 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
6144 return emsg(IObuff);
6145}
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006146
6147#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
6148/*
6149 * Read 2 bytes from "fd" and turn them into an int, MSB first.
6150 */
6151 int
6152get2c(fd)
6153 FILE *fd;
6154{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006155 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006156
6157 n = getc(fd);
6158 n = (n << 8) + getc(fd);
6159 return n;
6160}
6161
6162/*
6163 * Read 3 bytes from "fd" and turn them into an int, MSB first.
6164 */
6165 int
6166get3c(fd)
6167 FILE *fd;
6168{
Bram Moolenaar9db58062010-05-29 20:33:07 +02006169 int n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006170
6171 n = getc(fd);
6172 n = (n << 8) + getc(fd);
6173 n = (n << 8) + getc(fd);
6174 return n;
6175}
6176
6177/*
6178 * Read 4 bytes from "fd" and turn them into an int, MSB first.
6179 */
6180 int
6181get4c(fd)
6182 FILE *fd;
6183{
Bram Moolenaar95235e62013-09-08 16:07:07 +02006184 /* Use unsigned rather than int otherwise result is undefined
6185 * when left-shift sets the MSB. */
6186 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006187
Bram Moolenaar95235e62013-09-08 16:07:07 +02006188 n = (unsigned)getc(fd);
6189 n = (n << 8) + (unsigned)getc(fd);
6190 n = (n << 8) + (unsigned)getc(fd);
6191 n = (n << 8) + (unsigned)getc(fd);
6192 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006193}
6194
6195/*
6196 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
6197 */
6198 time_t
6199get8ctime(fd)
6200 FILE *fd;
6201{
6202 time_t n = 0;
6203 int i;
6204
6205 for (i = 0; i < 8; ++i)
6206 n = (n << 8) + getc(fd);
6207 return n;
6208}
6209
6210/*
6211 * Read a string of length "cnt" from "fd" into allocated memory.
6212 * Returns NULL when out of memory or unable to read that many bytes.
6213 */
6214 char_u *
6215read_string(fd, cnt)
6216 FILE *fd;
6217 int cnt;
6218{
6219 char_u *str;
6220 int i;
6221 int c;
6222
6223 /* allocate memory */
6224 str = alloc((unsigned)cnt + 1);
6225 if (str != NULL)
6226 {
6227 /* Read the string. Quit when running into the EOF. */
6228 for (i = 0; i < cnt; ++i)
6229 {
6230 c = getc(fd);
6231 if (c == EOF)
6232 {
6233 vim_free(str);
6234 return NULL;
6235 }
6236 str[i] = c;
6237 }
6238 str[i] = NUL;
6239 }
6240 return str;
6241}
6242
6243/*
6244 * Write a number to file "fd", MSB first, in "len" bytes.
6245 */
6246 int
6247put_bytes(fd, nr, len)
6248 FILE *fd;
6249 long_u nr;
6250 int len;
6251{
6252 int i;
6253
6254 for (i = len - 1; i >= 0; --i)
6255 if (putc((int)(nr >> (i * 8)), fd) == EOF)
6256 return FAIL;
6257 return OK;
6258}
6259
6260#ifdef _MSC_VER
6261# if (_MSC_VER <= 1200)
6262/* This line is required for VC6 without the service pack. Also see the
6263 * matching #pragma below. */
6264 # pragma optimize("", off)
6265# endif
6266#endif
6267
6268/*
6269 * Write time_t to file "fd" in 8 bytes.
6270 */
6271 void
6272put_time(fd, the_time)
6273 FILE *fd;
6274 time_t the_time;
6275{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006276 char_u buf[8];
6277
6278 time_to_bytes(the_time, buf);
6279 fwrite(buf, (size_t)8, (size_t)1, fd);
6280}
6281
6282/*
6283 * Write time_t to "buf[8]".
6284 */
6285 void
6286time_to_bytes(the_time, buf)
6287 time_t the_time;
6288 char_u *buf;
6289{
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006290 int c;
6291 int i;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006292 int bi = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006293 time_t wtime = the_time;
6294
6295 /* time_t can be up to 8 bytes in size, more than long_u, thus we
6296 * can't use put_bytes() here.
6297 * Another problem is that ">>" may do an arithmetic shift that keeps the
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006298 * sign. This happens for large values of wtime. A cast to long_u may
6299 * truncate if time_t is 8 bytes. So only use a cast when it is 4 bytes,
6300 * it's safe to assume that long_u is 4 bytes or more and when using 8
6301 * bytes the top bit won't be set. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006302 for (i = 7; i >= 0; --i)
6303 {
6304 if (i + 1 > (int)sizeof(time_t))
6305 /* ">>" doesn't work well when shifting more bits than avail */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006306 buf[bi++] = 0;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006307 else
6308 {
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006309#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006310 c = (int)(wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006311#else
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02006312 c = (int)((long_u)wtime >> (i * 8));
Bram Moolenaar644fdff2010-05-30 13:26:21 +02006313#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02006314 buf[bi++] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02006315 }
6316 }
6317}
6318
6319#ifdef _MSC_VER
6320# if (_MSC_VER <= 1200)
6321 # pragma optimize("", on)
6322# endif
6323#endif
6324
6325#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006326
6327#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
6328 || defined(FEAT_SPELL) || defined(PROTO)
6329/*
6330 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6331 * When "s" is NULL FALSE is returned.
6332 */
6333 int
6334has_non_ascii(s)
6335 char_u *s;
6336{
6337 char_u *p;
6338
6339 if (s != NULL)
6340 for (p = s; *p != NUL; ++p)
6341 if (*p >= 128)
6342 return TRUE;
6343 return FALSE;
6344}
6345#endif