blob: f072986f23e3ac557e61d3abbdcda0ae5015436b [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 Moolenaar071d4272004-06-13 20:20:40 +0000166static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000167#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
168static void ins_compl_add_list __ARGS((list_T *list));
Bram Moolenaar82139082011-09-14 16:52:09 +0200169static void ins_compl_add_dict __ARGS((dict_T *dict));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000170#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000171static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void ins_compl_delete __ARGS((void));
173static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000174static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000175static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000176static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000177static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000178static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000180static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif /* FEAT_INS_EXPAND */
182
183#define BACKSPACE_CHAR 1
184#define BACKSPACE_WORD 2
185#define BACKSPACE_WORD_NOT_SPACE 3
186#define BACKSPACE_LINE 4
187
Bram Moolenaar754b5602006-02-09 23:53:20 +0000188static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189static void ins_ctrl_v __ARGS((void));
190static void undisplay_dollar __ARGS((void));
191static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000192static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static void check_auto_format __ARGS((int));
194static void redo_literal __ARGS((int c));
195static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000196#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000197static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000198static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000199static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
202static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static int replace_pop __ARGS((void));
204static void replace_join __ARGS((int off));
205static void replace_pop_ins __ARGS((void));
206#ifdef FEAT_MBYTE
207static void mb_replace_pop_ins __ARGS((int cc));
208#endif
209static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000210static void replace_do_bs __ARGS((int limit_col));
211static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifdef FEAT_CINDENT
213static int cindent_on __ARGS((void));
214#endif
215static void ins_reg __ARGS((void));
216static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000217static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000218static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#ifdef FEAT_RIGHTLEFT
220static void ins_ctrl_ __ARGS((void));
221#endif
222#ifdef FEAT_VISUAL
223static int ins_start_select __ARGS((int c));
224#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000225static void ins_insert __ARGS((int replaceState));
226static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227static void ins_shift __ARGS((int c, int lastc));
228static void ins_del __ARGS((void));
229static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
230#ifdef FEAT_MOUSE
231static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200232static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000234#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
235static void ins_tabline __ARGS((int c));
236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237static void ins_left __ARGS((void));
238static void ins_home __ARGS((int c));
239static void ins_end __ARGS((int c));
240static void ins_s_left __ARGS((void));
241static void ins_right __ARGS((void));
242static void ins_s_right __ARGS((void));
243static void ins_up __ARGS((int startcol));
244static void ins_pageup __ARGS((void));
245static void ins_down __ARGS((int startcol));
246static void ins_pagedown __ARGS((void));
247#ifdef FEAT_DND
248static void ins_drop __ARGS((void));
249#endif
250static int ins_tab __ARGS((void));
251static int ins_eol __ARGS((int c));
252#ifdef FEAT_DIGRAPHS
253static int ins_digraph __ARGS((void));
254#endif
255static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000256static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_SMARTINDENT
258static void ins_try_si __ARGS((int c));
259#endif
260static colnr_T get_nolist_virtcol __ARGS((void));
261
262static colnr_T Insstart_textlen; /* length of line when insert started */
263static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
264
265static char_u *last_insert = NULL; /* the text of the previous insert,
266 K_SPECIAL and CSI are escaped */
267static int last_insert_skip; /* nr of chars in front of previous insert */
268static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000269static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271#ifdef FEAT_CINDENT
272static int can_cindent; /* may do cindenting on this line */
273#endif
274
275static int old_indent = 0; /* for ^^D command in insert mode */
276
277#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000278static int revins_on; /* reverse insert mode on */
279static int revins_chars; /* how much to skip after edit */
280static int revins_legal; /* was the last char 'legal'? */
281static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#endif
283
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284static int ins_need_undo; /* call u_save() before inserting a
285 char. Set when edit() is called.
286 after that arrow_used is used. */
287
288static int did_add_space = FALSE; /* auto_format() added an extra space
289 under the cursor */
290
291/*
292 * edit(): Start inserting text.
293 *
294 * "cmdchar" can be:
295 * 'i' normal insert command
296 * 'a' normal append command
297 * 'R' replace command
298 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
299 * but still only one <CR> is inserted. The <Esc> is not used for redo.
300 * 'g' "gI" command.
301 * 'V' "gR" command for Virtual Replace mode.
302 * 'v' "gr" command for single character Virtual Replace mode.
303 *
304 * This function is not called recursively. For CTRL-O commands, it returns
305 * and lets the caller handle the Normal-mode command.
306 *
307 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
308 */
309 int
310edit(cmdchar, startln, count)
311 int cmdchar;
312 int startln; /* if set, insert at start of line */
313 long count;
314{
315 int c = 0;
316 char_u *ptr;
317 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000318 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 int i;
321 int did_backspace = TRUE; /* previous char was backspace */
322#ifdef FEAT_CINDENT
323 int line_is_white = FALSE; /* line is empty before insert */
324#endif
325 linenr_T old_topline = 0; /* topline before insertion */
326#ifdef FEAT_DIFF
327 int old_topfill = -1;
328#endif
329 int inserted_space = FALSE; /* just inserted a space */
330 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000331 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000333 /* Remember whether editing was restarted after CTRL-O. */
334 did_restart_edit = restart_edit;
335
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 /* sleep before redrawing, needed for "CTRL-O :" that results in an
337 * error message */
338 check_for_delay(TRUE);
339
340#ifdef HAVE_SANDBOX
341 /* Don't allow inserting in the sandbox. */
342 if (sandbox != 0)
343 {
344 EMSG(_(e_sandbox));
345 return FALSE;
346 }
347#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000348 /* Don't allow changes in the buffer while editing the cmdline. The
349 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000350 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000351 {
352 EMSG(_(e_secure));
353 return FALSE;
354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
356#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000357 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000358 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000359 {
360 EMSG(_(e_secure));
361 return FALSE;
362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 ins_compl_clear(); /* clear stuff for CTRL-X mode */
364#endif
365
Bram Moolenaar843ee412004-06-30 16:16:41 +0000366#ifdef FEAT_AUTOCMD
367 /*
368 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
369 */
370 if (cmdchar != 'r' && cmdchar != 'v')
371 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000372# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000373 if (cmdchar == 'R')
374 ptr = (char_u *)"r";
375 else if (cmdchar == 'V')
376 ptr = (char_u *)"v";
377 else
378 ptr = (char_u *)"i";
379 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000380# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000381 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
382 }
383#endif
384
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200385#ifdef FEAT_CONCEAL
386 /* Check if the cursor line needs redrawing before changing State. If
387 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200388 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200389#endif
390
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#ifdef FEAT_MOUSE
392 /*
393 * When doing a paste with the middle mouse button, Insstart is set to
394 * where the paste started.
395 */
396 if (where_paste_started.lnum != 0)
397 Insstart = where_paste_started;
398 else
399#endif
400 {
401 Insstart = curwin->w_cursor;
402 if (startln)
403 Insstart.col = 0;
404 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000405 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 Insstart_blank_vcol = MAXCOL;
407 if (!did_ai)
408 ai_col = 0;
409
410 if (cmdchar != NUL && restart_edit == 0)
411 {
412 ResetRedobuff();
413 AppendNumberToRedobuff(count);
414#ifdef FEAT_VREPLACE
415 if (cmdchar == 'V' || cmdchar == 'v')
416 {
417 /* "gR" or "gr" command */
418 AppendCharToRedobuff('g');
419 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
420 }
421 else
422#endif
423 {
424 AppendCharToRedobuff(cmdchar);
425 if (cmdchar == 'g') /* "gI" command */
426 AppendCharToRedobuff('I');
427 else if (cmdchar == 'r') /* "r<CR>" command */
428 count = 1; /* insert only one <CR> */
429 }
430 }
431
432 if (cmdchar == 'R')
433 {
434#ifdef FEAT_FKMAP
435 if (p_fkmap && p_ri)
436 {
437 beep_flush();
438 EMSG(farsi_text_3); /* encoded in Farsi */
439 State = INSERT;
440 }
441 else
442#endif
443 State = REPLACE;
444 }
445#ifdef FEAT_VREPLACE
446 else if (cmdchar == 'V' || cmdchar == 'v')
447 {
448 State = VREPLACE;
449 replaceState = VREPLACE;
450 orig_line_count = curbuf->b_ml.ml_line_count;
451 vr_lines_changed = 1;
452 }
453#endif
454 else
455 State = INSERT;
456
457 stop_insert_mode = FALSE;
458
459 /*
460 * Need to recompute the cursor position, it might move when the cursor is
461 * on a TAB or special character.
462 */
463 curs_columns(TRUE);
464
465 /*
466 * Enable langmap or IME, indicated by 'iminsert'.
467 * Note that IME may enabled/disabled without us noticing here, thus the
468 * 'iminsert' value may not reflect what is actually used. It is updated
469 * when hitting <Esc>.
470 */
471 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
472 State |= LANGMAP;
473#ifdef USE_IM_CONTROL
474 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
475#endif
476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477#ifdef FEAT_MOUSE
478 setmouse();
479#endif
480#ifdef FEAT_CMDL_INFO
481 clear_showcmd();
482#endif
483#ifdef FEAT_RIGHTLEFT
484 /* there is no reverse replace mode */
485 revins_on = (State == INSERT && p_ri);
486 if (revins_on)
487 undisplay_dollar();
488 revins_chars = 0;
489 revins_legal = 0;
490 revins_scol = -1;
491#endif
492
493 /*
494 * Handle restarting Insert mode.
495 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
496 * restart_edit non-zero, and something in the stuff buffer.
497 */
498 if (restart_edit != 0 && stuff_empty())
499 {
500#ifdef FEAT_MOUSE
501 /*
502 * After a paste we consider text typed to be part of the insert for
503 * the pasted text. You can backspace over the pasted text too.
504 */
505 if (where_paste_started.lnum)
506 arrow_used = FALSE;
507 else
508#endif
509 arrow_used = TRUE;
510 restart_edit = 0;
511
512 /*
513 * If the cursor was after the end-of-line before the CTRL-O and it is
514 * now at the end-of-line, put it after the end-of-line (this is not
515 * correct in very rare cases).
516 * Also do this if curswant is greater than the current virtual
517 * column. Eg after "^O$" or "^O80|".
518 */
519 validate_virtcol();
520 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000521 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 || curwin->w_curswant > curwin->w_virtcol)
523 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
524 {
525 if (ptr[1] == NUL)
526 ++curwin->w_cursor.col;
527#ifdef FEAT_MBYTE
528 else if (has_mbyte)
529 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000530 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 if (ptr[i] == NUL)
532 curwin->w_cursor.col += i;
533 }
534#endif
535 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000536 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
538 else
539 arrow_used = FALSE;
540
541 /* we are in insert mode now, don't need to start it anymore */
542 need_start_insertmode = FALSE;
543
544 /* Need to save the line for undo before inserting the first char. */
545 ins_need_undo = TRUE;
546
547#ifdef FEAT_MOUSE
548 where_paste_started.lnum = 0;
549#endif
550#ifdef FEAT_CINDENT
551 can_cindent = TRUE;
552#endif
553#ifdef FEAT_FOLDING
554 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
555 * restarting. */
556 if (!p_im && did_restart_edit == 0)
557 foldOpenCursor();
558#endif
559
560 /*
561 * If 'showmode' is set, show the current (insert/replace/..) mode.
562 * A warning message for changing a readonly file is given here, before
563 * actually changing anything. It's put after the mode, if any.
564 */
565 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000566 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 i = showmode();
568
569 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000570 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572#ifdef CURSOR_SHAPE
573 ui_cursor_shape(); /* may show different cursor shape */
574#endif
575#ifdef FEAT_DIGRAPHS
576 do_digraph(-1); /* clear digraphs */
577#endif
578
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000579 /*
580 * Get the current length of the redo buffer, those characters have to be
581 * skipped if we want to get to the inserted characters.
582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 ptr = get_inserted();
584 if (ptr == NULL)
585 new_insert_skip = 0;
586 else
587 {
588 new_insert_skip = (int)STRLEN(ptr);
589 vim_free(ptr);
590 }
591
592 old_indent = 0;
593
594 /*
595 * Main loop in Insert mode: repeat until Insert mode is left.
596 */
597 for (;;)
598 {
599#ifdef FEAT_RIGHTLEFT
600 if (!revins_legal)
601 revins_scol = -1; /* reset on illegal motions */
602 else
603 revins_legal = 0;
604#endif
605 if (arrow_used) /* don't repeat insert when arrow key used */
606 count = 0;
607
608 if (stop_insert_mode)
609 {
610 /* ":stopinsert" used or 'insertmode' reset */
611 count = 0;
612 goto doESCkey;
613 }
614
615 /* set curwin->w_curswant for next K_DOWN or K_UP */
616 if (!arrow_used)
617 curwin->w_set_curswant = TRUE;
618
619 /* If there is no typeahead may check for timestamps (e.g., for when a
620 * menu invoked a shell command). */
621 if (stuff_empty())
622 {
623 did_check_timestamps = FALSE;
624 if (need_check_timestamps)
625 check_timestamps(FALSE);
626 }
627
628 /*
629 * When emsg() was called msg_scroll will have been set.
630 */
631 msg_scroll = FALSE;
632
633#ifdef FEAT_GUI
634 /* When 'mousefocus' is set a mouse movement may have taken us to
635 * another window. "need_mouse_correct" may then be set because of an
636 * autocommand. */
637 if (need_mouse_correct)
638 gui_mouse_correct();
639#endif
640
641#ifdef FEAT_FOLDING
642 /* Open fold at the cursor line, according to 'foldopen'. */
643 if (fdo_flags & FDO_INSERT)
644 foldOpenCursor();
645 /* Close folds where the cursor isn't, according to 'foldclose' */
646 if (!char_avail())
647 foldCheckClose();
648#endif
649
650 /*
651 * If we inserted a character at the last position of the last line in
652 * the window, scroll the window one line up. This avoids an extra
653 * redraw.
654 * This is detected when the cursor column is smaller after inserting
655 * something.
656 * Don't do this when the topline changed already, it has
657 * already been adjusted (by insertchar() calling open_line())).
658 */
659 if (curbuf->b_mod_set
660 && curwin->w_p_wrap
661 && !did_backspace
662 && curwin->w_topline == old_topline
663#ifdef FEAT_DIFF
664 && curwin->w_topfill == old_topfill
665#endif
666 )
667 {
668 mincol = curwin->w_wcol;
669 validate_cursor_col();
670
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000671 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 && curwin->w_wrow == W_WINROW(curwin)
673 + curwin->w_height - 1 - p_so
674 && (curwin->w_cursor.lnum != curwin->w_topline
675#ifdef FEAT_DIFF
676 || curwin->w_topfill > 0
677#endif
678 ))
679 {
680#ifdef FEAT_DIFF
681 if (curwin->w_topfill > 0)
682 --curwin->w_topfill;
683 else
684#endif
685#ifdef FEAT_FOLDING
686 if (hasFolding(curwin->w_topline, NULL, &old_topline))
687 set_topline(curwin, old_topline + 1);
688 else
689#endif
690 set_topline(curwin, curwin->w_topline + 1);
691 }
692 }
693
694 /* May need to adjust w_topline to show the cursor. */
695 update_topline();
696
697 did_backspace = FALSE;
698
699 validate_cursor(); /* may set must_redraw */
700
701 /*
702 * Redraw the display when no characters are waiting.
703 * Also shows mode, ruler and positions cursor.
704 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000705 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
707#ifdef FEAT_SCROLLBIND
708 if (curwin->w_p_scb)
709 do_check_scrollbind(TRUE);
710#endif
711
Bram Moolenaar860cae12010-06-05 23:22:07 +0200712#ifdef FEAT_CURSORBIND
713 if (curwin->w_p_crb)
714 do_check_cursorbind();
715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 update_curswant();
717 old_topline = curwin->w_topline;
718#ifdef FEAT_DIFF
719 old_topfill = curwin->w_topfill;
720#endif
721
722#ifdef USE_ON_FLY_SCROLL
723 dont_scroll = FALSE; /* allow scrolling here */
724#endif
725
726 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000727 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
729 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000730 do
731 {
732 c = safe_vgetc();
733 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000735#ifdef FEAT_AUTOCMD
736 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
737 did_cursorhold = TRUE;
738#endif
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740#ifdef FEAT_RIGHTLEFT
741 if (p_hkmap && KeyTyped)
742 c = hkmap(c); /* Hebrew mode mapping */
743#endif
744#ifdef FEAT_FKMAP
745 if (p_fkmap && KeyTyped)
746 c = fkmap(c); /* Farsi mode mapping */
747#endif
748
749#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000750 /*
751 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000752 * and the cursor is still in the completed word. Only when there is
753 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000754 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000755 if (compl_started
756 && pum_wanted()
757 && curwin->w_cursor.col >= compl_col
758 && (compl_shown_match == NULL
759 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000760 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000761 /* BS: Delete one character from "compl_leader". */
762 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000763 && curwin->w_cursor.col > compl_col
764 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000765 continue;
766
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000767 /* When no match was selected or it was edited. */
768 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000769 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000770 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000771 * "compl_leader". Except when at the original match and
772 * there is nothing to add, CTRL-L works like CTRL-P then. */
773 if (c == Ctrl_L
774 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000775 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000776 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000777 {
778 ins_compl_addfrommatch();
779 continue;
780 }
781
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000782 /* A non-white character that fits in with the current
783 * completion: Add to "compl_leader". */
784 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000785 {
786 ins_compl_addleader(c);
787 continue;
788 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000789
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000790 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000791 * compl_enter_selects is set the Enter key does the same. */
792 if (c == Ctrl_Y || (compl_enter_selects
793 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000794 {
795 ins_compl_delete();
796 ins_compl_insert();
797 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000798 }
799 }
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
802 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000803 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000804 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000805 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806#endif
807
Bram Moolenaar488c6512005-08-11 20:09:58 +0000808 /* CTRL-\ CTRL-N goes to Normal mode,
809 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
810 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (c == Ctrl_BSL)
812 {
813 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000814 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 ++no_mapping;
816 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000817 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 --no_mapping;
819 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000820 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000822 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 vungetc(c);
824 c = Ctrl_BSL;
825 }
826 else if (c == Ctrl_G && p_im)
827 continue;
828 else
829 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000830 if (c == Ctrl_O)
831 {
832 ins_ctrl_o();
833 ins_at_eol = FALSE; /* cursor keeps its column */
834 nomove = TRUE;
835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 count = 0;
837 goto doESCkey;
838 }
839 }
840
841#ifdef FEAT_DIGRAPHS
842 c = do_digraph(c);
843#endif
844
845#ifdef FEAT_INS_EXPAND
846 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
847 goto docomplete;
848#endif
849 if (c == Ctrl_V || c == Ctrl_Q)
850 {
851 ins_ctrl_v();
852 c = Ctrl_V; /* pretend CTRL-V is last typed character */
853 continue;
854 }
855
856#ifdef FEAT_CINDENT
857 if (cindent_on()
858# ifdef FEAT_INS_EXPAND
859 && ctrl_x_mode == 0
860# endif
861 )
862 {
863 /* A key name preceded by a bang means this key is not to be
864 * inserted. Skip ahead to the re-indenting below.
865 * A key name preceded by a star means that indenting has to be
866 * done before inserting the key. */
867 line_is_white = inindent(0);
868 if (in_cinkeys(c, '!', line_is_white))
869 goto force_cindent;
870 if (can_cindent && in_cinkeys(c, '*', line_is_white)
871 && stop_arrow() == OK)
872 do_c_expr_indent();
873 }
874#endif
875
876#ifdef FEAT_RIGHTLEFT
877 if (curwin->w_p_rl)
878 switch (c)
879 {
880 case K_LEFT: c = K_RIGHT; break;
881 case K_S_LEFT: c = K_S_RIGHT; break;
882 case K_C_LEFT: c = K_C_RIGHT; break;
883 case K_RIGHT: c = K_LEFT; break;
884 case K_S_RIGHT: c = K_S_LEFT; break;
885 case K_C_RIGHT: c = K_C_LEFT; break;
886 }
887#endif
888
889#ifdef FEAT_VISUAL
890 /*
891 * If 'keymodel' contains "startsel", may start selection. If it
892 * does, a CTRL-O and c will be stuffed, we need to get these
893 * characters.
894 */
895 if (ins_start_select(c))
896 continue;
897#endif
898
899 /*
900 * The big switch to handle a character in insert mode.
901 */
902 switch (c)
903 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000904 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (echeck_abbr(ESC + ABBR_OFF))
906 break;
907 /*FALLTHROUGH*/
908
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000909 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#ifdef FEAT_CMDWIN
911 if (c == Ctrl_C && cmdwin_type != 0)
912 {
913 /* Close the cmdline window. */
914 cmdwin_result = K_IGNORE;
915 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000916 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 goto doESCkey;
918 }
919#endif
920
921#ifdef UNIX
922do_intr:
923#endif
924 /* when 'insertmode' set, and not halfway a mapping, don't leave
925 * Insert mode */
926 if (goto_im())
927 {
928 if (got_int)
929 {
930 (void)vgetc(); /* flush all buffers */
931 got_int = FALSE;
932 }
933 else
934 vim_beep();
935 break;
936 }
937doESCkey:
938 /*
939 * This is the ONLY return from edit()!
940 */
941 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
942 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000943 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 o_lnum = curwin->w_cursor.lnum;
945
Bram Moolenaar488c6512005-08-11 20:09:58 +0000946 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000947 {
948#ifdef FEAT_AUTOCMD
949 if (cmdchar != 'r' && cmdchar != 'v')
950 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
951 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000952 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 continue;
957
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000958 case Ctrl_Z: /* suspend when 'insertmode' set */
959 if (!p_im)
960 goto normalchar; /* insert CTRL-Z as normal char */
961 stuffReadbuff((char_u *)":st\r");
962 c = Ctrl_O;
963 /*FALLTHROUGH*/
964
965 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000966#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000967 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000968 goto docomplete;
969#endif
970 if (echeck_abbr(Ctrl_O + ABBR_OFF))
971 break;
972 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000973
974#ifdef FEAT_VIRTUALEDIT
975 /* don't move the cursor left when 'virtualedit' has "onemore". */
976 if (ve_flags & VE_ONEMORE)
977 {
978 ins_at_eol = FALSE;
979 nomove = TRUE;
980 }
981#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000982 count = 0;
983 goto doESCkey;
984
Bram Moolenaar572cb562005-08-05 21:35:02 +0000985 case K_INS: /* toggle insert/replace mode */
986 case K_KINS:
987 ins_insert(replaceState);
988 break;
989
990 case K_SELECT: /* end of Select mode mapping - ignore */
991 break;
992
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000993#ifdef FEAT_SNIFF
994 case K_SNIFF: /* Sniff command received */
995 stuffcharReadbuff(K_SNIFF);
996 goto doESCkey;
997#endif
998
999 case K_HELP: /* Help key works like <ESC> <Help> */
1000 case K_F1:
1001 case K_XF1:
1002 stuffcharReadbuff(K_HELP);
1003 if (p_im)
1004 need_start_insertmode = TRUE;
1005 goto doESCkey;
1006
1007#ifdef FEAT_NETBEANS_INTG
1008 case K_F21: /* NetBeans command */
1009 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001010 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 --no_mapping;
1012 netbeans_keycommand(i);
1013 break;
1014#endif
1015
1016 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 case NUL:
1018 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001019 /* For ^@ the trailing ESC will end the insert, unless there is an
1020 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1022 && c != Ctrl_A && !p_im)
1023 goto doESCkey; /* quit insert mode */
1024 inserted_space = FALSE;
1025 break;
1026
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 ins_reg();
1029 auto_format(FALSE, TRUE);
1030 inserted_space = FALSE;
1031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 ins_ctrl_g();
1035 break;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_HAT: /* switch input mode and/or langmap */
1038 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 break;
1040
1041#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001042 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (!p_ari)
1044 goto normalchar;
1045 ins_ctrl_();
1046 break;
1047#endif
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1051 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1052 goto docomplete;
1053#endif
1054 /* FALLTHROUGH */
1055
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001056 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057# ifdef FEAT_INS_EXPAND
1058 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1059 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 if (has_compl_option(FALSE))
1061 goto docomplete;
1062 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064# endif
1065 ins_shift(c, lastc);
1066 auto_format(FALSE, TRUE);
1067 inserted_space = FALSE;
1068 break;
1069
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001070 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 case K_KDEL:
1072 ins_del();
1073 auto_format(FALSE, TRUE);
1074 break;
1075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001076 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 case Ctrl_H:
1078 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1079 auto_format(FALSE, TRUE);
1080 break;
1081
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001082 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1084 auto_format(FALSE, TRUE);
1085 break;
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001088# ifdef FEAT_COMPL_FUNC
1089 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001090 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001091 goto docomplete;
1092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1094 auto_format(FALSE, TRUE);
1095 inserted_space = FALSE;
1096 break;
1097
1098#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 case K_LEFTMOUSE_NM:
1101 case K_LEFTDRAG:
1102 case K_LEFTRELEASE:
1103 case K_LEFTRELEASE_NM:
1104 case K_MIDDLEMOUSE:
1105 case K_MIDDLEDRAG:
1106 case K_MIDDLERELEASE:
1107 case K_RIGHTMOUSE:
1108 case K_RIGHTDRAG:
1109 case K_RIGHTRELEASE:
1110 case K_X1MOUSE:
1111 case K_X1DRAG:
1112 case K_X1RELEASE:
1113 case K_X2MOUSE:
1114 case K_X2DRAG:
1115 case K_X2RELEASE:
1116 ins_mouse(c);
1117 break;
1118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001120 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 break;
1122
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001124 ins_mousescroll(MSCR_UP);
1125 break;
1126
1127 case K_MOUSELEFT: /* Scroll wheel left */
1128 ins_mousescroll(MSCR_LEFT);
1129 break;
1130
1131 case K_MOUSERIGHT: /* Scroll wheel right */
1132 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 break;
1134#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001135#ifdef FEAT_GUI_TABLINE
1136 case K_TABLINE:
1137 case K_TABMENU:
1138 ins_tabline(c);
1139 break;
1140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 break;
1144
Bram Moolenaar754b5602006-02-09 23:53:20 +00001145#ifdef FEAT_AUTOCMD
1146 case K_CURSORHOLD: /* Didn't type something for a while. */
1147 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1148 did_cursorhold = TRUE;
1149 break;
1150#endif
1151
Bram Moolenaar4770d092006-01-12 23:22:24 +00001152#ifdef FEAT_GUI_W32
1153 /* On Win32 ignore <M-F4>, we get it when closing the window was
1154 * cancelled. */
1155 case K_F4:
1156 if (mod_mask != MOD_MASK_ALT)
1157 goto normalchar;
1158 break;
1159#endif
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161#ifdef FEAT_GUI
1162 case K_VER_SCROLLBAR:
1163 ins_scroll();
1164 break;
1165
1166 case K_HOR_SCROLLBAR:
1167 ins_horscroll();
1168 break;
1169#endif
1170
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_S_HOME:
1174 case K_C_HOME:
1175 ins_home(c);
1176 break;
1177
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001178 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_S_END:
1181 case K_C_END:
1182 ins_end(c);
1183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001186 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1187 ins_s_left();
1188 else
1189 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 case K_C_LEFT:
1194 ins_s_left();
1195 break;
1196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001198 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1199 ins_s_right();
1200 else
1201 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 break;
1203
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 case K_C_RIGHT:
1206 ins_s_right();
1207 break;
1208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001210#ifdef FEAT_INS_EXPAND
1211 if (pum_visible())
1212 goto docomplete;
1213#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001214 if (mod_mask & MOD_MASK_SHIFT)
1215 ins_pageup();
1216 else
1217 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 break;
1219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 case K_PAGEUP:
1222 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001223#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001224 if (pum_visible())
1225 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 ins_pageup();
1228 break;
1229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001230 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001231#ifdef FEAT_INS_EXPAND
1232 if (pum_visible())
1233 goto docomplete;
1234#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001235 if (mod_mask & MOD_MASK_SHIFT)
1236 ins_pagedown();
1237 else
1238 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case K_PAGEDOWN:
1243 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001244#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001245 if (pum_visible())
1246 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ins_pagedown();
1249 break;
1250
1251#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001252 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 ins_drop();
1254 break;
1255#endif
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 c = TAB;
1259 /* FALLTHROUGH */
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1263 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1264 goto docomplete;
1265#endif
1266 inserted_space = FALSE;
1267 if (ins_tab())
1268 goto normalchar; /* insert TAB as a normal char */
1269 auto_format(FALSE, TRUE);
1270 break;
1271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001272 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 c = CAR;
1274 /* FALLTHROUGH */
1275 case CAR:
1276 case NL:
1277#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1278 /* In a quickfix window a <CR> jumps to the error under the
1279 * cursor. */
1280 if (bt_quickfix(curbuf) && c == CAR)
1281 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001282 if (curwin->w_llist_ref == NULL) /* quickfix window */
1283 do_cmdline_cmd((char_u *)".cc");
1284 else /* location list window */
1285 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 break;
1287 }
1288#endif
1289#ifdef FEAT_CMDWIN
1290 if (cmdwin_type != 0)
1291 {
1292 /* Execute the command in the cmdline window. */
1293 cmdwin_result = CAR;
1294 goto doESCkey;
1295 }
1296#endif
1297 if (ins_eol(c) && !p_im)
1298 goto doESCkey; /* out of memory */
1299 auto_format(FALSE, FALSE);
1300 inserted_space = FALSE;
1301 break;
1302
Bram Moolenaar860cae12010-06-05 23:22:07 +02001303#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305# ifdef FEAT_INS_EXPAND
1306 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1307 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 if (has_compl_option(TRUE))
1309 goto docomplete;
1310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312# endif
1313# ifdef FEAT_DIGRAPHS
1314 c = ins_digraph();
1315 if (c == NUL)
1316 break;
1317# endif
1318 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001322 case Ctrl_X: /* Enter CTRL-X mode */
1323 ins_ctrl_x();
1324 break;
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (ctrl_x_mode != CTRL_X_TAGS)
1328 goto normalchar;
1329 goto docomplete;
1330
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (ctrl_x_mode != CTRL_X_FILES)
1333 goto normalchar;
1334 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001335
1336 case 's': /* Spelling completion after ^X */
1337 case Ctrl_S:
1338 if (ctrl_x_mode != CTRL_X_SPELL)
1339 goto normalchar;
1340 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#endif
1342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344#ifdef FEAT_INS_EXPAND
1345 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1346#endif
1347 {
1348 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1349 if (p_im)
1350 {
1351 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1352 break;
1353 goto doESCkey;
1354 }
1355 goto normalchar;
1356 }
1357#ifdef FEAT_INS_EXPAND
1358 /* FALLTHROUGH */
1359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 case Ctrl_N:
1362 /* if 'complete' is empty then plain ^P is no longer special,
1363 * but it is under other ^X modes */
1364 if (*curbuf->b_p_cpt == NUL
1365 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001366 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 goto normalchar;
1368
1369docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001370 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001372 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001373 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 break;
1375#endif /* FEAT_INS_EXPAND */
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case Ctrl_Y: /* copy from previous line or scroll down */
1378 case Ctrl_E: /* copy from next line or scroll up */
1379 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 break;
1381
1382 default:
1383#ifdef UNIX
1384 if (c == intr_char) /* special interrupt char */
1385 goto do_intr;
1386#endif
1387
Bram Moolenaare659c952011-05-19 17:25:41 +02001388normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 /*
1390 * Insert a nomal character.
1391 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001392#ifdef FEAT_AUTOCMD
1393 if (!p_paste)
1394 {
1395 /* Trigger the InsertCharPre event. Lock the text to avoid
1396 * weird things from happening. */
1397 set_vim_var_char(c);
1398 ++textlock;
1399 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
1400 FALSE, curbuf))
1401 {
1402 /* Get the new value of v:char. If it is more than one
1403 * character insert it literally. */
1404 char_u *s = get_vim_var_str(VV_CHAR);
1405 if (MB_CHARLEN(s) > 1)
1406 {
1407 if (stop_arrow() != FAIL)
1408 {
1409 ins_str(s);
1410 AppendToRedobuffLit(s, -1);
1411 }
1412 c = NUL;
1413 }
1414 else
1415 c = PTR2CHAR(s);
1416 }
1417
1418 set_vim_var_string(VV_CHAR, NULL, -1);
1419 --textlock;
1420
1421 /* If the new value is an empty string then don't insert a
1422 * char. */
1423 if (c == NUL)
1424 break;
1425 }
1426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#ifdef FEAT_SMARTINDENT
1428 /* Try to perform smart-indenting. */
1429 ins_try_si(c);
1430#endif
1431
1432 if (c == ' ')
1433 {
1434 inserted_space = TRUE;
1435#ifdef FEAT_CINDENT
1436 if (inindent(0))
1437 can_cindent = FALSE;
1438#endif
1439 if (Insstart_blank_vcol == MAXCOL
1440 && curwin->w_cursor.lnum == Insstart.lnum)
1441 Insstart_blank_vcol = get_nolist_virtcol();
1442 }
1443
1444 if (vim_iswordc(c) || !echeck_abbr(
1445#ifdef FEAT_MBYTE
1446 /* Add ABBR_OFF for characters above 0x100, this is
1447 * what check_abbr() expects. */
1448 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1449#endif
1450 c))
1451 {
1452 insert_special(c, FALSE, FALSE);
1453#ifdef FEAT_RIGHTLEFT
1454 revins_legal++;
1455 revins_chars++;
1456#endif
1457 }
1458
1459 auto_format(FALSE, TRUE);
1460
1461#ifdef FEAT_FOLDING
1462 /* When inserting a character the cursor line must never be in a
1463 * closed fold. */
1464 foldOpenCursor();
1465#endif
1466 break;
1467 } /* end of switch (c) */
1468
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001469#ifdef FEAT_AUTOCMD
1470 /* If typed something may trigger CursorHoldI again. */
1471 if (c != K_CURSORHOLD)
1472 did_cursorhold = FALSE;
1473#endif
1474
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 /* If the cursor was moved we didn't just insert a space */
1476 if (arrow_used)
1477 inserted_space = FALSE;
1478
1479#ifdef FEAT_CINDENT
1480 if (can_cindent && cindent_on()
1481# ifdef FEAT_INS_EXPAND
1482 && ctrl_x_mode == 0
1483# endif
1484 )
1485 {
1486force_cindent:
1487 /*
1488 * Indent now if a key was typed that is in 'cinkeys'.
1489 */
1490 if (in_cinkeys(c, ' ', line_is_white))
1491 {
1492 if (stop_arrow() == OK)
1493 /* re-indent the current line */
1494 do_c_expr_indent();
1495 }
1496 }
1497#endif /* FEAT_CINDENT */
1498
1499 } /* for (;;) */
1500 /* NOTREACHED */
1501}
1502
1503/*
1504 * Redraw for Insert mode.
1505 * This is postponed until getting the next character to make '$' in the 'cpo'
1506 * option work correctly.
1507 * Only redraw when there are no characters available. This speeds up
1508 * inserting sequences of characters (e.g., for CTRL-R).
1509 */
1510 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001511ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001512 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001514#ifdef FEAT_CONCEAL
1515 linenr_T conceal_old_cursor_line = 0;
1516 linenr_T conceal_new_cursor_line = 0;
1517 int conceal_update_lines = FALSE;
1518#endif
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (!char_avail())
1521 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001522#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001523 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1524 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001525 if (ready && (
1526# ifdef FEAT_AUTOCMD
1527 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001528# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001529# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1530 ||
1531# endif
1532# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001533 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001534# endif
1535 )
1536 && !equalpos(last_cursormoved, curwin->w_cursor)
1537# ifdef FEAT_INS_EXPAND
1538 && !pum_visible()
1539# endif
1540 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001541 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001542# ifdef FEAT_SYN_HL
1543 /* Need to update the screen first, to make sure syntax
1544 * highlighting is correct after making a change (e.g., inserting
1545 * a "(". The autocommand may also require a redraw, so it's done
1546 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001547 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001548 update_screen(0);
1549# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001550# ifdef FEAT_AUTOCMD
1551 if (has_cursormovedI())
1552 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1553# endif
1554# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001555 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001556 {
1557 conceal_old_cursor_line = last_cursormoved.lnum;
1558 conceal_new_cursor_line = curwin->w_cursor.lnum;
1559 conceal_update_lines = TRUE;
1560 }
1561# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001562 last_cursormoved = curwin->w_cursor;
1563 }
1564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (must_redraw)
1566 update_screen(0);
1567 else if (clear_cmdline || redraw_cmdline)
1568 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001569# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001570 if ((conceal_update_lines
1571 && (conceal_old_cursor_line != conceal_new_cursor_line
1572 || conceal_cursor_line(curwin)))
1573 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001574 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001575 if (conceal_old_cursor_line != conceal_new_cursor_line)
1576 update_single_line(curwin, conceal_old_cursor_line);
1577 update_single_line(curwin, conceal_new_cursor_line == 0
1578 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001579 curwin->w_valid &= ~VALID_CROW;
1580 }
1581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 showruler(FALSE);
1583 setcursor();
1584 emsg_on_display = FALSE; /* may remove error message now */
1585 }
1586}
1587
1588/*
1589 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1590 */
1591 static void
1592ins_ctrl_v()
1593{
1594 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001595 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
1597 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001598 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001603 did_putchar = TRUE;
1604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1606
1607#ifdef FEAT_CMDL_INFO
1608 add_to_showcmd_c(Ctrl_V);
1609#endif
1610
1611 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001612 if (did_putchar)
1613 /* when the line fits in 'columns' the '^' is at the start of the next
1614 * line and will not removed by the redraw */
1615 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616#ifdef FEAT_CMDL_INFO
1617 clear_showcmd();
1618#endif
1619 insert_special(c, FALSE, TRUE);
1620#ifdef FEAT_RIGHTLEFT
1621 revins_chars++;
1622 revins_legal++;
1623#endif
1624}
1625
1626/*
1627 * Put a character directly onto the screen. It's not stored in a buffer.
1628 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1629 */
1630static int pc_status;
1631#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1632#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1633#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1634#define PC_STATUS_SET 3 /* pc_bytes was filled */
1635#ifdef FEAT_MBYTE
1636static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1637#else
1638static char_u pc_bytes[2]; /* saved bytes */
1639#endif
1640static int pc_attr;
1641static int pc_row;
1642static int pc_col;
1643
1644 void
1645edit_putchar(c, highlight)
1646 int c;
1647 int highlight;
1648{
1649 int attr;
1650
1651 if (ScreenLines != NULL)
1652 {
1653 update_topline(); /* just in case w_topline isn't valid */
1654 validate_cursor();
1655 if (highlight)
1656 attr = hl_attr(HLF_8);
1657 else
1658 attr = 0;
1659 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1660 pc_col = W_WINCOL(curwin);
1661#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1662 pc_status = PC_STATUS_UNSET;
1663#endif
1664#ifdef FEAT_RIGHTLEFT
1665 if (curwin->w_p_rl)
1666 {
1667 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1668# ifdef FEAT_MBYTE
1669 if (has_mbyte)
1670 {
1671 int fix_col = mb_fix_col(pc_col, pc_row);
1672
1673 if (fix_col != pc_col)
1674 {
1675 screen_putchar(' ', pc_row, fix_col, attr);
1676 --curwin->w_wcol;
1677 pc_status = PC_STATUS_RIGHT;
1678 }
1679 }
1680# endif
1681 }
1682 else
1683#endif
1684 {
1685 pc_col += curwin->w_wcol;
1686#ifdef FEAT_MBYTE
1687 if (mb_lefthalve(pc_row, pc_col))
1688 pc_status = PC_STATUS_LEFT;
1689#endif
1690 }
1691
1692 /* save the character to be able to put it back */
1693#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1694 if (pc_status == PC_STATUS_UNSET)
1695#endif
1696 {
1697 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1698 pc_status = PC_STATUS_SET;
1699 }
1700 screen_putchar(c, pc_row, pc_col, attr);
1701 }
1702}
1703
1704/*
1705 * Undo the previous edit_putchar().
1706 */
1707 void
1708edit_unputchar()
1709{
1710 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1711 {
1712#if defined(FEAT_MBYTE)
1713 if (pc_status == PC_STATUS_RIGHT)
1714 ++curwin->w_wcol;
1715 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1716 redrawWinline(curwin->w_cursor.lnum, FALSE);
1717 else
1718#endif
1719 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1720 }
1721}
1722
1723/*
1724 * Called when p_dollar is set: display a '$' at the end of the changed text
1725 * Only works when cursor is in the line that changes.
1726 */
1727 void
1728display_dollar(col)
1729 colnr_T col;
1730{
1731 colnr_T save_col;
1732
1733 if (!redrawing())
1734 return;
1735
1736 cursor_off();
1737 save_col = curwin->w_cursor.col;
1738 curwin->w_cursor.col = col;
1739#ifdef FEAT_MBYTE
1740 if (has_mbyte)
1741 {
1742 char_u *p;
1743
1744 /* If on the last byte of a multi-byte move to the first byte. */
1745 p = ml_get_curline();
1746 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1747 }
1748#endif
1749 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1750 if (curwin->w_wcol < W_WIDTH(curwin))
1751 {
1752 edit_putchar('$', FALSE);
1753 dollar_vcol = curwin->w_virtcol;
1754 }
1755 curwin->w_cursor.col = save_col;
1756}
1757
1758/*
1759 * Call this function before moving the cursor from the normal insert position
1760 * in insert mode.
1761 */
1762 static void
1763undisplay_dollar()
1764{
1765 if (dollar_vcol)
1766 {
1767 dollar_vcol = 0;
1768 redrawWinline(curwin->w_cursor.lnum, FALSE);
1769 }
1770}
1771
1772/*
1773 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1774 * Keep the cursor on the same character.
1775 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1776 * type == INDENT_DEC decrease indent (for CTRL-D)
1777 * type == INDENT_SET set indent to "amount"
1778 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1779 */
1780 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001781change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 int type;
1783 int amount;
1784 int round;
1785 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001786 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 int vcol;
1789 int last_vcol;
1790 int insstart_less; /* reduction for Insstart.col */
1791 int new_cursor_col;
1792 int i;
1793 char_u *ptr;
1794 int save_p_list;
1795 int start_col;
1796 colnr_T vc;
1797#ifdef FEAT_VREPLACE
1798 colnr_T orig_col = 0; /* init for GCC */
1799 char_u *new_line, *orig_line = NULL; /* init for GCC */
1800
1801 /* VREPLACE mode needs to know what the line was like before changing */
1802 if (State & VREPLACE_FLAG)
1803 {
1804 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1805 orig_col = curwin->w_cursor.col;
1806 }
1807#endif
1808
1809 /* for the following tricks we don't want list mode */
1810 save_p_list = curwin->w_p_list;
1811 curwin->w_p_list = FALSE;
1812 vc = getvcol_nolist(&curwin->w_cursor);
1813 vcol = vc;
1814
1815 /*
1816 * For Replace mode we need to fix the replace stack later, which is only
1817 * possible when the cursor is in the indent. Remember the number of
1818 * characters before the cursor if it's possible.
1819 */
1820 start_col = curwin->w_cursor.col;
1821
1822 /* determine offset from first non-blank */
1823 new_cursor_col = curwin->w_cursor.col;
1824 beginline(BL_WHITE);
1825 new_cursor_col -= curwin->w_cursor.col;
1826
1827 insstart_less = curwin->w_cursor.col;
1828
1829 /*
1830 * If the cursor is in the indent, compute how many screen columns the
1831 * cursor is to the left of the first non-blank.
1832 */
1833 if (new_cursor_col < 0)
1834 vcol = get_indent() - vcol;
1835
1836 if (new_cursor_col > 0) /* can't fix replace stack */
1837 start_col = -1;
1838
1839 /*
1840 * Set the new indent. The cursor will be put on the first non-blank.
1841 */
1842 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001843 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 else
1845 {
1846#ifdef FEAT_VREPLACE
1847 int save_State = State;
1848
1849 /* Avoid being called recursively. */
1850 if (State & VREPLACE_FLAG)
1851 State = INSERT;
1852#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001853 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#ifdef FEAT_VREPLACE
1855 State = save_State;
1856#endif
1857 }
1858 insstart_less -= curwin->w_cursor.col;
1859
1860 /*
1861 * Try to put cursor on same character.
1862 * If the cursor is at or after the first non-blank in the line,
1863 * compute the cursor column relative to the column of the first
1864 * non-blank character.
1865 * If we are not in insert mode, leave the cursor on the first non-blank.
1866 * If the cursor is before the first non-blank, position it relative
1867 * to the first non-blank, counted in screen columns.
1868 */
1869 if (new_cursor_col >= 0)
1870 {
1871 /*
1872 * When changing the indent while the cursor is touching it, reset
1873 * Insstart_col to 0.
1874 */
1875 if (new_cursor_col == 0)
1876 insstart_less = MAXCOL;
1877 new_cursor_col += curwin->w_cursor.col;
1878 }
1879 else if (!(State & INSERT))
1880 new_cursor_col = curwin->w_cursor.col;
1881 else
1882 {
1883 /*
1884 * Compute the screen column where the cursor should be.
1885 */
1886 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001887 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 /*
1890 * Advance the cursor until we reach the right screen column.
1891 */
1892 vcol = last_vcol = 0;
1893 new_cursor_col = -1;
1894 ptr = ml_get_curline();
1895 while (vcol <= (int)curwin->w_virtcol)
1896 {
1897 last_vcol = vcol;
1898#ifdef FEAT_MBYTE
1899 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001900 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 else
1902#endif
1903 ++new_cursor_col;
1904 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1905 }
1906 vcol = last_vcol;
1907
1908 /*
1909 * May need to insert spaces to be able to position the cursor on
1910 * the right screen column.
1911 */
1912 if (vcol != (int)curwin->w_virtcol)
1913 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001914 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001916 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (ptr != NULL)
1918 {
1919 new_cursor_col += i;
1920 ptr[i] = NUL;
1921 while (--i >= 0)
1922 ptr[i] = ' ';
1923 ins_str(ptr);
1924 vim_free(ptr);
1925 }
1926 }
1927
1928 /*
1929 * When changing the indent while the cursor is in it, reset
1930 * Insstart_col to 0.
1931 */
1932 insstart_less = MAXCOL;
1933 }
1934
1935 curwin->w_p_list = save_p_list;
1936
1937 if (new_cursor_col <= 0)
1938 curwin->w_cursor.col = 0;
1939 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001940 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 curwin->w_set_curswant = TRUE;
1942 changed_cline_bef_curs();
1943
1944 /*
1945 * May have to adjust the start of the insert.
1946 */
1947 if (State & INSERT)
1948 {
1949 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1950 {
1951 if ((int)Insstart.col <= insstart_less)
1952 Insstart.col = 0;
1953 else
1954 Insstart.col -= insstart_less;
1955 }
1956 if ((int)ai_col <= insstart_less)
1957 ai_col = 0;
1958 else
1959 ai_col -= insstart_less;
1960 }
1961
1962 /*
1963 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1964 * If the number of characters before the cursor decreased, need to pop a
1965 * few characters from the replace stack.
1966 * If the number of characters before the cursor increased, need to push a
1967 * few NULs onto the replace stack.
1968 */
1969 if (REPLACE_NORMAL(State) && start_col >= 0)
1970 {
1971 while (start_col > (int)curwin->w_cursor.col)
1972 {
1973 replace_join(0); /* remove a NUL from the replace stack */
1974 --start_col;
1975 }
1976 while (start_col < (int)curwin->w_cursor.col || replaced)
1977 {
1978 replace_push(NUL);
1979 if (replaced)
1980 {
1981 replace_push(replaced);
1982 replaced = NUL;
1983 }
1984 ++start_col;
1985 }
1986 }
1987
1988#ifdef FEAT_VREPLACE
1989 /*
1990 * For VREPLACE mode, we also have to fix the replace stack. In this case
1991 * it is always possible because we backspace over the whole line and then
1992 * put it back again the way we wanted it.
1993 */
1994 if (State & VREPLACE_FLAG)
1995 {
1996 /* If orig_line didn't allocate, just return. At least we did the job,
1997 * even if you can't backspace. */
1998 if (orig_line == NULL)
1999 return;
2000
2001 /* Save new line */
2002 new_line = vim_strsave(ml_get_curline());
2003 if (new_line == NULL)
2004 return;
2005
2006 /* We only put back the new line up to the cursor */
2007 new_line[curwin->w_cursor.col] = NUL;
2008
2009 /* Put back original line */
2010 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2011 curwin->w_cursor.col = orig_col;
2012
2013 /* Backspace from cursor to start of line */
2014 backspace_until_column(0);
2015
2016 /* Insert new stuff into line again */
2017 ins_bytes(new_line);
2018
2019 vim_free(new_line);
2020 }
2021#endif
2022}
2023
2024/*
2025 * Truncate the space at the end of a line. This is to be used only in an
2026 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2027 * modes.
2028 */
2029 void
2030truncate_spaces(line)
2031 char_u *line;
2032{
2033 int i;
2034
2035 /* find start of trailing white space */
2036 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2037 {
2038 if (State & REPLACE_FLAG)
2039 replace_join(0); /* remove a NUL from the replace stack */
2040 }
2041 line[i + 1] = NUL;
2042}
2043
2044#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2045 || defined(FEAT_COMMENTS) || defined(PROTO)
2046/*
2047 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2048 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002049 * Will attempt not to go before "col" even when there is a composing
2050 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 */
2052 void
2053backspace_until_column(col)
2054 int col;
2055{
2056 while ((int)curwin->w_cursor.col > col)
2057 {
2058 curwin->w_cursor.col--;
2059 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002060 replace_do_bs(col);
2061 else if (!del_char_after_col(col))
2062 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064}
2065#endif
2066
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002067/*
2068 * Like del_char(), but make sure not to go before column "limit_col".
2069 * Only matters when there are composing characters.
2070 * Return TRUE when something was deleted.
2071 */
2072 static int
2073del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002074 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002075{
2076#ifdef FEAT_MBYTE
2077 if (enc_utf8 && limit_col >= 0)
2078 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002079 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002080
2081 /* Make sure the cursor is at the start of a character, but
2082 * skip forward again when going too far back because of a
2083 * composing character. */
2084 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002085 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002086 {
2087 int l = utf_ptr2len(ml_get_cursor());
2088
2089 if (l == 0) /* end of line */
2090 break;
2091 curwin->w_cursor.col += l;
2092 }
2093 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2094 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002095 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002096 }
2097 else
2098#endif
2099 (void)del_char(FALSE);
2100 return TRUE;
2101}
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2104/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 * CTRL-X pressed in Insert mode.
2106 */
2107 static void
2108ins_ctrl_x()
2109{
2110 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2111 * CTRL-V works like CTRL-N */
2112 if (ctrl_x_mode != CTRL_X_CMDLINE)
2113 {
2114 /* if the next ^X<> won't ADD nothing, then reset
2115 * compl_cont_status */
2116 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002117 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002118 else
2119 compl_cont_status = 0;
2120 /* We're not sure which CTRL-X mode it will be yet */
2121 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2122 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2123 edit_submode_pre = NULL;
2124 showmode();
2125 }
2126}
2127
2128/*
2129 * Return TRUE if the 'dict' or 'tsr' option can be used.
2130 */
2131 static int
2132has_compl_option(dict_opt)
2133 int dict_opt;
2134{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002135 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002136# ifdef FEAT_SPELL
2137 && !curwin->w_p_spell
2138# endif
2139 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002140 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2141 {
2142 ctrl_x_mode = 0;
2143 edit_submode = NULL;
2144 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2145 : (char_u *)_("'thesaurus' option is empty"),
2146 hl_attr(HLF_E));
2147 if (emsg_silent == 0)
2148 {
2149 vim_beep();
2150 setcursor();
2151 out_flush();
2152 ui_delay(2000L, FALSE);
2153 }
2154 return FALSE;
2155 }
2156 return TRUE;
2157}
2158
2159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2161 * This depends on the current mode.
2162 */
2163 int
2164vim_is_ctrl_x_key(c)
2165 int c;
2166{
2167 /* Always allow ^R - let it's results then be checked */
2168 if (c == Ctrl_R)
2169 return TRUE;
2170
Bram Moolenaare3226be2005-12-18 22:10:00 +00002171 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002172 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002173 return TRUE;
2174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 switch (ctrl_x_mode)
2176 {
2177 case 0: /* Not in any CTRL-X mode */
2178 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2179 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002180 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2182 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2183 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002184 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2185 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 case CTRL_X_SCROLL:
2187 return (c == Ctrl_Y || c == Ctrl_E);
2188 case CTRL_X_WHOLE_LINE:
2189 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2190 case CTRL_X_FILES:
2191 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2192 case CTRL_X_DICTIONARY:
2193 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2194 case CTRL_X_THESAURUS:
2195 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2196 case CTRL_X_TAGS:
2197 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2198#ifdef FEAT_FIND_ID
2199 case CTRL_X_PATH_PATTERNS:
2200 return (c == Ctrl_P || c == Ctrl_N);
2201 case CTRL_X_PATH_DEFINES:
2202 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2203#endif
2204 case CTRL_X_CMDLINE:
2205 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2206 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002207#ifdef FEAT_COMPL_FUNC
2208 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002209 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002210 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002211 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002212#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002213 case CTRL_X_SPELL:
2214 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216 EMSG(_(e_internal));
2217 return FALSE;
2218}
2219
2220/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002221 * Return TRUE when character "c" is part of the item currently being
2222 * completed. Used to decide whether to abandon complete mode when the menu
2223 * is visible.
2224 */
2225 static int
2226ins_compl_accept_char(c)
2227 int c;
2228{
2229 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2230 /* When expanding an identifier only accept identifier chars. */
2231 return vim_isIDc(c);
2232
2233 switch (ctrl_x_mode)
2234 {
2235 case CTRL_X_FILES:
2236 /* When expanding file name only accept file name chars. But not
2237 * path separators, so that "proto/<Tab>" expands files in
2238 * "proto", not "proto/" as a whole */
2239 return vim_isfilec(c) && !vim_ispathsep(c);
2240
2241 case CTRL_X_CMDLINE:
2242 case CTRL_X_OMNI:
2243 /* Command line and Omni completion can work with just about any
2244 * printable character, but do stop at white space. */
2245 return vim_isprintc(c) && !vim_iswhite(c);
2246
2247 case CTRL_X_WHOLE_LINE:
2248 /* For while line completion a space can be part of the line. */
2249 return vim_isprintc(c);
2250 }
2251 return vim_iswordc(c);
2252}
2253
2254/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002255 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002257 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * the rest of the word to be in -- webb
2259 */
2260 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002261ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 char_u *str;
2263 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002264 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 char_u *fname;
2266 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002267 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002269 char_u *p;
2270 int i, c;
2271 int actual_len; /* Take multi-byte characters */
2272 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002273 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002274 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 int has_lower = FALSE;
2276 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002278 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002280 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
Bram Moolenaara2993e12007-08-12 14:38:46 +00002282 /* Find actual length of completion. */
2283#ifdef FEAT_MBYTE
2284 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002286 p = str;
2287 actual_len = 0;
2288 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002290 mb_ptr_adv(p);
2291 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002294 else
2295#endif
2296 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaara2993e12007-08-12 14:38:46 +00002298 /* Find actual length of original text. */
2299#ifdef FEAT_MBYTE
2300 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002302 p = compl_orig_text;
2303 actual_compl_length = 0;
2304 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002306 mb_ptr_adv(p);
2307 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002310 else
2311#endif
2312 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002314 /* "actual_len" may be smaller than "actual_compl_length" when using
2315 * thesaurus, only use the minimum when comparing. */
2316 min_len = actual_len < actual_compl_length
2317 ? actual_len : actual_compl_length;
2318
Bram Moolenaara2993e12007-08-12 14:38:46 +00002319 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002320 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002321 if (wca != NULL)
2322 {
2323 p = str;
2324 for (i = 0; i < actual_len; ++i)
2325#ifdef FEAT_MBYTE
2326 if (has_mbyte)
2327 wca[i] = mb_ptr2char_adv(&p);
2328 else
2329#endif
2330 wca[i] = *(p++);
2331
2332 /* Rule 1: Were any chars converted to lower? */
2333 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002334 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002335 {
2336#ifdef FEAT_MBYTE
2337 if (has_mbyte)
2338 c = mb_ptr2char_adv(&p);
2339 else
2340#endif
2341 c = *(p++);
2342 if (MB_ISLOWER(c))
2343 {
2344 has_lower = TRUE;
2345 if (MB_ISUPPER(wca[i]))
2346 {
2347 /* Rule 1 is satisfied. */
2348 for (i = actual_compl_length; i < actual_len; ++i)
2349 wca[i] = MB_TOLOWER(wca[i]);
2350 break;
2351 }
2352 }
2353 }
2354
2355 /*
2356 * Rule 2: No lower case, 2nd consecutive letter converted to
2357 * upper case.
2358 */
2359 if (!has_lower)
2360 {
2361 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002362 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002363 {
2364#ifdef FEAT_MBYTE
2365 if (has_mbyte)
2366 c = mb_ptr2char_adv(&p);
2367 else
2368#endif
2369 c = *(p++);
2370 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2371 {
2372 /* Rule 2 is satisfied. */
2373 for (i = actual_compl_length; i < actual_len; ++i)
2374 wca[i] = MB_TOUPPER(wca[i]);
2375 break;
2376 }
2377 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2378 }
2379 }
2380
2381 /* Copy the original case of the part we typed. */
2382 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002383 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002384 {
2385#ifdef FEAT_MBYTE
2386 if (has_mbyte)
2387 c = mb_ptr2char_adv(&p);
2388 else
2389#endif
2390 c = *(p++);
2391 if (MB_ISLOWER(c))
2392 wca[i] = MB_TOLOWER(wca[i]);
2393 else if (MB_ISUPPER(c))
2394 wca[i] = MB_TOUPPER(wca[i]);
2395 }
2396
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002397 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002398 * Generate encoding specific output from wide character array.
2399 * Multi-byte characters can occupy up to five bytes more than
2400 * ASCII characters, and we also need one byte for NUL, so stay
2401 * six bytes away from the edge of IObuff.
2402 */
2403 p = IObuff;
2404 i = 0;
2405 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2406#ifdef FEAT_MBYTE
2407 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002408 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002409 else
2410#endif
2411 *(p++) = wca[i++];
2412 *p = NUL;
2413
2414 vim_free(wca);
2415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002417 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2418 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002420 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421}
2422
2423/*
2424 * Add a match to the list of matches.
2425 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002426 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002427 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002429 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002430ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 char_u *str;
2432 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002433 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002435 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002436 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002437 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002438 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002440 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002441 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443 ui_breakcheck();
2444 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if (len < 0)
2447 len = (int)STRLEN(str);
2448
2449 /*
2450 * If the same match is already present, don't add it.
2451 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002452 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002454 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 do
2456 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002457 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002458 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002459 && match->cp_str[len] == NUL)
2460 return NOTDONE;
2461 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002462 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002465 /* Remove any popup menu before changing the list of matches. */
2466 ins_compl_del_pum();
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 /*
2469 * Allocate a new match structure.
2470 * Copy the values to the new match structure.
2471 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002472 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002474 return FAIL;
2475 match->cp_number = -1;
2476 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002477 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002478 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002481 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002483 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002486 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2488 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002489 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002490 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002491 && compl_curr_match->cp_fname != NULL
2492 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002493 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002494 else if (fname != NULL)
2495 {
2496 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002497 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002500 match->cp_fname = NULL;
2501 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002502
2503 if (cptext != NULL)
2504 {
2505 int i;
2506
2507 for (i = 0; i < CPT_COUNT; ++i)
2508 if (cptext[i] != NULL && *cptext[i] != NUL)
2509 match->cp_text[i] = vim_strsave(cptext[i]);
2510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 /*
2513 * Link the new match structure in the list of matches.
2514 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002515 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002516 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 else if (dir == FORWARD)
2518 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002519 match->cp_next = compl_curr_match->cp_next;
2520 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 }
2522 else /* BACKWARD */
2523 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002524 match->cp_next = compl_curr_match;
2525 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002527 if (match->cp_next)
2528 match->cp_next->cp_prev = match;
2529 if (match->cp_prev)
2530 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002532 compl_first_match = match;
2533 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002535 /*
2536 * Find the longest common string if still doing that.
2537 */
2538 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2539 ins_compl_longest_match(match);
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 return OK;
2542}
2543
2544/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 * Return TRUE if "str[len]" matches with match->cp_str, considering
2546 * match->cp_icase.
2547 */
2548 static int
2549ins_compl_equal(match, str, len)
2550 compl_T *match;
2551 char_u *str;
2552 int len;
2553{
2554 if (match->cp_icase)
2555 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2556 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2557}
2558
2559/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002560 * Reduce the longest common string for match "match".
2561 */
2562 static void
2563ins_compl_longest_match(match)
2564 compl_T *match;
2565{
2566 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002568 int had_match;
2569
2570 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002571 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002572 /* First match, use it as a whole. */
2573 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002574 if (compl_leader != NULL)
2575 {
2576 had_match = (curwin->w_cursor.col > compl_col);
2577 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002578 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002579 ins_redraw(FALSE);
2580
2581 /* When the match isn't there (to avoid matching itself) remove it
2582 * again after redrawing. */
2583 if (!had_match)
2584 ins_compl_delete();
2585 compl_used_match = FALSE;
2586 }
2587 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002588 else
2589 {
2590 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002591 p = compl_leader;
2592 s = match->cp_str;
2593 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002594 {
2595#ifdef FEAT_MBYTE
2596 if (has_mbyte)
2597 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002598 c1 = mb_ptr2char(p);
2599 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002600 }
2601 else
2602#endif
2603 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002604 c1 = *p;
2605 c2 = *s;
2606 }
2607 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2608 : (c1 != c2))
2609 break;
2610#ifdef FEAT_MBYTE
2611 if (has_mbyte)
2612 {
2613 mb_ptr_adv(p);
2614 mb_ptr_adv(s);
2615 }
2616 else
2617#endif
2618 {
2619 ++p;
2620 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002621 }
2622 }
2623
2624 if (*p != NUL)
2625 {
2626 /* Leader was shortened, need to change the inserted text. */
2627 *p = NUL;
2628 had_match = (curwin->w_cursor.col > compl_col);
2629 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002630 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002631 ins_redraw(FALSE);
2632
2633 /* When the match isn't there (to avoid matching itself) remove it
2634 * again after redrawing. */
2635 if (!had_match)
2636 ins_compl_delete();
2637 }
2638
2639 compl_used_match = FALSE;
2640 }
2641}
2642
2643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * Add an array of matches to the list of matches.
2645 * Frees matches[].
2646 */
2647 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002648ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 int num_matches;
2650 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002651 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
2653 int i;
2654 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002655 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
Bram Moolenaar572cb562005-08-05 21:35:02 +00002657 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002658 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002659 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002661 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 FreeWild(num_matches, matches);
2663}
2664
2665/* Make the completion list cyclic.
2666 * Return the number of matches (excluding the original).
2667 */
2668 static int
2669ins_compl_make_cyclic()
2670{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002671 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int count = 0;
2673
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002674 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
2676 /*
2677 * Find the end of the list.
2678 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002679 match = compl_first_match;
2680 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002681 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002683 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 ++count;
2685 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002686 match->cp_next = compl_first_match;
2687 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689 return count;
2690}
2691
Bram Moolenaara94bc432006-03-10 21:42:59 +00002692/*
2693 * Start completion for the complete() function.
2694 * "startcol" is where the matched text starts (1 is first column).
2695 * "list" is the list of matches.
2696 */
2697 void
2698set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002699 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002700 list_T *list;
2701{
2702 /* If already doing completions stop it. */
2703 if (ctrl_x_mode != 0)
2704 ins_compl_prep(' ');
2705 ins_compl_clear();
2706
2707 if (stop_arrow() == FAIL)
2708 return;
2709
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002710 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002711 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002712 startcol = curwin->w_cursor.col;
2713 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002714 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002715 /* compl_pattern doesn't need to be set */
2716 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2717 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002718 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002719 return;
2720
2721 /* Handle like dictionary completion. */
2722 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2723
2724 ins_compl_add_list(list);
2725 compl_matches = ins_compl_make_cyclic();
2726 compl_started = TRUE;
2727 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002728 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002729
2730 compl_curr_match = compl_first_match;
2731 ins_complete(Ctrl_N);
2732 out_flush();
2733}
2734
2735
Bram Moolenaar9372a112005-12-06 19:59:18 +00002736/* "compl_match_array" points the currently displayed list of entries in the
2737 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002738static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002739static int compl_match_arraysize;
2740
2741/*
2742 * Update the screen and when there is any scrolling remove the popup menu.
2743 */
2744 static void
2745ins_compl_upd_pum()
2746{
2747 int h;
2748
2749 if (compl_match_array != NULL)
2750 {
2751 h = curwin->w_cline_height;
2752 update_screen(0);
2753 if (h != curwin->w_cline_height)
2754 ins_compl_del_pum();
2755 }
2756}
2757
2758/*
2759 * Remove any popup menu.
2760 */
2761 static void
2762ins_compl_del_pum()
2763{
2764 if (compl_match_array != NULL)
2765 {
2766 pum_undisplay();
2767 vim_free(compl_match_array);
2768 compl_match_array = NULL;
2769 }
2770}
2771
2772/*
2773 * Return TRUE if the popup menu should be displayed.
2774 */
2775 static int
2776pum_wanted()
2777{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002778 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002779 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002780 return FALSE;
2781
2782 /* The display looks bad on a B&W display. */
2783 if (t_colors < 8
2784#ifdef FEAT_GUI
2785 && !gui.in_use
2786#endif
2787 )
2788 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002789 return TRUE;
2790}
2791
2792/*
2793 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002794 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002795 */
2796 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002797pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002798{
2799 compl_T *compl;
2800 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002801
2802 /* Don't display the popup menu if there are no matches or there is only
2803 * one (ignoring the original text). */
2804 compl = compl_first_match;
2805 i = 0;
2806 do
2807 {
2808 if (compl == NULL
2809 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2810 break;
2811 compl = compl->cp_next;
2812 } while (compl != compl_first_match);
2813
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002814 if (strstr((char *)p_cot, "menuone") != NULL)
2815 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002816 return (i >= 2);
2817}
2818
2819/*
2820 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002821 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002822 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002823 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002824ins_compl_show_pum()
2825{
2826 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002827 compl_T *shown_compl = NULL;
2828 int did_find_shown_match = FALSE;
2829 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002830 int i;
2831 int cur = -1;
2832 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002833 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002834
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002835 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002836 return;
2837
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002838#if defined(FEAT_EVAL)
2839 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2840 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2841#endif
2842
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002843 /* Update the screen before drawing the popup menu over it. */
2844 update_screen(0);
2845
2846 if (compl_match_array == NULL)
2847 {
2848 /* Need to build the popup menu list. */
2849 compl_match_arraysize = 0;
2850 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002851 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002852 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002853 do
2854 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002855 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2856 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002857 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002858 ++compl_match_arraysize;
2859 compl = compl->cp_next;
2860 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002861 if (compl_match_arraysize == 0)
2862 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002863 compl_match_array = (pumitem_T *)alloc_clear(
2864 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002865 * compl_match_arraysize));
2866 if (compl_match_array != NULL)
2867 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002868 /* If the current match is the original text don't find the first
2869 * match after it, don't highlight anything. */
2870 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2871 shown_match_ok = TRUE;
2872
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002873 i = 0;
2874 compl = compl_first_match;
2875 do
2876 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002877 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2878 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002879 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002880 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002881 if (!shown_match_ok)
2882 {
2883 if (compl == compl_shown_match || did_find_shown_match)
2884 {
2885 /* This item is the shown match or this is the
2886 * first displayed item after the shown match. */
2887 compl_shown_match = compl;
2888 did_find_shown_match = TRUE;
2889 shown_match_ok = TRUE;
2890 }
2891 else
2892 /* Remember this displayed match for when the
2893 * shown match is just below it. */
2894 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002896 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002897
2898 if (compl->cp_text[CPT_ABBR] != NULL)
2899 compl_match_array[i].pum_text =
2900 compl->cp_text[CPT_ABBR];
2901 else
2902 compl_match_array[i].pum_text = compl->cp_str;
2903 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2904 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2905 if (compl->cp_text[CPT_MENU] != NULL)
2906 compl_match_array[i++].pum_extra =
2907 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002908 else
2909 compl_match_array[i++].pum_extra = compl->cp_fname;
2910 }
2911
2912 if (compl == compl_shown_match)
2913 {
2914 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002915
2916 /* When the original text is the shown match don't set
2917 * compl_shown_match. */
2918 if (compl->cp_flags & ORIGINAL_TEXT)
2919 shown_match_ok = TRUE;
2920
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002921 if (!shown_match_ok && shown_compl != NULL)
2922 {
2923 /* The shown match isn't displayed, set it to the
2924 * previously displayed match. */
2925 compl_shown_match = shown_compl;
2926 shown_match_ok = TRUE;
2927 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928 }
2929 compl = compl->cp_next;
2930 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002931
2932 if (!shown_match_ok) /* no displayed match at all */
2933 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002934 }
2935 }
2936 else
2937 {
2938 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002939 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002940 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2941 || compl_match_array[i].pum_text
2942 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002943 {
2944 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002945 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002946 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002947 }
2948
2949 if (compl_match_array != NULL)
2950 {
2951 /* Compute the screen column of the start of the completed text.
2952 * Use the cursor to get all wrapping and other settings right. */
2953 col = curwin->w_cursor.col;
2954 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002955 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002956 curwin->w_cursor.col = col;
2957 }
2958}
2959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960#define DICT_FIRST (1) /* use just first element in "dict" */
2961#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002964 * Add any identifiers that match the given pattern in the list of dictionary
2965 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 */
2967 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002968ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2969 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002971 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002972 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002974 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 char_u *ptr;
2976 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 char_u **files;
2979 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002981 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaar0b238792006-03-02 22:49:12 +00002983 if (*dict == NUL)
2984 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002985#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002986 /* When 'dictionary' is empty and spell checking is enabled use
2987 * "spell". */
2988 if (!thesaurus && curwin->w_p_spell)
2989 dict = (char_u *)"spell";
2990 else
2991#endif
2992 return;
2993 }
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002996 if (buf == NULL)
2997 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002998 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 /* If 'infercase' is set, don't use 'smartcase' here */
3001 save_p_scs = p_scs;
3002 if (curbuf->b_p_inf)
3003 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003004
3005 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3006 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003007 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003008 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3009 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003010 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003011 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003012
3013 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003014 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003015 len = STRLEN(pat_esc) + 10;
3016 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003017 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003018 {
3019 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003020 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003021 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003022 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003023 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3024 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003025 vim_free(ptr);
3026 }
3027 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003028 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003029 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003030 if (regmatch.regprog == NULL)
3031 goto theend;
3032 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3035 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003036 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
3038 /* copy one dictionary file name into buf */
3039 if (flags == DICT_EXACT)
3040 {
3041 count = 1;
3042 files = &dict;
3043 }
3044 else
3045 {
3046 /* Expand wildcards in the dictionary name, but do not allow
3047 * backticks (for security, the 'dict' option may have been set in
3048 * a modeline). */
3049 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003050# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003051 if (!thesaurus && STRCMP(buf, "spell") == 0)
3052 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003053 else
3054# endif
3055 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 || expand_wildcards(1, &buf, &count, &files,
3057 EW_FILE|EW_SILENT) != OK)
3058 count = 0;
3059 }
3060
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003061# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003062 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003064 /* Complete from active spelling. Skip "\<" in the pattern, we
3065 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003066 if (pat[0] == '\\' && pat[1] == '<')
3067 ptr = pat + 2;
3068 else
3069 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003070 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003072 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003073# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003074 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003075 {
3076 ins_compl_files(count, files, thesaurus, flags,
3077 &regmatch, buf, &dir);
3078 if (flags != DICT_EXACT)
3079 FreeWild(count, files);
3080 }
3081 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 break;
3083 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003084
3085theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 p_scs = save_p_scs;
3087 vim_free(regmatch.regprog);
3088 vim_free(buf);
3089}
3090
Bram Moolenaar0b238792006-03-02 22:49:12 +00003091 static void
3092ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3093 int count;
3094 char_u **files;
3095 int thesaurus;
3096 int flags;
3097 regmatch_T *regmatch;
3098 char_u *buf;
3099 int *dir;
3100{
3101 char_u *ptr;
3102 int i;
3103 FILE *fp;
3104 int add_r;
3105
3106 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3107 {
3108 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3109 if (flags != DICT_EXACT)
3110 {
3111 vim_snprintf((char *)IObuff, IOSIZE,
3112 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003113 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003114 }
3115
3116 if (fp != NULL)
3117 {
3118 /*
3119 * Read dictionary file line by line.
3120 * Check each line for a match.
3121 */
3122 while (!got_int && !compl_interrupted
3123 && !vim_fgets(buf, LSIZE, fp))
3124 {
3125 ptr = buf;
3126 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3127 {
3128 ptr = regmatch->startp[0];
3129 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3130 ptr = find_line_end(ptr);
3131 else
3132 ptr = find_word_end(ptr);
3133 add_r = ins_compl_add_infercase(regmatch->startp[0],
3134 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003135 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003136 if (thesaurus)
3137 {
3138 char_u *wstart;
3139
3140 /*
3141 * Add the other matches on the line
3142 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003143 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003144 while (!got_int)
3145 {
3146 /* Find start of the next word. Skip white
3147 * space and punctuation. */
3148 ptr = find_word_start(ptr);
3149 if (*ptr == NUL || *ptr == NL)
3150 break;
3151 wstart = ptr;
3152
Bram Moolenaara2993e12007-08-12 14:38:46 +00003153 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003154#ifdef FEAT_MBYTE
3155 if (has_mbyte)
3156 /* Japanese words may have characters in
3157 * different classes, only separate words
3158 * with single-byte non-word characters. */
3159 while (*ptr != NUL)
3160 {
3161 int l = (*mb_ptr2len)(ptr);
3162
3163 if (l < 2 && !vim_iswordc(*ptr))
3164 break;
3165 ptr += l;
3166 }
3167 else
3168#endif
3169 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003170
3171 /* Add the word. Skip the regexp match. */
3172 if (wstart != regmatch->startp[0])
3173 add_r = ins_compl_add_infercase(wstart,
3174 (int)(ptr - wstart),
3175 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003176 }
3177 }
3178 if (add_r == OK)
3179 /* if dir was BACKWARD then honor it just once */
3180 *dir = FORWARD;
3181 else if (add_r == FAIL)
3182 break;
3183 /* avoid expensive call to vim_regexec() when at end
3184 * of line */
3185 if (*ptr == '\n' || got_int)
3186 break;
3187 }
3188 line_breakcheck();
3189 ins_compl_check_keys(50);
3190 }
3191 fclose(fp);
3192 }
3193 }
3194}
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196/*
3197 * Find the start of the next word.
3198 * Returns a pointer to the first char of the word. Also stops at a NUL.
3199 */
3200 char_u *
3201find_word_start(ptr)
3202 char_u *ptr;
3203{
3204#ifdef FEAT_MBYTE
3205 if (has_mbyte)
3206 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003207 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
3209#endif
3210 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3211 ++ptr;
3212 return ptr;
3213}
3214
3215/*
3216 * Find the end of the word. Assumes it starts inside a word.
3217 * Returns a pointer to just after the word.
3218 */
3219 char_u *
3220find_word_end(ptr)
3221 char_u *ptr;
3222{
3223#ifdef FEAT_MBYTE
3224 int start_class;
3225
3226 if (has_mbyte)
3227 {
3228 start_class = mb_get_class(ptr);
3229 if (start_class > 1)
3230 while (*ptr != NUL)
3231 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003232 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (mb_get_class(ptr) != start_class)
3234 break;
3235 }
3236 }
3237 else
3238#endif
3239 while (vim_iswordc(*ptr))
3240 ++ptr;
3241 return ptr;
3242}
3243
3244/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003245 * Find the end of the line, omitting CR and NL at the end.
3246 * Returns a pointer to just after the line.
3247 */
3248 static char_u *
3249find_line_end(ptr)
3250 char_u *ptr;
3251{
3252 char_u *s;
3253
3254 s = ptr + STRLEN(ptr);
3255 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3256 --s;
3257 return s;
3258}
3259
3260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 * Free the list of completions
3262 */
3263 static void
3264ins_compl_free()
3265{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003266 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003267 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003269 vim_free(compl_pattern);
3270 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003271 vim_free(compl_leader);
3272 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003274 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003276
3277 ins_compl_del_pum();
3278 pum_clear();
3279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003280 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 do
3282 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003283 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003284 compl_curr_match = compl_curr_match->cp_next;
3285 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003287 if (match->cp_flags & FREE_FNAME)
3288 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003289 for (i = 0; i < CPT_COUNT; ++i)
3290 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003292 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3293 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003294 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297 static void
3298ins_compl_clear()
3299{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003300 compl_cont_status = 0;
3301 compl_started = FALSE;
3302 compl_matches = 0;
3303 vim_free(compl_pattern);
3304 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003305 vim_free(compl_leader);
3306 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003308 vim_free(compl_orig_text);
3309 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003310 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311}
3312
3313/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003314 * Return TRUE when Insert completion is active.
3315 */
3316 int
3317ins_compl_active()
3318{
3319 return compl_started;
3320}
3321
3322/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003323 * Delete one character before the cursor and show the subset of the matches
3324 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003325 * Returns the character to be used, NUL if the work is done and another char
3326 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003327 */
3328 static int
3329ins_compl_bs()
3330{
3331 char_u *line;
3332 char_u *p;
3333
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003334 line = ml_get_curline();
3335 p = line + curwin->w_cursor.col;
3336 mb_ptr_back(line, p);
3337
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003338 /* Stop completion when the whole word was deleted. For Omni completion
3339 * allow the word to be deleted, we won't match everything. */
3340 if ((int)(p - line) - (int)compl_col < 0
3341 || ((int)(p - line) - (int)compl_col == 0
3342 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003343 return K_BS;
3344
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003345 /* Deleted more than what was used to find matches or didn't finish
3346 * finding all matches: need to look for matches all over again. */
3347 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003348 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003349 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003350
Bram Moolenaara6557602006-02-04 22:43:20 +00003351 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003352 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003353 if (compl_leader != NULL)
3354 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003355 ins_compl_new_leader();
3356 return NUL;
3357 }
3358 return K_BS;
3359}
Bram Moolenaara6557602006-02-04 22:43:20 +00003360
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003361/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003362 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3363 * be called.
3364 */
3365 static int
3366ins_compl_need_restart()
3367{
3368 /* Return TRUE if we didn't complete finding matches or when the
3369 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3370 return compl_was_interrupted
3371 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3372 && compl_opt_refresh_always);
3373}
3374
3375/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003376 * Called after changing "compl_leader".
3377 * Show the popup menu with a different set of matches.
3378 * May also search for matches again if the previous search was interrupted.
3379 */
3380 static void
3381ins_compl_new_leader()
3382{
3383 ins_compl_del_pum();
3384 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003385 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003386 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003387
3388 if (compl_started)
3389 ins_compl_set_original_text(compl_leader);
3390 else
3391 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003392#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003393 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003394#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003395 /*
3396 * Matches were cleared, need to search for them now. First display
3397 * the changed text before the cursor. Set "compl_restarting" to
3398 * avoid that the first match is inserted.
3399 */
3400 update_screen(0);
3401#ifdef FEAT_GUI
3402 if (gui.in_use)
3403 {
3404 /* Show the cursor after the match, not after the redrawn text. */
3405 setcursor();
3406 out_flush();
3407 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003408 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003409#endif
3410 compl_restarting = TRUE;
3411 if (ins_complete(Ctrl_N) == FAIL)
3412 compl_cont_status = 0;
3413 compl_restarting = FALSE;
3414 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003415
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003416 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003417
3418 /* Show the popup menu with a different set of matches. */
3419 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003420
3421 /* Don't let Enter select the original text when there is no popup menu. */
3422 if (compl_match_array == NULL)
3423 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003424}
3425
3426/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003427 * Return the length of the completion, from the completion start column to
3428 * the cursor column. Making sure it never goes below zero.
3429 */
3430 static int
3431ins_compl_len()
3432{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003433 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003434
3435 if (off < 0)
3436 return 0;
3437 return off;
3438}
3439
3440/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003441 * Append one character to the match leader. May reduce the number of
3442 * matches.
3443 */
3444 static void
3445ins_compl_addleader(c)
3446 int c;
3447{
3448#ifdef FEAT_MBYTE
3449 int cc;
3450
3451 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3452 {
3453 char_u buf[MB_MAXBYTES + 1];
3454
3455 (*mb_char2bytes)(c, buf);
3456 buf[cc] = NUL;
3457 ins_char_bytes(buf, cc);
3458 }
3459 else
3460#endif
3461 ins_char(c);
3462
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003463 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003464 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003465 ins_compl_restart();
3466
Bram Moolenaara6557602006-02-04 22:43:20 +00003467 vim_free(compl_leader);
3468 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003469 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003470 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003471 ins_compl_new_leader();
3472}
3473
3474/*
3475 * Setup for finding completions again without leaving CTRL-X mode. Used when
3476 * BS or a key was typed while still searching for matches.
3477 */
3478 static void
3479ins_compl_restart()
3480{
3481 ins_compl_free();
3482 compl_started = FALSE;
3483 compl_matches = 0;
3484 compl_cont_status = 0;
3485 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003486}
3487
3488/*
3489 * Set the first match, the original text.
3490 */
3491 static void
3492ins_compl_set_original_text(str)
3493 char_u *str;
3494{
3495 char_u *p;
3496
3497 /* Replace the original text entry. */
3498 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3499 {
3500 p = vim_strsave(str);
3501 if (p != NULL)
3502 {
3503 vim_free(compl_first_match->cp_str);
3504 compl_first_match->cp_str = p;
3505 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003506 }
3507}
3508
3509/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003510 * Append one character to the match leader. May reduce the number of
3511 * matches.
3512 */
3513 static void
3514ins_compl_addfrommatch()
3515{
3516 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003517 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003518 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003519 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003520
3521 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003522 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003523 {
3524 /* When still at the original match use the first entry that matches
3525 * the leader. */
3526 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3527 {
3528 p = NULL;
3529 for (cp = compl_shown_match->cp_next; cp != NULL
3530 && cp != compl_first_match; cp = cp->cp_next)
3531 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003532 if (compl_leader == NULL
3533 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003534 (int)STRLEN(compl_leader)))
3535 {
3536 p = cp->cp_str;
3537 break;
3538 }
3539 }
3540 if (p == NULL || (int)STRLEN(p) <= len)
3541 return;
3542 }
3543 else
3544 return;
3545 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003546 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003547 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003548 ins_compl_addleader(c);
3549}
3550
3551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003553 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003554 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003556 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557ins_compl_prep(c)
3558 int c;
3559{
3560 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003562 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 /* Forget any previous 'special' messages if this is actually
3565 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3566 */
3567 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3568 edit_submode_extra = NULL;
3569
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003570 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003571 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3572 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003573 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003575 /* Set "compl_get_longest" when finding the first matches. */
3576 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3577 || (ctrl_x_mode == 0 && !compl_started))
3578 {
3579 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3580 compl_used_match = TRUE;
3581 }
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3584 {
3585 /*
3586 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3587 * it will be yet. Now we decide.
3588 */
3589 switch (c)
3590 {
3591 case Ctrl_E:
3592 case Ctrl_Y:
3593 ctrl_x_mode = CTRL_X_SCROLL;
3594 if (!(State & REPLACE_FLAG))
3595 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3596 else
3597 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3598 edit_submode_pre = NULL;
3599 showmode();
3600 break;
3601 case Ctrl_L:
3602 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3603 break;
3604 case Ctrl_F:
3605 ctrl_x_mode = CTRL_X_FILES;
3606 break;
3607 case Ctrl_K:
3608 ctrl_x_mode = CTRL_X_DICTIONARY;
3609 break;
3610 case Ctrl_R:
3611 /* Simply allow ^R to happen without affecting ^X mode */
3612 break;
3613 case Ctrl_T:
3614 ctrl_x_mode = CTRL_X_THESAURUS;
3615 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003616#ifdef FEAT_COMPL_FUNC
3617 case Ctrl_U:
3618 ctrl_x_mode = CTRL_X_FUNCTION;
3619 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003620 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003621 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003622 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003623#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003624 case 's':
3625 case Ctrl_S:
3626 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003627#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003628 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003629 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003630 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003631#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 case Ctrl_RSB:
3634 ctrl_x_mode = CTRL_X_TAGS;
3635 break;
3636#ifdef FEAT_FIND_ID
3637 case Ctrl_I:
3638 case K_S_TAB:
3639 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3640 break;
3641 case Ctrl_D:
3642 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3643 break;
3644#endif
3645 case Ctrl_V:
3646 case Ctrl_Q:
3647 ctrl_x_mode = CTRL_X_CMDLINE;
3648 break;
3649 case Ctrl_P:
3650 case Ctrl_N:
3651 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3652 * just started ^X mode, or there were enough ^X's to cancel
3653 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3654 * do normal expansion when interrupting a different mode (say
3655 * ^X^F^X^P or ^P^X^X^P, see below)
3656 * nothing changes if interrupting mode 0, (eg, the flag
3657 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003658 if (!(compl_cont_status & CONT_INTRPT))
3659 compl_cont_status |= CONT_LOCAL;
3660 else if (compl_cont_mode != 0)
3661 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 /* FALLTHROUGH */
3663 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 /* If we have typed at least 2 ^X's... for modes != 0, we set
3665 * compl_cont_status = 0 (eg, as if we had just started ^X
3666 * mode).
3667 * For mode 0, we set "compl_cont_mode" to an impossible
3668 * value, in both cases ^X^X can be used to restart the same
3669 * mode (avoiding ADDING mode).
3670 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3671 * 'complete' and local ^P expansions respectively.
3672 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3673 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (c == Ctrl_X)
3675 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003676 if (compl_cont_mode != 0)
3677 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 ctrl_x_mode = 0;
3682 edit_submode = NULL;
3683 showmode();
3684 break;
3685 }
3686 }
3687 else if (ctrl_x_mode != 0)
3688 {
3689 /* We're already in CTRL-X mode, do we stay in it? */
3690 if (!vim_is_ctrl_x_key(c))
3691 {
3692 if (ctrl_x_mode == CTRL_X_SCROLL)
3693 ctrl_x_mode = 0;
3694 else
3695 ctrl_x_mode = CTRL_X_FINISHED;
3696 edit_submode = NULL;
3697 }
3698 showmode();
3699 }
3700
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003701 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
3703 /* Show error message from attempted keyword completion (probably
3704 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003705 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003707 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3708 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 || ctrl_x_mode == CTRL_X_FINISHED)
3710 {
3711 /* Get here when we have finished typing a sequence of ^N and
3712 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003713 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003714 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003716 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003717 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003720 * If any of the original typed text has been changed, eg when
3721 * ignorecase is set, we must add back-spaces to the redo
3722 * buffer. We add as few as necessary to delete just the part
3723 * of the original text that has changed.
3724 * When using the longest match, edited the match or used
3725 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003727 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3728 ptr = compl_curr_match->cp_str;
3729 else if (compl_leader != NULL)
3730 ptr = compl_leader;
3731 else
3732 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003733 if (compl_orig_text != NULL)
3734 {
3735 p = compl_orig_text;
3736 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3737 ++temp)
3738 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003739#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003740 if (temp > 0)
3741 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003742#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003743 for (p += temp; *p != NUL; mb_ptr_adv(p))
3744 AppendCharToRedobuff(K_BS);
3745 }
3746 if (ptr != NULL)
3747 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749
3750#ifdef FEAT_CINDENT
3751 want_cindent = (can_cindent && cindent_on());
3752#endif
3753 /*
3754 * When completing whole lines: fix indent for 'cindent'.
3755 * Otherwise, break line if it's too long.
3756 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003757 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
3759#ifdef FEAT_CINDENT
3760 /* re-indent the current line */
3761 if (want_cindent)
3762 {
3763 do_c_expr_indent();
3764 want_cindent = FALSE; /* don't do it again */
3765 }
3766#endif
3767 }
3768 else
3769 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003770 int prev_col = curwin->w_cursor.col;
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003773 if (prev_col > 0)
3774 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 if (stop_arrow() == OK)
3776 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003777 if (prev_col > 0
3778 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3779 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003782 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003783 * the selection without inserting anything. When
3784 * compl_enter_selects is set the Enter key does the same. */
3785 if ((c == Ctrl_Y || (compl_enter_selects
3786 && (c == CAR || c == K_KENTER || c == NL)))
3787 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003788 retval = TRUE;
3789
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003790 /* CTRL-E means completion is Ended, go back to the typed text. */
3791 if (c == Ctrl_E)
3792 {
3793 ins_compl_delete();
3794 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003795 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003796 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003797 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003798 retval = TRUE;
3799 }
3800
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003801 auto_format(FALSE, TRUE);
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003804 compl_started = FALSE;
3805 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 msg_clr_cmdline(); /* necessary for "noshowmode" */
3807 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003808 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (edit_submode != NULL)
3810 {
3811 edit_submode = NULL;
3812 showmode();
3813 }
3814
3815#ifdef FEAT_CINDENT
3816 /*
3817 * Indent now if a key was typed that is in 'cinkeys'.
3818 */
3819 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3820 do_c_expr_indent();
3821#endif
3822 }
3823 }
3824
3825 /* reset continue_* if we left expansion-mode, if we stay they'll be
3826 * (re)set properly in ins_complete() */
3827 if (!vim_is_ctrl_x_key(c))
3828 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 compl_cont_status = 0;
3830 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003832
3833 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834}
3835
3836/*
3837 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3838 * (depending on flag) starting from buf and looking for a non-scanned
3839 * buffer (other than curbuf). curbuf is special, if it is called with
3840 * buf=curbuf then it has to be the first call for a given flag/expansion.
3841 *
3842 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3843 */
3844 static buf_T *
3845ins_compl_next_buf(buf, flag)
3846 buf_T *buf;
3847 int flag;
3848{
3849#ifdef FEAT_WINDOWS
3850 static win_T *wp;
3851#endif
3852
3853 if (flag == 'w') /* just windows */
3854 {
3855#ifdef FEAT_WINDOWS
3856 if (buf == curbuf) /* first call for this flag/expansion */
3857 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003858 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 && wp->w_buffer->b_scanned)
3860 ;
3861 buf = wp->w_buffer;
3862#else
3863 buf = curbuf;
3864#endif
3865 }
3866 else
3867 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3868 * (unlisted buffers)
3869 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003870 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 && ((flag == 'U'
3872 ? buf->b_p_bl
3873 : (!buf->b_p_bl
3874 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003875 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 ;
3877 return buf;
3878}
3879
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003880#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003881static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003882
3883/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003884 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003885 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003886 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003887 static void
3888expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003889 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003890 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003891{
Bram Moolenaar82139082011-09-14 16:52:09 +02003892 list_T *matchlist = NULL;
3893 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003894 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003895 char_u *funcname;
3896 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003897 win_T *curwin_save;
3898 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003899 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003900
Bram Moolenaare344bea2005-09-01 20:46:49 +00003901 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3902 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003903 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003904
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003905 /* Call 'completefunc' to obtain the list of matches. */
3906 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003907 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003908
Bram Moolenaare344bea2005-09-01 20:46:49 +00003909 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003910 curwin_save = curwin;
3911 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02003912
3913 /* Call a function, which returns a list or dict. */
3914 if (call_vim_function(funcname, 2, args, FALSE, &rettv) == OK)
3915 {
3916 switch (rettv.v_type)
3917 {
3918 case VAR_LIST:
3919 matchlist = rettv.vval.v_list;
3920 break;
3921 case VAR_DICT:
3922 matchdict = rettv.vval.v_dict;
3923 break;
3924 default:
3925 /* TODO: Give error message? */
3926 clear_tv(&rettv);
3927 break;
3928 }
3929 }
3930
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003931 if (curwin_save != curwin || curbuf_save != curbuf)
3932 {
3933 EMSG(_(e_complwin));
3934 goto theend;
3935 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00003936 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003937 check_cursor();
3938 if (!equalpos(curwin->w_cursor, pos))
3939 {
3940 EMSG(_(e_compldel));
3941 goto theend;
3942 }
Bram Moolenaar82139082011-09-14 16:52:09 +02003943
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003944 if (matchlist != NULL)
3945 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02003946 else if (matchdict != NULL)
3947 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003948
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003949theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02003950 if (matchdict != NULL)
3951 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003952 if (matchlist != NULL)
3953 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003954}
3955#endif /* FEAT_COMPL_FUNC */
3956
Bram Moolenaar39f05632006-03-19 22:15:26 +00003957#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003958/*
3959 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003960 */
3961 static void
3962ins_compl_add_list(list)
3963 list_T *list;
3964{
3965 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003966 int dir = compl_direction;
3967
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003968 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003969 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003970 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003971 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3972 /* if dir was BACKWARD then honor it just once */
3973 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003974 else if (did_emsg)
3975 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003976 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003977}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003978
3979/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003980 * Add completions from a dict.
3981 */
3982 static void
3983ins_compl_add_dict(dict)
3984 dict_T *dict;
3985{
3986 dictitem_T *refresh;
3987 dictitem_T *words;
3988
3989 /* Check for optional "refresh" item. */
3990 compl_opt_refresh_always = FALSE;
3991 refresh = dict_find(dict, (char_u *)"refresh", 7);
3992 if (refresh != NULL && refresh->di_tv.v_type == VAR_STRING)
3993 {
3994 char_u *v = refresh->di_tv.vval.v_string;
3995
3996 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
3997 compl_opt_refresh_always = TRUE;
3998 }
3999
4000 /* Add completions from a "words" list. */
4001 words = dict_find(dict, (char_u *)"words", 5);
4002 if (words != NULL && words->di_tv.v_type == VAR_LIST)
4003 ins_compl_add_list(words->di_tv.vval.v_list);
4004}
4005
4006/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004007 * Add a match to the list of matches from a typeval_T.
4008 * If the given string is already in the list of completions, then return
4009 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4010 * maybe because alloc() returns NULL, then FAIL is returned.
4011 */
4012 int
4013ins_compl_add_tv(tv, dir)
4014 typval_T *tv;
4015 int dir;
4016{
4017 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004018 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004019 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004020 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004021 char_u *(cptext[CPT_COUNT]);
4022
4023 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4024 {
4025 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4026 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4027 (char_u *)"abbr", FALSE);
4028 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4029 (char_u *)"menu", FALSE);
4030 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4031 (char_u *)"kind", FALSE);
4032 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4033 (char_u *)"info", FALSE);
4034 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4035 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004036 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004037 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004038 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4039 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004040 }
4041 else
4042 {
4043 word = get_tv_string_chk(tv);
4044 vim_memset(cptext, 0, sizeof(cptext));
4045 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004046 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004047 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004048 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004049}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004050#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004051
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004052/*
4053 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004054 * The search starts at position "ini" in curbuf and in the direction
4055 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004056 * When "compl_started" is FALSE start at that position, otherwise continue
4057 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 * This may return before finding all the matches.
4059 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 */
4061 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004062ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
4065 static pos_T first_match_pos;
4066 static pos_T last_match_pos;
4067 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004068 static int found_all = FALSE; /* Found all matches of a
4069 certain type. */
4070 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
Bram Moolenaar572cb562005-08-05 21:35:02 +00004072 pos_T *pos;
4073 char_u **matches;
4074 int save_p_scs;
4075 int save_p_ws;
4076 int save_p_ic;
4077 int i;
4078 int num_matches;
4079 int len;
4080 int found_new_match;
4081 int type = ctrl_x_mode;
4082 char_u *ptr;
4083 char_u *dict = NULL;
4084 int dict_f = 0;
4085 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 {
4089 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4090 ins_buf->b_scanned = 0;
4091 found_all = FALSE;
4092 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004094 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 last_match_pos = first_match_pos = *ini;
4096 }
4097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004099 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4101 for (;;)
4102 {
4103 found_new_match = FAIL;
4104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004105 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * or if found_all says this entry is done. For ^X^L only use the
4107 * entries from 'complete' that look in loaded buffers. */
4108 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004109 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
4111 found_all = FALSE;
4112 while (*e_cpt == ',' || *e_cpt == ' ')
4113 e_cpt++;
4114 if (*e_cpt == '.' && !curbuf->b_scanned)
4115 {
4116 ins_buf = curbuf;
4117 first_match_pos = *ini;
4118 /* So that ^N can match word immediately after cursor */
4119 if (ctrl_x_mode == 0)
4120 dec(&first_match_pos);
4121 last_match_pos = first_match_pos;
4122 type = 0;
4123 }
4124 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4125 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4126 {
4127 /* Scan a buffer, but not the current one. */
4128 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4129 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 first_match_pos.col = last_match_pos.col = 0;
4132 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4133 last_match_pos.lnum = 0;
4134 type = 0;
4135 }
4136 else /* unloaded buffer, scan like dictionary */
4137 {
4138 found_all = TRUE;
4139 if (ins_buf->b_fname == NULL)
4140 continue;
4141 type = CTRL_X_DICTIONARY;
4142 dict = ins_buf->b_fname;
4143 dict_f = DICT_EXACT;
4144 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004145 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 ins_buf->b_fname == NULL
4147 ? buf_spname(ins_buf)
4148 : ins_buf->b_sfname == NULL
4149 ? (char *)ins_buf->b_fname
4150 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004151 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 else if (*e_cpt == NUL)
4154 break;
4155 else
4156 {
4157 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4158 type = -1;
4159 else if (*e_cpt == 'k' || *e_cpt == 's')
4160 {
4161 if (*e_cpt == 'k')
4162 type = CTRL_X_DICTIONARY;
4163 else
4164 type = CTRL_X_THESAURUS;
4165 if (*++e_cpt != ',' && *e_cpt != NUL)
4166 {
4167 dict = e_cpt;
4168 dict_f = DICT_FIRST;
4169 }
4170 }
4171#ifdef FEAT_FIND_ID
4172 else if (*e_cpt == 'i')
4173 type = CTRL_X_PATH_PATTERNS;
4174 else if (*e_cpt == 'd')
4175 type = CTRL_X_PATH_DEFINES;
4176#endif
4177 else if (*e_cpt == ']' || *e_cpt == 't')
4178 {
4179 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004180 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004181 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 else
4184 type = -1;
4185
4186 /* in any case e_cpt is advanced to the next entry */
4187 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4188
4189 found_all = TRUE;
4190 if (type == -1)
4191 continue;
4192 }
4193 }
4194
4195 switch (type)
4196 {
4197 case -1:
4198 break;
4199#ifdef FEAT_FIND_ID
4200 case CTRL_X_PATH_PATTERNS:
4201 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004202 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004203 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4207 (linenr_T)1, (linenr_T)MAXLNUM);
4208 break;
4209#endif
4210
4211 case CTRL_X_DICTIONARY:
4212 case CTRL_X_THESAURUS:
4213 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004214 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 : (type == CTRL_X_THESAURUS
4216 ? (*curbuf->b_p_tsr == NUL
4217 ? p_tsr
4218 : curbuf->b_p_tsr)
4219 : (*curbuf->b_p_dict == NUL
4220 ? p_dict
4221 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004222 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004223 dict != NULL ? dict_f
4224 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 dict = NULL;
4226 break;
4227
4228 case CTRL_X_TAGS:
4229 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4230 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004231 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004233 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 * of matches is found when compl_pattern is empty */
4235 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4237 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4238 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4239 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004240 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 p_ic = save_p_ic;
4243 break;
4244
4245 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4248 {
4249
4250 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004252 ins_compl_add_matches(num_matches, matches,
4253#ifdef CASE_INSENSITIVE_FILENAME
4254 TRUE
4255#else
4256 FALSE
4257#endif
4258 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 break;
4261
4262 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004263 if (expand_cmdline(&compl_xp, compl_pattern,
4264 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004266 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004269#ifdef FEAT_COMPL_FUNC
4270 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004272 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004273 break;
4274#endif
4275
Bram Moolenaar488c6512005-08-11 20:09:58 +00004276 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004277#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004278 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004279 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004280 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004281 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004282#endif
4283 break;
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 default: /* normal ^P/^N and ^X^L */
4286 /*
4287 * If 'infercase' is set, don't use 'smartcase' here
4288 */
4289 save_p_scs = p_scs;
4290 if (ins_buf->b_p_inf)
4291 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 /* buffers other than curbuf are scanned from the beginning or the
4294 * end but never from the middle, thus setting nowrapscan in this
4295 * buffers is a good idea, on the other hand, we always set
4296 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4297 save_p_ws = p_ws;
4298 if (ins_buf != curbuf)
4299 p_ws = FALSE;
4300 else if (*e_cpt == '.')
4301 p_ws = TRUE;
4302 for (;;)
4303 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004304 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004306 ++msg_silent; /* Don't want messages for wrapscan. */
4307
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004308 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4309 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004311 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004313 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004315 found_new_match = searchit(NULL, ins_buf, pos,
4316 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004317 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004318 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004319 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004322 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 first_match_pos = *pos;
4325 last_match_pos = *pos;
4326 }
4327 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004328 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 found_new_match = FAIL;
4330 if (found_new_match == FAIL)
4331 {
4332 if (ins_buf == curbuf)
4333 found_all = TRUE;
4334 break;
4335 }
4336
4337 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 && ini->lnum == pos->lnum
4340 && ini->col == pos->col)
4341 continue;
4342 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4343 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4344 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004345 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
4347 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4348 continue;
4349 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4350 if (!p_paste)
4351 ptr = skipwhite(ptr);
4352 }
4353 len = (int)STRLEN(ptr);
4354 }
4355 else
4356 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 char_u *tmp_ptr = ptr;
4358
4359 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 /* Skip if already inside a word. */
4363 if (vim_iswordp(tmp_ptr))
4364 continue;
4365 /* Find start of next word. */
4366 tmp_ptr = find_word_start(tmp_ptr);
4367 }
4368 /* Find end of this word. */
4369 tmp_ptr = find_word_end(tmp_ptr);
4370 len = (int)(tmp_ptr - ptr);
4371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372 if ((compl_cont_status & CONT_ADDING)
4373 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 {
4375 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4376 {
4377 /* Try next line, if any. the new word will be
4378 * "join" as if the normal command "J" was used.
4379 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 * works -- Acevedo */
4382 STRNCPY(IObuff, ptr, len);
4383 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4384 tmp_ptr = ptr = skipwhite(ptr);
4385 /* Find start of next word. */
4386 tmp_ptr = find_word_start(tmp_ptr);
4387 /* Find end of next word. */
4388 tmp_ptr = find_word_end(tmp_ptr);
4389 if (tmp_ptr > ptr)
4390 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004391 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004393 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 IObuff[len++] = ' ';
4395 /* IObuf =~ "\k.* ", thus len >= 2 */
4396 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004397 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 || (vim_strchr(p_cpo, CPO_JOINSP)
4399 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004400 && (IObuff[len - 2] == '?'
4401 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 IObuff[len++] = ' ';
4403 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004404 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (tmp_ptr - ptr >= IOSIZE - len)
4406 tmp_ptr = ptr + IOSIZE - len - 1;
4407 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4408 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004409 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 IObuff[len] = NUL;
4412 ptr = IObuff;
4413 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 continue;
4416 }
4417 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004418 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004419 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004420 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
4422 found_new_match = OK;
4423 break;
4424 }
4425 }
4426 p_scs = save_p_scs;
4427 p_ws = save_p_ws;
4428 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004429
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004431 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004432 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 found_new_match = OK;
4434
4435 /* break the loop for specialized modes (use 'complete' just for the
4436 * generic ctrl_x_mode == 0) or when we've found a new match */
4437 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004439 {
4440 if (got_int)
4441 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004442 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004443 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004444 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004445
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004446 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4447 || compl_interrupted)
4448 break;
4449 compl_started = TRUE;
4450 }
4451 else
4452 {
4453 /* Mark a buffer scanned when it has been scanned completely */
4454 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4455 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004457 compl_started = FALSE;
4458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4463 && *e_cpt == NUL) /* Got to end of 'complete' */
4464 found_new_match = FAIL;
4465
4466 i = -1; /* total of matches, unknown */
4467 if (found_new_match == FAIL
4468 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4469 i = ins_compl_make_cyclic();
4470
4471 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472 * just been made cyclic then we have to move compl_curr_match to the next
4473 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004474 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4475 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476 if (compl_curr_match == NULL)
4477 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return i;
4479}
4480
4481/* Delete the old text being completed. */
4482 static void
4483ins_compl_delete()
4484{
4485 int i;
4486
4487 /*
4488 * In insert mode: Delete the typed part.
4489 * In replace mode: Put the old characters back, if any.
4490 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 backspace_until_column(i);
4493 changed_cline_bef_curs();
4494}
4495
4496/* Insert the new text being completed. */
4497 static void
4498ins_compl_insert()
4499{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004500 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004501 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4502 compl_used_match = FALSE;
4503 else
4504 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505}
4506
4507/*
4508 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004509 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4510 * get more completions. If it is FALSE, then we just do nothing when there
4511 * are no more completions in a given direction. The latter case is used when
4512 * we are still in the middle of finding completions, to allow browsing
4513 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 * Return the total number of matches, or -1 if still unknown -- webb.
4515 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004516 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4517 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 *
4519 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004520 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4521 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004524ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004526 int count; /* repeat completion this many times; should
4527 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004528 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529{
4530 int num_matches = -1;
4531 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004532 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004533 compl_T *found_compl = NULL;
4534 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004535 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004537 if (compl_leader != NULL
4538 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004540 /* Set "compl_shown_match" to the actually shown match, it may differ
4541 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004542 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004543 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004544 && compl_shown_match->cp_next != NULL
4545 && compl_shown_match->cp_next != compl_first_match)
4546 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004547
4548 /* If we didn't find it searching forward, and compl_shows_dir is
4549 * backward, find the last match. */
4550 if (compl_shows_dir == BACKWARD
4551 && !ins_compl_equal(compl_shown_match,
4552 compl_leader, (int)STRLEN(compl_leader))
4553 && (compl_shown_match->cp_next == NULL
4554 || compl_shown_match->cp_next == compl_first_match))
4555 {
4556 while (!ins_compl_equal(compl_shown_match,
4557 compl_leader, (int)STRLEN(compl_leader))
4558 && compl_shown_match->cp_prev != NULL
4559 && compl_shown_match->cp_prev != compl_first_match)
4560 compl_shown_match = compl_shown_match->cp_prev;
4561 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004562 }
4563
4564 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004565 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 /* Delete old text to be replaced */
4567 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004568
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004569 /* When finding the longest common text we stick at the original text,
4570 * don't let CTRL-N or CTRL-P move to the first match. */
4571 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4572
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004573 /* When restarting the search don't insert the first match either. */
4574 if (compl_restarting)
4575 {
4576 advance = FALSE;
4577 compl_restarting = FALSE;
4578 }
4579
Bram Moolenaare3226be2005-12-18 22:10:00 +00004580 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4581 * around. */
4582 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004584 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004586 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004587 found_end = (compl_first_match != NULL
4588 && (compl_shown_match->cp_next == compl_first_match
4589 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004590 }
4591 else if (compl_shows_dir == BACKWARD
4592 && compl_shown_match->cp_prev != NULL)
4593 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004594 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004595 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004596 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004599 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004600 if (!allow_get_expansion)
4601 {
4602 if (advance)
4603 {
4604 if (compl_shows_dir == BACKWARD)
4605 compl_pending -= todo + 1;
4606 else
4607 compl_pending += todo + 1;
4608 }
4609 return -1;
4610 }
4611
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004612 if (advance)
4613 {
4614 if (compl_shows_dir == BACKWARD)
4615 --compl_pending;
4616 else
4617 ++compl_pending;
4618 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004619
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004620 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004621 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004622
4623 /* handle any pending completions */
4624 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004625 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004626 {
4627 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4628 {
4629 compl_shown_match = compl_shown_match->cp_next;
4630 --compl_pending;
4631 }
4632 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4633 {
4634 compl_shown_match = compl_shown_match->cp_prev;
4635 ++compl_pending;
4636 }
4637 else
4638 break;
4639 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004640 found_end = FALSE;
4641 }
4642 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4643 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004644 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004645 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004646 ++todo;
4647 else
4648 /* Remember a matching item. */
4649 found_compl = compl_shown_match;
4650
4651 /* Stop at the end of the list when we found a usable match. */
4652 if (found_end)
4653 {
4654 if (found_compl != NULL)
4655 {
4656 compl_shown_match = found_compl;
4657 break;
4658 }
4659 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004663 /* Insert the text of the new completion, or the compl_leader. */
4664 if (insert_match)
4665 {
4666 if (!compl_get_longest || compl_used_match)
4667 ins_compl_insert();
4668 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004669 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004670 }
4671 else
4672 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 if (!allow_get_expansion)
4675 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004676 /* may undisplay the popup menu first */
4677 ins_compl_upd_pum();
4678
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004679 /* redraw to show the user what was inserted */
4680 update_screen(0);
4681
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004682 /* display the updated popup menu */
4683 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004684#ifdef FEAT_GUI
4685 if (gui.in_use)
4686 {
4687 /* Show the cursor after the match, not after the redrawn text. */
4688 setcursor();
4689 out_flush();
4690 gui_update_cursor(FALSE, FALSE);
4691 }
4692#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 /* Delete old text to be replaced, since we're still searching and
4695 * don't want to match ourselves! */
4696 ins_compl_delete();
4697 }
4698
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004699 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004700 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004701 compl_enter_selects = !insert_match && compl_match_array != NULL;
4702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /*
4704 * Show the file name for the match (if any)
4705 * Truncate the file name to avoid a wait for return.
4706 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004707 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004710 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (i <= 0)
4712 i = 0;
4713 else
4714 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004715 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 msg(IObuff);
4717 redraw_cmdline = FALSE; /* don't overwrite! */
4718 }
4719
4720 return num_matches;
4721}
4722
4723/*
4724 * Call this while finding completions, to check whether the user has hit a key
4725 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004726 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004728 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 */
4730 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004731ins_compl_check_keys(frequency)
4732 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 static int count = 0;
4735
4736 int c;
4737
4738 /* Don't check when reading keys from a script. That would break the test
4739 * scripts */
4740 if (using_script())
4741 return;
4742
4743 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004744 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return;
4746 count = 0;
4747
Bram Moolenaara260a972006-06-23 19:36:29 +00004748 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4749 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (c != NUL)
4752 {
4753 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4754 {
4755 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004756 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004757 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4758 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004760 else
4761 {
4762 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004763 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004764 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004765 if (c != K_IGNORE)
4766 {
4767 /* Don't interrupt completion when the character wasn't typed,
4768 * e.g., when doing @q to replay keys. */
4769 if (c != Ctrl_R && KeyTyped)
4770 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004771
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004772 vungetc(c);
4773 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004776 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004777 {
4778 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4779
4780 compl_pending = 0;
4781 (void)ins_compl_next(FALSE, todo, TRUE);
4782 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004783}
4784
4785/*
4786 * Decide the direction of Insert mode complete from the key typed.
4787 * Returns BACKWARD or FORWARD.
4788 */
4789 static int
4790ins_compl_key2dir(c)
4791 int c;
4792{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004793 if (c == Ctrl_P || c == Ctrl_L
4794 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4795 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004796 return BACKWARD;
4797 return FORWARD;
4798}
4799
4800/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004801 * Return TRUE for keys that are used for completion only when the popup menu
4802 * is visible.
4803 */
4804 static int
4805ins_compl_pum_key(c)
4806 int c;
4807{
4808 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004809 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4810 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004811}
4812
4813/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004814 * Decide the number of completions to move forward.
4815 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4816 */
4817 static int
4818ins_compl_key2count(c)
4819 int c;
4820{
4821 int h;
4822
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004823 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004824 {
4825 h = pum_get_height();
4826 if (h > 3)
4827 h -= 2; /* keep some context */
4828 return h;
4829 }
4830 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831}
4832
4833/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004834 * Return TRUE if completion with "c" should insert the match, FALSE if only
4835 * to change the currently selected completion.
4836 */
4837 static int
4838ins_compl_use_match(c)
4839 int c;
4840{
4841 switch (c)
4842 {
4843 case K_UP:
4844 case K_DOWN:
4845 case K_PAGEDOWN:
4846 case K_KPAGEDOWN:
4847 case K_S_DOWN:
4848 case K_PAGEUP:
4849 case K_KPAGEUP:
4850 case K_S_UP:
4851 return FALSE;
4852 }
4853 return TRUE;
4854}
4855
4856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 * Do Insert mode completion.
4858 * Called when character "c" was typed, which has a meaning for completion.
4859 * Returns OK if completion was done, FAIL if something failed (out of mem).
4860 */
4861 static int
4862ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004863 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004865 char_u *line;
4866 int startcol = 0; /* column where searched text starts */
4867 colnr_T curs_col; /* cursor column */
4868 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004869 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaare3226be2005-12-18 22:10:00 +00004871 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874 /* First time we hit ^N or ^P (in a row, I mean) */
4875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 did_ai = FALSE;
4877#ifdef FEAT_SMARTINDENT
4878 did_si = FALSE;
4879 can_si = FALSE;
4880 can_si_back = FALSE;
4881#endif
4882 if (stop_arrow() == FAIL)
4883 return FAIL;
4884
4885 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004886 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004887 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004889 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004890 * "compl_startpos" to the cursor as a pattern to add a new word
4891 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004892 * "compl_startpos" is not in the same line as the cursor then fix it
4893 * (the line has been split because it was longer than 'tw'). if SOL
4894 * is set then skip the previous pattern, a word at the beginning of
4895 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004896 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4897 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
4899 /*
4900 * it is a continued search
4901 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004902 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4904 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4905 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004908 /* line (probably) wrapped, set compl_startpos to the
4909 * first non_blank in the line, if it is not a wordchar
4910 * include it to get a better pattern, but then we don't
4911 * want the "\\<" prefix, check it bellow */
4912 compl_col = (colnr_T)(skipwhite(line) - line);
4913 compl_startpos.col = compl_col;
4914 compl_startpos.lnum = curwin->w_cursor.lnum;
4915 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 {
4919 /* S_IPOS was set when we inserted a word that was at the
4920 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004921 * mode but first we need to redefine compl_startpos */
4922 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004924 compl_cont_status |= CONT_SOL;
4925 compl_startpos.col = (colnr_T)(skipwhite(
4926 line + compl_length
4927 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004929 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004931 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004932 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004933 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004935 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004937 compl_cont_status &= ~CONT_SOL;
4938 compl_length = (IOSIZE - MIN_SPACE);
4939 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004941 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4942 if (compl_length < 1)
4943 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004946 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004948 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004951 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004953 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004955 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004957 compl_cont_status = 0;
4958 compl_cont_status |= CONT_N_ADDS;
4959 compl_startpos = curwin->w_cursor;
4960 startcol = (int)curs_col;
4961 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963
4964 /* Work out completion pattern and original text -- webb */
4965 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4966 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004967 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4969 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004970 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004972 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004974 compl_col += ++startcol;
4975 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004978 compl_pattern = str_foldcase(line + compl_col,
4979 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004981 compl_pattern = vim_strnsave(line + compl_col,
4982 compl_length);
4983 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 return FAIL;
4985 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004986 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
4988 char_u *prefix = (char_u *)"\\<";
4989
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004990 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004991 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004992 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004993 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004995 if (!vim_iswordp(line + compl_col)
4996 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 && (
4998#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004999 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005001 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002#endif
5003 )))
5004 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005005 STRCPY((char *)compl_pattern, prefix);
5006 (void)quote_meta(compl_pattern + STRLEN(prefix),
5007 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005009 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005011 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005013 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#endif
5015 )
5016 {
5017 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005018 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5019 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005021 compl_col += curs_col;
5022 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 else
5025 {
5026#ifdef FEAT_MBYTE
5027 /* Search the point of change class of multibyte character
5028 * or not a word single byte character backward. */
5029 if (has_mbyte)
5030 {
5031 int base_class;
5032 int head_off;
5033
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005034 startcol -= (*mb_head_off)(line, line + startcol);
5035 base_class = mb_get_class(line + startcol);
5036 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 head_off = (*mb_head_off)(line, line + startcol);
5039 if (base_class != mb_get_class(line + startcol
5040 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005042 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 }
5045 else
5046#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005049 compl_col += ++startcol;
5050 compl_length = (int)curs_col - startcol;
5051 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 /* Only match word with at least two chars -- webb
5054 * there's no need to call quote_meta,
5055 * alloc(7) is enough -- Acevedo
5056 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005057 compl_pattern = alloc(7);
5058 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 STRCPY((char *)compl_pattern, "\\<");
5061 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5062 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 else
5065 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005066 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005067 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005068 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 STRCPY((char *)compl_pattern, "\\<");
5071 (void)quote_meta(compl_pattern + 2, line + compl_col,
5072 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 }
5075 }
5076 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5077 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005078 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005079 compl_length = (int)curs_col - (int)compl_col;
5080 if (compl_length < 0) /* cursor in indent: empty pattern */
5081 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005083 compl_pattern = str_foldcase(line + compl_col, compl_length,
5084 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5087 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 return FAIL;
5089 }
5090 else if (ctrl_x_mode == CTRL_X_FILES)
5091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005092 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 compl_col += ++startcol;
5095 compl_length = (int)curs_col - startcol;
5096 compl_pattern = addstar(line + compl_col, compl_length,
5097 EXPAND_FILES);
5098 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 return FAIL;
5100 }
5101 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5102 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 compl_pattern = vim_strnsave(line, curs_col);
5104 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 set_cmd_context(&compl_xp, compl_pattern,
5107 (int)STRLEN(compl_pattern), curs_col);
5108 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5109 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005110 /* No completion possible, use an empty pattern to get a
5111 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005112 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005113 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005114 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5115 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005117 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005118 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005119#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005120 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005121 * Call user defined function 'completefunc' with "a:findstart"
5122 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005123 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005124 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005125 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005126 char_u *funcname;
5127 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005128 win_T *curwin_save;
5129 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005130
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005131 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005132 * string */
5133 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5134 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5135 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005136 {
5137 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5138 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005139 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005140 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005141
5142 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005143 args[1] = NULL;
5144 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005145 curwin_save = curwin;
5146 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005147 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005148 if (curwin_save != curwin || curbuf_save != curbuf)
5149 {
5150 EMSG(_(e_complwin));
5151 return FAIL;
5152 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005153 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005154 check_cursor();
5155 if (!equalpos(curwin->w_cursor, pos))
5156 {
5157 EMSG(_(e_compldel));
5158 return FAIL;
5159 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005160
Bram Moolenaar82139082011-09-14 16:52:09 +02005161 /*
5162 * Reset extended parameters of completion, when start new
5163 * completion.
5164 */
5165 compl_opt_refresh_always = FALSE;
5166
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005167 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005168 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005169 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005170 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005171 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 /* Setup variables for completion. Need to obtain "line" again,
5174 * it may have become invalid. */
5175 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005176 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5178 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005179#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 return FAIL;
5181 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005182 else if (ctrl_x_mode == CTRL_X_SPELL)
5183 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005184#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005185 if (spell_bad_len > 0)
5186 compl_col = curs_col - spell_bad_len;
5187 else
5188 compl_col = spell_word_start(startcol);
5189 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005190 {
5191 compl_length = 0;
5192 compl_col = curs_col;
5193 }
5194 else
5195 {
5196 spell_expand_check_cap(compl_col);
5197 compl_length = (int)curs_col - compl_col;
5198 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005199 /* Need to obtain "line" again, it may have become invalid. */
5200 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005201 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5202 if (compl_pattern == NULL)
5203#endif
5204 return FAIL;
5205 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005206 else
5207 {
5208 EMSG2(_(e_intern2), "ins_complete()");
5209 return FAIL;
5210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005212 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
5214 edit_submode_pre = (char_u *)_(" Adding");
5215 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5216 {
5217 /* Insert a new line, keep indentation but ignore 'comments' */
5218#ifdef FEAT_COMMENTS
5219 char_u *old = curbuf->b_p_com;
5220
5221 curbuf->b_p_com = (char_u *)"";
5222#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 compl_startpos.lnum = curwin->w_cursor.lnum;
5224 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ins_eol('\r');
5226#ifdef FEAT_COMMENTS
5227 curbuf->b_p_com = old;
5228#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 compl_length = 0;
5230 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232 }
5233 else
5234 {
5235 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 if (compl_cont_status & CONT_LOCAL)
5240 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
5242 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5243
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005244 /* Always add completion for the original text. */
5245 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005246 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5247 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005248 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005250 vim_free(compl_pattern);
5251 compl_pattern = NULL;
5252 vim_free(compl_orig_text);
5253 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return FAIL;
5255 }
5256
5257 /* showmode might reset the internal line pointers, so it must
5258 * be called before line = ml_get(), or when this address is no
5259 * longer needed. -- Acevedo.
5260 */
5261 edit_submode_extra = (char_u *)_("-- Searching...");
5262 edit_submode_highl = HLF_COUNT;
5263 showmode();
5264 edit_submode_extra = NULL;
5265 out_flush();
5266 }
5267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005268 compl_shown_match = compl_curr_match;
5269 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005272 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005274 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005275 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005277 /* may undisplay the popup menu */
5278 ins_compl_upd_pum();
5279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 if (n > 1) /* all matches have been found */
5281 compl_matches = n;
5282 compl_curr_match = compl_shown_match;
5283 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
Bram Moolenaard68071d2006-05-02 22:08:30 +00005285 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5286 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (got_int && !global_busy)
5288 {
5289 (void)vgetc();
5290 got_int = FALSE;
5291 }
5292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005293 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005294 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5297 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5299 edit_submode_highl = HLF_E;
5300 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5301 * because we couldn't expand anything at first place, but if we used
5302 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5303 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 if ( compl_length > 1
5305 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 || (ctrl_x_mode != 0
5307 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5308 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
5311
Bram Moolenaar572cb562005-08-05 21:35:02 +00005312 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317 if (edit_submode_extra == NULL)
5318 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005319 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
5321 edit_submode_extra = (char_u *)_("Back at original");
5322 edit_submode_highl = HLF_W;
5323 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 edit_submode_extra = (char_u *)_("Word from other line");
5327 edit_submode_highl = HLF_COUNT;
5328 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005329 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 edit_submode_extra = (char_u *)_("The only match");
5332 edit_submode_highl = HLF_COUNT;
5333 }
5334 else
5335 {
5336 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005337 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005339 int number = 0;
5340 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
5344 /* search backwards for the first valid (!= -1) number.
5345 * This should normally succeed already at the first loop
5346 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005347 for (match = compl_curr_match->cp_prev; match != NULL
5348 && match != compl_first_match;
5349 match = match->cp_prev)
5350 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005352 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 break;
5354 }
5355 if (match != NULL)
5356 /* go up and assign all numbers which are not assigned
5357 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005358 for (match = match->cp_next;
5359 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005360 match = match->cp_next)
5361 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 else /* BACKWARD */
5364 {
5365 /* search forwards (upwards) for the first valid (!= -1)
5366 * number. This should normally succeed already at the
5367 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005368 for (match = compl_curr_match->cp_next; match != NULL
5369 && match != compl_first_match;
5370 match = match->cp_next)
5371 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005373 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 break;
5375 }
5376 if (match != NULL)
5377 /* go down and assign all numbers which are not
5378 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005379 for (match = match->cp_prev; match
5380 && match->cp_number == -1;
5381 match = match->cp_prev)
5382 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384 }
5385
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005386 /* The match should always have a sequence number now, this is
5387 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005388 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005390 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5391 * Translations may need more than twice that. */
5392 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005395 vim_snprintf((char *)match_ref, sizeof(match_ref),
5396 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005397 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005399 vim_snprintf((char *)match_ref, sizeof(match_ref),
5400 _("match %d"),
5401 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 edit_submode_extra = match_ref;
5403 edit_submode_highl = HLF_R;
5404 if (dollar_vcol)
5405 curs_columns(FALSE);
5406 }
5407 }
5408 }
5409
5410 /* Show a message about what (completion) mode we're in. */
5411 showmode();
5412 if (edit_submode_extra != NULL)
5413 {
5414 if (!p_smd)
5415 msg_attr(edit_submode_extra,
5416 edit_submode_highl < HLF_COUNT
5417 ? hl_attr(edit_submode_highl) : 0);
5418 }
5419 else
5420 msg_clr_cmdline(); /* necessary for "noshowmode" */
5421
Bram Moolenaard68071d2006-05-02 22:08:30 +00005422 /* Show the popup menu, unless we got interrupted. */
5423 if (!compl_interrupted)
5424 {
5425 /* RedrawingDisabled may be set when invoked through complete(). */
5426 n = RedrawingDisabled;
5427 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005428
5429 /* If the cursor moved we need to remove the pum first. */
5430 setcursor();
5431 if (save_w_wrow != curwin->w_wrow)
5432 ins_compl_del_pum();
5433
Bram Moolenaard68071d2006-05-02 22:08:30 +00005434 ins_compl_show_pum();
5435 setcursor();
5436 RedrawingDisabled = n;
5437 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005438 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005439 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return OK;
5442}
5443
5444/*
5445 * Looks in the first "len" chars. of "src" for search-metachars.
5446 * If dest is not NULL the chars. are copied there quoting (with
5447 * a backslash) the metachars, and dest would be NUL terminated.
5448 * Returns the length (needed) of dest
5449 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005450 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451quote_meta(dest, src, len)
5452 char_u *dest;
5453 char_u *src;
5454 int len;
5455{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005456 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005458 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
5460 switch (*src)
5461 {
5462 case '.':
5463 case '*':
5464 case '[':
5465 if (ctrl_x_mode == CTRL_X_DICTIONARY
5466 || ctrl_x_mode == CTRL_X_THESAURUS)
5467 break;
5468 case '~':
5469 if (!p_magic) /* quote these only if magic is set */
5470 break;
5471 case '\\':
5472 if (ctrl_x_mode == CTRL_X_DICTIONARY
5473 || ctrl_x_mode == CTRL_X_THESAURUS)
5474 break;
5475 case '^': /* currently it's not needed. */
5476 case '$':
5477 m++;
5478 if (dest != NULL)
5479 *dest++ = '\\';
5480 break;
5481 }
5482 if (dest != NULL)
5483 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005484# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 /* Copy remaining bytes of a multibyte character. */
5486 if (has_mbyte)
5487 {
5488 int i, mb_len;
5489
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005490 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (mb_len > 0 && len >= mb_len)
5492 for (i = 0; i < mb_len; ++i)
5493 {
5494 --len;
5495 ++src;
5496 if (dest != NULL)
5497 *dest++ = *src;
5498 }
5499 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 if (dest != NULL)
5503 *dest = NUL;
5504
5505 return m;
5506}
5507#endif /* FEAT_INS_EXPAND */
5508
5509/*
5510 * Next character is interpreted literally.
5511 * A one, two or three digit decimal number is interpreted as its byte value.
5512 * If one or two digits are entered, the next character is given to vungetc().
5513 * For Unicode a character > 255 may be returned.
5514 */
5515 int
5516get_literal()
5517{
5518 int cc;
5519 int nc;
5520 int i;
5521 int hex = FALSE;
5522 int octal = FALSE;
5523#ifdef FEAT_MBYTE
5524 int unicode = 0;
5525#endif
5526
5527 if (got_int)
5528 return Ctrl_C;
5529
5530#ifdef FEAT_GUI
5531 /*
5532 * In GUI there is no point inserting the internal code for a special key.
5533 * It is more useful to insert the string "<KEY>" instead. This would
5534 * probably be useful in a text window too, but it would not be
5535 * vi-compatible (maybe there should be an option for it?) -- webb
5536 */
5537 if (gui.in_use)
5538 ++allow_keys;
5539#endif
5540#ifdef USE_ON_FLY_SCROLL
5541 dont_scroll = TRUE; /* disallow scrolling here */
5542#endif
5543 ++no_mapping; /* don't map the next key hits */
5544 cc = 0;
5545 i = 0;
5546 for (;;)
5547 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005548 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549#ifdef FEAT_CMDL_INFO
5550 if (!(State & CMDLINE)
5551# ifdef FEAT_MBYTE
5552 && MB_BYTE2LEN_CHECK(nc) == 1
5553# endif
5554 )
5555 add_to_showcmd(nc);
5556#endif
5557 if (nc == 'x' || nc == 'X')
5558 hex = TRUE;
5559 else if (nc == 'o' || nc == 'O')
5560 octal = TRUE;
5561#ifdef FEAT_MBYTE
5562 else if (nc == 'u' || nc == 'U')
5563 unicode = nc;
5564#endif
5565 else
5566 {
5567 if (hex
5568#ifdef FEAT_MBYTE
5569 || unicode != 0
5570#endif
5571 )
5572 {
5573 if (!vim_isxdigit(nc))
5574 break;
5575 cc = cc * 16 + hex2nr(nc);
5576 }
5577 else if (octal)
5578 {
5579 if (nc < '0' || nc > '7')
5580 break;
5581 cc = cc * 8 + nc - '0';
5582 }
5583 else
5584 {
5585 if (!VIM_ISDIGIT(nc))
5586 break;
5587 cc = cc * 10 + nc - '0';
5588 }
5589
5590 ++i;
5591 }
5592
5593 if (cc > 255
5594#ifdef FEAT_MBYTE
5595 && unicode == 0
5596#endif
5597 )
5598 cc = 255; /* limit range to 0-255 */
5599 nc = 0;
5600
5601 if (hex) /* hex: up to two chars */
5602 {
5603 if (i >= 2)
5604 break;
5605 }
5606#ifdef FEAT_MBYTE
5607 else if (unicode) /* Unicode: up to four or eight chars */
5608 {
5609 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5610 break;
5611 }
5612#endif
5613 else if (i >= 3) /* decimal or octal: up to three chars */
5614 break;
5615 }
5616 if (i == 0) /* no number entered */
5617 {
5618 if (nc == K_ZERO) /* NUL is stored as NL */
5619 {
5620 cc = '\n';
5621 nc = 0;
5622 }
5623 else
5624 {
5625 cc = nc;
5626 nc = 0;
5627 }
5628 }
5629
5630 if (cc == 0) /* NUL is stored as NL */
5631 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005632#ifdef FEAT_MBYTE
5633 if (enc_dbcs && (cc & 0xff) == 0)
5634 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5635 second byte will cause trouble! */
5636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
5638 --no_mapping;
5639#ifdef FEAT_GUI
5640 if (gui.in_use)
5641 --allow_keys;
5642#endif
5643 if (nc)
5644 vungetc(nc);
5645 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5646 return cc;
5647}
5648
5649/*
5650 * Insert character, taking care of special keys and mod_mask
5651 */
5652 static void
5653insert_special(c, allow_modmask, ctrlv)
5654 int c;
5655 int allow_modmask;
5656 int ctrlv; /* c was typed after CTRL-V */
5657{
5658 char_u *p;
5659 int len;
5660
5661 /*
5662 * Special function key, translate into "<Key>". Up to the last '>' is
5663 * inserted with ins_str(), so as not to replace characters in replace
5664 * mode.
5665 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5666 * unless 'allow_modmask' is TRUE.
5667 */
5668#ifdef MACOS
5669 /* Command-key never produces a normal key */
5670 if (mod_mask & MOD_MASK_CMD)
5671 allow_modmask = TRUE;
5672#endif
5673 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5674 {
5675 p = get_special_key_name(c, mod_mask);
5676 len = (int)STRLEN(p);
5677 c = p[len - 1];
5678 if (len > 2)
5679 {
5680 if (stop_arrow() == FAIL)
5681 return;
5682 p[len - 1] = NUL;
5683 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005684 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 ctrlv = FALSE;
5686 }
5687 }
5688 if (stop_arrow() == OK)
5689 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5690}
5691
5692/*
5693 * Special characters in this context are those that need processing other
5694 * than the simple insertion that can be performed here. This includes ESC
5695 * which terminates the insert, and CR/NL which need special processing to
5696 * open up a new line. This routine tries to optimize insertions performed by
5697 * the "redo", "undo" or "put" commands, so it needs to know when it should
5698 * stop and defer processing to the "normal" mechanism.
5699 * '0' and '^' are special, because they can be followed by CTRL-D.
5700 */
5701#ifdef EBCDIC
5702# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5703#else
5704# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5705#endif
5706
5707#ifdef FEAT_MBYTE
5708# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5709#else
5710# define WHITECHAR(cc) vim_iswhite(cc)
5711#endif
5712
5713 void
5714insertchar(c, flags, second_indent)
5715 int c; /* character to insert or NUL */
5716 int flags; /* INSCHAR_FORMAT, etc. */
5717 int second_indent; /* indent for second line if >= 0 */
5718{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 int textwidth;
5720#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5726 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
5728 /*
5729 * Try to break the line in two or more pieces when:
5730 * - Always do this if we have been called to do formatting only.
5731 * - Always do this when 'formatoptions' has the 'a' flag and the line
5732 * ends in white space.
5733 * - Otherwise:
5734 * - Don't do this if inserting a blank
5735 * - Don't do this if an existing character is being replaced, unless
5736 * we're in VREPLACE mode.
5737 * - Do this if the cursor is not on the line where insert started
5738 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5739 * before the insert.
5740 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5741 * before 'textwidth'
5742 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005743 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 && ((flags & INSCHAR_FORMAT)
5745 || (!vim_iswhite(c)
5746 && !((State & REPLACE_FLAG)
5747#ifdef FEAT_VREPLACE
5748 && !(State & VREPLACE_FLAG)
5749#endif
5750 && *ml_get_cursor() != NUL)
5751 && (curwin->w_cursor.lnum != Insstart.lnum
5752 || ((!has_format_option(FO_INS_LONG)
5753 || Insstart_textlen <= (colnr_T)textwidth)
5754 && (!fo_ins_blank
5755 || Insstart_blank_vcol <= (colnr_T)textwidth
5756 ))))))
5757 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005758 /* Format with 'formatexpr' when it's set. Use internal formatting
5759 * when 'formatexpr' isn't set or it returns non-zero. */
5760#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005761 int do_internal = TRUE;
5762
Bram Moolenaar81a82092008-03-12 16:27:00 +00005763 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005764 {
5765 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5766 /* It may be required to save for undo again, e.g. when setline()
5767 * was called. */
5768 ins_need_undo = TRUE;
5769 }
5770 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005772 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (c == NUL) /* only formatting was wanted */
5776 return;
5777
5778#ifdef FEAT_COMMENTS
5779 /* Check whether this character should end a comment. */
5780 if (did_ai && (int)c == end_comment_pending)
5781 {
5782 char_u *line;
5783 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5784 int middle_len, end_len;
5785 int i;
5786
5787 /*
5788 * Need to remove existing (middle) comment leader and insert end
5789 * comment leader. First, check what comment leader we can find.
5790 */
5791 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5792 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5793 {
5794 /* Skip middle-comment string */
5795 while (*p && p[-1] != ':') /* find end of middle flags */
5796 ++p;
5797 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5798 /* Don't count trailing white space for middle_len */
5799 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5800 --middle_len;
5801
5802 /* Find the end-comment string */
5803 while (*p && p[-1] != ':') /* find end of end flags */
5804 ++p;
5805 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5806
5807 /* Skip white space before the cursor */
5808 i = curwin->w_cursor.col;
5809 while (--i >= 0 && vim_iswhite(line[i]))
5810 ;
5811 i++;
5812
5813 /* Skip to before the middle leader */
5814 i -= middle_len;
5815
5816 /* Check some expected things before we go on */
5817 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5818 {
5819 /* Backspace over all the stuff we want to replace */
5820 backspace_until_column(i);
5821
5822 /*
5823 * Insert the end-comment string, except for the last
5824 * character, which will get inserted as normal later.
5825 */
5826 ins_bytes_len(lead_end, end_len - 1);
5827 }
5828 }
5829 }
5830 end_comment_pending = NUL;
5831#endif
5832
5833 did_ai = FALSE;
5834#ifdef FEAT_SMARTINDENT
5835 did_si = FALSE;
5836 can_si = FALSE;
5837 can_si_back = FALSE;
5838#endif
5839
5840 /*
5841 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5842 * This speeds up normal text input considerably.
5843 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5844 * need to re-indent at a ':', or any other character (but not what
5845 * 'paste' is set)..
5846 */
5847#ifdef USE_ON_FLY_SCROLL
5848 dont_scroll = FALSE; /* allow scrolling here */
5849#endif
5850
5851 if ( !ISSPECIAL(c)
5852#ifdef FEAT_MBYTE
5853 && (!has_mbyte || (*mb_char2len)(c) == 1)
5854#endif
5855 && vpeekc() != NUL
5856 && !(State & REPLACE_FLAG)
5857#ifdef FEAT_CINDENT
5858 && !cindent_on()
5859#endif
5860#ifdef FEAT_RIGHTLEFT
5861 && !p_ri
5862#endif
5863 )
5864 {
5865#define INPUT_BUFLEN 100
5866 char_u buf[INPUT_BUFLEN + 1];
5867 int i;
5868 colnr_T virtcol = 0;
5869
5870 buf[0] = c;
5871 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005872 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 virtcol = get_nolist_virtcol();
5874 /*
5875 * Stop the string when:
5876 * - no more chars available
5877 * - finding a special character (command key)
5878 * - buffer is full
5879 * - running into the 'textwidth' boundary
5880 * - need to check for abbreviation: A non-word char after a word-char
5881 */
5882 while ( (c = vpeekc()) != NUL
5883 && !ISSPECIAL(c)
5884#ifdef FEAT_MBYTE
5885 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5886#endif
5887 && i < INPUT_BUFLEN
5888 && (textwidth == 0
5889 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5890 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5891 {
5892#ifdef FEAT_RIGHTLEFT
5893 c = vgetc();
5894 if (p_hkmap && KeyTyped)
5895 c = hkmap(c); /* Hebrew mode mapping */
5896# ifdef FEAT_FKMAP
5897 if (p_fkmap && KeyTyped)
5898 c = fkmap(c); /* Farsi mode mapping */
5899# endif
5900 buf[i++] = c;
5901#else
5902 buf[i++] = vgetc();
5903#endif
5904 }
5905
5906#ifdef FEAT_DIGRAPHS
5907 do_digraph(-1); /* clear digraphs */
5908 do_digraph(buf[i-1]); /* may be the start of a digraph */
5909#endif
5910 buf[i] = NUL;
5911 ins_str(buf);
5912 if (flags & INSCHAR_CTRLV)
5913 {
5914 redo_literal(*buf);
5915 i = 1;
5916 }
5917 else
5918 i = 0;
5919 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005920 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 }
5922 else
5923 {
5924#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005925 int cc;
5926
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5928 {
5929 char_u buf[MB_MAXBYTES + 1];
5930
5931 (*mb_char2bytes)(c, buf);
5932 buf[cc] = NUL;
5933 ins_char_bytes(buf, cc);
5934 AppendCharToRedobuff(c);
5935 }
5936 else
5937#endif
5938 {
5939 ins_char(c);
5940 if (flags & INSCHAR_CTRLV)
5941 redo_literal(c);
5942 else
5943 AppendCharToRedobuff(c);
5944 }
5945 }
5946}
5947
5948/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005949 * Format text at the current insert position.
5950 */
5951 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005952internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005953 int textwidth;
5954 int second_indent;
5955 int flags;
5956 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005957 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005958{
5959 int cc;
5960 int save_char = NUL;
5961 int haveto_redraw = FALSE;
5962 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5963#ifdef FEAT_MBYTE
5964 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5965#endif
5966 int fo_white_par = has_format_option(FO_WHITE_PAR);
5967 int first_line = TRUE;
5968#ifdef FEAT_COMMENTS
5969 colnr_T leader_len;
5970 int no_leader = FALSE;
5971 int do_comments = (flags & INSCHAR_DO_COM);
5972#endif
5973
5974 /*
5975 * When 'ai' is off we don't want a space under the cursor to be
5976 * deleted. Replace it with an 'x' temporarily.
5977 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005978 if (!curbuf->b_p_ai
5979#ifdef FEAT_VREPLACE
5980 && !(State & VREPLACE_FLAG)
5981#endif
5982 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005983 {
5984 cc = gchar_cursor();
5985 if (vim_iswhite(cc))
5986 {
5987 save_char = cc;
5988 pchar_cursor('x');
5989 }
5990 }
5991
5992 /*
5993 * Repeat breaking lines, until the current line is not too long.
5994 */
5995 while (!got_int)
5996 {
5997 int startcol; /* Cursor column at entry */
5998 int wantcol; /* column at textwidth border */
5999 int foundcol; /* column for start of spaces */
6000 int end_foundcol = 0; /* column for start of word */
6001 colnr_T len;
6002 colnr_T virtcol;
6003#ifdef FEAT_VREPLACE
6004 int orig_col = 0;
6005 char_u *saved_text = NULL;
6006#endif
6007 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006008 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006009
Bram Moolenaar97b98102009-11-17 16:41:01 +00006010 virtcol = get_nolist_virtcol()
6011 + char2cells(c != NUL ? c : gchar_cursor());
6012 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006013 break;
6014
6015#ifdef FEAT_COMMENTS
6016 if (no_leader)
6017 do_comments = FALSE;
6018 else if (!(flags & INSCHAR_FORMAT)
6019 && has_format_option(FO_WRAP_COMS))
6020 do_comments = TRUE;
6021
6022 /* Don't break until after the comment leader */
6023 if (do_comments)
6024 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
6025 else
6026 leader_len = 0;
6027
6028 /* If the line doesn't start with a comment leader, then don't
6029 * start one in a following broken line. Avoids that a %word
6030 * moved to the start of the next line causes all following lines
6031 * to start with %. */
6032 if (leader_len == 0)
6033 no_leader = TRUE;
6034#endif
6035 if (!(flags & INSCHAR_FORMAT)
6036#ifdef FEAT_COMMENTS
6037 && leader_len == 0
6038#endif
6039 && !has_format_option(FO_WRAP))
6040
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006041 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006042 if ((startcol = curwin->w_cursor.col) == 0)
6043 break;
6044
6045 /* find column of textwidth border */
6046 coladvance((colnr_T)textwidth);
6047 wantcol = curwin->w_cursor.col;
6048
Bram Moolenaar97b98102009-11-17 16:41:01 +00006049 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050 foundcol = 0;
6051
6052 /*
6053 * Find position to break at.
6054 * Stop at first entered white when 'formatoptions' has 'v'
6055 */
6056 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
6057 || curwin->w_cursor.lnum != Insstart.lnum
6058 || curwin->w_cursor.col >= Insstart.col)
6059 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006060 if (curwin->w_cursor.col == startcol && c != NUL)
6061 cc = c;
6062 else
6063 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006064 if (WHITECHAR(cc))
6065 {
6066 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006067 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006068
6069 /* find start of sequence of blanks */
6070 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6071 {
6072 dec_cursor();
6073 cc = gchar_cursor();
6074 }
6075 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6076 break; /* only spaces in front of text */
6077#ifdef FEAT_COMMENTS
6078 /* Don't break until after the comment leader */
6079 if (curwin->w_cursor.col < leader_len)
6080 break;
6081#endif
6082 if (has_format_option(FO_ONE_LETTER))
6083 {
6084 /* do not break after one-letter words */
6085 if (curwin->w_cursor.col == 0)
6086 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006087#ifdef FEAT_COMMENTS
6088 /* do not break "#a b" when 'tw' is 2 */
6089 if (curwin->w_cursor.col <= leader_len)
6090 break;
6091#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006092 col = curwin->w_cursor.col;
6093 dec_cursor();
6094 cc = gchar_cursor();
6095
6096 if (WHITECHAR(cc))
6097 continue; /* one-letter, continue */
6098 curwin->w_cursor.col = col;
6099 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006100
6101 inc_cursor();
6102
6103 end_foundcol = end_col + 1;
6104 foundcol = curwin->w_cursor.col;
6105 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006106 break;
6107 }
6108#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006109 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006110 {
6111 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006112 if (curwin->w_cursor.col != startcol)
6113 {
6114#ifdef FEAT_COMMENTS
6115 /* Don't break until after the comment leader */
6116 if (curwin->w_cursor.col < leader_len)
6117 break;
6118#endif
6119 col = curwin->w_cursor.col;
6120 inc_cursor();
6121 /* Don't change end_foundcol if already set. */
6122 if (foundcol != curwin->w_cursor.col)
6123 {
6124 foundcol = curwin->w_cursor.col;
6125 end_foundcol = foundcol;
6126 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6127 break;
6128 }
6129 curwin->w_cursor.col = col;
6130 }
6131
6132 if (curwin->w_cursor.col == 0)
6133 break;
6134
6135 col = curwin->w_cursor.col;
6136
6137 dec_cursor();
6138 cc = gchar_cursor();
6139
6140 if (WHITECHAR(cc))
6141 continue; /* break with space */
6142#ifdef FEAT_COMMENTS
6143 /* Don't break until after the comment leader */
6144 if (curwin->w_cursor.col < leader_len)
6145 break;
6146#endif
6147
6148 curwin->w_cursor.col = col;
6149
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006150 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006151 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006152 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6153 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006154 }
6155#endif
6156 if (curwin->w_cursor.col == 0)
6157 break;
6158 dec_cursor();
6159 }
6160
6161 if (foundcol == 0) /* no spaces, cannot break line */
6162 {
6163 curwin->w_cursor.col = startcol;
6164 break;
6165 }
6166
6167 /* Going to break the line, remove any "$" now. */
6168 undisplay_dollar();
6169
6170 /*
6171 * Offset between cursor position and line break is used by replace
6172 * stack functions. VREPLACE does not use this, and backspaces
6173 * over the text instead.
6174 */
6175#ifdef FEAT_VREPLACE
6176 if (State & VREPLACE_FLAG)
6177 orig_col = startcol; /* Will start backspacing from here */
6178 else
6179#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006180 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181
6182 /*
6183 * adjust startcol for spaces that will be deleted and
6184 * characters that will remain on top line
6185 */
6186 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006187 while ((cc = gchar_cursor(), WHITECHAR(cc))
6188 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006189 inc_cursor();
6190 startcol -= curwin->w_cursor.col;
6191 if (startcol < 0)
6192 startcol = 0;
6193
6194#ifdef FEAT_VREPLACE
6195 if (State & VREPLACE_FLAG)
6196 {
6197 /*
6198 * In VREPLACE mode, we will backspace over the text to be
6199 * wrapped, so save a copy now to put on the next line.
6200 */
6201 saved_text = vim_strsave(ml_get_cursor());
6202 curwin->w_cursor.col = orig_col;
6203 if (saved_text == NULL)
6204 break; /* Can't do it, out of memory */
6205 saved_text[startcol] = NUL;
6206
6207 /* Backspace over characters that will move to the next line */
6208 if (!fo_white_par)
6209 backspace_until_column(foundcol);
6210 }
6211 else
6212#endif
6213 {
6214 /* put cursor after pos. to break line */
6215 if (!fo_white_par)
6216 curwin->w_cursor.col = foundcol;
6217 }
6218
6219 /*
6220 * Split the line just before the margin.
6221 * Only insert/delete lines, but don't really redraw the window.
6222 */
6223 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6224 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6225#ifdef FEAT_COMMENTS
6226 + (do_comments ? OPENLINE_DO_COM : 0)
6227#endif
6228 , old_indent);
6229 old_indent = 0;
6230
6231 replace_offset = 0;
6232 if (first_line)
6233 {
6234 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6235 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6236 if (second_indent >= 0)
6237 {
6238#ifdef FEAT_VREPLACE
6239 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006240 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241 else
6242#endif
6243 (void)set_indent(second_indent, SIN_CHANGED);
6244 }
6245 first_line = FALSE;
6246 }
6247
6248#ifdef FEAT_VREPLACE
6249 if (State & VREPLACE_FLAG)
6250 {
6251 /*
6252 * In VREPLACE mode we have backspaced over the text to be
6253 * moved, now we re-insert it into the new line.
6254 */
6255 ins_bytes(saved_text);
6256 vim_free(saved_text);
6257 }
6258 else
6259#endif
6260 {
6261 /*
6262 * Check if cursor is not past the NUL off the line, cindent
6263 * may have added or removed indent.
6264 */
6265 curwin->w_cursor.col += startcol;
6266 len = (colnr_T)STRLEN(ml_get_curline());
6267 if (curwin->w_cursor.col > len)
6268 curwin->w_cursor.col = len;
6269 }
6270
6271 haveto_redraw = TRUE;
6272#ifdef FEAT_CINDENT
6273 can_cindent = TRUE;
6274#endif
6275 /* moved the cursor, don't autoindent or cindent now */
6276 did_ai = FALSE;
6277#ifdef FEAT_SMARTINDENT
6278 did_si = FALSE;
6279 can_si = FALSE;
6280 can_si_back = FALSE;
6281#endif
6282 line_breakcheck();
6283 }
6284
6285 if (save_char != NUL) /* put back space after cursor */
6286 pchar_cursor(save_char);
6287
6288 if (!format_only && haveto_redraw)
6289 {
6290 update_topline();
6291 redraw_curbuf_later(VALID);
6292 }
6293}
6294
6295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 * Called after inserting or deleting text: When 'formatoptions' includes the
6297 * 'a' flag format from the current line until the end of the paragraph.
6298 * Keep the cursor at the same position relative to the text.
6299 * The caller must have saved the cursor line for undo, following ones will be
6300 * saved here.
6301 */
6302 void
6303auto_format(trailblank, prev_line)
6304 int trailblank; /* when TRUE also format with trailing blank */
6305 int prev_line; /* may start in previous line */
6306{
6307 pos_T pos;
6308 colnr_T len;
6309 char_u *old;
6310 char_u *new, *pnew;
6311 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006312 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
6314 if (!has_format_option(FO_AUTO))
6315 return;
6316
6317 pos = curwin->w_cursor;
6318 old = ml_get_curline();
6319
6320 /* may remove added space */
6321 check_auto_format(FALSE);
6322
6323 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6324 * user might insert normal text next. Also skip formatting when "1" is
6325 * in 'formatoptions' and there is a single character before the cursor.
6326 * Otherwise the line would be broken and when typing another non-white
6327 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006328 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (*old != NUL && !trailblank && wasatend)
6330 {
6331 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006332 cc = gchar_cursor();
6333 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6334 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006336 cc = gchar_cursor();
6337 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
6339 curwin->w_cursor = pos;
6340 return;
6341 }
6342 curwin->w_cursor = pos;
6343 }
6344
6345#ifdef FEAT_COMMENTS
6346 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6347 * comments. */
6348 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6349 && get_leader_len(old, NULL, FALSE) == 0)
6350 return;
6351#endif
6352
6353 /*
6354 * May start formatting in a previous line, so that after "x" a word is
6355 * moved to the previous line if it fits there now. Only when this is not
6356 * the start of a paragraph.
6357 */
6358 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6359 {
6360 --curwin->w_cursor.lnum;
6361 if (u_save_cursor() == FAIL)
6362 return;
6363 }
6364
6365 /*
6366 * Do the formatting and restore the cursor position. "saved_cursor" will
6367 * be adjusted for the text formatting.
6368 */
6369 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006370 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 curwin->w_cursor = saved_cursor;
6372 saved_cursor.lnum = 0;
6373
6374 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6375 {
6376 /* "cannot happen" */
6377 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6378 coladvance((colnr_T)MAXCOL);
6379 }
6380 else
6381 check_cursor_col();
6382
6383 /* Insert mode: If the cursor is now after the end of the line while it
6384 * previously wasn't, the line was broken. Because of the rule above we
6385 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6386 * formatted. */
6387 if (!wasatend && has_format_option(FO_WHITE_PAR))
6388 {
6389 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006390 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 if (curwin->w_cursor.col == len)
6392 {
6393 pnew = vim_strnsave(new, len + 2);
6394 pnew[len] = ' ';
6395 pnew[len + 1] = NUL;
6396 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6397 /* remove the space later */
6398 did_add_space = TRUE;
6399 }
6400 else
6401 /* may remove added space */
6402 check_auto_format(FALSE);
6403 }
6404
6405 check_cursor();
6406}
6407
6408/*
6409 * When an extra space was added to continue a paragraph for auto-formatting,
6410 * delete it now. The space must be under the cursor, just after the insert
6411 * position.
6412 */
6413 static void
6414check_auto_format(end_insert)
6415 int end_insert; /* TRUE when ending Insert mode */
6416{
6417 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006418 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 if (did_add_space)
6421 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006422 cc = gchar_cursor();
6423 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 /* Somehow the space was removed already. */
6425 did_add_space = FALSE;
6426 else
6427 {
6428 if (!end_insert)
6429 {
6430 inc_cursor();
6431 c = gchar_cursor();
6432 dec_cursor();
6433 }
6434 if (c != NUL)
6435 {
6436 /* The space is no longer at the end of the line, delete it. */
6437 del_char(FALSE);
6438 did_add_space = FALSE;
6439 }
6440 }
6441 }
6442}
6443
6444/*
6445 * Find out textwidth to be used for formatting:
6446 * if 'textwidth' option is set, use it
6447 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6448 * if invalid value, use 0.
6449 * Set default to window width (maximum 79) for "gq" operator.
6450 */
6451 int
6452comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006453 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int textwidth;
6456
6457 textwidth = curbuf->b_p_tw;
6458 if (textwidth == 0 && curbuf->b_p_wm)
6459 {
6460 /* The width is the window width minus 'wrapmargin' minus all the
6461 * things that add to the margin. */
6462 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6463#ifdef FEAT_CMDWIN
6464 if (cmdwin_type != 0)
6465 textwidth -= 1;
6466#endif
6467#ifdef FEAT_FOLDING
6468 textwidth -= curwin->w_p_fdc;
6469#endif
6470#ifdef FEAT_SIGNS
6471 if (curwin->w_buffer->b_signlist != NULL
6472# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006473 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474# endif
6475 )
6476 textwidth -= 1;
6477#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006478 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 textwidth -= 8;
6480 }
6481 if (textwidth < 0)
6482 textwidth = 0;
6483 if (ff && textwidth == 0)
6484 {
6485 textwidth = W_WIDTH(curwin) - 1;
6486 if (textwidth > 79)
6487 textwidth = 79;
6488 }
6489 return textwidth;
6490}
6491
6492/*
6493 * Put a character in the redo buffer, for when just after a CTRL-V.
6494 */
6495 static void
6496redo_literal(c)
6497 int c;
6498{
6499 char_u buf[10];
6500
6501 /* Only digits need special treatment. Translate them into a string of
6502 * three digits. */
6503 if (VIM_ISDIGIT(c))
6504 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006505 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 AppendToRedobuff(buf);
6507 }
6508 else
6509 AppendCharToRedobuff(c);
6510}
6511
6512/*
6513 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006514 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
6516 static void
6517start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006518 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 if (!arrow_used) /* something has been inserted */
6521 {
6522 AppendToRedobuff(ESC_STR);
6523 stop_insert(end_insert_pos, FALSE);
6524 arrow_used = TRUE; /* this means we stopped the current insert */
6525 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006526#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006527 check_spell_redraw();
6528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006531#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006532/*
6533 * If we skipped highlighting word at cursor, do it now.
6534 * It may be skipped again, thus reset spell_redraw_lnum first.
6535 */
6536 static void
6537check_spell_redraw()
6538{
6539 if (spell_redraw_lnum != 0)
6540 {
6541 linenr_T lnum = spell_redraw_lnum;
6542
6543 spell_redraw_lnum = 0;
6544 redrawWinline(lnum, FALSE);
6545 }
6546}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006547
6548/*
6549 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6550 * spelled word, if there is one.
6551 */
6552 static void
6553spell_back_to_badword()
6554{
6555 pos_T tpos = curwin->w_cursor;
6556
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006557 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006558 if (curwin->w_cursor.col != tpos.col)
6559 start_arrow(&tpos);
6560}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006561#endif
6562
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563/*
6564 * stop_arrow() is called before a change is made in insert mode.
6565 * If an arrow key has been used, start a new insertion.
6566 * Returns FAIL if undo is impossible, shouldn't insert then.
6567 */
6568 int
6569stop_arrow()
6570{
6571 if (arrow_used)
6572 {
6573 if (u_save_cursor() == OK)
6574 {
6575 arrow_used = FALSE;
6576 ins_need_undo = FALSE;
6577 }
6578 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006579 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 ai_col = 0;
6581#ifdef FEAT_VREPLACE
6582 if (State & VREPLACE_FLAG)
6583 {
6584 orig_line_count = curbuf->b_ml.ml_line_count;
6585 vr_lines_changed = 1;
6586 }
6587#endif
6588 ResetRedobuff();
6589 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006590 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 }
6592 else if (ins_need_undo)
6593 {
6594 if (u_save_cursor() == OK)
6595 ins_need_undo = FALSE;
6596 }
6597
6598#ifdef FEAT_FOLDING
6599 /* Always open fold at the cursor line when inserting something. */
6600 foldOpenCursor();
6601#endif
6602
6603 return (arrow_used || ins_need_undo ? FAIL : OK);
6604}
6605
6606/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006607 * Do a few things to stop inserting.
6608 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6609 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 */
6611 static void
6612stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006613 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006614 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006616 int cc;
6617 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 stop_redo_ins();
6620 replace_flush(); /* abandon replace stack */
6621
6622 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006623 * Save the inserted text for later redo with ^@ and CTRL-A.
6624 * Don't do it when "restart_edit" was set and nothing was inserted,
6625 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006627 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006628 if (did_restart_edit == 0 || (ptr != NULL
6629 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006630 {
6631 vim_free(last_insert);
6632 last_insert = ptr;
6633 last_insert_skip = new_insert_skip;
6634 }
6635 else
6636 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006638 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
6640 /* Auto-format now. It may seem strange to do this when stopping an
6641 * insertion (or moving the cursor), but it's required when appending
6642 * a line and having it end in a space. But only do it when something
6643 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006644 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006646 pos_T tpos = curwin->w_cursor;
6647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 /* When the cursor is at the end of the line after a space the
6649 * formatting will move it to the following word. Avoid that by
6650 * moving the cursor onto the space. */
6651 cc = 'x';
6652 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6653 {
6654 dec_cursor();
6655 cc = gchar_cursor();
6656 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006657 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659
6660 auto_format(TRUE, FALSE);
6661
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006662 if (vim_iswhite(cc))
6663 {
6664 if (gchar_cursor() != NUL)
6665 inc_cursor();
6666#ifdef FEAT_VIRTUALEDIT
6667 /* If the cursor is still at the same character, also keep
6668 * the "coladd". */
6669 if (gchar_cursor() == NUL
6670 && curwin->w_cursor.lnum == tpos.lnum
6671 && curwin->w_cursor.col == tpos.col)
6672 curwin->w_cursor.coladd = tpos.coladd;
6673#endif
6674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 }
6676
6677 /* If a space was inserted for auto-formatting, remove it now. */
6678 check_auto_format(TRUE);
6679
6680 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006681 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006682 * Do this when ESC was used or moving the cursor up/down.
6683 * Check for the old position still being valid, just in case the text
6684 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006685 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006686 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6687 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006689 pos_T tpos = curwin->w_cursor;
6690
6691 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006692 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006693 for (;;)
6694 {
6695 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6696 --curwin->w_cursor.col;
6697 cc = gchar_cursor();
6698 if (!vim_iswhite(cc))
6699 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006700 if (del_char(TRUE) == FAIL)
6701 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006702 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006703 if (curwin->w_cursor.lnum != tpos.lnum)
6704 curwin->w_cursor = tpos;
6705 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6707
6708#ifdef FEAT_VISUAL
6709 /* <C-S-Right> may have started Visual mode, adjust the position for
6710 * deleted characters. */
6711 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6712 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006713 int len = (int)STRLEN(ml_get_curline());
6714
6715 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006717 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718# ifdef FEAT_VIRTUALEDIT
6719 VIsual.coladd = 0;
6720# endif
6721 }
6722 }
6723#endif
6724 }
6725 }
6726 did_ai = FALSE;
6727#ifdef FEAT_SMARTINDENT
6728 did_si = FALSE;
6729 can_si = FALSE;
6730 can_si_back = FALSE;
6731#endif
6732
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006733 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6734 * now in a different buffer. */
6735 if (end_insert_pos != NULL)
6736 {
6737 curbuf->b_op_start = Insstart;
6738 curbuf->b_op_end = *end_insert_pos;
6739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740}
6741
6742/*
6743 * Set the last inserted text to a single character.
6744 * Used for the replace command.
6745 */
6746 void
6747set_last_insert(c)
6748 int c;
6749{
6750 char_u *s;
6751
6752 vim_free(last_insert);
6753#ifdef FEAT_MBYTE
6754 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6755#else
6756 last_insert = alloc(6);
6757#endif
6758 if (last_insert != NULL)
6759 {
6760 s = last_insert;
6761 /* Use the CTRL-V only when entering a special char */
6762 if (c < ' ' || c == DEL)
6763 *s++ = Ctrl_V;
6764 s = add_char2buf(c, s);
6765 *s++ = ESC;
6766 *s++ = NUL;
6767 last_insert_skip = 0;
6768 }
6769}
6770
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006771#if defined(EXITFREE) || defined(PROTO)
6772 void
6773free_last_insert()
6774{
6775 vim_free(last_insert);
6776 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006777# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006778 vim_free(compl_orig_text);
6779 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006780# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006781}
6782#endif
6783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784/*
6785 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6786 * and CSI. Handle multi-byte characters.
6787 * Returns a pointer to after the added bytes.
6788 */
6789 char_u *
6790add_char2buf(c, s)
6791 int c;
6792 char_u *s;
6793{
6794#ifdef FEAT_MBYTE
6795 char_u temp[MB_MAXBYTES];
6796 int i;
6797 int len;
6798
6799 len = (*mb_char2bytes)(c, temp);
6800 for (i = 0; i < len; ++i)
6801 {
6802 c = temp[i];
6803#endif
6804 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6805 if (c == K_SPECIAL)
6806 {
6807 *s++ = K_SPECIAL;
6808 *s++ = KS_SPECIAL;
6809 *s++ = KE_FILLER;
6810 }
6811#ifdef FEAT_GUI
6812 else if (c == CSI)
6813 {
6814 *s++ = CSI;
6815 *s++ = KS_EXTRA;
6816 *s++ = (int)KE_CSI;
6817 }
6818#endif
6819 else
6820 *s++ = c;
6821#ifdef FEAT_MBYTE
6822 }
6823#endif
6824 return s;
6825}
6826
6827/*
6828 * move cursor to start of line
6829 * if flags & BL_WHITE move to first non-white
6830 * if flags & BL_SOL move to first non-white if startofline is set,
6831 * otherwise keep "curswant" column
6832 * if flags & BL_FIX don't leave the cursor on a NUL.
6833 */
6834 void
6835beginline(flags)
6836 int flags;
6837{
6838 if ((flags & BL_SOL) && !p_sol)
6839 coladvance(curwin->w_curswant);
6840 else
6841 {
6842 curwin->w_cursor.col = 0;
6843#ifdef FEAT_VIRTUALEDIT
6844 curwin->w_cursor.coladd = 0;
6845#endif
6846
6847 if (flags & (BL_WHITE | BL_SOL))
6848 {
6849 char_u *ptr;
6850
6851 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6852 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6853 ++curwin->w_cursor.col;
6854 }
6855 curwin->w_set_curswant = TRUE;
6856 }
6857}
6858
6859/*
6860 * oneright oneleft cursor_down cursor_up
6861 *
6862 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006863 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 * Return OK when successful, FAIL when we hit a line of file boundary.
6865 */
6866
6867 int
6868oneright()
6869{
6870 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873#ifdef FEAT_VIRTUALEDIT
6874 if (virtual_active())
6875 {
6876 pos_T prevpos = curwin->w_cursor;
6877
6878 /* Adjust for multi-wide char (excluding TAB) */
6879 ptr = ml_get_cursor();
6880 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006881# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006883# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 ))
6887 ? ptr2cells(ptr) : 1));
6888 curwin->w_set_curswant = TRUE;
6889 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6890 return (prevpos.col != curwin->w_cursor.col
6891 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6892 }
6893#endif
6894
6895 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006896 if (*ptr == NUL)
6897 return FAIL; /* already at the very end */
6898
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006900 if (has_mbyte)
6901 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 else
6903#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006904 l = 1;
6905
6906 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6907 * contains "onemore". */
6908 if (ptr[l] == NUL
6909#ifdef FEAT_VIRTUALEDIT
6910 && (ve_flags & VE_ONEMORE) == 0
6911#endif
6912 )
6913 return FAIL;
6914 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
6916 curwin->w_set_curswant = TRUE;
6917 return OK;
6918}
6919
6920 int
6921oneleft()
6922{
6923#ifdef FEAT_VIRTUALEDIT
6924 if (virtual_active())
6925 {
6926 int width;
6927 int v = getviscol();
6928
6929 if (v == 0)
6930 return FAIL;
6931
6932# ifdef FEAT_LINEBREAK
6933 /* We might get stuck on 'showbreak', skip over it. */
6934 width = 1;
6935 for (;;)
6936 {
6937 coladvance(v - width);
6938 /* getviscol() is slow, skip it when 'showbreak' is empty and
6939 * there are no multi-byte characters */
6940 if ((*p_sbr == NUL
6941# ifdef FEAT_MBYTE
6942 && !has_mbyte
6943# endif
6944 ) || getviscol() < v)
6945 break;
6946 ++width;
6947 }
6948# else
6949 coladvance(v - 1);
6950# endif
6951
6952 if (curwin->w_cursor.coladd == 1)
6953 {
6954 char_u *ptr;
6955
6956 /* Adjust for multi-wide char (not a TAB) */
6957 ptr = ml_get_cursor();
6958 if (*ptr != TAB && vim_isprintc(
6959# ifdef FEAT_MBYTE
6960 (*mb_ptr2char)(ptr)
6961# else
6962 *ptr
6963# endif
6964 ) && ptr2cells(ptr) > 1)
6965 curwin->w_cursor.coladd = 0;
6966 }
6967
6968 curwin->w_set_curswant = TRUE;
6969 return OK;
6970 }
6971#endif
6972
6973 if (curwin->w_cursor.col == 0)
6974 return FAIL;
6975
6976 curwin->w_set_curswant = TRUE;
6977 --curwin->w_cursor.col;
6978
6979#ifdef FEAT_MBYTE
6980 /* if the character on the left of the current cursor is a multi-byte
6981 * character, move to its first byte */
6982 if (has_mbyte)
6983 mb_adjust_cursor();
6984#endif
6985 return OK;
6986}
6987
6988 int
6989cursor_up(n, upd_topline)
6990 long n;
6991 int upd_topline; /* When TRUE: update topline */
6992{
6993 linenr_T lnum;
6994
6995 if (n > 0)
6996 {
6997 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006998 /* This fails if the cursor is already in the first line or the count
6999 * is larger than the line number and '-' is in 'cpoptions' */
7000 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 return FAIL;
7002 if (n >= lnum)
7003 lnum = 1;
7004 else
7005#ifdef FEAT_FOLDING
7006 if (hasAnyFolding(curwin))
7007 {
7008 /*
7009 * Count each sequence of folded lines as one logical line.
7010 */
7011 /* go to the the start of the current fold */
7012 (void)hasFolding(lnum, &lnum, NULL);
7013
7014 while (n--)
7015 {
7016 /* move up one line */
7017 --lnum;
7018 if (lnum <= 1)
7019 break;
7020 /* If we entered a fold, move to the beginning, unless in
7021 * Insert mode or when 'foldopen' contains "all": it will open
7022 * in a moment. */
7023 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7024 (void)hasFolding(lnum, &lnum, NULL);
7025 }
7026 if (lnum < 1)
7027 lnum = 1;
7028 }
7029 else
7030#endif
7031 lnum -= n;
7032 curwin->w_cursor.lnum = lnum;
7033 }
7034
7035 /* try to advance to the column we want to be at */
7036 coladvance(curwin->w_curswant);
7037
7038 if (upd_topline)
7039 update_topline(); /* make sure curwin->w_topline is valid */
7040
7041 return OK;
7042}
7043
7044/*
7045 * Cursor down a number of logical lines.
7046 */
7047 int
7048cursor_down(n, upd_topline)
7049 long n;
7050 int upd_topline; /* When TRUE: update topline */
7051{
7052 linenr_T lnum;
7053
7054 if (n > 0)
7055 {
7056 lnum = curwin->w_cursor.lnum;
7057#ifdef FEAT_FOLDING
7058 /* Move to last line of fold, will fail if it's the end-of-file. */
7059 (void)hasFolding(lnum, NULL, &lnum);
7060#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007061 /* This fails if the cursor is already in the last line or would move
7062 * beyound the last line and '-' is in 'cpoptions' */
7063 if (lnum >= curbuf->b_ml.ml_line_count
7064 || (lnum + n > curbuf->b_ml.ml_line_count
7065 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 return FAIL;
7067 if (lnum + n >= curbuf->b_ml.ml_line_count)
7068 lnum = curbuf->b_ml.ml_line_count;
7069 else
7070#ifdef FEAT_FOLDING
7071 if (hasAnyFolding(curwin))
7072 {
7073 linenr_T last;
7074
7075 /* count each sequence of folded lines as one logical line */
7076 while (n--)
7077 {
7078 if (hasFolding(lnum, NULL, &last))
7079 lnum = last + 1;
7080 else
7081 ++lnum;
7082 if (lnum >= curbuf->b_ml.ml_line_count)
7083 break;
7084 }
7085 if (lnum > curbuf->b_ml.ml_line_count)
7086 lnum = curbuf->b_ml.ml_line_count;
7087 }
7088 else
7089#endif
7090 lnum += n;
7091 curwin->w_cursor.lnum = lnum;
7092 }
7093
7094 /* try to advance to the column we want to be at */
7095 coladvance(curwin->w_curswant);
7096
7097 if (upd_topline)
7098 update_topline(); /* make sure curwin->w_topline is valid */
7099
7100 return OK;
7101}
7102
7103/*
7104 * Stuff the last inserted text in the read buffer.
7105 * Last_insert actually is a copy of the redo buffer, so we
7106 * first have to remove the command.
7107 */
7108 int
7109stuff_inserted(c, count, no_esc)
7110 int c; /* Command character to be inserted */
7111 long count; /* Repeat this many times */
7112 int no_esc; /* Don't add an ESC at the end */
7113{
7114 char_u *esc_ptr;
7115 char_u *ptr;
7116 char_u *last_ptr;
7117 char_u last = NUL;
7118
7119 ptr = get_last_insert();
7120 if (ptr == NULL)
7121 {
7122 EMSG(_(e_noinstext));
7123 return FAIL;
7124 }
7125
7126 /* may want to stuff the command character, to start Insert mode */
7127 if (c != NUL)
7128 stuffcharReadbuff(c);
7129 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7130 *esc_ptr = NUL; /* remove the ESC */
7131
7132 /* when the last char is either "0" or "^" it will be quoted if no ESC
7133 * comes after it OR if it will inserted more than once and "ptr"
7134 * starts with ^D. -- Acevedo
7135 */
7136 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7137 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7138 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7139 {
7140 last = *last_ptr;
7141 *last_ptr = NUL;
7142 }
7143
7144 do
7145 {
7146 stuffReadbuff(ptr);
7147 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7148 if (last)
7149 stuffReadbuff((char_u *)(last == '0'
7150 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7151 : IF_EB("\026^", CTRL_V_STR "^")));
7152 }
7153 while (--count > 0);
7154
7155 if (last)
7156 *last_ptr = last;
7157
7158 if (esc_ptr != NULL)
7159 *esc_ptr = ESC; /* put the ESC back */
7160
7161 /* may want to stuff a trailing ESC, to get out of Insert mode */
7162 if (!no_esc)
7163 stuffcharReadbuff(ESC);
7164
7165 return OK;
7166}
7167
7168 char_u *
7169get_last_insert()
7170{
7171 if (last_insert == NULL)
7172 return NULL;
7173 return last_insert + last_insert_skip;
7174}
7175
7176/*
7177 * Get last inserted string, and remove trailing <Esc>.
7178 * Returns pointer to allocated memory (must be freed) or NULL.
7179 */
7180 char_u *
7181get_last_insert_save()
7182{
7183 char_u *s;
7184 int len;
7185
7186 if (last_insert == NULL)
7187 return NULL;
7188 s = vim_strsave(last_insert + last_insert_skip);
7189 if (s != NULL)
7190 {
7191 len = (int)STRLEN(s);
7192 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7193 s[len - 1] = NUL;
7194 }
7195 return s;
7196}
7197
7198/*
7199 * Check the word in front of the cursor for an abbreviation.
7200 * Called when the non-id character "c" has been entered.
7201 * When an abbreviation is recognized it is removed from the text and
7202 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7203 */
7204 static int
7205echeck_abbr(c)
7206 int c;
7207{
7208 /* Don't check for abbreviation in paste mode, when disabled and just
7209 * after moving around with cursor keys. */
7210 if (p_paste || no_abbr || arrow_used)
7211 return FALSE;
7212
7213 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7214 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7215}
7216
7217/*
7218 * replace-stack functions
7219 *
7220 * When replacing characters, the replaced characters are remembered for each
7221 * new character. This is used to re-insert the old text when backspacing.
7222 *
7223 * There is a NUL headed list of characters for each character that is
7224 * currently in the file after the insertion point. When BS is used, one NUL
7225 * headed list is put back for the deleted character.
7226 *
7227 * For a newline, there are two NUL headed lists. One contains the characters
7228 * that the NL replaced. The extra one stores the characters after the cursor
7229 * that were deleted (always white space).
7230 *
7231 * Replace_offset is normally 0, in which case replace_push will add a new
7232 * character at the end of the stack. If replace_offset is not 0, that many
7233 * characters will be left on the stack above the newly inserted character.
7234 */
7235
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007236static char_u *replace_stack = NULL;
7237static long replace_stack_nr = 0; /* next entry in replace stack */
7238static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 void
7241replace_push(c)
7242 int c; /* character that is replaced (NUL is none) */
7243{
7244 char_u *p;
7245
7246 if (replace_stack_nr < replace_offset) /* nothing to do */
7247 return;
7248 if (replace_stack_len <= replace_stack_nr)
7249 {
7250 replace_stack_len += 50;
7251 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7252 if (p == NULL) /* out of memory */
7253 {
7254 replace_stack_len -= 50;
7255 return;
7256 }
7257 if (replace_stack != NULL)
7258 {
7259 mch_memmove(p, replace_stack,
7260 (size_t)(replace_stack_nr * sizeof(char_u)));
7261 vim_free(replace_stack);
7262 }
7263 replace_stack = p;
7264 }
7265 p = replace_stack + replace_stack_nr - replace_offset;
7266 if (replace_offset)
7267 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7268 *p = c;
7269 ++replace_stack_nr;
7270}
7271
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007272#if defined(FEAT_MBYTE) || defined(PROTO)
7273/*
7274 * Push a character onto the replace stack. Handles a multi-byte character in
7275 * reverse byte order, so that the first byte is popped off first.
7276 * Return the number of bytes done (includes composing characters).
7277 */
7278 int
7279replace_push_mb(p)
7280 char_u *p;
7281{
7282 int l = (*mb_ptr2len)(p);
7283 int j;
7284
7285 for (j = l - 1; j >= 0; --j)
7286 replace_push(p[j]);
7287 return l;
7288}
7289#endif
7290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291/*
7292 * Pop one item from the replace stack.
7293 * return -1 if stack empty
7294 * return replaced character or NUL otherwise
7295 */
7296 static int
7297replace_pop()
7298{
7299 if (replace_stack_nr == 0)
7300 return -1;
7301 return (int)replace_stack[--replace_stack_nr];
7302}
7303
7304/*
7305 * Join the top two items on the replace stack. This removes to "off"'th NUL
7306 * encountered.
7307 */
7308 static void
7309replace_join(off)
7310 int off; /* offset for which NUL to remove */
7311{
7312 int i;
7313
7314 for (i = replace_stack_nr; --i >= 0; )
7315 if (replace_stack[i] == NUL && off-- <= 0)
7316 {
7317 --replace_stack_nr;
7318 mch_memmove(replace_stack + i, replace_stack + i + 1,
7319 (size_t)(replace_stack_nr - i));
7320 return;
7321 }
7322}
7323
7324/*
7325 * Pop bytes from the replace stack until a NUL is found, and insert them
7326 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7327 */
7328 static void
7329replace_pop_ins()
7330{
7331 int cc;
7332 int oldState = State;
7333
7334 State = NORMAL; /* don't want REPLACE here */
7335 while ((cc = replace_pop()) > 0)
7336 {
7337#ifdef FEAT_MBYTE
7338 mb_replace_pop_ins(cc);
7339#else
7340 ins_char(cc);
7341#endif
7342 dec_cursor();
7343 }
7344 State = oldState;
7345}
7346
7347#ifdef FEAT_MBYTE
7348/*
7349 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7350 * indicates a multi-byte char, pop the other bytes too.
7351 */
7352 static void
7353mb_replace_pop_ins(cc)
7354 int cc;
7355{
7356 int n;
7357 char_u buf[MB_MAXBYTES];
7358 int i;
7359 int c;
7360
7361 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7362 {
7363 buf[0] = cc;
7364 for (i = 1; i < n; ++i)
7365 buf[i] = replace_pop();
7366 ins_bytes_len(buf, n);
7367 }
7368 else
7369 ins_char(cc);
7370
7371 if (enc_utf8)
7372 /* Handle composing chars. */
7373 for (;;)
7374 {
7375 c = replace_pop();
7376 if (c == -1) /* stack empty */
7377 break;
7378 if ((n = MB_BYTE2LEN(c)) == 1)
7379 {
7380 /* Not a multi-byte char, put it back. */
7381 replace_push(c);
7382 break;
7383 }
7384 else
7385 {
7386 buf[0] = c;
7387 for (i = 1; i < n; ++i)
7388 buf[i] = replace_pop();
7389 if (utf_iscomposing(utf_ptr2char(buf)))
7390 ins_bytes_len(buf, n);
7391 else
7392 {
7393 /* Not a composing char, put it back. */
7394 for (i = n - 1; i >= 0; --i)
7395 replace_push(buf[i]);
7396 break;
7397 }
7398 }
7399 }
7400}
7401#endif
7402
7403/*
7404 * make the replace stack empty
7405 * (called when exiting replace mode)
7406 */
7407 static void
7408replace_flush()
7409{
7410 vim_free(replace_stack);
7411 replace_stack = NULL;
7412 replace_stack_len = 0;
7413 replace_stack_nr = 0;
7414}
7415
7416/*
7417 * Handle doing a BS for one character.
7418 * cc < 0: replace stack empty, just move cursor
7419 * cc == 0: character was inserted, delete it
7420 * cc > 0: character was replaced, put cc (first byte of original char) back
7421 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007422 * When "limit_col" is >= 0, don't delete before this column. Matters when
7423 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 */
7425 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007426replace_do_bs(limit_col)
7427 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 int cc;
7430#ifdef FEAT_VREPLACE
7431 int orig_len = 0;
7432 int ins_len;
7433 int orig_vcols = 0;
7434 colnr_T start_vcol;
7435 char_u *p;
7436 int i;
7437 int vcol;
7438#endif
7439
7440 cc = replace_pop();
7441 if (cc > 0)
7442 {
7443#ifdef FEAT_VREPLACE
7444 if (State & VREPLACE_FLAG)
7445 {
7446 /* Get the number of screen cells used by the character we are
7447 * going to delete. */
7448 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7449 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7450 }
7451#endif
7452#ifdef FEAT_MBYTE
7453 if (has_mbyte)
7454 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007455 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456# ifdef FEAT_VREPLACE
7457 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007458 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459# endif
7460 replace_push(cc);
7461 }
7462 else
7463#endif
7464 {
7465 pchar_cursor(cc);
7466#ifdef FEAT_VREPLACE
7467 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007468 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469#endif
7470 }
7471 replace_pop_ins();
7472
7473#ifdef FEAT_VREPLACE
7474 if (State & VREPLACE_FLAG)
7475 {
7476 /* Get the number of screen cells used by the inserted characters */
7477 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007478 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 vcol = start_vcol;
7480 for (i = 0; i < ins_len; ++i)
7481 {
7482 vcol += chartabsize(p + i, vcol);
7483#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007484 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485#endif
7486 }
7487 vcol -= start_vcol;
7488
7489 /* Delete spaces that were inserted after the cursor to keep the
7490 * text aligned. */
7491 curwin->w_cursor.col += ins_len;
7492 while (vcol > orig_vcols && gchar_cursor() == ' ')
7493 {
7494 del_char(FALSE);
7495 ++orig_vcols;
7496 }
7497 curwin->w_cursor.col -= ins_len;
7498 }
7499#endif
7500
7501 /* mark the buffer as changed and prepare for displaying */
7502 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7503 }
7504 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007505 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506}
7507
7508#ifdef FEAT_CINDENT
7509/*
7510 * Return TRUE if C-indenting is on.
7511 */
7512 static int
7513cindent_on()
7514{
7515 return (!p_paste && (curbuf->b_p_cin
7516# ifdef FEAT_EVAL
7517 || *curbuf->b_p_inde != NUL
7518# endif
7519 ));
7520}
7521#endif
7522
7523#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7524/*
7525 * Re-indent the current line, based on the current contents of it and the
7526 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7527 * confused what all the part that handles Control-T is doing that I'm not.
7528 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7529 */
7530
7531 void
7532fixthisline(get_the_indent)
7533 int (*get_the_indent) __ARGS((void));
7534{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007535 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (linewhite(curwin->w_cursor.lnum))
7537 did_ai = TRUE; /* delete the indent if the line stays empty */
7538}
7539
7540 void
7541fix_indent()
7542{
7543 if (p_paste)
7544 return;
7545# ifdef FEAT_LISP
7546 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7547 fixthisline(get_lisp_indent);
7548# endif
7549# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7550 else
7551# endif
7552# ifdef FEAT_CINDENT
7553 if (cindent_on())
7554 do_c_expr_indent();
7555# endif
7556}
7557
7558#endif
7559
7560#ifdef FEAT_CINDENT
7561/*
7562 * return TRUE if 'cinkeys' contains the key "keytyped",
7563 * when == '*': Only if key is preceded with '*' (indent before insert)
7564 * when == '!': Only if key is prededed with '!' (don't insert)
7565 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7566 *
7567 * "keytyped" can have a few special values:
7568 * KEY_OPEN_FORW
7569 * KEY_OPEN_BACK
7570 * KEY_COMPLETE just finished completion.
7571 *
7572 * If line_is_empty is TRUE accept keys with '0' before them.
7573 */
7574 int
7575in_cinkeys(keytyped, when, line_is_empty)
7576 int keytyped;
7577 int when;
7578 int line_is_empty;
7579{
7580 char_u *look;
7581 int try_match;
7582 int try_match_word;
7583 char_u *p;
7584 char_u *line;
7585 int icase;
7586 int i;
7587
Bram Moolenaar30848942009-12-24 14:46:12 +00007588 if (keytyped == NUL)
7589 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7590 return FALSE;
7591
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592#ifdef FEAT_EVAL
7593 if (*curbuf->b_p_inde != NUL)
7594 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7595 else
7596#endif
7597 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7598 while (*look)
7599 {
7600 /*
7601 * Find out if we want to try a match with this key, depending on
7602 * 'when' and a '*' or '!' before the key.
7603 */
7604 switch (when)
7605 {
7606 case '*': try_match = (*look == '*'); break;
7607 case '!': try_match = (*look == '!'); break;
7608 default: try_match = (*look != '*'); break;
7609 }
7610 if (*look == '*' || *look == '!')
7611 ++look;
7612
7613 /*
7614 * If there is a '0', only accept a match if the line is empty.
7615 * But may still match when typing last char of a word.
7616 */
7617 if (*look == '0')
7618 {
7619 try_match_word = try_match;
7620 if (!line_is_empty)
7621 try_match = FALSE;
7622 ++look;
7623 }
7624 else
7625 try_match_word = FALSE;
7626
7627 /*
7628 * does it look like a control character?
7629 */
7630 if (*look == '^'
7631#ifdef EBCDIC
7632 && (Ctrl_chr(look[1]) != 0)
7633#else
7634 && look[1] >= '?' && look[1] <= '_'
7635#endif
7636 )
7637 {
7638 if (try_match && keytyped == Ctrl_chr(look[1]))
7639 return TRUE;
7640 look += 2;
7641 }
7642 /*
7643 * 'o' means "o" command, open forward.
7644 * 'O' means "O" command, open backward.
7645 */
7646 else if (*look == 'o')
7647 {
7648 if (try_match && keytyped == KEY_OPEN_FORW)
7649 return TRUE;
7650 ++look;
7651 }
7652 else if (*look == 'O')
7653 {
7654 if (try_match && keytyped == KEY_OPEN_BACK)
7655 return TRUE;
7656 ++look;
7657 }
7658
7659 /*
7660 * 'e' means to check for "else" at start of line and just before the
7661 * cursor.
7662 */
7663 else if (*look == 'e')
7664 {
7665 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7666 {
7667 p = ml_get_curline();
7668 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7669 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7670 return TRUE;
7671 }
7672 ++look;
7673 }
7674
7675 /*
7676 * ':' only causes an indent if it is at the end of a label or case
7677 * statement, or when it was before typing the ':' (to fix
7678 * class::method for C++).
7679 */
7680 else if (*look == ':')
7681 {
7682 if (try_match && keytyped == ':')
7683 {
7684 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007685 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7686 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007688 /* Need to get the line again after cin_islabel(). */
7689 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (curwin->w_cursor.col > 2
7691 && p[curwin->w_cursor.col - 1] == ':'
7692 && p[curwin->w_cursor.col - 2] == ':')
7693 {
7694 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007695 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 || cin_islabel(30));
7697 p = ml_get_curline();
7698 p[curwin->w_cursor.col - 1] = ':';
7699 if (i)
7700 return TRUE;
7701 }
7702 }
7703 ++look;
7704 }
7705
7706
7707 /*
7708 * Is it a key in <>, maybe?
7709 */
7710 else if (*look == '<')
7711 {
7712 if (try_match)
7713 {
7714 /*
7715 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7716 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7717 * >, *, : and ! keys if they really really want to.
7718 */
7719 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7720 && keytyped == look[1])
7721 return TRUE;
7722
7723 if (keytyped == get_special_key_code(look + 1))
7724 return TRUE;
7725 }
7726 while (*look && *look != '>')
7727 look++;
7728 while (*look == '>')
7729 look++;
7730 }
7731
7732 /*
7733 * Is it a word: "=word"?
7734 */
7735 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7736 {
7737 ++look;
7738 if (*look == '~')
7739 {
7740 icase = TRUE;
7741 ++look;
7742 }
7743 else
7744 icase = FALSE;
7745 p = vim_strchr(look, ',');
7746 if (p == NULL)
7747 p = look + STRLEN(look);
7748 if ((try_match || try_match_word)
7749 && curwin->w_cursor.col >= (colnr_T)(p - look))
7750 {
7751 int match = FALSE;
7752
7753#ifdef FEAT_INS_EXPAND
7754 if (keytyped == KEY_COMPLETE)
7755 {
7756 char_u *s;
7757
7758 /* Just completed a word, check if it starts with "look".
7759 * search back for the start of a word. */
7760 line = ml_get_curline();
7761# ifdef FEAT_MBYTE
7762 if (has_mbyte)
7763 {
7764 char_u *n;
7765
7766 for (s = line + curwin->w_cursor.col; s > line; s = n)
7767 {
7768 n = mb_prevptr(line, s);
7769 if (!vim_iswordp(n))
7770 break;
7771 }
7772 }
7773 else
7774# endif
7775 for (s = line + curwin->w_cursor.col; s > line; --s)
7776 if (!vim_iswordc(s[-1]))
7777 break;
7778 if (s + (p - look) <= line + curwin->w_cursor.col
7779 && (icase
7780 ? MB_STRNICMP(s, look, p - look)
7781 : STRNCMP(s, look, p - look)) == 0)
7782 match = TRUE;
7783 }
7784 else
7785#endif
7786 /* TODO: multi-byte */
7787 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7788 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7789 {
7790 line = ml_get_cursor();
7791 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7792 || !vim_iswordc(line[-(p - look) - 1]))
7793 && (icase
7794 ? MB_STRNICMP(line - (p - look), look, p - look)
7795 : STRNCMP(line - (p - look), look, p - look))
7796 == 0)
7797 match = TRUE;
7798 }
7799 if (match && try_match_word && !try_match)
7800 {
7801 /* "0=word": Check if there are only blanks before the
7802 * word. */
7803 line = ml_get_curline();
7804 if ((int)(skipwhite(line) - line) !=
7805 (int)(curwin->w_cursor.col - (p - look)))
7806 match = FALSE;
7807 }
7808 if (match)
7809 return TRUE;
7810 }
7811 look = p;
7812 }
7813
7814 /*
7815 * ok, it's a boring generic character.
7816 */
7817 else
7818 {
7819 if (try_match && *look == keytyped)
7820 return TRUE;
7821 ++look;
7822 }
7823
7824 /*
7825 * Skip over ", ".
7826 */
7827 look = skip_to_option_part(look);
7828 }
7829 return FALSE;
7830}
7831#endif /* FEAT_CINDENT */
7832
7833#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7834/*
7835 * Map Hebrew keyboard when in hkmap mode.
7836 */
7837 int
7838hkmap(c)
7839 int c;
7840{
7841 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7842 {
7843 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7844 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7845 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7846 static char_u map[26] =
7847 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7848 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7849 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7850 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7851 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7852 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7853 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7854 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7855 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7856
7857 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7858 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7859 /* '-1'='sofit' */
7860 else if (c == 'x')
7861 return 'X';
7862 else if (c == 'q')
7863 return '\''; /* {geresh}={'} */
7864 else if (c == 246)
7865 return ' '; /* \"o --> ' ' for a german keyboard */
7866 else if (c == 228)
7867 return ' '; /* \"a --> ' ' -- / -- */
7868 else if (c == 252)
7869 return ' '; /* \"u --> ' ' -- / -- */
7870#ifdef EBCDIC
7871 else if (islower(c))
7872#else
7873 /* NOTE: islower() does not do the right thing for us on Linux so we
7874 * do this the same was as 5.7 and previous, so it works correctly on
7875 * all systems. Specifically, the e.g. Delete and Arrow keys are
7876 * munged and won't work if e.g. searching for Hebrew text.
7877 */
7878 else if (c >= 'a' && c <= 'z')
7879#endif
7880 return (int)(map[CharOrdLow(c)] + p_aleph);
7881 else
7882 return c;
7883 }
7884 else
7885 {
7886 switch (c)
7887 {
7888 case '`': return ';';
7889 case '/': return '.';
7890 case '\'': return ',';
7891 case 'q': return '/';
7892 case 'w': return '\'';
7893
7894 /* Hebrew letters - set offset from 'a' */
7895 case ',': c = '{'; break;
7896 case '.': c = 'v'; break;
7897 case ';': c = 't'; break;
7898 default: {
7899 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7900
7901#ifdef EBCDIC
7902 /* see note about islower() above */
7903 if (!islower(c))
7904#else
7905 if (c < 'a' || c > 'z')
7906#endif
7907 return c;
7908 c = str[CharOrdLow(c)];
7909 break;
7910 }
7911 }
7912
7913 return (int)(CharOrdLow(c) + p_aleph);
7914 }
7915}
7916#endif
7917
7918 static void
7919ins_reg()
7920{
7921 int need_redraw = FALSE;
7922 int regname;
7923 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007924#ifdef FEAT_VISUAL
7925 int vis_active = VIsual_active;
7926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
7928 /*
7929 * If we are going to wait for a character, show a '"'.
7930 */
7931 pc_status = PC_STATUS_UNSET;
7932 if (redrawing() && !char_avail())
7933 {
7934 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007935 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937 edit_putchar('"', TRUE);
7938#ifdef FEAT_CMDL_INFO
7939 add_to_showcmd_c(Ctrl_R);
7940#endif
7941 }
7942
7943#ifdef USE_ON_FLY_SCROLL
7944 dont_scroll = TRUE; /* disallow scrolling here */
7945#endif
7946
7947 /*
7948 * Don't map the register name. This also prevents the mode message to be
7949 * deleted when ESC is hit.
7950 */
7951 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007952 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7955 {
7956 /* Get a third key for literal register insertion */
7957 literally = regname;
7958#ifdef FEAT_CMDL_INFO
7959 add_to_showcmd_c(literally);
7960#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007961 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964 --no_mapping;
7965
7966#ifdef FEAT_EVAL
7967 /*
7968 * Don't call u_sync() while getting the expression,
7969 * evaluating it or giving an error message for it!
7970 */
7971 ++no_u_sync;
7972 if (regname == '=')
7973 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007974# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007978# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 /* Restore the Input Method. */
7980 if (im_on)
7981 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007984 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7985 {
7986 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 else
7990 {
7991#endif
7992 if (literally == Ctrl_O || literally == Ctrl_P)
7993 {
7994 /* Append the command to the redo buffer. */
7995 AppendCharToRedobuff(Ctrl_R);
7996 AppendCharToRedobuff(literally);
7997 AppendCharToRedobuff(regname);
7998
7999 do_put(regname, BACKWARD, 1L,
8000 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8001 }
8002 else if (insert_reg(regname, literally) == FAIL)
8003 {
8004 vim_beep();
8005 need_redraw = TRUE; /* remove the '"' */
8006 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008007 else if (stop_insert_mode)
8008 /* When the '=' register was used and a function was invoked that
8009 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8010 * insert anything, need to remove the '"' */
8011 need_redraw = TRUE;
8012
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013#ifdef FEAT_EVAL
8014 }
8015 --no_u_sync;
8016#endif
8017#ifdef FEAT_CMDL_INFO
8018 clear_showcmd();
8019#endif
8020
8021 /* If the inserted register is empty, we need to remove the '"' */
8022 if (need_redraw || stuff_empty())
8023 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008024
8025#ifdef FEAT_VISUAL
8026 /* Disallow starting Visual mode here, would get a weird mode. */
8027 if (!vis_active && VIsual_active)
8028 end_visual_mode();
8029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
8032/*
8033 * CTRL-G commands in Insert mode.
8034 */
8035 static void
8036ins_ctrl_g()
8037{
8038 int c;
8039
8040#ifdef FEAT_INS_EXPAND
8041 /* Right after CTRL-X the cursor will be after the ruler. */
8042 setcursor();
8043#endif
8044
8045 /*
8046 * Don't map the second key. This also prevents the mode message to be
8047 * deleted when ESC is hit.
8048 */
8049 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008050 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 --no_mapping;
8052 switch (c)
8053 {
8054 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8055 case K_UP:
8056 case Ctrl_K:
8057 case 'k': ins_up(TRUE);
8058 break;
8059
8060 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8061 case K_DOWN:
8062 case Ctrl_J:
8063 case 'j': ins_down(TRUE);
8064 break;
8065
8066 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008067 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008069
8070 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008071 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008072 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 break;
8074
8075 /* Unknown CTRL-G command, reserved for future expansion. */
8076 default: vim_beep();
8077 }
8078}
8079
8080/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008081 * CTRL-^ in Insert mode.
8082 */
8083 static void
8084ins_ctrl_hat()
8085{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008086 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008087 {
8088 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8089 if (State & LANGMAP)
8090 {
8091 curbuf->b_p_iminsert = B_IMODE_NONE;
8092 State &= ~LANGMAP;
8093 }
8094 else
8095 {
8096 curbuf->b_p_iminsert = B_IMODE_LMAP;
8097 State |= LANGMAP;
8098#ifdef USE_IM_CONTROL
8099 im_set_active(FALSE);
8100#endif
8101 }
8102 }
8103#ifdef USE_IM_CONTROL
8104 else
8105 {
8106 /* There are no ":lmap" mappings, toggle IM */
8107 if (im_get_status())
8108 {
8109 curbuf->b_p_iminsert = B_IMODE_NONE;
8110 im_set_active(FALSE);
8111 }
8112 else
8113 {
8114 curbuf->b_p_iminsert = B_IMODE_IM;
8115 State &= ~LANGMAP;
8116 im_set_active(TRUE);
8117 }
8118 }
8119#endif
8120 set_iminsert_global();
8121 showmode();
8122#ifdef FEAT_GUI
8123 /* may show different cursor shape or color */
8124 if (gui.in_use)
8125 gui_update_cursor(TRUE, FALSE);
8126#endif
8127#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8128 /* Show/unshow value of 'keymap' in status lines. */
8129 status_redraw_curbuf();
8130#endif
8131}
8132
8133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 * Handle ESC in insert mode.
8135 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8136 * insert.
8137 */
8138 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008139ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 long *count;
8141 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008142 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
8144 int temp;
8145 static int disabled_redraw = FALSE;
8146
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008147#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008148 check_spell_redraw();
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150#if defined(FEAT_HANGULIN)
8151# if defined(ESC_CHG_TO_ENG_MODE)
8152 hangul_input_state_set(0);
8153# endif
8154 if (composing_hangul)
8155 {
8156 push_raw_key(composing_hangul_buffer, 2);
8157 composing_hangul = 0;
8158 }
8159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
8161 temp = curwin->w_cursor.col;
8162 if (disabled_redraw)
8163 {
8164 --RedrawingDisabled;
8165 disabled_redraw = FALSE;
8166 }
8167 if (!arrow_used)
8168 {
8169 /*
8170 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008171 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8172 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 */
8174 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008175 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176
8177 /*
8178 * Repeating insert may take a long time. Check for
8179 * interrupt now and then.
8180 */
8181 if (*count > 0)
8182 {
8183 line_breakcheck();
8184 if (got_int)
8185 *count = 0;
8186 }
8187
8188 if (--*count > 0) /* repeat what was typed */
8189 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008190 /* Vi repeats the insert without replacing characters. */
8191 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8192 State &= ~REPLACE_FLAG;
8193
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 (void)start_redo_ins();
8195 if (cmdchar == 'r' || cmdchar == 'v')
8196 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8197 ++RedrawingDisabled;
8198 disabled_redraw = TRUE;
8199 return FALSE; /* repeat the insert */
8200 }
8201 stop_insert(&curwin->w_cursor, TRUE);
8202 undisplay_dollar();
8203 }
8204
8205 /* When an autoindent was removed, curswant stays after the
8206 * indent */
8207 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8208 curwin->w_set_curswant = TRUE;
8209
8210 /* Remember the last Insert position in the '^ mark. */
8211 if (!cmdmod.keepjumps)
8212 curbuf->b_last_insert = curwin->w_cursor;
8213
8214 /*
8215 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008216 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008218 if (!nomove
8219 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220#ifdef FEAT_VIRTUALEDIT
8221 || curwin->w_cursor.coladd > 0
8222#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008223 )
8224 && (restart_edit == NUL
8225 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008227 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008229 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230#ifdef FEAT_RIGHTLEFT
8231 && !revins_on
8232#endif
8233 )
8234 {
8235#ifdef FEAT_VIRTUALEDIT
8236 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8237 {
8238 oneleft();
8239 if (restart_edit != NUL)
8240 ++curwin->w_cursor.coladd;
8241 }
8242 else
8243#endif
8244 {
8245 --curwin->w_cursor.col;
8246#ifdef FEAT_MBYTE
8247 /* Correct cursor for multi-byte character. */
8248 if (has_mbyte)
8249 mb_adjust_cursor();
8250#endif
8251 }
8252 }
8253
8254#ifdef USE_IM_CONTROL
8255 /* Disable IM to allow typing English directly for Normal mode commands.
8256 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8257 * well). */
8258 if (!(State & LANGMAP))
8259 im_save_status(&curbuf->b_p_iminsert);
8260 im_set_active(FALSE);
8261#endif
8262
8263 State = NORMAL;
8264 /* need to position cursor again (e.g. when on a TAB ) */
8265 changed_cline_bef_curs();
8266
8267#ifdef FEAT_MOUSE
8268 setmouse();
8269#endif
8270#ifdef CURSOR_SHAPE
8271 ui_cursor_shape(); /* may show different cursor shape */
8272#endif
8273
8274 /*
8275 * When recording or for CTRL-O, need to display the new mode.
8276 * Otherwise remove the mode message.
8277 */
8278 if (Recording || restart_edit != NUL)
8279 showmode();
8280 else if (p_smd)
8281 MSG("");
8282
8283 return TRUE; /* exit Insert mode */
8284}
8285
8286#ifdef FEAT_RIGHTLEFT
8287/*
8288 * Toggle language: hkmap and revins_on.
8289 * Move to end of reverse inserted text.
8290 */
8291 static void
8292ins_ctrl_()
8293{
8294 if (revins_on && revins_chars && revins_scol >= 0)
8295 {
8296 while (gchar_cursor() != NUL && revins_chars--)
8297 ++curwin->w_cursor.col;
8298 }
8299 p_ri = !p_ri;
8300 revins_on = (State == INSERT && p_ri);
8301 if (revins_on)
8302 {
8303 revins_scol = curwin->w_cursor.col;
8304 revins_legal++;
8305 revins_chars = 0;
8306 undisplay_dollar();
8307 }
8308 else
8309 revins_scol = -1;
8310#ifdef FEAT_FKMAP
8311 if (p_altkeymap)
8312 {
8313 /*
8314 * to be consistent also for redo command, using '.'
8315 * set arrow_used to true and stop it - causing to redo
8316 * characters entered in one mode (normal/reverse insert).
8317 */
8318 arrow_used = TRUE;
8319 (void)stop_arrow();
8320 p_fkmap = curwin->w_p_rl ^ p_ri;
8321 if (p_fkmap && p_ri)
8322 State = INSERT;
8323 }
8324 else
8325#endif
8326 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8327 showmode();
8328}
8329#endif
8330
8331#ifdef FEAT_VISUAL
8332/*
8333 * If 'keymodel' contains "startsel", may start selection.
8334 * Returns TRUE when a CTRL-O and other keys stuffed.
8335 */
8336 static int
8337ins_start_select(c)
8338 int c;
8339{
8340 if (km_startsel)
8341 switch (c)
8342 {
8343 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 case K_PAGEUP:
8346 case K_KPAGEUP:
8347 case K_PAGEDOWN:
8348 case K_KPAGEDOWN:
8349# ifdef MACOS
8350 case K_LEFT:
8351 case K_RIGHT:
8352 case K_UP:
8353 case K_DOWN:
8354 case K_END:
8355 case K_HOME:
8356# endif
8357 if (!(mod_mask & MOD_MASK_SHIFT))
8358 break;
8359 /* FALLTHROUGH */
8360 case K_S_LEFT:
8361 case K_S_RIGHT:
8362 case K_S_UP:
8363 case K_S_DOWN:
8364 case K_S_END:
8365 case K_S_HOME:
8366 /* Start selection right away, the cursor can move with
8367 * CTRL-O when beyond the end of the line. */
8368 start_selection();
8369
8370 /* Execute the key in (insert) Select mode. */
8371 stuffcharReadbuff(Ctrl_O);
8372 if (mod_mask)
8373 {
8374 char_u buf[4];
8375
8376 buf[0] = K_SPECIAL;
8377 buf[1] = KS_MODIFIER;
8378 buf[2] = mod_mask;
8379 buf[3] = NUL;
8380 stuffReadbuff(buf);
8381 }
8382 stuffcharReadbuff(c);
8383 return TRUE;
8384 }
8385 return FALSE;
8386}
8387#endif
8388
8389/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008390 * <Insert> key in Insert mode: toggle insert/remplace mode.
8391 */
8392 static void
8393ins_insert(replaceState)
8394 int replaceState;
8395{
8396#ifdef FEAT_FKMAP
8397 if (p_fkmap && p_ri)
8398 {
8399 beep_flush();
8400 EMSG(farsi_text_3); /* encoded in Farsi */
8401 return;
8402 }
8403#endif
8404
8405#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008406# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008407 set_vim_var_string(VV_INSERTMODE,
8408 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409# ifdef FEAT_VREPLACE
8410 replaceState == VREPLACE ? "v" :
8411# endif
8412 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008413# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008414 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8415#endif
8416 if (State & REPLACE_FLAG)
8417 State = INSERT | (State & LANGMAP);
8418 else
8419 State = replaceState | (State & LANGMAP);
8420 AppendCharToRedobuff(K_INS);
8421 showmode();
8422#ifdef CURSOR_SHAPE
8423 ui_cursor_shape(); /* may show different cursor shape */
8424#endif
8425}
8426
8427/*
8428 * Pressed CTRL-O in Insert mode.
8429 */
8430 static void
8431ins_ctrl_o()
8432{
8433#ifdef FEAT_VREPLACE
8434 if (State & VREPLACE_FLAG)
8435 restart_edit = 'V';
8436 else
8437#endif
8438 if (State & REPLACE_FLAG)
8439 restart_edit = 'R';
8440 else
8441 restart_edit = 'I';
8442#ifdef FEAT_VIRTUALEDIT
8443 if (virtual_active())
8444 ins_at_eol = FALSE; /* cursor always keeps its column */
8445 else
8446#endif
8447 ins_at_eol = (gchar_cursor() == NUL);
8448}
8449
8450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 * If the cursor is on an indent, ^T/^D insert/delete one
8452 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008453 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 * with vi. But vi only supports ^T and ^D after an
8455 * autoindent, we support it everywhere.
8456 */
8457 static void
8458ins_shift(c, lastc)
8459 int c;
8460 int lastc;
8461{
8462 if (stop_arrow() == FAIL)
8463 return;
8464 AppendCharToRedobuff(c);
8465
8466 /*
8467 * 0^D and ^^D: remove all indent.
8468 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008469 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8470 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
8472 --curwin->w_cursor.col;
8473 (void)del_char(FALSE); /* delete the '^' or '0' */
8474 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8475 if (State & REPLACE_FLAG)
8476 replace_pop_ins();
8477 if (lastc == '^')
8478 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008479 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
8481 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008482 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8485 did_ai = FALSE;
8486#ifdef FEAT_SMARTINDENT
8487 did_si = FALSE;
8488 can_si = FALSE;
8489 can_si_back = FALSE;
8490#endif
8491#ifdef FEAT_CINDENT
8492 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8493#endif
8494}
8495
8496 static void
8497ins_del()
8498{
8499 int temp;
8500
8501 if (stop_arrow() == FAIL)
8502 return;
8503 if (gchar_cursor() == NUL) /* delete newline */
8504 {
8505 temp = curwin->w_cursor.col;
8506 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008507 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 vim_beep();
8509 else
8510 curwin->w_cursor.col = temp;
8511 }
8512 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8513 vim_beep();
8514 did_ai = FALSE;
8515#ifdef FEAT_SMARTINDENT
8516 did_si = FALSE;
8517 can_si = FALSE;
8518 can_si_back = FALSE;
8519#endif
8520 AppendCharToRedobuff(K_DEL);
8521}
8522
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008523static void ins_bs_one __ARGS((colnr_T *vcolp));
8524
8525/*
8526 * Delete one character for ins_bs().
8527 */
8528 static void
8529ins_bs_one(vcolp)
8530 colnr_T *vcolp;
8531{
8532 dec_cursor();
8533 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8534 if (State & REPLACE_FLAG)
8535 {
8536 /* Don't delete characters before the insert point when in
8537 * Replace mode */
8538 if (curwin->w_cursor.lnum != Insstart.lnum
8539 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008540 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008541 }
8542 else
8543 (void)del_char(FALSE);
8544}
8545
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546/*
8547 * Handle Backspace, delete-word and delete-line in Insert mode.
8548 * Return TRUE when backspace was actually used.
8549 */
8550 static int
8551ins_bs(c, mode, inserted_space_p)
8552 int c;
8553 int mode;
8554 int *inserted_space_p;
8555{
8556 linenr_T lnum;
8557 int cc;
8558 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008559 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 colnr_T mincol;
8561 int did_backspace = FALSE;
8562 int in_indent;
8563 int oldState;
8564#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008565 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566#endif
8567
8568 /*
8569 * can't delete anything in an empty file
8570 * can't backup past first character in buffer
8571 * can't backup past starting point unless 'backspace' > 1
8572 * can backup to a previous line if 'backspace' == 0
8573 */
8574 if ( bufempty()
8575 || (
8576#ifdef FEAT_RIGHTLEFT
8577 !revins_on &&
8578#endif
8579 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8580 || (!can_bs(BS_START)
8581 && (arrow_used
8582 || (curwin->w_cursor.lnum == Insstart.lnum
8583 && curwin->w_cursor.col <= Insstart.col)))
8584 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8585 && curwin->w_cursor.col <= ai_col)
8586 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8587 {
8588 vim_beep();
8589 return FALSE;
8590 }
8591
8592 if (stop_arrow() == FAIL)
8593 return FALSE;
8594 in_indent = inindent(0);
8595#ifdef FEAT_CINDENT
8596 if (in_indent)
8597 can_cindent = FALSE;
8598#endif
8599#ifdef FEAT_COMMENTS
8600 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8601#endif
8602#ifdef FEAT_RIGHTLEFT
8603 if (revins_on) /* put cursor after last inserted char */
8604 inc_cursor();
8605#endif
8606
8607#ifdef FEAT_VIRTUALEDIT
8608 /* Virtualedit:
8609 * BACKSPACE_CHAR eats a virtual space
8610 * BACKSPACE_WORD eats all coladd
8611 * BACKSPACE_LINE eats all coladd and keeps going
8612 */
8613 if (curwin->w_cursor.coladd > 0)
8614 {
8615 if (mode == BACKSPACE_CHAR)
8616 {
8617 --curwin->w_cursor.coladd;
8618 return TRUE;
8619 }
8620 if (mode == BACKSPACE_WORD)
8621 {
8622 curwin->w_cursor.coladd = 0;
8623 return TRUE;
8624 }
8625 curwin->w_cursor.coladd = 0;
8626 }
8627#endif
8628
8629 /*
8630 * delete newline!
8631 */
8632 if (curwin->w_cursor.col == 0)
8633 {
8634 lnum = Insstart.lnum;
8635 if (curwin->w_cursor.lnum == Insstart.lnum
8636#ifdef FEAT_RIGHTLEFT
8637 || revins_on
8638#endif
8639 )
8640 {
8641 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8642 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8643 return FALSE;
8644 --Insstart.lnum;
8645 Insstart.col = MAXCOL;
8646 }
8647 /*
8648 * In replace mode:
8649 * cc < 0: NL was inserted, delete it
8650 * cc >= 0: NL was replaced, put original characters back
8651 */
8652 cc = -1;
8653 if (State & REPLACE_FLAG)
8654 cc = replace_pop(); /* returns -1 if NL was inserted */
8655 /*
8656 * In replace mode, in the line we started replacing, we only move the
8657 * cursor.
8658 */
8659 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8660 {
8661 dec_cursor();
8662 }
8663 else
8664 {
8665#ifdef FEAT_VREPLACE
8666 if (!(State & VREPLACE_FLAG)
8667 || curwin->w_cursor.lnum > orig_line_count)
8668#endif
8669 {
8670 temp = gchar_cursor(); /* remember current char */
8671 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008672
8673 /* When "aw" is in 'formatoptions' we must delete the space at
8674 * the end of the line, otherwise the line will be broken
8675 * again when auto-formatting. */
8676 if (has_format_option(FO_AUTO)
8677 && has_format_option(FO_WHITE_PAR))
8678 {
8679 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8680 TRUE);
8681 int len;
8682
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008683 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008684 if (len > 0 && ptr[len - 1] == ' ')
8685 ptr[len - 1] = NUL;
8686 }
8687
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008688 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if (temp == NUL && gchar_cursor() != NUL)
8690 inc_cursor();
8691 }
8692#ifdef FEAT_VREPLACE
8693 else
8694 dec_cursor();
8695#endif
8696
8697 /*
8698 * In REPLACE mode we have to put back the text that was replaced
8699 * by the NL. On the replace stack is first a NUL-terminated
8700 * sequence of characters that were deleted and then the
8701 * characters that NL replaced.
8702 */
8703 if (State & REPLACE_FLAG)
8704 {
8705 /*
8706 * Do the next ins_char() in NORMAL state, to
8707 * prevent ins_char() from replacing characters and
8708 * avoiding showmatch().
8709 */
8710 oldState = State;
8711 State = NORMAL;
8712 /*
8713 * restore characters (blanks) deleted after cursor
8714 */
8715 while (cc > 0)
8716 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008717 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718#ifdef FEAT_MBYTE
8719 mb_replace_pop_ins(cc);
8720#else
8721 ins_char(cc);
8722#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008723 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 cc = replace_pop();
8725 }
8726 /* restore the characters that NL replaced */
8727 replace_pop_ins();
8728 State = oldState;
8729 }
8730 }
8731 did_ai = FALSE;
8732 }
8733 else
8734 {
8735 /*
8736 * Delete character(s) before the cursor.
8737 */
8738#ifdef FEAT_RIGHTLEFT
8739 if (revins_on) /* put cursor on last inserted char */
8740 dec_cursor();
8741#endif
8742 mincol = 0;
8743 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008744 if (mode == BACKSPACE_LINE
8745 && (curbuf->b_p_ai
8746#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008747 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008748#endif
8749 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750#ifdef FEAT_RIGHTLEFT
8751 && !revins_on
8752#endif
8753 )
8754 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008755 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008757 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008759 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 }
8761
8762 /*
8763 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8764 */
8765 if ( mode == BACKSPACE_CHAR
8766 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008767 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008768 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 && (*(ml_get_cursor() - 1) == TAB
8770 || (*(ml_get_cursor() - 1) == ' '
8771 && (!*inserted_space_p
8772 || arrow_used))))))
8773 {
8774 int ts;
8775 colnr_T vcol;
8776 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008777 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
8779 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008780 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 ts = curbuf->b_p_sw;
8782 else
8783 ts = curbuf->b_p_sts;
8784 /* Compute the virtual column where we want to be. Since
8785 * 'showbreak' may get in the way, need to get the last column of
8786 * the previous character. */
8787 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008788 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 dec_cursor();
8790 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8791 inc_cursor();
8792 want_vcol = (want_vcol / ts) * ts;
8793
8794 /* delete characters until we are at or before want_vcol */
8795 while (vcol > want_vcol
8796 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008797 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
8799 /* insert extra spaces until we are at want_vcol */
8800 while (vcol < want_vcol)
8801 {
8802 /* Remember the first char we inserted */
8803 if (curwin->w_cursor.lnum == Insstart.lnum
8804 && curwin->w_cursor.col < Insstart.col)
8805 Insstart.col = curwin->w_cursor.col;
8806
8807#ifdef FEAT_VREPLACE
8808 if (State & VREPLACE_FLAG)
8809 ins_char(' ');
8810 else
8811#endif
8812 {
8813 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008814 if ((State & REPLACE_FLAG))
8815 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8818 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008819
8820 /* If we are now back where we started delete one character. Can
8821 * happen when using 'sts' and 'linebreak'. */
8822 if (vcol >= start_vcol)
8823 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 }
8825
8826 /*
8827 * Delete upto starting point, start of line or previous word.
8828 */
8829 else do
8830 {
8831#ifdef FEAT_RIGHTLEFT
8832 if (!revins_on) /* put cursor on char to be deleted */
8833#endif
8834 dec_cursor();
8835
8836 /* start of word? */
8837 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8838 {
8839 mode = BACKSPACE_WORD_NOT_SPACE;
8840 temp = vim_iswordc(gchar_cursor());
8841 }
8842 /* end of word? */
8843 else if (mode == BACKSPACE_WORD_NOT_SPACE
8844 && (vim_isspace(cc = gchar_cursor())
8845 || vim_iswordc(cc) != temp))
8846 {
8847#ifdef FEAT_RIGHTLEFT
8848 if (!revins_on)
8849#endif
8850 inc_cursor();
8851#ifdef FEAT_RIGHTLEFT
8852 else if (State & REPLACE_FLAG)
8853 dec_cursor();
8854#endif
8855 break;
8856 }
8857 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008858 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 else
8860 {
8861#ifdef FEAT_MBYTE
8862 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864#endif
8865 (void)del_char(FALSE);
8866#ifdef FEAT_MBYTE
8867 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008868 * If there are combining characters and 'delcombine' is set
8869 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 * character.
8871 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008872 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 inc_cursor();
8874#endif
8875#ifdef FEAT_RIGHTLEFT
8876 if (revins_chars)
8877 {
8878 revins_chars--;
8879 revins_legal++;
8880 }
8881 if (revins_on && gchar_cursor() == NUL)
8882 break;
8883#endif
8884 }
8885 /* Just a single backspace?: */
8886 if (mode == BACKSPACE_CHAR)
8887 break;
8888 } while (
8889#ifdef FEAT_RIGHTLEFT
8890 revins_on ||
8891#endif
8892 (curwin->w_cursor.col > mincol
8893 && (curwin->w_cursor.lnum != Insstart.lnum
8894 || curwin->w_cursor.col != Insstart.col)));
8895 did_backspace = TRUE;
8896 }
8897#ifdef FEAT_SMARTINDENT
8898 did_si = FALSE;
8899 can_si = FALSE;
8900 can_si_back = FALSE;
8901#endif
8902 if (curwin->w_cursor.col <= 1)
8903 did_ai = FALSE;
8904 /*
8905 * It's a little strange to put backspaces into the redo
8906 * buffer, but it makes auto-indent a lot easier to deal
8907 * with.
8908 */
8909 AppendCharToRedobuff(c);
8910
8911 /* If deleted before the insertion point, adjust it */
8912 if (curwin->w_cursor.lnum == Insstart.lnum
8913 && curwin->w_cursor.col < Insstart.col)
8914 Insstart.col = curwin->w_cursor.col;
8915
8916 /* vi behaviour: the cursor moves backward but the character that
8917 * was there remains visible
8918 * Vim behaviour: the cursor moves backward and the character that
8919 * was there is erased from the screen.
8920 * We can emulate the vi behaviour by pretending there is a dollar
8921 * displayed even when there isn't.
8922 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8923 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8924 dollar_vcol = curwin->w_virtcol;
8925
Bram Moolenaarce3be472008-01-14 19:12:28 +00008926#ifdef FEAT_FOLDING
8927 /* When deleting a char the cursor line must never be in a closed fold.
8928 * E.g., when 'foldmethod' is indent and deleting the first non-white
8929 * char before a Tab. */
8930 if (did_backspace)
8931 foldOpenCursor();
8932#endif
8933
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 return did_backspace;
8935}
8936
8937#ifdef FEAT_MOUSE
8938 static void
8939ins_mouse(c)
8940 int c;
8941{
8942 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008943 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945# ifdef FEAT_GUI
8946 /* When GUI is active, also move/paste when 'mouse' is empty */
8947 if (!gui.in_use)
8948# endif
8949 if (!mouse_has(MOUSE_INSERT))
8950 return;
8951
8952 undisplay_dollar();
8953 tpos = curwin->w_cursor;
8954 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8955 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008956#ifdef FEAT_WINDOWS
8957 win_T *new_curwin = curwin;
8958
8959 if (curwin != old_curwin && win_valid(old_curwin))
8960 {
8961 /* Mouse took us to another window. We need to go back to the
8962 * previous one to stop insert there properly. */
8963 curwin = old_curwin;
8964 curbuf = curwin->w_buffer;
8965 }
8966#endif
8967 start_arrow(curwin == old_curwin ? &tpos : NULL);
8968#ifdef FEAT_WINDOWS
8969 if (curwin != new_curwin && win_valid(new_curwin))
8970 {
8971 curwin = new_curwin;
8972 curbuf = curwin->w_buffer;
8973 }
8974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975# ifdef FEAT_CINDENT
8976 can_cindent = TRUE;
8977# endif
8978 }
8979
8980#ifdef FEAT_WINDOWS
8981 /* redraw status lines (in case another window became active) */
8982 redraw_statuslines();
8983#endif
8984}
8985
8986 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008987ins_mousescroll(dir)
8988 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008991# if defined(FEAT_WINDOWS)
8992 win_T *old_curwin = curwin;
8993# endif
8994# ifdef FEAT_INS_EXPAND
8995 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996# endif
8997
8998 tpos = curwin->w_cursor;
8999
9000# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 /* Currently the mouse coordinates are only known in the GUI. */
9002 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
9003 {
9004 int row, col;
9005
9006 row = mouse_row;
9007 col = mouse_col;
9008
9009 /* find the window at the pointer coordinates */
9010 curwin = mouse_find_win(&row, &col);
9011 curbuf = curwin->w_buffer;
9012 }
9013 if (curwin == old_curwin)
9014# endif
9015 undisplay_dollar();
9016
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009017# ifdef FEAT_INS_EXPAND
9018 /* Don't scroll the window in which completion is being done. */
9019 if (!pum_visible()
9020# if defined(FEAT_WINDOWS)
9021 || curwin != old_curwin
9022# endif
9023 )
9024# endif
9025 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009026 if (dir == MSCR_DOWN || dir == MSCR_UP)
9027 {
9028 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9029 scroll_redraw(dir,
9030 (long)(curwin->w_botline - curwin->w_topline));
9031 else
9032 scroll_redraw(dir, 3L);
9033 }
9034#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009035 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009036 {
9037 int val, step = 6;
9038
9039 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9040 step = W_WIDTH(curwin);
9041 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9042 if (val < 0)
9043 val = 0;
9044 gui_do_horiz_scroll(val, TRUE);
9045 }
9046#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009047# ifdef FEAT_INS_EXPAND
9048 did_scroll = TRUE;
9049# endif
9050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
9053 curwin->w_redr_status = TRUE;
9054
9055 curwin = old_curwin;
9056 curbuf = curwin->w_buffer;
9057# endif
9058
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009059# ifdef FEAT_INS_EXPAND
9060 /* The popup menu may overlay the window, need to redraw it.
9061 * TODO: Would be more efficient to only redraw the windows that are
9062 * overlapped by the popup menu. */
9063 if (pum_visible() && did_scroll)
9064 {
9065 redraw_all_later(NOT_VALID);
9066 ins_compl_show_pum();
9067 }
9068# endif
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 if (!equalpos(curwin->w_cursor, tpos))
9071 {
9072 start_arrow(&tpos);
9073# ifdef FEAT_CINDENT
9074 can_cindent = TRUE;
9075# endif
9076 }
9077}
9078#endif
9079
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009080#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009081 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009082ins_tabline(c)
9083 int c;
9084{
9085 /* We will be leaving the current window, unless closing another tab. */
9086 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9087 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9088 {
9089 undisplay_dollar();
9090 start_arrow(&curwin->w_cursor);
9091# ifdef FEAT_CINDENT
9092 can_cindent = TRUE;
9093# endif
9094 }
9095
9096 if (c == K_TABLINE)
9097 goto_tabpage(current_tab);
9098 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009099 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009100 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009101 redraw_statuslines(); /* will redraw the tabline when needed */
9102 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009103}
9104#endif
9105
9106#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 void
9108ins_scroll()
9109{
9110 pos_T tpos;
9111
9112 undisplay_dollar();
9113 tpos = curwin->w_cursor;
9114 if (gui_do_scroll())
9115 {
9116 start_arrow(&tpos);
9117# ifdef FEAT_CINDENT
9118 can_cindent = TRUE;
9119# endif
9120 }
9121}
9122
9123 void
9124ins_horscroll()
9125{
9126 pos_T tpos;
9127
9128 undisplay_dollar();
9129 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009130 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 {
9132 start_arrow(&tpos);
9133# ifdef FEAT_CINDENT
9134 can_cindent = TRUE;
9135# endif
9136 }
9137}
9138#endif
9139
9140 static void
9141ins_left()
9142{
9143 pos_T tpos;
9144
9145#ifdef FEAT_FOLDING
9146 if ((fdo_flags & FDO_HOR) && KeyTyped)
9147 foldOpenCursor();
9148#endif
9149 undisplay_dollar();
9150 tpos = curwin->w_cursor;
9151 if (oneleft() == OK)
9152 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009153#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9154 /* Only call start_arrow() when not busy with preediting, it will
9155 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9156 if (!im_is_preediting())
9157#endif
9158 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#ifdef FEAT_RIGHTLEFT
9160 /* If exit reversed string, position is fixed */
9161 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9162 revins_legal++;
9163 revins_chars++;
9164#endif
9165 }
9166
9167 /*
9168 * if 'whichwrap' set for cursor in insert mode may go to
9169 * previous line
9170 */
9171 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9172 {
9173 start_arrow(&tpos);
9174 --(curwin->w_cursor.lnum);
9175 coladvance((colnr_T)MAXCOL);
9176 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9177 }
9178 else
9179 vim_beep();
9180}
9181
9182 static void
9183ins_home(c)
9184 int c;
9185{
9186 pos_T tpos;
9187
9188#ifdef FEAT_FOLDING
9189 if ((fdo_flags & FDO_HOR) && KeyTyped)
9190 foldOpenCursor();
9191#endif
9192 undisplay_dollar();
9193 tpos = curwin->w_cursor;
9194 if (c == K_C_HOME)
9195 curwin->w_cursor.lnum = 1;
9196 curwin->w_cursor.col = 0;
9197#ifdef FEAT_VIRTUALEDIT
9198 curwin->w_cursor.coladd = 0;
9199#endif
9200 curwin->w_curswant = 0;
9201 start_arrow(&tpos);
9202}
9203
9204 static void
9205ins_end(c)
9206 int c;
9207{
9208 pos_T tpos;
9209
9210#ifdef FEAT_FOLDING
9211 if ((fdo_flags & FDO_HOR) && KeyTyped)
9212 foldOpenCursor();
9213#endif
9214 undisplay_dollar();
9215 tpos = curwin->w_cursor;
9216 if (c == K_C_END)
9217 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9218 coladvance((colnr_T)MAXCOL);
9219 curwin->w_curswant = MAXCOL;
9220
9221 start_arrow(&tpos);
9222}
9223
9224 static void
9225ins_s_left()
9226{
9227#ifdef FEAT_FOLDING
9228 if ((fdo_flags & FDO_HOR) && KeyTyped)
9229 foldOpenCursor();
9230#endif
9231 undisplay_dollar();
9232 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9233 {
9234 start_arrow(&curwin->w_cursor);
9235 (void)bck_word(1L, FALSE, FALSE);
9236 curwin->w_set_curswant = TRUE;
9237 }
9238 else
9239 vim_beep();
9240}
9241
9242 static void
9243ins_right()
9244{
9245#ifdef FEAT_FOLDING
9246 if ((fdo_flags & FDO_HOR) && KeyTyped)
9247 foldOpenCursor();
9248#endif
9249 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009250 if (gchar_cursor() != NUL
9251#ifdef FEAT_VIRTUALEDIT
9252 || virtual_active()
9253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 )
9255 {
9256 start_arrow(&curwin->w_cursor);
9257 curwin->w_set_curswant = TRUE;
9258#ifdef FEAT_VIRTUALEDIT
9259 if (virtual_active())
9260 oneright();
9261 else
9262#endif
9263 {
9264#ifdef FEAT_MBYTE
9265 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009266 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 else
9268#endif
9269 ++curwin->w_cursor.col;
9270 }
9271
9272#ifdef FEAT_RIGHTLEFT
9273 revins_legal++;
9274 if (revins_chars)
9275 revins_chars--;
9276#endif
9277 }
9278 /* if 'whichwrap' set for cursor in insert mode, may move the
9279 * cursor to the next line */
9280 else if (vim_strchr(p_ww, ']') != NULL
9281 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9282 {
9283 start_arrow(&curwin->w_cursor);
9284 curwin->w_set_curswant = TRUE;
9285 ++curwin->w_cursor.lnum;
9286 curwin->w_cursor.col = 0;
9287 }
9288 else
9289 vim_beep();
9290}
9291
9292 static void
9293ins_s_right()
9294{
9295#ifdef FEAT_FOLDING
9296 if ((fdo_flags & FDO_HOR) && KeyTyped)
9297 foldOpenCursor();
9298#endif
9299 undisplay_dollar();
9300 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9301 || gchar_cursor() != NUL)
9302 {
9303 start_arrow(&curwin->w_cursor);
9304 (void)fwd_word(1L, FALSE, 0);
9305 curwin->w_set_curswant = TRUE;
9306 }
9307 else
9308 vim_beep();
9309}
9310
9311 static void
9312ins_up(startcol)
9313 int startcol; /* when TRUE move to Insstart.col */
9314{
9315 pos_T tpos;
9316 linenr_T old_topline = curwin->w_topline;
9317#ifdef FEAT_DIFF
9318 int old_topfill = curwin->w_topfill;
9319#endif
9320
9321 undisplay_dollar();
9322 tpos = curwin->w_cursor;
9323 if (cursor_up(1L, TRUE) == OK)
9324 {
9325 if (startcol)
9326 coladvance(getvcol_nolist(&Insstart));
9327 if (old_topline != curwin->w_topline
9328#ifdef FEAT_DIFF
9329 || old_topfill != curwin->w_topfill
9330#endif
9331 )
9332 redraw_later(VALID);
9333 start_arrow(&tpos);
9334#ifdef FEAT_CINDENT
9335 can_cindent = TRUE;
9336#endif
9337 }
9338 else
9339 vim_beep();
9340}
9341
9342 static void
9343ins_pageup()
9344{
9345 pos_T tpos;
9346
9347 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009348
9349#ifdef FEAT_WINDOWS
9350 if (mod_mask & MOD_MASK_CTRL)
9351 {
9352 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009353 if (first_tabpage->tp_next != NULL)
9354 {
9355 start_arrow(&curwin->w_cursor);
9356 goto_tabpage(-1);
9357 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009358 return;
9359 }
9360#endif
9361
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 tpos = curwin->w_cursor;
9363 if (onepage(BACKWARD, 1L) == OK)
9364 {
9365 start_arrow(&tpos);
9366#ifdef FEAT_CINDENT
9367 can_cindent = TRUE;
9368#endif
9369 }
9370 else
9371 vim_beep();
9372}
9373
9374 static void
9375ins_down(startcol)
9376 int startcol; /* when TRUE move to Insstart.col */
9377{
9378 pos_T tpos;
9379 linenr_T old_topline = curwin->w_topline;
9380#ifdef FEAT_DIFF
9381 int old_topfill = curwin->w_topfill;
9382#endif
9383
9384 undisplay_dollar();
9385 tpos = curwin->w_cursor;
9386 if (cursor_down(1L, TRUE) == OK)
9387 {
9388 if (startcol)
9389 coladvance(getvcol_nolist(&Insstart));
9390 if (old_topline != curwin->w_topline
9391#ifdef FEAT_DIFF
9392 || old_topfill != curwin->w_topfill
9393#endif
9394 )
9395 redraw_later(VALID);
9396 start_arrow(&tpos);
9397#ifdef FEAT_CINDENT
9398 can_cindent = TRUE;
9399#endif
9400 }
9401 else
9402 vim_beep();
9403}
9404
9405 static void
9406ins_pagedown()
9407{
9408 pos_T tpos;
9409
9410 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009411
9412#ifdef FEAT_WINDOWS
9413 if (mod_mask & MOD_MASK_CTRL)
9414 {
9415 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009416 if (first_tabpage->tp_next != NULL)
9417 {
9418 start_arrow(&curwin->w_cursor);
9419 goto_tabpage(0);
9420 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009421 return;
9422 }
9423#endif
9424
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 tpos = curwin->w_cursor;
9426 if (onepage(FORWARD, 1L) == OK)
9427 {
9428 start_arrow(&tpos);
9429#ifdef FEAT_CINDENT
9430 can_cindent = TRUE;
9431#endif
9432 }
9433 else
9434 vim_beep();
9435}
9436
9437#ifdef FEAT_DND
9438 static void
9439ins_drop()
9440{
9441 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9442}
9443#endif
9444
9445/*
9446 * Handle TAB in Insert or Replace mode.
9447 * Return TRUE when the TAB needs to be inserted like a normal character.
9448 */
9449 static int
9450ins_tab()
9451{
9452 int ind;
9453 int i;
9454 int temp;
9455
9456 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9457 Insstart_blank_vcol = get_nolist_virtcol();
9458 if (echeck_abbr(TAB + ABBR_OFF))
9459 return FALSE;
9460
9461 ind = inindent(0);
9462#ifdef FEAT_CINDENT
9463 if (ind)
9464 can_cindent = FALSE;
9465#endif
9466
9467 /*
9468 * When nothing special, insert TAB like a normal character
9469 */
9470 if (!curbuf->b_p_et
9471 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9472 && curbuf->b_p_sts == 0)
9473 return TRUE;
9474
9475 if (stop_arrow() == FAIL)
9476 return TRUE;
9477
9478 did_ai = FALSE;
9479#ifdef FEAT_SMARTINDENT
9480 did_si = FALSE;
9481 can_si = FALSE;
9482 can_si_back = FALSE;
9483#endif
9484 AppendToRedobuff((char_u *)"\t");
9485
9486 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9487 temp = (int)curbuf->b_p_sw;
9488 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9489 temp = (int)curbuf->b_p_sts;
9490 else /* otherwise use 'tabstop' */
9491 temp = (int)curbuf->b_p_ts;
9492 temp -= get_nolist_virtcol() % temp;
9493
9494 /*
9495 * Insert the first space with ins_char(). It will delete one char in
9496 * replace mode. Insert the rest with ins_str(); it will not delete any
9497 * chars. For VREPLACE mode, we use ins_char() for all characters.
9498 */
9499 ins_char(' ');
9500 while (--temp > 0)
9501 {
9502#ifdef FEAT_VREPLACE
9503 if (State & VREPLACE_FLAG)
9504 ins_char(' ');
9505 else
9506#endif
9507 {
9508 ins_str((char_u *)" ");
9509 if (State & REPLACE_FLAG) /* no char replaced */
9510 replace_push(NUL);
9511 }
9512 }
9513
9514 /*
9515 * When 'expandtab' not set: Replace spaces by TABs where possible.
9516 */
9517 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9518 {
9519 char_u *ptr;
9520#ifdef FEAT_VREPLACE
9521 char_u *saved_line = NULL; /* init for GCC */
9522 pos_T pos;
9523#endif
9524 pos_T fpos;
9525 pos_T *cursor;
9526 colnr_T want_vcol, vcol;
9527 int change_col = -1;
9528 int save_list = curwin->w_p_list;
9529
9530 /*
9531 * Get the current line. For VREPLACE mode, don't make real changes
9532 * yet, just work on a copy of the line.
9533 */
9534#ifdef FEAT_VREPLACE
9535 if (State & VREPLACE_FLAG)
9536 {
9537 pos = curwin->w_cursor;
9538 cursor = &pos;
9539 saved_line = vim_strsave(ml_get_curline());
9540 if (saved_line == NULL)
9541 return FALSE;
9542 ptr = saved_line + pos.col;
9543 }
9544 else
9545#endif
9546 {
9547 ptr = ml_get_cursor();
9548 cursor = &curwin->w_cursor;
9549 }
9550
9551 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9552 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9553 curwin->w_p_list = FALSE;
9554
9555 /* Find first white before the cursor */
9556 fpos = curwin->w_cursor;
9557 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9558 {
9559 --fpos.col;
9560 --ptr;
9561 }
9562
9563 /* In Replace mode, don't change characters before the insert point. */
9564 if ((State & REPLACE_FLAG)
9565 && fpos.lnum == Insstart.lnum
9566 && fpos.col < Insstart.col)
9567 {
9568 ptr += Insstart.col - fpos.col;
9569 fpos.col = Insstart.col;
9570 }
9571
9572 /* compute virtual column numbers of first white and cursor */
9573 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9574 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9575
9576 /* Use as many TABs as possible. Beware of 'showbreak' and
9577 * 'linebreak' adding extra virtual columns. */
9578 while (vim_iswhite(*ptr))
9579 {
9580 i = lbr_chartabsize((char_u *)"\t", vcol);
9581 if (vcol + i > want_vcol)
9582 break;
9583 if (*ptr != TAB)
9584 {
9585 *ptr = TAB;
9586 if (change_col < 0)
9587 {
9588 change_col = fpos.col; /* Column of first change */
9589 /* May have to adjust Insstart */
9590 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9591 Insstart.col = fpos.col;
9592 }
9593 }
9594 ++fpos.col;
9595 ++ptr;
9596 vcol += i;
9597 }
9598
9599 if (change_col >= 0)
9600 {
9601 int repl_off = 0;
9602
9603 /* Skip over the spaces we need. */
9604 while (vcol < want_vcol && *ptr == ' ')
9605 {
9606 vcol += lbr_chartabsize(ptr, vcol);
9607 ++ptr;
9608 ++repl_off;
9609 }
9610 if (vcol > want_vcol)
9611 {
9612 /* Must have a char with 'showbreak' just before it. */
9613 --ptr;
9614 --repl_off;
9615 }
9616 fpos.col += repl_off;
9617
9618 /* Delete following spaces. */
9619 i = cursor->col - fpos.col;
9620 if (i > 0)
9621 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009622 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 /* correct replace stack. */
9624 if ((State & REPLACE_FLAG)
9625#ifdef FEAT_VREPLACE
9626 && !(State & VREPLACE_FLAG)
9627#endif
9628 )
9629 for (temp = i; --temp >= 0; )
9630 replace_join(repl_off);
9631 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009632#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009633 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009634 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009635 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009636 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9637 (char_u *)"\t", 1);
9638 }
9639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 cursor->col -= i;
9641
9642#ifdef FEAT_VREPLACE
9643 /*
9644 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9645 * backspacing over the changed spacing and then inserting the new
9646 * spacing.
9647 */
9648 if (State & VREPLACE_FLAG)
9649 {
9650 /* Backspace from real cursor to change_col */
9651 backspace_until_column(change_col);
9652
9653 /* Insert each char in saved_line from changed_col to
9654 * ptr-cursor */
9655 ins_bytes_len(saved_line + change_col,
9656 cursor->col - change_col);
9657 }
9658#endif
9659 }
9660
9661#ifdef FEAT_VREPLACE
9662 if (State & VREPLACE_FLAG)
9663 vim_free(saved_line);
9664#endif
9665 curwin->w_p_list = save_list;
9666 }
9667
9668 return FALSE;
9669}
9670
9671/*
9672 * Handle CR or NL in insert mode.
9673 * Return TRUE when out of memory or can't undo.
9674 */
9675 static int
9676ins_eol(c)
9677 int c;
9678{
9679 int i;
9680
9681 if (echeck_abbr(c + ABBR_OFF))
9682 return FALSE;
9683 if (stop_arrow() == FAIL)
9684 return TRUE;
9685 undisplay_dollar();
9686
9687 /*
9688 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9689 * character under the cursor. Only push a NUL on the replace stack,
9690 * nothing to put back when the NL is deleted.
9691 */
9692 if ((State & REPLACE_FLAG)
9693#ifdef FEAT_VREPLACE
9694 && !(State & VREPLACE_FLAG)
9695#endif
9696 )
9697 replace_push(NUL);
9698
9699 /*
9700 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9701 * replacing the next line, so we push all of the characters left on the
9702 * line onto the replace stack. This is not done here though, it is done
9703 * in open_line().
9704 */
9705
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009706#ifdef FEAT_VIRTUALEDIT
9707 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9708 * CTRL-O). */
9709 if (virtual_active() && curwin->w_cursor.coladd > 0)
9710 coladvance(getviscol());
9711#endif
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713#ifdef FEAT_RIGHTLEFT
9714# ifdef FEAT_FKMAP
9715 if (p_altkeymap && p_fkmap)
9716 fkmap(NL);
9717# endif
9718 /* NL in reverse insert will always start in the end of
9719 * current line. */
9720 if (revins_on)
9721 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9722#endif
9723
9724 AppendToRedobuff(NL_STR);
9725 i = open_line(FORWARD,
9726#ifdef FEAT_COMMENTS
9727 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9728#endif
9729 0, old_indent);
9730 old_indent = 0;
9731#ifdef FEAT_CINDENT
9732 can_cindent = TRUE;
9733#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009734#ifdef FEAT_FOLDING
9735 /* When inserting a line the cursor line must never be in a closed fold. */
9736 foldOpenCursor();
9737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738
9739 return (!i);
9740}
9741
9742#ifdef FEAT_DIGRAPHS
9743/*
9744 * Handle digraph in insert mode.
9745 * Returns character still to be inserted, or NUL when nothing remaining to be
9746 * done.
9747 */
9748 static int
9749ins_digraph()
9750{
9751 int c;
9752 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009753 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754
9755 pc_status = PC_STATUS_UNSET;
9756 if (redrawing() && !char_avail())
9757 {
9758 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009759 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760
9761 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009762 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#ifdef FEAT_CMDL_INFO
9764 add_to_showcmd_c(Ctrl_K);
9765#endif
9766 }
9767
9768#ifdef USE_ON_FLY_SCROLL
9769 dont_scroll = TRUE; /* disallow scrolling here */
9770#endif
9771
9772 /* don't map the digraph chars. This also prevents the
9773 * mode message to be deleted when ESC is hit */
9774 ++no_mapping;
9775 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009776 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 --no_mapping;
9778 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009779 if (did_putchar)
9780 /* when the line fits in 'columns' the '?' is at the start of the next
9781 * line and will not be removed by the redraw */
9782 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009783
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 if (IS_SPECIAL(c) || mod_mask) /* special key */
9785 {
9786#ifdef FEAT_CMDL_INFO
9787 clear_showcmd();
9788#endif
9789 insert_special(c, TRUE, FALSE);
9790 return NUL;
9791 }
9792 if (c != ESC)
9793 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009794 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 if (redrawing() && !char_avail())
9796 {
9797 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009798 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799
9800 if (char2cells(c) == 1)
9801 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009802 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009804 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 }
9806#ifdef FEAT_CMDL_INFO
9807 add_to_showcmd_c(c);
9808#endif
9809 }
9810 ++no_mapping;
9811 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009812 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 --no_mapping;
9814 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009815 if (did_putchar)
9816 /* when the line fits in 'columns' the '?' is at the start of the
9817 * next line and will not be removed by a redraw */
9818 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 if (cc != ESC)
9820 {
9821 AppendToRedobuff((char_u *)CTRL_V_STR);
9822 c = getdigraph(c, cc, TRUE);
9823#ifdef FEAT_CMDL_INFO
9824 clear_showcmd();
9825#endif
9826 return c;
9827 }
9828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829#ifdef FEAT_CMDL_INFO
9830 clear_showcmd();
9831#endif
9832 return NUL;
9833}
9834#endif
9835
9836/*
9837 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9838 * Returns the char to be inserted, or NUL if none found.
9839 */
9840 static int
9841ins_copychar(lnum)
9842 linenr_T lnum;
9843{
9844 int c;
9845 int temp;
9846 char_u *ptr, *prev_ptr;
9847
9848 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9849 {
9850 vim_beep();
9851 return NUL;
9852 }
9853
9854 /* try to advance to the cursor column */
9855 temp = 0;
9856 ptr = ml_get(lnum);
9857 prev_ptr = ptr;
9858 validate_virtcol();
9859 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9860 {
9861 prev_ptr = ptr;
9862 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9863 }
9864 if ((colnr_T)temp > curwin->w_virtcol)
9865 ptr = prev_ptr;
9866
9867#ifdef FEAT_MBYTE
9868 c = (*mb_ptr2char)(ptr);
9869#else
9870 c = *ptr;
9871#endif
9872 if (c == NUL)
9873 vim_beep();
9874 return c;
9875}
9876
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009877/*
9878 * CTRL-Y or CTRL-E typed in Insert mode.
9879 */
9880 static int
9881ins_ctrl_ey(tc)
9882 int tc;
9883{
9884 int c = tc;
9885
9886#ifdef FEAT_INS_EXPAND
9887 if (ctrl_x_mode == CTRL_X_SCROLL)
9888 {
9889 if (c == Ctrl_Y)
9890 scrolldown_clamp();
9891 else
9892 scrollup_clamp();
9893 redraw_later(VALID);
9894 }
9895 else
9896#endif
9897 {
9898 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9899 if (c != NUL)
9900 {
9901 long tw_save;
9902
9903 /* The character must be taken literally, insert like it
9904 * was typed after a CTRL-V, and pretend 'textwidth'
9905 * wasn't set. Digits, 'o' and 'x' are special after a
9906 * CTRL-V, don't use it for these. */
9907 if (c < 256 && !isalnum(c))
9908 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9909 tw_save = curbuf->b_p_tw;
9910 curbuf->b_p_tw = -1;
9911 insert_special(c, TRUE, FALSE);
9912 curbuf->b_p_tw = tw_save;
9913#ifdef FEAT_RIGHTLEFT
9914 revins_chars++;
9915 revins_legal++;
9916#endif
9917 c = Ctrl_V; /* pretend CTRL-V is last character */
9918 auto_format(FALSE, TRUE);
9919 }
9920 }
9921 return c;
9922}
9923
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#ifdef FEAT_SMARTINDENT
9925/*
9926 * Try to do some very smart auto-indenting.
9927 * Used when inserting a "normal" character.
9928 */
9929 static void
9930ins_try_si(c)
9931 int c;
9932{
9933 pos_T *pos, old_pos;
9934 char_u *ptr;
9935 int i;
9936 int temp;
9937
9938 /*
9939 * do some very smart indenting when entering '{' or '}'
9940 */
9941 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9942 {
9943 /*
9944 * for '}' set indent equal to indent of line containing matching '{'
9945 */
9946 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9947 {
9948 old_pos = curwin->w_cursor;
9949 /*
9950 * If the matching '{' has a ')' immediately before it (ignoring
9951 * white-space), then line up with the start of the line
9952 * containing the matching '(' if there is one. This handles the
9953 * case where an "if (..\n..) {" statement continues over multiple
9954 * lines -- webb
9955 */
9956 ptr = ml_get(pos->lnum);
9957 i = pos->col;
9958 if (i > 0) /* skip blanks before '{' */
9959 while (--i > 0 && vim_iswhite(ptr[i]))
9960 ;
9961 curwin->w_cursor.lnum = pos->lnum;
9962 curwin->w_cursor.col = i;
9963 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9964 curwin->w_cursor = *pos;
9965 i = get_indent();
9966 curwin->w_cursor = old_pos;
9967#ifdef FEAT_VREPLACE
9968 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009969 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 else
9971#endif
9972 (void)set_indent(i, SIN_CHANGED);
9973 }
9974 else if (curwin->w_cursor.col > 0)
9975 {
9976 /*
9977 * when inserting '{' after "O" reduce indent, but not
9978 * more than indent of previous line
9979 */
9980 temp = TRUE;
9981 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9982 {
9983 old_pos = curwin->w_cursor;
9984 i = get_indent();
9985 while (curwin->w_cursor.lnum > 1)
9986 {
9987 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9988
9989 /* ignore empty lines and lines starting with '#'. */
9990 if (*ptr != '#' && *ptr != NUL)
9991 break;
9992 }
9993 if (get_indent() >= i)
9994 temp = FALSE;
9995 curwin->w_cursor = old_pos;
9996 }
9997 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009998 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 }
10000 }
10001
10002 /*
10003 * set indent of '#' always to 0
10004 */
10005 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10006 {
10007 /* remember current indent for next line */
10008 old_indent = get_indent();
10009 (void)set_indent(0, SIN_CHANGED);
10010 }
10011
10012 /* Adjust ai_col, the char at this position can be deleted. */
10013 if (ai_col > curwin->w_cursor.col)
10014 ai_col = curwin->w_cursor.col;
10015}
10016#endif
10017
10018/*
10019 * Get the value that w_virtcol would have when 'list' is off.
10020 * Unless 'cpo' contains the 'L' flag.
10021 */
10022 static colnr_T
10023get_nolist_virtcol()
10024{
10025 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10026 return getvcol_nolist(&curwin->w_cursor);
10027 validate_virtcol();
10028 return curwin->w_virtcol;
10029}