diff options
| author | Eliseo Martínez <eliseomarmol@gmail.com> | 2014-05-12 02:25:17 +0200 |
|---|---|---|
| committer | Eliseo Martínez <eliseomarmol@gmail.com> | 2014-05-15 20:46:01 +0200 |
| commit | da51dc9cf202772f60bd2da975dbef257bd9237c (patch) | |
| tree | 5c16b93238a153f55634e9323077f30c8133970c /src/getchar.c | |
| parent | ffe61e5ba1721340ca51d56bae3ddaca415fb5bc (diff) | |
| download | rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.tar.gz rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.tar.bz2 rneovim-da51dc9cf202772f60bd2da975dbef257bd9237c.zip | |
Introduce nvim namespace: Move files.
Move files from src/ to src/nvim/.
- src/nvim/ becomes the new root dir for nvim executable sources.
- src/libnvim/ is planned to become root dir of the neovim library.
Diffstat (limited to 'src/getchar.c')
| -rw-r--r-- | src/getchar.c | 4333 |
1 files changed, 0 insertions, 4333 deletions
diff --git a/src/getchar.c b/src/getchar.c deleted file mode 100644 index 99dbc11e4d..0000000000 --- a/src/getchar.c +++ /dev/null @@ -1,4333 +0,0 @@ -/* - * 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 <string.h> - -#include "vim.h" -#include "getchar.h" -#include "charset.h" -#include "edit.h" -#include "eval.h" -#include "ex_docmd.h" -#include "ex_getln.h" -#include "farsi.h" -#include "main.h" -#include "mbyte.h" -#include "memline.h" -#include "memory.h" -#include "message.h" -#include "misc1.h" -#include "misc2.h" -#include "keymap.h" -#include "garray.h" -#include "move.h" -#include "normal.h" -#include "ops.h" -#include "option.h" -#include "regexp.h" -#include "screen.h" -#include "term.h" -#include "ui.h" -#include "undo.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 characters: 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 buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0}; -static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; -static buffheader_T save_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; -static buffheader_T save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; -static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0}; - -// First read ahead buffer. Used for translated commands. -static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0}; - -// Second read ahead buffer. Used for redo. -static buffheader_T readbuf2 = {{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 + SELECTMODE + \ - 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 = 0; /* remapping flags */ - -/* - * 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 */ -#define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */ - -/* 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(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 char_u *eval_map_expr(char_u *str, int c); -static bool is_user_input(int k); - -/* - * Free and clear a buffer. - */ -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; - 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(buffheader_T *buffer, - int dozero // count == zero is not an error - ) -{ - size_t count = 0; - char_u *p = NULL; - char_u *p2; - char_u *str; - - /* compute the total length of the string */ - for (buffblock_T *bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) - count += STRLEN(bp->b_str); - - if (count || dozero) { - p = xmalloc(count + 1); - p2 = p; - for (buffblock_T *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(void) -{ - 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(void) -{ - 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 ( - buffheader_T *buf, - char_u *s, - long slen /* length of "s" or -1 */ -) -{ - 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) - 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; - - ssize_t len; - if (buf->bh_space >= (int)slen) { - len = 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; - buffblock_T *p = xmalloc(sizeof(buffblock_T) + len); - 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; -} - -/* - * Add number "n" to buffer "buf". - */ -static void add_num_buff(buffheader_T *buf, long n) -{ - char_u number[32]; - - sprintf((char *)number, "%" PRId64, (int64_t)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(buffheader_T *buf, int c) -{ - char_u bytes[MB_MAXBYTES + 1]; - int len; - int i; - char_u temp[4]; - - 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]; - - 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; - } else { - temp[0] = c; - temp[1] = NUL; - } - add_buff(buf, temp, -1L); - } -} - -/* - * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2 - * if that one is empty. - * If advance == TRUE go to the next char. - * No translation is done K_SPECIAL and CSI are escaped. - */ -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) /* buffer is empty */ - 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; - free(curr); - buf->bh_index = 0; - } - } - return c; -} - -/* - * Prepare the read buffers for reading (if they contain something). - */ -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; - } -} - -/* - * Return TRUE if the stuff buffer is empty. - */ -int stuff_empty(void) -{ - return (readbuf1.bh_first.b_next == NULL && readbuf2.bh_first.b_next == NULL); -} - -/* - * Return TRUE if readbuf1 is empty. There may still be redo characters in - * redbuf2. - */ -int readbuf1_empty() -{ - return (readbuf1.bh_first.b_next == NULL); -} - -/* - * Set a typeahead character that won't be flushed. - */ -void typeahead_noflush(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 "flush_typeahead" is true, - * flush all typeahead characters (used when interrupted by a CTRL-C). - */ -void flush_buffers(int flush_typeahead) -{ - init_typebuf(); - - start_stuff(); - while (read_readbuffers(TRUE) != NUL) { - } - - if (flush_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 at the start 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(void) -{ - if (!block_redo) { - free_buff(&old_redobuff); - old_redobuff = redobuff; - redobuff.bh_first.b_next = NULL; - } -} - -/* - * Discard the contents of the redo buffer and restore the previous redo - * buffer. - */ -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) { - } - } -} - -/* - * 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(void) -{ - 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); - free(s); - } - } -} - -/* - * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff. - * Used after executing autocommands and user functions. - */ -void restoreRedobuff(void) -{ - if (--save_level == 0) { - free_buff(&redobuff); - redobuff = save_redobuff; - free_buff(&old_redobuff); - old_redobuff = save_old_redobuff; - } -} - -/* - * Append "s" to the redo buffer. - * K_SPECIAL and CSI should already have been escaped. - */ -void AppendToRedobuff(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 ( - char_u *str, - int len /* length of "str" or -1 for up to the NUL */ -) -{ - char_u *s = str; - int c; - char_u *start; - - if (block_redo) - return; - - while (len < 0 ? *s != NUL : s - str < len) { - /* Put a string of normal characters in the redo buffer (that's - * faster). */ - start = s; - while (*s >= ' ' - && *s < DEL /* EBCDIC: all chars above space are normal */ - && (len < 0 || s - str < len)) - ++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 || (len >= 0 && s - str >= len)) - break; - - /* Handle a special or multibyte character. */ - if (has_mbyte) - /* Handle composing chars separately. */ - c = mb_cptr2char_adv(&s); - else - 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') - add_buff(&redobuff, (char_u *)"048", 3L); - 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(int c) -{ - if (!block_redo) - add_char_buff(&redobuff, c); -} - -/* - * Append a number to the redo buffer. - */ -void AppendNumberToRedobuff(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(char_u *s) -{ - add_buff(&readbuf1, s, -1L); -} - -void stuffReadbuffLen(char_u *s, long len) -{ - add_buff(&readbuf1, s, len); -} - -/* - * Stuff "s" into the stuff buffer, leaving special key codes unmodified and - * escaping other K_SPECIAL and CSI bytes. - * Change CR, LF and ESC into a space. - */ -void stuffReadbuffSpec(char_u *s) -{ - int c; - - while (*s != NUL) { - if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL) { - /* Insert special key literally. */ - stuffReadbuffLen(s, 3L); - s += 3; - } else { - c = mb_ptr2char_adv(&s); - if (c == CAR || c == NL || c == ESC) - c = ' '; - stuffcharReadbuff(c); - } - } -} - -/* - * Append a character to the stuff buffer. - * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. - */ -void stuffcharReadbuff(int c) -{ - add_char_buff(&readbuf1, c); -} - -/* - * Append a number to the stuff buffer. - */ -void stuffnumReadbuff(long n) -{ - add_num_buff(&readbuf1, 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(int init, int old_redo) -{ - static buffblock_T *bp; - static char_u *p; - int c; - int n; - char_u buf[MB_MAXBYTES + 1]; - int i; - - 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() */ - /* 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) { - if (c == K_SPECIAL) { /* special key or escaped K_SPECIAL */ - c = TO_SPECIAL(p[1], p[2]); - p += 2; - } - if (*++p == NUL && bp->b_next != NULL) { - bp = bp->b_next; - p = bp->b_str; - } - 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; - } - } - - 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(int old_redo) -{ - int c; - - while ((c = read_redo(FALSE, old_redo)) != NUL) { - add_char_buff(&readbuf2, c); - } -} - -/* - * Stuff the redo buffer into readbuf2. - * 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(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(&readbuf2, (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(&readbuf2, c); - c = read_redo(FALSE, old_redo); - } - - 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); - } - - /* 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(&readbuf2, count); - } - - /* copy from the redo buffer into the stuff buffer */ - add_char_buff(&readbuf2, 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 readbuf2. - * return FAIL for failure, OK otherwise - */ -int start_redo_ins(void) -{ - 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') { - add_buff(&readbuf2, NL_STR, -1L); - } - 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(void) -{ - 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(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 = 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_SKIP, fist char of new string cannot be mapped again, - * but abbreviations are allowed. - * 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(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; - memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen); - } - /* - * Need to allocate a 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 */ - free(s1); - return FAIL; - } - typebuf.tb_buflen = newlen; - - /* copy the old chars, before the insertion point */ - memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off, - (size_t)offset); - /* copy the new chars */ - memmove(s1 + newoff + offset, str, (size_t)addlen); - /* copy the old chars, after the insertion point, including the NUL at - * the end */ - 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) - free(typebuf.tb_buf); - typebuf.tb_buf = s1; - - memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off, - (size_t)offset); - memmove(s2 + newoff + offset + addlen, - typebuf.tb_noremap + typebuf.tb_off + offset, - (size_t)(typebuf.tb_len - offset)); - if (typebuf.tb_noremap != noremapbuf_init) - 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 if (noremap == REMAP_SKIP) - val = RM_ABBR; - 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 == 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; - - /* 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; -} - -/* - * Put character "c" back into the typeahead buffer. - * Can be used for a character obtained by vgetc() that needs to be put back. - * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to - * the char. - */ -void ins_char_typebuf(int c) -{ - char_u buf[MB_MAXBYTES + 1]; - if (IS_SPECIAL(c)) { - buf[0] = K_SPECIAL; - buf[1] = K_SECOND(c); - buf[2] = K_THIRD(c); - buf[3] = NUL; - } else { - buf[(*mb_char2bytes)(c, buf)] = NUL; - } - (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent); -} - -/* - * Return TRUE if the typeahead buffer was changed (while waiting for a - * character to arrive). Happens when a message was received from a client or - * from feedkeys(). - * 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 ( - int tb_change_cnt /* old value of typebuf.tb_change_cnt */ -) -{ - return tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt - || typebuf_was_filled - ); -} - -/* - * 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(void) -{ - return typebuf.tb_maplen == 0; -} - -/* - * Return the number of characters that are mapped (or not typed). - */ -int typebuf_maplen(void) -{ - return typebuf.tb_maplen; -} - -/* - * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset] - */ -void del_typebuf(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) { - memmove(typebuf.tb_buf + MAXMAPLEN, - typebuf.tb_buf + typebuf.tb_off, (size_t)offset); - 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) */ - memmove(typebuf.tb_buf + typebuf.tb_off + offset, - typebuf.tb_buf + i + len, - (size_t)(typebuf.tb_len - offset + 1)); - /* adjust typebuf.tb_noremap[] */ - 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; - } - - /* Reset the flag that text received from a client or from feedkeys() - * was inserted in the typeahead buffer. */ - typebuf_was_filled = FALSE; - 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(char_u *chars, int len) -{ - char_u *s = chars; - int c; - char_u buf[2]; - int todo = len; - - /* remember how many chars were last recorded */ - if (Recording) - last_recorded_len += len; - - buf[1] = NUL; - while (todo--) { - /* 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(); - - /* output "debug mode" message next time in debug mode */ - debug_did_msg = FALSE; - - /* 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. |