#include "vim.h"
#ifdef FEAT_INS_EXPAND
#define CTRL_X_WANT_IDENT 0x100
#define CTRL_X_NOT_DEFINED_YET 1
#define CTRL_X_SCROLL 2
#define CTRL_X_WHOLE_LINE 3
#define CTRL_X_FILES 4
#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
#define CTRL_X_FINISHED 8
#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
#define CTRL_X_CMDLINE 11
#define CTRL_X_FUNCTION 12
#define CTRL_X_OMNI 13
#define CTRL_X_SPELL 14
#define CTRL_X_LOCAL_MSG 15
#define CTRL_X_EVAL 16
#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
static char *ctrl_x_msgs[] =
{
N_(" Keyword completion (^N^P)"),
N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
NULL,
N_(" Whole line completion (^L^N^P)"),
N_(" File name completion (^F^N^P)"),
N_(" Tag completion (^]^N^P)"),
N_(" Path pattern completion (^N^P)"),
N_(" Definition completion (^D^N^P)"),
NULL,
N_(" Dictionary completion (^K^N^P)"),
N_(" Thesaurus completion (^T^N^P)"),
N_(" Command-line completion (^V^N^P)"),
N_(" User defined completion (^U^N^P)"),
N_(" Omni completion (^O^N^P)"),
N_(" Spelling suggestion (s^N^P)"),
N_(" Keyword Local completion (^N^P)"),
NULL,
};
static char e_hitend[] = N_("Hit end of paragraph");
#ifdef FEAT_COMPL_FUNC
static char e_complwin[] = N_("E839: Completion function changed window");
static char e_compldel[] = N_("E840: Completion function deleted text");
#endif
typedef struct compl_S compl_T;
struct compl_S
{
compl_T *cp_next;
compl_T *cp_prev;
char_u *cp_str;
char cp_icase;
char_u *(cp_text[CPT_COUNT]);
char_u *cp_fname;
int cp_flags;
int cp_number;
};
#define ORIGINAL_TEXT (1)
#define FREE_FNAME (2)
static compl_T *compl_first_match = NULL;
static compl_T *compl_curr_match = NULL;
static compl_T *compl_shown_match = NULL;
static compl_T *compl_old_match = NULL;
static int compl_enter_selects = FALSE;
static char_u *compl_leader = NULL;
static int compl_get_longest = FALSE;
static int compl_no_insert = FALSE;
static int compl_no_select = FALSE;
static int compl_used_match;
static int compl_was_interrupted = FALSE;
static int compl_restarting = FALSE;
static int compl_started = FALSE;
static int compl_busy = FALSE;
static int compl_matches = 0;
static char_u *compl_pattern = NULL;
static int compl_direction = FORWARD;
static int compl_shows_dir = FORWARD;
static int compl_pending = 0;
static pos_T compl_startpos;
static colnr_T compl_col = 0;
static char_u *compl_orig_text = NULL;
static int compl_cont_mode = 0;
static expand_T compl_xp;
static int compl_opt_refresh_always = FALSE;
static void ins_ctrl_x(void);
static int has_compl_option(int dict_opt);
static int ins_compl_accept_char(int c);
static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
static int ins_compl_equal(compl_T *match, char_u *str, int len);
static void ins_compl_longest_match(compl_T *match);
static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
static int ins_compl_make_cyclic(void);
static void ins_compl_upd_pum(void);
static void ins_compl_del_pum(void);
static int pum_wanted(void);
static int pum_enough_matches(void);
static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
static char_u *find_line_end(char_u *ptr);
static void ins_compl_free(void);
static void ins_compl_clear(void);
static int ins_compl_bs(void);
static int ins_compl_need_restart(void);
static void ins_compl_new_leader(void);
static void ins_compl_addleader(int c);
static int ins_compl_len(void);
static void ins_compl_restart(void);
static void ins_compl_set_original_text(char_u *str);
static void ins_compl_addfrommatch(void);
static int ins_compl_prep(int c);
static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
static void ins_compl_add_list(list_T *list);
static void ins_compl_add_dict(dict_T *dict);
#endif
static int ins_compl_get_exp(pos_T *ini);
static void ins_compl_delete(void);
static void ins_compl_insert(int in_compl_func);
static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
static int ins_compl_key2dir(int c);
static int ins_compl_pum_key(int c);
static int ins_compl_key2count(int c);
static int ins_compl_use_match(int c);
static int ins_complete(int c, int enable_pum);
static void show_pum(int prev_w_wrow, int prev_w_leftcol);
static unsigned quote_meta(char_u *dest, char_u *str, int len);
#endif
#define BACKSPACE_CHAR 1
#define BACKSPACE_WORD 2
#define BACKSPACE_WORD_NOT_SPACE 3
#define BACKSPACE_LINE 4
static void ins_redraw(int ready);
static void ins_ctrl_v(void);
static void undisplay_dollar(void);
static void insert_special(int, int, int);
static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
static void check_auto_format(int);
static void redo_literal(int c);
static void start_arrow(pos_T *end_insert_pos);
static void start_arrow_with_change(pos_T *end_insert_pos, int change);
static void start_arrow_common(pos_T *end_insert_pos, int change);
#ifdef FEAT_SPELL
static void check_spell_redraw(void);
static void spell_back_to_badword(void);
static int spell_bad_len = 0;
#endif
static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
static int echeck_abbr(int);
static int replace_pop(void);
static void replace_join(int off);
static void replace_pop_ins(void);
#ifdef FEAT_MBYTE
static void mb_replace_pop_ins(int cc);
#endif
static void replace_flush(void);
static void replace_do_bs(int limit_col);
static int del_char_after_col(int limit_col);
#ifdef FEAT_CINDENT
static int cindent_on(void);
#endif
static void ins_reg(void);
static void ins_ctrl_g(void);
static void ins_ctrl_hat(void);
static int ins_esc(long *count, int cmdchar, int nomove);
#ifdef FEAT_RIGHTLEFT
static void ins_ctrl_(void);
#endif
static int ins_start_select(int c);
static void ins_insert(int replaceState);
static void ins_ctrl_o(void);
static void ins_shift(int c, int lastc);
static void ins_del(void);
static int ins_bs(int c, int mode, int *inserted_space_p);
#ifdef FEAT_MOUSE
static void ins_mouse(int c);
static void ins_mousescroll(int dir);
#endif
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
static void ins_tabline(int c);
#endif
static void ins_left(int end_change);
static void ins_home(int c);
static void ins_end(int c);
static void ins_s_left(void);
static void ins_right(int end_change);
static void ins_s_right(void);
static void ins_up(int startcol);
static void ins_pageup(void);
static void ins_down(int startcol);
static void ins_pagedown(void);
#ifdef FEAT_DND
static void ins_drop(void);
#endif
static int ins_tab(void);
static int ins_eol(int c);
#ifdef FEAT_DIGRAPHS
static int ins_digraph(void);
#endif
static int ins_ctrl_ey(int tc);
#ifdef FEAT_SMARTINDENT
static void ins_try_si(int c);
#endif
static colnr_T get_nolist_virtcol(void);
#ifdef FEAT_AUTOCMD
static char_u *do_insert_char_pre(int c);
#endif
static colnr_T Insstart_textlen;
static colnr_T Insstart_blank_vcol;
static int update_Insstart_orig = TRUE;
static char_u *last_insert = NULL;
static int last_insert_skip;
static int new_insert_skip;
static int did_restart_edit;
#ifdef FEAT_CINDENT
static int can_cindent;
#endif
static int old_indent = 0;
#ifdef FEAT_RIGHTLEFT
static int revins_on;
static int revins_chars;
static int revins_legal;
static int revins_scol;
#endif
static int ins_need_undo;
static int did_add_space = FALSE;
static int dont_sync_undo = FALSE;
int
edit(
int cmdchar,
int startln,
long count)
{
int c = 0;
char_u *ptr;
int lastc = 0;
int mincol;
static linenr_T o_lnum = 0;
int i;
int did_backspace = TRUE;
#ifdef FEAT_CINDENT
int line_is_white = FALSE;
#endif
linenr_T old_topline = 0;
#ifdef FEAT_DIFF
int old_topfill = -1;
#endif
int inserted_space = FALSE;
int replaceState = REPLACE;
int nomove = FALSE;
int orig_count = count;
did_restart_edit = restart_edit;
check_for_delay(TRUE);
update_Insstart_orig = TRUE;
#ifdef HAVE_SANDBOX
if (sandbox != 0)
{
EMSG(_(e_sandbox));
return FALSE;
}
#endif
if (textlock != 0)
{
EMSG(_(e_secure));
return FALSE;
}
#ifdef FEAT_INS_EXPAND
if (compl_started || compl_busy || pum_visible())
{
EMSG(_(e_secure));
return FALSE;
}
ins_compl_clear();
#endif
#ifdef FEAT_AUTOCMD
if (cmdchar != 'r' && cmdchar != 'v')
{
pos_T save_cursor = curwin->w_cursor;
# ifdef FEAT_EVAL
if (cmdchar == 'R')
ptr = (char_u *)"r";
else if (cmdchar == 'V')
ptr = (char_u *)"v";
else
ptr = (char_u *)"i";
set_vim_var_string(VV_INSERTMODE, ptr, 1);
set_vim_var_string(VV_CHAR, NULL, -1);
# endif
apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
if (!EQUAL_POS(curwin->w_cursor, save_cursor)
# ifdef FEAT_EVAL
&& *get_vim_var_str(VV_CHAR) == NUL
# endif
&& save_cursor.lnum <= curbuf->b_ml.ml_line_count)
{
int save_state = State;
curwin->w_cursor = save_cursor;
State = INSERT;
check_cursor_col();
State = save_state;
}
}
#endif
#ifdef FEAT_CONCEAL
conceal_check_cursur_line();
#endif
#ifdef FEAT_MOUSE
if (where_paste_started.lnum != 0)
Insstart = where_paste_started;
else
#endif
{
Insstart = curwin->w_cursor;
if (startln)
Insstart.col = 0;
}
Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Insstart_blank_vcol = MAXCOL;
if (!did_ai)
ai_col = 0;
if (cmdchar != NUL && restart_edit == 0)
{
ResetRedobuff();
AppendNumberToRedobuff(count);
#ifdef FEAT_VREPLACE
if (cmdchar == 'V' || cmdchar == 'v')
{
AppendCharToRedobuff('g');
AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
}
else
#endif
{
if (cmdchar == K_PS)
AppendCharToRedobuff('a');
else
AppendCharToRedobuff(cmdchar);
if (cmdchar == 'g')
AppendCharToRedobuff('I');
else if (cmdchar == 'r' && !Unix2003_compat)
count = 1;
}
}
if (cmdchar == 'R')
{
#ifdef FEAT_FKMAP
if (p_fkmap && p_ri)
{
beep_flush();
EMSG(farsi_text_3);
State = INSERT;
}
else
#endif
State = REPLACE;
}
#ifdef FEAT_VREPLACE
else if (cmdchar == 'V' || cmdchar == 'v')
{
State = VREPLACE;
replaceState = VREPLACE;
orig_line_count = curbuf->b_ml.ml_line_count;
vr_lines_changed = 1;
}
#endif
else
State = INSERT;
stop_insert_mode = FALSE;
curs_columns(TRUE);
if (curbuf->b_p_iminsert == B_IMODE_LMAP)
State |= LANGMAP;
#ifdef USE_IM_CONTROL
im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
#endif
#ifdef FEAT_MOUSE
setmouse();
#endif
#ifdef FEAT_CMDL_INFO
clear_showcmd();
#endif
#ifdef FEAT_RIGHTLEFT
revins_on = (State == INSERT && p_ri);
if (revins_on)
undisplay_dollar();
revins_chars = 0;
revins_legal = 0;
revins_scol = -1;
#endif
if (!p_ek)
out_str(T_BD);
if (restart_edit != 0 && stuff_empty())
{
#ifdef FEAT_MOUSE
if (where_paste_started.lnum)
arrow_used = FALSE;
else
#endif
arrow_used = TRUE;
restart_edit = 0;
validate_virtcol();
update_curswant();
if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
|| curwin->w_curswant > curwin->w_virtcol)
&& *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
{
if (ptr[1] == NUL)
++curwin->w_cursor.col;
#ifdef FEAT_MBYTE
else if (has_mbyte)
{
i = (*mb_ptr2len)(ptr);
if (ptr[i] == NUL)
curwin->w_cursor.col += i;
}
#endif
}
ins_at_eol = FALSE;
}
else
arrow_used = FALSE;
need_start_insertmode = FALSE;
ins_need_undo = TRUE;
#ifdef FEAT_MOUSE
where_paste_started.lnum = 0;
#endif
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
#ifdef FEAT_FOLDING
if (!p_im && did_restart_edit == 0)
foldOpenCursor();
#endif
i = 0;
if (p_smd && msg_silent == 0)
i = showmode();
if (!p_im && did_restart_edit == 0)
change_warning(i == 0 ? 0 : i + 1);
#ifdef CURSOR_SHAPE
ui_cursor_shape();
#endif
#ifdef FEAT_DIGRAPHS
do_digraph(-1);
#endif
ptr = get_inserted();
if (ptr == NULL)
new_insert_skip = 0;
else
{
new_insert_skip = (int)STRLEN(ptr);
vim_free(ptr);
}
old_indent = 0;
for (;;)
{
#ifdef FEAT_RIGHTLEFT
if (!revins_legal)
revins_scol = -1;
else
revins_legal = 0;
#endif
if (arrow_used)
count = 0;
if (update_Insstart_orig)
Insstart_orig = Insstart;
if (stop_insert_mode
#ifdef FEAT_INS_EXPAND
&& !pum_visible()
#endif
)
{
count = 0;
goto doESCkey;
}
if (!arrow_used)
curwin->w_set_curswant = TRUE;
if (stuff_empty())
{
did_check_timestamps = FALSE;
if (need_check_timestamps)
check_timestamps(FALSE);
}
msg_scroll = FALSE;
#ifdef FEAT_GUI
if (need_mouse_correct)
gui_mouse_correct();
#endif
#ifdef FEAT_FOLDING
if (fdo_flags & FDO_INSERT)
foldOpenCursor();
if (!char_avail())
foldCheckClose();
#endif
if (curbuf->b_mod_set
&& curwin->w_p_wrap
&& !did_backspace
&& curwin->w_topline == old_topline
#ifdef FEAT_DIFF
&& curwin->w_topfill == old_topfill
#endif
)
{
mincol = curwin->w_wcol;
validate_cursor_col();
if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
&& curwin->w_wrow == W_WINROW(curwin)
+ curwin->w_height - 1 - p_so
&& (curwin->w_cursor.lnum != curwin->w_topline
#ifdef FEAT_DIFF
|| curwin->w_topfill > 0
#endif
))
{
#ifdef FEAT_DIFF
if (curwin->w_topfill > 0)
--curwin->w_topfill;
else
#endif
#ifdef FEAT_FOLDING
if (hasFolding(curwin->w_topline, NULL, &old_topline))
set_topline(curwin, old_topline + 1);
else
#endif
set_topline(curwin, curwin->w_topline + 1);
}
}
update_topline();
did_backspace = FALSE;
validate_cursor();
ins_redraw(TRUE);
#ifdef FEAT_SCROLLBIND
if (curwin->w_p_scb)
do_check_scrollbind(TRUE);
#endif
#ifdef FEAT_CURSORBIND
if (curwin->w_p_crb)
do_check_cursorbind();
#endif
update_curswant();
old_topline = curwin->w_topline;
#ifdef FEAT_DIFF
old_topfill = curwin->w_topfill;
#endif
#ifdef USE_ON_FLY_SCROLL
dont_scroll = FALSE;
#endif
if (c != K_CURSORHOLD)
lastc = c;
if (dont_sync_undo == MAYBE)
dont_sync_undo = TRUE;
else
dont_sync_undo = FALSE;
if (cmdchar == K_PS)
c = K_PS;
else
do
{
c = safe_vgetc();
} while (c == K_IGNORE);
#ifdef FEAT_AUTOCMD
did_cursorhold = TRUE;
#endif
#ifdef FEAT_RIGHTLEFT
if (p_hkmap && KeyTyped)
c = hkmap(c);
#endif
#ifdef FEAT_FKMAP
if (p_fkmap && KeyTyped)
c = fkmap(c);
#endif
#ifdef FEAT_INS_EXPAND
if (compl_started
&& pum_wanted()
&& curwin->w_cursor.col >= compl_col
&& (compl_shown_match == NULL
|| compl_shown_match != compl_shown_match->cp_next))
{
if ((c == K_BS || c == Ctrl_H)
&& curwin->w_cursor.col > compl_col
&& (c = ins_compl_bs()) == NUL)
continue;
if (!compl_used_match)
{
if (c == Ctrl_L
&& (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
|| (int)STRLEN(compl_shown_match->cp_str)
> curwin->w_cursor.col - compl_col))
{
ins_compl_addfrommatch();
continue;
}
if (ins_compl_accept_char(c))
{
#ifdef FEAT_AUTOCMD
char_u *str = do_insert_char_pre(c);
char_u *p;
if (str != NULL)
{
for (p = str; *p != NUL; MB_PTR_ADV(p))
ins_compl_addleader(PTR2CHAR(p));
vim_free(str);
}
else
#endif
ins_compl_addleader(c);
continue;
}
if ((c == Ctrl_Y || (compl_enter_selects
&& (c == CAR || c == K_KENTER || c == NL)))
&& stop_arrow() == OK)
{
ins_compl_delete();
ins_compl_insert(FALSE);
}
}
}
compl_get_longest = FALSE;
if (ins_compl_prep(c))
continue;
#endif
if (c == Ctrl_BSL)
{
ins_redraw(FALSE);
++no_mapping;
++allow_keys;
c = plain_vgetc();
--no_mapping;
--allow_keys;
if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
{
vungetc(c);
c = Ctrl_BSL;
}
else if (c == Ctrl_G && p_im)
continue;
else
{
if (c == Ctrl_O)
{
ins_ctrl_o();
ins_at_eol = FALSE;
nomove = TRUE;
}
count = 0;
goto doESCkey;
}
}
#ifdef FEAT_DIGRAPHS
c = do_digraph(c);
#endif
#ifdef FEAT_INS_EXPAND
if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
goto docomplete;
#endif
if (c == Ctrl_V || c == Ctrl_Q)
{
ins_ctrl_v();
c = Ctrl_V;
continue;
}
#ifdef FEAT_CINDENT
if (cindent_on()
# ifdef FEAT_INS_EXPAND
&& ctrl_x_mode == 0
# endif
)
{
line_is_white = inindent(0);
if (in_cinkeys(c, '!', line_is_white))
goto force_cindent;
if (can_cindent && in_cinkeys(c, '*', line_is_white)
&& stop_arrow() == OK)
do_c_expr_indent();
}
#endif
#ifdef FEAT_RIGHTLEFT
if (curwin->w_p_rl)
switch (c)
{
case K_LEFT: c = K_RIGHT; break;
case K_S_LEFT: c = K_S_RIGHT; break;
case K_C_LEFT: c = K_C_RIGHT; break;
case K_RIGHT: c = K_LEFT; break;
case K_S_RIGHT: c = K_S_LEFT; break;
case K_C_RIGHT: c = K_C_LEFT; break;
}
#endif
if (ins_start_select(c))
continue;
switch (c)
{
case ESC:
if (echeck_abbr(ESC + ABBR_OFF))
break;
case Ctrl_C:
#ifdef FEAT_CMDWIN
if (c == Ctrl_C && cmdwin_type != 0)
{
cmdwin_result = K_IGNORE;
got_int = FALSE;
nomove = TRUE;
goto doESCkey;
}
#endif
#ifdef UNIX
do_intr:
#endif
if (goto_im())
{
if (got_int)
{
(void)vgetc();
got_int = FALSE;
}
else
vim_beep(BO_IM);
break;
}
doESCkey:
if (ins_at_eol && gchar_cursor() == NUL)
o_lnum = curwin->w_cursor.lnum;
if (ins_esc(&count, cmdchar, nomove))
{
#ifdef FEAT_AUTOCMD
if (cmdchar != 'r' && cmdchar != 'v')
apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
FALSE, curbuf);
did_cursorhold = FALSE;
#endif
return (c == Ctrl_O);
}
continue;
case Ctrl_Z:
if (!p_im)
goto normalchar;
do_cmdline_cmd((char_u *)"stop");
#ifdef CURSOR_SHAPE
ui_cursor_shape();
#endif
continue;
case Ctrl_O:
#ifdef FEAT_COMPL_FUNC
if (ctrl_x_mode == CTRL_X_OMNI)
goto docomplete;
#endif
if (echeck_abbr(Ctrl_O + ABBR_OFF))
break;
ins_ctrl_o();
#ifdef FEAT_VIRTUALEDIT
if (ve_flags & VE_ONEMORE)
{
ins_at_eol = FALSE;
nomove = TRUE;
}
#endif
count = 0;
goto doESCkey;
case K_INS:
case K_KINS:
ins_insert(replaceState);
break;
case K_SELECT:
break;
case K_HELP:
case K_F1:
case K_XF1:
stuffcharReadbuff(K_HELP);
if (p_im)
need_start_insertmode = TRUE;
goto doESCkey;
#ifdef FEAT_NETBEANS_INTG
case K_F21:
++no_mapping;
i = plain_vgetc();
--no_mapping;
netbeans_keycommand(i);
break;
#endif
case K_ZERO:
case NUL:
case Ctrl_A:
if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
&& c != Ctrl_A && !p_im)
goto doESCkey;
inserted_space = FALSE;
break;
case Ctrl_R:
ins_reg();
auto_format(FALSE, TRUE);
inserted_space = FALSE;
break;
case Ctrl_G:
ins_ctrl_g();
break;
case Ctrl_HAT:
ins_ctrl_hat();
break;
#ifdef FEAT_RIGHTLEFT
case Ctrl__:
if (!p_ari)
goto normalchar;
ins_ctrl_();
break;
#endif
case Ctrl_D:
#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
goto docomplete;
#endif
case Ctrl_T:
# ifdef FEAT_INS_EXPAND
if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
{
if (has_compl_option(FALSE))
goto docomplete;
break;
}
# endif
ins_shift(c, lastc);
auto_format(FALSE, TRUE);
inserted_space = FALSE;
break;
case K_DEL:
case K_KDEL:
ins_del();
auto_format(FALSE, TRUE);
break;
case K_BS:
case Ctrl_H:
did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
auto_format(FALSE, TRUE);
break;
case Ctrl_W:
did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
auto_format(FALSE, TRUE);
break;
case Ctrl_U:
# ifdef FEAT_COMPL_FUNC
if (ctrl_x_mode == CTRL_X_FUNCTION)
goto docomplete;
# endif
did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
auto_format(FALSE, TRUE);
inserted_space = FALSE;
break;
#ifdef FEAT_MOUSE
case K_LEFTMOUSE:
case K_LEFTMOUSE_NM:
case K_LEFTDRAG:
case K_LEFTRELEASE:
case K_LEFTRELEASE_NM:
case K_MIDDLEMOUSE:
case K_MIDDLEDRAG:
case K_MIDDLERELEASE:
case K_RIGHTMOUSE:
case K_RIGHTDRAG:
case K_RIGHTRELEASE:
case K_X1MOUSE:
case K_X1DRAG:
case K_X1RELEASE:
case K_X2MOUSE:
case K_X2DRAG:
case K_X2RELEASE:
ins_mouse(c);
break;
case K_MOUSEDOWN:
ins_mousescroll(MSCR_DOWN);
break;
case K_MOUSEUP:
ins_mousescroll(MSCR_UP);
break;
case K_MOUSELEFT:
ins_mousescroll(MSCR_LEFT);
break;
case K_MOUSERIGHT:
ins_mousescroll(MSCR_RIGHT);
break;
#endif
case K_PS:
bracketed_paste(PASTE_INSERT, FALSE, NULL);
if (cmdchar == K_PS)
goto doESCkey;
break;
case K_PE:
break;
#ifdef FEAT_GUI_TABLINE
case K_TABLINE:
case K_TABMENU:
ins_tabline(c);
break;
#endif
case K_IGNORE:
break;
#ifdef FEAT_AUTOCMD
case K_CURSORHOLD:
apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
did_cursorhold = TRUE;
break;
#endif
#ifdef FEAT_GUI_W32
case K_F4:
if (mod_mask != MOD_MASK_ALT)
goto normalchar;
break;
#endif
#ifdef FEAT_GUI
case K_VER_SCROLLBAR:
ins_scroll();
break;
case K_HOR_SCROLLBAR:
ins_horscroll();
break;
#endif
case K_HOME:
case K_KHOME:
case K_S_HOME:
case K_C_HOME:
ins_home(c);
break;
case K_END:
case K_KEND:
case K_S_END:
case K_C_END:
ins_end(c);
break;
case K_LEFT:
if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
ins_s_left();
else
ins_left(dont_sync_undo == FALSE);
break;
case K_S_LEFT:
case K_C_LEFT:
ins_s_left();
break;
case K_RIGHT:
if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
ins_s_right();
else
ins_right(dont_sync_undo == FALSE);
break;
case K_S_RIGHT:
case K_C_RIGHT:
ins_s_right();
break;
case K_UP:
#ifdef FEAT_INS_EXPAND
if (pum_visible())
goto docomplete;
#endif
if (mod_mask & MOD_MASK_SHIFT)
ins_pageup();
else
ins_up(FALSE);
break;
case K_S_UP:
case K_PAGEUP:
case K_KPAGEUP:
#ifdef FEAT_INS_EXPAND
if (pum_visible())
goto docomplete;
#endif
ins_pageup();
break;
case K_DOWN:
#ifdef FEAT_INS_EXPAND
if (pum_visible())
goto docomplete;
#endif
if (mod_mask & MOD_MASK_SHIFT)
ins_pagedown();
else
ins_down(FALSE);
break;
case K_S_DOWN:
case K_PAGEDOWN:
case K_KPAGEDOWN:
#ifdef FEAT_INS_EXPAND
if (pum_visible())
goto docomplete;
#endif
ins_pagedown();
break;
#ifdef FEAT_DND
case K_DROP:
ins_drop();
break;
#endif
case K_S_TAB:
c = TAB;
case TAB:
#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
goto docomplete;
#endif
inserted_space = FALSE;
if (ins_tab())
goto normalchar;
auto_format(FALSE, TRUE);
break;
case K_KENTER:
c = CAR;
case CAR:
case NL:
#if defined(FEAT_QUICKFIX)
if (bt_quickfix(curbuf) && c == CAR)
{
if (curwin->w_llist_ref == NULL)
do_cmdline_cmd((char_u *)".cc");
else
do_cmdline_cmd((char_u *)".ll");
break;
}
#endif
#ifdef FEAT_CMDWIN
if (cmdwin_type != 0)
{
cmdwin_result = CAR;
goto doESCkey;
}
#endif
if (Unix2003_compat && (cmdchar == 'r') && (orig_count > 1))
did_ai = TRUE;
if (ins_eol(c) && !p_im)
goto doESCkey;
auto_format(FALSE, FALSE);
inserted_space = FALSE;
break;
#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
case Ctrl_K:
# ifdef FEAT_INS_EXPAND
if (ctrl_x_mode == CTRL_X_DICTIONARY)
{
if (has_compl_option(TRUE))
goto docomplete;
break;
}
# endif
# ifdef FEAT_DIGRAPHS
c = ins_digraph();
if (c == NUL)
break;
# endif
goto normalchar;
#endif
#ifdef FEAT_INS_EXPAND
case Ctrl_X:
ins_ctrl_x();
break;
case Ctrl_RSB:
if (ctrl_x_mode != CTRL_X_TAGS)
goto normalchar;
goto docomplete;
case Ctrl_F:
if (ctrl_x_mode != CTRL_X_FILES)
goto normalchar;
goto docomplete;
case 's':
case Ctrl_S:
if (ctrl_x_mode != CTRL_X_SPELL)
goto normalchar;
goto docomplete;
#endif
case Ctrl_L:
#ifdef FEAT_INS_EXPAND
if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
#endif
{
if (p_im)
{
if (echeck_abbr(Ctrl_L + ABBR_OFF))
break;
goto doESCkey;
}
goto normalchar;
}
#ifdef FEAT_INS_EXPAND
case Ctrl_P:
case Ctrl_N:
if (*curbuf->b_p_cpt == NUL
&& ctrl_x_mode != 0
&& !(compl_cont_status & CONT_LOCAL))
goto normalchar;
docomplete:
compl_busy = TRUE;
#ifdef FEAT_FOLDING
disable_fold_update++;
#endif
if (ins_complete(c, TRUE) == FAIL)
compl_cont_status = 0;
#ifdef FEAT_FOLDING
disable_fold_update--;
#endif
compl_busy = FALSE;
break;
#endif
case Ctrl_Y:
case Ctrl_E:
c = ins_ctrl_ey(c);
break;
default:
#ifdef UNIX
if (c == intr_char)
goto do_intr;
#endif
normalchar:
#ifdef FEAT_AUTOCMD
if (!p_paste)
{
char_u *str = do_insert_char_pre(c);
char_u *p;
if (str != NULL)
{
if (*str != NUL && stop_arrow() != FAIL)
{
for (p = str; *p != NUL; MB_PTR_ADV(p))
{
c = PTR2CHAR(p);
if (c == CAR || c == K_KENTER || c == NL)
ins_eol(c);
else
ins_char(c);
}
AppendToRedobuffLit(str, -1);
}
vim_free(str);
c = NUL;
}
if (c == NUL)
break;
}
#endif
#ifdef FEAT_SMARTINDENT
ins_try_si(c);
#endif
if (c == ' ')
{
inserted_space = TRUE;
#ifdef FEAT_CINDENT
if (inindent(0))
can_cindent = FALSE;
#endif
if (Insstart_blank_vcol == MAXCOL
&& curwin->w_cursor.lnum == Insstart.lnum)
Insstart_blank_vcol = get_nolist_virtcol();
}
if (vim_iswordc(c) || (!echeck_abbr(
#ifdef FEAT_MBYTE
(has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
#endif
c) && c != Ctrl_RSB))
{
insert_special(c, FALSE, FALSE);
#ifdef FEAT_RIGHTLEFT
revins_legal++;
revins_chars++;
#endif
}
auto_format(FALSE, TRUE);
#ifdef FEAT_FOLDING
foldOpenCursor();
#endif
break;
}
#ifdef FEAT_AUTOCMD
if (c != K_CURSORHOLD
# ifdef FEAT_COMPL_FUNC
&& ctrl_x_mode == 0
# endif
)
did_cursorhold = FALSE;
#endif
if (arrow_used)
inserted_space = FALSE;
#ifdef FEAT_CINDENT
if (can_cindent && cindent_on()
# ifdef FEAT_INS_EXPAND
&& ctrl_x_mode == 0
# endif
)
{
force_cindent:
if (in_cinkeys(c, ' ', line_is_white))
{
if (stop_arrow() == OK)
do_c_expr_indent();
}
}
#endif
}
}
static void
ins_redraw(
int ready UNUSED)
{
#ifdef FEAT_CONCEAL
linenr_T conceal_old_cursor_line = 0;
linenr_T conceal_new_cursor_line = 0;
int conceal_update_lines = FALSE;
#endif
if (char_avail())
return;
#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
if (ready && (
# ifdef FEAT_AUTOCMD
has_cursormovedI()
# endif
# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
||
# endif
# ifdef FEAT_CONCEAL
curwin->w_p_cole > 0
# endif
)
# ifdef FEAT_AUTOCMD
&& !EQUAL_POS(last_cursormoved, curwin->w_cursor)
# endif
# ifdef FEAT_INS_EXPAND
&& !pum_visible()
# endif
)
{
# ifdef FEAT_SYN_HL
if (syntax_present(curwin) && must_redraw)
update_screen(0);
# endif
# ifdef FEAT_AUTOCMD
if (has_cursormovedI())
{
update_curswant();
apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
}
# endif
# ifdef FEAT_CONCEAL
if (curwin->w_p_cole > 0)
{
# ifdef FEAT_AUTOCMD
conceal_old_cursor_line = last_cursormoved.lnum;
# endif
conceal_new_cursor_line = curwin->w_cursor.lnum;
conceal_update_lines = TRUE;
}
# endif
# ifdef FEAT_AUTOCMD
last_cursormoved = curwin->w_cursor;
# endif
}
#endif
#ifdef FEAT_AUTOCMD
if (ready && has_textchangedI()
&& last_changedtick != CHANGEDTICK(curbuf)
# ifdef FEAT_INS_EXPAND
&& !pum_visible()
# endif
)
{
if (last_changedtick_buf == curbuf)
apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
last_changedtick_buf = curbuf;
last_changedtick = CHANGEDTICK(curbuf);
}
#endif
if (must_redraw)
update_screen(0);
else if (clear_cmdline || redraw_cmdline)
showmode();
# if defined(FEAT_CONCEAL)
if ((conceal_update_lines
&& (conceal_old_cursor_line != conceal_new_cursor_line
|| conceal_cursor_line(curwin)))
|| need_cursor_line_redraw)
{
if (conceal_old_cursor_line != conceal_new_cursor_line)
update_single_line(curwin, conceal_old_cursor_line);
update_single_line(curwin, conceal_new_cursor_line == 0
? curwin->w_cursor.lnum : conceal_new_cursor_line);
curwin->w_valid &= ~VALID_CROW;
}
# endif
showruler(FALSE);
setcursor();
emsg_on_display = FALSE;
}
static void
ins_ctrl_v(void)
{
int c;
int did_putchar = FALSE;
ins_redraw(FALSE);
if (redrawing() && !char_avail())
{
edit_putchar('^', TRUE);
did_putchar = TRUE;
}
AppendToRedobuff((char_u *)CTRL_V_STR);
#ifdef FEAT_CMDL_INFO
add_to_showcmd_c(Ctrl_V);
#endif
c = get_literal();
if (did_putchar)
edit_unputchar();
#ifdef FEAT_CMDL_INFO
clear_showcmd();
#endif
insert_special(c, FALSE, TRUE);
#ifdef FEAT_RIGHTLEFT
revins_chars++;
revins_legal++;
#endif
}
static int pc_status;
#define PC_STATUS_UNSET 0
#define PC_STATUS_RIGHT 1
#define PC_STATUS_LEFT 2
#define PC_STATUS_SET 3
static char_u pc_bytes[MB_MAXBYTES + 1];
static int pc_attr;
static int pc_row;
static int pc_col;
void
edit_putchar(int c, int highlight)
{
int attr;
if (ScreenLines != NULL)
{
update_topline();
validate_cursor();
if (highlight)
attr = HL_ATTR(HLF_8);
else
attr = 0;
pc_row = W_WINROW(curwin) + curwin->w_wrow;
pc_col = curwin->w_wincol;
#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
pc_status = PC_STATUS_UNSET;
#endif
#ifdef FEAT_RIGHTLEFT
if (curwin->w_p_rl)
{
pc_col += curwin->w_width - 1 - curwin->w_wcol;
# ifdef FEAT_MBYTE
if (has_mbyte)
{
int fix_col = mb_fix_col(pc_col, pc_row);
if (fix_col != pc_col)
{
screen_putchar(' ', pc_row, fix_col, attr);
--curwin->w_wcol;
pc_status = PC_STATUS_RIGHT;
}
}
# endif
}
else
#endif
{
pc_col += curwin->w_wcol;
#ifdef FEAT_MBYTE
if (mb_lefthalve(pc_row, pc_col))
pc_status = PC_STATUS_LEFT;
#endif
}
#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
if (pc_status == PC_STATUS_UNSET)
#endif
{
screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
pc_status = PC_STATUS_SET;
}
screen_putchar(c, pc_row, pc_col, attr);
}
}
void
edit_unputchar(void)
{
if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
{
#if defined(FEAT_MBYTE)
if (pc_status == PC_STATUS_RIGHT)
++curwin->w_wcol;
if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
redrawWinline(curwin->w_cursor.lnum, FALSE);
else
#endif
screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
}
}
void
display_dollar(colnr_T col)
{
colnr_T save_col;
if (!redrawing())
return;
cursor_off();
save_col = curwin->w_cursor.col;
curwin->w_cursor.col = col;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
char_u *p;
p = ml_get_curline();
curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
}
#endif
curs_columns(FALSE);
if (curwin->w_wcol < curwin->w_width)
{
edit_putchar('$', FALSE);
dollar_vcol = curwin->w_virtcol;
}
curwin->w_cursor.col = save_col;
}
static void
undisplay_dollar(void)
{
if (dollar_vcol >= 0)
{
dollar_vcol = -1;
redrawWinline(curwin->w_cursor.lnum, FALSE);
}
}
void
change_indent(
int type,
int amount,
int round,
int replaced,
int call_changed_bytes)
{
int vcol;
int last_vcol;
int insstart_less;
int new_cursor_col;
int i;
char_u *ptr;
int save_p_list;
int start_col;
colnr_T vc;
#ifdef FEAT_VREPLACE
colnr_T orig_col = 0;
char_u *new_line, *orig_line = NULL;
if (State & VREPLACE_FLAG)
{
orig_line = vim_strsave(ml_get_curline());
orig_col = curwin->w_cursor.col;
}
#endif
save_p_list = curwin->w_p_list;
curwin->w_p_list = FALSE;
vc = getvcol_nolist(&curwin->w_cursor);
vcol = vc;
start_col = curwin->w_cursor.col;
new_cursor_col = curwin->w_cursor.col;
beginline(BL_WHITE);
new_cursor_col -= curwin->w_cursor.col;
insstart_less = curwin->w_cursor.col;
if (new_cursor_col < 0)
vcol = get_indent() - vcol;
if (new_cursor_col > 0)
start_col = -1;
if (type == INDENT_SET)
(void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
else
{
#ifdef FEAT_VREPLACE
int save_State = State;
if (State & VREPLACE_FLAG)
State = INSERT;
#endif
shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
#ifdef FEAT_VREPLACE
State = save_State;
#endif
}
insstart_less -= curwin->w_cursor.col;
if (new_cursor_col >= 0)
{
if (new_cursor_col == 0)
insstart_less = MAXCOL;
new_cursor_col += curwin->w_cursor.col;
}
else if (!(State & INSERT))
new_cursor_col = curwin->w_cursor.col;
else
{
vcol = get_indent() - vcol;
curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
vcol = last_vcol = 0;
new_cursor_col = -1;
ptr = ml_get_curline();
while (vcol <= (int)curwin->w_virtcol)
{
last_vcol = vcol;
#ifdef FEAT_MBYTE
if (has_mbyte && new_cursor_col >= 0)
new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
else
#endif
++new_cursor_col;
vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
}
vcol = last_vcol;
if (vcol != (int)curwin->w_virtcol)
{
curwin->w_cursor.col = (colnr_T)new_cursor_col;
i = (int)curwin->w_virtcol - vcol;
ptr = alloc((unsigned)(i + 1));
if (ptr != NULL)
{
new_cursor_col += i;
ptr[i] = NUL;
while (--i >= 0)
ptr[i] = ' ';
ins_str(ptr);
vim_free(ptr);
}
}
insstart_less = MAXCOL;
}
curwin->w_p_list = save_p_list;
if (new_cursor_col <= 0)
curwin->w_cursor.col = 0;
else
curwin->w_cursor.col = (colnr_T)new_cursor_col;
curwin->w_set_curswant = TRUE;
changed_cline_bef_curs();
if (State & INSERT)
{
if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
{
if ((int)Insstart.col <= insstart_less)
Insstart.col = 0;
else
Insstart.col -= insstart_less;
}
if ((int)ai_col <= insstart_less)
ai_col = 0;
else
ai_col -= insstart_less;
}
if (REPLACE_NORMAL(State) && start_col >= 0)
{
while (start_col > (int)curwin->w_cursor.col)
{
replace_join(0);
--start_col;
}
while (start_col < (int)curwin->w_cursor.col || replaced)
{
replace_push(NUL);
if (replaced)
{
replace_push(replaced);
replaced = NUL;
}
++start_col;
}
}
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
if (orig_line == NULL)
return;
new_line = vim_strsave(ml_get_curline());
if (new_line == NULL)
return;
new_line[curwin->w_cursor.col] = NUL;
ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
curwin->w_cursor.col = orig_col;
backspace_until_column(0);
ins_bytes(new_line);
vim_free(new_line);
}
#endif
}
void
truncate_spaces(char_u *line)
{
int i;
for (i = (int)STRLEN(line) - 1; i >= 0 && VIM_ISWHITE(line[i]); i--)
{
if (State & REPLACE_FLAG)
replace_join(0);
}
line[i + 1] = NUL;
}
#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
|| defined(FEAT_COMMENTS) || defined(PROTO)
void
backspace_until_column(int col)
{
while ((int)curwin->w_cursor.col > col)
{
curwin->w_cursor.col--;
if (State & REPLACE_FLAG)
replace_do_bs(col);
else if (!del_char_after_col(col))
break;
}
}
#endif
static int
del_char_after_col(int limit_col UNUSED)
{
#ifdef FEAT_MBYTE
if (enc_utf8 && limit_col >= 0)
{
colnr_T ecol = curwin->w_cursor.col + 1;
mb_adjust_cursor();
while (curwin->w_cursor.col < (colnr_T)limit_col)
{
int l = utf_ptr2len(ml_get_cursor());
if (l == 0)
break;
curwin->w_cursor.col += l;
}
if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
return FALSE;
del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
}
else
#endif
(void)del_char(FALSE);
return TRUE;
}
#if defined(FEAT_INS_EXPAND) || defined(PROTO)
static void
ins_ctrl_x(void)
{
if (ctrl_x_mode != CTRL_X_CMDLINE)
{
if (compl_cont_status & CONT_N_ADDS)
compl_cont_status |= CONT_INTRPT;
else
compl_cont_status = 0;
ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
edit_submode_pre = NULL;
showmode();
}
}
static int
has_compl_option(int dict_opt)
{
if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
# ifdef FEAT_SPELL
&& !curwin->w_p_spell
# endif
)
: (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
{
ctrl_x_mode = 0;
edit_submode = NULL;
msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
: (char_u *)_("'thesaurus' option is empty"),
HL_ATTR(HLF_E));
if (emsg_silent == 0)
{
vim_beep(BO_COMPL);
setcursor();
out_flush();
#ifdef FEAT_EVAL
if (!get_vim_var_nr(VV_TESTING))
#endif
ui_delay(2000L, FALSE);
}
return FALSE;
}
return TRUE;
}
int
vim_is_ctrl_x_key(int c)
{
if (c == Ctrl_R)
return TRUE;
if (ins_compl_pum_key(c))
return TRUE;
switch (ctrl_x_mode)
{
case 0:
return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
case CTRL_X_NOT_DEFINED_YET:
return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
|| c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
|| c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
|| c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
|| c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
|| c == Ctrl_S || c == Ctrl_K || c == 's');
case CTRL_X_SCROLL:
return (c == Ctrl_Y || c == Ctrl_E);
case CTRL_X_WHOLE_LINE:
return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_FILES:
return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_DICTIONARY:
return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_THESAURUS:
return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_TAGS:
return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
#ifdef FEAT_FIND_ID
case CTRL_X_PATH_PATTERNS:
return (c == Ctrl_P || c == Ctrl_N);
case CTRL_X_PATH_DEFINES:
return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
#endif
case CTRL_X_CMDLINE:
return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
|| c == Ctrl_X);
#ifdef FEAT_COMPL_FUNC
case CTRL_X_FUNCTION:
return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_OMNI:
return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
#endif
case CTRL_X_SPELL:
return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_EVAL:
return (c == Ctrl_P || c == Ctrl_N);
}
internal_error("vim_is_ctrl_x_key()");
return FALSE;
}
static int
ins_compl_accept_char(int c)
{
if (ctrl_x_mode & CTRL_X_WANT_IDENT)
return vim_isIDc(c);
switch (ctrl_x_mode)
{
case CTRL_X_FILES:
return vim_isfilec(c) && !vim_ispathsep(c);
case CTRL_X_CMDLINE:
case CTRL_X_OMNI:
return vim_isprintc(c) && !VIM_ISWHITE(c);
case CTRL_X_WHOLE_LINE:
return vim_isprintc(c);
}
return vim_iswordc(c);
}
int
ins_compl_add_infercase(
char_u *str,
int len,
int icase,
char_u *fname,
int dir,
int flags)
{
char_u *p;
int i, c;
int actual_len;
int actual_compl_length;
int min_len;
int *wca;
int has_lower = FALSE;
int was_letter = FALSE;
if (p_ic && curbuf->b_p_inf && len > 0)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
p = str;
actual_len = 0;
while (*p != NUL)
{
MB_PTR_ADV(p);
++actual_len;
}
}
else
#endif
actual_len = len;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
p = compl_orig_text;
actual_compl_length = 0;
while (*p != NUL)
{
MB_PTR_ADV(p);
++actual_compl_length;
}
}
else
#endif
actual_compl_length = compl_length;
min_len = actual_len < actual_compl_length
? actual_len : actual_compl_length;
wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
if (wca != NULL)
{
p = str;
for (i = 0; i < actual_len; ++i)
#ifdef FEAT_MBYTE
if (has_mbyte)
wca[i] = mb_ptr2char_adv(&p);
else
#endif
wca[i] = *(p++);
p = compl_orig_text;
for (i = 0; i < min_len; ++i)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
#endif
c = *(p++);
if (MB_ISLOWER(c))
{
has_lower = TRUE;
if (MB_ISUPPER(wca[i]))
{
for (i = actual_compl_length; i < actual_len; ++i)
wca[i] = MB_TOLOWER(wca[i]);
break;
}
}
}
if (!has_lower)
{
p = compl_orig_text;
for (i = 0; i < min_len; ++i)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
#endif
c = *(p++);
if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
{
for (i = actual_compl_length; i < actual_len; ++i)
wca[i] = MB_TOUPPER(wca[i]);
break;
}
was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
}
}
p = compl_orig_text;
for (i = 0; i < min_len; ++i)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
#endif
c = *(p++);
if (MB_ISLOWER(c))
wca[i] = MB_TOLOWER(wca[i]);
else if (MB_ISUPPER(c))
wca[i] = MB_TOUPPER(wca[i]);
}
p = IObuff;
i = 0;
while (i < actual_len && (p - IObuff + 6) < IOSIZE)
#ifdef FEAT_MBYTE
if (has_mbyte)
p += (*mb_char2bytes)(wca[i++], p);
else
#endif
*(p++) = wca[i++];
*p = NUL;
vim_free(wca);
}
return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
flags, FALSE);
}
return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
}
static int
ins_compl_add(
char_u *str,
int len,
int icase,
char_u *fname,
char_u **cptext,
int cdir,
int flags,
int adup)
{
compl_T *match;
int dir = (cdir == 0 ? compl_direction : cdir);
ui_breakcheck();
if (got_int)
return FAIL;
if (len < 0)
len = (int)STRLEN(str);
if (compl_first_match != NULL && !adup)
{
match = compl_first_match;
do
{
if ( !(match->cp_flags & ORIGINAL_TEXT)
&& STRNCMP(match->cp_str, str, len) == 0
&& match->cp_str[len] == NUL)
return NOTDONE;
match = match->cp_next;
} while (match != NULL && match != compl_first_match);
}
ins_compl_del_pum();
match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
if (match == NULL)
return FAIL;
match->cp_number = -1;
if (flags & ORIGINAL_TEXT)
match->cp_number = 0;
if ((match->cp_str = vim_strnsave(str, len)) == NULL)
{
vim_free(match);
return FAIL;
}
match->cp_icase = icase;
if (fname != NULL
&& compl_curr_match != NULL
&& compl_curr_match->cp_fname != NULL
&& STRCMP(fname, compl_curr_match->cp_fname) == 0)
match->cp_fname = compl_curr_match->cp_fname;
else if (fname != NULL)
{
match->cp_fname = vim_strsave(fname);
flags |= FREE_FNAME;
}
else
match->cp_fname = NULL;
match->cp_flags = flags;
if (cptext != NULL)
{
int i;
for (i = 0; i < CPT_COUNT; ++i)
if (cptext[i] != NULL && *cptext[i] != NUL)
match->cp_text[i] = vim_strsave(cptext[i]);
}
if (compl_first_match == NULL)
match->cp_next = match->cp_prev = NULL;
else if (dir == FORWARD)
{
match->cp_next = compl_curr_match->cp_next;
match->cp_prev = compl_curr_match;
}
else
{
match->cp_next = compl_curr_match;
match->cp_prev = compl_curr_match->cp_prev;
}
if (match->cp_next)
match->cp_next->cp_prev = match;
if (match->cp_prev)
match->cp_prev->cp_next = match;
else
compl_first_match = match;
compl_curr_match = match;
if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
ins_compl_longest_match(match);
return OK;
}
static int
ins_compl_equal(compl_T *match, char_u *str, int len)
{
if (match->cp_icase)
return STRNICMP(match->cp_str, str, (size_t)len) == 0;
return STRNCMP(match->cp_str, str, (size_t)len) == 0;
}
static void
ins_compl_longest_match(compl_T *match)
{
char_u *p, *s;
int c1, c2;
int had_match;
if (compl_leader == NULL)
{
compl_leader = vim_strsave(match->cp_str);
if (compl_leader != NULL)
{
had_match = (curwin->w_cursor.col > compl_col);
ins_compl_delete();
ins_bytes(compl_leader + ins_compl_len());
ins_redraw(FALSE);
if (!had_match)
ins_compl_delete();
compl_used_match = FALSE;
}
}
else
{
p = compl_leader;
s = match->cp_str;
while (*p != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
c1 = mb_ptr2char(p);
c2 = mb_ptr2char(s);
}
else
#endif
{
c1 = *p;
c2 = *s;
}
if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
: (c1 != c2))
break;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
MB_PTR_ADV(p);
MB_PTR_ADV(s);
}
else
#endif
{
++p;
++s;
}
}
if (*p != NUL)
{
*p = NUL;
had_match = (curwin->w_cursor.col > compl_col);
ins_compl_delete();
ins_bytes(compl_leader + ins_compl_len());
ins_redraw(FALSE);
if (!had_match)
ins_compl_delete();
}
compl_used_match = FALSE;
}
}
static void
ins_compl_add_matches(
int num_matches,
char_u **matches,
int icase)
{
int i;
int add_r = OK;
int dir = compl_direction;
for (i = 0; i < num_matches && add_r != FAIL; i++)
if ((add_r = ins_compl_add(matches[i], -1, icase,
NULL, NULL, dir, 0, FALSE)) == OK)
dir = FORWARD;
FreeWild(num_matches, matches);
}
static int
ins_compl_make_cyclic(void)
{
compl_T *match;
int count = 0;
if (compl_first_match != NULL)
{
match = compl_first_match;
while (match->cp_next != NULL && match->cp_next != compl_first_match)
{
match = match->cp_next;
++count;
}
match->cp_next = compl_first_match;
compl_first_match->cp_prev = match;
}
return count;
}
void
completeopt_was_set(void)
{
compl_no_insert = FALSE;
compl_no_select = FALSE;
if (strstr((char *)p_cot, "noselect") != NULL)
compl_no_select = TRUE;
if (strstr((char *)p_cot, "noinsert") != NULL)
compl_no_insert = TRUE;
}
void
set_completion(colnr_T startcol, list_T *list)
{
int save_w_wrow = curwin->w_wrow;
int save_w_leftcol = curwin->w_leftcol;
if (ctrl_x_mode != 0)
ins_compl_prep(' ');
ins_compl_clear();
ins_compl_free();
compl_direction = FORWARD;
if (startcol > curwin->w_cursor.col)
startcol = curwin->w_cursor.col;
compl_col = startcol;
compl_length = (int)curwin->w_cursor.col - (int)startcol;
compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
-1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
return;
ctrl_x_mode = CTRL_X_EVAL;
ins_compl_add_list(list);
compl_matches = ins_compl_make_cyclic();
compl_started = TRUE;
compl_used_match = TRUE;
compl_cont_status = 0;
compl_curr_match = compl_first_match;
if (compl_no_insert || compl_no_select)
{
ins_complete(K_DOWN, FALSE);
if (compl_no_select)
ins_complete(K_UP, FALSE);
}
else
ins_complete(Ctrl_N, FALSE);
compl_enter_selects = compl_no_insert;
if (!compl_interrupted)
show_pum(save_w_wrow, save_w_leftcol);
out_flush();
}
static pumitem_T *compl_match_array = NULL;
static int compl_match_arraysize;
static void
ins_compl_upd_pum(void)
{
int h;
if (compl_match_array != NULL)
{
h = curwin->w_cline_height;
update_screen(0);
if (h != curwin->w_cline_height)
ins_compl_del_pum();
}
}
static void
ins_compl_del_pum(void)
{
if (compl_match_array != NULL)
{
pum_undisplay();
vim_free(compl_match_array);
compl_match_array = NULL;
}
}
static int
pum_wanted(void)
{
if (vim_strchr(p_cot, 'm') == NULL)
return FALSE;
if (t_colors < 8
#ifdef FEAT_GUI
&& !gui.in_use
#endif
)
return FALSE;
return TRUE;
}
static int
pum_enough_matches(void)
{
compl_T *compl;
int i;
compl = compl_first_match;
i = 0;
do
{
if (compl == NULL
|| ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
break;
compl = compl->cp_next;
} while (compl != compl_first_match);
if (strstr((char *)p_cot, "menuone") != NULL)
return (i >= 1);
return (i >= 2);
}
void
ins_compl_show_pum(void)
{
compl_T *compl;
compl_T *shown_compl = NULL;
int did_find_shown_match = FALSE;
int shown_match_ok = FALSE;
int i;
int cur = -1;
colnr_T col;
int lead_len = 0;
if (!pum_wanted() || !pum_enough_matches())
return;
#if defined(FEAT_EVAL)
do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
#endif
update_screen(0);
if (compl_match_array == NULL)
{
compl_match_arraysize = 0;
compl = compl_first_match;
if (compl_leader != NULL)
lead_len = (int)STRLEN(compl_leader);
do
{
if ((compl->cp_flags & ORIGINAL_TEXT) == 0
&& (compl_leader == NULL
|| ins_compl_equal(compl, compl_leader, lead_len)))
++compl_match_arraysize;
compl = compl->cp_next;
} while (compl != NULL && compl != compl_first_match);
if (compl_match_arraysize == 0)
return;
compl_match_array = (pumitem_T *)alloc_clear(
(unsigned)(sizeof(pumitem_T)
* compl_match_arraysize));
if (compl_match_array != NULL)
{
if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
shown_match_ok = TRUE;
i = 0;
compl = compl_first_match;
do
{
if ((compl->cp_flags & ORIGINAL_TEXT) == 0
&& (compl_leader == NULL
|| ins_compl_equal(compl, compl_leader, lead_len)))
{
if (!shown_match_ok)
{
if (compl == compl_shown_match || did_find_shown_match)
{
compl_shown_match = compl;
did_find_shown_match = TRUE;
shown_match_ok = TRUE;
}
else
shown_compl = compl;
cur = i;
}
if (compl->cp_text[CPT_ABBR] != NULL)
compl_match_array[i].pum_text =
compl->cp_text[CPT_ABBR];
else
compl_match_array[i].pum_text = compl->cp_str;
compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
if (compl->cp_text[CPT_MENU] != NULL)
compl_match_array[i++].pum_extra =
compl->cp_text[CPT_MENU];
else
compl_match_array[i++].pum_extra = compl->cp_fname;
}
if (compl == compl_shown_match)
{
did_find_shown_match = TRUE;
if (compl->cp_flags & ORIGINAL_TEXT)
shown_match_ok = TRUE;
if (!shown_match_ok && shown_compl != NULL)
{
compl_shown_match = shown_compl;
shown_match_ok = TRUE;
}
}
compl = compl->cp_next;
} while (compl != NULL && compl != compl_first_match);
if (!shown_match_ok)
cur = -1;
}
}
else
{
for (i = 0; i < compl_match_arraysize; ++i)
if (compl_match_array[i].pum_text == compl_shown_match->cp_str
|| compl_match_array[i].pum_text
== compl_shown_match->cp_text[CPT_ABBR])
{
cur = i;
break;
}
}
if (compl_match_array != NULL)
{
dollar_vcol = -1;
col = curwin->w_cursor.col;
curwin->w_cursor.col = compl_col;
pum_display(compl_match_array, compl_match_arraysize, cur);
curwin->w_cursor.col = col;
}
}
#define DICT_FIRST (1)
#define DICT_EXACT (2)
static void
ins_compl_dictionaries(
char_u *dict_start,
char_u *pat,
int flags,
int thesaurus)
{
char_u *dict = dict_start;
char_u *ptr;
char_u *buf;
regmatch_T regmatch;
char_u **files;
int count;
int save_p_scs;
int dir = compl_direction;
if (*dict == NUL)
{
#ifdef FEAT_SPELL
if (!thesaurus && curwin->w_p_spell)
dict = (char_u *)"spell";
else
#endif
return;
}
buf = alloc(LSIZE);
if (buf == NULL)
return;
regmatch.regprog = NULL;
save_p_scs = p_scs;
if (curbuf->b_p_inf)
p_scs = FALSE;
if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
{
char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
size_t len;
if (pat_esc == NULL)
goto theend;
len = STRLEN(pat_esc) + 10;
ptr = alloc((unsigned)len);
if (ptr == NULL)
{
vim_free(pat_esc);
goto theend;
}
vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
vim_free(pat_esc);
vim_free(ptr);
}
else
{
regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
if (regmatch.regprog == NULL)
goto theend;
}
regmatch.rm_ic = ignorecase(pat);
while (*dict != NUL && !got_int && !compl_interrupted)
{
if (flags == DICT_EXACT)
{
count = 1;
files = &dict;
}
else
{
copy_option_part(&dict, buf, LSIZE, ",");
# ifdef FEAT_SPELL
if (!thesaurus && STRCMP(buf, "spell") == 0)
count = -1;
else
# endif
if (vim_strchr(buf, '`') != NULL
|| expand_wildcards(1, &buf, &count, &files,
EW_FILE|EW_SILENT) != OK)
count = 0;
}
# ifdef FEAT_SPELL
if (count == -1)
{
if (pat[0] == '\\' && pat[1] == '<')
ptr = pat + 2;
else
ptr = pat;
spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
}
else
# endif
if (count > 0)
{
ins_compl_files(count, files, thesaurus, flags,
®match, buf, &dir);
if (flags != DICT_EXACT)
FreeWild(count, files);
}
if (flags != 0)
break;
}
theend:
p_scs = save_p_scs;
vim_regfree(regmatch.regprog);
vim_free(buf);
}
static void
ins_compl_files(
int count,
char_u **files,
int thesaurus,
int flags,
regmatch_T *regmatch,
char_u *buf,
int *dir)
{
char_u *ptr;
int i;
FILE *fp;
int add_r;
for (i = 0; i < count && !got_int && !compl_interrupted; i++)
{
fp = mch_fopen((char *)files[i], "r");
if (flags != DICT_EXACT)
{
vim_snprintf((char *)IObuff, IOSIZE,
_("Scanning dictionary: %s"), (char *)files[i]);
(void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
}
if (fp != NULL)
{
while (!got_int && !compl_interrupted
&& !vim_fgets(buf, LSIZE, fp))
{
ptr = buf;
while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
{
ptr = regmatch->startp[0];
if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
ptr = find_line_end(ptr);
else
ptr = find_word_end(ptr);
add_r = ins_compl_add_infercase(regmatch->startp[0],
(int)(ptr - regmatch->startp[0]),
p_ic, files[i], *dir, 0);
if (thesaurus)
{
char_u *wstart;
ptr = buf;
while (!got_int)
{
ptr = find_word_start(ptr);
if (*ptr == NUL || *ptr == NL)
break;
wstart = ptr;
#ifdef FEAT_MBYTE
if (has_mbyte)
while (*ptr != NUL)
{
int l = (*mb_ptr2len)(ptr);
if (l < 2 && !vim_iswordc(*ptr))
break;
ptr += l;
}
else
#endif
ptr = find_word_end(ptr);
if (wstart != regmatch->startp[0])
add_r = ins_compl_add_infercase(wstart,
(int)(ptr - wstart),
p_ic, files[i], *dir, 0);
}
}
if (add_r == OK)
*dir = FORWARD;
else if (add_r == FAIL)
break;
if (*ptr == '\n' || got_int)
break;
}
line_breakcheck();
ins_compl_check_keys(50, FALSE);
}
fclose(fp);
}
}
}
char_u *
find_word_start(char_u *ptr)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
ptr += (*mb_ptr2len)(ptr);
else
#endif
while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
++ptr;
return ptr;
}
char_u *
find_word_end(char_u *ptr)
{
#ifdef FEAT_MBYTE
int start_class;
if (has_mbyte)
{
start_class = mb_get_class(ptr);
if (start_class > 1)
while (*ptr != NUL)
{
ptr += (*mb_ptr2len)(ptr);
if (mb_get_class(ptr) != start_class)
break;
}
}
else
#endif
while (vim_iswordc(*ptr))
++ptr;
return ptr;
}
static char_u *
find_line_end(char_u *ptr)
{
char_u *s;
s = ptr + STRLEN(ptr);
while (s > ptr && (s[-1] == CAR || s[-1] == NL))
--s;
return s;
}
static void
ins_compl_free(void)
{
compl_T *match;
int i;
vim_free(compl_pattern);
compl_pattern = NULL;
vim_free(compl_leader);
compl_leader = NULL;
if (compl_first_match == NULL)
return;
ins_compl_del_pum();
pum_clear();
compl_curr_match = compl_first_match;
do
{
match = compl_curr_match;
compl_curr_match = compl_curr_match->cp_next;
vim_free(match->cp_str);
if (match->cp_flags & FREE_FNAME)
vim_free(match->cp_fname);
for (i = 0; i < CPT_COUNT; ++i)
vim_free(match->cp_text[i]);
vim_free(match);
} while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
compl_first_match = compl_curr_match = NULL;
compl_shown_match = NULL;
compl_old_match = NULL;
}
static void
ins_compl_clear(void)
{
compl_cont_status = 0;
compl_started = FALSE;
compl_matches = 0;
vim_free(compl_pattern);
compl_pattern = NULL;
vim_free(compl_leader);
compl_leader = NULL;
edit_submode_extra = NULL;
vim_free(compl_orig_text);
compl_orig_text = NULL;
compl_enter_selects = FALSE;
set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
}
int
ins_compl_active(void)
{
return compl_started;
}
static int
ins_compl_bs(void)
{
char_u *line;
char_u *p;
line = ml_get_curline();
p = line + curwin->w_cursor.col;
MB_PTR_BACK(line, p);
if ((int)(p - line) - (int)compl_col < 0
|| ((int)(p - line) - (int)compl_col == 0
&& ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL
|| (!can_bs(BS_START) && (int)(p - line) - (int)compl_col
- compl_length < 0))
return K_BS;
if (curwin->w_cursor.col <= compl_col + compl_length
|| ins_compl_need_restart())
ins_compl_restart();
vim_free(compl_leader);
compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
if (compl_leader != NULL)
{
ins_compl_new_leader();
if (compl_shown_match != NULL)
compl_curr_match = compl_shown_match;
return NUL;
}
return K_BS;
}
static int
ins_compl_need_restart(void)
{
return compl_was_interrupted
|| ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
&& compl_opt_refresh_always);
}
static void
ins_compl_new_leader(void)
{
ins_compl_del_pum();
ins_compl_delete();
ins_bytes(compl_leader + ins_compl_len());
compl_used_match = FALSE;
if (compl_started)
ins_compl_set_original_text(compl_leader);
else
{
#ifdef FEAT_SPELL
spell_bad_len = 0;
#endif
update_screen(0);
#ifdef FEAT_GUI
if (gui.in_use)
{
setcursor();
out_flush();
gui_update_cursor(FALSE, FALSE);
}
#endif
compl_restarting = TRUE;
if (ins_complete(Ctrl_N, TRUE) == FAIL)
compl_cont_status = 0;
compl_restarting = FALSE;
}
compl_enter_selects = !compl_used_match;
ins_compl_show_pum();
if (compl_match_array == NULL)
compl_enter_selects = FALSE;
}
static int
ins_compl_len(void)
{
int off = (int)curwin->w_cursor.col - (int)compl_col;
if (off < 0)
return 0;
return off;
}
static void
ins_compl_addleader(int c)
{
#ifdef FEAT_MBYTE
int cc;
#endif
if (stop_arrow() == FAIL)
return;
#ifdef FEAT_MBYTE
if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
{
char_u buf[MB_MAXBYTES + 1];
(*mb_char2bytes)(c, buf);
buf[cc] = NUL;
ins_char_bytes(buf, cc);
if (compl_opt_refresh_always)
AppendToRedobuff(buf);
}
else
#endif
{
ins_char(c);
if (compl_opt_refresh_always)
AppendCharToRedobuff(c);
}
if (ins_compl_need_restart())
ins_compl_restart();
if (!compl_opt_refresh_always)
{
vim_free(compl_leader);
compl_leader = vim_strnsave(ml_get_curline() + compl_col,
(int)(curwin->w_cursor.col - compl_col));
if (compl_leader != NULL)
ins_compl_new_leader();
}
}
static void
ins_compl_restart(void)
{
ins_compl_free();
compl_started = FALSE;
compl_matches = 0;
compl_cont_status = 0;
compl_cont_mode = 0;
}
static void
ins_compl_set_original_text(char_u *str)
{
char_u *p;
if (compl_first_match->cp_flags & ORIGINAL_TEXT)
{
p = vim_strsave(str);
if (p != NULL)
{
vim_free(compl_first_match->cp_str);
compl_first_match->cp_str = p;
}
}
}
static void
ins_compl_addfrommatch(void)
{
char_u *p;
int len = (int)curwin->w_cursor.col - (int)compl_col;
int c;
compl_T *cp;
p = compl_shown_match->cp_str;
if ((int)STRLEN(p) <= len)
{
if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
{
p = NULL;
for (cp = compl_shown_match->cp_next; cp != NULL
&& cp != compl_first_match; cp = cp->cp_next)
{
if (compl_leader == NULL
|| ins_compl_equal(cp, compl_leader,
(int)STRLEN(compl_leader)))
{
p = cp->cp_str;
break;
}
}
if (p == NULL || (int)STRLEN(p) <= len)
return;
}
else
return;
}
p += len;
c = PTR2CHAR(p);
ins_compl_addleader(c);
}
static int
ins_compl_prep(int c)
{
char_u *ptr;
int want_cindent;
int retval = FALSE;
if (c != Ctrl_R && vim_is_ctrl_x_key(c))
edit_submode_extra = NULL;
if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
|| c == K_MOUSELEFT || c == K_MOUSERIGHT)
return retval;
if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
|| (ctrl_x_mode == 0 && !compl_started))
{
compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
compl_used_match = TRUE;
}
if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
{
switch (c)
{
case Ctrl_E:
case Ctrl_Y:
ctrl_x_mode = CTRL_X_SCROLL;
if (!(State & REPLACE_FLAG))
edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
else
edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
edit_submode_pre = NULL;
showmode();
break;
case Ctrl_L:
ctrl_x_mode = CTRL_X_WHOLE_LINE;
break;
case Ctrl_F:
ctrl_x_mode = CTRL_X_FILES;
break;
case Ctrl_K:
ctrl_x_mode = CTRL_X_DICTIONARY;
break;
case Ctrl_R:
break;
case Ctrl_T:
ctrl_x_mode = CTRL_X_THESAURUS;
break;
#ifdef FEAT_COMPL_FUNC
case Ctrl_U:
ctrl_x_mode = CTRL_X_FUNCTION;
break;
case Ctrl_O:
ctrl_x_mode = CTRL_X_OMNI;
break;
#endif
case 's':
case Ctrl_S:
ctrl_x_mode = CTRL_X_SPELL;
#ifdef FEAT_SPELL
++emsg_off;
spell_back_to_badword();
--emsg_off;
#endif
break;
case Ctrl_RSB:
ctrl_x_mode = CTRL_X_TAGS;
break;
#ifdef FEAT_FIND_ID
case Ctrl_I:
case K_S_TAB:
ctrl_x_mode = CTRL_X_PATH_PATTERNS;
break;
case Ctrl_D:
ctrl_x_mode = CTRL_X_PATH_DEFINES;
break;
#endif
case Ctrl_V:
case Ctrl_Q:
ctrl_x_mode = CTRL_X_CMDLINE;
break;
case Ctrl_P:
case Ctrl_N:
if (!(compl_cont_status & CONT_INTRPT))
compl_cont_status |= CONT_LOCAL;
else if (compl_cont_mode != 0)
compl_cont_status &= ~CONT_LOCAL;
default:
if (c == Ctrl_X)
{
if (compl_cont_mode != 0)
compl_cont_status = 0;
else
compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
}
ctrl_x_mode = 0;
edit_submode = NULL;
showmode();
break;
}
}
else if (ctrl_x_mode != 0)
{
if (!vim_is_ctrl_x_key(c))
{
if (ctrl_x_mode == CTRL_X_SCROLL)
ctrl_x_mode = 0;
else
ctrl_x_mode = CTRL_X_FINISHED;
edit_submode = NULL;
}
showmode();
}
if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
{
showmode();
if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
&& !ins_compl_pum_key(c))
|| ctrl_x_mode == CTRL_X_FINISHED)
{
if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
{
if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
ptr = compl_curr_match->cp_str;
else
ptr = NULL;
ins_compl_fixRedoBufForLeader(ptr);
}
#ifdef FEAT_CINDENT
want_cindent = (can_cindent && cindent_on());
#endif
if (compl_cont_mode == CTRL_X_WHOLE_LINE)
{
#ifdef FEAT_CINDENT
if (want_cindent)
{
do_c_expr_indent();
want_cindent = FALSE;
}
#endif
}
else
{
int prev_col = curwin->w_cursor.col;
if (prev_col > 0)
dec_cursor();
if (!arrow_used && !ins_need_undo && c != Ctrl_E)
insertchar(NUL, 0, -1);
if (prev_col > 0
&& ml_get_curline()[curwin->w_cursor.col] != NUL)
inc_cursor();
}
if ((c == Ctrl_Y || (compl_enter_selects
&& (c == CAR || c == K_KENTER || c == NL)))
&& pum_visible())
retval = TRUE;
if (c == Ctrl_E)
{
ins_compl_delete();
if (compl_leader != NULL)
ins_bytes(compl_leader + ins_compl_len());
else if (compl_first_match != NULL)
ins_bytes(compl_orig_text + ins_compl_len());
retval = TRUE;
}
auto_format(FALSE, TRUE);
ins_compl_free();
compl_started = FALSE;
compl_matches = 0;
if (!shortmess(SHM_COMPLETIONMENU))
msg_clr_cmdline();
ctrl_x_mode = 0;
compl_enter_selects = FALSE;
if (edit_submode != NULL)
{
edit_submode = NULL;
showmode();
}
#ifdef FEAT_CMDWIN
if (c == Ctrl_C && cmdwin_type != 0)
update_screen(0);
#endif
#ifdef FEAT_CINDENT
if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
do_c_expr_indent();
#endif
#ifdef FEAT_AUTOCMD
apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
#endif
}
}
#ifdef FEAT_AUTOCMD
else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
#endif
if (!vim_is_ctrl_x_key(c))
{
compl_cont_status = 0;
compl_cont_mode = 0;
}
return retval;
}
static void
ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
{
int len;
char_u *p;
char_u *ptr = ptr_arg;
if (ptr == NULL)
{
if (compl_leader != NULL)
ptr = compl_leader;
else
return;
}
if (compl_orig_text != NULL)
{
p = compl_orig_text;
for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
;
#ifdef FEAT_MBYTE
if (len > 0)
len -= (*mb_head_off)(p, p + len);
#endif
for (p += len; *p != NUL; MB_PTR_ADV(p))
AppendCharToRedobuff(K_BS);
}
else
len = 0;
if (ptr != NULL)
AppendToRedobuffLit(ptr + len, -1);
}
static buf_T *
ins_compl_next_buf(buf_T *buf, int flag)
{
static win_T *wp;
if (flag == 'w')
{
if (buf == curbuf)
wp = curwin;
while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
&& wp->w_buffer->b_scanned)
;
buf = wp->w_buffer;
}
else
while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
&& ((flag == 'U'
? buf->b_p_bl
: (!buf->b_p_bl
|| (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
|| buf->b_scanned))
;
return buf;
}
#ifdef FEAT_COMPL_FUNC
static void
expand_by_function(
int type,
char_u *base)
{
list_T *matchlist = NULL;
dict_T *matchdict = NULL;
char_u *args[2];
char_u *funcname;
pos_T pos;
win_T *curwin_save;
buf_T *curbuf_save;
typval_T rettv;
funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
if (*funcname == NUL)
return;
args[0] = (char_u *)"0";
args[1] = base;
pos = curwin->w_cursor;
curwin_save = curwin;
curbuf_save = curbuf;
if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
{
switch (rettv.v_type)
{
case VAR_LIST:
matchlist = rettv.vval.v_list;
break;
case VAR_DICT:
matchdict = rettv.vval.v_dict;
break;
default:
clear_tv(&rettv);
break;
}
}
if (curwin_save != curwin || curbuf_save != curbuf)
{
EMSG(_(e_complwin));
goto theend;
}
curwin->w_cursor = pos;
validate_cursor();
if (!EQUAL_POS(curwin->w_cursor, pos))
{
EMSG(_(e_compldel));
goto theend;
}
if (matchlist != NULL)
ins_compl_add_list(matchlist);
else if (matchdict != NULL)
ins_compl_add_dict(matchdict);
theend:
if (matchdict != NULL)
dict_unref(matchdict);
if (matchlist != NULL)
list_unref(matchlist);
}
#endif
#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
static void
ins_compl_add_list(list_T *list)
{
listitem_T *li;
int dir = compl_direction;
for (li = list->lv_first; li != NULL; li = li->li_next)
{
if (ins_compl_add_tv(&li->li_tv, dir) == OK)
dir = FORWARD;
else if (did_emsg)
break;
}
}
static void
ins_compl_add_dict(dict_T *dict)
{
dictitem_T *di_refresh;
dictitem_T *di_words;
compl_opt_refresh_always = FALSE;
di_refresh = dict_find(dict, (char_u *)"refresh", 7);
if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
{
char_u *v = di_refresh->di_tv.vval.v_string;
if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
compl_opt_refresh_always = TRUE;
}
di_words = dict_find(dict, (char_u *)"words", 5);
if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
ins_compl_add_list(di_words->di_tv.vval.v_list);
}
int
ins_compl_add_tv(typval_T *tv, int dir)
{
char_u *word;
int icase = FALSE;
int adup = FALSE;
int aempty = FALSE;
char_u *(cptext[CPT_COUNT]);
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
{
word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
(char_u *)"abbr", FALSE);
cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
(char_u *)"menu", FALSE);
cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
(char_u *)"kind", FALSE);
cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
(char_u *)"info", FALSE);
if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
}
else
{
word = get_tv_string_chk(tv);
vim_memset(cptext, 0, sizeof(cptext));
}
if (word == NULL || (!aempty && *word == NUL))
return FAIL;
return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
}
#endif
static int
ins_compl_get_exp(pos_T *ini)
{
static pos_T first_match_pos;
static pos_T last_match_pos;
static char_u *e_cpt = (char_u *)"";
static int found_all = FALSE;
static buf_T *ins_buf = NULL;
pos_T *pos;
char_u **matches;
int save_p_scs;
int save_p_ws;
int save_p_ic;
int i;
int num_matches;
int len;
int found_new_match;
int type = ctrl_x_mode;
char_u *ptr;
char_u *dict = NULL;
int dict_f = 0;
int set_match_pos;
if (!compl_started)
{
FOR_ALL_BUFFERS(ins_buf)
ins_buf->b_scanned = 0;
found_all = FALSE;
ins_buf = curbuf;
e_cpt = (compl_cont_status & CONT_LOCAL)
? (char_u *)"." : curbuf->b_p_cpt;
last_match_pos = first_match_pos = *ini;
}
compl_old_match = compl_curr_match;
pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
for (;;)
{
found_new_match = FAIL;
set_match_pos = FALSE;
if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
&& (!compl_started || found_all))
{
found_all = FALSE;
while (*e_cpt == ',' || *e_cpt == ' ')
e_cpt++;
if (*e_cpt == '.' && !curbuf->b_scanned)
{
ins_buf = curbuf;
first_match_pos = *ini;
if (ctrl_x_mode == 0 && dec(&first_match_pos) < 0)
{
first_match_pos.lnum = ins_buf->b_ml.ml_line_count;
first_match_pos.col =
(colnr_T)STRLEN(ml_get(first_match_pos.lnum));
}
last_match_pos = first_match_pos;
type = 0;
set_match_pos = TRUE;
}
else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
&& (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
{
if (ins_buf->b_ml.ml_mfp != NULL)
{
compl_started = TRUE;
first_match_pos.col = last_match_pos.col = 0;
first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
last_match_pos.lnum = 0;
type = 0;
}
else
{
found_all = TRUE;
if (ins_buf->b_fname == NULL)
continue;
type = CTRL_X_DICTIONARY;
dict = ins_buf->b_fname;
dict_f = DICT_EXACT;
}
vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
ins_buf->b_fname == NULL
? buf_spname(ins_buf)
: ins_buf->b_sfname == NULL
? ins_buf->b_fname
: ins_buf->b_sfname);
(void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
}
else if (*e_cpt == NUL)
break;
else
{
if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
type = -1;
else if (*e_cpt == 'k' || *e_cpt == 's')
{
if (*e_cpt == 'k')
type = CTRL_X_DICTIONARY;
else
type = CTRL_X_THESAURUS;
if (*++e_cpt != ',' && *e_cpt != NUL)
{
dict = e_cpt;
dict_f = DICT_FIRST;
}
}
#ifdef FEAT_FIND_ID
else if (*e_cpt == 'i')
type = CTRL_X_PATH_PATTERNS;
else if (*e_cpt == 'd')
type = CTRL_X_PATH_DEFINES;
#endif
else if (*e_cpt == ']' || *e_cpt == 't')
{
type = CTRL_X_TAGS;
vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
(void)msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
}
else
type = -1;
(void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
found_all = TRUE;
if (type == -1)
continue;
}
}
if (compl_pattern == NULL)
break;
switch (type)
{
case -1:
break;
#ifdef FEAT_FIND_ID
case CTRL_X_PATH_PATTERNS:
case CTRL_X_PATH_DEFINES:
find_pattern_in_path(compl_pattern, compl_direction,
(int)STRLEN(compl_pattern), FALSE, FALSE,
(type == CTRL_X_PATH_DEFINES
&& !(compl_cont_status & CONT_SOL))
? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
(linenr_T)1, (linenr_T)MAXLNUM);
break;
#endif
case CTRL_X_DICTIONARY:
case CTRL_X_THESAURUS:
ins_compl_dictionaries(
dict != NULL ? dict
: (type == CTRL_X_THESAURUS
? (*curbuf->b_p_tsr == NUL
? p_tsr
: curbuf->b_p_tsr)
: (*curbuf->b_p_dict == NUL
? p_dict
: curbuf->b_p_dict)),
compl_pattern,
dict != NULL ? dict_f
: 0, type == CTRL_X_THESAURUS);
dict = NULL;
break;
case CTRL_X_TAGS:
save_p_ic = p_ic;
p_ic = ignorecase(compl_pattern);
if (find_tags(compl_pattern, &num_matches, &matches,
TAG_REGEXP | TAG_NAMES | TAG_NOIC |
TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
{
ins_compl_add_matches(num_matches, matches, p_ic);
}
p_ic = save_p_ic;
break;
case CTRL_X_FILES:
if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
{
tilde_replace(compl_pattern, num_matches, matches);
ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
}
break;
case CTRL_X_CMDLINE:
if (expand_cmdline(&compl_xp, compl_pattern,
(int)STRLEN(compl_pattern),
&num_matches, &matches) == EXPAND_OK)
ins_compl_add_matches(num_matches, matches, FALSE);
break;
#ifdef FEAT_COMPL_FUNC
case CTRL_X_FUNCTION:
case CTRL_X_OMNI:
expand_by_function(type, compl_pattern);
break;
#endif
case CTRL_X_SPELL:
#ifdef FEAT_SPELL
num_matches = expand_spelling(first_match_pos.lnum,
compl_pattern, &matches);
if (num_matches > 0)
ins_compl_add_matches(num_matches, matches, p_ic);
#endif
break;
default:
save_p_scs = p_scs;
if (ins_buf->b_p_inf)
p_scs = FALSE;
save_p_ws = p_ws;
if (ins_buf != curbuf)
p_ws = FALSE;
else if (*e_cpt == '.')
p_ws = TRUE;
for (;;)
{
int flags = 0;
++msg_silent;
if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
|| (compl_cont_status & CONT_SOL))
found_new_match = search_for_exact_line(ins_buf, pos,
compl_direction, compl_pattern);
else
found_new_match = searchit(NULL, ins_buf, pos,
compl_direction,
compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
RE_LAST, (linenr_T)0, NULL, NULL);
--msg_silent;
if (!compl_started || set_match_pos)
{
compl_started = TRUE;
first_match_pos = *pos;
last_match_pos = *pos;
set_match_pos = FALSE;
}
else if (first_match_pos.lnum == last_match_pos.lnum
&& first_match_pos.col == last_match_pos.col)
found_new_match = FAIL;
if (found_new_match == FAIL)
{
if (ins_buf == curbuf)
found_all = TRUE;
break;
}
if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
&& ini->lnum == pos->lnum
&& ini->col == pos->col)
continue;
ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
{
if (compl_cont_status & CONT_ADDING)
{
if (pos->lnum >= ins_buf->b_ml.ml_line_count)
continue;
ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
if (!p_paste)
ptr = skipwhite(ptr);
}
len = (int)STRLEN(ptr);
}
else
{
char_u *tmp_ptr = ptr;
if (compl_cont_status & CONT_ADDING)
{
tmp_ptr += compl_length;
if (vim_iswordp(tmp_ptr))
continue;
tmp_ptr = find_word_start(tmp_ptr);
}
tmp_ptr = find_word_end(tmp_ptr);
len = (int)(tmp_ptr - ptr);
if ((compl_cont_status & CONT_ADDING)
&& len == compl_length)
{
if (pos->lnum < ins_buf->b_ml.ml_line_count)
{
STRNCPY(IObuff, ptr, len);
ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
tmp_ptr = ptr = skipwhite(ptr);
tmp_ptr = find_word_start(tmp_ptr);
tmp_ptr = find_word_end(tmp_ptr);
if (tmp_ptr > ptr)
{
if (*ptr != ')' && IObuff[len - 1] != TAB)
{
if (IObuff[len - 1] != ' ')
IObuff[len++] = ' ';
if (p_js
&& (IObuff[len - 2] == '.'
|| (vim_strchr(p_cpo, CPO_JOINSP)
== NULL
&& (IObuff[len - 2] == '?'
|| IObuff[len - 2] == '!'))))
IObuff[len++] = ' ';
}
if (tmp_ptr - ptr >= IOSIZE - len)
tmp_ptr = ptr + IOSIZE - len - 1;
STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
len += (int)(tmp_ptr - ptr);
flags |= CONT_S_IPOS;
}
IObuff[len] = NUL;
ptr = IObuff;
}
if (len == compl_length)
continue;
}
}
if (ins_compl_add_infercase(ptr, len, p_ic,
ins_buf == curbuf ? NULL : ins_buf->b_sfname,
0, flags) != NOTDONE)
{
found_new_match = OK;
break;
}
}
p_scs = save_p_scs;
p_ws = save_p_ws;
}
if (type != 0 && compl_curr_match != compl_old_match)
found_new_match = OK;
if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
|| found_new_match != FAIL)
{
if (got_int)
break;
if (type != -1)
ins_compl_check_keys(0, FALSE);
if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
|| compl_interrupted)
break;
compl_started = TRUE;
}
else
{
if (type == 0 || type == CTRL_X_PATH_PATTERNS)
ins_buf->b_scanned = TRUE;
compl_started = FALSE;
}
}
compl_started = TRUE;
if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
&& *e_cpt == NUL)
found_new_match = FAIL;
i = -1;
if (found_new_match == FAIL
|| (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
i = ins_compl_make_cyclic();
if (compl_old_match != NULL)
{
compl_curr_match = compl_direction == FORWARD ? compl_old_match->cp_next
: compl_old_match->cp_prev;
if (compl_curr_match == NULL)
compl_curr_match = compl_old_match;
}
return i;
}
static void
ins_compl_delete(void)
{
int col;
col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
if ((int)curwin->w_cursor.col > col)
{
if (stop_arrow() == FAIL)
return;
backspace_until_column(col);
}
changed_cline_bef_curs();
set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
}
static void
ins_compl_insert(int in_compl_func)
{
dict_T *dict;
ins_bytes(compl_shown_match->cp_str + ins_compl_len());
if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
compl_used_match = FALSE;
else
compl_used_match = TRUE;
dict = dict_alloc();
if (dict != NULL)
{
dict_add_nr_str(dict, "word", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_str));
dict_add_nr_str(dict, "abbr", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
dict_add_nr_str(dict, "menu", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
dict_add_nr_str(dict, "kind", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
dict_add_nr_str(dict, "info", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
}
set_vim_var_dict(VV_COMPLETED_ITEM, dict);
if (!in_compl_func)
compl_curr_match = compl_shown_match;
}
static int
ins_compl_next(
int allow_get_expansion,
int count,
int insert_match,
int in_compl_func)
{
int num_matches = -1;
int todo = count;
compl_T *found_compl = NULL;
int found_end = FALSE;
int advance;
int started = compl_started;
if (compl_shown_match == NULL)
return -1;
if (compl_leader != NULL
&& (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
{
while (!ins_compl_equal(compl_shown_match,
compl_leader, (int)STRLEN(compl_leader))
&& compl_shown_match->cp_next != NULL
&& compl_shown_match->cp_next != compl_first_match)
compl_shown_match = compl_shown_match->cp_next;
if (compl_shows_dir == BACKWARD
&& !ins_compl_equal(compl_shown_match,
compl_leader, (int)STRLEN(compl_leader))
&& (compl_shown_match->cp_next == NULL
|| compl_shown_match->cp_next == compl_first_match))
{
while (!ins_compl_equal(compl_shown_match,
compl_leader, (int)STRLEN(compl_leader))
&& compl_shown_match->cp_prev != NULL
&& compl_shown_match->cp_prev != compl_first_match)
compl_shown_match = compl_shown_match->cp_prev;
}
}
if (allow_get_expansion && insert_match
&& (!(compl_get_longest || compl_restarting) || compl_used_match))
ins_compl_delete();
advance = count != 1 || !allow_get_expansion || !compl_get_longest;
if (compl_restarting)
{
advance = FALSE;
compl_restarting = FALSE;
}
while (--todo >= 0)
{
if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
{
compl_shown_match = compl_shown_match->cp_next;
found_end = (compl_first_match != NULL
&& (compl_shown_match->cp_next == compl_first_match
|| compl_shown_match == compl_first_match));
}
else if (compl_shows_dir == BACKWARD
&& compl_shown_match->cp_prev != NULL)
{
found_end = (compl_shown_match == compl_first_match);
compl_shown_match = compl_shown_match->cp_prev;
found_end |= (compl_shown_match == compl_first_match);
}
else
{
if (!allow_get_expansion)
{
if (advance)
{
if (compl_shows_dir == BACKWARD)
compl_pending -= todo + 1;
else
compl_pending += todo + 1;
}
return -1;
}
if (!compl_no_select && advance)
{
if (compl_shows_dir == BACKWARD)
--compl_pending;
else
++compl_pending;
}
num_matches = ins_compl_get_exp(&compl_startpos);
while (compl_pending != 0 && compl_direction == compl_shows_dir
&& advance)
{
if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
{
compl_shown_match = compl_shown_match->cp_next;
--compl_pending;
}
if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
{
compl_shown_match = compl_shown_match->cp_prev;
++compl_pending;
}
else
break;
}
found_end = FALSE;
}
if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
&& compl_leader != NULL
&& !ins_compl_equal(compl_shown_match,
compl_leader, (int)STRLEN(compl_leader)))
++todo;
else
found_compl = compl_shown_match;
if (found_end)
{
if (found_compl != NULL)
{
compl_shown_match = found_compl;
break;
}
todo = 1;
}
}
if (compl_no_insert && !started)
{
ins_bytes(compl_orig_text + ins_compl_len());
compl_used_match = FALSE;
}
else if (insert_match)
{
if (!compl_get_longest || compl_used_match)
ins_compl_insert(in_compl_func);
else
ins_bytes(compl_leader + ins_compl_len());
}
else
compl_used_match = FALSE;
if (!allow_get_expansion)
{
ins_compl_upd_pum();
update_screen(0);
ins_compl_show_pum();
#ifdef FEAT_GUI
if (gui.in_use)
{
setcursor();
out_flush();
gui_update_cursor(FALSE, FALSE);
}
#endif
ins_compl_delete();
}
if (compl_no_insert && !started)
compl_enter_selects = TRUE;
else
compl_enter_selects = !insert_match && compl_match_array != NULL;
if (compl_shown_match->cp_fname != NULL)
{
char *lead = _("match in file");
int space = sc_col - vim_strsize((char_u *)lead) - 2;
char_u *s;
char_u *e;
if (space > 0)
{
for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
{
space -= ptr2cells(e);
while (space < 0)
{
space += ptr2cells(s);
MB_PTR_ADV(s);
}
}
vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
s > compl_shown_match->cp_fname ? "<" : "", s);
msg(IObuff);
redraw_cmdline = FALSE;
}
}
return num_matches;
}
void
ins_compl_check_keys(int frequency, int in_compl_func)
{
static int count = 0;
int c;
if (using_script())
return;
if (++count < frequency)
return;
count = 0;
c = vpeekc_any();
if (c != NUL)
{
if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
{
c = safe_vgetc();
compl_shows_dir = ins_compl_key2dir(c);
(void)ins_compl_next(FALSE, ins_compl_key2count(c),
c != K_UP && c != K_DOWN, in_compl_func);
}
else
{
c = safe_vgetc();
if (c != K_IGNORE)
{
if (c != Ctrl_R && KeyTyped)
compl_interrupted = TRUE;
vungetc(c);
}
}
}
if (compl_pending != 0 && !got_int && !compl_no_insert)
{
int todo = compl_pending > 0 ? compl_pending : -compl_pending;
compl_pending = 0;
(void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
}
}
static int
ins_compl_key2dir(int c)
{
if (c == Ctrl_P || c == Ctrl_L
|| c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
return BACKWARD;
return FORWARD;
}
static int
ins_compl_pum_key(int c)
{
return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
|| c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
|| c == K_UP || c == K_DOWN);
}
static int
ins_compl_key2count(int c)
{
int h;
if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
{
h = pum_get_height();
if (h > 3)
h -= 2;
return h;
}
return 1;
}
static int
ins_compl_use_match(int c)
{
switch (c)
{
case K_UP:
case K_DOWN:
case K_PAGEDOWN:
case K_KPAGEDOWN:
case K_S_DOWN:
case K_PAGEUP:
case K_KPAGEUP:
case K_S_UP:
return FALSE;
}
return TRUE;
}
static int
ins_complete(int c, int enable_pum)
{
char_u *line;
int startcol = 0;
colnr_T curs_col;
int n;
int save_w_wrow;
int save_w_leftcol;
int insert_match;
int save_did_ai = did_ai;
compl_direction = ins_compl_key2dir(c);
insert_match = ins_compl_use_match(c);
if (!compl_started)
{
did_ai = FALSE;
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
if (stop_arrow() == FAIL)
return FAIL;
line = ml_get(curwin->w_cursor.lnum);
curs_col = curwin->w_cursor.col;
compl_pending = 0;
if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
&& compl_cont_mode == ctrl_x_mode)
{
compl_cont_status &= ~CONT_INTRPT;
if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
|| ctrl_x_mode == CTRL_X_PATH_DEFINES)
{
if (compl_startpos.lnum != curwin->w_cursor.lnum)
{
compl_col = (colnr_T)getwhitecols(line);
compl_startpos.col = compl_col;
compl_startpos.lnum = curwin->w_cursor.lnum;
compl_cont_status &= ~CONT_SOL;
}
else
{
if (compl_cont_status & CONT_S_IPOS)
{
compl_cont_status |= CONT_SOL;
compl_startpos.col = (colnr_T)(skipwhite(
line + compl_length
+ compl_startpos.col) - line);
}
compl_col = compl_startpos.col;
}
compl_length = curwin->w_cursor.col - (int)compl_col;
#define MIN_SPACE 75
if (compl_length > (IOSIZE - MIN_SPACE))
{
compl_cont_status &= ~CONT_SOL;
compl_length = (IOSIZE - MIN_SPACE);
compl_col = curwin->w_cursor.col - compl_length;
}
compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
if (compl_length < 1)
compl_cont_status &= CONT_LOCAL;
}
else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
compl_cont_status = CONT_ADDING | CONT_N_ADDS;
else
compl_cont_status = 0;
}
else
compl_cont_status &= CONT_LOCAL;
if (!(compl_cont_status & CONT_ADDING))
{
compl_cont_mode = ctrl_x_mode;
if (ctrl_x_mode != 0)
compl_cont_status = 0;
compl_cont_status |= CONT_N_ADDS;
compl_startpos = curwin->w_cursor;
startcol = (int)curs_col;
compl_col = 0;
}
if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
{
if ((compl_cont_status & CONT_SOL)
|| ctrl_x_mode == CTRL_X_PATH_DEFINES)
{
if (!(compl_cont_status & CONT_ADDING))
{
while (--startcol >= 0 && vim_isIDc(line[startcol]))
;
compl_col += ++startcol;
compl_length = curs_col - startcol;
}
if (p_ic)
compl_pattern = str_foldcase(line + compl_col,
compl_length, NULL, 0);
else
compl_pattern = vim_strnsave(line + compl_col,
compl_length);
if (compl_pattern == NULL)
return FAIL;
}
else if (compl_cont_status & CONT_ADDING)
{
char_u *prefix = (char_u *)"\\<";
compl_pattern = alloc(quote_meta(NULL, line + compl_col,
compl_length) + 2);
if (compl_pattern == NULL)
return FAIL;
if (!vim_iswordp(line + compl_col)
|| (compl_col > 0
&& (
#ifdef FEAT_MBYTE
vim_iswordp(mb_prevptr(line, line + compl_col))
#else
vim_iswordc(line[compl_col - 1])
#endif
)))
prefix = (char_u *)"";
STRCPY((char *)compl_pattern, prefix);
(void)quote_meta(compl_pattern + STRLEN(prefix),
line + compl_col, compl_length);
}
else if (--startcol < 0 ||
#ifdef FEAT_MBYTE
!vim_iswordp(mb_prevptr(line, line + startcol + 1))
#else
!vim_iswordc(line[startcol])
#endif
)
{
compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
if (compl_pattern == NULL)
return FAIL;
compl_col += curs_col;
compl_length = 0;
}
else
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int base_class;
int head_off;
startcol -= (*mb_head_off)(line, line + startcol);
base_class = mb_get_class(line + startcol);
while (--startcol >= 0)
{
head_off = (*mb_head_off)(line, line + startcol);
if (base_class != mb_get_class(line + startcol
- head_off))
break;
startcol -= head_off;
}
}
else
#endif
while (--startcol >= 0 && vim_iswordc(line[startcol]))
;
compl_col += ++startcol;
compl_length = (int)curs_col - startcol;
if (compl_length == 1)
{
compl_pattern = alloc(7);
if (compl_pattern == NULL)
return FAIL;
STRCPY((char *)compl_pattern, "\\<");
(void)quote_meta(compl_pattern + 2, line + compl_col, 1);
STRCAT((char *)compl_pattern, "\\k");
}
else
{
compl_pattern = alloc(quote_meta(NULL, line + compl_col,
compl_length) + 2);
if (compl_pattern == NULL)
return FAIL;
STRCPY((char *)compl_pattern, "\\<");
(void)quote_meta(compl_pattern + 2, line + compl_col,
compl_length);
}
}
}
else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
{
compl_col = (colnr_T)getwhitecols(line);
compl_length = (int)curs_col - (int)compl_col;
if (compl_length < 0)
compl_length = 0;
if (p_ic)
compl_pattern = str_foldcase(line + compl_col, compl_length,
NULL, 0);
else
compl_pattern = vim_strnsave(line + compl_col, compl_length);
if (compl_pattern == NULL)
return FAIL;
}
else if (ctrl_x_mode == CTRL_X_FILES)
{
if (startcol > 0)
{
char_u *p = line + startcol;
MB_PTR_BACK(line, p);
while (p > line && vim_isfilec(PTR2CHAR(p)))
MB_PTR_BACK(line, p);
if (p == line && vim_isfilec(PTR2CHAR(p)))
startcol = 0;
else
startcol = (int)(p - line) + 1;
}
compl_col += startcol;
compl_length = (int)curs_col - startcol;
compl_pattern = addstar(line + compl_col, compl_length,
EXPAND_FILES);
if (compl_pattern == NULL)
return FAIL;
}
else if (ctrl_x_mode == CTRL_X_CMDLINE)
{
compl_pattern = vim_strnsave(line, curs_col);
if (compl_pattern == NULL)
return FAIL;
set_cmd_context(&compl_xp, compl_pattern,
(int)STRLEN(compl_pattern), curs_col, FALSE);
if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
|| compl_xp.xp_context == EXPAND_NOTHING)
compl_col = curs_col;
else
compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
compl_length = curs_col - compl_col;
}
else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
{
#ifdef FEAT_COMPL_FUNC
char_u *args[2];
int col;
char_u *funcname;
pos_T pos;
win_T *curwin_save;
buf_T *curbuf_save;
funcname = ctrl_x_mode == CTRL_X_FUNCTION
? curbuf->b_p_cfu : curbuf->b_p_ofu;
if (*funcname == NUL)
{
EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
? "completefunc" : "omnifunc");
did_ai = save_did_ai;
return FAIL;
}
args[0] = (char_u *)"1";
args[1] = NULL;
pos = curwin->w_cursor;
curwin_save = curwin;
curbuf_save = curbuf;
col = call_func_retnr(funcname, 2, args, FALSE);
if (curwin_save != curwin || curbuf_save != curbuf)
{
EMSG(_(e_complwin));
return FAIL;
}
curwin->w_cursor = pos;
validate_cursor();
if (!EQUAL_POS(curwin->w_cursor, pos))
{
EMSG(_(e_compldel));
return FAIL;
}
if (col == -2)
return FAIL;
if (col == -3)
{
ctrl_x_mode = 0;
edit_submode = NULL;
if (!shortmess(SHM_COMPLETIONMENU))
msg_clr_cmdline();
return FAIL;
}
compl_opt_refresh_always = FALSE;
if (col < 0)
col = curs_col;
compl_col = col;
if (compl_col > curs_col)
compl_col = curs_col;
line = ml_get(curwin->w_cursor.lnum);
compl_length = curs_col - compl_col;
compl_pattern = vim_strnsave(line + compl_col, compl_length);
if (compl_pattern == NULL)
#endif
return FAIL;
}
else if (ctrl_x_mode == CTRL_X_SPELL)
{
#ifdef FEAT_SPELL
if (spell_bad_len > 0)
compl_col = curs_col - spell_bad_len;
else
compl_col = spell_word_start(startcol);
if (compl_col >= (colnr_T)startcol)
{
compl_length = 0;
compl_col = curs_col;
}
else
{
spell_expand_check_cap(compl_col);
compl_length = (int)curs_col - compl_col;
}
line = ml_get(curwin->w_cursor.lnum);
compl_pattern = vim_strnsave(line + compl_col, compl_length);
if (compl_pattern == NULL)
#endif
return FAIL;
}
else
{
internal_error("ins_complete()");
return FAIL;
}
if (compl_cont_status & CONT_ADDING)
{
edit_submode_pre = (char_u *)_(" Adding");
if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
{
#ifdef FEAT_COMMENTS
char_u *old = curbuf->b_p_com;
curbuf->b_p_com = (char_u *)"";
#endif
compl_startpos.lnum = curwin->w_cursor.lnum;
compl_startpos.col = compl_col;
ins_eol('\r');
#ifdef FEAT_COMMENTS
curbuf->b_p_com = old;
#endif
compl_length = 0;
compl_col = curwin->w_cursor.col;
}
}
else
{
edit_submode_pre = NULL;
compl_startpos.col = compl_col;
}
if (compl_cont_status & CONT_LOCAL)
edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
else
edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
ins_compl_fixRedoBufForLeader(NULL);
vim_free(compl_orig_text);
compl_orig_text = vim_strnsave(line + compl_col, compl_length);
if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
-1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
{
vim_free(compl_pattern);
compl_pattern = NULL;
vim_free(compl_orig_text);
compl_orig_text = NULL;
return FAIL;
}
edit_submode_extra = (char_u *)_("-- Searching...");
edit_submode_highl = HLF_COUNT;
showmode();
edit_submode_extra = NULL;
out_flush();
}
else if (insert_match && stop_arrow() == FAIL)
return FAIL;
compl_shown_match = compl_curr_match;
compl_shows_dir = compl_direction;
save_w_wrow = curwin->w_wrow;
save_w_leftcol = curwin->w_leftcol;
n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
ins_compl_upd_pum();
if (n > 1)
compl_matches = n;
compl_curr_match = compl_shown_match;
compl_direction = compl_shows_dir;
if (got_int && !global_busy)
{
(void)vgetc();
got_int = FALSE;
}
if (compl_first_match == compl_first_match->cp_next)
{
edit_submode_extra = (compl_cont_status & CONT_ADDING)
&& compl_length > 1
? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
edit_submode_highl = HLF_E;
if ( compl_length > 1
|| (compl_cont_status & CONT_ADDING)
|| (ctrl_x_mode != 0
&& ctrl_x_mode != CTRL_X_PATH_PATTERNS
&& ctrl_x_mode != CTRL_X_PATH_DEFINES))
compl_cont_status &= ~CONT_N_ADDS;
}
if (compl_curr_match->cp_flags & CONT_S_IPOS)
compl_cont_status |= CONT_S_IPOS;
else
compl_cont_status &= ~CONT_S_IPOS;
if (edit_submode_extra == NULL)
{
if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
{
edit_submode_extra = (char_u *)_("Back at original");
edit_submode_highl = HLF_W;
}
else if (compl_cont_status & CONT_S_IPOS)
{
edit_submode_extra = (char_u *)_("Word from other line");
edit_submode_highl = HLF_COUNT;
}
else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
{
edit_submode_extra = (char_u *)_("The only match");
edit_submode_highl = HLF_COUNT;
}
else
{
if (compl_curr_match->cp_number == -1)
{
int number = 0;
compl_T *match;
if (compl_direction == FORWARD)
{
for (match = compl_curr_match->cp_prev; match != NULL
&& match != compl_first_match;
match = match->cp_prev)
if (match->cp_number != -1)
{
number = match->cp_number;
break;
}
if (match != NULL)
for (match = match->cp_next;
match != NULL && match->cp_number == -1;
match = match->cp_next)
match->cp_number = ++number;
}
else
{
for (match = compl_curr_match->cp_next; match != NULL
&& match != compl_first_match;
match = match->cp_next)
if (match->cp_number != -1)
{
number = match->cp_number;
break;
}
if (match != NULL)
for (match = match->cp_prev; match
&& match->cp_number == -1;
match = match->cp_prev)
match->cp_number = ++number;
}
}
if (compl_curr_match->cp_number != -1)
{
static char_u match_ref[81];
if (compl_matches > 0)
vim_snprintf((char *)match_ref, sizeof(match_ref),
_("match %d of %d"),
compl_curr_match->cp_number, compl_matches);
else
vim_snprintf((char *)match_ref, sizeof(match_ref),
_("match %d"),
compl_curr_match->cp_number);
edit_submode_extra = match_ref;
edit_submode_highl = HLF_R;
if (dollar_vcol >= 0)
curs_columns(FALSE);
}
}
}
showmode();
if (!shortmess(SHM_COMPLETIONMENU))
{
if (edit_submode_extra != NULL)
{
if (!p_smd)
msg_attr(edit_submode_extra,
edit_submode_highl < HLF_COUNT
? HL_ATTR(edit_submode_highl) : 0);
}
else
msg_clr_cmdline();
}
if (enable_pum && !compl_interrupted)
show_pum(save_w_wrow, save_w_leftcol);
compl_was_interrupted = compl_interrupted;
compl_interrupted = FALSE;
return OK;
}
static void
show_pum(int prev_w_wrow, int prev_w_leftcol)
{
int n = RedrawingDisabled;
RedrawingDisabled = 0;
setcursor();
if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol)
ins_compl_del_pum();
ins_compl_show_pum();
setcursor();
RedrawingDisabled = n;
}
static unsigned
quote_meta(char_u *dest, char_u *src, int len)
{
unsigned m = (unsigned)len + 1;
for ( ; --len >= 0; src++)
{
switch (*src)
{
case '.':
case '*':
case '[':
if (ctrl_x_mode == CTRL_X_DICTIONARY
|| ctrl_x_mode == CTRL_X_THESAURUS)
break;
case '~':
if (!p_magic)
break;
case '\\':
if (ctrl_x_mode == CTRL_X_DICTIONARY
|| ctrl_x_mode == CTRL_X_THESAURUS)
break;
case '^':
case '$':
m++;
if (dest != NULL)
*dest++ = '\\';
break;
}
if (dest != NULL)
*dest++ = *src;
# ifdef FEAT_MBYTE
if (has_mbyte)
{
int i, mb_len;
mb_len = (*mb_ptr2len)(src) - 1;
if (mb_len > 0 && len >= mb_len)
for (i = 0; i < mb_len; ++i)
{
--len;
++src;
if (dest != NULL)
*dest++ = *src;
}
}
# endif
}
if (dest != NULL)
*dest = NUL;
return m;
}
#endif
int
get_literal(void)
{
int cc;
int nc;
int i;
int hex = FALSE;
int octal = FALSE;
#ifdef FEAT_MBYTE
int unicode = 0;
#endif
if (got_int)
return Ctrl_C;
#ifdef FEAT_GUI
if (gui.in_use)
++allow_keys;
#endif
#ifdef USE_ON_FLY_SCROLL
dont_scroll = TRUE;
#endif
++no_mapping;
cc = 0;
i = 0;
for (;;)
{
nc = plain_vgetc();
#ifdef FEAT_CMDL_INFO
if (!(State & CMDLINE)
# ifdef FEAT_MBYTE
&& MB_BYTE2LEN_CHECK(nc) == 1
# endif
)
add_to_showcmd(nc);
#endif
if (nc == 'x' || nc == 'X')
hex = TRUE;
else if (nc == 'o' || nc == 'O')
octal = TRUE;
#ifdef FEAT_MBYTE
else if (nc == 'u' || nc == 'U')
unicode = nc;
#endif
else
{
if (hex
#ifdef FEAT_MBYTE
|| unicode != 0
#endif
)
{
if (!vim_isxdigit(nc))
break;
cc = cc * 16 + hex2nr(nc);
}
else if (octal)
{
if (nc < '0' || nc > '7')
break;
cc = cc * 8 + nc - '0';
}
else
{
if (!VIM_ISDIGIT(nc))
break;
cc = cc * 10 + nc - '0';
}
++i;
}
if (cc > 255
#ifdef FEAT_MBYTE
&& unicode == 0
#endif
)
cc = 255;
nc = 0;
if (hex)
{
if (i >= 2)
break;
}
#ifdef FEAT_MBYTE
else if (unicode)
{
if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
break;
}
#endif
else if (i >= 3)
break;
}
if (i == 0)
{
if (nc == K_ZERO)
{
cc = '\n';
nc = 0;
}
else
{
cc = nc;
nc = 0;
}
}
if (cc == 0)
cc = '\n';
#ifdef FEAT_MBYTE
if (enc_dbcs && (cc & 0xff) == 0)
cc = '?';
#endif
--no_mapping;
#ifdef FEAT_GUI
if (gui.in_use)
--allow_keys;
#endif
if (nc)
vungetc(nc);
got_int = FALSE;
return cc;
}
static void
insert_special(
int c,
int allow_modmask,
int ctrlv)
{
char_u *p;
int len;
#ifdef MACOS_X
if (mod_mask & MOD_MASK_CMD)
allow_modmask = TRUE;
#endif
if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
{
p = get_special_key_name(c, mod_mask);
len = (int)STRLEN(p);
c = p[len - 1];
if (len > 2)
{
if (stop_arrow() == FAIL)
return;
p[len - 1] = NUL;
ins_str(p);
AppendToRedobuffLit(p, -1);
ctrlv = FALSE;
}
}
if (stop_arrow() == OK)
insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
}
#ifdef EBCDIC
# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
#else
# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
#endif
#ifdef FEAT_MBYTE
# define WHITECHAR(cc) (VIM_ISWHITE(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
#else
# define WHITECHAR(cc) VIM_ISWHITE(cc)
#endif
void
insertchar(
int c,
int flags,
int second_indent)
{
int textwidth;
#ifdef FEAT_COMMENTS
char_u *p;
#endif
int fo_ins_blank;
int force_format = flags & INSCHAR_FORMAT;
textwidth = comp_textwidth(force_format);
fo_ins_blank = has_format_option(FO_INS_BLANK);
if (textwidth > 0
&& (force_format
|| (!VIM_ISWHITE(c)
&& !((State & REPLACE_FLAG)
#ifdef FEAT_VREPLACE
&& !(State & VREPLACE_FLAG)
#endif
&& *ml_get_cursor() != NUL)
&& (curwin->w_cursor.lnum != Insstart.lnum
|| ((!has_format_option(FO_INS_LONG)
|| Insstart_textlen <= (colnr_T)textwidth)
&& (!fo_ins_blank
|| Insstart_blank_vcol <= (colnr_T)textwidth
))))))
{
#if defined(FEAT_EVAL)
int do_internal = TRUE;
colnr_T virtcol = get_nolist_virtcol()
+ char2cells(c != NUL ? c : gchar_cursor());
if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
&& (force_format || virtcol > (colnr_T)textwidth))
{
do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
ins_need_undo = TRUE;
}
if (do_internal)
#endif
internal_format(textwidth, second_indent, flags, c == NUL, c);
}
if (c == NUL)
return;
#ifdef FEAT_COMMENTS
if (did_ai && (int)c == end_comment_pending)
{
char_u *line;
char_u lead_end[COM_MAX_LEN];
int middle_len, end_len;
int i;
i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL)
{
while (*p && p[-1] != ':')
++p;
middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
while (middle_len > 0 && VIM_ISWHITE(lead_end[middle_len - 1]))
--middle_len;
while (*p && p[-1] != ':')
++p;
end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
i = curwin->w_cursor.col;
while (--i >= 0 && VIM_ISWHITE(line[i]))
;
i++;
i -= middle_len;
if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
{
backspace_until_column(i);
ins_bytes_len(lead_end, end_len - 1);
}
}
}
end_comment_pending = NUL;
#endif
did_ai = FALSE;
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
#ifdef USE_ON_FLY_SCROLL
dont_scroll = FALSE;
#endif
if ( !ISSPECIAL(c)
#ifdef FEAT_MBYTE
&& (!has_mbyte || (*mb_char2len)(c) == 1)
#endif
&& vpeekc() != NUL
&& !(State & REPLACE_FLAG)
#ifdef FEAT_CINDENT
&& !cindent_on()
#endif
#ifdef FEAT_RIGHTLEFT
&& !p_ri
#endif
#ifdef FEAT_AUTOCMD
&& !has_insertcharpre()
#endif
)
{
#define INPUT_BUFLEN 100
char_u buf[INPUT_BUFLEN + 1];
int i;
colnr_T virtcol = 0;
buf[0] = c;
i = 1;
if (textwidth > 0)
virtcol = get_nolist_virtcol();
while ( (c = vpeekc()) != NUL
&& !ISSPECIAL(c)
#ifdef FEAT_MBYTE
&& (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
#endif
&& i < INPUT_BUFLEN
# ifdef FEAT_FKMAP
&& !(p_fkmap && KeyTyped)
# endif
&& (textwidth == 0
|| (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
&& !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
{
#ifdef FEAT_RIGHTLEFT
c = vgetc();
if (p_hkmap && KeyTyped)
c = hkmap(c);
buf[i++] = c;
#else
buf[i++] = vgetc();
#endif
}
#ifdef FEAT_DIGRAPHS
do_digraph(-1);
do_digraph(buf[i-1]);
#endif
buf[i] = NUL;
ins_str(buf);
if (flags & INSCHAR_CTRLV)
{
redo_literal(*buf);
i = 1;
}
else
i = 0;
if (buf[i] != NUL)
AppendToRedobuffLit(buf + i, -1);
}
else
{
#ifdef FEAT_MBYTE
int cc;
if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
{
char_u buf[MB_MAXBYTES + 1];
(*mb_char2bytes)(c, buf);
buf[cc] = NUL;
ins_char_bytes(buf, cc);
AppendCharToRedobuff(c);
}
else
#endif
{
ins_char(c);
if (flags & INSCHAR_CTRLV)
redo_literal(c);
else
AppendCharToRedobuff(c);
}
}
}
static void
internal_format(
int textwidth,
int second_indent,
int flags,
int format_only,
int c)
{
int cc;
int save_char = NUL;
int haveto_redraw = FALSE;
int fo_ins_blank = has_format_option(FO_INS_BLANK);
#ifdef FEAT_MBYTE
int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
#endif
int fo_white_par = has_format_option(FO_WHITE_PAR);
int first_line = TRUE;
#ifdef FEAT_COMMENTS
colnr_T leader_len;
int no_leader = FALSE;
int do_comments = (flags & INSCHAR_DO_COM);
#endif
#ifdef FEAT_LINEBREAK
int has_lbr = curwin->w_p_lbr;
curwin->w_p_lbr = FALSE;
#endif
if (!curbuf->b_p_ai
#ifdef FEAT_VREPLACE
&& !(State & VREPLACE_FLAG)
#endif
)
{
cc = gchar_cursor();
if (VIM_ISWHITE(cc))
{
save_char = cc;
pchar_cursor('x');
}
}
while (!got_int)
{
int startcol;
int wantcol;
int foundcol;
int end_foundcol = 0;
colnr_T len;
colnr_T virtcol;
#ifdef FEAT_VREPLACE
int orig_col = 0;
char_u *saved_text = NULL;
#endif
colnr_T col;
colnr_T end_col;
virtcol = get_nolist_virtcol()
+ char2cells(c != NUL ? c : gchar_cursor());
if (virtcol <= (colnr_T)textwidth)
break;
#ifdef FEAT_COMMENTS
if (no_leader)
do_comments = FALSE;
else if (!(flags & INSCHAR_FORMAT)
&& has_format_option(FO_WRAP_COMS))
do_comments = TRUE;
if (do_comments)
leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
else
leader_len = 0;
if (leader_len == 0)
no_leader = TRUE;
#endif
if (!(flags & INSCHAR_FORMAT)
#ifdef FEAT_COMMENTS
&& leader_len == 0
#endif
&& !has_format_option(FO_WRAP))
break;
if ((startcol = curwin->w_cursor.col) == 0)
break;
coladvance((colnr_T)textwidth);
wantcol = curwin->w_cursor.col;
curwin->w_cursor.col = startcol;
foundcol = 0;
while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
|| (flags & INSCHAR_FORMAT)
|| curwin->w_cursor.lnum != Insstart.lnum
|| curwin->w_cursor.col >= Insstart.col)
{
if (curwin->w_cursor.col == startcol && c != NUL)
cc = c;
else
cc = gchar_cursor();
if (WHITECHAR(cc))
{
end_col = curwin->w_cursor.col;
while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
{
dec_cursor();
cc = gchar_cursor();
}
if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
break;
#ifdef FEAT_COMMENTS
if (curwin->w_cursor.col < leader_len)
break;
#endif
if (has_format_option(FO_ONE_LETTER))
{
if (curwin->w_cursor.col == 0)
break;
#ifdef FEAT_COMMENTS
if (curwin->w_cursor.col <= leader_len)
break;
#endif
col = curwin->w_cursor.col;
dec_cursor();
cc = gchar_cursor();
if (WHITECHAR(cc))
continue;
curwin->w_cursor.col = col;
}
inc_cursor();
end_foundcol = end_col + 1;
foundcol = curwin->w_cursor.col;
if (curwin->w_cursor.col <= (colnr_T)wantcol)
break;
}
#ifdef FEAT_MBYTE
else if (cc >= 0x100 && fo_multibyte)
{
if (curwin->w_cursor.col != startcol)
{
#ifdef FEAT_COMMENTS
if (curwin->w_cursor.col < leader_len)
break;
#endif
col = curwin->w_cursor.col;
inc_cursor();
if (foundcol != curwin->w_cursor.col)
{
foundcol = curwin->w_cursor.col;
end_foundcol = foundcol;
if (curwin->w_cursor.col <= (colnr_T)wantcol)
break;
}
curwin->w_cursor.col = col;
}
if (curwin->w_cursor.col == 0)
break;
col = curwin->w_cursor.col;
dec_cursor();
cc = gchar_cursor();
if (WHITECHAR(cc))
continue;
#ifdef FEAT_COMMENTS
if (curwin->w_cursor.col < leader_len)
break;
#endif
curwin->w_cursor.col = col;
foundcol = curwin->w_cursor.col;
end_foundcol = foundcol;
if (curwin->w_cursor.col <= (colnr_T)wantcol)
break;
}
#endif
if (curwin->w_cursor.col == 0)
break;
dec_cursor();
}
if (foundcol == 0)
{
curwin->w_cursor.col = startcol;
break;
}
undisplay_dollar();
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
orig_col = startcol;
else
#endif
replace_offset = startcol - end_foundcol;
curwin->w_cursor.col = foundcol;
while ((cc = gchar_cursor(), WHITECHAR(cc))
&& (!fo_white_par || curwin->w_cursor.col < startcol))
inc_cursor();
startcol -= curwin->w_cursor.col;
if (startcol < 0)
startcol = 0;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
saved_text = vim_strsave(ml_get_cursor());
curwin->w_cursor.col = orig_col;
if (saved_text == NULL)
break;
saved_text[startcol] = NUL;
if (!fo_white_par)
backspace_until_column(foundcol);
}
else
#endif
{
if (!fo_white_par)
curwin->w_cursor.col = foundcol;
}
open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
+ (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
#ifdef FEAT_COMMENTS
+ (do_comments ? OPENLINE_DO_COM : 0)
+ ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
#endif
, ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
if (!(flags & INSCHAR_COM_LIST))
old_indent = 0;
replace_offset = 0;
if (first_line)
{
if (!(flags & INSCHAR_COM_LIST))
{
if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
second_indent =
get_number_indent(curwin->w_cursor.lnum - 1);
if (second_indent >= 0)
{
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
change_indent(INDENT_SET, second_indent,
FALSE, NUL, TRUE);
else
#endif
#ifdef FEAT_COMMENTS
if (leader_len > 0 && second_indent - leader_len > 0)
{
int i;
int padding = second_indent - leader_len;
for (i = 0; i < padding; i++)
ins_str((char_u *)" ");
changed_bytes(curwin->w_cursor.lnum, leader_len);
}
else
{
#endif
(void)set_indent(second_indent, SIN_CHANGED);
#ifdef FEAT_COMMENTS
}
#endif
}
}
first_line = FALSE;
}
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
ins_bytes(saved_text);
vim_free(saved_text);
}
else
#endif
{
curwin->w_cursor.col += startcol;
len = (colnr_T)STRLEN(ml_get_curline());
if (curwin->w_cursor.col > len)
curwin->w_cursor.col = len;
}
haveto_redraw = TRUE;
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
did_ai = FALSE;
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
line_breakcheck();
}
if (save_char != NUL)
pchar_cursor(save_char);
#ifdef FEAT_LINEBREAK
curwin->w_p_lbr = has_lbr;
#endif
if (!format_only && haveto_redraw)
{
update_topline();
redraw_curbuf_later(VALID);
}
}
void
auto_format(
int trailblank,
int prev_line)
{
pos_T pos;
colnr_T len;
char_u *old;
char_u *new, *pnew;
int wasatend;
int cc;
if (!has_format_option(FO_AUTO))
return;
pos = curwin->w_cursor;
old = ml_get_curline();
check_auto_format(FALSE);
wasatend = (pos.col == (colnr_T)STRLEN(old));
if (*old != NUL && !trailblank && wasatend)
{
dec_cursor();
cc = gchar_cursor();
if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
&& has_format_option(FO_ONE_LETTER))
dec_cursor();
cc = gchar_cursor();
if (WHITECHAR(cc))
{
curwin->w_cursor = pos;
return;
}
curwin->w_cursor = pos;
}
#ifdef FEAT_COMMENTS
if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
&& get_leader_len(old, NULL, FALSE, TRUE) == 0)
return;
#endif
if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
{
--curwin->w_cursor.lnum;
if (u_save_cursor() == FAIL)
return;
}
saved_cursor = pos;
format_lines((linenr_T)-1, FALSE);
curwin->w_cursor = saved_cursor;
saved_cursor.lnum = 0;
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
{
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
coladvance((colnr_T)MAXCOL);
}
else
check_cursor_col();
if (!wasatend && has_format_option(FO_WHITE_PAR))
{
new = ml_get_curline();
len = (colnr_T)STRLEN(new);
if (curwin->w_cursor.col == len)
{
pnew = vim_strnsave(new, len + 2);
pnew[len] = ' ';
pnew[len + 1] = NUL;
ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
did_add_space = TRUE;
}
else
check_auto_format(FALSE);
}
check_cursor();
}
static void
check_auto_format(
int end_insert)
{
int c = ' ';
int cc;
if (did_add_space)
{
cc = gchar_cursor();
if (!WHITECHAR(cc))
did_add_space = FALSE;
else
{
if (!end_insert)
{
inc_cursor();
c = gchar_cursor();
dec_cursor();
}
if (c != NUL)
{
del_char(FALSE);
did_add_space = FALSE;
}
}
}
}
int
comp_textwidth(
int ff)
{
int textwidth;
textwidth = curbuf->b_p_tw;
if (textwidth == 0 && curbuf->b_p_wm)
{
textwidth = curwin->w_width - curbuf->b_p_wm;
#ifdef FEAT_CMDWIN
if (cmdwin_type != 0)
textwidth -= 1;
#endif
#ifdef FEAT_FOLDING
textwidth -= curwin->w_p_fdc;
#endif
#ifdef FEAT_SIGNS
if (signcolumn_on(curwin))
textwidth -= 1;
#endif
if (curwin->w_p_nu || curwin->w_p_rnu)
textwidth -= 8;
}
if (textwidth < 0)
textwidth = 0;
if (ff && textwidth == 0)
{
textwidth = curwin->w_width - 1;
if (textwidth > 79)
textwidth = 79;
}
return textwidth;
}
static void
redo_literal(int c)
{
char_u buf[10];
if (VIM_ISDIGIT(c))
{
vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
AppendToRedobuff(buf);
}
else
AppendCharToRedobuff(c);
}
static void
start_arrow(
pos_T *end_insert_pos)
{
start_arrow_common(end_insert_pos, TRUE);
}
static void
start_arrow_with_change(
pos_T *end_insert_pos,
int end_change)
{
start_arrow_common(end_insert_pos, end_change);
if (!end_change)
{
AppendCharToRedobuff(Ctrl_G);
AppendCharToRedobuff('U');
}
}
static void
start_arrow_common(
pos_T *end_insert_pos,
int end_change)
{
if (!arrow_used && end_change)
{
AppendToRedobuff(ESC_STR);
stop_insert(end_insert_pos, FALSE, FALSE);
arrow_used = TRUE;
}
#ifdef FEAT_SPELL
check_spell_redraw();
#endif
}
#ifdef FEAT_SPELL
static void
check_spell_redraw(void)
{
if (spell_redraw_lnum != 0)
{
linenr_T lnum = spell_redraw_lnum;
spell_redraw_lnum = 0;
redrawWinline(lnum, FALSE);
}
}
static void
spell_back_to_badword(void)
{
pos_T tpos = curwin->w_cursor;
spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
if (curwin->w_cursor.col != tpos.col)
start_arrow(&tpos);
}
#endif
int
stop_arrow(void)
{
if (arrow_used)
{
Insstart = curwin->w_cursor;
if (Insstart.col > Insstart_orig.col && !ins_need_undo)
update_Insstart_orig = FALSE;
Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
if (u_save_cursor() == OK)
{
arrow_used = FALSE;
ins_need_undo = FALSE;
}
ai_col = 0;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
orig_line_count = curbuf->b_ml.ml_line_count;
vr_lines_changed = 1;
}
#endif
ResetRedobuff();
AppendToRedobuff((char_u *)"1i");
new_insert_skip = 2;
}
else if (ins_need_undo)
{
if (u_save_cursor() == OK)
ins_need_undo = FALSE;
}
#ifdef FEAT_FOLDING
foldOpenCursor();
#endif
return (arrow_used || ins_need_undo ? FAIL : OK);
}
static void
stop_insert(
pos_T *end_insert_pos,
int esc,
int nomove)
{
int cc;
char_u *ptr;
stop_redo_ins();
replace_flush();
ptr = get_inserted();
if (did_restart_edit == 0 || (ptr != NULL
&& (int)STRLEN(ptr) > new_insert_skip))
{
vim_free(last_insert);
last_insert = ptr;
last_insert_skip = new_insert_skip;
}
else
vim_free(ptr);
if (!arrow_used && end_insert_pos != NULL)
{
if (!ins_need_undo && has_format_option(FO_AUTO))
{
pos_T tpos = curwin->w_cursor;
cc = 'x';
if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
{
dec_cursor();
cc = gchar_cursor();
if (!VIM_ISWHITE(cc))
curwin->w_cursor = tpos;
}
auto_format(TRUE, FALSE);
if (VIM_ISWHITE(cc))
{
if (gchar_cursor() != NUL)
inc_cursor();
#ifdef FEAT_VIRTUALEDIT
if (gchar_cursor() == NUL
&& curwin->w_cursor.lnum == tpos.lnum
&& curwin->w_cursor.col == tpos.col)
curwin->w_cursor.coladd = tpos.coladd;
#endif
}
}
check_auto_format(TRUE);
if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
&& curwin->w_cursor.lnum != end_insert_pos->lnum))
&& end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
{
pos_T tpos = curwin->w_cursor;
curwin->w_cursor = *end_insert_pos;
check_cursor_col();
for (;;)
{
if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
--curwin->w_cursor.col;
cc = gchar_cursor();
if (!VIM_ISWHITE(cc))
break;
if (del_char(TRUE) == FAIL)
break;
}
if (curwin->w_cursor.lnum != tpos.lnum)
curwin->w_cursor = tpos;
else
{
tpos = curwin->w_cursor;
tpos.col++;
if (cc != NUL && gchar_pos(&tpos) == NUL)
++curwin->w_cursor.col;
}
if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
{
int len = (int)STRLEN(ml_get_curline());
if (VIsual.col > len)
{
VIsual.col = len;
#ifdef FEAT_VIRTUALEDIT
VIsual.coladd = 0;
#endif
}
}
}
}
did_ai = FALSE;
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
if (end_insert_pos != NULL)
{
curbuf->b_op_start = Insstart;
curbuf->b_op_start_orig = Insstart_orig;
curbuf->b_op_end = *end_insert_pos;
}
}
void
set_last_insert(int c)
{
char_u *s;
vim_free(last_insert);
last_insert = alloc(MB_MAXBYTES * 3 + 5);
if (last_insert != NULL)
{
s = last_insert;
if (c < ' ' || c == DEL)
*s++ = Ctrl_V;
s = add_char2buf(c, s);
*s++ = ESC;
*s++ = NUL;
last_insert_skip = 0;
}
}
#if defined(EXITFREE) || defined(PROTO)
void
free_last_insert(void)
{
vim_free(last_insert);
last_insert = NULL;
# ifdef FEAT_INS_EXPAND
vim_free(compl_orig_text);
compl_orig_text = NULL;
# endif
}
#endif
char_u *
add_char2buf(int c, char_u *s)
{
#ifdef FEAT_MBYTE
char_u temp[MB_MAXBYTES + 1];
int i;
int len;
len = (*mb_char2bytes)(c, temp);
for (i = 0; i < len; ++i)
{
c = temp[i];
#endif
if (c == K_SPECIAL)
{
*s++ = K_SPECIAL;
*s++ = KS_SPECIAL;
*s++ = KE_FILLER;
}
#ifdef FEAT_GUI
else if (c == CSI)
{
*s++ = CSI;
*s++ = KS_EXTRA;
*s++ = (int)KE_CSI;
}
#endif
else
*s++ = c;
#ifdef FEAT_MBYTE
}
#endif
return s;
}
void
beginline(int flags)
{
if ((flags & BL_SOL) && !p_sol)
coladvance(curwin->w_curswant);
else
{
curwin->w_cursor.col = 0;
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
if (flags & (BL_WHITE | BL_SOL))
{
char_u *ptr;
for (ptr = ml_get_curline(); VIM_ISWHITE(*ptr)
&& !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
++curwin->w_cursor.col;
}
curwin->w_set_curswant = TRUE;
}
}
int
oneright(void)
{
char_u *ptr;
int l;
#ifdef FEAT_VIRTUALEDIT
if (virtual_active())
{
pos_T prevpos = curwin->w_cursor;
ptr = ml_get_cursor();
coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
# ifdef FEAT_MBYTE
(*mb_ptr2char)(ptr)
# else
*ptr
# endif
))
? ptr2cells(ptr) : 1));
curwin->w_set_curswant = TRUE;
return (prevpos.col != curwin->w_cursor.col
|| prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
}
#endif
ptr = ml_get_cursor();
if (*ptr == NUL)
return FAIL;
#ifdef FEAT_MBYTE
if (has_mbyte)
l = (*mb_ptr2len)(ptr);
else
#endif
l = 1;
if (ptr[l] == NUL
#ifdef FEAT_VIRTUALEDIT
&& (ve_flags & VE_ONEMORE) == 0
#endif
)
return FAIL;
curwin->w_cursor.col += l;
curwin->w_set_curswant = TRUE;
return OK;
}
int
oneleft(void)
{
#ifdef FEAT_VIRTUALEDIT
if (virtual_active())
{
# ifdef FEAT_LINEBREAK
int width;
# endif
int v = getviscol();
if (v == 0)
return FAIL;
# ifdef FEAT_LINEBREAK
width = 1;
for (;;)
{
coladvance(v - width);
if ((*p_sbr == NUL && !curwin->w_p_bri
# ifdef FEAT_MBYTE
&& !has_mbyte
# endif
) || getviscol() < v)
break;
++width;
}
# else
coladvance(v - 1);
# endif
if (curwin->w_cursor.coladd == 1)
{
char_u *ptr;
ptr = ml_get_cursor();
if (*ptr != TAB && vim_isprintc(
# ifdef FEAT_MBYTE
(*mb_ptr2char)(ptr)
# else
*ptr
# endif
) && ptr2cells(ptr) > 1)
curwin->w_cursor.coladd = 0;
}
curwin->w_set_curswant = TRUE;
return OK;
}
#endif
if (curwin->w_cursor.col == 0)
return FAIL;
curwin->w_set_curswant = TRUE;
--curwin->w_cursor.col;
#ifdef FEAT_MBYTE
if (has_mbyte)
mb_adjust_cursor();
#endif
return OK;
}
int
cursor_up(
long n,
int upd_topline)
{
linenr_T lnum;
if (n > 0)
{
lnum = curwin->w_cursor.lnum;
if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
return FAIL;
if (n >= lnum)
lnum = 1;
else
#ifdef FEAT_FOLDING
if (hasAnyFolding(curwin))
{
(void)hasFolding(lnum, &lnum, NULL);
while (n--)
{
--lnum;
if (lnum <= 1)
break;
if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
(void)hasFolding(lnum, &lnum, NULL);
}
if (lnum < 1)
lnum = 1;
}
else
#endif
lnum -= n;
curwin->w_cursor.lnum = lnum;
}
coladvance(curwin->w_curswant);
if (upd_topline)
update_topline();
return OK;
}
int
cursor_down(
long n,
int upd_topline)
{
linenr_T lnum;
if (n > 0)
{
lnum = curwin->w_cursor.lnum;
#ifdef FEAT_FOLDING
(void)hasFolding(lnum, NULL, &lnum);
#endif
if (lnum >= curbuf->b_ml.ml_line_count
|| (lnum + n > curbuf->b_ml.ml_line_count
&& vim_strchr(p_cpo, CPO_MINUS) != NULL))
return FAIL;
if (lnum + n >= curbuf->b_ml.ml_line_count)
lnum = curbuf->b_ml.ml_line_count;
else
#ifdef FEAT_FOLDING
if (hasAnyFolding(curwin))
{
linenr_T last;
while (n--)
{
if (hasFolding(lnum, NULL, &last))
lnum = last + 1;
else
++lnum;
if (lnum >= curbuf->b_ml.ml_line_count)
break;
}
if (lnum > curbuf->b_ml.ml_line_count)
lnum = curbuf->b_ml.ml_line_count;
}
else
#endif
lnum += n;
curwin->w_cursor.lnum = lnum;
}
coladvance(curwin->w_curswant);
if (upd_topline)
update_topline();
return OK;
}
int
stuff_inserted(
int c,
long count,
int no_esc)
{
char_u *esc_ptr;
char_u *ptr;
char_u *last_ptr;
char_u last = NUL;
ptr = get_last_insert();
if (ptr == NULL)
{
EMSG(_(e_noinstext));
return FAIL;
}
if (c != NUL)
stuffcharReadbuff(c);
if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
*esc_ptr = NUL;
last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
&& (no_esc || (*ptr == Ctrl_D && count > 1)))
{
last = *last_ptr;
*last_ptr = NUL;
}
do
{
stuffReadbuff(ptr);
if (last)
stuffReadbuff((char_u *)(last == '0'
? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
: IF_EB("\026^", CTRL_V_STR "^")));
}
while (--count > 0);
if (last)
*last_ptr = last;
if (esc_ptr != NULL)
*esc_ptr = ESC;
if (!no_esc)
stuffcharReadbuff(ESC);
return OK;
}
char_u *
get_last_insert(void)
{
if (last_insert == NULL)
return NULL;
return last_insert + last_insert_skip;
}
char_u *
get_last_insert_save(void)
{
char_u *s;
int len;
if (last_insert == NULL)
return NULL;
s = vim_strsave(last_insert + last_insert_skip);
if (s != NULL)
{
len = (int)STRLEN(s);
if (len > 0 && s[len - 1] == ESC)
s[len - 1] = NUL;
}
return s;
}
static int
echeck_abbr(int c)
{
if (p_paste || no_abbr || arrow_used)
return FALSE;
return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
}
static char_u *replace_stack = NULL;
static long replace_stack_nr = 0;
static long replace_stack_len = 0;
void
replace_push(
int c)
{
char_u *p;
if (replace_stack_nr < replace_offset)
return;
if (replace_stack_len <= replace_stack_nr)
{
replace_stack_len += 50;
p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
if (p == NULL)
{
replace_stack_len -= 50;
return;
}
if (replace_stack != NULL)
{
mch_memmove(p, replace_stack,
(size_t)(replace_stack_nr * sizeof(char_u)));
vim_free(replace_stack);
}
replace_stack = p;
}
p = replace_stack + replace_stack_nr - replace_offset;
if (replace_offset)
mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
*p = c;
++replace_stack_nr;
}
#if defined(FEAT_MBYTE) || defined(PROTO)
int
replace_push_mb(char_u *p)
{
int l = (*mb_ptr2len)(p);
int j;
for (j = l - 1; j >= 0; --j)
replace_push(p[j]);
return l;
}
#endif
static int
replace_pop(void)
{
if (replace_stack_nr == 0)
return -1;
return (int)replace_stack[--replace_stack_nr];
}
static void
replace_join(
int off)
{
int i;
for (i = replace_stack_nr; --i >= 0; )
if (replace_stack[i] == NUL && off-- <= 0)
{
--replace_stack_nr;
mch_memmove(replace_stack + i, replace_stack + i + 1,
(size_t)(replace_stack_nr - i));
return;
}
}
static void
replace_pop_ins(void)
{
int cc;
int oldState = State;
State = NORMAL;
while ((cc = replace_pop()) > 0)
{
#ifdef FEAT_MBYTE
mb_replace_pop_ins(cc);
#else
ins_char(cc);
#endif
dec_cursor();
}
State = oldState;
}
#ifdef FEAT_MBYTE
static void
mb_replace_pop_ins(int cc)
{
int n;
char_u buf[MB_MAXBYTES + 1];
int i;
int c;
if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
{
buf[0] = cc;
for (i = 1; i < n; ++i)
buf[i] = replace_pop();
ins_bytes_len(buf, n);
}
else
ins_char(cc);
if (enc_utf8)
for (;;)
{
c = replace_pop();
if (c == -1)
break;
if ((n = MB_BYTE2LEN(c)) == 1)
{
replace_push(c);
break;
}
else
{
buf[0] = c;
for (i = 1; i < n; ++i)
buf[i] = replace_pop();
if (utf_iscomposing(utf_ptr2char(buf)))
ins_bytes_len(buf, n);
else
{
for (i = n - 1; i >= 0; --i)
replace_push(buf[i]);
break;
}
}
}
}
#endif
static void
replace_flush(void)
{
vim_free(replace_stack);
replace_stack = NULL;
replace_stack_len = 0;
replace_stack_nr = 0;
}
static void
replace_do_bs(int limit_col)
{
int cc;
#ifdef FEAT_VREPLACE
int orig_len = 0;
int ins_len;
int orig_vcols = 0;
colnr_T start_vcol;
char_u *p;
int i;
int vcol;
#endif
cc = replace_pop();
if (cc > 0)
{
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
}
#endif
#ifdef FEAT_MBYTE
if (has_mbyte)
{
(void)del_char_after_col(limit_col);
# ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
orig_len = (int)STRLEN(ml_get_cursor());
# endif
replace_push(cc);
}
else
#endif
{
pchar_cursor(cc);
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
orig_len = (int)STRLEN(ml_get_cursor()) - 1;
#endif
}
replace_pop_ins();
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
p = ml_get_cursor();
ins_len = (int)STRLEN(p) - orig_len;
vcol = start_vcol;
for (i = 0; i < ins_len; ++i)
{
vcol += chartabsize(p + i, vcol);
#ifdef FEAT_MBYTE
i += (*mb_ptr2len)(p) - 1;
#endif
}
vcol -= start_vcol;
curwin->w_cursor.col += ins_len;
while (vcol > orig_vcols && gchar_cursor() == ' ')
{
del_char(FALSE);
++orig_vcols;
}
curwin->w_cursor.col -= ins_len;
}
#endif
changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
}
else if (cc == 0)
(void)del_char_after_col(limit_col);
}
#ifdef FEAT_CINDENT
static int
cindent_on(void)
{
return (!p_paste && (curbuf->b_p_cin
# ifdef FEAT_EVAL
|| *curbuf->b_p_inde != NUL
# endif
));
}
#endif
#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
void
fixthisline(int (*get_the_indent)(void))
{
int amount = get_the_indent();
if (amount >= 0)
{
change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
if (linewhite(curwin->w_cursor.lnum))
did_ai = TRUE;
}
}
void
fix_indent(void)
{
if (p_paste)
return;
# ifdef FEAT_LISP
if (curbuf->b_p_lisp && curbuf->b_p_ai)
fixthisline(get_lisp_indent);
# endif
# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
else
# endif
# ifdef FEAT_CINDENT
if (cindent_on())
do_c_expr_indent();
# endif
}
#endif
#ifdef FEAT_CINDENT
int
in_cinkeys(
int keytyped,
int when,
int line_is_empty)
{
char_u *look;
int try_match;
int try_match_word;
char_u *p;
char_u *line;
int icase;
int i;
if (keytyped == NUL)
return FALSE;
#ifdef FEAT_EVAL
if (*curbuf->b_p_inde != NUL)
look = curbuf->b_p_indk;
else
#endif
look = curbuf->b_p_cink;
while (*look)
{
switch (when)
{
case '*': try_match = (*look == '*'); break;
case '!': try_match = (*look == '!'); break;
default: try_match = (*look != '*'); break;
}
if (*look == '*' || *look == '!')
++look;
if (*look == '0')
{
try_match_word = try_match;
if (!line_is_empty)
try_match = FALSE;
++look;
}
else
try_match_word = FALSE;
if (*look == '^'
#ifdef EBCDIC
&& (Ctrl_chr(look[1]) != 0)
#else
&& look[1] >= '?' && look[1] <= '_'
#endif
)
{
if (try_match && keytyped == Ctrl_chr(look[1]))
return TRUE;
look += 2;
}
else if (*look == 'o')
{
if (try_match && keytyped == KEY_OPEN_FORW)
return TRUE;
++look;
}
else if (*look == 'O')
{
if (try_match && keytyped == KEY_OPEN_BACK)
return TRUE;
++look;
}
else if (*look == 'e')
{
if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
{
p = ml_get_curline();
if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
return TRUE;
}
++look;
}
else if (*look == ':')
{
if (try_match && keytyped == ':')
{
p = ml_get_curline();
if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
return TRUE;
p = ml_get_curline();
if (curwin->w_cursor.col > 2
&& p[curwin->w_cursor.col - 1] == ':'
&& p[curwin->w_cursor.col - 2] == ':')
{
p[curwin->w_cursor.col - 1] = ' ';
i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
|| cin_islabel());
p = ml_get_curline();
p[curwin->w_cursor.col - 1] = ':';
if (i)
return TRUE;
}
}
++look;
}
else if (*look == '<')
{
if (try_match)
{
if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
&& keytyped == look[1])
return TRUE;
if (keytyped == get_special_key_code(look + 1))
return TRUE;
}
while (*look && *look != '>')
look++;
while (*look == '>')
look++;
}
else if (*look == '=' && look[1] != ',' && look[1] != NUL)
{
++look;
if (*look == '~')
{
icase = TRUE;
++look;
}
else
icase = FALSE;
p = vim_strchr(look, ',');
if (p == NULL)
p = look + STRLEN(look);
if ((try_match || try_match_word)
&& curwin->w_cursor.col >= (colnr_T)(p - look))
{
int match = FALSE;
#ifdef FEAT_INS_EXPAND
if (keytyped == KEY_COMPLETE)
{
char_u *s;
line = ml_get_curline();
# ifdef FEAT_MBYTE
if (has_mbyte)
{
char_u *n;
for (s = line + curwin->w_cursor.col; s > line; s = n)
{
n = mb_prevptr(line, s);
if (!vim_iswordp(n))
break;
}
}
else
# endif
for (s = line + curwin->w_cursor.col; s > line; --s)
if (!vim_iswordc(s[-1]))
break;
if (s + (p - look) <= line + curwin->w_cursor.col
&& (icase
? MB_STRNICMP(s, look, p - look)
: STRNCMP(s, look, p - look)) == 0)
match = TRUE;
}
else
#endif
if (keytyped == (int)p[-1] || (icase && keytyped < 256
&& TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
{
line = ml_get_cursor();
if ((curwin->w_cursor.col == (colnr_T)(p - look)
|| !vim_iswordc(line[-(p - look) - 1]))
&& (icase
? MB_STRNICMP(line - (p - look), look, p - look)
: STRNCMP(line - (p - look), look, p - look))
== 0)
match = TRUE;
}
if (match && try_match_word && !try_match)
{
if (getwhitecols_curline() !=
(int)(curwin->w_cursor.col - (p - look)))
match = FALSE;
}
if (match)
return TRUE;
}
look = p;
}
else
{
if (try_match && *look == keytyped)
return TRUE;
if (*look != NUL)
++look;
}
look = skip_to_option_part(look);
}
return FALSE;
}
#endif
#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
int
hkmap(int c)
{
if (p_hkmapp)
{
enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
static char_u map[26] =
{(char_u)hALEF, (char_u)BET , (char_u)hKAF ,
(char_u)DALET, (char_u)-1 , (char_u)PEIsofit,
(char_u)GIMEL, (char_u)HEI , (char_u)IUD ,
(char_u)HET , (char_u)KOF , (char_u)LAMED ,
(char_u)MEM , (char_u)NUN , (char_u)SAMEH ,
(char_u)PEI , (char_u)-1 , (char_u)RESH ,
(char_u)ZAIN , (char_u)TAV , (char_u)TET ,
(char_u)VAV , (char_u)hSHIN, (char_u)-1 ,
(char_u)AIN , (char_u)ZADI };
if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
return (int)(map[CharOrd(c)] - 1 + p_aleph);
else if (c == 'x')
return 'X';
else if (c == 'q')
return '\'';
else if (c == 246)
return ' ';
else if (c == 228)
return ' ';
else if (c == 252)
return ' ';
#ifdef EBCDIC
else if (islower(c))
#else
else if (c >= 'a' && c <= 'z')
#endif
return (int)(map[CharOrdLow(c)] + p_aleph);
else
return c;
}
else
{
switch (c)
{
case '`': return ';';
case '/': return '.';
case '\'': return ',';
case 'q': return '/';
case 'w': return '\'';
case ',': c = '{'; break;
case '.': c = 'v'; break;
case ';': c = 't'; break;
default: {
static char str[] = "zqbcxlsjphmkwonu ydafe rig";
#ifdef EBCDIC
if (!islower(c))
#else
if (c < 'a' || c > 'z')
#endif
return c;
c = str[CharOrdLow(c)];
break;
}
}
return (int)(CharOrdLow(c) + p_aleph);
}
}
#endif
static void
ins_reg(void)
{
int need_redraw = FALSE;
int regname;
int literally = 0;
int vis_active = VIsual_active;
pc_status = PC_STATUS_UNSET;
if (redrawing() && !char_avail())
{
ins_redraw(FALSE);
edit_putchar('"', TRUE);
#ifdef FEAT_CMDL_INFO
add_to_showcmd_c(Ctrl_R);
#endif
}
#ifdef USE_ON_FLY_SCROLL
dont_scroll = TRUE;
#endif
++no_mapping;
regname = plain_vgetc();
LANGMAP_ADJUST(regname, TRUE);
if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
{
literally = regname;
#ifdef FEAT_CMDL_INFO
add_to_showcmd_c(literally);
#endif
regname = plain_vgetc();
LANGMAP_ADJUST(regname, TRUE);
}
--no_mapping;
#ifdef FEAT_EVAL
++no_u_sync;
if (regname == '=')
{
# ifdef USE_IM_CONTROL
int im_on = im_get_status();
# endif
u_sync_once = 2;
regname = get_expr_register();
# ifdef USE_IM_CONTROL
if (im_on)
im_set_active(TRUE);
# endif
}
if (regname == NUL || !valid_yank_reg(regname, FALSE))
{
vim_beep(BO_REG);
need_redraw = TRUE;
}
else
{
#endif
if (literally == Ctrl_O || literally == Ctrl_P)
{
AppendCharToRedobuff(Ctrl_R);
AppendCharToRedobuff(literally);
AppendCharToRedobuff(regname);
do_put(regname, BACKWARD, 1L,
(literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
}
else if (insert_reg(regname, literally) == FAIL)
{
vim_beep(BO_REG);
need_redraw = TRUE;
}
else if (stop_insert_mode)
need_redraw = TRUE;
#ifdef FEAT_EVAL
}
--no_u_sync;
if (u_sync_once == 1)
ins_need_undo = TRUE;
u_sync_once = 0;
#endif
#ifdef FEAT_CMDL_INFO
clear_showcmd();
#endif
if (need_redraw || stuff_empty())
edit_unputchar();
if (!vis_active && VIsual_active)
end_visual_mode();
}
static void
ins_ctrl_g(void)
{
int c;
#ifdef FEAT_INS_EXPAND
setcursor();
#endif
++no_mapping;
c = plain_vgetc();
--no_mapping;
switch (c)
{
case K_UP:
case Ctrl_K:
case 'k': ins_up(TRUE);
break;
case K_DOWN:
case Ctrl_J:
case 'j': ins_down(TRUE);
break;
case 'u': u_sync(TRUE);
ins_need_undo = TRUE;
update_Insstart_orig = FALSE;
Insstart = curwin->w_cursor;
break;
case 'U':
dont_sync_undo = MAYBE;
break;
default: vim_beep(BO_CTRLG);
}
}
static void
ins_ctrl_hat(void)
{
if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
{
if (State & LANGMAP)
{
curbuf->b_p_iminsert = B_IMODE_NONE;
State &= ~LANGMAP;
}
else
{
curbuf->b_p_iminsert = B_IMODE_LMAP;
State |= LANGMAP;
#ifdef USE_IM_CONTROL
im_set_active(FALSE);
#endif
}
}
#ifdef USE_IM_CONTROL
else
{
if (im_get_status())
{
curbuf->b_p_iminsert = B_IMODE_NONE;
im_set_active(FALSE);
}
else
{
curbuf->b_p_iminsert = B_IMODE_IM;
State &= ~LANGMAP;
im_set_active(TRUE);
}
}
#endif
set_iminsert_global();
showmode();
#ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor(TRUE, FALSE);
#endif
#if defined(FEAT_KEYMAP)
status_redraw_curbuf();
#endif
}
static int
ins_esc(
long *count,
int cmdchar,
int nomove)
{
int temp;
static int disabled_redraw = FALSE;
#ifdef FEAT_SPELL
check_spell_redraw();
#endif
#if defined(FEAT_HANGULIN)
# if defined(ESC_CHG_TO_ENG_MODE)
hangul_input_state_set(0);
# endif
if (composing_hangul)
{
push_raw_key(composing_hangul_buffer, 2);
composing_hangul = 0;
}
#endif
temp = curwin->w_cursor.col;
if (disabled_redraw)
{
--RedrawingDisabled;
disabled_redraw = FALSE;
}
if (!arrow_used)
{
if (cmdchar != 'r' && cmdchar != 'v')
AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
if (*count > 0)
{
line_breakcheck();
if (got_int)
*count = 0;
}
if (--*count > 0)
{
if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
State &= ~REPLACE_FLAG;
(void)start_redo_ins();
if (cmdchar == 'r' || cmdchar == 'v')
stuffRedoReadbuff(ESC_STR);
++RedrawingDisabled;
disabled_redraw = TRUE;
return FALSE;
}
stop_insert(&curwin->w_cursor, TRUE, nomove);
undisplay_dollar();
}
if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
curwin->w_set_curswant = TRUE;
if (!cmdmod.keepjumps)
curbuf->b_last_insert = curwin->w_cursor;
if (!nomove
&& (curwin->w_cursor.col != 0
#ifdef FEAT_VIRTUALEDIT
|| curwin->w_cursor.coladd > 0
#endif
)
&& (restart_edit == NUL
|| (gchar_cursor() == NUL && !VIsual_active))
#ifdef FEAT_RIGHTLEFT
&& !revins_on
#endif
)
{
#ifdef FEAT_VIRTUALEDIT
if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
{
oneleft();
if (restart_edit != NUL)
++curwin->w_cursor.coladd;
}
else
#endif
{
--curwin->w_cursor.col;
#ifdef FEAT_MBYTE
if (has_mbyte)
mb_adjust_cursor();
#endif
}
}
#ifdef USE_IM_CONTROL
if (!(State & LANGMAP))
im_save_status(&curbuf->b_p_iminsert);
im_set_active(FALSE);
#endif
State = NORMAL;
changed_cline_bef_curs();
#ifdef FEAT_MOUSE
setmouse();
#endif
#ifdef CURSOR_SHAPE
ui_cursor_shape();
#endif
if (!p_ek)
out_str(T_BE);
if (Recording || restart_edit != NUL)
showmode();
else if (p_smd)
MSG("");
return TRUE;
}
#ifdef FEAT_RIGHTLEFT
static void
ins_ctrl_(void)
{
if (revins_on && revins_chars && revins_scol >= 0)
{
while (gchar_cursor() != NUL && revins_chars--)
++curwin->w_cursor.col;
}
p_ri = !p_ri;
revins_on = (State == INSERT && p_ri);
if (revins_on)
{
revins_scol = curwin->w_cursor.col;
revins_legal++;
revins_chars = 0;
undisplay_dollar();
}
else
revins_scol = -1;
#ifdef FEAT_FKMAP
if (p_altkeymap)
{
arrow_used = TRUE;
(void)stop_arrow();
p_fkmap = curwin->w_p_rl ^ p_ri;
if (p_fkmap && p_ri)
State = INSERT;
}
else
#endif
p_hkmap = curwin->w_p_rl ^ p_ri;
showmode();
}
#endif
static int
ins_start_select(int c)
{
if (km_startsel)
switch (c)
{
case K_KHOME:
case K_KEND:
case K_PAGEUP:
case K_KPAGEUP:
case K_PAGEDOWN:
case K_KPAGEDOWN:
# ifdef MACOS_X
case K_LEFT:
case K_RIGHT:
case K_UP:
case K_DOWN:
case K_END:
case K_HOME:
# endif
if (!(mod_mask & MOD_MASK_SHIFT))
break;
case K_S_LEFT:
case K_S_RIGHT:
case K_S_UP:
case K_S_DOWN:
case K_S_END:
case K_S_HOME:
start_selection();
stuffcharReadbuff(Ctrl_O);
if (mod_mask)
{
char_u buf[4];
buf[0] = K_SPECIAL;
buf[1] = KS_MODIFIER;
buf[2] = mod_mask;
buf[3] = NUL;
stuffReadbuff(buf);
}
stuffcharReadbuff(c);
return TRUE;
}
return FALSE;
}
static void
ins_insert(int replaceState)
{
#ifdef FEAT_FKMAP
if (p_fkmap && p_ri)
{
beep_flush();
EMSG(farsi_text_3);
return;
}
#endif
#ifdef FEAT_AUTOCMD
# ifdef FEAT_EVAL
set_vim_var_string(VV_INSERTMODE,
(char_u *)((State & REPLACE_FLAG) ? "i" :
# ifdef FEAT_VREPLACE
replaceState == VREPLACE ? "v" :
# endif
"r"), 1);
# endif
apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
#endif
if (State & REPLACE_FLAG)
State = INSERT | (State & LANGMAP);
else
State = replaceState | (State & LANGMAP);
AppendCharToRedobuff(K_INS);
showmode();
#ifdef CURSOR_SHAPE
ui_cursor_shape();
#endif
}
static void
ins_ctrl_o(void)
{
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
restart_edit = 'V';
else
#endif
if (State & REPLACE_FLAG)
restart_edit = 'R';
else
restart_edit = 'I';
#ifdef FEAT_VIRTUALEDIT
if (virtual_active())
ins_at_eol = FALSE;
else
#endif
ins_at_eol = (gchar_cursor() == NUL);
}
static void
ins_shift(int c, int lastc)
{
if (stop_arrow() == FAIL)
return;
AppendCharToRedobuff(c);
if (c == Ctrl_D && (lastc == '0' || lastc == '^')
&& curwin->w_cursor.col > 0)
{
--curwin->w_cursor.col;
(void)del_char(FALSE);
if (State & REPLACE_FLAG)
replace_pop_ins();
if (lastc == '^')
old_indent = get_indent();
change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
}
else
change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
if (did_ai && *skipwhite(ml_get_curline()) != NUL)
did_ai = FALSE;
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
#ifdef FEAT_CINDENT
can_cindent = FALSE;
#endif
}
static void
ins_del(void)
{
int temp;
if (stop_arrow() == FAIL)
return;
if (gchar_cursor() == NUL)
{
temp = curwin->w_cursor.col;
if (!can_bs(BS_EOL)
|| do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
vim_beep(BO_BS);
else
curwin->w_cursor.col = temp;
}
else if (del_char(FALSE) == FAIL)
vim_beep(BO_BS);
did_ai = FALSE;
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
AppendCharToRedobuff(K_DEL);
}
static void ins_bs_one(colnr_T *vcolp);
static void
ins_bs_one(colnr_T *vcolp)
{
dec_cursor();
getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
if (State & REPLACE_FLAG)
{
if (curwin->w_cursor.lnum != Insstart.lnum
|| curwin->w_cursor.col >= Insstart.col)
replace_do_bs(-1);
}
else
(void)del_char(FALSE);
}
static int
ins_bs(
int c,
int mode,
int *inserted_space_p)
{
linenr_T lnum;
int cc;
int temp = 0;
colnr_T save_col;
colnr_T mincol;
int did_backspace = FALSE;
int in_indent;
int oldState;
#ifdef FEAT_MBYTE
int cpc[MAX_MCO];
#endif
if ( BUFEMPTY()
|| (
#ifdef FEAT_RIGHTLEFT
!revins_on &&
#endif
((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
|| (!can_bs(BS_START)
&& (arrow_used
|| (curwin->w_cursor.lnum == Insstart_orig.lnum
&& curwin->w_cursor.col <= Insstart_orig.col)))
|| (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
&& curwin->w_cursor.col <= ai_col)
|| (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
{
vim_beep(BO_BS);
return FALSE;
}
if (stop_arrow() == FAIL)
return FALSE;
in_indent = inindent(0);
#ifdef FEAT_CINDENT
if (in_indent)
can_cindent = FALSE;
#endif
#ifdef FEAT_COMMENTS
end_comment_pending = NUL;
#endif
#ifdef FEAT_RIGHTLEFT
if (revins_on)
inc_cursor();
#endif
#ifdef FEAT_VIRTUALEDIT
if (curwin->w_cursor.coladd > 0)
{
if (mode == BACKSPACE_CHAR)
{
--curwin->w_cursor.coladd;
return TRUE;
}
if (mode == BACKSPACE_WORD)
{
curwin->w_cursor.coladd = 0;
return TRUE;
}
curwin->w_cursor.coladd = 0;
}
#endif
if (curwin->w_cursor.col == 0)
{
lnum = Insstart.lnum;
if (curwin->w_cursor.lnum == lnum
#ifdef FEAT_RIGHTLEFT
|| revins_on
#endif
)
{
if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
(linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
return FALSE;
--Insstart.lnum;
Insstart.col = (colnr_T)STRLEN(ml_get(Insstart.lnum));
}
cc = -1;
if (State & REPLACE_FLAG)
cc = replace_pop();
if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
{
dec_cursor();
}
else
{
#ifdef FEAT_VREPLACE
if (!(State & VREPLACE_FLAG)
|| curwin->w_cursor.lnum > orig_line_count)
#endif
{
temp = gchar_cursor();
--curwin->w_cursor.lnum;
if (has_format_option(FO_AUTO)
&& has_format_option(FO_WHITE_PAR))
{
char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
TRUE);
int len;
len = (int)STRLEN(ptr);
if (len > 0 && ptr[len - 1] == ' ')
ptr[len - 1] = NUL;
}
(void)do_join(2, FALSE, FALSE, FALSE, FALSE);
if (temp == NUL && gchar_cursor() != NUL)
inc_cursor();
}
#ifdef FEAT_VREPLACE
else
dec_cursor();
#endif
if (State & REPLACE_FLAG)
{
oldState = State;
State = NORMAL;
while (cc > 0)
{
save_col = curwin->w_cursor.col;
#ifdef FEAT_MBYTE
mb_replace_pop_ins(cc);
#else
ins_char(cc);
#endif
curwin->w_cursor.col = save_col;
cc = replace_pop();
}
replace_pop_ins();
State = oldState;
}
}
did_ai = FALSE;
}
else
{
#ifdef FEAT_RIGHTLEFT
if (revins_on)
dec_cursor();
#endif
mincol = 0;
if (mode == BACKSPACE_LINE
&& (curbuf->b_p_ai
#ifdef FEAT_CINDENT
|| cindent_on()
#endif
)
#ifdef FEAT_RIGHTLEFT
&& !revins_on
#endif
)
{
save_col = curwin->w_cursor.col;
beginline(BL_WHITE);
if (curwin->w_cursor.col < save_col)
mincol = curwin->w_cursor.col;
curwin->w_cursor.col = save_col;
}
if ( mode == BACKSPACE_CHAR
&& ((p_sta && in_indent)
|| (get_sts_value() != 0
&& curwin->w_cursor.col > 0
&& (*(ml_get_cursor() - 1) == TAB
|| (*(ml_get_cursor() - 1) == ' '
&& (!*inserted_space_p
|| arrow_used))))))
{
int ts;
colnr_T vcol;
colnr_T want_vcol;
colnr_T start_vcol;
*inserted_space_p = FALSE;
if (p_sta && in_indent)
ts = (int)get_sw_value(curbuf);
else
ts = (int)get_sts_value();
getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
start_vcol = vcol;
dec_cursor();
getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
inc_cursor();
want_vcol = (want_vcol / ts) * ts;
while (vcol > want_vcol
&& (cc = *(ml_get_cursor() - 1), VIM_ISWHITE(cc)))
ins_bs_one(&vcol);
while (vcol < want_vcol)
{
if (curwin->w_cursor.lnum == Insstart_orig.lnum
&& curwin->w_cursor.col < Insstart_orig.col)
Insstart_orig.col = curwin->w_cursor.col;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
ins_char(' ');
else
#endif
{
ins_str((char_u *)" ");
if ((State & REPLACE_FLAG))
replace_push(NUL);
}
getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
}
if (vcol >= start_vcol)
ins_bs_one(&vcol);
}
else
{
#ifdef FEAT_MBYTE
int cclass = 0, prev_cclass = 0;
if (has_mbyte)
cclass = mb_get_class(ml_get_cursor());
#endif
do
{
#ifdef FEAT_RIGHTLEFT
if (!revins_on)
#endif
dec_cursor();
cc = gchar_cursor();
#ifdef FEAT_MBYTE
if (has_mbyte)
{
prev_cclass = cclass;
cclass = mb_get_class(ml_get_cursor());
}
#endif
if (mode == BACKSPACE_WORD && !vim_isspace(cc))
{
mode = BACKSPACE_WORD_NOT_SPACE;
temp = vim_iswordc(cc);
}
else if (mode == BACKSPACE_WORD_NOT_SPACE
&& ((vim_isspace(cc) || vim_iswordc(cc) != temp)
#ifdef FEAT_MBYTE
|| prev_cclass != cclass
#endif
))
{
#ifdef FEAT_RIGHTLEFT
if (!revins_on)
#endif
inc_cursor();
#ifdef FEAT_RIGHTLEFT
else if (State & REPLACE_FLAG)
dec_cursor();
#endif
break;
}
if (State & REPLACE_FLAG)
replace_do_bs(-1);
else
{
#ifdef FEAT_MBYTE
if (enc_utf8 && p_deco)
(void)utfc_ptr2char(ml_get_cursor(), cpc);
#endif
(void)del_char(FALSE);
#ifdef FEAT_MBYTE
if (enc_utf8 && p_deco && cpc[0] != NUL)
inc_cursor();
#endif
#ifdef FEAT_RIGHTLEFT
if (revins_chars)
{
revins_chars--;
revins_legal++;
}
if (revins_on && gchar_cursor() == NUL)
break;
#endif
}
if (mode == BACKSPACE_CHAR)
break;
} while (
#ifdef FEAT_RIGHTLEFT
revins_on ||
#endif
(curwin->w_cursor.col > mincol
&& (curwin->w_cursor.lnum != Insstart_orig.lnum
|| curwin->w_cursor.col != Insstart_orig.col)));
}
did_backspace = TRUE;
}
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
if (curwin->w_cursor.col <= 1)
did_ai = FALSE;
AppendCharToRedobuff(c);
if (curwin->w_cursor.lnum == Insstart_orig.lnum
&& curwin->w_cursor.col < Insstart_orig.col)
Insstart_orig.col = curwin->w_cursor.col;
if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
dollar_vcol = curwin->w_virtcol;
#ifdef FEAT_FOLDING
if (did_backspace)
foldOpenCursor();
#endif
return did_backspace;
}
#ifdef FEAT_MOUSE
static void
ins_mouse(int c)
{
pos_T tpos;
win_T *old_curwin = curwin;
# ifdef FEAT_GUI
if (!gui.in_use)
# endif
if (!mouse_has(MOUSE_INSERT))
return;
undisplay_dollar();
tpos = curwin->w_cursor;
if (do_mouse(NULL, c, BACKWARD, 1L, 0))
{
win_T *new_curwin = curwin;
if (curwin != old_curwin && win_valid(old_curwin))
{
curwin = old_curwin;
curbuf = curwin->w_buffer;
}
start_arrow(curwin == old_curwin ? &tpos : NULL);
if (curwin != new_curwin && win_valid(new_curwin))
{
curwin = new_curwin;
curbuf = curwin->w_buffer;
}
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
}
redraw_statuslines();
}
static void
ins_mousescroll(int dir)
{
pos_T tpos;
win_T *old_curwin = curwin, *wp;
# ifdef FEAT_INS_EXPAND
int did_scroll = FALSE;
# endif
tpos = curwin->w_cursor;
if (mouse_row >= 0 && mouse_col >= 0)
{
int row, col;
row = mouse_row;
col = mouse_col;
wp = mouse_find_win(&row, &col);
if (wp == NULL)
return;
curwin = wp;
curbuf = curwin->w_buffer;
}
if (curwin == old_curwin)
undisplay_dollar();
# ifdef FEAT_INS_EXPAND
if (!pum_visible() || curwin != old_curwin)
# endif
{
if (dir == MSCR_DOWN || dir == MSCR_UP)
{
if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
scroll_redraw(dir,
(long)(curwin->w_botline - curwin->w_topline));
else
scroll_redraw(dir, 3L);
}
#ifdef FEAT_GUI
else
{
int val, step = 6;
if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
step = curwin->w_width;
val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
if (val < 0)
val = 0;
gui_do_horiz_scroll(val, TRUE);
}
#endif
# ifdef FEAT_INS_EXPAND
did_scroll = TRUE;
# endif
}
curwin->w_redr_status = TRUE;
curwin = old_curwin;
curbuf = curwin->w_buffer;
# ifdef FEAT_INS_EXPAND
if (pum_visible() && did_scroll)
{
redraw_all_later(NOT_VALID);
ins_compl_show_pum();
}
# endif
if (!EQUAL_POS(curwin->w_cursor, tpos))
{
start_arrow(&tpos);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
}
}
#endif
int
bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
{
int c;
char_u buf[NUMBUFLEN + MB_MAXBYTES];
int idx = 0;
char_u *end = find_termcode((char_u *)"PE");
int ret_char = -1;
int save_allow_keys = allow_keys;
int save_paste = p_paste;
int save_ai = curbuf->b_p_ai;
if (STRLEN(end) >= NUMBUFLEN)
end = NULL;
++no_mapping;
allow_keys = 0;
p_paste = TRUE;
curbuf->b_p_ai = FALSE;
for (;;)
{
if (end == NULL && vpeekc() == NUL)
break;
c = plain_vgetc();
#ifdef FEAT_MBYTE
if (has_mbyte)
idx += (*mb_char2bytes)(c, buf + idx);
else
#endif
buf[idx++] = c;
buf[idx] = NUL;
if (end != NULL && STRNCMP(buf, end, idx) == 0)
{
if (end[idx] == NUL)
break;
continue;
}
if (!drop)
{
switch (mode)
{
case PASTE_CMDLINE:
put_on_cmdline(buf, idx, TRUE);
break;
case PASTE_EX:
if (gap != NULL && ga_grow(gap, idx) == OK)
{
mch_memmove((char *)gap->ga_data + gap->ga_len,
buf, (size_t)idx);
gap->ga_len += idx;
}
break;
case PASTE_INSERT:
if (stop_arrow() == OK)
{
c = buf[0];
if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
ins_eol(c);
else
{
ins_char_bytes(buf, idx);
AppendToRedobuffLit(buf, idx);
}
}
break;
case PASTE_ONE_CHAR:
if (ret_char == -1)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
ret_char = (*mb_ptr2char)(buf);
else
#endif
ret_char = buf[0];
}
break;
}
}
idx = 0;
}
--no_mapping;
allow_keys = save_allow_keys;
p_paste = save_paste;
curbuf->b_p_ai = save_ai;
return ret_char;
}
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
static void
ins_tabline(int c)
{
if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
|| (current_tab != 0 && current_tab != tabpage_index(curtab)))
{
undisplay_dollar();
start_arrow(&curwin->w_cursor);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
}
if (c == K_TABLINE)
goto_tabpage(current_tab);
else
{
handle_tabmenu();
redraw_statuslines();
}
}
#endif
#if defined(FEAT_GUI) || defined(PROTO)
void
ins_scroll(void)
{
pos_T tpos;
undisplay_dollar();
tpos = curwin->w_cursor;
if (gui_do_scroll())
{
start_arrow(&tpos);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
}
}
void
ins_horscroll(void)
{
pos_T tpos;
undisplay_dollar();
tpos = curwin->w_cursor;
if (gui_do_horiz_scroll(scrollbar_value, FALSE))
{
start_arrow(&tpos);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
}
}
#endif
static void
ins_left(
int end_change)
{
pos_T tpos;
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_HOR) && KeyTyped)
foldOpenCursor();
#endif
undisplay_dollar();
tpos = curwin->w_cursor;
if (oneleft() == OK)
{
#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
if (p_imst == IM_OVER_THE_SPOT || !im_is_preediting())
#endif
{
start_arrow_with_change(&tpos, end_change);
if (!end_change)
AppendCharToRedobuff(K_LEFT);
}
#ifdef FEAT_RIGHTLEFT
if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
revins_legal++;
revins_chars++;
#endif
}
else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
{
start_arrow(&tpos);
--(curwin->w_cursor.lnum);
coladvance((colnr_T)MAXCOL);
curwin->w_set_curswant = TRUE;
}
else
vim_beep(BO_CRSR);
dont_sync_undo = FALSE;
}
static void
ins_home(int c)
{
pos_T tpos;
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_HOR) && KeyTyped)
foldOpenCursor();
#endif
undisplay_dollar();
tpos = curwin->w_cursor;
if (c == K_C_HOME)
curwin->w_cursor.lnum = 1;
curwin->w_cursor.col = 0;
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
curwin->w_curswant = 0;
start_arrow(&tpos);
}
static void
ins_end(int c)
{
pos_T tpos;
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_HOR) && KeyTyped)
foldOpenCursor();
#endif
undisplay_dollar();
tpos = curwin->w_cursor;
if (c == K_C_END)
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
coladvance((colnr_T)MAXCOL);
curwin->w_curswant = MAXCOL;
start_arrow(&tpos);
}
static void
ins_s_left(void)
{
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_HOR) && KeyTyped)
foldOpenCursor();
#endif
undisplay_dollar();
if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
{
start_arrow(&curwin->w_cursor);
(void)bck_word(1L, FALSE, FALSE);
curwin->w_set_curswant = TRUE;
}
else
vim_beep(BO_CRSR);
}
static void
ins_right(
int end_change)
{
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_HOR) && KeyTyped)
foldOpenCursor();
#endif
undisplay_dollar();
if (gchar_cursor() != NUL
#ifdef FEAT_VIRTUALEDIT
|| virtual_active()
#endif
)
{
start_arrow_with_change(&curwin->w_cursor, end_change);
if (!end_change)
AppendCharToRedobuff(K_RIGHT);
curwin->w_set_curswant = TRUE;
#ifdef FEAT_VIRTUALEDIT
if (virtual_active())
oneright();
else
#endif
{
#ifdef FEAT_MBYTE
if (has_mbyte)
curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
else
#endif
++curwin->w_cursor.col;
}
#ifdef FEAT_RIGHTLEFT
revins_legal++;
if (revins_chars)
revins_chars--;
#endif
}
else if (vim_strchr(p_ww, ']') != NULL
&& curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
{
start_arrow(&curwin->w_cursor);
curwin->w_set_curswant = TRUE;
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
else
vim_beep(BO_CRSR);
dont_sync_undo = FALSE;
}
static void
ins_s_right(void)
{
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_HOR) && KeyTyped)
foldOpenCursor();
#endif
undisplay_dollar();
if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
|| gchar_cursor() != NUL)
{
start_arrow(&curwin->w_cursor);
(void)fwd_word(1L, FALSE, 0);
curwin->w_set_curswant = TRUE;
}
else
vim_beep(BO_CRSR);
}
static void
ins_up(
int startcol)
{
pos_T tpos;
linenr_T old_topline = curwin->w_topline;
#ifdef FEAT_DIFF
int old_topfill = curwin->w_topfill;
#endif
undisplay_dollar();
tpos = curwin->w_cursor;
if (cursor_up(1L, TRUE) == OK)
{
if (startcol)
coladvance(getvcol_nolist(&Insstart));
if (old_topline != curwin->w_topline
#ifdef FEAT_DIFF
|| old_topfill != curwin->w_topfill
#endif
)
redraw_later(VALID);
start_arrow(&tpos);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
}
else
vim_beep(BO_CRSR);
}
static void
ins_pageup(void)
{
pos_T tpos;
undisplay_dollar();
if (mod_mask & MOD_MASK_CTRL)
{
if (first_tabpage->tp_next != NULL)
{
start_arrow(&curwin->w_cursor);
goto_tabpage(-1);
}
return;
}
tpos = curwin->w_cursor;
if (onepage(BACKWARD, 1L) == OK)
{
start_arrow(&tpos);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
}
else
vim_beep(BO_CRSR);
}
static void
ins_down(
int startcol)
{
pos_T tpos;
linenr_T old_topline = curwin->w_topline;
#ifdef FEAT_DIFF
int old_topfill = curwin->w_topfill;
#endif
undisplay_dollar();
tpos = curwin->w_cursor;
if (cursor_down(1L, TRUE) == OK)
{
if (startcol)
coladvance(getvcol_nolist(&Insstart));
if (old_topline != curwin->w_topline
#ifdef FEAT_DIFF
|| old_topfill != curwin->w_topfill
#endif
)
redraw_later(VALID);
start_arrow(&tpos);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
}
else
vim_beep(BO_CRSR);
}
static void
ins_pagedown(void)
{
pos_T tpos;
undisplay_dollar();
if (mod_mask & MOD_MASK_CTRL)
{
if (first_tabpage->tp_next != NULL)
{
start_arrow(&curwin->w_cursor);
goto_tabpage(0);
}
return;
}
tpos = curwin->w_cursor;
if (onepage(FORWARD, 1L) == OK)
{
start_arrow(&tpos);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
}
else
vim_beep(BO_CRSR);
}
#ifdef FEAT_DND
static void
ins_drop(void)
{
do_put('~', BACKWARD, 1L, PUT_CURSEND);
}
#endif
static int
ins_tab(void)
{
int ind;
int i;
int temp;
if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
Insstart_blank_vcol = get_nolist_virtcol();
if (echeck_abbr(TAB + ABBR_OFF))
return FALSE;
ind = inindent(0);
#ifdef FEAT_CINDENT
if (ind)
can_cindent = FALSE;
#endif
if (!curbuf->b_p_et
&& !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
&& get_sts_value() == 0)
return TRUE;
if (stop_arrow() == FAIL)
return TRUE;
did_ai = FALSE;
#ifdef FEAT_SMARTINDENT
did_si = FALSE;
can_si = FALSE;
can_si_back = FALSE;
#endif
AppendToRedobuff((char_u *)"\t");
if (p_sta && ind)
temp = (int)get_sw_value(curbuf);
else if (curbuf->b_p_sts != 0)
temp = (int)get_sts_value();
else
temp = (int)curbuf->b_p_ts;
temp -= get_nolist_virtcol() % temp;
ins_char(' ');
while (--temp > 0)
{
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
ins_char(' ');
else
#endif
{
ins_str((char_u *)" ");
if (State & REPLACE_FLAG)
replace_push(NUL);
}
}
if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
{
char_u *ptr;
#ifdef FEAT_VREPLACE
char_u *saved_line = NULL;
pos_T pos;
#endif
pos_T fpos;
pos_T *cursor;
colnr_T want_vcol, vcol;
int change_col = -1;
int save_list = curwin->w_p_list;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
pos = curwin->w_cursor;
cursor = &pos;
saved_line = vim_strsave(ml_get_curline());
if (saved_line == NULL)
return FALSE;
ptr = saved_line + pos.col;
}
else
#endif
{
ptr = ml_get_cursor();
cursor = &curwin->w_cursor;
}
if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
curwin->w_p_list = FALSE;
fpos = curwin->w_cursor;
while (fpos.col > 0 && VIM_ISWHITE(ptr[-1]))
{
--fpos.col;
--ptr;
}
if ((State & REPLACE_FLAG)
&& fpos.lnum == Insstart.lnum
&& fpos.col < Insstart.col)
{
ptr += Insstart.col - fpos.col;
fpos.col = Insstart.col;
}
getvcol(curwin, &fpos, &vcol, NULL, NULL);
getvcol(curwin, cursor, &want_vcol, NULL, NULL);
while (VIM_ISWHITE(*ptr))
{
i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
if (vcol + i > want_vcol)
break;
if (*ptr != TAB)
{
*ptr = TAB;
if (change_col < 0)
{
change_col = fpos.col;
if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
Insstart.col = fpos.col;
}
}
++fpos.col;
++ptr;
vcol += i;
}
if (change_col >= 0)
{
int repl_off = 0;
char_u *line = ptr;
while (vcol < want_vcol && *ptr == ' ')
{
vcol += lbr_chartabsize(line, ptr, vcol);
++ptr;
++repl_off;
}
if (vcol > want_vcol)
{
--ptr;
--repl_off;
}
fpos.col += repl_off;
i = cursor->col - fpos.col;
if (i > 0)
{
STRMOVE(ptr, ptr + i);
if ((State & REPLACE_FLAG)
#ifdef FEAT_VREPLACE
&& !(State & VREPLACE_FLAG)
#endif
)
for (temp = i; --temp >= 0; )
replace_join(repl_off);
}
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active())
{
netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
netbeans_inserted(curbuf, fpos.lnum, cursor->col,
(char_u *)"\t", 1);
}
#endif
cursor->col -= i;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
backspace_until_column(change_col);
ins_bytes_len(saved_line + change_col,
cursor->col - change_col);
}
#endif
}
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
vim_free(saved_line);
#endif
curwin->w_p_list = save_list;
}
return FALSE;
}
static int
ins_eol(int c)
{
int i;
if (echeck_abbr(c + ABBR_OFF))
return FALSE;
if (stop_arrow() == FAIL)
return TRUE;
undisplay_dollar();
if ((State & REPLACE_FLAG)
#ifdef FEAT_VREPLACE
&& !(State & VREPLACE_FLAG)
#endif
)
replace_push(NUL);
#ifdef FEAT_VIRTUALEDIT
if (virtual_active() && curwin->w_cursor.coladd > 0)
coladvance(getviscol());
#endif
#ifdef FEAT_RIGHTLEFT
# ifdef FEAT_FKMAP
if (p_altkeymap && p_fkmap)
fkmap(NL);
# endif
if (revins_on)
curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
#endif
AppendToRedobuff(NL_STR);
i = open_line(FORWARD,
#ifdef FEAT_COMMENTS
has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
#endif
0, old_indent);
old_indent = 0;
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
#ifdef FEAT_FOLDING
foldOpenCursor();
#endif
return (!i);
}
#ifdef FEAT_DIGRAPHS
static int
ins_digraph(void)
{
int c;
int cc;
int did_putchar = FALSE;
pc_status = PC_STATUS_UNSET;
if (redrawing() && !char_avail())
{
ins_redraw(FALSE);
edit_putchar('?', TRUE);
did_putchar = TRUE;
#ifdef FEAT_CMDL_INFO
add_to_showcmd_c(Ctrl_K);
#endif
}
#ifdef USE_ON_FLY_SCROLL
dont_scroll = TRUE;
#endif
++no_mapping;
++allow_keys;
c = plain_vgetc();
--no_mapping;
--allow_keys;
if (did_putchar)
edit_unputchar();
if (IS_SPECIAL(c) || mod_mask)
{
#ifdef FEAT_CMDL_INFO
clear_showcmd();
#endif
insert_special(c, TRUE, FALSE);
return NUL;
}
if (c != ESC)
{
did_putchar = FALSE;
if (redrawing() && !char_avail())
{
ins_redraw(FALSE);
if (char2cells(c) == 1)
{
ins_redraw(FALSE);
edit_putchar(c, TRUE);
did_putchar = TRUE;
}
#ifdef FEAT_CMDL_INFO
add_to_showcmd_c(c);
#endif
}
++no_mapping;
++allow_keys;
cc = plain_vgetc();
--no_mapping;
--allow_keys;
if (did_putchar)
edit_unputchar();
if (cc != ESC)
{
AppendToRedobuff((char_u *)CTRL_V_STR);
c = getdigraph(c, cc, TRUE);
#ifdef FEAT_CMDL_INFO
clear_showcmd();
#endif
return c;
}
}
#ifdef FEAT_CMDL_INFO
clear_showcmd();
#endif
return NUL;
}
#endif
int
ins_copychar(linenr_T lnum)
{
int c;
int temp;
char_u *ptr, *prev_ptr;
char_u *line;
if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
{
vim_beep(BO_COPY);
return NUL;
}
temp = 0;
line = ptr = ml_get(lnum);
prev_ptr = ptr;
validate_virtcol();
while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
{
prev_ptr = ptr;
temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
}
if ((colnr_T)temp > curwin->w_virtcol)
ptr = prev_ptr;
#ifdef FEAT_MBYTE
c = (*mb_ptr2char)(ptr);
#else
c = *ptr;
#endif
if (c == NUL)
vim_beep(BO_COPY);
return c;
}
static int
ins_ctrl_ey(int tc)
{
int c = tc;
#ifdef FEAT_INS_EXPAND
if (ctrl_x_mode == CTRL_X_SCROLL)
{
if (c == Ctrl_Y)
scrolldown_clamp();
else
scrollup_clamp();
redraw_later(VALID);
}
else
#endif
{
c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
if (c != NUL)
{
long tw_save;
if (c < 256 && !isalnum(c))
AppendToRedobuff((char_u *)CTRL_V_STR);
tw_save = curbuf->b_p_tw;
curbuf->b_p_tw = -1;
insert_special(c, TRUE, FALSE);
curbuf->b_p_tw = tw_save;
#ifdef FEAT_RIGHTLEFT
revins_chars++;
revins_legal++;
#endif
c = Ctrl_V;
auto_format(FALSE, TRUE);
}
}
return c;
}
#ifdef FEAT_SMARTINDENT
static void
ins_try_si(int c)
{
pos_T *pos, old_pos;
char_u *ptr;
int i;
int temp;
if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
{
if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
{
old_pos = curwin->w_cursor;
ptr = ml_get(pos->lnum);
i = pos->col;
if (i > 0)
while (--i > 0 && VIM_ISWHITE(ptr[i]))
;
curwin->w_cursor.lnum = pos->lnum;
curwin->w_cursor.col = i;
if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
curwin->w_cursor = *pos;
i = get_indent();
curwin->w_cursor = old_pos;
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
else
#endif
(void)set_indent(i, SIN_CHANGED);
}
else if (curwin->w_cursor.col > 0)
{
temp = TRUE;
if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
{
old_pos = curwin->w_cursor;
i = get_indent();
while (curwin->w_cursor.lnum > 1)
{
ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
if (*ptr != '#' && *ptr != NUL)
break;
}
if (get_indent() >= i)
temp = FALSE;
curwin->w_cursor = old_pos;
}
if (temp)
shift_line(TRUE, FALSE, 1, TRUE);
}
}
if (curwin->w_cursor.col > 0 && can_si && c == '#')
{
old_indent = get_indent();
(void)set_indent(0, SIN_CHANGED);
}
if (ai_col > curwin->w_cursor.col)
ai_col = curwin->w_cursor.col;
}
#endif
static colnr_T
get_nolist_virtcol(void)
{
if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
return getvcol_nolist(&curwin->w_cursor);
validate_virtcol();
return curwin->w_virtcol;
}
#ifdef FEAT_AUTOCMD
static char_u *
do_insert_char_pre(int c)
{
char_u *res;
char_u buf[MB_MAXBYTES + 1];
if (!has_insertcharpre())
return NULL;
#ifdef FEAT_MBYTE
if (has_mbyte)
buf[(*mb_char2bytes)(c, buf)] = NUL;
else
#endif
{
buf[0] = c;
buf[1] = NUL;
}
++textlock;
set_vim_var_string(VV_CHAR, buf, -1);
res = NULL;
if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
{
if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
res = vim_strsave(get_vim_var_str(VV_CHAR));
}
set_vim_var_string(VV_CHAR, NULL, -1);
--textlock;
return res;
}
#endif