blob: 5d804270614c532aa89cdc45e2fdabcd05beacfb [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
15#ifdef HAVE_FCNTL_H
16# include <fcntl.h> /* for chdir() */
17#endif
18
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000019static char_u *username = NULL; /* cached result of mch_get_user_name() */
20
21static char_u *ff_expand_buffer = NULL; /* used for expanding filenames */
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#if defined(FEAT_VIRTUALEDIT) || defined(PROTO)
24static int coladvance2 __ARGS((pos_T *pos, int addspaces, int finetune, colnr_T wcol));
25
26/*
27 * Return TRUE if in the current mode we need to use virtual.
28 */
29 int
30virtual_active()
31{
32 /* While an operator is being executed we return "virtual_op", because
33 * VIsual_active has already been reset, thus we can't check for "block"
34 * being used. */
35 if (virtual_op != MAYBE)
36 return virtual_op;
37 return (ve_flags == VE_ALL
38# ifdef FEAT_VISUAL
39 || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
40# endif
41 || ((ve_flags & VE_INSERT) && (State & INSERT)));
42}
43
44/*
45 * Get the screen position of the cursor.
46 */
47 int
48getviscol()
49{
50 colnr_T x;
51
52 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
53 return (int)x;
54}
55
56/*
57 * Get the screen position of character col with a coladd in the cursor line.
58 */
59 int
60getviscol2(col, coladd)
61 colnr_T col;
62 colnr_T coladd;
63{
64 colnr_T x;
65 pos_T pos;
66
67 pos.lnum = curwin->w_cursor.lnum;
68 pos.col = col;
69 pos.coladd = coladd;
70 getvvcol(curwin, &pos, &x, NULL, NULL);
71 return (int)x;
72}
73
74/*
75 * Go to column "wcol", and add/insert white space as neccessary to get the
76 * cursor in that column.
77 * The caller must have saved the cursor line for undo!
78 */
79 int
80coladvance_force(wcol)
81 colnr_T wcol;
82{
83 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
84
85 if (wcol == MAXCOL)
86 curwin->w_valid &= ~VALID_VIRTCOL;
87 else
88 {
89 /* Virtcol is valid */
90 curwin->w_valid |= VALID_VIRTCOL;
91 curwin->w_virtcol = wcol;
92 }
93 return rc;
94}
95#endif
96
97/*
98 * Try to advance the Cursor to the specified screen column.
99 * If virtual editing: fine tune the cursor position.
100 * Note that all virtual positions off the end of a line should share
101 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
102 * beginning at coladd 0.
103 *
104 * return OK if desired column is reached, FAIL if not
105 */
106 int
107coladvance(wcol)
108 colnr_T wcol;
109{
110 int rc = getvpos(&curwin->w_cursor, wcol);
111
112 if (wcol == MAXCOL || rc == FAIL)
113 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000114 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 {
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000116 /* Virtcol is valid when not on a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 curwin->w_valid |= VALID_VIRTCOL;
118 curwin->w_virtcol = wcol;
119 }
120 return rc;
121}
122
123/*
124 * Return in "pos" the position of the cursor advanced to screen column "wcol".
125 * return OK if desired column is reached, FAIL if not
126 */
127 int
128getvpos(pos, wcol)
129 pos_T *pos;
130 colnr_T wcol;
131{
132#ifdef FEAT_VIRTUALEDIT
133 return coladvance2(pos, FALSE, virtual_active(), wcol);
134}
135
136 static int
137coladvance2(pos, addspaces, finetune, wcol)
138 pos_T *pos;
139 int addspaces; /* change the text to achieve our goal? */
140 int finetune; /* change char offset for the excact column */
141 colnr_T wcol; /* column to move to */
142{
143#endif
144 int idx;
145 char_u *ptr;
146 char_u *line;
147 colnr_T col = 0;
148 int csize = 0;
149 int one_more;
150#ifdef FEAT_LINEBREAK
151 int head = 0;
152#endif
153
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000154 one_more = (State & INSERT)
155 || restart_edit != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#ifdef FEAT_VISUAL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000157 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000159#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000160 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000161#endif
162 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 line = ml_get_curline();
164
165 if (wcol >= MAXCOL)
166 {
167 idx = (int)STRLEN(line) - 1 + one_more;
168 col = wcol;
169
170#ifdef FEAT_VIRTUALEDIT
171 if ((addspaces || finetune) && !VIsual_active)
172 {
173 curwin->w_curswant = linetabsize(line) + one_more;
174 if (curwin->w_curswant > 0)
175 --curwin->w_curswant;
176 }
177#endif
178 }
179 else
180 {
181#ifdef FEAT_VIRTUALEDIT
182 int width = W_WIDTH(curwin) - win_col_off(curwin);
183
Bram Moolenaarebefac62005-12-28 22:39:57 +0000184 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 && curwin->w_p_wrap
186# ifdef FEAT_VERTSPLIT
187 && curwin->w_width != 0
188# endif
189 && wcol >= (colnr_T)width)
190 {
191 csize = linetabsize(line);
192 if (csize > 0)
193 csize--;
194
Bram Moolenaarebefac62005-12-28 22:39:57 +0000195 if (wcol / width > (colnr_T)csize / width
196 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 {
198 /* In case of line wrapping don't move the cursor beyond the
Bram Moolenaarebefac62005-12-28 22:39:57 +0000199 * right screen edge. In Insert mode allow going just beyond
200 * the last character (like what happens when typing and
201 * reaching the right window edge). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 wcol = (csize / width + 1) * width - 1;
203 }
204 }
205#endif
206
207 idx = -1;
208 ptr = line;
209 while (col <= wcol && *ptr != NUL)
210 {
211 /* Count a tab for what it's worth (if list mode not on) */
212#ifdef FEAT_LINEBREAK
213 csize = win_lbr_chartabsize(curwin, ptr, col, &head);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000214 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#else
216 csize = lbr_chartabsize_adv(&ptr, col);
217#endif
218 col += csize;
219 }
220 idx = (int)(ptr - line);
221 /*
222 * Handle all the special cases. The virtual_active() check
223 * is needed to ensure that a virtual position off the end of
224 * a line has the correct indexing. The one_more comparison
225 * replaces an explicit add of one_more later on.
226 */
227 if (col > wcol || (!virtual_active() && one_more == 0))
228 {
229 idx -= 1;
230# ifdef FEAT_LINEBREAK
231 /* Don't count the chars from 'showbreak'. */
232 csize -= head;
233# endif
234 col -= csize;
235 }
236
237#ifdef FEAT_VIRTUALEDIT
238 if (virtual_active()
239 && addspaces
240 && ((col != wcol && col != wcol + 1) || csize > 1))
241 {
242 /* 'virtualedit' is set: The difference between wcol and col is
243 * filled with spaces. */
244
245 if (line[idx] == NUL)
246 {
247 /* Append spaces */
248 int correct = wcol - col;
249 char_u *newline = alloc(idx + correct + 1);
250 int t;
251
252 if (newline == NULL)
253 return FAIL;
254
255 for (t = 0; t < idx; ++t)
256 newline[t] = line[t];
257
258 for (t = 0; t < correct; ++t)
259 newline[t + idx] = ' ';
260
261 newline[idx + correct] = NUL;
262
263 ml_replace(pos->lnum, newline, FALSE);
264 changed_bytes(pos->lnum, (colnr_T)idx);
265 idx += correct;
266 col = wcol;
267 }
268 else
269 {
270 /* Break a tab */
271 int linelen = (int)STRLEN(line);
272 int correct = wcol - col - csize + 1; /* negative!! */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000273 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 int t, s = 0;
275 int v;
276
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000277 if (-correct > csize)
278 return FAIL;
279
280 newline = alloc(linelen + csize);
281 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 return FAIL;
283
284 for (t = 0; t < linelen; t++)
285 {
286 if (t != idx)
287 newline[s++] = line[t];
288 else
289 for (v = 0; v < csize; v++)
290 newline[s++] = ' ';
291 }
292
293 newline[linelen + csize - 1] = NUL;
294
295 ml_replace(pos->lnum, newline, FALSE);
296 changed_bytes(pos->lnum, idx);
297 idx += (csize - 1 + correct);
298 col += correct;
299 }
300 }
301#endif
302 }
303
304 if (idx < 0)
305 pos->col = 0;
306 else
307 pos->col = idx;
308
309#ifdef FEAT_VIRTUALEDIT
310 pos->coladd = 0;
311
312 if (finetune)
313 {
314 if (wcol == MAXCOL)
315 {
316 /* The width of the last character is used to set coladd. */
317 if (!one_more)
318 {
319 colnr_T scol, ecol;
320
321 getvcol(curwin, pos, &scol, NULL, &ecol);
322 pos->coladd = ecol - scol;
323 }
324 }
325 else
326 {
327 int b = (int)wcol - (int)col;
328
329 /* The difference between wcol and col is used to set coladd. */
330 if (b > 0 && b < (MAXCOL - 2 * W_WIDTH(curwin)))
331 pos->coladd = b;
332
333 col += b;
334 }
335 }
336#endif
337
338#ifdef FEAT_MBYTE
339 /* prevent cursor from moving on the trail byte */
340 if (has_mbyte)
341 mb_adjust_cursor();
342#endif
343
344 if (col < wcol)
345 return FAIL;
346 return OK;
347}
348
349/*
350 * inc(p)
351 *
352 * Increment the line pointer 'p' crossing line boundaries as necessary.
353 * Return 1 when going to the next line.
354 * Return 2 when moving forward onto a NUL at the end of the line).
355 * Return -1 when at the end of file.
356 * Return 0 otherwise.
357 */
358 int
359inc_cursor()
360{
361 return inc(&curwin->w_cursor);
362}
363
364 int
365inc(lp)
366 pos_T *lp;
367{
368 char_u *p = ml_get_pos(lp);
369
370 if (*p != NUL) /* still within line, move to next char (may be NUL) */
371 {
372#ifdef FEAT_MBYTE
373 if (has_mbyte)
374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000375 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376
377 lp->col += l;
378 return ((p[l] != NUL) ? 0 : 2);
379 }
380#endif
381 lp->col++;
382#ifdef FEAT_VIRTUALEDIT
383 lp->coladd = 0;
384#endif
385 return ((p[1] != NUL) ? 0 : 2);
386 }
387 if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
388 {
389 lp->col = 0;
390 lp->lnum++;
391#ifdef FEAT_VIRTUALEDIT
392 lp->coladd = 0;
393#endif
394 return 1;
395 }
396 return -1;
397}
398
399/*
400 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
401 */
402 int
403incl(lp)
404 pos_T *lp;
405{
406 int r;
407
408 if ((r = inc(lp)) >= 1 && lp->col)
409 r = inc(lp);
410 return r;
411}
412
413/*
414 * dec(p)
415 *
416 * Decrement the line pointer 'p' crossing line boundaries as necessary.
417 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
418 */
419 int
420dec_cursor()
421{
422 return dec(&curwin->w_cursor);
423}
424
425 int
426dec(lp)
427 pos_T *lp;
428{
429 char_u *p;
430
431#ifdef FEAT_VIRTUALEDIT
432 lp->coladd = 0;
433#endif
434 if (lp->col > 0) /* still within line */
435 {
436 lp->col--;
437#ifdef FEAT_MBYTE
438 if (has_mbyte)
439 {
440 p = ml_get(lp->lnum);
441 lp->col -= (*mb_head_off)(p, p + lp->col);
442 }
443#endif
444 return 0;
445 }
446 if (lp->lnum > 1) /* there is a prior line */
447 {
448 lp->lnum--;
449 p = ml_get(lp->lnum);
450 lp->col = (colnr_T)STRLEN(p);
451#ifdef FEAT_MBYTE
452 if (has_mbyte)
453 lp->col -= (*mb_head_off)(p, p + lp->col);
454#endif
455 return 1;
456 }
457 return -1; /* at start of file */
458}
459
460/*
461 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
462 */
463 int
464decl(lp)
465 pos_T *lp;
466{
467 int r;
468
469 if ((r = dec(lp)) == 1 && lp->col)
470 r = dec(lp);
471 return r;
472}
473
474/*
475 * Make sure curwin->w_cursor.lnum is valid.
476 */
477 void
478check_cursor_lnum()
479{
480 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
481 {
482#ifdef FEAT_FOLDING
483 /* If there is a closed fold at the end of the file, put the cursor in
484 * its first line. Otherwise in the last line. */
485 if (!hasFolding(curbuf->b_ml.ml_line_count,
486 &curwin->w_cursor.lnum, NULL))
487#endif
488 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
489 }
490 if (curwin->w_cursor.lnum <= 0)
491 curwin->w_cursor.lnum = 1;
492}
493
494/*
495 * Make sure curwin->w_cursor.col is valid.
496 */
497 void
498check_cursor_col()
499{
500 colnr_T len;
501#ifdef FEAT_VIRTUALEDIT
502 colnr_T oldcol = curwin->w_cursor.col + curwin->w_cursor.coladd;
503#endif
504
505 len = (colnr_T)STRLEN(ml_get_curline());
506 if (len == 0)
507 curwin->w_cursor.col = 0;
508 else if (curwin->w_cursor.col >= len)
509 {
510 /* Allow cursor past end-of-line in Insert mode, restarting Insert
511 * mode or when in Visual mode and 'selection' isn't "old" */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000512 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513#ifdef FEAT_VISUAL
514 || (VIsual_active && *p_sel != 'o')
515#endif
516 || virtual_active())
517 curwin->w_cursor.col = len;
518 else
519 curwin->w_cursor.col = len - 1;
520 }
521
522#ifdef FEAT_VIRTUALEDIT
523 /* If virtual editing is on, we can leave the cursor on the old position,
524 * only we must set it to virtual. But don't do it when at the end of the
525 * line. */
526 if (oldcol == MAXCOL)
527 curwin->w_cursor.coladd = 0;
528 else if (ve_flags == VE_ALL)
529 curwin->w_cursor.coladd = oldcol - curwin->w_cursor.col;
530#endif
531}
532
533/*
534 * make sure curwin->w_cursor in on a valid character
535 */
536 void
537check_cursor()
538{
539 check_cursor_lnum();
540 check_cursor_col();
541}
542
543#if defined(FEAT_TEXTOBJ) || defined(PROTO)
544/*
545 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
546 * Allow it when in Visual mode and 'selection' is not "old".
547 */
548 void
549adjust_cursor_col()
550{
551 if (curwin->w_cursor.col > 0
552# ifdef FEAT_VISUAL
553 && (!VIsual_active || *p_sel == 'o')
554# endif
555 && gchar_cursor() == NUL)
556 --curwin->w_cursor.col;
557}
558#endif
559
560/*
561 * When curwin->w_leftcol has changed, adjust the cursor position.
562 * Return TRUE if the cursor was moved.
563 */
564 int
565leftcol_changed()
566{
567 long lastcol;
568 colnr_T s, e;
569 int retval = FALSE;
570
571 changed_cline_bef_curs();
572 lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1;
573 validate_virtcol();
574
575 /*
576 * If the cursor is right or left of the screen, move it to last or first
577 * character.
578 */
579 if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
580 {
581 retval = TRUE;
582 coladvance((colnr_T)(lastcol - p_siso));
583 }
584 else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
585 {
586 retval = TRUE;
587 (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
588 }
589
590 /*
591 * If the start of the character under the cursor is not on the screen,
592 * advance the cursor one more char. If this fails (last char of the
593 * line) adjust the scrolling.
594 */
595 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
596 if (e > (colnr_T)lastcol)
597 {
598 retval = TRUE;
599 coladvance(s - 1);
600 }
601 else if (s < curwin->w_leftcol)
602 {
603 retval = TRUE;
604 if (coladvance(e + 1) == FAIL) /* there isn't another character */
605 {
606 curwin->w_leftcol = s; /* adjust w_leftcol instead */
607 changed_cline_bef_curs();
608 }
609 }
610
611 if (retval)
612 curwin->w_set_curswant = TRUE;
613 redraw_later(NOT_VALID);
614 return retval;
615}
616
617/**********************************************************************
618 * Various routines dealing with allocation and deallocation of memory.
619 */
620
621#if defined(MEM_PROFILE) || defined(PROTO)
622
623# define MEM_SIZES 8200
624static long_u mem_allocs[MEM_SIZES];
625static long_u mem_frees[MEM_SIZES];
626static long_u mem_allocated;
627static long_u mem_freed;
628static long_u mem_peak;
629static long_u num_alloc;
630static long_u num_freed;
631
632static void mem_pre_alloc_s __ARGS((size_t *sizep));
633static void mem_pre_alloc_l __ARGS((long_u *sizep));
634static void mem_post_alloc __ARGS((void **pp, size_t size));
635static void mem_pre_free __ARGS((void **pp));
636
637 static void
638mem_pre_alloc_s(sizep)
639 size_t *sizep;
640{
641 *sizep += sizeof(size_t);
642}
643
644 static void
645mem_pre_alloc_l(sizep)
646 long_u *sizep;
647{
648 *sizep += sizeof(size_t);
649}
650
651 static void
652mem_post_alloc(pp, size)
653 void **pp;
654 size_t size;
655{
656 if (*pp == NULL)
657 return;
658 size -= sizeof(size_t);
659 *(long_u *)*pp = size;
660 if (size <= MEM_SIZES-1)
661 mem_allocs[size-1]++;
662 else
663 mem_allocs[MEM_SIZES-1]++;
664 mem_allocated += size;
665 if (mem_allocated - mem_freed > mem_peak)
666 mem_peak = mem_allocated - mem_freed;
667 num_alloc++;
668 *pp = (void *)((char *)*pp + sizeof(size_t));
669}
670
671 static void
672mem_pre_free(pp)
673 void **pp;
674{
675 long_u size;
676
677 *pp = (void *)((char *)*pp - sizeof(size_t));
678 size = *(size_t *)*pp;
679 if (size <= MEM_SIZES-1)
680 mem_frees[size-1]++;
681 else
682 mem_frees[MEM_SIZES-1]++;
683 mem_freed += size;
684 num_freed++;
685}
686
687/*
688 * called on exit via atexit()
689 */
690 void
691vim_mem_profile_dump()
692{
693 int i, j;
694
695 printf("\r\n");
696 j = 0;
697 for (i = 0; i < MEM_SIZES - 1; i++)
698 {
699 if (mem_allocs[i] || mem_frees[i])
700 {
701 if (mem_frees[i] > mem_allocs[i])
702 printf("\r\n%s", _("ERROR: "));
703 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
704 j++;
705 if (j > 3)
706 {
707 j = 0;
708 printf("\r\n");
709 }
710 }
711 }
712
713 i = MEM_SIZES - 1;
714 if (mem_allocs[i])
715 {
716 printf("\r\n");
717 if (mem_frees[i] > mem_allocs[i])
718 printf(_("ERROR: "));
719 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
720 }
721
722 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
723 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
724 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
725 num_alloc, num_freed);
726}
727
728#endif /* MEM_PROFILE */
729
730/*
731 * Some memory is reserved for error messages and for being able to
732 * call mf_release_all(), which needs some memory for mf_trans_add().
733 */
734#if defined(MSDOS) && !defined(DJGPP)
735# define SMALL_MEM
736# define KEEP_ROOM 8192L
737#else
738# define KEEP_ROOM (2 * 8192L)
739#endif
740
741/*
742 * Note: if unsinged is 16 bits we can only allocate up to 64K with alloc().
743 * Use lalloc for larger blocks.
744 */
745 char_u *
746alloc(size)
747 unsigned size;
748{
749 return (lalloc((long_u)size, TRUE));
750}
751
752/*
753 * Allocate memory and set all bytes to zero.
754 */
755 char_u *
756alloc_clear(size)
757 unsigned size;
758{
759 char_u *p;
760
761 p = (lalloc((long_u)size, TRUE));
762 if (p != NULL)
763 (void)vim_memset(p, 0, (size_t)size);
764 return p;
765}
766
767/*
768 * alloc() with check for maximum line length
769 */
770 char_u *
771alloc_check(size)
772 unsigned size;
773{
774#if !defined(UNIX) && !defined(__EMX__)
775 if (sizeof(int) == 2 && size > 0x7fff)
776 {
777 /* Don't hide this message */
778 emsg_silent = 0;
779 EMSG(_("E340: Line is becoming too long"));
780 return NULL;
781 }
782#endif
783 return (lalloc((long_u)size, TRUE));
784}
785
786/*
787 * Allocate memory like lalloc() and set all bytes to zero.
788 */
789 char_u *
790lalloc_clear(size, message)
791 long_u size;
792 int message;
793{
794 char_u *p;
795
796 p = (lalloc(size, message));
797 if (p != NULL)
798 (void)vim_memset(p, 0, (size_t)size);
799 return p;
800}
801
802/*
803 * Low level memory allocation function.
804 * This is used often, KEEP IT FAST!
805 */
806 char_u *
807lalloc(size, message)
808 long_u size;
809 int message;
810{
811 char_u *p; /* pointer to new storage space */
812 static int releasing = FALSE; /* don't do mf_release_all() recursive */
813 int try_again;
814#if defined(HAVE_AVAIL_MEM) && !defined(SMALL_MEM)
815 static long_u allocated = 0; /* allocated since last avail check */
816#endif
817
818 /* Safety check for allocating zero bytes */
819 if (size == 0)
820 {
821 /* Don't hide this message */
822 emsg_silent = 0;
823 EMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
824 return NULL;
825 }
826
827#ifdef MEM_PROFILE
828 mem_pre_alloc_l(&size);
829#endif
830
831#if defined(MSDOS) && !defined(DJGPP)
832 if (size >= 0xfff0) /* in MSDOS we can't deal with >64K blocks */
833 p = NULL;
834 else
835#endif
836
837 /*
838 * Loop when out of memory: Try to release some memfile blocks and
839 * if some blocks are released call malloc again.
840 */
841 for (;;)
842 {
843 /*
844 * Handle three kind of systems:
845 * 1. No check for available memory: Just return.
846 * 2. Slow check for available memory: call mch_avail_mem() after
847 * allocating KEEP_ROOM amount of memory.
848 * 3. Strict check for available memory: call mch_avail_mem()
849 */
850 if ((p = (char_u *)malloc((size_t)size)) != NULL)
851 {
852#ifndef HAVE_AVAIL_MEM
853 /* 1. No check for available memory: Just return. */
854 goto theend;
855#else
856# ifndef SMALL_MEM
857 /* 2. Slow check for available memory: call mch_avail_mem() after
858 * allocating (KEEP_ROOM / 2) amount of memory. */
859 allocated += size;
860 if (allocated < KEEP_ROOM / 2)
861 goto theend;
862 allocated = 0;
863# endif
864 /* 3. check for available memory: call mch_avail_mem() */
865 if (mch_avail_mem(TRUE) < KEEP_ROOM && !releasing)
866 {
867 vim_free((char *)p); /* System is low... no go! */
868 p = NULL;
869 }
870 else
871 goto theend;
872#endif
873 }
874 /*
875 * Remember that mf_release_all() is being called to avoid an endless
876 * loop, because mf_release_all() may call alloc() recursively.
877 */
878 if (releasing)
879 break;
880 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000881
882 clear_sb_text(); /* free any scrollback text */
883 try_again = mf_release_all(); /* release as many blocks as possible */
Bram Moolenaar39a58ca2005-06-27 22:42:44 +0000884#ifdef FEAT_EVAL
Bram Moolenaar661b1822005-07-28 22:36:45 +0000885 try_again |= garbage_collect(); /* cleanup recursive lists/dicts */
Bram Moolenaar39a58ca2005-06-27 22:42:44 +0000886#endif
Bram Moolenaar661b1822005-07-28 22:36:45 +0000887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 releasing = FALSE;
889 if (!try_again)
890 break;
891 }
892
893 if (message && p == NULL)
894 do_outofmem_msg(size);
895
896theend:
897#ifdef MEM_PROFILE
898 mem_post_alloc((void **)&p, (size_t)size);
899#endif
900 return p;
901}
902
903#if defined(MEM_PROFILE) || defined(PROTO)
904/*
905 * realloc() with memory profiling.
906 */
907 void *
908mem_realloc(ptr, size)
909 void *ptr;
910 size_t size;
911{
912 void *p;
913
914 mem_pre_free(&ptr);
915 mem_pre_alloc_s(&size);
916
917 p = realloc(ptr, size);
918
919 mem_post_alloc(&p, size);
920
921 return p;
922}
923#endif
924
925/*
926* Avoid repeating the error message many times (they take 1 second each).
927* Did_outofmem_msg is reset when a character is read.
928*/
929 void
930do_outofmem_msg(size)
931 long_u size;
932{
933 if (!did_outofmem_msg)
934 {
935 /* Don't hide this message */
936 emsg_silent = 0;
937 EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
938 did_outofmem_msg = TRUE;
939 }
940}
941
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000942#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000943
944# if defined(FEAT_SEARCHPATH)
945static void free_findfile __ARGS((void));
946# endif
947
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000948/*
949 * Free everything that we allocated.
950 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000951 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
952 * surprised if Vim crashes...
953 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000954 */
955 void
956free_all_mem()
957{
958 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000959 static int entered = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000960 win_T *win;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000961
962 /* When we cause a crash here it is caught and Vim tries to exit cleanly.
963 * Don't try freeing everything again. */
964 if (entered)
965 return;
966 entered = TRUE;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000967
968 ++autocmd_block; /* don't want to trigger autocommands here */
969
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000970#ifdef FEAT_WINDOWS
971 /* close all tabs and windows */
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000972 if (first_tabpage->tp_next != NULL)
973 do_cmdline_cmd((char_u *)"tabonly!");
974 if (firstwin != lastwin)
975 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000976#endif
977
Bram Moolenaarc4956c82006-03-12 21:58:43 +0000978# if defined(FEAT_SPELL)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000979 /* Free all spell info. */
980 spell_free_all();
981# endif
982
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000983# if defined(FEAT_USR_CMDS)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000984 /* Clear user commands (before deleting buffers). */
985 ex_comclear(NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000986# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000987
988# ifdef FEAT_MENU
989 /* Clear menus. */
990 do_cmdline_cmd((char_u *)"aunmenu *");
991# endif
992
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000993 /* Clear mappings, abbreviations, breakpoints. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000994 do_cmdline_cmd((char_u *)"mapclear");
995 do_cmdline_cmd((char_u *)"mapclear!");
996 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000997# if defined(FEAT_EVAL)
998 do_cmdline_cmd((char_u *)"breakdel *");
999# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001000# if defined(FEAT_PROFILE)
1001 do_cmdline_cmd((char_u *)"profdel *");
1002# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001003
1004# ifdef FEAT_TITLE
1005 free_titles();
1006# endif
1007# if defined(FEAT_SEARCHPATH)
1008 free_findfile();
1009# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001010
1011 /* Obviously named calls. */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001012# if defined(FEAT_AUTOCMD)
1013 free_all_autocmds();
1014# endif
1015 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001016 free_all_options();
1017 free_all_marks();
1018 alist_clear(&global_alist);
1019 free_homedir();
1020 free_search_patterns();
1021 free_old_sub();
1022 free_last_insert();
1023 free_prev_shellcmd();
1024 free_regexp_stuff();
1025 free_tag_stuff();
1026 free_cd_dir();
1027 set_expr_line(NULL);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001028 diff_clear(curtab);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001029 clear_sb_text(); /* free any scrollback text */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001030
1031 /* Free some global vars. */
1032 vim_free(username);
1033 vim_free(clip_exclude_prog);
1034 vim_free(last_cmdline);
1035 vim_free(new_last_cmdline);
Bram Moolenaar238a5642006-02-21 22:12:05 +00001036 set_keep_msg(NULL, 0);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001037 vim_free(ff_expand_buffer);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001038
1039 /* Clear cmdline history. */
1040 p_hi = 0;
1041 init_history();
1042
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001043#ifdef FEAT_QUICKFIX
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001044 qf_free_all(NULL);
1045 /* Free all location lists */
1046 FOR_ALL_WINDOWS(win)
1047 qf_free_all(win);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001048#endif
1049
1050 /* Close all script inputs. */
1051 close_all_scripts();
1052
1053#if defined(FEAT_WINDOWS)
1054 /* Destroy all windows. Must come before freeing buffers. */
1055 win_free_all();
1056#endif
1057
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001058 /* Free all buffers. */
1059 for (buf = firstbuf; buf != NULL; )
1060 {
1061 nextbuf = buf->b_next;
1062 close_buffer(NULL, buf, DOBUF_WIPE);
1063 if (buf_valid(buf))
1064 buf = nextbuf; /* didn't work, try next one */
1065 else
1066 buf = firstbuf;
1067 }
1068
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001069#ifdef FEAT_ARABIC
1070 free_cmdline_buf();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001071#endif
1072
1073 /* Clear registers. */
1074 clear_registers();
1075 ResetRedobuff();
1076 ResetRedobuff();
1077
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001078#ifdef FEAT_CLIENTSERVER
1079 vim_free(serverDelayedStartName);
1080#endif
1081
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001082 /* highlight info */
1083 free_highlight();
1084
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001085 reset_last_sourcing();
1086
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001087#ifdef FEAT_WINDOWS
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001088 free_tabpage(first_tabpage);
1089 first_tabpage = NULL;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001090#endif
1091
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001092# ifdef UNIX
1093 /* Machine-specific free. */
1094 mch_free_mem();
1095# endif
1096
1097 /* message history */
1098 for (;;)
1099 if (delete_first_msg() == FAIL)
1100 break;
1101
1102# ifdef FEAT_EVAL
1103 eval_clear();
1104# endif
1105
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001106 free_termoptions();
1107
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001108 /* screenlines (can't display anything now!) */
1109 free_screenlines();
1110
1111#if defined(USE_XSMP)
1112 xsmp_close();
1113#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001114#ifdef FEAT_GUI_GTK
1115 gui_mch_free_all();
1116#endif
1117 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001118
1119 vim_free(IObuff);
1120 vim_free(NameBuff);
1121}
1122#endif
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124/*
1125 * copy a string into newly allocated memory
1126 */
1127 char_u *
1128vim_strsave(string)
1129 char_u *string;
1130{
1131 char_u *p;
1132 unsigned len;
1133
1134 len = (unsigned)STRLEN(string) + 1;
1135 p = alloc(len);
1136 if (p != NULL)
1137 mch_memmove(p, string, (size_t)len);
1138 return p;
1139}
1140
1141 char_u *
1142vim_strnsave(string, len)
1143 char_u *string;
1144 int len;
1145{
1146 char_u *p;
1147
1148 p = alloc((unsigned)(len + 1));
1149 if (p != NULL)
1150 {
1151 STRNCPY(p, string, len);
1152 p[len] = NUL;
1153 }
1154 return p;
1155}
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157/*
1158 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1159 * by a backslash.
1160 */
1161 char_u *
1162vim_strsave_escaped(string, esc_chars)
1163 char_u *string;
1164 char_u *esc_chars;
1165{
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001166 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167}
1168
1169/*
1170 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape
1171 * characters where rem_backslash() would remove the backslash.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001172 * Escape the characters with "cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 */
1174 char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001175vim_strsave_escaped_ext(string, esc_chars, cc, bsl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 char_u *string;
1177 char_u *esc_chars;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001178 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 int bsl;
1180{
1181 char_u *p;
1182 char_u *p2;
1183 char_u *escaped_string;
1184 unsigned length;
1185#ifdef FEAT_MBYTE
1186 int l;
1187#endif
1188
1189 /*
1190 * First count the number of backslashes required.
1191 * Then allocate the memory and insert them.
1192 */
1193 length = 1; /* count the trailing NUL */
1194 for (p = string; *p; p++)
1195 {
1196#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001197 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 length += l; /* count a multibyte char */
1200 p += l - 1;
1201 continue;
1202 }
1203#endif
1204 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
1205 ++length; /* count a backslash */
1206 ++length; /* count an ordinary char */
1207 }
1208 escaped_string = alloc(length);
1209 if (escaped_string != NULL)
1210 {
1211 p2 = escaped_string;
1212 for (p = string; *p; p++)
1213 {
1214#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001215 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
1217 mch_memmove(p2, p, (size_t)l);
1218 p2 += l;
1219 p += l - 1; /* skip multibyte char */
1220 continue;
1221 }
1222#endif
1223 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001224 *p2++ = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 *p2++ = *p;
1226 }
1227 *p2 = NUL;
1228 }
1229 return escaped_string;
1230}
1231
1232/*
1233 * Like vim_strsave(), but make all characters uppercase.
1234 * This uses ASCII lower-to-upper case translation, language independent.
1235 */
1236 char_u *
1237vim_strsave_up(string)
1238 char_u *string;
1239{
1240 char_u *p1;
1241
1242 p1 = vim_strsave(string);
1243 vim_strup(p1);
1244 return p1;
1245}
1246
1247/*
1248 * Like vim_strnsave(), but make all characters uppercase.
1249 * This uses ASCII lower-to-upper case translation, language independent.
1250 */
1251 char_u *
1252vim_strnsave_up(string, len)
1253 char_u *string;
1254 int len;
1255{
1256 char_u *p1;
1257
1258 p1 = vim_strnsave(string, len);
1259 vim_strup(p1);
1260 return p1;
1261}
1262
1263/*
1264 * ASCII lower-to-upper case translation, language independent.
1265 */
1266 void
1267vim_strup(p)
1268 char_u *p;
1269{
1270 char_u *p2;
1271 int c;
1272
1273 if (p != NULL)
1274 {
1275 p2 = p;
1276 while ((c = *p2) != NUL)
1277#ifdef EBCDIC
1278 *p2++ = isalpha(c) ? toupper(c) : c;
1279#else
1280 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
1281#endif
1282 }
1283}
1284
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001285#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001286/*
1287 * Make string "s" all upper-case and return it in allocated memory.
1288 * Handles multi-byte characters as well as possible.
1289 * Returns NULL when out of memory.
1290 */
1291 char_u *
1292strup_save(orig)
1293 char_u *orig;
1294{
1295 char_u *p;
1296 char_u *res;
1297
1298 res = p = vim_strsave(orig);
1299
1300 if (res != NULL)
1301 while (*p != NUL)
1302 {
1303# ifdef FEAT_MBYTE
1304 int l;
1305
1306 if (enc_utf8)
1307 {
1308 int c, uc;
1309 int nl;
1310 char_u *s;
1311
1312 c = utf_ptr2char(p);
1313 uc = utf_toupper(c);
1314
1315 /* Reallocate string when byte count changes. This is rare,
1316 * thus it's OK to do another malloc()/free(). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001317 l = utf_ptr2len(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001318 nl = utf_char2len(uc);
1319 if (nl != l)
1320 {
1321 s = alloc((unsigned)STRLEN(res) + 1 + nl - l);
1322 if (s == NULL)
1323 break;
1324 mch_memmove(s, res, p - res);
1325 STRCPY(s + (p - res) + nl, p + l);
1326 p = s + (p - res);
1327 vim_free(res);
1328 res = s;
1329 }
1330
1331 utf_char2bytes(uc, p);
1332 p += nl;
1333 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001334 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001335 p += l; /* skip multi-byte character */
1336 else
1337# endif
1338 {
1339 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
1340 p++;
1341 }
1342 }
1343
1344 return res;
1345}
1346#endif
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348/*
1349 * copy a space a number of times
1350 */
1351 void
1352copy_spaces(ptr, count)
1353 char_u *ptr;
1354 size_t count;
1355{
1356 size_t i = count;
1357 char_u *p = ptr;
1358
1359 while (i--)
1360 *p++ = ' ';
1361}
1362
1363#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1364/*
1365 * Copy a character a number of times.
1366 * Does not work for multi-byte charactes!
1367 */
1368 void
1369copy_chars(ptr, count, c)
1370 char_u *ptr;
1371 size_t count;
1372 int c;
1373{
1374 size_t i = count;
1375 char_u *p = ptr;
1376
1377 while (i--)
1378 *p++ = c;
1379}
1380#endif
1381
1382/*
1383 * delete spaces at the end of a string
1384 */
1385 void
1386del_trailing_spaces(ptr)
1387 char_u *ptr;
1388{
1389 char_u *q;
1390
1391 q = ptr + STRLEN(ptr);
1392 while (--q > ptr && vim_iswhite(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
1393 *q = NUL;
1394}
1395
1396/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001397 * Like strncpy(), but always terminate the result with one NUL.
Bram Moolenaard042c562005-06-30 22:04:15 +00001398 * "to" must be "len + 1" long!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
1400 void
1401vim_strncpy(to, from, len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001402 char_u *to;
1403 char_u *from;
Bram Moolenaarbbebc852005-07-18 21:47:53 +00001404 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001406 STRNCPY(to, from, len);
1407 to[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408}
1409
1410/*
1411 * Isolate one part of a string option where parts are separated with
1412 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001413 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 * "*option" is advanced to the next part.
1415 * The length is returned.
1416 */
1417 int
1418copy_option_part(option, buf, maxlen, sep_chars)
1419 char_u **option;
1420 char_u *buf;
1421 int maxlen;
1422 char *sep_chars;
1423{
1424 int len = 0;
1425 char_u *p = *option;
1426
1427 /* skip '.' at start of option part, for 'suffixes' */
1428 if (*p == '.')
1429 buf[len++] = *p++;
1430 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1431 {
1432 /*
1433 * Skip backslash before a separator character and space.
1434 */
1435 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1436 ++p;
1437 if (len < maxlen - 1)
1438 buf[len++] = *p;
1439 ++p;
1440 }
1441 buf[len] = NUL;
1442
1443 if (*p != NUL && *p != ',') /* skip non-standard separator */
1444 ++p;
1445 p = skip_to_option_part(p); /* p points to next file name */
1446
1447 *option = p;
1448 return len;
1449}
1450
1451/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001452 * Replacement for free() that ignores NULL pointers.
1453 * Also skip free() when exiting for sure, this helps when we caught a deadly
1454 * signal that was caused by a crash in free().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 void
1457vim_free(x)
1458 void *x;
1459{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001460 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
1462#ifdef MEM_PROFILE
1463 mem_pre_free(&x);
1464#endif
1465 free(x);
1466 }
1467}
1468
1469#ifndef HAVE_MEMSET
1470 void *
1471vim_memset(ptr, c, size)
1472 void *ptr;
1473 int c;
1474 size_t size;
1475{
1476 char *p = ptr;
1477
1478 while (size-- > 0)
1479 *p++ = c;
1480 return ptr;
1481}
1482#endif
1483
1484#ifdef VIM_MEMCMP
1485/*
1486 * Return zero when "b1" and "b2" are the same for "len" bytes.
1487 * Return non-zero otherwise.
1488 */
1489 int
1490vim_memcmp(b1, b2, len)
1491 void *b1;
1492 void *b2;
1493 size_t len;
1494{
1495 char_u *p1 = (char_u *)b1, *p2 = (char_u *)b2;
1496
1497 for ( ; len > 0; --len)
1498 {
1499 if (*p1 != *p2)
1500 return 1;
1501 ++p1;
1502 ++p2;
1503 }
1504 return 0;
1505}
1506#endif
1507
1508#ifdef VIM_MEMMOVE
1509/*
1510 * Version of memmove() that handles overlapping source and destination.
1511 * For systems that don't have a function that is guaranteed to do that (SYSV).
1512 */
1513 void
1514mch_memmove(dst_arg, src_arg, len)
1515 void *src_arg, *dst_arg;
1516 size_t len;
1517{
1518 /*
1519 * A void doesn't have a size, we use char pointers.
1520 */
1521 char *dst = dst_arg, *src = src_arg;
1522
1523 /* overlap, copy backwards */
1524 if (dst > src && dst < src + len)
1525 {
1526 src += len;
1527 dst += len;
1528 while (len-- > 0)
1529 *--dst = *--src;
1530 }
1531 else /* copy forwards */
1532 while (len-- > 0)
1533 *dst++ = *src++;
1534}
1535#endif
1536
1537#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
1538/*
1539 * Compare two strings, ignoring case, using current locale.
1540 * Doesn't work for multi-byte characters.
1541 * return 0 for match, < 0 for smaller, > 0 for bigger
1542 */
1543 int
1544vim_stricmp(s1, s2)
1545 char *s1;
1546 char *s2;
1547{
1548 int i;
1549
1550 for (;;)
1551 {
1552 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1553 if (i != 0)
1554 return i; /* this character different */
1555 if (*s1 == NUL)
1556 break; /* strings match until NUL */
1557 ++s1;
1558 ++s2;
1559 }
1560 return 0; /* strings match */
1561}
1562#endif
1563
1564#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
1565/*
1566 * Compare two strings, for length "len", ignoring case, using current locale.
1567 * Doesn't work for multi-byte characters.
1568 * return 0 for match, < 0 for smaller, > 0 for bigger
1569 */
1570 int
1571vim_strnicmp(s1, s2, len)
1572 char *s1;
1573 char *s2;
1574 size_t len;
1575{
1576 int i;
1577
1578 while (len > 0)
1579 {
1580 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
1581 if (i != 0)
1582 return i; /* this character different */
1583 if (*s1 == NUL)
1584 break; /* strings match until NUL */
1585 ++s1;
1586 ++s2;
1587 --len;
1588 }
1589 return 0; /* strings match */
1590}
1591#endif
1592
1593#if 0 /* currently not used */
1594/*
1595 * Check if string "s2" appears somewhere in "s1" while ignoring case.
1596 * Return NULL if not, a pointer to the first occurrence if it does.
1597 */
1598 char_u *
1599vim_stristr(s1, s2)
1600 char_u *s1;
1601 char_u *s2;
1602{
1603 char_u *p;
1604 int len = STRLEN(s2);
1605 char_u *end = s1 + STRLEN(s1) - len;
1606
1607 for (p = s1; p <= end; ++p)
1608 if (STRNICMP(p, s2, len) == 0)
1609 return p;
1610 return NULL;
1611}
1612#endif
1613
1614/*
1615 * Version of strchr() and strrchr() that handle unsigned char strings
Bram Moolenaar05159a02005-02-26 23:04:13 +00001616 * with characters from 128 to 255 correctly. It also doesn't return a
1617 * pointer to the NUL at the end of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
1619 char_u *
1620vim_strchr(string, c)
1621 char_u *string;
1622 int c;
1623{
1624 char_u *p;
1625 int b;
1626
1627 p = string;
1628#ifdef FEAT_MBYTE
1629 if (enc_utf8 && c >= 0x80)
1630 {
1631 while (*p != NUL)
1632 {
1633 if (utf_ptr2char(p) == c)
1634 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001635 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 return NULL;
1638 }
1639 if (enc_dbcs != 0 && c > 255)
1640 {
1641 int n2 = c & 0xff;
1642
1643 c = ((unsigned)c >> 8) & 0xff;
1644 while ((b = *p) != NUL)
1645 {
1646 if (b == c && p[1] == n2)
1647 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001648 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
1650 return NULL;
1651 }
1652 if (has_mbyte)
1653 {
1654 while ((b = *p) != NUL)
1655 {
1656 if (b == c)
1657 return p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001658 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
1660 return NULL;
1661 }
1662#endif
1663 while ((b = *p) != NUL)
1664 {
1665 if (b == c)
1666 return p;
1667 ++p;
1668 }
1669 return NULL;
1670}
1671
1672/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00001673 * Version of strchr() that only works for bytes and handles unsigned char
1674 * strings with characters above 128 correctly. It also doesn't return a
1675 * pointer to the NUL at the end of the string.
1676 */
1677 char_u *
1678vim_strbyte(string, c)
1679 char_u *string;
1680 int c;
1681{
1682 char_u *p = string;
1683
1684 while (*p != NUL)
1685 {
1686 if (*p == c)
1687 return p;
1688 ++p;
1689 }
1690 return NULL;
1691}
1692
1693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 * Search for last occurrence of "c" in "string".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001695 * Return NULL if not found.
Bram Moolenaar05159a02005-02-26 23:04:13 +00001696 * Does not handle multi-byte char for "c"!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 */
1698 char_u *
1699vim_strrchr(string, c)
1700 char_u *string;
1701 int c;
1702{
1703 char_u *retval = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001704 char_u *p = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaar05159a02005-02-26 23:04:13 +00001706 while (*p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001708 if (*p == c)
1709 retval = p;
1710 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 }
1712 return retval;
1713}
1714
1715/*
1716 * Vim's version of strpbrk(), in case it's missing.
1717 * Don't generate a prototype for this, causes problems when it's not used.
1718 */
1719#ifndef PROTO
1720# ifndef HAVE_STRPBRK
1721# ifdef vim_strpbrk
1722# undef vim_strpbrk
1723# endif
1724 char_u *
1725vim_strpbrk(s, charset)
1726 char_u *s;
1727 char_u *charset;
1728{
1729 while (*s)
1730 {
1731 if (vim_strchr(charset, *s) != NULL)
1732 return s;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001733 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
1735 return NULL;
1736}
1737# endif
1738#endif
1739
1740/*
1741 * Vim has its own isspace() function, because on some machines isspace()
1742 * can't handle characters above 128.
1743 */
1744 int
1745vim_isspace(x)
1746 int x;
1747{
1748 return ((x >= 9 && x <= 13) || x == ' ');
1749}
1750
1751/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001752 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 */
1754
1755/*
1756 * Clear an allocated growing array.
1757 */
1758 void
1759ga_clear(gap)
1760 garray_T *gap;
1761{
1762 vim_free(gap->ga_data);
1763 ga_init(gap);
1764}
1765
1766/*
1767 * Clear a growing array that contains a list of strings.
1768 */
1769 void
1770ga_clear_strings(gap)
1771 garray_T *gap;
1772{
1773 int i;
1774
1775 for (i = 0; i < gap->ga_len; ++i)
1776 vim_free(((char_u **)(gap->ga_data))[i]);
1777 ga_clear(gap);
1778}
1779
1780/*
1781 * Initialize a growing array. Don't forget to set ga_itemsize and
1782 * ga_growsize! Or use ga_init2().
1783 */
1784 void
1785ga_init(gap)
1786 garray_T *gap;
1787{
1788 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00001789 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 gap->ga_len = 0;
1791}
1792
1793 void
1794ga_init2(gap, itemsize, growsize)
1795 garray_T *gap;
1796 int itemsize;
1797 int growsize;
1798{
1799 ga_init(gap);
1800 gap->ga_itemsize = itemsize;
1801 gap->ga_growsize = growsize;
1802}
1803
1804/*
1805 * Make room in growing array "gap" for at least "n" items.
1806 * Return FAIL for failure, OK otherwise.
1807 */
1808 int
1809ga_grow(gap, n)
1810 garray_T *gap;
1811 int n;
1812{
1813 size_t len;
1814 char_u *pp;
1815
Bram Moolenaar86b68352004-12-27 21:59:20 +00001816 if (gap->ga_maxlen - gap->ga_len < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 if (n < gap->ga_growsize)
1819 n = gap->ga_growsize;
1820 len = gap->ga_itemsize * (gap->ga_len + n);
1821 pp = alloc_clear((unsigned)len);
1822 if (pp == NULL)
1823 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00001824 gap->ga_maxlen = gap->ga_len + n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (gap->ga_data != NULL)
1826 {
1827 mch_memmove(pp, gap->ga_data,
1828 (size_t)(gap->ga_itemsize * gap->ga_len));
1829 vim_free(gap->ga_data);
1830 }
1831 gap->ga_data = pp;
1832 }
1833 return OK;
1834}
1835
1836/*
1837 * Concatenate a string to a growarray which contains characters.
1838 * Note: Does NOT copy the NUL at the end!
1839 */
1840 void
1841ga_concat(gap, s)
1842 garray_T *gap;
1843 char_u *s;
1844{
1845 int len = (int)STRLEN(s);
1846
1847 if (ga_grow(gap, len) == OK)
1848 {
1849 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
1850 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852}
1853
1854/*
1855 * Append one byte to a growarray which contains bytes.
1856 */
1857 void
1858ga_append(gap, c)
1859 garray_T *gap;
1860 int c;
1861{
1862 if (ga_grow(gap, 1) == OK)
1863 {
1864 *((char *)gap->ga_data + gap->ga_len) = c;
1865 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867}
1868
1869/************************************************************************
1870 * functions that use lookup tables for various things, generally to do with
1871 * special key codes.
1872 */
1873
1874/*
1875 * Some useful tables.
1876 */
1877
1878static struct modmasktable
1879{
1880 short mod_mask; /* Bit-mask for particular key modifier */
1881 short mod_flag; /* Bit(s) for particular key modifier */
1882 char_u name; /* Single letter name of modifier */
1883} mod_mask_table[] =
1884{
1885 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001886 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
1888 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
1889 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
1890 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
1891 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
1892#ifdef MACOS
1893 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
1894#endif
1895 /* 'A' must be the last one */
1896 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
1897 {0, 0, NUL}
1898};
1899
1900/*
1901 * Shifted key terminal codes and their unshifted equivalent.
1902 * Don't add mouse codes here, they are handled seperately!
1903 */
1904#define MOD_KEYS_ENTRY_SIZE 5
1905
1906static char_u modifier_keys_table[] =
1907{
1908/* mod mask with modifier without modifier */
1909 MOD_MASK_SHIFT, '&', '9', '@', '1', /* begin */
1910 MOD_MASK_SHIFT, '&', '0', '@', '2', /* cancel */
1911 MOD_MASK_SHIFT, '*', '1', '@', '4', /* command */
1912 MOD_MASK_SHIFT, '*', '2', '@', '5', /* copy */
1913 MOD_MASK_SHIFT, '*', '3', '@', '6', /* create */
1914 MOD_MASK_SHIFT, '*', '4', 'k', 'D', /* delete char */
1915 MOD_MASK_SHIFT, '*', '5', 'k', 'L', /* delete line */
1916 MOD_MASK_SHIFT, '*', '7', '@', '7', /* end */
1917 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', /* end */
1918 MOD_MASK_SHIFT, '*', '9', '@', '9', /* exit */
1919 MOD_MASK_SHIFT, '*', '0', '@', '0', /* find */
1920 MOD_MASK_SHIFT, '#', '1', '%', '1', /* help */
1921 MOD_MASK_SHIFT, '#', '2', 'k', 'h', /* home */
1922 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', /* home */
1923 MOD_MASK_SHIFT, '#', '3', 'k', 'I', /* insert */
1924 MOD_MASK_SHIFT, '#', '4', 'k', 'l', /* left arrow */
1925 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', /* left arrow */
1926 MOD_MASK_SHIFT, '%', 'a', '%', '3', /* message */
1927 MOD_MASK_SHIFT, '%', 'b', '%', '4', /* move */
1928 MOD_MASK_SHIFT, '%', 'c', '%', '5', /* next */
1929 MOD_MASK_SHIFT, '%', 'd', '%', '7', /* options */
1930 MOD_MASK_SHIFT, '%', 'e', '%', '8', /* previous */
1931 MOD_MASK_SHIFT, '%', 'f', '%', '9', /* print */
1932 MOD_MASK_SHIFT, '%', 'g', '%', '0', /* redo */
1933 MOD_MASK_SHIFT, '%', 'h', '&', '3', /* replace */
1934 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', /* right arr. */
1935 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', /* right arr. */
1936 MOD_MASK_SHIFT, '%', 'j', '&', '5', /* resume */
1937 MOD_MASK_SHIFT, '!', '1', '&', '6', /* save */
1938 MOD_MASK_SHIFT, '!', '2', '&', '7', /* suspend */
1939 MOD_MASK_SHIFT, '!', '3', '&', '8', /* undo */
1940 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', /* up arrow */
1941 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', /* down arrow */
1942
1943 /* vt100 F1 */
1944 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
1945 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
1946 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
1947 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
1948
1949 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', /* F1 */
1950 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
1951 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
1952 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
1953 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
1954 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
1955 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
1956 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
1957 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
1958 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', /* F10 */
1959
1960 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
1961 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
1962 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
1963 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
1964 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
1965 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
1966 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
1967 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
1968 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
1969 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
1970
1971 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
1972 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
1973 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
1974 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
1975 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
1976 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
1977 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
1978 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
1979 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
1980 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
1981
1982 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
1983 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
1984 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
1985 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
1986 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
1987 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
1988 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
1989
1990 /* TAB pseudo code*/
1991 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
1992
1993 NUL
1994};
1995
1996static struct key_name_entry
1997{
1998 int key; /* Special key code or ascii value */
1999 char_u *name; /* Name of key */
2000} key_names_table[] =
2001{
2002 {' ', (char_u *)"Space"},
2003 {TAB, (char_u *)"Tab"},
2004 {K_TAB, (char_u *)"Tab"},
2005 {NL, (char_u *)"NL"},
2006 {NL, (char_u *)"NewLine"}, /* Alternative name */
2007 {NL, (char_u *)"LineFeed"}, /* Alternative name */
2008 {NL, (char_u *)"LF"}, /* Alternative name */
2009 {CAR, (char_u *)"CR"},
2010 {CAR, (char_u *)"Return"}, /* Alternative name */
2011 {CAR, (char_u *)"Enter"}, /* Alternative name */
2012 {K_BS, (char_u *)"BS"},
2013 {K_BS, (char_u *)"BackSpace"}, /* Alternative name */
2014 {ESC, (char_u *)"Esc"},
2015 {CSI, (char_u *)"CSI"},
2016 {K_CSI, (char_u *)"xCSI"},
2017 {'|', (char_u *)"Bar"},
2018 {'\\', (char_u *)"Bslash"},
2019 {K_DEL, (char_u *)"Del"},
2020 {K_DEL, (char_u *)"Delete"}, /* Alternative name */
2021 {K_KDEL, (char_u *)"kDel"},
2022 {K_UP, (char_u *)"Up"},
2023 {K_DOWN, (char_u *)"Down"},
2024 {K_LEFT, (char_u *)"Left"},
2025 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002026 {K_XUP, (char_u *)"xUp"},
2027 {K_XDOWN, (char_u *)"xDown"},
2028 {K_XLEFT, (char_u *)"xLeft"},
2029 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 {K_F1, (char_u *)"F1"},
2032 {K_F2, (char_u *)"F2"},
2033 {K_F3, (char_u *)"F3"},
2034 {K_F4, (char_u *)"F4"},
2035 {K_F5, (char_u *)"F5"},
2036 {K_F6, (char_u *)"F6"},
2037 {K_F7, (char_u *)"F7"},
2038 {K_F8, (char_u *)"F8"},
2039 {K_F9, (char_u *)"F9"},
2040 {K_F10, (char_u *)"F10"},
2041
2042 {K_F11, (char_u *)"F11"},
2043 {K_F12, (char_u *)"F12"},
2044 {K_F13, (char_u *)"F13"},
2045 {K_F14, (char_u *)"F14"},
2046 {K_F15, (char_u *)"F15"},
2047 {K_F16, (char_u *)"F16"},
2048 {K_F17, (char_u *)"F17"},
2049 {K_F18, (char_u *)"F18"},
2050 {K_F19, (char_u *)"F19"},
2051 {K_F20, (char_u *)"F20"},
2052
2053 {K_F21, (char_u *)"F21"},
2054 {K_F22, (char_u *)"F22"},
2055 {K_F23, (char_u *)"F23"},
2056 {K_F24, (char_u *)"F24"},
2057 {K_F25, (char_u *)"F25"},
2058 {K_F26, (char_u *)"F26"},
2059 {K_F27, (char_u *)"F27"},
2060 {K_F28, (char_u *)"F28"},
2061 {K_F29, (char_u *)"F29"},
2062 {K_F30, (char_u *)"F30"},
2063
2064 {K_F31, (char_u *)"F31"},
2065 {K_F32, (char_u *)"F32"},
2066 {K_F33, (char_u *)"F33"},
2067 {K_F34, (char_u *)"F34"},
2068 {K_F35, (char_u *)"F35"},
2069 {K_F36, (char_u *)"F36"},
2070 {K_F37, (char_u *)"F37"},
2071
2072 {K_XF1, (char_u *)"xF1"},
2073 {K_XF2, (char_u *)"xF2"},
2074 {K_XF3, (char_u *)"xF3"},
2075 {K_XF4, (char_u *)"xF4"},
2076
2077 {K_HELP, (char_u *)"Help"},
2078 {K_UNDO, (char_u *)"Undo"},
2079 {K_INS, (char_u *)"Insert"},
2080 {K_INS, (char_u *)"Ins"}, /* Alternative name */
2081 {K_KINS, (char_u *)"kInsert"},
2082 {K_HOME, (char_u *)"Home"},
2083 {K_KHOME, (char_u *)"kHome"},
2084 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002085 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {K_END, (char_u *)"End"},
2087 {K_KEND, (char_u *)"kEnd"},
2088 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002089 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {K_PAGEUP, (char_u *)"PageUp"},
2091 {K_PAGEDOWN, (char_u *)"PageDown"},
2092 {K_KPAGEUP, (char_u *)"kPageUp"},
2093 {K_KPAGEDOWN, (char_u *)"kPageDown"},
2094
2095 {K_KPLUS, (char_u *)"kPlus"},
2096 {K_KMINUS, (char_u *)"kMinus"},
2097 {K_KDIVIDE, (char_u *)"kDivide"},
2098 {K_KMULTIPLY, (char_u *)"kMultiply"},
2099 {K_KENTER, (char_u *)"kEnter"},
2100 {K_KPOINT, (char_u *)"kPoint"},
2101
2102 {K_K0, (char_u *)"k0"},
2103 {K_K1, (char_u *)"k1"},
2104 {K_K2, (char_u *)"k2"},
2105 {K_K3, (char_u *)"k3"},
2106 {K_K4, (char_u *)"k4"},
2107 {K_K5, (char_u *)"k5"},
2108 {K_K6, (char_u *)"k6"},
2109 {K_K7, (char_u *)"k7"},
2110 {K_K8, (char_u *)"k8"},
2111 {K_K9, (char_u *)"k9"},
2112
2113 {'<', (char_u *)"lt"},
2114
2115 {K_MOUSE, (char_u *)"Mouse"},
2116 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
2117 {K_DEC_MOUSE, (char_u *)"DecMouse"},
2118 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
2119 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
2120 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
2121 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
2122 {K_LEFTDRAG, (char_u *)"LeftDrag"},
2123 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
2124 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
2125 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
2126 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
2127 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
2128 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
2129 {K_RIGHTDRAG, (char_u *)"RightDrag"},
2130 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
2131 {K_MOUSEDOWN, (char_u *)"MouseDown"},
2132 {K_MOUSEUP, (char_u *)"MouseUp"},
2133 {K_X1MOUSE, (char_u *)"X1Mouse"},
2134 {K_X1DRAG, (char_u *)"X1Drag"},
2135 {K_X1RELEASE, (char_u *)"X1Release"},
2136 {K_X2MOUSE, (char_u *)"X2Mouse"},
2137 {K_X2DRAG, (char_u *)"X2Drag"},
2138 {K_X2RELEASE, (char_u *)"X2Release"},
2139 {K_DROP, (char_u *)"Drop"},
2140 {K_ZERO, (char_u *)"Nul"},
2141#ifdef FEAT_EVAL
2142 {K_SNR, (char_u *)"SNR"},
2143#endif
2144 {K_PLUG, (char_u *)"Plug"},
2145 {0, NULL}
2146};
2147
2148#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
2149
2150#ifdef FEAT_MOUSE
2151static struct mousetable
2152{
2153 int pseudo_code; /* Code for pseudo mouse event */
2154 int button; /* Which mouse button is it? */
2155 int is_click; /* Is it a mouse button click event? */
2156 int is_drag; /* Is it a mouse drag event? */
2157} mouse_table[] =
2158{
2159 {(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
2160#ifdef FEAT_GUI
2161 {(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
2162#endif
2163 {(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
2164 {(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
2165#ifdef FEAT_GUI
2166 {(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
2167#endif
2168 {(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
2169 {(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
2170 {(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
2171 {(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
2172 {(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
2173 {(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
2174 {(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
2175 {(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
2176 {(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
2177 {(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
2178 {(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
2179 {(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
2180 /* DRAG without CLICK */
2181 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
2182 /* RELEASE without CLICK */
2183 {(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
2184 {0, 0, 0, 0},
2185};
2186#endif /* FEAT_MOUSE */
2187
2188/*
2189 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
2190 * modifier name ('S' for Shift, 'C' for Ctrl etc).
2191 */
2192 int
2193name_to_mod_mask(c)
2194 int c;
2195{
2196 int i;
2197
2198 c = TOUPPER_ASC(c);
2199 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
2200 if (c == mod_mask_table[i].name)
2201 return mod_mask_table[i].mod_flag;
2202 return 0;
2203}
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205/*
2206 * Check if if there is a special key code for "key" that includes the
2207 * modifiers specified.
2208 */
2209 int
2210simplify_key(key, modifiers)
2211 int key;
2212 int *modifiers;
2213{
2214 int i;
2215 int key0;
2216 int key1;
2217
2218 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
2219 {
2220 /* TAB is a special case */
2221 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
2222 {
2223 *modifiers &= ~MOD_MASK_SHIFT;
2224 return K_S_TAB;
2225 }
2226 key0 = KEY2TERMCAP0(key);
2227 key1 = KEY2TERMCAP1(key);
2228 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
2229 if (key0 == modifier_keys_table[i + 3]
2230 && key1 == modifier_keys_table[i + 4]
2231 && (*modifiers & modifier_keys_table[i]))
2232 {
2233 *modifiers &= ~modifier_keys_table[i];
2234 return TERMCAP2KEY(modifier_keys_table[i + 1],
2235 modifier_keys_table[i + 2]);
2236 }
2237 }
2238 return key;
2239}
2240
2241/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002242 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002243 */
2244 int
2245handle_x_keys(key)
2246 int key;
2247{
2248 switch (key)
2249 {
2250 case K_XUP: return K_UP;
2251 case K_XDOWN: return K_DOWN;
2252 case K_XLEFT: return K_LEFT;
2253 case K_XRIGHT: return K_RIGHT;
2254 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002255 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002256 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002257 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002258 case K_XF1: return K_F1;
2259 case K_XF2: return K_F2;
2260 case K_XF3: return K_F3;
2261 case K_XF4: return K_F4;
2262 case K_S_XF1: return K_S_F1;
2263 case K_S_XF2: return K_S_F2;
2264 case K_S_XF3: return K_S_F3;
2265 case K_S_XF4: return K_S_F4;
2266 }
2267 return key;
2268}
2269
2270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * Return a string which contains the name of the given key when the given
2272 * modifiers are down.
2273 */
2274 char_u *
2275get_special_key_name(c, modifiers)
2276 int c;
2277 int modifiers;
2278{
2279 static char_u string[MAX_KEY_NAME_LEN + 1];
2280
2281 int i, idx;
2282 int table_idx;
2283 char_u *s;
2284
2285 string[0] = '<';
2286 idx = 1;
2287
2288 /* Key that stands for a normal character. */
2289 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2290 c = KEY2TERMCAP1(c);
2291
2292 /*
2293 * Translate shifted special keys into unshifted keys and set modifier.
2294 * Same for CTRL and ALT modifiers.
2295 */
2296 if (IS_SPECIAL(c))
2297 {
2298 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2299 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2300 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2301 {
2302 modifiers |= modifier_keys_table[i];
2303 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2304 modifier_keys_table[i + 4]);
2305 break;
2306 }
2307 }
2308
2309 /* try to find the key in the special key table */
2310 table_idx = find_special_key_in_table(c);
2311
2312 /*
2313 * When not a known special key, and not a printable character, try to
2314 * extract modifiers.
2315 */
2316 if (c > 0
2317#ifdef FEAT_MBYTE
2318 && (*mb_char2len)(c) == 1
2319#endif
2320 )
2321 {
2322 if (table_idx < 0
2323 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2324 && (c & 0x80))
2325 {
2326 c &= 0x7f;
2327 modifiers |= MOD_MASK_ALT;
2328 /* try again, to find the un-alted key in the special key table */
2329 table_idx = find_special_key_in_table(c);
2330 }
2331 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2332 {
2333#ifdef EBCDIC
2334 c = CtrlChar(c);
2335#else
2336 c += '@';
2337#endif
2338 modifiers |= MOD_MASK_CTRL;
2339 }
2340 }
2341
2342 /* translate the modifier into a string */
2343 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2344 if ((modifiers & mod_mask_table[i].mod_mask)
2345 == mod_mask_table[i].mod_flag)
2346 {
2347 string[idx++] = mod_mask_table[i].name;
2348 string[idx++] = (char_u)'-';
2349 }
2350
2351 if (table_idx < 0) /* unknown special key, may output t_xx */
2352 {
2353 if (IS_SPECIAL(c))
2354 {
2355 string[idx++] = 't';
2356 string[idx++] = '_';
2357 string[idx++] = KEY2TERMCAP0(c);
2358 string[idx++] = KEY2TERMCAP1(c);
2359 }
2360 /* Not a special key, only modifiers, output directly */
2361 else
2362 {
2363#ifdef FEAT_MBYTE
2364 if (has_mbyte && (*mb_char2len)(c) > 1)
2365 idx += (*mb_char2bytes)(c, string + idx);
2366 else
2367#endif
2368 if (vim_isprintc(c))
2369 string[idx++] = c;
2370 else
2371 {
2372 s = transchar(c);
2373 while (*s)
2374 string[idx++] = *s++;
2375 }
2376 }
2377 }
2378 else /* use name of special key */
2379 {
2380 STRCPY(string + idx, key_names_table[table_idx].name);
2381 idx = (int)STRLEN(string);
2382 }
2383 string[idx++] = '>';
2384 string[idx] = NUL;
2385 return string;
2386}
2387
2388/*
2389 * Try translating a <> name at (*srcp)[] to dst[].
2390 * Return the number of characters added to dst[], zero for no match.
2391 * If there is a match, srcp is advanced to after the <> name.
2392 * dst[] must be big enough to hold the result (up to six characters)!
2393 */
2394 int
2395trans_special(srcp, dst, keycode)
2396 char_u **srcp;
2397 char_u *dst;
2398 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2399{
2400 int modifiers = 0;
2401 int key;
2402 int dlen = 0;
2403
2404 key = find_special_key(srcp, &modifiers, keycode);
2405 if (key == 0)
2406 return 0;
2407
2408 /* Put the appropriate modifier in a string */
2409 if (modifiers != 0)
2410 {
2411 dst[dlen++] = K_SPECIAL;
2412 dst[dlen++] = KS_MODIFIER;
2413 dst[dlen++] = modifiers;
2414 }
2415
2416 if (IS_SPECIAL(key))
2417 {
2418 dst[dlen++] = K_SPECIAL;
2419 dst[dlen++] = KEY2TERMCAP0(key);
2420 dst[dlen++] = KEY2TERMCAP1(key);
2421 }
2422#ifdef FEAT_MBYTE
2423 else if (has_mbyte && !keycode)
2424 dlen += (*mb_char2bytes)(key, dst + dlen);
2425#endif
2426 else if (keycode)
2427 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2428 else
2429 dst[dlen++] = key;
2430
2431 return dlen;
2432}
2433
2434/*
2435 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2436 * srcp is advanced to after the <> name.
2437 * returns 0 if there is no match.
2438 */
2439 int
2440find_special_key(srcp, modp, keycode)
2441 char_u **srcp;
2442 int *modp;
2443 int keycode; /* prefer key code, e.g. K_DEL instead of DEL */
2444{
2445 char_u *last_dash;
2446 char_u *end_of_name;
2447 char_u *src;
2448 char_u *bp;
2449 int modifiers;
2450 int bit;
2451 int key;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002452 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 src = *srcp;
2455 if (src[0] != '<')
2456 return 0;
2457
2458 /* Find end of modifier list */
2459 last_dash = src;
2460 for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
2461 {
2462 if (*bp == '-')
2463 {
2464 last_dash = bp;
2465 if (bp[1] != NUL && bp[2] == '>')
2466 ++bp; /* anything accepted, like <C-?> */
2467 }
2468 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
2469 bp += 3; /* skip t_xx, xx may be '-' or '>' */
2470 }
2471
2472 if (*bp == '>') /* found matching '>' */
2473 {
2474 end_of_name = bp + 1;
2475
2476 if (STRNICMP(src + 1, "char-", 5) == 0 && VIM_ISDIGIT(src[6]))
2477 {
2478 /* <Char-123> or <Char-033> or <Char-0x33> */
2479 vim_str2nr(src + 6, NULL, NULL, TRUE, TRUE, NULL, &n);
2480 *modp = 0;
2481 *srcp = end_of_name;
2482 return (int)n;
2483 }
2484
2485 /* Which modifiers are given? */
2486 modifiers = 0x0;
2487 for (bp = src + 1; bp < last_dash; bp++)
2488 {
2489 if (*bp != '-')
2490 {
2491 bit = name_to_mod_mask(*bp);
2492 if (bit == 0x0)
2493 break; /* Illegal modifier name */
2494 modifiers |= bit;
2495 }
2496 }
2497
2498 /*
2499 * Legal modifier name.
2500 */
2501 if (bp >= last_dash)
2502 {
2503 /*
2504 * Modifier with single letter, or special key name.
2505 */
2506 if (modifiers != 0 && last_dash[2] == '>')
2507 key = last_dash[1];
2508 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 key = get_special_key_code(last_dash + 1);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002511 key = handle_x_keys(key);
2512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514 /*
2515 * get_special_key_code() may return NUL for invalid
2516 * special key name.
2517 */
2518 if (key != NUL)
2519 {
2520 /*
2521 * Only use a modifier when there is no special key code that
2522 * includes the modifier.
2523 */
2524 key = simplify_key(key, &modifiers);
2525
2526 if (!keycode)
2527 {
2528 /* don't want keycode, use single byte code */
2529 if (key == K_BS)
2530 key = BS;
2531 else if (key == K_DEL || key == K_KDEL)
2532 key = DEL;
2533 }
2534
2535 /*
2536 * Normal Key with modifier: Try to make a single byte code.
2537 */
2538 if (!IS_SPECIAL(key))
2539 key = extract_modifiers(key, &modifiers);
2540
2541 *modp = modifiers;
2542 *srcp = end_of_name;
2543 return key;
2544 }
2545 }
2546 }
2547 return 0;
2548}
2549
2550/*
2551 * Try to include modifiers in the key.
2552 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
2553 */
2554 int
2555extract_modifiers(key, modp)
2556 int key;
2557 int *modp;
2558{
2559 int modifiers = *modp;
2560
2561#ifdef MACOS
2562 /* Command-key really special, No fancynest */
2563 if (!(modifiers & MOD_MASK_CMD))
2564#endif
2565 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2566 {
2567 key = TOUPPER_ASC(key);
2568 modifiers &= ~MOD_MASK_SHIFT;
2569 }
2570 if ((modifiers & MOD_MASK_CTRL)
2571#ifdef EBCDIC
2572 /* * TODO: EBCDIC Better use:
2573 * && (Ctrl_chr(key) || key == '?')
2574 * ??? */
2575 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2576 != NULL
2577#else
2578 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2579#endif
2580 )
2581 {
2582 key = Ctrl_chr(key);
2583 modifiers &= ~MOD_MASK_CTRL;
2584 /* <C-@> is <Nul> */
2585 if (key == 0)
2586 key = K_ZERO;
2587 }
2588#ifdef MACOS
2589 /* Command-key really special, No fancynest */
2590 if (!(modifiers & MOD_MASK_CMD))
2591#endif
2592 if ((modifiers & MOD_MASK_ALT) && key < 0x80
2593#ifdef FEAT_MBYTE
2594 && !enc_dbcs /* avoid creating a lead byte */
2595#endif
2596 )
2597 {
2598 key |= 0x80;
2599 modifiers &= ~MOD_MASK_ALT; /* remove the META modifier */
2600 }
2601
2602 *modp = modifiers;
2603 return key;
2604}
2605
2606/*
2607 * Try to find key "c" in the special key table.
2608 * Return the index when found, -1 when not found.
2609 */
2610 int
2611find_special_key_in_table(c)
2612 int c;
2613{
2614 int i;
2615
2616 for (i = 0; key_names_table[i].name != NULL; i++)
2617 if (c == key_names_table[i].key)
2618 break;
2619 if (key_names_table[i].name == NULL)
2620 i = -1;
2621 return i;
2622}
2623
2624/*
2625 * Find the special key with the given name (the given string does not have to
2626 * end with NUL, the name is assumed to end before the first non-idchar).
2627 * If the name starts with "t_" the next two characters are interpreted as a
2628 * termcap name.
2629 * Return the key code, or 0 if not found.
2630 */
2631 int
2632get_special_key_code(name)
2633 char_u *name;
2634{
2635 char_u *table_name;
2636 char_u string[3];
2637 int i, j;
2638
2639 /*
2640 * If it's <t_xx> we get the code for xx from the termcap
2641 */
2642 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
2643 {
2644 string[0] = name[2];
2645 string[1] = name[3];
2646 string[2] = NUL;
2647 if (add_termcap_entry(string, FALSE) == OK)
2648 return TERMCAP2KEY(name[2], name[3]);
2649 }
2650 else
2651 for (i = 0; key_names_table[i].name != NULL; i++)
2652 {
2653 table_name = key_names_table[i].name;
2654 for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
2655 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
2656 break;
2657 if (!vim_isIDc(name[j]) && table_name[j] == NUL)
2658 return key_names_table[i].key;
2659 }
2660 return 0;
2661}
2662
2663#ifdef FEAT_CMDL_COMPL
2664 char_u *
2665get_key_name(i)
2666 int i;
2667{
2668 if (i >= KEY_NAMES_TABLE_LEN)
2669 return NULL;
2670 return key_names_table[i].name;
2671}
2672#endif
2673
2674#ifdef FEAT_MOUSE
2675/*
2676 * Look up the given mouse code to return the relevant information in the other
2677 * arguments. Return which button is down or was released.
2678 */
2679 int
2680get_mouse_button(code, is_click, is_drag)
2681 int code;
2682 int *is_click;
2683 int *is_drag;
2684{
2685 int i;
2686
2687 for (i = 0; mouse_table[i].pseudo_code; i++)
2688 if (code == mouse_table[i].pseudo_code)
2689 {
2690 *is_click = mouse_table[i].is_click;
2691 *is_drag = mouse_table[i].is_drag;
2692 return mouse_table[i].button;
2693 }
2694 return 0; /* Shouldn't get here */
2695}
2696
2697/*
2698 * Return the appropriate pseudo mouse event token (KE_LEFTMOUSE etc) based on
2699 * the given information about which mouse button is down, and whether the
2700 * mouse was clicked, dragged or released.
2701 */
2702 int
2703get_pseudo_mouse_code(button, is_click, is_drag)
2704 int button; /* eg MOUSE_LEFT */
2705 int is_click;
2706 int is_drag;
2707{
2708 int i;
2709
2710 for (i = 0; mouse_table[i].pseudo_code; i++)
2711 if (button == mouse_table[i].button
2712 && is_click == mouse_table[i].is_click
2713 && is_drag == mouse_table[i].is_drag)
2714 {
2715#ifdef FEAT_GUI
Bram Moolenaarc91506a2005-04-24 22:04:21 +00002716 /* Trick: a non mappable left click and release has mouse_col -1
2717 * or added MOUSE_COLOFF. Used for 'mousefocus' in
2718 * gui_mouse_moved() */
2719 if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
Bram Moolenaarc91506a2005-04-24 22:04:21 +00002721 if (mouse_col < 0)
2722 mouse_col = 0;
2723 else
2724 mouse_col -= MOUSE_COLOFF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
2726 return (int)KE_LEFTMOUSE_NM;
2727 if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
2728 return (int)KE_LEFTRELEASE_NM;
2729 }
2730#endif
2731 return mouse_table[i].pseudo_code;
2732 }
2733 return (int)KE_IGNORE; /* not recongnized, ignore it */
2734}
2735#endif /* FEAT_MOUSE */
2736
2737/*
2738 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
2739 */
2740 int
2741get_fileformat(buf)
2742 buf_T *buf;
2743{
2744 int c = *buf->b_p_ff;
2745
2746 if (buf->b_p_bin || c == 'u')
2747 return EOL_UNIX;
2748 if (c == 'm')
2749 return EOL_MAC;
2750 return EOL_DOS;
2751}
2752
2753/*
2754 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
2755 * argument.
2756 */
2757 int
2758get_fileformat_force(buf, eap)
2759 buf_T *buf;
2760 exarg_T *eap; /* can be NULL! */
2761{
2762 int c;
2763
2764 if (eap != NULL && eap->force_ff != 0)
2765 c = eap->cmd[eap->force_ff];
2766 else
2767 {
2768 if ((eap != NULL && eap->force_bin != 0)
2769 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
2770 return EOL_UNIX;
2771 c = *buf->b_p_ff;
2772 }
2773 if (c == 'u')
2774 return EOL_UNIX;
2775 if (c == 'm')
2776 return EOL_MAC;
2777 return EOL_DOS;
2778}
2779
2780/*
2781 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
2782 * Sets both 'textmode' and 'fileformat'.
2783 * Note: Does _not_ set global value of 'textmode'!
2784 */
2785 void
2786set_fileformat(t, opt_flags)
2787 int t;
2788 int opt_flags; /* OPT_LOCAL and/or OPT_GLOBAL */
2789{
2790 char *p = NULL;
2791
2792 switch (t)
2793 {
2794 case EOL_DOS:
2795 p = FF_DOS;
2796 curbuf->b_p_tx = TRUE;
2797 break;
2798 case EOL_UNIX:
2799 p = FF_UNIX;
2800 curbuf->b_p_tx = FALSE;
2801 break;
2802 case EOL_MAC:
2803 p = FF_MAC;
2804 curbuf->b_p_tx = FALSE;
2805 break;
2806 }
2807 if (p != NULL)
2808 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002809 OPT_FREE | opt_flags, 0);
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00002812 /* This may cause the buffer to become (un)modified. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002814 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
2816#ifdef FEAT_TITLE
2817 need_maketitle = TRUE; /* set window title later */
2818#endif
2819}
2820
2821/*
2822 * Return the default fileformat from 'fileformats'.
2823 */
2824 int
2825default_fileformat()
2826{
2827 switch (*p_ffs)
2828 {
2829 case 'm': return EOL_MAC;
2830 case 'd': return EOL_DOS;
2831 }
2832 return EOL_UNIX;
2833}
2834
2835/*
2836 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
2837 */
2838 int
2839call_shell(cmd, opt)
2840 char_u *cmd;
2841 int opt;
2842{
2843 char_u *ncmd;
2844 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002845#ifdef FEAT_PROFILE
2846 proftime_T wait_time;
2847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
2849 if (p_verbose > 3)
2850 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002851 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00002852 smsg((char_u *)_("Calling shell to execute: \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 cmd == NULL ? p_sh : cmd);
2854 out_char('\n');
2855 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002856 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858
Bram Moolenaar05159a02005-02-26 23:04:13 +00002859#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002860 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002861 prof_child_enter(&wait_time);
2862#endif
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (*p_sh == NUL)
2865 {
2866 EMSG(_(e_shellempty));
2867 retval = -1;
2868 }
2869 else
2870 {
2871#ifdef FEAT_GUI_MSWIN
2872 /* Don't hide the pointer while executing a shell command. */
2873 gui_mch_mousehide(FALSE);
2874#endif
2875#ifdef FEAT_GUI
2876 ++hold_gui_events;
2877#endif
2878 /* The external command may update a tags file, clear cached tags. */
2879 tag_freematch();
2880
2881 if (cmd == NULL || *p_sxq == NUL)
2882 retval = mch_call_shell(cmd, opt);
2883 else
2884 {
2885 ncmd = alloc((unsigned)(STRLEN(cmd) + STRLEN(p_sxq) * 2 + 1));
2886 if (ncmd != NULL)
2887 {
2888 STRCPY(ncmd, p_sxq);
2889 STRCAT(ncmd, cmd);
2890 STRCAT(ncmd, p_sxq);
2891 retval = mch_call_shell(ncmd, opt);
2892 vim_free(ncmd);
2893 }
2894 else
2895 retval = -1;
2896 }
2897#ifdef FEAT_GUI
2898 --hold_gui_events;
2899#endif
2900 /*
2901 * Check the window size, in case it changed while executing the
2902 * external command.
2903 */
2904 shell_resized_check();
2905 }
2906
2907#ifdef FEAT_EVAL
2908 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002909# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002910 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002911 prof_child_exit(&wait_time);
2912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913#endif
2914
2915 return retval;
2916}
2917
2918/*
Bram Moolenaar01265852006-03-20 21:50:15 +00002919 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
2920 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 */
2922 int
2923get_real_state()
2924{
2925 if (State & NORMAL)
2926 {
2927#ifdef FEAT_VISUAL
2928 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00002929 {
2930 if (VIsual_select)
2931 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00002933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 else
2935#endif
2936 if (finish_op)
Bram Moolenaar01265852006-03-20 21:50:15 +00002937 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
2939 return State;
2940}
2941
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002942#if defined(FEAT_MBYTE) || defined(PROTO)
2943/*
2944 * Return TRUE if "p" points to just after a path separator.
2945 * Take care of multi-byte characters.
2946 * "b" must point to the start of the file name
2947 */
2948 int
2949after_pathsep(b, p)
2950 char_u *b;
2951 char_u *p;
2952{
2953 return vim_ispathsep(p[-1])
2954 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
2955}
2956#endif
2957
2958/*
2959 * Return TRUE if file names "f1" and "f2" are in the same directory.
2960 * "f1" may be a short name, "f2" must be a full path.
2961 */
2962 int
2963same_directory(f1, f2)
2964 char_u *f1;
2965 char_u *f2;
2966{
2967 char_u ffname[MAXPATHL];
2968 char_u *t1;
2969 char_u *t2;
2970
2971 /* safety check */
2972 if (f1 == NULL || f2 == NULL)
2973 return FALSE;
2974
2975 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
2976 t1 = gettail_sep(ffname);
2977 t2 = gettail_sep(f2);
2978 return (t1 - ffname == t2 - f2
2979 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
2980}
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00002983 || ((defined(FEAT_GUI_GTK)) \
Bram Moolenaar843ee412004-06-30 16:16:41 +00002984 && ( defined(FEAT_WINDOWS) || defined(FEAT_DND)) ) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 || defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
2986 || defined(PROTO)
2987/*
2988 * Change to a file's directory.
2989 * Caller must call shorten_fnames()!
2990 * Return OK or FAIL.
2991 */
2992 int
2993vim_chdirfile(fname)
2994 char_u *fname;
2995{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002996 char_u dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002998 vim_strncpy(dir, fname, MAXPATHL - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002999 *gettail_sep(dir) = NUL;
3000 return mch_chdir((char *)dir) == 0 ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002#endif
3003
3004#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
3005/*
3006 * Check if "name" ends in a slash and is not a directory.
3007 * Used for systems where stat() ignores a trailing slash on a file name.
3008 * The Vim code assumes a trailing slash is only ignored for a directory.
3009 */
3010 int
3011illegal_slash(name)
3012 char *name;
3013{
3014 if (name[0] == NUL)
3015 return FALSE; /* no file name is not illegal */
3016 if (name[strlen(name) - 1] != '/')
3017 return FALSE; /* no trailing slash */
3018 if (mch_isdir((char_u *)name))
3019 return FALSE; /* trailing slash for a directory */
3020 return TRUE;
3021}
3022#endif
3023
3024#if defined(CURSOR_SHAPE) || defined(PROTO)
3025
3026/*
3027 * Handling of cursor and mouse pointer shapes in various modes.
3028 */
3029
3030cursorentry_T shape_table[SHAPE_IDX_COUNT] =
3031{
3032 /* The values will be filled in from the 'guicursor' and 'mouseshape'
3033 * defaults when Vim starts.
3034 * Adjust the SHAPE_IDX_ defines when making changes! */
3035 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
3036 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
3037 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
3038 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
3039 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
3040 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
3041 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
3042 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
3043 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
3044 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
3045 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
3046 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
3047 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
3048 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
3049 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
3050 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
3051 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
3052};
3053
3054#ifdef FEAT_MOUSESHAPE
3055/*
3056 * Table with names for mouse shapes. Keep in sync with all the tables for
3057 * mch_set_mouse_shape()!.
3058 */
3059static char * mshape_names[] =
3060{
3061 "arrow", /* default, must be the first one */
3062 "blank", /* hidden */
3063 "beam",
3064 "updown",
3065 "udsizing",
3066 "leftright",
3067 "lrsizing",
3068 "busy",
3069 "no",
3070 "crosshair",
3071 "hand1",
3072 "hand2",
3073 "pencil",
3074 "question",
3075 "rightup-arrow",
3076 "up-arrow",
3077 NULL
3078};
3079#endif
3080
3081/*
3082 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
3083 * ("what" is SHAPE_MOUSE).
3084 * Returns error message for an illegal option, NULL otherwise.
3085 */
3086 char_u *
3087parse_shape_opt(what)
3088 int what;
3089{
3090 char_u *modep;
3091 char_u *colonp;
3092 char_u *commap;
3093 char_u *slashp;
3094 char_u *p, *endp;
3095 int idx = 0; /* init for GCC */
3096 int all_idx;
3097 int len;
3098 int i;
3099 long n;
3100 int found_ve = FALSE; /* found "ve" flag */
3101 int round;
3102
3103 /*
3104 * First round: check for errors; second round: do it for real.
3105 */
3106 for (round = 1; round <= 2; ++round)
3107 {
3108 /*
3109 * Repeat for all comma separated parts.
3110 */
3111#ifdef FEAT_MOUSESHAPE
3112 if (what == SHAPE_MOUSE)
3113 modep = p_mouseshape;
3114 else
3115#endif
3116 modep = p_guicursor;
3117 while (*modep != NUL)
3118 {
3119 colonp = vim_strchr(modep, ':');
3120 if (colonp == NULL)
3121 return (char_u *)N_("E545: Missing colon");
3122 if (colonp == modep)
3123 return (char_u *)N_("E546: Illegal mode");
3124 commap = vim_strchr(modep, ',');
3125
3126 /*
3127 * Repeat for all mode's before the colon.
3128 * For the 'a' mode, we loop to handle all the modes.
3129 */
3130 all_idx = -1;
3131 while (modep < colonp || all_idx >= 0)
3132 {
3133 if (all_idx < 0)
3134 {
3135 /* Find the mode. */
3136 if (modep[1] == '-' || modep[1] == ':')
3137 len = 1;
3138 else
3139 len = 2;
3140 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
3141 all_idx = SHAPE_IDX_COUNT - 1;
3142 else
3143 {
3144 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
3145 if (STRNICMP(modep, shape_table[idx].name, len)
3146 == 0)
3147 break;
3148 if (idx == SHAPE_IDX_COUNT
3149 || (shape_table[idx].used_for & what) == 0)
3150 return (char_u *)N_("E546: Illegal mode");
3151 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
3152 found_ve = TRUE;
3153 }
3154 modep += len + 1;
3155 }
3156
3157 if (all_idx >= 0)
3158 idx = all_idx--;
3159 else if (round == 2)
3160 {
3161#ifdef FEAT_MOUSESHAPE
3162 if (what == SHAPE_MOUSE)
3163 {
3164 /* Set the default, for the missing parts */
3165 shape_table[idx].mshape = 0;
3166 }
3167 else
3168#endif
3169 {
3170 /* Set the defaults, for the missing parts */
3171 shape_table[idx].shape = SHAPE_BLOCK;
3172 shape_table[idx].blinkwait = 700L;
3173 shape_table[idx].blinkon = 400L;
3174 shape_table[idx].blinkoff = 250L;
3175 }
3176 }
3177
3178 /* Parse the part after the colon */
3179 for (p = colonp + 1; *p && *p != ','; )
3180 {
3181#ifdef FEAT_MOUSESHAPE
3182 if (what == SHAPE_MOUSE)
3183 {
3184 for (i = 0; ; ++i)
3185 {
3186 if (mshape_names[i] == NULL)
3187 {
3188 if (!VIM_ISDIGIT(*p))
3189 return (char_u *)N_("E547: Illegal mouseshape");
3190 if (round == 2)
3191 shape_table[idx].mshape =
3192 getdigits(&p) + MSHAPE_NUMBERED;
3193 else
3194 (void)getdigits(&p);
3195 break;
3196 }
3197 len = (int)STRLEN(mshape_names[i]);
3198 if (STRNICMP(p, mshape_names[i], len) == 0)
3199 {
3200 if (round == 2)
3201 shape_table[idx].mshape = i;
3202 p += len;
3203 break;
3204 }
3205 }
3206 }
3207 else /* if (what == SHAPE_MOUSE) */
3208#endif
3209 {
3210 /*
3211 * First handle the ones with a number argument.
3212 */
3213 i = *p;
3214 len = 0;
3215 if (STRNICMP(p, "ver", 3) == 0)
3216 len = 3;
3217 else if (STRNICMP(p, "hor", 3) == 0)
3218 len = 3;
3219 else if (STRNICMP(p, "blinkwait", 9) == 0)
3220 len = 9;
3221 else if (STRNICMP(p, "blinkon", 7) == 0)
3222 len = 7;
3223 else if (STRNICMP(p, "blinkoff", 8) == 0)
3224 len = 8;
3225 if (len != 0)
3226 {
3227 p += len;
3228 if (!VIM_ISDIGIT(*p))
3229 return (char_u *)N_("E548: digit expected");
3230 n = getdigits(&p);
3231 if (len == 3) /* "ver" or "hor" */
3232 {
3233 if (n == 0)
3234 return (char_u *)N_("E549: Illegal percentage");
3235 if (round == 2)
3236 {
3237 if (TOLOWER_ASC(i) == 'v')
3238 shape_table[idx].shape = SHAPE_VER;
3239 else
3240 shape_table[idx].shape = SHAPE_HOR;
3241 shape_table[idx].percentage = n;
3242 }
3243 }
3244 else if (round == 2)
3245 {
3246 if (len == 9)
3247 shape_table[idx].blinkwait = n;
3248 else if (len == 7)
3249 shape_table[idx].blinkon = n;
3250 else
3251 shape_table[idx].blinkoff = n;
3252 }
3253 }
3254 else if (STRNICMP(p, "block", 5) == 0)
3255 {
3256 if (round == 2)
3257 shape_table[idx].shape = SHAPE_BLOCK;
3258 p += 5;
3259 }
3260 else /* must be a highlight group name then */
3261 {
3262 endp = vim_strchr(p, '-');
3263 if (commap == NULL) /* last part */
3264 {
3265 if (endp == NULL)
3266 endp = p + STRLEN(p); /* find end of part */
3267 }
3268 else if (endp > commap || endp == NULL)
3269 endp = commap;
3270 slashp = vim_strchr(p, '/');
3271 if (slashp != NULL && slashp < endp)
3272 {
3273 /* "group/langmap_group" */
3274 i = syn_check_group(p, (int)(slashp - p));
3275 p = slashp + 1;
3276 }
3277 if (round == 2)
3278 {
3279 shape_table[idx].id = syn_check_group(p,
3280 (int)(endp - p));
3281 shape_table[idx].id_lm = shape_table[idx].id;
3282 if (slashp != NULL && slashp < endp)
3283 shape_table[idx].id = i;
3284 }
3285 p = endp;
3286 }
3287 } /* if (what != SHAPE_MOUSE) */
3288
3289 if (*p == '-')
3290 ++p;
3291 }
3292 }
3293 modep = p;
3294 if (*modep == ',')
3295 ++modep;
3296 }
3297 }
3298
3299 /* If the 's' flag is not given, use the 'v' cursor for 's' */
3300 if (!found_ve)
3301 {
3302#ifdef FEAT_MOUSESHAPE
3303 if (what == SHAPE_MOUSE)
3304 {
3305 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3306 }
3307 else
3308#endif
3309 {
3310 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3311 shape_table[SHAPE_IDX_VE].percentage =
3312 shape_table[SHAPE_IDX_V].percentage;
3313 shape_table[SHAPE_IDX_VE].blinkwait =
3314 shape_table[SHAPE_IDX_V].blinkwait;
3315 shape_table[SHAPE_IDX_VE].blinkon =
3316 shape_table[SHAPE_IDX_V].blinkon;
3317 shape_table[SHAPE_IDX_VE].blinkoff =
3318 shape_table[SHAPE_IDX_V].blinkoff;
3319 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3320 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3321 }
3322 }
3323
3324 return NULL;
3325}
3326
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003327# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3328 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329/*
3330 * Return the index into shape_table[] for the current mode.
3331 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3332 */
3333 int
3334get_shape_idx(mouse)
3335 int mouse;
3336{
3337#ifdef FEAT_MOUSESHAPE
3338 if (mouse && (State == HITRETURN || State == ASKMORE))
3339 {
3340# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003341 int x, y;
3342 gui_mch_getmouse(&x, &y);
3343 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 return SHAPE_IDX_MOREL;
3345# endif
3346 return SHAPE_IDX_MORE;
3347 }
3348 if (mouse && drag_status_line)
3349 return SHAPE_IDX_SDRAG;
3350# ifdef FEAT_VERTSPLIT
3351 if (mouse && drag_sep_line)
3352 return SHAPE_IDX_VDRAG;
3353# endif
3354#endif
3355 if (!mouse && State == SHOWMATCH)
3356 return SHAPE_IDX_SM;
3357#ifdef FEAT_VREPLACE
3358 if (State & VREPLACE_FLAG)
3359 return SHAPE_IDX_R;
3360#endif
3361 if (State & REPLACE_FLAG)
3362 return SHAPE_IDX_R;
3363 if (State & INSERT)
3364 return SHAPE_IDX_I;
3365 if (State & CMDLINE)
3366 {
3367 if (cmdline_at_end())
3368 return SHAPE_IDX_C;
3369 if (cmdline_overstrike())
3370 return SHAPE_IDX_CR;
3371 return SHAPE_IDX_CI;
3372 }
3373 if (finish_op)
3374 return SHAPE_IDX_O;
3375#ifdef FEAT_VISUAL
3376 if (VIsual_active)
3377 {
3378 if (*p_sel == 'e')
3379 return SHAPE_IDX_VE;
3380 else
3381 return SHAPE_IDX_V;
3382 }
3383#endif
3384 return SHAPE_IDX_N;
3385}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3389static int old_mouse_shape = 0;
3390
3391/*
3392 * Set the mouse shape:
3393 * If "shape" is -1, use shape depending on the current mode,
3394 * depending on the current state.
3395 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3396 * when the mouse moves off the status or command line).
3397 */
3398 void
3399update_mouseshape(shape_idx)
3400 int shape_idx;
3401{
3402 int new_mouse_shape;
3403
3404 /* Only works in GUI mode. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003405 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return;
3407
3408 /* Postpone the updating when more is to come. Speeds up executing of
3409 * mappings. */
3410 if (shape_idx == -1 && char_avail())
3411 {
3412 postponed_mouseshape = TRUE;
3413 return;
3414 }
3415
Bram Moolenaar14716812006-05-04 21:54:08 +00003416 /* When ignoring the mouse don't change shape on the statusline. */
3417 if (*p_mouse == NUL
3418 && (shape_idx == SHAPE_IDX_CLINE
3419 || shape_idx == SHAPE_IDX_STATUS
3420 || shape_idx == SHAPE_IDX_VSEP))
3421 shape_idx = -2;
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (shape_idx == -2
3424 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3425 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3426 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3427 return;
3428 if (shape_idx < 0)
3429 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3430 else
3431 new_mouse_shape = shape_table[shape_idx].mshape;
3432 if (new_mouse_shape != old_mouse_shape)
3433 {
3434 mch_set_mouse_shape(new_mouse_shape);
3435 old_mouse_shape = new_mouse_shape;
3436 }
3437 postponed_mouseshape = FALSE;
3438}
3439# endif
3440
3441#endif /* CURSOR_SHAPE */
3442
3443
3444#ifdef FEAT_CRYPT
3445/*
3446 * Optional encryption suypport.
3447 * Mohsin Ahmed, mosh@sasi.com, 98-09-24
3448 * Based on zip/crypt sources.
3449 *
3450 * NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to
3451 * most countries. There are a few exceptions, but that still should not be a
3452 * problem since this code was originally created in Europe and India.
3453 */
3454
3455/* from zip.h */
3456
3457typedef unsigned short ush; /* unsigned 16-bit value */
3458typedef unsigned long ulg; /* unsigned 32-bit value */
3459
3460static void make_crc_tab __ARGS((void));
3461
Bram Moolenaard6f676d2005-06-01 21:51:55 +00003462static ulg crc_32_tab[256];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464/*
3465 * Fill the CRC table.
3466 */
3467 static void
3468make_crc_tab()
3469{
3470 ulg s,t,v;
3471 static int done = FALSE;
3472
3473 if (done)
3474 return;
3475 for (t = 0; t < 256; t++)
3476 {
3477 v = t;
3478 for (s = 0; s < 8; s++)
3479 v = (v >> 1) ^ ((v & 1) * (ulg)0xedb88320L);
3480 crc_32_tab[t] = v;
3481 }
3482 done = TRUE;
3483}
3484
3485#define CRC32(c, b) (crc_32_tab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
3486
3487
3488static ulg keys[3]; /* keys defining the pseudo-random sequence */
3489
3490/*
3491 * Return the next byte in the pseudo-random sequence
3492 */
3493 int
3494decrypt_byte()
3495{
3496 ush temp;
3497
3498 temp = (ush)keys[2] | 2;
3499 return (int)(((unsigned)(temp * (temp ^ 1)) >> 8) & 0xff);
3500}
3501
3502/*
3503 * Update the encryption keys with the next byte of plain text
3504 */
3505 int
3506update_keys(c)
3507 int c; /* byte of plain text */
3508{
3509 keys[0] = CRC32(keys[0], c);
3510 keys[1] += keys[0] & 0xff;
3511 keys[1] = keys[1] * 134775813L + 1;
3512 keys[2] = CRC32(keys[2], (int)(keys[1] >> 24));
3513 return c;
3514}
3515
3516/*
3517 * Initialize the encryption keys and the random header according to
3518 * the given password.
3519 * If "passwd" is NULL or empty, don't do anything.
3520 */
3521 void
3522crypt_init_keys(passwd)
3523 char_u *passwd; /* password string with which to modify keys */
3524{
3525 if (passwd != NULL && *passwd != NUL)
3526 {
3527 make_crc_tab();
3528 keys[0] = 305419896L;
3529 keys[1] = 591751049L;
3530 keys[2] = 878082192L;
3531 while (*passwd != '\0')
3532 update_keys((int)*passwd++);
3533 }
3534}
3535
3536/*
3537 * Ask the user for a crypt key.
3538 * When "store" is TRUE, the new key in stored in the 'key' option, and the
3539 * 'key' option value is returned: Don't free it.
3540 * When "store" is FALSE, the typed key is returned in allocated memory.
3541 * Returns NULL on failure.
3542 */
3543 char_u *
3544get_crypt_key(store, twice)
3545 int store;
3546 int twice; /* Ask for the key twice. */
3547{
3548 char_u *p1, *p2 = NULL;
3549 int round;
3550
3551 for (round = 0; ; ++round)
3552 {
3553 cmdline_star = TRUE;
3554 cmdline_row = msg_row;
3555 p1 = getcmdline_prompt(NUL, round == 0
3556 ? (char_u *)_("Enter encryption key: ")
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003557 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING,
3558 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 cmdline_star = FALSE;
3560
3561 if (p1 == NULL)
3562 break;
3563
3564 if (round == twice)
3565 {
3566 if (p2 != NULL && STRCMP(p1, p2) != 0)
3567 {
3568 MSG(_("Keys don't match!"));
3569 vim_free(p1);
3570 vim_free(p2);
3571 p2 = NULL;
3572 round = -1; /* do it again */
3573 continue;
3574 }
3575 if (store)
3576 {
3577 set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL);
3578 vim_free(p1);
3579 p1 = curbuf->b_p_key;
3580 }
3581 break;
3582 }
3583 p2 = p1;
3584 }
3585
3586 /* since the user typed this, no need to wait for return */
3587 need_wait_return = FALSE;
3588 msg_didout = FALSE;
3589
3590 vim_free(p2);
3591 return p1;
3592}
3593
3594#endif /* FEAT_CRYPT */
3595
3596/* TODO: make some #ifdef for this */
3597/*--------[ file searching ]-------------------------------------------------*/
3598/*
3599 * File searching functions for 'path', 'tags' and 'cdpath' options.
3600 * External visible functions:
3601 * vim_findfile_init() creates/initialises the search context
3602 * vim_findfile_free_visited() free list of visited files/dirs of search
3603 * context
3604 * vim_findfile() find a file in the search context
3605 * vim_findfile_cleanup() cleanup/free search context created by
3606 * vim_findfile_init()
3607 *
3608 * All static functions and variables start with 'ff_'
3609 *
3610 * In general it works like this:
3611 * First you create yourself a search context by calling vim_findfile_init().
3612 * It is possible to give a search context from a previous call to
3613 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3614 * until you are satisfied with the result or it returns NULL. On every call it
3615 * returns the next file which matches the conditions given to
3616 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3617 *
3618 * It is possible to call vim_findfile_init() again to reinitialise your search
3619 * with some new parameters. Don't forget to pass your old search context to
3620 * it, so it can reuse it and especially reuse the list of already visited
3621 * directories. If you want to delete the list of already visited directories
3622 * simply call vim_findfile_free_visited().
3623 *
3624 * When you are done call vim_findfile_cleanup() to free the search context.
3625 *
3626 * The function vim_findfile_init() has a long comment, which describes the
3627 * needed parameters.
3628 *
3629 *
3630 *
3631 * ATTENTION:
3632 * ==========
3633 * Also we use an allocated search context here, this functions ARE NOT
3634 * thread-safe!!!!!
3635 *
3636 * To minimize parameter passing (or because I'm to lazy), only the
3637 * external visible functions get a search context as a parameter. This is
3638 * then assigned to a static global, which is used throughout the local
3639 * functions.
3640 */
3641
3642/*
3643 * type for the directory search stack
3644 */
3645typedef struct ff_stack
3646{
3647 struct ff_stack *ffs_prev;
3648
3649 /* the fix part (no wildcards) and the part containing the wildcards
3650 * of the search path
3651 */
3652 char_u *ffs_fix_path;
3653#ifdef FEAT_PATH_EXTRA
3654 char_u *ffs_wc_path;
3655#endif
3656
3657 /* files/dirs found in the above directory, matched by the first wildcard
3658 * of wc_part
3659 */
3660 char_u **ffs_filearray;
3661 int ffs_filearray_size;
3662 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3663
3664 /* to store status of partly handled directories
3665 * 0: we work the on this directory for the first time
3666 * 1: this directory was partly searched in an earlier step
3667 */
3668 int ffs_stage;
3669
3670 /* How deep are we in the directory tree?
3671 * Counts backward from value of level parameter to vim_findfile_init
3672 */
3673 int ffs_level;
3674
3675 /* Did we already expand '**' to an empty string? */
3676 int ffs_star_star_empty;
3677} ff_stack_T;
3678
3679/*
3680 * type for already visited directories or files.
3681 */
3682typedef struct ff_visited
3683{
3684 struct ff_visited *ffv_next;
3685
3686#ifdef FEAT_PATH_EXTRA
3687 /* Visited directories are different if the wildcard string are
3688 * different. So we have to save it.
3689 */
3690 char_u *ffv_wc_path;
3691#endif
3692 /* for unix use inode etc for comparison (needed because of links), else
3693 * use filename.
3694 */
3695#ifdef UNIX
3696 int ffv_dev; /* device number (-1 if not set) */
3697 ino_t ffv_ino; /* inode number */
3698#endif
3699 /* The memory for this struct is allocated according to the length of
3700 * ffv_fname.
3701 */
3702 char_u ffv_fname[1]; /* actually longer */
3703} ff_visited_T;
3704
3705/*
3706 * We might have to manage several visited lists during a search.
3707 * This is expecially needed for the tags option. If tags is set to:
3708 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3709 * So we have to do 3 searches:
3710 * 1) search from the current files directory downward for the file "tags"
3711 * 2) search from the current files directory downward for the file "TAGS"
3712 * 3) search from Vims current directory downwards for the file "tags"
3713 * As you can see, the first and the third search are for the same file, so for
3714 * the third search we can use the visited list of the first search. For the
3715 * second search we must start from a empty visited list.
3716 * The struct ff_visited_list_hdr is used to manage a linked list of already
3717 * visited lists.
3718 */
3719typedef struct ff_visited_list_hdr
3720{
3721 struct ff_visited_list_hdr *ffvl_next;
3722
3723 /* the filename the attached visited list is for */
3724 char_u *ffvl_filename;
3725
3726 ff_visited_T *ffvl_visited_list;
3727
3728} ff_visited_list_hdr_T;
3729
3730
3731/*
3732 * '**' can be expanded to several directory levels.
3733 * Set the default maximium depth.
3734 */
3735#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
3736/*
3737 * The search context:
3738 * ffsc_stack_ptr: the stack for the dirs to search
3739 * ffsc_visited_list: the currently active visited list
3740 * ffsc_dir_visited_list: the currently active visited list for search dirs
3741 * ffsc_visited_lists_list: the list of all visited lists
3742 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3743 * ffsc_file_to_search: the file to search for
3744 * ffsc_start_dir: the starting directory, if search path was relative
3745 * ffsc_fix_path: the fix part of the given path (without wildcards)
3746 * Needed for upward search.
3747 * ffsc_wc_path: the part of the given path containing wildcards
3748 * ffsc_level: how many levels of dirs to search downwards
3749 * ffsc_stopdirs_v: array of stop directories for upward search
3750 * ffsc_need_dir: TRUE if we search for a directory
3751 */
3752typedef struct ff_search_ctx_T
3753{
3754 ff_stack_T *ffsc_stack_ptr;
3755 ff_visited_list_hdr_T *ffsc_visited_list;
3756 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3757 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3758 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3759 char_u *ffsc_file_to_search;
3760 char_u *ffsc_start_dir;
3761 char_u *ffsc_fix_path;
3762#ifdef FEAT_PATH_EXTRA
3763 char_u *ffsc_wc_path;
3764 int ffsc_level;
3765 char_u **ffsc_stopdirs_v;
3766#endif
3767 int ffsc_need_dir;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003768} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003770static ff_search_ctx_T *ff_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772/* locally needed functions */
3773#ifdef FEAT_PATH_EXTRA
3774static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *));
3775#else
3776static int ff_check_visited __ARGS((ff_visited_T **, char_u *));
3777#endif
3778static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp));
3779static void ff_free_visited_list __ARGS((ff_visited_T *vl));
3780static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp));
3781#ifdef FEAT_PATH_EXTRA
3782static int ff_wc_equal __ARGS((char_u *s1, char_u *s2));
3783#endif
3784
3785static void ff_push __ARGS((ff_stack_T *));
3786static ff_stack_T * ff_pop __ARGS((void));
3787static void ff_clear __ARGS((void));
3788static void ff_free_stack_element __ARGS((ff_stack_T *));
3789#ifdef FEAT_PATH_EXTRA
3790static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int));
3791#else
3792static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int));
3793#endif
3794#ifdef FEAT_PATH_EXTRA
3795static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **));
3796#endif
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798#if 0
3799/*
3800 * if someone likes findfirst/findnext, here are the functions
3801 * NOT TESTED!!
3802 */
3803
3804static void *ff_fn_search_context = NULL;
3805
3806 char_u *
3807vim_findfirst(path, filename, level)
3808 char_u *path;
3809 char_u *filename;
3810 int level;
3811{
3812 ff_fn_search_context =
3813 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
3814 ff_fn_search_context, rel_fname);
3815 if (NULL == ff_fn_search_context)
3816 return NULL;
3817 else
3818 return vim_findnext()
3819}
3820
3821 char_u *
3822vim_findnext()
3823{
3824 char_u *ret = vim_findfile(ff_fn_search_context);
3825
3826 if (NULL == ret)
3827 {
3828 vim_findfile_cleanup(ff_fn_search_context);
3829 ff_fn_search_context = NULL;
3830 }
3831 return ret;
3832}
3833#endif
3834
3835/*
3836 * Initialization routine for vim_findfile.
3837 *
3838 * Returns the newly allocated search context or NULL if an error occured.
3839 *
3840 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
3841 * with the search context.
3842 *
3843 * Find the file 'filename' in the directory 'path'.
3844 * The parameter 'path' may contain wildcards. If so only search 'level'
3845 * directories deep. The parameter 'level' is the absolute maximum and is
3846 * not related to restricts given to the '**' wildcard. If 'level' is 100
3847 * and you use '**200' vim_findfile() will stop after 100 levels.
3848 *
3849 * If 'stopdirs' is not NULL and nothing is found downward, the search is
3850 * restarted on the next higher directory level. This is repeated until the
3851 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
3852 * format ";*<dirname>*\(;<dirname>\)*;\=$".
3853 *
3854 * If the 'path' is relative, the starting dir for the search is either VIM's
3855 * current dir or if the path starts with "./" the current files dir.
3856 * If the 'path' is absolut, the starting dir is that part of the path before
3857 * the first wildcard.
3858 *
3859 * Upward search is only done on the starting dir.
3860 *
3861 * If 'free_visited' is TRUE the list of already visited files/directories is
3862 * cleared. Set this to FALSE if you just want to search from another
3863 * directory, but want to be sure that no directory from a previous search is
3864 * searched again. This is useful if you search for a file at different places.
3865 * The list of visited files/dirs can also be cleared with the function
3866 * vim_findfile_free_visited().
3867 *
3868 * Set the parameter 'need_dir' to TRUE if you want to search for a directory
3869 * instead of a file.
3870 *
3871 * A search context returned by a previous call to vim_findfile_init() can be
3872 * passed in the parameter 'search_ctx'. This context is than reused and
3873 * reinitialized with the new parameters. The list of already viseted
3874 * directories from this context is only deleted if the parameter
3875 * 'free_visited' is true. Be aware that the passed search_context is freed if
3876 * the reinitialization fails.
3877 *
3878 * If you don't have a search context from a previous call 'search_ctx' must be
3879 * NULL.
3880 *
3881 * This function silently ignores a few errors, vim_findfile() will have
3882 * limited functionality then.
3883 */
3884/*ARGSUSED*/
3885 void *
3886vim_findfile_init(path, filename, stopdirs, level, free_visited, need_dir,
3887 search_ctx, tagfile, rel_fname)
3888 char_u *path;
3889 char_u *filename;
3890 char_u *stopdirs;
3891 int level;
3892 int free_visited;
3893 int need_dir;
3894 void *search_ctx;
3895 int tagfile;
3896 char_u *rel_fname; /* file name to use for "." */
3897{
3898#ifdef FEAT_PATH_EXTRA
3899 char_u *wc_part;
3900#endif
3901 ff_stack_T *sptr;
3902
3903 /* If a search context is given by the caller, reuse it, else allocate a
3904 * new one.
3905 */
3906 if (search_ctx != NULL)
3907 ff_search_ctx = search_ctx;
3908 else
3909 {
3910 ff_search_ctx = (ff_search_ctx_T*)alloc(
3911 (unsigned)sizeof(ff_search_ctx_T));
3912 if (ff_search_ctx == NULL)
3913 goto error_return;
3914 memset(ff_search_ctx, 0, sizeof(ff_search_ctx_T));
3915 }
3916
3917 /* clear the search context, but NOT the visited lists */
3918 ff_clear();
3919
3920 /* clear visited list if wanted */
3921 if (free_visited == TRUE)
3922 vim_findfile_free_visited(ff_search_ctx);
3923 else
3924 {
3925 /* Reuse old visited lists. Get the visited list for the given
3926 * filename. If no list for the current filename exists, creates a new
3927 * one.
3928 */
3929 ff_search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
3930 &ff_search_ctx->ffsc_visited_lists_list);
3931 if (ff_search_ctx->ffsc_visited_list == NULL)
3932 goto error_return;
3933 ff_search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
3934 &ff_search_ctx->ffsc_dir_visited_lists_list);
3935 if (ff_search_ctx->ffsc_dir_visited_list == NULL)
3936 goto error_return;
3937 }
3938
3939 if (ff_expand_buffer == NULL)
3940 {
3941 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
3942 if (ff_expand_buffer == NULL)
3943 goto error_return;
3944 }
3945
3946 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003947 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (path[0] == '.'
3949 && (vim_ispathsep(path[1]) || path[1] == NUL)
3950 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
3951 && rel_fname != NULL)
3952 {
3953 int len = (int)(gettail(rel_fname) - rel_fname);
3954
3955 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
3956 {
3957 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003958 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 ff_search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer,
3960 FALSE);
3961 }
3962 else
3963 ff_search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
3964 if (ff_search_ctx->ffsc_start_dir == NULL)
3965 goto error_return;
3966 if (*++path != NUL)
3967 ++path;
3968 }
3969 else if (*path == NUL || !vim_isAbsName(path))
3970 {
3971#ifdef BACKSLASH_IN_FILENAME
3972 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
3973 if (*path != NUL && path[1] == ':')
3974 {
3975 char_u drive[3];
3976
3977 drive[0] = path[0];
3978 drive[1] = ':';
3979 drive[2] = NUL;
3980 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
3981 goto error_return;
3982 path += 2;
3983 }
3984 else
3985#endif
3986 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
3987 goto error_return;
3988
3989 ff_search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
3990 if (ff_search_ctx->ffsc_start_dir == NULL)
3991 goto error_return;
3992
3993#ifdef BACKSLASH_IN_FILENAME
3994 /* A path that starts with "/dir" is relative to the drive, not to the
3995 * directory (but not for "//machine/dir"). Only use the drive name. */
3996 if ((*path == '/' || *path == '\\')
3997 && path[1] != path[0]
3998 && ff_search_ctx->ffsc_start_dir[1] == ':')
3999 ff_search_ctx->ffsc_start_dir[2] = NUL;
4000#endif
4001 }
4002
4003#ifdef FEAT_PATH_EXTRA
4004 /*
4005 * If stopdirs are given, split them into an array of pointers.
4006 * If this fails (mem allocation), there is no upward search at all or a
4007 * stop directory is not recognized -> continue silently.
4008 * If stopdirs just contains a ";" or is empty,
4009 * ff_search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
4010 * is handled as unlimited upward search. See function
4011 * ff_path_in_stoplist() for details.
4012 */
4013 if (stopdirs != NULL)
4014 {
4015 char_u *walker = stopdirs;
4016 int dircount;
4017
4018 while (*walker == ';')
4019 walker++;
4020
4021 dircount = 1;
4022 ff_search_ctx->ffsc_stopdirs_v =
4023 (char_u **)alloc((unsigned)sizeof(char_u *));
4024
4025 if (ff_search_ctx->ffsc_stopdirs_v != NULL)
4026 {
4027 do
4028 {
4029 char_u *helper;
4030 void *ptr;
4031
4032 helper = walker;
4033 ptr = vim_realloc(ff_search_ctx->ffsc_stopdirs_v,
4034 (dircount + 1) * sizeof(char_u *));
4035 if (ptr)
4036 ff_search_ctx->ffsc_stopdirs_v = ptr;
4037 else
4038 /* ignore, keep what we have and continue */
4039 break;
4040 walker = vim_strchr(walker, ';');
4041 if (walker)
4042 {
4043 ff_search_ctx->ffsc_stopdirs_v[dircount-1] =
4044 vim_strnsave(helper, (int)(walker - helper));
4045 walker++;
4046 }
4047 else
4048 /* this might be "", which means ascent till top
4049 * of directory tree.
4050 */
4051 ff_search_ctx->ffsc_stopdirs_v[dircount-1] =
4052 vim_strsave(helper);
4053
4054 dircount++;
4055
4056 } while (walker != NULL);
4057 ff_search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
4058 }
4059 }
4060#endif
4061
4062#ifdef FEAT_PATH_EXTRA
4063 ff_search_ctx->ffsc_level = level;
4064
4065 /* split into:
4066 * -fix path
4067 * -wildcard_stuff (might be NULL)
4068 */
4069 wc_part = vim_strchr(path, '*');
4070 if (wc_part != NULL)
4071 {
4072 int llevel;
4073 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004074 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 /* save the fix part of the path */
4077 ff_search_ctx->ffsc_fix_path = vim_strnsave(path,
4078 (int)(wc_part - path));
4079
4080 /*
4081 * copy wc_path and add restricts to the '**' wildcard.
4082 * The octett after a '**' is used as a (binary) counter.
4083 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4084 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4085 * For EBCDIC you get different character values.
4086 * If no restrict is given after '**' the default is used.
4087 * Due to this technic the path looks awful if you print it as a
4088 * string.
4089 */
4090 len = 0;
4091 while (*wc_part != NUL)
4092 {
4093 if (STRNCMP(wc_part, "**", 2) == 0)
4094 {
4095 ff_expand_buffer[len++] = *wc_part++;
4096 ff_expand_buffer[len++] = *wc_part++;
4097
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004098 llevel = strtol((char *)wc_part, &errpt, 10);
4099 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004101 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 /* restrict is 0 -> remove already added '**' */
4103 len -= 2;
4104 else
4105 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004106 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004107 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
4109 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4110 goto error_return;
4111 }
4112 }
4113 else
4114 ff_expand_buffer[len++] = *wc_part++;
4115 }
4116 ff_expand_buffer[len] = NUL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004117 ff_search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 if (ff_search_ctx->ffsc_wc_path == NULL)
4120 goto error_return;
4121 }
4122 else
4123#endif
4124 ff_search_ctx->ffsc_fix_path = vim_strsave(path);
4125
4126 if (ff_search_ctx->ffsc_start_dir == NULL)
4127 {
4128 /* store the fix part as startdir.
4129 * This is needed if the parameter path is fully qualified.
4130 */
4131 ff_search_ctx->ffsc_start_dir = vim_strsave(ff_search_ctx->ffsc_fix_path);
4132 if (ff_search_ctx->ffsc_start_dir)
4133 ff_search_ctx->ffsc_fix_path[0] = NUL;
4134 }
4135
4136 /* create an absolute path */
4137 STRCPY(ff_expand_buffer, ff_search_ctx->ffsc_start_dir);
4138 add_pathsep(ff_expand_buffer);
4139 STRCAT(ff_expand_buffer, ff_search_ctx->ffsc_fix_path);
4140 add_pathsep(ff_expand_buffer);
4141
4142 sptr = ff_create_stack_element(ff_expand_buffer,
4143#ifdef FEAT_PATH_EXTRA
4144 ff_search_ctx->ffsc_wc_path,
4145#endif
4146 level, 0);
4147
4148 if (sptr == NULL)
4149 goto error_return;
4150
4151 ff_push(sptr);
4152
4153 ff_search_ctx->ffsc_file_to_search = vim_strsave(filename);
4154 if (ff_search_ctx->ffsc_file_to_search == NULL)
4155 goto error_return;
4156
4157 return ff_search_ctx;
4158
4159error_return:
4160 /*
4161 * We clear the search context now!
4162 * Even when the caller gave us a (perhaps valid) context we free it here,
4163 * as we might have already destroyed it.
4164 */
4165 vim_findfile_cleanup(ff_search_ctx);
4166 return NULL;
4167}
4168
4169#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4170/*
4171 * Get the stopdir string. Check that ';' is not escaped.
4172 */
4173 char_u *
4174vim_findfile_stopdir(buf)
4175 char_u *buf;
4176{
4177 char_u *r_ptr = buf;
4178
4179 while (*r_ptr != NUL && *r_ptr != ';')
4180 {
4181 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4182 {
4183 /* overwrite the escape char,
4184 * use STRLEN(r_ptr) to move the trailing '\0'
4185 */
4186 mch_memmove(r_ptr, r_ptr + 1, STRLEN(r_ptr));
4187 r_ptr++;
4188 }
4189 r_ptr++;
4190 }
4191 if (*r_ptr == ';')
4192 {
4193 *r_ptr = 0;
4194 r_ptr++;
4195 }
4196 else if (*r_ptr == NUL)
4197 r_ptr = NULL;
4198 return r_ptr;
4199}
4200#endif
4201
4202/* Clean up the given search context. Can handle a NULL pointer */
4203 void
4204vim_findfile_cleanup(ctx)
4205 void *ctx;
4206{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004207 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return;
4209
4210 ff_search_ctx = ctx;
4211
4212 vim_findfile_free_visited(ctx);
4213 ff_clear();
4214 vim_free(ctx);
4215 ff_search_ctx = NULL;
4216}
4217
4218/*
4219 * Find a file in a search context.
4220 * The search context was created with vim_findfile_init() above.
4221 * Return a pointer to an allocated file name or NULL if nothing found.
4222 * To get all matching files call this function until you get NULL.
4223 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004224 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 *
4226 * The search algorithm is depth first. To change this replace the
4227 * stack with a list (don't forget to leave partly searched directories on the
4228 * top of the list).
4229 */
4230 char_u *
4231vim_findfile(search_ctx)
4232 void *search_ctx;
4233{
4234 char_u *file_path;
4235#ifdef FEAT_PATH_EXTRA
4236 char_u *rest_of_wildcards;
4237 char_u *path_end = NULL;
4238#endif
4239 ff_stack_T *ctx;
4240#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4241 int len;
4242#endif
4243 int i;
4244 char_u *p;
4245#ifdef FEAT_SEARCHPATH
4246 char_u *suf;
4247#endif
4248
4249 if (search_ctx == NULL)
4250 return NULL;
4251
4252 ff_search_ctx = (ff_search_ctx_T*)search_ctx;
4253
4254 /*
4255 * filepath is used as buffer for various actions and as the storage to
4256 * return a found filename.
4257 */
4258 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4259 return NULL;
4260
4261#ifdef FEAT_PATH_EXTRA
4262 /* store the end of the start dir -- needed for upward search */
4263 if (ff_search_ctx->ffsc_start_dir != NULL)
4264 path_end = &ff_search_ctx->ffsc_start_dir[STRLEN(ff_search_ctx->ffsc_start_dir)];
4265#endif
4266
4267#ifdef FEAT_PATH_EXTRA
4268 /* upward search loop */
4269 for (;;)
4270 {
4271#endif
4272 /* downward search loop */
4273 for (;;)
4274 {
4275 /* check if user user wants to stop the search*/
4276 ui_breakcheck();
4277 if (got_int)
4278 break;
4279
4280 /* get directory to work on from stack */
4281 ctx = ff_pop();
4282 if (ctx == NULL)
4283 break;
4284
4285 /*
4286 * TODO: decide if we leave this test in
4287 *
4288 * GOOD: don't search a directory(-tree) twice.
4289 * BAD: - check linked list for every new directory entered.
4290 * - check for double files also done below
4291 *
4292 * Here we check if we already searched this directory.
4293 * We already searched a directory if:
4294 * 1) The directory is the same.
4295 * 2) We would use the same wildcard string.
4296 *
4297 * Good if you have links on same directory via several ways
4298 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4299 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4300 *
4301 * This check is only needed for directories we work on for the
4302 * first time (hence ctx->ff_filearray == NULL)
4303 */
4304 if (ctx->ffs_filearray == NULL
4305 && ff_check_visited(&ff_search_ctx->ffsc_dir_visited_list
4306 ->ffvl_visited_list,
4307 ctx->ffs_fix_path
4308#ifdef FEAT_PATH_EXTRA
4309 , ctx->ffs_wc_path
4310#endif
4311 ) == FAIL)
4312 {
4313#ifdef FF_VERBOSE
4314 if (p_verbose >= 5)
4315 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004316 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 smsg((char_u *)"Already Searched: %s (%s)",
4318 ctx->ffs_fix_path, ctx->ffs_wc_path);
4319 /* don't overwrite this either */
4320 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004321 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323#endif
4324 ff_free_stack_element(ctx);
4325 continue;
4326 }
4327#ifdef FF_VERBOSE
4328 else if (p_verbose >= 5)
4329 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004330 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004331 smsg((char_u *)"Searching: %s (%s)",
4332 ctx->ffs_fix_path, ctx->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 /* don't overwrite this either */
4334 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004335 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337#endif
4338
4339 /* check depth */
4340 if (ctx->ffs_level <= 0)
4341 {
4342 ff_free_stack_element(ctx);
4343 continue;
4344 }
4345
4346 file_path[0] = NUL;
4347
4348 /*
4349 * If no filearray till now expand wildcards
4350 * The function expand_wildcards() can handle an array of paths
4351 * and all possible expands are returned in one array. We use this
4352 * to handle the expansion of '**' into an empty string.
4353 */
4354 if (ctx->ffs_filearray == NULL)
4355 {
4356 char_u *dirptrs[2];
4357
4358 /* we use filepath to build the path expand_wildcards() should
4359 * expand.
4360 */
4361 dirptrs[0] = file_path;
4362 dirptrs[1] = NULL;
4363
4364 /* if we have a start dir copy it in */
4365 if (!vim_isAbsName(ctx->ffs_fix_path)
4366 && ff_search_ctx->ffsc_start_dir)
4367 {
4368 STRCPY(file_path, ff_search_ctx->ffsc_start_dir);
4369 add_pathsep(file_path);
4370 }
4371
4372 /* append the fix part of the search path */
4373 STRCAT(file_path, ctx->ffs_fix_path);
4374 add_pathsep(file_path);
4375
4376#ifdef FEAT_PATH_EXTRA
4377 rest_of_wildcards = ctx->ffs_wc_path;
4378 if (*rest_of_wildcards != NUL)
4379 {
4380 len = (int)STRLEN(file_path);
4381 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4382 {
4383 /* pointer to the restrict byte
4384 * The restrict byte is not a character!
4385 */
4386 p = rest_of_wildcards + 2;
4387
4388 if (*p > 0)
4389 {
4390 (*p)--;
4391 file_path[len++] = '*';
4392 }
4393
4394 if (*p == 0)
4395 {
4396 /* remove '**<numb> from wildcards */
4397 mch_memmove(rest_of_wildcards,
4398 rest_of_wildcards + 3,
4399 STRLEN(rest_of_wildcards + 3) + 1);
4400 }
4401 else
4402 rest_of_wildcards += 3;
4403
4404 if (ctx->ffs_star_star_empty == 0)
4405 {
4406 /* if not done before, expand '**' to empty */
4407 ctx->ffs_star_star_empty = 1;
4408 dirptrs[1] = ctx->ffs_fix_path;
4409 }
4410 }
4411
4412 /*
4413 * Here we copy until the next path separator or the end of
4414 * the path. If we stop at a path separator, there is
4415 * still somthing else left. This is handled below by
4416 * pushing every directory returned from expand_wildcards()
4417 * on the stack again for further search.
4418 */
4419 while (*rest_of_wildcards
4420 && !vim_ispathsep(*rest_of_wildcards))
4421 file_path[len++] = *rest_of_wildcards++;
4422
4423 file_path[len] = NUL;
4424 if (vim_ispathsep(*rest_of_wildcards))
4425 rest_of_wildcards++;
4426 }
4427#endif
4428
4429 /*
4430 * Expand wildcards like "*" and "$VAR".
4431 * If the path is a URL don't try this.
4432 */
4433 if (path_with_url(dirptrs[0]))
4434 {
4435 ctx->ffs_filearray = (char_u **)
4436 alloc((unsigned)sizeof(char *));
4437 if (ctx->ffs_filearray != NULL
4438 && (ctx->ffs_filearray[0]
4439 = vim_strsave(dirptrs[0])) != NULL)
4440 ctx->ffs_filearray_size = 1;
4441 else
4442 ctx->ffs_filearray_size = 0;
4443 }
4444 else
4445 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
4446 &ctx->ffs_filearray_size,
4447 &ctx->ffs_filearray,
4448 EW_DIR|EW_ADDSLASH|EW_SILENT);
4449
4450 ctx->ffs_filearray_cur = 0;
4451 ctx->ffs_stage = 0;
4452 }
4453#ifdef FEAT_PATH_EXTRA
4454 else
4455 rest_of_wildcards = &ctx->ffs_wc_path[STRLEN(ctx->ffs_wc_path)];
4456#endif
4457
4458 if (ctx->ffs_stage == 0)
4459 {
4460 /* this is the first time we work on this directory */
4461#ifdef FEAT_PATH_EXTRA
4462 if (*rest_of_wildcards == NUL)
4463#endif
4464 {
4465 /*
4466 * we don't have further wildcards to expand, so we have to
4467 * check for the final file now
4468 */
4469 for (i = ctx->ffs_filearray_cur;
4470 i < ctx->ffs_filearray_size; ++i)
4471 {
4472 if (!path_with_url(ctx->ffs_filearray[i])
4473 && !mch_isdir(ctx->ffs_filearray[i]))
4474 continue; /* not a directory */
4475
4476 /* prepare the filename to be checked for existance
4477 * below */
4478 STRCPY(file_path, ctx->ffs_filearray[i]);
4479 add_pathsep(file_path);
4480 STRCAT(file_path, ff_search_ctx->ffsc_file_to_search);
4481
4482 /*
4483 * Try without extra suffix and then with suffixes
4484 * from 'suffixesadd'.
4485 */
4486#ifdef FEAT_SEARCHPATH
4487 len = (int)STRLEN(file_path);
4488 suf = curbuf->b_p_sua;
4489 for (;;)
4490#endif
4491 {
4492 /* if file exists and we didn't already find it */
4493 if ((path_with_url(file_path)
4494 || (mch_getperm(file_path) >= 0
4495 && (!ff_search_ctx->ffsc_need_dir
4496 || mch_isdir(file_path))))
4497#ifndef FF_VERBOSE
4498 && (ff_check_visited(
4499 &ff_search_ctx->ffsc_visited_list->ffvl_visited_list,
4500 file_path
4501#ifdef FEAT_PATH_EXTRA
4502 , (char_u *)""
4503#endif
4504 ) == OK)
4505#endif
4506 )
4507 {
4508#ifdef FF_VERBOSE
4509 if (ff_check_visited(
4510 &ff_search_ctx->ffsc_visited_list->ffvl_visited_list,
4511 file_path
4512#ifdef FEAT_PATH_EXTRA
4513 , (char_u *)""
4514#endif
4515 ) == FAIL)
4516 {
4517 if (p_verbose >= 5)
4518 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004519 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004520 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 file_path);
4522 /* don't overwrite this either */
4523 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004524 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 continue;
4527 }
4528#endif
4529
4530 /* push dir to examine rest of subdirs later */
4531 ctx->ffs_filearray_cur = i + 1;
4532 ff_push(ctx);
4533
4534 simplify_filename(file_path);
4535 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4536 == OK)
4537 {
4538 p = shorten_fname(file_path,
4539 ff_expand_buffer);
4540 if (p != NULL)
4541 mch_memmove(file_path, p,
4542 STRLEN(p) + 1);
4543 }
4544#ifdef FF_VERBOSE
4545 if (p_verbose >= 5)
4546 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004547 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004548 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /* don't overwrite this either */
4550 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004551 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553#endif
4554 return file_path;
4555 }
4556
4557#ifdef FEAT_SEARCHPATH
4558 /* Not found or found already, try next suffix. */
4559 if (*suf == NUL)
4560 break;
4561 copy_option_part(&suf, file_path + len,
4562 MAXPATHL - len, ",");
4563#endif
4564 }
4565 }
4566 }
4567#ifdef FEAT_PATH_EXTRA
4568 else
4569 {
4570 /*
4571 * still wildcards left, push the directories for further
4572 * search
4573 */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004574 for (i = ctx->ffs_filearray_cur;
4575 i < ctx->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 if (!mch_isdir(ctx->ffs_filearray[i]))
4578 continue; /* not a directory */
4579
4580 ff_push(ff_create_stack_element(ctx->ffs_filearray[i],
4581 rest_of_wildcards, ctx->ffs_level - 1, 0));
4582 }
4583 }
4584#endif
4585 ctx->ffs_filearray_cur = 0;
4586 ctx->ffs_stage = 1;
4587 }
4588
4589#ifdef FEAT_PATH_EXTRA
4590 /*
4591 * if wildcards contains '**' we have to descent till we reach the
4592 * leaves of the directory tree.
4593 */
4594 if (STRNCMP(ctx->ffs_wc_path, "**", 2) == 0)
4595 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004596 for (i = ctx->ffs_filearray_cur;
4597 i < ctx->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 if (fnamecmp(ctx->ffs_filearray[i], ctx->ffs_fix_path) == 0)
4600 continue; /* don't repush same directory */
4601 if (!mch_isdir(ctx->ffs_filearray[i]))
4602 continue; /* not a directory */
4603 ff_push(ff_create_stack_element(ctx->ffs_filearray[i],
4604 ctx->ffs_wc_path, ctx->ffs_level - 1, 1));
4605 }
4606 }
4607#endif
4608
4609 /* we are done with the current directory */
4610 ff_free_stack_element(ctx);
4611
4612 }
4613
4614#ifdef FEAT_PATH_EXTRA
4615 /* If we reached this, we didn't find anything downwards.
4616 * Let's check if we should do an upward search.
4617 */
4618 if (ff_search_ctx->ffsc_start_dir
4619 && ff_search_ctx->ffsc_stopdirs_v != NULL && !got_int)
4620 {
4621 ff_stack_T *sptr;
4622
4623 /* is the last starting directory in the stop list? */
4624 if (ff_path_in_stoplist(ff_search_ctx->ffsc_start_dir,
4625 (int)(path_end - ff_search_ctx->ffsc_start_dir),
4626 ff_search_ctx->ffsc_stopdirs_v) == TRUE)
4627 break;
4628
4629 /* cut of last dir */
4630 while (path_end > ff_search_ctx->ffsc_start_dir
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004631 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 path_end--;
4633 while (path_end > ff_search_ctx->ffsc_start_dir
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004634 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 path_end--;
4636 *path_end = 0;
4637 path_end--;
4638
4639 if (*ff_search_ctx->ffsc_start_dir == 0)
4640 break;
4641
4642 STRCPY(file_path, ff_search_ctx->ffsc_start_dir);
4643 add_pathsep(file_path);
4644 STRCAT(file_path, ff_search_ctx->ffsc_fix_path);
4645
4646 /* create a new stack entry */
4647 sptr = ff_create_stack_element(file_path,
4648 ff_search_ctx->ffsc_wc_path, ff_search_ctx->ffsc_level, 0);
4649 if (sptr == NULL)
4650 break;
4651 ff_push(sptr);
4652 }
4653 else
4654 break;
4655 }
4656#endif
4657
4658 vim_free(file_path);
4659 return NULL;
4660}
4661
4662/*
4663 * Free the list of lists of visited files and directories
4664 * Can handle it if the passed search_context is NULL;
4665 */
4666 void
4667vim_findfile_free_visited(search_ctx)
4668 void *search_ctx;
4669{
4670 if (search_ctx == NULL)
4671 return;
4672
4673 ff_search_ctx = (ff_search_ctx_T *)search_ctx;
4674
4675 vim_findfile_free_visited_list(&ff_search_ctx->ffsc_visited_lists_list);
4676 vim_findfile_free_visited_list(&ff_search_ctx->ffsc_dir_visited_lists_list);
4677}
4678
4679 static void
4680vim_findfile_free_visited_list(list_headp)
4681 ff_visited_list_hdr_T **list_headp;
4682{
4683 ff_visited_list_hdr_T *vp;
4684
4685 while (*list_headp != NULL)
4686 {
4687 vp = (*list_headp)->ffvl_next;
4688 ff_free_visited_list((*list_headp)->ffvl_visited_list);
4689
4690 vim_free((*list_headp)->ffvl_filename);
4691 vim_free(*list_headp);
4692 *list_headp = vp;
4693 }
4694 *list_headp = NULL;
4695}
4696
4697 static void
4698ff_free_visited_list(vl)
4699 ff_visited_T *vl;
4700{
4701 ff_visited_T *vp;
4702
4703 while (vl != NULL)
4704 {
4705 vp = vl->ffv_next;
4706#ifdef FEAT_PATH_EXTRA
4707 vim_free(vl->ffv_wc_path);
4708#endif
4709 vim_free(vl);
4710 vl = vp;
4711 }
4712 vl = NULL;
4713}
4714
4715/*
4716 * Returns the already visited list for the given filename. If none is found it
4717 * allocates a new one.
4718 */
4719 static ff_visited_list_hdr_T*
4720ff_get_visited_list(filename, list_headp)
4721 char_u *filename;
4722 ff_visited_list_hdr_T **list_headp;
4723{
4724 ff_visited_list_hdr_T *retptr = NULL;
4725
4726 /* check if a visited list for the given filename exists */
4727 if (*list_headp != NULL)
4728 {
4729 retptr = *list_headp;
4730 while (retptr != NULL)
4731 {
4732 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
4733 {
4734#ifdef FF_VERBOSE
4735 if (p_verbose >= 5)
4736 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004737 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004738 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 filename);
4740 /* don't overwrite this either */
4741 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004742 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744#endif
4745 return retptr;
4746 }
4747 retptr = retptr->ffvl_next;
4748 }
4749 }
4750
4751#ifdef FF_VERBOSE
4752 if (p_verbose >= 5)
4753 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004754 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004755 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 /* don't overwrite this either */
4757 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004758 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760#endif
4761
4762 /*
4763 * if we reach this we didn't find a list and we have to allocate new list
4764 */
4765 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
4766 if (retptr == NULL)
4767 return NULL;
4768
4769 retptr->ffvl_visited_list = NULL;
4770 retptr->ffvl_filename = vim_strsave(filename);
4771 if (retptr->ffvl_filename == NULL)
4772 {
4773 vim_free(retptr);
4774 return NULL;
4775 }
4776 retptr->ffvl_next = *list_headp;
4777 *list_headp = retptr;
4778
4779 return retptr;
4780}
4781
4782#ifdef FEAT_PATH_EXTRA
4783/*
4784 * check if two wildcard paths are equal. Returns TRUE or FALSE.
4785 * They are equal if:
4786 * - both paths are NULL
4787 * - they have the same length
4788 * - char by char comparison is OK
4789 * - the only differences are in the counters behind a '**', so
4790 * '**\20' is equal to '**\24'
4791 */
4792 static int
4793ff_wc_equal(s1, s2)
4794 char_u *s1;
4795 char_u *s2;
4796{
4797 int i;
4798
4799 if (s1 == s2)
4800 return TRUE;
4801
4802 if (s1 == NULL || s2 == NULL)
4803 return FALSE;
4804
4805 if (STRLEN(s1) != STRLEN(s2))
4806 return FAIL;
4807
4808 for (i = 0; s1[i] != NUL && s2[i] != NUL; i++)
4809 {
4810 if (s1[i] != s2[i]
4811#ifdef CASE_INSENSITIVE_FILENAME
4812 && TOUPPER_LOC(s1[i]) != TOUPPER_LOC(s2[i])
4813#endif
4814 )
4815 {
4816 if (i >= 2)
4817 if (s1[i-1] == '*' && s1[i-2] == '*')
4818 continue;
4819 else
4820 return FAIL;
4821 else
4822 return FAIL;
4823 }
4824 }
4825 return TRUE;
4826}
4827#endif
4828
4829/*
4830 * maintains the list of already visited files and dirs
4831 * returns FAIL if the given file/dir is already in the list
4832 * returns OK if it is newly added
4833 *
4834 * TODO: What to do on memory allocation problems?
4835 * -> return TRUE - Better the file is found several times instead of
4836 * never.
4837 */
4838 static int
4839ff_check_visited(visited_list, fname
4840#ifdef FEAT_PATH_EXTRA
4841 , wc_path
4842#endif
4843 )
4844 ff_visited_T **visited_list;
4845 char_u *fname;
4846#ifdef FEAT_PATH_EXTRA
4847 char_u *wc_path;
4848#endif
4849{
4850 ff_visited_T *vp;
4851#ifdef UNIX
4852 struct stat st;
4853 int url = FALSE;
4854#endif
4855
4856 /* For an URL we only compare the name, otherwise we compare the
4857 * device/inode (unix) or the full path name (not Unix). */
4858 if (path_with_url(fname))
4859 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004860 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#ifdef UNIX
4862 url = TRUE;
4863#endif
4864 }
4865 else
4866 {
4867 ff_expand_buffer[0] = NUL;
4868#ifdef UNIX
4869 if (mch_stat((char *)fname, &st) < 0)
4870#else
4871 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4872#endif
4873 return FAIL;
4874 }
4875
4876 /* check against list of already visited files */
4877 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
4878 {
4879 if (
4880#ifdef UNIX
4881 !url
4882 ? (vp->ffv_dev == st.st_dev
4883 && vp->ffv_ino == st.st_ino)
4884 :
4885#endif
4886 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
4887 )
4888 {
4889#ifdef FEAT_PATH_EXTRA
4890 /* are the wildcard parts equal */
4891 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
4892#endif
4893 /* already visited */
4894 return FAIL;
4895 }
4896 }
4897
4898 /*
4899 * New file/dir. Add it to the list of visited files/dirs.
4900 */
4901 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
4902 + STRLEN(ff_expand_buffer)));
4903
4904 if (vp != NULL)
4905 {
4906#ifdef UNIX
4907 if (!url)
4908 {
4909 vp->ffv_ino = st.st_ino;
4910 vp->ffv_dev = st.st_dev;
4911 vp->ffv_fname[0] = NUL;
4912 }
4913 else
4914 {
4915 vp->ffv_ino = 0;
4916 vp->ffv_dev = -1;
4917#endif
4918 STRCPY(vp->ffv_fname, ff_expand_buffer);
4919#ifdef UNIX
4920 }
4921#endif
4922#ifdef FEAT_PATH_EXTRA
4923 if (wc_path != NULL)
4924 vp->ffv_wc_path = vim_strsave(wc_path);
4925 else
4926 vp->ffv_wc_path = NULL;
4927#endif
4928
4929 vp->ffv_next = *visited_list;
4930 *visited_list = vp;
4931 }
4932
4933 return OK;
4934}
4935
4936/*
4937 * create stack element from given path pieces
4938 */
4939 static ff_stack_T *
4940ff_create_stack_element(fix_part,
4941#ifdef FEAT_PATH_EXTRA
4942 wc_part,
4943#endif
4944 level, star_star_empty)
4945 char_u *fix_part;
4946#ifdef FEAT_PATH_EXTRA
4947 char_u *wc_part;
4948#endif
4949 int level;
4950 int star_star_empty;
4951{
4952 ff_stack_T *new;
4953
4954 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
4955 if (new == NULL)
4956 return NULL;
4957
4958 new->ffs_prev = NULL;
4959 new->ffs_filearray = NULL;
4960 new->ffs_filearray_size = 0;
4961 new->ffs_filearray_cur = 0;
4962 new->ffs_stage = 0;
4963 new->ffs_level = level;
4964 new->ffs_star_star_empty = star_star_empty;;
4965
4966 /* the following saves NULL pointer checks in vim_findfile */
4967 if (fix_part == NULL)
4968 fix_part = (char_u *)"";
4969 new->ffs_fix_path = vim_strsave(fix_part);
4970
4971#ifdef FEAT_PATH_EXTRA
4972 if (wc_part == NULL)
4973 wc_part = (char_u *)"";
4974 new->ffs_wc_path = vim_strsave(wc_part);
4975#endif
4976
4977 if (new->ffs_fix_path == NULL
4978#ifdef FEAT_PATH_EXTRA
4979 || new->ffs_wc_path == NULL
4980#endif
4981 )
4982 {
4983 ff_free_stack_element(new);
4984 new = NULL;
4985 }
4986
4987 return new;
4988}
4989
4990/*
4991 * push a dir on the directory stack
4992 */
4993 static void
4994ff_push(ctx)
4995 ff_stack_T *ctx;
4996{
4997 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004998 * to prevent a crash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 if (ctx != NULL)
5000 {
5001 ctx->ffs_prev = ff_search_ctx->ffsc_stack_ptr;
5002 ff_search_ctx->ffsc_stack_ptr = ctx;
5003 }
5004}
5005
5006/*
5007 * pop a dir from the directory stack
5008 * returns NULL if stack is empty
5009 */
5010 static ff_stack_T *
5011ff_pop()
5012{
5013 ff_stack_T *sptr;
5014
5015 sptr = ff_search_ctx->ffsc_stack_ptr;
5016 if (ff_search_ctx->ffsc_stack_ptr != NULL)
5017 ff_search_ctx->ffsc_stack_ptr = ff_search_ctx->ffsc_stack_ptr->ffs_prev;
5018
5019 return sptr;
5020}
5021
5022/*
5023 * free the given stack element
5024 */
5025 static void
5026ff_free_stack_element(ctx)
5027 ff_stack_T *ctx;
5028{
5029 /* vim_free handles possible NULL pointers */
5030 vim_free(ctx->ffs_fix_path);
5031#ifdef FEAT_PATH_EXTRA
5032 vim_free(ctx->ffs_wc_path);
5033#endif
5034
5035 if (ctx->ffs_filearray != NULL)
5036 FreeWild(ctx->ffs_filearray_size, ctx->ffs_filearray);
5037
5038 vim_free(ctx);
5039}
5040
5041/*
5042 * clear the search context
5043 */
5044 static void
5045ff_clear()
5046{
5047 ff_stack_T *sptr;
5048
5049 /* clear up stack */
5050 while ((sptr = ff_pop()) != NULL)
5051 ff_free_stack_element(sptr);
5052
5053 vim_free(ff_search_ctx->ffsc_file_to_search);
5054 vim_free(ff_search_ctx->ffsc_start_dir);
5055 vim_free(ff_search_ctx->ffsc_fix_path);
5056#ifdef FEAT_PATH_EXTRA
5057 vim_free(ff_search_ctx->ffsc_wc_path);
5058#endif
5059
5060#ifdef FEAT_PATH_EXTRA
5061 if (ff_search_ctx->ffsc_stopdirs_v != NULL)
5062 {
5063 int i = 0;
5064
5065 while (ff_search_ctx->ffsc_stopdirs_v[i] != NULL)
5066 {
5067 vim_free(ff_search_ctx->ffsc_stopdirs_v[i]);
5068 i++;
5069 }
5070 vim_free(ff_search_ctx->ffsc_stopdirs_v);
5071 }
5072 ff_search_ctx->ffsc_stopdirs_v = NULL;
5073#endif
5074
5075 /* reset everything */
5076 ff_search_ctx->ffsc_file_to_search = NULL;
5077 ff_search_ctx->ffsc_start_dir = NULL;
5078 ff_search_ctx->ffsc_fix_path = NULL;
5079#ifdef FEAT_PATH_EXTRA
5080 ff_search_ctx->ffsc_wc_path = NULL;
5081 ff_search_ctx->ffsc_level = 0;
5082#endif
5083}
5084
5085#ifdef FEAT_PATH_EXTRA
5086/*
5087 * check if the given path is in the stopdirs
5088 * returns TRUE if yes else FALSE
5089 */
5090 static int
5091ff_path_in_stoplist(path, path_len, stopdirs_v)
5092 char_u *path;
5093 int path_len;
5094 char_u **stopdirs_v;
5095{
5096 int i = 0;
5097
5098 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005099 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 path_len--;
5101
5102 /* if no path consider it as match */
5103 if (path_len == 0)
5104 return TRUE;
5105
5106 for (i = 0; stopdirs_v[i] != NULL; i++)
5107 {
5108 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5109 {
5110 /* match for parent directory. So '/home' also matches
5111 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5112 * '/home/r' would also match '/home/rks'
5113 */
5114 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005115 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 return TRUE;
5117 }
5118 else
5119 {
5120 if (fnamecmp(stopdirs_v[i], path) == 0)
5121 return TRUE;
5122 }
5123 }
5124 return FALSE;
5125}
5126#endif
5127
5128#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5129/*
5130 * Find the file name "ptr[len]" in the path.
5131 *
5132 * On the first call set the parameter 'first' to TRUE to initialize
5133 * the search. For repeating calls to FALSE.
5134 *
5135 * Repeating calls will return other files called 'ptr[len]' from the path.
5136 *
5137 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5138 * don't need valid values.
5139 *
5140 * If nothing found on the first call the option FNAME_MESS will issue the
5141 * message:
5142 * 'Can't find file "<file>" in path'
5143 * On repeating calls:
5144 * 'No more file "<file>" found in path'
5145 *
5146 * options:
5147 * FNAME_MESS give error message when not found
5148 *
5149 * Uses NameBuff[]!
5150 *
5151 * Returns an allocated string for the file name. NULL for error.
5152 *
5153 */
5154 char_u *
5155find_file_in_path(ptr, len, options, first, rel_fname)
5156 char_u *ptr; /* file name */
5157 int len; /* length of file name */
5158 int options;
5159 int first; /* use count'th matching file name */
5160 char_u *rel_fname; /* file name searching relative to */
5161{
5162 return find_file_in_path_option(ptr, len, options, first,
5163 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005164 FALSE, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165}
5166
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005167static char_u *ff_file_to_find = NULL;
5168static void *fdip_search_ctx = NULL;
5169
5170#if defined(EXITFREE)
5171 static void
5172free_findfile()
5173{
5174 vim_free(ff_file_to_find);
5175 vim_findfile_cleanup(fdip_search_ctx);
5176}
5177#endif
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179/*
5180 * Find the directory name "ptr[len]" in the path.
5181 *
5182 * options:
5183 * FNAME_MESS give error message when not found
5184 *
5185 * Uses NameBuff[]!
5186 *
5187 * Returns an allocated string for the file name. NULL for error.
5188 */
5189 char_u *
5190find_directory_in_path(ptr, len, options, rel_fname)
5191 char_u *ptr; /* file name */
5192 int len; /* length of file name */
5193 int options;
5194 char_u *rel_fname; /* file name searching relative to */
5195{
5196 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005197 TRUE, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198}
5199
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005200 char_u *
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005201find_file_in_path_option(ptr, len, options, first, path_option, need_dir, rel_fname, suffixes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 char_u *ptr; /* file name */
5203 int len; /* length of file name */
5204 int options;
5205 int first; /* use count'th matching file name */
5206 char_u *path_option; /* p_path or p_cdpath */
5207 int need_dir; /* looking for directory name */
5208 char_u *rel_fname; /* file name we are looking relative to. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005209 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 static int did_findfile_init = FALSE;
5213 char_u save_char;
5214 char_u *file_name = NULL;
5215 char_u *buf = NULL;
5216 int rel_to_curdir;
5217#ifdef AMIGA
5218 struct Process *proc = (struct Process *)FindTask(0L);
5219 APTR save_winptr = proc->pr_WindowPtr;
5220
5221 /* Avoid a requester here for a volume that doesn't exist. */
5222 proc->pr_WindowPtr = (APTR)-1L;
5223#endif
5224
5225 if (first == TRUE)
5226 {
5227 /* copy file name into NameBuff, expanding environment variables */
5228 save_char = ptr[len];
5229 ptr[len] = NUL;
5230 expand_env(ptr, NameBuff, MAXPATHL);
5231 ptr[len] = save_char;
5232
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005233 vim_free(ff_file_to_find);
5234 ff_file_to_find = vim_strsave(NameBuff);
5235 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 file_name = NULL;
5238 goto theend;
5239 }
5240 }
5241
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005242 rel_to_curdir = (ff_file_to_find[0] == '.'
5243 && (ff_file_to_find[1] == NUL
5244 || vim_ispathsep(ff_file_to_find[1])
5245 || (ff_file_to_find[1] == '.'
5246 && (ff_file_to_find[2] == NUL
5247 || vim_ispathsep(ff_file_to_find[2])))));
5248 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 /* "..", "../path", "." and "./path": don't use the path_option */
5250 || rel_to_curdir
5251#if defined(MSWIN) || defined(MSDOS) || defined(OS2)
5252 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005253 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 /* handle "c:name" as absulute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005255 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#endif
5257#ifdef AMIGA
5258 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005259 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#endif
5261 )
5262 {
5263 /*
5264 * Absolute path, no need to use "path_option".
5265 * If this is not a first call, return NULL. We already returned a
5266 * filename on the first call.
5267 */
5268 if (first == TRUE)
5269 {
5270 int l;
5271 int run;
5272
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005273 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005275 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 goto theend;
5277 }
5278
5279 /* When FNAME_REL flag given first use the directory of the file.
5280 * Otherwise or when this fails use the current directory. */
5281 for (run = 1; run <= 2; ++run)
5282 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005283 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if (run == 1
5285 && rel_to_curdir
5286 && (options & FNAME_REL)
5287 && rel_fname != NULL
5288 && STRLEN(rel_fname) + l < MAXPATHL)
5289 {
5290 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005291 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 l = (int)STRLEN(NameBuff);
5293 }
5294 else
5295 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005296 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 run = 2;
5298 }
5299
5300 /* When the file doesn't exist, try adding parts of
5301 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005302 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 for (;;)
5304 {
5305 if (
5306#ifdef DJGPP
5307 /* "C:" by itself will fail for mch_getperm(),
5308 * assume it's always valid. */
5309 (need_dir && NameBuff[0] != NUL
5310 && NameBuff[1] == ':'
5311 && NameBuff[2] == NUL) ||
5312#endif
5313 (mch_getperm(NameBuff) >= 0
5314 && (!need_dir || mch_isdir(NameBuff))))
5315 {
5316 file_name = vim_strsave(NameBuff);
5317 goto theend;
5318 }
5319 if (*buf == NUL)
5320 break;
5321 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5322 }
5323 }
5324 }
5325 }
5326 else
5327 {
5328 /*
5329 * Loop over all paths in the 'path' or 'cdpath' option.
5330 * When "first" is set, first setup to the start of the option.
5331 * Otherwise continue to find the next match.
5332 */
5333 if (first == TRUE)
5334 {
5335 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005336 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 dir = path_option;
5338 did_findfile_init = FALSE;
5339 }
5340
5341 for (;;)
5342 {
5343 if (did_findfile_init)
5344 {
5345 ff_search_ctx->ffsc_need_dir = need_dir;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005346 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 ff_search_ctx->ffsc_need_dir = FALSE;
5348 if (file_name != NULL)
5349 break;
5350
5351 did_findfile_init = FALSE;
5352 }
5353 else
5354 {
5355 char_u *r_ptr;
5356
5357 if (dir == NULL || *dir == NUL)
5358 {
5359 /* We searched all paths of the option, now we can
5360 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005361 vim_findfile_cleanup(fdip_search_ctx);
5362 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 break;
5364 }
5365
5366 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5367 break;
5368
5369 /* copy next path */
5370 buf[0] = 0;
5371 copy_option_part(&dir, buf, MAXPATHL, " ,");
5372
5373#ifdef FEAT_PATH_EXTRA
5374 /* get the stopdir string */
5375 r_ptr = vim_findfile_stopdir(buf);
5376#else
5377 r_ptr = NULL;
5378#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005379 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
5380 r_ptr, 100, FALSE, TRUE,
5381 fdip_search_ctx, FALSE, rel_fname);
5382 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 did_findfile_init = TRUE;
5384 vim_free(buf);
5385 }
5386 }
5387 }
5388 if (file_name == NULL && (options & FNAME_MESS))
5389 {
5390 if (first == TRUE)
5391 {
5392 if (need_dir)
5393 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005394 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 else
5396 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005397 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 else
5400 {
5401 if (need_dir)
5402 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005403 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 else
5405 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005406 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408 }
5409
5410theend:
5411#ifdef AMIGA
5412 proc->pr_WindowPtr = save_winptr;
5413#endif
5414 return file_name;
5415}
5416
5417#endif /* FEAT_SEARCHPATH */
5418
5419/*
5420 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5421 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5422 */
5423 int
5424vim_chdir(new_dir)
5425 char_u *new_dir;
5426{
5427#ifndef FEAT_SEARCHPATH
5428 return mch_chdir((char *)new_dir);
5429#else
5430 char_u *dir_name;
5431 int r;
5432
5433 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5434 FNAME_MESS, curbuf->b_ffname);
5435 if (dir_name == NULL)
5436 return -1;
5437 r = mch_chdir((char *)dir_name);
5438 vim_free(dir_name);
5439 return r;
5440#endif
5441}
5442
5443/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005444 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005446 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5447 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 * Returns OK or FAIL.
5449 */
5450 int
5451get_user_name(buf, len)
5452 char_u *buf;
5453 int len;
5454{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005455 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 if (mch_get_user_name(buf, len) == FAIL)
5458 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005459 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
5461 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005462 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 return OK;
5464}
5465
5466#ifndef HAVE_QSORT
5467/*
5468 * Our own qsort(), for systems that don't have it.
5469 * It's simple and slow. From the K&R C book.
5470 */
5471 void
5472qsort(base, elm_count, elm_size, cmp)
5473 void *base;
5474 size_t elm_count;
5475 size_t elm_size;
5476 int (*cmp) __ARGS((const void *, const void *));
5477{
5478 char_u *buf;
5479 char_u *p1;
5480 char_u *p2;
5481 int i, j;
5482 int gap;
5483
5484 buf = alloc((unsigned)elm_size);
5485 if (buf == NULL)
5486 return;
5487
5488 for (gap = elm_count / 2; gap > 0; gap /= 2)
5489 for (i = gap; i < elm_count; ++i)
5490 for (j = i - gap; j >= 0; j -= gap)
5491 {
5492 /* Compare the elements. */
5493 p1 = (char_u *)base + j * elm_size;
5494 p2 = (char_u *)base + (j + gap) * elm_size;
5495 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5496 break;
5497 /* Exchange the elemets. */
5498 mch_memmove(buf, p1, elm_size);
5499 mch_memmove(p1, p2, elm_size);
5500 mch_memmove(p2, buf, elm_size);
5501 }
5502
5503 vim_free(buf);
5504}
5505#endif
5506
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507/*
5508 * Sort an array of strings.
5509 */
5510static int
5511#ifdef __BORLANDC__
5512_RTLENTRYF
5513#endif
5514sort_compare __ARGS((const void *s1, const void *s2));
5515
5516 static int
5517#ifdef __BORLANDC__
5518_RTLENTRYF
5519#endif
5520sort_compare(s1, s2)
5521 const void *s1;
5522 const void *s2;
5523{
5524 return STRCMP(*(char **)s1, *(char **)s2);
5525}
5526
5527 void
5528sort_strings(files, count)
5529 char_u **files;
5530 int count;
5531{
5532 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535#if !defined(NO_EXPANDPATH) || defined(PROTO)
5536/*
5537 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005538 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 * Return value like strcmp(p, q), but consider path separators.
5540 */
5541 int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005542pathcmp(p, q, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 const char *p, *q;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005544 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 int i;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005547 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005549 for (i = 0; maxlen < 0 || i < maxlen; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 {
5551 /* End of "p": check if "q" also ends or just has a slash. */
5552 if (p[i] == NUL)
5553 {
5554 if (q[i] == NUL) /* full match */
5555 return 0;
5556 s = q;
5557 break;
5558 }
5559
5560 /* End of "q": check if "p" just has a slash. */
5561 if (q[i] == NUL)
5562 {
5563 s = p;
5564 break;
5565 }
5566
5567 if (
5568#ifdef CASE_INSENSITIVE_FILENAME
5569 TOUPPER_LOC(p[i]) != TOUPPER_LOC(q[i])
5570#else
5571 p[i] != q[i]
5572#endif
5573#ifdef BACKSLASH_IN_FILENAME
5574 /* consider '/' and '\\' to be equal */
5575 && !((p[i] == '/' && q[i] == '\\')
5576 || (p[i] == '\\' && q[i] == '/'))
5577#endif
5578 )
5579 {
5580 if (vim_ispathsep(p[i]))
5581 return -1;
5582 if (vim_ispathsep(q[i]))
5583 return 1;
5584 return ((char_u *)p)[i] - ((char_u *)q)[i]; /* no match */
5585 }
5586 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005587 if (s == NULL) /* "i" ran into "maxlen" */
5588 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005591 if (s[i + 1] == NUL
5592 && i > 0
5593 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar86b68352004-12-27 21:59:20 +00005595 && (s[i] == '/' || s[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596#else
Bram Moolenaar86b68352004-12-27 21:59:20 +00005597 && s[i] == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005599 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 return 0; /* match with trailing slash */
5601 if (s == q)
5602 return -1; /* no match */
5603 return 1;
5604}
5605#endif
5606
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607/*
5608 * The putenv() implementation below comes from the "screen" program.
5609 * Included with permission from Juergen Weigert.
5610 * See pty.c for the copyright notice.
5611 */
5612
5613/*
5614 * putenv -- put value into environment
5615 *
5616 * Usage: i = putenv (string)
5617 * int i;
5618 * char *string;
5619 *
5620 * where string is of the form <name>=<value>.
5621 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5622 *
5623 * Putenv may need to add a new name into the environment, or to
5624 * associate a value longer than the current value with a particular
5625 * name. So, to make life simpler, putenv() copies your entire
5626 * environment into the heap (i.e. malloc()) from the stack
5627 * (i.e. where it resides when your process is initiated) the first
5628 * time you call it.
5629 *
5630 * (history removed, not very interesting. See the "screen" sources.)
5631 */
5632
5633#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5634
5635#define EXTRASIZE 5 /* increment to add to env. size */
5636
5637static int envsize = -1; /* current size of environment */
5638#ifndef MACOS_CLASSIC
5639extern
5640#endif
5641 char **environ; /* the global which is your env. */
5642
5643static int findenv __ARGS((char *name)); /* look for a name in the env. */
5644static int newenv __ARGS((void)); /* copy env. from stack to heap */
5645static int moreenv __ARGS((void)); /* incr. size of env. */
5646
5647 int
5648putenv(string)
5649 const char *string;
5650{
5651 int i;
5652 char *p;
5653
5654 if (envsize < 0)
5655 { /* first time putenv called */
5656 if (newenv() < 0) /* copy env. to heap */
5657 return -1;
5658 }
5659
5660 i = findenv((char *)string); /* look for name in environment */
5661
5662 if (i < 0)
5663 { /* name must be added */
5664 for (i = 0; environ[i]; i++);
5665 if (i >= (envsize - 1))
5666 { /* need new slot */
5667 if (moreenv() < 0)
5668 return -1;
5669 }
5670 p = (char *)alloc((unsigned)(strlen(string) + 1));
5671 if (p == NULL) /* not enough core */
5672 return -1;
5673 environ[i + 1] = 0; /* new end of env. */
5674 }
5675 else
5676 { /* name already in env. */
5677 p = vim_realloc(environ[i], strlen(string) + 1);
5678 if (p == NULL)
5679 return -1;
5680 }
5681 sprintf(p, "%s", string); /* copy into env. */
5682 environ[i] = p;
5683
5684 return 0;
5685}
5686
5687 static int
5688findenv(name)
5689 char *name;
5690{
5691 char *namechar, *envchar;
5692 int i, found;
5693
5694 found = 0;
5695 for (i = 0; environ[i] && !found; i++)
5696 {
5697 envchar = environ[i];
5698 namechar = name;
5699 while (*namechar && *namechar != '=' && (*namechar == *envchar))
5700 {
5701 namechar++;
5702 envchar++;
5703 }
5704 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
5705 }
5706 return found ? i - 1 : -1;
5707}
5708
5709 static int
5710newenv()
5711{
5712 char **env, *elem;
5713 int i, esize;
5714
5715#ifdef MACOS
5716 /* for Mac a new, empty environment is created */
5717 i = 0;
5718#else
5719 for (i = 0; environ[i]; i++)
5720 ;
5721#endif
5722 esize = i + EXTRASIZE + 1;
5723 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
5724 if (env == NULL)
5725 return -1;
5726
5727#ifndef MACOS
5728 for (i = 0; environ[i]; i++)
5729 {
5730 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
5731 if (elem == NULL)
5732 return -1;
5733 env[i] = elem;
5734 strcpy(elem, environ[i]);
5735 }
5736#endif
5737
5738 env[i] = 0;
5739 environ = env;
5740 envsize = esize;
5741 return 0;
5742}
5743
5744 static int
5745moreenv()
5746{
5747 int esize;
5748 char **env;
5749
5750 esize = envsize + EXTRASIZE;
5751 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
5752 if (env == 0)
5753 return -1;
5754 environ = env;
5755 envsize = esize;
5756 return 0;
5757}
5758
5759# ifdef USE_VIMPTY_GETENV
5760 char_u *
5761vimpty_getenv(string)
5762 const char_u *string;
5763{
5764 int i;
5765 char_u *p;
5766
5767 if (envsize < 0)
5768 return NULL;
5769
5770 i = findenv((char *)string);
5771
5772 if (i < 0)
5773 return NULL;
5774
5775 p = vim_strchr((char_u *)environ[i], '=');
5776 return (p + 1);
5777}
5778# endif
5779
5780#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00005781
Bram Moolenaarc4956c82006-03-12 21:58:43 +00005782#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00005783/*
5784 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
5785 * rights to write into.
5786 */
5787 int
5788filewritable(fname)
5789 char_u *fname;
5790{
5791 int retval = 0;
5792#if defined(UNIX) || defined(VMS)
5793 int perm = 0;
5794#endif
5795
5796#if defined(UNIX) || defined(VMS)
5797 perm = mch_getperm(fname);
5798#endif
5799#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
5800 if (
5801# ifdef WIN3264
5802 mch_writable(fname) &&
5803# else
5804# if defined(UNIX) || defined(VMS)
5805 (perm & 0222) &&
5806# endif
5807# endif
5808 mch_access((char *)fname, W_OK) == 0
5809 )
5810#endif
5811 {
5812 ++retval;
5813 if (mch_isdir(fname))
5814 ++retval;
5815 }
5816 return retval;
5817}
5818#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005819
5820/*
5821 * Print an error message with one or two "%s" and one or two string arguments.
5822 * This is not in message.c to avoid a warning for prototypes.
5823 */
5824 int
5825emsg3(s, a1, a2)
5826 char_u *s, *a1, *a2;
5827{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005828 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005829 return TRUE; /* no error messages at the moment */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005830 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2);
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005831 return emsg(IObuff);
5832}
5833
5834/*
5835 * Print an error message with one "%ld" and one long int argument.
5836 * This is not in message.c to avoid a warning for prototypes.
5837 */
5838 int
5839emsgn(s, n)
5840 char_u *s;
5841 long n;
5842{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005843 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005844 return TRUE; /* no error messages at the moment */
5845 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
5846 return emsg(IObuff);
5847}