#include "vim.h"
static char_u *username = NULL;
static char_u *ff_expand_buffer = NULL;
#if defined(FEAT_VIRTUALEDIT) || defined(PROTO)
static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
int
virtual_active(void)
{
if (virtual_op != MAYBE)
return virtual_op;
return (ve_flags == VE_ALL
|| ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
|| ((ve_flags & VE_INSERT) && (State & INSERT)));
}
int
getviscol(void)
{
colnr_T x;
getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
return (int)x;
}
int
getviscol2(colnr_T col, colnr_T coladd)
{
colnr_T x;
pos_T pos;
pos.lnum = curwin->w_cursor.lnum;
pos.col = col;
pos.coladd = coladd;
getvvcol(curwin, &pos, &x, NULL, NULL);
return (int)x;
}
int
coladvance_force(colnr_T wcol)
{
int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
if (wcol == MAXCOL)
curwin->w_valid &= ~VALID_VIRTCOL;
else
{
curwin->w_valid |= VALID_VIRTCOL;
curwin->w_virtcol = wcol;
}
return rc;
}
#endif
int
coladvance(colnr_T wcol)
{
int rc = getvpos(&curwin->w_cursor, wcol);
if (wcol == MAXCOL || rc == FAIL)
curwin->w_valid &= ~VALID_VIRTCOL;
else if (*ml_get_cursor() != TAB)
{
curwin->w_valid |= VALID_VIRTCOL;
curwin->w_virtcol = wcol;
}
return rc;
}
int
getvpos(pos_T *pos, colnr_T wcol)
{
#ifdef FEAT_VIRTUALEDIT
return coladvance2(pos, FALSE, virtual_active(), wcol);
}
static int
coladvance2(
pos_T *pos,
int addspaces,
int finetune,
colnr_T wcol)
{
#endif
int idx;
char_u *ptr;
char_u *line;
colnr_T col = 0;
int csize = 0;
int one_more;
#ifdef FEAT_LINEBREAK
int head = 0;
#endif
one_more = (State & INSERT)
|| restart_edit != NUL
|| (VIsual_active && *p_sel != 'o')
#ifdef FEAT_VIRTUALEDIT
|| ((ve_flags & VE_ONEMORE) && wcol < MAXCOL)
#endif
;
line = ml_get_buf(curbuf, pos->lnum, FALSE);
if (wcol >= MAXCOL)
{
idx = (int)STRLEN(line) - 1 + one_more;
col = wcol;
#ifdef FEAT_VIRTUALEDIT
if ((addspaces || finetune) && !VIsual_active)
{
curwin->w_curswant = linetabsize(line) + one_more;
if (curwin->w_curswant > 0)
--curwin->w_curswant;
}
#endif
}
else
{
#ifdef FEAT_VIRTUALEDIT
int width = curwin->w_width - win_col_off(curwin);
if (finetune
&& curwin->w_p_wrap
&& curwin->w_width != 0
&& wcol >= (colnr_T)width)
{
csize = linetabsize(line);
if (csize > 0)
csize--;
if (wcol / width > (colnr_T)csize / width
&& ((State & INSERT) == 0 || (int)wcol > csize + 1))
{
wcol = (csize / width + 1) * width - 1;
}
}
#endif
ptr = line;
while (col <= wcol && *ptr != NUL)
{
#ifdef FEAT_LINEBREAK
csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
MB_PTR_ADV(ptr);
#else
csize = lbr_chartabsize_adv(line, &ptr, col);
#endif
col += csize;
}
idx = (int)(ptr - line);
if (col > wcol || (!virtual_active() && one_more == 0))
{
idx -= 1;
# ifdef FEAT_LINEBREAK
csize -= head;
# endif
col -= csize;
}
#ifdef FEAT_VIRTUALEDIT
if (virtual_active()
&& addspaces
&& ((col != wcol && col != wcol + 1) || csize > 1))
{
if (line[idx] == NUL)
{
int correct = wcol - col;
char_u *newline = alloc(idx + correct + 1);
int t;
if (newline == NULL)
return FAIL;
for (t = 0; t < idx; ++t)
newline[t] = line[t];
for (t = 0; t < correct; ++t)
newline[t + idx] = ' ';
newline[idx + correct] = NUL;
ml_replace(pos->lnum, newline, FALSE);
changed_bytes(pos->lnum, (colnr_T)idx);
idx += correct;
col = wcol;
}
else
{
int linelen = (int)STRLEN(line);
int correct = wcol - col - csize + 1;
char_u *newline;
int t, s = 0;
int v;
if (-correct > csize)
return FAIL;
newline = alloc(linelen + csize);
if (newline == NULL)
return FAIL;
for (t = 0; t < linelen; t++)
{
if (t != idx)
newline[s++] = line[t];
else
for (v = 0; v < csize; v++)
newline[s++] = ' ';
}
newline[linelen + csize - 1] = NUL;
ml_replace(pos->lnum, newline, FALSE);
changed_bytes(pos->lnum, idx);
idx += (csize - 1 + correct);
col += correct;
}
}
#endif
}
if (idx < 0)
pos->col = 0;
else
pos->col = idx;
#ifdef FEAT_VIRTUALEDIT
pos->coladd = 0;
if (finetune)
{
if (wcol == MAXCOL)
{
if (!one_more)
{
colnr_T scol, ecol;
getvcol(curwin, pos, &scol, NULL, &ecol);
pos->coladd = ecol - scol;
}
}
else
{
int b = (int)wcol - (int)col;
if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
pos->coladd = b;
col += b;
}
}
#endif
#ifdef FEAT_MBYTE
if (has_mbyte)
mb_adjustpos(curbuf, pos);
#endif
if (col < wcol)
return FAIL;
return OK;
}
int
inc_cursor(void)
{
return inc(&curwin->w_cursor);
}
int
inc(pos_T *lp)
{
char_u *p = ml_get_pos(lp);
if (*p != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int l = (*mb_ptr2len)(p);
lp->col += l;
return ((p[l] != NUL) ? 0 : 2);
}
#endif
lp->col++;
#ifdef FEAT_VIRTUALEDIT
lp->coladd = 0;
#endif
return ((p[1] != NUL) ? 0 : 2);
}
if (lp->lnum != curbuf->b_ml.ml_line_count)
{
lp->col = 0;
lp->lnum++;
#ifdef FEAT_VIRTUALEDIT
lp->coladd = 0;
#endif
return 1;
}
return -1;
}
int
incl(pos_T *lp)
{
int r;
if ((r = inc(lp)) >= 1 && lp->col)
r = inc(lp);
return r;
}
int
dec_cursor(void)
{
return dec(&curwin->w_cursor);
}
int
dec(pos_T *lp)
{
char_u *p;
#ifdef FEAT_VIRTUALEDIT
lp->coladd = 0;
#endif
if (lp->col > 0)
{
lp->col--;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
p = ml_get(lp->lnum);
lp->col -= (*mb_head_off)(p, p + lp->col);
}
#endif
return 0;
}
if (lp->lnum > 1)
{
lp->lnum--;
p = ml_get(lp->lnum);
lp->col = (colnr_T)STRLEN(p);
#ifdef FEAT_MBYTE
if (has_mbyte)
lp->col -= (*mb_head_off)(p, p + lp->col);
#endif
return 1;
}
return -1;
}
int
decl(pos_T *lp)
{
int r;
if ((r = dec(lp)) == 1 && lp->col)
r = dec(lp);
return r;
}
linenr_T
get_cursor_rel_lnum(
win_T *wp,
linenr_T lnum)
{
linenr_T cursor = wp->w_cursor.lnum;
linenr_T retval = 0;
#ifdef FEAT_FOLDING
if (hasAnyFolding(wp))
{
if (lnum > cursor)
{
while (lnum > cursor)
{
(void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
if (lnum > cursor)
retval++;
lnum--;
}
}
else if (lnum < cursor)
{
while (lnum < cursor)
{
(void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
if (lnum < cursor)
retval--;
lnum++;
}
}
}
else
#endif
retval = lnum - cursor;
return retval;
}
void
check_pos(buf_T *buf, pos_T *pos)
{
char_u *line;
colnr_T len;
if (pos->lnum > buf->b_ml.ml_line_count)
pos->lnum = buf->b_ml.ml_line_count;
if (pos->col > 0)
{
line = ml_get_buf(buf, pos->lnum, FALSE);
len = (colnr_T)STRLEN(line);
if (pos->col > len)
pos->col = len;
}
}
void
check_cursor_lnum(void)
{
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
{
#ifdef FEAT_FOLDING
if (!hasFolding(curbuf->b_ml.ml_line_count,
&curwin->w_cursor.lnum, NULL))
#endif
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
}
if (curwin->w_cursor.lnum <= 0)
curwin->w_cursor.lnum = 1;
}
void
check_cursor_col(void)
{
check_cursor_col_win(curwin);
}
void
check_cursor_col_win(win_T *win)
{
colnr_T len;
#ifdef FEAT_VIRTUALEDIT
colnr_T oldcol = win->w_cursor.col;
colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
#endif
len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
if (len == 0)
win->w_cursor.col = 0;
else if (win->w_cursor.col >= len)
{
if ((State & INSERT) || restart_edit
|| (VIsual_active && *p_sel != 'o')
#ifdef FEAT_VIRTUALEDIT
|| (ve_flags & VE_ONEMORE)
#endif
|| virtual_active())
win->w_cursor.col = len;
else
{
win->w_cursor.col = len - 1;
#ifdef FEAT_MBYTE
if (has_mbyte)
mb_adjustpos(win->w_buffer, &win->w_cursor);
#endif
}
}
else if (win->w_cursor.col < 0)
win->w_cursor.col = 0;
#ifdef FEAT_VIRTUALEDIT
if (oldcol == MAXCOL)
win->w_cursor.coladd = 0;
else if (ve_flags == VE_ALL)
{
if (oldcoladd > win->w_cursor.col)
{
win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
{
int cs, ce;
getvcol(win, &win->w_cursor, &cs, NULL, &ce);
if (win->w_cursor.coladd > ce - cs)
win->w_cursor.coladd = ce - cs;
}
}
else
win->w_cursor.coladd = 0;
}
#endif
}
void
check_cursor(void)
{
check_cursor_lnum();
check_cursor_col();
}
#if defined(FEAT_TEXTOBJ) || defined(PROTO)
void
adjust_cursor_col(void)
{
if (curwin->w_cursor.col > 0
&& (!VIsual_active || *p_sel == 'o')
&& gchar_cursor() == NUL)
--curwin->w_cursor.col;
}
#endif
int
leftcol_changed(void)
{
long lastcol;
colnr_T s, e;
int retval = FALSE;
changed_cline_bef_curs();
lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
validate_virtcol();
if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
{
retval = TRUE;
coladvance((colnr_T)(lastcol - p_siso));
}
else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
{
retval = TRUE;
(void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
}
getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
if (e > (colnr_T)lastcol)
{
retval = TRUE;
coladvance(s - 1);
}
else if (s < curwin->w_leftcol)
{
retval = TRUE;
if (coladvance(e + 1) == FAIL)
{
curwin->w_leftcol = s;
changed_cline_bef_curs();
}
}
if (retval)
curwin->w_set_curswant = TRUE;
redraw_later(NOT_VALID);
return retval;
}
#if defined(MEM_PROFILE) || defined(PROTO)
# define MEM_SIZES 8200
static long_u mem_allocs[MEM_SIZES];
static long_u mem_frees[MEM_SIZES];
static long_u mem_allocated;
static long_u mem_freed;
static long_u mem_peak;
static long_u num_alloc;
static long_u num_freed;
static void mem_pre_alloc_s(size_t *sizep);
static void mem_pre_alloc_l(long_u *sizep);
static void mem_post_alloc(void **pp, size_t size);
static void mem_pre_free(void **pp);
static void
mem_pre_alloc_s(size_t *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_pre_alloc_l(long_u *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_post_alloc(
void **pp,
size_t size)
{
if (*pp == NULL)
return;
size -= sizeof(size_t);
*(long_u *)*pp = size;
if (size <= MEM_SIZES-1)
mem_allocs[size-1]++;
else
mem_allocs[MEM_SIZES-1]++;
mem_allocated += size;
if (mem_allocated - mem_freed > mem_peak)
mem_peak = mem_allocated - mem_freed;
num_alloc++;
*pp = (void *)((char *)*pp + sizeof(size_t));
}
static void
mem_pre_free(void **pp)
{
long_u size;
*pp = (void *)((char *)*pp - sizeof(size_t));
size = *(size_t *)*pp;
if (size <= MEM_SIZES-1)
mem_frees[size-1]++;
else
mem_frees[MEM_SIZES-1]++;
mem_freed += size;
num_freed++;
}
void
vim_mem_profile_dump(void)
{
int i, j;
printf("\r\n");
j = 0;
for (i = 0; i < MEM_SIZES - 1; i++)
{
if (mem_allocs[i] || mem_frees[i])
{
if (mem_frees[i] > mem_allocs[i])
printf("\r\n%s", _("ERROR: "));
printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
j++;
if (j > 3)
{
j = 0;
printf("\r\n");
}
}
}
i = MEM_SIZES - 1;
if (mem_allocs[i])
{
printf("\r\n");
if (mem_frees[i] > mem_allocs[i])
puts(_("ERROR: "));
printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
}
printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
num_alloc, num_freed);
}
#endif
#ifdef FEAT_EVAL
static int alloc_does_fail(long_u size);
static int
alloc_does_fail(long_u size)
{
if (alloc_fail_countdown == 0)
{
if (--alloc_fail_repeat <= 0)
alloc_fail_id = 0;
do_outofmem_msg(size);
return TRUE;
}
--alloc_fail_countdown;
return FALSE;
}
#endif
#define KEEP_ROOM (2 * 8192L)
#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
char_u *
alloc(unsigned size)
{
return (lalloc((long_u)size, TRUE));
}
char_u *
alloc_id(unsigned size, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail((long_u)size))
return NULL;
#endif
return (lalloc((long_u)size, TRUE));
}
char_u *
alloc_clear(unsigned size)
{
char_u *p;
p = lalloc((long_u)size, TRUE);
if (p != NULL)
(void)vim_memset(p, 0, (size_t)size);
return p;
}
char_u *
alloc_check(unsigned size)
{
#if !defined(UNIX)
if (sizeof(int) == 2 && size > 0x7fff)
{
emsg_silent = 0;
EMSG(_("E340: Line is becoming too long"));
return NULL;
}
#endif
return (lalloc((long_u)size, TRUE));
}
char_u *
lalloc_clear(long_u size, int message)
{
char_u *p;
p = (lalloc(size, message));
if (p != NULL)
(void)vim_memset(p, 0, (size_t)size);
return p;
}
char_u *
lalloc(long_u size, int message)
{
char_u *p;
static int releasing = FALSE;
int try_again;
#if defined(HAVE_AVAIL_MEM)
static long_u allocated = 0;
#endif
if (size == 0)
{
emsg_silent = 0;
IEMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
return NULL;
}
#ifdef MEM_PROFILE
mem_pre_alloc_l(&size);
#endif
for (;;)
{
if ((p = (char_u *)malloc((size_t)size)) != NULL)
{
#ifndef HAVE_AVAIL_MEM
goto theend;
#else
allocated += size;
if (allocated < KEEP_ROOM / 2)
goto theend;
allocated = 0;
if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
{
free((char *)p);
p = NULL;
}
else
goto theend;
#endif
}
if (releasing)
break;
releasing = TRUE;
clear_sb_text(TRUE);
try_again = mf_release_all();
releasing = FALSE;
if (!try_again)
break;
}
if (message && p == NULL)
do_outofmem_msg(size);
theend:
#ifdef MEM_PROFILE
mem_post_alloc((void **)&p, (size_t)size);
#endif
return p;
}
char_u *
lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail(size))
return NULL;
#endif
return (lalloc((long_u)size, message));
}
#if defined(MEM_PROFILE) || defined(PROTO)
void *
mem_realloc(void *ptr, size_t size)
{
void *p;
mem_pre_free(&ptr);
mem_pre_alloc_s(&size);
p = realloc(ptr, size);
mem_post_alloc(&p, size);
return p;
}
#endif
void
do_outofmem_msg(long_u size)
{
if (!did_outofmem_msg)
{
emsg_silent = 0;
did_outofmem_msg = TRUE;
EMSGN(_("E342: Out of memory! (allocating %lu bytes)"), size);
}
}
#if defined(EXITFREE) || defined(PROTO)
# if defined(FEAT_SEARCHPATH)
static void free_findfile(void);
# endif
void
free_all_mem(void)
{
buf_T *buf, *nextbuf;
if (entered_free_all_mem)
return;
entered_free_all_mem = TRUE;
# ifdef FEAT_AUTOCMD
block_autocmds();
# endif
p_ea = FALSE;
if (first_tabpage->tp_next != NULL)
do_cmdline_cmd((char_u *)"tabonly!");
if (!ONE_WINDOW)
do_cmdline_cmd((char_u *)"only!");
# if defined(FEAT_SPELL)
spell_free_all();
# endif
# if defined(FEAT_USR_CMDS)
ex_comclear(NULL);
# endif
# ifdef FEAT_MENU
do_cmdline_cmd((char_u *)"aunmenu *");
# ifdef FEAT_MULTI_LANG
do_cmdline_cmd((char_u *)"menutranslate clear");
# endif
# endif
do_cmdline_cmd((char_u *)"lmapclear");
do_cmdline_cmd((char_u *)"xmapclear");
do_cmdline_cmd((char_u *)"mapclear");
do_cmdline_cmd((char_u *)"mapclear!");
do_cmdline_cmd((char_u *)"abclear");
# if defined(FEAT_EVAL)
do_cmdline_cmd((char_u *)"breakdel *");
# endif
# if defined(FEAT_PROFILE)
do_cmdline_cmd((char_u *)"profdel *");
# endif
# if defined(FEAT_KEYMAP)
do_cmdline_cmd((char_u *)"set keymap=");
#endif
# ifdef FEAT_TITLE
free_titles();
# endif
# if defined(FEAT_SEARCHPATH)
free_findfile();
# endif
# if defined(FEAT_AUTOCMD)
free_all_autocmds();
# endif
clear_termcodes();
free_all_marks();
alist_clear(&global_alist);
free_homedir();
# if defined(FEAT_CMDL_COMPL)
free_users();
# endif
free_search_patterns();
free_old_sub();
free_last_insert();
free_prev_shellcmd();
free_regexp_stuff();
free_tag_stuff();
free_cd_dir();
# ifdef FEAT_SIGNS
free_signs();
# endif
# ifdef FEAT_EVAL
set_expr_line(NULL);
# endif
# ifdef FEAT_DIFF
diff_clear(curtab);
# endif
clear_sb_text(TRUE);
vim_free(username);
# ifdef FEAT_CLIPBOARD
vim_regfree(clip_exclude_prog);
# endif
vim_free(last_cmdline);
# ifdef FEAT_CMDHIST
vim_free(new_last_cmdline);
# endif
set_keep_msg(NULL, 0);
vim_free(ff_expand_buffer);
p_hi = 0;
# ifdef FEAT_CMDHIST
init_history();
# endif
#ifdef FEAT_QUICKFIX
{
win_T *win;
tabpage_T *tab;
qf_free_all(NULL);
FOR_ALL_TAB_WINDOWS(tab, win)
qf_free_all(win);
}
#endif
close_all_scripts();
win_free_all();
free_all_options();
#ifdef FEAT_AUTOCHDIR
p_acd = FALSE;
#endif
for (buf = firstbuf; buf != NULL; )
{
bufref_T bufref;
set_bufref(&bufref, buf);
nextbuf = buf->b_next;
close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
if (bufref_valid(&bufref))
buf = nextbuf;
else
buf = firstbuf;
}
#ifdef FEAT_ARABIC
free_cmdline_buf();
#endif
clear_registers();
ResetRedobuff();
ResetRedobuff();
#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
vim_free(serverDelayedStartName);
#endif
free_highlight();
reset_last_sourcing();
free_tabpage(first_tabpage);
first_tabpage = NULL;
# ifdef UNIX
mch_free_mem();
# endif
for (;;)
if (delete_first_msg() == FAIL)
break;
# ifdef FEAT_JOB_CHANNEL
channel_free_all();
# endif
#ifdef FEAT_TIMERS
timer_free_all();
#endif
# ifdef FEAT_EVAL
eval_clear();
# endif
# ifdef FEAT_JOB_CHANNEL
job_free_all();
# endif
free_termoptions();
free_screenlines();
#if defined(USE_XSMP)
xsmp_close();
#endif
#ifdef FEAT_GUI_GTK
gui_mch_free_all();
#endif
clear_hl_tables();
vim_free(IObuff);
vim_free(NameBuff);
}
#endif
char_u *
vim_strsave(char_u *string)
{
char_u *p;
unsigned len;
len = (unsigned)STRLEN(string) + 1;
p = alloc(len);
if (p != NULL)
mch_memmove(p, string, (size_t)len);
return p;
}
char_u *
vim_strnsave(char_u *string, int len)
{
char_u *p;
p = alloc((unsigned)(len + 1));
if (p != NULL)
{
STRNCPY(p, string, len);
p[len] = NUL;
}
return p;
}
char_u *
vim_strsave_escaped(char_u *string, char_u *esc_chars)
{
return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
}
char_u *
vim_strsave_escaped_ext(
char_u *string,
char_u *esc_chars,
int cc,
int bsl)
{
char_u *p;
char_u *p2;
char_u *escaped_string;
unsigned length;
#ifdef FEAT_MBYTE
int l;
#endif
length = 1;
for (p = string; *p; p++)
{
#ifdef FEAT_MBYTE
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
{
length += l;
p += l - 1;
continue;
}
#endif
if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
++length;
++length;
}
escaped_string = alloc(length);
if (escaped_string != NULL)
{
p2 = escaped_string;
for (p = string; *p; p++)
{
#ifdef FEAT_MBYTE
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
{
mch_memmove(p2, p, (size_t)l);
p2 += l;
p += l - 1;
continue;
}
#endif
if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p)))
*p2++ = cc;
*p2++ = *p;
}
*p2 = NUL;
}
return escaped_string;
}
int
csh_like_shell(void)
{
return (strstr((char *)gettail(p_sh), "csh") != NULL);
}
char_u *
vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
{
unsigned length;
char_u *p;
char_u *d;
char_u *escaped_string;
int l;
int csh_like;
csh_like = csh_like_shell();
length = (unsigned)STRLEN(string) + 3;
for (p = string; *p != NUL; MB_PTR_ADV(p))
{
# ifdef WIN32
if (!p_ssl)
{
if (*p == '"')
++length;
}
else
# endif
if (*p == '\'')
length += 3;
if ((*p == '\n' && (csh_like || do_newline))
|| (*p == '!' && (csh_like || do_special)))
{
++length;
if (csh_like && do_special)
++length;
}
if (do_special && find_cmdline_var(p, &l) >= 0)
{
++length;
p += l - 1;
}
}
escaped_string = alloc(length);
if (escaped_string != NULL)
{
d = escaped_string;
# ifdef WIN32
if (!p_ssl)
*d++ = '"';
else
# endif
*d++ = '\'';
for (p = string; *p != NUL; )
{
# ifdef WIN32
if (!p_ssl)
{
if (*p == '"')
{
*d++ = '"';
*d++ = '"';
++p;
continue;
}
}
else
# endif
if (*p == '\'')
{
*d++ = '\'';
*d++ = '\\';
*d++ = '\'';
*d++ = '\'';
++p;
continue;
}
if ((*p == '\n' && (csh_like || do_newline))
|| (*p == '!' && (csh_like || do_special)))
{
*d++ = '\\';
if (csh_like && do_special)
*d++ = '\\';
*d++ = *p++;
continue;
}
if (do_special && find_cmdline_var(p, &l) >= 0)
{
*d++ = '\\';
while (--l >= 0)
*d++ = *p++;
continue;
}
MB_COPY_CHAR(p, d);
}
# ifdef WIN32
if (!p_ssl)
*d++ = '"';
else
# endif
*d++ = '\'';
*d = NUL;
}
return escaped_string;
}
char_u *
vim_strsave_up(char_u *string)
{
char_u *p1;
p1 = vim_strsave(string);
vim_strup(p1);
return p1;
}
char_u *
vim_strnsave_up(char_u *string, int len)
{
char_u *p1;
p1 = vim_strnsave(string, len);
vim_strup(p1);
return p1;
}
void
vim_strup(
char_u *p)
{
char_u *p2;
int c;
if (p != NULL)
{
p2 = p;
while ((c = *p2) != NUL)
#ifdef EBCDIC
*p2++ = isalpha(c) ? toupper(c) : c;
#else
*p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20);
#endif
}
}
#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
char_u *
strup_save(char_u *orig)
{
char_u *p;
char_u *res;
res = p = vim_strsave(orig);
if (res != NULL)
while (*p != NUL)
{
# ifdef FEAT_MBYTE
int l;
if (enc_utf8)
{
int c, uc;
int newl;
char_u *s;
c = utf_ptr2char(p);
uc = utf_toupper(c);
l = utf_ptr2len(p);
newl = utf_char2len(uc);
if (newl != l)
{
s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
if (s == NULL)
{
vim_free(res);
return NULL;
}
mch_memmove(s, res, p - res);
STRCPY(s + (p - res) + newl, p + l);
p = s + (p - res);
vim_free(res);
res = s;
}
utf_char2bytes(uc, p);
p += newl;
}
else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
p += l;
else
# endif
{
*p = TOUPPER_LOC(*p);
p++;
}
}
return res;
}
char_u *
strlow_save(char_u *orig)
{
char_u *p;
char_u *res;
res = p = vim_strsave(orig);
if (res != NULL)
while (*p != NUL)
{
# ifdef FEAT_MBYTE
int l;
if (enc_utf8)
{
int c, lc;
int newl;
char_u *s;
c = utf_ptr2char(p);
lc = utf_tolower(c);
l = utf_ptr2len(p);
newl = utf_char2len(lc);
if (newl != l)
{
s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
if (s == NULL)
{
vim_free(res);
return NULL;
}
mch_memmove(s, res, p - res);
STRCPY(s + (p - res) + newl, p + l);
p = s + (p - res);
vim_free(res);
res = s;
}
utf_char2bytes(lc, p);
p += newl;
}
else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
p += l;
else
# endif
{
*p = TOLOWER_LOC(*p);
p++;
}
}
return res;
}
#endif
void
del_trailing_spaces(char_u *ptr)
{
char_u *q;
q = ptr + STRLEN(ptr);
while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V)
*q = NUL;
}
void
vim_strncpy(char_u *to, char_u *from, size_t len)
{
STRNCPY(to, from, len);
to[len] = NUL;
}
void
vim_strcat(char_u *to, char_u *from, size_t tosize)
{
size_t tolen = STRLEN(to);
size_t fromlen = STRLEN(from);
if (tolen + fromlen + 1 > tosize)
{
mch_memmove(to + tolen, from, tosize - tolen - 1);
to[tosize - 1] = NUL;
}
else
mch_memmove(to + tolen, from, fromlen + 1);
}
int
copy_option_part(
char_u **option,
char_u *buf,
int maxlen,
char *sep_chars)
{
int len = 0;
char_u *p = *option;
if (*p == '.')
buf[len++] = *p++;
while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
{
if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
++p;
if (len < maxlen - 1)
buf[len++] = *p;
++p;
}
buf[len] = NUL;
if (*p != NUL && *p != ',')
++p;
p = skip_to_option_part(p);
*option = p;
return len;
}
void
vim_free(void *x)
{
if (x != NULL && !really_exiting)
{
#ifdef MEM_PROFILE
mem_pre_free(&x);
#endif
free(x);
}
}
#ifndef HAVE_MEMSET
void *
vim_memset(void *ptr, int c, size_t size)
{
char *p = ptr;
while (size-- > 0)
*p++ = c;
return ptr;
}
#endif
#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
int
vim_stricmp(char *s1, char *s2)
{
int i;
for (;;)
{
i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
if (i != 0)
return i;
if (*s1 == NUL)
break;
++s1;
++s2;
}
return 0;
}
#endif
#if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO)
int
vim_strnicmp(char *s1, char *s2, size_t len)
{
int i;
while (len > 0)
{
i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2);
if (i != 0)
return i;
if (*s1 == NUL)
break;
++s1;
++s2;
--len;
}
return 0;
}
#endif
char_u *
vim_strchr(char_u *string, int c)
{
char_u *p;
int b;
p = string;
#ifdef FEAT_MBYTE
if (enc_utf8 && c >= 0x80)
{
while (*p != NUL)
{
int l = utfc_ptr2len(p);
if (utf_ptr2char(p) == c && l > 1)
return p;
p += l;
}
return NULL;
}
if (enc_dbcs != 0 && c > 255)
{
int n2 = c & 0xff;
c = ((unsigned)c >> 8) & 0xff;
while ((b = *p) != NUL)
{
if (b == c && p[1] == n2)
return p;
p += (*mb_ptr2len)(p);
}
return NULL;
}
if (has_mbyte)
{
while ((b = *p) != NUL)
{
if (b == c)
return p;
p += (*mb_ptr2len)(p);
}
return NULL;
}
#endif
while ((b = *p) != NUL)
{
if (b == c)
return p;
++p;
}
return NULL;
}
char_u *
vim_strbyte(char_u *string, int c)
{
char_u *p = string;
while (*p != NUL)
{
if (*p == c)
return p;
++p;
}
return NULL;
}
char_u *
vim_strrchr(char_u *string, int c)
{
char_u *retval = NULL;
char_u *p = string;
while (*p)
{
if (*p == c)
retval = p;
MB_PTR_ADV(p);
}
return retval;
}
#ifndef PROTO
# ifndef HAVE_STRPBRK
# ifdef vim_strpbrk
# undef vim_strpbrk
# endif
char_u *
vim_strpbrk(char_u *s, char_u *charset)
{
while (*s)
{
if (vim_strchr(charset, *s) != NULL)
return s;
MB_PTR_ADV(s);
}
return NULL;
}
# endif
#endif
int
vim_isspace(int x)
{
return ((x >= 9 && x <= 13) || x == ' ');
}
void
ga_clear(garray_T *gap)
{
vim_free(gap->ga_data);
ga_init(gap);
}
void
ga_clear_strings(garray_T *gap)
{
int i;
for (i = 0; i < gap->ga_len; ++i)
vim_free(((char_u **)(gap->ga_data))[i]);
ga_clear(gap);
}
void
ga_init(garray_T *gap)
{
gap->ga_data = NULL;
gap->ga_maxlen = 0;
gap->ga_len = 0;
}
void
ga_init2(garray_T *gap, int itemsize, int growsize)
{
ga_init(gap);
gap->ga_itemsize = itemsize;
gap->ga_growsize = growsize;
}
int
ga_grow(garray_T *gap, int n)
{
size_t old_len;
size_t new_len;
char_u *pp;
if (gap->ga_maxlen - gap->ga_len < n)
{
if (n < gap->ga_growsize)
n = gap->ga_growsize;
new_len = gap->ga_itemsize * (gap->ga_len + n);
pp = (gap->ga_data == NULL)
? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
if (pp == NULL)
return FAIL;
old_len = gap->ga_itemsize * gap->ga_maxlen;
vim_memset(pp + old_len, 0, new_len - old_len);
gap->ga_maxlen = gap->ga_len + n;
gap->ga_data = pp;
}
return OK;
}
char_u *
ga_concat_strings(garray_T *gap, char *sep)
{
int i;
int len = 0;
int sep_len = (int)STRLEN(sep);
char_u *s;
char_u *p;
for (i = 0; i < gap->ga_len; ++i)
len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
s = alloc(len + 1);
if (s != NULL)
{
*s = NUL;
p = s;
for (i = 0; i < gap->ga_len; ++i)
{
if (p != s)
{
STRCPY(p, sep);
p += sep_len;
}
STRCPY(p, ((char_u **)(gap->ga_data))[i]);
p += STRLEN(p);
}
}
return s;
}
#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
void
ga_add_string(garray_T *gap, char_u *p)
{
char_u *cp = vim_strsave(p);
if (cp != NULL)
{
if (ga_grow(gap, 1) == OK)
((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
else
vim_free(cp);
}
}
#endif
void
ga_concat(garray_T *gap, char_u *s)
{
int len;
if (s == NULL || *s == NUL)
return;
len = (int)STRLEN(s);
if (ga_grow(gap, len) == OK)
{
mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
gap->ga_len += len;
}
}
void
ga_append(garray_T *gap, int c)
{
if (ga_grow(gap, 1) == OK)
{
*((char *)gap->ga_data + gap->ga_len) = c;
++gap->ga_len;
}
}
#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264) \
|| defined(PROTO)
void
append_ga_line(garray_T *gap)
{
if (gap->ga_len > 0
&& !curbuf->b_p_bin
&& ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
--gap->ga_len;
ga_append(gap, NUL);
ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
gap->ga_len = 0;
}
#endif
static struct modmasktable
{
short mod_mask;
short mod_flag;
char_u name;
} mod_mask_table[] =
{
{MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
{MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
{MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
{MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
#ifdef MACOS_X
{MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
#endif
{MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
{0, 0, NUL}
};
#define MOD_KEYS_ENTRY_SIZE 5
static char_u modifier_keys_table[] =
{
MOD_MASK_SHIFT, '&', '9', '@', '1',
MOD_MASK_SHIFT, '&', '0', '@', '2',
MOD_MASK_SHIFT, '*', '1', '@', '4',
MOD_MASK_SHIFT, '*', '2', '@', '5',
MOD_MASK_SHIFT, '*', '3', '@', '6',
MOD_MASK_SHIFT, '*', '4', 'k', 'D',
MOD_MASK_SHIFT, '*', '5', 'k', 'L',
MOD_MASK_SHIFT, '*', '7', '@', '7',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7',
MOD_MASK_SHIFT, '*', '9', '@', '9',
MOD_MASK_SHIFT, '*', '0', '@', '0',
MOD_MASK_SHIFT, '#', '1', '%', '1',
MOD_MASK_SHIFT, '#', '2', 'k', 'h',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h',
MOD_MASK_SHIFT, '#', '3', 'k', 'I',
MOD_MASK_SHIFT, '#', '4', 'k', 'l',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l',
MOD_MASK_SHIFT, '%', 'a', '%', '3',
MOD_MASK_SHIFT, '%', 'b', '%', '4',
MOD_MASK_SHIFT, '%', 'c', '%', '5',
MOD_MASK_SHIFT, '%', 'd', '%', '7',
MOD_MASK_SHIFT, '%', 'e', '%', '8',
MOD_MASK_SHIFT, '%', 'f', '%', '9',
MOD_MASK_SHIFT, '%', 'g', '%', '0',
MOD_MASK_SHIFT, '%', 'h', '&', '3',
MOD_MASK_SHIFT, '%', 'i', 'k', 'r',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r',
MOD_MASK_SHIFT, '%', 'j', '&', '5',
MOD_MASK_SHIFT, '!', '1', '&', '6',
MOD_MASK_SHIFT, '!', '2', '&', '7',
MOD_MASK_SHIFT, '!', '3', '&', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
NUL
};
static struct key_name_entry
{
int key;
char_u *name;
} key_names_table[] =
{
{' ', (char_u *)"Space"},
{TAB, (char_u *)"Tab"},
{K_TAB, (char_u *)"Tab"},
{NL, (char_u *)"NL"},
{NL, (char_u *)"NewLine"},
{NL, (char_u *)"LineFeed"},
{NL, (char_u *)"LF"},
{CAR, (char_u *)"CR"},
{CAR, (char_u *)"Return"},
{CAR, (char_u *)"Enter"},
{K_BS, (char_u *)"BS"},
{K_BS, (char_u *)"BackSpace"},
{ESC, (char_u *)"Esc"},
{CSI, (char_u *)"CSI"},
{K_CSI, (char_u *)"xCSI"},
{'|', (char_u *)"Bar"},
{'\\', (char_u *)"Bslash"},
{K_DEL, (char_u *)"Del"},
{K_DEL, (char_u *)"Delete"},
{K_KDEL, (char_u *)"kDel"},
{K_UP, (char_u *)"Up"},
{K_DOWN, (char_u *)"Down"},
{K_LEFT, (char_u *)"Left"},
{K_RIGHT, (char_u *)"Right"},
{K_XUP, (char_u *)"xUp"},
{K_XDOWN, (char_u *)"xDown"},
{K_XLEFT, (char_u *)"xLeft"},
{K_XRIGHT, (char_u *)"xRight"},
{K_PS, (char_u *)"PasteStart"},
{K_PE, (char_u *)"PasteEnd"},
{K_F1, (char_u *)"F1"},
{K_F2, (char_u *)"F2"},
{K_F3, (char_u *)"F3"},
{K_F4, (char_u *)"F4"},
{K_F5, (char_u *)"F5"},
{K_F6, (char_u *)"F6"},
{K_F7, (char_u *)"F7"},
{K_F8, (char_u *)"F8"},
{K_F9, (char_u *)"F9"},
{K_F10, (char_u *)"F10"},
{K_F11, (char_u *)"F11"},
{K_F12, (char_u *)"F12"},
{K_F13, (char_u *)"F13"},
{K_F14, (char_u *)"F14"},
{K_F15, (char_u *)"F15"},
{K_F16, (char_u *)"F16"},
{K_F17, (char_u *)"F17"},
{K_F18, (char_u *)"F18"},
{K_F19, (char_u *)"F19"},
{K_F20, (char_u *)"F20"},
{K_F21, (char_u *)"F21"},
{K_F22, (char_u *)"F22"},
{K_F23, (char_u *)"F23"},
{K_F24, (char_u *)"F24"},
{K_F25, (char_u *)"F25"},
{K_F26, (char_u *)"F26"},
{K_F27, (char_u *)"F27"},
{K_F28, (char_u *)"F28"},
{K_F29, (char_u *)"F29"},
{K_F30, (char_u *)"F30"},
{K_F31, (char_u *)"F31"},
{K_F32, (char_u *)"F32"},
{K_F33, (char_u *)"F33"},
{K_F34, (char_u *)"F34"},
{K_F35, (char_u *)"F35"},
{K_F36, (char_u *)"F36"},
{K_F37, (char_u *)"F37"},
{K_XF1, (char_u *)"xF1"},
{K_XF2, (char_u *)"xF2"},
{K_XF3, (char_u *)"xF3"},
{K_XF4, (char_u *)"xF4"},
{K_HELP, (char_u *)"Help"},
{K_UNDO, (char_u *)"Undo"},
{K_INS, (char_u *)"Insert"},
{K_INS, (char_u *)"Ins"},
{K_KINS, (char_u *)"kInsert"},
{K_HOME, (char_u *)"Home"},
{K_KHOME, (char_u *)"kHome"},
{K_XHOME, (char_u *)"xHome"},
{K_ZHOME, (char_u *)"zHome"},
{K_END, (char_u *)"End"},
{K_KEND, (char_u *)"kEnd"},
{K_XEND, (char_u *)"xEnd"},
{K_ZEND, (char_u *)"zEnd"},
{K_PAGEUP, (char_u *)"PageUp"},
{K_PAGEDOWN, (char_u *)"PageDown"},
{K_KPAGEUP, (char_u *)"kPageUp"},
{K_KPAGEDOWN, (char_u *)"kPageDown"},
{K_KPLUS, (char_u *)"kPlus"},
{K_KMINUS, (char_u *)"kMinus"},
{K_KDIVIDE, (char_u *)"kDivide"},
{K_KMULTIPLY, (char_u *)"kMultiply"},
{K_KENTER, (char_u *)"kEnter"},
{K_KPOINT, (char_u *)"kPoint"},
{K_K0, (char_u *)"k0"},
{K_K1, (char_u *)"k1"},
{K_K2, (char_u *)"k2"},
{K_K3, (char_u *)"k3"},
{K_K4, (char_u *)"k4"},
{K_K5, (char_u *)"k5"},
{K_K6, (char_u *)"k6"},
{K_K7, (char_u *)"k7"},
{K_K8, (char_u *)"k8"},
{K_K9, (char_u *)"k9"},
{'<', (char_u *)"lt"},
{K_MOUSE, (char_u *)"Mouse"},
#ifdef FEAT_MOUSE_NET
{K_NETTERM_MOUSE, (char_u *)"NetMouse"},
#endif
#ifdef FEAT_MOUSE_DEC
{K_DEC_MOUSE, (char_u *)"DecMouse"},
#endif
#ifdef FEAT_MOUSE_JSB
{K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
#endif
#ifdef FEAT_MOUSE_PTERM
{K_PTERM_MOUSE, (char_u *)"PtermMouse"},
#endif
#ifdef FEAT_MOUSE_URXVT
{K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
#endif
#ifdef FEAT_MOUSE_SGR
{K_SGR_MOUSE, (char_u *)"SgrMouse"},
{K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelelase"},
#endif
{K_LEFTMOUSE, (char_u *)"LeftMouse"},
{K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
{K_LEFTDRAG, (char_u *)"LeftDrag"},
{K_LEFTRELEASE, (char_u *)"LeftRelease"},
{K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
{K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
{K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
{K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
{K_RIGHTMOUSE, (char_u *)"RightMouse"},
{K_RIGHTDRAG, (char_u *)"RightDrag"},
{K_RIGHTRELEASE, (char_u *)"RightRelease"},
{K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
{K_MOUSEUP, (char_u *)"ScrollWheelDown"},
{K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
{K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
{K_MOUSEDOWN, (char_u *)"MouseDown"},
{K_MOUSEUP, (char_u *)"MouseUp"},
{K_X1MOUSE, (char_u *)"X1Mouse"},
{K_X1DRAG, (char_u *)"X1Drag"},
{K_X1RELEASE, (char_u *)"X1Release"},
{K_X2MOUSE, (char_u *)"X2Mouse"},
{K_X2DRAG, (char_u *)"X2Drag"},
{K_X2RELEASE, (char_u *)"X2Release"},
{K_DROP, (char_u *)"Drop"},
{K_ZERO, (char_u *)"Nul"},
#ifdef FEAT_EVAL
{K_SNR, (char_u *)"SNR"},
#endif
{K_PLUG, (char_u *)"Plug"},
{K_CURSORHOLD, (char_u *)"CursorHold"},
{0, NULL}
};
#define KEY_NAMES_TABLE_LEN (sizeof(key_names_table) / sizeof(struct key_name_entry))
#ifdef FEAT_MOUSE
static struct mousetable
{
int pseudo_code;
int button;
int is_click;
int is_drag;
} mouse_table[] =
{
{(int)KE_LEFTMOUSE, MOUSE_LEFT, TRUE, FALSE},
#ifdef FEAT_GUI
{(int)KE_LEFTMOUSE_NM, MOUSE_LEFT, TRUE, FALSE},
#endif
{(int)KE_LEFTDRAG, MOUSE_LEFT, FALSE, TRUE},
{(int)KE_LEFTRELEASE, MOUSE_LEFT, FALSE, FALSE},
#ifdef FEAT_GUI
{(int)KE_LEFTRELEASE_NM, MOUSE_LEFT, FALSE, FALSE},
#endif
{(int)KE_MIDDLEMOUSE, MOUSE_MIDDLE, TRUE, FALSE},
{(int)KE_MIDDLEDRAG, MOUSE_MIDDLE, FALSE, TRUE},
{(int)KE_MIDDLERELEASE, MOUSE_MIDDLE, FALSE, FALSE},
{(int)KE_RIGHTMOUSE, MOUSE_RIGHT, TRUE, FALSE},
{(int)KE_RIGHTDRAG, MOUSE_RIGHT, FALSE, TRUE},
{(int)KE_RIGHTRELEASE, MOUSE_RIGHT, FALSE, FALSE},
{(int)KE_X1MOUSE, MOUSE_X1, TRUE, FALSE},
{(int)KE_X1DRAG, MOUSE_X1, FALSE, TRUE},
{(int)KE_X1RELEASE, MOUSE_X1, FALSE, FALSE},
{(int)KE_X2MOUSE, MOUSE_X2, TRUE, FALSE},
{(int)KE_X2DRAG, MOUSE_X2, FALSE, TRUE},
{(int)KE_X2RELEASE, MOUSE_X2, FALSE, FALSE},
{(int)KE_IGNORE, MOUSE_RELEASE, FALSE, TRUE},
{(int)KE_IGNORE, MOUSE_RELEASE, FALSE, FALSE},
{0, 0, 0, 0},
};
#endif
int
name_to_mod_mask(int c)
{
int i;
c = TOUPPER_ASC(c);
for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
if (c == mod_mask_table[i].name)
return mod_mask_table[i].mod_flag;
return 0;
}
int
simplify_key(int key, int *modifiers)
{
int i;
int key0;
int key1;
if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
{
if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
{
*modifiers &= ~MOD_MASK_SHIFT;
return K_S_TAB;
}
key0 = KEY2TERMCAP0(key);
key1 = KEY2TERMCAP1(key);
for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
if (key0 == modifier_keys_table[i + 3]
&& key1 == modifier_keys_table[i + 4]
&& (*modifiers & modifier_keys_table[i]))
{
*modifiers &= ~modifier_keys_table[i];
return TERMCAP2KEY(modifier_keys_table[i + 1],
modifier_keys_table[i + 2]);
}
}
return key;
}
int
handle_x_keys(int key)
{
switch (key)
{
case K_XUP: return K_UP;
case K_XDOWN: return K_DOWN;
case K_XLEFT: return K_LEFT;
case K_XRIGHT: return K_RIGHT;
case K_XHOME: return K_HOME;
case K_ZHOME: return K_HOME;
case K_XEND: return K_END;
case K_ZEND: return K_END;
case K_XF1: return K_F1;
case K_XF2: return K_F2;
case K_XF3: return K_F3;
case K_XF4: return K_F4;
case K_S_XF1: return K_S_F1;
case K_S_XF2: return K_S_F2;
case K_S_XF3: return K_S_F3;
case K_S_XF4: return K_S_F4;
}
return key;
}
char_u *
get_special_key_name(int c, int modifiers)
{
static char_u string[MAX_KEY_NAME_LEN + 1];
int i, idx;
int table_idx;
char_u *s;
string[0] = '<';
idx = 1;
if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
c = KEY2TERMCAP1(c);
if (IS_SPECIAL(c))
{
for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
&& (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
{
modifiers |= modifier_keys_table[i];
c = TERMCAP2KEY(modifier_keys_table[i + 3],
modifier_keys_table[i + 4]);
break;
}
}
table_idx = find_special_key_in_table(c);
if (c > 0
#ifdef FEAT_MBYTE
&& (*mb_char2len)(c) == 1
#endif
)
{
if (table_idx < 0
&& (!vim_isprintc(c) || (c & 0x7f) == ' ')
&& (c & 0x80))
{
c &= 0x7f;
modifiers |= MOD_MASK_ALT;
table_idx = find_special_key_in_table(c);
}
if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
{
#ifdef EBCDIC
c = CtrlChar(c);
#else
c += '@';
#endif
modifiers |= MOD_MASK_CTRL;
}
}
for (i = 0; mod_mask_table[i].name != 'A'; i++)
if ((modifiers & mod_mask_table[i].mod_mask)
== mod_mask_table[i].mod_flag)
{
string[idx++] = mod_mask_table[i].name;
string[idx++] = (char_u)'-';
}
if (table_idx < 0)
{
if (IS_SPECIAL(c))
{
string[idx++] = 't';
string[idx++] = '_';
string[idx++] = KEY2TERMCAP0(c);
string[idx++] = KEY2TERMCAP1(c);
}
else
{
#ifdef FEAT_MBYTE
if (has_mbyte && (*mb_char2len)(c) > 1)
idx += (*mb_char2bytes)(c, string + idx);
else
#endif
if (vim_isprintc(c))
string[idx++] = c;
else
{
s = transchar(c);
while (*s)
string[idx++] = *s++;
}
}
}
else
{
size_t len = STRLEN(key_names_table[table_idx].name);
if (len + idx + 2 <= MAX_KEY_NAME_LEN)
{
STRCPY(string + idx, key_names_table[table_idx].name);
idx += (int)len;
}
}
string[idx++] = '>';
string[idx] = NUL;
return string;
}
int
trans_special(
char_u **srcp,
char_u *dst,
int keycode,
int in_string)
{
int modifiers = 0;
int key;
int dlen = 0;
key = find_special_key(srcp, &modifiers, keycode, FALSE, in_string);
if (key == 0)
return 0;
if (modifiers != 0)
{
dst[dlen++] = K_SPECIAL;
dst[dlen++] = KS_MODIFIER;
dst[dlen++] = modifiers;
}
if (IS_SPECIAL(key))
{
dst[dlen++] = K_SPECIAL;
dst[dlen++] = KEY2TERMCAP0(key);
dst[dlen++] = KEY2TERMCAP1(key);
}
#ifdef FEAT_MBYTE
else if (has_mbyte && !keycode)
dlen += (*mb_char2bytes)(key, dst + dlen);
#endif
else if (keycode)
dlen = (int)(add_char2buf(key, dst + dlen) - dst);
else
dst[dlen++] = key;
return dlen;
}
int
find_special_key(
char_u **srcp,
int *modp,
int keycode,
int keep_x_key,
int in_string)
{
char_u *last_dash;
char_u *end_of_name;
char_u *src;
char_u *bp;
int modifiers;
int bit;
int key;
uvarnumber_T n;
int l;
src = *srcp;
if (src[0] != '<')
return 0;
last_dash = src;
for (bp = src + 1; *bp == '-' || vim_isIDc(*bp); bp++)
{
if (*bp == '-')
{
last_dash = bp;
if (bp[1] != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
l = mb_ptr2len(bp + 1);
else
#endif
l = 1;
if (!(in_string && bp[1] == '"') && bp[2] == '>')
bp += l;
else if (in_string && bp[1] == '\\' && bp[2] == '"'
&& bp[3] == '>')
bp += 2;
}
}
if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
bp += 3;
else if (STRNICMP(bp, "char-", 5) == 0)
{
vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
bp += l + 5;
break;
}
}
if (*bp == '>')
{
end_of_name = bp + 1;
modifiers = 0x0;
for (bp = src + 1; bp < last_dash; bp++)
{
if (*bp != '-')
{
bit = name_to_mod_mask(*bp);
if (bit == 0x0)
break;
modifiers |= bit;
}
}
if (bp >= last_dash)
{
if (STRNICMP(last_dash + 1, "char-", 5) == 0
&& VIM_ISDIGIT(last_dash[6]))
{
vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
key = (int)n;
}
else
{
int off = 1;
if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
off = 2;
#ifdef FEAT_MBYTE
if (has_mbyte)
l = mb_ptr2len(last_dash + off);
else
#endif
l = 1;
if (modifiers != 0 && last_dash[l + off] == '>')
key = PTR2CHAR(last_dash + off);
else
{
key = get_special_key_code(last_dash + off);
if (!keep_x_key)
key = handle_x_keys(key);
}
}
if (key != NUL)
{
key = simplify_key(key, &modifiers);
if (!keycode)
{
if (key == K_BS)
key = BS;
else if (key == K_DEL || key == K_KDEL)
key = DEL;
}
if (!IS_SPECIAL(key))
key = extract_modifiers(key, &modifiers);
*modp = modifiers;
*srcp = end_of_name;
return key;
}
}
}
return 0;
}
int
extract_modifiers(int key, int *modp)
{
int modifiers = *modp;
#ifdef MACOS_X
if (!(modifiers & MOD_MASK_CMD))
#endif
if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
{
key = TOUPPER_ASC(key);
modifiers &= ~MOD_MASK_SHIFT;
}
if ((modifiers & MOD_MASK_CTRL)
#ifdef EBCDIC
&& strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
!= NULL
#else
&& ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
#endif
)
{
key = Ctrl_chr(key);
modifiers &= ~MOD_MASK_CTRL;
if (key == 0)
key = K_ZERO;
}
#ifdef MACOS_X
if (!(modifiers & MOD_MASK_CMD))
#endif
if ((modifiers & MOD_MASK_ALT) && key < 0x80
#ifdef FEAT_MBYTE
&& !enc_dbcs
#endif
)
{
key |= 0x80;
modifiers &= ~MOD_MASK_ALT;
}
*modp = modifiers;
return key;
}
int
find_special_key_in_table(int c)
{
int i;
for (i = 0; key_names_table[i].name != NULL; i++)
if (c == key_names_table[i].key)
break;
if (key_names_table[i].name == NULL)
i = -1;
return i;
}
int
get_special_key_code(char_u *name)
{
char_u *table_name;
char_u string[3];
int i, j;
if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
{
string[0] = name[2];
string[1] = name[3];
string[2] = NUL;
if (add_termcap_entry(string, FALSE) == OK)
return TERMCAP2KEY(name[2], name[3]);
}
else
for (i = 0; key_names_table[i].name != NULL; i++)
{
table_name = key_names_table[i].name;
for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++)
if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
break;
if (!vim_isIDc(name[j]) && table_name[j] == NUL)
return key_names_table[i].key;
}
return 0;
}
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
char_u *
get_key_name(int i)
{
if (i >= (int)KEY_NAMES_TABLE_LEN)
return NULL;
return key_names_table[i].name;
}
#endif
#if defined(FEAT_MOUSE) || defined(PROTO)
int
get_mouse_button(int code, int *is_click, int *is_drag)
{
int i;
for (i = 0; mouse_table[i].pseudo_code; i++)
if (code == mouse_table[i].pseudo_code)
{
*is_click = mouse_table[i].is_click;
*is_drag = mouse_table[i].is_drag;
return mouse_table[i].button;
}
return 0;
}
int
get_pseudo_mouse_code(
int button,
int is_click,
int is_drag)
{
int i;
for (i = 0; mouse_table[i].pseudo_code; i++)
if (button == mouse_table[i].button
&& is_click == mouse_table[i].is_click
&& is_drag == mouse_table[i].is_drag)
{
#ifdef FEAT_GUI
if (mouse_col < 0 || mouse_col > MOUSE_COLOFF)
{
if (mouse_col < 0)
mouse_col = 0;
else
mouse_col -= MOUSE_COLOFF;
if (mouse_table[i].pseudo_code == (int)KE_LEFTMOUSE)
return (int)KE_LEFTMOUSE_NM;
if (mouse_table[i].pseudo_code == (int)KE_LEFTRELEASE)
return (int)KE_LEFTRELEASE_NM;
}
#endif
return mouse_table[i].pseudo_code;
}
return (int)KE_IGNORE;
}
#endif
int
get_fileformat(buf_T *buf)
{
int c = *buf->b_p_ff;
if (buf->b_p_bin || c == 'u')
return EOL_UNIX;
if (c == 'm')
return EOL_MAC;
return EOL_DOS;
}
int
get_fileformat_force(
buf_T *buf,
exarg_T *eap)
{
int c;
if (eap != NULL && eap->force_ff != 0)
c = eap->cmd[eap->force_ff];
else
{
if ((eap != NULL && eap->force_bin != 0)
? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
return EOL_UNIX;
c = *buf->b_p_ff;
}
if (c == 'u')
return EOL_UNIX;
if (c == 'm')
return EOL_MAC;
return EOL_DOS;
}
void
set_fileformat(
int t,
int opt_flags)
{
char *p = NULL;
switch (t)
{
case EOL_DOS:
p = FF_DOS;
curbuf->b_p_tx = TRUE;
break;
case EOL_UNIX:
p = FF_UNIX;
curbuf->b_p_tx = FALSE;
break;
case EOL_MAC:
p = FF_MAC;
curbuf->b_p_tx = FALSE;
break;
}
if (p != NULL)
set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
OPT_FREE | opt_flags, 0);
check_status(curbuf);
redraw_tabline = TRUE;
#ifdef FEAT_TITLE
need_maketitle = TRUE;
#endif
}
int
default_fileformat(void)
{
switch (*p_ffs)
{
case 'm': return EOL_MAC;
case 'd': return EOL_DOS;
}
return EOL_UNIX;
}
int
call_shell(char_u *cmd, int opt)
{
char_u *ncmd;
int retval;
#ifdef FEAT_PROFILE
proftime_T wait_time;
#endif
if (p_verbose > 3)
{
verbose_enter();
smsg((char_u *)_("Calling shell to execute: \"%s\""),
cmd == NULL ? p_sh : cmd);
out_char('\n');
cursor_on();
verbose_leave();
}
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
prof_child_enter(&wait_time);
#endif
if (*p_sh == NUL)
{
EMSG(_(e_shellempty));
retval = -1;
}
else
{
#ifdef FEAT_GUI_MSWIN
gui_mch_mousehide(FALSE);
#endif
#ifdef FEAT_GUI
++hold_gui_events;
#endif
tag_freematch();
if (cmd == NULL || *p_sxq == NUL)
retval = mch_call_shell(cmd, opt);
else
{
char_u *ecmd = cmd;
if (*p_sxe != NUL && STRCMP(p_sxq, "(") == 0)
{
ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
if (ecmd == NULL)
ecmd = cmd;
}
ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
if (ncmd != NULL)
{
STRCPY(ncmd, p_sxq);
STRCAT(ncmd, ecmd);
STRCAT(ncmd, STRCMP(p_sxq, "(") == 0 ? (char_u *)")"
: STRCMP(p_sxq, "\"(") == 0 ? (char_u *)")\""
: p_sxq);
retval = mch_call_shell(ncmd, opt);
vim_free(ncmd);
}
else
retval = -1;
if (ecmd != cmd)
vim_free(ecmd);
}
#ifdef FEAT_GUI
--hold_gui_events;
#endif
shell_resized_check();
}
#ifdef FEAT_EVAL
set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
prof_child_exit(&wait_time);
# endif
#endif
return retval;
}
int
get_real_state(void)
{
if (State & NORMAL)
{
if (VIsual_active)
{
if (VIsual_select)
return SELECTMODE;
return VISUAL;
}
else if (finish_op)
return OP_PENDING;
}
return State;
}
#if defined(FEAT_MBYTE) || defined(PROTO)
int
after_pathsep(char_u *b, char_u *p)
{
return p > b && vim_ispathsep(p[-1])
&& (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
}
#endif
int
same_directory(char_u *f1, char_u *f2)
{
char_u ffname[MAXPATHL];
char_u *t1;
char_u *t2;
if (f1 == NULL || f2 == NULL)
return FALSE;
(void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
t1 = gettail_sep(ffname);
t2 = gettail_sep(f2);
return (t1 - ffname == t2 - f2
&& pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
}
#if defined(FEAT_SESSION) || defined(MSWIN) || defined(FEAT_GUI_MAC) \
|| defined(FEAT_GUI_GTK) \
|| defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
|| defined(PROTO)
int
vim_chdirfile(char_u *fname)
{
char_u dir[MAXPATHL];
vim_strncpy(dir, fname, MAXPATHL - 1);
*gettail_sep(dir) = NUL;
return mch_chdir((char *)dir) == 0 ? OK : FAIL;
}
#endif
#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
static int
illegal_slash(const char *name)
{
if (name[0] == NUL)
return FALSE;
if (name[strlen(name) - 1] != '/')
return FALSE;
if (mch_isdir((char_u *)name))
return FALSE;
return TRUE;
}
int
vim_stat(const char *name, stat_T *stp)
{
return illegal_slash(name) ? -1 : stat(name, stp);
}
#endif
#if defined(CURSOR_SHAPE) || defined(PROTO)
cursorentry_T shape_table[SHAPE_IDX_COUNT] =
{
{0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
{0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
{0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
};
#ifdef FEAT_MOUSESHAPE
static char * mshape_names[] =
{
"arrow",
"blank",
"beam",
"updown",
"udsizing",
"leftright",
"lrsizing",
"busy",
"no",
"crosshair",
"hand1",
"hand2",
"pencil",
"question",
"rightup-arrow",
"up-arrow",
NULL
};
#endif
char_u *
parse_shape_opt(int what)
{
char_u *modep;
char_u *colonp;
char_u *commap;
char_u *slashp;
char_u *p, *endp;
int idx = 0;
int all_idx;
int len;
int i;
long n;
int found_ve = FALSE;
int round;
for (round = 1; round <= 2; ++round)
{
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
modep = p_mouseshape;
else
#endif
modep = p_guicursor;
while (*modep != NUL)
{
colonp = vim_strchr(modep, ':');
commap = vim_strchr(modep, ',');
if (colonp == NULL || (commap != NULL && commap < colonp))
return (char_u *)N_("E545: Missing colon");
if (colonp == modep)
return (char_u *)N_("E546: Illegal mode");
all_idx = -1;
while (modep < colonp || all_idx >= 0)
{
if (all_idx < 0)
{
if (modep[1] == '-' || modep[1] == ':')
len = 1;
else
len = 2;
if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
all_idx = SHAPE_IDX_COUNT - 1;
else
{
for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
if (STRNICMP(modep, shape_table[idx].name, len)
== 0)
break;
if (idx == SHAPE_IDX_COUNT
|| (shape_table[idx].used_for & what) == 0)
return (char_u *)N_("E546: Illegal mode");
if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
found_ve = TRUE;
}
modep += len + 1;
}
if (all_idx >= 0)
idx = all_idx--;
else if (round == 2)
{
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
{
shape_table[idx].mshape = 0;
}
else
#endif
{
shape_table[idx].shape = SHAPE_BLOCK;
shape_table[idx].blinkwait = 700L;
shape_table[idx].blinkon = 400L;
shape_table[idx].blinkoff = 250L;
}
}
for (p = colonp + 1; *p && *p != ','; )
{
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
{
for (i = 0; ; ++i)
{
if (mshape_names[i] == NULL)
{
if (!VIM_ISDIGIT(*p))
return (char_u *)N_("E547: Illegal mouseshape");
if (round == 2)
shape_table[idx].mshape =
getdigits(&p) + MSHAPE_NUMBERED;
else
(void)getdigits(&p);
break;
}
len = (int)STRLEN(mshape_names[i]);
if (STRNICMP(p, mshape_names[i], len) == 0)
{
if (round == 2)
shape_table[idx].mshape = i;
p += len;
break;
}
}
}
else
#endif
{
i = *p;
len = 0;
if (STRNICMP(p, "ver", 3) == 0)
len = 3;
else if (STRNICMP(p, "hor", 3) == 0)
len = 3;
else if (STRNICMP(p, "blinkwait", 9) == 0)
len = 9;
else if (STRNICMP(p, "blinkon", 7) == 0)
len = 7;
else if (STRNICMP(p, "blinkoff", 8) == 0)
len = 8;
if (len != 0)
{
p += len;
if (!VIM_ISDIGIT(*p))
return (char_u *)N_("E548: digit expected");
n = getdigits(&p);
if (len == 3)
{
if (n == 0)
return (char_u *)N_("E549: Illegal percentage");
if (round == 2)
{
if (TOLOWER_ASC(i) == 'v')
shape_table[idx].shape = SHAPE_VER;
else
shape_table[idx].shape = SHAPE_HOR;
shape_table[idx].percentage = n;
}
}
else if (round == 2)
{
if (len == 9)
shape_table[idx].blinkwait = n;
else if (len == 7)
shape_table[idx].blinkon = n;
else
shape_table[idx].blinkoff = n;
}
}
else if (STRNICMP(p, "block", 5) == 0)
{
if (round == 2)
shape_table[idx].shape = SHAPE_BLOCK;
p += 5;
}
else
{
endp = vim_strchr(p, '-');
if (commap == NULL)
{
if (endp == NULL)
endp = p + STRLEN(p);
}
else if (endp > commap || endp == NULL)
endp = commap;
slashp = vim_strchr(p, '/');
if (slashp != NULL && slashp < endp)
{
i = syn_check_group(p, (int)(slashp - p));
p = slashp + 1;
}
if (round == 2)
{
shape_table[idx].id = syn_check_group(p,
(int)(endp - p));
shape_table[idx].id_lm = shape_table[idx].id;
if (slashp != NULL && slashp < endp)
shape_table[idx].id = i;
}
p = endp;
}
}
if (*p == '-')
++p;
}
}
modep = p;
if (*modep == ',')
++modep;
}
}
if (!found_ve)
{
#ifdef FEAT_MOUSESHAPE
if (what == SHAPE_MOUSE)
{
shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
}
else
#endif
{
shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
shape_table[SHAPE_IDX_VE].percentage =
shape_table[SHAPE_IDX_V].percentage;
shape_table[SHAPE_IDX_VE].blinkwait =
shape_table[SHAPE_IDX_V].blinkwait;
shape_table[SHAPE_IDX_VE].blinkon =
shape_table[SHAPE_IDX_V].blinkon;
shape_table[SHAPE_IDX_VE].blinkoff =
shape_table[SHAPE_IDX_V].blinkoff;
shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
}
}
return NULL;
}
# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
|| defined(FEAT_MOUSESHAPE) || defined(PROTO)
int
get_shape_idx(int mouse)
{
#ifdef FEAT_MOUSESHAPE
if (mouse && (State == HITRETURN || State == ASKMORE))
{
# ifdef FEAT_GUI
int x, y;
gui_mch_getmouse(&x, &y);
if (Y_2_ROW(y) == Rows - 1)
return SHAPE_IDX_MOREL;
# endif
return SHAPE_IDX_MORE;
}
if (mouse && drag_status_line)
return SHAPE_IDX_SDRAG;
if (mouse && drag_sep_line)
return SHAPE_IDX_VDRAG;
#endif
if (!mouse && State == SHOWMATCH)
return SHAPE_IDX_SM;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
return SHAPE_IDX_R;
#endif
if (State & REPLACE_FLAG)
return SHAPE_IDX_R;
if (State & INSERT)
return SHAPE_IDX_I;
if (State & CMDLINE)
{
if (cmdline_at_end())
return SHAPE_IDX_C;
if (cmdline_overstrike())
return SHAPE_IDX_CR;
return SHAPE_IDX_CI;
}
if (finish_op)
return SHAPE_IDX_O;
if (VIsual_active)
{
if (*p_sel == 'e')
return SHAPE_IDX_VE;
else
return SHAPE_IDX_V;
}
return SHAPE_IDX_N;
}
#endif
# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
static int old_mouse_shape = 0;
void
update_mouseshape(int shape_idx)
{
int new_mouse_shape;
if (!gui.in_use || gui.starting)
return;
if (shape_idx == -1 && char_avail())
{
postponed_mouseshape = TRUE;
return;
}
if (*p_mouse == NUL
&& (shape_idx == SHAPE_IDX_CLINE
|| shape_idx == SHAPE_IDX_STATUS
|| shape_idx == SHAPE_IDX_VSEP))
shape_idx = -2;
if (shape_idx == -2
&& old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
&& old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
&& old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
return;
if (shape_idx < 0)
new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
else
new_mouse_shape = shape_table[shape_idx].mshape;
if (new_mouse_shape != old_mouse_shape)
{
mch_set_mouse_shape(new_mouse_shape);
old_mouse_shape = new_mouse_shape;
}
postponed_mouseshape = FALSE;
}
# endif
#endif
typedef struct ff_stack
{
struct ff_stack *ffs_prev;
char_u *ffs_fix_path;
#ifdef FEAT_PATH_EXTRA
char_u *ffs_wc_path;
#endif
char_u **ffs_filearray;
int ffs_filearray_size;
char_u ffs_filearray_cur;
int ffs_stage;
int ffs_level;
int ffs_star_star_empty;
} ff_stack_T;
typedef struct ff_visited
{
struct ff_visited *ffv_next;
#ifdef FEAT_PATH_EXTRA
char_u *ffv_wc_path;
#endif
#ifdef UNIX
int ffv_dev_valid;
dev_t ffv_dev;
ino_t ffv_ino;
#endif
char_u ffv_fname[1];
} ff_visited_T;
typedef struct ff_visited_list_hdr
{
struct ff_visited_list_hdr *ffvl_next;
char_u *ffvl_filename;
ff_visited_T *ffvl_visited_list;
} ff_visited_list_hdr_T;
#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
typedef struct ff_search_ctx_T
{
ff_stack_T *ffsc_stack_ptr;
ff_visited_list_hdr_T *ffsc_visited_list;
ff_visited_list_hdr_T *ffsc_dir_visited_list;
ff_visited_list_hdr_T *ffsc_visited_lists_list;
ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
char_u *ffsc_file_to_search;
char_u *ffsc_start_dir;
char_u *ffsc_fix_path;
#ifdef FEAT_PATH_EXTRA
char_u *ffsc_wc_path;
int ffsc_level;
char_u **ffsc_stopdirs_v;
#endif
int ffsc_find_what;
int ffsc_tagfile;
} ff_search_ctx_T;
#ifdef FEAT_PATH_EXTRA
static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
#else
static int ff_check_visited(ff_visited_T **, char_u *);
#endif
static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
static void ff_free_visited_list(ff_visited_T *vl);
static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, ff_visited_list_hdr_T **list_headp);
#ifdef FEAT_PATH_EXTRA
static int ff_wc_equal(char_u *s1, char_u *s2);
#endif
static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
static void ff_clear(ff_search_ctx_T *search_ctx);
static void ff_free_stack_element(ff_stack_T *stack_ptr);
#ifdef FEAT_PATH_EXTRA
static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
#else
static ff_stack_T *ff_create_stack_element(char_u *, int, int);
#endif
#ifdef FEAT_PATH_EXTRA
static int ff_path_in_stoplist(char_u *, int, char_u **);
#endif
static char_u e_pathtoolong[] = N_("E854: path too long for completion");
#if 0
static void *ff_fn_search_context = NULL;
char_u *
vim_findfirst(char_u *path, char_u *filename, int level)
{
ff_fn_search_context =
vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
ff_fn_search_context, rel_fname);
if (NULL == ff_fn_search_context)
return NULL;
else
return vim_findnext()
}
char_u *
vim_findnext(void)
{
char_u *ret = vim_findfile(ff_fn_search_context);
if (NULL == ret)
{
vim_findfile_cleanup(ff_fn_search_context);
ff_fn_search_context = NULL;
}
return ret;
}
#endif
void *
vim_findfile_init(
char_u *path,
char_u *filename,
char_u *stopdirs UNUSED,
int level,
int free_visited,
int find_what,
void *search_ctx_arg,
int tagfile,
char_u *rel_fname)
{
#ifdef FEAT_PATH_EXTRA
char_u *wc_part;
#endif
ff_stack_T *sptr;
ff_search_ctx_T *search_ctx;
if (search_ctx_arg != NULL)
search_ctx = search_ctx_arg;
else
{
search_ctx = (ff_search_ctx_T*)alloc((unsigned)sizeof(ff_search_ctx_T));
if (search_ctx == NULL)
goto error_return;
vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
}
search_ctx->ffsc_find_what = find_what;
search_ctx->ffsc_tagfile = tagfile;
ff_clear(search_ctx);
if (free_visited == TRUE)
vim_findfile_free_visited(search_ctx);
else
{
search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
&search_ctx->ffsc_visited_lists_list);
if (search_ctx->ffsc_visited_list == NULL)
goto error_return;
search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
&search_ctx->ffsc_dir_visited_lists_list);
if (search_ctx->ffsc_dir_visited_list == NULL)
goto error_return;
}
if (ff_expand_buffer == NULL)
{
ff_expand_buffer = (char_u*)alloc(MAXPATHL);
if (ff_expand_buffer == NULL)
goto error_return;
}
if (path[0] == '.'
&& (vim_ispathsep(path[1]) || path[1] == NUL)
&& (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
&& rel_fname != NULL)
{
int len = (int)(gettail(rel_fname) - rel_fname);
if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
{
vim_strncpy(ff_expand_buffer, rel_fname, len);
search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
}
else
search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
if (search_ctx->ffsc_start_dir == NULL)
goto error_return;
if (*++path != NUL)
++path;
}
else if (*path == NUL || !vim_isAbsName(path))
{
#ifdef BACKSLASH_IN_FILENAME
if (*path != NUL && path[1] == ':')
{
char_u drive[3];
drive[0] = path[0];
drive[1] = ':';
drive[2] = NUL;
if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
goto error_return;
path += 2;
}
else
#endif
if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
goto error_return;
search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
if (search_ctx->ffsc_start_dir == NULL)
goto error_return;
#ifdef BACKSLASH_IN_FILENAME
if ((*path == '/' || *path == '\\')
&& path[1] != path[0]
&& search_ctx->ffsc_start_dir[1] == ':')
search_ctx->ffsc_start_dir[2] = NUL;
#endif
}
#ifdef FEAT_PATH_EXTRA
if (stopdirs != NULL)
{
char_u *walker = stopdirs;
int dircount;
while (*walker == ';')
walker++;
dircount = 1;
search_ctx->ffsc_stopdirs_v =
(char_u **)alloc((unsigned)sizeof(char_u *));
if (search_ctx->ffsc_stopdirs_v != NULL)
{
do
{
char_u *helper;
void *ptr;
helper = walker;
ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
(dircount + 1) * sizeof(char_u *));
if (ptr)
search_ctx->ffsc_stopdirs_v = ptr;
else
break;
walker = vim_strchr(walker, ';');
if (walker)
{
search_ctx->ffsc_stopdirs_v[dircount-1] =
vim_strnsave(helper, (int)(walker - helper));
walker++;
}
else
search_ctx->ffsc_stopdirs_v[dircount-1] =
vim_strsave(helper);
dircount++;
} while (walker != NULL);
search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
}
}
#endif
#ifdef FEAT_PATH_EXTRA
search_ctx->ffsc_level = level;
wc_part = vim_strchr(path, '*');
if (wc_part != NULL)
{
int llevel;
int len;
char *errpt;
search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
len = 0;
while (*wc_part != NUL)
{
if (len + 5 >= MAXPATHL)
{
EMSG(_(e_pathtoolong));
break;
}
if (STRNCMP(wc_part, "**", 2) == 0)
{
ff_expand_buffer[len++] = *wc_part++;
ff_expand_buffer[len++] = *wc_part++;
llevel = strtol((char *)wc_part, &errpt, 10);
if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
ff_expand_buffer[len++] = llevel;
else if ((char_u *)errpt != wc_part && llevel == 0)
len -= 2;
else
ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
wc_part = (char_u *)errpt;
if (*wc_part != NUL && !vim_ispathsep(*wc_part))
{
EMSG2(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
goto error_return;
}
}
else
ff_expand_buffer[len++] = *wc_part++;
}
ff_expand_buffer[len] = NUL;
search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
if (search_ctx->ffsc_wc_path == NULL)
goto error_return;
}
else
#endif
search_ctx->ffsc_fix_path = vim_strsave(path);
if (search_ctx->ffsc_start_dir == NULL)
{
search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
if (search_ctx->ffsc_start_dir == NULL)
goto error_return;
search_ctx->ffsc_fix_path[0] = NUL;
}
if (STRLEN(search_ctx->ffsc_start_dir)
+ STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
{
EMSG(_(e_pathtoolong));
goto error_return;
}
STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
add_pathsep(ff_expand_buffer);
{
int eb_len = (int)STRLEN(ff_expand_buffer);
char_u *buf = alloc(eb_len
+ (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
STRCPY(buf, ff_expand_buffer);
STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
if (mch_isdir(buf))
{
STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
add_pathsep(ff_expand_buffer);
}
#ifdef FEAT_PATH_EXTRA
else
{
char_u *p = gettail(search_ctx->ffsc_fix_path);
char_u *wc_path = NULL;
char_u *temp = NULL;
int len = 0;
if (p > search_ctx->ffsc_fix_path)
{
len = (int)(p - search_ctx->ffsc_fix_path) - 1;
STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
add_pathsep(ff_expand_buffer);
}
else
len = (int)STRLEN(search_ctx->ffsc_fix_path);
if (search_ctx->ffsc_wc_path != NULL)
{
wc_path = vim_strsave(search_ctx->ffsc_wc_path);
temp = alloc((int)(STRLEN(search_ctx->ffsc_wc_path)
+ STRLEN(search_ctx->ffsc_fix_path + len)
+ 1));
if (temp == NULL || wc_path == NULL)
{
vim_free(buf);
vim_free(temp);
vim_free(wc_path);
goto error_return;
}
STRCPY(temp, search_ctx->ffsc_fix_path + len);
STRCAT(temp, search_ctx->ffsc_wc_path);
vim_free(search_ctx->ffsc_wc_path);
vim_free(wc_path);
search_ctx->ffsc_wc_path = temp;
}
}
#endif
vim_free(buf);
}
sptr = ff_create_stack_element(ff_expand_buffer,
#ifdef FEAT_PATH_EXTRA
search_ctx->ffsc_wc_path,
#endif
level, 0);
if (sptr == NULL)
goto error_return;
ff_push(search_ctx, sptr);
search_ctx->ffsc_file_to_search = vim_strsave(filename);
if (search_ctx->ffsc_file_to_search == NULL)
goto error_return;
return search_ctx;
error_return:
vim_findfile_cleanup(search_ctx);
return NULL;
}
#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
char_u *
vim_findfile_stopdir(char_u *buf)
{
char_u *r_ptr = buf;
while (*r_ptr != NUL && *r_ptr != ';')
{
if (r_ptr[0] == '\\' && r_ptr[1] == ';')
{
STRMOVE(r_ptr, r_ptr + 1);
r_ptr++;
}
r_ptr++;
}
if (*r_ptr == ';')
{
*r_ptr = 0;
r_ptr++;
}
else if (*r_ptr == NUL)
r_ptr = NULL;
return r_ptr;
}
#endif
void
vim_findfile_cleanup(void *ctx)
{
if (ctx == NULL)
return;
vim_findfile_free_visited(ctx);
ff_clear(ctx);
vim_free(ctx);
}
char_u *
vim_findfile(void *search_ctx_arg)
{
char_u *file_path;
#ifdef FEAT_PATH_EXTRA
char_u *rest_of_wildcards;
char_u *path_end = NULL;
#endif
ff_stack_T *stackp;
#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
int len;
#endif
int i;
char_u *p;
#ifdef FEAT_SEARCHPATH
char_u *suf;
#endif
ff_search_ctx_T *search_ctx;
if (search_ctx_arg == NULL)
return NULL;
search_ctx = (ff_search_ctx_T *)search_ctx_arg;
if ((file_path = alloc((int)MAXPATHL)) == NULL)
return NULL;
#ifdef FEAT_PATH_EXTRA
if (search_ctx->ffsc_start_dir != NULL)
path_end = &search_ctx->ffsc_start_dir[
STRLEN(search_ctx->ffsc_start_dir)];
#endif
#ifdef FEAT_PATH_EXTRA
for (;;)
{
#endif
for (;;)
{
ui_breakcheck();
if (got_int)
break;
stackp = ff_pop(search_ctx);
if (stackp == NULL)
break;
if (stackp->ffs_filearray == NULL
&& ff_check_visited(&search_ctx->ffsc_dir_visited_list
->ffvl_visited_list,
stackp->ffs_fix_path
#ifdef FEAT_PATH_EXTRA
, stackp->ffs_wc_path
#endif
) == FAIL)
{
#ifdef FF_VERBOSE
if (p_verbose >= 5)
{
verbose_enter_scroll();
smsg((char_u *)"Already Searched: %s (%s)",
stackp->ffs_fix_path, stackp->ffs_wc_path);
msg_puts((char_u *)"\n");
verbose_leave_scroll();
}
#endif
ff_free_stack_element(stackp);
continue;
}
#ifdef FF_VERBOSE
else if (p_verbose >= 5)
{
verbose_enter_scroll();
smsg((char_u *)"Searching: %s (%s)",
stackp->ffs_fix_path, stackp->ffs_wc_path);
msg_puts((char_u *)"\n");
verbose_leave_scroll();
}
#endif
if (stackp->ffs_level <= 0)
{
ff_free_stack_element(stackp);
continue;
}
file_path[0] = NUL;
if (stackp->ffs_filearray == NULL)
{
char_u *dirptrs[2];
dirptrs[0] = file_path;
dirptrs[1] = NULL;
if (!vim_isAbsName(stackp->ffs_fix_path)
&& search_ctx->ffsc_start_dir)
{
if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
{
STRCPY(file_path, search_ctx->ffsc_start_dir);
add_pathsep(file_path);
}
else
goto fail;
}
if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
{
STRCAT(file_path, stackp->ffs_fix_path);
add_pathsep(file_path);
}
else
goto fail;
#ifdef FEAT_PATH_EXTRA
rest_of_wildcards = stackp->ffs_wc_path;
if (*rest_of_wildcards != NUL)
{
len = (int)STRLEN(file_path);
if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
{
p = rest_of_wildcards + 2;
if (*p > 0)
{
(*p)--;
if (len + 1 < MAXPATHL)
file_path[len++] = '*';
else
goto fail;
}
if (*p == 0)
{
STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
}
else
rest_of_wildcards += 3;
if (stackp->ffs_star_star_empty == 0)
{
stackp->ffs_star_star_empty = 1;
dirptrs[1] = stackp->ffs_fix_path;
}
}
while (*rest_of_wildcards
&& !vim_ispathsep(*rest_of_wildcards))
if (len + 1 < MAXPATHL)
file_path[len++] = *rest_of_wildcards++;
else
goto fail;
file_path[len] = NUL;
if (vim_ispathsep(*rest_of_wildcards))
rest_of_wildcards++;
}
#endif
if (path_with_url(dirptrs[0]))
{
stackp->ffs_filearray = (char_u **)
alloc((unsigned)sizeof(char *));
if (stackp->ffs_filearray != NULL
&& (stackp->ffs_filearray[0]
= vim_strsave(dirptrs[0])) != NULL)
stackp->ffs_filearray_size = 1;
else
stackp->ffs_filearray_size = 0;
}
else
expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
&stackp->ffs_filearray_size,
&stackp->ffs_filearray,
EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
stackp->ffs_filearray_cur = 0;
stackp->ffs_stage = 0;
}
#ifdef FEAT_PATH_EXTRA
else
rest_of_wildcards = &stackp->ffs_wc_path[
STRLEN(stackp->ffs_wc_path)];
#endif
if (stackp->ffs_stage == 0)
{
#ifdef FEAT_PATH_EXTRA
if (*rest_of_wildcards == NUL)
#endif
{
for (i = stackp->ffs_filearray_cur;
i < stackp->ffs_filearray_size; ++i)
{
if (!path_with_url(stackp->ffs_filearray[i])
&& !mch_isdir(stackp->ffs_filearray[i]))
continue;
if (STRLEN(stackp->ffs_filearray[i]) + 1
+ STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
{
STRCPY(file_path, stackp->ffs_filearray[i]);
add_pathsep(file_path);
STRCAT(file_path, search_ctx->ffsc_file_to_search);
}
else
goto fail;
#ifdef FEAT_SEARCHPATH
len = (int)STRLEN(file_path);
if (search_ctx->ffsc_tagfile)
suf = (char_u *)"";
else
suf = curbuf->b_p_sua;
for (;;)
#endif
{
if ((path_with_url(file_path)
|| (mch_getperm(file_path) >= 0
&& (search_ctx->ffsc_find_what
== FINDFILE_BOTH
|| ((search_ctx->ffsc_find_what
== FINDFILE_DIR)
== mch_isdir(file_path)))))
#ifndef FF_VERBOSE
&& (ff_check_visited(
&search_ctx->ffsc_visited_list->ffvl_visited_list,
file_path
#ifdef FEAT_PATH_EXTRA
, (char_u *)""
#endif
) == OK)
#endif
)
{
#ifdef FF_VERBOSE
if (ff_check_visited(
&search_ctx->ffsc_visited_list->ffvl_visited_list,
file_path
#ifdef FEAT_PATH_EXTRA
, (char_u *)""
#endif
) == FAIL)
{
if (p_verbose >= 5)
{
verbose_enter_scroll();
smsg((char_u *)"Already: %s",
file_path);
msg_puts((char_u *)"\n");
verbose_leave_scroll();
}
continue;
}
#endif
stackp->ffs_filearray_cur = i + 1;
ff_push(search_ctx, stackp);
if (!path_with_url(file_path))
simplify_filename(file_path);
if (mch_dirname(ff_expand_buffer, MAXPATHL)
== OK)
{
p = shorten_fname(file_path,
ff_expand_buffer);
if (p != NULL)
STRMOVE(file_path, p);
}
#ifdef FF_VERBOSE
if (p_verbose >= 5)
{
verbose_enter_scroll();
smsg((char_u *)"HIT: %s", file_path);
msg_puts((char_u *)"\n");
verbose_leave_scroll();
}
#endif
return file_path;
}
#ifdef FEAT_SEARCHPATH
if (*suf == NUL)
break;
copy_option_part(&suf, file_path + len,
MAXPATHL - len, ",");
#endif
}
}
}
#ifdef FEAT_PATH_EXTRA
else
{
for (i = stackp->ffs_filearray_cur;
i < stackp->ffs_filearray_size; ++i)
{
if (!mch_isdir(stackp->ffs_filearray[i]))
continue;
ff_push(search_ctx,
ff_create_stack_element(
stackp->ffs_filearray[i],
rest_of_wildcards,
stackp->ffs_level - 1, 0));
}
}
#endif
stackp->ffs_filearray_cur = 0;
stackp->ffs_stage = 1;
}
#ifdef FEAT_PATH_EXTRA
if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
{
for (i = stackp->ffs_filearray_cur;
i < stackp->ffs_filearray_size; ++i)
{
if (fnamecmp(stackp->ffs_filearray[i],
stackp->ffs_fix_path) == 0)
continue;
if (!mch_isdir(stackp->ffs_filearray[i]))
continue;
ff_push(search_ctx,
ff_create_stack_element(stackp->ffs_filearray[i],
stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
}
}
#endif
ff_free_stack_element(stackp);
}
#ifdef FEAT_PATH_EXTRA
if (search_ctx->ffsc_start_dir
&& search_ctx->ffsc_stopdirs_v != NULL && !got_int)
{
ff_stack_T *sptr;
if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
(int)(path_end - search_ctx->ffsc_start_dir),
search_ctx->ffsc_stopdirs_v) == TRUE)
break;
while (path_end > search_ctx->ffsc_start_dir
&& vim_ispathsep(*path_end))
path_end--;
while (path_end > search_ctx->ffsc_start_dir
&& !vim_ispathsep(path_end[-1]))
path_end--;
*path_end = 0;
path_end--;
if (*search_ctx->ffsc_start_dir == 0)
break;
if (STRLEN(search_ctx->ffsc_start_dir) + 1
+ STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
{
STRCPY(file_path, search_ctx->ffsc_start_dir);
add_pathsep(file_path);
STRCAT(file_path, search_ctx->ffsc_fix_path);
}
else
goto fail;
sptr = ff_create_stack_element(file_path,
search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
if (sptr == NULL)
break;
ff_push(search_ctx, sptr);
}
else
break;
}
#endif
fail:
vim_free(file_path);
return NULL;
}
void
vim_findfile_free_visited(void *search_ctx_arg)
{
ff_search_ctx_T *search_ctx;
if (search_ctx_arg == NULL)
return;
search_ctx = (ff_search_ctx_T *)search_ctx_arg;
vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
}
static void
vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
{
ff_visited_list_hdr_T *vp;
while (*list_headp != NULL)
{
vp = (*list_headp)->ffvl_next;
ff_free_visited_list((*list_headp)->ffvl_visited_list);
vim_free((*list_headp)->ffvl_filename);
vim_free(*list_headp);
*list_headp = vp;
}
*list_headp = NULL;
}
static void
ff_free_visited_list(ff_visited_T *vl)
{
ff_visited_T *vp;
while (vl != NULL)
{
vp = vl->ffv_next;
#ifdef FEAT_PATH_EXTRA
vim_free(vl->ffv_wc_path);
#endif
vim_free(vl);
vl = vp;
}
vl = NULL;
}
static ff_visited_list_hdr_T*
ff_get_visited_list(
char_u *filename,
ff_visited_list_hdr_T **list_headp)
{
ff_visited_list_hdr_T *retptr = NULL;
if (*list_headp != NULL)
{
retptr = *list_headp;
while (retptr != NULL)
{
if (fnamecmp(filename, retptr->ffvl_filename) == 0)
{
#ifdef FF_VERBOSE
if (p_verbose >= 5)
{
verbose_enter_scroll();
smsg((char_u *)"ff_get_visited_list: FOUND list for %s",
filename);
msg_puts((char_u *)"\n");
verbose_leave_scroll();
}
#endif
return retptr;
}
retptr = retptr->ffvl_next;
}
}
#ifdef FF_VERBOSE
if (p_verbose >= 5)
{
verbose_enter_scroll();
smsg((char_u *)"ff_get_visited_list: new list for %s", filename);
msg_puts((char_u *)"\n");
verbose_leave_scroll();
}
#endif
retptr = (ff_visited_list_hdr_T*)alloc((unsigned)sizeof(*retptr));
if (retptr == NULL)
return NULL;
retptr->ffvl_visited_list = NULL;
retptr->ffvl_filename = vim_strsave(filename);
if (retptr->ffvl_filename == NULL)
{
vim_free(retptr);
return NULL;
}
retptr->ffvl_next = *list_headp;
*list_headp = retptr;
return retptr;
}
#ifdef FEAT_PATH_EXTRA
static int
ff_wc_equal(char_u *s1, char_u *s2)
{
int i, j;
int c1 = NUL;
int c2 = NUL;
int prev1 = NUL;
int prev2 = NUL;
if (s1 == s2)
return TRUE;
if (s1 == NULL || s2 == NULL)
return FALSE;
for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
{
c1 = PTR2CHAR(s1 + i);
c2 = PTR2CHAR(s2 + j);
if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
&& (prev1 != '*' || prev2 != '*'))
return FALSE;
prev2 = prev1;
prev1 = c1;
i += MB_PTR2LEN(s1 + i);
j += MB_PTR2LEN(s2 + j);
}
return s1[i] == s2[j];
}
#endif
static int
ff_check_visited(
ff_visited_T **visited_list,
char_u *fname
#ifdef FEAT_PATH_EXTRA
, char_u *wc_path
#endif
)
{
ff_visited_T *vp;
#ifdef UNIX
stat_T st;
int url = FALSE;
#endif
if (path_with_url(fname))
{
vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
#ifdef UNIX
url = TRUE;
#endif
}
else
{
ff_expand_buffer[0] = NUL;
#ifdef UNIX
if (mch_stat((char *)fname, &st) < 0)
#else
if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
#endif
return FAIL;
}
for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
{
if (
#ifdef UNIX
!url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
&& vp->ffv_ino == st.st_ino)
:
#endif
fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
)
{
#ifdef FEAT_PATH_EXTRA
if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
#endif
return FAIL;
}
}
vp = (ff_visited_T *)alloc((unsigned)(sizeof(ff_visited_T)
+ STRLEN(ff_expand_buffer)));
if (vp != NULL)
{
#ifdef UNIX
if (!url)
{
vp->ffv_dev_valid = TRUE;
vp->ffv_ino = st.st_ino;
vp->ffv_dev = st.st_dev;
vp->ffv_fname[0] = NUL;
}
else
{
vp->ffv_dev_valid = FALSE;
#endif
STRCPY(vp->ffv_fname, ff_expand_buffer);
#ifdef UNIX
}
#endif
#ifdef FEAT_PATH_EXTRA
if (wc_path != NULL)
vp->ffv_wc_path = vim_strsave(wc_path);
else
vp->ffv_wc_path = NULL;
#endif
vp->ffv_next = *visited_list;
*visited_list = vp;
}
return OK;
}
static ff_stack_T *
ff_create_stack_element(
char_u *fix_part,
#ifdef FEAT_PATH_EXTRA
char_u *wc_part,
#endif
int level,
int star_star_empty)
{
ff_stack_T *new;
new = (ff_stack_T *)alloc((unsigned)sizeof(ff_stack_T));
if (new == NULL)
return NULL;
new->ffs_prev = NULL;
new->ffs_filearray = NULL;
new->ffs_filearray_size = 0;
new->ffs_filearray_cur = 0;
new->ffs_stage = 0;
new->ffs_level = level;
new->ffs_star_star_empty = star_star_empty;
if (fix_part == NULL)
fix_part = (char_u *)"";
new->ffs_fix_path = vim_strsave(fix_part);
#ifdef FEAT_PATH_EXTRA
if (wc_part == NULL)
wc_part = (char_u *)"";
new->ffs_wc_path = vim_strsave(wc_part);
#endif
if (new->ffs_fix_path == NULL
#ifdef FEAT_PATH_EXTRA
|| new->ffs_wc_path == NULL
#endif
)
{
ff_free_stack_element(new);
new = NULL;
}
return new;
}
static void
ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
{
if (stack_ptr != NULL)
{
stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
search_ctx->ffsc_stack_ptr = stack_ptr;
}
}
static ff_stack_T *
ff_pop(ff_search_ctx_T *search_ctx)
{
ff_stack_T *sptr;
sptr = search_ctx->ffsc_stack_ptr;
if (search_ctx->ffsc_stack_ptr != NULL)
search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
return sptr;
}
static void
ff_free_stack_element(ff_stack_T *stack_ptr)
{
vim_free(stack_ptr->ffs_fix_path);
#ifdef FEAT_PATH_EXTRA
vim_free(stack_ptr->ffs_wc_path);
#endif
if (stack_ptr->ffs_filearray != NULL)
FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
vim_free(stack_ptr);
}
static void
ff_clear(ff_search_ctx_T *search_ctx)
{
ff_stack_T *sptr;
while ((sptr = ff_pop(search_ctx)) != NULL)
ff_free_stack_element(sptr);
vim_free(search_ctx->ffsc_file_to_search);
vim_free(search_ctx->ffsc_start_dir);
vim_free(search_ctx->ffsc_fix_path);
#ifdef FEAT_PATH_EXTRA
vim_free(search_ctx->ffsc_wc_path);
#endif
#ifdef FEAT_PATH_EXTRA
if (search_ctx->ffsc_stopdirs_v != NULL)
{
int i = 0;
while (search_ctx->ffsc_stopdirs_v[i] != NULL)
{
vim_free(search_ctx->ffsc_stopdirs_v[i]);
i++;
}
vim_free(search_ctx->ffsc_stopdirs_v);
}
search_ctx->ffsc_stopdirs_v = NULL;
#endif
search_ctx->ffsc_file_to_search = NULL;
search_ctx->ffsc_start_dir = NULL;
search_ctx->ffsc_fix_path = NULL;
#ifdef FEAT_PATH_EXTRA
search_ctx->ffsc_wc_path = NULL;
search_ctx->ffsc_level = 0;
#endif
}
#ifdef FEAT_PATH_EXTRA
static int
ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
{
int i = 0;
while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
path_len--;
if (path_len == 0)
return TRUE;
for (i = 0; stopdirs_v[i] != NULL; i++)
{
if ((int)STRLEN(stopdirs_v[i]) > path_len)
{
if (fnamencmp(stopdirs_v[i], path, path_len) == 0
&& vim_ispathsep(stopdirs_v[i][path_len]))
return TRUE;
}
else
{
if (fnamecmp(stopdirs_v[i], path) == 0)
return TRUE;
}
}
return FALSE;
}
#endif
#if defined(FEAT_SEARCHPATH) || defined(PROTO)
char_u *
find_file_in_path(
char_u *ptr,
int len,
int options,
int first,
char_u *rel_fname)
{
return find_file_in_path_option(ptr, len, options, first,
*curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
}
static char_u *ff_file_to_find = NULL;
static void *fdip_search_ctx = NULL;
#if defined(EXITFREE)
static void
free_findfile(void)
{
vim_free(ff_file_to_find);
vim_findfile_cleanup(fdip_search_ctx);
}
#endif
char_u *
find_directory_in_path(
char_u *ptr,
int len,
int options,
char_u *rel_fname)
{
return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
FINDFILE_DIR, rel_fname, (char_u *)"");
}
char_u *
find_file_in_path_option(
char_u *ptr,
int len,
int options,
int first,
char_u *path_option,
int find_what,
char_u *rel_fname,
char_u *suffixes)
{
static char_u *dir;
static int did_findfile_init = FALSE;
char_u save_char;
char_u *file_name = NULL;
char_u *buf = NULL;
int rel_to_curdir;
#ifdef AMIGA
struct Process *proc = (struct Process *)FindTask(0L);
APTR save_winptr = proc->pr_WindowPtr;
proc->pr_WindowPtr = (APTR)-1L;
#endif
if (first == TRUE)
{
save_char = ptr[len];
ptr[len] = NUL;
expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
ptr[len] = save_char;
vim_free(ff_file_to_find);
ff_file_to_find = vim_strsave(NameBuff);
if (ff_file_to_find == NULL)
{
file_name = NULL;
goto theend;
}
if (options & FNAME_UNESC)
{
for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
if (ptr[0] == '\\' && ptr[1] == ' ')
mch_memmove(ptr, ptr + 1, STRLEN(ptr));
}
}
rel_to_curdir = (ff_file_to_find[0] == '.'
&& (ff_file_to_find[1] == NUL
|| vim_ispathsep(ff_file_to_find[1])
|| (ff_file_to_find[1] == '.'
&& (ff_file_to_find[2] == NUL
|| vim_ispathsep(ff_file_to_find[2])))));
if (vim_isAbsName(ff_file_to_find)
|| rel_to_curdir
#if defined(MSWIN)
|| vim_ispathsep(ff_file_to_find[0])
|| (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
#endif
#ifdef AMIGA
|| ff_file_to_find[0] == ':'
#endif
)
{
if (first == TRUE)
{
int l;
int run;
if (path_with_url(ff_file_to_find))
{
file_name = vim_strsave(ff_file_to_find);
goto theend;
}
for (run = 1; run <= 2; ++run)
{
l = (int)STRLEN(ff_file_to_find);
if (run == 1
&& rel_to_curdir
&& (options & FNAME_REL)
&& rel_fname != NULL
&& STRLEN(rel_fname) + l < MAXPATHL)
{
STRCPY(NameBuff, rel_fname);
STRCPY(gettail(NameBuff), ff_file_to_find);
l = (int)STRLEN(NameBuff);
}
else
{
STRCPY(NameBuff, ff_file_to_find);
run = 2;
}
buf = suffixes;
for (;;)
{
if (mch_getperm(NameBuff) >= 0
&& (find_what == FINDFILE_BOTH
|| ((find_what == FINDFILE_DIR)
== mch_isdir(NameBuff))))
{
file_name = vim_strsave(NameBuff);
goto theend;
}
if (*buf == NUL)
break;
copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
}
}
}
}
else
{
if (first == TRUE)
{
vim_findfile_free_visited(fdip_search_ctx);
dir = path_option;
did_findfile_init = FALSE;
}
for (;;)
{
if (did_findfile_init)
{
file_name = vim_findfile(fdip_search_ctx);
if (file_name != NULL)
break;
did_findfile_init = FALSE;
}
else
{
char_u *r_ptr;
if (dir == NULL || *dir == NUL)
{
vim_findfile_cleanup(fdip_search_ctx);
fdip_search_ctx = NULL;
break;
}
if ((buf = alloc((int)(MAXPATHL))) == NULL)
break;
buf[0] = 0;
copy_option_part(&dir, buf, MAXPATHL, " ,");
#ifdef FEAT_PATH_EXTRA
r_ptr = vim_findfile_stopdir(buf);
#else
r_ptr = NULL;
#endif
fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
r_ptr, 100, FALSE, find_what,
fdip_search_ctx, FALSE, rel_fname);
if (fdip_search_ctx != NULL)
did_findfile_init = TRUE;
vim_free(buf);
}
}
}
if (file_name == NULL && (options & FNAME_MESS))
{
if (first == TRUE)
{
if (find_what == FINDFILE_DIR)
EMSG2(_("E344: Can't find directory \"%s\" in cdpath"),
ff_file_to_find);
else
EMSG2(_("E345: Can't find file \"%s\" in path"),
ff_file_to_find);
}
else
{
if (find_what == FINDFILE_DIR)
EMSG2(_("E346: No more directory \"%s\" found in cdpath"),
ff_file_to_find);
else
EMSG2(_("E347: No more file \"%s\" found in path"),
ff_file_to_find);
}
}
theend:
#ifdef AMIGA
proc->pr_WindowPtr = save_winptr;
#endif
return file_name;
}
#endif
int
vim_chdir(char_u *new_dir)
{
#ifndef FEAT_SEARCHPATH
return mch_chdir((char *)new_dir);
#else
char_u *dir_name;
int r;
dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
FNAME_MESS, curbuf->b_ffname);
if (dir_name == NULL)
return -1;
r = mch_chdir((char *)dir_name);
vim_free(dir_name);
return r;
#endif
}
int
get_user_name(char_u *buf, int len)
{
if (username == NULL)
{
if (mch_get_user_name(buf, len) == FAIL)
return FAIL;
username = vim_strsave(buf);
}
else
vim_strncpy(buf, username, len - 1);
return OK;
}
#ifndef HAVE_QSORT
void
qsort(
void *base,
size_t elm_count,
size_t elm_size,
int (*cmp)(const void *, const void *))
{
char_u *buf;
char_u *p1;
char_u *p2;
int i, j;
int gap;
buf = alloc((unsigned)elm_size);
if (buf == NULL)
return;
for (gap = elm_count / 2; gap > 0; gap /= 2)
for (i = gap; i < elm_count; ++i)
for (j = i - gap; j >= 0; j -= gap)
{
p1 = (char_u *)base + j * elm_size;
p2 = (char_u *)base + (j + gap) * elm_size;
if ((*cmp)((void *)p1, (void *)p2) <= 0)
break;
mch_memmove(buf, p1, elm_size);
mch_memmove(p1, p2, elm_size);
mch_memmove(p2, buf, elm_size);
}
vim_free(buf);
}
#endif
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
sort_compare(const void *s1, const void *s2);
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
sort_compare(const void *s1, const void *s2)
{
return STRCMP(*(char **)s1, *(char **)s2);
}
void
sort_strings(
char_u **files,
int count)
{
qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
}
#if !defined(NO_EXPANDPATH) || defined(PROTO)
int
pathcmp(const char *p, const char *q, int maxlen)
{
int i, j;
int c1, c2;
const char *s = NULL;
for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
{
c1 = PTR2CHAR((char_u *)p + i);
c2 = PTR2CHAR((char_u *)q + j);
if (c1 == NUL)
{
if (c2 == NUL)
return 0;
s = q;
i = j;
break;
}
if (c2 == NUL)
{
s = p;
break;
}
if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
#ifdef BACKSLASH_IN_FILENAME
&& !((c1 == '/' && c2 == '\\')
|| (c1 == '\\' && c2 == '/'))
#endif
)
{
if (vim_ispathsep(c1))
return -1;
if (vim_ispathsep(c2))
return 1;
return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
: c1 - c2;
}
i += MB_PTR2LEN((char_u *)p + i);
j += MB_PTR2LEN((char_u *)q + j);
}
if (s == NULL)
return 0;
c1 = PTR2CHAR((char_u *)s + i);
c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
if (c2 == NUL
&& i > 0
&& !after_pathsep((char_u *)s, (char_u *)s + i)
#ifdef BACKSLASH_IN_FILENAME
&& (c1 == '/' || c1 == '\\')
#else
&& c1 == '/'
#endif
)
return 0;
if (s == q)
return -1;
return 1;
}
#endif
#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
#define EXTRASIZE 5
static int envsize = -1;
extern char **environ;
static int findenv(char *name);
static int newenv(void);
static int moreenv(void);
int
putenv(const char *string)
{
int i;
char *p;
if (envsize < 0)
{
if (newenv() < 0)
return -1;
}
i = findenv((char *)string);
if (i < 0)
{
for (i = 0; environ[i]; i++);
if (i >= (envsize - 1))
{
if (moreenv() < 0)
return -1;
}
p = (char *)alloc((unsigned)(strlen(string) + 1));
if (p == NULL)
return -1;
environ[i + 1] = 0;
}
else
{
p = vim_realloc(environ[i], strlen(string) + 1);
if (p == NULL)
return -1;
}
sprintf(p, "%s", string);
environ[i] = p;
return 0;
}
static int
findenv(char *name)
{
char *namechar, *envchar;
int i, found;
found = 0;
for (i = 0; environ[i] && !found; i++)
{
envchar = environ[i];
namechar = name;
while (*namechar && *namechar != '=' && (*namechar == *envchar))
{
namechar++;
envchar++;
}
found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
}
return found ? i - 1 : -1;
}
static int
newenv(void)
{
char **env, *elem;
int i, esize;
for (i = 0; environ[i]; i++)
;
esize = i + EXTRASIZE + 1;
env = (char **)alloc((unsigned)(esize * sizeof (elem)));
if (env == NULL)
return -1;
for (i = 0; environ[i]; i++)
{
elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
if (elem == NULL)
return -1;
env[i] = elem;
strcpy(elem, environ[i]);
}
env[i] = 0;
environ = env;
envsize = esize;
return 0;
}
static int
moreenv(void)
{
int esize;
char **env;
esize = envsize + EXTRASIZE;
env = (char **)vim_realloc((char *)environ, esize * sizeof (*env));
if (env == 0)
return -1;
environ = env;
envsize = esize;
return 0;
}
# ifdef USE_VIMPTY_GETENV
char_u *
vimpty_getenv(const char_u *string)
{
int i;
char_u *p;
if (envsize < 0)
return NULL;
i = findenv((char *)string);
if (i < 0)
return NULL;
p = vim_strchr((char_u *)environ[i], '=');
return (p + 1);
}
# endif
#endif
#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
int
filewritable(char_u *fname)
{
int retval = 0;
#if defined(UNIX) || defined(VMS)
int perm = 0;
#endif
#if defined(UNIX) || defined(VMS)
perm = mch_getperm(fname);
#endif
if (
# ifdef WIN3264
mch_writable(fname) &&
# else
# if defined(UNIX) || defined(VMS)
(perm & 0222) &&
# endif
# endif
mch_access((char *)fname, W_OK) == 0
)
{
++retval;
if (mch_isdir(fname))
++retval;
}
return retval;
}
#endif
#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
int
get2c(FILE *fd)
{
int n;
n = getc(fd);
n = (n << 8) + getc(fd);
return n;
}
int
get3c(FILE *fd)
{
int n;
n = getc(fd);
n = (n << 8) + getc(fd);
n = (n << 8) + getc(fd);
return n;
}
int
get4c(FILE *fd)
{
unsigned n;
n = (unsigned)getc(fd);
n = (n << 8) + (unsigned)getc(fd);
n = (n << 8) + (unsigned)getc(fd);
n = (n << 8) + (unsigned)getc(fd);
return (int)n;
}
time_T
get8ctime(FILE *fd)
{
time_T n = 0;
int i;
for (i = 0; i < 8; ++i)
n = (n << 8) + getc(fd);
return n;
}
char_u *
read_string(FILE *fd, int cnt)
{
char_u *str;
int i;
int c;
str = alloc((unsigned)cnt + 1);
if (str != NULL)
{
for (i = 0; i < cnt; ++i)
{
c = getc(fd);
if (c == EOF)
{
vim_free(str);
return NULL;
}
str[i] = c;
}
str[i] = NUL;
}
return str;
}
int
put_bytes(FILE *fd, long_u nr, int len)
{
int i;
for (i = len - 1; i >= 0; --i)
if (putc((int)(nr >> (i * 8)), fd) == EOF)
return FAIL;
return OK;
}
#ifdef _MSC_VER
# if (_MSC_VER <= 1200)
# pragma optimize("", off)
# endif
#endif
int
put_time(FILE *fd, time_T the_time)
{
char_u buf[8];
time_to_bytes(the_time, buf);
return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL;
}
void
time_to_bytes(time_T the_time, char_u *buf)
{
int c;
int i;
int bi = 0;
time_T wtime = the_time;
for (i = 7; i >= 0; --i)
{
if (i + 1 > (int)sizeof(time_T))
buf[bi++] = 0;
else
{
#if defined(VIM_SIZEOF_TIME_T) && VIM_SIZEOF_TIME_T > 4
c = (int)(wtime >> (i * 8));
#else
c = (int)((long_u)wtime >> (i * 8));
#endif
buf[bi++] = c;
}
}
}
#ifdef _MSC_VER
# if (_MSC_VER <= 1200)
# pragma optimize("", on)
# endif
#endif
#endif
#if (defined(FEAT_MBYTE) && defined(FEAT_QUICKFIX)) \
|| defined(FEAT_SPELL) || defined(PROTO)
int
has_non_ascii(char_u *s)
{
char_u *p;
if (s != NULL)
for (p = s; *p != NUL; ++p)
if (*p >= 128)
return TRUE;
return FALSE;
}
#endif
#if defined(MESSAGE_QUEUE) || defined(PROTO)
void
parse_queued_messages(void)
{
win_T *old_curwin = curwin;
# if defined(WIN32) && defined(FEAT_JOB_CHANNEL)
channel_handle_events(FALSE);
# endif
# ifdef FEAT_NETBEANS_INTG
netbeans_parse_messages();
# endif
# ifdef FEAT_JOB_CHANNEL
channel_write_any_lines();
channel_parse_messages();
# endif
# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
server_parse_messages();
# endif
# ifdef FEAT_JOB_CHANNEL
job_check_ended();
# endif
if (curwin != old_curwin)
ins_char_typebuf(K_IGNORE);
}
#endif
#ifndef PROTO
# ifdef ELAPSED_TIMEVAL
long
elapsed(struct timeval *start_tv)
{
struct timeval now_tv;
gettimeofday(&now_tv, NULL);
return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
+ (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
}
# endif
# ifdef ELAPSED_TICKCOUNT
long
elapsed(DWORD start_tick)
{
DWORD now = GetTickCount();
return (long)now - (long)start_tick;
}
# endif
#endif