diff options
author | Bram Moolenaar <Bram@vim.org> | 2004-06-13 20:20:40 +0000 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2004-06-13 20:20:40 +0000 |
commit | 071d4279d6ab81b7187b48f3a0fc61e587b6db6c (patch) | |
tree | 221cbe3c40e043163c06f61c52a7ba2eb41e12ce /src/getchar.c | |
parent | b4210b3bc14e2918f153a7307530fbe6eba659e1 (diff) |
updated for version 7.0001v7.0001
Diffstat (limited to 'src/getchar.c')
-rw-r--r-- | src/getchar.c | 4636 |
1 files changed, 4636 insertions, 0 deletions
diff --git a/src/getchar.c b/src/getchar.c new file mode 100644 index 0000000000..3a84383579 --- /dev/null +++ b/src/getchar.c @@ -0,0 +1,4636 @@ +/* vi:set ts=8 sts=4 sw=4: + * + * VIM - Vi IMproved by Bram Moolenaar + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ + +/* + * getchar.c + * + * functions related with getting a character from the user/mapping/redo/... + * + * manipulations with redo buffer and stuff buffer + * mappings and abbreviations + */ + +#include "vim.h" + +/* + * These buffers are used for storing: + * - stuffed characters: A command that is translated into another command. + * - redo characters: will redo the last change. + * - recorded chracters: for the "q" command. + * + * The bytes are stored like in the typeahead buffer: + * - K_SPECIAL introduces a special key (two more bytes follow). A literal + * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER. + * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE, + * otherwise switching the GUI on would make mappings invalid). + * A literal CSI is stored as CSI KS_EXTRA KE_CSI. + * These translations are also done on multi-byte characters! + * + * Escaping CSI bytes is done by the system-specific input functions, called + * by ui_inchar(). + * Escaping K_SPECIAL is done by inchar(). + * Un-escaping is done by vgetc(). + */ + +#define MINIMAL_SIZE 20 /* minimal size for b_str */ + +static struct buffheader redobuff = {{NULL, {NUL}}, NULL, 0, 0}; +static struct buffheader old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; +#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO) +static struct buffheader save_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; +static struct buffheader save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; +#endif +static struct buffheader recordbuff = {{NULL, {NUL}}, NULL, 0, 0}; + +static int typeahead_char = 0; /* typeahead char that's not flushed */ + +/* + * when block_redo is TRUE redo buffer will not be changed + * used by edit() to repeat insertions and 'V' command for redoing + */ +static int block_redo = FALSE; + +/* + * Make a hash value for a mapping. + * "mode" is the lower 4 bits of the State for the mapping. + * "c1" is the first character of the "lhs". + * Returns a value between 0 and 255, index in maphash. + * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode. + */ +#define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + OP_PENDING)) ? (c1) : ((c1) ^ 0x80)) + +/* + * Each mapping is put in one of the 256 hash lists, to speed up finding it. + */ +static mapblock_T *(maphash[256]); +static int maphash_valid = FALSE; + +/* + * List used for abbreviations. + */ +static mapblock_T *first_abbr = NULL; /* first entry in abbrlist */ + +static int KeyNoremap = FALSE; /* remapping disabled */ + +/* + * variables used by vgetorpeek() and flush_buffers() + * + * typebuf.tb_buf[] contains all characters that are not consumed yet. + * typebuf.tb_buf[typebuf.tb_off] is the first valid character. + * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char. + * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL. + * The head of the buffer may contain the result of mappings, abbreviations + * and @a commands. The length of this part is typebuf.tb_maplen. + * typebuf.tb_silent is the part where <silent> applies. + * After the head are characters that come from the terminal. + * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that + * should not be considered for abbreviations. + * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered + * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and + * contains RM_NONE for the characters that are not to be remapped. + * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag. + * (typebuf has been put in globals.h, because check_termcode() needs it). + */ +#define RM_YES 0 /* tb_noremap: remap */ +#define RM_NONE 1 /* tb_noremap: don't remap */ +#define RM_SCRIPT 2 /* tb_noremap: remap local script mappings */ + +/* typebuf.tb_buf has three parts: room in front (for result of mappings), the + * middle for typeahead and room for new characters (which needs to be 3 * + * MAXMAPLEN) for the Amiga). + */ +#define TYPELEN_INIT (5 * (MAXMAPLEN + 3)) +static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */ +static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */ + +static int last_recorded_len = 0; /* number of last recorded chars */ + +static char_u *get_buffcont __ARGS((struct buffheader *, int)); +static void add_buff __ARGS((struct buffheader *, char_u *, long n)); +static void add_num_buff __ARGS((struct buffheader *, long)); +static void add_char_buff __ARGS((struct buffheader *, int)); +static int read_stuff __ARGS((int advance)); +static void start_stuff __ARGS((void)); +static int read_redo __ARGS((int, int)); +static void copy_redo __ARGS((int)); +static void init_typebuf __ARGS((void)); +static void gotchars __ARGS((char_u *, int)); +static void may_sync_undo __ARGS((void)); +static void closescript __ARGS((void)); +static int vgetorpeek __ARGS((int)); +static void map_free __ARGS((mapblock_T **)); +static void validate_maphash __ARGS((void)); +static void showmap __ARGS((mapblock_T *mp, int local)); + +/* + * Free and clear a buffer. + */ + void +free_buff(buf) + struct buffheader *buf; +{ + struct buffblock *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; +} + +/* + * Return the contents of a buffer as a single string. + * K_SPECIAL and CSI in the returned string are escaped. + */ + static char_u * +get_buffcont(buffer, dozero) + struct buffheader *buffer; + int dozero; /* count == zero is not an error */ +{ + long_u count = 0; + char_u *p = NULL; + char_u *p2; + char_u *str; + struct buffblock *bp; + + /* compute the total length of the string */ + 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); +} + +/* + * Return the contents of the record buffer as a single string + * and clear the record buffer. + * K_SPECIAL and CSI in the returned string are escaped. + */ + char_u * +get_recorded() +{ + char_u *p; + size_t len; + + p = get_buffcont(&recordbuff, TRUE); + free_buff(&recordbuff); + + /* + * Remove the characters that were added the last time, these must be the + * (possibly mapped) characters that stopped the recording. + */ + len = STRLEN(p); + if ((int)len >= last_recorded_len) + { + len -= last_recorded_len; + p[len] = NUL; + } + + /* + * When stopping recording from Insert mode with CTRL-O q, also remove the + * CTRL-O. + */ + if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O) + p[len - 1] = NUL; + + return (p); +} + +/* + * Return the contents of the redo buffer as a single string. + * K_SPECIAL and CSI in the returned string are escaped. + */ + char_u * +get_inserted() +{ + return(get_buffcont(&redobuff, FALSE)); +} + +/* + * add string "s" after the current block of buffer "buf" + * K_SPECIAL and CSI should have been escaped already. + */ + static void +add_buff(buf, s, slen) + struct buffheader *buf; + char_u *s; + long slen; /* length of "s" or -1 */ +{ + struct buffblock *p; + long_u len; + + if (slen < 0) + slen = (long)STRLEN(s); + if (slen == 0) /* don't add empty strings */ + return; + + if (buf->bh_first.b_next == NULL) /* first add to list */ + { + buf->bh_space = 0; + buf->bh_curr = &(buf->bh_first); + } + else if (buf->bh_curr == NULL) /* buffer has already been read */ + { + EMSG(_("E222: Add to read buffer")); + return; + } + else if (buf->bh_index != 0) + STRCPY(buf->bh_first.b_next->b_str, + buf->bh_first.b_next->b_str + buf->bh_index); + buf->bh_index = 0; + + if (buf->bh_space >= (int)slen) + { + len = (long_u)STRLEN(buf->bh_curr->b_str); + STRNCPY(buf->bh_curr->b_str + len, s, slen); + buf->bh_curr->b_str[len + slen] = NUL; + buf->bh_space -= slen; + } + else + { + if (slen < MINIMAL_SIZE) + len = MINIMAL_SIZE; + else + len = slen; + p = (struct buffblock *)lalloc((long_u)(sizeof(struct buffblock) + len), + TRUE); + if (p == NULL) + return; /* no space, just forget it */ + buf->bh_space = len - slen; + STRNCPY(p->b_str, s, slen); + p->b_str[slen] = NUL; + + p->b_next = buf->bh_curr->b_next; + buf->bh_curr->b_next = p; + buf->bh_curr = p; + } + return; +} + +/* + * Add number "n" to buffer "buf". + */ + static void +add_num_buff(buf, n) + struct buffheader *buf; + long n; +{ + char_u number[32]; + + sprintf((char *)number, "%ld", n); + add_buff(buf, number, -1L); +} + +/* + * Add character 'c' to buffer "buf". + * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. + */ + static void +add_char_buff(buf, c) + struct buffheader *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) + { + /* translate special key code into three byte sequence */ + temp[0] = K_SPECIAL; + temp[1] = K_SECOND(c); + temp[2] = K_THIRD(c); + temp[3] = NUL; + } +#ifdef FEAT_GUI + else if (c == CSI) + { + /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */ + 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 +} + +/* + * Get one byte from the stuff buffer. + * If advance == TRUE go to the next char. + * No translation is done K_SPECIAL and CSI are escaped. + */ + static int +read_stuff(advance) + int advance; +{ + char_u c; + struct buffblock *curr; + + if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */ + return NUL; + + curr = stuffbuff.bh_first.b_next; + c = curr->b_str[stuffbuff.bh_index]; + + if (advance) + { + if (curr->b_str[++stuffbuff.bh_index] == NUL) + { + stuffbuff.bh_first.b_next = curr->b_next; + vim_free(curr); + stuffbuff.bh_index = 0; + } + } + return c; +} + +/* + * Prepare the stuff buffer for reading (if it contains something). + */ + static void +start_stuff() +{ + if (stuffbuff.bh_first.b_next != NULL) + { + stuffbuff.bh_curr = &(stuffbuff.bh_first); + stuffbuff.bh_space = 0; + } +} + +/* + * Return TRUE if the stuff buffer is empty. + */ + int +stuff_empty() +{ + return (stuffbuff.bh_first.b_next == NULL); +} + +/* + * Set a typeahead character that won't be flushed. + */ + void +typeahead_noflush(c) + int c; +{ + typeahead_char = c; +} + +/* + * Remove the contents of the stuff buffer and the mapped characters in the + * typeahead buffer (used in case of an error). If 'typeahead' is true, + * flush all typeahead characters (used when interrupted by a CTRL-C). + */ + void +flush_buffers(typeahead) + int typeahead; +{ + init_typebuf(); + + start_stuff(); + while (read_stuff(TRUE) != NUL) + ; + + if (typeahead) /* remove all typeahead */ + { + /* + * We have to get all characters, because we may delete the first part + * of an escape sequence. + * In an xterm we get one char at a time and we have to get them all. + */ + while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L, + typebuf.tb_change_cnt) != 0) + ; + typebuf.tb_off = MAXMAPLEN; + typebuf.tb_len = 0; + } + else /* remove mapped characters only */ + { + 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; +} + +/* + * The previous contents of the redo buffer is kept in old_redobuffer. + * This is used for the CTRL-O <.> command in insert mode. + */ + void +ResetRedobuff() +{ + if (!block_redo) + { + free_buff(&old_redobuff); + old_redobuff = redobuff; + redobuff.bh_first.b_next = NULL; + } +} + +#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO) +/* + * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff. + * Used before executing autocommands and user functions. + */ +static int save_level = 0; + + void +saveRedobuff() +{ + char_u *s; + + if (save_level++ == 0) + { + save_redobuff = redobuff; + redobuff.bh_first.b_next = NULL; + save_old_redobuff = old_redobuff; + old_redobuff.bh_first.b_next = NULL; + + /* Make a copy, so that ":normal ." in a function works. */ + s = get_buffcont(&save_redobuff, FALSE); + if (s != NULL) + { + add_buff(&redobuff, s, -1L); + vim_free(s); + } + } +} + +/* + * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff. + * Used after executing autocommands and user functions. + */ + void +restoreRedobuff() +{ + if (--save_level == 0) + { + free_buff(&redobuff); + redobuff = save_redobuff; + free_buff(&old_redobuff); + old_redobuff = save_old_redobuff; + } +} +#endif + +/* + * Append "s" to the redo buffer. + * K_SPECIAL and CSI should already have been escaped. + */ + void +AppendToRedobuff(s) + char_u *s; +{ + if (!block_redo) + add_buff(&redobuff, s, -1L); +} + +/* + * Append to Redo buffer literally, escaping special characters with CTRL-V. + * K_SPECIAL and CSI are escaped as well. + */ + void +AppendToRedobuffLit(s) + char_u *s; +{ + int c; + char_u *start; + + if (block_redo) + return; + + while (*s != NUL) + { + /* Put a string of normal characters in the redo buffer (that's + * faster). */ + start = s; + while (*s >= ' ' +#ifndef EBCDIC + && *s < DEL /* EBCDIC: all chars above space are normal */ +#endif + ) + ++s; + + /* Don't put '0' or '^' as last character, just in case a CTRL-D is + * typed next. */ + if (*s == NUL && (s[-1] == '0' || s[-1] == '^')) + --s; + if (s > start) + add_buff(&redobuff, start, (long)(s - start)); + + if (*s != NUL) + { + /* Handle a special or multibyte character. */ +#ifdef FEAT_MBYTE + if (has_mbyte) + { + c = (*mb_ptr2char)(s); + if (enc_utf8) + /* Handle composing chars as well. */ + s += utf_ptr2len_check(s); + else + s += (*mb_ptr2len_check)(s); + } + else +#endif + c = *s++; + if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^'))) + add_char_buff(&redobuff, Ctrl_V); + + /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */ + 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); + } + } +} + +/* + * Append a character to the redo buffer. + * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. + */ + void +AppendCharToRedobuff(c) + int c; +{ + if (!block_redo) + add_char_buff(&redobuff, c); +} + +/* + * Append a number to the redo buffer. + */ + void +AppendNumberToRedobuff(n) + long n; +{ + if (!block_redo) + add_num_buff(&redobuff, n); +} + +/* + * Append string "s" to the stuff buffer. + * CSI and K_SPECIAL must already have been escaped. + */ + void +stuffReadbuff(s) + char_u *s; +{ + add_buff(&stuffbuff, s, -1L); +} + + void +stuffReadbuffLen(s, len) + char_u *s; + long len; +{ + add_buff(&stuffbuff, s, len); +} + +#if defined(FEAT_EVAL) || defined(PROTO) +/* + * Stuff "s" into the stuff buffer, leaving special key codes unmodified and + * escaping other K_SPECIAL and CSI bytes. + */ + void +stuffReadbuffSpec(s) + char_u *s; +{ + while (*s != NUL) + { + if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL) + { + /* Insert special key literally. */ + stuffReadbuffLen(s, 3L); + s += 3; + } + else +#ifdef FEAT_MBYTE + stuffcharReadbuff(mb_ptr2char_adv(&s)); +#else + stuffcharReadbuff(*s++); +#endif + } +} +#endif + +/* + * Append a character to the stuff buffer. + * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. + */ + void +stuffcharReadbuff(c) + int c; +{ + add_char_buff(&stuffbuff, c); +} + +/* + * Append a number to the stuff buffer. + */ + void +stuffnumReadbuff(n) + long n; +{ + add_num_buff(&stuffbuff, n); +} + +/* + * Read a character from the redo buffer. Translates K_SPECIAL, CSI and + * multibyte characters. + * The redo buffer is left as it is. + * if init is TRUE, prepare for redo, return FAIL if nothing to redo, OK + * otherwise + * if old is TRUE, use old_redobuff instead of redobuff + */ + static int +read_redo(init, old_redo) + int init; + int old_redo; +{ + static struct buffblock *bp; + static char_u *p; + int c; +#ifdef FEAT_MBYTE + int n; + char_u buf[MB_MAXBYTES]; + 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) + { + /* Reverse the conversion done by add_char_buff() */ +#ifdef FEAT_MBYTE + /* For a multi-byte character get all the bytes and return the + * converted character. */ + 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) /* special key or escaped K_SPECIAL */ + { + c = TO_SPECIAL(p[1], p[2]); + p += 2; + } +#ifdef FEAT_GUI + if (c == CSI) /* escaped 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) /* last byte of a character */ + { + if (n != 1) + c = (*mb_ptr2char)(buf); + break; + } + c = *p; + if (c == NUL) /* cannot happen? */ + break; +#endif + } + } + + return c; +} + +/* + * Copy the rest of the redo buffer into the stuff buffer (in a slow way). + * If old_redo is TRUE, use old_redobuff instead of redobuff. + * The escaped K_SPECIAL and CSI are copied without translation. + */ + static void +copy_redo(old_redo) + int old_redo; +{ + int c; + + while ((c = read_redo(FALSE, old_redo)) != NUL) + stuffcharReadbuff(c); +} + +/* + * Stuff the redo buffer into the stuffbuff. + * Insert the redo count into the command. + * If "old_redo" is TRUE, the last but one command is repeated + * instead of the last command (inserting text). This is used for + * CTRL-O <.> in insert mode + * + * return FAIL for failure, OK otherwise + */ + int +start_redo(count, old_redo) + long count; + int old_redo; +{ + int c; + + /* init the pointers; return if nothing to redo */ + if (read_redo(TRUE, old_redo) == FAIL) + return FAIL; + + c = read_redo(FALSE, old_redo); + + /* copy the buffer name, if present */ + if (c == '"') + { + add_buff(&stuffbuff, (char_u *)"\"", 1L); + c = read_redo(FALSE, old_redo); + + /* if a numbered buffer is used, increment the number */ + if (c >= '1' && c < '9') + ++c; + add_char_buff(&stuffbuff, c); + c = read_redo(FALSE, old_redo); + } + +#ifdef FEAT_VISUAL + if (c == 'v') /* redo Visual */ + { + VIsual = curwin->w_cursor; + VIsual_active = TRUE; + VIsual_select = FALSE; + VIsual_reselect = TRUE; + redo_VIsual_busy = TRUE; + c = read_redo(FALSE, old_redo); + } +#endif + + /* try to enter the count (in place of a previous count) */ + if (count) + { + while (VIM_ISDIGIT(c)) /* skip "old" count */ + c = read_redo(FALSE, old_redo); + add_num_buff(&stuffbuff, count); + } + + /* copy from the redo buffer into the stuff buffer */ + add_char_buff(&stuffbuff, c); + copy_redo(old_redo); + return OK; +} + +/* + * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing + * the redo buffer into the stuffbuff. + * return FAIL for failure, OK otherwise + */ + int +start_redo_ins() +{ + int c; + + if (read_redo(TRUE, FALSE) == FAIL) + return FAIL; + start_stuff(); + + /* skip the count and the command character */ + while ((c = read_redo(FALSE, FALSE)) != NUL) + { + if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL) + { + if (c == 'O' || c == 'o') + stuffReadbuff(NL_STR); + break; + } + } + + /* copy the typed text from the redo buffer into the stuff buffer */ + copy_redo(FALSE); + block_redo = TRUE; + return OK; +} + + void +stop_redo_ins() +{ + block_redo = FALSE; +} + +/* + * Initialize typebuf.tb_buf to point to typebuf_init. + * alloc() cannot be used here: In out-of-memory situations it would + * be impossible to type anything. + */ + static void +init_typebuf() +{ + 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 = 0; + typebuf.tb_change_cnt = 1; + } +} + +/* + * insert a string in position 'offset' in the typeahead buffer (for "@r" + * and ":normal" command, vgetorpeek() and check_termcode()) + * + * If noremap is REMAP_YES, new string can be mapped again. + * If noremap is REMAP_NONE, new string cannot be mapped again. + * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for + * script-local mappings. + * If noremap is > 0, that many characters of the new string cannot be mapped. + * + * If nottyped is TRUE, the string does not return KeyTyped (don't use when + * offset is non-zero!). + * + * If silent is TRUE, cmd_silent is set when the characters are obtained. + * + * return FAIL for failure, OK otherwise + */ + int +ins_typebuf(str, noremap, offset, nottyped, silent) + 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); + /* + * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off] + */ + if (offset == 0 && addlen <= typebuf.tb_off) + { + typebuf.tb_off -= addlen; + mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen); + } + /* + * Need to allocate new buffer. + * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4 + * characters. We add some extra room to avoid having to allocate too + * often. + */ + else + { + newoff = MAXMAPLEN + 4; + newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4); + if (newlen < 0) /* string is getting too long */ + { + EMSG(_(e_toocompl)); /* also calls flush_buffers */ + setcursor(); + return FAIL; + } + s1 = alloc(newlen); + if (s1 == NULL) /* out of memory */ + return FAIL; + s2 = alloc(newlen); + if (s2 == NULL) /* out of memory */ + { + vim_free(s1); + return FAIL; + } + typebuf.tb_buflen = newlen; + + /* copy the old chars, before the insertion point */ + mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off, + (size_t)offset); + /* copy the new chars */ + mch_memmove(s1 + newoff + offset, str, (size_t)addlen); + /* copy the old chars, after the insertion point, including the NUL at + * the end */ + 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: do remap script-local mappings. */ + if (noremap == REMAP_SCRIPT) + val = RM_SCRIPT; + else + val = RM_NONE; + + /* + * Adjust typebuf.tb_noremap[] for the new characters: + * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are + * (sometimes) not remappable + * If noremap == REMAP_YES: all the new characters are mappable + * If noremap > 0: "noremap" characters are not remappable, the rest + * mappable + */ + 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; + + /* tb_maplen and tb_silent only remember the length of mapped and/or + * silent mappings at the start of the buffer, assuming that a mapped + * sequence doesn't result in typed characters. */ + 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) /* and not used for abbrev.s */ + typebuf.tb_no_abbr_cnt += addlen; + + return OK; +} + +/* + * Return TRUE if the typeahead buffer was changed (while waiting for a + * character to arrive). Happens when a message was received from a client. + * But check in a more generic way to avoid trouble: When "typebuf.tb_buf" + * changed it was reallocated and the old pointer can no longer be used. + * Or "typebuf.tb_off" may have been changed and we would overwrite characters + * that was just added. + */ + int +typebuf_changed(tb_change_cnt) + int tb_change_cnt; /* old value of typebuf.tb_change_cnt */ +{ + return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt +#ifdef FEAT_CLIENTSERVER + || received_from_client +#endif + )); +} + +/* + * Return TRUE if there are no characters in the typeahead buffer that have + * not been typed (result from a mapping or come from ":normal"). + */ + int +typebuf_typed() +{ + return typebuf.tb_maplen == 0; +} + +/* + * Return the number of characters that are mapped (or not typed). + */ + int +typebuf_maplen() +{ + return typebuf.tb_maplen; +} + +/* + * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset] + */ + void +del_typebuf(len, offset) + int len; + int offset; +{ + int i; + + if (len == 0) + return; /* nothing to do */ + + typebuf.tb_len -= len; + + /* + * Easy case: Just increase typebuf.tb_off. + */ + if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len) + >= 3 * MAXMAPLEN + 3) + typebuf.tb_off += len; + /* + * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[] + */ + else + { + i = typebuf.tb_off + offset; + /* + * Leave some extra room at the end to avoid reallocation. + */ + 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; + } + /* adjust typebuf.tb_buf (include the NUL at the end) */ + mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, + typebuf.tb_buf + i + len, + (size_t)(typebuf.tb_len - offset + 1)); + /* adjust typebuf.tb_noremap[] */ + 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) /* adjust tb_maplen */ + { + if (typebuf.tb_maplen < offset + len) + typebuf.tb_maplen = offset; + else + typebuf.tb_maplen -= len; + } + if (typebuf.tb_silent > offset) /* adjust tb_silent */ + { + if (typebuf.tb_silent < offset + len) + typebuf.tb_silent = offset; + else + typebuf.tb_silent -= len; + } + if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */ + { + if (typebuf.tb_no_abbr_cnt < offset + len) + typebuf.tb_no_abbr_cnt = offset; + else + typebuf.tb_no_abbr_cnt -= len; + } + +#ifdef FEAT_CLIENTSERVER + /* Reset the flag that text received from a client was inserted in the + * typeahead buffer. */ + received_from_client = FALSE; +#endif + if (++typebuf.tb_change_cnt == 0) + typebuf.tb_change_cnt = 1; +} + +/* + * Write typed characters to script file. + * If recording is on put the character in the recordbuffer. + */ + static void +gotchars(s, len) + char_u *s; + int len; +{ + int c; + char_u buf[2]; + + /* remember how many chars were last recorded */ + if (Recording) + last_recorded_len += len; + + buf[1] = NUL; + while (len--) + { + /* Handle one byte at a time; no translation to be done. */ + c = *s++; + updatescript(c); + + if (Recording) + { + buf[0] = c; + add_buff(&recordbuff, buf, 1L); + } + } + may_sync_undo(); + +#ifdef FEAT_EVAL + /* output "debug mode" message next time in debug mode */ + debug_did_msg = FALSE; +#endif + + /* Since characters have been typed, consider the following to be in + * another mapping. Search string will be kept in history. */ + ++maptick; +} + +/* + * Sync undo. Called when typed characters are obtained from the typeahead + * buffer, or when a menu is used. + * Do not sync: + * - In Insert mode, unless cursor key has been used. + * - While reading a script file. + * - When no_u_sync is non-zero. + */ + static void +may_sync_undo() +{ + if ((!(State & (INSERT + CMDLINE)) || arrow_used) + && scriptin[curscript] == NULL && no_u_sync == 0) + u_sync(); +} + +/* + * Make "typebuf" empty and allocate new buffers. + * Returns FAIL when out of memory. + */ + int +alloc_typebuf() +{ + 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 = 0; + 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; +} + |