#include "vim.h"
#define MINIMAL_SIZE 20
static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
static int typeahead_char = 0;
static int block_redo = FALSE;
#define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING + TERMINAL)) ? (c1) : ((c1) ^ 0x80))
static mapblock_T *(maphash[256]);
static int maphash_valid = FALSE;
static mapblock_T *first_abbr = NULL;
static int KeyNoremap = 0;
#define RM_YES 0
#define RM_NONE 1
#define RM_SCRIPT 2
#define RM_ABBR 4
#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
static char_u typebuf_init[TYPELEN_INIT];
static char_u noremapbuf_init[TYPELEN_INIT];
static int last_recorded_len = 0;
static char_u *get_buffcont(buffheader_T *, int);
static void add_buff(buffheader_T *, char_u *, long n);
static void add_num_buff(buffheader_T *, long);
static void add_char_buff(buffheader_T *, int);
static int read_readbuffers(int advance);
static int read_readbuf(buffheader_T *buf, int advance);
static void start_stuff(void);
static int read_redo(int, int);
static void copy_redo(int);
static void init_typebuf(void);
static void gotchars(char_u *, int);
static void may_sync_undo(void);
static void closescript(void);
static int vgetorpeek(int);
static void map_free(mapblock_T **);
static void validate_maphash(void);
static void showmap(mapblock_T *mp, int local);
static int inchar(char_u *buf, int maxlen, long wait_time);
#ifdef FEAT_EVAL
static char_u *eval_map_expr(char_u *str, int c);
#endif
void
free_buff(buffheader_T *buf)
{
buffblock_T *p, *np;
for (p = buf->bh_first.b_next; p != NULL; p = np)
{
np = p->b_next;
vim_free(p);
}
buf->bh_first.b_next = NULL;
}
static char_u *
get_buffcont(
buffheader_T *buffer,
int dozero)
{
long_u count = 0;
char_u *p = NULL;
char_u *p2;
char_u *str;
buffblock_T *bp;
for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
count += (long_u)STRLEN(bp->b_str);
if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != NULL)
{
p2 = p;
for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
for (str = bp->b_str; *str; )
*p2++ = *str++;
*p2 = NUL;
}
return (p);
}
char_u *
get_recorded(void)
{
char_u *p;
size_t len;
p = get_buffcont(&recordbuff, TRUE);
free_buff(&recordbuff);
len = STRLEN(p);
if ((int)len >= last_recorded_len)
{
len -= last_recorded_len;
p[len] = NUL;
}
if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
p[len - 1] = NUL;
return (p);
}
char_u *
get_inserted(void)
{
return get_buffcont(&redobuff, FALSE);
}
static void
add_buff(
buffheader_T *buf,
char_u *s,
long slen)
{
buffblock_T *p;
long_u len;
if (slen < 0)
slen = (long)STRLEN(s);
if (slen == 0)
return;
if (buf->bh_first.b_next == NULL)
{
buf->bh_space = 0;
buf->bh_curr = &(buf->bh_first);
}
else if (buf->bh_curr == NULL)
{
IEMSG(_("E222: Add to read buffer"));
return;
}
else if (buf->bh_index != 0)
mch_memmove(buf->bh_first.b_next->b_str,
buf->bh_first.b_next->b_str + buf->bh_index,
STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
buf->bh_index = 0;
if (buf->bh_space >= (int)slen)
{
len = (long_u)STRLEN(buf->bh_curr->b_str);
vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
buf->bh_space -= slen;
}
else
{
if (slen < MINIMAL_SIZE)
len = MINIMAL_SIZE;
else
len = slen;
p = (buffblock_T *)lalloc((long_u)(sizeof(buffblock_T) + len),
TRUE);
if (p == NULL)
return;
buf->bh_space = (int)(len - slen);
vim_strncpy(p->b_str, s, (size_t)slen);
p->b_next = buf->bh_curr->b_next;
buf->bh_curr->b_next = p;
buf->bh_curr = p;
}
return;
}
static void
add_num_buff(buffheader_T *buf, long n)
{
char_u number[32];
sprintf((char *)number, "%ld", n);
add_buff(buf, number, -1L);
}
static void
add_char_buff(buffheader_T *buf, int c)
{
#ifdef FEAT_MBYTE
char_u bytes[MB_MAXBYTES + 1];
int len;
int i;
#endif
char_u temp[4];
#ifdef FEAT_MBYTE
if (IS_SPECIAL(c))
len = 1;
else
len = (*mb_char2bytes)(c, bytes);
for (i = 0; i < len; ++i)
{
if (!IS_SPECIAL(c))
c = bytes[i];
#endif
if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
{
temp[0] = K_SPECIAL;
temp[1] = K_SECOND(c);
temp[2] = K_THIRD(c);
temp[3] = NUL;
}
#ifdef FEAT_GUI
else if (c == CSI)
{
temp[0] = CSI;
temp[1] = KS_EXTRA;
temp[2] = (int)KE_CSI;
temp[3] = NUL;
}
#endif
else
{
temp[0] = c;
temp[1] = NUL;
}
add_buff(buf, temp, -1L);
#ifdef FEAT_MBYTE
}
#endif
}
static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
static int
read_readbuffers(int advance)
{
int c;
c = read_readbuf(&readbuf1, advance);
if (c == NUL)
c = read_readbuf(&readbuf2, advance);
return c;
}
static int
read_readbuf(buffheader_T *buf, int advance)
{
char_u c;
buffblock_T *curr;
if (buf->bh_first.b_next == NULL)
return NUL;
curr = buf->bh_first.b_next;
c = curr->b_str[buf->bh_index];
if (advance)
{
if (curr->b_str[++buf->bh_index] == NUL)
{
buf->bh_first.b_next = curr->b_next;
vim_free(curr);
buf->bh_index = 0;
}
}
return c;
}
static void
start_stuff(void)
{
if (readbuf1.bh_first.b_next != NULL)
{
readbuf1.bh_curr = &(readbuf1.bh_first);
readbuf1.bh_space = 0;
}
if (readbuf2.bh_first.b_next != NULL)
{
readbuf2.bh_curr = &(readbuf2.bh_first);
readbuf2.bh_space = 0;
}
}
int
stuff_empty(void)
{
return (readbuf1.bh_first.b_next == NULL
&& readbuf2.bh_first.b_next == NULL);
}
int
readbuf1_empty(void)
{
return (readbuf1.bh_first.b_next == NULL);
}
void
typeahead_noflush(int c)
{
typeahead_char = c;
}
void
flush_buffers(int flush_typeahead)
{
init_typebuf();
start_stuff();
while (read_readbuffers(TRUE) != NUL)
;
if (flush_typeahead)
{
while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
;
typebuf.tb_off = MAXMAPLEN;
typebuf.tb_len = 0;
#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
typebuf_was_filled = FALSE;
#endif
}
else
{
typebuf.tb_off += typebuf.tb_maplen;
typebuf.tb_len -= typebuf.tb_maplen;
}
typebuf.tb_maplen = 0;
typebuf.tb_silent = 0;
cmd_silent = FALSE;
typebuf.tb_no_abbr_cnt = 0;
}
void
ResetRedobuff(void)
{
if (!block_redo)
{
free_buff(&old_redobuff);
old_redobuff = redobuff;
redobuff.bh_first.b_next = NULL;
}
}
void
CancelRedo(void)
{
if (!block_redo)
{
free_buff(&redobuff);
redobuff = old_redobuff;
old_redobuff.bh_first.b_next = NULL;
start_stuff();
while (read_readbuffers(TRUE) != NUL)
;
}
}
#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
void
saveRedobuff(save_redo_T *save_redo)
{
char_u *s;
save_redo->sr_redobuff = redobuff;
redobuff.bh_first.b_next = NULL;
save_redo->sr_old_redobuff = old_redobuff;
old_redobuff.bh_first.b_next = NULL;
s = get_buffcont(&save_redo->sr_redobuff, FALSE);
if (s != NULL)
{
add_buff(&redobuff, s, -1L);
vim_free(s);
}
}
void
restoreRedobuff(save_redo_T *save_redo)
{
free_buff(&redobuff);
redobuff = save_redo->sr_redobuff;
free_buff(&old_redobuff);
old_redobuff = save_redo->sr_old_redobuff;
}
#endif
void
AppendToRedobuff(char_u *s)
{
if (!block_redo)
add_buff(&redobuff, s, -1L);
}
void
AppendToRedobuffLit(
char_u *str,
int len)
{
char_u *s = str;
int c;
char_u *start;
if (block_redo)
return;
while (len < 0 ? *s != NUL : s - str < len)
{
start = s;
while (*s >= ' '
#ifndef EBCDIC
&& *s < DEL
#endif
&& (len < 0 || s - str < len))
++s;
if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
--s;
if (s > start)
add_buff(&redobuff, start, (long)(s - start));
if (*s == NUL || (len >= 0 && s - str >= len))
break;
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_cptr2char_adv(&s);
else
#endif
c = *s++;
if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
add_char_buff(&redobuff, Ctrl_V);
if (*s == NUL && c == '0')
#ifdef EBCDIC
add_buff(&redobuff, (char_u *)"xf0", 3L);
#else
add_buff(&redobuff, (char_u *)"048", 3L);
#endif
else
add_char_buff(&redobuff, c);
}
}
void
AppendCharToRedobuff(int c)
{
if (!block_redo)
add_char_buff(&redobuff, c);
}
void
AppendNumberToRedobuff(long n)
{
if (!block_redo)
add_num_buff(&redobuff, n);
}
void
stuffReadbuff(char_u *s)
{
add_buff(&readbuf1, s, -1L);
}
void
stuffRedoReadbuff(char_u *s)
{
add_buff(&readbuf2, s, -1L);
}
void
stuffReadbuffLen(char_u *s, long len)
{
add_buff(&readbuf1, s, len);
}
#if defined(FEAT_EVAL) || defined(PROTO)
void
stuffReadbuffSpec(char_u *s)
{
int c;
while (*s != NUL)
{
if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
{
stuffReadbuffLen(s, 3L);
s += 3;
}
else
{
#ifdef FEAT_MBYTE
c = mb_ptr2char_adv(&s);
#else
c = *s++;
#endif
if (c == CAR || c == NL || c == ESC)
c = ' ';
stuffcharReadbuff(c);
}
}
}
#endif
void
stuffcharReadbuff(int c)
{
add_char_buff(&readbuf1, c);
}
void
stuffnumReadbuff(long n)
{
add_num_buff(&readbuf1, n);
}
static int
read_redo(int init, int old_redo)
{
static buffblock_T *bp;
static char_u *p;
int c;
#ifdef FEAT_MBYTE
int n;
char_u buf[MB_MAXBYTES + 1];
int i;
#endif
if (init)
{
if (old_redo)
bp = old_redobuff.bh_first.b_next;
else
bp = redobuff.bh_first.b_next;
if (bp == NULL)
return FAIL;
p = bp->b_str;
return OK;
}
if ((c = *p) != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
n = MB_BYTE2LEN_CHECK(c);
else
n = 1;
for (i = 0; ; ++i)
#endif
{
if (c == K_SPECIAL)
{
c = TO_SPECIAL(p[1], p[2]);
p += 2;
}
#ifdef FEAT_GUI
if (c == CSI)
p += 2;
#endif
if (*++p == NUL && bp->b_next != NULL)
{
bp = bp->b_next;
p = bp->b_str;
}
#ifdef FEAT_MBYTE
buf[i] = c;
if (i == n - 1)
{
if (n != 1)
c = (*mb_ptr2char)(buf);
break;
}
c = *p;
if (c == NUL)
break;
#endif
}
}
return c;
}
static void
copy_redo(int old_redo)
{
int c;
while ((c = read_redo(FALSE, old_redo)) != NUL)
add_char_buff(&readbuf2, c);
}
int
start_redo(long count, int old_redo)
{
int c;
if (read_redo(TRUE, old_redo) == FAIL)
return FAIL;
c = read_redo(FALSE, old_redo);
if (c == '"')
{
add_buff(&readbuf2, (char_u *)"\"", 1L);
c = read_redo(FALSE, old_redo);
if (c >= '1' && c < '9')
++c;
add_char_buff(&readbuf2, c);
c = read_redo(FALSE, old_redo);
}
if (c == 'v')
{
VIsual = curwin->w_cursor;
VIsual_active = TRUE;
VIsual_select = FALSE;
VIsual_reselect = TRUE;
redo_VIsual_busy = TRUE;
c = read_redo(FALSE, old_redo);
}
if (count)
{
while (VIM_ISDIGIT(c))
c = read_redo(FALSE, old_redo);
add_num_buff(&readbuf2, count);
}
add_char_buff(&readbuf2, c);
copy_redo(old_redo);
return OK;
}
int
start_redo_ins(void)
{
int c;
if (read_redo(TRUE, FALSE) == FAIL)
return FAIL;
start_stuff();
while ((c = read_redo(FALSE, FALSE)) != NUL)
{
if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
{
if (c == 'O' || c == 'o')
add_buff(&readbuf2, NL_STR, -1L);
break;
}
}
copy_redo(FALSE);
block_redo = TRUE;
return OK;
}
void
stop_redo_ins(void)
{
block_redo = FALSE;
}
static void
init_typebuf(void)
{
if (typebuf.tb_buf == NULL)
{
typebuf.tb_buf = typebuf_init;
typebuf.tb_noremap = noremapbuf_init;
typebuf.tb_buflen = TYPELEN_INIT;
typebuf.tb_len = 0;
typebuf.tb_off = MAXMAPLEN + 4;
typebuf.tb_change_cnt = 1;
}
}
int
ins_typebuf(
char_u *str,
int noremap,
int offset,
int nottyped,
int silent)
{
char_u *s1, *s2;
int newlen;
int addlen;
int i;
int newoff;
int val;
int nrm;
init_typebuf();
if (++typebuf.tb_change_cnt == 0)
typebuf.tb_change_cnt = 1;
addlen = (int)STRLEN(str);
if (offset == 0 && addlen <= typebuf.tb_off)
{
typebuf.tb_off -= addlen;
mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
}
else if (typebuf.tb_len == 0 && typebuf.tb_buflen
>= addlen + 3 * (MAXMAPLEN + 4))
{
typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
}
else
{
newoff = MAXMAPLEN + 4;
newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
if (newlen < 0)
{
EMSG(_(e_toocompl));
setcursor();
return FAIL;
}
s1 = alloc(newlen);
if (s1 == NULL)
return FAIL;
s2 = alloc(newlen);
if (s2 == NULL)
{
vim_free(s1);
return FAIL;
}
typebuf.tb_buflen = newlen;
mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
(size_t)offset);
mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
mch_memmove(s1 + newoff + offset + addlen,
typebuf.tb_buf + typebuf.tb_off + offset,
(size_t)(typebuf.tb_len - offset + 1));
if (typebuf.tb_buf != typebuf_init)
vim_free(typebuf.tb_buf);
typebuf.tb_buf = s1;
mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
(size_t)offset);
mch_memmove(s2 + newoff + offset + addlen,
typebuf.tb_noremap + typebuf.tb_off + offset,
(size_t)(typebuf.tb_len - offset));
if (typebuf.tb_noremap != noremapbuf_init)
vim_free(typebuf.tb_noremap);
typebuf.tb_noremap = s2;
typebuf.tb_off = newoff;
}
typebuf.tb_len += addlen;
if (noremap == REMAP_SCRIPT)
val = RM_SCRIPT;
else if (noremap == REMAP_SKIP)
val = RM_ABBR;
else
val = RM_NONE;
if (noremap == REMAP_SKIP)
nrm = 1;
else if (noremap < 0)
nrm = addlen;
else
nrm = noremap;
for (i = 0; i < addlen; ++i)
typebuf.tb_noremap[typebuf.tb_off + i + offset] =
(--nrm >= 0) ? val : RM_YES;
if (nottyped || typebuf.tb_maplen > offset)
typebuf.tb_maplen += addlen;
if (silent || typebuf.tb_silent > offset)
{
typebuf.tb_silent += addlen;
cmd_silent = TRUE;
}
if (typebuf.tb_no_abbr_cnt && offset == 0)
typebuf.tb_no_abbr_cnt += addlen;
return OK;
}
void
ins_char_typebuf(int c)
{
#ifdef FEAT_MBYTE
char_u buf[MB_MAXBYTES + 1];
#else
char_u buf[4];
#endif
if (IS_SPECIAL(c))
{
buf[0] = K_SPECIAL;
buf[1] = K_SECOND(c);
buf[2] = K_THIRD(c);
buf[3] = NUL;
}
else
{
#ifdef FEAT_MBYTE
buf[(*mb_char2bytes)(c, buf)] = NUL;
#else
buf[0] = c;
buf[1] = NUL;
#endif
}
(void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
}
int
typebuf_changed(
int tb_change_cnt)
{
return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
|| typebuf_was_filled
#endif
));
}
int
typebuf_typed(void)
{
return typebuf.tb_maplen == 0;
}
int
typebuf_maplen(void)
{
return typebuf.tb_maplen;
}
void
del_typebuf(int len, int offset)
{
int i;
if (len == 0)
return;
typebuf.tb_len -= len;
if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
>= 3 * MAXMAPLEN + 3)
typebuf.tb_off += len;
else
{
i = typebuf.tb_off + offset;
if (typebuf.tb_off > MAXMAPLEN)
{
mch_memmove(typebuf.tb_buf + MAXMAPLEN,
typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
typebuf.tb_off = MAXMAPLEN;
}
mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
typebuf.tb_buf + i + len,
(size_t)(typebuf.tb_len - offset + 1));
mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
typebuf.tb_noremap + i + len,
(size_t)(typebuf.tb_len - offset));
}
if (typebuf.tb_maplen > offset)
{
if (typebuf.tb_maplen < offset + len)
typebuf.tb_maplen = offset;
else
typebuf.tb_maplen -= len;
}
if (typebuf.tb_silent > offset)
{
if (typebuf.tb_silent < offset + len)
typebuf.tb_silent = offset;
else
typebuf.tb_silent -= len;
}
if (typebuf.tb_no_abbr_cnt > offset)
{
if (typebuf.tb_no_abbr_cnt < offset + len)
typebuf.tb_no_abbr_cnt = offset;
else
typebuf.tb_no_abbr_cnt -= len;
}
#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
typebuf_was_filled = FALSE;
#endif
if (++typebuf.tb_change_cnt == 0)
typebuf.tb_change_cnt = 1;
}
static void
gotchars(char_u *chars, int len)
{
char_u *s = chars;
int c;
char_u buf[2];
int todo = len;
if (Recording)
last_recorded_len += len;
buf[1] = NUL;
while (todo--)
{
c = *s++;
updatescript(c);
if (Recording)
{
buf[0] = c;
add_buff(&recordbuff, buf, 1L);
}
}
may_sync_undo();
#ifdef FEAT_EVAL
debug_did_msg = FALSE;
#endif
++maptick;
}
static void
may_sync_undo(void)
{
if ((!(State & (INSERT + CMDLINE)) || arrow_used)
&& scriptin[curscript] == NULL)
u_sync(FALSE);
}
int
alloc_typebuf(void)
{
typebuf.tb_buf = alloc(TYPELEN_INIT);
typebuf.tb_noremap = alloc(TYPELEN_INIT);
if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
{
free_typebuf();
return FAIL;
}
typebuf.tb_buflen = TYPELEN_INIT;
typebuf.tb_off = MAXMAPLEN + 4;
typebuf.tb_len = 0;
typebuf.tb_maplen = 0;
typebuf.tb_silent = 0;
typebuf.tb_no_abbr_cnt = 0;
if (++typebuf.tb_change_cnt == 0)
typebuf.tb_change_cnt = 1;
return OK;
}
void
free_typebuf(void)
{
if (typebuf.tb_buf == typebuf_init)
internal_error("Free typebuf 1");
else
vim_free(typebuf.tb_buf);
if (typebuf.tb_noremap == noremapbuf_init)
internal_error("Free typebuf 2");
else
vim_free(typebuf.tb_noremap);
}
static typebuf_T saved_typebuf[NSCRIPT];
int
save_typebuf(void)
{
init_typebuf();
saved_typebuf[curscript] = typebuf;
if (alloc_typebuf() == FAIL)
{
closescript();
return FAIL;
}
return OK;
}
static int old_char = -1;
static int old_mod_mask;
#ifdef FEAT_MOUSE
static int old_mouse_row;
static int old_mouse_col;
#endif
void
save_typeahead(tasave_T *tp)
{
tp->save_typebuf = typebuf;
tp->typebuf_valid = (alloc_typebuf() == OK);
if (!tp->typebuf_valid)
typebuf = tp->save_typebuf;
tp->old_char = old_char;
tp->old_mod_mask = old_mod_mask;
old_char = -1;
tp->save_readbuf1 = readbuf1;
readbuf1.bh_first.b_next = NULL;
tp->save_readbuf2 = readbuf2;
readbuf2.bh_first.b_next = NULL;
# ifdef USE_INPUT_BUF
tp->save_inputbuf = get_input_buf();
# endif
}
void
restore_typeahead(tasave_T *tp)
{
if (tp->typebuf_valid)
{
free_typebuf();
typebuf = tp->save_typebuf;
}
old_char = tp->old_char;
old_mod_mask = tp->old_mod_mask;
free_buff(&readbuf1);
readbuf1 = tp->save_readbuf1;
free_buff(&readbuf2);
readbuf2 = tp->save_readbuf2;
# ifdef USE_INPUT_BUF
set_input_buf(tp->save_inputbuf);
# endif
}
void
openscript(
char_u *name,
int directly)
{
if (curscript + 1 == NSCRIPT)
{
EMSG(_(e_nesting));
return;
}
#ifdef FEAT_EVAL
if (ignore_script)
return;
#endif
if (scriptin[curscript] != NULL)
++curscript;
expand_env(name, NameBuff, MAXPATHL);
if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
{
EMSG2(_(e_notopen), name);
if (curscript)
--curscript;
return;
}
if (save_typebuf() == FAIL)
return;
if (directly)
{
oparg_T oa;
int oldcurscript;
int save_State = State;
int save_restart_edit = restart_edit;
int save_insertmode = p_im;
int save_finish_op = finish_op;
int save_msg_scroll = msg_scroll;
State = NORMAL;
msg_scroll = FALSE;
restart_edit = 0;
p_im = FALSE;
clear_oparg(&oa);
finish_op = FALSE;
oldcurscript = curscript;
do
{
update_topline_cursor();
normal_cmd(&oa, FALSE);
vpeekc();
}
while (scriptin[oldcurscript] != NULL);
State = save_State;
msg_scroll = save_msg_scroll;
restart_edit = save_restart_edit;
p_im = save_insertmode;
finish_op = save_finish_op;
}
}
static void
closescript(void)
{
free_typebuf();
typebuf = saved_typebuf[curscript];
fclose(scriptin[curscript]);
scriptin[curscript] = NULL;
if (curscript > 0)
--curscript;
}
#if defined(EXITFREE) || defined(PROTO)
void
close_all_scripts(void)
{
while (scriptin[0] != NULL)
closescript();
}
#endif
#if defined(FEAT_INS_EXPAND) || defined(PROTO)
int
using_script(void)
{
return scriptin[curscript] != NULL;
}
#endif
void
before_blocking(void)
{
updatescript(0);
#ifdef FEAT_EVAL
if (may_garbage_collect)
garbage_collect(FALSE);
#endif
}
void
updatescript(int c)
{
static int count = 0;
if (c && scriptout)
putc(c, scriptout);
if (c == 0 || (p_uc > 0 && ++count >= p_uc))
{
ml_sync_all(c == 0, TRUE);
count = 0;
}
}
int
vgetc(void)
{
int c, c2;
#ifdef FEAT_MBYTE
int n;
char_u buf[MB_MAXBYTES + 1];
int i;
#endif
#ifdef FEAT_EVAL
if (may_garbage_collect && want_garbage_collect)
garbage_collect(FALSE);
#endif
if (old_char != -1)
{
c = old_char;
old_char = -1;
mod_mask = old_mod_mask;
#ifdef FEAT_MOUSE
mouse_row = old_mouse_row;
mouse_col = old_mouse_col;
#endif
}
else
{
mod_mask = 0x0;
last_recorded_len = 0;
for (;;)
{
int did_inc = FALSE;
if (mod_mask
#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
|| im_is_preediting()
#endif
)
{
++no_mapping;
++allow_keys;
did_inc = TRUE;
}
c = vgetorpeek(TRUE);
if (did_inc)
{
--no_mapping;
--allow_keys;
}
if (c == K_SPECIAL
#ifdef FEAT_GUI
|| c == CSI
#endif
)
{
int save_allow_keys = allow_keys;
++no_mapping;
allow_keys = 0;
c2 = vgetorpeek(TRUE);
c = vgetorpeek(TRUE);
--no_mapping;
allow_keys = save_allow_keys;
if (c2 == KS_MODIFIER)
{
mod_mask = c;
continue;
}
c = TO_SPECIAL(c2, c);
#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
if (c == K_TEAROFF)
{
char_u name[200];
int i;
for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
{
name[i] = c;
if (i < 199)
++i;
}
name[i] = NUL;
gui_make_tearoff(name);
continue;
}
#endif
#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
if (c == K_F10 && gui.menubar != NULL)
{
gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE);
continue;
}
#endif
#ifdef FEAT_GUI
if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
{
ui_focus_change(c == K_FOCUSGAINED);
c = K_IGNORE;
}
if (c == K_CSI)
c = CSI;
#endif
}
switch (c)
{
case K_KPLUS: c = '+'; break;
case K_KMINUS: c = '-'; break;
case K_KDIVIDE: c = '/'; break;
case K_KMULTIPLY: c = '*'; break;
case K_KENTER: c = CAR; break;
case K_KPOINT:
#ifdef WIN32
c = MapVirtualKey(VK_DECIMAL, 2); break;
#else
c = '.'; break;
#endif
case K_K0: c = '0'; break;
case K_K1: c = '1'; break;
case K_K2: c = '2'; break;
case K_K3: c = '3'; break;
case K_K4: c = '4'; break;
case K_K5: c = '5'; break;
case K_K6: c = '6'; break;
case K_K7: c = '7'; break;
case K_K8: c = '8'; break;
case K_K9: c = '9'; break;
case K_XHOME:
case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
{
c = K_S_HOME;
mod_mask = 0;
}
else if (mod_mask == MOD_MASK_CTRL)
{
c = K_C_HOME;
mod_mask = 0;
}
else
c = K_HOME;
break;
case K_XEND:
case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
{
c = K_S_END;
mod_mask = 0;
}
else if (mod_mask == MOD_MASK_CTRL)
{
c = K_C_END;
mod_mask = 0;
}
else
c = K_END;
break;
case K_XUP: c = K_UP; break;
case K_XDOWN: c = K_DOWN; break;
case K_XLEFT: c = K_LEFT; break;
case K_XRIGHT: c = K_RIGHT; break;
}
#ifdef FEAT_MBYTE
if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
{
++no_mapping;
buf[0] = c;
for (i = 1; i < n; ++i)
{
buf[i] = vgetorpeek(TRUE);
if (buf[i] == K_SPECIAL
#ifdef FEAT_GUI
|| buf[i] == CSI
#endif
)
{
c = vgetorpeek(TRUE);
if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
buf[i] = CSI;
}
}
--no_mapping;
c = (*mb_ptr2char)(buf);
}
#endif
break;
}
}
#ifdef FEAT_EVAL
may_garbage_collect = FALSE;
#endif
return c;
}
int
safe_vgetc(void)
{
int c;
c = vgetc();
if (c == NUL)
c = get_keystroke();
return c;
}
int
plain_vgetc(void)
{
int c;
do
{
c = safe_vgetc();
} while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
if (c == K_PS)
c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
return c;
}
int
vpeekc(void)
{
if (old_char != -1)
return old_char;
return vgetorpeek(FALSE);
}
#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
int
vpeekc_nomap(void)
{
int c;
++no_mapping;
++allow_keys;
c = vpeekc();
--no_mapping;
--allow_keys;
return c;
}
#endif
#if defined(FEAT_INS_EXPAND) || defined(FEAT_EVAL) || defined(PROTO)
int
vpeekc_any(void)
{
int c;
c = vpeekc();
if (c == NUL && typebuf.tb_len > 0)
c = ESC;
return c;
}
#endif
int
char_avail(void)
{
int retval;
#ifdef FEAT_EVAL
if (disable_char_avail_for_testing)
return FALSE;
#endif
++no_mapping;
retval = vpeekc();
--no_mapping;
return (retval != NUL);
}
void
vungetc(int c)
{
old_char = c;
old_mod_mask = mod_mask;
#ifdef FEAT_MOUSE
old_mouse_row = mouse_row;
old_mouse_col = mouse_col;
#endif
}
static int
vgetorpeek(int advance)
{
int c, c1;
int keylen;
char_u *s;
mapblock_T *mp;
#ifdef FEAT_LOCALMAP
mapblock_T *mp2;
#endif
mapblock_T *mp_match;
int mp_match_len = 0;
int timedout = FALSE;
int mapdepth = 0;
int mode_deleted = FALSE;
int local_State;
int mlen;
int max_mlen;
int i;
#ifdef FEAT_CMDL_INFO
int new_wcol, new_wrow;
#endif
#ifdef FEAT_GUI
# ifdef FEAT_MENU
int idx;
# endif
int shape_changed = FALSE;
#endif
int n;
#ifdef FEAT_LANGMAP
int nolmaplen;
#endif
int old_wcol, old_wrow;
int wait_tb_len;
if (vgetc_busy > 0 && ex_normal_busy == 0)
return NUL;
local_State = get_real_state();
++vgetc_busy;
if (advance)
KeyStuffed = FALSE;
init_typebuf();
start_stuff();
if (advance && typebuf.tb_maplen == 0)
Exec_reg = FALSE;
do
{
if (typeahead_char != 0)
{
c = typeahead_char;
if (advance)
typeahead_char = 0;
}
else
c = read_readbuffers(advance);
if (c != NUL && !got_int)
{
if (advance)
{
KeyStuffed = TRUE;
}
if (typebuf.tb_no_abbr_cnt == 0)
typebuf.tb_no_abbr_cnt = 1;
}
else
{
for (;;)
{
if (typebuf.tb_maplen)
line_breakcheck();
else
ui_breakcheck();
keylen = 0;
if (got_int)
{
c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
if ((c || typebuf.tb_maplen)
&& (State & (INSERT + CMDLINE)))
c = ESC;
else
c = Ctrl_C;
flush_buffers(TRUE);
if (advance)
{
*typebuf.tb_buf = c;
gotchars(typebuf.tb_buf, 1);
}
cmd_silent = FALSE;
break;
}
else if (typebuf.tb_len > 0)
{
mp = NULL;
max_mlen = 0;
c1 = typebuf.tb_buf[typebuf.tb_off];
if (no_mapping == 0 && maphash_valid
&& (no_zero_mapping == 0 || c1 != '0')
&& (typebuf.tb_maplen == 0
|| (p_remap
&& (typebuf.tb_noremap[typebuf.tb_off]
& (RM_NONE|RM_ABBR)) == 0))
&& !(p_paste && (State & (INSERT + CMDLINE)))
&& !(State == HITRETURN && (c1 == CAR || c1 == ' '))
&& State != ASKMORE
&& State != CONFIRM
#ifdef FEAT_INS_EXPAND
&& !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1))
|| ((compl_cont_status & CONT_LOCAL)
&& (c1 == Ctrl_N || c1 == Ctrl_P)))
#endif
)
{
#ifdef FEAT_LANGMAP
if (c1 == K_SPECIAL)
nolmaplen = 2;
else
{
LANGMAP_ADJUST(c1,
(State & (CMDLINE | INSERT)) == 0
&& get_real_state() != SELECTMODE);
nolmaplen = 0;
}
#endif
#ifdef FEAT_LOCALMAP
mp = curbuf->b_maphash[MAP_HASH(local_State, c1)];
mp2 = maphash[MAP_HASH(local_State, c1)];
if (mp == NULL)
{
mp = mp2;
mp2 = NULL;
}
#else
mp = maphash[MAP_HASH(local_State, c1)];
#endif
mp_match = NULL;
mp_match_len = 0;
for ( ; mp != NULL;
#ifdef FEAT_LOCALMAP
mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
#endif
(mp = mp->m_next))
{
if (mp->m_keys[0] == c1
&& (mp->m_mode & local_State)
&& ((mp->m_mode & LANGMAP) == 0
|| typebuf.tb_maplen == 0))
{
#ifdef FEAT_LANGMAP
int nomap = nolmaplen;
int c2;
#endif
for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
{
#ifdef FEAT_LANGMAP
c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
if (nomap > 0)
--nomap;
else if (c2 == K_SPECIAL)
nomap = 2;
else
LANGMAP_ADJUST(c2, TRUE);
if (mp->m_keys[mlen] != c2)
#else
if (mp->m_keys[mlen] !=
typebuf.tb_buf[typebuf.tb_off + mlen])
#endif
break;
}
#ifdef FEAT_MBYTE
{
char_u *p1 = mp->m_keys;
char_u *p2 = mb_unescape(&p1);
if (has_mbyte && p2 != NULL
&& MB_BYTE2LEN(c1) > MB_PTR2LEN(p2))
mlen = 0;
}
#endif
keylen = mp->m_keylen;
if (mlen == keylen
|| (mlen == typebuf.tb_len
&& typebuf.tb_len < keylen))
{
s = typebuf.tb_noremap + typebuf.tb_off;
if (*s == RM_SCRIPT
&& (mp->m_keys[0] != K_SPECIAL
|| mp->m_keys[1] != KS_EXTRA
|| mp->m_keys[2]
!= (int)KE_SNR))
continue;
for (n = mlen; --n >= 0; )
if (*s++ & (RM_NONE|RM_ABBR))
break;
if (n >= 0)
continue;
if (keylen > typebuf.tb_len)
{
if (!timedout && !(mp_match != NULL
&& mp_match->m_nowait))
{
keylen = KEYLEN_PART_MAP;
break;
}
}
else if (keylen > mp_match_len)
{
mp_match = mp;
mp_match_len = keylen;
}
}
else
if (max_mlen < mlen)
max_mlen = mlen;
}
}
if (keylen != KEYLEN_PART_MAP)
{
mp = mp_match;
keylen = mp_match_len;
}
}
if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
{
for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
++mlen)
if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
+ mlen])
break;
if (p_pt[mlen] == NUL)
{
if (mlen > typebuf.tb_maplen)
gotchars(typebuf.tb_buf + typebuf.tb_off
+ typebuf.tb_maplen,
mlen - typebuf.tb_maplen);
del_typebuf(mlen, 0);
set_option_value((char_u *)"paste",
(long)!p_paste, NULL, 0);
if (!(State & INSERT))
{
msg_col = 0;
msg_row = Rows - 1;
msg_clr_eos();
}
status_redraw_all();
redraw_statuslines();
showmode();
setcursor();
continue;
}
if (mlen == typebuf.tb_len)
keylen = KEYLEN_PART_KEY;
else if (max_mlen < mlen)
max_mlen = mlen + 1;
}
if ((mp == NULL || max_mlen >= mp_match_len)
&& keylen != KEYLEN_PART_MAP)
{
int save_keylen = keylen;
if ((no_mapping == 0 || allow_keys != 0)
&& (typebuf.tb_maplen == 0
|| (p_remap && typebuf.tb_noremap[
typebuf.tb_off] == RM_YES))
&& !timedout)
{
keylen = check_termcode(max_mlen + 1,
NULL, 0, NULL);
if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
keylen = KEYLEN_PART_KEY;
if (keylen < 0
&& typebuf.tb_len == typebuf.tb_maplen)
keylen = 0;
}
else
keylen = 0;
if (keylen == 0)
{
#ifdef AMIGA
if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
typebuf.tb_off] & 0xff) == CSI)
{
for (s = typebuf.tb_buf + typebuf.tb_off + 1;
s < typebuf.tb_buf + typebuf.tb_off
+ typebuf.tb_len
&& (VIM_ISDIGIT(*s) || *s == ';'
|| *s == ' ');
++s)
;
if (*s == 'r' || *s == '|')
{
del_typebuf((int)(s + 1 -
(typebuf.tb_buf + typebuf.tb_off)), 0);
shell_resized();
continue;
}
if (*s == NUL)
keylen = KEYLEN_PART_KEY;
}
if (keylen >= 0)
#endif
if (mp == NULL)
{
c = typebuf.tb_buf[typebuf.tb_off] & 255;
if (advance)
{
cmd_silent = (typebuf.tb_silent > 0);
if (typebuf.tb_maplen > 0)
KeyTyped = FALSE;
else
{
KeyTyped = TRUE;
gotchars(typebuf.tb_buf
+ typebuf.tb_off, 1);
}
KeyNoremap = typebuf.tb_noremap[
typebuf.tb_off];
del_typebuf(1, 0);
}
break;
}
}
if (keylen > 0)
{
#if defined(FEAT_GUI) && defined(FEAT_MENU)
if (typebuf.tb_len >= 2
&& typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
&& typebuf.tb_buf[typebuf.tb_off + 1]
== KS_MENU)
{
may_sync_undo();
del_typebuf(3, 0);
idx = get_menu_index(current_menu, local_State);
if (idx != MENU_INDEX_INVALID)
{
if (VIsual_active && VIsual_select
&& (current_menu->modes & VISUAL))
{
VIsual_select = FALSE;
(void)ins_typebuf(K_SELECT_STRING,
REMAP_NONE, 0, TRUE, FALSE);
}
ins_typebuf(current_menu->strings[idx],
current_menu->noremap[idx],
0, TRUE,
current_menu->silent[idx]);
}
}
#endif
continue;
}
if (mp == NULL || keylen < 0)
keylen = KEYLEN_PART_KEY;
else
keylen = mp_match_len;
}
if (keylen >= 0 && keylen <= typebuf.tb_len)
{
#ifdef FEAT_EVAL
int save_m_expr;
int save_m_noremap;
int save_m_silent;
char_u *save_m_keys;
char_u *save_m_str;
#else
# define save_m_noremap mp->m_noremap
# define save_m_silent mp->m_silent
#endif
if (keylen > typebuf.tb_maplen)
gotchars(typebuf.tb_buf + typebuf.tb_off
+ typebuf.tb_maplen,
keylen - typebuf.tb_maplen);
cmd_silent = (typebuf.tb_silent > 0);
del_typebuf(keylen, 0);
if (++mapdepth >= p_mmd)
{
EMSG(_("E223: recursive mapping"));
if (State & CMDLINE)
redrawcmdline();
else
setcursor();
flush_buffers(FALSE);
mapdepth = 0;
c = -1;
break;
}
if (VIsual_active && VIsual_select
&& (mp->m_mode & VISUAL))
{
VIsual_select = FALSE;
(void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
0, TRUE, FALSE);
}
#ifdef FEAT_EVAL
save_m_expr = mp->m_expr;
save_m_noremap = mp->m_noremap;
save_m_silent = mp->m_silent;
save_m_keys = NULL;
save_m_str = NULL;
if (mp->m_expr)
{
int save_vgetc_busy = vgetc_busy;
vgetc_busy = 0;
save_m_keys = vim_strsave(mp->m_keys);
save_m_str = vim_strsave(mp->m_str);
s = eval_map_expr(save_m_str, NUL);
vgetc_busy = save_vgetc_busy;
}
else
#endif
s = mp->m_str;
if (s == NULL)
i = FAIL;
else
{
int noremap;
if (save_m_noremap != REMAP_YES)
noremap = save_m_noremap;
else if (
#ifdef FEAT_EVAL
STRNCMP(s, save_m_keys != NULL
? save_m_keys : mp->m_keys,
(size_t)keylen)
#else
STRNCMP(s, mp->m_keys, (size_t)keylen)
#endif
!= 0)
noremap = REMAP_YES;
else
noremap = REMAP_SKIP;
i = ins_typebuf(s, noremap,
0, TRUE, cmd_silent || save_m_silent);
#ifdef FEAT_EVAL
if (save_m_expr)
vim_free(s);
#endif
}
#ifdef FEAT_EVAL
vim_free(save_m_keys);
vim_free(save_m_str);
#endif
if (i == FAIL)
{
c = -1;
break;
}
continue;
}
}
c = 0;
#ifdef FEAT_CMDL_INFO
new_wcol = curwin->w_wcol;
new_wrow = curwin->w_wrow;
#endif
if ( advance
&& typebuf.tb_len == 1
&& typebuf.tb_buf[typebuf.tb_off] == ESC
&& !no_mapping
&& ex_normal_busy == 0
&& typebuf.tb_maplen == 0
&& (State & INSERT)
&& (p_timeout
|| (keylen == KEYLEN_PART_KEY && p_ttimeout))
&& (c = inchar(typebuf.tb_buf + typebuf.tb_off
+ typebuf.tb_len, 3, 25L)) == 0)
{
colnr_T col = 0, vcol;
char_u *ptr;
if (mode_displayed)
{
unshowmode(TRUE);
mode_deleted = TRUE;
}
#ifdef FEAT_GUI
if (gui.in_use && State != NORMAL && !cmd_silent)
{
int save_State;
save_State = State;
State = NORMAL;
gui_update_cursor(TRUE, FALSE);
State = save_State;
shape_changed = TRUE;
}
#endif
validate_cursor();
old_wcol = curwin->w_wcol;
old_wrow = curwin->w_wrow;
if (curwin->w_cursor.col != 0)
{
if (curwin->w_wcol > 0)
{
if (did_ai)
{
col = vcol = curwin->w_wcol = 0;
ptr = ml_get_curline();
while (col < curwin->w_cursor.col)
{
if (!VIM_ISWHITE(ptr[col]))
curwin->w_wcol = vcol;
vcol += lbr_chartabsize(ptr, ptr + col,
(colnr_T)vcol);
#ifdef FEAT_MBYTE
if (has_mbyte)
col += (*mb_ptr2len)(ptr + col);
else
#endif
++col;
}
curwin->w_wrow = curwin->w_cline_row
+ curwin->w_wcol / curwin->w_width;
curwin->w_wcol %= curwin->w_width;
curwin->w_wcol += curwin_col_off();
#ifdef FEAT_MBYTE
col = 0;
#endif
}
else
{
--curwin->w_wcol;
#ifdef FEAT_MBYTE
col = curwin->w_cursor.col - 1;
#endif
}
}
else if (curwin->w_p_wrap && curwin->w_wrow)
{
--curwin->w_wrow;
curwin->w_wcol = curwin->w_width - 1;
#ifdef FEAT_MBYTE
col = curwin->w_cursor.col - 1;
#endif
}
#ifdef FEAT_MBYTE
if (has_mbyte && col > 0 && curwin->w_wcol > 0)
{
ptr = ml_get_curline();
col -= (*mb_head_off)(ptr, ptr + col);
if ((*mb_ptr2cells)(ptr + col) > 1)
--curwin->w_wcol;
}
#endif
}
setcursor();
out_flush();
#ifdef FEAT_CMDL_INFO
new_wcol = curwin->w_wcol;
new_wrow = curwin->w_wrow;
#endif
curwin->w_wcol = old_wcol;
curwin->w_wrow = old_wrow;
}
if (c < 0)
continue;
for (n = 1; n <= c; ++n)
typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
typebuf.tb_len += c;
if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
{
timedout = TRUE;
continue;
}
if (ex_normal_busy > 0)
{
#ifdef FEAT_CMDWIN
static int tc = 0;
#endif
if (typebuf.tb_len > 0)
{
timedout = TRUE;
continue;
}
if (p_im && (State & INSERT))
c = Ctrl_L;
else if ((State & CMDLINE)
#ifdef FEAT_CMDWIN
|| (cmdwin_type > 0 && tc == ESC)
#endif
)
c = Ctrl_C;
else
c = ESC;
#ifdef FEAT_CMDWIN
tc = c;
#endif
break;
}
if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
&& advance && must_redraw != 0 && !need_wait_return)
{
update_screen(0);
setcursor();
}
#ifdef FEAT_CMDL_INFO
i = 0;
#endif
c1 = 0;
if (typebuf.tb_len > 0 && advance && !exmode_active)
{
if (((State & (NORMAL | INSERT)) || State == LANGMAP)
&& State != HITRETURN)
{
if (State & INSERT
&& ptr2cells(typebuf.tb_buf + typebuf.tb_off
+ typebuf.tb_len - 1) == 1)
{
edit_putchar(typebuf.tb_buf[typebuf.tb_off
+ typebuf.tb_len - 1], FALSE);
setcursor();
c1 = 1;
}
#ifdef FEAT_CMDL_INFO
old_wcol = curwin->w_wcol;
old_wrow = curwin->w_wrow;
curwin->w_wcol = new_wcol;
curwin->w_wrow = new_wrow;
push_showcmd();
if (typebuf.tb_len > SHOWCMD_COLS)
i = typebuf.tb_len - SHOWCMD_COLS;
while (i < typebuf.tb_len)
(void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
+ i++]);
curwin->w_wcol = old_wcol;
curwin->w_wrow = old_wrow;
#endif
}
if ((State & CMDLINE)
#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
&& cmdline_star == 0
#endif
&& ptr2cells(typebuf.tb_buf + typebuf.tb_off
+ typebuf.tb_len - 1) == 1)
{
putcmdline(typebuf.tb_buf[typebuf.tb_off
+ typebuf.tb_len - 1], FALSE);
c1 = 1;
}
}
wait_tb_len = typebuf.tb_len;
c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
!advance
? 0
: ((typebuf.tb_len == 0
|| !(p_timeout || (p_ttimeout
&& keylen == KEYLEN_PART_KEY)))
? -1L
: ((keylen == KEYLEN_PART_KEY && p_ttm >= 0)
? p_ttm
: p_tm)));
#ifdef FEAT_CMDL_INFO
if (i != 0)
pop_showcmd();
#endif
if (c1 == 1)
{
if (State & INSERT)
edit_unputchar();
if (State & CMDLINE)
unputcmdline();
else
setcursor();
}
if (c < 0)
continue;
if (c == NUL)
{
if (!advance)
break;
if (wait_tb_len > 0)
{
timedout = TRUE;
continue;
}
}
else
{
while (typebuf.tb_buf[typebuf.tb_off
+ typebuf.tb_len] != NUL)
typebuf.tb_noremap[typebuf.tb_off
+ typebuf.tb_len++] = RM_YES;
#ifdef USE_IM_CONTROL
vgetc_im_active = im_get_status();
#endif
}
}
}
} while (c < 0 || (advance && c == NUL));
if (advance && p_smd && msg_silent == 0 && (State & INSERT))
{
if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
{
if (typebuf.tb_len && !KeyTyped)
redraw_cmdline = TRUE;
else
unshowmode(FALSE);
}
else if (c != ESC && mode_deleted)
{
if (typebuf.tb_len && !KeyTyped)
redraw_cmdline = TRUE;
else
showmode();
}
}
#ifdef FEAT_GUI
if (gui.in_use && shape_changed)
gui_update_cursor(TRUE, FALSE);
#endif
--vgetc_busy;
return c;
}
static int
inchar(
char_u *buf,
int maxlen,
long wait_time)
{
int len = 0;
int retesc = FALSE;
int script_char;
int tb_change_cnt = typebuf.tb_change_cnt;
if (wait_time == -1L || wait_time > 100L)
{
cursor_on();
out_flush();
#ifdef FEAT_GUI
if (gui.in_use)
{
gui_update_cursor(FALSE, FALSE);
# ifdef FEAT_MOUSESHAPE
if (postponed_mouseshape)
update_mouseshape(-1);
# endif
}
#endif
}
if (State != HITRETURN)
{
did_outofmem_msg = FALSE;
did_swapwrite_msg = FALSE;
}
undo_off = FALSE;
script_char = -1;
while (scriptin[curscript] != NULL && script_char < 0
#ifdef FEAT_EVAL
&& !ignore_script
#endif
)
{
#ifdef MESSAGE_QUEUE
parse_queued_messages();
#endif
if (got_int || (script_char = getc(scriptin[curscript])) < 0)
{
closescript();
if (got_int)
retesc = TRUE;
else
return -1;
}
else
{
buf[0] = script_char;
len = 1;
}
}
if (script_char < 0)
{
if (got_int)
{
#define DUM_LEN MAXMAPLEN * 3 + 3
char_u dum[DUM_LEN + 1];
for (;;)
{
len = ui_inchar(dum, DUM_LEN, 0L, 0);
if (len == 0 || (len == 1 && dum[0] == 3))
break;
}
return retesc;
}
out_flush();
len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
}
if (typebuf_changed(tb_change_cnt))
return 0;
if (len > 0 && ++typebuf.tb_change_cnt == 0)
typebuf.tb_change_cnt = 1;
return fix_input_buffer(buf, len);
}
int
fix_input_buffer(char_u *buf, int len)
{
int i;
char_u *p = buf;
for (i = len; --i >= 0; ++p)
{
#ifdef FEAT_GUI
if (gui.in_use && p[0] == CSI && i >= 2)
{
p += 2;
i -= 2;
}
else if (!gui.in_use && p[0] == CSI)
{
mch_memmove(p + 3, p + 1, (size_t)i);
*p++ = K_SPECIAL;
*p++ = KS_EXTRA;
*p = (int)KE_CSI;
len += 2;
}
else
#endif
if (p[0] == NUL || (p[0] == K_SPECIAL
#ifdef FEAT_AUTOCMD
&& (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
#endif
#if defined(WIN3264) && !defined(FEAT_GUI)
&& (i < 2 || p[1] != KS_MODIFIER)
#endif
))
{
mch_memmove(p + 3, p + 1, (size_t)i);
p[2] = K_THIRD(p[0]);
p[1] = K_SECOND(p[0]);
p[0] = K_SPECIAL;
p += 2;
len += 2;
}
}
*p = NUL;
return len;
}
#if defined(USE_INPUT_BUF) || defined(PROTO)
int
input_available(void)
{
return (!vim_is_input_buf_empty()
# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
|| typebuf_was_filled
# endif
);
}
#endif
int
do_map(
int maptype,
char_u *arg,
int mode,
int abbrev)
{
char_u *keys;
mapblock_T *mp, **mpp;
char_u *rhs;
char_u *p;
int n;
int len = 0;
char_u *newstr;
int hasarg;
int haskey;
int did_it = FALSE;
#ifdef FEAT_LOCALMAP
int did_local = FALSE;
#endif
int round;
char_u *keys_buf = NULL;
char_u *arg_buf = NULL;
int retval = 0;
int do_backslash;
int hash;
int new_hash;
mapblock_T **abbr_table;
mapblock_T **map_table;
int unique = FALSE;
int nowait = FALSE;
int silent = FALSE;
int special = FALSE;
#ifdef FEAT_EVAL
int expr = FALSE;
#endif
int noremap;
char_u *orig_rhs;
keys = arg;
map_table = maphash;
abbr_table = &first_abbr;
if (maptype == 2)
noremap = REMAP_NONE;
else
noremap = REMAP_YES;
for (;;)
{
#ifdef FEAT_LOCALMAP
if (STRNCMP(keys, "<buffer>", 8) == 0)
{
keys = skipwhite(keys + 8);
map_table = curbuf->b_maphash;
abbr_table = &curbuf->b_first_abbr;
continue;
}
#endif
if (STRNCMP(keys, "<nowait>", 8) == 0)
{
keys = skipwhite(keys + 8);
nowait = TRUE;
continue;
}
if (STRNCMP(keys, "<silent>", 8) == 0)
{
keys = skipwhite(keys + 8);
silent = TRUE;
continue;
}
if (STRNCMP(keys, "<special>", 9) == 0)
{
keys = skipwhite(keys + 9);
special = TRUE;
continue;
}
#ifdef FEAT_EVAL
if (STRNCMP(keys, "<script>", 8) == 0)
{
keys = skipwhite(keys + 8);
noremap = REMAP_SCRIPT;
continue;
}
if (STRNCMP(keys, "<expr>", 6) == 0)
{
keys = skipwhite(keys + 6);
expr = TRUE;
continue;
}
#endif
if (STRNCMP(keys, "<unique>", 8) == 0)
{
keys = skipwhite(keys + 8);
unique = TRUE;
continue;
}
break;
}
validate_maphash();
p = keys;
do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
while (*p && (maptype == 1 || !VIM_ISWHITE(*p)))
{
if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) &&
p[1] != NUL)
++p;
++p;
}
if (*p != NUL)
*p++ = NUL;
p = skipwhite(p);
rhs = p;
hasarg = (*rhs != NUL);
haskey = (*keys != NUL);
if (maptype == 1 && !haskey)
{
retval = 1;
goto theend;
}
if (haskey)
keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special);
orig_rhs = rhs;
if (hasarg)
{
if (STRICMP(rhs, "<nop>") == 0)
rhs = (char_u *)"";
else
rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special);
}
#ifdef FEAT_FKMAP
if (p_altkeymap && curwin->w_p_rl)
lrswap(rhs);
#endif
if (haskey)
{
len = (int)STRLEN(keys);
if (len > MAXMAPLEN)
{
retval = 1;
goto theend;
}
if (abbrev && maptype != 1)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int first, last;
int same = -1;
first = vim_iswordp(keys);
last = first;
p = keys + (*mb_ptr2len)(keys);
n = 1;
while (p < keys + len)
{
++n;
last = vim_iswordp(p);
if (same == -1 && last != first)
same = n - 1;
p += (*mb_ptr2len)(p);
}
if (last && n > 2 && same >= 0 && same < n - 1)
{
retval = 1;
goto theend;
}
}
else
#endif
if (vim_iswordc(keys[len - 1]))
for (n = 0; n < len - 2; ++n)
if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2]))
{
retval = 1;
goto theend;
}
for (n = 0; n < len; ++n)
if (VIM_ISWHITE(keys[n]))
{
retval = 1;
goto theend;
}
}
}
if (haskey && hasarg && abbrev)
no_abbr = FALSE;
if (!haskey || (maptype != 1 && !hasarg))
msg_start();
#ifdef FEAT_LOCALMAP
if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1)
{
for (hash = 0; hash < 256 && !got_int; ++hash)
{
if (abbrev)
{
if (hash != 0)
break;
mp = first_abbr;
}
else
mp = maphash[hash];
for ( ; mp != NULL && !got_int; mp = mp->m_next)
{
if ((mp->m_mode & mode) != 0
&& mp->m_keylen == len
&& unique
&& STRNCMP(mp->m_keys, keys, (size_t)len) == 0)
{
if (abbrev)
EMSG2(_("E224: global abbreviation already exists for %s"),
mp->m_keys);
else
EMSG2(_("E225: global mapping already exists for %s"),
mp->m_keys);
retval = 5;
goto theend;
}
}
}
}
if (map_table != curbuf->b_maphash && !hasarg && maptype != 1)
{
for (hash = 0; hash < 256 && !got_int; ++hash)
{
if (abbrev)
{
if (hash != 0)
break;
mp = curbuf->b_first_abbr;
}
else
mp = curbuf->b_maphash[hash];
for ( ; mp != NULL && !got_int; mp = mp->m_next)
{
if ((mp->m_mode & mode) != 0)
{
if (!haskey)
{
showmap(mp, TRUE);
did_local = TRUE;
}
else
{
n = mp->m_keylen;
if (STRNCMP(mp->m_keys, keys,
(size_t)(n < len ? n : len)) == 0)
{
showmap(mp, TRUE);
did_local = TRUE;
}
}
}
}
}
}
#endif
for (round = 0; (round == 0 || maptype == 1) && round <= 1
&& !did_it && !got_int; ++round)
{
for (hash = 0; hash < 256 && !got_int; ++hash)
{
if (abbrev)
{
if (hash > 0)
break;
mpp = abbr_table;
}
else
mpp = &(map_table[hash]);
for (mp = *mpp; mp != NULL && !got_int; mp = *mpp)
{
if (!(mp->m_mode & mode))
{
mpp = &(mp->m_next);
continue;
}
if (!haskey)
{
showmap(mp, map_table != maphash);
did_it = TRUE;
}
else
{
if (round)
{
n = (int)STRLEN(mp->m_str);
p = mp->m_str;
}
else
{
n = mp->m_keylen;
p = mp->m_keys;
}
if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0)
{
if (maptype == 1)
{
if (n != len && (!abbrev || round || n > len
|| *skipwhite(keys + n) != NUL))
{
mpp = &(mp->m_next);
continue;
}
mp->m_mode &= ~mode;
did_it = TRUE;
}
else if (!hasarg)
{
showmap(mp, map_table != maphash);
did_it = TRUE;
}
else if (n != len)
{
mpp = &(mp->m_next);
continue;
}
else if (unique)
{
if (abbrev)
EMSG2(_("E226: abbreviation already exists for %s"),
p);
else
EMSG2(_("E227: mapping already exists for %s"), p);
retval = 5;
goto theend;
}
else
{
mp->m_mode &= ~mode;
if (mp->m_mode == 0 && !did_it)
{
newstr = vim_strsave(rhs);
if (newstr == NULL)
{
retval = 4;
goto theend;
}
vim_free(mp->m_str);
mp->m_str = newstr;
vim_free(mp->m_orig_str);
mp->m_orig_str = vim_strsave(orig_rhs);
mp->m_noremap = noremap;
mp->m_nowait = nowait;
mp->m_silent = silent;
mp->m_mode = mode;
#ifdef FEAT_EVAL
mp->m_expr = expr;
mp->m_script_ID = current_SID;
#endif
did_it = TRUE;
}
}
if (mp->m_mode == 0)
{
map_free(mpp);
continue;
}
new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
if (!abbrev && new_hash != hash)
{
*mpp = mp->m_next;
mp->m_next = map_table[new_hash];
map_table[new_hash] = mp;
continue;
}
}
}
mpp = &(mp->m_next);
}
}
}
if (maptype == 1)
{
if (!did_it)
retval = 2;
else if (*keys == Ctrl_C)
{
#ifdef FEAT_LOCALMAP
if (map_table == curbuf->b_maphash)
curbuf->b_mapped_ctrl_c &= ~mode;
else
#endif
mapped_ctrl_c &= ~mode;
}
goto theend;
}
if (!haskey || !hasarg)
{
if (!did_it
#ifdef FEAT_LOCALMAP
&& !did_local
#endif
)
{
if (abbrev)
MSG(_("No abbreviation found"));
else
MSG(_("No mapping found"));
}
goto theend;
}
if (did_it)
goto theend;
mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T));
if (mp == NULL)
{
retval = 4;
goto theend;
}
if (*keys == Ctrl_C)
{
#ifdef FEAT_LOCALMAP
if (map_table == curbuf->b_maphash)
curbuf->b_mapped_ctrl_c |= mode;
else
#endif
mapped_ctrl_c |= mode;
}
mp->m_keys = vim_strsave(keys);
mp->m_str = vim_strsave(rhs);
mp->m_orig_str = vim_strsave(orig_rhs);
if (mp->m_keys == NULL || mp->m_str == NULL)
{
vim_free(mp->m_keys);
vim_free(mp->m_str);
vim_free(mp->m_orig_str);
vim_free(mp);
retval = 4;
goto theend;
}
mp->m_keylen = (int)STRLEN(mp->m_keys);
mp->m_noremap = noremap;
mp->m_nowait = nowait;
mp->m_silent = silent;
mp->m_mode = mode;
#ifdef FEAT_EVAL
mp->m_expr = expr;
mp->m_script_ID = current_SID;
#endif
if (abbrev)
{
mp->m_next = *abbr_table;
*abbr_table = mp;
}
else
{
n = MAP_HASH(mp->m_mode, mp->m_keys[0]);
mp->m_next = map_table[n];
map_table[n] = mp;
}
theend:
vim_free(keys_buf);
vim_free(arg_buf);
return retval;
}
static void
map_free(mapblock_T **mpp)
{
mapblock_T *mp;
mp = *mpp;
vim_free(mp->m_keys);
vim_free(mp->m_str);
vim_free(mp->m_orig_str);
*mpp = mp->m_next;
vim_free(mp);
}
static void
validate_maphash(void)
{
if (!maphash_valid)
{
vim_memset(maphash, 0, sizeof(maphash));
maphash_valid = TRUE;
}
}
int
get_map_mode(char_u **cmdp, int forceit)
{
char_u *p;
int modec;
int mode;
p = *cmdp;
modec = *p++;
if (modec == 'i')
mode = INSERT;
else if (modec == 'l')
mode = LANGMAP;
else if (modec == 'c')
mode = CMDLINE;
else if (modec == 'n' && *p != 'o')
mode = NORMAL;
else if (modec == 'v')
mode = VISUAL + SELECTMODE;
else if (modec == 'x')
mode = VISUAL;
else if (modec == 's')
mode = SELECTMODE;
else if (modec == 'o')
mode = OP_PENDING;
else if (modec == 't')
mode = TERMINAL;
else
{
--p;
if (forceit)
mode = INSERT + CMDLINE;
else
mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;
}
*cmdp = p;
return mode;
}
void
map_clear(
char_u *cmdp,
char_u *arg UNUSED,
int forceit,
int abbr)
{
int mode;
#ifdef FEAT_LOCALMAP
int local;
local = (STRCMP(arg, "<buffer>") == 0);
if (!local && *arg != NUL)
{
EMSG(_(e_invarg));
return;
}
#endif
mode = get_map_mode(&cmdp, forceit);
map_clear_int(curbuf, mode,
#ifdef FEAT_LOCALMAP
local,
#else
FALSE,
#endif
abbr);
}
void
map_clear_int(
buf_T *buf UNUSED,
int mode,
int local UNUSED,
int abbr)
{
mapblock_T *mp, **mpp;
int hash;
int new_hash;
validate_maphash();
for (hash = 0; hash < 256; ++hash)
{
if (abbr)
{
if (hash > 0)
break;
#ifdef FEAT_LOCALMAP
if (local)
mpp = &buf->b_first_abbr;
else
#endif
mpp = &first_abbr;
}
else
{
#ifdef FEAT_LOCALMAP
if (local)
mpp = &buf->b_maphash[hash];
else
#endif
mpp = &maphash[hash];
}
while (*mpp != NULL)
{
mp = *mpp;
if (mp->m_mode & mode)
{
mp->m_mode &= ~mode;
if (mp->m_mode == 0)
{
map_free(mpp);
continue;
}
new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]);
if (!abbr && new_hash != hash)
{
*mpp = mp->m_next;
#ifdef FEAT_LOCALMAP
if (local)
{
mp->m_next = buf->b_maphash[new_hash];
buf->b_maphash[new_hash] = mp;
}
else
#endif
{
mp->m_next = maphash[new_hash];
maphash[new_hash] = mp;
}
continue;
}
}
mpp = &(mp->m_next);
}
}
}
char_u *
map_mode_to_chars(int mode)
{
garray_T mapmode;
ga_init2(&mapmode, 1, 7);
if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
ga_append(&mapmode, '!');
else if (mode & INSERT)
ga_append(&mapmode, 'i');
else if (mode & LANGMAP)
ga_append(&mapmode, 'l');
else if (mode & CMDLINE)
ga_append(&mapmode, 'c');
else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING))
== NORMAL + VISUAL + SELECTMODE + OP_PENDING)
ga_append(&mapmode, ' ');
else
{
if (mode & NORMAL)
ga_append(&mapmode, 'n');
if (mode & OP_PENDING)
ga_append(&mapmode, 'o');
if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE)
ga_append(&mapmode, 'v');
else
{
if (mode & VISUAL)
ga_append(&mapmode, 'x');
if (mode & SELECTMODE)
ga_append(&mapmode, 's');
}
}
ga_append(&mapmode, NUL);
return (char_u *)mapmode.ga_data;
}
static void
showmap(
mapblock_T *mp,
int local)
{
int len = 1;
char_u *mapchars;
if (message_filtered(mp->m_keys) && message_filtered(mp->m_str))
return;
if (msg_didout || msg_silent != 0)
{
msg_putchar('\n');
if (got_int)
return;
}
mapchars = map_mode_to_chars(mp->m_mode);
if (mapchars != NULL)
{
msg_puts(mapchars);
len = (int)STRLEN(mapchars);
vim_free(mapchars);
}
while (++len <= 3)
msg_putchar(' ');
len = msg_outtrans_special(mp->m_keys, TRUE);
do
{
msg_putchar(' ');
++len;
} while (len < 12);
if (mp->m_noremap == REMAP_NONE)
msg_puts_attr((char_u *)"*", HL_ATTR(HLF_8));
else if (mp->m_noremap == REMAP_SCRIPT)
msg_puts_attr((char_u *)"&", HL_ATTR(HLF_8));
else
msg_putchar(' ');
if (local)
msg_putchar('@');
else
msg_putchar(' ');
if (*mp->m_str == NUL)
msg_puts_attr((char_u *)"<Nop>", HL_ATTR(HLF_8));
else
{
char_u *s = vim_strsave(mp->m_str);
if (s != NULL)
{
vim_unescape_csi(s);
msg_outtrans_special(s, FALSE);
vim_free(s);
}
}
#ifdef FEAT_EVAL
if (p_verbose > 0)
last_set_msg(mp->m_script_ID);
#endif
out_flush();
}
#if defined(FEAT_EVAL) || defined(PROTO)
int
map_to_exists(char_u *str, char_u *modechars, int abbr)
{
int mode = 0;
char_u *rhs;
char_u *buf;
int retval;
rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE);
if (vim_strchr(modechars, 'n') != NULL)
mode |= NORMAL;
if (vim_strchr(modechars, 'v') != NULL)
mode |= VISUAL + SELECTMODE;
if (vim_strchr(modechars, 'x') != NULL)
mode |= VISUAL;
if (vim_strchr(modechars, 's') != NULL)
mode |= SELECTMODE;
if (vim_strchr(modechars, 'o') != NULL)
mode |= OP_PENDING;
if (vim_strchr(modechars, 'i') != NULL)
mode |= INSERT;
if (vim_strchr(modechars, 'l') != NULL)
mode |= LANGMAP;
if (vim_strchr(modechars, 'c') != NULL)
mode |= CMDLINE;
retval = map_to_exists_mode(rhs, mode, abbr);
vim_free(buf);
return retval;
}
#endif
int
map_to_exists_mode(char_u *rhs, int mode, int abbr)
{
mapblock_T *mp;
int hash;
# ifdef FEAT_LOCALMAP
int expand_buffer = FALSE;
validate_maphash();
for (;;)
{
# endif
for (hash = 0; hash < 256; ++hash)
{
if (abbr)
{
if (hash > 0)
break;
#ifdef FEAT_LOCALMAP
if (expand_buffer)
mp = curbuf->b_first_abbr;
else
#endif
mp = first_abbr;
}
# ifdef FEAT_LOCALMAP
else if (expand_buffer)
mp = curbuf->b_maphash[hash];
# endif
else
mp = maphash[hash];
for (; mp; mp = mp->m_next)
{
if ((mp->m_mode & mode)
&& strstr((char *)mp->m_str, (char *)rhs) != NULL)
return TRUE;
}
}
# ifdef FEAT_LOCALMAP
if (expand_buffer)
break;
expand_buffer = TRUE;
}
# endif
return FALSE;
}
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
static int expand_mapmodes = 0;
static int expand_isabbrev = 0;
#ifdef FEAT_LOCALMAP
static int expand_buffer = FALSE;
#endif
char_u *
set_context_in_map_cmd(
expand_T *xp,
char_u *cmd,
char_u *arg,
int forceit,
int isabbrev,
int isunmap,
cmdidx_T cmdidx)
{
if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap)
xp->xp_context = EXPAND_NOTHING;
else
{
if (isunmap)
expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev);
else
{
expand_mapmodes = INSERT + CMDLINE;
if (!isabbrev)
expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING;
}
expand_isabbrev = isabbrev;
xp->xp_context = EXPAND_MAPPINGS;
#ifdef FEAT_LOCALMAP
expand_buffer = FALSE;
#endif
for (;;)
{
#ifdef FEAT_LOCALMAP
if (STRNCMP(arg, "<buffer>", 8) == 0)
{
expand_buffer = TRUE;
arg = skipwhite(arg + 8);
continue;
}
#endif
if (STRNCMP(arg, "<unique>", 8) == 0)
{
arg = skipwhite(arg + 8);
continue;
}
if (STRNCMP(arg, "<nowait>", 8) == 0)
{
arg = skipwhite(arg + 8);
continue;
}
if (STRNCMP(arg, "<silent>", 8) == 0)
{
arg = skipwhite(arg + 8);
continue;
}
if (STRNCMP(arg, "<special>", 9) == 0)
{
arg = skipwhite(arg + 9);
continue;
}
#ifdef FEAT_EVAL
if (STRNCMP(arg, "<script>", 8) == 0)
{
arg = skipwhite(arg + 8);
continue;
}
if (STRNCMP(arg, "<expr>", 6) == 0)
{
arg = skipwhite(arg + 6);
continue;
}
#endif
break;
}
xp->xp_pattern = arg;
}
return NULL;
}
int
ExpandMappings(
regmatch_T *regmatch,
int *num_file,
char_u ***file)
{
mapblock_T *mp;
int hash;
int count;
int round;
char_u *p;
int i;
validate_maphash();
*num_file = 0;
*file = NULL;
for (round = 1; round <= 2; ++round)
{
count = 0;
for (i = 0; i < 7; ++i)
{
if (i == 0)
p = (char_u *)"<silent>";
else if (i == 1)
p = (char_u *)"<unique>";
#ifdef FEAT_EVAL
else if (i == 2)
p = (char_u *)"<script>";
else if (i == 3)
p = (char_u *)"<expr>";
#endif
#ifdef FEAT_LOCALMAP
else if (i == 4 && !expand_buffer)
p = (char_u *)"<buffer>";
#endif
else if (i == 5)
p = (char_u *)"<nowait>";
else if (i == 6)
p = (char_u *)"<special>";
else
continue;
if (vim_regexec(regmatch, p, (colnr_T)0))
{
if (round == 1)
++count;
else
(*file)[count++] = vim_strsave(p);
}
}
for (hash = 0; hash < 256; ++hash)
{
if (expand_isabbrev)
{
if (hash > 0)
break;
mp = first_abbr;
}
#ifdef FEAT_LOCALMAP
else if (expand_buffer)
mp = curbuf->b_maphash[hash];
#endif
else
mp = maphash[hash];
for (; mp; mp = mp->m_next)
{
if (mp->m_mode & expand_mapmodes)
{
p = translate_mapping(mp->m_keys, TRUE);
if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0))
{
if (round == 1)
++count;
else
{
(*file)[count++] = p;
p = NULL;
}
}
vim_free(p);
}
}
}
if (count == 0)
break;
if (round == 1)
{
*file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
if (*file == NULL)
return FAIL;
}
}
if (count > 1)
{
char_u **ptr1;
char_u **ptr2;
char_u **ptr3;
sort_strings(*file, count);
ptr1 = *file;
ptr2 = ptr1 + 1;
ptr3 = ptr1 + count;
while (ptr2 < ptr3)
{
if (STRCMP(*ptr1, *ptr2))
*++ptr1 = *ptr2++;
else
{
vim_free(*ptr2++);
count--;
}
}
}
*num_file = count;
return (count == 0 ? FAIL : OK);
}
#endif
int
check_abbr(
int c,
char_u *ptr,
int col,
int mincol)
{
int len;
int scol;
int j;
char_u *s;
char_u tb[MB_MAXBYTES + 4];
mapblock_T *mp;
#ifdef FEAT_LOCALMAP
mapblock_T *mp2;
#endif
#ifdef FEAT_MBYTE
int clen = 0;
#endif
int is_id = TRUE;
int vim_abbr;
if (typebuf.tb_no_abbr_cnt)
return FALSE;
if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0 && c != Ctrl_RSB)
return FALSE;
if (col == 0)
return FALSE;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
char_u *p;
p = mb_prevptr(ptr, ptr + col);
if (!vim_iswordp(p))
vim_abbr = TRUE;
else
{
vim_abbr = FALSE;
if (p > ptr)
is_id = vim_iswordp(mb_prevptr(ptr, p));
}
clen = 1;
while (p > ptr + mincol)
{
p = mb_prevptr(ptr, p);
if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p)))
{
p += (*mb_ptr2len)(p);
break;
}
++clen;
}
scol = (int)(p - ptr);
}
else
#endif
{
if (!vim_iswordc(ptr[col - 1]))
vim_abbr = TRUE;
else
{
vim_abbr = FALSE;
if (col > 1)
is_id = vim_iswordc(ptr[col - 2]);
}
for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1])
&& (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol)
;
}
if (scol < mincol)
scol = mincol;
if (scol < col)
{
ptr += scol;
len = col - scol;
#ifdef FEAT_LOCALMAP
mp = curbuf->b_first_abbr;
mp2 = first_abbr;
if (mp == NULL)
{
mp = mp2;
mp2 = NULL;
}
#else
mp = first_abbr;
#endif
for ( ; mp;
#ifdef FEAT_LOCALMAP
mp->m_next == NULL ? (mp = mp2, mp2 = NULL) :
#endif
(mp = mp->m_next))
{
int qlen = mp->m_keylen;
char_u *q = mp->m_keys;
int match;
if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL)
{
q = vim_strsave(mp->m_keys);
if (q != NULL)
{
vim_unescape_csi(q);
qlen = (int)STRLEN(q);
}
}
match = (mp->m_mode & State)
&& qlen == len
&& !STRNCMP(q, ptr, (size_t)len);
if (q != mp->m_keys)
vim_free(q);
if (match)
break;
}
if (mp != NULL)
{
j = 0;
if (c != Ctrl_RSB)
{
if (IS_SPECIAL(c) || c == K_SPECIAL)
{
tb[j++] = K_SPECIAL;
tb[j++] = K_SECOND(c);
tb[j++] = K_THIRD(c);
}
else
{
if (c < ABBR_OFF && (c < ' ' || c > '~'))
tb[j++] = Ctrl_V;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
if (c >= ABBR_OFF)
c -= ABBR_OFF;
j += (*mb_char2bytes)(c, tb + j);
}
else
#endif
tb[j++] = c;
}
tb[j] = NUL;
(void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
}
#ifdef FEAT_EVAL
if (mp->m_expr)
s = eval_map_expr(mp->m_str, c);
else
#endif
s = mp->m_str;
if (s != NULL)
{
(void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
#ifdef FEAT_EVAL
if (mp->m_expr)
vim_free(s);
#endif
}
tb[0] = Ctrl_H;
tb[1] = NUL;
#ifdef FEAT_MBYTE
if (has_mbyte)
len = clen;
#endif
while (len-- > 0)
(void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
return TRUE;
}
}
return FALSE;
}
#ifdef FEAT_EVAL
static char_u *
eval_map_expr(
char_u *str,
int c)
{
char_u *res;
char_u *p;
char_u *expr;
char_u *save_cmd;
pos_T save_cursor;
int save_msg_col;
int save_msg_row;
expr = vim_strsave(str);
if (expr == NULL)
return NULL;
vim_unescape_csi(expr);
save_cmd = save_cmdline_alloc();
if (save_cmd == NULL)
{
vim_free(expr);
return NULL;
}
++textlock;
++ex_normal_lock;
set_vim_var_char(c);
save_cursor = curwin->w_cursor;
save_msg_col = msg_col;
save_msg_row = msg_row;
p = eval_to_string(expr, NULL, FALSE);
--textlock;
--ex_normal_lock;
curwin->w_cursor = save_cursor;
msg_col = save_msg_col;
msg_row = save_msg_row;
restore_cmdline_alloc(save_cmd);
vim_free(expr);
if (p == NULL)
return NULL;
res = vim_strsave_escape_csi(p);
vim_free(p);
return res;
}
#endif
char_u *
vim_strsave_escape_csi(
char_u *p)
{
char_u *res;
char_u *s, *d;
res = alloc((unsigned)(STRLEN(p) *
#ifdef FEAT_MBYTE
4
#else
3
#endif
) + 1);
if (res != NULL)
{
d = res;
for (s = p; *s != NUL; )
{
if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL)
{
*d++ = *s++;
*d++ = *s++;
*d++ = *s++;
}
else
{
d = add_char2buf(PTR2CHAR(s), d);
s += MB_CPTR2LEN(s);
}
}
*d = NUL;
}
return res;
}
void
vim_unescape_csi(char_u *p)
{
char_u *s = p, *d = p;
while (*s != NUL)
{
if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER)
{
*d++ = K_SPECIAL;
s += 3;
}
else if ((s[0] == K_SPECIAL || s[0] == CSI)
&& s[1] == KS_EXTRA && s[2] == (int)KE_CSI)
{
*d++ = CSI;
s += 3;
}
else
*d++ = *s++;
}
*d = NUL;
}
int
makemap(
FILE *fd,
buf_T *buf)
{
mapblock_T *mp;
char_u c1, c2, c3;
char_u *p;
char *cmd;
int abbr;
int hash;
int did_cpo = FALSE;
int i;
validate_maphash();
for (abbr = 0; abbr < 2; ++abbr)
for (hash = 0; hash < 256; ++hash)
{
if (abbr)
{
if (hash > 0)
break;
#ifdef FEAT_LOCALMAP
if (buf != NULL)
mp = buf->b_first_abbr;
else
#endif
mp = first_abbr;
}
else
{
#ifdef FEAT_LOCALMAP
if (buf != NULL)
mp = buf->b_maphash[hash];
else
#endif
mp = maphash[hash];
}
for ( ; mp; mp = mp->m_next)
{
if (mp->m_noremap == REMAP_SCRIPT)
continue;
for (p = mp->m_str; *p != NUL; ++p)
if (p[0] == K_SPECIAL && p[1] == KS_EXTRA
&& p[2] == (int)KE_SNR)
break;
if (*p != NUL)
continue;
c1 = NUL;
c2 = NUL;
c3 = NUL;
if (abbr)
cmd = "abbr";
else
cmd = "map";
switch (mp->m_mode)
{
case NORMAL + VISUAL + SELECTMODE + OP_PENDING:
break;
case NORMAL:
c1 = 'n';
break;
case VISUAL:
c1 = 'x';
break;
case SELECTMODE:
c1 = 's';
break;
case OP_PENDING:
c1 = 'o';
break;
case NORMAL + VISUAL:
c1 = 'n';
c2 = 'x';
break;
case NORMAL + SELECTMODE:
c1 = 'n';
c2 = 's';
break;
case NORMAL + OP_PENDING:
c1 = 'n';
c2 = 'o';
break;
case VISUAL + SELECTMODE:
c1 = 'v';
break;
case VISUAL + OP_PENDING:
c1 = 'x';
c2 = 'o';
break;
case SELECTMODE + OP_PENDING:
c1 = 's';
c2 = 'o';
break;
case NORMAL + VISUAL + SELECTMODE:
c1 = 'n';
c2 = 'v';
break;
case NORMAL + VISUAL + OP_PENDING:
c1 = 'n';
c2 = 'x';
c3 = 'o';
break;
case NORMAL + SELECTMODE + OP_PENDING:
c1 = 'n';
c2 = 's';
c3 = 'o';
break;
case VISUAL + SELECTMODE + OP_PENDING:
c1 = 'v';
c2 = 'o';
break;
case CMDLINE + INSERT:
if (!abbr)
cmd = "map!";
break;
case CMDLINE:
c1 = 'c';
break;
case INSERT:
c1 = 'i';
break;
case LANGMAP:
c1 = 'l';
break;
case TERMINAL:
c1 = 't';
break;
default:
IEMSG(_("E228: makemap: Illegal mode"));
return FAIL;
}
do
{
if (!did_cpo)
{
if (*mp->m_str == NUL)
did_cpo = TRUE;
else
for (i = 0; i < 2; ++i)
for (p = (i ? mp->m_str : mp->m_keys); *p; ++p)
if (*p == K_SPECIAL || *p == NL)
did_cpo = TRUE;
if (did_cpo)
{
if (fprintf(fd, "let s:cpo_save=&cpo") < 0
|| put_eol(fd) < 0
|| fprintf(fd, "set cpo&vim") < 0
|| put_eol(fd) < 0)
return FAIL;
}
}
if (c1 && putc(c1, fd) < 0)
return FAIL;
if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0)
return FAIL;
if (fputs(cmd, fd) < 0)
return FAIL;
if (buf != NULL && fputs(" <buffer>", fd) < 0)
return FAIL;
if (mp->m_nowait && fputs(" <nowait>", fd) < 0)
return FAIL;
if (mp->m_silent && fputs(" <silent>", fd) < 0)
return FAIL;
#ifdef FEAT_EVAL
if (mp->m_noremap == REMAP_SCRIPT
&& fputs("<script>", fd) < 0)
return FAIL;
if (mp->m_expr && fputs(" <expr>", fd) < 0)
return FAIL;
#endif
if ( putc(' ', fd) < 0
|| put_escstr(fd, mp->m_keys, 0) == FAIL
|| putc(' ', fd) < 0
|| put_escstr(fd, mp->m_str, 1) == FAIL
|| put_eol(fd) < 0)
return FAIL;
c1 = c2;
c2 = c3;
c3 = NUL;
} while (c1 != NUL);
}
}
if (did_cpo)
if (fprintf(fd, "let &cpo=s:cpo_save") < 0
|| put_eol(fd) < 0
|| fprintf(fd, "unlet s:cpo_save") < 0
|| put_eol(fd) < 0)
return FAIL;
return OK;
}
int
put_escstr(FILE *fd, char_u *strstart, int what)
{
char_u *str = strstart;
int c;
int modifiers;
if (*str == NUL && what == 1)
{
if (fprintf(fd, "<Nop>") < 0)
return FAIL;
return OK;
}
for ( ; *str != NUL; ++str)
{
#ifdef FEAT_MBYTE
char_u *p;
p = mb_unescape(&str);
if (p != NULL)
{
while (*p != NUL)
if (fputc(*p++, fd) < 0)
return FAIL;
--str;
continue;
}
#endif
c = *str;
if (c == K_SPECIAL && what != 2)
{
modifiers = 0x0;
if (str[1] == KS_MODIFIER)
{
modifiers = str[2];
str += 3;
c = *str;
}
if (c == K_SPECIAL)
{
c = TO_SPECIAL(str[1], str[2]);
str += 2;
}
if (IS_SPECIAL(c) || modifiers)
{
if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0)
return FAIL;
continue;
}
}
if (c == NL)
{
if (what == 2)
{
if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0)
return FAIL;
}
else
{
if (fprintf(fd, "<NL>") < 0)
return FAIL;
}
continue;
}
if (what == 2 && (VIM_ISWHITE(c) || c == '"' || c == '\\'))
{
if (putc('\\', fd) < 0)
return FAIL;
}
else if (c < ' ' || c > '~' || c == '|'
|| (what == 0 && c == ' ')
|| (what == 1 && str == strstart && c == ' ')
|| (what != 2 && c == '<'))
{
if (putc(Ctrl_V, fd) < 0)
return FAIL;
}
if (putc(c, fd) < 0)
return FAIL;
}
return OK;
}
void
check_map_keycodes(void)
{
mapblock_T *mp;
char_u *p;
int i;
char_u buf[3];
char_u *save_name;
int abbr;
int hash;
#ifdef FEAT_LOCALMAP
buf_T *bp;
#endif
validate_maphash();
save_name = sourcing_name;
sourcing_name = (char_u *)"mappings";
#ifdef FEAT_LOCALMAP
for (bp = firstbuf; ; bp = bp->b_next)
{
#endif
for (abbr = 0; abbr <= 1; ++abbr)
for (hash = 0; hash < 256; ++hash)
{
if (abbr)
{
if (hash)
break;
#ifdef FEAT_LOCALMAP
if (bp != NULL)
mp = bp->b_first_abbr;
else
#endif
mp = first_abbr;
}
else
{
#ifdef FEAT_LOCALMAP
if (bp != NULL)
mp = bp->b_maphash[hash];
else
#endif
mp = maphash[hash];
}
for ( ; mp != NULL; mp = mp->m_next)
{
for (i = 0; i <= 1; ++i)
{
if (i == 0)
p = mp->m_keys;
else
p = mp->m_str;
while (*p)
{
if (*p == K_SPECIAL)
{
++p;
if (*p < 128)
{
buf[0] = p[0];
buf[1] = p[1];
buf[2] = NUL;
(void)add_termcap_entry(buf, FALSE);
}
++p;
}
++p;
}
}
}
}
#ifdef FEAT_LOCALMAP
if (bp == NULL)
break;
}
#endif
sourcing_name = save_name;
}
#if defined(FEAT_EVAL) || defined(PROTO)
char_u *
check_map(
char_u *keys,
int mode,
int exact,
int ign_mod,
int abbr,
mapblock_T **mp_ptr,
int *local_ptr)
{
int hash;
int len, minlen;
mapblock_T *mp;
char_u *s;
#ifdef FEAT_LOCALMAP
int local;
#endif
validate_maphash();
len = (int)STRLEN(keys);
#ifdef FEAT_LOCALMAP
for (local = 1; local >= 0; --local)
#endif
for (hash = 0; hash < 256; ++hash)
{
if (abbr)
{
if (hash > 0)
break;
#ifdef FEAT_LOCALMAP
if (local)
mp = curbuf->b_first_abbr;
else
#endif
mp = first_abbr;
}
#ifdef FEAT_LOCALMAP
else if (local)
mp = curbuf->b_maphash[hash];
#endif
else
mp = maphash[hash];
for ( ; mp != NULL; mp = mp->m_next)
{
if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len))
{
if (len > mp->m_keylen)
minlen = mp->m_keylen;
else
minlen = len;
s = mp->m_keys;
if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER
&& s[2] != NUL)
{
s += 3;
if (len > mp->m_keylen - 3)
minlen = mp->m_keylen - 3;
}
if (STRNCMP(s, keys, minlen) == 0)
{
if (mp_ptr != NULL)
*mp_ptr = mp;
if (local_ptr != NULL)
#ifdef FEAT_LOCALMAP
*local_ptr = local;
#else
*local_ptr = 0;
#endif
return mp->m_str;
}
}
}
}
return NULL;
}
#endif
#if defined(MSWIN) || defined(MACOS_X)
#define VIS_SEL (VISUAL+SELECTMODE)
static struct initmap
{
char_u *arg;
int mode;
} initmappings[] =
{
#if defined(MSWIN)
# ifdef FEAT_GUI
{(char_u *)"<S-Insert> \"*P", NORMAL},
{(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL},
{(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE},
{(char_u *)"<C-Insert> \"*y", VIS_SEL},
{(char_u *)"<S-Del> \"*d", VIS_SEL},
{(char_u *)"<C-Del> \"*d", VIS_SEL},
{(char_u *)"<C-X> \"*d", VIS_SEL},
# else
{(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL},
{(char_u *)"\316w <C-Home>", INSERT+CMDLINE},
{(char_u *)"\316u <C-End>", NORMAL+VIS_SEL},
{(char_u *)"\316u <C-End>", INSERT+CMDLINE},
# ifdef FEAT_CLIPBOARD
{(char_u *)"\316\324 \"*P", NORMAL},
{(char_u *)"\316\324 \"-d\"*P", VIS_SEL},
{(char_u *)"\316\324 \022\017*", INSERT},
{(char_u *)"\316\325 \"*y", VIS_SEL},
{(char_u *)"\316\327 \"*d", VIS_SEL},
{(char_u *)"\316\330 \"*d", VIS_SEL},
{(char_u *)"\030 \"-d", VIS_SEL},
# else
{(char_u *)"\316\324 P", NORMAL},
{(char_u *)"\316\324 \"-dP", VIS_SEL},
{(char_u *)"\316\324 \022\017\"", INSERT},
{(char_u *)"\316\325 y", VIS_SEL},
{(char_u *)"\316\327 d", VIS_SEL},
{(char_u *)"\316\330 d", VIS_SEL},
# endif
# endif
#endif
#if defined(MACOS_X)
{(char_u *)"<D-v> \"*P", NORMAL},
{(char_u *)"<D-v> \"-d\"*P", VIS_SEL},
{(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE},
{(char_u *)"<D-c> \"*y", VIS_SEL},
{(char_u *)"<D-x> \"*d", VIS_SEL},
{(char_u *)"<Backspace> \"-d", VIS_SEL},
#endif
};
# undef VIS_SEL
#endif
void
init_mappings(void)
{
#if defined(MSWIN) || defined(MACOS_X)
int i;
for (i = 0; i < (int)(sizeof(initmappings) / sizeof(struct initmap)); ++i)
add_map(initmappings[i].arg, initmappings[i].mode);
#endif
}
#if defined(MSWIN) || defined(FEAT_CMDWIN) || defined(MACOS_X) \
|| defined(PROTO)
void
add_map(char_u *map, int mode)
{
char_u *s;
char_u *cpo_save = p_cpo;
p_cpo = (char_u *)"";
s = vim_strsave(map);
if (s != NULL)
{
(void)do_map(0, s, mode, FALSE);
vim_free(s);
}
p_cpo = cpo_save;
}
#endif