blob: daab0fee7f0d9f727de9eb307d124a1bcf14cced [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000060static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010061#ifdef FEAT_COMPL_FUNC
62static char e_complwin[] = N_("E839: Completion function changed window");
63static char e_compldel[] = N_("E840: Completion function deleted text");
64#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to store one match for insert completion.
68 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000069typedef struct compl_S compl_T;
70struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar572cb562005-08-05 21:35:02 +000072 compl_T *cp_next;
73 compl_T *cp_prev;
74 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000075 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000076 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 char_u *cp_fname; /* file containing the match, allocated when
78 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000079 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
80 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081};
82
Bram Moolenaar572cb562005-08-05 21:35:02 +000083#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define FREE_FNAME (2)
85
86/*
87 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000088 * "compl_first_match" points to the start of the list.
89 * "compl_curr_match" points to the currently selected entry.
90 * "compl_shown_match" is different from compl_curr_match during
91 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000093static compl_T *compl_first_match = NULL;
94static compl_T *compl_curr_match = NULL;
95static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar779b74b2006-04-10 14:55:34 +000097/* After using a cursor key <Enter> selects a match in the popup menu,
98 * otherwise it inserts a line break. */
99static int compl_enter_selects = FALSE;
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101/* When "compl_leader" is not NULL only matches that start with this string
102 * are used. */
103static char_u *compl_leader = NULL;
104
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000105static int compl_get_longest = FALSE; /* put longest common string
106 in compl_leader */
107
Bram Moolenaara6557602006-02-04 22:43:20 +0000108static int compl_used_match; /* Selected one of the matches. When
109 FALSE the match was edited or using
110 the longest common string. */
111
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000112static int compl_was_interrupted = FALSE; /* didn't finish finding
113 completions. */
114
115static int compl_restarting = FALSE; /* don't insert match */
116
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000117/* When the first completion is done "compl_started" is set. When it's
118 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000121/* Set when doing something for completion that may call edit() recursively,
122 * which is not allowed. */
123static int compl_busy = FALSE;
124
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static int compl_matches = 0;
126static char_u *compl_pattern = NULL;
127static int compl_direction = FORWARD;
128static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000129static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000130static pos_T compl_startpos;
131static colnr_T compl_col = 0; /* column where the text starts
132 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static char_u *compl_orig_text = NULL; /* text as it was before
134 * completion started */
135static int compl_cont_mode = 0;
136static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaar82139082011-09-14 16:52:09 +0200138static int compl_opt_refresh_always = FALSE;
139
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000140static void ins_ctrl_x __ARGS((void));
141static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000142static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000143static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000144static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000145static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000146static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000148static void ins_compl_upd_pum __ARGS((void));
149static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000150static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000151static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000152static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000153static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000154static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ins_compl_free __ARGS((void));
156static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000157static int ins_compl_bs __ARGS((void));
Bram Moolenaar82139082011-09-14 16:52:09 +0200158static int ins_compl_need_restart __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000159static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000160static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar82139082011-09-14 16:52:09 +0200161static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000162static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000163static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000164static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000165static int ins_compl_prep __ARGS((int c));
Bram Moolenaar62951b12011-09-21 18:23:05 +0200166static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000168#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
169static void ins_compl_add_list __ARGS((list_T *list));
Bram Moolenaar82139082011-09-14 16:52:09 +0200170static void ins_compl_add_dict __ARGS((dict_T *dict));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000171#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000172static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173static void ins_compl_delete __ARGS((void));
174static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000175static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000176static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000177static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000178static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000179static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000181static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#endif /* FEAT_INS_EXPAND */
183
184#define BACKSPACE_CHAR 1
185#define BACKSPACE_WORD 2
186#define BACKSPACE_WORD_NOT_SPACE 3
187#define BACKSPACE_LINE 4
188
Bram Moolenaar754b5602006-02-09 23:53:20 +0000189static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void ins_ctrl_v __ARGS((void));
191static void undisplay_dollar __ARGS((void));
192static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000193static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static void check_auto_format __ARGS((int));
195static void redo_literal __ARGS((int c));
196static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000197#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000198static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000199static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000200static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
203static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204static int replace_pop __ARGS((void));
205static void replace_join __ARGS((int off));
206static void replace_pop_ins __ARGS((void));
207#ifdef FEAT_MBYTE
208static void mb_replace_pop_ins __ARGS((int cc));
209#endif
210static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000211static void replace_do_bs __ARGS((int limit_col));
212static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifdef FEAT_CINDENT
214static int cindent_on __ARGS((void));
215#endif
216static void ins_reg __ARGS((void));
217static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000218static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000219static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#ifdef FEAT_RIGHTLEFT
221static void ins_ctrl_ __ARGS((void));
222#endif
223#ifdef FEAT_VISUAL
224static int ins_start_select __ARGS((int c));
225#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000226static void ins_insert __ARGS((int replaceState));
227static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static void ins_shift __ARGS((int c, int lastc));
229static void ins_del __ARGS((void));
230static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
231#ifdef FEAT_MOUSE
232static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200233static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000235#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
236static void ins_tabline __ARGS((int c));
237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238static void ins_left __ARGS((void));
239static void ins_home __ARGS((int c));
240static void ins_end __ARGS((int c));
241static void ins_s_left __ARGS((void));
242static void ins_right __ARGS((void));
243static void ins_s_right __ARGS((void));
244static void ins_up __ARGS((int startcol));
245static void ins_pageup __ARGS((void));
246static void ins_down __ARGS((int startcol));
247static void ins_pagedown __ARGS((void));
248#ifdef FEAT_DND
249static void ins_drop __ARGS((void));
250#endif
251static int ins_tab __ARGS((void));
252static int ins_eol __ARGS((int c));
253#ifdef FEAT_DIGRAPHS
254static int ins_digraph __ARGS((void));
255#endif
256static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000257static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_SMARTINDENT
259static void ins_try_si __ARGS((int c));
260#endif
261static colnr_T get_nolist_virtcol __ARGS((void));
262
263static colnr_T Insstart_textlen; /* length of line when insert started */
264static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
265
266static char_u *last_insert = NULL; /* the text of the previous insert,
267 K_SPECIAL and CSI are escaped */
268static int last_insert_skip; /* nr of chars in front of previous insert */
269static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000270static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271
272#ifdef FEAT_CINDENT
273static int can_cindent; /* may do cindenting on this line */
274#endif
275
276static int old_indent = 0; /* for ^^D command in insert mode */
277
278#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000279static int revins_on; /* reverse insert mode on */
280static int revins_chars; /* how much to skip after edit */
281static int revins_legal; /* was the last char 'legal'? */
282static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#endif
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285static int ins_need_undo; /* call u_save() before inserting a
286 char. Set when edit() is called.
287 after that arrow_used is used. */
288
289static int did_add_space = FALSE; /* auto_format() added an extra space
290 under the cursor */
291
292/*
293 * edit(): Start inserting text.
294 *
295 * "cmdchar" can be:
296 * 'i' normal insert command
297 * 'a' normal append command
298 * 'R' replace command
299 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
300 * but still only one <CR> is inserted. The <Esc> is not used for redo.
301 * 'g' "gI" command.
302 * 'V' "gR" command for Virtual Replace mode.
303 * 'v' "gr" command for single character Virtual Replace mode.
304 *
305 * This function is not called recursively. For CTRL-O commands, it returns
306 * and lets the caller handle the Normal-mode command.
307 *
308 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
309 */
310 int
311edit(cmdchar, startln, count)
312 int cmdchar;
313 int startln; /* if set, insert at start of line */
314 long count;
315{
316 int c = 0;
317 char_u *ptr;
318 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000319 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 int i;
322 int did_backspace = TRUE; /* previous char was backspace */
323#ifdef FEAT_CINDENT
324 int line_is_white = FALSE; /* line is empty before insert */
325#endif
326 linenr_T old_topline = 0; /* topline before insertion */
327#ifdef FEAT_DIFF
328 int old_topfill = -1;
329#endif
330 int inserted_space = FALSE; /* just inserted a space */
331 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000332 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000334 /* Remember whether editing was restarted after CTRL-O. */
335 did_restart_edit = restart_edit;
336
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 /* sleep before redrawing, needed for "CTRL-O :" that results in an
338 * error message */
339 check_for_delay(TRUE);
340
341#ifdef HAVE_SANDBOX
342 /* Don't allow inserting in the sandbox. */
343 if (sandbox != 0)
344 {
345 EMSG(_(e_sandbox));
346 return FALSE;
347 }
348#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000349 /* Don't allow changes in the buffer while editing the cmdline. The
350 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000351 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000352 {
353 EMSG(_(e_secure));
354 return FALSE;
355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356
357#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000358 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000359 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000360 {
361 EMSG(_(e_secure));
362 return FALSE;
363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 ins_compl_clear(); /* clear stuff for CTRL-X mode */
365#endif
366
Bram Moolenaar843ee412004-06-30 16:16:41 +0000367#ifdef FEAT_AUTOCMD
368 /*
369 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
370 */
371 if (cmdchar != 'r' && cmdchar != 'v')
372 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000373# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000374 if (cmdchar == 'R')
375 ptr = (char_u *)"r";
376 else if (cmdchar == 'V')
377 ptr = (char_u *)"v";
378 else
379 ptr = (char_u *)"i";
380 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000381# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000382 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
383 }
384#endif
385
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200386#ifdef FEAT_CONCEAL
387 /* Check if the cursor line needs redrawing before changing State. If
388 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200389 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200390#endif
391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef FEAT_MOUSE
393 /*
394 * When doing a paste with the middle mouse button, Insstart is set to
395 * where the paste started.
396 */
397 if (where_paste_started.lnum != 0)
398 Insstart = where_paste_started;
399 else
400#endif
401 {
402 Insstart = curwin->w_cursor;
403 if (startln)
404 Insstart.col = 0;
405 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000406 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 Insstart_blank_vcol = MAXCOL;
408 if (!did_ai)
409 ai_col = 0;
410
411 if (cmdchar != NUL && restart_edit == 0)
412 {
413 ResetRedobuff();
414 AppendNumberToRedobuff(count);
415#ifdef FEAT_VREPLACE
416 if (cmdchar == 'V' || cmdchar == 'v')
417 {
418 /* "gR" or "gr" command */
419 AppendCharToRedobuff('g');
420 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
421 }
422 else
423#endif
424 {
425 AppendCharToRedobuff(cmdchar);
426 if (cmdchar == 'g') /* "gI" command */
427 AppendCharToRedobuff('I');
428 else if (cmdchar == 'r') /* "r<CR>" command */
429 count = 1; /* insert only one <CR> */
430 }
431 }
432
433 if (cmdchar == 'R')
434 {
435#ifdef FEAT_FKMAP
436 if (p_fkmap && p_ri)
437 {
438 beep_flush();
439 EMSG(farsi_text_3); /* encoded in Farsi */
440 State = INSERT;
441 }
442 else
443#endif
444 State = REPLACE;
445 }
446#ifdef FEAT_VREPLACE
447 else if (cmdchar == 'V' || cmdchar == 'v')
448 {
449 State = VREPLACE;
450 replaceState = VREPLACE;
451 orig_line_count = curbuf->b_ml.ml_line_count;
452 vr_lines_changed = 1;
453 }
454#endif
455 else
456 State = INSERT;
457
458 stop_insert_mode = FALSE;
459
460 /*
461 * Need to recompute the cursor position, it might move when the cursor is
462 * on a TAB or special character.
463 */
464 curs_columns(TRUE);
465
466 /*
467 * Enable langmap or IME, indicated by 'iminsert'.
468 * Note that IME may enabled/disabled without us noticing here, thus the
469 * 'iminsert' value may not reflect what is actually used. It is updated
470 * when hitting <Esc>.
471 */
472 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
473 State |= LANGMAP;
474#ifdef USE_IM_CONTROL
475 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
476#endif
477
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478#ifdef FEAT_MOUSE
479 setmouse();
480#endif
481#ifdef FEAT_CMDL_INFO
482 clear_showcmd();
483#endif
484#ifdef FEAT_RIGHTLEFT
485 /* there is no reverse replace mode */
486 revins_on = (State == INSERT && p_ri);
487 if (revins_on)
488 undisplay_dollar();
489 revins_chars = 0;
490 revins_legal = 0;
491 revins_scol = -1;
492#endif
493
494 /*
495 * Handle restarting Insert mode.
496 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
497 * restart_edit non-zero, and something in the stuff buffer.
498 */
499 if (restart_edit != 0 && stuff_empty())
500 {
501#ifdef FEAT_MOUSE
502 /*
503 * After a paste we consider text typed to be part of the insert for
504 * the pasted text. You can backspace over the pasted text too.
505 */
506 if (where_paste_started.lnum)
507 arrow_used = FALSE;
508 else
509#endif
510 arrow_used = TRUE;
511 restart_edit = 0;
512
513 /*
514 * If the cursor was after the end-of-line before the CTRL-O and it is
515 * now at the end-of-line, put it after the end-of-line (this is not
516 * correct in very rare cases).
517 * Also do this if curswant is greater than the current virtual
518 * column. Eg after "^O$" or "^O80|".
519 */
520 validate_virtcol();
521 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000522 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 || curwin->w_curswant > curwin->w_virtcol)
524 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
525 {
526 if (ptr[1] == NUL)
527 ++curwin->w_cursor.col;
528#ifdef FEAT_MBYTE
529 else if (has_mbyte)
530 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000531 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (ptr[i] == NUL)
533 curwin->w_cursor.col += i;
534 }
535#endif
536 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000537 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
539 else
540 arrow_used = FALSE;
541
542 /* we are in insert mode now, don't need to start it anymore */
543 need_start_insertmode = FALSE;
544
545 /* Need to save the line for undo before inserting the first char. */
546 ins_need_undo = TRUE;
547
548#ifdef FEAT_MOUSE
549 where_paste_started.lnum = 0;
550#endif
551#ifdef FEAT_CINDENT
552 can_cindent = TRUE;
553#endif
554#ifdef FEAT_FOLDING
555 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
556 * restarting. */
557 if (!p_im && did_restart_edit == 0)
558 foldOpenCursor();
559#endif
560
561 /*
562 * If 'showmode' is set, show the current (insert/replace/..) mode.
563 * A warning message for changing a readonly file is given here, before
564 * actually changing anything. It's put after the mode, if any.
565 */
566 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000567 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 i = showmode();
569
570 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000571 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
573#ifdef CURSOR_SHAPE
574 ui_cursor_shape(); /* may show different cursor shape */
575#endif
576#ifdef FEAT_DIGRAPHS
577 do_digraph(-1); /* clear digraphs */
578#endif
579
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000580 /*
581 * Get the current length of the redo buffer, those characters have to be
582 * skipped if we want to get to the inserted characters.
583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 ptr = get_inserted();
585 if (ptr == NULL)
586 new_insert_skip = 0;
587 else
588 {
589 new_insert_skip = (int)STRLEN(ptr);
590 vim_free(ptr);
591 }
592
593 old_indent = 0;
594
595 /*
596 * Main loop in Insert mode: repeat until Insert mode is left.
597 */
598 for (;;)
599 {
600#ifdef FEAT_RIGHTLEFT
601 if (!revins_legal)
602 revins_scol = -1; /* reset on illegal motions */
603 else
604 revins_legal = 0;
605#endif
606 if (arrow_used) /* don't repeat insert when arrow key used */
607 count = 0;
608
609 if (stop_insert_mode)
610 {
611 /* ":stopinsert" used or 'insertmode' reset */
612 count = 0;
613 goto doESCkey;
614 }
615
616 /* set curwin->w_curswant for next K_DOWN or K_UP */
617 if (!arrow_used)
618 curwin->w_set_curswant = TRUE;
619
620 /* If there is no typeahead may check for timestamps (e.g., for when a
621 * menu invoked a shell command). */
622 if (stuff_empty())
623 {
624 did_check_timestamps = FALSE;
625 if (need_check_timestamps)
626 check_timestamps(FALSE);
627 }
628
629 /*
630 * When emsg() was called msg_scroll will have been set.
631 */
632 msg_scroll = FALSE;
633
634#ifdef FEAT_GUI
635 /* When 'mousefocus' is set a mouse movement may have taken us to
636 * another window. "need_mouse_correct" may then be set because of an
637 * autocommand. */
638 if (need_mouse_correct)
639 gui_mouse_correct();
640#endif
641
642#ifdef FEAT_FOLDING
643 /* Open fold at the cursor line, according to 'foldopen'. */
644 if (fdo_flags & FDO_INSERT)
645 foldOpenCursor();
646 /* Close folds where the cursor isn't, according to 'foldclose' */
647 if (!char_avail())
648 foldCheckClose();
649#endif
650
651 /*
652 * If we inserted a character at the last position of the last line in
653 * the window, scroll the window one line up. This avoids an extra
654 * redraw.
655 * This is detected when the cursor column is smaller after inserting
656 * something.
657 * Don't do this when the topline changed already, it has
658 * already been adjusted (by insertchar() calling open_line())).
659 */
660 if (curbuf->b_mod_set
661 && curwin->w_p_wrap
662 && !did_backspace
663 && curwin->w_topline == old_topline
664#ifdef FEAT_DIFF
665 && curwin->w_topfill == old_topfill
666#endif
667 )
668 {
669 mincol = curwin->w_wcol;
670 validate_cursor_col();
671
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000672 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 && curwin->w_wrow == W_WINROW(curwin)
674 + curwin->w_height - 1 - p_so
675 && (curwin->w_cursor.lnum != curwin->w_topline
676#ifdef FEAT_DIFF
677 || curwin->w_topfill > 0
678#endif
679 ))
680 {
681#ifdef FEAT_DIFF
682 if (curwin->w_topfill > 0)
683 --curwin->w_topfill;
684 else
685#endif
686#ifdef FEAT_FOLDING
687 if (hasFolding(curwin->w_topline, NULL, &old_topline))
688 set_topline(curwin, old_topline + 1);
689 else
690#endif
691 set_topline(curwin, curwin->w_topline + 1);
692 }
693 }
694
695 /* May need to adjust w_topline to show the cursor. */
696 update_topline();
697
698 did_backspace = FALSE;
699
700 validate_cursor(); /* may set must_redraw */
701
702 /*
703 * Redraw the display when no characters are waiting.
704 * Also shows mode, ruler and positions cursor.
705 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000706 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
708#ifdef FEAT_SCROLLBIND
709 if (curwin->w_p_scb)
710 do_check_scrollbind(TRUE);
711#endif
712
Bram Moolenaar860cae12010-06-05 23:22:07 +0200713#ifdef FEAT_CURSORBIND
714 if (curwin->w_p_crb)
715 do_check_cursorbind();
716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 update_curswant();
718 old_topline = curwin->w_topline;
719#ifdef FEAT_DIFF
720 old_topfill = curwin->w_topfill;
721#endif
722
723#ifdef USE_ON_FLY_SCROLL
724 dont_scroll = FALSE; /* allow scrolling here */
725#endif
726
727 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000728 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
730 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000731 do
732 {
733 c = safe_vgetc();
734 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000736#ifdef FEAT_AUTOCMD
737 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
738 did_cursorhold = TRUE;
739#endif
740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741#ifdef FEAT_RIGHTLEFT
742 if (p_hkmap && KeyTyped)
743 c = hkmap(c); /* Hebrew mode mapping */
744#endif
745#ifdef FEAT_FKMAP
746 if (p_fkmap && KeyTyped)
747 c = fkmap(c); /* Farsi mode mapping */
748#endif
749
750#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000751 /*
752 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000753 * and the cursor is still in the completed word. Only when there is
754 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000755 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000756 if (compl_started
757 && pum_wanted()
758 && curwin->w_cursor.col >= compl_col
759 && (compl_shown_match == NULL
760 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000761 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000762 /* BS: Delete one character from "compl_leader". */
763 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000764 && curwin->w_cursor.col > compl_col
765 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000766 continue;
767
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000768 /* When no match was selected or it was edited. */
769 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000770 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000771 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000772 * "compl_leader". Except when at the original match and
773 * there is nothing to add, CTRL-L works like CTRL-P then. */
774 if (c == Ctrl_L
775 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000776 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000777 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000778 {
779 ins_compl_addfrommatch();
780 continue;
781 }
782
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000783 /* A non-white character that fits in with the current
784 * completion: Add to "compl_leader". */
785 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000786 {
787 ins_compl_addleader(c);
788 continue;
789 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000790
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000791 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000792 * compl_enter_selects is set the Enter key does the same. */
793 if (c == Ctrl_Y || (compl_enter_selects
794 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000795 {
796 ins_compl_delete();
797 ins_compl_insert();
798 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000799 }
800 }
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
803 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000804 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000805 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000806 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#endif
808
Bram Moolenaar488c6512005-08-11 20:09:58 +0000809 /* CTRL-\ CTRL-N goes to Normal mode,
810 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
811 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if (c == Ctrl_BSL)
813 {
814 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000815 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 ++no_mapping;
817 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000818 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 --no_mapping;
820 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000821 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000823 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 vungetc(c);
825 c = Ctrl_BSL;
826 }
827 else if (c == Ctrl_G && p_im)
828 continue;
829 else
830 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000831 if (c == Ctrl_O)
832 {
833 ins_ctrl_o();
834 ins_at_eol = FALSE; /* cursor keeps its column */
835 nomove = TRUE;
836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 count = 0;
838 goto doESCkey;
839 }
840 }
841
842#ifdef FEAT_DIGRAPHS
843 c = do_digraph(c);
844#endif
845
846#ifdef FEAT_INS_EXPAND
847 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
848 goto docomplete;
849#endif
850 if (c == Ctrl_V || c == Ctrl_Q)
851 {
852 ins_ctrl_v();
853 c = Ctrl_V; /* pretend CTRL-V is last typed character */
854 continue;
855 }
856
857#ifdef FEAT_CINDENT
858 if (cindent_on()
859# ifdef FEAT_INS_EXPAND
860 && ctrl_x_mode == 0
861# endif
862 )
863 {
864 /* A key name preceded by a bang means this key is not to be
865 * inserted. Skip ahead to the re-indenting below.
866 * A key name preceded by a star means that indenting has to be
867 * done before inserting the key. */
868 line_is_white = inindent(0);
869 if (in_cinkeys(c, '!', line_is_white))
870 goto force_cindent;
871 if (can_cindent && in_cinkeys(c, '*', line_is_white)
872 && stop_arrow() == OK)
873 do_c_expr_indent();
874 }
875#endif
876
877#ifdef FEAT_RIGHTLEFT
878 if (curwin->w_p_rl)
879 switch (c)
880 {
881 case K_LEFT: c = K_RIGHT; break;
882 case K_S_LEFT: c = K_S_RIGHT; break;
883 case K_C_LEFT: c = K_C_RIGHT; break;
884 case K_RIGHT: c = K_LEFT; break;
885 case K_S_RIGHT: c = K_S_LEFT; break;
886 case K_C_RIGHT: c = K_C_LEFT; break;
887 }
888#endif
889
890#ifdef FEAT_VISUAL
891 /*
892 * If 'keymodel' contains "startsel", may start selection. If it
893 * does, a CTRL-O and c will be stuffed, we need to get these
894 * characters.
895 */
896 if (ins_start_select(c))
897 continue;
898#endif
899
900 /*
901 * The big switch to handle a character in insert mode.
902 */
903 switch (c)
904 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000905 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (echeck_abbr(ESC + ABBR_OFF))
907 break;
908 /*FALLTHROUGH*/
909
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000910 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#ifdef FEAT_CMDWIN
912 if (c == Ctrl_C && cmdwin_type != 0)
913 {
914 /* Close the cmdline window. */
915 cmdwin_result = K_IGNORE;
916 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000917 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 goto doESCkey;
919 }
920#endif
921
922#ifdef UNIX
923do_intr:
924#endif
925 /* when 'insertmode' set, and not halfway a mapping, don't leave
926 * Insert mode */
927 if (goto_im())
928 {
929 if (got_int)
930 {
931 (void)vgetc(); /* flush all buffers */
932 got_int = FALSE;
933 }
934 else
935 vim_beep();
936 break;
937 }
938doESCkey:
939 /*
940 * This is the ONLY return from edit()!
941 */
942 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
943 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000944 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 o_lnum = curwin->w_cursor.lnum;
946
Bram Moolenaar488c6512005-08-11 20:09:58 +0000947 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000948 {
949#ifdef FEAT_AUTOCMD
950 if (cmdchar != 'r' && cmdchar != 'v')
951 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
952 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000953 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 continue;
958
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000959 case Ctrl_Z: /* suspend when 'insertmode' set */
960 if (!p_im)
961 goto normalchar; /* insert CTRL-Z as normal char */
962 stuffReadbuff((char_u *)":st\r");
963 c = Ctrl_O;
964 /*FALLTHROUGH*/
965
966 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000967#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000968 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000969 goto docomplete;
970#endif
971 if (echeck_abbr(Ctrl_O + ABBR_OFF))
972 break;
973 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000974
975#ifdef FEAT_VIRTUALEDIT
976 /* don't move the cursor left when 'virtualedit' has "onemore". */
977 if (ve_flags & VE_ONEMORE)
978 {
979 ins_at_eol = FALSE;
980 nomove = TRUE;
981 }
982#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 count = 0;
984 goto doESCkey;
985
Bram Moolenaar572cb562005-08-05 21:35:02 +0000986 case K_INS: /* toggle insert/replace mode */
987 case K_KINS:
988 ins_insert(replaceState);
989 break;
990
991 case K_SELECT: /* end of Select mode mapping - ignore */
992 break;
993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994#ifdef FEAT_SNIFF
995 case K_SNIFF: /* Sniff command received */
996 stuffcharReadbuff(K_SNIFF);
997 goto doESCkey;
998#endif
999
1000 case K_HELP: /* Help key works like <ESC> <Help> */
1001 case K_F1:
1002 case K_XF1:
1003 stuffcharReadbuff(K_HELP);
1004 if (p_im)
1005 need_start_insertmode = TRUE;
1006 goto doESCkey;
1007
1008#ifdef FEAT_NETBEANS_INTG
1009 case K_F21: /* NetBeans command */
1010 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001011 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001012 --no_mapping;
1013 netbeans_keycommand(i);
1014 break;
1015#endif
1016
1017 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 case NUL:
1019 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001020 /* For ^@ the trailing ESC will end the insert, unless there is an
1021 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1023 && c != Ctrl_A && !p_im)
1024 goto doESCkey; /* quit insert mode */
1025 inserted_space = FALSE;
1026 break;
1027
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 ins_reg();
1030 auto_format(FALSE, TRUE);
1031 inserted_space = FALSE;
1032 break;
1033
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 ins_ctrl_g();
1036 break;
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case Ctrl_HAT: /* switch input mode and/or langmap */
1039 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 break;
1041
1042#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (!p_ari)
1045 goto normalchar;
1046 ins_ctrl_();
1047 break;
1048#endif
1049
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1052 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1053 goto docomplete;
1054#endif
1055 /* FALLTHROUGH */
1056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058# ifdef FEAT_INS_EXPAND
1059 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1060 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 if (has_compl_option(FALSE))
1062 goto docomplete;
1063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065# endif
1066 ins_shift(c, lastc);
1067 auto_format(FALSE, TRUE);
1068 inserted_space = FALSE;
1069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 case K_KDEL:
1073 ins_del();
1074 auto_format(FALSE, TRUE);
1075 break;
1076
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001077 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 case Ctrl_H:
1079 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1080 auto_format(FALSE, TRUE);
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1085 auto_format(FALSE, TRUE);
1086 break;
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001089# ifdef FEAT_COMPL_FUNC
1090 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001092 goto docomplete;
1093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1095 auto_format(FALSE, TRUE);
1096 inserted_space = FALSE;
1097 break;
1098
1099#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 case K_LEFTMOUSE_NM:
1102 case K_LEFTDRAG:
1103 case K_LEFTRELEASE:
1104 case K_LEFTRELEASE_NM:
1105 case K_MIDDLEMOUSE:
1106 case K_MIDDLEDRAG:
1107 case K_MIDDLERELEASE:
1108 case K_RIGHTMOUSE:
1109 case K_RIGHTDRAG:
1110 case K_RIGHTRELEASE:
1111 case K_X1MOUSE:
1112 case K_X1DRAG:
1113 case K_X1RELEASE:
1114 case K_X2MOUSE:
1115 case K_X2DRAG:
1116 case K_X2RELEASE:
1117 ins_mouse(c);
1118 break;
1119
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001121 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 break;
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001125 ins_mousescroll(MSCR_UP);
1126 break;
1127
1128 case K_MOUSELEFT: /* Scroll wheel left */
1129 ins_mousescroll(MSCR_LEFT);
1130 break;
1131
1132 case K_MOUSERIGHT: /* Scroll wheel right */
1133 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 break;
1135#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001136#ifdef FEAT_GUI_TABLINE
1137 case K_TABLINE:
1138 case K_TABMENU:
1139 ins_tabline(c);
1140 break;
1141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 break;
1145
Bram Moolenaar754b5602006-02-09 23:53:20 +00001146#ifdef FEAT_AUTOCMD
1147 case K_CURSORHOLD: /* Didn't type something for a while. */
1148 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1149 did_cursorhold = TRUE;
1150 break;
1151#endif
1152
Bram Moolenaar4770d092006-01-12 23:22:24 +00001153#ifdef FEAT_GUI_W32
1154 /* On Win32 ignore <M-F4>, we get it when closing the window was
1155 * cancelled. */
1156 case K_F4:
1157 if (mod_mask != MOD_MASK_ALT)
1158 goto normalchar;
1159 break;
1160#endif
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#ifdef FEAT_GUI
1163 case K_VER_SCROLLBAR:
1164 ins_scroll();
1165 break;
1166
1167 case K_HOR_SCROLLBAR:
1168 ins_horscroll();
1169 break;
1170#endif
1171
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 case K_S_HOME:
1175 case K_C_HOME:
1176 ins_home(c);
1177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 case K_S_END:
1182 case K_C_END:
1183 ins_end(c);
1184 break;
1185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001186 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001187 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1188 ins_s_left();
1189 else
1190 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 break;
1192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 case K_C_LEFT:
1195 ins_s_left();
1196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001199 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1200 ins_s_right();
1201 else
1202 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 case K_C_RIGHT:
1207 ins_s_right();
1208 break;
1209
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001211#ifdef FEAT_INS_EXPAND
1212 if (pum_visible())
1213 goto docomplete;
1214#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001215 if (mod_mask & MOD_MASK_SHIFT)
1216 ins_pageup();
1217 else
1218 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 break;
1220
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001221 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 case K_PAGEUP:
1223 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001224#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001225 if (pum_visible())
1226 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 ins_pageup();
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001232#ifdef FEAT_INS_EXPAND
1233 if (pum_visible())
1234 goto docomplete;
1235#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001236 if (mod_mask & MOD_MASK_SHIFT)
1237 ins_pagedown();
1238 else
1239 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 break;
1241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001242 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 case K_PAGEDOWN:
1244 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001245#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001246 if (pum_visible())
1247 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 ins_pagedown();
1250 break;
1251
1252#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 ins_drop();
1255 break;
1256#endif
1257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 c = TAB;
1260 /* FALLTHROUGH */
1261
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1264 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1265 goto docomplete;
1266#endif
1267 inserted_space = FALSE;
1268 if (ins_tab())
1269 goto normalchar; /* insert TAB as a normal char */
1270 auto_format(FALSE, TRUE);
1271 break;
1272
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001273 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 c = CAR;
1275 /* FALLTHROUGH */
1276 case CAR:
1277 case NL:
1278#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1279 /* In a quickfix window a <CR> jumps to the error under the
1280 * cursor. */
1281 if (bt_quickfix(curbuf) && c == CAR)
1282 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001283 if (curwin->w_llist_ref == NULL) /* quickfix window */
1284 do_cmdline_cmd((char_u *)".cc");
1285 else /* location list window */
1286 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 break;
1288 }
1289#endif
1290#ifdef FEAT_CMDWIN
1291 if (cmdwin_type != 0)
1292 {
1293 /* Execute the command in the cmdline window. */
1294 cmdwin_result = CAR;
1295 goto doESCkey;
1296 }
1297#endif
1298 if (ins_eol(c) && !p_im)
1299 goto doESCkey; /* out of memory */
1300 auto_format(FALSE, FALSE);
1301 inserted_space = FALSE;
1302 break;
1303
Bram Moolenaar860cae12010-06-05 23:22:07 +02001304#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001305 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306# ifdef FEAT_INS_EXPAND
1307 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1308 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001309 if (has_compl_option(TRUE))
1310 goto docomplete;
1311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313# endif
1314# ifdef FEAT_DIGRAPHS
1315 c = ins_digraph();
1316 if (c == NUL)
1317 break;
1318# endif
1319 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001323 case Ctrl_X: /* Enter CTRL-X mode */
1324 ins_ctrl_x();
1325 break;
1326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001327 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (ctrl_x_mode != CTRL_X_TAGS)
1329 goto normalchar;
1330 goto docomplete;
1331
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001332 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (ctrl_x_mode != CTRL_X_FILES)
1334 goto normalchar;
1335 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001336
1337 case 's': /* Spelling completion after ^X */
1338 case Ctrl_S:
1339 if (ctrl_x_mode != CTRL_X_SPELL)
1340 goto normalchar;
1341 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342#endif
1343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#ifdef FEAT_INS_EXPAND
1346 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1347#endif
1348 {
1349 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1350 if (p_im)
1351 {
1352 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1353 break;
1354 goto doESCkey;
1355 }
1356 goto normalchar;
1357 }
1358#ifdef FEAT_INS_EXPAND
1359 /* FALLTHROUGH */
1360
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001361 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 case Ctrl_N:
1363 /* if 'complete' is empty then plain ^P is no longer special,
1364 * but it is under other ^X modes */
1365 if (*curbuf->b_p_cpt == NUL
1366 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001367 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 goto normalchar;
1369
1370docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001371 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001373 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001374 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 break;
1376#endif /* FEAT_INS_EXPAND */
1377
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001378 case Ctrl_Y: /* copy from previous line or scroll down */
1379 case Ctrl_E: /* copy from next line or scroll up */
1380 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 break;
1382
1383 default:
1384#ifdef UNIX
1385 if (c == intr_char) /* special interrupt char */
1386 goto do_intr;
1387#endif
1388
Bram Moolenaare659c952011-05-19 17:25:41 +02001389normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /*
1391 * Insert a nomal character.
1392 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001393#ifdef FEAT_AUTOCMD
1394 if (!p_paste)
1395 {
1396 /* Trigger the InsertCharPre event. Lock the text to avoid
1397 * weird things from happening. */
1398 set_vim_var_char(c);
1399 ++textlock;
1400 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
1401 FALSE, curbuf))
1402 {
1403 /* Get the new value of v:char. If it is more than one
1404 * character insert it literally. */
1405 char_u *s = get_vim_var_str(VV_CHAR);
1406 if (MB_CHARLEN(s) > 1)
1407 {
1408 if (stop_arrow() != FAIL)
1409 {
1410 ins_str(s);
1411 AppendToRedobuffLit(s, -1);
1412 }
1413 c = NUL;
1414 }
1415 else
1416 c = PTR2CHAR(s);
1417 }
1418
1419 set_vim_var_string(VV_CHAR, NULL, -1);
1420 --textlock;
1421
1422 /* If the new value is an empty string then don't insert a
1423 * char. */
1424 if (c == NUL)
1425 break;
1426 }
1427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#ifdef FEAT_SMARTINDENT
1429 /* Try to perform smart-indenting. */
1430 ins_try_si(c);
1431#endif
1432
1433 if (c == ' ')
1434 {
1435 inserted_space = TRUE;
1436#ifdef FEAT_CINDENT
1437 if (inindent(0))
1438 can_cindent = FALSE;
1439#endif
1440 if (Insstart_blank_vcol == MAXCOL
1441 && curwin->w_cursor.lnum == Insstart.lnum)
1442 Insstart_blank_vcol = get_nolist_virtcol();
1443 }
1444
1445 if (vim_iswordc(c) || !echeck_abbr(
1446#ifdef FEAT_MBYTE
1447 /* Add ABBR_OFF for characters above 0x100, this is
1448 * what check_abbr() expects. */
1449 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1450#endif
1451 c))
1452 {
1453 insert_special(c, FALSE, FALSE);
1454#ifdef FEAT_RIGHTLEFT
1455 revins_legal++;
1456 revins_chars++;
1457#endif
1458 }
1459
1460 auto_format(FALSE, TRUE);
1461
1462#ifdef FEAT_FOLDING
1463 /* When inserting a character the cursor line must never be in a
1464 * closed fold. */
1465 foldOpenCursor();
1466#endif
1467 break;
1468 } /* end of switch (c) */
1469
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001470#ifdef FEAT_AUTOCMD
1471 /* If typed something may trigger CursorHoldI again. */
1472 if (c != K_CURSORHOLD)
1473 did_cursorhold = FALSE;
1474#endif
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 /* If the cursor was moved we didn't just insert a space */
1477 if (arrow_used)
1478 inserted_space = FALSE;
1479
1480#ifdef FEAT_CINDENT
1481 if (can_cindent && cindent_on()
1482# ifdef FEAT_INS_EXPAND
1483 && ctrl_x_mode == 0
1484# endif
1485 )
1486 {
1487force_cindent:
1488 /*
1489 * Indent now if a key was typed that is in 'cinkeys'.
1490 */
1491 if (in_cinkeys(c, ' ', line_is_white))
1492 {
1493 if (stop_arrow() == OK)
1494 /* re-indent the current line */
1495 do_c_expr_indent();
1496 }
1497 }
1498#endif /* FEAT_CINDENT */
1499
1500 } /* for (;;) */
1501 /* NOTREACHED */
1502}
1503
1504/*
1505 * Redraw for Insert mode.
1506 * This is postponed until getting the next character to make '$' in the 'cpo'
1507 * option work correctly.
1508 * Only redraw when there are no characters available. This speeds up
1509 * inserting sequences of characters (e.g., for CTRL-R).
1510 */
1511 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001512ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001513 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001515#ifdef FEAT_CONCEAL
1516 linenr_T conceal_old_cursor_line = 0;
1517 linenr_T conceal_new_cursor_line = 0;
1518 int conceal_update_lines = FALSE;
1519#endif
1520
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (!char_avail())
1522 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001523#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001524 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1525 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001526 if (ready && (
1527# ifdef FEAT_AUTOCMD
1528 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001529# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001530# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1531 ||
1532# endif
1533# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001534 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001535# endif
1536 )
1537 && !equalpos(last_cursormoved, curwin->w_cursor)
1538# ifdef FEAT_INS_EXPAND
1539 && !pum_visible()
1540# endif
1541 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001542 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001543# ifdef FEAT_SYN_HL
1544 /* Need to update the screen first, to make sure syntax
1545 * highlighting is correct after making a change (e.g., inserting
1546 * a "(". The autocommand may also require a redraw, so it's done
1547 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001548 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001549 update_screen(0);
1550# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001551# ifdef FEAT_AUTOCMD
1552 if (has_cursormovedI())
1553 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1554# endif
1555# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001556 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001557 {
1558 conceal_old_cursor_line = last_cursormoved.lnum;
1559 conceal_new_cursor_line = curwin->w_cursor.lnum;
1560 conceal_update_lines = TRUE;
1561 }
1562# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001563 last_cursormoved = curwin->w_cursor;
1564 }
1565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (must_redraw)
1567 update_screen(0);
1568 else if (clear_cmdline || redraw_cmdline)
1569 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001570# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001571 if ((conceal_update_lines
1572 && (conceal_old_cursor_line != conceal_new_cursor_line
1573 || conceal_cursor_line(curwin)))
1574 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001575 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001576 if (conceal_old_cursor_line != conceal_new_cursor_line)
1577 update_single_line(curwin, conceal_old_cursor_line);
1578 update_single_line(curwin, conceal_new_cursor_line == 0
1579 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001580 curwin->w_valid &= ~VALID_CROW;
1581 }
1582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 showruler(FALSE);
1584 setcursor();
1585 emsg_on_display = FALSE; /* may remove error message now */
1586 }
1587}
1588
1589/*
1590 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1591 */
1592 static void
1593ins_ctrl_v()
1594{
1595 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001596 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001599 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001604 did_putchar = TRUE;
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1607
1608#ifdef FEAT_CMDL_INFO
1609 add_to_showcmd_c(Ctrl_V);
1610#endif
1611
1612 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001613 if (did_putchar)
1614 /* when the line fits in 'columns' the '^' is at the start of the next
1615 * line and will not removed by the redraw */
1616 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617#ifdef FEAT_CMDL_INFO
1618 clear_showcmd();
1619#endif
1620 insert_special(c, FALSE, TRUE);
1621#ifdef FEAT_RIGHTLEFT
1622 revins_chars++;
1623 revins_legal++;
1624#endif
1625}
1626
1627/*
1628 * Put a character directly onto the screen. It's not stored in a buffer.
1629 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1630 */
1631static int pc_status;
1632#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1633#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1634#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1635#define PC_STATUS_SET 3 /* pc_bytes was filled */
1636#ifdef FEAT_MBYTE
1637static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1638#else
1639static char_u pc_bytes[2]; /* saved bytes */
1640#endif
1641static int pc_attr;
1642static int pc_row;
1643static int pc_col;
1644
1645 void
1646edit_putchar(c, highlight)
1647 int c;
1648 int highlight;
1649{
1650 int attr;
1651
1652 if (ScreenLines != NULL)
1653 {
1654 update_topline(); /* just in case w_topline isn't valid */
1655 validate_cursor();
1656 if (highlight)
1657 attr = hl_attr(HLF_8);
1658 else
1659 attr = 0;
1660 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1661 pc_col = W_WINCOL(curwin);
1662#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1663 pc_status = PC_STATUS_UNSET;
1664#endif
1665#ifdef FEAT_RIGHTLEFT
1666 if (curwin->w_p_rl)
1667 {
1668 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1669# ifdef FEAT_MBYTE
1670 if (has_mbyte)
1671 {
1672 int fix_col = mb_fix_col(pc_col, pc_row);
1673
1674 if (fix_col != pc_col)
1675 {
1676 screen_putchar(' ', pc_row, fix_col, attr);
1677 --curwin->w_wcol;
1678 pc_status = PC_STATUS_RIGHT;
1679 }
1680 }
1681# endif
1682 }
1683 else
1684#endif
1685 {
1686 pc_col += curwin->w_wcol;
1687#ifdef FEAT_MBYTE
1688 if (mb_lefthalve(pc_row, pc_col))
1689 pc_status = PC_STATUS_LEFT;
1690#endif
1691 }
1692
1693 /* save the character to be able to put it back */
1694#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1695 if (pc_status == PC_STATUS_UNSET)
1696#endif
1697 {
1698 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1699 pc_status = PC_STATUS_SET;
1700 }
1701 screen_putchar(c, pc_row, pc_col, attr);
1702 }
1703}
1704
1705/*
1706 * Undo the previous edit_putchar().
1707 */
1708 void
1709edit_unputchar()
1710{
1711 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1712 {
1713#if defined(FEAT_MBYTE)
1714 if (pc_status == PC_STATUS_RIGHT)
1715 ++curwin->w_wcol;
1716 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1717 redrawWinline(curwin->w_cursor.lnum, FALSE);
1718 else
1719#endif
1720 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1721 }
1722}
1723
1724/*
1725 * Called when p_dollar is set: display a '$' at the end of the changed text
1726 * Only works when cursor is in the line that changes.
1727 */
1728 void
1729display_dollar(col)
1730 colnr_T col;
1731{
1732 colnr_T save_col;
1733
1734 if (!redrawing())
1735 return;
1736
1737 cursor_off();
1738 save_col = curwin->w_cursor.col;
1739 curwin->w_cursor.col = col;
1740#ifdef FEAT_MBYTE
1741 if (has_mbyte)
1742 {
1743 char_u *p;
1744
1745 /* If on the last byte of a multi-byte move to the first byte. */
1746 p = ml_get_curline();
1747 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1748 }
1749#endif
1750 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1751 if (curwin->w_wcol < W_WIDTH(curwin))
1752 {
1753 edit_putchar('$', FALSE);
1754 dollar_vcol = curwin->w_virtcol;
1755 }
1756 curwin->w_cursor.col = save_col;
1757}
1758
1759/*
1760 * Call this function before moving the cursor from the normal insert position
1761 * in insert mode.
1762 */
1763 static void
1764undisplay_dollar()
1765{
1766 if (dollar_vcol)
1767 {
1768 dollar_vcol = 0;
1769 redrawWinline(curwin->w_cursor.lnum, FALSE);
1770 }
1771}
1772
1773/*
1774 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1775 * Keep the cursor on the same character.
1776 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1777 * type == INDENT_DEC decrease indent (for CTRL-D)
1778 * type == INDENT_SET set indent to "amount"
1779 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1780 */
1781 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001782change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int type;
1784 int amount;
1785 int round;
1786 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001787 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 int vcol;
1790 int last_vcol;
1791 int insstart_less; /* reduction for Insstart.col */
1792 int new_cursor_col;
1793 int i;
1794 char_u *ptr;
1795 int save_p_list;
1796 int start_col;
1797 colnr_T vc;
1798#ifdef FEAT_VREPLACE
1799 colnr_T orig_col = 0; /* init for GCC */
1800 char_u *new_line, *orig_line = NULL; /* init for GCC */
1801
1802 /* VREPLACE mode needs to know what the line was like before changing */
1803 if (State & VREPLACE_FLAG)
1804 {
1805 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1806 orig_col = curwin->w_cursor.col;
1807 }
1808#endif
1809
1810 /* for the following tricks we don't want list mode */
1811 save_p_list = curwin->w_p_list;
1812 curwin->w_p_list = FALSE;
1813 vc = getvcol_nolist(&curwin->w_cursor);
1814 vcol = vc;
1815
1816 /*
1817 * For Replace mode we need to fix the replace stack later, which is only
1818 * possible when the cursor is in the indent. Remember the number of
1819 * characters before the cursor if it's possible.
1820 */
1821 start_col = curwin->w_cursor.col;
1822
1823 /* determine offset from first non-blank */
1824 new_cursor_col = curwin->w_cursor.col;
1825 beginline(BL_WHITE);
1826 new_cursor_col -= curwin->w_cursor.col;
1827
1828 insstart_less = curwin->w_cursor.col;
1829
1830 /*
1831 * If the cursor is in the indent, compute how many screen columns the
1832 * cursor is to the left of the first non-blank.
1833 */
1834 if (new_cursor_col < 0)
1835 vcol = get_indent() - vcol;
1836
1837 if (new_cursor_col > 0) /* can't fix replace stack */
1838 start_col = -1;
1839
1840 /*
1841 * Set the new indent. The cursor will be put on the first non-blank.
1842 */
1843 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001844 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 else
1846 {
1847#ifdef FEAT_VREPLACE
1848 int save_State = State;
1849
1850 /* Avoid being called recursively. */
1851 if (State & VREPLACE_FLAG)
1852 State = INSERT;
1853#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001854 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#ifdef FEAT_VREPLACE
1856 State = save_State;
1857#endif
1858 }
1859 insstart_less -= curwin->w_cursor.col;
1860
1861 /*
1862 * Try to put cursor on same character.
1863 * If the cursor is at or after the first non-blank in the line,
1864 * compute the cursor column relative to the column of the first
1865 * non-blank character.
1866 * If we are not in insert mode, leave the cursor on the first non-blank.
1867 * If the cursor is before the first non-blank, position it relative
1868 * to the first non-blank, counted in screen columns.
1869 */
1870 if (new_cursor_col >= 0)
1871 {
1872 /*
1873 * When changing the indent while the cursor is touching it, reset
1874 * Insstart_col to 0.
1875 */
1876 if (new_cursor_col == 0)
1877 insstart_less = MAXCOL;
1878 new_cursor_col += curwin->w_cursor.col;
1879 }
1880 else if (!(State & INSERT))
1881 new_cursor_col = curwin->w_cursor.col;
1882 else
1883 {
1884 /*
1885 * Compute the screen column where the cursor should be.
1886 */
1887 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001888 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 /*
1891 * Advance the cursor until we reach the right screen column.
1892 */
1893 vcol = last_vcol = 0;
1894 new_cursor_col = -1;
1895 ptr = ml_get_curline();
1896 while (vcol <= (int)curwin->w_virtcol)
1897 {
1898 last_vcol = vcol;
1899#ifdef FEAT_MBYTE
1900 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001901 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 else
1903#endif
1904 ++new_cursor_col;
1905 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1906 }
1907 vcol = last_vcol;
1908
1909 /*
1910 * May need to insert spaces to be able to position the cursor on
1911 * the right screen column.
1912 */
1913 if (vcol != (int)curwin->w_virtcol)
1914 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001915 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001917 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (ptr != NULL)
1919 {
1920 new_cursor_col += i;
1921 ptr[i] = NUL;
1922 while (--i >= 0)
1923 ptr[i] = ' ';
1924 ins_str(ptr);
1925 vim_free(ptr);
1926 }
1927 }
1928
1929 /*
1930 * When changing the indent while the cursor is in it, reset
1931 * Insstart_col to 0.
1932 */
1933 insstart_less = MAXCOL;
1934 }
1935
1936 curwin->w_p_list = save_p_list;
1937
1938 if (new_cursor_col <= 0)
1939 curwin->w_cursor.col = 0;
1940 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001941 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 curwin->w_set_curswant = TRUE;
1943 changed_cline_bef_curs();
1944
1945 /*
1946 * May have to adjust the start of the insert.
1947 */
1948 if (State & INSERT)
1949 {
1950 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1951 {
1952 if ((int)Insstart.col <= insstart_less)
1953 Insstart.col = 0;
1954 else
1955 Insstart.col -= insstart_less;
1956 }
1957 if ((int)ai_col <= insstart_less)
1958 ai_col = 0;
1959 else
1960 ai_col -= insstart_less;
1961 }
1962
1963 /*
1964 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1965 * If the number of characters before the cursor decreased, need to pop a
1966 * few characters from the replace stack.
1967 * If the number of characters before the cursor increased, need to push a
1968 * few NULs onto the replace stack.
1969 */
1970 if (REPLACE_NORMAL(State) && start_col >= 0)
1971 {
1972 while (start_col > (int)curwin->w_cursor.col)
1973 {
1974 replace_join(0); /* remove a NUL from the replace stack */
1975 --start_col;
1976 }
1977 while (start_col < (int)curwin->w_cursor.col || replaced)
1978 {
1979 replace_push(NUL);
1980 if (replaced)
1981 {
1982 replace_push(replaced);
1983 replaced = NUL;
1984 }
1985 ++start_col;
1986 }
1987 }
1988
1989#ifdef FEAT_VREPLACE
1990 /*
1991 * For VREPLACE mode, we also have to fix the replace stack. In this case
1992 * it is always possible because we backspace over the whole line and then
1993 * put it back again the way we wanted it.
1994 */
1995 if (State & VREPLACE_FLAG)
1996 {
1997 /* If orig_line didn't allocate, just return. At least we did the job,
1998 * even if you can't backspace. */
1999 if (orig_line == NULL)
2000 return;
2001
2002 /* Save new line */
2003 new_line = vim_strsave(ml_get_curline());
2004 if (new_line == NULL)
2005 return;
2006
2007 /* We only put back the new line up to the cursor */
2008 new_line[curwin->w_cursor.col] = NUL;
2009
2010 /* Put back original line */
2011 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2012 curwin->w_cursor.col = orig_col;
2013
2014 /* Backspace from cursor to start of line */
2015 backspace_until_column(0);
2016
2017 /* Insert new stuff into line again */
2018 ins_bytes(new_line);
2019
2020 vim_free(new_line);
2021 }
2022#endif
2023}
2024
2025/*
2026 * Truncate the space at the end of a line. This is to be used only in an
2027 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2028 * modes.
2029 */
2030 void
2031truncate_spaces(line)
2032 char_u *line;
2033{
2034 int i;
2035
2036 /* find start of trailing white space */
2037 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2038 {
2039 if (State & REPLACE_FLAG)
2040 replace_join(0); /* remove a NUL from the replace stack */
2041 }
2042 line[i + 1] = NUL;
2043}
2044
2045#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2046 || defined(FEAT_COMMENTS) || defined(PROTO)
2047/*
2048 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2049 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002050 * Will attempt not to go before "col" even when there is a composing
2051 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 */
2053 void
2054backspace_until_column(col)
2055 int col;
2056{
2057 while ((int)curwin->w_cursor.col > col)
2058 {
2059 curwin->w_cursor.col--;
2060 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002061 replace_do_bs(col);
2062 else if (!del_char_after_col(col))
2063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065}
2066#endif
2067
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002068/*
2069 * Like del_char(), but make sure not to go before column "limit_col".
2070 * Only matters when there are composing characters.
2071 * Return TRUE when something was deleted.
2072 */
2073 static int
2074del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002075 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002076{
2077#ifdef FEAT_MBYTE
2078 if (enc_utf8 && limit_col >= 0)
2079 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002080 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002081
2082 /* Make sure the cursor is at the start of a character, but
2083 * skip forward again when going too far back because of a
2084 * composing character. */
2085 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002086 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002087 {
2088 int l = utf_ptr2len(ml_get_cursor());
2089
2090 if (l == 0) /* end of line */
2091 break;
2092 curwin->w_cursor.col += l;
2093 }
2094 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2095 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002096 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002097 }
2098 else
2099#endif
2100 (void)del_char(FALSE);
2101 return TRUE;
2102}
2103
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2105/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002106 * CTRL-X pressed in Insert mode.
2107 */
2108 static void
2109ins_ctrl_x()
2110{
2111 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2112 * CTRL-V works like CTRL-N */
2113 if (ctrl_x_mode != CTRL_X_CMDLINE)
2114 {
2115 /* if the next ^X<> won't ADD nothing, then reset
2116 * compl_cont_status */
2117 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002118 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002119 else
2120 compl_cont_status = 0;
2121 /* We're not sure which CTRL-X mode it will be yet */
2122 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2123 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2124 edit_submode_pre = NULL;
2125 showmode();
2126 }
2127}
2128
2129/*
2130 * Return TRUE if the 'dict' or 'tsr' option can be used.
2131 */
2132 static int
2133has_compl_option(dict_opt)
2134 int dict_opt;
2135{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002136 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002137# ifdef FEAT_SPELL
2138 && !curwin->w_p_spell
2139# endif
2140 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002141 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2142 {
2143 ctrl_x_mode = 0;
2144 edit_submode = NULL;
2145 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2146 : (char_u *)_("'thesaurus' option is empty"),
2147 hl_attr(HLF_E));
2148 if (emsg_silent == 0)
2149 {
2150 vim_beep();
2151 setcursor();
2152 out_flush();
2153 ui_delay(2000L, FALSE);
2154 }
2155 return FALSE;
2156 }
2157 return TRUE;
2158}
2159
2160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2162 * This depends on the current mode.
2163 */
2164 int
2165vim_is_ctrl_x_key(c)
2166 int c;
2167{
2168 /* Always allow ^R - let it's results then be checked */
2169 if (c == Ctrl_R)
2170 return TRUE;
2171
Bram Moolenaare3226be2005-12-18 22:10:00 +00002172 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002174 return TRUE;
2175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 switch (ctrl_x_mode)
2177 {
2178 case 0: /* Not in any CTRL-X mode */
2179 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2180 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002181 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2183 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2184 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002185 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002186 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 case CTRL_X_SCROLL:
2188 return (c == Ctrl_Y || c == Ctrl_E);
2189 case CTRL_X_WHOLE_LINE:
2190 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2191 case CTRL_X_FILES:
2192 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2193 case CTRL_X_DICTIONARY:
2194 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2195 case CTRL_X_THESAURUS:
2196 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2197 case CTRL_X_TAGS:
2198 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2199#ifdef FEAT_FIND_ID
2200 case CTRL_X_PATH_PATTERNS:
2201 return (c == Ctrl_P || c == Ctrl_N);
2202 case CTRL_X_PATH_DEFINES:
2203 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2204#endif
2205 case CTRL_X_CMDLINE:
2206 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2207 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002208#ifdef FEAT_COMPL_FUNC
2209 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002210 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002211 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002212 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002213#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002214 case CTRL_X_SPELL:
2215 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 EMSG(_(e_internal));
2218 return FALSE;
2219}
2220
2221/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002222 * Return TRUE when character "c" is part of the item currently being
2223 * completed. Used to decide whether to abandon complete mode when the menu
2224 * is visible.
2225 */
2226 static int
2227ins_compl_accept_char(c)
2228 int c;
2229{
2230 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2231 /* When expanding an identifier only accept identifier chars. */
2232 return vim_isIDc(c);
2233
2234 switch (ctrl_x_mode)
2235 {
2236 case CTRL_X_FILES:
2237 /* When expanding file name only accept file name chars. But not
2238 * path separators, so that "proto/<Tab>" expands files in
2239 * "proto", not "proto/" as a whole */
2240 return vim_isfilec(c) && !vim_ispathsep(c);
2241
2242 case CTRL_X_CMDLINE:
2243 case CTRL_X_OMNI:
2244 /* Command line and Omni completion can work with just about any
2245 * printable character, but do stop at white space. */
2246 return vim_isprintc(c) && !vim_iswhite(c);
2247
2248 case CTRL_X_WHOLE_LINE:
2249 /* For while line completion a space can be part of the line. */
2250 return vim_isprintc(c);
2251 }
2252 return vim_iswordc(c);
2253}
2254
2255/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002256 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002258 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 * the rest of the word to be in -- webb
2260 */
2261 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002262ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 char_u *str;
2264 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002265 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 char_u *fname;
2267 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002268 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002270 char_u *p;
2271 int i, c;
2272 int actual_len; /* Take multi-byte characters */
2273 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002274 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002275 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 int has_lower = FALSE;
2277 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002279 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002281 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaara2993e12007-08-12 14:38:46 +00002283 /* Find actual length of completion. */
2284#ifdef FEAT_MBYTE
2285 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002287 p = str;
2288 actual_len = 0;
2289 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002291 mb_ptr_adv(p);
2292 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002295 else
2296#endif
2297 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaara2993e12007-08-12 14:38:46 +00002299 /* Find actual length of original text. */
2300#ifdef FEAT_MBYTE
2301 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002303 p = compl_orig_text;
2304 actual_compl_length = 0;
2305 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002307 mb_ptr_adv(p);
2308 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002311 else
2312#endif
2313 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002315 /* "actual_len" may be smaller than "actual_compl_length" when using
2316 * thesaurus, only use the minimum when comparing. */
2317 min_len = actual_len < actual_compl_length
2318 ? actual_len : actual_compl_length;
2319
Bram Moolenaara2993e12007-08-12 14:38:46 +00002320 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002321 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002322 if (wca != NULL)
2323 {
2324 p = str;
2325 for (i = 0; i < actual_len; ++i)
2326#ifdef FEAT_MBYTE
2327 if (has_mbyte)
2328 wca[i] = mb_ptr2char_adv(&p);
2329 else
2330#endif
2331 wca[i] = *(p++);
2332
2333 /* Rule 1: Were any chars converted to lower? */
2334 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002335 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002336 {
2337#ifdef FEAT_MBYTE
2338 if (has_mbyte)
2339 c = mb_ptr2char_adv(&p);
2340 else
2341#endif
2342 c = *(p++);
2343 if (MB_ISLOWER(c))
2344 {
2345 has_lower = TRUE;
2346 if (MB_ISUPPER(wca[i]))
2347 {
2348 /* Rule 1 is satisfied. */
2349 for (i = actual_compl_length; i < actual_len; ++i)
2350 wca[i] = MB_TOLOWER(wca[i]);
2351 break;
2352 }
2353 }
2354 }
2355
2356 /*
2357 * Rule 2: No lower case, 2nd consecutive letter converted to
2358 * upper case.
2359 */
2360 if (!has_lower)
2361 {
2362 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002363 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002364 {
2365#ifdef FEAT_MBYTE
2366 if (has_mbyte)
2367 c = mb_ptr2char_adv(&p);
2368 else
2369#endif
2370 c = *(p++);
2371 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2372 {
2373 /* Rule 2 is satisfied. */
2374 for (i = actual_compl_length; i < actual_len; ++i)
2375 wca[i] = MB_TOUPPER(wca[i]);
2376 break;
2377 }
2378 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2379 }
2380 }
2381
2382 /* Copy the original case of the part we typed. */
2383 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002384 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002385 {
2386#ifdef FEAT_MBYTE
2387 if (has_mbyte)
2388 c = mb_ptr2char_adv(&p);
2389 else
2390#endif
2391 c = *(p++);
2392 if (MB_ISLOWER(c))
2393 wca[i] = MB_TOLOWER(wca[i]);
2394 else if (MB_ISUPPER(c))
2395 wca[i] = MB_TOUPPER(wca[i]);
2396 }
2397
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002398 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002399 * Generate encoding specific output from wide character array.
2400 * Multi-byte characters can occupy up to five bytes more than
2401 * ASCII characters, and we also need one byte for NUL, so stay
2402 * six bytes away from the edge of IObuff.
2403 */
2404 p = IObuff;
2405 i = 0;
2406 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2407#ifdef FEAT_MBYTE
2408 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002409 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 else
2411#endif
2412 *(p++) = wca[i++];
2413 *p = NUL;
2414
2415 vim_free(wca);
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002418 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2419 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002421 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422}
2423
2424/*
2425 * Add a match to the list of matches.
2426 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002427 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002428 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002430 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002431ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 char_u *str;
2433 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002434 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002436 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002437 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002438 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002439 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002441 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002442 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444 ui_breakcheck();
2445 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 if (len < 0)
2448 len = (int)STRLEN(str);
2449
2450 /*
2451 * If the same match is already present, don't add it.
2452 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002453 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002455 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 do
2457 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002458 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002459 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002460 && match->cp_str[len] == NUL)
2461 return NOTDONE;
2462 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002463 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002466 /* Remove any popup menu before changing the list of matches. */
2467 ins_compl_del_pum();
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 /*
2470 * Allocate a new match structure.
2471 * Copy the values to the new match structure.
2472 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002473 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002475 return FAIL;
2476 match->cp_number = -1;
2477 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002478 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002479 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002482 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002484 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002485
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002487 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2489 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002490 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002491 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002492 && compl_curr_match->cp_fname != NULL
2493 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002494 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002495 else if (fname != NULL)
2496 {
2497 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002498 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002501 match->cp_fname = NULL;
2502 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002503
2504 if (cptext != NULL)
2505 {
2506 int i;
2507
2508 for (i = 0; i < CPT_COUNT; ++i)
2509 if (cptext[i] != NULL && *cptext[i] != NUL)
2510 match->cp_text[i] = vim_strsave(cptext[i]);
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 /*
2514 * Link the new match structure in the list of matches.
2515 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002516 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002517 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 else if (dir == FORWARD)
2519 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002520 match->cp_next = compl_curr_match->cp_next;
2521 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
2523 else /* BACKWARD */
2524 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002525 match->cp_next = compl_curr_match;
2526 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002528 if (match->cp_next)
2529 match->cp_next->cp_prev = match;
2530 if (match->cp_prev)
2531 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002533 compl_first_match = match;
2534 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002536 /*
2537 * Find the longest common string if still doing that.
2538 */
2539 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2540 ins_compl_longest_match(match);
2541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 return OK;
2543}
2544
2545/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002546 * Return TRUE if "str[len]" matches with match->cp_str, considering
2547 * match->cp_icase.
2548 */
2549 static int
2550ins_compl_equal(match, str, len)
2551 compl_T *match;
2552 char_u *str;
2553 int len;
2554{
2555 if (match->cp_icase)
2556 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2557 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2558}
2559
2560/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002561 * Reduce the longest common string for match "match".
2562 */
2563 static void
2564ins_compl_longest_match(match)
2565 compl_T *match;
2566{
2567 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002568 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002569 int had_match;
2570
2571 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002572 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002573 /* First match, use it as a whole. */
2574 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002575 if (compl_leader != NULL)
2576 {
2577 had_match = (curwin->w_cursor.col > compl_col);
2578 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002579 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002580 ins_redraw(FALSE);
2581
2582 /* When the match isn't there (to avoid matching itself) remove it
2583 * again after redrawing. */
2584 if (!had_match)
2585 ins_compl_delete();
2586 compl_used_match = FALSE;
2587 }
2588 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002589 else
2590 {
2591 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002592 p = compl_leader;
2593 s = match->cp_str;
2594 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002595 {
2596#ifdef FEAT_MBYTE
2597 if (has_mbyte)
2598 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002599 c1 = mb_ptr2char(p);
2600 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002601 }
2602 else
2603#endif
2604 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002605 c1 = *p;
2606 c2 = *s;
2607 }
2608 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2609 : (c1 != c2))
2610 break;
2611#ifdef FEAT_MBYTE
2612 if (has_mbyte)
2613 {
2614 mb_ptr_adv(p);
2615 mb_ptr_adv(s);
2616 }
2617 else
2618#endif
2619 {
2620 ++p;
2621 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002622 }
2623 }
2624
2625 if (*p != NUL)
2626 {
2627 /* Leader was shortened, need to change the inserted text. */
2628 *p = NUL;
2629 had_match = (curwin->w_cursor.col > compl_col);
2630 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002631 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002632 ins_redraw(FALSE);
2633
2634 /* When the match isn't there (to avoid matching itself) remove it
2635 * again after redrawing. */
2636 if (!had_match)
2637 ins_compl_delete();
2638 }
2639
2640 compl_used_match = FALSE;
2641 }
2642}
2643
2644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * Add an array of matches to the list of matches.
2646 * Frees matches[].
2647 */
2648 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002649ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 int num_matches;
2651 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002652 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653{
2654 int i;
2655 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002656 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
Bram Moolenaar572cb562005-08-05 21:35:02 +00002658 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002659 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002660 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002662 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 FreeWild(num_matches, matches);
2664}
2665
2666/* Make the completion list cyclic.
2667 * Return the number of matches (excluding the original).
2668 */
2669 static int
2670ins_compl_make_cyclic()
2671{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002672 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int count = 0;
2674
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002675 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 /*
2678 * Find the end of the list.
2679 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002680 match = compl_first_match;
2681 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002682 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002684 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 ++count;
2686 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002687 match->cp_next = compl_first_match;
2688 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690 return count;
2691}
2692
Bram Moolenaara94bc432006-03-10 21:42:59 +00002693/*
2694 * Start completion for the complete() function.
2695 * "startcol" is where the matched text starts (1 is first column).
2696 * "list" is the list of matches.
2697 */
2698 void
2699set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002700 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002701 list_T *list;
2702{
2703 /* If already doing completions stop it. */
2704 if (ctrl_x_mode != 0)
2705 ins_compl_prep(' ');
2706 ins_compl_clear();
2707
2708 if (stop_arrow() == FAIL)
2709 return;
2710
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002711 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002712 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002713 startcol = curwin->w_cursor.col;
2714 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002715 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002716 /* compl_pattern doesn't need to be set */
2717 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2718 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002719 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002720 return;
2721
2722 /* Handle like dictionary completion. */
2723 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2724
2725 ins_compl_add_list(list);
2726 compl_matches = ins_compl_make_cyclic();
2727 compl_started = TRUE;
2728 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002729 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002730
2731 compl_curr_match = compl_first_match;
2732 ins_complete(Ctrl_N);
2733 out_flush();
2734}
2735
2736
Bram Moolenaar9372a112005-12-06 19:59:18 +00002737/* "compl_match_array" points the currently displayed list of entries in the
2738 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002739static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002740static int compl_match_arraysize;
2741
2742/*
2743 * Update the screen and when there is any scrolling remove the popup menu.
2744 */
2745 static void
2746ins_compl_upd_pum()
2747{
2748 int h;
2749
2750 if (compl_match_array != NULL)
2751 {
2752 h = curwin->w_cline_height;
2753 update_screen(0);
2754 if (h != curwin->w_cline_height)
2755 ins_compl_del_pum();
2756 }
2757}
2758
2759/*
2760 * Remove any popup menu.
2761 */
2762 static void
2763ins_compl_del_pum()
2764{
2765 if (compl_match_array != NULL)
2766 {
2767 pum_undisplay();
2768 vim_free(compl_match_array);
2769 compl_match_array = NULL;
2770 }
2771}
2772
2773/*
2774 * Return TRUE if the popup menu should be displayed.
2775 */
2776 static int
2777pum_wanted()
2778{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002779 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002780 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002781 return FALSE;
2782
2783 /* The display looks bad on a B&W display. */
2784 if (t_colors < 8
2785#ifdef FEAT_GUI
2786 && !gui.in_use
2787#endif
2788 )
2789 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002790 return TRUE;
2791}
2792
2793/*
2794 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002795 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002796 */
2797 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002798pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002799{
2800 compl_T *compl;
2801 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002802
2803 /* Don't display the popup menu if there are no matches or there is only
2804 * one (ignoring the original text). */
2805 compl = compl_first_match;
2806 i = 0;
2807 do
2808 {
2809 if (compl == NULL
2810 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2811 break;
2812 compl = compl->cp_next;
2813 } while (compl != compl_first_match);
2814
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002815 if (strstr((char *)p_cot, "menuone") != NULL)
2816 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002817 return (i >= 2);
2818}
2819
2820/*
2821 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002822 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002823 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002824 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002825ins_compl_show_pum()
2826{
2827 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002828 compl_T *shown_compl = NULL;
2829 int did_find_shown_match = FALSE;
2830 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002831 int i;
2832 int cur = -1;
2833 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002834 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002835
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002836 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002837 return;
2838
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002839#if defined(FEAT_EVAL)
2840 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2841 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2842#endif
2843
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002844 /* Update the screen before drawing the popup menu over it. */
2845 update_screen(0);
2846
2847 if (compl_match_array == NULL)
2848 {
2849 /* Need to build the popup menu list. */
2850 compl_match_arraysize = 0;
2851 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002852 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002853 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002854 do
2855 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002856 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2857 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002858 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002859 ++compl_match_arraysize;
2860 compl = compl->cp_next;
2861 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002862 if (compl_match_arraysize == 0)
2863 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002864 compl_match_array = (pumitem_T *)alloc_clear(
2865 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002866 * compl_match_arraysize));
2867 if (compl_match_array != NULL)
2868 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002869 /* If the current match is the original text don't find the first
2870 * match after it, don't highlight anything. */
2871 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2872 shown_match_ok = TRUE;
2873
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874 i = 0;
2875 compl = compl_first_match;
2876 do
2877 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002878 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2879 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002880 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002881 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002882 if (!shown_match_ok)
2883 {
2884 if (compl == compl_shown_match || did_find_shown_match)
2885 {
2886 /* This item is the shown match or this is the
2887 * first displayed item after the shown match. */
2888 compl_shown_match = compl;
2889 did_find_shown_match = TRUE;
2890 shown_match_ok = TRUE;
2891 }
2892 else
2893 /* Remember this displayed match for when the
2894 * shown match is just below it. */
2895 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002896 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002897 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002898
2899 if (compl->cp_text[CPT_ABBR] != NULL)
2900 compl_match_array[i].pum_text =
2901 compl->cp_text[CPT_ABBR];
2902 else
2903 compl_match_array[i].pum_text = compl->cp_str;
2904 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2905 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2906 if (compl->cp_text[CPT_MENU] != NULL)
2907 compl_match_array[i++].pum_extra =
2908 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002909 else
2910 compl_match_array[i++].pum_extra = compl->cp_fname;
2911 }
2912
2913 if (compl == compl_shown_match)
2914 {
2915 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002916
2917 /* When the original text is the shown match don't set
2918 * compl_shown_match. */
2919 if (compl->cp_flags & ORIGINAL_TEXT)
2920 shown_match_ok = TRUE;
2921
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002922 if (!shown_match_ok && shown_compl != NULL)
2923 {
2924 /* The shown match isn't displayed, set it to the
2925 * previously displayed match. */
2926 compl_shown_match = shown_compl;
2927 shown_match_ok = TRUE;
2928 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002929 }
2930 compl = compl->cp_next;
2931 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002932
2933 if (!shown_match_ok) /* no displayed match at all */
2934 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002935 }
2936 }
2937 else
2938 {
2939 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002940 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002941 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2942 || compl_match_array[i].pum_text
2943 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002944 {
2945 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002946 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002947 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002948 }
2949
2950 if (compl_match_array != NULL)
2951 {
2952 /* Compute the screen column of the start of the completed text.
2953 * Use the cursor to get all wrapping and other settings right. */
2954 col = curwin->w_cursor.col;
2955 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002956 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002957 curwin->w_cursor.col = col;
2958 }
2959}
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#define DICT_FIRST (1) /* use just first element in "dict" */
2962#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002965 * Add any identifiers that match the given pattern in the list of dictionary
2966 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 */
2968 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002969ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2970 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002972 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002973 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002975 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 char_u *ptr;
2977 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 char_u **files;
2980 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002982 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaar0b238792006-03-02 22:49:12 +00002984 if (*dict == NUL)
2985 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002986#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002987 /* When 'dictionary' is empty and spell checking is enabled use
2988 * "spell". */
2989 if (!thesaurus && curwin->w_p_spell)
2990 dict = (char_u *)"spell";
2991 else
2992#endif
2993 return;
2994 }
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002997 if (buf == NULL)
2998 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002999 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 /* If 'infercase' is set, don't use 'smartcase' here */
3002 save_p_scs = p_scs;
3003 if (curbuf->b_p_inf)
3004 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003005
3006 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3007 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003008 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003009 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3010 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003011 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003012 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003013
3014 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003015 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003016 len = STRLEN(pat_esc) + 10;
3017 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003018 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003019 {
3020 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003021 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003022 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003023 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003024 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3025 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003026 vim_free(ptr);
3027 }
3028 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003029 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003030 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003031 if (regmatch.regprog == NULL)
3032 goto theend;
3033 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3036 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003037 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 /* copy one dictionary file name into buf */
3040 if (flags == DICT_EXACT)
3041 {
3042 count = 1;
3043 files = &dict;
3044 }
3045 else
3046 {
3047 /* Expand wildcards in the dictionary name, but do not allow
3048 * backticks (for security, the 'dict' option may have been set in
3049 * a modeline). */
3050 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003051# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003052 if (!thesaurus && STRCMP(buf, "spell") == 0)
3053 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003054 else
3055# endif
3056 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 || expand_wildcards(1, &buf, &count, &files,
3058 EW_FILE|EW_SILENT) != OK)
3059 count = 0;
3060 }
3061
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003062# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003063 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003065 /* Complete from active spelling. Skip "\<" in the pattern, we
3066 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003067 if (pat[0] == '\\' && pat[1] == '<')
3068 ptr = pat + 2;
3069 else
3070 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003071 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003073 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003074# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003075 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003076 {
3077 ins_compl_files(count, files, thesaurus, flags,
3078 &regmatch, buf, &dir);
3079 if (flags != DICT_EXACT)
3080 FreeWild(count, files);
3081 }
3082 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 break;
3084 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003085
3086theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 p_scs = save_p_scs;
3088 vim_free(regmatch.regprog);
3089 vim_free(buf);
3090}
3091
Bram Moolenaar0b238792006-03-02 22:49:12 +00003092 static void
3093ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3094 int count;
3095 char_u **files;
3096 int thesaurus;
3097 int flags;
3098 regmatch_T *regmatch;
3099 char_u *buf;
3100 int *dir;
3101{
3102 char_u *ptr;
3103 int i;
3104 FILE *fp;
3105 int add_r;
3106
3107 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3108 {
3109 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3110 if (flags != DICT_EXACT)
3111 {
3112 vim_snprintf((char *)IObuff, IOSIZE,
3113 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003114 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003115 }
3116
3117 if (fp != NULL)
3118 {
3119 /*
3120 * Read dictionary file line by line.
3121 * Check each line for a match.
3122 */
3123 while (!got_int && !compl_interrupted
3124 && !vim_fgets(buf, LSIZE, fp))
3125 {
3126 ptr = buf;
3127 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3128 {
3129 ptr = regmatch->startp[0];
3130 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3131 ptr = find_line_end(ptr);
3132 else
3133 ptr = find_word_end(ptr);
3134 add_r = ins_compl_add_infercase(regmatch->startp[0],
3135 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003136 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003137 if (thesaurus)
3138 {
3139 char_u *wstart;
3140
3141 /*
3142 * Add the other matches on the line
3143 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003144 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003145 while (!got_int)
3146 {
3147 /* Find start of the next word. Skip white
3148 * space and punctuation. */
3149 ptr = find_word_start(ptr);
3150 if (*ptr == NUL || *ptr == NL)
3151 break;
3152 wstart = ptr;
3153
Bram Moolenaara2993e12007-08-12 14:38:46 +00003154 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003155#ifdef FEAT_MBYTE
3156 if (has_mbyte)
3157 /* Japanese words may have characters in
3158 * different classes, only separate words
3159 * with single-byte non-word characters. */
3160 while (*ptr != NUL)
3161 {
3162 int l = (*mb_ptr2len)(ptr);
3163
3164 if (l < 2 && !vim_iswordc(*ptr))
3165 break;
3166 ptr += l;
3167 }
3168 else
3169#endif
3170 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003171
3172 /* Add the word. Skip the regexp match. */
3173 if (wstart != regmatch->startp[0])
3174 add_r = ins_compl_add_infercase(wstart,
3175 (int)(ptr - wstart),
3176 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003177 }
3178 }
3179 if (add_r == OK)
3180 /* if dir was BACKWARD then honor it just once */
3181 *dir = FORWARD;
3182 else if (add_r == FAIL)
3183 break;
3184 /* avoid expensive call to vim_regexec() when at end
3185 * of line */
3186 if (*ptr == '\n' || got_int)
3187 break;
3188 }
3189 line_breakcheck();
3190 ins_compl_check_keys(50);
3191 }
3192 fclose(fp);
3193 }
3194 }
3195}
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197/*
3198 * Find the start of the next word.
3199 * Returns a pointer to the first char of the word. Also stops at a NUL.
3200 */
3201 char_u *
3202find_word_start(ptr)
3203 char_u *ptr;
3204{
3205#ifdef FEAT_MBYTE
3206 if (has_mbyte)
3207 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003208 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 else
3210#endif
3211 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3212 ++ptr;
3213 return ptr;
3214}
3215
3216/*
3217 * Find the end of the word. Assumes it starts inside a word.
3218 * Returns a pointer to just after the word.
3219 */
3220 char_u *
3221find_word_end(ptr)
3222 char_u *ptr;
3223{
3224#ifdef FEAT_MBYTE
3225 int start_class;
3226
3227 if (has_mbyte)
3228 {
3229 start_class = mb_get_class(ptr);
3230 if (start_class > 1)
3231 while (*ptr != NUL)
3232 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003233 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 if (mb_get_class(ptr) != start_class)
3235 break;
3236 }
3237 }
3238 else
3239#endif
3240 while (vim_iswordc(*ptr))
3241 ++ptr;
3242 return ptr;
3243}
3244
3245/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003246 * Find the end of the line, omitting CR and NL at the end.
3247 * Returns a pointer to just after the line.
3248 */
3249 static char_u *
3250find_line_end(ptr)
3251 char_u *ptr;
3252{
3253 char_u *s;
3254
3255 s = ptr + STRLEN(ptr);
3256 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3257 --s;
3258 return s;
3259}
3260
3261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 * Free the list of completions
3263 */
3264 static void
3265ins_compl_free()
3266{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003267 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003268 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003270 vim_free(compl_pattern);
3271 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003272 vim_free(compl_leader);
3273 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003275 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003277
3278 ins_compl_del_pum();
3279 pum_clear();
3280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003281 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 do
3283 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003284 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003285 compl_curr_match = compl_curr_match->cp_next;
3286 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003288 if (match->cp_flags & FREE_FNAME)
3289 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003290 for (i = 0; i < CPT_COUNT; ++i)
3291 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003293 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3294 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003295 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296}
3297
3298 static void
3299ins_compl_clear()
3300{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003301 compl_cont_status = 0;
3302 compl_started = FALSE;
3303 compl_matches = 0;
3304 vim_free(compl_pattern);
3305 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003306 vim_free(compl_leader);
3307 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003309 vim_free(compl_orig_text);
3310 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003311 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312}
3313
3314/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003315 * Return TRUE when Insert completion is active.
3316 */
3317 int
3318ins_compl_active()
3319{
3320 return compl_started;
3321}
3322
3323/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003324 * Delete one character before the cursor and show the subset of the matches
3325 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003326 * Returns the character to be used, NUL if the work is done and another char
3327 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003328 */
3329 static int
3330ins_compl_bs()
3331{
3332 char_u *line;
3333 char_u *p;
3334
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003335 line = ml_get_curline();
3336 p = line + curwin->w_cursor.col;
3337 mb_ptr_back(line, p);
3338
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003339 /* Stop completion when the whole word was deleted. For Omni completion
3340 * allow the word to be deleted, we won't match everything. */
3341 if ((int)(p - line) - (int)compl_col < 0
3342 || ((int)(p - line) - (int)compl_col == 0
3343 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003344 return K_BS;
3345
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003346 /* Deleted more than what was used to find matches or didn't finish
3347 * finding all matches: need to look for matches all over again. */
3348 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003349 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003350 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003351
Bram Moolenaara6557602006-02-04 22:43:20 +00003352 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003353 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003354 if (compl_leader != NULL)
3355 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003356 ins_compl_new_leader();
3357 return NUL;
3358 }
3359 return K_BS;
3360}
Bram Moolenaara6557602006-02-04 22:43:20 +00003361
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003362/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003363 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3364 * be called.
3365 */
3366 static int
3367ins_compl_need_restart()
3368{
3369 /* Return TRUE if we didn't complete finding matches or when the
3370 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3371 return compl_was_interrupted
3372 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3373 && compl_opt_refresh_always);
3374}
3375
3376/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003377 * Called after changing "compl_leader".
3378 * Show the popup menu with a different set of matches.
3379 * May also search for matches again if the previous search was interrupted.
3380 */
3381 static void
3382ins_compl_new_leader()
3383{
3384 ins_compl_del_pum();
3385 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003386 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003387 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003388
3389 if (compl_started)
3390 ins_compl_set_original_text(compl_leader);
3391 else
3392 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003393#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003394 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003395#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003396 /*
3397 * Matches were cleared, need to search for them now. First display
3398 * the changed text before the cursor. Set "compl_restarting" to
3399 * avoid that the first match is inserted.
3400 */
3401 update_screen(0);
3402#ifdef FEAT_GUI
3403 if (gui.in_use)
3404 {
3405 /* Show the cursor after the match, not after the redrawn text. */
3406 setcursor();
3407 out_flush();
3408 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003409 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003410#endif
3411 compl_restarting = TRUE;
3412 if (ins_complete(Ctrl_N) == FAIL)
3413 compl_cont_status = 0;
3414 compl_restarting = FALSE;
3415 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003416
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003417 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003418
3419 /* Show the popup menu with a different set of matches. */
3420 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003421
3422 /* Don't let Enter select the original text when there is no popup menu. */
3423 if (compl_match_array == NULL)
3424 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003425}
3426
3427/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003428 * Return the length of the completion, from the completion start column to
3429 * the cursor column. Making sure it never goes below zero.
3430 */
3431 static int
3432ins_compl_len()
3433{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003434 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003435
3436 if (off < 0)
3437 return 0;
3438 return off;
3439}
3440
3441/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003442 * Append one character to the match leader. May reduce the number of
3443 * matches.
3444 */
3445 static void
3446ins_compl_addleader(c)
3447 int c;
3448{
3449#ifdef FEAT_MBYTE
3450 int cc;
3451
3452 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3453 {
3454 char_u buf[MB_MAXBYTES + 1];
3455
3456 (*mb_char2bytes)(c, buf);
3457 buf[cc] = NUL;
3458 ins_char_bytes(buf, cc);
3459 }
3460 else
3461#endif
3462 ins_char(c);
3463
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003464 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003465 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003466 ins_compl_restart();
3467
Bram Moolenaara6557602006-02-04 22:43:20 +00003468 vim_free(compl_leader);
3469 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003470 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003471 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472 ins_compl_new_leader();
3473}
3474
3475/*
3476 * Setup for finding completions again without leaving CTRL-X mode. Used when
3477 * BS or a key was typed while still searching for matches.
3478 */
3479 static void
3480ins_compl_restart()
3481{
3482 ins_compl_free();
3483 compl_started = FALSE;
3484 compl_matches = 0;
3485 compl_cont_status = 0;
3486 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003487}
3488
3489/*
3490 * Set the first match, the original text.
3491 */
3492 static void
3493ins_compl_set_original_text(str)
3494 char_u *str;
3495{
3496 char_u *p;
3497
3498 /* Replace the original text entry. */
3499 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3500 {
3501 p = vim_strsave(str);
3502 if (p != NULL)
3503 {
3504 vim_free(compl_first_match->cp_str);
3505 compl_first_match->cp_str = p;
3506 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003507 }
3508}
3509
3510/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003511 * Append one character to the match leader. May reduce the number of
3512 * matches.
3513 */
3514 static void
3515ins_compl_addfrommatch()
3516{
3517 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003518 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003519 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003520 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003521
3522 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003523 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003524 {
3525 /* When still at the original match use the first entry that matches
3526 * the leader. */
3527 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3528 {
3529 p = NULL;
3530 for (cp = compl_shown_match->cp_next; cp != NULL
3531 && cp != compl_first_match; cp = cp->cp_next)
3532 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003533 if (compl_leader == NULL
3534 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003535 (int)STRLEN(compl_leader)))
3536 {
3537 p = cp->cp_str;
3538 break;
3539 }
3540 }
3541 if (p == NULL || (int)STRLEN(p) <= len)
3542 return;
3543 }
3544 else
3545 return;
3546 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003547 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003548 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003549 ins_compl_addleader(c);
3550}
3551
3552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003554 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003555 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003557 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558ins_compl_prep(c)
3559 int c;
3560{
3561 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003563 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 /* Forget any previous 'special' messages if this is actually
3566 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3567 */
3568 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3569 edit_submode_extra = NULL;
3570
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003571 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003572 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3573 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003574 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003576 /* Set "compl_get_longest" when finding the first matches. */
3577 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3578 || (ctrl_x_mode == 0 && !compl_started))
3579 {
3580 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3581 compl_used_match = TRUE;
3582 }
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3585 {
3586 /*
3587 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3588 * it will be yet. Now we decide.
3589 */
3590 switch (c)
3591 {
3592 case Ctrl_E:
3593 case Ctrl_Y:
3594 ctrl_x_mode = CTRL_X_SCROLL;
3595 if (!(State & REPLACE_FLAG))
3596 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3597 else
3598 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3599 edit_submode_pre = NULL;
3600 showmode();
3601 break;
3602 case Ctrl_L:
3603 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3604 break;
3605 case Ctrl_F:
3606 ctrl_x_mode = CTRL_X_FILES;
3607 break;
3608 case Ctrl_K:
3609 ctrl_x_mode = CTRL_X_DICTIONARY;
3610 break;
3611 case Ctrl_R:
3612 /* Simply allow ^R to happen without affecting ^X mode */
3613 break;
3614 case Ctrl_T:
3615 ctrl_x_mode = CTRL_X_THESAURUS;
3616 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003617#ifdef FEAT_COMPL_FUNC
3618 case Ctrl_U:
3619 ctrl_x_mode = CTRL_X_FUNCTION;
3620 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003621 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003622 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003623 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003624#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003625 case 's':
3626 case Ctrl_S:
3627 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003628#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003629 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003630 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003631 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003632#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 case Ctrl_RSB:
3635 ctrl_x_mode = CTRL_X_TAGS;
3636 break;
3637#ifdef FEAT_FIND_ID
3638 case Ctrl_I:
3639 case K_S_TAB:
3640 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3641 break;
3642 case Ctrl_D:
3643 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3644 break;
3645#endif
3646 case Ctrl_V:
3647 case Ctrl_Q:
3648 ctrl_x_mode = CTRL_X_CMDLINE;
3649 break;
3650 case Ctrl_P:
3651 case Ctrl_N:
3652 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3653 * just started ^X mode, or there were enough ^X's to cancel
3654 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3655 * do normal expansion when interrupting a different mode (say
3656 * ^X^F^X^P or ^P^X^X^P, see below)
3657 * nothing changes if interrupting mode 0, (eg, the flag
3658 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003659 if (!(compl_cont_status & CONT_INTRPT))
3660 compl_cont_status |= CONT_LOCAL;
3661 else if (compl_cont_mode != 0)
3662 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 /* FALLTHROUGH */
3664 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003665 /* If we have typed at least 2 ^X's... for modes != 0, we set
3666 * compl_cont_status = 0 (eg, as if we had just started ^X
3667 * mode).
3668 * For mode 0, we set "compl_cont_mode" to an impossible
3669 * value, in both cases ^X^X can be used to restart the same
3670 * mode (avoiding ADDING mode).
3671 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3672 * 'complete' and local ^P expansions respectively.
3673 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3674 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (c == Ctrl_X)
3676 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003677 if (compl_cont_mode != 0)
3678 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003680 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682 ctrl_x_mode = 0;
3683 edit_submode = NULL;
3684 showmode();
3685 break;
3686 }
3687 }
3688 else if (ctrl_x_mode != 0)
3689 {
3690 /* We're already in CTRL-X mode, do we stay in it? */
3691 if (!vim_is_ctrl_x_key(c))
3692 {
3693 if (ctrl_x_mode == CTRL_X_SCROLL)
3694 ctrl_x_mode = 0;
3695 else
3696 ctrl_x_mode = CTRL_X_FINISHED;
3697 edit_submode = NULL;
3698 }
3699 showmode();
3700 }
3701
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 /* Show error message from attempted keyword completion (probably
3705 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003706 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003708 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3709 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 || ctrl_x_mode == CTRL_X_FINISHED)
3711 {
3712 /* Get here when we have finished typing a sequence of ^N and
3713 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003715 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
3717 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003718 * If any of the original typed text has been changed, eg when
3719 * ignorecase is set, we must add back-spaces to the redo
3720 * buffer. We add as few as necessary to delete just the part
3721 * of the original text that has changed.
3722 * When using the longest match, edited the match or used
3723 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003725 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3726 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003727 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003728 ptr = NULL;
3729 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731
3732#ifdef FEAT_CINDENT
3733 want_cindent = (can_cindent && cindent_on());
3734#endif
3735 /*
3736 * When completing whole lines: fix indent for 'cindent'.
3737 * Otherwise, break line if it's too long.
3738 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003739 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
3741#ifdef FEAT_CINDENT
3742 /* re-indent the current line */
3743 if (want_cindent)
3744 {
3745 do_c_expr_indent();
3746 want_cindent = FALSE; /* don't do it again */
3747 }
3748#endif
3749 }
3750 else
3751 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003752 int prev_col = curwin->w_cursor.col;
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003755 if (prev_col > 0)
3756 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (stop_arrow() == OK)
3758 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003759 if (prev_col > 0
3760 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3761 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003764 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003765 * the selection without inserting anything. When
3766 * compl_enter_selects is set the Enter key does the same. */
3767 if ((c == Ctrl_Y || (compl_enter_selects
3768 && (c == CAR || c == K_KENTER || c == NL)))
3769 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003770 retval = TRUE;
3771
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003772 /* CTRL-E means completion is Ended, go back to the typed text. */
3773 if (c == Ctrl_E)
3774 {
3775 ins_compl_delete();
3776 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003777 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003778 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003779 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003780 retval = TRUE;
3781 }
3782
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003783 auto_format(FALSE, TRUE);
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 compl_started = FALSE;
3787 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 msg_clr_cmdline(); /* necessary for "noshowmode" */
3789 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003790 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (edit_submode != NULL)
3792 {
3793 edit_submode = NULL;
3794 showmode();
3795 }
3796
3797#ifdef FEAT_CINDENT
3798 /*
3799 * Indent now if a key was typed that is in 'cinkeys'.
3800 */
3801 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3802 do_c_expr_indent();
3803#endif
3804 }
3805 }
3806
3807 /* reset continue_* if we left expansion-mode, if we stay they'll be
3808 * (re)set properly in ins_complete() */
3809 if (!vim_is_ctrl_x_key(c))
3810 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 compl_cont_status = 0;
3812 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003814
3815 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816}
3817
3818/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003819 * Fix the redo buffer for the completion leader replacing some of the typed
3820 * text. This inserts backspaces and appends the changed text.
3821 * "ptr" is the known leader text or NUL.
3822 */
3823 static void
3824ins_compl_fixRedoBufForLeader(ptr_arg)
3825 char_u *ptr_arg;
3826{
3827 int len;
3828 char_u *p;
3829 char_u *ptr = ptr_arg;
3830
3831 if (ptr == NULL)
3832 {
3833 if (compl_leader != NULL)
3834 ptr = compl_leader;
3835 else
3836 return; /* nothing to do */
3837 }
3838 if (compl_orig_text != NULL)
3839 {
3840 p = compl_orig_text;
3841 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3842 ;
3843#ifdef FEAT_MBYTE
3844 if (len > 0)
3845 len -= (*mb_head_off)(p, p + len);
3846#endif
3847 for (p += len; *p != NUL; mb_ptr_adv(p))
3848 AppendCharToRedobuff(K_BS);
3849 }
3850 else
3851 len = 0;
3852 if (ptr != NULL)
3853 AppendToRedobuffLit(ptr + len, -1);
3854}
3855
3856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3858 * (depending on flag) starting from buf and looking for a non-scanned
3859 * buffer (other than curbuf). curbuf is special, if it is called with
3860 * buf=curbuf then it has to be the first call for a given flag/expansion.
3861 *
3862 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3863 */
3864 static buf_T *
3865ins_compl_next_buf(buf, flag)
3866 buf_T *buf;
3867 int flag;
3868{
3869#ifdef FEAT_WINDOWS
3870 static win_T *wp;
3871#endif
3872
3873 if (flag == 'w') /* just windows */
3874 {
3875#ifdef FEAT_WINDOWS
3876 if (buf == curbuf) /* first call for this flag/expansion */
3877 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003878 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 && wp->w_buffer->b_scanned)
3880 ;
3881 buf = wp->w_buffer;
3882#else
3883 buf = curbuf;
3884#endif
3885 }
3886 else
3887 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3888 * (unlisted buffers)
3889 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003890 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 && ((flag == 'U'
3892 ? buf->b_p_bl
3893 : (!buf->b_p_bl
3894 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003895 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 ;
3897 return buf;
3898}
3899
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003900#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003901static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003902
3903/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003904 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003905 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003906 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003907 static void
3908expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003909 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003910 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003911{
Bram Moolenaar82139082011-09-14 16:52:09 +02003912 list_T *matchlist = NULL;
3913 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003914 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003915 char_u *funcname;
3916 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003917 win_T *curwin_save;
3918 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003919 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003920
Bram Moolenaare344bea2005-09-01 20:46:49 +00003921 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3922 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003923 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003924
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003925 /* Call 'completefunc' to obtain the list of matches. */
3926 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003927 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003928
Bram Moolenaare344bea2005-09-01 20:46:49 +00003929 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003930 curwin_save = curwin;
3931 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02003932
3933 /* Call a function, which returns a list or dict. */
3934 if (call_vim_function(funcname, 2, args, FALSE, &rettv) == OK)
3935 {
3936 switch (rettv.v_type)
3937 {
3938 case VAR_LIST:
3939 matchlist = rettv.vval.v_list;
3940 break;
3941 case VAR_DICT:
3942 matchdict = rettv.vval.v_dict;
3943 break;
3944 default:
3945 /* TODO: Give error message? */
3946 clear_tv(&rettv);
3947 break;
3948 }
3949 }
3950
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003951 if (curwin_save != curwin || curbuf_save != curbuf)
3952 {
3953 EMSG(_(e_complwin));
3954 goto theend;
3955 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00003956 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003957 check_cursor();
3958 if (!equalpos(curwin->w_cursor, pos))
3959 {
3960 EMSG(_(e_compldel));
3961 goto theend;
3962 }
Bram Moolenaar82139082011-09-14 16:52:09 +02003963
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003964 if (matchlist != NULL)
3965 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02003966 else if (matchdict != NULL)
3967 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003968
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003969theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02003970 if (matchdict != NULL)
3971 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003972 if (matchlist != NULL)
3973 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003974}
3975#endif /* FEAT_COMPL_FUNC */
3976
Bram Moolenaar39f05632006-03-19 22:15:26 +00003977#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003978/*
3979 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003980 */
3981 static void
3982ins_compl_add_list(list)
3983 list_T *list;
3984{
3985 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003986 int dir = compl_direction;
3987
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003988 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003989 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003990 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003991 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3992 /* if dir was BACKWARD then honor it just once */
3993 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003994 else if (did_emsg)
3995 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003996 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003997}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003998
3999/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004000 * Add completions from a dict.
4001 */
4002 static void
4003ins_compl_add_dict(dict)
4004 dict_T *dict;
4005{
4006 dictitem_T *refresh;
4007 dictitem_T *words;
4008
4009 /* Check for optional "refresh" item. */
4010 compl_opt_refresh_always = FALSE;
4011 refresh = dict_find(dict, (char_u *)"refresh", 7);
4012 if (refresh != NULL && refresh->di_tv.v_type == VAR_STRING)
4013 {
4014 char_u *v = refresh->di_tv.vval.v_string;
4015
4016 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4017 compl_opt_refresh_always = TRUE;
4018 }
4019
4020 /* Add completions from a "words" list. */
4021 words = dict_find(dict, (char_u *)"words", 5);
4022 if (words != NULL && words->di_tv.v_type == VAR_LIST)
4023 ins_compl_add_list(words->di_tv.vval.v_list);
4024}
4025
4026/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004027 * Add a match to the list of matches from a typeval_T.
4028 * If the given string is already in the list of completions, then return
4029 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4030 * maybe because alloc() returns NULL, then FAIL is returned.
4031 */
4032 int
4033ins_compl_add_tv(tv, dir)
4034 typval_T *tv;
4035 int dir;
4036{
4037 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004038 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004039 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004040 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004041 char_u *(cptext[CPT_COUNT]);
4042
4043 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4044 {
4045 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4046 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4047 (char_u *)"abbr", FALSE);
4048 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4049 (char_u *)"menu", FALSE);
4050 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4051 (char_u *)"kind", FALSE);
4052 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4053 (char_u *)"info", FALSE);
4054 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4055 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004056 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004057 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004058 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4059 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004060 }
4061 else
4062 {
4063 word = get_tv_string_chk(tv);
4064 vim_memset(cptext, 0, sizeof(cptext));
4065 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004066 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004067 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004068 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004069}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004070#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072/*
4073 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004074 * The search starts at position "ini" in curbuf and in the direction
4075 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004076 * When "compl_started" is FALSE start at that position, otherwise continue
4077 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 * This may return before finding all the matches.
4079 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 */
4081 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004082ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
4085 static pos_T first_match_pos;
4086 static pos_T last_match_pos;
4087 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 static int found_all = FALSE; /* Found all matches of a
4089 certain type. */
4090 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Bram Moolenaar572cb562005-08-05 21:35:02 +00004092 pos_T *pos;
4093 char_u **matches;
4094 int save_p_scs;
4095 int save_p_ws;
4096 int save_p_ic;
4097 int i;
4098 int num_matches;
4099 int len;
4100 int found_new_match;
4101 int type = ctrl_x_mode;
4102 char_u *ptr;
4103 char_u *dict = NULL;
4104 int dict_f = 0;
4105 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004107 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
4109 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4110 ins_buf->b_scanned = 0;
4111 found_all = FALSE;
4112 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004113 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004114 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 last_match_pos = first_match_pos = *ini;
4116 }
4117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004119 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4121 for (;;)
4122 {
4123 found_new_match = FAIL;
4124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004125 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * or if found_all says this entry is done. For ^X^L only use the
4127 * entries from 'complete' that look in loaded buffers. */
4128 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 found_all = FALSE;
4132 while (*e_cpt == ',' || *e_cpt == ' ')
4133 e_cpt++;
4134 if (*e_cpt == '.' && !curbuf->b_scanned)
4135 {
4136 ins_buf = curbuf;
4137 first_match_pos = *ini;
4138 /* So that ^N can match word immediately after cursor */
4139 if (ctrl_x_mode == 0)
4140 dec(&first_match_pos);
4141 last_match_pos = first_match_pos;
4142 type = 0;
4143 }
4144 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4145 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4146 {
4147 /* Scan a buffer, but not the current one. */
4148 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4149 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 first_match_pos.col = last_match_pos.col = 0;
4152 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4153 last_match_pos.lnum = 0;
4154 type = 0;
4155 }
4156 else /* unloaded buffer, scan like dictionary */
4157 {
4158 found_all = TRUE;
4159 if (ins_buf->b_fname == NULL)
4160 continue;
4161 type = CTRL_X_DICTIONARY;
4162 dict = ins_buf->b_fname;
4163 dict_f = DICT_EXACT;
4164 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004165 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 ins_buf->b_fname == NULL
4167 ? buf_spname(ins_buf)
4168 : ins_buf->b_sfname == NULL
4169 ? (char *)ins_buf->b_fname
4170 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004171 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 else if (*e_cpt == NUL)
4174 break;
4175 else
4176 {
4177 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4178 type = -1;
4179 else if (*e_cpt == 'k' || *e_cpt == 's')
4180 {
4181 if (*e_cpt == 'k')
4182 type = CTRL_X_DICTIONARY;
4183 else
4184 type = CTRL_X_THESAURUS;
4185 if (*++e_cpt != ',' && *e_cpt != NUL)
4186 {
4187 dict = e_cpt;
4188 dict_f = DICT_FIRST;
4189 }
4190 }
4191#ifdef FEAT_FIND_ID
4192 else if (*e_cpt == 'i')
4193 type = CTRL_X_PATH_PATTERNS;
4194 else if (*e_cpt == 'd')
4195 type = CTRL_X_PATH_DEFINES;
4196#endif
4197 else if (*e_cpt == ']' || *e_cpt == 't')
4198 {
4199 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004200 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004201 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 else
4204 type = -1;
4205
4206 /* in any case e_cpt is advanced to the next entry */
4207 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4208
4209 found_all = TRUE;
4210 if (type == -1)
4211 continue;
4212 }
4213 }
4214
4215 switch (type)
4216 {
4217 case -1:
4218 break;
4219#ifdef FEAT_FIND_ID
4220 case CTRL_X_PATH_PATTERNS:
4221 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004222 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4227 (linenr_T)1, (linenr_T)MAXLNUM);
4228 break;
4229#endif
4230
4231 case CTRL_X_DICTIONARY:
4232 case CTRL_X_THESAURUS:
4233 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004234 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 : (type == CTRL_X_THESAURUS
4236 ? (*curbuf->b_p_tsr == NUL
4237 ? p_tsr
4238 : curbuf->b_p_tsr)
4239 : (*curbuf->b_p_dict == NUL
4240 ? p_dict
4241 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004242 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004243 dict != NULL ? dict_f
4244 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 dict = NULL;
4246 break;
4247
4248 case CTRL_X_TAGS:
4249 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4250 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004253 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 * of matches is found when compl_pattern is empty */
4255 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4257 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4258 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4259 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004260 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 p_ic = save_p_ic;
4263 break;
4264
4265 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4268 {
4269
4270 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004272 ins_compl_add_matches(num_matches, matches,
4273#ifdef CASE_INSENSITIVE_FILENAME
4274 TRUE
4275#else
4276 FALSE
4277#endif
4278 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 break;
4281
4282 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 if (expand_cmdline(&compl_xp, compl_pattern,
4284 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004286 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 break;
4288
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004289#ifdef FEAT_COMPL_FUNC
4290 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004291 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004292 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004293 break;
4294#endif
4295
Bram Moolenaar488c6512005-08-11 20:09:58 +00004296 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004297#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004298 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004299 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004300 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004301 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004302#endif
4303 break;
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 default: /* normal ^P/^N and ^X^L */
4306 /*
4307 * If 'infercase' is set, don't use 'smartcase' here
4308 */
4309 save_p_scs = p_scs;
4310 if (ins_buf->b_p_inf)
4311 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004312
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 /* buffers other than curbuf are scanned from the beginning or the
4314 * end but never from the middle, thus setting nowrapscan in this
4315 * buffers is a good idea, on the other hand, we always set
4316 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4317 save_p_ws = p_ws;
4318 if (ins_buf != curbuf)
4319 p_ws = FALSE;
4320 else if (*e_cpt == '.')
4321 p_ws = TRUE;
4322 for (;;)
4323 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004324 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004326 ++msg_silent; /* Don't want messages for wrapscan. */
4327
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004328 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4329 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004333 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004335 found_new_match = searchit(NULL, ins_buf, pos,
4336 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004338 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004339 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004342 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004343 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 first_match_pos = *pos;
4345 last_match_pos = *pos;
4346 }
4347 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004348 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 found_new_match = FAIL;
4350 if (found_new_match == FAIL)
4351 {
4352 if (ins_buf == curbuf)
4353 found_all = TRUE;
4354 break;
4355 }
4356
4357 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 && ini->lnum == pos->lnum
4360 && ini->col == pos->col)
4361 continue;
4362 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4363 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4364 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
4367 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4368 continue;
4369 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4370 if (!p_paste)
4371 ptr = skipwhite(ptr);
4372 }
4373 len = (int)STRLEN(ptr);
4374 }
4375 else
4376 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004377 char_u *tmp_ptr = ptr;
4378
4379 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004381 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 /* Skip if already inside a word. */
4383 if (vim_iswordp(tmp_ptr))
4384 continue;
4385 /* Find start of next word. */
4386 tmp_ptr = find_word_start(tmp_ptr);
4387 }
4388 /* Find end of this word. */
4389 tmp_ptr = find_word_end(tmp_ptr);
4390 len = (int)(tmp_ptr - ptr);
4391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 if ((compl_cont_status & CONT_ADDING)
4393 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4396 {
4397 /* Try next line, if any. the new word will be
4398 * "join" as if the normal command "J" was used.
4399 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 * works -- Acevedo */
4402 STRNCPY(IObuff, ptr, len);
4403 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4404 tmp_ptr = ptr = skipwhite(ptr);
4405 /* Find start of next word. */
4406 tmp_ptr = find_word_start(tmp_ptr);
4407 /* Find end of next word. */
4408 tmp_ptr = find_word_end(tmp_ptr);
4409 if (tmp_ptr > ptr)
4410 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004411 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004413 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 IObuff[len++] = ' ';
4415 /* IObuf =~ "\k.* ", thus len >= 2 */
4416 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004417 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 || (vim_strchr(p_cpo, CPO_JOINSP)
4419 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004420 && (IObuff[len - 2] == '?'
4421 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 IObuff[len++] = ' ';
4423 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004424 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (tmp_ptr - ptr >= IOSIZE - len)
4426 tmp_ptr = ptr + IOSIZE - len - 1;
4427 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4428 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004429 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 IObuff[len] = NUL;
4432 ptr = IObuff;
4433 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 continue;
4436 }
4437 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004438 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004439 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004440 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
4442 found_new_match = OK;
4443 break;
4444 }
4445 }
4446 p_scs = save_p_scs;
4447 p_ws = save_p_ws;
4448 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004449
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004451 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004452 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 found_new_match = OK;
4454
4455 /* break the loop for specialized modes (use 'complete' just for the
4456 * generic ctrl_x_mode == 0) or when we've found a new match */
4457 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004459 {
4460 if (got_int)
4461 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004462 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004463 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004464 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004465
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004466 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4467 || compl_interrupted)
4468 break;
4469 compl_started = TRUE;
4470 }
4471 else
4472 {
4473 /* Mark a buffer scanned when it has been scanned completely */
4474 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4475 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004477 compl_started = FALSE;
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4483 && *e_cpt == NUL) /* Got to end of 'complete' */
4484 found_new_match = FAIL;
4485
4486 i = -1; /* total of matches, unknown */
4487 if (found_new_match == FAIL
4488 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4489 i = ins_compl_make_cyclic();
4490
4491 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 * just been made cyclic then we have to move compl_curr_match to the next
4493 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004494 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4495 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004496 if (compl_curr_match == NULL)
4497 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 return i;
4499}
4500
4501/* Delete the old text being completed. */
4502 static void
4503ins_compl_delete()
4504{
4505 int i;
4506
4507 /*
4508 * In insert mode: Delete the typed part.
4509 * In replace mode: Put the old characters back, if any.
4510 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004511 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 backspace_until_column(i);
4513 changed_cline_bef_curs();
4514}
4515
4516/* Insert the new text being completed. */
4517 static void
4518ins_compl_insert()
4519{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004520 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004521 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4522 compl_used_match = FALSE;
4523 else
4524 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525}
4526
4527/*
4528 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004529 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4530 * get more completions. If it is FALSE, then we just do nothing when there
4531 * are no more completions in a given direction. The latter case is used when
4532 * we are still in the middle of finding completions, to allow browsing
4533 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 * Return the total number of matches, or -1 if still unknown -- webb.
4535 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4537 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 *
4539 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004540 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4541 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
4543 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004544ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004546 int count; /* repeat completion this many times; should
4547 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004548 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549{
4550 int num_matches = -1;
4551 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004552 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004553 compl_T *found_compl = NULL;
4554 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004555 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004557 if (compl_leader != NULL
4558 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004560 /* Set "compl_shown_match" to the actually shown match, it may differ
4561 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004562 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004563 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004564 && compl_shown_match->cp_next != NULL
4565 && compl_shown_match->cp_next != compl_first_match)
4566 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004567
4568 /* If we didn't find it searching forward, and compl_shows_dir is
4569 * backward, find the last match. */
4570 if (compl_shows_dir == BACKWARD
4571 && !ins_compl_equal(compl_shown_match,
4572 compl_leader, (int)STRLEN(compl_leader))
4573 && (compl_shown_match->cp_next == NULL
4574 || compl_shown_match->cp_next == compl_first_match))
4575 {
4576 while (!ins_compl_equal(compl_shown_match,
4577 compl_leader, (int)STRLEN(compl_leader))
4578 && compl_shown_match->cp_prev != NULL
4579 && compl_shown_match->cp_prev != compl_first_match)
4580 compl_shown_match = compl_shown_match->cp_prev;
4581 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004582 }
4583
4584 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004585 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /* Delete old text to be replaced */
4587 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004588
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004589 /* When finding the longest common text we stick at the original text,
4590 * don't let CTRL-N or CTRL-P move to the first match. */
4591 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4592
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004593 /* When restarting the search don't insert the first match either. */
4594 if (compl_restarting)
4595 {
4596 advance = FALSE;
4597 compl_restarting = FALSE;
4598 }
4599
Bram Moolenaare3226be2005-12-18 22:10:00 +00004600 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4601 * around. */
4602 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004604 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004606 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004607 found_end = (compl_first_match != NULL
4608 && (compl_shown_match->cp_next == compl_first_match
4609 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004610 }
4611 else if (compl_shows_dir == BACKWARD
4612 && compl_shown_match->cp_prev != NULL)
4613 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004614 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004615 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004616 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004619 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004620 if (!allow_get_expansion)
4621 {
4622 if (advance)
4623 {
4624 if (compl_shows_dir == BACKWARD)
4625 compl_pending -= todo + 1;
4626 else
4627 compl_pending += todo + 1;
4628 }
4629 return -1;
4630 }
4631
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004632 if (advance)
4633 {
4634 if (compl_shows_dir == BACKWARD)
4635 --compl_pending;
4636 else
4637 ++compl_pending;
4638 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004639
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004640 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004641 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004642
4643 /* handle any pending completions */
4644 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004645 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004646 {
4647 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4648 {
4649 compl_shown_match = compl_shown_match->cp_next;
4650 --compl_pending;
4651 }
4652 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4653 {
4654 compl_shown_match = compl_shown_match->cp_prev;
4655 ++compl_pending;
4656 }
4657 else
4658 break;
4659 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004660 found_end = FALSE;
4661 }
4662 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4663 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004664 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004665 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004666 ++todo;
4667 else
4668 /* Remember a matching item. */
4669 found_compl = compl_shown_match;
4670
4671 /* Stop at the end of the list when we found a usable match. */
4672 if (found_end)
4673 {
4674 if (found_compl != NULL)
4675 {
4676 compl_shown_match = found_compl;
4677 break;
4678 }
4679 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004683 /* Insert the text of the new completion, or the compl_leader. */
4684 if (insert_match)
4685 {
4686 if (!compl_get_longest || compl_used_match)
4687 ins_compl_insert();
4688 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004689 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004690 }
4691 else
4692 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
4694 if (!allow_get_expansion)
4695 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004696 /* may undisplay the popup menu first */
4697 ins_compl_upd_pum();
4698
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004699 /* redraw to show the user what was inserted */
4700 update_screen(0);
4701
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004702 /* display the updated popup menu */
4703 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004704#ifdef FEAT_GUI
4705 if (gui.in_use)
4706 {
4707 /* Show the cursor after the match, not after the redrawn text. */
4708 setcursor();
4709 out_flush();
4710 gui_update_cursor(FALSE, FALSE);
4711 }
4712#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 /* Delete old text to be replaced, since we're still searching and
4715 * don't want to match ourselves! */
4716 ins_compl_delete();
4717 }
4718
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004719 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004720 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004721 compl_enter_selects = !insert_match && compl_match_array != NULL;
4722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 /*
4724 * Show the file name for the match (if any)
4725 * Truncate the file name to avoid a wait for return.
4726 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004727 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
4729 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004730 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (i <= 0)
4732 i = 0;
4733 else
4734 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004735 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 msg(IObuff);
4737 redraw_cmdline = FALSE; /* don't overwrite! */
4738 }
4739
4740 return num_matches;
4741}
4742
4743/*
4744 * Call this while finding completions, to check whether the user has hit a key
4745 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004746 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004748 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 */
4750 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004751ins_compl_check_keys(frequency)
4752 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753{
4754 static int count = 0;
4755
4756 int c;
4757
4758 /* Don't check when reading keys from a script. That would break the test
4759 * scripts */
4760 if (using_script())
4761 return;
4762
4763 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004764 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 return;
4766 count = 0;
4767
Bram Moolenaara260a972006-06-23 19:36:29 +00004768 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4769 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (c != NUL)
4772 {
4773 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4774 {
4775 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004776 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004777 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4778 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004780 else
4781 {
4782 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004783 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004784 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004785 if (c != K_IGNORE)
4786 {
4787 /* Don't interrupt completion when the character wasn't typed,
4788 * e.g., when doing @q to replay keys. */
4789 if (c != Ctrl_R && KeyTyped)
4790 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004791
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004792 vungetc(c);
4793 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004796 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004797 {
4798 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4799
4800 compl_pending = 0;
4801 (void)ins_compl_next(FALSE, todo, TRUE);
4802 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004803}
4804
4805/*
4806 * Decide the direction of Insert mode complete from the key typed.
4807 * Returns BACKWARD or FORWARD.
4808 */
4809 static int
4810ins_compl_key2dir(c)
4811 int c;
4812{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004813 if (c == Ctrl_P || c == Ctrl_L
4814 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4815 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004816 return BACKWARD;
4817 return FORWARD;
4818}
4819
4820/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004821 * Return TRUE for keys that are used for completion only when the popup menu
4822 * is visible.
4823 */
4824 static int
4825ins_compl_pum_key(c)
4826 int c;
4827{
4828 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004829 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4830 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004831}
4832
4833/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004834 * Decide the number of completions to move forward.
4835 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4836 */
4837 static int
4838ins_compl_key2count(c)
4839 int c;
4840{
4841 int h;
4842
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004843 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004844 {
4845 h = pum_get_height();
4846 if (h > 3)
4847 h -= 2; /* keep some context */
4848 return h;
4849 }
4850 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851}
4852
4853/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004854 * Return TRUE if completion with "c" should insert the match, FALSE if only
4855 * to change the currently selected completion.
4856 */
4857 static int
4858ins_compl_use_match(c)
4859 int c;
4860{
4861 switch (c)
4862 {
4863 case K_UP:
4864 case K_DOWN:
4865 case K_PAGEDOWN:
4866 case K_KPAGEDOWN:
4867 case K_S_DOWN:
4868 case K_PAGEUP:
4869 case K_KPAGEUP:
4870 case K_S_UP:
4871 return FALSE;
4872 }
4873 return TRUE;
4874}
4875
4876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 * Do Insert mode completion.
4878 * Called when character "c" was typed, which has a meaning for completion.
4879 * Returns OK if completion was done, FAIL if something failed (out of mem).
4880 */
4881 static int
4882ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004883 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004885 char_u *line;
4886 int startcol = 0; /* column where searched text starts */
4887 colnr_T curs_col; /* cursor column */
4888 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004889 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
Bram Moolenaare3226be2005-12-18 22:10:00 +00004891 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 /* First time we hit ^N or ^P (in a row, I mean) */
4895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 did_ai = FALSE;
4897#ifdef FEAT_SMARTINDENT
4898 did_si = FALSE;
4899 can_si = FALSE;
4900 can_si_back = FALSE;
4901#endif
4902 if (stop_arrow() == FAIL)
4903 return FAIL;
4904
4905 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004907 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004909 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004910 * "compl_startpos" to the cursor as a pattern to add a new word
4911 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004912 * "compl_startpos" is not in the same line as the cursor then fix it
4913 * (the line has been split because it was longer than 'tw'). if SOL
4914 * is set then skip the previous pattern, a word at the beginning of
4915 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004916 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4917 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
4919 /*
4920 * it is a continued search
4921 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004922 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4924 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4925 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004926 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004928 /* line (probably) wrapped, set compl_startpos to the
4929 * first non_blank in the line, if it is not a wordchar
4930 * include it to get a better pattern, but then we don't
4931 * want the "\\<" prefix, check it bellow */
4932 compl_col = (colnr_T)(skipwhite(line) - line);
4933 compl_startpos.col = compl_col;
4934 compl_startpos.lnum = curwin->w_cursor.lnum;
4935 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 else
4938 {
4939 /* S_IPOS was set when we inserted a word that was at the
4940 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004941 * mode but first we need to redefine compl_startpos */
4942 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004944 compl_cont_status |= CONT_SOL;
4945 compl_startpos.col = (colnr_T)(skipwhite(
4946 line + compl_length
4947 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004949 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004951 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004952 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004953 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004955 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004957 compl_cont_status &= ~CONT_SOL;
4958 compl_length = (IOSIZE - MIN_SPACE);
4959 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004961 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4962 if (compl_length < 1)
4963 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004966 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004968 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
4970 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004971 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004973 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004975 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004977 compl_cont_status = 0;
4978 compl_cont_status |= CONT_N_ADDS;
4979 compl_startpos = curwin->w_cursor;
4980 startcol = (int)curs_col;
4981 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983
4984 /* Work out completion pattern and original text -- webb */
4985 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4986 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004987 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4989 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004990 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004992 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004994 compl_col += ++startcol;
4995 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004998 compl_pattern = str_foldcase(line + compl_col,
4999 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005001 compl_pattern = vim_strnsave(line + compl_col,
5002 compl_length);
5003 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 return FAIL;
5005 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005006 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
5008 char_u *prefix = (char_u *)"\\<";
5009
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005010 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005011 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005012 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005013 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005015 if (!vim_iswordp(line + compl_col)
5016 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 && (
5018#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005019 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005021 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022#endif
5023 )))
5024 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005025 STRCPY((char *)compl_pattern, prefix);
5026 (void)quote_meta(compl_pattern + STRLEN(prefix),
5027 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005029 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005031 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005033 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034#endif
5035 )
5036 {
5037 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5039 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 compl_col += curs_col;
5042 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 else
5045 {
5046#ifdef FEAT_MBYTE
5047 /* Search the point of change class of multibyte character
5048 * or not a word single byte character backward. */
5049 if (has_mbyte)
5050 {
5051 int base_class;
5052 int head_off;
5053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005054 startcol -= (*mb_head_off)(line, line + startcol);
5055 base_class = mb_get_class(line + startcol);
5056 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005058 head_off = (*mb_head_off)(line, line + startcol);
5059 if (base_class != mb_get_class(line + startcol
5060 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 }
5065 else
5066#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005069 compl_col += ++startcol;
5070 compl_length = (int)curs_col - startcol;
5071 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 /* Only match word with at least two chars -- webb
5074 * there's no need to call quote_meta,
5075 * alloc(7) is enough -- Acevedo
5076 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005077 compl_pattern = alloc(7);
5078 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005080 STRCPY((char *)compl_pattern, "\\<");
5081 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5082 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084 else
5085 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005087 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005090 STRCPY((char *)compl_pattern, "\\<");
5091 (void)quote_meta(compl_pattern + 2, line + compl_col,
5092 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 }
5095 }
5096 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5097 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005098 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 compl_length = (int)curs_col - (int)compl_col;
5100 if (compl_length < 0) /* cursor in indent: empty pattern */
5101 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 compl_pattern = str_foldcase(line + compl_col, compl_length,
5104 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5107 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 return FAIL;
5109 }
5110 else if (ctrl_x_mode == CTRL_X_FILES)
5111 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 compl_col += ++startcol;
5115 compl_length = (int)curs_col - startcol;
5116 compl_pattern = addstar(line + compl_col, compl_length,
5117 EXPAND_FILES);
5118 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 return FAIL;
5120 }
5121 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5122 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 compl_pattern = vim_strnsave(line, curs_col);
5124 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 set_cmd_context(&compl_xp, compl_pattern,
5127 (int)STRLEN(compl_pattern), curs_col);
5128 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5129 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005130 /* No completion possible, use an empty pattern to get a
5131 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005132 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005133 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005134 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5135 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005137 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005138 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005139#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005140 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005141 * Call user defined function 'completefunc' with "a:findstart"
5142 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005143 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005144 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005145 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005146 char_u *funcname;
5147 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005148 win_T *curwin_save;
5149 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005150
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005151 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005152 * string */
5153 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5154 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5155 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005156 {
5157 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5158 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005159 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005160 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005161
5162 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005163 args[1] = NULL;
5164 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005165 curwin_save = curwin;
5166 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005167 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005168 if (curwin_save != curwin || curbuf_save != curbuf)
5169 {
5170 EMSG(_(e_complwin));
5171 return FAIL;
5172 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005173 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005174 check_cursor();
5175 if (!equalpos(curwin->w_cursor, pos))
5176 {
5177 EMSG(_(e_compldel));
5178 return FAIL;
5179 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005180
Bram Moolenaar82139082011-09-14 16:52:09 +02005181 /*
5182 * Reset extended parameters of completion, when start new
5183 * completion.
5184 */
5185 compl_opt_refresh_always = FALSE;
5186
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005187 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005188 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005189 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005190 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005191 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 /* Setup variables for completion. Need to obtain "line" again,
5194 * it may have become invalid. */
5195 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005196 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5198 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005199#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 return FAIL;
5201 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005202 else if (ctrl_x_mode == CTRL_X_SPELL)
5203 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005204#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005205 if (spell_bad_len > 0)
5206 compl_col = curs_col - spell_bad_len;
5207 else
5208 compl_col = spell_word_start(startcol);
5209 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005210 {
5211 compl_length = 0;
5212 compl_col = curs_col;
5213 }
5214 else
5215 {
5216 spell_expand_check_cap(compl_col);
5217 compl_length = (int)curs_col - compl_col;
5218 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005219 /* Need to obtain "line" again, it may have become invalid. */
5220 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005221 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5222 if (compl_pattern == NULL)
5223#endif
5224 return FAIL;
5225 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 else
5227 {
5228 EMSG2(_(e_intern2), "ins_complete()");
5229 return FAIL;
5230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 edit_submode_pre = (char_u *)_(" Adding");
5235 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5236 {
5237 /* Insert a new line, keep indentation but ignore 'comments' */
5238#ifdef FEAT_COMMENTS
5239 char_u *old = curbuf->b_p_com;
5240
5241 curbuf->b_p_com = (char_u *)"";
5242#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005243 compl_startpos.lnum = curwin->w_cursor.lnum;
5244 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 ins_eol('\r');
5246#ifdef FEAT_COMMENTS
5247 curbuf->b_p_com = old;
5248#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 compl_length = 0;
5250 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252 }
5253 else
5254 {
5255 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005259 if (compl_cont_status & CONT_LOCAL)
5260 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 else
5262 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5263
Bram Moolenaar62951b12011-09-21 18:23:05 +02005264 /* If any of the original typed text has been changed we need to fix
5265 * the redo buffer. */
5266 ins_compl_fixRedoBufForLeader(NULL);
5267
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005268 /* Always add completion for the original text. */
5269 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5271 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005272 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005274 vim_free(compl_pattern);
5275 compl_pattern = NULL;
5276 vim_free(compl_orig_text);
5277 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 return FAIL;
5279 }
5280
5281 /* showmode might reset the internal line pointers, so it must
5282 * be called before line = ml_get(), or when this address is no
5283 * longer needed. -- Acevedo.
5284 */
5285 edit_submode_extra = (char_u *)_("-- Searching...");
5286 edit_submode_highl = HLF_COUNT;
5287 showmode();
5288 edit_submode_extra = NULL;
5289 out_flush();
5290 }
5291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 compl_shown_match = compl_curr_match;
5293 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005296 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005298 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005299 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005301 /* may undisplay the popup menu */
5302 ins_compl_upd_pum();
5303
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 if (n > 1) /* all matches have been found */
5305 compl_matches = n;
5306 compl_curr_match = compl_shown_match;
5307 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
Bram Moolenaard68071d2006-05-02 22:08:30 +00005309 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5310 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (got_int && !global_busy)
5312 {
5313 (void)vgetc();
5314 got_int = FALSE;
5315 }
5316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005318 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5321 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5323 edit_submode_highl = HLF_E;
5324 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5325 * because we couldn't expand anything at first place, but if we used
5326 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5327 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 if ( compl_length > 1
5329 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 || (ctrl_x_mode != 0
5331 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5332 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
Bram Moolenaar572cb562005-08-05 21:35:02 +00005336 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 if (edit_submode_extra == NULL)
5342 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005343 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
5345 edit_submode_extra = (char_u *)_("Back at original");
5346 edit_submode_highl = HLF_W;
5347 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 edit_submode_extra = (char_u *)_("Word from other line");
5351 edit_submode_highl = HLF_COUNT;
5352 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005353 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 edit_submode_extra = (char_u *)_("The only match");
5356 edit_submode_highl = HLF_COUNT;
5357 }
5358 else
5359 {
5360 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005361 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005363 int number = 0;
5364 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005366 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
5368 /* search backwards for the first valid (!= -1) number.
5369 * This should normally succeed already at the first loop
5370 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005371 for (match = compl_curr_match->cp_prev; match != NULL
5372 && match != compl_first_match;
5373 match = match->cp_prev)
5374 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005376 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 break;
5378 }
5379 if (match != NULL)
5380 /* go up and assign all numbers which are not assigned
5381 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005382 for (match = match->cp_next;
5383 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005384 match = match->cp_next)
5385 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387 else /* BACKWARD */
5388 {
5389 /* search forwards (upwards) for the first valid (!= -1)
5390 * number. This should normally succeed already at the
5391 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005392 for (match = compl_curr_match->cp_next; match != NULL
5393 && match != compl_first_match;
5394 match = match->cp_next)
5395 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005397 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 break;
5399 }
5400 if (match != NULL)
5401 /* go down and assign all numbers which are not
5402 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005403 for (match = match->cp_prev; match
5404 && match->cp_number == -1;
5405 match = match->cp_prev)
5406 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408 }
5409
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005410 /* The match should always have a sequence number now, this is
5411 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005412 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005414 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5415 * Translations may need more than twice that. */
5416 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005419 vim_snprintf((char *)match_ref, sizeof(match_ref),
5420 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005421 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005423 vim_snprintf((char *)match_ref, sizeof(match_ref),
5424 _("match %d"),
5425 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 edit_submode_extra = match_ref;
5427 edit_submode_highl = HLF_R;
5428 if (dollar_vcol)
5429 curs_columns(FALSE);
5430 }
5431 }
5432 }
5433
5434 /* Show a message about what (completion) mode we're in. */
5435 showmode();
5436 if (edit_submode_extra != NULL)
5437 {
5438 if (!p_smd)
5439 msg_attr(edit_submode_extra,
5440 edit_submode_highl < HLF_COUNT
5441 ? hl_attr(edit_submode_highl) : 0);
5442 }
5443 else
5444 msg_clr_cmdline(); /* necessary for "noshowmode" */
5445
Bram Moolenaard68071d2006-05-02 22:08:30 +00005446 /* Show the popup menu, unless we got interrupted. */
5447 if (!compl_interrupted)
5448 {
5449 /* RedrawingDisabled may be set when invoked through complete(). */
5450 n = RedrawingDisabled;
5451 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005452
5453 /* If the cursor moved we need to remove the pum first. */
5454 setcursor();
5455 if (save_w_wrow != curwin->w_wrow)
5456 ins_compl_del_pum();
5457
Bram Moolenaard68071d2006-05-02 22:08:30 +00005458 ins_compl_show_pum();
5459 setcursor();
5460 RedrawingDisabled = n;
5461 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005462 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005463 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return OK;
5466}
5467
5468/*
5469 * Looks in the first "len" chars. of "src" for search-metachars.
5470 * If dest is not NULL the chars. are copied there quoting (with
5471 * a backslash) the metachars, and dest would be NUL terminated.
5472 * Returns the length (needed) of dest
5473 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005474 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475quote_meta(dest, src, len)
5476 char_u *dest;
5477 char_u *src;
5478 int len;
5479{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005480 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005482 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 switch (*src)
5485 {
5486 case '.':
5487 case '*':
5488 case '[':
5489 if (ctrl_x_mode == CTRL_X_DICTIONARY
5490 || ctrl_x_mode == CTRL_X_THESAURUS)
5491 break;
5492 case '~':
5493 if (!p_magic) /* quote these only if magic is set */
5494 break;
5495 case '\\':
5496 if (ctrl_x_mode == CTRL_X_DICTIONARY
5497 || ctrl_x_mode == CTRL_X_THESAURUS)
5498 break;
5499 case '^': /* currently it's not needed. */
5500 case '$':
5501 m++;
5502 if (dest != NULL)
5503 *dest++ = '\\';
5504 break;
5505 }
5506 if (dest != NULL)
5507 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005508# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 /* Copy remaining bytes of a multibyte character. */
5510 if (has_mbyte)
5511 {
5512 int i, mb_len;
5513
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005514 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (mb_len > 0 && len >= mb_len)
5516 for (i = 0; i < mb_len; ++i)
5517 {
5518 --len;
5519 ++src;
5520 if (dest != NULL)
5521 *dest++ = *src;
5522 }
5523 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 }
5526 if (dest != NULL)
5527 *dest = NUL;
5528
5529 return m;
5530}
5531#endif /* FEAT_INS_EXPAND */
5532
5533/*
5534 * Next character is interpreted literally.
5535 * A one, two or three digit decimal number is interpreted as its byte value.
5536 * If one or two digits are entered, the next character is given to vungetc().
5537 * For Unicode a character > 255 may be returned.
5538 */
5539 int
5540get_literal()
5541{
5542 int cc;
5543 int nc;
5544 int i;
5545 int hex = FALSE;
5546 int octal = FALSE;
5547#ifdef FEAT_MBYTE
5548 int unicode = 0;
5549#endif
5550
5551 if (got_int)
5552 return Ctrl_C;
5553
5554#ifdef FEAT_GUI
5555 /*
5556 * In GUI there is no point inserting the internal code for a special key.
5557 * It is more useful to insert the string "<KEY>" instead. This would
5558 * probably be useful in a text window too, but it would not be
5559 * vi-compatible (maybe there should be an option for it?) -- webb
5560 */
5561 if (gui.in_use)
5562 ++allow_keys;
5563#endif
5564#ifdef USE_ON_FLY_SCROLL
5565 dont_scroll = TRUE; /* disallow scrolling here */
5566#endif
5567 ++no_mapping; /* don't map the next key hits */
5568 cc = 0;
5569 i = 0;
5570 for (;;)
5571 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005572 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#ifdef FEAT_CMDL_INFO
5574 if (!(State & CMDLINE)
5575# ifdef FEAT_MBYTE
5576 && MB_BYTE2LEN_CHECK(nc) == 1
5577# endif
5578 )
5579 add_to_showcmd(nc);
5580#endif
5581 if (nc == 'x' || nc == 'X')
5582 hex = TRUE;
5583 else if (nc == 'o' || nc == 'O')
5584 octal = TRUE;
5585#ifdef FEAT_MBYTE
5586 else if (nc == 'u' || nc == 'U')
5587 unicode = nc;
5588#endif
5589 else
5590 {
5591 if (hex
5592#ifdef FEAT_MBYTE
5593 || unicode != 0
5594#endif
5595 )
5596 {
5597 if (!vim_isxdigit(nc))
5598 break;
5599 cc = cc * 16 + hex2nr(nc);
5600 }
5601 else if (octal)
5602 {
5603 if (nc < '0' || nc > '7')
5604 break;
5605 cc = cc * 8 + nc - '0';
5606 }
5607 else
5608 {
5609 if (!VIM_ISDIGIT(nc))
5610 break;
5611 cc = cc * 10 + nc - '0';
5612 }
5613
5614 ++i;
5615 }
5616
5617 if (cc > 255
5618#ifdef FEAT_MBYTE
5619 && unicode == 0
5620#endif
5621 )
5622 cc = 255; /* limit range to 0-255 */
5623 nc = 0;
5624
5625 if (hex) /* hex: up to two chars */
5626 {
5627 if (i >= 2)
5628 break;
5629 }
5630#ifdef FEAT_MBYTE
5631 else if (unicode) /* Unicode: up to four or eight chars */
5632 {
5633 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5634 break;
5635 }
5636#endif
5637 else if (i >= 3) /* decimal or octal: up to three chars */
5638 break;
5639 }
5640 if (i == 0) /* no number entered */
5641 {
5642 if (nc == K_ZERO) /* NUL is stored as NL */
5643 {
5644 cc = '\n';
5645 nc = 0;
5646 }
5647 else
5648 {
5649 cc = nc;
5650 nc = 0;
5651 }
5652 }
5653
5654 if (cc == 0) /* NUL is stored as NL */
5655 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005656#ifdef FEAT_MBYTE
5657 if (enc_dbcs && (cc & 0xff) == 0)
5658 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5659 second byte will cause trouble! */
5660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 --no_mapping;
5663#ifdef FEAT_GUI
5664 if (gui.in_use)
5665 --allow_keys;
5666#endif
5667 if (nc)
5668 vungetc(nc);
5669 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5670 return cc;
5671}
5672
5673/*
5674 * Insert character, taking care of special keys and mod_mask
5675 */
5676 static void
5677insert_special(c, allow_modmask, ctrlv)
5678 int c;
5679 int allow_modmask;
5680 int ctrlv; /* c was typed after CTRL-V */
5681{
5682 char_u *p;
5683 int len;
5684
5685 /*
5686 * Special function key, translate into "<Key>". Up to the last '>' is
5687 * inserted with ins_str(), so as not to replace characters in replace
5688 * mode.
5689 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5690 * unless 'allow_modmask' is TRUE.
5691 */
5692#ifdef MACOS
5693 /* Command-key never produces a normal key */
5694 if (mod_mask & MOD_MASK_CMD)
5695 allow_modmask = TRUE;
5696#endif
5697 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5698 {
5699 p = get_special_key_name(c, mod_mask);
5700 len = (int)STRLEN(p);
5701 c = p[len - 1];
5702 if (len > 2)
5703 {
5704 if (stop_arrow() == FAIL)
5705 return;
5706 p[len - 1] = NUL;
5707 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005708 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 ctrlv = FALSE;
5710 }
5711 }
5712 if (stop_arrow() == OK)
5713 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5714}
5715
5716/*
5717 * Special characters in this context are those that need processing other
5718 * than the simple insertion that can be performed here. This includes ESC
5719 * which terminates the insert, and CR/NL which need special processing to
5720 * open up a new line. This routine tries to optimize insertions performed by
5721 * the "redo", "undo" or "put" commands, so it needs to know when it should
5722 * stop and defer processing to the "normal" mechanism.
5723 * '0' and '^' are special, because they can be followed by CTRL-D.
5724 */
5725#ifdef EBCDIC
5726# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5727#else
5728# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5729#endif
5730
5731#ifdef FEAT_MBYTE
5732# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5733#else
5734# define WHITECHAR(cc) vim_iswhite(cc)
5735#endif
5736
5737 void
5738insertchar(c, flags, second_indent)
5739 int c; /* character to insert or NUL */
5740 int flags; /* INSCHAR_FORMAT, etc. */
5741 int second_indent; /* indent for second line if >= 0 */
5742{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 int textwidth;
5744#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
5749 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5750 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
5752 /*
5753 * Try to break the line in two or more pieces when:
5754 * - Always do this if we have been called to do formatting only.
5755 * - Always do this when 'formatoptions' has the 'a' flag and the line
5756 * ends in white space.
5757 * - Otherwise:
5758 * - Don't do this if inserting a blank
5759 * - Don't do this if an existing character is being replaced, unless
5760 * we're in VREPLACE mode.
5761 * - Do this if the cursor is not on the line where insert started
5762 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5763 * before the insert.
5764 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5765 * before 'textwidth'
5766 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005767 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 && ((flags & INSCHAR_FORMAT)
5769 || (!vim_iswhite(c)
5770 && !((State & REPLACE_FLAG)
5771#ifdef FEAT_VREPLACE
5772 && !(State & VREPLACE_FLAG)
5773#endif
5774 && *ml_get_cursor() != NUL)
5775 && (curwin->w_cursor.lnum != Insstart.lnum
5776 || ((!has_format_option(FO_INS_LONG)
5777 || Insstart_textlen <= (colnr_T)textwidth)
5778 && (!fo_ins_blank
5779 || Insstart_blank_vcol <= (colnr_T)textwidth
5780 ))))))
5781 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005782 /* Format with 'formatexpr' when it's set. Use internal formatting
5783 * when 'formatexpr' isn't set or it returns non-zero. */
5784#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005785 int do_internal = TRUE;
5786
Bram Moolenaar81a82092008-03-12 16:27:00 +00005787 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005788 {
5789 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5790 /* It may be required to save for undo again, e.g. when setline()
5791 * was called. */
5792 ins_need_undo = TRUE;
5793 }
5794 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005796 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (c == NUL) /* only formatting was wanted */
5800 return;
5801
5802#ifdef FEAT_COMMENTS
5803 /* Check whether this character should end a comment. */
5804 if (did_ai && (int)c == end_comment_pending)
5805 {
5806 char_u *line;
5807 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5808 int middle_len, end_len;
5809 int i;
5810
5811 /*
5812 * Need to remove existing (middle) comment leader and insert end
5813 * comment leader. First, check what comment leader we can find.
5814 */
5815 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5816 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5817 {
5818 /* Skip middle-comment string */
5819 while (*p && p[-1] != ':') /* find end of middle flags */
5820 ++p;
5821 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5822 /* Don't count trailing white space for middle_len */
5823 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5824 --middle_len;
5825
5826 /* Find the end-comment string */
5827 while (*p && p[-1] != ':') /* find end of end flags */
5828 ++p;
5829 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5830
5831 /* Skip white space before the cursor */
5832 i = curwin->w_cursor.col;
5833 while (--i >= 0 && vim_iswhite(line[i]))
5834 ;
5835 i++;
5836
5837 /* Skip to before the middle leader */
5838 i -= middle_len;
5839
5840 /* Check some expected things before we go on */
5841 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5842 {
5843 /* Backspace over all the stuff we want to replace */
5844 backspace_until_column(i);
5845
5846 /*
5847 * Insert the end-comment string, except for the last
5848 * character, which will get inserted as normal later.
5849 */
5850 ins_bytes_len(lead_end, end_len - 1);
5851 }
5852 }
5853 }
5854 end_comment_pending = NUL;
5855#endif
5856
5857 did_ai = FALSE;
5858#ifdef FEAT_SMARTINDENT
5859 did_si = FALSE;
5860 can_si = FALSE;
5861 can_si_back = FALSE;
5862#endif
5863
5864 /*
5865 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5866 * This speeds up normal text input considerably.
5867 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5868 * need to re-indent at a ':', or any other character (but not what
5869 * 'paste' is set)..
5870 */
5871#ifdef USE_ON_FLY_SCROLL
5872 dont_scroll = FALSE; /* allow scrolling here */
5873#endif
5874
5875 if ( !ISSPECIAL(c)
5876#ifdef FEAT_MBYTE
5877 && (!has_mbyte || (*mb_char2len)(c) == 1)
5878#endif
5879 && vpeekc() != NUL
5880 && !(State & REPLACE_FLAG)
5881#ifdef FEAT_CINDENT
5882 && !cindent_on()
5883#endif
5884#ifdef FEAT_RIGHTLEFT
5885 && !p_ri
5886#endif
5887 )
5888 {
5889#define INPUT_BUFLEN 100
5890 char_u buf[INPUT_BUFLEN + 1];
5891 int i;
5892 colnr_T virtcol = 0;
5893
5894 buf[0] = c;
5895 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005896 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 virtcol = get_nolist_virtcol();
5898 /*
5899 * Stop the string when:
5900 * - no more chars available
5901 * - finding a special character (command key)
5902 * - buffer is full
5903 * - running into the 'textwidth' boundary
5904 * - need to check for abbreviation: A non-word char after a word-char
5905 */
5906 while ( (c = vpeekc()) != NUL
5907 && !ISSPECIAL(c)
5908#ifdef FEAT_MBYTE
5909 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5910#endif
5911 && i < INPUT_BUFLEN
5912 && (textwidth == 0
5913 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5914 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5915 {
5916#ifdef FEAT_RIGHTLEFT
5917 c = vgetc();
5918 if (p_hkmap && KeyTyped)
5919 c = hkmap(c); /* Hebrew mode mapping */
5920# ifdef FEAT_FKMAP
5921 if (p_fkmap && KeyTyped)
5922 c = fkmap(c); /* Farsi mode mapping */
5923# endif
5924 buf[i++] = c;
5925#else
5926 buf[i++] = vgetc();
5927#endif
5928 }
5929
5930#ifdef FEAT_DIGRAPHS
5931 do_digraph(-1); /* clear digraphs */
5932 do_digraph(buf[i-1]); /* may be the start of a digraph */
5933#endif
5934 buf[i] = NUL;
5935 ins_str(buf);
5936 if (flags & INSCHAR_CTRLV)
5937 {
5938 redo_literal(*buf);
5939 i = 1;
5940 }
5941 else
5942 i = 0;
5943 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005944 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946 else
5947 {
5948#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005949 int cc;
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5952 {
5953 char_u buf[MB_MAXBYTES + 1];
5954
5955 (*mb_char2bytes)(c, buf);
5956 buf[cc] = NUL;
5957 ins_char_bytes(buf, cc);
5958 AppendCharToRedobuff(c);
5959 }
5960 else
5961#endif
5962 {
5963 ins_char(c);
5964 if (flags & INSCHAR_CTRLV)
5965 redo_literal(c);
5966 else
5967 AppendCharToRedobuff(c);
5968 }
5969 }
5970}
5971
5972/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005973 * Format text at the current insert position.
5974 */
5975 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005976internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005977 int textwidth;
5978 int second_indent;
5979 int flags;
5980 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005981 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982{
5983 int cc;
5984 int save_char = NUL;
5985 int haveto_redraw = FALSE;
5986 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5987#ifdef FEAT_MBYTE
5988 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5989#endif
5990 int fo_white_par = has_format_option(FO_WHITE_PAR);
5991 int first_line = TRUE;
5992#ifdef FEAT_COMMENTS
5993 colnr_T leader_len;
5994 int no_leader = FALSE;
5995 int do_comments = (flags & INSCHAR_DO_COM);
5996#endif
5997
5998 /*
5999 * When 'ai' is off we don't want a space under the cursor to be
6000 * deleted. Replace it with an 'x' temporarily.
6001 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006002 if (!curbuf->b_p_ai
6003#ifdef FEAT_VREPLACE
6004 && !(State & VREPLACE_FLAG)
6005#endif
6006 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006007 {
6008 cc = gchar_cursor();
6009 if (vim_iswhite(cc))
6010 {
6011 save_char = cc;
6012 pchar_cursor('x');
6013 }
6014 }
6015
6016 /*
6017 * Repeat breaking lines, until the current line is not too long.
6018 */
6019 while (!got_int)
6020 {
6021 int startcol; /* Cursor column at entry */
6022 int wantcol; /* column at textwidth border */
6023 int foundcol; /* column for start of spaces */
6024 int end_foundcol = 0; /* column for start of word */
6025 colnr_T len;
6026 colnr_T virtcol;
6027#ifdef FEAT_VREPLACE
6028 int orig_col = 0;
6029 char_u *saved_text = NULL;
6030#endif
6031 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006032 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006033
Bram Moolenaar97b98102009-11-17 16:41:01 +00006034 virtcol = get_nolist_virtcol()
6035 + char2cells(c != NUL ? c : gchar_cursor());
6036 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006037 break;
6038
6039#ifdef FEAT_COMMENTS
6040 if (no_leader)
6041 do_comments = FALSE;
6042 else if (!(flags & INSCHAR_FORMAT)
6043 && has_format_option(FO_WRAP_COMS))
6044 do_comments = TRUE;
6045
6046 /* Don't break until after the comment leader */
6047 if (do_comments)
6048 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
6049 else
6050 leader_len = 0;
6051
6052 /* If the line doesn't start with a comment leader, then don't
6053 * start one in a following broken line. Avoids that a %word
6054 * moved to the start of the next line causes all following lines
6055 * to start with %. */
6056 if (leader_len == 0)
6057 no_leader = TRUE;
6058#endif
6059 if (!(flags & INSCHAR_FORMAT)
6060#ifdef FEAT_COMMENTS
6061 && leader_len == 0
6062#endif
6063 && !has_format_option(FO_WRAP))
6064
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006066 if ((startcol = curwin->w_cursor.col) == 0)
6067 break;
6068
6069 /* find column of textwidth border */
6070 coladvance((colnr_T)textwidth);
6071 wantcol = curwin->w_cursor.col;
6072
Bram Moolenaar97b98102009-11-17 16:41:01 +00006073 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006074 foundcol = 0;
6075
6076 /*
6077 * Find position to break at.
6078 * Stop at first entered white when 'formatoptions' has 'v'
6079 */
6080 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006081 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006082 || curwin->w_cursor.lnum != Insstart.lnum
6083 || curwin->w_cursor.col >= Insstart.col)
6084 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006085 if (curwin->w_cursor.col == startcol && c != NUL)
6086 cc = c;
6087 else
6088 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006089 if (WHITECHAR(cc))
6090 {
6091 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006092 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006093
6094 /* find start of sequence of blanks */
6095 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6096 {
6097 dec_cursor();
6098 cc = gchar_cursor();
6099 }
6100 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6101 break; /* only spaces in front of text */
6102#ifdef FEAT_COMMENTS
6103 /* Don't break until after the comment leader */
6104 if (curwin->w_cursor.col < leader_len)
6105 break;
6106#endif
6107 if (has_format_option(FO_ONE_LETTER))
6108 {
6109 /* do not break after one-letter words */
6110 if (curwin->w_cursor.col == 0)
6111 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006112#ifdef FEAT_COMMENTS
6113 /* do not break "#a b" when 'tw' is 2 */
6114 if (curwin->w_cursor.col <= leader_len)
6115 break;
6116#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006117 col = curwin->w_cursor.col;
6118 dec_cursor();
6119 cc = gchar_cursor();
6120
6121 if (WHITECHAR(cc))
6122 continue; /* one-letter, continue */
6123 curwin->w_cursor.col = col;
6124 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006125
6126 inc_cursor();
6127
6128 end_foundcol = end_col + 1;
6129 foundcol = curwin->w_cursor.col;
6130 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006131 break;
6132 }
6133#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006134 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006135 {
6136 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006137 if (curwin->w_cursor.col != startcol)
6138 {
6139#ifdef FEAT_COMMENTS
6140 /* Don't break until after the comment leader */
6141 if (curwin->w_cursor.col < leader_len)
6142 break;
6143#endif
6144 col = curwin->w_cursor.col;
6145 inc_cursor();
6146 /* Don't change end_foundcol if already set. */
6147 if (foundcol != curwin->w_cursor.col)
6148 {
6149 foundcol = curwin->w_cursor.col;
6150 end_foundcol = foundcol;
6151 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6152 break;
6153 }
6154 curwin->w_cursor.col = col;
6155 }
6156
6157 if (curwin->w_cursor.col == 0)
6158 break;
6159
6160 col = curwin->w_cursor.col;
6161
6162 dec_cursor();
6163 cc = gchar_cursor();
6164
6165 if (WHITECHAR(cc))
6166 continue; /* break with space */
6167#ifdef FEAT_COMMENTS
6168 /* Don't break until after the comment leader */
6169 if (curwin->w_cursor.col < leader_len)
6170 break;
6171#endif
6172
6173 curwin->w_cursor.col = col;
6174
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006176 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006177 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6178 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006179 }
6180#endif
6181 if (curwin->w_cursor.col == 0)
6182 break;
6183 dec_cursor();
6184 }
6185
6186 if (foundcol == 0) /* no spaces, cannot break line */
6187 {
6188 curwin->w_cursor.col = startcol;
6189 break;
6190 }
6191
6192 /* Going to break the line, remove any "$" now. */
6193 undisplay_dollar();
6194
6195 /*
6196 * Offset between cursor position and line break is used by replace
6197 * stack functions. VREPLACE does not use this, and backspaces
6198 * over the text instead.
6199 */
6200#ifdef FEAT_VREPLACE
6201 if (State & VREPLACE_FLAG)
6202 orig_col = startcol; /* Will start backspacing from here */
6203 else
6204#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006205 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206
6207 /*
6208 * adjust startcol for spaces that will be deleted and
6209 * characters that will remain on top line
6210 */
6211 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006212 while ((cc = gchar_cursor(), WHITECHAR(cc))
6213 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 inc_cursor();
6215 startcol -= curwin->w_cursor.col;
6216 if (startcol < 0)
6217 startcol = 0;
6218
6219#ifdef FEAT_VREPLACE
6220 if (State & VREPLACE_FLAG)
6221 {
6222 /*
6223 * In VREPLACE mode, we will backspace over the text to be
6224 * wrapped, so save a copy now to put on the next line.
6225 */
6226 saved_text = vim_strsave(ml_get_cursor());
6227 curwin->w_cursor.col = orig_col;
6228 if (saved_text == NULL)
6229 break; /* Can't do it, out of memory */
6230 saved_text[startcol] = NUL;
6231
6232 /* Backspace over characters that will move to the next line */
6233 if (!fo_white_par)
6234 backspace_until_column(foundcol);
6235 }
6236 else
6237#endif
6238 {
6239 /* put cursor after pos. to break line */
6240 if (!fo_white_par)
6241 curwin->w_cursor.col = foundcol;
6242 }
6243
6244 /*
6245 * Split the line just before the margin.
6246 * Only insert/delete lines, but don't really redraw the window.
6247 */
6248 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6249 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6250#ifdef FEAT_COMMENTS
6251 + (do_comments ? OPENLINE_DO_COM : 0)
6252#endif
6253 , old_indent);
6254 old_indent = 0;
6255
6256 replace_offset = 0;
6257 if (first_line)
6258 {
6259 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6260 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6261 if (second_indent >= 0)
6262 {
6263#ifdef FEAT_VREPLACE
6264 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006265 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266 else
6267#endif
6268 (void)set_indent(second_indent, SIN_CHANGED);
6269 }
6270 first_line = FALSE;
6271 }
6272
6273#ifdef FEAT_VREPLACE
6274 if (State & VREPLACE_FLAG)
6275 {
6276 /*
6277 * In VREPLACE mode we have backspaced over the text to be
6278 * moved, now we re-insert it into the new line.
6279 */
6280 ins_bytes(saved_text);
6281 vim_free(saved_text);
6282 }
6283 else
6284#endif
6285 {
6286 /*
6287 * Check if cursor is not past the NUL off the line, cindent
6288 * may have added or removed indent.
6289 */
6290 curwin->w_cursor.col += startcol;
6291 len = (colnr_T)STRLEN(ml_get_curline());
6292 if (curwin->w_cursor.col > len)
6293 curwin->w_cursor.col = len;
6294 }
6295
6296 haveto_redraw = TRUE;
6297#ifdef FEAT_CINDENT
6298 can_cindent = TRUE;
6299#endif
6300 /* moved the cursor, don't autoindent or cindent now */
6301 did_ai = FALSE;
6302#ifdef FEAT_SMARTINDENT
6303 did_si = FALSE;
6304 can_si = FALSE;
6305 can_si_back = FALSE;
6306#endif
6307 line_breakcheck();
6308 }
6309
6310 if (save_char != NUL) /* put back space after cursor */
6311 pchar_cursor(save_char);
6312
6313 if (!format_only && haveto_redraw)
6314 {
6315 update_topline();
6316 redraw_curbuf_later(VALID);
6317 }
6318}
6319
6320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * Called after inserting or deleting text: When 'formatoptions' includes the
6322 * 'a' flag format from the current line until the end of the paragraph.
6323 * Keep the cursor at the same position relative to the text.
6324 * The caller must have saved the cursor line for undo, following ones will be
6325 * saved here.
6326 */
6327 void
6328auto_format(trailblank, prev_line)
6329 int trailblank; /* when TRUE also format with trailing blank */
6330 int prev_line; /* may start in previous line */
6331{
6332 pos_T pos;
6333 colnr_T len;
6334 char_u *old;
6335 char_u *new, *pnew;
6336 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006337 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 if (!has_format_option(FO_AUTO))
6340 return;
6341
6342 pos = curwin->w_cursor;
6343 old = ml_get_curline();
6344
6345 /* may remove added space */
6346 check_auto_format(FALSE);
6347
6348 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6349 * user might insert normal text next. Also skip formatting when "1" is
6350 * in 'formatoptions' and there is a single character before the cursor.
6351 * Otherwise the line would be broken and when typing another non-white
6352 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006353 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (*old != NUL && !trailblank && wasatend)
6355 {
6356 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006357 cc = gchar_cursor();
6358 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6359 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006361 cc = gchar_cursor();
6362 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 {
6364 curwin->w_cursor = pos;
6365 return;
6366 }
6367 curwin->w_cursor = pos;
6368 }
6369
6370#ifdef FEAT_COMMENTS
6371 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6372 * comments. */
6373 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6374 && get_leader_len(old, NULL, FALSE) == 0)
6375 return;
6376#endif
6377
6378 /*
6379 * May start formatting in a previous line, so that after "x" a word is
6380 * moved to the previous line if it fits there now. Only when this is not
6381 * the start of a paragraph.
6382 */
6383 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6384 {
6385 --curwin->w_cursor.lnum;
6386 if (u_save_cursor() == FAIL)
6387 return;
6388 }
6389
6390 /*
6391 * Do the formatting and restore the cursor position. "saved_cursor" will
6392 * be adjusted for the text formatting.
6393 */
6394 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006395 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 curwin->w_cursor = saved_cursor;
6397 saved_cursor.lnum = 0;
6398
6399 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6400 {
6401 /* "cannot happen" */
6402 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6403 coladvance((colnr_T)MAXCOL);
6404 }
6405 else
6406 check_cursor_col();
6407
6408 /* Insert mode: If the cursor is now after the end of the line while it
6409 * previously wasn't, the line was broken. Because of the rule above we
6410 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6411 * formatted. */
6412 if (!wasatend && has_format_option(FO_WHITE_PAR))
6413 {
6414 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006415 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (curwin->w_cursor.col == len)
6417 {
6418 pnew = vim_strnsave(new, len + 2);
6419 pnew[len] = ' ';
6420 pnew[len + 1] = NUL;
6421 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6422 /* remove the space later */
6423 did_add_space = TRUE;
6424 }
6425 else
6426 /* may remove added space */
6427 check_auto_format(FALSE);
6428 }
6429
6430 check_cursor();
6431}
6432
6433/*
6434 * When an extra space was added to continue a paragraph for auto-formatting,
6435 * delete it now. The space must be under the cursor, just after the insert
6436 * position.
6437 */
6438 static void
6439check_auto_format(end_insert)
6440 int end_insert; /* TRUE when ending Insert mode */
6441{
6442 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006443 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
6445 if (did_add_space)
6446 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006447 cc = gchar_cursor();
6448 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 /* Somehow the space was removed already. */
6450 did_add_space = FALSE;
6451 else
6452 {
6453 if (!end_insert)
6454 {
6455 inc_cursor();
6456 c = gchar_cursor();
6457 dec_cursor();
6458 }
6459 if (c != NUL)
6460 {
6461 /* The space is no longer at the end of the line, delete it. */
6462 del_char(FALSE);
6463 did_add_space = FALSE;
6464 }
6465 }
6466 }
6467}
6468
6469/*
6470 * Find out textwidth to be used for formatting:
6471 * if 'textwidth' option is set, use it
6472 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6473 * if invalid value, use 0.
6474 * Set default to window width (maximum 79) for "gq" operator.
6475 */
6476 int
6477comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006478 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 int textwidth;
6481
6482 textwidth = curbuf->b_p_tw;
6483 if (textwidth == 0 && curbuf->b_p_wm)
6484 {
6485 /* The width is the window width minus 'wrapmargin' minus all the
6486 * things that add to the margin. */
6487 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6488#ifdef FEAT_CMDWIN
6489 if (cmdwin_type != 0)
6490 textwidth -= 1;
6491#endif
6492#ifdef FEAT_FOLDING
6493 textwidth -= curwin->w_p_fdc;
6494#endif
6495#ifdef FEAT_SIGNS
6496 if (curwin->w_buffer->b_signlist != NULL
6497# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006498 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499# endif
6500 )
6501 textwidth -= 1;
6502#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006503 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 textwidth -= 8;
6505 }
6506 if (textwidth < 0)
6507 textwidth = 0;
6508 if (ff && textwidth == 0)
6509 {
6510 textwidth = W_WIDTH(curwin) - 1;
6511 if (textwidth > 79)
6512 textwidth = 79;
6513 }
6514 return textwidth;
6515}
6516
6517/*
6518 * Put a character in the redo buffer, for when just after a CTRL-V.
6519 */
6520 static void
6521redo_literal(c)
6522 int c;
6523{
6524 char_u buf[10];
6525
6526 /* Only digits need special treatment. Translate them into a string of
6527 * three digits. */
6528 if (VIM_ISDIGIT(c))
6529 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006530 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 AppendToRedobuff(buf);
6532 }
6533 else
6534 AppendCharToRedobuff(c);
6535}
6536
6537/*
6538 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006539 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 */
6541 static void
6542start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006543 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 if (!arrow_used) /* something has been inserted */
6546 {
6547 AppendToRedobuff(ESC_STR);
6548 stop_insert(end_insert_pos, FALSE);
6549 arrow_used = TRUE; /* this means we stopped the current insert */
6550 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006551#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006552 check_spell_redraw();
6553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554}
6555
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006556#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006557/*
6558 * If we skipped highlighting word at cursor, do it now.
6559 * It may be skipped again, thus reset spell_redraw_lnum first.
6560 */
6561 static void
6562check_spell_redraw()
6563{
6564 if (spell_redraw_lnum != 0)
6565 {
6566 linenr_T lnum = spell_redraw_lnum;
6567
6568 spell_redraw_lnum = 0;
6569 redrawWinline(lnum, FALSE);
6570 }
6571}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006572
6573/*
6574 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6575 * spelled word, if there is one.
6576 */
6577 static void
6578spell_back_to_badword()
6579{
6580 pos_T tpos = curwin->w_cursor;
6581
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006582 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006583 if (curwin->w_cursor.col != tpos.col)
6584 start_arrow(&tpos);
6585}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006586#endif
6587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588/*
6589 * stop_arrow() is called before a change is made in insert mode.
6590 * If an arrow key has been used, start a new insertion.
6591 * Returns FAIL if undo is impossible, shouldn't insert then.
6592 */
6593 int
6594stop_arrow()
6595{
6596 if (arrow_used)
6597 {
6598 if (u_save_cursor() == OK)
6599 {
6600 arrow_used = FALSE;
6601 ins_need_undo = FALSE;
6602 }
6603 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006604 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 ai_col = 0;
6606#ifdef FEAT_VREPLACE
6607 if (State & VREPLACE_FLAG)
6608 {
6609 orig_line_count = curbuf->b_ml.ml_line_count;
6610 vr_lines_changed = 1;
6611 }
6612#endif
6613 ResetRedobuff();
6614 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006615 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
6617 else if (ins_need_undo)
6618 {
6619 if (u_save_cursor() == OK)
6620 ins_need_undo = FALSE;
6621 }
6622
6623#ifdef FEAT_FOLDING
6624 /* Always open fold at the cursor line when inserting something. */
6625 foldOpenCursor();
6626#endif
6627
6628 return (arrow_used || ins_need_undo ? FAIL : OK);
6629}
6630
6631/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006632 * Do a few things to stop inserting.
6633 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6634 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 */
6636 static void
6637stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006638 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006639 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006641 int cc;
6642 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 stop_redo_ins();
6645 replace_flush(); /* abandon replace stack */
6646
6647 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006648 * Save the inserted text for later redo with ^@ and CTRL-A.
6649 * Don't do it when "restart_edit" was set and nothing was inserted,
6650 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006652 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006653 if (did_restart_edit == 0 || (ptr != NULL
6654 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006655 {
6656 vim_free(last_insert);
6657 last_insert = ptr;
6658 last_insert_skip = new_insert_skip;
6659 }
6660 else
6661 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006663 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
6665 /* Auto-format now. It may seem strange to do this when stopping an
6666 * insertion (or moving the cursor), but it's required when appending
6667 * a line and having it end in a space. But only do it when something
6668 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006669 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006671 pos_T tpos = curwin->w_cursor;
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 /* When the cursor is at the end of the line after a space the
6674 * formatting will move it to the following word. Avoid that by
6675 * moving the cursor onto the space. */
6676 cc = 'x';
6677 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6678 {
6679 dec_cursor();
6680 cc = gchar_cursor();
6681 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006682 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 }
6684
6685 auto_format(TRUE, FALSE);
6686
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006687 if (vim_iswhite(cc))
6688 {
6689 if (gchar_cursor() != NUL)
6690 inc_cursor();
6691#ifdef FEAT_VIRTUALEDIT
6692 /* If the cursor is still at the same character, also keep
6693 * the "coladd". */
6694 if (gchar_cursor() == NUL
6695 && curwin->w_cursor.lnum == tpos.lnum
6696 && curwin->w_cursor.col == tpos.col)
6697 curwin->w_cursor.coladd = tpos.coladd;
6698#endif
6699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701
6702 /* If a space was inserted for auto-formatting, remove it now. */
6703 check_auto_format(TRUE);
6704
6705 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006706 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006707 * Do this when ESC was used or moving the cursor up/down.
6708 * Check for the old position still being valid, just in case the text
6709 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006710 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006711 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6712 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006714 pos_T tpos = curwin->w_cursor;
6715
6716 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006717 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006718 for (;;)
6719 {
6720 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6721 --curwin->w_cursor.col;
6722 cc = gchar_cursor();
6723 if (!vim_iswhite(cc))
6724 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006725 if (del_char(TRUE) == FAIL)
6726 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006727 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006728 if (curwin->w_cursor.lnum != tpos.lnum)
6729 curwin->w_cursor = tpos;
6730 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6732
6733#ifdef FEAT_VISUAL
6734 /* <C-S-Right> may have started Visual mode, adjust the position for
6735 * deleted characters. */
6736 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6737 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006738 int len = (int)STRLEN(ml_get_curline());
6739
6740 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006742 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743# ifdef FEAT_VIRTUALEDIT
6744 VIsual.coladd = 0;
6745# endif
6746 }
6747 }
6748#endif
6749 }
6750 }
6751 did_ai = FALSE;
6752#ifdef FEAT_SMARTINDENT
6753 did_si = FALSE;
6754 can_si = FALSE;
6755 can_si_back = FALSE;
6756#endif
6757
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006758 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6759 * now in a different buffer. */
6760 if (end_insert_pos != NULL)
6761 {
6762 curbuf->b_op_start = Insstart;
6763 curbuf->b_op_end = *end_insert_pos;
6764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765}
6766
6767/*
6768 * Set the last inserted text to a single character.
6769 * Used for the replace command.
6770 */
6771 void
6772set_last_insert(c)
6773 int c;
6774{
6775 char_u *s;
6776
6777 vim_free(last_insert);
6778#ifdef FEAT_MBYTE
6779 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6780#else
6781 last_insert = alloc(6);
6782#endif
6783 if (last_insert != NULL)
6784 {
6785 s = last_insert;
6786 /* Use the CTRL-V only when entering a special char */
6787 if (c < ' ' || c == DEL)
6788 *s++ = Ctrl_V;
6789 s = add_char2buf(c, s);
6790 *s++ = ESC;
6791 *s++ = NUL;
6792 last_insert_skip = 0;
6793 }
6794}
6795
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006796#if defined(EXITFREE) || defined(PROTO)
6797 void
6798free_last_insert()
6799{
6800 vim_free(last_insert);
6801 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006802# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006803 vim_free(compl_orig_text);
6804 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006805# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006806}
6807#endif
6808
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809/*
6810 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6811 * and CSI. Handle multi-byte characters.
6812 * Returns a pointer to after the added bytes.
6813 */
6814 char_u *
6815add_char2buf(c, s)
6816 int c;
6817 char_u *s;
6818{
6819#ifdef FEAT_MBYTE
6820 char_u temp[MB_MAXBYTES];
6821 int i;
6822 int len;
6823
6824 len = (*mb_char2bytes)(c, temp);
6825 for (i = 0; i < len; ++i)
6826 {
6827 c = temp[i];
6828#endif
6829 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6830 if (c == K_SPECIAL)
6831 {
6832 *s++ = K_SPECIAL;
6833 *s++ = KS_SPECIAL;
6834 *s++ = KE_FILLER;
6835 }
6836#ifdef FEAT_GUI
6837 else if (c == CSI)
6838 {
6839 *s++ = CSI;
6840 *s++ = KS_EXTRA;
6841 *s++ = (int)KE_CSI;
6842 }
6843#endif
6844 else
6845 *s++ = c;
6846#ifdef FEAT_MBYTE
6847 }
6848#endif
6849 return s;
6850}
6851
6852/*
6853 * move cursor to start of line
6854 * if flags & BL_WHITE move to first non-white
6855 * if flags & BL_SOL move to first non-white if startofline is set,
6856 * otherwise keep "curswant" column
6857 * if flags & BL_FIX don't leave the cursor on a NUL.
6858 */
6859 void
6860beginline(flags)
6861 int flags;
6862{
6863 if ((flags & BL_SOL) && !p_sol)
6864 coladvance(curwin->w_curswant);
6865 else
6866 {
6867 curwin->w_cursor.col = 0;
6868#ifdef FEAT_VIRTUALEDIT
6869 curwin->w_cursor.coladd = 0;
6870#endif
6871
6872 if (flags & (BL_WHITE | BL_SOL))
6873 {
6874 char_u *ptr;
6875
6876 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6877 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6878 ++curwin->w_cursor.col;
6879 }
6880 curwin->w_set_curswant = TRUE;
6881 }
6882}
6883
6884/*
6885 * oneright oneleft cursor_down cursor_up
6886 *
6887 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006888 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 * Return OK when successful, FAIL when we hit a line of file boundary.
6890 */
6891
6892 int
6893oneright()
6894{
6895 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898#ifdef FEAT_VIRTUALEDIT
6899 if (virtual_active())
6900 {
6901 pos_T prevpos = curwin->w_cursor;
6902
6903 /* Adjust for multi-wide char (excluding TAB) */
6904 ptr = ml_get_cursor();
6905 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006906# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006908# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 ))
6912 ? ptr2cells(ptr) : 1));
6913 curwin->w_set_curswant = TRUE;
6914 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6915 return (prevpos.col != curwin->w_cursor.col
6916 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6917 }
6918#endif
6919
6920 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006921 if (*ptr == NUL)
6922 return FAIL; /* already at the very end */
6923
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006925 if (has_mbyte)
6926 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 else
6928#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006929 l = 1;
6930
6931 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6932 * contains "onemore". */
6933 if (ptr[l] == NUL
6934#ifdef FEAT_VIRTUALEDIT
6935 && (ve_flags & VE_ONEMORE) == 0
6936#endif
6937 )
6938 return FAIL;
6939 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941 curwin->w_set_curswant = TRUE;
6942 return OK;
6943}
6944
6945 int
6946oneleft()
6947{
6948#ifdef FEAT_VIRTUALEDIT
6949 if (virtual_active())
6950 {
6951 int width;
6952 int v = getviscol();
6953
6954 if (v == 0)
6955 return FAIL;
6956
6957# ifdef FEAT_LINEBREAK
6958 /* We might get stuck on 'showbreak', skip over it. */
6959 width = 1;
6960 for (;;)
6961 {
6962 coladvance(v - width);
6963 /* getviscol() is slow, skip it when 'showbreak' is empty and
6964 * there are no multi-byte characters */
6965 if ((*p_sbr == NUL
6966# ifdef FEAT_MBYTE
6967 && !has_mbyte
6968# endif
6969 ) || getviscol() < v)
6970 break;
6971 ++width;
6972 }
6973# else
6974 coladvance(v - 1);
6975# endif
6976
6977 if (curwin->w_cursor.coladd == 1)
6978 {
6979 char_u *ptr;
6980
6981 /* Adjust for multi-wide char (not a TAB) */
6982 ptr = ml_get_cursor();
6983 if (*ptr != TAB && vim_isprintc(
6984# ifdef FEAT_MBYTE
6985 (*mb_ptr2char)(ptr)
6986# else
6987 *ptr
6988# endif
6989 ) && ptr2cells(ptr) > 1)
6990 curwin->w_cursor.coladd = 0;
6991 }
6992
6993 curwin->w_set_curswant = TRUE;
6994 return OK;
6995 }
6996#endif
6997
6998 if (curwin->w_cursor.col == 0)
6999 return FAIL;
7000
7001 curwin->w_set_curswant = TRUE;
7002 --curwin->w_cursor.col;
7003
7004#ifdef FEAT_MBYTE
7005 /* if the character on the left of the current cursor is a multi-byte
7006 * character, move to its first byte */
7007 if (has_mbyte)
7008 mb_adjust_cursor();
7009#endif
7010 return OK;
7011}
7012
7013 int
7014cursor_up(n, upd_topline)
7015 long n;
7016 int upd_topline; /* When TRUE: update topline */
7017{
7018 linenr_T lnum;
7019
7020 if (n > 0)
7021 {
7022 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007023 /* This fails if the cursor is already in the first line or the count
7024 * is larger than the line number and '-' is in 'cpoptions' */
7025 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 return FAIL;
7027 if (n >= lnum)
7028 lnum = 1;
7029 else
7030#ifdef FEAT_FOLDING
7031 if (hasAnyFolding(curwin))
7032 {
7033 /*
7034 * Count each sequence of folded lines as one logical line.
7035 */
7036 /* go to the the start of the current fold */
7037 (void)hasFolding(lnum, &lnum, NULL);
7038
7039 while (n--)
7040 {
7041 /* move up one line */
7042 --lnum;
7043 if (lnum <= 1)
7044 break;
7045 /* If we entered a fold, move to the beginning, unless in
7046 * Insert mode or when 'foldopen' contains "all": it will open
7047 * in a moment. */
7048 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7049 (void)hasFolding(lnum, &lnum, NULL);
7050 }
7051 if (lnum < 1)
7052 lnum = 1;
7053 }
7054 else
7055#endif
7056 lnum -= n;
7057 curwin->w_cursor.lnum = lnum;
7058 }
7059
7060 /* try to advance to the column we want to be at */
7061 coladvance(curwin->w_curswant);
7062
7063 if (upd_topline)
7064 update_topline(); /* make sure curwin->w_topline is valid */
7065
7066 return OK;
7067}
7068
7069/*
7070 * Cursor down a number of logical lines.
7071 */
7072 int
7073cursor_down(n, upd_topline)
7074 long n;
7075 int upd_topline; /* When TRUE: update topline */
7076{
7077 linenr_T lnum;
7078
7079 if (n > 0)
7080 {
7081 lnum = curwin->w_cursor.lnum;
7082#ifdef FEAT_FOLDING
7083 /* Move to last line of fold, will fail if it's the end-of-file. */
7084 (void)hasFolding(lnum, NULL, &lnum);
7085#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007086 /* This fails if the cursor is already in the last line or would move
7087 * beyound the last line and '-' is in 'cpoptions' */
7088 if (lnum >= curbuf->b_ml.ml_line_count
7089 || (lnum + n > curbuf->b_ml.ml_line_count
7090 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 return FAIL;
7092 if (lnum + n >= curbuf->b_ml.ml_line_count)
7093 lnum = curbuf->b_ml.ml_line_count;
7094 else
7095#ifdef FEAT_FOLDING
7096 if (hasAnyFolding(curwin))
7097 {
7098 linenr_T last;
7099
7100 /* count each sequence of folded lines as one logical line */
7101 while (n--)
7102 {
7103 if (hasFolding(lnum, NULL, &last))
7104 lnum = last + 1;
7105 else
7106 ++lnum;
7107 if (lnum >= curbuf->b_ml.ml_line_count)
7108 break;
7109 }
7110 if (lnum > curbuf->b_ml.ml_line_count)
7111 lnum = curbuf->b_ml.ml_line_count;
7112 }
7113 else
7114#endif
7115 lnum += n;
7116 curwin->w_cursor.lnum = lnum;
7117 }
7118
7119 /* try to advance to the column we want to be at */
7120 coladvance(curwin->w_curswant);
7121
7122 if (upd_topline)
7123 update_topline(); /* make sure curwin->w_topline is valid */
7124
7125 return OK;
7126}
7127
7128/*
7129 * Stuff the last inserted text in the read buffer.
7130 * Last_insert actually is a copy of the redo buffer, so we
7131 * first have to remove the command.
7132 */
7133 int
7134stuff_inserted(c, count, no_esc)
7135 int c; /* Command character to be inserted */
7136 long count; /* Repeat this many times */
7137 int no_esc; /* Don't add an ESC at the end */
7138{
7139 char_u *esc_ptr;
7140 char_u *ptr;
7141 char_u *last_ptr;
7142 char_u last = NUL;
7143
7144 ptr = get_last_insert();
7145 if (ptr == NULL)
7146 {
7147 EMSG(_(e_noinstext));
7148 return FAIL;
7149 }
7150
7151 /* may want to stuff the command character, to start Insert mode */
7152 if (c != NUL)
7153 stuffcharReadbuff(c);
7154 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7155 *esc_ptr = NUL; /* remove the ESC */
7156
7157 /* when the last char is either "0" or "^" it will be quoted if no ESC
7158 * comes after it OR if it will inserted more than once and "ptr"
7159 * starts with ^D. -- Acevedo
7160 */
7161 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7162 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7163 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7164 {
7165 last = *last_ptr;
7166 *last_ptr = NUL;
7167 }
7168
7169 do
7170 {
7171 stuffReadbuff(ptr);
7172 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7173 if (last)
7174 stuffReadbuff((char_u *)(last == '0'
7175 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7176 : IF_EB("\026^", CTRL_V_STR "^")));
7177 }
7178 while (--count > 0);
7179
7180 if (last)
7181 *last_ptr = last;
7182
7183 if (esc_ptr != NULL)
7184 *esc_ptr = ESC; /* put the ESC back */
7185
7186 /* may want to stuff a trailing ESC, to get out of Insert mode */
7187 if (!no_esc)
7188 stuffcharReadbuff(ESC);
7189
7190 return OK;
7191}
7192
7193 char_u *
7194get_last_insert()
7195{
7196 if (last_insert == NULL)
7197 return NULL;
7198 return last_insert + last_insert_skip;
7199}
7200
7201/*
7202 * Get last inserted string, and remove trailing <Esc>.
7203 * Returns pointer to allocated memory (must be freed) or NULL.
7204 */
7205 char_u *
7206get_last_insert_save()
7207{
7208 char_u *s;
7209 int len;
7210
7211 if (last_insert == NULL)
7212 return NULL;
7213 s = vim_strsave(last_insert + last_insert_skip);
7214 if (s != NULL)
7215 {
7216 len = (int)STRLEN(s);
7217 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7218 s[len - 1] = NUL;
7219 }
7220 return s;
7221}
7222
7223/*
7224 * Check the word in front of the cursor for an abbreviation.
7225 * Called when the non-id character "c" has been entered.
7226 * When an abbreviation is recognized it is removed from the text and
7227 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7228 */
7229 static int
7230echeck_abbr(c)
7231 int c;
7232{
7233 /* Don't check for abbreviation in paste mode, when disabled and just
7234 * after moving around with cursor keys. */
7235 if (p_paste || no_abbr || arrow_used)
7236 return FALSE;
7237
7238 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7239 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7240}
7241
7242/*
7243 * replace-stack functions
7244 *
7245 * When replacing characters, the replaced characters are remembered for each
7246 * new character. This is used to re-insert the old text when backspacing.
7247 *
7248 * There is a NUL headed list of characters for each character that is
7249 * currently in the file after the insertion point. When BS is used, one NUL
7250 * headed list is put back for the deleted character.
7251 *
7252 * For a newline, there are two NUL headed lists. One contains the characters
7253 * that the NL replaced. The extra one stores the characters after the cursor
7254 * that were deleted (always white space).
7255 *
7256 * Replace_offset is normally 0, in which case replace_push will add a new
7257 * character at the end of the stack. If replace_offset is not 0, that many
7258 * characters will be left on the stack above the newly inserted character.
7259 */
7260
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007261static char_u *replace_stack = NULL;
7262static long replace_stack_nr = 0; /* next entry in replace stack */
7263static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 void
7266replace_push(c)
7267 int c; /* character that is replaced (NUL is none) */
7268{
7269 char_u *p;
7270
7271 if (replace_stack_nr < replace_offset) /* nothing to do */
7272 return;
7273 if (replace_stack_len <= replace_stack_nr)
7274 {
7275 replace_stack_len += 50;
7276 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7277 if (p == NULL) /* out of memory */
7278 {
7279 replace_stack_len -= 50;
7280 return;
7281 }
7282 if (replace_stack != NULL)
7283 {
7284 mch_memmove(p, replace_stack,
7285 (size_t)(replace_stack_nr * sizeof(char_u)));
7286 vim_free(replace_stack);
7287 }
7288 replace_stack = p;
7289 }
7290 p = replace_stack + replace_stack_nr - replace_offset;
7291 if (replace_offset)
7292 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7293 *p = c;
7294 ++replace_stack_nr;
7295}
7296
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007297#if defined(FEAT_MBYTE) || defined(PROTO)
7298/*
7299 * Push a character onto the replace stack. Handles a multi-byte character in
7300 * reverse byte order, so that the first byte is popped off first.
7301 * Return the number of bytes done (includes composing characters).
7302 */
7303 int
7304replace_push_mb(p)
7305 char_u *p;
7306{
7307 int l = (*mb_ptr2len)(p);
7308 int j;
7309
7310 for (j = l - 1; j >= 0; --j)
7311 replace_push(p[j]);
7312 return l;
7313}
7314#endif
7315
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316/*
7317 * Pop one item from the replace stack.
7318 * return -1 if stack empty
7319 * return replaced character or NUL otherwise
7320 */
7321 static int
7322replace_pop()
7323{
7324 if (replace_stack_nr == 0)
7325 return -1;
7326 return (int)replace_stack[--replace_stack_nr];
7327}
7328
7329/*
7330 * Join the top two items on the replace stack. This removes to "off"'th NUL
7331 * encountered.
7332 */
7333 static void
7334replace_join(off)
7335 int off; /* offset for which NUL to remove */
7336{
7337 int i;
7338
7339 for (i = replace_stack_nr; --i >= 0; )
7340 if (replace_stack[i] == NUL && off-- <= 0)
7341 {
7342 --replace_stack_nr;
7343 mch_memmove(replace_stack + i, replace_stack + i + 1,
7344 (size_t)(replace_stack_nr - i));
7345 return;
7346 }
7347}
7348
7349/*
7350 * Pop bytes from the replace stack until a NUL is found, and insert them
7351 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7352 */
7353 static void
7354replace_pop_ins()
7355{
7356 int cc;
7357 int oldState = State;
7358
7359 State = NORMAL; /* don't want REPLACE here */
7360 while ((cc = replace_pop()) > 0)
7361 {
7362#ifdef FEAT_MBYTE
7363 mb_replace_pop_ins(cc);
7364#else
7365 ins_char(cc);
7366#endif
7367 dec_cursor();
7368 }
7369 State = oldState;
7370}
7371
7372#ifdef FEAT_MBYTE
7373/*
7374 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7375 * indicates a multi-byte char, pop the other bytes too.
7376 */
7377 static void
7378mb_replace_pop_ins(cc)
7379 int cc;
7380{
7381 int n;
7382 char_u buf[MB_MAXBYTES];
7383 int i;
7384 int c;
7385
7386 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7387 {
7388 buf[0] = cc;
7389 for (i = 1; i < n; ++i)
7390 buf[i] = replace_pop();
7391 ins_bytes_len(buf, n);
7392 }
7393 else
7394 ins_char(cc);
7395
7396 if (enc_utf8)
7397 /* Handle composing chars. */
7398 for (;;)
7399 {
7400 c = replace_pop();
7401 if (c == -1) /* stack empty */
7402 break;
7403 if ((n = MB_BYTE2LEN(c)) == 1)
7404 {
7405 /* Not a multi-byte char, put it back. */
7406 replace_push(c);
7407 break;
7408 }
7409 else
7410 {
7411 buf[0] = c;
7412 for (i = 1; i < n; ++i)
7413 buf[i] = replace_pop();
7414 if (utf_iscomposing(utf_ptr2char(buf)))
7415 ins_bytes_len(buf, n);
7416 else
7417 {
7418 /* Not a composing char, put it back. */
7419 for (i = n - 1; i >= 0; --i)
7420 replace_push(buf[i]);
7421 break;
7422 }
7423 }
7424 }
7425}
7426#endif
7427
7428/*
7429 * make the replace stack empty
7430 * (called when exiting replace mode)
7431 */
7432 static void
7433replace_flush()
7434{
7435 vim_free(replace_stack);
7436 replace_stack = NULL;
7437 replace_stack_len = 0;
7438 replace_stack_nr = 0;
7439}
7440
7441/*
7442 * Handle doing a BS for one character.
7443 * cc < 0: replace stack empty, just move cursor
7444 * cc == 0: character was inserted, delete it
7445 * cc > 0: character was replaced, put cc (first byte of original char) back
7446 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007447 * When "limit_col" is >= 0, don't delete before this column. Matters when
7448 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 */
7450 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007451replace_do_bs(limit_col)
7452 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454 int cc;
7455#ifdef FEAT_VREPLACE
7456 int orig_len = 0;
7457 int ins_len;
7458 int orig_vcols = 0;
7459 colnr_T start_vcol;
7460 char_u *p;
7461 int i;
7462 int vcol;
7463#endif
7464
7465 cc = replace_pop();
7466 if (cc > 0)
7467 {
7468#ifdef FEAT_VREPLACE
7469 if (State & VREPLACE_FLAG)
7470 {
7471 /* Get the number of screen cells used by the character we are
7472 * going to delete. */
7473 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7474 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7475 }
7476#endif
7477#ifdef FEAT_MBYTE
7478 if (has_mbyte)
7479 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007480 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481# ifdef FEAT_VREPLACE
7482 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007483 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484# endif
7485 replace_push(cc);
7486 }
7487 else
7488#endif
7489 {
7490 pchar_cursor(cc);
7491#ifdef FEAT_VREPLACE
7492 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007493 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494#endif
7495 }
7496 replace_pop_ins();
7497
7498#ifdef FEAT_VREPLACE
7499 if (State & VREPLACE_FLAG)
7500 {
7501 /* Get the number of screen cells used by the inserted characters */
7502 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007503 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 vcol = start_vcol;
7505 for (i = 0; i < ins_len; ++i)
7506 {
7507 vcol += chartabsize(p + i, vcol);
7508#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007509 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510#endif
7511 }
7512 vcol -= start_vcol;
7513
7514 /* Delete spaces that were inserted after the cursor to keep the
7515 * text aligned. */
7516 curwin->w_cursor.col += ins_len;
7517 while (vcol > orig_vcols && gchar_cursor() == ' ')
7518 {
7519 del_char(FALSE);
7520 ++orig_vcols;
7521 }
7522 curwin->w_cursor.col -= ins_len;
7523 }
7524#endif
7525
7526 /* mark the buffer as changed and prepare for displaying */
7527 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7528 }
7529 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007530 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531}
7532
7533#ifdef FEAT_CINDENT
7534/*
7535 * Return TRUE if C-indenting is on.
7536 */
7537 static int
7538cindent_on()
7539{
7540 return (!p_paste && (curbuf->b_p_cin
7541# ifdef FEAT_EVAL
7542 || *curbuf->b_p_inde != NUL
7543# endif
7544 ));
7545}
7546#endif
7547
7548#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7549/*
7550 * Re-indent the current line, based on the current contents of it and the
7551 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7552 * confused what all the part that handles Control-T is doing that I'm not.
7553 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7554 */
7555
7556 void
7557fixthisline(get_the_indent)
7558 int (*get_the_indent) __ARGS((void));
7559{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007560 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 if (linewhite(curwin->w_cursor.lnum))
7562 did_ai = TRUE; /* delete the indent if the line stays empty */
7563}
7564
7565 void
7566fix_indent()
7567{
7568 if (p_paste)
7569 return;
7570# ifdef FEAT_LISP
7571 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7572 fixthisline(get_lisp_indent);
7573# endif
7574# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7575 else
7576# endif
7577# ifdef FEAT_CINDENT
7578 if (cindent_on())
7579 do_c_expr_indent();
7580# endif
7581}
7582
7583#endif
7584
7585#ifdef FEAT_CINDENT
7586/*
7587 * return TRUE if 'cinkeys' contains the key "keytyped",
7588 * when == '*': Only if key is preceded with '*' (indent before insert)
7589 * when == '!': Only if key is prededed with '!' (don't insert)
7590 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7591 *
7592 * "keytyped" can have a few special values:
7593 * KEY_OPEN_FORW
7594 * KEY_OPEN_BACK
7595 * KEY_COMPLETE just finished completion.
7596 *
7597 * If line_is_empty is TRUE accept keys with '0' before them.
7598 */
7599 int
7600in_cinkeys(keytyped, when, line_is_empty)
7601 int keytyped;
7602 int when;
7603 int line_is_empty;
7604{
7605 char_u *look;
7606 int try_match;
7607 int try_match_word;
7608 char_u *p;
7609 char_u *line;
7610 int icase;
7611 int i;
7612
Bram Moolenaar30848942009-12-24 14:46:12 +00007613 if (keytyped == NUL)
7614 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7615 return FALSE;
7616
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617#ifdef FEAT_EVAL
7618 if (*curbuf->b_p_inde != NUL)
7619 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7620 else
7621#endif
7622 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7623 while (*look)
7624 {
7625 /*
7626 * Find out if we want to try a match with this key, depending on
7627 * 'when' and a '*' or '!' before the key.
7628 */
7629 switch (when)
7630 {
7631 case '*': try_match = (*look == '*'); break;
7632 case '!': try_match = (*look == '!'); break;
7633 default: try_match = (*look != '*'); break;
7634 }
7635 if (*look == '*' || *look == '!')
7636 ++look;
7637
7638 /*
7639 * If there is a '0', only accept a match if the line is empty.
7640 * But may still match when typing last char of a word.
7641 */
7642 if (*look == '0')
7643 {
7644 try_match_word = try_match;
7645 if (!line_is_empty)
7646 try_match = FALSE;
7647 ++look;
7648 }
7649 else
7650 try_match_word = FALSE;
7651
7652 /*
7653 * does it look like a control character?
7654 */
7655 if (*look == '^'
7656#ifdef EBCDIC
7657 && (Ctrl_chr(look[1]) != 0)
7658#else
7659 && look[1] >= '?' && look[1] <= '_'
7660#endif
7661 )
7662 {
7663 if (try_match && keytyped == Ctrl_chr(look[1]))
7664 return TRUE;
7665 look += 2;
7666 }
7667 /*
7668 * 'o' means "o" command, open forward.
7669 * 'O' means "O" command, open backward.
7670 */
7671 else if (*look == 'o')
7672 {
7673 if (try_match && keytyped == KEY_OPEN_FORW)
7674 return TRUE;
7675 ++look;
7676 }
7677 else if (*look == 'O')
7678 {
7679 if (try_match && keytyped == KEY_OPEN_BACK)
7680 return TRUE;
7681 ++look;
7682 }
7683
7684 /*
7685 * 'e' means to check for "else" at start of line and just before the
7686 * cursor.
7687 */
7688 else if (*look == 'e')
7689 {
7690 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7691 {
7692 p = ml_get_curline();
7693 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7694 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7695 return TRUE;
7696 }
7697 ++look;
7698 }
7699
7700 /*
7701 * ':' only causes an indent if it is at the end of a label or case
7702 * statement, or when it was before typing the ':' (to fix
7703 * class::method for C++).
7704 */
7705 else if (*look == ':')
7706 {
7707 if (try_match && keytyped == ':')
7708 {
7709 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007710 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7711 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007713 /* Need to get the line again after cin_islabel(). */
7714 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (curwin->w_cursor.col > 2
7716 && p[curwin->w_cursor.col - 1] == ':'
7717 && p[curwin->w_cursor.col - 2] == ':')
7718 {
7719 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007720 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 || cin_islabel(30));
7722 p = ml_get_curline();
7723 p[curwin->w_cursor.col - 1] = ':';
7724 if (i)
7725 return TRUE;
7726 }
7727 }
7728 ++look;
7729 }
7730
7731
7732 /*
7733 * Is it a key in <>, maybe?
7734 */
7735 else if (*look == '<')
7736 {
7737 if (try_match)
7738 {
7739 /*
7740 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7741 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7742 * >, *, : and ! keys if they really really want to.
7743 */
7744 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7745 && keytyped == look[1])
7746 return TRUE;
7747
7748 if (keytyped == get_special_key_code(look + 1))
7749 return TRUE;
7750 }
7751 while (*look && *look != '>')
7752 look++;
7753 while (*look == '>')
7754 look++;
7755 }
7756
7757 /*
7758 * Is it a word: "=word"?
7759 */
7760 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7761 {
7762 ++look;
7763 if (*look == '~')
7764 {
7765 icase = TRUE;
7766 ++look;
7767 }
7768 else
7769 icase = FALSE;
7770 p = vim_strchr(look, ',');
7771 if (p == NULL)
7772 p = look + STRLEN(look);
7773 if ((try_match || try_match_word)
7774 && curwin->w_cursor.col >= (colnr_T)(p - look))
7775 {
7776 int match = FALSE;
7777
7778#ifdef FEAT_INS_EXPAND
7779 if (keytyped == KEY_COMPLETE)
7780 {
7781 char_u *s;
7782
7783 /* Just completed a word, check if it starts with "look".
7784 * search back for the start of a word. */
7785 line = ml_get_curline();
7786# ifdef FEAT_MBYTE
7787 if (has_mbyte)
7788 {
7789 char_u *n;
7790
7791 for (s = line + curwin->w_cursor.col; s > line; s = n)
7792 {
7793 n = mb_prevptr(line, s);
7794 if (!vim_iswordp(n))
7795 break;
7796 }
7797 }
7798 else
7799# endif
7800 for (s = line + curwin->w_cursor.col; s > line; --s)
7801 if (!vim_iswordc(s[-1]))
7802 break;
7803 if (s + (p - look) <= line + curwin->w_cursor.col
7804 && (icase
7805 ? MB_STRNICMP(s, look, p - look)
7806 : STRNCMP(s, look, p - look)) == 0)
7807 match = TRUE;
7808 }
7809 else
7810#endif
7811 /* TODO: multi-byte */
7812 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7813 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7814 {
7815 line = ml_get_cursor();
7816 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7817 || !vim_iswordc(line[-(p - look) - 1]))
7818 && (icase
7819 ? MB_STRNICMP(line - (p - look), look, p - look)
7820 : STRNCMP(line - (p - look), look, p - look))
7821 == 0)
7822 match = TRUE;
7823 }
7824 if (match && try_match_word && !try_match)
7825 {
7826 /* "0=word": Check if there are only blanks before the
7827 * word. */
7828 line = ml_get_curline();
7829 if ((int)(skipwhite(line) - line) !=
7830 (int)(curwin->w_cursor.col - (p - look)))
7831 match = FALSE;
7832 }
7833 if (match)
7834 return TRUE;
7835 }
7836 look = p;
7837 }
7838
7839 /*
7840 * ok, it's a boring generic character.
7841 */
7842 else
7843 {
7844 if (try_match && *look == keytyped)
7845 return TRUE;
7846 ++look;
7847 }
7848
7849 /*
7850 * Skip over ", ".
7851 */
7852 look = skip_to_option_part(look);
7853 }
7854 return FALSE;
7855}
7856#endif /* FEAT_CINDENT */
7857
7858#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7859/*
7860 * Map Hebrew keyboard when in hkmap mode.
7861 */
7862 int
7863hkmap(c)
7864 int c;
7865{
7866 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7867 {
7868 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7869 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7870 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7871 static char_u map[26] =
7872 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7873 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7874 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7875 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7876 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7877 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7878 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7879 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7880 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7881
7882 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7883 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7884 /* '-1'='sofit' */
7885 else if (c == 'x')
7886 return 'X';
7887 else if (c == 'q')
7888 return '\''; /* {geresh}={'} */
7889 else if (c == 246)
7890 return ' '; /* \"o --> ' ' for a german keyboard */
7891 else if (c == 228)
7892 return ' '; /* \"a --> ' ' -- / -- */
7893 else if (c == 252)
7894 return ' '; /* \"u --> ' ' -- / -- */
7895#ifdef EBCDIC
7896 else if (islower(c))
7897#else
7898 /* NOTE: islower() does not do the right thing for us on Linux so we
7899 * do this the same was as 5.7 and previous, so it works correctly on
7900 * all systems. Specifically, the e.g. Delete and Arrow keys are
7901 * munged and won't work if e.g. searching for Hebrew text.
7902 */
7903 else if (c >= 'a' && c <= 'z')
7904#endif
7905 return (int)(map[CharOrdLow(c)] + p_aleph);
7906 else
7907 return c;
7908 }
7909 else
7910 {
7911 switch (c)
7912 {
7913 case '`': return ';';
7914 case '/': return '.';
7915 case '\'': return ',';
7916 case 'q': return '/';
7917 case 'w': return '\'';
7918
7919 /* Hebrew letters - set offset from 'a' */
7920 case ',': c = '{'; break;
7921 case '.': c = 'v'; break;
7922 case ';': c = 't'; break;
7923 default: {
7924 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7925
7926#ifdef EBCDIC
7927 /* see note about islower() above */
7928 if (!islower(c))
7929#else
7930 if (c < 'a' || c > 'z')
7931#endif
7932 return c;
7933 c = str[CharOrdLow(c)];
7934 break;
7935 }
7936 }
7937
7938 return (int)(CharOrdLow(c) + p_aleph);
7939 }
7940}
7941#endif
7942
7943 static void
7944ins_reg()
7945{
7946 int need_redraw = FALSE;
7947 int regname;
7948 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007949#ifdef FEAT_VISUAL
7950 int vis_active = VIsual_active;
7951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952
7953 /*
7954 * If we are going to wait for a character, show a '"'.
7955 */
7956 pc_status = PC_STATUS_UNSET;
7957 if (redrawing() && !char_avail())
7958 {
7959 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007960 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
7962 edit_putchar('"', TRUE);
7963#ifdef FEAT_CMDL_INFO
7964 add_to_showcmd_c(Ctrl_R);
7965#endif
7966 }
7967
7968#ifdef USE_ON_FLY_SCROLL
7969 dont_scroll = TRUE; /* disallow scrolling here */
7970#endif
7971
7972 /*
7973 * Don't map the register name. This also prevents the mode message to be
7974 * deleted when ESC is hit.
7975 */
7976 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007977 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7980 {
7981 /* Get a third key for literal register insertion */
7982 literally = regname;
7983#ifdef FEAT_CMDL_INFO
7984 add_to_showcmd_c(literally);
7985#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007986 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
7989 --no_mapping;
7990
7991#ifdef FEAT_EVAL
7992 /*
7993 * Don't call u_sync() while getting the expression,
7994 * evaluating it or giving an error message for it!
7995 */
7996 ++no_u_sync;
7997 if (regname == '=')
7998 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007999# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008003# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 /* Restore the Input Method. */
8005 if (im_on)
8006 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008009 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8010 {
8011 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 else
8015 {
8016#endif
8017 if (literally == Ctrl_O || literally == Ctrl_P)
8018 {
8019 /* Append the command to the redo buffer. */
8020 AppendCharToRedobuff(Ctrl_R);
8021 AppendCharToRedobuff(literally);
8022 AppendCharToRedobuff(regname);
8023
8024 do_put(regname, BACKWARD, 1L,
8025 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8026 }
8027 else if (insert_reg(regname, literally) == FAIL)
8028 {
8029 vim_beep();
8030 need_redraw = TRUE; /* remove the '"' */
8031 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008032 else if (stop_insert_mode)
8033 /* When the '=' register was used and a function was invoked that
8034 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8035 * insert anything, need to remove the '"' */
8036 need_redraw = TRUE;
8037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038#ifdef FEAT_EVAL
8039 }
8040 --no_u_sync;
8041#endif
8042#ifdef FEAT_CMDL_INFO
8043 clear_showcmd();
8044#endif
8045
8046 /* If the inserted register is empty, we need to remove the '"' */
8047 if (need_redraw || stuff_empty())
8048 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008049
8050#ifdef FEAT_VISUAL
8051 /* Disallow starting Visual mode here, would get a weird mode. */
8052 if (!vis_active && VIsual_active)
8053 end_visual_mode();
8054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056
8057/*
8058 * CTRL-G commands in Insert mode.
8059 */
8060 static void
8061ins_ctrl_g()
8062{
8063 int c;
8064
8065#ifdef FEAT_INS_EXPAND
8066 /* Right after CTRL-X the cursor will be after the ruler. */
8067 setcursor();
8068#endif
8069
8070 /*
8071 * Don't map the second key. This also prevents the mode message to be
8072 * deleted when ESC is hit.
8073 */
8074 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008075 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 --no_mapping;
8077 switch (c)
8078 {
8079 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8080 case K_UP:
8081 case Ctrl_K:
8082 case 'k': ins_up(TRUE);
8083 break;
8084
8085 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8086 case K_DOWN:
8087 case Ctrl_J:
8088 case 'j': ins_down(TRUE);
8089 break;
8090
8091 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008092 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008094
8095 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008096 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008097 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 break;
8099
8100 /* Unknown CTRL-G command, reserved for future expansion. */
8101 default: vim_beep();
8102 }
8103}
8104
8105/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008106 * CTRL-^ in Insert mode.
8107 */
8108 static void
8109ins_ctrl_hat()
8110{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008111 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008112 {
8113 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8114 if (State & LANGMAP)
8115 {
8116 curbuf->b_p_iminsert = B_IMODE_NONE;
8117 State &= ~LANGMAP;
8118 }
8119 else
8120 {
8121 curbuf->b_p_iminsert = B_IMODE_LMAP;
8122 State |= LANGMAP;
8123#ifdef USE_IM_CONTROL
8124 im_set_active(FALSE);
8125#endif
8126 }
8127 }
8128#ifdef USE_IM_CONTROL
8129 else
8130 {
8131 /* There are no ":lmap" mappings, toggle IM */
8132 if (im_get_status())
8133 {
8134 curbuf->b_p_iminsert = B_IMODE_NONE;
8135 im_set_active(FALSE);
8136 }
8137 else
8138 {
8139 curbuf->b_p_iminsert = B_IMODE_IM;
8140 State &= ~LANGMAP;
8141 im_set_active(TRUE);
8142 }
8143 }
8144#endif
8145 set_iminsert_global();
8146 showmode();
8147#ifdef FEAT_GUI
8148 /* may show different cursor shape or color */
8149 if (gui.in_use)
8150 gui_update_cursor(TRUE, FALSE);
8151#endif
8152#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8153 /* Show/unshow value of 'keymap' in status lines. */
8154 status_redraw_curbuf();
8155#endif
8156}
8157
8158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 * Handle ESC in insert mode.
8160 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8161 * insert.
8162 */
8163 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008164ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 long *count;
8166 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008167 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 int temp;
8170 static int disabled_redraw = FALSE;
8171
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008172#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008173 check_spell_redraw();
8174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175#if defined(FEAT_HANGULIN)
8176# if defined(ESC_CHG_TO_ENG_MODE)
8177 hangul_input_state_set(0);
8178# endif
8179 if (composing_hangul)
8180 {
8181 push_raw_key(composing_hangul_buffer, 2);
8182 composing_hangul = 0;
8183 }
8184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185
8186 temp = curwin->w_cursor.col;
8187 if (disabled_redraw)
8188 {
8189 --RedrawingDisabled;
8190 disabled_redraw = FALSE;
8191 }
8192 if (!arrow_used)
8193 {
8194 /*
8195 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008196 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8197 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 */
8199 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008200 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
8202 /*
8203 * Repeating insert may take a long time. Check for
8204 * interrupt now and then.
8205 */
8206 if (*count > 0)
8207 {
8208 line_breakcheck();
8209 if (got_int)
8210 *count = 0;
8211 }
8212
8213 if (--*count > 0) /* repeat what was typed */
8214 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008215 /* Vi repeats the insert without replacing characters. */
8216 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8217 State &= ~REPLACE_FLAG;
8218
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 (void)start_redo_ins();
8220 if (cmdchar == 'r' || cmdchar == 'v')
8221 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8222 ++RedrawingDisabled;
8223 disabled_redraw = TRUE;
8224 return FALSE; /* repeat the insert */
8225 }
8226 stop_insert(&curwin->w_cursor, TRUE);
8227 undisplay_dollar();
8228 }
8229
8230 /* When an autoindent was removed, curswant stays after the
8231 * indent */
8232 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8233 curwin->w_set_curswant = TRUE;
8234
8235 /* Remember the last Insert position in the '^ mark. */
8236 if (!cmdmod.keepjumps)
8237 curbuf->b_last_insert = curwin->w_cursor;
8238
8239 /*
8240 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008241 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008243 if (!nomove
8244 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#ifdef FEAT_VIRTUALEDIT
8246 || curwin->w_cursor.coladd > 0
8247#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008248 )
8249 && (restart_edit == NUL
8250 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008252 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008254 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255#ifdef FEAT_RIGHTLEFT
8256 && !revins_on
8257#endif
8258 )
8259 {
8260#ifdef FEAT_VIRTUALEDIT
8261 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8262 {
8263 oneleft();
8264 if (restart_edit != NUL)
8265 ++curwin->w_cursor.coladd;
8266 }
8267 else
8268#endif
8269 {
8270 --curwin->w_cursor.col;
8271#ifdef FEAT_MBYTE
8272 /* Correct cursor for multi-byte character. */
8273 if (has_mbyte)
8274 mb_adjust_cursor();
8275#endif
8276 }
8277 }
8278
8279#ifdef USE_IM_CONTROL
8280 /* Disable IM to allow typing English directly for Normal mode commands.
8281 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8282 * well). */
8283 if (!(State & LANGMAP))
8284 im_save_status(&curbuf->b_p_iminsert);
8285 im_set_active(FALSE);
8286#endif
8287
8288 State = NORMAL;
8289 /* need to position cursor again (e.g. when on a TAB ) */
8290 changed_cline_bef_curs();
8291
8292#ifdef FEAT_MOUSE
8293 setmouse();
8294#endif
8295#ifdef CURSOR_SHAPE
8296 ui_cursor_shape(); /* may show different cursor shape */
8297#endif
8298
8299 /*
8300 * When recording or for CTRL-O, need to display the new mode.
8301 * Otherwise remove the mode message.
8302 */
8303 if (Recording || restart_edit != NUL)
8304 showmode();
8305 else if (p_smd)
8306 MSG("");
8307
8308 return TRUE; /* exit Insert mode */
8309}
8310
8311#ifdef FEAT_RIGHTLEFT
8312/*
8313 * Toggle language: hkmap and revins_on.
8314 * Move to end of reverse inserted text.
8315 */
8316 static void
8317ins_ctrl_()
8318{
8319 if (revins_on && revins_chars && revins_scol >= 0)
8320 {
8321 while (gchar_cursor() != NUL && revins_chars--)
8322 ++curwin->w_cursor.col;
8323 }
8324 p_ri = !p_ri;
8325 revins_on = (State == INSERT && p_ri);
8326 if (revins_on)
8327 {
8328 revins_scol = curwin->w_cursor.col;
8329 revins_legal++;
8330 revins_chars = 0;
8331 undisplay_dollar();
8332 }
8333 else
8334 revins_scol = -1;
8335#ifdef FEAT_FKMAP
8336 if (p_altkeymap)
8337 {
8338 /*
8339 * to be consistent also for redo command, using '.'
8340 * set arrow_used to true and stop it - causing to redo
8341 * characters entered in one mode (normal/reverse insert).
8342 */
8343 arrow_used = TRUE;
8344 (void)stop_arrow();
8345 p_fkmap = curwin->w_p_rl ^ p_ri;
8346 if (p_fkmap && p_ri)
8347 State = INSERT;
8348 }
8349 else
8350#endif
8351 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8352 showmode();
8353}
8354#endif
8355
8356#ifdef FEAT_VISUAL
8357/*
8358 * If 'keymodel' contains "startsel", may start selection.
8359 * Returns TRUE when a CTRL-O and other keys stuffed.
8360 */
8361 static int
8362ins_start_select(c)
8363 int c;
8364{
8365 if (km_startsel)
8366 switch (c)
8367 {
8368 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 case K_PAGEUP:
8371 case K_KPAGEUP:
8372 case K_PAGEDOWN:
8373 case K_KPAGEDOWN:
8374# ifdef MACOS
8375 case K_LEFT:
8376 case K_RIGHT:
8377 case K_UP:
8378 case K_DOWN:
8379 case K_END:
8380 case K_HOME:
8381# endif
8382 if (!(mod_mask & MOD_MASK_SHIFT))
8383 break;
8384 /* FALLTHROUGH */
8385 case K_S_LEFT:
8386 case K_S_RIGHT:
8387 case K_S_UP:
8388 case K_S_DOWN:
8389 case K_S_END:
8390 case K_S_HOME:
8391 /* Start selection right away, the cursor can move with
8392 * CTRL-O when beyond the end of the line. */
8393 start_selection();
8394
8395 /* Execute the key in (insert) Select mode. */
8396 stuffcharReadbuff(Ctrl_O);
8397 if (mod_mask)
8398 {
8399 char_u buf[4];
8400
8401 buf[0] = K_SPECIAL;
8402 buf[1] = KS_MODIFIER;
8403 buf[2] = mod_mask;
8404 buf[3] = NUL;
8405 stuffReadbuff(buf);
8406 }
8407 stuffcharReadbuff(c);
8408 return TRUE;
8409 }
8410 return FALSE;
8411}
8412#endif
8413
8414/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008415 * <Insert> key in Insert mode: toggle insert/remplace mode.
8416 */
8417 static void
8418ins_insert(replaceState)
8419 int replaceState;
8420{
8421#ifdef FEAT_FKMAP
8422 if (p_fkmap && p_ri)
8423 {
8424 beep_flush();
8425 EMSG(farsi_text_3); /* encoded in Farsi */
8426 return;
8427 }
8428#endif
8429
8430#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008431# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008432 set_vim_var_string(VV_INSERTMODE,
8433 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008434# ifdef FEAT_VREPLACE
8435 replaceState == VREPLACE ? "v" :
8436# endif
8437 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008438# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008439 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8440#endif
8441 if (State & REPLACE_FLAG)
8442 State = INSERT | (State & LANGMAP);
8443 else
8444 State = replaceState | (State & LANGMAP);
8445 AppendCharToRedobuff(K_INS);
8446 showmode();
8447#ifdef CURSOR_SHAPE
8448 ui_cursor_shape(); /* may show different cursor shape */
8449#endif
8450}
8451
8452/*
8453 * Pressed CTRL-O in Insert mode.
8454 */
8455 static void
8456ins_ctrl_o()
8457{
8458#ifdef FEAT_VREPLACE
8459 if (State & VREPLACE_FLAG)
8460 restart_edit = 'V';
8461 else
8462#endif
8463 if (State & REPLACE_FLAG)
8464 restart_edit = 'R';
8465 else
8466 restart_edit = 'I';
8467#ifdef FEAT_VIRTUALEDIT
8468 if (virtual_active())
8469 ins_at_eol = FALSE; /* cursor always keeps its column */
8470 else
8471#endif
8472 ins_at_eol = (gchar_cursor() == NUL);
8473}
8474
8475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 * If the cursor is on an indent, ^T/^D insert/delete one
8477 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008478 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 * with vi. But vi only supports ^T and ^D after an
8480 * autoindent, we support it everywhere.
8481 */
8482 static void
8483ins_shift(c, lastc)
8484 int c;
8485 int lastc;
8486{
8487 if (stop_arrow() == FAIL)
8488 return;
8489 AppendCharToRedobuff(c);
8490
8491 /*
8492 * 0^D and ^^D: remove all indent.
8493 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008494 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8495 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {
8497 --curwin->w_cursor.col;
8498 (void)del_char(FALSE); /* delete the '^' or '0' */
8499 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8500 if (State & REPLACE_FLAG)
8501 replace_pop_ins();
8502 if (lastc == '^')
8503 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008504 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 }
8506 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008507 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
8509 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8510 did_ai = FALSE;
8511#ifdef FEAT_SMARTINDENT
8512 did_si = FALSE;
8513 can_si = FALSE;
8514 can_si_back = FALSE;
8515#endif
8516#ifdef FEAT_CINDENT
8517 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8518#endif
8519}
8520
8521 static void
8522ins_del()
8523{
8524 int temp;
8525
8526 if (stop_arrow() == FAIL)
8527 return;
8528 if (gchar_cursor() == NUL) /* delete newline */
8529 {
8530 temp = curwin->w_cursor.col;
8531 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008532 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 vim_beep();
8534 else
8535 curwin->w_cursor.col = temp;
8536 }
8537 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8538 vim_beep();
8539 did_ai = FALSE;
8540#ifdef FEAT_SMARTINDENT
8541 did_si = FALSE;
8542 can_si = FALSE;
8543 can_si_back = FALSE;
8544#endif
8545 AppendCharToRedobuff(K_DEL);
8546}
8547
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008548static void ins_bs_one __ARGS((colnr_T *vcolp));
8549
8550/*
8551 * Delete one character for ins_bs().
8552 */
8553 static void
8554ins_bs_one(vcolp)
8555 colnr_T *vcolp;
8556{
8557 dec_cursor();
8558 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8559 if (State & REPLACE_FLAG)
8560 {
8561 /* Don't delete characters before the insert point when in
8562 * Replace mode */
8563 if (curwin->w_cursor.lnum != Insstart.lnum
8564 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008565 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008566 }
8567 else
8568 (void)del_char(FALSE);
8569}
8570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571/*
8572 * Handle Backspace, delete-word and delete-line in Insert mode.
8573 * Return TRUE when backspace was actually used.
8574 */
8575 static int
8576ins_bs(c, mode, inserted_space_p)
8577 int c;
8578 int mode;
8579 int *inserted_space_p;
8580{
8581 linenr_T lnum;
8582 int cc;
8583 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008584 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 colnr_T mincol;
8586 int did_backspace = FALSE;
8587 int in_indent;
8588 int oldState;
8589#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008590 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#endif
8592
8593 /*
8594 * can't delete anything in an empty file
8595 * can't backup past first character in buffer
8596 * can't backup past starting point unless 'backspace' > 1
8597 * can backup to a previous line if 'backspace' == 0
8598 */
8599 if ( bufempty()
8600 || (
8601#ifdef FEAT_RIGHTLEFT
8602 !revins_on &&
8603#endif
8604 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8605 || (!can_bs(BS_START)
8606 && (arrow_used
8607 || (curwin->w_cursor.lnum == Insstart.lnum
8608 && curwin->w_cursor.col <= Insstart.col)))
8609 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8610 && curwin->w_cursor.col <= ai_col)
8611 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8612 {
8613 vim_beep();
8614 return FALSE;
8615 }
8616
8617 if (stop_arrow() == FAIL)
8618 return FALSE;
8619 in_indent = inindent(0);
8620#ifdef FEAT_CINDENT
8621 if (in_indent)
8622 can_cindent = FALSE;
8623#endif
8624#ifdef FEAT_COMMENTS
8625 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8626#endif
8627#ifdef FEAT_RIGHTLEFT
8628 if (revins_on) /* put cursor after last inserted char */
8629 inc_cursor();
8630#endif
8631
8632#ifdef FEAT_VIRTUALEDIT
8633 /* Virtualedit:
8634 * BACKSPACE_CHAR eats a virtual space
8635 * BACKSPACE_WORD eats all coladd
8636 * BACKSPACE_LINE eats all coladd and keeps going
8637 */
8638 if (curwin->w_cursor.coladd > 0)
8639 {
8640 if (mode == BACKSPACE_CHAR)
8641 {
8642 --curwin->w_cursor.coladd;
8643 return TRUE;
8644 }
8645 if (mode == BACKSPACE_WORD)
8646 {
8647 curwin->w_cursor.coladd = 0;
8648 return TRUE;
8649 }
8650 curwin->w_cursor.coladd = 0;
8651 }
8652#endif
8653
8654 /*
8655 * delete newline!
8656 */
8657 if (curwin->w_cursor.col == 0)
8658 {
8659 lnum = Insstart.lnum;
8660 if (curwin->w_cursor.lnum == Insstart.lnum
8661#ifdef FEAT_RIGHTLEFT
8662 || revins_on
8663#endif
8664 )
8665 {
8666 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8667 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8668 return FALSE;
8669 --Insstart.lnum;
8670 Insstart.col = MAXCOL;
8671 }
8672 /*
8673 * In replace mode:
8674 * cc < 0: NL was inserted, delete it
8675 * cc >= 0: NL was replaced, put original characters back
8676 */
8677 cc = -1;
8678 if (State & REPLACE_FLAG)
8679 cc = replace_pop(); /* returns -1 if NL was inserted */
8680 /*
8681 * In replace mode, in the line we started replacing, we only move the
8682 * cursor.
8683 */
8684 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8685 {
8686 dec_cursor();
8687 }
8688 else
8689 {
8690#ifdef FEAT_VREPLACE
8691 if (!(State & VREPLACE_FLAG)
8692 || curwin->w_cursor.lnum > orig_line_count)
8693#endif
8694 {
8695 temp = gchar_cursor(); /* remember current char */
8696 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008697
8698 /* When "aw" is in 'formatoptions' we must delete the space at
8699 * the end of the line, otherwise the line will be broken
8700 * again when auto-formatting. */
8701 if (has_format_option(FO_AUTO)
8702 && has_format_option(FO_WHITE_PAR))
8703 {
8704 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8705 TRUE);
8706 int len;
8707
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008708 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008709 if (len > 0 && ptr[len - 1] == ' ')
8710 ptr[len - 1] = NUL;
8711 }
8712
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008713 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 if (temp == NUL && gchar_cursor() != NUL)
8715 inc_cursor();
8716 }
8717#ifdef FEAT_VREPLACE
8718 else
8719 dec_cursor();
8720#endif
8721
8722 /*
8723 * In REPLACE mode we have to put back the text that was replaced
8724 * by the NL. On the replace stack is first a NUL-terminated
8725 * sequence of characters that were deleted and then the
8726 * characters that NL replaced.
8727 */
8728 if (State & REPLACE_FLAG)
8729 {
8730 /*
8731 * Do the next ins_char() in NORMAL state, to
8732 * prevent ins_char() from replacing characters and
8733 * avoiding showmatch().
8734 */
8735 oldState = State;
8736 State = NORMAL;
8737 /*
8738 * restore characters (blanks) deleted after cursor
8739 */
8740 while (cc > 0)
8741 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008742 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#ifdef FEAT_MBYTE
8744 mb_replace_pop_ins(cc);
8745#else
8746 ins_char(cc);
8747#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008748 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 cc = replace_pop();
8750 }
8751 /* restore the characters that NL replaced */
8752 replace_pop_ins();
8753 State = oldState;
8754 }
8755 }
8756 did_ai = FALSE;
8757 }
8758 else
8759 {
8760 /*
8761 * Delete character(s) before the cursor.
8762 */
8763#ifdef FEAT_RIGHTLEFT
8764 if (revins_on) /* put cursor on last inserted char */
8765 dec_cursor();
8766#endif
8767 mincol = 0;
8768 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008769 if (mode == BACKSPACE_LINE
8770 && (curbuf->b_p_ai
8771#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008772 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008773#endif
8774 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775#ifdef FEAT_RIGHTLEFT
8776 && !revins_on
8777#endif
8778 )
8779 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008780 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008782 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008784 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 }
8786
8787 /*
8788 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8789 */
8790 if ( mode == BACKSPACE_CHAR
8791 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008792 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008793 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 && (*(ml_get_cursor() - 1) == TAB
8795 || (*(ml_get_cursor() - 1) == ' '
8796 && (!*inserted_space_p
8797 || arrow_used))))))
8798 {
8799 int ts;
8800 colnr_T vcol;
8801 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008802 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803
8804 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008805 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 ts = curbuf->b_p_sw;
8807 else
8808 ts = curbuf->b_p_sts;
8809 /* Compute the virtual column where we want to be. Since
8810 * 'showbreak' may get in the way, need to get the last column of
8811 * the previous character. */
8812 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008813 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 dec_cursor();
8815 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8816 inc_cursor();
8817 want_vcol = (want_vcol / ts) * ts;
8818
8819 /* delete characters until we are at or before want_vcol */
8820 while (vcol > want_vcol
8821 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008822 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823
8824 /* insert extra spaces until we are at want_vcol */
8825 while (vcol < want_vcol)
8826 {
8827 /* Remember the first char we inserted */
8828 if (curwin->w_cursor.lnum == Insstart.lnum
8829 && curwin->w_cursor.col < Insstart.col)
8830 Insstart.col = curwin->w_cursor.col;
8831
8832#ifdef FEAT_VREPLACE
8833 if (State & VREPLACE_FLAG)
8834 ins_char(' ');
8835 else
8836#endif
8837 {
8838 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008839 if ((State & REPLACE_FLAG))
8840 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8843 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008844
8845 /* If we are now back where we started delete one character. Can
8846 * happen when using 'sts' and 'linebreak'. */
8847 if (vcol >= start_vcol)
8848 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 }
8850
8851 /*
8852 * Delete upto starting point, start of line or previous word.
8853 */
8854 else do
8855 {
8856#ifdef FEAT_RIGHTLEFT
8857 if (!revins_on) /* put cursor on char to be deleted */
8858#endif
8859 dec_cursor();
8860
8861 /* start of word? */
8862 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8863 {
8864 mode = BACKSPACE_WORD_NOT_SPACE;
8865 temp = vim_iswordc(gchar_cursor());
8866 }
8867 /* end of word? */
8868 else if (mode == BACKSPACE_WORD_NOT_SPACE
8869 && (vim_isspace(cc = gchar_cursor())
8870 || vim_iswordc(cc) != temp))
8871 {
8872#ifdef FEAT_RIGHTLEFT
8873 if (!revins_on)
8874#endif
8875 inc_cursor();
8876#ifdef FEAT_RIGHTLEFT
8877 else if (State & REPLACE_FLAG)
8878 dec_cursor();
8879#endif
8880 break;
8881 }
8882 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008883 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 else
8885 {
8886#ifdef FEAT_MBYTE
8887 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008888 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889#endif
8890 (void)del_char(FALSE);
8891#ifdef FEAT_MBYTE
8892 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008893 * If there are combining characters and 'delcombine' is set
8894 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 * character.
8896 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008897 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 inc_cursor();
8899#endif
8900#ifdef FEAT_RIGHTLEFT
8901 if (revins_chars)
8902 {
8903 revins_chars--;
8904 revins_legal++;
8905 }
8906 if (revins_on && gchar_cursor() == NUL)
8907 break;
8908#endif
8909 }
8910 /* Just a single backspace?: */
8911 if (mode == BACKSPACE_CHAR)
8912 break;
8913 } while (
8914#ifdef FEAT_RIGHTLEFT
8915 revins_on ||
8916#endif
8917 (curwin->w_cursor.col > mincol
8918 && (curwin->w_cursor.lnum != Insstart.lnum
8919 || curwin->w_cursor.col != Insstart.col)));
8920 did_backspace = TRUE;
8921 }
8922#ifdef FEAT_SMARTINDENT
8923 did_si = FALSE;
8924 can_si = FALSE;
8925 can_si_back = FALSE;
8926#endif
8927 if (curwin->w_cursor.col <= 1)
8928 did_ai = FALSE;
8929 /*
8930 * It's a little strange to put backspaces into the redo
8931 * buffer, but it makes auto-indent a lot easier to deal
8932 * with.
8933 */
8934 AppendCharToRedobuff(c);
8935
8936 /* If deleted before the insertion point, adjust it */
8937 if (curwin->w_cursor.lnum == Insstart.lnum
8938 && curwin->w_cursor.col < Insstart.col)
8939 Insstart.col = curwin->w_cursor.col;
8940
8941 /* vi behaviour: the cursor moves backward but the character that
8942 * was there remains visible
8943 * Vim behaviour: the cursor moves backward and the character that
8944 * was there is erased from the screen.
8945 * We can emulate the vi behaviour by pretending there is a dollar
8946 * displayed even when there isn't.
8947 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8948 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8949 dollar_vcol = curwin->w_virtcol;
8950
Bram Moolenaarce3be472008-01-14 19:12:28 +00008951#ifdef FEAT_FOLDING
8952 /* When deleting a char the cursor line must never be in a closed fold.
8953 * E.g., when 'foldmethod' is indent and deleting the first non-white
8954 * char before a Tab. */
8955 if (did_backspace)
8956 foldOpenCursor();
8957#endif
8958
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 return did_backspace;
8960}
8961
8962#ifdef FEAT_MOUSE
8963 static void
8964ins_mouse(c)
8965 int c;
8966{
8967 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008968 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969
8970# ifdef FEAT_GUI
8971 /* When GUI is active, also move/paste when 'mouse' is empty */
8972 if (!gui.in_use)
8973# endif
8974 if (!mouse_has(MOUSE_INSERT))
8975 return;
8976
8977 undisplay_dollar();
8978 tpos = curwin->w_cursor;
8979 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8980 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008981#ifdef FEAT_WINDOWS
8982 win_T *new_curwin = curwin;
8983
8984 if (curwin != old_curwin && win_valid(old_curwin))
8985 {
8986 /* Mouse took us to another window. We need to go back to the
8987 * previous one to stop insert there properly. */
8988 curwin = old_curwin;
8989 curbuf = curwin->w_buffer;
8990 }
8991#endif
8992 start_arrow(curwin == old_curwin ? &tpos : NULL);
8993#ifdef FEAT_WINDOWS
8994 if (curwin != new_curwin && win_valid(new_curwin))
8995 {
8996 curwin = new_curwin;
8997 curbuf = curwin->w_buffer;
8998 }
8999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000# ifdef FEAT_CINDENT
9001 can_cindent = TRUE;
9002# endif
9003 }
9004
9005#ifdef FEAT_WINDOWS
9006 /* redraw status lines (in case another window became active) */
9007 redraw_statuslines();
9008#endif
9009}
9010
9011 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009012ins_mousescroll(dir)
9013 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009016# if defined(FEAT_WINDOWS)
9017 win_T *old_curwin = curwin;
9018# endif
9019# ifdef FEAT_INS_EXPAND
9020 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021# endif
9022
9023 tpos = curwin->w_cursor;
9024
9025# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 /* Currently the mouse coordinates are only known in the GUI. */
9027 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
9028 {
9029 int row, col;
9030
9031 row = mouse_row;
9032 col = mouse_col;
9033
9034 /* find the window at the pointer coordinates */
9035 curwin = mouse_find_win(&row, &col);
9036 curbuf = curwin->w_buffer;
9037 }
9038 if (curwin == old_curwin)
9039# endif
9040 undisplay_dollar();
9041
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009042# ifdef FEAT_INS_EXPAND
9043 /* Don't scroll the window in which completion is being done. */
9044 if (!pum_visible()
9045# if defined(FEAT_WINDOWS)
9046 || curwin != old_curwin
9047# endif
9048 )
9049# endif
9050 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009051 if (dir == MSCR_DOWN || dir == MSCR_UP)
9052 {
9053 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9054 scroll_redraw(dir,
9055 (long)(curwin->w_botline - curwin->w_topline));
9056 else
9057 scroll_redraw(dir, 3L);
9058 }
9059#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009060 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009061 {
9062 int val, step = 6;
9063
9064 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9065 step = W_WIDTH(curwin);
9066 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9067 if (val < 0)
9068 val = 0;
9069 gui_do_horiz_scroll(val, TRUE);
9070 }
9071#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009072# ifdef FEAT_INS_EXPAND
9073 did_scroll = TRUE;
9074# endif
9075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076
9077# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
9078 curwin->w_redr_status = TRUE;
9079
9080 curwin = old_curwin;
9081 curbuf = curwin->w_buffer;
9082# endif
9083
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009084# ifdef FEAT_INS_EXPAND
9085 /* The popup menu may overlay the window, need to redraw it.
9086 * TODO: Would be more efficient to only redraw the windows that are
9087 * overlapped by the popup menu. */
9088 if (pum_visible() && did_scroll)
9089 {
9090 redraw_all_later(NOT_VALID);
9091 ins_compl_show_pum();
9092 }
9093# endif
9094
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (!equalpos(curwin->w_cursor, tpos))
9096 {
9097 start_arrow(&tpos);
9098# ifdef FEAT_CINDENT
9099 can_cindent = TRUE;
9100# endif
9101 }
9102}
9103#endif
9104
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009105#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009106 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009107ins_tabline(c)
9108 int c;
9109{
9110 /* We will be leaving the current window, unless closing another tab. */
9111 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9112 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9113 {
9114 undisplay_dollar();
9115 start_arrow(&curwin->w_cursor);
9116# ifdef FEAT_CINDENT
9117 can_cindent = TRUE;
9118# endif
9119 }
9120
9121 if (c == K_TABLINE)
9122 goto_tabpage(current_tab);
9123 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009124 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009125 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009126 redraw_statuslines(); /* will redraw the tabline when needed */
9127 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009128}
9129#endif
9130
9131#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 void
9133ins_scroll()
9134{
9135 pos_T tpos;
9136
9137 undisplay_dollar();
9138 tpos = curwin->w_cursor;
9139 if (gui_do_scroll())
9140 {
9141 start_arrow(&tpos);
9142# ifdef FEAT_CINDENT
9143 can_cindent = TRUE;
9144# endif
9145 }
9146}
9147
9148 void
9149ins_horscroll()
9150{
9151 pos_T tpos;
9152
9153 undisplay_dollar();
9154 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009155 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 {
9157 start_arrow(&tpos);
9158# ifdef FEAT_CINDENT
9159 can_cindent = TRUE;
9160# endif
9161 }
9162}
9163#endif
9164
9165 static void
9166ins_left()
9167{
9168 pos_T tpos;
9169
9170#ifdef FEAT_FOLDING
9171 if ((fdo_flags & FDO_HOR) && KeyTyped)
9172 foldOpenCursor();
9173#endif
9174 undisplay_dollar();
9175 tpos = curwin->w_cursor;
9176 if (oneleft() == OK)
9177 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009178#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9179 /* Only call start_arrow() when not busy with preediting, it will
9180 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9181 if (!im_is_preediting())
9182#endif
9183 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#ifdef FEAT_RIGHTLEFT
9185 /* If exit reversed string, position is fixed */
9186 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9187 revins_legal++;
9188 revins_chars++;
9189#endif
9190 }
9191
9192 /*
9193 * if 'whichwrap' set for cursor in insert mode may go to
9194 * previous line
9195 */
9196 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9197 {
9198 start_arrow(&tpos);
9199 --(curwin->w_cursor.lnum);
9200 coladvance((colnr_T)MAXCOL);
9201 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9202 }
9203 else
9204 vim_beep();
9205}
9206
9207 static void
9208ins_home(c)
9209 int c;
9210{
9211 pos_T tpos;
9212
9213#ifdef FEAT_FOLDING
9214 if ((fdo_flags & FDO_HOR) && KeyTyped)
9215 foldOpenCursor();
9216#endif
9217 undisplay_dollar();
9218 tpos = curwin->w_cursor;
9219 if (c == K_C_HOME)
9220 curwin->w_cursor.lnum = 1;
9221 curwin->w_cursor.col = 0;
9222#ifdef FEAT_VIRTUALEDIT
9223 curwin->w_cursor.coladd = 0;
9224#endif
9225 curwin->w_curswant = 0;
9226 start_arrow(&tpos);
9227}
9228
9229 static void
9230ins_end(c)
9231 int c;
9232{
9233 pos_T tpos;
9234
9235#ifdef FEAT_FOLDING
9236 if ((fdo_flags & FDO_HOR) && KeyTyped)
9237 foldOpenCursor();
9238#endif
9239 undisplay_dollar();
9240 tpos = curwin->w_cursor;
9241 if (c == K_C_END)
9242 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9243 coladvance((colnr_T)MAXCOL);
9244 curwin->w_curswant = MAXCOL;
9245
9246 start_arrow(&tpos);
9247}
9248
9249 static void
9250ins_s_left()
9251{
9252#ifdef FEAT_FOLDING
9253 if ((fdo_flags & FDO_HOR) && KeyTyped)
9254 foldOpenCursor();
9255#endif
9256 undisplay_dollar();
9257 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9258 {
9259 start_arrow(&curwin->w_cursor);
9260 (void)bck_word(1L, FALSE, FALSE);
9261 curwin->w_set_curswant = TRUE;
9262 }
9263 else
9264 vim_beep();
9265}
9266
9267 static void
9268ins_right()
9269{
9270#ifdef FEAT_FOLDING
9271 if ((fdo_flags & FDO_HOR) && KeyTyped)
9272 foldOpenCursor();
9273#endif
9274 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009275 if (gchar_cursor() != NUL
9276#ifdef FEAT_VIRTUALEDIT
9277 || virtual_active()
9278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 )
9280 {
9281 start_arrow(&curwin->w_cursor);
9282 curwin->w_set_curswant = TRUE;
9283#ifdef FEAT_VIRTUALEDIT
9284 if (virtual_active())
9285 oneright();
9286 else
9287#endif
9288 {
9289#ifdef FEAT_MBYTE
9290 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009291 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 else
9293#endif
9294 ++curwin->w_cursor.col;
9295 }
9296
9297#ifdef FEAT_RIGHTLEFT
9298 revins_legal++;
9299 if (revins_chars)
9300 revins_chars--;
9301#endif
9302 }
9303 /* if 'whichwrap' set for cursor in insert mode, may move the
9304 * cursor to the next line */
9305 else if (vim_strchr(p_ww, ']') != NULL
9306 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9307 {
9308 start_arrow(&curwin->w_cursor);
9309 curwin->w_set_curswant = TRUE;
9310 ++curwin->w_cursor.lnum;
9311 curwin->w_cursor.col = 0;
9312 }
9313 else
9314 vim_beep();
9315}
9316
9317 static void
9318ins_s_right()
9319{
9320#ifdef FEAT_FOLDING
9321 if ((fdo_flags & FDO_HOR) && KeyTyped)
9322 foldOpenCursor();
9323#endif
9324 undisplay_dollar();
9325 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9326 || gchar_cursor() != NUL)
9327 {
9328 start_arrow(&curwin->w_cursor);
9329 (void)fwd_word(1L, FALSE, 0);
9330 curwin->w_set_curswant = TRUE;
9331 }
9332 else
9333 vim_beep();
9334}
9335
9336 static void
9337ins_up(startcol)
9338 int startcol; /* when TRUE move to Insstart.col */
9339{
9340 pos_T tpos;
9341 linenr_T old_topline = curwin->w_topline;
9342#ifdef FEAT_DIFF
9343 int old_topfill = curwin->w_topfill;
9344#endif
9345
9346 undisplay_dollar();
9347 tpos = curwin->w_cursor;
9348 if (cursor_up(1L, TRUE) == OK)
9349 {
9350 if (startcol)
9351 coladvance(getvcol_nolist(&Insstart));
9352 if (old_topline != curwin->w_topline
9353#ifdef FEAT_DIFF
9354 || old_topfill != curwin->w_topfill
9355#endif
9356 )
9357 redraw_later(VALID);
9358 start_arrow(&tpos);
9359#ifdef FEAT_CINDENT
9360 can_cindent = TRUE;
9361#endif
9362 }
9363 else
9364 vim_beep();
9365}
9366
9367 static void
9368ins_pageup()
9369{
9370 pos_T tpos;
9371
9372 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009373
9374#ifdef FEAT_WINDOWS
9375 if (mod_mask & MOD_MASK_CTRL)
9376 {
9377 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009378 if (first_tabpage->tp_next != NULL)
9379 {
9380 start_arrow(&curwin->w_cursor);
9381 goto_tabpage(-1);
9382 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009383 return;
9384 }
9385#endif
9386
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 tpos = curwin->w_cursor;
9388 if (onepage(BACKWARD, 1L) == OK)
9389 {
9390 start_arrow(&tpos);
9391#ifdef FEAT_CINDENT
9392 can_cindent = TRUE;
9393#endif
9394 }
9395 else
9396 vim_beep();
9397}
9398
9399 static void
9400ins_down(startcol)
9401 int startcol; /* when TRUE move to Insstart.col */
9402{
9403 pos_T tpos;
9404 linenr_T old_topline = curwin->w_topline;
9405#ifdef FEAT_DIFF
9406 int old_topfill = curwin->w_topfill;
9407#endif
9408
9409 undisplay_dollar();
9410 tpos = curwin->w_cursor;
9411 if (cursor_down(1L, TRUE) == OK)
9412 {
9413 if (startcol)
9414 coladvance(getvcol_nolist(&Insstart));
9415 if (old_topline != curwin->w_topline
9416#ifdef FEAT_DIFF
9417 || old_topfill != curwin->w_topfill
9418#endif
9419 )
9420 redraw_later(VALID);
9421 start_arrow(&tpos);
9422#ifdef FEAT_CINDENT
9423 can_cindent = TRUE;
9424#endif
9425 }
9426 else
9427 vim_beep();
9428}
9429
9430 static void
9431ins_pagedown()
9432{
9433 pos_T tpos;
9434
9435 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009436
9437#ifdef FEAT_WINDOWS
9438 if (mod_mask & MOD_MASK_CTRL)
9439 {
9440 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009441 if (first_tabpage->tp_next != NULL)
9442 {
9443 start_arrow(&curwin->w_cursor);
9444 goto_tabpage(0);
9445 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009446 return;
9447 }
9448#endif
9449
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 tpos = curwin->w_cursor;
9451 if (onepage(FORWARD, 1L) == OK)
9452 {
9453 start_arrow(&tpos);
9454#ifdef FEAT_CINDENT
9455 can_cindent = TRUE;
9456#endif
9457 }
9458 else
9459 vim_beep();
9460}
9461
9462#ifdef FEAT_DND
9463 static void
9464ins_drop()
9465{
9466 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9467}
9468#endif
9469
9470/*
9471 * Handle TAB in Insert or Replace mode.
9472 * Return TRUE when the TAB needs to be inserted like a normal character.
9473 */
9474 static int
9475ins_tab()
9476{
9477 int ind;
9478 int i;
9479 int temp;
9480
9481 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9482 Insstart_blank_vcol = get_nolist_virtcol();
9483 if (echeck_abbr(TAB + ABBR_OFF))
9484 return FALSE;
9485
9486 ind = inindent(0);
9487#ifdef FEAT_CINDENT
9488 if (ind)
9489 can_cindent = FALSE;
9490#endif
9491
9492 /*
9493 * When nothing special, insert TAB like a normal character
9494 */
9495 if (!curbuf->b_p_et
9496 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9497 && curbuf->b_p_sts == 0)
9498 return TRUE;
9499
9500 if (stop_arrow() == FAIL)
9501 return TRUE;
9502
9503 did_ai = FALSE;
9504#ifdef FEAT_SMARTINDENT
9505 did_si = FALSE;
9506 can_si = FALSE;
9507 can_si_back = FALSE;
9508#endif
9509 AppendToRedobuff((char_u *)"\t");
9510
9511 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9512 temp = (int)curbuf->b_p_sw;
9513 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9514 temp = (int)curbuf->b_p_sts;
9515 else /* otherwise use 'tabstop' */
9516 temp = (int)curbuf->b_p_ts;
9517 temp -= get_nolist_virtcol() % temp;
9518
9519 /*
9520 * Insert the first space with ins_char(). It will delete one char in
9521 * replace mode. Insert the rest with ins_str(); it will not delete any
9522 * chars. For VREPLACE mode, we use ins_char() for all characters.
9523 */
9524 ins_char(' ');
9525 while (--temp > 0)
9526 {
9527#ifdef FEAT_VREPLACE
9528 if (State & VREPLACE_FLAG)
9529 ins_char(' ');
9530 else
9531#endif
9532 {
9533 ins_str((char_u *)" ");
9534 if (State & REPLACE_FLAG) /* no char replaced */
9535 replace_push(NUL);
9536 }
9537 }
9538
9539 /*
9540 * When 'expandtab' not set: Replace spaces by TABs where possible.
9541 */
9542 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9543 {
9544 char_u *ptr;
9545#ifdef FEAT_VREPLACE
9546 char_u *saved_line = NULL; /* init for GCC */
9547 pos_T pos;
9548#endif
9549 pos_T fpos;
9550 pos_T *cursor;
9551 colnr_T want_vcol, vcol;
9552 int change_col = -1;
9553 int save_list = curwin->w_p_list;
9554
9555 /*
9556 * Get the current line. For VREPLACE mode, don't make real changes
9557 * yet, just work on a copy of the line.
9558 */
9559#ifdef FEAT_VREPLACE
9560 if (State & VREPLACE_FLAG)
9561 {
9562 pos = curwin->w_cursor;
9563 cursor = &pos;
9564 saved_line = vim_strsave(ml_get_curline());
9565 if (saved_line == NULL)
9566 return FALSE;
9567 ptr = saved_line + pos.col;
9568 }
9569 else
9570#endif
9571 {
9572 ptr = ml_get_cursor();
9573 cursor = &curwin->w_cursor;
9574 }
9575
9576 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9577 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9578 curwin->w_p_list = FALSE;
9579
9580 /* Find first white before the cursor */
9581 fpos = curwin->w_cursor;
9582 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9583 {
9584 --fpos.col;
9585 --ptr;
9586 }
9587
9588 /* In Replace mode, don't change characters before the insert point. */
9589 if ((State & REPLACE_FLAG)
9590 && fpos.lnum == Insstart.lnum
9591 && fpos.col < Insstart.col)
9592 {
9593 ptr += Insstart.col - fpos.col;
9594 fpos.col = Insstart.col;
9595 }
9596
9597 /* compute virtual column numbers of first white and cursor */
9598 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9599 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9600
9601 /* Use as many TABs as possible. Beware of 'showbreak' and
9602 * 'linebreak' adding extra virtual columns. */
9603 while (vim_iswhite(*ptr))
9604 {
9605 i = lbr_chartabsize((char_u *)"\t", vcol);
9606 if (vcol + i > want_vcol)
9607 break;
9608 if (*ptr != TAB)
9609 {
9610 *ptr = TAB;
9611 if (change_col < 0)
9612 {
9613 change_col = fpos.col; /* Column of first change */
9614 /* May have to adjust Insstart */
9615 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9616 Insstart.col = fpos.col;
9617 }
9618 }
9619 ++fpos.col;
9620 ++ptr;
9621 vcol += i;
9622 }
9623
9624 if (change_col >= 0)
9625 {
9626 int repl_off = 0;
9627
9628 /* Skip over the spaces we need. */
9629 while (vcol < want_vcol && *ptr == ' ')
9630 {
9631 vcol += lbr_chartabsize(ptr, vcol);
9632 ++ptr;
9633 ++repl_off;
9634 }
9635 if (vcol > want_vcol)
9636 {
9637 /* Must have a char with 'showbreak' just before it. */
9638 --ptr;
9639 --repl_off;
9640 }
9641 fpos.col += repl_off;
9642
9643 /* Delete following spaces. */
9644 i = cursor->col - fpos.col;
9645 if (i > 0)
9646 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009647 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 /* correct replace stack. */
9649 if ((State & REPLACE_FLAG)
9650#ifdef FEAT_VREPLACE
9651 && !(State & VREPLACE_FLAG)
9652#endif
9653 )
9654 for (temp = i; --temp >= 0; )
9655 replace_join(repl_off);
9656 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009657#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009658 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009659 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009660 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009661 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9662 (char_u *)"\t", 1);
9663 }
9664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 cursor->col -= i;
9666
9667#ifdef FEAT_VREPLACE
9668 /*
9669 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9670 * backspacing over the changed spacing and then inserting the new
9671 * spacing.
9672 */
9673 if (State & VREPLACE_FLAG)
9674 {
9675 /* Backspace from real cursor to change_col */
9676 backspace_until_column(change_col);
9677
9678 /* Insert each char in saved_line from changed_col to
9679 * ptr-cursor */
9680 ins_bytes_len(saved_line + change_col,
9681 cursor->col - change_col);
9682 }
9683#endif
9684 }
9685
9686#ifdef FEAT_VREPLACE
9687 if (State & VREPLACE_FLAG)
9688 vim_free(saved_line);
9689#endif
9690 curwin->w_p_list = save_list;
9691 }
9692
9693 return FALSE;
9694}
9695
9696/*
9697 * Handle CR or NL in insert mode.
9698 * Return TRUE when out of memory or can't undo.
9699 */
9700 static int
9701ins_eol(c)
9702 int c;
9703{
9704 int i;
9705
9706 if (echeck_abbr(c + ABBR_OFF))
9707 return FALSE;
9708 if (stop_arrow() == FAIL)
9709 return TRUE;
9710 undisplay_dollar();
9711
9712 /*
9713 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9714 * character under the cursor. Only push a NUL on the replace stack,
9715 * nothing to put back when the NL is deleted.
9716 */
9717 if ((State & REPLACE_FLAG)
9718#ifdef FEAT_VREPLACE
9719 && !(State & VREPLACE_FLAG)
9720#endif
9721 )
9722 replace_push(NUL);
9723
9724 /*
9725 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9726 * replacing the next line, so we push all of the characters left on the
9727 * line onto the replace stack. This is not done here though, it is done
9728 * in open_line().
9729 */
9730
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009731#ifdef FEAT_VIRTUALEDIT
9732 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9733 * CTRL-O). */
9734 if (virtual_active() && curwin->w_cursor.coladd > 0)
9735 coladvance(getviscol());
9736#endif
9737
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#ifdef FEAT_RIGHTLEFT
9739# ifdef FEAT_FKMAP
9740 if (p_altkeymap && p_fkmap)
9741 fkmap(NL);
9742# endif
9743 /* NL in reverse insert will always start in the end of
9744 * current line. */
9745 if (revins_on)
9746 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9747#endif
9748
9749 AppendToRedobuff(NL_STR);
9750 i = open_line(FORWARD,
9751#ifdef FEAT_COMMENTS
9752 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9753#endif
9754 0, old_indent);
9755 old_indent = 0;
9756#ifdef FEAT_CINDENT
9757 can_cindent = TRUE;
9758#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009759#ifdef FEAT_FOLDING
9760 /* When inserting a line the cursor line must never be in a closed fold. */
9761 foldOpenCursor();
9762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
9764 return (!i);
9765}
9766
9767#ifdef FEAT_DIGRAPHS
9768/*
9769 * Handle digraph in insert mode.
9770 * Returns character still to be inserted, or NUL when nothing remaining to be
9771 * done.
9772 */
9773 static int
9774ins_digraph()
9775{
9776 int c;
9777 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009778 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779
9780 pc_status = PC_STATUS_UNSET;
9781 if (redrawing() && !char_avail())
9782 {
9783 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009784 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785
9786 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009787 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#ifdef FEAT_CMDL_INFO
9789 add_to_showcmd_c(Ctrl_K);
9790#endif
9791 }
9792
9793#ifdef USE_ON_FLY_SCROLL
9794 dont_scroll = TRUE; /* disallow scrolling here */
9795#endif
9796
9797 /* don't map the digraph chars. This also prevents the
9798 * mode message to be deleted when ESC is hit */
9799 ++no_mapping;
9800 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009801 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 --no_mapping;
9803 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009804 if (did_putchar)
9805 /* when the line fits in 'columns' the '?' is at the start of the next
9806 * line and will not be removed by the redraw */
9807 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009808
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 if (IS_SPECIAL(c) || mod_mask) /* special key */
9810 {
9811#ifdef FEAT_CMDL_INFO
9812 clear_showcmd();
9813#endif
9814 insert_special(c, TRUE, FALSE);
9815 return NUL;
9816 }
9817 if (c != ESC)
9818 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009819 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (redrawing() && !char_avail())
9821 {
9822 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009823 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824
9825 if (char2cells(c) == 1)
9826 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009827 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009829 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 }
9831#ifdef FEAT_CMDL_INFO
9832 add_to_showcmd_c(c);
9833#endif
9834 }
9835 ++no_mapping;
9836 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009837 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 --no_mapping;
9839 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009840 if (did_putchar)
9841 /* when the line fits in 'columns' the '?' is at the start of the
9842 * next line and will not be removed by a redraw */
9843 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 if (cc != ESC)
9845 {
9846 AppendToRedobuff((char_u *)CTRL_V_STR);
9847 c = getdigraph(c, cc, TRUE);
9848#ifdef FEAT_CMDL_INFO
9849 clear_showcmd();
9850#endif
9851 return c;
9852 }
9853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854#ifdef FEAT_CMDL_INFO
9855 clear_showcmd();
9856#endif
9857 return NUL;
9858}
9859#endif
9860
9861/*
9862 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9863 * Returns the char to be inserted, or NUL if none found.
9864 */
9865 static int
9866ins_copychar(lnum)
9867 linenr_T lnum;
9868{
9869 int c;
9870 int temp;
9871 char_u *ptr, *prev_ptr;
9872
9873 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9874 {
9875 vim_beep();
9876 return NUL;
9877 }
9878
9879 /* try to advance to the cursor column */
9880 temp = 0;
9881 ptr = ml_get(lnum);
9882 prev_ptr = ptr;
9883 validate_virtcol();
9884 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9885 {
9886 prev_ptr = ptr;
9887 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9888 }
9889 if ((colnr_T)temp > curwin->w_virtcol)
9890 ptr = prev_ptr;
9891
9892#ifdef FEAT_MBYTE
9893 c = (*mb_ptr2char)(ptr);
9894#else
9895 c = *ptr;
9896#endif
9897 if (c == NUL)
9898 vim_beep();
9899 return c;
9900}
9901
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009902/*
9903 * CTRL-Y or CTRL-E typed in Insert mode.
9904 */
9905 static int
9906ins_ctrl_ey(tc)
9907 int tc;
9908{
9909 int c = tc;
9910
9911#ifdef FEAT_INS_EXPAND
9912 if (ctrl_x_mode == CTRL_X_SCROLL)
9913 {
9914 if (c == Ctrl_Y)
9915 scrolldown_clamp();
9916 else
9917 scrollup_clamp();
9918 redraw_later(VALID);
9919 }
9920 else
9921#endif
9922 {
9923 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9924 if (c != NUL)
9925 {
9926 long tw_save;
9927
9928 /* The character must be taken literally, insert like it
9929 * was typed after a CTRL-V, and pretend 'textwidth'
9930 * wasn't set. Digits, 'o' and 'x' are special after a
9931 * CTRL-V, don't use it for these. */
9932 if (c < 256 && !isalnum(c))
9933 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9934 tw_save = curbuf->b_p_tw;
9935 curbuf->b_p_tw = -1;
9936 insert_special(c, TRUE, FALSE);
9937 curbuf->b_p_tw = tw_save;
9938#ifdef FEAT_RIGHTLEFT
9939 revins_chars++;
9940 revins_legal++;
9941#endif
9942 c = Ctrl_V; /* pretend CTRL-V is last character */
9943 auto_format(FALSE, TRUE);
9944 }
9945 }
9946 return c;
9947}
9948
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949#ifdef FEAT_SMARTINDENT
9950/*
9951 * Try to do some very smart auto-indenting.
9952 * Used when inserting a "normal" character.
9953 */
9954 static void
9955ins_try_si(c)
9956 int c;
9957{
9958 pos_T *pos, old_pos;
9959 char_u *ptr;
9960 int i;
9961 int temp;
9962
9963 /*
9964 * do some very smart indenting when entering '{' or '}'
9965 */
9966 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9967 {
9968 /*
9969 * for '}' set indent equal to indent of line containing matching '{'
9970 */
9971 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9972 {
9973 old_pos = curwin->w_cursor;
9974 /*
9975 * If the matching '{' has a ')' immediately before it (ignoring
9976 * white-space), then line up with the start of the line
9977 * containing the matching '(' if there is one. This handles the
9978 * case where an "if (..\n..) {" statement continues over multiple
9979 * lines -- webb
9980 */
9981 ptr = ml_get(pos->lnum);
9982 i = pos->col;
9983 if (i > 0) /* skip blanks before '{' */
9984 while (--i > 0 && vim_iswhite(ptr[i]))
9985 ;
9986 curwin->w_cursor.lnum = pos->lnum;
9987 curwin->w_cursor.col = i;
9988 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9989 curwin->w_cursor = *pos;
9990 i = get_indent();
9991 curwin->w_cursor = old_pos;
9992#ifdef FEAT_VREPLACE
9993 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009994 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 else
9996#endif
9997 (void)set_indent(i, SIN_CHANGED);
9998 }
9999 else if (curwin->w_cursor.col > 0)
10000 {
10001 /*
10002 * when inserting '{' after "O" reduce indent, but not
10003 * more than indent of previous line
10004 */
10005 temp = TRUE;
10006 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10007 {
10008 old_pos = curwin->w_cursor;
10009 i = get_indent();
10010 while (curwin->w_cursor.lnum > 1)
10011 {
10012 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10013
10014 /* ignore empty lines and lines starting with '#'. */
10015 if (*ptr != '#' && *ptr != NUL)
10016 break;
10017 }
10018 if (get_indent() >= i)
10019 temp = FALSE;
10020 curwin->w_cursor = old_pos;
10021 }
10022 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010023 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 }
10026
10027 /*
10028 * set indent of '#' always to 0
10029 */
10030 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10031 {
10032 /* remember current indent for next line */
10033 old_indent = get_indent();
10034 (void)set_indent(0, SIN_CHANGED);
10035 }
10036
10037 /* Adjust ai_col, the char at this position can be deleted. */
10038 if (ai_col > curwin->w_cursor.col)
10039 ai_col = curwin->w_cursor.col;
10040}
10041#endif
10042
10043/*
10044 * Get the value that w_virtcol would have when 'list' is off.
10045 * Unless 'cpo' contains the 'L' flag.
10046 */
10047 static colnr_T
10048get_nolist_virtcol()
10049{
10050 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10051 return getvcol_nolist(&curwin->w_cursor);
10052 validate_virtcol();
10053 return curwin->w_virtcol;
10054}