blob: 6346e3fdf1bc4a77ee4406cb5db888d73591cdce [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
3416 if (shape_idx == -2
3417 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3418 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3419 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3420 return;
3421 if (shape_idx < 0)
3422 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3423 else
3424 new_mouse_shape = shape_table[shape_idx].mshape;
3425 if (new_mouse_shape != old_mouse_shape)
3426 {
3427 mch_set_mouse_shape(new_mouse_shape);
3428 old_mouse_shape = new_mouse_shape;
3429 }
3430 postponed_mouseshape = FALSE;
3431}
3432# endif
3433
3434#endif /* CURSOR_SHAPE */
3435
3436
3437#ifdef FEAT_CRYPT
3438/*
3439 * Optional encryption suypport.
3440 * Mohsin Ahmed, mosh@sasi.com, 98-09-24
3441 * Based on zip/crypt sources.
3442 *
3443 * NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to
3444 * most countries. There are a few exceptions, but that still should not be a
3445 * problem since this code was originally created in Europe and India.
3446 */
3447
3448/* from zip.h */
3449
3450typedef unsigned short ush; /* unsigned 16-bit value */
3451typedef unsigned long ulg; /* unsigned 32-bit value */
3452
3453static void make_crc_tab __ARGS((void));
3454
Bram Moolenaard6f676d2005-06-01 21:51:55 +00003455static ulg crc_32_tab[256];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457/*
3458 * Fill the CRC table.
3459 */
3460 static void
3461make_crc_tab()
3462{
3463 ulg s,t,v;
3464 static int done = FALSE;
3465
3466 if (done)
3467 return;
3468 for (t = 0; t < 256; t++)
3469 {
3470 v = t;
3471 for (s = 0; s < 8; s++)
3472 v = (v >> 1) ^ ((v & 1) * (ulg)0xedb88320L);
3473 crc_32_tab[t] = v;
3474 }
3475 done = TRUE;
3476}
3477
3478#define CRC32(c, b) (crc_32_tab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
3479
3480
3481static ulg keys[3]; /* keys defining the pseudo-random sequence */
3482
3483/*
3484 * Return the next byte in the pseudo-random sequence
3485 */
3486 int
3487decrypt_byte()
3488{
3489 ush temp;
3490
3491 temp = (ush)keys[2] | 2;
3492 return (int)(((unsigned)(temp * (temp ^ 1)) >> 8) & 0xff);
3493}
3494
3495/*
3496 * Update the encryption keys with the next byte of plain text
3497 */
3498 int
3499update_keys(c)
3500 int c; /* byte of plain text */
3501{
3502 keys[0] = CRC32(keys[0], c);
3503 keys[1] += keys[0] & 0xff;
3504 keys[1] = keys[1] * 134775813L + 1;
3505 keys[2] = CRC32(keys[2], (int)(keys[1] >> 24));
3506 return c;
3507}
3508
3509/*
3510 * Initialize the encryption keys and the random header according to
3511 * the given password.
3512 * If "passwd" is NULL or empty, don't do anything.
3513 */
3514 void
3515crypt_init_keys(passwd)
3516 char_u *passwd; /* password string with which to modify keys */
3517{
3518 if (passwd != NULL && *passwd != NUL)
3519 {
3520 make_crc_tab();
3521 keys[0] = 305419896L;
3522 keys[1] = 591751049L;
3523 keys[2] = 878082192L;
3524 while (*passwd != '\0')
3525 update_keys((int)*passwd++);
3526 }
3527}
3528
3529/*
3530 * Ask the user for a crypt key.
3531 * When "store" is TRUE, the new key in stored in the 'key' option, and the
3532 * 'key' option value is returned: Don't free it.
3533 * When "store" is FALSE, the typed key is returned in allocated memory.
3534 * Returns NULL on failure.
3535 */
3536 char_u *
3537get_crypt_key(store, twice)
3538 int store;
3539 int twice; /* Ask for the key twice. */
3540{
3541 char_u *p1, *p2 = NULL;
3542 int round;
3543
3544 for (round = 0; ; ++round)
3545 {
3546 cmdline_star = TRUE;
3547 cmdline_row = msg_row;
3548 p1 = getcmdline_prompt(NUL, round == 0
3549 ? (char_u *)_("Enter encryption key: ")
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003550 : (char_u *)_("Enter same key again: "), 0, EXPAND_NOTHING,
3551 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 cmdline_star = FALSE;
3553
3554 if (p1 == NULL)
3555 break;
3556
3557 if (round == twice)
3558 {
3559 if (p2 != NULL && STRCMP(p1, p2) != 0)
3560 {
3561 MSG(_("Keys don't match!"));
3562 vim_free(p1);
3563 vim_free(p2);
3564 p2 = NULL;
3565 round = -1; /* do it again */
3566 continue;
3567 }
3568 if (store)
3569 {
3570 set_option_value((char_u *)"key", 0L, p1, OPT_LOCAL);
3571 vim_free(p1);
3572 p1 = curbuf->b_p_key;
3573 }
3574 break;
3575 }
3576 p2 = p1;
3577 }
3578
3579 /* since the user typed this, no need to wait for return */
3580 need_wait_return = FALSE;
3581 msg_didout = FALSE;
3582
3583 vim_free(p2);
3584 return p1;
3585}
3586
3587#endif /* FEAT_CRYPT */
3588
3589/* TODO: make some #ifdef for this */
3590/*--------[ file searching ]-------------------------------------------------*/
3591/*
3592 * File searching functions for 'path', 'tags' and 'cdpath' options.
3593 * External visible functions:
3594 * vim_findfile_init() creates/initialises the search context
3595 * vim_findfile_free_visited() free list of visited files/dirs of search
3596 * context
3597 * vim_findfile() find a file in the search context
3598 * vim_findfile_cleanup() cleanup/free search context created by
3599 * vim_findfile_init()
3600 *
3601 * All static functions and variables start with 'ff_'
3602 *
3603 * In general it works like this:
3604 * First you create yourself a search context by calling vim_findfile_init().
3605 * It is possible to give a search context from a previous call to
3606 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
3607 * until you are satisfied with the result or it returns NULL. On every call it
3608 * returns the next file which matches the conditions given to
3609 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
3610 *
3611 * It is possible to call vim_findfile_init() again to reinitialise your search
3612 * with some new parameters. Don't forget to pass your old search context to
3613 * it, so it can reuse it and especially reuse the list of already visited
3614 * directories. If you want to delete the list of already visited directories
3615 * simply call vim_findfile_free_visited().
3616 *
3617 * When you are done call vim_findfile_cleanup() to free the search context.
3618 *
3619 * The function vim_findfile_init() has a long comment, which describes the
3620 * needed parameters.
3621 *
3622 *
3623 *
3624 * ATTENTION:
3625 * ==========
3626 * Also we use an allocated search context here, this functions ARE NOT
3627 * thread-safe!!!!!
3628 *
3629 * To minimize parameter passing (or because I'm to lazy), only the
3630 * external visible functions get a search context as a parameter. This is
3631 * then assigned to a static global, which is used throughout the local
3632 * functions.
3633 */
3634
3635/*
3636 * type for the directory search stack
3637 */
3638typedef struct ff_stack
3639{
3640 struct ff_stack *ffs_prev;
3641
3642 /* the fix part (no wildcards) and the part containing the wildcards
3643 * of the search path
3644 */
3645 char_u *ffs_fix_path;
3646#ifdef FEAT_PATH_EXTRA
3647 char_u *ffs_wc_path;
3648#endif
3649
3650 /* files/dirs found in the above directory, matched by the first wildcard
3651 * of wc_part
3652 */
3653 char_u **ffs_filearray;
3654 int ffs_filearray_size;
3655 char_u ffs_filearray_cur; /* needed for partly handled dirs */
3656
3657 /* to store status of partly handled directories
3658 * 0: we work the on this directory for the first time
3659 * 1: this directory was partly searched in an earlier step
3660 */
3661 int ffs_stage;
3662
3663 /* How deep are we in the directory tree?
3664 * Counts backward from value of level parameter to vim_findfile_init
3665 */
3666 int ffs_level;
3667
3668 /* Did we already expand '**' to an empty string? */
3669 int ffs_star_star_empty;
3670} ff_stack_T;
3671
3672/*
3673 * type for already visited directories or files.
3674 */
3675typedef struct ff_visited
3676{
3677 struct ff_visited *ffv_next;
3678
3679#ifdef FEAT_PATH_EXTRA
3680 /* Visited directories are different if the wildcard string are
3681 * different. So we have to save it.
3682 */
3683 char_u *ffv_wc_path;
3684#endif
3685 /* for unix use inode etc for comparison (needed because of links), else
3686 * use filename.
3687 */
3688#ifdef UNIX
3689 int ffv_dev; /* device number (-1 if not set) */
3690 ino_t ffv_ino; /* inode number */
3691#endif
3692 /* The memory for this struct is allocated according to the length of
3693 * ffv_fname.
3694 */
3695 char_u ffv_fname[1]; /* actually longer */
3696} ff_visited_T;
3697
3698/*
3699 * We might have to manage several visited lists during a search.
3700 * This is expecially needed for the tags option. If tags is set to:
3701 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
3702 * So we have to do 3 searches:
3703 * 1) search from the current files directory downward for the file "tags"
3704 * 2) search from the current files directory downward for the file "TAGS"
3705 * 3) search from Vims current directory downwards for the file "tags"
3706 * As you can see, the first and the third search are for the same file, so for
3707 * the third search we can use the visited list of the first search. For the
3708 * second search we must start from a empty visited list.
3709 * The struct ff_visited_list_hdr is used to manage a linked list of already
3710 * visited lists.
3711 */
3712typedef struct ff_visited_list_hdr
3713{
3714 struct ff_visited_list_hdr *ffvl_next;
3715
3716 /* the filename the attached visited list is for */
3717 char_u *ffvl_filename;
3718
3719 ff_visited_T *ffvl_visited_list;
3720
3721} ff_visited_list_hdr_T;
3722
3723
3724/*
3725 * '**' can be expanded to several directory levels.
3726 * Set the default maximium depth.
3727 */
3728#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
3729/*
3730 * The search context:
3731 * ffsc_stack_ptr: the stack for the dirs to search
3732 * ffsc_visited_list: the currently active visited list
3733 * ffsc_dir_visited_list: the currently active visited list for search dirs
3734 * ffsc_visited_lists_list: the list of all visited lists
3735 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
3736 * ffsc_file_to_search: the file to search for
3737 * ffsc_start_dir: the starting directory, if search path was relative
3738 * ffsc_fix_path: the fix part of the given path (without wildcards)
3739 * Needed for upward search.
3740 * ffsc_wc_path: the part of the given path containing wildcards
3741 * ffsc_level: how many levels of dirs to search downwards
3742 * ffsc_stopdirs_v: array of stop directories for upward search
3743 * ffsc_need_dir: TRUE if we search for a directory
3744 */
3745typedef struct ff_search_ctx_T
3746{
3747 ff_stack_T *ffsc_stack_ptr;
3748 ff_visited_list_hdr_T *ffsc_visited_list;
3749 ff_visited_list_hdr_T *ffsc_dir_visited_list;
3750 ff_visited_list_hdr_T *ffsc_visited_lists_list;
3751 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
3752 char_u *ffsc_file_to_search;
3753 char_u *ffsc_start_dir;
3754 char_u *ffsc_fix_path;
3755#ifdef FEAT_PATH_EXTRA
3756 char_u *ffsc_wc_path;
3757 int ffsc_level;
3758 char_u **ffsc_stopdirs_v;
3759#endif
3760 int ffsc_need_dir;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003761} ff_search_ctx_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003763static ff_search_ctx_T *ff_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765/* locally needed functions */
3766#ifdef FEAT_PATH_EXTRA
3767static int ff_check_visited __ARGS((ff_visited_T **, char_u *, char_u *));
3768#else
3769static int ff_check_visited __ARGS((ff_visited_T **, char_u *));
3770#endif
3771static void vim_findfile_free_visited_list __ARGS((ff_visited_list_hdr_T **list_headp));
3772static void ff_free_visited_list __ARGS((ff_visited_T *vl));
3773static ff_visited_list_hdr_T* ff_get_visited_list __ARGS((char_u *, ff_visited_list_hdr_T **list_headp));
3774#ifdef FEAT_PATH_EXTRA
3775static int ff_wc_equal __ARGS((char_u *s1, char_u *s2));
3776#endif
3777
3778static void ff_push __ARGS((ff_stack_T *));
3779static ff_stack_T * ff_pop __ARGS((void));
3780static void ff_clear __ARGS((void));
3781static void ff_free_stack_element __ARGS((ff_stack_T *));
3782#ifdef FEAT_PATH_EXTRA
3783static ff_stack_T *ff_create_stack_element __ARGS((char_u *, char_u *, int, int));
3784#else
3785static ff_stack_T *ff_create_stack_element __ARGS((char_u *, int, int));
3786#endif
3787#ifdef FEAT_PATH_EXTRA
3788static int ff_path_in_stoplist __ARGS((char_u *, int, char_u **));
3789#endif
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791#if 0
3792/*
3793 * if someone likes findfirst/findnext, here are the functions
3794 * NOT TESTED!!
3795 */
3796
3797static void *ff_fn_search_context = NULL;
3798
3799 char_u *
3800vim_findfirst(path, filename, level)
3801 char_u *path;
3802 char_u *filename;
3803 int level;
3804{
3805 ff_fn_search_context =
3806 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
3807 ff_fn_search_context, rel_fname);
3808 if (NULL == ff_fn_search_context)
3809 return NULL;
3810 else
3811 return vim_findnext()
3812}
3813
3814 char_u *
3815vim_findnext()
3816{
3817 char_u *ret = vim_findfile(ff_fn_search_context);
3818
3819 if (NULL == ret)
3820 {
3821 vim_findfile_cleanup(ff_fn_search_context);
3822 ff_fn_search_context = NULL;
3823 }
3824 return ret;
3825}
3826#endif
3827
3828/*
3829 * Initialization routine for vim_findfile.
3830 *
3831 * Returns the newly allocated search context or NULL if an error occured.
3832 *
3833 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
3834 * with the search context.
3835 *
3836 * Find the file 'filename' in the directory 'path'.
3837 * The parameter 'path' may contain wildcards. If so only search 'level'
3838 * directories deep. The parameter 'level' is the absolute maximum and is
3839 * not related to restricts given to the '**' wildcard. If 'level' is 100
3840 * and you use '**200' vim_findfile() will stop after 100 levels.
3841 *
3842 * If 'stopdirs' is not NULL and nothing is found downward, the search is
3843 * restarted on the next higher directory level. This is repeated until the
3844 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
3845 * format ";*<dirname>*\(;<dirname>\)*;\=$".
3846 *
3847 * If the 'path' is relative, the starting dir for the search is either VIM's
3848 * current dir or if the path starts with "./" the current files dir.
3849 * If the 'path' is absolut, the starting dir is that part of the path before
3850 * the first wildcard.
3851 *
3852 * Upward search is only done on the starting dir.
3853 *
3854 * If 'free_visited' is TRUE the list of already visited files/directories is
3855 * cleared. Set this to FALSE if you just want to search from another
3856 * directory, but want to be sure that no directory from a previous search is
3857 * searched again. This is useful if you search for a file at different places.
3858 * The list of visited files/dirs can also be cleared with the function
3859 * vim_findfile_free_visited().
3860 *
3861 * Set the parameter 'need_dir' to TRUE if you want to search for a directory
3862 * instead of a file.
3863 *
3864 * A search context returned by a previous call to vim_findfile_init() can be
3865 * passed in the parameter 'search_ctx'. This context is than reused and
3866 * reinitialized with the new parameters. The list of already viseted
3867 * directories from this context is only deleted if the parameter
3868 * 'free_visited' is true. Be aware that the passed search_context is freed if
3869 * the reinitialization fails.
3870 *
3871 * If you don't have a search context from a previous call 'search_ctx' must be
3872 * NULL.
3873 *
3874 * This function silently ignores a few errors, vim_findfile() will have
3875 * limited functionality then.
3876 */
3877/*ARGSUSED*/
3878 void *
3879vim_findfile_init(path, filename, stopdirs, level, free_visited, need_dir,
3880 search_ctx, tagfile, rel_fname)
3881 char_u *path;
3882 char_u *filename;
3883 char_u *stopdirs;
3884 int level;
3885 int free_visited;
3886 int need_dir;
3887 void *search_ctx;
3888 int tagfile;
3889 char_u *rel_fname; /* file name to use for "." */
3890{
3891#ifdef FEAT_PATH_EXTRA
3892 char_u *wc_part;
3893#endif
3894 ff_stack_T *sptr;
3895
3896 /* If a search context is given by the caller, reuse it, else allocate a
3897 * new one.
3898 */
3899 if (search_ctx != NULL)
3900 ff_search_ctx = search_ctx;
3901 else
3902 {
3903 ff_search_ctx = (ff_search_ctx_T*)alloc(
3904 (unsigned)sizeof(ff_search_ctx_T));
3905 if (ff_search_ctx == NULL)
3906 goto error_return;
3907 memset(ff_search_ctx, 0, sizeof(ff_search_ctx_T));
3908 }
3909
3910 /* clear the search context, but NOT the visited lists */
3911 ff_clear();
3912
3913 /* clear visited list if wanted */
3914 if (free_visited == TRUE)
3915 vim_findfile_free_visited(ff_search_ctx);
3916 else
3917 {
3918 /* Reuse old visited lists. Get the visited list for the given
3919 * filename. If no list for the current filename exists, creates a new
3920 * one.
3921 */
3922 ff_search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
3923 &ff_search_ctx->ffsc_visited_lists_list);
3924 if (ff_search_ctx->ffsc_visited_list == NULL)
3925 goto error_return;
3926 ff_search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
3927 &ff_search_ctx->ffsc_dir_visited_lists_list);
3928 if (ff_search_ctx->ffsc_dir_visited_list == NULL)
3929 goto error_return;
3930 }
3931
3932 if (ff_expand_buffer == NULL)
3933 {
3934 ff_expand_buffer = (char_u*)alloc(MAXPATHL);
3935 if (ff_expand_buffer == NULL)
3936 goto error_return;
3937 }
3938
3939 /* Store information on starting dir now if path is relative.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003940 * If path is absolute, we do that later. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if (path[0] == '.'
3942 && (vim_ispathsep(path[1]) || path[1] == NUL)
3943 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
3944 && rel_fname != NULL)
3945 {
3946 int len = (int)(gettail(rel_fname) - rel_fname);
3947
3948 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
3949 {
3950 /* Make the start dir an absolute path name. */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003951 vim_strncpy(ff_expand_buffer, rel_fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 ff_search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer,
3953 FALSE);
3954 }
3955 else
3956 ff_search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
3957 if (ff_search_ctx->ffsc_start_dir == NULL)
3958 goto error_return;
3959 if (*++path != NUL)
3960 ++path;
3961 }
3962 else if (*path == NUL || !vim_isAbsName(path))
3963 {
3964#ifdef BACKSLASH_IN_FILENAME
3965 /* "c:dir" needs "c:" to be expanded, otherwise use current dir */
3966 if (*path != NUL && path[1] == ':')
3967 {
3968 char_u drive[3];
3969
3970 drive[0] = path[0];
3971 drive[1] = ':';
3972 drive[2] = NUL;
3973 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
3974 goto error_return;
3975 path += 2;
3976 }
3977 else
3978#endif
3979 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
3980 goto error_return;
3981
3982 ff_search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
3983 if (ff_search_ctx->ffsc_start_dir == NULL)
3984 goto error_return;
3985
3986#ifdef BACKSLASH_IN_FILENAME
3987 /* A path that starts with "/dir" is relative to the drive, not to the
3988 * directory (but not for "//machine/dir"). Only use the drive name. */
3989 if ((*path == '/' || *path == '\\')
3990 && path[1] != path[0]
3991 && ff_search_ctx->ffsc_start_dir[1] == ':')
3992 ff_search_ctx->ffsc_start_dir[2] = NUL;
3993#endif
3994 }
3995
3996#ifdef FEAT_PATH_EXTRA
3997 /*
3998 * If stopdirs are given, split them into an array of pointers.
3999 * If this fails (mem allocation), there is no upward search at all or a
4000 * stop directory is not recognized -> continue silently.
4001 * If stopdirs just contains a ";" or is empty,
4002 * ff_search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
4003 * is handled as unlimited upward search. See function
4004 * ff_path_in_stoplist() for details.
4005 */
4006 if (stopdirs != NULL)
4007 {
4008 char_u *walker = stopdirs;
4009 int dircount;
4010
4011 while (*walker == ';')
4012 walker++;
4013
4014 dircount = 1;
4015 ff_search_ctx->ffsc_stopdirs_v =
4016 (char_u **)alloc((unsigned)sizeof(char_u *));
4017
4018 if (ff_search_ctx->ffsc_stopdirs_v != NULL)
4019 {
4020 do
4021 {
4022 char_u *helper;
4023 void *ptr;
4024
4025 helper = walker;
4026 ptr = vim_realloc(ff_search_ctx->ffsc_stopdirs_v,
4027 (dircount + 1) * sizeof(char_u *));
4028 if (ptr)
4029 ff_search_ctx->ffsc_stopdirs_v = ptr;
4030 else
4031 /* ignore, keep what we have and continue */
4032 break;
4033 walker = vim_strchr(walker, ';');
4034 if (walker)
4035 {
4036 ff_search_ctx->ffsc_stopdirs_v[dircount-1] =
4037 vim_strnsave(helper, (int)(walker - helper));
4038 walker++;
4039 }
4040 else
4041 /* this might be "", which means ascent till top
4042 * of directory tree.
4043 */
4044 ff_search_ctx->ffsc_stopdirs_v[dircount-1] =
4045 vim_strsave(helper);
4046
4047 dircount++;
4048
4049 } while (walker != NULL);
4050 ff_search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
4051 }
4052 }
4053#endif
4054
4055#ifdef FEAT_PATH_EXTRA
4056 ff_search_ctx->ffsc_level = level;
4057
4058 /* split into:
4059 * -fix path
4060 * -wildcard_stuff (might be NULL)
4061 */
4062 wc_part = vim_strchr(path, '*');
4063 if (wc_part != NULL)
4064 {
4065 int llevel;
4066 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004067 char *errpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 /* save the fix part of the path */
4070 ff_search_ctx->ffsc_fix_path = vim_strnsave(path,
4071 (int)(wc_part - path));
4072
4073 /*
4074 * copy wc_path and add restricts to the '**' wildcard.
4075 * The octett after a '**' is used as a (binary) counter.
4076 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
4077 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
4078 * For EBCDIC you get different character values.
4079 * If no restrict is given after '**' the default is used.
4080 * Due to this technic the path looks awful if you print it as a
4081 * string.
4082 */
4083 len = 0;
4084 while (*wc_part != NUL)
4085 {
4086 if (STRNCMP(wc_part, "**", 2) == 0)
4087 {
4088 ff_expand_buffer[len++] = *wc_part++;
4089 ff_expand_buffer[len++] = *wc_part++;
4090
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004091 llevel = strtol((char *)wc_part, &errpt, 10);
4092 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 ff_expand_buffer[len++] = llevel;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004094 else if ((char_u *)errpt != wc_part && llevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 /* restrict is 0 -> remove already added '**' */
4096 len -= 2;
4097 else
4098 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004099 wc_part = (char_u *)errpt;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004100 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
4103 goto error_return;
4104 }
4105 }
4106 else
4107 ff_expand_buffer[len++] = *wc_part++;
4108 }
4109 ff_expand_buffer[len] = NUL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004110 ff_search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 if (ff_search_ctx->ffsc_wc_path == NULL)
4113 goto error_return;
4114 }
4115 else
4116#endif
4117 ff_search_ctx->ffsc_fix_path = vim_strsave(path);
4118
4119 if (ff_search_ctx->ffsc_start_dir == NULL)
4120 {
4121 /* store the fix part as startdir.
4122 * This is needed if the parameter path is fully qualified.
4123 */
4124 ff_search_ctx->ffsc_start_dir = vim_strsave(ff_search_ctx->ffsc_fix_path);
4125 if (ff_search_ctx->ffsc_start_dir)
4126 ff_search_ctx->ffsc_fix_path[0] = NUL;
4127 }
4128
4129 /* create an absolute path */
4130 STRCPY(ff_expand_buffer, ff_search_ctx->ffsc_start_dir);
4131 add_pathsep(ff_expand_buffer);
4132 STRCAT(ff_expand_buffer, ff_search_ctx->ffsc_fix_path);
4133 add_pathsep(ff_expand_buffer);
4134
4135 sptr = ff_create_stack_element(ff_expand_buffer,
4136#ifdef FEAT_PATH_EXTRA
4137 ff_search_ctx->ffsc_wc_path,
4138#endif
4139 level, 0);
4140
4141 if (sptr == NULL)
4142 goto error_return;
4143
4144 ff_push(sptr);
4145
4146 ff_search_ctx->ffsc_file_to_search = vim_strsave(filename);
4147 if (ff_search_ctx->ffsc_file_to_search == NULL)
4148 goto error_return;
4149
4150 return ff_search_ctx;
4151
4152error_return:
4153 /*
4154 * We clear the search context now!
4155 * Even when the caller gave us a (perhaps valid) context we free it here,
4156 * as we might have already destroyed it.
4157 */
4158 vim_findfile_cleanup(ff_search_ctx);
4159 return NULL;
4160}
4161
4162#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
4163/*
4164 * Get the stopdir string. Check that ';' is not escaped.
4165 */
4166 char_u *
4167vim_findfile_stopdir(buf)
4168 char_u *buf;
4169{
4170 char_u *r_ptr = buf;
4171
4172 while (*r_ptr != NUL && *r_ptr != ';')
4173 {
4174 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
4175 {
4176 /* overwrite the escape char,
4177 * use STRLEN(r_ptr) to move the trailing '\0'
4178 */
4179 mch_memmove(r_ptr, r_ptr + 1, STRLEN(r_ptr));
4180 r_ptr++;
4181 }
4182 r_ptr++;
4183 }
4184 if (*r_ptr == ';')
4185 {
4186 *r_ptr = 0;
4187 r_ptr++;
4188 }
4189 else if (*r_ptr == NUL)
4190 r_ptr = NULL;
4191 return r_ptr;
4192}
4193#endif
4194
4195/* Clean up the given search context. Can handle a NULL pointer */
4196 void
4197vim_findfile_cleanup(ctx)
4198 void *ctx;
4199{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004200 if (ctx == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return;
4202
4203 ff_search_ctx = ctx;
4204
4205 vim_findfile_free_visited(ctx);
4206 ff_clear();
4207 vim_free(ctx);
4208 ff_search_ctx = NULL;
4209}
4210
4211/*
4212 * Find a file in a search context.
4213 * The search context was created with vim_findfile_init() above.
4214 * Return a pointer to an allocated file name or NULL if nothing found.
4215 * To get all matching files call this function until you get NULL.
4216 *
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004217 * If the passed search_context is NULL, NULL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 *
4219 * The search algorithm is depth first. To change this replace the
4220 * stack with a list (don't forget to leave partly searched directories on the
4221 * top of the list).
4222 */
4223 char_u *
4224vim_findfile(search_ctx)
4225 void *search_ctx;
4226{
4227 char_u *file_path;
4228#ifdef FEAT_PATH_EXTRA
4229 char_u *rest_of_wildcards;
4230 char_u *path_end = NULL;
4231#endif
4232 ff_stack_T *ctx;
4233#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
4234 int len;
4235#endif
4236 int i;
4237 char_u *p;
4238#ifdef FEAT_SEARCHPATH
4239 char_u *suf;
4240#endif
4241
4242 if (search_ctx == NULL)
4243 return NULL;
4244
4245 ff_search_ctx = (ff_search_ctx_T*)search_ctx;
4246
4247 /*
4248 * filepath is used as buffer for various actions and as the storage to
4249 * return a found filename.
4250 */
4251 if ((file_path = alloc((int)MAXPATHL)) == NULL)
4252 return NULL;
4253
4254#ifdef FEAT_PATH_EXTRA
4255 /* store the end of the start dir -- needed for upward search */
4256 if (ff_search_ctx->ffsc_start_dir != NULL)
4257 path_end = &ff_search_ctx->ffsc_start_dir[STRLEN(ff_search_ctx->ffsc_start_dir)];
4258#endif
4259
4260#ifdef FEAT_PATH_EXTRA
4261 /* upward search loop */
4262 for (;;)
4263 {
4264#endif
4265 /* downward search loop */
4266 for (;;)
4267 {
4268 /* check if user user wants to stop the search*/
4269 ui_breakcheck();
4270 if (got_int)
4271 break;
4272
4273 /* get directory to work on from stack */
4274 ctx = ff_pop();
4275 if (ctx == NULL)
4276 break;
4277
4278 /*
4279 * TODO: decide if we leave this test in
4280 *
4281 * GOOD: don't search a directory(-tree) twice.
4282 * BAD: - check linked list for every new directory entered.
4283 * - check for double files also done below
4284 *
4285 * Here we check if we already searched this directory.
4286 * We already searched a directory if:
4287 * 1) The directory is the same.
4288 * 2) We would use the same wildcard string.
4289 *
4290 * Good if you have links on same directory via several ways
4291 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
4292 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
4293 *
4294 * This check is only needed for directories we work on for the
4295 * first time (hence ctx->ff_filearray == NULL)
4296 */
4297 if (ctx->ffs_filearray == NULL
4298 && ff_check_visited(&ff_search_ctx->ffsc_dir_visited_list
4299 ->ffvl_visited_list,
4300 ctx->ffs_fix_path
4301#ifdef FEAT_PATH_EXTRA
4302 , ctx->ffs_wc_path
4303#endif
4304 ) == FAIL)
4305 {
4306#ifdef FF_VERBOSE
4307 if (p_verbose >= 5)
4308 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004309 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 smsg((char_u *)"Already Searched: %s (%s)",
4311 ctx->ffs_fix_path, ctx->ffs_wc_path);
4312 /* don't overwrite this either */
4313 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004314 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316#endif
4317 ff_free_stack_element(ctx);
4318 continue;
4319 }
4320#ifdef FF_VERBOSE
4321 else if (p_verbose >= 5)
4322 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004323 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004324 smsg((char_u *)"Searching: %s (%s)",
4325 ctx->ffs_fix_path, ctx->ffs_wc_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 /* don't overwrite this either */
4327 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004328 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330#endif
4331
4332 /* check depth */
4333 if (ctx->ffs_level <= 0)
4334 {
4335 ff_free_stack_element(ctx);
4336 continue;
4337 }
4338
4339 file_path[0] = NUL;
4340
4341 /*
4342 * If no filearray till now expand wildcards
4343 * The function expand_wildcards() can handle an array of paths
4344 * and all possible expands are returned in one array. We use this
4345 * to handle the expansion of '**' into an empty string.
4346 */
4347 if (ctx->ffs_filearray == NULL)
4348 {
4349 char_u *dirptrs[2];
4350
4351 /* we use filepath to build the path expand_wildcards() should
4352 * expand.
4353 */
4354 dirptrs[0] = file_path;
4355 dirptrs[1] = NULL;
4356
4357 /* if we have a start dir copy it in */
4358 if (!vim_isAbsName(ctx->ffs_fix_path)
4359 && ff_search_ctx->ffsc_start_dir)
4360 {
4361 STRCPY(file_path, ff_search_ctx->ffsc_start_dir);
4362 add_pathsep(file_path);
4363 }
4364
4365 /* append the fix part of the search path */
4366 STRCAT(file_path, ctx->ffs_fix_path);
4367 add_pathsep(file_path);
4368
4369#ifdef FEAT_PATH_EXTRA
4370 rest_of_wildcards = ctx->ffs_wc_path;
4371 if (*rest_of_wildcards != NUL)
4372 {
4373 len = (int)STRLEN(file_path);
4374 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
4375 {
4376 /* pointer to the restrict byte
4377 * The restrict byte is not a character!
4378 */
4379 p = rest_of_wildcards + 2;
4380
4381 if (*p > 0)
4382 {
4383 (*p)--;
4384 file_path[len++] = '*';
4385 }
4386
4387 if (*p == 0)
4388 {
4389 /* remove '**<numb> from wildcards */
4390 mch_memmove(rest_of_wildcards,
4391 rest_of_wildcards + 3,
4392 STRLEN(rest_of_wildcards + 3) + 1);
4393 }
4394 else
4395 rest_of_wildcards += 3;
4396
4397 if (ctx->ffs_star_star_empty == 0)
4398 {
4399 /* if not done before, expand '**' to empty */
4400 ctx->ffs_star_star_empty = 1;
4401 dirptrs[1] = ctx->ffs_fix_path;
4402 }
4403 }
4404
4405 /*
4406 * Here we copy until the next path separator or the end of
4407 * the path. If we stop at a path separator, there is
4408 * still somthing else left. This is handled below by
4409 * pushing every directory returned from expand_wildcards()
4410 * on the stack again for further search.
4411 */
4412 while (*rest_of_wildcards
4413 && !vim_ispathsep(*rest_of_wildcards))
4414 file_path[len++] = *rest_of_wildcards++;
4415
4416 file_path[len] = NUL;
4417 if (vim_ispathsep(*rest_of_wildcards))
4418 rest_of_wildcards++;
4419 }
4420#endif
4421
4422 /*
4423 * Expand wildcards like "*" and "$VAR".
4424 * If the path is a URL don't try this.
4425 */
4426 if (path_with_url(dirptrs[0]))
4427 {
4428 ctx->ffs_filearray = (char_u **)
4429 alloc((unsigned)sizeof(char *));
4430 if (ctx->ffs_filearray != NULL
4431 && (ctx->ffs_filearray[0]
4432 = vim_strsave(dirptrs[0])) != NULL)
4433 ctx->ffs_filearray_size = 1;
4434 else
4435 ctx->ffs_filearray_size = 0;
4436 }
4437 else
4438 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
4439 &ctx->ffs_filearray_size,
4440 &ctx->ffs_filearray,
4441 EW_DIR|EW_ADDSLASH|EW_SILENT);
4442
4443 ctx->ffs_filearray_cur = 0;
4444 ctx->ffs_stage = 0;
4445 }
4446#ifdef FEAT_PATH_EXTRA
4447 else
4448 rest_of_wildcards = &ctx->ffs_wc_path[STRLEN(ctx->ffs_wc_path)];
4449#endif
4450
4451 if (ctx->ffs_stage == 0)
4452 {
4453 /* this is the first time we work on this directory */
4454#ifdef FEAT_PATH_EXTRA
4455 if (*rest_of_wildcards == NUL)
4456#endif
4457 {
4458 /*
4459 * we don't have further wildcards to expand, so we have to
4460 * check for the final file now
4461 */
4462 for (i = ctx->ffs_filearray_cur;
4463 i < ctx->ffs_filearray_size; ++i)
4464 {
4465 if (!path_with_url(ctx->ffs_filearray[i])
4466 && !mch_isdir(ctx->ffs_filearray[i]))
4467 continue; /* not a directory */
4468
4469 /* prepare the filename to be checked for existance
4470 * below */
4471 STRCPY(file_path, ctx->ffs_filearray[i]);
4472 add_pathsep(file_path);
4473 STRCAT(file_path, ff_search_ctx->ffsc_file_to_search);
4474
4475 /*
4476 * Try without extra suffix and then with suffixes
4477 * from 'suffixesadd'.
4478 */
4479#ifdef FEAT_SEARCHPATH
4480 len = (int)STRLEN(file_path);
4481 suf = curbuf->b_p_sua;
4482 for (;;)
4483#endif
4484 {
4485 /* if file exists and we didn't already find it */
4486 if ((path_with_url(file_path)
4487 || (mch_getperm(file_path) >= 0
4488 && (!ff_search_ctx->ffsc_need_dir
4489 || mch_isdir(file_path))))
4490#ifndef FF_VERBOSE
4491 && (ff_check_visited(
4492 &ff_search_ctx->ffsc_visited_list->ffvl_visited_list,
4493 file_path
4494#ifdef FEAT_PATH_EXTRA
4495 , (char_u *)""
4496#endif
4497 ) == OK)
4498#endif
4499 )
4500 {
4501#ifdef FF_VERBOSE
4502 if (ff_check_visited(
4503 &ff_search_ctx->ffsc_visited_list->ffvl_visited_list,
4504 file_path
4505#ifdef FEAT_PATH_EXTRA
4506 , (char_u *)""
4507#endif
4508 ) == FAIL)
4509 {
4510 if (p_verbose >= 5)
4511 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004512 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004513 smsg((char_u *)"Already: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 file_path);
4515 /* don't overwrite this either */
4516 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004517 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519 continue;
4520 }
4521#endif
4522
4523 /* push dir to examine rest of subdirs later */
4524 ctx->ffs_filearray_cur = i + 1;
4525 ff_push(ctx);
4526
4527 simplify_filename(file_path);
4528 if (mch_dirname(ff_expand_buffer, MAXPATHL)
4529 == OK)
4530 {
4531 p = shorten_fname(file_path,
4532 ff_expand_buffer);
4533 if (p != NULL)
4534 mch_memmove(file_path, p,
4535 STRLEN(p) + 1);
4536 }
4537#ifdef FF_VERBOSE
4538 if (p_verbose >= 5)
4539 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004540 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004541 smsg((char_u *)"HIT: %s", file_path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 /* don't overwrite this either */
4543 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004544 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546#endif
4547 return file_path;
4548 }
4549
4550#ifdef FEAT_SEARCHPATH
4551 /* Not found or found already, try next suffix. */
4552 if (*suf == NUL)
4553 break;
4554 copy_option_part(&suf, file_path + len,
4555 MAXPATHL - len, ",");
4556#endif
4557 }
4558 }
4559 }
4560#ifdef FEAT_PATH_EXTRA
4561 else
4562 {
4563 /*
4564 * still wildcards left, push the directories for further
4565 * search
4566 */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004567 for (i = ctx->ffs_filearray_cur;
4568 i < ctx->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
4570 if (!mch_isdir(ctx->ffs_filearray[i]))
4571 continue; /* not a directory */
4572
4573 ff_push(ff_create_stack_element(ctx->ffs_filearray[i],
4574 rest_of_wildcards, ctx->ffs_level - 1, 0));
4575 }
4576 }
4577#endif
4578 ctx->ffs_filearray_cur = 0;
4579 ctx->ffs_stage = 1;
4580 }
4581
4582#ifdef FEAT_PATH_EXTRA
4583 /*
4584 * if wildcards contains '**' we have to descent till we reach the
4585 * leaves of the directory tree.
4586 */
4587 if (STRNCMP(ctx->ffs_wc_path, "**", 2) == 0)
4588 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004589 for (i = ctx->ffs_filearray_cur;
4590 i < ctx->ffs_filearray_size; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 if (fnamecmp(ctx->ffs_filearray[i], ctx->ffs_fix_path) == 0)
4593 continue; /* don't repush same directory */
4594 if (!mch_isdir(ctx->ffs_filearray[i]))
4595 continue; /* not a directory */
4596 ff_push(ff_create_stack_element(ctx->ffs_filearray[i],
4597 ctx->ffs_wc_path, ctx->ffs_level - 1, 1));
4598 }
4599 }
4600#endif
4601
4602 /* we are done with the current directory */
4603 ff_free_stack_element(ctx);
4604
4605 }
4606
4607#ifdef FEAT_PATH_EXTRA
4608 /* If we reached this, we didn't find anything downwards.
4609 * Let's check if we should do an upward search.
4610 */
4611 if (ff_search_ctx->ffsc_start_dir
4612 && ff_search_ctx->ffsc_stopdirs_v != NULL && !got_int)
4613 {
4614 ff_stack_T *sptr;
4615
4616 /* is the last starting directory in the stop list? */
4617 if (ff_path_in_stoplist(ff_search_ctx->ffsc_start_dir,
4618 (int)(path_end - ff_search_ctx->ffsc_start_dir),
4619 ff_search_ctx->ffsc_stopdirs_v) == TRUE)
4620 break;
4621
4622 /* cut of last dir */
4623 while (path_end > ff_search_ctx->ffsc_start_dir
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004624 && vim_ispathsep(*path_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 path_end--;
4626 while (path_end > ff_search_ctx->ffsc_start_dir
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00004627 && !vim_ispathsep(path_end[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 path_end--;
4629 *path_end = 0;
4630 path_end--;
4631
4632 if (*ff_search_ctx->ffsc_start_dir == 0)
4633 break;
4634
4635 STRCPY(file_path, ff_search_ctx->ffsc_start_dir);
4636 add_pathsep(file_path);
4637 STRCAT(file_path, ff_search_ctx->ffsc_fix_path);
4638
4639 /* create a new stack entry */
4640 sptr = ff_create_stack_element(file_path,
4641 ff_search_ctx->ffsc_wc_path, ff_search_ctx->ffsc_level, 0);
4642 if (sptr == NULL)
4643 break;
4644 ff_push(sptr);
4645 }
4646 else
4647 break;
4648 }
4649#endif
4650
4651 vim_free(file_path);
4652 return NULL;
4653}
4654
4655/*
4656 * Free the list of lists of visited files and directories
4657 * Can handle it if the passed search_context is NULL;
4658 */
4659 void
4660vim_findfile_free_visited(search_ctx)
4661 void *search_ctx;
4662{
4663 if (search_ctx == NULL)
4664 return;
4665
4666 ff_search_ctx = (ff_search_ctx_T *)search_ctx;
4667
4668 vim_findfile_free_visited_list(&ff_search_ctx->ffsc_visited_lists_list);
4669 vim_findfile_free_visited_list(&ff_search_ctx->ffsc_dir_visited_lists_list);
4670}
4671
4672 static void
4673vim_findfile_free_visited_list(list_headp)
4674 ff_visited_list_hdr_T **list_headp;
4675{
4676 ff_visited_list_hdr_T *vp;
4677
4678 while (*list_headp != NULL)
4679 {
4680 vp = (*list_headp)->ffvl_next;
4681 ff_free_visited_list((*list_headp)->ffvl_visited_list);
4682
4683 vim_free((*list_headp)->ffvl_filename);
4684 vim_free(*list_headp);
4685 *list_headp = vp;
4686 }
4687 *list_headp = NULL;
4688}
4689
4690 static void
4691ff_free_visited_list(vl)
4692 ff_visited_T *vl;
4693{
4694 ff_visited_T *vp;
4695
4696 while (vl != NULL)
4697 {
4698 vp = vl->ffv_next;
4699#ifdef FEAT_PATH_EXTRA
4700 vim_free(vl->ffv_wc_path);
4701#endif
4702 vim_free(vl);
4703 vl = vp;
4704 }
4705 vl = NULL;
4706}
4707
4708/*
4709 * Returns the already visited list for the given filename. If none is found it
4710 * allocates a new one.
4711 */
4712 static ff_visited_list_hdr_T*
4713ff_get_visited_list(filename, list_headp)
4714 char_u *filename;
4715 ff_visited_list_hdr_T **list_headp;
4716{
4717 ff_visited_list_hdr_T *retptr = NULL;
4718
4719 /* check if a visited list for the given filename exists */
4720 if (*list_headp != NULL)
4721 {
4722 retptr = *list_headp;
4723 while (retptr != NULL)
4724 {
4725 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
4726 {
4727#ifdef FF_VERBOSE
4728 if (p_verbose >= 5)
4729 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004730 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004731 smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 filename);
4733 /* don't overwrite this either */
4734 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004735 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737#endif
4738 return retptr;
4739 }
4740 retptr = retptr->ffvl_next;
4741 }
4742 }
4743
4744#ifdef FF_VERBOSE
4745 if (p_verbose >= 5)
4746 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004747 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00004748 smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 /* don't overwrite this either */
4750 msg_puts((char_u *)"\n");
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00004751 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753#endif
4754
4755 /*
4756 * if we reach this we didn't find a list and we have to allocate new list
4757 */
4758 retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
4759 if (retptr == NULL)
4760 return NULL;
4761
4762 retptr->ffvl_visited_list = NULL;
4763 retptr->ffvl_filename = vim_strsave(filename);
4764 if (retptr->ffvl_filename == NULL)
4765 {
4766 vim_free(retptr);
4767 return NULL;
4768 }
4769 retptr->ffvl_next = *list_headp;
4770 *list_headp = retptr;
4771
4772 return retptr;
4773}
4774
4775#ifdef FEAT_PATH_EXTRA
4776/*
4777 * check if two wildcard paths are equal. Returns TRUE or FALSE.
4778 * They are equal if:
4779 * - both paths are NULL
4780 * - they have the same length
4781 * - char by char comparison is OK
4782 * - the only differences are in the counters behind a '**', so
4783 * '**\20' is equal to '**\24'
4784 */
4785 static int
4786ff_wc_equal(s1, s2)
4787 char_u *s1;
4788 char_u *s2;
4789{
4790 int i;
4791
4792 if (s1 == s2)
4793 return TRUE;
4794
4795 if (s1 == NULL || s2 == NULL)
4796 return FALSE;
4797
4798 if (STRLEN(s1) != STRLEN(s2))
4799 return FAIL;
4800
4801 for (i = 0; s1[i] != NUL && s2[i] != NUL; i++)
4802 {
4803 if (s1[i] != s2[i]
4804#ifdef CASE_INSENSITIVE_FILENAME
4805 && TOUPPER_LOC(s1[i]) != TOUPPER_LOC(s2[i])
4806#endif
4807 )
4808 {
4809 if (i >= 2)
4810 if (s1[i-1] == '*' && s1[i-2] == '*')
4811 continue;
4812 else
4813 return FAIL;
4814 else
4815 return FAIL;
4816 }
4817 }
4818 return TRUE;
4819}
4820#endif
4821
4822/*
4823 * maintains the list of already visited files and dirs
4824 * returns FAIL if the given file/dir is already in the list
4825 * returns OK if it is newly added
4826 *
4827 * TODO: What to do on memory allocation problems?
4828 * -> return TRUE - Better the file is found several times instead of
4829 * never.
4830 */
4831 static int
4832ff_check_visited(visited_list, fname
4833#ifdef FEAT_PATH_EXTRA
4834 , wc_path
4835#endif
4836 )
4837 ff_visited_T **visited_list;
4838 char_u *fname;
4839#ifdef FEAT_PATH_EXTRA
4840 char_u *wc_path;
4841#endif
4842{
4843 ff_visited_T *vp;
4844#ifdef UNIX
4845 struct stat st;
4846 int url = FALSE;
4847#endif
4848
4849 /* For an URL we only compare the name, otherwise we compare the
4850 * device/inode (unix) or the full path name (not Unix). */
4851 if (path_with_url(fname))
4852 {
Bram Moolenaarbbebc852005-07-18 21:47:53 +00004853 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#ifdef UNIX
4855 url = TRUE;
4856#endif
4857 }
4858 else
4859 {
4860 ff_expand_buffer[0] = NUL;
4861#ifdef UNIX
4862 if (mch_stat((char *)fname, &st) < 0)
4863#else
4864 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
4865#endif
4866 return FAIL;
4867 }
4868
4869 /* check against list of already visited files */
4870 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
4871 {
4872 if (
4873#ifdef UNIX
4874 !url
4875 ? (vp->ffv_dev == st.st_dev
4876 && vp->ffv_ino == st.st_ino)
4877 :
4878#endif
4879 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
4880 )
4881 {
4882#ifdef FEAT_PATH_EXTRA
4883 /* are the wildcard parts equal */
4884 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
4885#endif
4886 /* already visited */
4887 return FAIL;
4888 }
4889 }
4890
4891 /*
4892 * New file/dir. Add it to the list of visited files/dirs.
4893 */
4894 vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
4895 + STRLEN(ff_expand_buffer)));
4896
4897 if (vp != NULL)
4898 {
4899#ifdef UNIX
4900 if (!url)
4901 {
4902 vp->ffv_ino = st.st_ino;
4903 vp->ffv_dev = st.st_dev;
4904 vp->ffv_fname[0] = NUL;
4905 }
4906 else
4907 {
4908 vp->ffv_ino = 0;
4909 vp->ffv_dev = -1;
4910#endif
4911 STRCPY(vp->ffv_fname, ff_expand_buffer);
4912#ifdef UNIX
4913 }
4914#endif
4915#ifdef FEAT_PATH_EXTRA
4916 if (wc_path != NULL)
4917 vp->ffv_wc_path = vim_strsave(wc_path);
4918 else
4919 vp->ffv_wc_path = NULL;
4920#endif
4921
4922 vp->ffv_next = *visited_list;
4923 *visited_list = vp;
4924 }
4925
4926 return OK;
4927}
4928
4929/*
4930 * create stack element from given path pieces
4931 */
4932 static ff_stack_T *
4933ff_create_stack_element(fix_part,
4934#ifdef FEAT_PATH_EXTRA
4935 wc_part,
4936#endif
4937 level, star_star_empty)
4938 char_u *fix_part;
4939#ifdef FEAT_PATH_EXTRA
4940 char_u *wc_part;
4941#endif
4942 int level;
4943 int star_star_empty;
4944{
4945 ff_stack_T *new;
4946
4947 new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
4948 if (new == NULL)
4949 return NULL;
4950
4951 new->ffs_prev = NULL;
4952 new->ffs_filearray = NULL;
4953 new->ffs_filearray_size = 0;
4954 new->ffs_filearray_cur = 0;
4955 new->ffs_stage = 0;
4956 new->ffs_level = level;
4957 new->ffs_star_star_empty = star_star_empty;;
4958
4959 /* the following saves NULL pointer checks in vim_findfile */
4960 if (fix_part == NULL)
4961 fix_part = (char_u *)"";
4962 new->ffs_fix_path = vim_strsave(fix_part);
4963
4964#ifdef FEAT_PATH_EXTRA
4965 if (wc_part == NULL)
4966 wc_part = (char_u *)"";
4967 new->ffs_wc_path = vim_strsave(wc_part);
4968#endif
4969
4970 if (new->ffs_fix_path == NULL
4971#ifdef FEAT_PATH_EXTRA
4972 || new->ffs_wc_path == NULL
4973#endif
4974 )
4975 {
4976 ff_free_stack_element(new);
4977 new = NULL;
4978 }
4979
4980 return new;
4981}
4982
4983/*
4984 * push a dir on the directory stack
4985 */
4986 static void
4987ff_push(ctx)
4988 ff_stack_T *ctx;
4989{
4990 /* check for NULL pointer, not to return an error to the user, but
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004991 * to prevent a crash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (ctx != NULL)
4993 {
4994 ctx->ffs_prev = ff_search_ctx->ffsc_stack_ptr;
4995 ff_search_ctx->ffsc_stack_ptr = ctx;
4996 }
4997}
4998
4999/*
5000 * pop a dir from the directory stack
5001 * returns NULL if stack is empty
5002 */
5003 static ff_stack_T *
5004ff_pop()
5005{
5006 ff_stack_T *sptr;
5007
5008 sptr = ff_search_ctx->ffsc_stack_ptr;
5009 if (ff_search_ctx->ffsc_stack_ptr != NULL)
5010 ff_search_ctx->ffsc_stack_ptr = ff_search_ctx->ffsc_stack_ptr->ffs_prev;
5011
5012 return sptr;
5013}
5014
5015/*
5016 * free the given stack element
5017 */
5018 static void
5019ff_free_stack_element(ctx)
5020 ff_stack_T *ctx;
5021{
5022 /* vim_free handles possible NULL pointers */
5023 vim_free(ctx->ffs_fix_path);
5024#ifdef FEAT_PATH_EXTRA
5025 vim_free(ctx->ffs_wc_path);
5026#endif
5027
5028 if (ctx->ffs_filearray != NULL)
5029 FreeWild(ctx->ffs_filearray_size, ctx->ffs_filearray);
5030
5031 vim_free(ctx);
5032}
5033
5034/*
5035 * clear the search context
5036 */
5037 static void
5038ff_clear()
5039{
5040 ff_stack_T *sptr;
5041
5042 /* clear up stack */
5043 while ((sptr = ff_pop()) != NULL)
5044 ff_free_stack_element(sptr);
5045
5046 vim_free(ff_search_ctx->ffsc_file_to_search);
5047 vim_free(ff_search_ctx->ffsc_start_dir);
5048 vim_free(ff_search_ctx->ffsc_fix_path);
5049#ifdef FEAT_PATH_EXTRA
5050 vim_free(ff_search_ctx->ffsc_wc_path);
5051#endif
5052
5053#ifdef FEAT_PATH_EXTRA
5054 if (ff_search_ctx->ffsc_stopdirs_v != NULL)
5055 {
5056 int i = 0;
5057
5058 while (ff_search_ctx->ffsc_stopdirs_v[i] != NULL)
5059 {
5060 vim_free(ff_search_ctx->ffsc_stopdirs_v[i]);
5061 i++;
5062 }
5063 vim_free(ff_search_ctx->ffsc_stopdirs_v);
5064 }
5065 ff_search_ctx->ffsc_stopdirs_v = NULL;
5066#endif
5067
5068 /* reset everything */
5069 ff_search_ctx->ffsc_file_to_search = NULL;
5070 ff_search_ctx->ffsc_start_dir = NULL;
5071 ff_search_ctx->ffsc_fix_path = NULL;
5072#ifdef FEAT_PATH_EXTRA
5073 ff_search_ctx->ffsc_wc_path = NULL;
5074 ff_search_ctx->ffsc_level = 0;
5075#endif
5076}
5077
5078#ifdef FEAT_PATH_EXTRA
5079/*
5080 * check if the given path is in the stopdirs
5081 * returns TRUE if yes else FALSE
5082 */
5083 static int
5084ff_path_in_stoplist(path, path_len, stopdirs_v)
5085 char_u *path;
5086 int path_len;
5087 char_u **stopdirs_v;
5088{
5089 int i = 0;
5090
5091 /* eat up trailing path separators, except the first */
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005092 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 path_len--;
5094
5095 /* if no path consider it as match */
5096 if (path_len == 0)
5097 return TRUE;
5098
5099 for (i = 0; stopdirs_v[i] != NULL; i++)
5100 {
5101 if ((int)STRLEN(stopdirs_v[i]) > path_len)
5102 {
5103 /* match for parent directory. So '/home' also matches
5104 * '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
5105 * '/home/r' would also match '/home/rks'
5106 */
5107 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005108 && vim_ispathsep(stopdirs_v[i][path_len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return TRUE;
5110 }
5111 else
5112 {
5113 if (fnamecmp(stopdirs_v[i], path) == 0)
5114 return TRUE;
5115 }
5116 }
5117 return FALSE;
5118}
5119#endif
5120
5121#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5122/*
5123 * Find the file name "ptr[len]" in the path.
5124 *
5125 * On the first call set the parameter 'first' to TRUE to initialize
5126 * the search. For repeating calls to FALSE.
5127 *
5128 * Repeating calls will return other files called 'ptr[len]' from the path.
5129 *
5130 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
5131 * don't need valid values.
5132 *
5133 * If nothing found on the first call the option FNAME_MESS will issue the
5134 * message:
5135 * 'Can't find file "<file>" in path'
5136 * On repeating calls:
5137 * 'No more file "<file>" found in path'
5138 *
5139 * options:
5140 * FNAME_MESS give error message when not found
5141 *
5142 * Uses NameBuff[]!
5143 *
5144 * Returns an allocated string for the file name. NULL for error.
5145 *
5146 */
5147 char_u *
5148find_file_in_path(ptr, len, options, first, rel_fname)
5149 char_u *ptr; /* file name */
5150 int len; /* length of file name */
5151 int options;
5152 int first; /* use count'th matching file name */
5153 char_u *rel_fname; /* file name searching relative to */
5154{
5155 return find_file_in_path_option(ptr, len, options, first,
5156 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005157 FALSE, rel_fname, curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158}
5159
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005160static char_u *ff_file_to_find = NULL;
5161static void *fdip_search_ctx = NULL;
5162
5163#if defined(EXITFREE)
5164 static void
5165free_findfile()
5166{
5167 vim_free(ff_file_to_find);
5168 vim_findfile_cleanup(fdip_search_ctx);
5169}
5170#endif
5171
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172/*
5173 * Find the directory name "ptr[len]" in the path.
5174 *
5175 * options:
5176 * FNAME_MESS give error message when not found
5177 *
5178 * Uses NameBuff[]!
5179 *
5180 * Returns an allocated string for the file name. NULL for error.
5181 */
5182 char_u *
5183find_directory_in_path(ptr, len, options, rel_fname)
5184 char_u *ptr; /* file name */
5185 int len; /* length of file name */
5186 int options;
5187 char_u *rel_fname; /* file name searching relative to */
5188{
5189 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005190 TRUE, rel_fname, (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191}
5192
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005193 char_u *
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005194find_file_in_path_option(ptr, len, options, first, path_option, need_dir, rel_fname, suffixes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 char_u *ptr; /* file name */
5196 int len; /* length of file name */
5197 int options;
5198 int first; /* use count'th matching file name */
5199 char_u *path_option; /* p_path or p_cdpath */
5200 int need_dir; /* looking for directory name */
5201 char_u *rel_fname; /* file name we are looking relative to. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005202 char_u *suffixes; /* list of suffixes, 'suffixesadd' option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 static char_u *dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 static int did_findfile_init = FALSE;
5206 char_u save_char;
5207 char_u *file_name = NULL;
5208 char_u *buf = NULL;
5209 int rel_to_curdir;
5210#ifdef AMIGA
5211 struct Process *proc = (struct Process *)FindTask(0L);
5212 APTR save_winptr = proc->pr_WindowPtr;
5213
5214 /* Avoid a requester here for a volume that doesn't exist. */
5215 proc->pr_WindowPtr = (APTR)-1L;
5216#endif
5217
5218 if (first == TRUE)
5219 {
5220 /* copy file name into NameBuff, expanding environment variables */
5221 save_char = ptr[len];
5222 ptr[len] = NUL;
5223 expand_env(ptr, NameBuff, MAXPATHL);
5224 ptr[len] = save_char;
5225
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005226 vim_free(ff_file_to_find);
5227 ff_file_to_find = vim_strsave(NameBuff);
5228 if (ff_file_to_find == NULL) /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 file_name = NULL;
5231 goto theend;
5232 }
5233 }
5234
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005235 rel_to_curdir = (ff_file_to_find[0] == '.'
5236 && (ff_file_to_find[1] == NUL
5237 || vim_ispathsep(ff_file_to_find[1])
5238 || (ff_file_to_find[1] == '.'
5239 && (ff_file_to_find[2] == NUL
5240 || vim_ispathsep(ff_file_to_find[2])))));
5241 if (vim_isAbsName(ff_file_to_find)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 /* "..", "../path", "." and "./path": don't use the path_option */
5243 || rel_to_curdir
5244#if defined(MSWIN) || defined(MSDOS) || defined(OS2)
5245 /* handle "\tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005246 || vim_ispathsep(ff_file_to_find[0])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 /* handle "c:name" as absulute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005248 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#endif
5250#ifdef AMIGA
5251 /* handle ":tmp" as absolute path */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005252 || ff_file_to_find[0] == ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#endif
5254 )
5255 {
5256 /*
5257 * Absolute path, no need to use "path_option".
5258 * If this is not a first call, return NULL. We already returned a
5259 * filename on the first call.
5260 */
5261 if (first == TRUE)
5262 {
5263 int l;
5264 int run;
5265
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005266 if (path_with_url(ff_file_to_find))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005268 file_name = vim_strsave(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 goto theend;
5270 }
5271
5272 /* When FNAME_REL flag given first use the directory of the file.
5273 * Otherwise or when this fails use the current directory. */
5274 for (run = 1; run <= 2; ++run)
5275 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005276 l = (int)STRLEN(ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 if (run == 1
5278 && rel_to_curdir
5279 && (options & FNAME_REL)
5280 && rel_fname != NULL
5281 && STRLEN(rel_fname) + l < MAXPATHL)
5282 {
5283 STRCPY(NameBuff, rel_fname);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005284 STRCPY(gettail(NameBuff), ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 l = (int)STRLEN(NameBuff);
5286 }
5287 else
5288 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005289 STRCPY(NameBuff, ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 run = 2;
5291 }
5292
5293 /* When the file doesn't exist, try adding parts of
5294 * 'suffixesadd'. */
Bram Moolenaar433f7c82006-03-21 21:29:36 +00005295 buf = suffixes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 for (;;)
5297 {
5298 if (
5299#ifdef DJGPP
5300 /* "C:" by itself will fail for mch_getperm(),
5301 * assume it's always valid. */
5302 (need_dir && NameBuff[0] != NUL
5303 && NameBuff[1] == ':'
5304 && NameBuff[2] == NUL) ||
5305#endif
5306 (mch_getperm(NameBuff) >= 0
5307 && (!need_dir || mch_isdir(NameBuff))))
5308 {
5309 file_name = vim_strsave(NameBuff);
5310 goto theend;
5311 }
5312 if (*buf == NUL)
5313 break;
5314 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
5315 }
5316 }
5317 }
5318 }
5319 else
5320 {
5321 /*
5322 * Loop over all paths in the 'path' or 'cdpath' option.
5323 * When "first" is set, first setup to the start of the option.
5324 * Otherwise continue to find the next match.
5325 */
5326 if (first == TRUE)
5327 {
5328 /* vim_findfile_free_visited can handle a possible NULL pointer */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005329 vim_findfile_free_visited(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 dir = path_option;
5331 did_findfile_init = FALSE;
5332 }
5333
5334 for (;;)
5335 {
5336 if (did_findfile_init)
5337 {
5338 ff_search_ctx->ffsc_need_dir = need_dir;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005339 file_name = vim_findfile(fdip_search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 ff_search_ctx->ffsc_need_dir = FALSE;
5341 if (file_name != NULL)
5342 break;
5343
5344 did_findfile_init = FALSE;
5345 }
5346 else
5347 {
5348 char_u *r_ptr;
5349
5350 if (dir == NULL || *dir == NUL)
5351 {
5352 /* We searched all paths of the option, now we can
5353 * free the search context. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005354 vim_findfile_cleanup(fdip_search_ctx);
5355 fdip_search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 break;
5357 }
5358
5359 if ((buf = alloc((int)(MAXPATHL))) == NULL)
5360 break;
5361
5362 /* copy next path */
5363 buf[0] = 0;
5364 copy_option_part(&dir, buf, MAXPATHL, " ,");
5365
5366#ifdef FEAT_PATH_EXTRA
5367 /* get the stopdir string */
5368 r_ptr = vim_findfile_stopdir(buf);
5369#else
5370 r_ptr = NULL;
5371#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005372 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
5373 r_ptr, 100, FALSE, TRUE,
5374 fdip_search_ctx, FALSE, rel_fname);
5375 if (fdip_search_ctx != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 did_findfile_init = TRUE;
5377 vim_free(buf);
5378 }
5379 }
5380 }
5381 if (file_name == NULL && (options & FNAME_MESS))
5382 {
5383 if (first == TRUE)
5384 {
5385 if (need_dir)
5386 EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005387 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
5389 EMSG2(_("E345: Can't find file \"%s\" in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005390 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
5392 else
5393 {
5394 if (need_dir)
5395 EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005396 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 else
5398 EMSG2(_("E347: No more file \"%s\" found in path"),
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005399 ff_file_to_find);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401 }
5402
5403theend:
5404#ifdef AMIGA
5405 proc->pr_WindowPtr = save_winptr;
5406#endif
5407 return file_name;
5408}
5409
5410#endif /* FEAT_SEARCHPATH */
5411
5412/*
5413 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
5414 * 'cdpath' for relative directory names, otherwise just mch_chdir().
5415 */
5416 int
5417vim_chdir(new_dir)
5418 char_u *new_dir;
5419{
5420#ifndef FEAT_SEARCHPATH
5421 return mch_chdir((char *)new_dir);
5422#else
5423 char_u *dir_name;
5424 int r;
5425
5426 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
5427 FNAME_MESS, curbuf->b_ffname);
5428 if (dir_name == NULL)
5429 return -1;
5430 r = mch_chdir((char *)dir_name);
5431 vim_free(dir_name);
5432 return r;
5433#endif
5434}
5435
5436/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005437 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005439 * Some systems are quite slow in obtaining the user name (Windows NT), thus
5440 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 * Returns OK or FAIL.
5442 */
5443 int
5444get_user_name(buf, len)
5445 char_u *buf;
5446 int len;
5447{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005448 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {
5450 if (mch_get_user_name(buf, len) == FAIL)
5451 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005452 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
5454 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00005455 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 return OK;
5457}
5458
5459#ifndef HAVE_QSORT
5460/*
5461 * Our own qsort(), for systems that don't have it.
5462 * It's simple and slow. From the K&R C book.
5463 */
5464 void
5465qsort(base, elm_count, elm_size, cmp)
5466 void *base;
5467 size_t elm_count;
5468 size_t elm_size;
5469 int (*cmp) __ARGS((const void *, const void *));
5470{
5471 char_u *buf;
5472 char_u *p1;
5473 char_u *p2;
5474 int i, j;
5475 int gap;
5476
5477 buf = alloc((unsigned)elm_size);
5478 if (buf == NULL)
5479 return;
5480
5481 for (gap = elm_count / 2; gap > 0; gap /= 2)
5482 for (i = gap; i < elm_count; ++i)
5483 for (j = i - gap; j >= 0; j -= gap)
5484 {
5485 /* Compare the elements. */
5486 p1 = (char_u *)base + j * elm_size;
5487 p2 = (char_u *)base + (j + gap) * elm_size;
5488 if ((*cmp)((void *)p1, (void *)p2) <= 0)
5489 break;
5490 /* Exchange the elemets. */
5491 mch_memmove(buf, p1, elm_size);
5492 mch_memmove(p1, p2, elm_size);
5493 mch_memmove(p2, buf, elm_size);
5494 }
5495
5496 vim_free(buf);
5497}
5498#endif
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500/*
5501 * Sort an array of strings.
5502 */
5503static int
5504#ifdef __BORLANDC__
5505_RTLENTRYF
5506#endif
5507sort_compare __ARGS((const void *s1, const void *s2));
5508
5509 static int
5510#ifdef __BORLANDC__
5511_RTLENTRYF
5512#endif
5513sort_compare(s1, s2)
5514 const void *s1;
5515 const void *s2;
5516{
5517 return STRCMP(*(char **)s1, *(char **)s2);
5518}
5519
5520 void
5521sort_strings(files, count)
5522 char_u **files;
5523 int count;
5524{
5525 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
5526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528#if !defined(NO_EXPANDPATH) || defined(PROTO)
5529/*
5530 * Compare path "p[]" to "q[]".
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005531 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 * Return value like strcmp(p, q), but consider path separators.
5533 */
5534 int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005535pathcmp(p, q, maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 const char *p, *q;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005537 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 int i;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005540 const char *s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005542 for (i = 0; maxlen < 0 || i < maxlen; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
5544 /* End of "p": check if "q" also ends or just has a slash. */
5545 if (p[i] == NUL)
5546 {
5547 if (q[i] == NUL) /* full match */
5548 return 0;
5549 s = q;
5550 break;
5551 }
5552
5553 /* End of "q": check if "p" just has a slash. */
5554 if (q[i] == NUL)
5555 {
5556 s = p;
5557 break;
5558 }
5559
5560 if (
5561#ifdef CASE_INSENSITIVE_FILENAME
5562 TOUPPER_LOC(p[i]) != TOUPPER_LOC(q[i])
5563#else
5564 p[i] != q[i]
5565#endif
5566#ifdef BACKSLASH_IN_FILENAME
5567 /* consider '/' and '\\' to be equal */
5568 && !((p[i] == '/' && q[i] == '\\')
5569 || (p[i] == '\\' && q[i] == '/'))
5570#endif
5571 )
5572 {
5573 if (vim_ispathsep(p[i]))
5574 return -1;
5575 if (vim_ispathsep(q[i]))
5576 return 1;
5577 return ((char_u *)p)[i] - ((char_u *)q)[i]; /* no match */
5578 }
5579 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005580 if (s == NULL) /* "i" ran into "maxlen" */
5581 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583 /* ignore a trailing slash, but not "//" or ":/" */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005584 if (s[i + 1] == NUL
5585 && i > 0
5586 && !after_pathsep((char_u *)s, (char_u *)s + i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar86b68352004-12-27 21:59:20 +00005588 && (s[i] == '/' || s[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589#else
Bram Moolenaar86b68352004-12-27 21:59:20 +00005590 && s[i] == '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00005592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 return 0; /* match with trailing slash */
5594 if (s == q)
5595 return -1; /* no match */
5596 return 1;
5597}
5598#endif
5599
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600/*
5601 * The putenv() implementation below comes from the "screen" program.
5602 * Included with permission from Juergen Weigert.
5603 * See pty.c for the copyright notice.
5604 */
5605
5606/*
5607 * putenv -- put value into environment
5608 *
5609 * Usage: i = putenv (string)
5610 * int i;
5611 * char *string;
5612 *
5613 * where string is of the form <name>=<value>.
5614 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
5615 *
5616 * Putenv may need to add a new name into the environment, or to
5617 * associate a value longer than the current value with a particular
5618 * name. So, to make life simpler, putenv() copies your entire
5619 * environment into the heap (i.e. malloc()) from the stack
5620 * (i.e. where it resides when your process is initiated) the first
5621 * time you call it.
5622 *
5623 * (history removed, not very interesting. See the "screen" sources.)
5624 */
5625
5626#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
5627
5628#define EXTRASIZE 5 /* increment to add to env. size */
5629
5630static int envsize = -1; /* current size of environment */
5631#ifndef MACOS_CLASSIC
5632extern
5633#endif
5634 char **environ; /* the global which is your env. */
5635
5636static int findenv __ARGS((char *name)); /* look for a name in the env. */
5637static int newenv __ARGS((void)); /* copy env. from stack to heap */
5638static int moreenv __ARGS((void)); /* incr. size of env. */
5639
5640 int
5641putenv(string)
5642 const char *string;
5643{
5644 int i;
5645 char *p;
5646
5647 if (envsize < 0)
5648 { /* first time putenv called */
5649 if (newenv() < 0) /* copy env. to heap */
5650 return -1;
5651 }
5652
5653 i = findenv((char *)string); /* look for name in environment */
5654
5655 if (i < 0)
5656 { /* name must be added */
5657 for (i = 0; environ[i]; i++);
5658 if (i >= (envsize - 1))
5659 { /* need new slot */
5660 if (moreenv() < 0)
5661 return -1;
5662 }
5663 p = (char *)alloc((unsigned)(strlen(string) + 1));
5664 if (p == NULL) /* not enough core */
5665 return -1;
5666 environ[i + 1] = 0; /* new end of env. */
5667 }
5668 else
5669 { /* name already in env. */
5670 p = vim_realloc(environ[i], strlen(string) + 1);
5671 if (p == NULL)
5672 return -1;
5673 }
5674 sprintf(p, "%s", string); /* copy into env. */
5675 environ[i] = p;
5676
5677 return 0;
5678}
5679
5680 static int
5681findenv(name)
5682 char *name;
5683{
5684 char *namechar, *envchar;
5685 int i, found;
5686
5687 found = 0;
5688 for (i = 0; environ[i] && !found; i++)
5689 {
5690 envchar = environ[i];
5691 namechar = name;
5692 while (*namechar && *namechar != '=' && (*namechar == *envchar))
5693 {
5694 namechar++;
5695 envchar++;
5696 }
5697 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
5698 }
5699 return found ? i - 1 : -1;
5700}
5701
5702 static int
5703newenv()
5704{
5705 char **env, *elem;
5706 int i, esize;
5707
5708#ifdef MACOS
5709 /* for Mac a new, empty environment is created */
5710 i = 0;
5711#else
5712 for (i = 0; environ[i]; i++)
5713 ;
5714#endif
5715 esize = i + EXTRASIZE + 1;
5716 env = (char **)alloc((unsigned)(esize * sizeof (elem)));
5717 if (env == NULL)
5718 return -1;
5719
5720#ifndef MACOS
5721 for (i = 0; environ[i]; i++)
5722 {
5723 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
5724 if (elem == NULL)
5725 return -1;
5726 env[i] = elem;
5727 strcpy(elem, environ[i]);
5728 }
5729#endif
5730
5731 env[i] = 0;
5732 environ = env;
5733 envsize = esize;
5734 return 0;
5735}
5736
5737 static int
5738moreenv()
5739{
5740 int esize;
5741 char **env;
5742
5743 esize = envsize + EXTRASIZE;
5744 env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
5745 if (env == 0)
5746 return -1;
5747 environ = env;
5748 envsize = esize;
5749 return 0;
5750}
5751
5752# ifdef USE_VIMPTY_GETENV
5753 char_u *
5754vimpty_getenv(string)
5755 const char_u *string;
5756{
5757 int i;
5758 char_u *p;
5759
5760 if (envsize < 0)
5761 return NULL;
5762
5763 i = findenv((char *)string);
5764
5765 if (i < 0)
5766 return NULL;
5767
5768 p = vim_strchr((char_u *)environ[i], '=');
5769 return (p + 1);
5770}
5771# endif
5772
5773#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00005774
Bram Moolenaarc4956c82006-03-12 21:58:43 +00005775#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00005776/*
5777 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
5778 * rights to write into.
5779 */
5780 int
5781filewritable(fname)
5782 char_u *fname;
5783{
5784 int retval = 0;
5785#if defined(UNIX) || defined(VMS)
5786 int perm = 0;
5787#endif
5788
5789#if defined(UNIX) || defined(VMS)
5790 perm = mch_getperm(fname);
5791#endif
5792#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
5793 if (
5794# ifdef WIN3264
5795 mch_writable(fname) &&
5796# else
5797# if defined(UNIX) || defined(VMS)
5798 (perm & 0222) &&
5799# endif
5800# endif
5801 mch_access((char *)fname, W_OK) == 0
5802 )
5803#endif
5804 {
5805 ++retval;
5806 if (mch_isdir(fname))
5807 ++retval;
5808 }
5809 return retval;
5810}
5811#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005812
5813/*
5814 * Print an error message with one or two "%s" and one or two string arguments.
5815 * This is not in message.c to avoid a warning for prototypes.
5816 */
5817 int
5818emsg3(s, a1, a2)
5819 char_u *s, *a1, *a2;
5820{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005821 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005822 return TRUE; /* no error messages at the moment */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005823 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (long_u)a1, (long_u)a2);
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005824 return emsg(IObuff);
5825}
5826
5827/*
5828 * Print an error message with one "%ld" and one long int argument.
5829 * This is not in message.c to avoid a warning for prototypes.
5830 */
5831 int
5832emsgn(s, n)
5833 char_u *s;
5834 long n;
5835{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005836 if (emsg_not_now())
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00005837 return TRUE; /* no error messages at the moment */
5838 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
5839 return emsg(IObuff);
5840}