blob: 2aec22608025eec8ba13f146d2fdf057514f9afb [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 Moolenaar071d4272004-06-13 20:20:40 +000061
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000072#ifdef S_SPLINT_S /* splint can't handle array of pointers */
73 char_u **cp_text; /* text for the menu */
74#else
Bram Moolenaar39f05632006-03-19 22:15:26 +000075 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000076#endif
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 Moolenaar572cb562005-08-05 21:35:02 +0000121static int compl_matches = 0;
122static char_u *compl_pattern = NULL;
123static int compl_direction = FORWARD;
124static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000125static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000126static pos_T compl_startpos;
127static colnr_T compl_col = 0; /* column where the text starts
128 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000129static char_u *compl_orig_text = NULL; /* text as it was before
130 * completion started */
131static int compl_cont_mode = 0;
132static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000134static void ins_ctrl_x __ARGS((void));
135static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000136static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000137static 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 +0000138static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000139static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000140static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000142static void ins_compl_upd_pum __ARGS((void));
143static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000144static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000145static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000146static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000147static 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 +0000148static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149static void ins_compl_free __ARGS((void));
150static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000151static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000152static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000153static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000154static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000155static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000156static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000158static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000160#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
161static void ins_compl_add_list __ARGS((list_T *list));
162#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000163static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void ins_compl_delete __ARGS((void));
165static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000167static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000168static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000169static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000170static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000172static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif /* FEAT_INS_EXPAND */
174
175#define BACKSPACE_CHAR 1
176#define BACKSPACE_WORD 2
177#define BACKSPACE_WORD_NOT_SPACE 3
178#define BACKSPACE_LINE 4
179
Bram Moolenaar754b5602006-02-09 23:53:20 +0000180static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static void ins_ctrl_v __ARGS((void));
182static void undisplay_dollar __ARGS((void));
183static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000184static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static void check_auto_format __ARGS((int));
186static void redo_literal __ARGS((int c));
187static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000188#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000189static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000190static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000191static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
194static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000195#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198static int replace_pop __ARGS((void));
199static void replace_join __ARGS((int off));
200static void replace_pop_ins __ARGS((void));
201#ifdef FEAT_MBYTE
202static void mb_replace_pop_ins __ARGS((int cc));
203#endif
204static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000205static void replace_do_bs __ARGS((int limit_col));
206static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_CINDENT
208static int cindent_on __ARGS((void));
209#endif
210static void ins_reg __ARGS((void));
211static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000212static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000213static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_RIGHTLEFT
215static void ins_ctrl_ __ARGS((void));
216#endif
217#ifdef FEAT_VISUAL
218static int ins_start_select __ARGS((int c));
219#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000220static void ins_insert __ARGS((int replaceState));
221static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ins_shift __ARGS((int c, int lastc));
223static void ins_del __ARGS((void));
224static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
225#ifdef FEAT_MOUSE
226static void ins_mouse __ARGS((int c));
227static void ins_mousescroll __ARGS((int up));
228#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000229#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
230static void ins_tabline __ARGS((int c));
231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232static void ins_left __ARGS((void));
233static void ins_home __ARGS((int c));
234static void ins_end __ARGS((int c));
235static void ins_s_left __ARGS((void));
236static void ins_right __ARGS((void));
237static void ins_s_right __ARGS((void));
238static void ins_up __ARGS((int startcol));
239static void ins_pageup __ARGS((void));
240static void ins_down __ARGS((int startcol));
241static void ins_pagedown __ARGS((void));
242#ifdef FEAT_DND
243static void ins_drop __ARGS((void));
244#endif
245static int ins_tab __ARGS((void));
246static int ins_eol __ARGS((int c));
247#ifdef FEAT_DIGRAPHS
248static int ins_digraph __ARGS((void));
249#endif
250static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000251static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_SMARTINDENT
253static void ins_try_si __ARGS((int c));
254#endif
255static colnr_T get_nolist_virtcol __ARGS((void));
256
257static colnr_T Insstart_textlen; /* length of line when insert started */
258static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
259
260static char_u *last_insert = NULL; /* the text of the previous insert,
261 K_SPECIAL and CSI are escaped */
262static int last_insert_skip; /* nr of chars in front of previous insert */
263static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000264static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266#ifdef FEAT_CINDENT
267static int can_cindent; /* may do cindenting on this line */
268#endif
269
270static int old_indent = 0; /* for ^^D command in insert mode */
271
272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000273static int revins_on; /* reverse insert mode on */
274static int revins_chars; /* how much to skip after edit */
275static int revins_legal; /* was the last char 'legal'? */
276static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static int ins_need_undo; /* call u_save() before inserting a
280 char. Set when edit() is called.
281 after that arrow_used is used. */
282
283static int did_add_space = FALSE; /* auto_format() added an extra space
284 under the cursor */
285
286/*
287 * edit(): Start inserting text.
288 *
289 * "cmdchar" can be:
290 * 'i' normal insert command
291 * 'a' normal append command
292 * 'R' replace command
293 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
294 * but still only one <CR> is inserted. The <Esc> is not used for redo.
295 * 'g' "gI" command.
296 * 'V' "gR" command for Virtual Replace mode.
297 * 'v' "gr" command for single character Virtual Replace mode.
298 *
299 * This function is not called recursively. For CTRL-O commands, it returns
300 * and lets the caller handle the Normal-mode command.
301 *
302 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
303 */
304 int
305edit(cmdchar, startln, count)
306 int cmdchar;
307 int startln; /* if set, insert at start of line */
308 long count;
309{
310 int c = 0;
311 char_u *ptr;
312 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000313 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 int i;
316 int did_backspace = TRUE; /* previous char was backspace */
317#ifdef FEAT_CINDENT
318 int line_is_white = FALSE; /* line is empty before insert */
319#endif
320 linenr_T old_topline = 0; /* topline before insertion */
321#ifdef FEAT_DIFF
322 int old_topfill = -1;
323#endif
324 int inserted_space = FALSE; /* just inserted a space */
325 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000326 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000328 /* Remember whether editing was restarted after CTRL-O. */
329 did_restart_edit = restart_edit;
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /* sleep before redrawing, needed for "CTRL-O :" that results in an
332 * error message */
333 check_for_delay(TRUE);
334
335#ifdef HAVE_SANDBOX
336 /* Don't allow inserting in the sandbox. */
337 if (sandbox != 0)
338 {
339 EMSG(_(e_sandbox));
340 return FALSE;
341 }
342#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000343 /* Don't allow changes in the buffer while editing the cmdline. The
344 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000345 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000346 {
347 EMSG(_(e_secure));
348 return FALSE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000352 /* Don't allow recursive insert mode when busy with completion. */
353 if (compl_started || pum_visible())
354 {
355 EMSG(_(e_secure));
356 return FALSE;
357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 ins_compl_clear(); /* clear stuff for CTRL-X mode */
359#endif
360
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361#ifdef FEAT_AUTOCMD
362 /*
363 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
364 */
365 if (cmdchar != 'r' && cmdchar != 'v')
366 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000367# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000368 if (cmdchar == 'R')
369 ptr = (char_u *)"r";
370 else if (cmdchar == 'V')
371 ptr = (char_u *)"v";
372 else
373 ptr = (char_u *)"i";
374 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000375# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000376 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
377 }
378#endif
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#ifdef FEAT_MOUSE
381 /*
382 * When doing a paste with the middle mouse button, Insstart is set to
383 * where the paste started.
384 */
385 if (where_paste_started.lnum != 0)
386 Insstart = where_paste_started;
387 else
388#endif
389 {
390 Insstart = curwin->w_cursor;
391 if (startln)
392 Insstart.col = 0;
393 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000394 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 Insstart_blank_vcol = MAXCOL;
396 if (!did_ai)
397 ai_col = 0;
398
399 if (cmdchar != NUL && restart_edit == 0)
400 {
401 ResetRedobuff();
402 AppendNumberToRedobuff(count);
403#ifdef FEAT_VREPLACE
404 if (cmdchar == 'V' || cmdchar == 'v')
405 {
406 /* "gR" or "gr" command */
407 AppendCharToRedobuff('g');
408 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
409 }
410 else
411#endif
412 {
413 AppendCharToRedobuff(cmdchar);
414 if (cmdchar == 'g') /* "gI" command */
415 AppendCharToRedobuff('I');
416 else if (cmdchar == 'r') /* "r<CR>" command */
417 count = 1; /* insert only one <CR> */
418 }
419 }
420
421 if (cmdchar == 'R')
422 {
423#ifdef FEAT_FKMAP
424 if (p_fkmap && p_ri)
425 {
426 beep_flush();
427 EMSG(farsi_text_3); /* encoded in Farsi */
428 State = INSERT;
429 }
430 else
431#endif
432 State = REPLACE;
433 }
434#ifdef FEAT_VREPLACE
435 else if (cmdchar == 'V' || cmdchar == 'v')
436 {
437 State = VREPLACE;
438 replaceState = VREPLACE;
439 orig_line_count = curbuf->b_ml.ml_line_count;
440 vr_lines_changed = 1;
441 }
442#endif
443 else
444 State = INSERT;
445
446 stop_insert_mode = FALSE;
447
448 /*
449 * Need to recompute the cursor position, it might move when the cursor is
450 * on a TAB or special character.
451 */
452 curs_columns(TRUE);
453
454 /*
455 * Enable langmap or IME, indicated by 'iminsert'.
456 * Note that IME may enabled/disabled without us noticing here, thus the
457 * 'iminsert' value may not reflect what is actually used. It is updated
458 * when hitting <Esc>.
459 */
460 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
461 State |= LANGMAP;
462#ifdef USE_IM_CONTROL
463 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#ifdef FEAT_MOUSE
467 setmouse();
468#endif
469#ifdef FEAT_CMDL_INFO
470 clear_showcmd();
471#endif
472#ifdef FEAT_RIGHTLEFT
473 /* there is no reverse replace mode */
474 revins_on = (State == INSERT && p_ri);
475 if (revins_on)
476 undisplay_dollar();
477 revins_chars = 0;
478 revins_legal = 0;
479 revins_scol = -1;
480#endif
481
482 /*
483 * Handle restarting Insert mode.
484 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
485 * restart_edit non-zero, and something in the stuff buffer.
486 */
487 if (restart_edit != 0 && stuff_empty())
488 {
489#ifdef FEAT_MOUSE
490 /*
491 * After a paste we consider text typed to be part of the insert for
492 * the pasted text. You can backspace over the pasted text too.
493 */
494 if (where_paste_started.lnum)
495 arrow_used = FALSE;
496 else
497#endif
498 arrow_used = TRUE;
499 restart_edit = 0;
500
501 /*
502 * If the cursor was after the end-of-line before the CTRL-O and it is
503 * now at the end-of-line, put it after the end-of-line (this is not
504 * correct in very rare cases).
505 * Also do this if curswant is greater than the current virtual
506 * column. Eg after "^O$" or "^O80|".
507 */
508 validate_virtcol();
509 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000510 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 || curwin->w_curswant > curwin->w_virtcol)
512 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
513 {
514 if (ptr[1] == NUL)
515 ++curwin->w_cursor.col;
516#ifdef FEAT_MBYTE
517 else if (has_mbyte)
518 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000519 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr[i] == NUL)
521 curwin->w_cursor.col += i;
522 }
523#endif
524 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000525 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527 else
528 arrow_used = FALSE;
529
530 /* we are in insert mode now, don't need to start it anymore */
531 need_start_insertmode = FALSE;
532
533 /* Need to save the line for undo before inserting the first char. */
534 ins_need_undo = TRUE;
535
536#ifdef FEAT_MOUSE
537 where_paste_started.lnum = 0;
538#endif
539#ifdef FEAT_CINDENT
540 can_cindent = TRUE;
541#endif
542#ifdef FEAT_FOLDING
543 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
544 * restarting. */
545 if (!p_im && did_restart_edit == 0)
546 foldOpenCursor();
547#endif
548
549 /*
550 * If 'showmode' is set, show the current (insert/replace/..) mode.
551 * A warning message for changing a readonly file is given here, before
552 * actually changing anything. It's put after the mode, if any.
553 */
554 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000555 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 i = showmode();
557
558 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000559 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561#ifdef CURSOR_SHAPE
562 ui_cursor_shape(); /* may show different cursor shape */
563#endif
564#ifdef FEAT_DIGRAPHS
565 do_digraph(-1); /* clear digraphs */
566#endif
567
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000568 /*
569 * Get the current length of the redo buffer, those characters have to be
570 * skipped if we want to get to the inserted characters.
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ptr = get_inserted();
573 if (ptr == NULL)
574 new_insert_skip = 0;
575 else
576 {
577 new_insert_skip = (int)STRLEN(ptr);
578 vim_free(ptr);
579 }
580
581 old_indent = 0;
582
583 /*
584 * Main loop in Insert mode: repeat until Insert mode is left.
585 */
586 for (;;)
587 {
588#ifdef FEAT_RIGHTLEFT
589 if (!revins_legal)
590 revins_scol = -1; /* reset on illegal motions */
591 else
592 revins_legal = 0;
593#endif
594 if (arrow_used) /* don't repeat insert when arrow key used */
595 count = 0;
596
597 if (stop_insert_mode)
598 {
599 /* ":stopinsert" used or 'insertmode' reset */
600 count = 0;
601 goto doESCkey;
602 }
603
604 /* set curwin->w_curswant for next K_DOWN or K_UP */
605 if (!arrow_used)
606 curwin->w_set_curswant = TRUE;
607
608 /* If there is no typeahead may check for timestamps (e.g., for when a
609 * menu invoked a shell command). */
610 if (stuff_empty())
611 {
612 did_check_timestamps = FALSE;
613 if (need_check_timestamps)
614 check_timestamps(FALSE);
615 }
616
617 /*
618 * When emsg() was called msg_scroll will have been set.
619 */
620 msg_scroll = FALSE;
621
622#ifdef FEAT_GUI
623 /* When 'mousefocus' is set a mouse movement may have taken us to
624 * another window. "need_mouse_correct" may then be set because of an
625 * autocommand. */
626 if (need_mouse_correct)
627 gui_mouse_correct();
628#endif
629
630#ifdef FEAT_FOLDING
631 /* Open fold at the cursor line, according to 'foldopen'. */
632 if (fdo_flags & FDO_INSERT)
633 foldOpenCursor();
634 /* Close folds where the cursor isn't, according to 'foldclose' */
635 if (!char_avail())
636 foldCheckClose();
637#endif
638
639 /*
640 * If we inserted a character at the last position of the last line in
641 * the window, scroll the window one line up. This avoids an extra
642 * redraw.
643 * This is detected when the cursor column is smaller after inserting
644 * something.
645 * Don't do this when the topline changed already, it has
646 * already been adjusted (by insertchar() calling open_line())).
647 */
648 if (curbuf->b_mod_set
649 && curwin->w_p_wrap
650 && !did_backspace
651 && curwin->w_topline == old_topline
652#ifdef FEAT_DIFF
653 && curwin->w_topfill == old_topfill
654#endif
655 )
656 {
657 mincol = curwin->w_wcol;
658 validate_cursor_col();
659
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000660 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 && curwin->w_wrow == W_WINROW(curwin)
662 + curwin->w_height - 1 - p_so
663 && (curwin->w_cursor.lnum != curwin->w_topline
664#ifdef FEAT_DIFF
665 || curwin->w_topfill > 0
666#endif
667 ))
668 {
669#ifdef FEAT_DIFF
670 if (curwin->w_topfill > 0)
671 --curwin->w_topfill;
672 else
673#endif
674#ifdef FEAT_FOLDING
675 if (hasFolding(curwin->w_topline, NULL, &old_topline))
676 set_topline(curwin, old_topline + 1);
677 else
678#endif
679 set_topline(curwin, curwin->w_topline + 1);
680 }
681 }
682
683 /* May need to adjust w_topline to show the cursor. */
684 update_topline();
685
686 did_backspace = FALSE;
687
688 validate_cursor(); /* may set must_redraw */
689
690 /*
691 * Redraw the display when no characters are waiting.
692 * Also shows mode, ruler and positions cursor.
693 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000694 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696#ifdef FEAT_SCROLLBIND
697 if (curwin->w_p_scb)
698 do_check_scrollbind(TRUE);
699#endif
700
701 update_curswant();
702 old_topline = curwin->w_topline;
703#ifdef FEAT_DIFF
704 old_topfill = curwin->w_topfill;
705#endif
706
707#ifdef USE_ON_FLY_SCROLL
708 dont_scroll = FALSE; /* allow scrolling here */
709#endif
710
711 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000712 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 */
714 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000715 do
716 {
717 c = safe_vgetc();
718 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000720#ifdef FEAT_AUTOCMD
721 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
722 did_cursorhold = TRUE;
723#endif
724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef FEAT_RIGHTLEFT
726 if (p_hkmap && KeyTyped)
727 c = hkmap(c); /* Hebrew mode mapping */
728#endif
729#ifdef FEAT_FKMAP
730 if (p_fkmap && KeyTyped)
731 c = fkmap(c); /* Farsi mode mapping */
732#endif
733
734#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 /*
736 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000737 * and the cursor is still in the completed word. Only when there is
738 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000739 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000740 if (compl_started
741 && pum_wanted()
742 && curwin->w_cursor.col >= compl_col
743 && (compl_shown_match == NULL
744 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000745 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000746 /* BS: Delete one character from "compl_leader". */
747 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000748 && curwin->w_cursor.col > compl_col
749 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000750 continue;
751
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000752 /* When no match was selected or it was edited. */
753 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000754 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000755 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000756 * "compl_leader". Except when at the original match and
757 * there is nothing to add, CTRL-L works like CTRL-P then. */
758 if (c == Ctrl_L
759 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000760 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000761 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000762 {
763 ins_compl_addfrommatch();
764 continue;
765 }
766
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000767 /* A non-white character that fits in with the current
768 * completion: Add to "compl_leader". */
769 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000770 {
771 ins_compl_addleader(c);
772 continue;
773 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000774
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000775 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000776 * compl_enter_selects is set the Enter key does the same. */
777 if (c == Ctrl_Y || (compl_enter_selects
778 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000779 {
780 ins_compl_delete();
781 ins_compl_insert();
782 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000783 }
784 }
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
787 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000788 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000789 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000790 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#endif
792
Bram Moolenaar488c6512005-08-11 20:09:58 +0000793 /* CTRL-\ CTRL-N goes to Normal mode,
794 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
795 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (c == Ctrl_BSL)
797 {
798 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000799 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 ++no_mapping;
801 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000802 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 --no_mapping;
804 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000805 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000807 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 vungetc(c);
809 c = Ctrl_BSL;
810 }
811 else if (c == Ctrl_G && p_im)
812 continue;
813 else
814 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000815 if (c == Ctrl_O)
816 {
817 ins_ctrl_o();
818 ins_at_eol = FALSE; /* cursor keeps its column */
819 nomove = TRUE;
820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 count = 0;
822 goto doESCkey;
823 }
824 }
825
826#ifdef FEAT_DIGRAPHS
827 c = do_digraph(c);
828#endif
829
830#ifdef FEAT_INS_EXPAND
831 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
832 goto docomplete;
833#endif
834 if (c == Ctrl_V || c == Ctrl_Q)
835 {
836 ins_ctrl_v();
837 c = Ctrl_V; /* pretend CTRL-V is last typed character */
838 continue;
839 }
840
841#ifdef FEAT_CINDENT
842 if (cindent_on()
843# ifdef FEAT_INS_EXPAND
844 && ctrl_x_mode == 0
845# endif
846 )
847 {
848 /* A key name preceded by a bang means this key is not to be
849 * inserted. Skip ahead to the re-indenting below.
850 * A key name preceded by a star means that indenting has to be
851 * done before inserting the key. */
852 line_is_white = inindent(0);
853 if (in_cinkeys(c, '!', line_is_white))
854 goto force_cindent;
855 if (can_cindent && in_cinkeys(c, '*', line_is_white)
856 && stop_arrow() == OK)
857 do_c_expr_indent();
858 }
859#endif
860
861#ifdef FEAT_RIGHTLEFT
862 if (curwin->w_p_rl)
863 switch (c)
864 {
865 case K_LEFT: c = K_RIGHT; break;
866 case K_S_LEFT: c = K_S_RIGHT; break;
867 case K_C_LEFT: c = K_C_RIGHT; break;
868 case K_RIGHT: c = K_LEFT; break;
869 case K_S_RIGHT: c = K_S_LEFT; break;
870 case K_C_RIGHT: c = K_C_LEFT; break;
871 }
872#endif
873
874#ifdef FEAT_VISUAL
875 /*
876 * If 'keymodel' contains "startsel", may start selection. If it
877 * does, a CTRL-O and c will be stuffed, we need to get these
878 * characters.
879 */
880 if (ins_start_select(c))
881 continue;
882#endif
883
884 /*
885 * The big switch to handle a character in insert mode.
886 */
887 switch (c)
888 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000889 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (echeck_abbr(ESC + ABBR_OFF))
891 break;
892 /*FALLTHROUGH*/
893
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000894 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#ifdef FEAT_CMDWIN
896 if (c == Ctrl_C && cmdwin_type != 0)
897 {
898 /* Close the cmdline window. */
899 cmdwin_result = K_IGNORE;
900 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000901 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 goto doESCkey;
903 }
904#endif
905
906#ifdef UNIX
907do_intr:
908#endif
909 /* when 'insertmode' set, and not halfway a mapping, don't leave
910 * Insert mode */
911 if (goto_im())
912 {
913 if (got_int)
914 {
915 (void)vgetc(); /* flush all buffers */
916 got_int = FALSE;
917 }
918 else
919 vim_beep();
920 break;
921 }
922doESCkey:
923 /*
924 * This is the ONLY return from edit()!
925 */
926 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
927 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000928 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 o_lnum = curwin->w_cursor.lnum;
930
Bram Moolenaar488c6512005-08-11 20:09:58 +0000931 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000932 {
933#ifdef FEAT_AUTOCMD
934 if (cmdchar != 'r' && cmdchar != 'v')
935 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
936 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000937 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 continue;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case Ctrl_Z: /* suspend when 'insertmode' set */
944 if (!p_im)
945 goto normalchar; /* insert CTRL-Z as normal char */
946 stuffReadbuff((char_u *)":st\r");
947 c = Ctrl_O;
948 /*FALLTHROUGH*/
949
950 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000951#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000952 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 goto docomplete;
954#endif
955 if (echeck_abbr(Ctrl_O + ABBR_OFF))
956 break;
957 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000958
959#ifdef FEAT_VIRTUALEDIT
960 /* don't move the cursor left when 'virtualedit' has "onemore". */
961 if (ve_flags & VE_ONEMORE)
962 {
963 ins_at_eol = FALSE;
964 nomove = TRUE;
965 }
966#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000967 count = 0;
968 goto doESCkey;
969
Bram Moolenaar572cb562005-08-05 21:35:02 +0000970 case K_INS: /* toggle insert/replace mode */
971 case K_KINS:
972 ins_insert(replaceState);
973 break;
974
975 case K_SELECT: /* end of Select mode mapping - ignore */
976 break;
977
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978#ifdef FEAT_SNIFF
979 case K_SNIFF: /* Sniff command received */
980 stuffcharReadbuff(K_SNIFF);
981 goto doESCkey;
982#endif
983
984 case K_HELP: /* Help key works like <ESC> <Help> */
985 case K_F1:
986 case K_XF1:
987 stuffcharReadbuff(K_HELP);
988 if (p_im)
989 need_start_insertmode = TRUE;
990 goto doESCkey;
991
992#ifdef FEAT_NETBEANS_INTG
993 case K_F21: /* NetBeans command */
994 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000995 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000996 --no_mapping;
997 netbeans_keycommand(i);
998 break;
999#endif
1000
1001 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 case NUL:
1003 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 /* For ^@ the trailing ESC will end the insert, unless there is an
1005 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1007 && c != Ctrl_A && !p_im)
1008 goto doESCkey; /* quit insert mode */
1009 inserted_space = FALSE;
1010 break;
1011
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001012 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 ins_reg();
1014 auto_format(FALSE, TRUE);
1015 inserted_space = FALSE;
1016 break;
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 ins_ctrl_g();
1020 break;
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case Ctrl_HAT: /* switch input mode and/or langmap */
1023 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 break;
1025
1026#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (!p_ari)
1029 goto normalchar;
1030 ins_ctrl_();
1031 break;
1032#endif
1033
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1036 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1037 goto docomplete;
1038#endif
1039 /* FALLTHROUGH */
1040
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042# ifdef FEAT_INS_EXPAND
1043 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1044 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 if (has_compl_option(FALSE))
1046 goto docomplete;
1047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049# endif
1050 ins_shift(c, lastc);
1051 auto_format(FALSE, TRUE);
1052 inserted_space = FALSE;
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 case K_KDEL:
1057 ins_del();
1058 auto_format(FALSE, TRUE);
1059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case Ctrl_H:
1063 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1064 auto_format(FALSE, TRUE);
1065 break;
1066
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1069 auto_format(FALSE, TRUE);
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001073# ifdef FEAT_COMPL_FUNC
1074 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001076 goto docomplete;
1077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1079 auto_format(FALSE, TRUE);
1080 inserted_space = FALSE;
1081 break;
1082
1083#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 case K_LEFTMOUSE_NM:
1086 case K_LEFTDRAG:
1087 case K_LEFTRELEASE:
1088 case K_LEFTRELEASE_NM:
1089 case K_MIDDLEMOUSE:
1090 case K_MIDDLEDRAG:
1091 case K_MIDDLERELEASE:
1092 case K_RIGHTMOUSE:
1093 case K_RIGHTDRAG:
1094 case K_RIGHTRELEASE:
1095 case K_X1MOUSE:
1096 case K_X1DRAG:
1097 case K_X1RELEASE:
1098 case K_X2MOUSE:
1099 case K_X2DRAG:
1100 case K_X2RELEASE:
1101 ins_mouse(c);
1102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ins_mousescroll(FALSE);
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_mousescroll(TRUE);
1110 break;
1111#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001112#ifdef FEAT_GUI_TABLINE
1113 case K_TABLINE:
1114 case K_TABMENU:
1115 ins_tabline(c);
1116 break;
1117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar754b5602006-02-09 23:53:20 +00001122#ifdef FEAT_AUTOCMD
1123 case K_CURSORHOLD: /* Didn't type something for a while. */
1124 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1125 did_cursorhold = TRUE;
1126 break;
1127#endif
1128
Bram Moolenaar4770d092006-01-12 23:22:24 +00001129#ifdef FEAT_GUI_W32
1130 /* On Win32 ignore <M-F4>, we get it when closing the window was
1131 * cancelled. */
1132 case K_F4:
1133 if (mod_mask != MOD_MASK_ALT)
1134 goto normalchar;
1135 break;
1136#endif
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_GUI
1139 case K_VER_SCROLLBAR:
1140 ins_scroll();
1141 break;
1142
1143 case K_HOR_SCROLLBAR:
1144 ins_horscroll();
1145 break;
1146#endif
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 case K_S_HOME:
1151 case K_C_HOME:
1152 ins_home(c);
1153 break;
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 case K_S_END:
1158 case K_C_END:
1159 ins_end(c);
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001163 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1164 ins_s_left();
1165 else
1166 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case K_C_LEFT:
1171 ins_s_left();
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001175 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1176 ins_s_right();
1177 else
1178 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 break;
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 case K_C_RIGHT:
1183 ins_s_right();
1184 break;
1185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001186 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001187#ifdef FEAT_INS_EXPAND
1188 if (pum_visible())
1189 goto docomplete;
1190#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001191 if (mod_mask & MOD_MASK_SHIFT)
1192 ins_pageup();
1193 else
1194 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 break;
1196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 case K_PAGEUP:
1199 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001200#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001201 if (pum_visible())
1202 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 ins_pageup();
1205 break;
1206
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001207 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001208#ifdef FEAT_INS_EXPAND
1209 if (pum_visible())
1210 goto docomplete;
1211#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001212 if (mod_mask & MOD_MASK_SHIFT)
1213 ins_pagedown();
1214 else
1215 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 break;
1217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001218 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 case K_PAGEDOWN:
1220 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001221#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001222 if (pum_visible())
1223 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 ins_pagedown();
1226 break;
1227
1228#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ins_drop();
1231 break;
1232#endif
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 c = TAB;
1236 /* FALLTHROUGH */
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1240 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1241 goto docomplete;
1242#endif
1243 inserted_space = FALSE;
1244 if (ins_tab())
1245 goto normalchar; /* insert TAB as a normal char */
1246 auto_format(FALSE, TRUE);
1247 break;
1248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001249 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 c = CAR;
1251 /* FALLTHROUGH */
1252 case CAR:
1253 case NL:
1254#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1255 /* In a quickfix window a <CR> jumps to the error under the
1256 * cursor. */
1257 if (bt_quickfix(curbuf) && c == CAR)
1258 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001259 if (curwin->w_llist_ref == NULL) /* quickfix window */
1260 do_cmdline_cmd((char_u *)".cc");
1261 else /* location list window */
1262 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 break;
1264 }
1265#endif
1266#ifdef FEAT_CMDWIN
1267 if (cmdwin_type != 0)
1268 {
1269 /* Execute the command in the cmdline window. */
1270 cmdwin_result = CAR;
1271 goto doESCkey;
1272 }
1273#endif
1274 if (ins_eol(c) && !p_im)
1275 goto doESCkey; /* out of memory */
1276 auto_format(FALSE, FALSE);
1277 inserted_space = FALSE;
1278 break;
1279
1280#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282# ifdef FEAT_INS_EXPAND
1283 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1284 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 if (has_compl_option(TRUE))
1286 goto docomplete;
1287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289# endif
1290# ifdef FEAT_DIGRAPHS
1291 c = ins_digraph();
1292 if (c == NUL)
1293 break;
1294# endif
1295 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001299 case Ctrl_X: /* Enter CTRL-X mode */
1300 ins_ctrl_x();
1301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (ctrl_x_mode != CTRL_X_TAGS)
1305 goto normalchar;
1306 goto docomplete;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (ctrl_x_mode != CTRL_X_FILES)
1310 goto normalchar;
1311 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001312
1313 case 's': /* Spelling completion after ^X */
1314 case Ctrl_S:
1315 if (ctrl_x_mode != CTRL_X_SPELL)
1316 goto normalchar;
1317 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#endif
1319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321#ifdef FEAT_INS_EXPAND
1322 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1323#endif
1324 {
1325 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1326 if (p_im)
1327 {
1328 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1329 break;
1330 goto doESCkey;
1331 }
1332 goto normalchar;
1333 }
1334#ifdef FEAT_INS_EXPAND
1335 /* FALLTHROUGH */
1336
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 case Ctrl_N:
1339 /* if 'complete' is empty then plain ^P is no longer special,
1340 * but it is under other ^X modes */
1341 if (*curbuf->b_p_cpt == NUL
1342 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 goto normalchar;
1345
1346docomplete:
1347 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001348 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 break;
1350#endif /* FEAT_INS_EXPAND */
1351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001352 case Ctrl_Y: /* copy from previous line or scroll down */
1353 case Ctrl_E: /* copy from next line or scroll up */
1354 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 break;
1356
1357 default:
1358#ifdef UNIX
1359 if (c == intr_char) /* special interrupt char */
1360 goto do_intr;
1361#endif
1362
1363 /*
1364 * Insert a nomal character.
1365 */
1366normalchar:
1367#ifdef FEAT_SMARTINDENT
1368 /* Try to perform smart-indenting. */
1369 ins_try_si(c);
1370#endif
1371
1372 if (c == ' ')
1373 {
1374 inserted_space = TRUE;
1375#ifdef FEAT_CINDENT
1376 if (inindent(0))
1377 can_cindent = FALSE;
1378#endif
1379 if (Insstart_blank_vcol == MAXCOL
1380 && curwin->w_cursor.lnum == Insstart.lnum)
1381 Insstart_blank_vcol = get_nolist_virtcol();
1382 }
1383
1384 if (vim_iswordc(c) || !echeck_abbr(
1385#ifdef FEAT_MBYTE
1386 /* Add ABBR_OFF for characters above 0x100, this is
1387 * what check_abbr() expects. */
1388 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1389#endif
1390 c))
1391 {
1392 insert_special(c, FALSE, FALSE);
1393#ifdef FEAT_RIGHTLEFT
1394 revins_legal++;
1395 revins_chars++;
1396#endif
1397 }
1398
1399 auto_format(FALSE, TRUE);
1400
1401#ifdef FEAT_FOLDING
1402 /* When inserting a character the cursor line must never be in a
1403 * closed fold. */
1404 foldOpenCursor();
1405#endif
1406 break;
1407 } /* end of switch (c) */
1408
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001409#ifdef FEAT_AUTOCMD
1410 /* If typed something may trigger CursorHoldI again. */
1411 if (c != K_CURSORHOLD)
1412 did_cursorhold = FALSE;
1413#endif
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 /* If the cursor was moved we didn't just insert a space */
1416 if (arrow_used)
1417 inserted_space = FALSE;
1418
1419#ifdef FEAT_CINDENT
1420 if (can_cindent && cindent_on()
1421# ifdef FEAT_INS_EXPAND
1422 && ctrl_x_mode == 0
1423# endif
1424 )
1425 {
1426force_cindent:
1427 /*
1428 * Indent now if a key was typed that is in 'cinkeys'.
1429 */
1430 if (in_cinkeys(c, ' ', line_is_white))
1431 {
1432 if (stop_arrow() == OK)
1433 /* re-indent the current line */
1434 do_c_expr_indent();
1435 }
1436 }
1437#endif /* FEAT_CINDENT */
1438
1439 } /* for (;;) */
1440 /* NOTREACHED */
1441}
1442
1443/*
1444 * Redraw for Insert mode.
1445 * This is postponed until getting the next character to make '$' in the 'cpo'
1446 * option work correctly.
1447 * Only redraw when there are no characters available. This speeds up
1448 * inserting sequences of characters (e.g., for CTRL-R).
1449 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001450/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001452ins_redraw(ready)
1453 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 if (!char_avail())
1456 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001457#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001458 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1459 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001460 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001461 && !equalpos(last_cursormoved, curwin->w_cursor)
1462# ifdef FEAT_INS_EXPAND
1463 && !pum_visible()
1464# endif
1465 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001466 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001467# ifdef FEAT_SYN_HL
1468 /* Need to update the screen first, to make sure syntax
1469 * highlighting is correct after making a change (e.g., inserting
1470 * a "(". The autocommand may also require a redraw, so it's done
1471 * again below, unfortunately. */
1472 if (syntax_present(curbuf) && must_redraw)
1473 update_screen(0);
1474# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001475 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1476 last_cursormoved = curwin->w_cursor;
1477 }
1478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (must_redraw)
1480 update_screen(0);
1481 else if (clear_cmdline || redraw_cmdline)
1482 showmode(); /* clear cmdline and show mode */
1483 showruler(FALSE);
1484 setcursor();
1485 emsg_on_display = FALSE; /* may remove error message now */
1486 }
1487}
1488
1489/*
1490 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1491 */
1492 static void
1493ins_ctrl_v()
1494{
1495 int c;
1496
1497 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001498 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
1500 if (redrawing() && !char_avail())
1501 edit_putchar('^', TRUE);
1502 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1503
1504#ifdef FEAT_CMDL_INFO
1505 add_to_showcmd_c(Ctrl_V);
1506#endif
1507
1508 c = get_literal();
1509#ifdef FEAT_CMDL_INFO
1510 clear_showcmd();
1511#endif
1512 insert_special(c, FALSE, TRUE);
1513#ifdef FEAT_RIGHTLEFT
1514 revins_chars++;
1515 revins_legal++;
1516#endif
1517}
1518
1519/*
1520 * Put a character directly onto the screen. It's not stored in a buffer.
1521 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1522 */
1523static int pc_status;
1524#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1525#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1526#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1527#define PC_STATUS_SET 3 /* pc_bytes was filled */
1528#ifdef FEAT_MBYTE
1529static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1530#else
1531static char_u pc_bytes[2]; /* saved bytes */
1532#endif
1533static int pc_attr;
1534static int pc_row;
1535static int pc_col;
1536
1537 void
1538edit_putchar(c, highlight)
1539 int c;
1540 int highlight;
1541{
1542 int attr;
1543
1544 if (ScreenLines != NULL)
1545 {
1546 update_topline(); /* just in case w_topline isn't valid */
1547 validate_cursor();
1548 if (highlight)
1549 attr = hl_attr(HLF_8);
1550 else
1551 attr = 0;
1552 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1553 pc_col = W_WINCOL(curwin);
1554#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1555 pc_status = PC_STATUS_UNSET;
1556#endif
1557#ifdef FEAT_RIGHTLEFT
1558 if (curwin->w_p_rl)
1559 {
1560 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1561# ifdef FEAT_MBYTE
1562 if (has_mbyte)
1563 {
1564 int fix_col = mb_fix_col(pc_col, pc_row);
1565
1566 if (fix_col != pc_col)
1567 {
1568 screen_putchar(' ', pc_row, fix_col, attr);
1569 --curwin->w_wcol;
1570 pc_status = PC_STATUS_RIGHT;
1571 }
1572 }
1573# endif
1574 }
1575 else
1576#endif
1577 {
1578 pc_col += curwin->w_wcol;
1579#ifdef FEAT_MBYTE
1580 if (mb_lefthalve(pc_row, pc_col))
1581 pc_status = PC_STATUS_LEFT;
1582#endif
1583 }
1584
1585 /* save the character to be able to put it back */
1586#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1587 if (pc_status == PC_STATUS_UNSET)
1588#endif
1589 {
1590 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1591 pc_status = PC_STATUS_SET;
1592 }
1593 screen_putchar(c, pc_row, pc_col, attr);
1594 }
1595}
1596
1597/*
1598 * Undo the previous edit_putchar().
1599 */
1600 void
1601edit_unputchar()
1602{
1603 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1604 {
1605#if defined(FEAT_MBYTE)
1606 if (pc_status == PC_STATUS_RIGHT)
1607 ++curwin->w_wcol;
1608 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1609 redrawWinline(curwin->w_cursor.lnum, FALSE);
1610 else
1611#endif
1612 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1613 }
1614}
1615
1616/*
1617 * Called when p_dollar is set: display a '$' at the end of the changed text
1618 * Only works when cursor is in the line that changes.
1619 */
1620 void
1621display_dollar(col)
1622 colnr_T col;
1623{
1624 colnr_T save_col;
1625
1626 if (!redrawing())
1627 return;
1628
1629 cursor_off();
1630 save_col = curwin->w_cursor.col;
1631 curwin->w_cursor.col = col;
1632#ifdef FEAT_MBYTE
1633 if (has_mbyte)
1634 {
1635 char_u *p;
1636
1637 /* If on the last byte of a multi-byte move to the first byte. */
1638 p = ml_get_curline();
1639 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1640 }
1641#endif
1642 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1643 if (curwin->w_wcol < W_WIDTH(curwin))
1644 {
1645 edit_putchar('$', FALSE);
1646 dollar_vcol = curwin->w_virtcol;
1647 }
1648 curwin->w_cursor.col = save_col;
1649}
1650
1651/*
1652 * Call this function before moving the cursor from the normal insert position
1653 * in insert mode.
1654 */
1655 static void
1656undisplay_dollar()
1657{
1658 if (dollar_vcol)
1659 {
1660 dollar_vcol = 0;
1661 redrawWinline(curwin->w_cursor.lnum, FALSE);
1662 }
1663}
1664
1665/*
1666 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1667 * Keep the cursor on the same character.
1668 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1669 * type == INDENT_DEC decrease indent (for CTRL-D)
1670 * type == INDENT_SET set indent to "amount"
1671 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1672 */
1673 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001674change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 int type;
1676 int amount;
1677 int round;
1678 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001679 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
1681 int vcol;
1682 int last_vcol;
1683 int insstart_less; /* reduction for Insstart.col */
1684 int new_cursor_col;
1685 int i;
1686 char_u *ptr;
1687 int save_p_list;
1688 int start_col;
1689 colnr_T vc;
1690#ifdef FEAT_VREPLACE
1691 colnr_T orig_col = 0; /* init for GCC */
1692 char_u *new_line, *orig_line = NULL; /* init for GCC */
1693
1694 /* VREPLACE mode needs to know what the line was like before changing */
1695 if (State & VREPLACE_FLAG)
1696 {
1697 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1698 orig_col = curwin->w_cursor.col;
1699 }
1700#endif
1701
1702 /* for the following tricks we don't want list mode */
1703 save_p_list = curwin->w_p_list;
1704 curwin->w_p_list = FALSE;
1705 vc = getvcol_nolist(&curwin->w_cursor);
1706 vcol = vc;
1707
1708 /*
1709 * For Replace mode we need to fix the replace stack later, which is only
1710 * possible when the cursor is in the indent. Remember the number of
1711 * characters before the cursor if it's possible.
1712 */
1713 start_col = curwin->w_cursor.col;
1714
1715 /* determine offset from first non-blank */
1716 new_cursor_col = curwin->w_cursor.col;
1717 beginline(BL_WHITE);
1718 new_cursor_col -= curwin->w_cursor.col;
1719
1720 insstart_less = curwin->w_cursor.col;
1721
1722 /*
1723 * If the cursor is in the indent, compute how many screen columns the
1724 * cursor is to the left of the first non-blank.
1725 */
1726 if (new_cursor_col < 0)
1727 vcol = get_indent() - vcol;
1728
1729 if (new_cursor_col > 0) /* can't fix replace stack */
1730 start_col = -1;
1731
1732 /*
1733 * Set the new indent. The cursor will be put on the first non-blank.
1734 */
1735 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001736 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 else
1738 {
1739#ifdef FEAT_VREPLACE
1740 int save_State = State;
1741
1742 /* Avoid being called recursively. */
1743 if (State & VREPLACE_FLAG)
1744 State = INSERT;
1745#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001746 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747#ifdef FEAT_VREPLACE
1748 State = save_State;
1749#endif
1750 }
1751 insstart_less -= curwin->w_cursor.col;
1752
1753 /*
1754 * Try to put cursor on same character.
1755 * If the cursor is at or after the first non-blank in the line,
1756 * compute the cursor column relative to the column of the first
1757 * non-blank character.
1758 * If we are not in insert mode, leave the cursor on the first non-blank.
1759 * If the cursor is before the first non-blank, position it relative
1760 * to the first non-blank, counted in screen columns.
1761 */
1762 if (new_cursor_col >= 0)
1763 {
1764 /*
1765 * When changing the indent while the cursor is touching it, reset
1766 * Insstart_col to 0.
1767 */
1768 if (new_cursor_col == 0)
1769 insstart_less = MAXCOL;
1770 new_cursor_col += curwin->w_cursor.col;
1771 }
1772 else if (!(State & INSERT))
1773 new_cursor_col = curwin->w_cursor.col;
1774 else
1775 {
1776 /*
1777 * Compute the screen column where the cursor should be.
1778 */
1779 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001780 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 /*
1783 * Advance the cursor until we reach the right screen column.
1784 */
1785 vcol = last_vcol = 0;
1786 new_cursor_col = -1;
1787 ptr = ml_get_curline();
1788 while (vcol <= (int)curwin->w_virtcol)
1789 {
1790 last_vcol = vcol;
1791#ifdef FEAT_MBYTE
1792 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001793 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 else
1795#endif
1796 ++new_cursor_col;
1797 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1798 }
1799 vcol = last_vcol;
1800
1801 /*
1802 * May need to insert spaces to be able to position the cursor on
1803 * the right screen column.
1804 */
1805 if (vcol != (int)curwin->w_virtcol)
1806 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001807 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001809 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (ptr != NULL)
1811 {
1812 new_cursor_col += i;
1813 ptr[i] = NUL;
1814 while (--i >= 0)
1815 ptr[i] = ' ';
1816 ins_str(ptr);
1817 vim_free(ptr);
1818 }
1819 }
1820
1821 /*
1822 * When changing the indent while the cursor is in it, reset
1823 * Insstart_col to 0.
1824 */
1825 insstart_less = MAXCOL;
1826 }
1827
1828 curwin->w_p_list = save_p_list;
1829
1830 if (new_cursor_col <= 0)
1831 curwin->w_cursor.col = 0;
1832 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001833 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 curwin->w_set_curswant = TRUE;
1835 changed_cline_bef_curs();
1836
1837 /*
1838 * May have to adjust the start of the insert.
1839 */
1840 if (State & INSERT)
1841 {
1842 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1843 {
1844 if ((int)Insstart.col <= insstart_less)
1845 Insstart.col = 0;
1846 else
1847 Insstart.col -= insstart_less;
1848 }
1849 if ((int)ai_col <= insstart_less)
1850 ai_col = 0;
1851 else
1852 ai_col -= insstart_less;
1853 }
1854
1855 /*
1856 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1857 * If the number of characters before the cursor decreased, need to pop a
1858 * few characters from the replace stack.
1859 * If the number of characters before the cursor increased, need to push a
1860 * few NULs onto the replace stack.
1861 */
1862 if (REPLACE_NORMAL(State) && start_col >= 0)
1863 {
1864 while (start_col > (int)curwin->w_cursor.col)
1865 {
1866 replace_join(0); /* remove a NUL from the replace stack */
1867 --start_col;
1868 }
1869 while (start_col < (int)curwin->w_cursor.col || replaced)
1870 {
1871 replace_push(NUL);
1872 if (replaced)
1873 {
1874 replace_push(replaced);
1875 replaced = NUL;
1876 }
1877 ++start_col;
1878 }
1879 }
1880
1881#ifdef FEAT_VREPLACE
1882 /*
1883 * For VREPLACE mode, we also have to fix the replace stack. In this case
1884 * it is always possible because we backspace over the whole line and then
1885 * put it back again the way we wanted it.
1886 */
1887 if (State & VREPLACE_FLAG)
1888 {
1889 /* If orig_line didn't allocate, just return. At least we did the job,
1890 * even if you can't backspace. */
1891 if (orig_line == NULL)
1892 return;
1893
1894 /* Save new line */
1895 new_line = vim_strsave(ml_get_curline());
1896 if (new_line == NULL)
1897 return;
1898
1899 /* We only put back the new line up to the cursor */
1900 new_line[curwin->w_cursor.col] = NUL;
1901
1902 /* Put back original line */
1903 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1904 curwin->w_cursor.col = orig_col;
1905
1906 /* Backspace from cursor to start of line */
1907 backspace_until_column(0);
1908
1909 /* Insert new stuff into line again */
1910 ins_bytes(new_line);
1911
1912 vim_free(new_line);
1913 }
1914#endif
1915}
1916
1917/*
1918 * Truncate the space at the end of a line. This is to be used only in an
1919 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1920 * modes.
1921 */
1922 void
1923truncate_spaces(line)
1924 char_u *line;
1925{
1926 int i;
1927
1928 /* find start of trailing white space */
1929 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1930 {
1931 if (State & REPLACE_FLAG)
1932 replace_join(0); /* remove a NUL from the replace stack */
1933 }
1934 line[i + 1] = NUL;
1935}
1936
1937#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1938 || defined(FEAT_COMMENTS) || defined(PROTO)
1939/*
1940 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1941 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001942 * Will attempt not to go before "col" even when there is a composing
1943 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 */
1945 void
1946backspace_until_column(col)
1947 int col;
1948{
1949 while ((int)curwin->w_cursor.col > col)
1950 {
1951 curwin->w_cursor.col--;
1952 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001953 replace_do_bs(col);
1954 else if (!del_char_after_col(col))
1955 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957}
1958#endif
1959
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001960/*
1961 * Like del_char(), but make sure not to go before column "limit_col".
1962 * Only matters when there are composing characters.
1963 * Return TRUE when something was deleted.
1964 */
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001965/*ARGSUSED*/
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001966 static int
1967del_char_after_col(limit_col)
1968 int limit_col;
1969{
1970#ifdef FEAT_MBYTE
1971 if (enc_utf8 && limit_col >= 0)
1972 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001973 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001974
1975 /* Make sure the cursor is at the start of a character, but
1976 * skip forward again when going too far back because of a
1977 * composing character. */
1978 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001979 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001980 {
1981 int l = utf_ptr2len(ml_get_cursor());
1982
1983 if (l == 0) /* end of line */
1984 break;
1985 curwin->w_cursor.col += l;
1986 }
1987 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1988 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001989 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001990 }
1991 else
1992#endif
1993 (void)del_char(FALSE);
1994 return TRUE;
1995}
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1998/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001999 * CTRL-X pressed in Insert mode.
2000 */
2001 static void
2002ins_ctrl_x()
2003{
2004 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2005 * CTRL-V works like CTRL-N */
2006 if (ctrl_x_mode != CTRL_X_CMDLINE)
2007 {
2008 /* if the next ^X<> won't ADD nothing, then reset
2009 * compl_cont_status */
2010 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002011 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002012 else
2013 compl_cont_status = 0;
2014 /* We're not sure which CTRL-X mode it will be yet */
2015 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2016 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2017 edit_submode_pre = NULL;
2018 showmode();
2019 }
2020}
2021
2022/*
2023 * Return TRUE if the 'dict' or 'tsr' option can be used.
2024 */
2025 static int
2026has_compl_option(dict_opt)
2027 int dict_opt;
2028{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002029 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002030# ifdef FEAT_SPELL
2031 && !curwin->w_p_spell
2032# endif
2033 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002034 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2035 {
2036 ctrl_x_mode = 0;
2037 edit_submode = NULL;
2038 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2039 : (char_u *)_("'thesaurus' option is empty"),
2040 hl_attr(HLF_E));
2041 if (emsg_silent == 0)
2042 {
2043 vim_beep();
2044 setcursor();
2045 out_flush();
2046 ui_delay(2000L, FALSE);
2047 }
2048 return FALSE;
2049 }
2050 return TRUE;
2051}
2052
2053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2055 * This depends on the current mode.
2056 */
2057 int
2058vim_is_ctrl_x_key(c)
2059 int c;
2060{
2061 /* Always allow ^R - let it's results then be checked */
2062 if (c == Ctrl_R)
2063 return TRUE;
2064
Bram Moolenaare3226be2005-12-18 22:10:00 +00002065 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002066 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002067 return TRUE;
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 switch (ctrl_x_mode)
2070 {
2071 case 0: /* Not in any CTRL-X mode */
2072 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2073 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002074 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2076 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2077 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002078 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2079 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 case CTRL_X_SCROLL:
2081 return (c == Ctrl_Y || c == Ctrl_E);
2082 case CTRL_X_WHOLE_LINE:
2083 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2084 case CTRL_X_FILES:
2085 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2086 case CTRL_X_DICTIONARY:
2087 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2088 case CTRL_X_THESAURUS:
2089 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2090 case CTRL_X_TAGS:
2091 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2092#ifdef FEAT_FIND_ID
2093 case CTRL_X_PATH_PATTERNS:
2094 return (c == Ctrl_P || c == Ctrl_N);
2095 case CTRL_X_PATH_DEFINES:
2096 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2097#endif
2098 case CTRL_X_CMDLINE:
2099 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2100 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002101#ifdef FEAT_COMPL_FUNC
2102 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002103 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002104 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002106#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002107 case CTRL_X_SPELL:
2108 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 EMSG(_(e_internal));
2111 return FALSE;
2112}
2113
2114/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002115 * Return TRUE when character "c" is part of the item currently being
2116 * completed. Used to decide whether to abandon complete mode when the menu
2117 * is visible.
2118 */
2119 static int
2120ins_compl_accept_char(c)
2121 int c;
2122{
2123 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2124 /* When expanding an identifier only accept identifier chars. */
2125 return vim_isIDc(c);
2126
2127 switch (ctrl_x_mode)
2128 {
2129 case CTRL_X_FILES:
2130 /* When expanding file name only accept file name chars. But not
2131 * path separators, so that "proto/<Tab>" expands files in
2132 * "proto", not "proto/" as a whole */
2133 return vim_isfilec(c) && !vim_ispathsep(c);
2134
2135 case CTRL_X_CMDLINE:
2136 case CTRL_X_OMNI:
2137 /* Command line and Omni completion can work with just about any
2138 * printable character, but do stop at white space. */
2139 return vim_isprintc(c) && !vim_iswhite(c);
2140
2141 case CTRL_X_WHOLE_LINE:
2142 /* For while line completion a space can be part of the line. */
2143 return vim_isprintc(c);
2144 }
2145 return vim_iswordc(c);
2146}
2147
2148/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002149 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002151 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 * the rest of the word to be in -- webb
2153 */
2154 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002155ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 char_u *str;
2157 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002158 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 char_u *fname;
2160 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002161 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002163 char_u *p;
2164 int i, c;
2165 int actual_len; /* Take multi-byte characters */
2166 int actual_compl_length; /* into account. */
2167 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 int has_lower = FALSE;
2169 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002171 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002173 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaara2993e12007-08-12 14:38:46 +00002175 /* Find actual length of completion. */
2176#ifdef FEAT_MBYTE
2177 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002179 p = str;
2180 actual_len = 0;
2181 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002183 mb_ptr_adv(p);
2184 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002187 else
2188#endif
2189 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
Bram Moolenaara2993e12007-08-12 14:38:46 +00002191 /* Find actual length of original text. */
2192#ifdef FEAT_MBYTE
2193 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002195 p = compl_orig_text;
2196 actual_compl_length = 0;
2197 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002199 mb_ptr_adv(p);
2200 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 }
2202 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002203 else
2204#endif
2205 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
Bram Moolenaara2993e12007-08-12 14:38:46 +00002207 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002208 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002209 if (wca != NULL)
2210 {
2211 p = str;
2212 for (i = 0; i < actual_len; ++i)
2213#ifdef FEAT_MBYTE
2214 if (has_mbyte)
2215 wca[i] = mb_ptr2char_adv(&p);
2216 else
2217#endif
2218 wca[i] = *(p++);
2219
2220 /* Rule 1: Were any chars converted to lower? */
2221 p = compl_orig_text;
2222 for (i = 0; i < actual_compl_length; ++i)
2223 {
2224#ifdef FEAT_MBYTE
2225 if (has_mbyte)
2226 c = mb_ptr2char_adv(&p);
2227 else
2228#endif
2229 c = *(p++);
2230 if (MB_ISLOWER(c))
2231 {
2232 has_lower = TRUE;
2233 if (MB_ISUPPER(wca[i]))
2234 {
2235 /* Rule 1 is satisfied. */
2236 for (i = actual_compl_length; i < actual_len; ++i)
2237 wca[i] = MB_TOLOWER(wca[i]);
2238 break;
2239 }
2240 }
2241 }
2242
2243 /*
2244 * Rule 2: No lower case, 2nd consecutive letter converted to
2245 * upper case.
2246 */
2247 if (!has_lower)
2248 {
2249 p = compl_orig_text;
2250 for (i = 0; i < actual_compl_length; ++i)
2251 {
2252#ifdef FEAT_MBYTE
2253 if (has_mbyte)
2254 c = mb_ptr2char_adv(&p);
2255 else
2256#endif
2257 c = *(p++);
2258 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2259 {
2260 /* Rule 2 is satisfied. */
2261 for (i = actual_compl_length; i < actual_len; ++i)
2262 wca[i] = MB_TOUPPER(wca[i]);
2263 break;
2264 }
2265 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2266 }
2267 }
2268
2269 /* Copy the original case of the part we typed. */
2270 p = compl_orig_text;
2271 for (i = 0; i < actual_compl_length; ++i)
2272 {
2273#ifdef FEAT_MBYTE
2274 if (has_mbyte)
2275 c = mb_ptr2char_adv(&p);
2276 else
2277#endif
2278 c = *(p++);
2279 if (MB_ISLOWER(c))
2280 wca[i] = MB_TOLOWER(wca[i]);
2281 else if (MB_ISUPPER(c))
2282 wca[i] = MB_TOUPPER(wca[i]);
2283 }
2284
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002285 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002286 * Generate encoding specific output from wide character array.
2287 * Multi-byte characters can occupy up to five bytes more than
2288 * ASCII characters, and we also need one byte for NUL, so stay
2289 * six bytes away from the edge of IObuff.
2290 */
2291 p = IObuff;
2292 i = 0;
2293 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2294#ifdef FEAT_MBYTE
2295 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002296 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002297 else
2298#endif
2299 *(p++) = wca[i++];
2300 *p = NUL;
2301
2302 vim_free(wca);
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002305 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2306 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002308 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309}
2310
2311/*
2312 * Add a match to the list of matches.
2313 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002314 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002315 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002317 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002318ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 char_u *str;
2320 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002321 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002323 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002324 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002325 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002326 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002328 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002329 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 ui_breakcheck();
2332 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (len < 0)
2335 len = (int)STRLEN(str);
2336
2337 /*
2338 * If the same match is already present, don't add it.
2339 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002340 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002342 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 do
2344 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002345 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002346 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002347 && match->cp_str[len] == NUL)
2348 return NOTDONE;
2349 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002350 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002353 /* Remove any popup menu before changing the list of matches. */
2354 ins_compl_del_pum();
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 /*
2357 * Allocate a new match structure.
2358 * Copy the values to the new match structure.
2359 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002360 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002362 return FAIL;
2363 match->cp_number = -1;
2364 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002365 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002366 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
2368 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002371 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002374 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2376 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002377 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002378 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002379 && compl_curr_match->cp_fname != NULL
2380 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002381 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002382 else if (fname != NULL)
2383 {
2384 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002385 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002388 match->cp_fname = NULL;
2389 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002390
2391 if (cptext != NULL)
2392 {
2393 int i;
2394
2395 for (i = 0; i < CPT_COUNT; ++i)
2396 if (cptext[i] != NULL && *cptext[i] != NUL)
2397 match->cp_text[i] = vim_strsave(cptext[i]);
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 /*
2401 * Link the new match structure in the list of matches.
2402 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002403 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002404 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 else if (dir == FORWARD)
2406 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002407 match->cp_next = compl_curr_match->cp_next;
2408 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 else /* BACKWARD */
2411 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002412 match->cp_next = compl_curr_match;
2413 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002415 if (match->cp_next)
2416 match->cp_next->cp_prev = match;
2417 if (match->cp_prev)
2418 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002420 compl_first_match = match;
2421 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002423 /*
2424 * Find the longest common string if still doing that.
2425 */
2426 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2427 ins_compl_longest_match(match);
2428
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return OK;
2430}
2431
2432/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002433 * Return TRUE if "str[len]" matches with match->cp_str, considering
2434 * match->cp_icase.
2435 */
2436 static int
2437ins_compl_equal(match, str, len)
2438 compl_T *match;
2439 char_u *str;
2440 int len;
2441{
2442 if (match->cp_icase)
2443 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2444 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2445}
2446
2447/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002448 * Reduce the longest common string for match "match".
2449 */
2450 static void
2451ins_compl_longest_match(match)
2452 compl_T *match;
2453{
2454 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002455 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002456 int had_match;
2457
2458 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002459 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002460 /* First match, use it as a whole. */
2461 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002462 if (compl_leader != NULL)
2463 {
2464 had_match = (curwin->w_cursor.col > compl_col);
2465 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002466 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002467 ins_redraw(FALSE);
2468
2469 /* When the match isn't there (to avoid matching itself) remove it
2470 * again after redrawing. */
2471 if (!had_match)
2472 ins_compl_delete();
2473 compl_used_match = FALSE;
2474 }
2475 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002476 else
2477 {
2478 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002479 p = compl_leader;
2480 s = match->cp_str;
2481 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002482 {
2483#ifdef FEAT_MBYTE
2484 if (has_mbyte)
2485 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002486 c1 = mb_ptr2char(p);
2487 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002488 }
2489 else
2490#endif
2491 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002492 c1 = *p;
2493 c2 = *s;
2494 }
2495 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2496 : (c1 != c2))
2497 break;
2498#ifdef FEAT_MBYTE
2499 if (has_mbyte)
2500 {
2501 mb_ptr_adv(p);
2502 mb_ptr_adv(s);
2503 }
2504 else
2505#endif
2506 {
2507 ++p;
2508 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002509 }
2510 }
2511
2512 if (*p != NUL)
2513 {
2514 /* Leader was shortened, need to change the inserted text. */
2515 *p = NUL;
2516 had_match = (curwin->w_cursor.col > compl_col);
2517 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002518 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002519 ins_redraw(FALSE);
2520
2521 /* When the match isn't there (to avoid matching itself) remove it
2522 * again after redrawing. */
2523 if (!had_match)
2524 ins_compl_delete();
2525 }
2526
2527 compl_used_match = FALSE;
2528 }
2529}
2530
2531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 * Add an array of matches to the list of matches.
2533 * Frees matches[].
2534 */
2535 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002536ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 int num_matches;
2538 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002539 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 int i;
2542 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002543 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar572cb562005-08-05 21:35:02 +00002545 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002546 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002547 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002549 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 FreeWild(num_matches, matches);
2551}
2552
2553/* Make the completion list cyclic.
2554 * Return the number of matches (excluding the original).
2555 */
2556 static int
2557ins_compl_make_cyclic()
2558{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002559 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 int count = 0;
2561
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002562 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 /*
2565 * Find the end of the list.
2566 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002567 match = compl_first_match;
2568 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002571 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 ++count;
2573 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002574 match->cp_next = compl_first_match;
2575 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 return count;
2578}
2579
Bram Moolenaara94bc432006-03-10 21:42:59 +00002580/*
2581 * Start completion for the complete() function.
2582 * "startcol" is where the matched text starts (1 is first column).
2583 * "list" is the list of matches.
2584 */
2585 void
2586set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002587 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002588 list_T *list;
2589{
2590 /* If already doing completions stop it. */
2591 if (ctrl_x_mode != 0)
2592 ins_compl_prep(' ');
2593 ins_compl_clear();
2594
2595 if (stop_arrow() == FAIL)
2596 return;
2597
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002598 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002599 startcol = curwin->w_cursor.col;
2600 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002601 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002602 /* compl_pattern doesn't need to be set */
2603 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2604 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002605 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002606 return;
2607
2608 /* Handle like dictionary completion. */
2609 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2610
2611 ins_compl_add_list(list);
2612 compl_matches = ins_compl_make_cyclic();
2613 compl_started = TRUE;
2614 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002615 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002616
2617 compl_curr_match = compl_first_match;
2618 ins_complete(Ctrl_N);
2619 out_flush();
2620}
2621
2622
Bram Moolenaar9372a112005-12-06 19:59:18 +00002623/* "compl_match_array" points the currently displayed list of entries in the
2624 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002625static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002626static int compl_match_arraysize;
2627
2628/*
2629 * Update the screen and when there is any scrolling remove the popup menu.
2630 */
2631 static void
2632ins_compl_upd_pum()
2633{
2634 int h;
2635
2636 if (compl_match_array != NULL)
2637 {
2638 h = curwin->w_cline_height;
2639 update_screen(0);
2640 if (h != curwin->w_cline_height)
2641 ins_compl_del_pum();
2642 }
2643}
2644
2645/*
2646 * Remove any popup menu.
2647 */
2648 static void
2649ins_compl_del_pum()
2650{
2651 if (compl_match_array != NULL)
2652 {
2653 pum_undisplay();
2654 vim_free(compl_match_array);
2655 compl_match_array = NULL;
2656 }
2657}
2658
2659/*
2660 * Return TRUE if the popup menu should be displayed.
2661 */
2662 static int
2663pum_wanted()
2664{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002665 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002666 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002667 return FALSE;
2668
2669 /* The display looks bad on a B&W display. */
2670 if (t_colors < 8
2671#ifdef FEAT_GUI
2672 && !gui.in_use
2673#endif
2674 )
2675 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002676 return TRUE;
2677}
2678
2679/*
2680 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002681 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002682 */
2683 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002684pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002685{
2686 compl_T *compl;
2687 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002688
2689 /* Don't display the popup menu if there are no matches or there is only
2690 * one (ignoring the original text). */
2691 compl = compl_first_match;
2692 i = 0;
2693 do
2694 {
2695 if (compl == NULL
2696 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2697 break;
2698 compl = compl->cp_next;
2699 } while (compl != compl_first_match);
2700
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002701 if (strstr((char *)p_cot, "menuone") != NULL)
2702 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002703 return (i >= 2);
2704}
2705
2706/*
2707 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002708 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002709 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002710 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002711ins_compl_show_pum()
2712{
2713 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002714 compl_T *shown_compl = NULL;
2715 int did_find_shown_match = FALSE;
2716 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002717 int i;
2718 int cur = -1;
2719 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002720 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002721
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002722 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002723 return;
2724
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002725#if defined(FEAT_EVAL)
2726 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2727 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2728#endif
2729
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002730 /* Update the screen before drawing the popup menu over it. */
2731 update_screen(0);
2732
2733 if (compl_match_array == NULL)
2734 {
2735 /* Need to build the popup menu list. */
2736 compl_match_arraysize = 0;
2737 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002738 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002739 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002740 do
2741 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002742 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2743 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002744 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002745 ++compl_match_arraysize;
2746 compl = compl->cp_next;
2747 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002748 if (compl_match_arraysize == 0)
2749 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002750 compl_match_array = (pumitem_T *)alloc_clear(
2751 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002752 * compl_match_arraysize));
2753 if (compl_match_array != NULL)
2754 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002755 /* If the current match is the original text don't find the first
2756 * match after it, don't highlight anything. */
2757 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2758 shown_match_ok = TRUE;
2759
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002760 i = 0;
2761 compl = compl_first_match;
2762 do
2763 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002764 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2765 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002766 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002767 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002768 if (!shown_match_ok)
2769 {
2770 if (compl == compl_shown_match || did_find_shown_match)
2771 {
2772 /* This item is the shown match or this is the
2773 * first displayed item after the shown match. */
2774 compl_shown_match = compl;
2775 did_find_shown_match = TRUE;
2776 shown_match_ok = TRUE;
2777 }
2778 else
2779 /* Remember this displayed match for when the
2780 * shown match is just below it. */
2781 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002782 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002783 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002784
2785 if (compl->cp_text[CPT_ABBR] != NULL)
2786 compl_match_array[i].pum_text =
2787 compl->cp_text[CPT_ABBR];
2788 else
2789 compl_match_array[i].pum_text = compl->cp_str;
2790 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2791 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2792 if (compl->cp_text[CPT_MENU] != NULL)
2793 compl_match_array[i++].pum_extra =
2794 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002795 else
2796 compl_match_array[i++].pum_extra = compl->cp_fname;
2797 }
2798
2799 if (compl == compl_shown_match)
2800 {
2801 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002802
2803 /* When the original text is the shown match don't set
2804 * compl_shown_match. */
2805 if (compl->cp_flags & ORIGINAL_TEXT)
2806 shown_match_ok = TRUE;
2807
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002808 if (!shown_match_ok && shown_compl != NULL)
2809 {
2810 /* The shown match isn't displayed, set it to the
2811 * previously displayed match. */
2812 compl_shown_match = shown_compl;
2813 shown_match_ok = TRUE;
2814 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002815 }
2816 compl = compl->cp_next;
2817 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002818
2819 if (!shown_match_ok) /* no displayed match at all */
2820 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002821 }
2822 }
2823 else
2824 {
2825 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002826 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002827 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2828 || compl_match_array[i].pum_text
2829 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002830 {
2831 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002832 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002833 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002834 }
2835
2836 if (compl_match_array != NULL)
2837 {
2838 /* Compute the screen column of the start of the completed text.
2839 * Use the cursor to get all wrapping and other settings right. */
2840 col = curwin->w_cursor.col;
2841 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002842 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002843 curwin->w_cursor.col = col;
2844 }
2845}
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847#define DICT_FIRST (1) /* use just first element in "dict" */
2848#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002851 * Add any identifiers that match the given pattern in the list of dictionary
2852 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 */
2854 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002855ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2856 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002858 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002859 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002861 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 char_u *ptr;
2863 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 char_u **files;
2866 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002868 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar0b238792006-03-02 22:49:12 +00002870 if (*dict == NUL)
2871 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002872#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002873 /* When 'dictionary' is empty and spell checking is enabled use
2874 * "spell". */
2875 if (!thesaurus && curwin->w_p_spell)
2876 dict = (char_u *)"spell";
2877 else
2878#endif
2879 return;
2880 }
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002883 if (buf == NULL)
2884 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002885 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 /* If 'infercase' is set, don't use 'smartcase' here */
2888 save_p_scs = p_scs;
2889 if (curbuf->b_p_inf)
2890 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002891
2892 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2893 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002894 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002895 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2896 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002897 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002898 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002899
2900 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002901 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002902 len = STRLEN(pat_esc) + 10;
2903 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002904 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002905 {
2906 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002907 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002908 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002909 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002910 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2911 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002912 vim_free(ptr);
2913 }
2914 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002915 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002916 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002917 if (regmatch.regprog == NULL)
2918 goto theend;
2919 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2922 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002923 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925 /* copy one dictionary file name into buf */
2926 if (flags == DICT_EXACT)
2927 {
2928 count = 1;
2929 files = &dict;
2930 }
2931 else
2932 {
2933 /* Expand wildcards in the dictionary name, but do not allow
2934 * backticks (for security, the 'dict' option may have been set in
2935 * a modeline). */
2936 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002937# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002938 if (!thesaurus && STRCMP(buf, "spell") == 0)
2939 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002940 else
2941# endif
2942 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 || expand_wildcards(1, &buf, &count, &files,
2944 EW_FILE|EW_SILENT) != OK)
2945 count = 0;
2946 }
2947
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002948# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002949 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002951 /* Complete from active spelling. Skip "\<" in the pattern, we
2952 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002953 if (pat[0] == '\\' && pat[1] == '<')
2954 ptr = pat + 2;
2955 else
2956 ptr = pat;
2957 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002959 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002960# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002961 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002962 {
2963 ins_compl_files(count, files, thesaurus, flags,
2964 &regmatch, buf, &dir);
2965 if (flags != DICT_EXACT)
2966 FreeWild(count, files);
2967 }
2968 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 break;
2970 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002971
2972theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 p_scs = save_p_scs;
2974 vim_free(regmatch.regprog);
2975 vim_free(buf);
2976}
2977
Bram Moolenaar0b238792006-03-02 22:49:12 +00002978 static void
2979ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2980 int count;
2981 char_u **files;
2982 int thesaurus;
2983 int flags;
2984 regmatch_T *regmatch;
2985 char_u *buf;
2986 int *dir;
2987{
2988 char_u *ptr;
2989 int i;
2990 FILE *fp;
2991 int add_r;
2992
2993 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2994 {
2995 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2996 if (flags != DICT_EXACT)
2997 {
2998 vim_snprintf((char *)IObuff, IOSIZE,
2999 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003000 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003001 }
3002
3003 if (fp != NULL)
3004 {
3005 /*
3006 * Read dictionary file line by line.
3007 * Check each line for a match.
3008 */
3009 while (!got_int && !compl_interrupted
3010 && !vim_fgets(buf, LSIZE, fp))
3011 {
3012 ptr = buf;
3013 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3014 {
3015 ptr = regmatch->startp[0];
3016 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3017 ptr = find_line_end(ptr);
3018 else
3019 ptr = find_word_end(ptr);
3020 add_r = ins_compl_add_infercase(regmatch->startp[0],
3021 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003022 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003023 if (thesaurus)
3024 {
3025 char_u *wstart;
3026
3027 /*
3028 * Add the other matches on the line
3029 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003030 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003031 while (!got_int)
3032 {
3033 /* Find start of the next word. Skip white
3034 * space and punctuation. */
3035 ptr = find_word_start(ptr);
3036 if (*ptr == NUL || *ptr == NL)
3037 break;
3038 wstart = ptr;
3039
Bram Moolenaara2993e12007-08-12 14:38:46 +00003040 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003041#ifdef FEAT_MBYTE
3042 if (has_mbyte)
3043 /* Japanese words may have characters in
3044 * different classes, only separate words
3045 * with single-byte non-word characters. */
3046 while (*ptr != NUL)
3047 {
3048 int l = (*mb_ptr2len)(ptr);
3049
3050 if (l < 2 && !vim_iswordc(*ptr))
3051 break;
3052 ptr += l;
3053 }
3054 else
3055#endif
3056 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003057
3058 /* Add the word. Skip the regexp match. */
3059 if (wstart != regmatch->startp[0])
3060 add_r = ins_compl_add_infercase(wstart,
3061 (int)(ptr - wstart),
3062 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003063 }
3064 }
3065 if (add_r == OK)
3066 /* if dir was BACKWARD then honor it just once */
3067 *dir = FORWARD;
3068 else if (add_r == FAIL)
3069 break;
3070 /* avoid expensive call to vim_regexec() when at end
3071 * of line */
3072 if (*ptr == '\n' || got_int)
3073 break;
3074 }
3075 line_breakcheck();
3076 ins_compl_check_keys(50);
3077 }
3078 fclose(fp);
3079 }
3080 }
3081}
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083/*
3084 * Find the start of the next word.
3085 * Returns a pointer to the first char of the word. Also stops at a NUL.
3086 */
3087 char_u *
3088find_word_start(ptr)
3089 char_u *ptr;
3090{
3091#ifdef FEAT_MBYTE
3092 if (has_mbyte)
3093 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003094 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 else
3096#endif
3097 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3098 ++ptr;
3099 return ptr;
3100}
3101
3102/*
3103 * Find the end of the word. Assumes it starts inside a word.
3104 * Returns a pointer to just after the word.
3105 */
3106 char_u *
3107find_word_end(ptr)
3108 char_u *ptr;
3109{
3110#ifdef FEAT_MBYTE
3111 int start_class;
3112
3113 if (has_mbyte)
3114 {
3115 start_class = mb_get_class(ptr);
3116 if (start_class > 1)
3117 while (*ptr != NUL)
3118 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003119 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if (mb_get_class(ptr) != start_class)
3121 break;
3122 }
3123 }
3124 else
3125#endif
3126 while (vim_iswordc(*ptr))
3127 ++ptr;
3128 return ptr;
3129}
3130
3131/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003132 * Find the end of the line, omitting CR and NL at the end.
3133 * Returns a pointer to just after the line.
3134 */
3135 static char_u *
3136find_line_end(ptr)
3137 char_u *ptr;
3138{
3139 char_u *s;
3140
3141 s = ptr + STRLEN(ptr);
3142 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3143 --s;
3144 return s;
3145}
3146
3147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * Free the list of completions
3149 */
3150 static void
3151ins_compl_free()
3152{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003153 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003154 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003156 vim_free(compl_pattern);
3157 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003158 vim_free(compl_leader);
3159 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003161 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003163
3164 ins_compl_del_pum();
3165 pum_clear();
3166
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003167 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 do
3169 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003170 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003171 compl_curr_match = compl_curr_match->cp_next;
3172 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003174 if (match->cp_flags & FREE_FNAME)
3175 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003176 for (i = 0; i < CPT_COUNT; ++i)
3177 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003179 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3180 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181}
3182
3183 static void
3184ins_compl_clear()
3185{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003186 compl_cont_status = 0;
3187 compl_started = FALSE;
3188 compl_matches = 0;
3189 vim_free(compl_pattern);
3190 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003191 vim_free(compl_leader);
3192 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003194 vim_free(compl_orig_text);
3195 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003196 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197}
3198
3199/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003200 * Return TRUE when Insert completion is active.
3201 */
3202 int
3203ins_compl_active()
3204{
3205 return compl_started;
3206}
3207
3208/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003209 * Delete one character before the cursor and show the subset of the matches
3210 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003211 * Returns the character to be used, NUL if the work is done and another char
3212 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003213 */
3214 static int
3215ins_compl_bs()
3216{
3217 char_u *line;
3218 char_u *p;
3219
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003220 line = ml_get_curline();
3221 p = line + curwin->w_cursor.col;
3222 mb_ptr_back(line, p);
3223
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003224 /* Stop completion when the whole word was deleted. For Omni completion
3225 * allow the word to be deleted, we won't match everything. */
3226 if ((int)(p - line) - (int)compl_col < 0
3227 || ((int)(p - line) - (int)compl_col == 0
3228 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003229 return K_BS;
3230
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003231 /* Deleted more than what was used to find matches or didn't finish
3232 * finding all matches: need to look for matches all over again. */
3233 if (curwin->w_cursor.col <= compl_col + compl_length
3234 || compl_was_interrupted)
3235 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003236
Bram Moolenaara6557602006-02-04 22:43:20 +00003237 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003238 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003239 if (compl_leader != NULL)
3240 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003241 ins_compl_new_leader();
3242 return NUL;
3243 }
3244 return K_BS;
3245}
Bram Moolenaara6557602006-02-04 22:43:20 +00003246
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003247/*
3248 * Called after changing "compl_leader".
3249 * Show the popup menu with a different set of matches.
3250 * May also search for matches again if the previous search was interrupted.
3251 */
3252 static void
3253ins_compl_new_leader()
3254{
3255 ins_compl_del_pum();
3256 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003257 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003258 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003259
3260 if (compl_started)
3261 ins_compl_set_original_text(compl_leader);
3262 else
3263 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003264#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003265 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003266#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003267 /*
3268 * Matches were cleared, need to search for them now. First display
3269 * the changed text before the cursor. Set "compl_restarting" to
3270 * avoid that the first match is inserted.
3271 */
3272 update_screen(0);
3273#ifdef FEAT_GUI
3274 if (gui.in_use)
3275 {
3276 /* Show the cursor after the match, not after the redrawn text. */
3277 setcursor();
3278 out_flush();
3279 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003280 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003281#endif
3282 compl_restarting = TRUE;
3283 if (ins_complete(Ctrl_N) == FAIL)
3284 compl_cont_status = 0;
3285 compl_restarting = FALSE;
3286 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003287
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003288#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003289 if (!compl_used_match)
3290 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003291 /* Go to the original text, since none of the matches is inserted. */
3292 if (compl_first_match->cp_prev != NULL
3293 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3294 compl_shown_match = compl_first_match->cp_prev;
3295 else
3296 compl_shown_match = compl_first_match;
3297 compl_curr_match = compl_shown_match;
3298 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003299 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003300#endif
3301 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003302
3303 /* Show the popup menu with a different set of matches. */
3304 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003305
3306 /* Don't let Enter select the original text when there is no popup menu. */
3307 if (compl_match_array == NULL)
3308 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003309}
3310
3311/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003312 * Return the length of the completion, from the completion start column to
3313 * the cursor column. Making sure it never goes below zero.
3314 */
3315 static int
3316ins_compl_len()
3317{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003318 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003319
3320 if (off < 0)
3321 return 0;
3322 return off;
3323}
3324
3325/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003326 * Append one character to the match leader. May reduce the number of
3327 * matches.
3328 */
3329 static void
3330ins_compl_addleader(c)
3331 int c;
3332{
3333#ifdef FEAT_MBYTE
3334 int cc;
3335
3336 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3337 {
3338 char_u buf[MB_MAXBYTES + 1];
3339
3340 (*mb_char2bytes)(c, buf);
3341 buf[cc] = NUL;
3342 ins_char_bytes(buf, cc);
3343 }
3344 else
3345#endif
3346 ins_char(c);
3347
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003348 /* If we didn't complete finding matches we must search again. */
3349 if (compl_was_interrupted)
3350 ins_compl_restart();
3351
Bram Moolenaara6557602006-02-04 22:43:20 +00003352 vim_free(compl_leader);
3353 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003354 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003355 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003356 ins_compl_new_leader();
3357}
3358
3359/*
3360 * Setup for finding completions again without leaving CTRL-X mode. Used when
3361 * BS or a key was typed while still searching for matches.
3362 */
3363 static void
3364ins_compl_restart()
3365{
3366 ins_compl_free();
3367 compl_started = FALSE;
3368 compl_matches = 0;
3369 compl_cont_status = 0;
3370 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003371}
3372
3373/*
3374 * Set the first match, the original text.
3375 */
3376 static void
3377ins_compl_set_original_text(str)
3378 char_u *str;
3379{
3380 char_u *p;
3381
3382 /* Replace the original text entry. */
3383 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3384 {
3385 p = vim_strsave(str);
3386 if (p != NULL)
3387 {
3388 vim_free(compl_first_match->cp_str);
3389 compl_first_match->cp_str = p;
3390 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003391 }
3392}
3393
3394/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003395 * Append one character to the match leader. May reduce the number of
3396 * matches.
3397 */
3398 static void
3399ins_compl_addfrommatch()
3400{
3401 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003402 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003403 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003404 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003405
3406 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003407 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003408 {
3409 /* When still at the original match use the first entry that matches
3410 * the leader. */
3411 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3412 {
3413 p = NULL;
3414 for (cp = compl_shown_match->cp_next; cp != NULL
3415 && cp != compl_first_match; cp = cp->cp_next)
3416 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003417 if (compl_leader == NULL
3418 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003419 (int)STRLEN(compl_leader)))
3420 {
3421 p = cp->cp_str;
3422 break;
3423 }
3424 }
3425 if (p == NULL || (int)STRLEN(p) <= len)
3426 return;
3427 }
3428 else
3429 return;
3430 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003431 p += len;
3432#ifdef FEAT_MBYTE
3433 c = mb_ptr2char(p);
3434#else
3435 c = *p;
3436#endif
3437 ins_compl_addleader(c);
3438}
3439
3440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003442 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003443 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003445 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446ins_compl_prep(c)
3447 int c;
3448{
3449 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003451 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
3453 /* Forget any previous 'special' messages if this is actually
3454 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3455 */
3456 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3457 edit_submode_extra = NULL;
3458
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003459 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3460 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003461 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003463 /* Set "compl_get_longest" when finding the first matches. */
3464 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3465 || (ctrl_x_mode == 0 && !compl_started))
3466 {
3467 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3468 compl_used_match = TRUE;
3469 }
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3472 {
3473 /*
3474 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3475 * it will be yet. Now we decide.
3476 */
3477 switch (c)
3478 {
3479 case Ctrl_E:
3480 case Ctrl_Y:
3481 ctrl_x_mode = CTRL_X_SCROLL;
3482 if (!(State & REPLACE_FLAG))
3483 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3484 else
3485 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3486 edit_submode_pre = NULL;
3487 showmode();
3488 break;
3489 case Ctrl_L:
3490 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3491 break;
3492 case Ctrl_F:
3493 ctrl_x_mode = CTRL_X_FILES;
3494 break;
3495 case Ctrl_K:
3496 ctrl_x_mode = CTRL_X_DICTIONARY;
3497 break;
3498 case Ctrl_R:
3499 /* Simply allow ^R to happen without affecting ^X mode */
3500 break;
3501 case Ctrl_T:
3502 ctrl_x_mode = CTRL_X_THESAURUS;
3503 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003504#ifdef FEAT_COMPL_FUNC
3505 case Ctrl_U:
3506 ctrl_x_mode = CTRL_X_FUNCTION;
3507 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003508 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003509 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003510 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003511#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003512 case 's':
3513 case Ctrl_S:
3514 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003515#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003516 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003517 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003518 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003519#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 case Ctrl_RSB:
3522 ctrl_x_mode = CTRL_X_TAGS;
3523 break;
3524#ifdef FEAT_FIND_ID
3525 case Ctrl_I:
3526 case K_S_TAB:
3527 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3528 break;
3529 case Ctrl_D:
3530 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3531 break;
3532#endif
3533 case Ctrl_V:
3534 case Ctrl_Q:
3535 ctrl_x_mode = CTRL_X_CMDLINE;
3536 break;
3537 case Ctrl_P:
3538 case Ctrl_N:
3539 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3540 * just started ^X mode, or there were enough ^X's to cancel
3541 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3542 * do normal expansion when interrupting a different mode (say
3543 * ^X^F^X^P or ^P^X^X^P, see below)
3544 * nothing changes if interrupting mode 0, (eg, the flag
3545 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003546 if (!(compl_cont_status & CONT_INTRPT))
3547 compl_cont_status |= CONT_LOCAL;
3548 else if (compl_cont_mode != 0)
3549 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /* FALLTHROUGH */
3551 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 /* If we have typed at least 2 ^X's... for modes != 0, we set
3553 * compl_cont_status = 0 (eg, as if we had just started ^X
3554 * mode).
3555 * For mode 0, we set "compl_cont_mode" to an impossible
3556 * value, in both cases ^X^X can be used to restart the same
3557 * mode (avoiding ADDING mode).
3558 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3559 * 'complete' and local ^P expansions respectively.
3560 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3561 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (c == Ctrl_X)
3563 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003564 if (compl_cont_mode != 0)
3565 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569 ctrl_x_mode = 0;
3570 edit_submode = NULL;
3571 showmode();
3572 break;
3573 }
3574 }
3575 else if (ctrl_x_mode != 0)
3576 {
3577 /* We're already in CTRL-X mode, do we stay in it? */
3578 if (!vim_is_ctrl_x_key(c))
3579 {
3580 if (ctrl_x_mode == CTRL_X_SCROLL)
3581 ctrl_x_mode = 0;
3582 else
3583 ctrl_x_mode = CTRL_X_FINISHED;
3584 edit_submode = NULL;
3585 }
3586 showmode();
3587 }
3588
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003589 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 /* Show error message from attempted keyword completion (probably
3592 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003593 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003595 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3596 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 || ctrl_x_mode == CTRL_X_FINISHED)
3598 {
3599 /* Get here when we have finished typing a sequence of ^N and
3600 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003601 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003602 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003604 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003605 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003608 * If any of the original typed text has been changed, eg when
3609 * ignorecase is set, we must add back-spaces to the redo
3610 * buffer. We add as few as necessary to delete just the part
3611 * of the original text that has changed.
3612 * When using the longest match, edited the match or used
3613 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003615 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3616 ptr = compl_curr_match->cp_str;
3617 else if (compl_leader != NULL)
3618 ptr = compl_leader;
3619 else
3620 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003621 if (compl_orig_text != NULL)
3622 {
3623 p = compl_orig_text;
3624 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3625 ++temp)
3626 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003627#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003628 if (temp > 0)
3629 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003630#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003631 for (p += temp; *p != NUL; mb_ptr_adv(p))
3632 AppendCharToRedobuff(K_BS);
3633 }
3634 if (ptr != NULL)
3635 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637
3638#ifdef FEAT_CINDENT
3639 want_cindent = (can_cindent && cindent_on());
3640#endif
3641 /*
3642 * When completing whole lines: fix indent for 'cindent'.
3643 * Otherwise, break line if it's too long.
3644 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647#ifdef FEAT_CINDENT
3648 /* re-indent the current line */
3649 if (want_cindent)
3650 {
3651 do_c_expr_indent();
3652 want_cindent = FALSE; /* don't do it again */
3653 }
3654#endif
3655 }
3656 else
3657 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003658 int prev_col = curwin->w_cursor.col;
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003661 if (prev_col > 0)
3662 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (stop_arrow() == OK)
3664 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003665 if (prev_col > 0
3666 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3667 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003670 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003671 * the selection without inserting anything. When
3672 * compl_enter_selects is set the Enter key does the same. */
3673 if ((c == Ctrl_Y || (compl_enter_selects
3674 && (c == CAR || c == K_KENTER || c == NL)))
3675 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003676 retval = TRUE;
3677
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003678 /* CTRL-E means completion is Ended, go back to the typed text. */
3679 if (c == Ctrl_E)
3680 {
3681 ins_compl_delete();
3682 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003683 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003684 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003685 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003686 retval = TRUE;
3687 }
3688
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003689 auto_format(FALSE, TRUE);
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 compl_started = FALSE;
3693 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 msg_clr_cmdline(); /* necessary for "noshowmode" */
3695 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003696 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (edit_submode != NULL)
3698 {
3699 edit_submode = NULL;
3700 showmode();
3701 }
3702
3703#ifdef FEAT_CINDENT
3704 /*
3705 * Indent now if a key was typed that is in 'cinkeys'.
3706 */
3707 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3708 do_c_expr_indent();
3709#endif
3710 }
3711 }
3712
3713 /* reset continue_* if we left expansion-mode, if we stay they'll be
3714 * (re)set properly in ins_complete() */
3715 if (!vim_is_ctrl_x_key(c))
3716 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003717 compl_cont_status = 0;
3718 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003720
3721 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722}
3723
3724/*
3725 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3726 * (depending on flag) starting from buf and looking for a non-scanned
3727 * buffer (other than curbuf). curbuf is special, if it is called with
3728 * buf=curbuf then it has to be the first call for a given flag/expansion.
3729 *
3730 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3731 */
3732 static buf_T *
3733ins_compl_next_buf(buf, flag)
3734 buf_T *buf;
3735 int flag;
3736{
3737#ifdef FEAT_WINDOWS
3738 static win_T *wp;
3739#endif
3740
3741 if (flag == 'w') /* just windows */
3742 {
3743#ifdef FEAT_WINDOWS
3744 if (buf == curbuf) /* first call for this flag/expansion */
3745 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003746 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 && wp->w_buffer->b_scanned)
3748 ;
3749 buf = wp->w_buffer;
3750#else
3751 buf = curbuf;
3752#endif
3753 }
3754 else
3755 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3756 * (unlisted buffers)
3757 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003758 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 && ((flag == 'U'
3760 ? buf->b_p_bl
3761 : (!buf->b_p_bl
3762 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 ;
3765 return buf;
3766}
3767
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003768#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003769static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003770
3771/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003772 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003773 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003774 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003775 static void
3776expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003777 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003778 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003779{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003780 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003781 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003782 char_u *funcname;
3783 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003784
Bram Moolenaare344bea2005-09-01 20:46:49 +00003785 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3786 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003787 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003788
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003789 /* Call 'completefunc' to obtain the list of matches. */
3790 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003791 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003792
Bram Moolenaare344bea2005-09-01 20:46:49 +00003793 pos = curwin->w_cursor;
3794 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3795 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003796 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003797 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003798
Bram Moolenaara94bc432006-03-10 21:42:59 +00003799 ins_compl_add_list(matchlist);
3800 list_unref(matchlist);
3801}
3802#endif /* FEAT_COMPL_FUNC */
3803
Bram Moolenaar39f05632006-03-19 22:15:26 +00003804#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003805/*
3806 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003807 */
3808 static void
3809ins_compl_add_list(list)
3810 list_T *list;
3811{
3812 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003813 int dir = compl_direction;
3814
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003815 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003816 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003817 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003818 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3819 /* if dir was BACKWARD then honor it just once */
3820 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003821 else if (did_emsg)
3822 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003823 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003824}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003825
3826/*
3827 * Add a match to the list of matches from a typeval_T.
3828 * If the given string is already in the list of completions, then return
3829 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3830 * maybe because alloc() returns NULL, then FAIL is returned.
3831 */
3832 int
3833ins_compl_add_tv(tv, dir)
3834 typval_T *tv;
3835 int dir;
3836{
3837 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003838 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003839 int adup = FALSE;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003840#ifdef S_SPLINT_S /* splint doesn't parse array of pointers correctly */
3841 char_u **cptext;
3842#else
Bram Moolenaar39f05632006-03-19 22:15:26 +00003843 char_u *(cptext[CPT_COUNT]);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003844#endif
Bram Moolenaar39f05632006-03-19 22:15:26 +00003845
3846 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3847 {
3848 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3849 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3850 (char_u *)"abbr", FALSE);
3851 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3852 (char_u *)"menu", FALSE);
3853 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3854 (char_u *)"kind", FALSE);
3855 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3856 (char_u *)"info", FALSE);
3857 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3858 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003859 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003860 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003861 }
3862 else
3863 {
3864 word = get_tv_string_chk(tv);
3865 vim_memset(cptext, 0, sizeof(cptext));
3866 }
3867 if (word == NULL || *word == NUL)
3868 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003869 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003870}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003871#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003872
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003873/*
3874 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003875 * The search starts at position "ini" in curbuf and in the direction
3876 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003877 * When "compl_started" is FALSE start at that position, otherwise continue
3878 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003879 * This may return before finding all the matches.
3880 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 */
3882 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003883ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
3886 static pos_T first_match_pos;
3887 static pos_T last_match_pos;
3888 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003889 static int found_all = FALSE; /* Found all matches of a
3890 certain type. */
3891 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
Bram Moolenaar572cb562005-08-05 21:35:02 +00003893 pos_T *pos;
3894 char_u **matches;
3895 int save_p_scs;
3896 int save_p_ws;
3897 int save_p_ic;
3898 int i;
3899 int num_matches;
3900 int len;
3901 int found_new_match;
3902 int type = ctrl_x_mode;
3903 char_u *ptr;
3904 char_u *dict = NULL;
3905 int dict_f = 0;
3906 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
3910 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3911 ins_buf->b_scanned = 0;
3912 found_all = FALSE;
3913 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003914 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003915 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 last_match_pos = first_match_pos = *ini;
3917 }
3918
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003920 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3922 for (;;)
3923 {
3924 found_new_match = FAIL;
3925
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003926 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * or if found_all says this entry is done. For ^X^L only use the
3928 * entries from 'complete' that look in loaded buffers. */
3929 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003930 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
3932 found_all = FALSE;
3933 while (*e_cpt == ',' || *e_cpt == ' ')
3934 e_cpt++;
3935 if (*e_cpt == '.' && !curbuf->b_scanned)
3936 {
3937 ins_buf = curbuf;
3938 first_match_pos = *ini;
3939 /* So that ^N can match word immediately after cursor */
3940 if (ctrl_x_mode == 0)
3941 dec(&first_match_pos);
3942 last_match_pos = first_match_pos;
3943 type = 0;
3944 }
3945 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3946 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3947 {
3948 /* Scan a buffer, but not the current one. */
3949 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3950 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 first_match_pos.col = last_match_pos.col = 0;
3953 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3954 last_match_pos.lnum = 0;
3955 type = 0;
3956 }
3957 else /* unloaded buffer, scan like dictionary */
3958 {
3959 found_all = TRUE;
3960 if (ins_buf->b_fname == NULL)
3961 continue;
3962 type = CTRL_X_DICTIONARY;
3963 dict = ins_buf->b_fname;
3964 dict_f = DICT_EXACT;
3965 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003966 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 ins_buf->b_fname == NULL
3968 ? buf_spname(ins_buf)
3969 : ins_buf->b_sfname == NULL
3970 ? (char *)ins_buf->b_fname
3971 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003972 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 else if (*e_cpt == NUL)
3975 break;
3976 else
3977 {
3978 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3979 type = -1;
3980 else if (*e_cpt == 'k' || *e_cpt == 's')
3981 {
3982 if (*e_cpt == 'k')
3983 type = CTRL_X_DICTIONARY;
3984 else
3985 type = CTRL_X_THESAURUS;
3986 if (*++e_cpt != ',' && *e_cpt != NUL)
3987 {
3988 dict = e_cpt;
3989 dict_f = DICT_FIRST;
3990 }
3991 }
3992#ifdef FEAT_FIND_ID
3993 else if (*e_cpt == 'i')
3994 type = CTRL_X_PATH_PATTERNS;
3995 else if (*e_cpt == 'd')
3996 type = CTRL_X_PATH_DEFINES;
3997#endif
3998 else if (*e_cpt == ']' || *e_cpt == 't')
3999 {
4000 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004001 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004002 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004 else
4005 type = -1;
4006
4007 /* in any case e_cpt is advanced to the next entry */
4008 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4009
4010 found_all = TRUE;
4011 if (type == -1)
4012 continue;
4013 }
4014 }
4015
4016 switch (type)
4017 {
4018 case -1:
4019 break;
4020#ifdef FEAT_FIND_ID
4021 case CTRL_X_PATH_PATTERNS:
4022 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004023 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004026 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4028 (linenr_T)1, (linenr_T)MAXLNUM);
4029 break;
4030#endif
4031
4032 case CTRL_X_DICTIONARY:
4033 case CTRL_X_THESAURUS:
4034 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004035 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 : (type == CTRL_X_THESAURUS
4037 ? (*curbuf->b_p_tsr == NUL
4038 ? p_tsr
4039 : curbuf->b_p_tsr)
4040 : (*curbuf->b_p_dict == NUL
4041 ? p_dict
4042 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004043 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004044 dict != NULL ? dict_f
4045 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 dict = NULL;
4047 break;
4048
4049 case CTRL_X_TAGS:
4050 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4051 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004052 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055 * of matches is found when compl_pattern is empty */
4056 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4058 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4059 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4060 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004061 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 p_ic = save_p_ic;
4064 break;
4065
4066 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004067 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4069 {
4070
4071 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004073 ins_compl_add_matches(num_matches, matches,
4074#ifdef CASE_INSENSITIVE_FILENAME
4075 TRUE
4076#else
4077 FALSE
4078#endif
4079 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081 break;
4082
4083 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004084 if (expand_cmdline(&compl_xp, compl_pattern,
4085 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004087 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 break;
4089
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004090#ifdef FEAT_COMPL_FUNC
4091 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004092 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004093 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004094 break;
4095#endif
4096
Bram Moolenaar488c6512005-08-11 20:09:58 +00004097 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004098#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004099 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004100 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004101 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004102 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004103#endif
4104 break;
4105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 default: /* normal ^P/^N and ^X^L */
4107 /*
4108 * If 'infercase' is set, don't use 'smartcase' here
4109 */
4110 save_p_scs = p_scs;
4111 if (ins_buf->b_p_inf)
4112 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004113
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /* buffers other than curbuf are scanned from the beginning or the
4115 * end but never from the middle, thus setting nowrapscan in this
4116 * buffers is a good idea, on the other hand, we always set
4117 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4118 save_p_ws = p_ws;
4119 if (ins_buf != curbuf)
4120 p_ws = FALSE;
4121 else if (*e_cpt == '.')
4122 p_ws = TRUE;
4123 for (;;)
4124 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004125 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004127 ++msg_silent; /* Don't want messages for wrapscan. */
4128
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004129 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4130 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004132 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004134 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004136 found_new_match = searchit(NULL, ins_buf, pos,
4137 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004138 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004139 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004140 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004143 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 first_match_pos = *pos;
4146 last_match_pos = *pos;
4147 }
4148 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004149 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 found_new_match = FAIL;
4151 if (found_new_match == FAIL)
4152 {
4153 if (ins_buf == curbuf)
4154 found_all = TRUE;
4155 break;
4156 }
4157
4158 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004159 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 && ini->lnum == pos->lnum
4161 && ini->col == pos->col)
4162 continue;
4163 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4164 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4165 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004166 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
4168 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4169 continue;
4170 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4171 if (!p_paste)
4172 ptr = skipwhite(ptr);
4173 }
4174 len = (int)STRLEN(ptr);
4175 }
4176 else
4177 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004178 char_u *tmp_ptr = ptr;
4179
4180 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 /* Skip if already inside a word. */
4184 if (vim_iswordp(tmp_ptr))
4185 continue;
4186 /* Find start of next word. */
4187 tmp_ptr = find_word_start(tmp_ptr);
4188 }
4189 /* Find end of this word. */
4190 tmp_ptr = find_word_end(tmp_ptr);
4191 len = (int)(tmp_ptr - ptr);
4192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 if ((compl_cont_status & CONT_ADDING)
4194 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
4196 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4197 {
4198 /* Try next line, if any. the new word will be
4199 * "join" as if the normal command "J" was used.
4200 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004201 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 * works -- Acevedo */
4203 STRNCPY(IObuff, ptr, len);
4204 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4205 tmp_ptr = ptr = skipwhite(ptr);
4206 /* Find start of next word. */
4207 tmp_ptr = find_word_start(tmp_ptr);
4208 /* Find end of next word. */
4209 tmp_ptr = find_word_end(tmp_ptr);
4210 if (tmp_ptr > ptr)
4211 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004212 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004214 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 IObuff[len++] = ' ';
4216 /* IObuf =~ "\k.* ", thus len >= 2 */
4217 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004218 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 || (vim_strchr(p_cpo, CPO_JOINSP)
4220 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004221 && (IObuff[len - 2] == '?'
4222 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 IObuff[len++] = ' ';
4224 }
4225 /* copy as much as posible of the new word */
4226 if (tmp_ptr - ptr >= IOSIZE - len)
4227 tmp_ptr = ptr + IOSIZE - len - 1;
4228 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4229 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004230 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 IObuff[len] = NUL;
4233 ptr = IObuff;
4234 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 continue;
4237 }
4238 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004239 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004240 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004241 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 found_new_match = OK;
4244 break;
4245 }
4246 }
4247 p_scs = save_p_scs;
4248 p_ws = save_p_ws;
4249 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004250
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004252 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004253 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 found_new_match = OK;
4255
4256 /* break the loop for specialized modes (use 'complete' just for the
4257 * generic ctrl_x_mode == 0) or when we've found a new match */
4258 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004260 {
4261 if (got_int)
4262 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004263 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004264 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004265 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004266
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004267 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4268 || compl_interrupted)
4269 break;
4270 compl_started = TRUE;
4271 }
4272 else
4273 {
4274 /* Mark a buffer scanned when it has been scanned completely */
4275 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4276 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004278 compl_started = FALSE;
4279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004281 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4284 && *e_cpt == NUL) /* Got to end of 'complete' */
4285 found_new_match = FAIL;
4286
4287 i = -1; /* total of matches, unknown */
4288 if (found_new_match == FAIL
4289 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4290 i = ins_compl_make_cyclic();
4291
4292 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004293 * just been made cyclic then we have to move compl_curr_match to the next
4294 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004295 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4296 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 if (compl_curr_match == NULL)
4298 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return i;
4300}
4301
4302/* Delete the old text being completed. */
4303 static void
4304ins_compl_delete()
4305{
4306 int i;
4307
4308 /*
4309 * In insert mode: Delete the typed part.
4310 * In replace mode: Put the old characters back, if any.
4311 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004312 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 backspace_until_column(i);
4314 changed_cline_bef_curs();
4315}
4316
4317/* Insert the new text being completed. */
4318 static void
4319ins_compl_insert()
4320{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004321 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004322 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4323 compl_used_match = FALSE;
4324 else
4325 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326}
4327
4328/*
4329 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004330 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4331 * get more completions. If it is FALSE, then we just do nothing when there
4332 * are no more completions in a given direction. The latter case is used when
4333 * we are still in the middle of finding completions, to allow browsing
4334 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 * Return the total number of matches, or -1 if still unknown -- webb.
4336 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4338 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 *
4340 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004341 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4342 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 */
4344 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004345ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004347 int count; /* repeat completion this many times; should
4348 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004349 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351 int num_matches = -1;
4352 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004353 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004354 compl_T *found_compl = NULL;
4355 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004356 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004358 if (compl_leader != NULL
4359 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004361 /* Set "compl_shown_match" to the actually shown match, it may differ
4362 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004363 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004364 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004365 && compl_shown_match->cp_next != NULL
4366 && compl_shown_match->cp_next != compl_first_match)
4367 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004368
4369 /* If we didn't find it searching forward, and compl_shows_dir is
4370 * backward, find the last match. */
4371 if (compl_shows_dir == BACKWARD
4372 && !ins_compl_equal(compl_shown_match,
4373 compl_leader, (int)STRLEN(compl_leader))
4374 && (compl_shown_match->cp_next == NULL
4375 || compl_shown_match->cp_next == compl_first_match))
4376 {
4377 while (!ins_compl_equal(compl_shown_match,
4378 compl_leader, (int)STRLEN(compl_leader))
4379 && compl_shown_match->cp_prev != NULL
4380 && compl_shown_match->cp_prev != compl_first_match)
4381 compl_shown_match = compl_shown_match->cp_prev;
4382 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004383 }
4384
4385 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004386 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 /* Delete old text to be replaced */
4388 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004389
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004390 /* When finding the longest common text we stick at the original text,
4391 * don't let CTRL-N or CTRL-P move to the first match. */
4392 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4393
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004394 /* When restarting the search don't insert the first match either. */
4395 if (compl_restarting)
4396 {
4397 advance = FALSE;
4398 compl_restarting = FALSE;
4399 }
4400
Bram Moolenaare3226be2005-12-18 22:10:00 +00004401 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4402 * around. */
4403 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004405 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004407 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004408 found_end = (compl_first_match != NULL
4409 && (compl_shown_match->cp_next == compl_first_match
4410 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004411 }
4412 else if (compl_shows_dir == BACKWARD
4413 && compl_shown_match->cp_prev != NULL)
4414 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004415 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004416 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004417 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004420 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004421 if (!allow_get_expansion)
4422 {
4423 if (advance)
4424 {
4425 if (compl_shows_dir == BACKWARD)
4426 compl_pending -= todo + 1;
4427 else
4428 compl_pending += todo + 1;
4429 }
4430 return -1;
4431 }
4432
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004433 if (advance)
4434 {
4435 if (compl_shows_dir == BACKWARD)
4436 --compl_pending;
4437 else
4438 ++compl_pending;
4439 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004440
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004441 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004442 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004443
4444 /* handle any pending completions */
4445 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004446 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004447 {
4448 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4449 {
4450 compl_shown_match = compl_shown_match->cp_next;
4451 --compl_pending;
4452 }
4453 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4454 {
4455 compl_shown_match = compl_shown_match->cp_prev;
4456 ++compl_pending;
4457 }
4458 else
4459 break;
4460 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004461 found_end = FALSE;
4462 }
4463 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4464 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004465 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004466 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004467 ++todo;
4468 else
4469 /* Remember a matching item. */
4470 found_compl = compl_shown_match;
4471
4472 /* Stop at the end of the list when we found a usable match. */
4473 if (found_end)
4474 {
4475 if (found_compl != NULL)
4476 {
4477 compl_shown_match = found_compl;
4478 break;
4479 }
4480 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004484 /* Insert the text of the new completion, or the compl_leader. */
4485 if (insert_match)
4486 {
4487 if (!compl_get_longest || compl_used_match)
4488 ins_compl_insert();
4489 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004490 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004491 }
4492 else
4493 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
4495 if (!allow_get_expansion)
4496 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004497 /* may undisplay the popup menu first */
4498 ins_compl_upd_pum();
4499
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004500 /* redraw to show the user what was inserted */
4501 update_screen(0);
4502
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004503 /* display the updated popup menu */
4504 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004505#ifdef FEAT_GUI
4506 if (gui.in_use)
4507 {
4508 /* Show the cursor after the match, not after the redrawn text. */
4509 setcursor();
4510 out_flush();
4511 gui_update_cursor(FALSE, FALSE);
4512 }
4513#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /* Delete old text to be replaced, since we're still searching and
4516 * don't want to match ourselves! */
4517 ins_compl_delete();
4518 }
4519
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004520 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004521 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004522 compl_enter_selects = !insert_match && compl_match_array != NULL;
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /*
4525 * Show the file name for the match (if any)
4526 * Truncate the file name to avoid a wait for return.
4527 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004528 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004531 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 if (i <= 0)
4533 i = 0;
4534 else
4535 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004536 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 msg(IObuff);
4538 redraw_cmdline = FALSE; /* don't overwrite! */
4539 }
4540
4541 return num_matches;
4542}
4543
4544/*
4545 * Call this while finding completions, to check whether the user has hit a key
4546 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004547 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004549 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 */
4551 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004552ins_compl_check_keys(frequency)
4553 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
4555 static int count = 0;
4556
4557 int c;
4558
4559 /* Don't check when reading keys from a script. That would break the test
4560 * scripts */
4561 if (using_script())
4562 return;
4563
4564 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004565 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 return;
4567 count = 0;
4568
Bram Moolenaara260a972006-06-23 19:36:29 +00004569 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4570 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (c != NUL)
4573 {
4574 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4575 {
4576 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004577 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004578 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4579 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004581 else
4582 {
4583 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004584 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004585 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004586 if (c != K_IGNORE)
4587 {
4588 /* Don't interrupt completion when the character wasn't typed,
4589 * e.g., when doing @q to replay keys. */
4590 if (c != Ctrl_R && KeyTyped)
4591 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004592
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004593 vungetc(c);
4594 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004597 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004598 {
4599 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4600
4601 compl_pending = 0;
4602 (void)ins_compl_next(FALSE, todo, TRUE);
4603 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004604}
4605
4606/*
4607 * Decide the direction of Insert mode complete from the key typed.
4608 * Returns BACKWARD or FORWARD.
4609 */
4610 static int
4611ins_compl_key2dir(c)
4612 int c;
4613{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004614 if (c == Ctrl_P || c == Ctrl_L
4615 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4616 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004617 return BACKWARD;
4618 return FORWARD;
4619}
4620
4621/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004622 * Return TRUE for keys that are used for completion only when the popup menu
4623 * is visible.
4624 */
4625 static int
4626ins_compl_pum_key(c)
4627 int c;
4628{
4629 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004630 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4631 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004632}
4633
4634/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004635 * Decide the number of completions to move forward.
4636 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4637 */
4638 static int
4639ins_compl_key2count(c)
4640 int c;
4641{
4642 int h;
4643
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004644 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004645 {
4646 h = pum_get_height();
4647 if (h > 3)
4648 h -= 2; /* keep some context */
4649 return h;
4650 }
4651 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652}
4653
4654/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004655 * Return TRUE if completion with "c" should insert the match, FALSE if only
4656 * to change the currently selected completion.
4657 */
4658 static int
4659ins_compl_use_match(c)
4660 int c;
4661{
4662 switch (c)
4663 {
4664 case K_UP:
4665 case K_DOWN:
4666 case K_PAGEDOWN:
4667 case K_KPAGEDOWN:
4668 case K_S_DOWN:
4669 case K_PAGEUP:
4670 case K_KPAGEUP:
4671 case K_S_UP:
4672 return FALSE;
4673 }
4674 return TRUE;
4675}
4676
4677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 * Do Insert mode completion.
4679 * Called when character "c" was typed, which has a meaning for completion.
4680 * Returns OK if completion was done, FAIL if something failed (out of mem).
4681 */
4682 static int
4683ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004686 char_u *line;
4687 int startcol = 0; /* column where searched text starts */
4688 colnr_T curs_col; /* cursor column */
4689 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaare3226be2005-12-18 22:10:00 +00004691 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
4694 /* First time we hit ^N or ^P (in a row, I mean) */
4695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 did_ai = FALSE;
4697#ifdef FEAT_SMARTINDENT
4698 did_si = FALSE;
4699 can_si = FALSE;
4700 can_si_back = FALSE;
4701#endif
4702 if (stop_arrow() == FAIL)
4703 return FAIL;
4704
4705 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004706 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004707 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004709 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004710 * "compl_startpos" to the cursor as a pattern to add a new word
4711 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004712 * "compl_startpos" is not in the same line as the cursor then fix it
4713 * (the line has been split because it was longer than 'tw'). if SOL
4714 * is set then skip the previous pattern, a word at the beginning of
4715 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004716 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4717 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
4719 /*
4720 * it is a continued search
4721 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004722 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4724 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4725 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004728 /* line (probably) wrapped, set compl_startpos to the
4729 * first non_blank in the line, if it is not a wordchar
4730 * include it to get a better pattern, but then we don't
4731 * want the "\\<" prefix, check it bellow */
4732 compl_col = (colnr_T)(skipwhite(line) - line);
4733 compl_startpos.col = compl_col;
4734 compl_startpos.lnum = curwin->w_cursor.lnum;
4735 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737 else
4738 {
4739 /* S_IPOS was set when we inserted a word that was at the
4740 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004741 * mode but first we need to redefine compl_startpos */
4742 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004744 compl_cont_status |= CONT_SOL;
4745 compl_startpos.col = (colnr_T)(skipwhite(
4746 line + compl_length
4747 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004749 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004751 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004752 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004753 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004755 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004757 compl_cont_status &= ~CONT_SOL;
4758 compl_length = (IOSIZE - MIN_SPACE);
4759 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004761 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4762 if (compl_length < 1)
4763 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004768 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004771 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004773 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004777 compl_cont_status = 0;
4778 compl_cont_status |= CONT_N_ADDS;
4779 compl_startpos = curwin->w_cursor;
4780 startcol = (int)curs_col;
4781 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783
4784 /* Work out completion pattern and original text -- webb */
4785 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4786 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4789 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004794 compl_col += ++startcol;
4795 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 compl_pattern = str_foldcase(line + compl_col,
4799 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 compl_pattern = vim_strnsave(line + compl_col,
4802 compl_length);
4803 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return FAIL;
4805 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004806 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 char_u *prefix = (char_u *)"\\<";
4809
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004810 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004812 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 if (!vim_iswordp(line + compl_col)
4816 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 && (
4818#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004821 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#endif
4823 )))
4824 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004825 STRCPY((char *)compl_pattern, prefix);
4826 (void)quote_meta(compl_pattern + STRLEN(prefix),
4827 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004829 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004833 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#endif
4835 )
4836 {
4837 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004838 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4839 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004841 compl_col += curs_col;
4842 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else
4845 {
4846#ifdef FEAT_MBYTE
4847 /* Search the point of change class of multibyte character
4848 * or not a word single byte character backward. */
4849 if (has_mbyte)
4850 {
4851 int base_class;
4852 int head_off;
4853
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004854 startcol -= (*mb_head_off)(line, line + startcol);
4855 base_class = mb_get_class(line + startcol);
4856 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004858 head_off = (*mb_head_off)(line, line + startcol);
4859 if (base_class != mb_get_class(line + startcol
4860 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004862 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 }
4865 else
4866#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004867 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004869 compl_col += ++startcol;
4870 compl_length = (int)curs_col - startcol;
4871 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 /* Only match word with at least two chars -- webb
4874 * there's no need to call quote_meta,
4875 * alloc(7) is enough -- Acevedo
4876 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 compl_pattern = alloc(7);
4878 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004880 STRCPY((char *)compl_pattern, "\\<");
4881 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4882 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 else
4885 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004886 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004887 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004888 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004890 STRCPY((char *)compl_pattern, "\\<");
4891 (void)quote_meta(compl_pattern + 2, line + compl_col,
4892 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 }
4895 }
4896 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4897 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004898 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004899 compl_length = (int)curs_col - (int)compl_col;
4900 if (compl_length < 0) /* cursor in indent: empty pattern */
4901 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004903 compl_pattern = str_foldcase(line + compl_col, compl_length,
4904 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4907 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 return FAIL;
4909 }
4910 else if (ctrl_x_mode == CTRL_X_FILES)
4911 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004912 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004914 compl_col += ++startcol;
4915 compl_length = (int)curs_col - startcol;
4916 compl_pattern = addstar(line + compl_col, compl_length,
4917 EXPAND_FILES);
4918 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return FAIL;
4920 }
4921 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4922 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004923 compl_pattern = vim_strnsave(line, curs_col);
4924 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004926 set_cmd_context(&compl_xp, compl_pattern,
4927 (int)STRLEN(compl_pattern), curs_col);
4928 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4929 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004930 /* No completion possible, use an empty pattern to get a
4931 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004932 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004933 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004934 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4935 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004937 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004938 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004939#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004940 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004941 * Call user defined function 'completefunc' with "a:findstart"
4942 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004943 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004944 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004945 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004946 char_u *funcname;
4947 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004948
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004949 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004950 * string */
4951 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4952 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4953 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004954 {
4955 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4956 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004957 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004958 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004959
4960 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004961 args[1] = NULL;
4962 pos = curwin->w_cursor;
4963 col = call_func_retnr(funcname, 2, args, FALSE);
4964 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004965
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004966 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004967 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004968 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004969 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004970 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004971
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004972 /* Setup variables for completion. Need to obtain "line" again,
4973 * it may have become invalid. */
4974 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004975 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004976 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4977 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004978#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004979 return FAIL;
4980 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004981 else if (ctrl_x_mode == CTRL_X_SPELL)
4982 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004983#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004984 if (spell_bad_len > 0)
4985 compl_col = curs_col - spell_bad_len;
4986 else
4987 compl_col = spell_word_start(startcol);
4988 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004989 {
4990 compl_length = 0;
4991 compl_col = curs_col;
4992 }
4993 else
4994 {
4995 spell_expand_check_cap(compl_col);
4996 compl_length = (int)curs_col - compl_col;
4997 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004998 /* Need to obtain "line" again, it may have become invalid. */
4999 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005000 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5001 if (compl_pattern == NULL)
5002#endif
5003 return FAIL;
5004 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005005 else
5006 {
5007 EMSG2(_(e_intern2), "ins_complete()");
5008 return FAIL;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005011 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 edit_submode_pre = (char_u *)_(" Adding");
5014 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5015 {
5016 /* Insert a new line, keep indentation but ignore 'comments' */
5017#ifdef FEAT_COMMENTS
5018 char_u *old = curbuf->b_p_com;
5019
5020 curbuf->b_p_com = (char_u *)"";
5021#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005022 compl_startpos.lnum = curwin->w_cursor.lnum;
5023 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 ins_eol('\r');
5025#ifdef FEAT_COMMENTS
5026 curbuf->b_p_com = old;
5027#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005028 compl_length = 0;
5029 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 }
5032 else
5033 {
5034 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005035 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 if (compl_cont_status & CONT_LOCAL)
5039 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 else
5041 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5042
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005043 /* Always add completion for the original text. */
5044 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005045 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5046 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005047 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005049 vim_free(compl_pattern);
5050 compl_pattern = NULL;
5051 vim_free(compl_orig_text);
5052 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 return FAIL;
5054 }
5055
5056 /* showmode might reset the internal line pointers, so it must
5057 * be called before line = ml_get(), or when this address is no
5058 * longer needed. -- Acevedo.
5059 */
5060 edit_submode_extra = (char_u *)_("-- Searching...");
5061 edit_submode_highl = HLF_COUNT;
5062 showmode();
5063 edit_submode_extra = NULL;
5064 out_flush();
5065 }
5066
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 compl_shown_match = compl_curr_match;
5068 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005071 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005073 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005075 /* may undisplay the popup menu */
5076 ins_compl_upd_pum();
5077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005078 if (n > 1) /* all matches have been found */
5079 compl_matches = n;
5080 compl_curr_match = compl_shown_match;
5081 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
Bram Moolenaard68071d2006-05-02 22:08:30 +00005083 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5084 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (got_int && !global_busy)
5086 {
5087 (void)vgetc();
5088 got_int = FALSE;
5089 }
5090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005091 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005092 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5095 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5097 edit_submode_highl = HLF_E;
5098 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5099 * because we couldn't expand anything at first place, but if we used
5100 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5101 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 if ( compl_length > 1
5103 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 || (ctrl_x_mode != 0
5105 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5106 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109
Bram Moolenaar572cb562005-08-05 21:35:02 +00005110 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 if (edit_submode_extra == NULL)
5116 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005117 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 edit_submode_extra = (char_u *)_("Back at original");
5120 edit_submode_highl = HLF_W;
5121 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 edit_submode_extra = (char_u *)_("Word from other line");
5125 edit_submode_highl = HLF_COUNT;
5126 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005127 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 edit_submode_extra = (char_u *)_("The only match");
5130 edit_submode_highl = HLF_COUNT;
5131 }
5132 else
5133 {
5134 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005135 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005137 int number = 0;
5138 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005140 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 /* search backwards for the first valid (!= -1) number.
5143 * This should normally succeed already at the first loop
5144 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005145 for (match = compl_curr_match->cp_prev; match != NULL
5146 && match != compl_first_match;
5147 match = match->cp_prev)
5148 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005150 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152 }
5153 if (match != NULL)
5154 /* go up and assign all numbers which are not assigned
5155 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005156 for (match = match->cp_next;
5157 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005158 match = match->cp_next)
5159 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 else /* BACKWARD */
5162 {
5163 /* search forwards (upwards) for the first valid (!= -1)
5164 * number. This should normally succeed already at the
5165 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005166 for (match = compl_curr_match->cp_next; match != NULL
5167 && match != compl_first_match;
5168 match = match->cp_next)
5169 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005171 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 break;
5173 }
5174 if (match != NULL)
5175 /* go down and assign all numbers which are not
5176 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005177 for (match = match->cp_prev; match
5178 && match->cp_number == -1;
5179 match = match->cp_prev)
5180 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182 }
5183
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005184 /* The match should always have a sequence number now, this is
5185 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005186 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005188 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5189 * Translations may need more than twice that. */
5190 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005193 vim_snprintf((char *)match_ref, sizeof(match_ref),
5194 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005195 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005197 vim_snprintf((char *)match_ref, sizeof(match_ref),
5198 _("match %d"),
5199 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 edit_submode_extra = match_ref;
5201 edit_submode_highl = HLF_R;
5202 if (dollar_vcol)
5203 curs_columns(FALSE);
5204 }
5205 }
5206 }
5207
5208 /* Show a message about what (completion) mode we're in. */
5209 showmode();
5210 if (edit_submode_extra != NULL)
5211 {
5212 if (!p_smd)
5213 msg_attr(edit_submode_extra,
5214 edit_submode_highl < HLF_COUNT
5215 ? hl_attr(edit_submode_highl) : 0);
5216 }
5217 else
5218 msg_clr_cmdline(); /* necessary for "noshowmode" */
5219
Bram Moolenaard68071d2006-05-02 22:08:30 +00005220 /* Show the popup menu, unless we got interrupted. */
5221 if (!compl_interrupted)
5222 {
5223 /* RedrawingDisabled may be set when invoked through complete(). */
5224 n = RedrawingDisabled;
5225 RedrawingDisabled = 0;
5226 ins_compl_show_pum();
5227 setcursor();
5228 RedrawingDisabled = n;
5229 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005230 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005231 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return OK;
5234}
5235
5236/*
5237 * Looks in the first "len" chars. of "src" for search-metachars.
5238 * If dest is not NULL the chars. are copied there quoting (with
5239 * a backslash) the metachars, and dest would be NUL terminated.
5240 * Returns the length (needed) of dest
5241 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005242 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243quote_meta(dest, src, len)
5244 char_u *dest;
5245 char_u *src;
5246 int len;
5247{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005248 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005250 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 switch (*src)
5253 {
5254 case '.':
5255 case '*':
5256 case '[':
5257 if (ctrl_x_mode == CTRL_X_DICTIONARY
5258 || ctrl_x_mode == CTRL_X_THESAURUS)
5259 break;
5260 case '~':
5261 if (!p_magic) /* quote these only if magic is set */
5262 break;
5263 case '\\':
5264 if (ctrl_x_mode == CTRL_X_DICTIONARY
5265 || ctrl_x_mode == CTRL_X_THESAURUS)
5266 break;
5267 case '^': /* currently it's not needed. */
5268 case '$':
5269 m++;
5270 if (dest != NULL)
5271 *dest++ = '\\';
5272 break;
5273 }
5274 if (dest != NULL)
5275 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005276# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 /* Copy remaining bytes of a multibyte character. */
5278 if (has_mbyte)
5279 {
5280 int i, mb_len;
5281
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005282 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (mb_len > 0 && len >= mb_len)
5284 for (i = 0; i < mb_len; ++i)
5285 {
5286 --len;
5287 ++src;
5288 if (dest != NULL)
5289 *dest++ = *src;
5290 }
5291 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005292# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294 if (dest != NULL)
5295 *dest = NUL;
5296
5297 return m;
5298}
5299#endif /* FEAT_INS_EXPAND */
5300
5301/*
5302 * Next character is interpreted literally.
5303 * A one, two or three digit decimal number is interpreted as its byte value.
5304 * If one or two digits are entered, the next character is given to vungetc().
5305 * For Unicode a character > 255 may be returned.
5306 */
5307 int
5308get_literal()
5309{
5310 int cc;
5311 int nc;
5312 int i;
5313 int hex = FALSE;
5314 int octal = FALSE;
5315#ifdef FEAT_MBYTE
5316 int unicode = 0;
5317#endif
5318
5319 if (got_int)
5320 return Ctrl_C;
5321
5322#ifdef FEAT_GUI
5323 /*
5324 * In GUI there is no point inserting the internal code for a special key.
5325 * It is more useful to insert the string "<KEY>" instead. This would
5326 * probably be useful in a text window too, but it would not be
5327 * vi-compatible (maybe there should be an option for it?) -- webb
5328 */
5329 if (gui.in_use)
5330 ++allow_keys;
5331#endif
5332#ifdef USE_ON_FLY_SCROLL
5333 dont_scroll = TRUE; /* disallow scrolling here */
5334#endif
5335 ++no_mapping; /* don't map the next key hits */
5336 cc = 0;
5337 i = 0;
5338 for (;;)
5339 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005340 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#ifdef FEAT_CMDL_INFO
5342 if (!(State & CMDLINE)
5343# ifdef FEAT_MBYTE
5344 && MB_BYTE2LEN_CHECK(nc) == 1
5345# endif
5346 )
5347 add_to_showcmd(nc);
5348#endif
5349 if (nc == 'x' || nc == 'X')
5350 hex = TRUE;
5351 else if (nc == 'o' || nc == 'O')
5352 octal = TRUE;
5353#ifdef FEAT_MBYTE
5354 else if (nc == 'u' || nc == 'U')
5355 unicode = nc;
5356#endif
5357 else
5358 {
5359 if (hex
5360#ifdef FEAT_MBYTE
5361 || unicode != 0
5362#endif
5363 )
5364 {
5365 if (!vim_isxdigit(nc))
5366 break;
5367 cc = cc * 16 + hex2nr(nc);
5368 }
5369 else if (octal)
5370 {
5371 if (nc < '0' || nc > '7')
5372 break;
5373 cc = cc * 8 + nc - '0';
5374 }
5375 else
5376 {
5377 if (!VIM_ISDIGIT(nc))
5378 break;
5379 cc = cc * 10 + nc - '0';
5380 }
5381
5382 ++i;
5383 }
5384
5385 if (cc > 255
5386#ifdef FEAT_MBYTE
5387 && unicode == 0
5388#endif
5389 )
5390 cc = 255; /* limit range to 0-255 */
5391 nc = 0;
5392
5393 if (hex) /* hex: up to two chars */
5394 {
5395 if (i >= 2)
5396 break;
5397 }
5398#ifdef FEAT_MBYTE
5399 else if (unicode) /* Unicode: up to four or eight chars */
5400 {
5401 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5402 break;
5403 }
5404#endif
5405 else if (i >= 3) /* decimal or octal: up to three chars */
5406 break;
5407 }
5408 if (i == 0) /* no number entered */
5409 {
5410 if (nc == K_ZERO) /* NUL is stored as NL */
5411 {
5412 cc = '\n';
5413 nc = 0;
5414 }
5415 else
5416 {
5417 cc = nc;
5418 nc = 0;
5419 }
5420 }
5421
5422 if (cc == 0) /* NUL is stored as NL */
5423 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005424#ifdef FEAT_MBYTE
5425 if (enc_dbcs && (cc & 0xff) == 0)
5426 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5427 second byte will cause trouble! */
5428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
5430 --no_mapping;
5431#ifdef FEAT_GUI
5432 if (gui.in_use)
5433 --allow_keys;
5434#endif
5435 if (nc)
5436 vungetc(nc);
5437 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5438 return cc;
5439}
5440
5441/*
5442 * Insert character, taking care of special keys and mod_mask
5443 */
5444 static void
5445insert_special(c, allow_modmask, ctrlv)
5446 int c;
5447 int allow_modmask;
5448 int ctrlv; /* c was typed after CTRL-V */
5449{
5450 char_u *p;
5451 int len;
5452
5453 /*
5454 * Special function key, translate into "<Key>". Up to the last '>' is
5455 * inserted with ins_str(), so as not to replace characters in replace
5456 * mode.
5457 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5458 * unless 'allow_modmask' is TRUE.
5459 */
5460#ifdef MACOS
5461 /* Command-key never produces a normal key */
5462 if (mod_mask & MOD_MASK_CMD)
5463 allow_modmask = TRUE;
5464#endif
5465 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5466 {
5467 p = get_special_key_name(c, mod_mask);
5468 len = (int)STRLEN(p);
5469 c = p[len - 1];
5470 if (len > 2)
5471 {
5472 if (stop_arrow() == FAIL)
5473 return;
5474 p[len - 1] = NUL;
5475 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005476 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 ctrlv = FALSE;
5478 }
5479 }
5480 if (stop_arrow() == OK)
5481 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5482}
5483
5484/*
5485 * Special characters in this context are those that need processing other
5486 * than the simple insertion that can be performed here. This includes ESC
5487 * which terminates the insert, and CR/NL which need special processing to
5488 * open up a new line. This routine tries to optimize insertions performed by
5489 * the "redo", "undo" or "put" commands, so it needs to know when it should
5490 * stop and defer processing to the "normal" mechanism.
5491 * '0' and '^' are special, because they can be followed by CTRL-D.
5492 */
5493#ifdef EBCDIC
5494# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5495#else
5496# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5497#endif
5498
5499#ifdef FEAT_MBYTE
5500# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5501#else
5502# define WHITECHAR(cc) vim_iswhite(cc)
5503#endif
5504
5505 void
5506insertchar(c, flags, second_indent)
5507 int c; /* character to insert or NUL */
5508 int flags; /* INSCHAR_FORMAT, etc. */
5509 int second_indent; /* indent for second line if >= 0 */
5510{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 int textwidth;
5512#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5518 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 /*
5521 * Try to break the line in two or more pieces when:
5522 * - Always do this if we have been called to do formatting only.
5523 * - Always do this when 'formatoptions' has the 'a' flag and the line
5524 * ends in white space.
5525 * - Otherwise:
5526 * - Don't do this if inserting a blank
5527 * - Don't do this if an existing character is being replaced, unless
5528 * we're in VREPLACE mode.
5529 * - Do this if the cursor is not on the line where insert started
5530 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5531 * before the insert.
5532 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5533 * before 'textwidth'
5534 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005535 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 && ((flags & INSCHAR_FORMAT)
5537 || (!vim_iswhite(c)
5538 && !((State & REPLACE_FLAG)
5539#ifdef FEAT_VREPLACE
5540 && !(State & VREPLACE_FLAG)
5541#endif
5542 && *ml_get_cursor() != NUL)
5543 && (curwin->w_cursor.lnum != Insstart.lnum
5544 || ((!has_format_option(FO_INS_LONG)
5545 || Insstart_textlen <= (colnr_T)textwidth)
5546 && (!fo_ins_blank
5547 || Insstart_blank_vcol <= (colnr_T)textwidth
5548 ))))))
5549 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005550 /* Format with 'formatexpr' when it's set. Use internal formatting
5551 * when 'formatexpr' isn't set or it returns non-zero. */
5552#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005553 int do_internal = TRUE;
5554
Bram Moolenaar81a82092008-03-12 16:27:00 +00005555 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005556 {
5557 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5558 /* It may be required to save for undo again, e.g. when setline()
5559 * was called. */
5560 ins_need_undo = TRUE;
5561 }
5562 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005564 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (c == NUL) /* only formatting was wanted */
5568 return;
5569
5570#ifdef FEAT_COMMENTS
5571 /* Check whether this character should end a comment. */
5572 if (did_ai && (int)c == end_comment_pending)
5573 {
5574 char_u *line;
5575 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5576 int middle_len, end_len;
5577 int i;
5578
5579 /*
5580 * Need to remove existing (middle) comment leader and insert end
5581 * comment leader. First, check what comment leader we can find.
5582 */
5583 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5584 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5585 {
5586 /* Skip middle-comment string */
5587 while (*p && p[-1] != ':') /* find end of middle flags */
5588 ++p;
5589 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5590 /* Don't count trailing white space for middle_len */
5591 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5592 --middle_len;
5593
5594 /* Find the end-comment string */
5595 while (*p && p[-1] != ':') /* find end of end flags */
5596 ++p;
5597 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5598
5599 /* Skip white space before the cursor */
5600 i = curwin->w_cursor.col;
5601 while (--i >= 0 && vim_iswhite(line[i]))
5602 ;
5603 i++;
5604
5605 /* Skip to before the middle leader */
5606 i -= middle_len;
5607
5608 /* Check some expected things before we go on */
5609 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5610 {
5611 /* Backspace over all the stuff we want to replace */
5612 backspace_until_column(i);
5613
5614 /*
5615 * Insert the end-comment string, except for the last
5616 * character, which will get inserted as normal later.
5617 */
5618 ins_bytes_len(lead_end, end_len - 1);
5619 }
5620 }
5621 }
5622 end_comment_pending = NUL;
5623#endif
5624
5625 did_ai = FALSE;
5626#ifdef FEAT_SMARTINDENT
5627 did_si = FALSE;
5628 can_si = FALSE;
5629 can_si_back = FALSE;
5630#endif
5631
5632 /*
5633 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5634 * This speeds up normal text input considerably.
5635 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5636 * need to re-indent at a ':', or any other character (but not what
5637 * 'paste' is set)..
5638 */
5639#ifdef USE_ON_FLY_SCROLL
5640 dont_scroll = FALSE; /* allow scrolling here */
5641#endif
5642
5643 if ( !ISSPECIAL(c)
5644#ifdef FEAT_MBYTE
5645 && (!has_mbyte || (*mb_char2len)(c) == 1)
5646#endif
5647 && vpeekc() != NUL
5648 && !(State & REPLACE_FLAG)
5649#ifdef FEAT_CINDENT
5650 && !cindent_on()
5651#endif
5652#ifdef FEAT_RIGHTLEFT
5653 && !p_ri
5654#endif
5655 )
5656 {
5657#define INPUT_BUFLEN 100
5658 char_u buf[INPUT_BUFLEN + 1];
5659 int i;
5660 colnr_T virtcol = 0;
5661
5662 buf[0] = c;
5663 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005664 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 virtcol = get_nolist_virtcol();
5666 /*
5667 * Stop the string when:
5668 * - no more chars available
5669 * - finding a special character (command key)
5670 * - buffer is full
5671 * - running into the 'textwidth' boundary
5672 * - need to check for abbreviation: A non-word char after a word-char
5673 */
5674 while ( (c = vpeekc()) != NUL
5675 && !ISSPECIAL(c)
5676#ifdef FEAT_MBYTE
5677 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5678#endif
5679 && i < INPUT_BUFLEN
5680 && (textwidth == 0
5681 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5682 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5683 {
5684#ifdef FEAT_RIGHTLEFT
5685 c = vgetc();
5686 if (p_hkmap && KeyTyped)
5687 c = hkmap(c); /* Hebrew mode mapping */
5688# ifdef FEAT_FKMAP
5689 if (p_fkmap && KeyTyped)
5690 c = fkmap(c); /* Farsi mode mapping */
5691# endif
5692 buf[i++] = c;
5693#else
5694 buf[i++] = vgetc();
5695#endif
5696 }
5697
5698#ifdef FEAT_DIGRAPHS
5699 do_digraph(-1); /* clear digraphs */
5700 do_digraph(buf[i-1]); /* may be the start of a digraph */
5701#endif
5702 buf[i] = NUL;
5703 ins_str(buf);
5704 if (flags & INSCHAR_CTRLV)
5705 {
5706 redo_literal(*buf);
5707 i = 1;
5708 }
5709 else
5710 i = 0;
5711 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005712 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 else
5715 {
5716#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005717 int cc;
5718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5720 {
5721 char_u buf[MB_MAXBYTES + 1];
5722
5723 (*mb_char2bytes)(c, buf);
5724 buf[cc] = NUL;
5725 ins_char_bytes(buf, cc);
5726 AppendCharToRedobuff(c);
5727 }
5728 else
5729#endif
5730 {
5731 ins_char(c);
5732 if (flags & INSCHAR_CTRLV)
5733 redo_literal(c);
5734 else
5735 AppendCharToRedobuff(c);
5736 }
5737 }
5738}
5739
5740/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005741 * Format text at the current insert position.
5742 */
5743 static void
5744internal_format(textwidth, second_indent, flags, format_only)
5745 int textwidth;
5746 int second_indent;
5747 int flags;
5748 int format_only;
5749{
5750 int cc;
5751 int save_char = NUL;
5752 int haveto_redraw = FALSE;
5753 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5754#ifdef FEAT_MBYTE
5755 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5756#endif
5757 int fo_white_par = has_format_option(FO_WHITE_PAR);
5758 int first_line = TRUE;
5759#ifdef FEAT_COMMENTS
5760 colnr_T leader_len;
5761 int no_leader = FALSE;
5762 int do_comments = (flags & INSCHAR_DO_COM);
5763#endif
5764
5765 /*
5766 * When 'ai' is off we don't want a space under the cursor to be
5767 * deleted. Replace it with an 'x' temporarily.
5768 */
5769 if (!curbuf->b_p_ai)
5770 {
5771 cc = gchar_cursor();
5772 if (vim_iswhite(cc))
5773 {
5774 save_char = cc;
5775 pchar_cursor('x');
5776 }
5777 }
5778
5779 /*
5780 * Repeat breaking lines, until the current line is not too long.
5781 */
5782 while (!got_int)
5783 {
5784 int startcol; /* Cursor column at entry */
5785 int wantcol; /* column at textwidth border */
5786 int foundcol; /* column for start of spaces */
5787 int end_foundcol = 0; /* column for start of word */
5788 colnr_T len;
5789 colnr_T virtcol;
5790#ifdef FEAT_VREPLACE
5791 int orig_col = 0;
5792 char_u *saved_text = NULL;
5793#endif
5794 colnr_T col;
5795
5796 virtcol = get_nolist_virtcol();
5797 if (virtcol < (colnr_T)textwidth)
5798 break;
5799
5800#ifdef FEAT_COMMENTS
5801 if (no_leader)
5802 do_comments = FALSE;
5803 else if (!(flags & INSCHAR_FORMAT)
5804 && has_format_option(FO_WRAP_COMS))
5805 do_comments = TRUE;
5806
5807 /* Don't break until after the comment leader */
5808 if (do_comments)
5809 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5810 else
5811 leader_len = 0;
5812
5813 /* If the line doesn't start with a comment leader, then don't
5814 * start one in a following broken line. Avoids that a %word
5815 * moved to the start of the next line causes all following lines
5816 * to start with %. */
5817 if (leader_len == 0)
5818 no_leader = TRUE;
5819#endif
5820 if (!(flags & INSCHAR_FORMAT)
5821#ifdef FEAT_COMMENTS
5822 && leader_len == 0
5823#endif
5824 && !has_format_option(FO_WRAP))
5825
5826 {
5827 textwidth = 0;
5828 break;
5829 }
5830 if ((startcol = curwin->w_cursor.col) == 0)
5831 break;
5832
5833 /* find column of textwidth border */
5834 coladvance((colnr_T)textwidth);
5835 wantcol = curwin->w_cursor.col;
5836
5837 curwin->w_cursor.col = startcol - 1;
5838#ifdef FEAT_MBYTE
5839 /* Correct cursor for multi-byte character. */
5840 if (has_mbyte)
5841 mb_adjust_cursor();
5842#endif
5843 foundcol = 0;
5844
5845 /*
5846 * Find position to break at.
5847 * Stop at first entered white when 'formatoptions' has 'v'
5848 */
5849 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5850 || curwin->w_cursor.lnum != Insstart.lnum
5851 || curwin->w_cursor.col >= Insstart.col)
5852 {
5853 cc = gchar_cursor();
5854 if (WHITECHAR(cc))
5855 {
5856 /* remember position of blank just before text */
5857 end_foundcol = curwin->w_cursor.col;
5858
5859 /* find start of sequence of blanks */
5860 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5861 {
5862 dec_cursor();
5863 cc = gchar_cursor();
5864 }
5865 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5866 break; /* only spaces in front of text */
5867#ifdef FEAT_COMMENTS
5868 /* Don't break until after the comment leader */
5869 if (curwin->w_cursor.col < leader_len)
5870 break;
5871#endif
5872 if (has_format_option(FO_ONE_LETTER))
5873 {
5874 /* do not break after one-letter words */
5875 if (curwin->w_cursor.col == 0)
5876 break; /* one-letter word at begin */
5877
5878 col = curwin->w_cursor.col;
5879 dec_cursor();
5880 cc = gchar_cursor();
5881
5882 if (WHITECHAR(cc))
5883 continue; /* one-letter, continue */
5884 curwin->w_cursor.col = col;
5885 }
5886#ifdef FEAT_MBYTE
5887 if (has_mbyte)
5888 foundcol = curwin->w_cursor.col
5889 + (*mb_ptr2len)(ml_get_cursor());
5890 else
5891#endif
5892 foundcol = curwin->w_cursor.col + 1;
5893 if (curwin->w_cursor.col < (colnr_T)wantcol)
5894 break;
5895 }
5896#ifdef FEAT_MBYTE
5897 else if (cc >= 0x100 && fo_multibyte
5898 && curwin->w_cursor.col <= (colnr_T)wantcol)
5899 {
5900 /* Break after or before a multi-byte character. */
5901 foundcol = curwin->w_cursor.col;
5902 if (curwin->w_cursor.col < (colnr_T)wantcol)
5903 foundcol += (*mb_char2len)(cc);
5904 end_foundcol = foundcol;
5905 break;
5906 }
5907#endif
5908 if (curwin->w_cursor.col == 0)
5909 break;
5910 dec_cursor();
5911 }
5912
5913 if (foundcol == 0) /* no spaces, cannot break line */
5914 {
5915 curwin->w_cursor.col = startcol;
5916 break;
5917 }
5918
5919 /* Going to break the line, remove any "$" now. */
5920 undisplay_dollar();
5921
5922 /*
5923 * Offset between cursor position and line break is used by replace
5924 * stack functions. VREPLACE does not use this, and backspaces
5925 * over the text instead.
5926 */
5927#ifdef FEAT_VREPLACE
5928 if (State & VREPLACE_FLAG)
5929 orig_col = startcol; /* Will start backspacing from here */
5930 else
5931#endif
5932 replace_offset = startcol - end_foundcol - 1;
5933
5934 /*
5935 * adjust startcol for spaces that will be deleted and
5936 * characters that will remain on top line
5937 */
5938 curwin->w_cursor.col = foundcol;
5939 while (cc = gchar_cursor(), WHITECHAR(cc))
5940 inc_cursor();
5941 startcol -= curwin->w_cursor.col;
5942 if (startcol < 0)
5943 startcol = 0;
5944
5945#ifdef FEAT_VREPLACE
5946 if (State & VREPLACE_FLAG)
5947 {
5948 /*
5949 * In VREPLACE mode, we will backspace over the text to be
5950 * wrapped, so save a copy now to put on the next line.
5951 */
5952 saved_text = vim_strsave(ml_get_cursor());
5953 curwin->w_cursor.col = orig_col;
5954 if (saved_text == NULL)
5955 break; /* Can't do it, out of memory */
5956 saved_text[startcol] = NUL;
5957
5958 /* Backspace over characters that will move to the next line */
5959 if (!fo_white_par)
5960 backspace_until_column(foundcol);
5961 }
5962 else
5963#endif
5964 {
5965 /* put cursor after pos. to break line */
5966 if (!fo_white_par)
5967 curwin->w_cursor.col = foundcol;
5968 }
5969
5970 /*
5971 * Split the line just before the margin.
5972 * Only insert/delete lines, but don't really redraw the window.
5973 */
5974 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5975 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5976#ifdef FEAT_COMMENTS
5977 + (do_comments ? OPENLINE_DO_COM : 0)
5978#endif
5979 , old_indent);
5980 old_indent = 0;
5981
5982 replace_offset = 0;
5983 if (first_line)
5984 {
5985 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5986 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5987 if (second_indent >= 0)
5988 {
5989#ifdef FEAT_VREPLACE
5990 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00005991 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005992 else
5993#endif
5994 (void)set_indent(second_indent, SIN_CHANGED);
5995 }
5996 first_line = FALSE;
5997 }
5998
5999#ifdef FEAT_VREPLACE
6000 if (State & VREPLACE_FLAG)
6001 {
6002 /*
6003 * In VREPLACE mode we have backspaced over the text to be
6004 * moved, now we re-insert it into the new line.
6005 */
6006 ins_bytes(saved_text);
6007 vim_free(saved_text);
6008 }
6009 else
6010#endif
6011 {
6012 /*
6013 * Check if cursor is not past the NUL off the line, cindent
6014 * may have added or removed indent.
6015 */
6016 curwin->w_cursor.col += startcol;
6017 len = (colnr_T)STRLEN(ml_get_curline());
6018 if (curwin->w_cursor.col > len)
6019 curwin->w_cursor.col = len;
6020 }
6021
6022 haveto_redraw = TRUE;
6023#ifdef FEAT_CINDENT
6024 can_cindent = TRUE;
6025#endif
6026 /* moved the cursor, don't autoindent or cindent now */
6027 did_ai = FALSE;
6028#ifdef FEAT_SMARTINDENT
6029 did_si = FALSE;
6030 can_si = FALSE;
6031 can_si_back = FALSE;
6032#endif
6033 line_breakcheck();
6034 }
6035
6036 if (save_char != NUL) /* put back space after cursor */
6037 pchar_cursor(save_char);
6038
6039 if (!format_only && haveto_redraw)
6040 {
6041 update_topline();
6042 redraw_curbuf_later(VALID);
6043 }
6044}
6045
6046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 * Called after inserting or deleting text: When 'formatoptions' includes the
6048 * 'a' flag format from the current line until the end of the paragraph.
6049 * Keep the cursor at the same position relative to the text.
6050 * The caller must have saved the cursor line for undo, following ones will be
6051 * saved here.
6052 */
6053 void
6054auto_format(trailblank, prev_line)
6055 int trailblank; /* when TRUE also format with trailing blank */
6056 int prev_line; /* may start in previous line */
6057{
6058 pos_T pos;
6059 colnr_T len;
6060 char_u *old;
6061 char_u *new, *pnew;
6062 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006063 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
6065 if (!has_format_option(FO_AUTO))
6066 return;
6067
6068 pos = curwin->w_cursor;
6069 old = ml_get_curline();
6070
6071 /* may remove added space */
6072 check_auto_format(FALSE);
6073
6074 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6075 * user might insert normal text next. Also skip formatting when "1" is
6076 * in 'formatoptions' and there is a single character before the cursor.
6077 * Otherwise the line would be broken and when typing another non-white
6078 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006079 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (*old != NUL && !trailblank && wasatend)
6081 {
6082 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006083 cc = gchar_cursor();
6084 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6085 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006087 cc = gchar_cursor();
6088 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 {
6090 curwin->w_cursor = pos;
6091 return;
6092 }
6093 curwin->w_cursor = pos;
6094 }
6095
6096#ifdef FEAT_COMMENTS
6097 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6098 * comments. */
6099 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6100 && get_leader_len(old, NULL, FALSE) == 0)
6101 return;
6102#endif
6103
6104 /*
6105 * May start formatting in a previous line, so that after "x" a word is
6106 * moved to the previous line if it fits there now. Only when this is not
6107 * the start of a paragraph.
6108 */
6109 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6110 {
6111 --curwin->w_cursor.lnum;
6112 if (u_save_cursor() == FAIL)
6113 return;
6114 }
6115
6116 /*
6117 * Do the formatting and restore the cursor position. "saved_cursor" will
6118 * be adjusted for the text formatting.
6119 */
6120 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006121 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 curwin->w_cursor = saved_cursor;
6123 saved_cursor.lnum = 0;
6124
6125 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6126 {
6127 /* "cannot happen" */
6128 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6129 coladvance((colnr_T)MAXCOL);
6130 }
6131 else
6132 check_cursor_col();
6133
6134 /* Insert mode: If the cursor is now after the end of the line while it
6135 * previously wasn't, the line was broken. Because of the rule above we
6136 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6137 * formatted. */
6138 if (!wasatend && has_format_option(FO_WHITE_PAR))
6139 {
6140 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006141 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (curwin->w_cursor.col == len)
6143 {
6144 pnew = vim_strnsave(new, len + 2);
6145 pnew[len] = ' ';
6146 pnew[len + 1] = NUL;
6147 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6148 /* remove the space later */
6149 did_add_space = TRUE;
6150 }
6151 else
6152 /* may remove added space */
6153 check_auto_format(FALSE);
6154 }
6155
6156 check_cursor();
6157}
6158
6159/*
6160 * When an extra space was added to continue a paragraph for auto-formatting,
6161 * delete it now. The space must be under the cursor, just after the insert
6162 * position.
6163 */
6164 static void
6165check_auto_format(end_insert)
6166 int end_insert; /* TRUE when ending Insert mode */
6167{
6168 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006169 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170
6171 if (did_add_space)
6172 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006173 cc = gchar_cursor();
6174 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 /* Somehow the space was removed already. */
6176 did_add_space = FALSE;
6177 else
6178 {
6179 if (!end_insert)
6180 {
6181 inc_cursor();
6182 c = gchar_cursor();
6183 dec_cursor();
6184 }
6185 if (c != NUL)
6186 {
6187 /* The space is no longer at the end of the line, delete it. */
6188 del_char(FALSE);
6189 did_add_space = FALSE;
6190 }
6191 }
6192 }
6193}
6194
6195/*
6196 * Find out textwidth to be used for formatting:
6197 * if 'textwidth' option is set, use it
6198 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6199 * if invalid value, use 0.
6200 * Set default to window width (maximum 79) for "gq" operator.
6201 */
6202 int
6203comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006204 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 int textwidth;
6207
6208 textwidth = curbuf->b_p_tw;
6209 if (textwidth == 0 && curbuf->b_p_wm)
6210 {
6211 /* The width is the window width minus 'wrapmargin' minus all the
6212 * things that add to the margin. */
6213 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6214#ifdef FEAT_CMDWIN
6215 if (cmdwin_type != 0)
6216 textwidth -= 1;
6217#endif
6218#ifdef FEAT_FOLDING
6219 textwidth -= curwin->w_p_fdc;
6220#endif
6221#ifdef FEAT_SIGNS
6222 if (curwin->w_buffer->b_signlist != NULL
6223# ifdef FEAT_NETBEANS_INTG
6224 || usingNetbeans
6225# endif
6226 )
6227 textwidth -= 1;
6228#endif
6229 if (curwin->w_p_nu)
6230 textwidth -= 8;
6231 }
6232 if (textwidth < 0)
6233 textwidth = 0;
6234 if (ff && textwidth == 0)
6235 {
6236 textwidth = W_WIDTH(curwin) - 1;
6237 if (textwidth > 79)
6238 textwidth = 79;
6239 }
6240 return textwidth;
6241}
6242
6243/*
6244 * Put a character in the redo buffer, for when just after a CTRL-V.
6245 */
6246 static void
6247redo_literal(c)
6248 int c;
6249{
6250 char_u buf[10];
6251
6252 /* Only digits need special treatment. Translate them into a string of
6253 * three digits. */
6254 if (VIM_ISDIGIT(c))
6255 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006256 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 AppendToRedobuff(buf);
6258 }
6259 else
6260 AppendCharToRedobuff(c);
6261}
6262
6263/*
6264 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006265 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 */
6267 static void
6268start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006269 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 if (!arrow_used) /* something has been inserted */
6272 {
6273 AppendToRedobuff(ESC_STR);
6274 stop_insert(end_insert_pos, FALSE);
6275 arrow_used = TRUE; /* this means we stopped the current insert */
6276 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006277#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006278 check_spell_redraw();
6279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280}
6281
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006282#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006283/*
6284 * If we skipped highlighting word at cursor, do it now.
6285 * It may be skipped again, thus reset spell_redraw_lnum first.
6286 */
6287 static void
6288check_spell_redraw()
6289{
6290 if (spell_redraw_lnum != 0)
6291 {
6292 linenr_T lnum = spell_redraw_lnum;
6293
6294 spell_redraw_lnum = 0;
6295 redrawWinline(lnum, FALSE);
6296 }
6297}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006298
6299/*
6300 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6301 * spelled word, if there is one.
6302 */
6303 static void
6304spell_back_to_badword()
6305{
6306 pos_T tpos = curwin->w_cursor;
6307
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006308 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006309 if (curwin->w_cursor.col != tpos.col)
6310 start_arrow(&tpos);
6311}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006312#endif
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314/*
6315 * stop_arrow() is called before a change is made in insert mode.
6316 * If an arrow key has been used, start a new insertion.
6317 * Returns FAIL if undo is impossible, shouldn't insert then.
6318 */
6319 int
6320stop_arrow()
6321{
6322 if (arrow_used)
6323 {
6324 if (u_save_cursor() == OK)
6325 {
6326 arrow_used = FALSE;
6327 ins_need_undo = FALSE;
6328 }
6329 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006330 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 ai_col = 0;
6332#ifdef FEAT_VREPLACE
6333 if (State & VREPLACE_FLAG)
6334 {
6335 orig_line_count = curbuf->b_ml.ml_line_count;
6336 vr_lines_changed = 1;
6337 }
6338#endif
6339 ResetRedobuff();
6340 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006341 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
6343 else if (ins_need_undo)
6344 {
6345 if (u_save_cursor() == OK)
6346 ins_need_undo = FALSE;
6347 }
6348
6349#ifdef FEAT_FOLDING
6350 /* Always open fold at the cursor line when inserting something. */
6351 foldOpenCursor();
6352#endif
6353
6354 return (arrow_used || ins_need_undo ? FAIL : OK);
6355}
6356
6357/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006358 * Do a few things to stop inserting.
6359 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6360 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 */
6362 static void
6363stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006364 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006365 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006367 int cc;
6368 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
6370 stop_redo_ins();
6371 replace_flush(); /* abandon replace stack */
6372
6373 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006374 * Save the inserted text for later redo with ^@ and CTRL-A.
6375 * Don't do it when "restart_edit" was set and nothing was inserted,
6376 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006378 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006379 if (did_restart_edit == 0 || (ptr != NULL
6380 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006381 {
6382 vim_free(last_insert);
6383 last_insert = ptr;
6384 last_insert_skip = new_insert_skip;
6385 }
6386 else
6387 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006389 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 /* Auto-format now. It may seem strange to do this when stopping an
6392 * insertion (or moving the cursor), but it's required when appending
6393 * a line and having it end in a space. But only do it when something
6394 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006395 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006397 pos_T tpos = curwin->w_cursor;
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 /* When the cursor is at the end of the line after a space the
6400 * formatting will move it to the following word. Avoid that by
6401 * moving the cursor onto the space. */
6402 cc = 'x';
6403 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6404 {
6405 dec_cursor();
6406 cc = gchar_cursor();
6407 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006408 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410
6411 auto_format(TRUE, FALSE);
6412
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006413 if (vim_iswhite(cc))
6414 {
6415 if (gchar_cursor() != NUL)
6416 inc_cursor();
6417#ifdef FEAT_VIRTUALEDIT
6418 /* If the cursor is still at the same character, also keep
6419 * the "coladd". */
6420 if (gchar_cursor() == NUL
6421 && curwin->w_cursor.lnum == tpos.lnum
6422 && curwin->w_cursor.col == tpos.col)
6423 curwin->w_cursor.coladd = tpos.coladd;
6424#endif
6425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427
6428 /* If a space was inserted for auto-formatting, remove it now. */
6429 check_auto_format(TRUE);
6430
6431 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006432 * of the line, and put the cursor back.
6433 * Do this when ESC was used or moving the cursor up/down. */
6434 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006435 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006437 pos_T tpos = curwin->w_cursor;
6438
6439 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006440 for (;;)
6441 {
6442 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6443 --curwin->w_cursor.col;
6444 cc = gchar_cursor();
6445 if (!vim_iswhite(cc))
6446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006448 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006449 if (curwin->w_cursor.lnum != tpos.lnum)
6450 curwin->w_cursor = tpos;
6451 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6453
6454#ifdef FEAT_VISUAL
6455 /* <C-S-Right> may have started Visual mode, adjust the position for
6456 * deleted characters. */
6457 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6458 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006459 int len = (int)STRLEN(ml_get_curline());
6460
6461 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006463 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464# ifdef FEAT_VIRTUALEDIT
6465 VIsual.coladd = 0;
6466# endif
6467 }
6468 }
6469#endif
6470 }
6471 }
6472 did_ai = FALSE;
6473#ifdef FEAT_SMARTINDENT
6474 did_si = FALSE;
6475 can_si = FALSE;
6476 can_si_back = FALSE;
6477#endif
6478
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006479 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6480 * now in a different buffer. */
6481 if (end_insert_pos != NULL)
6482 {
6483 curbuf->b_op_start = Insstart;
6484 curbuf->b_op_end = *end_insert_pos;
6485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486}
6487
6488/*
6489 * Set the last inserted text to a single character.
6490 * Used for the replace command.
6491 */
6492 void
6493set_last_insert(c)
6494 int c;
6495{
6496 char_u *s;
6497
6498 vim_free(last_insert);
6499#ifdef FEAT_MBYTE
6500 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6501#else
6502 last_insert = alloc(6);
6503#endif
6504 if (last_insert != NULL)
6505 {
6506 s = last_insert;
6507 /* Use the CTRL-V only when entering a special char */
6508 if (c < ' ' || c == DEL)
6509 *s++ = Ctrl_V;
6510 s = add_char2buf(c, s);
6511 *s++ = ESC;
6512 *s++ = NUL;
6513 last_insert_skip = 0;
6514 }
6515}
6516
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006517#if defined(EXITFREE) || defined(PROTO)
6518 void
6519free_last_insert()
6520{
6521 vim_free(last_insert);
6522 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006523# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006524 vim_free(compl_orig_text);
6525 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006526# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006527}
6528#endif
6529
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530/*
6531 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6532 * and CSI. Handle multi-byte characters.
6533 * Returns a pointer to after the added bytes.
6534 */
6535 char_u *
6536add_char2buf(c, s)
6537 int c;
6538 char_u *s;
6539{
6540#ifdef FEAT_MBYTE
6541 char_u temp[MB_MAXBYTES];
6542 int i;
6543 int len;
6544
6545 len = (*mb_char2bytes)(c, temp);
6546 for (i = 0; i < len; ++i)
6547 {
6548 c = temp[i];
6549#endif
6550 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6551 if (c == K_SPECIAL)
6552 {
6553 *s++ = K_SPECIAL;
6554 *s++ = KS_SPECIAL;
6555 *s++ = KE_FILLER;
6556 }
6557#ifdef FEAT_GUI
6558 else if (c == CSI)
6559 {
6560 *s++ = CSI;
6561 *s++ = KS_EXTRA;
6562 *s++ = (int)KE_CSI;
6563 }
6564#endif
6565 else
6566 *s++ = c;
6567#ifdef FEAT_MBYTE
6568 }
6569#endif
6570 return s;
6571}
6572
6573/*
6574 * move cursor to start of line
6575 * if flags & BL_WHITE move to first non-white
6576 * if flags & BL_SOL move to first non-white if startofline is set,
6577 * otherwise keep "curswant" column
6578 * if flags & BL_FIX don't leave the cursor on a NUL.
6579 */
6580 void
6581beginline(flags)
6582 int flags;
6583{
6584 if ((flags & BL_SOL) && !p_sol)
6585 coladvance(curwin->w_curswant);
6586 else
6587 {
6588 curwin->w_cursor.col = 0;
6589#ifdef FEAT_VIRTUALEDIT
6590 curwin->w_cursor.coladd = 0;
6591#endif
6592
6593 if (flags & (BL_WHITE | BL_SOL))
6594 {
6595 char_u *ptr;
6596
6597 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6598 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6599 ++curwin->w_cursor.col;
6600 }
6601 curwin->w_set_curswant = TRUE;
6602 }
6603}
6604
6605/*
6606 * oneright oneleft cursor_down cursor_up
6607 *
6608 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006609 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 * Return OK when successful, FAIL when we hit a line of file boundary.
6611 */
6612
6613 int
6614oneright()
6615{
6616 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619#ifdef FEAT_VIRTUALEDIT
6620 if (virtual_active())
6621 {
6622 pos_T prevpos = curwin->w_cursor;
6623
6624 /* Adjust for multi-wide char (excluding TAB) */
6625 ptr = ml_get_cursor();
6626 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006627# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006629# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 ))
6633 ? ptr2cells(ptr) : 1));
6634 curwin->w_set_curswant = TRUE;
6635 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6636 return (prevpos.col != curwin->w_cursor.col
6637 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6638 }
6639#endif
6640
6641 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006642 if (*ptr == NUL)
6643 return FAIL; /* already at the very end */
6644
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006646 if (has_mbyte)
6647 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 else
6649#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006650 l = 1;
6651
6652 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6653 * contains "onemore". */
6654 if (ptr[l] == NUL
6655#ifdef FEAT_VIRTUALEDIT
6656 && (ve_flags & VE_ONEMORE) == 0
6657#endif
6658 )
6659 return FAIL;
6660 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 curwin->w_set_curswant = TRUE;
6663 return OK;
6664}
6665
6666 int
6667oneleft()
6668{
6669#ifdef FEAT_VIRTUALEDIT
6670 if (virtual_active())
6671 {
6672 int width;
6673 int v = getviscol();
6674
6675 if (v == 0)
6676 return FAIL;
6677
6678# ifdef FEAT_LINEBREAK
6679 /* We might get stuck on 'showbreak', skip over it. */
6680 width = 1;
6681 for (;;)
6682 {
6683 coladvance(v - width);
6684 /* getviscol() is slow, skip it when 'showbreak' is empty and
6685 * there are no multi-byte characters */
6686 if ((*p_sbr == NUL
6687# ifdef FEAT_MBYTE
6688 && !has_mbyte
6689# endif
6690 ) || getviscol() < v)
6691 break;
6692 ++width;
6693 }
6694# else
6695 coladvance(v - 1);
6696# endif
6697
6698 if (curwin->w_cursor.coladd == 1)
6699 {
6700 char_u *ptr;
6701
6702 /* Adjust for multi-wide char (not a TAB) */
6703 ptr = ml_get_cursor();
6704 if (*ptr != TAB && vim_isprintc(
6705# ifdef FEAT_MBYTE
6706 (*mb_ptr2char)(ptr)
6707# else
6708 *ptr
6709# endif
6710 ) && ptr2cells(ptr) > 1)
6711 curwin->w_cursor.coladd = 0;
6712 }
6713
6714 curwin->w_set_curswant = TRUE;
6715 return OK;
6716 }
6717#endif
6718
6719 if (curwin->w_cursor.col == 0)
6720 return FAIL;
6721
6722 curwin->w_set_curswant = TRUE;
6723 --curwin->w_cursor.col;
6724
6725#ifdef FEAT_MBYTE
6726 /* if the character on the left of the current cursor is a multi-byte
6727 * character, move to its first byte */
6728 if (has_mbyte)
6729 mb_adjust_cursor();
6730#endif
6731 return OK;
6732}
6733
6734 int
6735cursor_up(n, upd_topline)
6736 long n;
6737 int upd_topline; /* When TRUE: update topline */
6738{
6739 linenr_T lnum;
6740
6741 if (n > 0)
6742 {
6743 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006744 /* This fails if the cursor is already in the first line or the count
6745 * is larger than the line number and '-' is in 'cpoptions' */
6746 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 return FAIL;
6748 if (n >= lnum)
6749 lnum = 1;
6750 else
6751#ifdef FEAT_FOLDING
6752 if (hasAnyFolding(curwin))
6753 {
6754 /*
6755 * Count each sequence of folded lines as one logical line.
6756 */
6757 /* go to the the start of the current fold */
6758 (void)hasFolding(lnum, &lnum, NULL);
6759
6760 while (n--)
6761 {
6762 /* move up one line */
6763 --lnum;
6764 if (lnum <= 1)
6765 break;
6766 /* If we entered a fold, move to the beginning, unless in
6767 * Insert mode or when 'foldopen' contains "all": it will open
6768 * in a moment. */
6769 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6770 (void)hasFolding(lnum, &lnum, NULL);
6771 }
6772 if (lnum < 1)
6773 lnum = 1;
6774 }
6775 else
6776#endif
6777 lnum -= n;
6778 curwin->w_cursor.lnum = lnum;
6779 }
6780
6781 /* try to advance to the column we want to be at */
6782 coladvance(curwin->w_curswant);
6783
6784 if (upd_topline)
6785 update_topline(); /* make sure curwin->w_topline is valid */
6786
6787 return OK;
6788}
6789
6790/*
6791 * Cursor down a number of logical lines.
6792 */
6793 int
6794cursor_down(n, upd_topline)
6795 long n;
6796 int upd_topline; /* When TRUE: update topline */
6797{
6798 linenr_T lnum;
6799
6800 if (n > 0)
6801 {
6802 lnum = curwin->w_cursor.lnum;
6803#ifdef FEAT_FOLDING
6804 /* Move to last line of fold, will fail if it's the end-of-file. */
6805 (void)hasFolding(lnum, NULL, &lnum);
6806#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006807 /* This fails if the cursor is already in the last line or would move
6808 * beyound the last line and '-' is in 'cpoptions' */
6809 if (lnum >= curbuf->b_ml.ml_line_count
6810 || (lnum + n > curbuf->b_ml.ml_line_count
6811 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 return FAIL;
6813 if (lnum + n >= curbuf->b_ml.ml_line_count)
6814 lnum = curbuf->b_ml.ml_line_count;
6815 else
6816#ifdef FEAT_FOLDING
6817 if (hasAnyFolding(curwin))
6818 {
6819 linenr_T last;
6820
6821 /* count each sequence of folded lines as one logical line */
6822 while (n--)
6823 {
6824 if (hasFolding(lnum, NULL, &last))
6825 lnum = last + 1;
6826 else
6827 ++lnum;
6828 if (lnum >= curbuf->b_ml.ml_line_count)
6829 break;
6830 }
6831 if (lnum > curbuf->b_ml.ml_line_count)
6832 lnum = curbuf->b_ml.ml_line_count;
6833 }
6834 else
6835#endif
6836 lnum += n;
6837 curwin->w_cursor.lnum = lnum;
6838 }
6839
6840 /* try to advance to the column we want to be at */
6841 coladvance(curwin->w_curswant);
6842
6843 if (upd_topline)
6844 update_topline(); /* make sure curwin->w_topline is valid */
6845
6846 return OK;
6847}
6848
6849/*
6850 * Stuff the last inserted text in the read buffer.
6851 * Last_insert actually is a copy of the redo buffer, so we
6852 * first have to remove the command.
6853 */
6854 int
6855stuff_inserted(c, count, no_esc)
6856 int c; /* Command character to be inserted */
6857 long count; /* Repeat this many times */
6858 int no_esc; /* Don't add an ESC at the end */
6859{
6860 char_u *esc_ptr;
6861 char_u *ptr;
6862 char_u *last_ptr;
6863 char_u last = NUL;
6864
6865 ptr = get_last_insert();
6866 if (ptr == NULL)
6867 {
6868 EMSG(_(e_noinstext));
6869 return FAIL;
6870 }
6871
6872 /* may want to stuff the command character, to start Insert mode */
6873 if (c != NUL)
6874 stuffcharReadbuff(c);
6875 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6876 *esc_ptr = NUL; /* remove the ESC */
6877
6878 /* when the last char is either "0" or "^" it will be quoted if no ESC
6879 * comes after it OR if it will inserted more than once and "ptr"
6880 * starts with ^D. -- Acevedo
6881 */
6882 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6883 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6884 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6885 {
6886 last = *last_ptr;
6887 *last_ptr = NUL;
6888 }
6889
6890 do
6891 {
6892 stuffReadbuff(ptr);
6893 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6894 if (last)
6895 stuffReadbuff((char_u *)(last == '0'
6896 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6897 : IF_EB("\026^", CTRL_V_STR "^")));
6898 }
6899 while (--count > 0);
6900
6901 if (last)
6902 *last_ptr = last;
6903
6904 if (esc_ptr != NULL)
6905 *esc_ptr = ESC; /* put the ESC back */
6906
6907 /* may want to stuff a trailing ESC, to get out of Insert mode */
6908 if (!no_esc)
6909 stuffcharReadbuff(ESC);
6910
6911 return OK;
6912}
6913
6914 char_u *
6915get_last_insert()
6916{
6917 if (last_insert == NULL)
6918 return NULL;
6919 return last_insert + last_insert_skip;
6920}
6921
6922/*
6923 * Get last inserted string, and remove trailing <Esc>.
6924 * Returns pointer to allocated memory (must be freed) or NULL.
6925 */
6926 char_u *
6927get_last_insert_save()
6928{
6929 char_u *s;
6930 int len;
6931
6932 if (last_insert == NULL)
6933 return NULL;
6934 s = vim_strsave(last_insert + last_insert_skip);
6935 if (s != NULL)
6936 {
6937 len = (int)STRLEN(s);
6938 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6939 s[len - 1] = NUL;
6940 }
6941 return s;
6942}
6943
6944/*
6945 * Check the word in front of the cursor for an abbreviation.
6946 * Called when the non-id character "c" has been entered.
6947 * When an abbreviation is recognized it is removed from the text and
6948 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6949 */
6950 static int
6951echeck_abbr(c)
6952 int c;
6953{
6954 /* Don't check for abbreviation in paste mode, when disabled and just
6955 * after moving around with cursor keys. */
6956 if (p_paste || no_abbr || arrow_used)
6957 return FALSE;
6958
6959 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6960 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6961}
6962
6963/*
6964 * replace-stack functions
6965 *
6966 * When replacing characters, the replaced characters are remembered for each
6967 * new character. This is used to re-insert the old text when backspacing.
6968 *
6969 * There is a NUL headed list of characters for each character that is
6970 * currently in the file after the insertion point. When BS is used, one NUL
6971 * headed list is put back for the deleted character.
6972 *
6973 * For a newline, there are two NUL headed lists. One contains the characters
6974 * that the NL replaced. The extra one stores the characters after the cursor
6975 * that were deleted (always white space).
6976 *
6977 * Replace_offset is normally 0, in which case replace_push will add a new
6978 * character at the end of the stack. If replace_offset is not 0, that many
6979 * characters will be left on the stack above the newly inserted character.
6980 */
6981
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006982static char_u *replace_stack = NULL;
6983static long replace_stack_nr = 0; /* next entry in replace stack */
6984static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 void
6987replace_push(c)
6988 int c; /* character that is replaced (NUL is none) */
6989{
6990 char_u *p;
6991
6992 if (replace_stack_nr < replace_offset) /* nothing to do */
6993 return;
6994 if (replace_stack_len <= replace_stack_nr)
6995 {
6996 replace_stack_len += 50;
6997 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6998 if (p == NULL) /* out of memory */
6999 {
7000 replace_stack_len -= 50;
7001 return;
7002 }
7003 if (replace_stack != NULL)
7004 {
7005 mch_memmove(p, replace_stack,
7006 (size_t)(replace_stack_nr * sizeof(char_u)));
7007 vim_free(replace_stack);
7008 }
7009 replace_stack = p;
7010 }
7011 p = replace_stack + replace_stack_nr - replace_offset;
7012 if (replace_offset)
7013 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7014 *p = c;
7015 ++replace_stack_nr;
7016}
7017
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007018#if defined(FEAT_MBYTE) || defined(PROTO)
7019/*
7020 * Push a character onto the replace stack. Handles a multi-byte character in
7021 * reverse byte order, so that the first byte is popped off first.
7022 * Return the number of bytes done (includes composing characters).
7023 */
7024 int
7025replace_push_mb(p)
7026 char_u *p;
7027{
7028 int l = (*mb_ptr2len)(p);
7029 int j;
7030
7031 for (j = l - 1; j >= 0; --j)
7032 replace_push(p[j]);
7033 return l;
7034}
7035#endif
7036
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007037#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038/*
7039 * call replace_push(c) with replace_offset set to the first NUL.
7040 */
7041 static void
7042replace_push_off(c)
7043 int c;
7044{
7045 char_u *p;
7046
7047 p = replace_stack + replace_stack_nr;
7048 for (replace_offset = 1; replace_offset < replace_stack_nr;
7049 ++replace_offset)
7050 if (*--p == NUL)
7051 break;
7052 replace_push(c);
7053 replace_offset = 0;
7054}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057/*
7058 * Pop one item from the replace stack.
7059 * return -1 if stack empty
7060 * return replaced character or NUL otherwise
7061 */
7062 static int
7063replace_pop()
7064{
7065 if (replace_stack_nr == 0)
7066 return -1;
7067 return (int)replace_stack[--replace_stack_nr];
7068}
7069
7070/*
7071 * Join the top two items on the replace stack. This removes to "off"'th NUL
7072 * encountered.
7073 */
7074 static void
7075replace_join(off)
7076 int off; /* offset for which NUL to remove */
7077{
7078 int i;
7079
7080 for (i = replace_stack_nr; --i >= 0; )
7081 if (replace_stack[i] == NUL && off-- <= 0)
7082 {
7083 --replace_stack_nr;
7084 mch_memmove(replace_stack + i, replace_stack + i + 1,
7085 (size_t)(replace_stack_nr - i));
7086 return;
7087 }
7088}
7089
7090/*
7091 * Pop bytes from the replace stack until a NUL is found, and insert them
7092 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7093 */
7094 static void
7095replace_pop_ins()
7096{
7097 int cc;
7098 int oldState = State;
7099
7100 State = NORMAL; /* don't want REPLACE here */
7101 while ((cc = replace_pop()) > 0)
7102 {
7103#ifdef FEAT_MBYTE
7104 mb_replace_pop_ins(cc);
7105#else
7106 ins_char(cc);
7107#endif
7108 dec_cursor();
7109 }
7110 State = oldState;
7111}
7112
7113#ifdef FEAT_MBYTE
7114/*
7115 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7116 * indicates a multi-byte char, pop the other bytes too.
7117 */
7118 static void
7119mb_replace_pop_ins(cc)
7120 int cc;
7121{
7122 int n;
7123 char_u buf[MB_MAXBYTES];
7124 int i;
7125 int c;
7126
7127 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7128 {
7129 buf[0] = cc;
7130 for (i = 1; i < n; ++i)
7131 buf[i] = replace_pop();
7132 ins_bytes_len(buf, n);
7133 }
7134 else
7135 ins_char(cc);
7136
7137 if (enc_utf8)
7138 /* Handle composing chars. */
7139 for (;;)
7140 {
7141 c = replace_pop();
7142 if (c == -1) /* stack empty */
7143 break;
7144 if ((n = MB_BYTE2LEN(c)) == 1)
7145 {
7146 /* Not a multi-byte char, put it back. */
7147 replace_push(c);
7148 break;
7149 }
7150 else
7151 {
7152 buf[0] = c;
7153 for (i = 1; i < n; ++i)
7154 buf[i] = replace_pop();
7155 if (utf_iscomposing(utf_ptr2char(buf)))
7156 ins_bytes_len(buf, n);
7157 else
7158 {
7159 /* Not a composing char, put it back. */
7160 for (i = n - 1; i >= 0; --i)
7161 replace_push(buf[i]);
7162 break;
7163 }
7164 }
7165 }
7166}
7167#endif
7168
7169/*
7170 * make the replace stack empty
7171 * (called when exiting replace mode)
7172 */
7173 static void
7174replace_flush()
7175{
7176 vim_free(replace_stack);
7177 replace_stack = NULL;
7178 replace_stack_len = 0;
7179 replace_stack_nr = 0;
7180}
7181
7182/*
7183 * Handle doing a BS for one character.
7184 * cc < 0: replace stack empty, just move cursor
7185 * cc == 0: character was inserted, delete it
7186 * cc > 0: character was replaced, put cc (first byte of original char) back
7187 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007188 * When "limit_col" is >= 0, don't delete before this column. Matters when
7189 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 */
7191 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007192replace_do_bs(limit_col)
7193 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 int cc;
7196#ifdef FEAT_VREPLACE
7197 int orig_len = 0;
7198 int ins_len;
7199 int orig_vcols = 0;
7200 colnr_T start_vcol;
7201 char_u *p;
7202 int i;
7203 int vcol;
7204#endif
7205
7206 cc = replace_pop();
7207 if (cc > 0)
7208 {
7209#ifdef FEAT_VREPLACE
7210 if (State & VREPLACE_FLAG)
7211 {
7212 /* Get the number of screen cells used by the character we are
7213 * going to delete. */
7214 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7215 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7216 }
7217#endif
7218#ifdef FEAT_MBYTE
7219 if (has_mbyte)
7220 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007221 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222# ifdef FEAT_VREPLACE
7223 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007224 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225# endif
7226 replace_push(cc);
7227 }
7228 else
7229#endif
7230 {
7231 pchar_cursor(cc);
7232#ifdef FEAT_VREPLACE
7233 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007234 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235#endif
7236 }
7237 replace_pop_ins();
7238
7239#ifdef FEAT_VREPLACE
7240 if (State & VREPLACE_FLAG)
7241 {
7242 /* Get the number of screen cells used by the inserted characters */
7243 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007244 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 vcol = start_vcol;
7246 for (i = 0; i < ins_len; ++i)
7247 {
7248 vcol += chartabsize(p + i, vcol);
7249#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007250 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251#endif
7252 }
7253 vcol -= start_vcol;
7254
7255 /* Delete spaces that were inserted after the cursor to keep the
7256 * text aligned. */
7257 curwin->w_cursor.col += ins_len;
7258 while (vcol > orig_vcols && gchar_cursor() == ' ')
7259 {
7260 del_char(FALSE);
7261 ++orig_vcols;
7262 }
7263 curwin->w_cursor.col -= ins_len;
7264 }
7265#endif
7266
7267 /* mark the buffer as changed and prepare for displaying */
7268 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7269 }
7270 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007271 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272}
7273
7274#ifdef FEAT_CINDENT
7275/*
7276 * Return TRUE if C-indenting is on.
7277 */
7278 static int
7279cindent_on()
7280{
7281 return (!p_paste && (curbuf->b_p_cin
7282# ifdef FEAT_EVAL
7283 || *curbuf->b_p_inde != NUL
7284# endif
7285 ));
7286}
7287#endif
7288
7289#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7290/*
7291 * Re-indent the current line, based on the current contents of it and the
7292 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7293 * confused what all the part that handles Control-T is doing that I'm not.
7294 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7295 */
7296
7297 void
7298fixthisline(get_the_indent)
7299 int (*get_the_indent) __ARGS((void));
7300{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007301 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if (linewhite(curwin->w_cursor.lnum))
7303 did_ai = TRUE; /* delete the indent if the line stays empty */
7304}
7305
7306 void
7307fix_indent()
7308{
7309 if (p_paste)
7310 return;
7311# ifdef FEAT_LISP
7312 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7313 fixthisline(get_lisp_indent);
7314# endif
7315# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7316 else
7317# endif
7318# ifdef FEAT_CINDENT
7319 if (cindent_on())
7320 do_c_expr_indent();
7321# endif
7322}
7323
7324#endif
7325
7326#ifdef FEAT_CINDENT
7327/*
7328 * return TRUE if 'cinkeys' contains the key "keytyped",
7329 * when == '*': Only if key is preceded with '*' (indent before insert)
7330 * when == '!': Only if key is prededed with '!' (don't insert)
7331 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7332 *
7333 * "keytyped" can have a few special values:
7334 * KEY_OPEN_FORW
7335 * KEY_OPEN_BACK
7336 * KEY_COMPLETE just finished completion.
7337 *
7338 * If line_is_empty is TRUE accept keys with '0' before them.
7339 */
7340 int
7341in_cinkeys(keytyped, when, line_is_empty)
7342 int keytyped;
7343 int when;
7344 int line_is_empty;
7345{
7346 char_u *look;
7347 int try_match;
7348 int try_match_word;
7349 char_u *p;
7350 char_u *line;
7351 int icase;
7352 int i;
7353
7354#ifdef FEAT_EVAL
7355 if (*curbuf->b_p_inde != NUL)
7356 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7357 else
7358#endif
7359 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7360 while (*look)
7361 {
7362 /*
7363 * Find out if we want to try a match with this key, depending on
7364 * 'when' and a '*' or '!' before the key.
7365 */
7366 switch (when)
7367 {
7368 case '*': try_match = (*look == '*'); break;
7369 case '!': try_match = (*look == '!'); break;
7370 default: try_match = (*look != '*'); break;
7371 }
7372 if (*look == '*' || *look == '!')
7373 ++look;
7374
7375 /*
7376 * If there is a '0', only accept a match if the line is empty.
7377 * But may still match when typing last char of a word.
7378 */
7379 if (*look == '0')
7380 {
7381 try_match_word = try_match;
7382 if (!line_is_empty)
7383 try_match = FALSE;
7384 ++look;
7385 }
7386 else
7387 try_match_word = FALSE;
7388
7389 /*
7390 * does it look like a control character?
7391 */
7392 if (*look == '^'
7393#ifdef EBCDIC
7394 && (Ctrl_chr(look[1]) != 0)
7395#else
7396 && look[1] >= '?' && look[1] <= '_'
7397#endif
7398 )
7399 {
7400 if (try_match && keytyped == Ctrl_chr(look[1]))
7401 return TRUE;
7402 look += 2;
7403 }
7404 /*
7405 * 'o' means "o" command, open forward.
7406 * 'O' means "O" command, open backward.
7407 */
7408 else if (*look == 'o')
7409 {
7410 if (try_match && keytyped == KEY_OPEN_FORW)
7411 return TRUE;
7412 ++look;
7413 }
7414 else if (*look == 'O')
7415 {
7416 if (try_match && keytyped == KEY_OPEN_BACK)
7417 return TRUE;
7418 ++look;
7419 }
7420
7421 /*
7422 * 'e' means to check for "else" at start of line and just before the
7423 * cursor.
7424 */
7425 else if (*look == 'e')
7426 {
7427 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7428 {
7429 p = ml_get_curline();
7430 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7431 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7432 return TRUE;
7433 }
7434 ++look;
7435 }
7436
7437 /*
7438 * ':' only causes an indent if it is at the end of a label or case
7439 * statement, or when it was before typing the ':' (to fix
7440 * class::method for C++).
7441 */
7442 else if (*look == ':')
7443 {
7444 if (try_match && keytyped == ':')
7445 {
7446 p = ml_get_curline();
7447 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7448 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007449 /* Need to get the line again after cin_islabel(). */
7450 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 if (curwin->w_cursor.col > 2
7452 && p[curwin->w_cursor.col - 1] == ':'
7453 && p[curwin->w_cursor.col - 2] == ':')
7454 {
7455 p[curwin->w_cursor.col - 1] = ' ';
7456 i = (cin_iscase(p) || cin_isscopedecl(p)
7457 || cin_islabel(30));
7458 p = ml_get_curline();
7459 p[curwin->w_cursor.col - 1] = ':';
7460 if (i)
7461 return TRUE;
7462 }
7463 }
7464 ++look;
7465 }
7466
7467
7468 /*
7469 * Is it a key in <>, maybe?
7470 */
7471 else if (*look == '<')
7472 {
7473 if (try_match)
7474 {
7475 /*
7476 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7477 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7478 * >, *, : and ! keys if they really really want to.
7479 */
7480 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7481 && keytyped == look[1])
7482 return TRUE;
7483
7484 if (keytyped == get_special_key_code(look + 1))
7485 return TRUE;
7486 }
7487 while (*look && *look != '>')
7488 look++;
7489 while (*look == '>')
7490 look++;
7491 }
7492
7493 /*
7494 * Is it a word: "=word"?
7495 */
7496 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7497 {
7498 ++look;
7499 if (*look == '~')
7500 {
7501 icase = TRUE;
7502 ++look;
7503 }
7504 else
7505 icase = FALSE;
7506 p = vim_strchr(look, ',');
7507 if (p == NULL)
7508 p = look + STRLEN(look);
7509 if ((try_match || try_match_word)
7510 && curwin->w_cursor.col >= (colnr_T)(p - look))
7511 {
7512 int match = FALSE;
7513
7514#ifdef FEAT_INS_EXPAND
7515 if (keytyped == KEY_COMPLETE)
7516 {
7517 char_u *s;
7518
7519 /* Just completed a word, check if it starts with "look".
7520 * search back for the start of a word. */
7521 line = ml_get_curline();
7522# ifdef FEAT_MBYTE
7523 if (has_mbyte)
7524 {
7525 char_u *n;
7526
7527 for (s = line + curwin->w_cursor.col; s > line; s = n)
7528 {
7529 n = mb_prevptr(line, s);
7530 if (!vim_iswordp(n))
7531 break;
7532 }
7533 }
7534 else
7535# endif
7536 for (s = line + curwin->w_cursor.col; s > line; --s)
7537 if (!vim_iswordc(s[-1]))
7538 break;
7539 if (s + (p - look) <= line + curwin->w_cursor.col
7540 && (icase
7541 ? MB_STRNICMP(s, look, p - look)
7542 : STRNCMP(s, look, p - look)) == 0)
7543 match = TRUE;
7544 }
7545 else
7546#endif
7547 /* TODO: multi-byte */
7548 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7549 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7550 {
7551 line = ml_get_cursor();
7552 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7553 || !vim_iswordc(line[-(p - look) - 1]))
7554 && (icase
7555 ? MB_STRNICMP(line - (p - look), look, p - look)
7556 : STRNCMP(line - (p - look), look, p - look))
7557 == 0)
7558 match = TRUE;
7559 }
7560 if (match && try_match_word && !try_match)
7561 {
7562 /* "0=word": Check if there are only blanks before the
7563 * word. */
7564 line = ml_get_curline();
7565 if ((int)(skipwhite(line) - line) !=
7566 (int)(curwin->w_cursor.col - (p - look)))
7567 match = FALSE;
7568 }
7569 if (match)
7570 return TRUE;
7571 }
7572 look = p;
7573 }
7574
7575 /*
7576 * ok, it's a boring generic character.
7577 */
7578 else
7579 {
7580 if (try_match && *look == keytyped)
7581 return TRUE;
7582 ++look;
7583 }
7584
7585 /*
7586 * Skip over ", ".
7587 */
7588 look = skip_to_option_part(look);
7589 }
7590 return FALSE;
7591}
7592#endif /* FEAT_CINDENT */
7593
7594#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7595/*
7596 * Map Hebrew keyboard when in hkmap mode.
7597 */
7598 int
7599hkmap(c)
7600 int c;
7601{
7602 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7603 {
7604 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7605 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7606 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7607 static char_u map[26] =
7608 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7609 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7610 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7611 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7612 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7613 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7614 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7615 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7616 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7617
7618 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7619 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7620 /* '-1'='sofit' */
7621 else if (c == 'x')
7622 return 'X';
7623 else if (c == 'q')
7624 return '\''; /* {geresh}={'} */
7625 else if (c == 246)
7626 return ' '; /* \"o --> ' ' for a german keyboard */
7627 else if (c == 228)
7628 return ' '; /* \"a --> ' ' -- / -- */
7629 else if (c == 252)
7630 return ' '; /* \"u --> ' ' -- / -- */
7631#ifdef EBCDIC
7632 else if (islower(c))
7633#else
7634 /* NOTE: islower() does not do the right thing for us on Linux so we
7635 * do this the same was as 5.7 and previous, so it works correctly on
7636 * all systems. Specifically, the e.g. Delete and Arrow keys are
7637 * munged and won't work if e.g. searching for Hebrew text.
7638 */
7639 else if (c >= 'a' && c <= 'z')
7640#endif
7641 return (int)(map[CharOrdLow(c)] + p_aleph);
7642 else
7643 return c;
7644 }
7645 else
7646 {
7647 switch (c)
7648 {
7649 case '`': return ';';
7650 case '/': return '.';
7651 case '\'': return ',';
7652 case 'q': return '/';
7653 case 'w': return '\'';
7654
7655 /* Hebrew letters - set offset from 'a' */
7656 case ',': c = '{'; break;
7657 case '.': c = 'v'; break;
7658 case ';': c = 't'; break;
7659 default: {
7660 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7661
7662#ifdef EBCDIC
7663 /* see note about islower() above */
7664 if (!islower(c))
7665#else
7666 if (c < 'a' || c > 'z')
7667#endif
7668 return c;
7669 c = str[CharOrdLow(c)];
7670 break;
7671 }
7672 }
7673
7674 return (int)(CharOrdLow(c) + p_aleph);
7675 }
7676}
7677#endif
7678
7679 static void
7680ins_reg()
7681{
7682 int need_redraw = FALSE;
7683 int regname;
7684 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007685#ifdef FEAT_VISUAL
7686 int vis_active = VIsual_active;
7687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688
7689 /*
7690 * If we are going to wait for a character, show a '"'.
7691 */
7692 pc_status = PC_STATUS_UNSET;
7693 if (redrawing() && !char_avail())
7694 {
7695 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007696 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
7698 edit_putchar('"', TRUE);
7699#ifdef FEAT_CMDL_INFO
7700 add_to_showcmd_c(Ctrl_R);
7701#endif
7702 }
7703
7704#ifdef USE_ON_FLY_SCROLL
7705 dont_scroll = TRUE; /* disallow scrolling here */
7706#endif
7707
7708 /*
7709 * Don't map the register name. This also prevents the mode message to be
7710 * deleted when ESC is hit.
7711 */
7712 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007713 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7716 {
7717 /* Get a third key for literal register insertion */
7718 literally = regname;
7719#ifdef FEAT_CMDL_INFO
7720 add_to_showcmd_c(literally);
7721#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007722 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725 --no_mapping;
7726
7727#ifdef FEAT_EVAL
7728 /*
7729 * Don't call u_sync() while getting the expression,
7730 * evaluating it or giving an error message for it!
7731 */
7732 ++no_u_sync;
7733 if (regname == '=')
7734 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007735# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007739# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 /* Restore the Input Method. */
7741 if (im_on)
7742 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007745 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7746 {
7747 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 else
7751 {
7752#endif
7753 if (literally == Ctrl_O || literally == Ctrl_P)
7754 {
7755 /* Append the command to the redo buffer. */
7756 AppendCharToRedobuff(Ctrl_R);
7757 AppendCharToRedobuff(literally);
7758 AppendCharToRedobuff(regname);
7759
7760 do_put(regname, BACKWARD, 1L,
7761 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7762 }
7763 else if (insert_reg(regname, literally) == FAIL)
7764 {
7765 vim_beep();
7766 need_redraw = TRUE; /* remove the '"' */
7767 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007768 else if (stop_insert_mode)
7769 /* When the '=' register was used and a function was invoked that
7770 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7771 * insert anything, need to remove the '"' */
7772 need_redraw = TRUE;
7773
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774#ifdef FEAT_EVAL
7775 }
7776 --no_u_sync;
7777#endif
7778#ifdef FEAT_CMDL_INFO
7779 clear_showcmd();
7780#endif
7781
7782 /* If the inserted register is empty, we need to remove the '"' */
7783 if (need_redraw || stuff_empty())
7784 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007785
7786#ifdef FEAT_VISUAL
7787 /* Disallow starting Visual mode here, would get a weird mode. */
7788 if (!vis_active && VIsual_active)
7789 end_visual_mode();
7790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791}
7792
7793/*
7794 * CTRL-G commands in Insert mode.
7795 */
7796 static void
7797ins_ctrl_g()
7798{
7799 int c;
7800
7801#ifdef FEAT_INS_EXPAND
7802 /* Right after CTRL-X the cursor will be after the ruler. */
7803 setcursor();
7804#endif
7805
7806 /*
7807 * Don't map the second key. This also prevents the mode message to be
7808 * deleted when ESC is hit.
7809 */
7810 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007811 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 --no_mapping;
7813 switch (c)
7814 {
7815 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7816 case K_UP:
7817 case Ctrl_K:
7818 case 'k': ins_up(TRUE);
7819 break;
7820
7821 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7822 case K_DOWN:
7823 case Ctrl_J:
7824 case 'j': ins_down(TRUE);
7825 break;
7826
7827 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007828 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007830
7831 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007832 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007833 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 break;
7835
7836 /* Unknown CTRL-G command, reserved for future expansion. */
7837 default: vim_beep();
7838 }
7839}
7840
7841/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007842 * CTRL-^ in Insert mode.
7843 */
7844 static void
7845ins_ctrl_hat()
7846{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007847 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007848 {
7849 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7850 if (State & LANGMAP)
7851 {
7852 curbuf->b_p_iminsert = B_IMODE_NONE;
7853 State &= ~LANGMAP;
7854 }
7855 else
7856 {
7857 curbuf->b_p_iminsert = B_IMODE_LMAP;
7858 State |= LANGMAP;
7859#ifdef USE_IM_CONTROL
7860 im_set_active(FALSE);
7861#endif
7862 }
7863 }
7864#ifdef USE_IM_CONTROL
7865 else
7866 {
7867 /* There are no ":lmap" mappings, toggle IM */
7868 if (im_get_status())
7869 {
7870 curbuf->b_p_iminsert = B_IMODE_NONE;
7871 im_set_active(FALSE);
7872 }
7873 else
7874 {
7875 curbuf->b_p_iminsert = B_IMODE_IM;
7876 State &= ~LANGMAP;
7877 im_set_active(TRUE);
7878 }
7879 }
7880#endif
7881 set_iminsert_global();
7882 showmode();
7883#ifdef FEAT_GUI
7884 /* may show different cursor shape or color */
7885 if (gui.in_use)
7886 gui_update_cursor(TRUE, FALSE);
7887#endif
7888#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7889 /* Show/unshow value of 'keymap' in status lines. */
7890 status_redraw_curbuf();
7891#endif
7892}
7893
7894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 * Handle ESC in insert mode.
7896 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7897 * insert.
7898 */
7899 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007900ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 long *count;
7902 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007903 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 int temp;
7906 static int disabled_redraw = FALSE;
7907
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007908#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007909 check_spell_redraw();
7910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911#if defined(FEAT_HANGULIN)
7912# if defined(ESC_CHG_TO_ENG_MODE)
7913 hangul_input_state_set(0);
7914# endif
7915 if (composing_hangul)
7916 {
7917 push_raw_key(composing_hangul_buffer, 2);
7918 composing_hangul = 0;
7919 }
7920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921
7922 temp = curwin->w_cursor.col;
7923 if (disabled_redraw)
7924 {
7925 --RedrawingDisabled;
7926 disabled_redraw = FALSE;
7927 }
7928 if (!arrow_used)
7929 {
7930 /*
7931 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007932 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7933 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 */
7935 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007936 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
7938 /*
7939 * Repeating insert may take a long time. Check for
7940 * interrupt now and then.
7941 */
7942 if (*count > 0)
7943 {
7944 line_breakcheck();
7945 if (got_int)
7946 *count = 0;
7947 }
7948
7949 if (--*count > 0) /* repeat what was typed */
7950 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007951 /* Vi repeats the insert without replacing characters. */
7952 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7953 State &= ~REPLACE_FLAG;
7954
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 (void)start_redo_ins();
7956 if (cmdchar == 'r' || cmdchar == 'v')
7957 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7958 ++RedrawingDisabled;
7959 disabled_redraw = TRUE;
7960 return FALSE; /* repeat the insert */
7961 }
7962 stop_insert(&curwin->w_cursor, TRUE);
7963 undisplay_dollar();
7964 }
7965
7966 /* When an autoindent was removed, curswant stays after the
7967 * indent */
7968 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7969 curwin->w_set_curswant = TRUE;
7970
7971 /* Remember the last Insert position in the '^ mark. */
7972 if (!cmdmod.keepjumps)
7973 curbuf->b_last_insert = curwin->w_cursor;
7974
7975 /*
7976 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007977 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007979 if (!nomove
7980 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981#ifdef FEAT_VIRTUALEDIT
7982 || curwin->w_cursor.coladd > 0
7983#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007984 )
7985 && (restart_edit == NUL
7986 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007988 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007990 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991#ifdef FEAT_RIGHTLEFT
7992 && !revins_on
7993#endif
7994 )
7995 {
7996#ifdef FEAT_VIRTUALEDIT
7997 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7998 {
7999 oneleft();
8000 if (restart_edit != NUL)
8001 ++curwin->w_cursor.coladd;
8002 }
8003 else
8004#endif
8005 {
8006 --curwin->w_cursor.col;
8007#ifdef FEAT_MBYTE
8008 /* Correct cursor for multi-byte character. */
8009 if (has_mbyte)
8010 mb_adjust_cursor();
8011#endif
8012 }
8013 }
8014
8015#ifdef USE_IM_CONTROL
8016 /* Disable IM to allow typing English directly for Normal mode commands.
8017 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8018 * well). */
8019 if (!(State & LANGMAP))
8020 im_save_status(&curbuf->b_p_iminsert);
8021 im_set_active(FALSE);
8022#endif
8023
8024 State = NORMAL;
8025 /* need to position cursor again (e.g. when on a TAB ) */
8026 changed_cline_bef_curs();
8027
8028#ifdef FEAT_MOUSE
8029 setmouse();
8030#endif
8031#ifdef CURSOR_SHAPE
8032 ui_cursor_shape(); /* may show different cursor shape */
8033#endif
8034
8035 /*
8036 * When recording or for CTRL-O, need to display the new mode.
8037 * Otherwise remove the mode message.
8038 */
8039 if (Recording || restart_edit != NUL)
8040 showmode();
8041 else if (p_smd)
8042 MSG("");
8043
8044 return TRUE; /* exit Insert mode */
8045}
8046
8047#ifdef FEAT_RIGHTLEFT
8048/*
8049 * Toggle language: hkmap and revins_on.
8050 * Move to end of reverse inserted text.
8051 */
8052 static void
8053ins_ctrl_()
8054{
8055 if (revins_on && revins_chars && revins_scol >= 0)
8056 {
8057 while (gchar_cursor() != NUL && revins_chars--)
8058 ++curwin->w_cursor.col;
8059 }
8060 p_ri = !p_ri;
8061 revins_on = (State == INSERT && p_ri);
8062 if (revins_on)
8063 {
8064 revins_scol = curwin->w_cursor.col;
8065 revins_legal++;
8066 revins_chars = 0;
8067 undisplay_dollar();
8068 }
8069 else
8070 revins_scol = -1;
8071#ifdef FEAT_FKMAP
8072 if (p_altkeymap)
8073 {
8074 /*
8075 * to be consistent also for redo command, using '.'
8076 * set arrow_used to true and stop it - causing to redo
8077 * characters entered in one mode (normal/reverse insert).
8078 */
8079 arrow_used = TRUE;
8080 (void)stop_arrow();
8081 p_fkmap = curwin->w_p_rl ^ p_ri;
8082 if (p_fkmap && p_ri)
8083 State = INSERT;
8084 }
8085 else
8086#endif
8087 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8088 showmode();
8089}
8090#endif
8091
8092#ifdef FEAT_VISUAL
8093/*
8094 * If 'keymodel' contains "startsel", may start selection.
8095 * Returns TRUE when a CTRL-O and other keys stuffed.
8096 */
8097 static int
8098ins_start_select(c)
8099 int c;
8100{
8101 if (km_startsel)
8102 switch (c)
8103 {
8104 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 case K_PAGEUP:
8107 case K_KPAGEUP:
8108 case K_PAGEDOWN:
8109 case K_KPAGEDOWN:
8110# ifdef MACOS
8111 case K_LEFT:
8112 case K_RIGHT:
8113 case K_UP:
8114 case K_DOWN:
8115 case K_END:
8116 case K_HOME:
8117# endif
8118 if (!(mod_mask & MOD_MASK_SHIFT))
8119 break;
8120 /* FALLTHROUGH */
8121 case K_S_LEFT:
8122 case K_S_RIGHT:
8123 case K_S_UP:
8124 case K_S_DOWN:
8125 case K_S_END:
8126 case K_S_HOME:
8127 /* Start selection right away, the cursor can move with
8128 * CTRL-O when beyond the end of the line. */
8129 start_selection();
8130
8131 /* Execute the key in (insert) Select mode. */
8132 stuffcharReadbuff(Ctrl_O);
8133 if (mod_mask)
8134 {
8135 char_u buf[4];
8136
8137 buf[0] = K_SPECIAL;
8138 buf[1] = KS_MODIFIER;
8139 buf[2] = mod_mask;
8140 buf[3] = NUL;
8141 stuffReadbuff(buf);
8142 }
8143 stuffcharReadbuff(c);
8144 return TRUE;
8145 }
8146 return FALSE;
8147}
8148#endif
8149
8150/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008151 * <Insert> key in Insert mode: toggle insert/remplace mode.
8152 */
8153 static void
8154ins_insert(replaceState)
8155 int replaceState;
8156{
8157#ifdef FEAT_FKMAP
8158 if (p_fkmap && p_ri)
8159 {
8160 beep_flush();
8161 EMSG(farsi_text_3); /* encoded in Farsi */
8162 return;
8163 }
8164#endif
8165
8166#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008167# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008168 set_vim_var_string(VV_INSERTMODE,
8169 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008170# ifdef FEAT_VREPLACE
8171 replaceState == VREPLACE ? "v" :
8172# endif
8173 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008174# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008175 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8176#endif
8177 if (State & REPLACE_FLAG)
8178 State = INSERT | (State & LANGMAP);
8179 else
8180 State = replaceState | (State & LANGMAP);
8181 AppendCharToRedobuff(K_INS);
8182 showmode();
8183#ifdef CURSOR_SHAPE
8184 ui_cursor_shape(); /* may show different cursor shape */
8185#endif
8186}
8187
8188/*
8189 * Pressed CTRL-O in Insert mode.
8190 */
8191 static void
8192ins_ctrl_o()
8193{
8194#ifdef FEAT_VREPLACE
8195 if (State & VREPLACE_FLAG)
8196 restart_edit = 'V';
8197 else
8198#endif
8199 if (State & REPLACE_FLAG)
8200 restart_edit = 'R';
8201 else
8202 restart_edit = 'I';
8203#ifdef FEAT_VIRTUALEDIT
8204 if (virtual_active())
8205 ins_at_eol = FALSE; /* cursor always keeps its column */
8206 else
8207#endif
8208 ins_at_eol = (gchar_cursor() == NUL);
8209}
8210
8211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 * If the cursor is on an indent, ^T/^D insert/delete one
8213 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008214 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 * with vi. But vi only supports ^T and ^D after an
8216 * autoindent, we support it everywhere.
8217 */
8218 static void
8219ins_shift(c, lastc)
8220 int c;
8221 int lastc;
8222{
8223 if (stop_arrow() == FAIL)
8224 return;
8225 AppendCharToRedobuff(c);
8226
8227 /*
8228 * 0^D and ^^D: remove all indent.
8229 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008230 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8231 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
8233 --curwin->w_cursor.col;
8234 (void)del_char(FALSE); /* delete the '^' or '0' */
8235 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8236 if (State & REPLACE_FLAG)
8237 replace_pop_ins();
8238 if (lastc == '^')
8239 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008240 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 }
8242 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008243 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244
8245 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8246 did_ai = FALSE;
8247#ifdef FEAT_SMARTINDENT
8248 did_si = FALSE;
8249 can_si = FALSE;
8250 can_si_back = FALSE;
8251#endif
8252#ifdef FEAT_CINDENT
8253 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8254#endif
8255}
8256
8257 static void
8258ins_del()
8259{
8260 int temp;
8261
8262 if (stop_arrow() == FAIL)
8263 return;
8264 if (gchar_cursor() == NUL) /* delete newline */
8265 {
8266 temp = curwin->w_cursor.col;
8267 if (!can_bs(BS_EOL) /* only if "eol" included */
8268 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8269 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8270 || do_join(FALSE) == FAIL)
8271 vim_beep();
8272 else
8273 curwin->w_cursor.col = temp;
8274 }
8275 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8276 vim_beep();
8277 did_ai = FALSE;
8278#ifdef FEAT_SMARTINDENT
8279 did_si = FALSE;
8280 can_si = FALSE;
8281 can_si_back = FALSE;
8282#endif
8283 AppendCharToRedobuff(K_DEL);
8284}
8285
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008286static void ins_bs_one __ARGS((colnr_T *vcolp));
8287
8288/*
8289 * Delete one character for ins_bs().
8290 */
8291 static void
8292ins_bs_one(vcolp)
8293 colnr_T *vcolp;
8294{
8295 dec_cursor();
8296 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8297 if (State & REPLACE_FLAG)
8298 {
8299 /* Don't delete characters before the insert point when in
8300 * Replace mode */
8301 if (curwin->w_cursor.lnum != Insstart.lnum
8302 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008303 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008304 }
8305 else
8306 (void)del_char(FALSE);
8307}
8308
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309/*
8310 * Handle Backspace, delete-word and delete-line in Insert mode.
8311 * Return TRUE when backspace was actually used.
8312 */
8313 static int
8314ins_bs(c, mode, inserted_space_p)
8315 int c;
8316 int mode;
8317 int *inserted_space_p;
8318{
8319 linenr_T lnum;
8320 int cc;
8321 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008322 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 colnr_T mincol;
8324 int did_backspace = FALSE;
8325 int in_indent;
8326 int oldState;
8327#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008328 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329#endif
8330
8331 /*
8332 * can't delete anything in an empty file
8333 * can't backup past first character in buffer
8334 * can't backup past starting point unless 'backspace' > 1
8335 * can backup to a previous line if 'backspace' == 0
8336 */
8337 if ( bufempty()
8338 || (
8339#ifdef FEAT_RIGHTLEFT
8340 !revins_on &&
8341#endif
8342 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8343 || (!can_bs(BS_START)
8344 && (arrow_used
8345 || (curwin->w_cursor.lnum == Insstart.lnum
8346 && curwin->w_cursor.col <= Insstart.col)))
8347 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8348 && curwin->w_cursor.col <= ai_col)
8349 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8350 {
8351 vim_beep();
8352 return FALSE;
8353 }
8354
8355 if (stop_arrow() == FAIL)
8356 return FALSE;
8357 in_indent = inindent(0);
8358#ifdef FEAT_CINDENT
8359 if (in_indent)
8360 can_cindent = FALSE;
8361#endif
8362#ifdef FEAT_COMMENTS
8363 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8364#endif
8365#ifdef FEAT_RIGHTLEFT
8366 if (revins_on) /* put cursor after last inserted char */
8367 inc_cursor();
8368#endif
8369
8370#ifdef FEAT_VIRTUALEDIT
8371 /* Virtualedit:
8372 * BACKSPACE_CHAR eats a virtual space
8373 * BACKSPACE_WORD eats all coladd
8374 * BACKSPACE_LINE eats all coladd and keeps going
8375 */
8376 if (curwin->w_cursor.coladd > 0)
8377 {
8378 if (mode == BACKSPACE_CHAR)
8379 {
8380 --curwin->w_cursor.coladd;
8381 return TRUE;
8382 }
8383 if (mode == BACKSPACE_WORD)
8384 {
8385 curwin->w_cursor.coladd = 0;
8386 return TRUE;
8387 }
8388 curwin->w_cursor.coladd = 0;
8389 }
8390#endif
8391
8392 /*
8393 * delete newline!
8394 */
8395 if (curwin->w_cursor.col == 0)
8396 {
8397 lnum = Insstart.lnum;
8398 if (curwin->w_cursor.lnum == Insstart.lnum
8399#ifdef FEAT_RIGHTLEFT
8400 || revins_on
8401#endif
8402 )
8403 {
8404 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8405 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8406 return FALSE;
8407 --Insstart.lnum;
8408 Insstart.col = MAXCOL;
8409 }
8410 /*
8411 * In replace mode:
8412 * cc < 0: NL was inserted, delete it
8413 * cc >= 0: NL was replaced, put original characters back
8414 */
8415 cc = -1;
8416 if (State & REPLACE_FLAG)
8417 cc = replace_pop(); /* returns -1 if NL was inserted */
8418 /*
8419 * In replace mode, in the line we started replacing, we only move the
8420 * cursor.
8421 */
8422 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8423 {
8424 dec_cursor();
8425 }
8426 else
8427 {
8428#ifdef FEAT_VREPLACE
8429 if (!(State & VREPLACE_FLAG)
8430 || curwin->w_cursor.lnum > orig_line_count)
8431#endif
8432 {
8433 temp = gchar_cursor(); /* remember current char */
8434 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008435
8436 /* When "aw" is in 'formatoptions' we must delete the space at
8437 * the end of the line, otherwise the line will be broken
8438 * again when auto-formatting. */
8439 if (has_format_option(FO_AUTO)
8440 && has_format_option(FO_WHITE_PAR))
8441 {
8442 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8443 TRUE);
8444 int len;
8445
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008446 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008447 if (len > 0 && ptr[len - 1] == ' ')
8448 ptr[len - 1] = NUL;
8449 }
8450
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 (void)do_join(FALSE);
8452 if (temp == NUL && gchar_cursor() != NUL)
8453 inc_cursor();
8454 }
8455#ifdef FEAT_VREPLACE
8456 else
8457 dec_cursor();
8458#endif
8459
8460 /*
8461 * In REPLACE mode we have to put back the text that was replaced
8462 * by the NL. On the replace stack is first a NUL-terminated
8463 * sequence of characters that were deleted and then the
8464 * characters that NL replaced.
8465 */
8466 if (State & REPLACE_FLAG)
8467 {
8468 /*
8469 * Do the next ins_char() in NORMAL state, to
8470 * prevent ins_char() from replacing characters and
8471 * avoiding showmatch().
8472 */
8473 oldState = State;
8474 State = NORMAL;
8475 /*
8476 * restore characters (blanks) deleted after cursor
8477 */
8478 while (cc > 0)
8479 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008480 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481#ifdef FEAT_MBYTE
8482 mb_replace_pop_ins(cc);
8483#else
8484 ins_char(cc);
8485#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008486 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 cc = replace_pop();
8488 }
8489 /* restore the characters that NL replaced */
8490 replace_pop_ins();
8491 State = oldState;
8492 }
8493 }
8494 did_ai = FALSE;
8495 }
8496 else
8497 {
8498 /*
8499 * Delete character(s) before the cursor.
8500 */
8501#ifdef FEAT_RIGHTLEFT
8502 if (revins_on) /* put cursor on last inserted char */
8503 dec_cursor();
8504#endif
8505 mincol = 0;
8506 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008507 if (mode == BACKSPACE_LINE
8508 && (curbuf->b_p_ai
8509#ifdef FEAT_CINDENT
8510 || cindent_on()
8511#endif
8512 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513#ifdef FEAT_RIGHTLEFT
8514 && !revins_on
8515#endif
8516 )
8517 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008518 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 beginline(BL_WHITE);
8520 if (curwin->w_cursor.col < (colnr_T)temp)
8521 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008522 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 }
8524
8525 /*
8526 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8527 */
8528 if ( mode == BACKSPACE_CHAR
8529 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008530 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008531 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 && (*(ml_get_cursor() - 1) == TAB
8533 || (*(ml_get_cursor() - 1) == ' '
8534 && (!*inserted_space_p
8535 || arrow_used))))))
8536 {
8537 int ts;
8538 colnr_T vcol;
8539 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008540 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541
8542 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008543 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 ts = curbuf->b_p_sw;
8545 else
8546 ts = curbuf->b_p_sts;
8547 /* Compute the virtual column where we want to be. Since
8548 * 'showbreak' may get in the way, need to get the last column of
8549 * the previous character. */
8550 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008551 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 dec_cursor();
8553 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8554 inc_cursor();
8555 want_vcol = (want_vcol / ts) * ts;
8556
8557 /* delete characters until we are at or before want_vcol */
8558 while (vcol > want_vcol
8559 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008560 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
8562 /* insert extra spaces until we are at want_vcol */
8563 while (vcol < want_vcol)
8564 {
8565 /* Remember the first char we inserted */
8566 if (curwin->w_cursor.lnum == Insstart.lnum
8567 && curwin->w_cursor.col < Insstart.col)
8568 Insstart.col = curwin->w_cursor.col;
8569
8570#ifdef FEAT_VREPLACE
8571 if (State & VREPLACE_FLAG)
8572 ins_char(' ');
8573 else
8574#endif
8575 {
8576 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008577 if ((State & REPLACE_FLAG))
8578 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 }
8580 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8581 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008582
8583 /* If we are now back where we started delete one character. Can
8584 * happen when using 'sts' and 'linebreak'. */
8585 if (vcol >= start_vcol)
8586 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 }
8588
8589 /*
8590 * Delete upto starting point, start of line or previous word.
8591 */
8592 else do
8593 {
8594#ifdef FEAT_RIGHTLEFT
8595 if (!revins_on) /* put cursor on char to be deleted */
8596#endif
8597 dec_cursor();
8598
8599 /* start of word? */
8600 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8601 {
8602 mode = BACKSPACE_WORD_NOT_SPACE;
8603 temp = vim_iswordc(gchar_cursor());
8604 }
8605 /* end of word? */
8606 else if (mode == BACKSPACE_WORD_NOT_SPACE
8607 && (vim_isspace(cc = gchar_cursor())
8608 || vim_iswordc(cc) != temp))
8609 {
8610#ifdef FEAT_RIGHTLEFT
8611 if (!revins_on)
8612#endif
8613 inc_cursor();
8614#ifdef FEAT_RIGHTLEFT
8615 else if (State & REPLACE_FLAG)
8616 dec_cursor();
8617#endif
8618 break;
8619 }
8620 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008621 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 else
8623 {
8624#ifdef FEAT_MBYTE
8625 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008626 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#endif
8628 (void)del_char(FALSE);
8629#ifdef FEAT_MBYTE
8630 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008631 * If there are combining characters and 'delcombine' is set
8632 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 * character.
8634 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008635 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 inc_cursor();
8637#endif
8638#ifdef FEAT_RIGHTLEFT
8639 if (revins_chars)
8640 {
8641 revins_chars--;
8642 revins_legal++;
8643 }
8644 if (revins_on && gchar_cursor() == NUL)
8645 break;
8646#endif
8647 }
8648 /* Just a single backspace?: */
8649 if (mode == BACKSPACE_CHAR)
8650 break;
8651 } while (
8652#ifdef FEAT_RIGHTLEFT
8653 revins_on ||
8654#endif
8655 (curwin->w_cursor.col > mincol
8656 && (curwin->w_cursor.lnum != Insstart.lnum
8657 || curwin->w_cursor.col != Insstart.col)));
8658 did_backspace = TRUE;
8659 }
8660#ifdef FEAT_SMARTINDENT
8661 did_si = FALSE;
8662 can_si = FALSE;
8663 can_si_back = FALSE;
8664#endif
8665 if (curwin->w_cursor.col <= 1)
8666 did_ai = FALSE;
8667 /*
8668 * It's a little strange to put backspaces into the redo
8669 * buffer, but it makes auto-indent a lot easier to deal
8670 * with.
8671 */
8672 AppendCharToRedobuff(c);
8673
8674 /* If deleted before the insertion point, adjust it */
8675 if (curwin->w_cursor.lnum == Insstart.lnum
8676 && curwin->w_cursor.col < Insstart.col)
8677 Insstart.col = curwin->w_cursor.col;
8678
8679 /* vi behaviour: the cursor moves backward but the character that
8680 * was there remains visible
8681 * Vim behaviour: the cursor moves backward and the character that
8682 * was there is erased from the screen.
8683 * We can emulate the vi behaviour by pretending there is a dollar
8684 * displayed even when there isn't.
8685 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8686 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8687 dollar_vcol = curwin->w_virtcol;
8688
Bram Moolenaarce3be472008-01-14 19:12:28 +00008689#ifdef FEAT_FOLDING
8690 /* When deleting a char the cursor line must never be in a closed fold.
8691 * E.g., when 'foldmethod' is indent and deleting the first non-white
8692 * char before a Tab. */
8693 if (did_backspace)
8694 foldOpenCursor();
8695#endif
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 return did_backspace;
8698}
8699
8700#ifdef FEAT_MOUSE
8701 static void
8702ins_mouse(c)
8703 int c;
8704{
8705 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008706 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707
8708# ifdef FEAT_GUI
8709 /* When GUI is active, also move/paste when 'mouse' is empty */
8710 if (!gui.in_use)
8711# endif
8712 if (!mouse_has(MOUSE_INSERT))
8713 return;
8714
8715 undisplay_dollar();
8716 tpos = curwin->w_cursor;
8717 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8718 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008719#ifdef FEAT_WINDOWS
8720 win_T *new_curwin = curwin;
8721
8722 if (curwin != old_curwin && win_valid(old_curwin))
8723 {
8724 /* Mouse took us to another window. We need to go back to the
8725 * previous one to stop insert there properly. */
8726 curwin = old_curwin;
8727 curbuf = curwin->w_buffer;
8728 }
8729#endif
8730 start_arrow(curwin == old_curwin ? &tpos : NULL);
8731#ifdef FEAT_WINDOWS
8732 if (curwin != new_curwin && win_valid(new_curwin))
8733 {
8734 curwin = new_curwin;
8735 curbuf = curwin->w_buffer;
8736 }
8737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738# ifdef FEAT_CINDENT
8739 can_cindent = TRUE;
8740# endif
8741 }
8742
8743#ifdef FEAT_WINDOWS
8744 /* redraw status lines (in case another window became active) */
8745 redraw_statuslines();
8746#endif
8747}
8748
8749 static void
8750ins_mousescroll(up)
8751 int up;
8752{
8753 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008754# if defined(FEAT_WINDOWS)
8755 win_T *old_curwin = curwin;
8756# endif
8757# ifdef FEAT_INS_EXPAND
8758 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759# endif
8760
8761 tpos = curwin->w_cursor;
8762
8763# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 /* Currently the mouse coordinates are only known in the GUI. */
8765 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8766 {
8767 int row, col;
8768
8769 row = mouse_row;
8770 col = mouse_col;
8771
8772 /* find the window at the pointer coordinates */
8773 curwin = mouse_find_win(&row, &col);
8774 curbuf = curwin->w_buffer;
8775 }
8776 if (curwin == old_curwin)
8777# endif
8778 undisplay_dollar();
8779
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008780# ifdef FEAT_INS_EXPAND
8781 /* Don't scroll the window in which completion is being done. */
8782 if (!pum_visible()
8783# if defined(FEAT_WINDOWS)
8784 || curwin != old_curwin
8785# endif
8786 )
8787# endif
8788 {
8789 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8790 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8791 else
8792 scroll_redraw(up, 3L);
8793# ifdef FEAT_INS_EXPAND
8794 did_scroll = TRUE;
8795# endif
8796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
8798# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8799 curwin->w_redr_status = TRUE;
8800
8801 curwin = old_curwin;
8802 curbuf = curwin->w_buffer;
8803# endif
8804
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008805# ifdef FEAT_INS_EXPAND
8806 /* The popup menu may overlay the window, need to redraw it.
8807 * TODO: Would be more efficient to only redraw the windows that are
8808 * overlapped by the popup menu. */
8809 if (pum_visible() && did_scroll)
8810 {
8811 redraw_all_later(NOT_VALID);
8812 ins_compl_show_pum();
8813 }
8814# endif
8815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (!equalpos(curwin->w_cursor, tpos))
8817 {
8818 start_arrow(&tpos);
8819# ifdef FEAT_CINDENT
8820 can_cindent = TRUE;
8821# endif
8822 }
8823}
8824#endif
8825
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008826#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008827 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008828ins_tabline(c)
8829 int c;
8830{
8831 /* We will be leaving the current window, unless closing another tab. */
8832 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8833 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8834 {
8835 undisplay_dollar();
8836 start_arrow(&curwin->w_cursor);
8837# ifdef FEAT_CINDENT
8838 can_cindent = TRUE;
8839# endif
8840 }
8841
8842 if (c == K_TABLINE)
8843 goto_tabpage(current_tab);
8844 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008845 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008846 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008847 redraw_statuslines(); /* will redraw the tabline when needed */
8848 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008849}
8850#endif
8851
8852#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 void
8854ins_scroll()
8855{
8856 pos_T tpos;
8857
8858 undisplay_dollar();
8859 tpos = curwin->w_cursor;
8860 if (gui_do_scroll())
8861 {
8862 start_arrow(&tpos);
8863# ifdef FEAT_CINDENT
8864 can_cindent = TRUE;
8865# endif
8866 }
8867}
8868
8869 void
8870ins_horscroll()
8871{
8872 pos_T tpos;
8873
8874 undisplay_dollar();
8875 tpos = curwin->w_cursor;
8876 if (gui_do_horiz_scroll())
8877 {
8878 start_arrow(&tpos);
8879# ifdef FEAT_CINDENT
8880 can_cindent = TRUE;
8881# endif
8882 }
8883}
8884#endif
8885
8886 static void
8887ins_left()
8888{
8889 pos_T tpos;
8890
8891#ifdef FEAT_FOLDING
8892 if ((fdo_flags & FDO_HOR) && KeyTyped)
8893 foldOpenCursor();
8894#endif
8895 undisplay_dollar();
8896 tpos = curwin->w_cursor;
8897 if (oneleft() == OK)
8898 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008899#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8900 /* Only call start_arrow() when not busy with preediting, it will
8901 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8902 if (!im_is_preediting())
8903#endif
8904 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905#ifdef FEAT_RIGHTLEFT
8906 /* If exit reversed string, position is fixed */
8907 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8908 revins_legal++;
8909 revins_chars++;
8910#endif
8911 }
8912
8913 /*
8914 * if 'whichwrap' set for cursor in insert mode may go to
8915 * previous line
8916 */
8917 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8918 {
8919 start_arrow(&tpos);
8920 --(curwin->w_cursor.lnum);
8921 coladvance((colnr_T)MAXCOL);
8922 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8923 }
8924 else
8925 vim_beep();
8926}
8927
8928 static void
8929ins_home(c)
8930 int c;
8931{
8932 pos_T tpos;
8933
8934#ifdef FEAT_FOLDING
8935 if ((fdo_flags & FDO_HOR) && KeyTyped)
8936 foldOpenCursor();
8937#endif
8938 undisplay_dollar();
8939 tpos = curwin->w_cursor;
8940 if (c == K_C_HOME)
8941 curwin->w_cursor.lnum = 1;
8942 curwin->w_cursor.col = 0;
8943#ifdef FEAT_VIRTUALEDIT
8944 curwin->w_cursor.coladd = 0;
8945#endif
8946 curwin->w_curswant = 0;
8947 start_arrow(&tpos);
8948}
8949
8950 static void
8951ins_end(c)
8952 int c;
8953{
8954 pos_T tpos;
8955
8956#ifdef FEAT_FOLDING
8957 if ((fdo_flags & FDO_HOR) && KeyTyped)
8958 foldOpenCursor();
8959#endif
8960 undisplay_dollar();
8961 tpos = curwin->w_cursor;
8962 if (c == K_C_END)
8963 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8964 coladvance((colnr_T)MAXCOL);
8965 curwin->w_curswant = MAXCOL;
8966
8967 start_arrow(&tpos);
8968}
8969
8970 static void
8971ins_s_left()
8972{
8973#ifdef FEAT_FOLDING
8974 if ((fdo_flags & FDO_HOR) && KeyTyped)
8975 foldOpenCursor();
8976#endif
8977 undisplay_dollar();
8978 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8979 {
8980 start_arrow(&curwin->w_cursor);
8981 (void)bck_word(1L, FALSE, FALSE);
8982 curwin->w_set_curswant = TRUE;
8983 }
8984 else
8985 vim_beep();
8986}
8987
8988 static void
8989ins_right()
8990{
8991#ifdef FEAT_FOLDING
8992 if ((fdo_flags & FDO_HOR) && KeyTyped)
8993 foldOpenCursor();
8994#endif
8995 undisplay_dollar();
8996 if (gchar_cursor() != NUL || virtual_active()
8997 )
8998 {
8999 start_arrow(&curwin->w_cursor);
9000 curwin->w_set_curswant = TRUE;
9001#ifdef FEAT_VIRTUALEDIT
9002 if (virtual_active())
9003 oneright();
9004 else
9005#endif
9006 {
9007#ifdef FEAT_MBYTE
9008 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009009 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 else
9011#endif
9012 ++curwin->w_cursor.col;
9013 }
9014
9015#ifdef FEAT_RIGHTLEFT
9016 revins_legal++;
9017 if (revins_chars)
9018 revins_chars--;
9019#endif
9020 }
9021 /* if 'whichwrap' set for cursor in insert mode, may move the
9022 * cursor to the next line */
9023 else if (vim_strchr(p_ww, ']') != NULL
9024 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9025 {
9026 start_arrow(&curwin->w_cursor);
9027 curwin->w_set_curswant = TRUE;
9028 ++curwin->w_cursor.lnum;
9029 curwin->w_cursor.col = 0;
9030 }
9031 else
9032 vim_beep();
9033}
9034
9035 static void
9036ins_s_right()
9037{
9038#ifdef FEAT_FOLDING
9039 if ((fdo_flags & FDO_HOR) && KeyTyped)
9040 foldOpenCursor();
9041#endif
9042 undisplay_dollar();
9043 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9044 || gchar_cursor() != NUL)
9045 {
9046 start_arrow(&curwin->w_cursor);
9047 (void)fwd_word(1L, FALSE, 0);
9048 curwin->w_set_curswant = TRUE;
9049 }
9050 else
9051 vim_beep();
9052}
9053
9054 static void
9055ins_up(startcol)
9056 int startcol; /* when TRUE move to Insstart.col */
9057{
9058 pos_T tpos;
9059 linenr_T old_topline = curwin->w_topline;
9060#ifdef FEAT_DIFF
9061 int old_topfill = curwin->w_topfill;
9062#endif
9063
9064 undisplay_dollar();
9065 tpos = curwin->w_cursor;
9066 if (cursor_up(1L, TRUE) == OK)
9067 {
9068 if (startcol)
9069 coladvance(getvcol_nolist(&Insstart));
9070 if (old_topline != curwin->w_topline
9071#ifdef FEAT_DIFF
9072 || old_topfill != curwin->w_topfill
9073#endif
9074 )
9075 redraw_later(VALID);
9076 start_arrow(&tpos);
9077#ifdef FEAT_CINDENT
9078 can_cindent = TRUE;
9079#endif
9080 }
9081 else
9082 vim_beep();
9083}
9084
9085 static void
9086ins_pageup()
9087{
9088 pos_T tpos;
9089
9090 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009091
9092#ifdef FEAT_WINDOWS
9093 if (mod_mask & MOD_MASK_CTRL)
9094 {
9095 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009096 if (first_tabpage->tp_next != NULL)
9097 {
9098 start_arrow(&curwin->w_cursor);
9099 goto_tabpage(-1);
9100 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009101 return;
9102 }
9103#endif
9104
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 tpos = curwin->w_cursor;
9106 if (onepage(BACKWARD, 1L) == OK)
9107 {
9108 start_arrow(&tpos);
9109#ifdef FEAT_CINDENT
9110 can_cindent = TRUE;
9111#endif
9112 }
9113 else
9114 vim_beep();
9115}
9116
9117 static void
9118ins_down(startcol)
9119 int startcol; /* when TRUE move to Insstart.col */
9120{
9121 pos_T tpos;
9122 linenr_T old_topline = curwin->w_topline;
9123#ifdef FEAT_DIFF
9124 int old_topfill = curwin->w_topfill;
9125#endif
9126
9127 undisplay_dollar();
9128 tpos = curwin->w_cursor;
9129 if (cursor_down(1L, TRUE) == OK)
9130 {
9131 if (startcol)
9132 coladvance(getvcol_nolist(&Insstart));
9133 if (old_topline != curwin->w_topline
9134#ifdef FEAT_DIFF
9135 || old_topfill != curwin->w_topfill
9136#endif
9137 )
9138 redraw_later(VALID);
9139 start_arrow(&tpos);
9140#ifdef FEAT_CINDENT
9141 can_cindent = TRUE;
9142#endif
9143 }
9144 else
9145 vim_beep();
9146}
9147
9148 static void
9149ins_pagedown()
9150{
9151 pos_T tpos;
9152
9153 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009154
9155#ifdef FEAT_WINDOWS
9156 if (mod_mask & MOD_MASK_CTRL)
9157 {
9158 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009159 if (first_tabpage->tp_next != NULL)
9160 {
9161 start_arrow(&curwin->w_cursor);
9162 goto_tabpage(0);
9163 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009164 return;
9165 }
9166#endif
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 tpos = curwin->w_cursor;
9169 if (onepage(FORWARD, 1L) == OK)
9170 {
9171 start_arrow(&tpos);
9172#ifdef FEAT_CINDENT
9173 can_cindent = TRUE;
9174#endif
9175 }
9176 else
9177 vim_beep();
9178}
9179
9180#ifdef FEAT_DND
9181 static void
9182ins_drop()
9183{
9184 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9185}
9186#endif
9187
9188/*
9189 * Handle TAB in Insert or Replace mode.
9190 * Return TRUE when the TAB needs to be inserted like a normal character.
9191 */
9192 static int
9193ins_tab()
9194{
9195 int ind;
9196 int i;
9197 int temp;
9198
9199 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9200 Insstart_blank_vcol = get_nolist_virtcol();
9201 if (echeck_abbr(TAB + ABBR_OFF))
9202 return FALSE;
9203
9204 ind = inindent(0);
9205#ifdef FEAT_CINDENT
9206 if (ind)
9207 can_cindent = FALSE;
9208#endif
9209
9210 /*
9211 * When nothing special, insert TAB like a normal character
9212 */
9213 if (!curbuf->b_p_et
9214 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9215 && curbuf->b_p_sts == 0)
9216 return TRUE;
9217
9218 if (stop_arrow() == FAIL)
9219 return TRUE;
9220
9221 did_ai = FALSE;
9222#ifdef FEAT_SMARTINDENT
9223 did_si = FALSE;
9224 can_si = FALSE;
9225 can_si_back = FALSE;
9226#endif
9227 AppendToRedobuff((char_u *)"\t");
9228
9229 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9230 temp = (int)curbuf->b_p_sw;
9231 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9232 temp = (int)curbuf->b_p_sts;
9233 else /* otherwise use 'tabstop' */
9234 temp = (int)curbuf->b_p_ts;
9235 temp -= get_nolist_virtcol() % temp;
9236
9237 /*
9238 * Insert the first space with ins_char(). It will delete one char in
9239 * replace mode. Insert the rest with ins_str(); it will not delete any
9240 * chars. For VREPLACE mode, we use ins_char() for all characters.
9241 */
9242 ins_char(' ');
9243 while (--temp > 0)
9244 {
9245#ifdef FEAT_VREPLACE
9246 if (State & VREPLACE_FLAG)
9247 ins_char(' ');
9248 else
9249#endif
9250 {
9251 ins_str((char_u *)" ");
9252 if (State & REPLACE_FLAG) /* no char replaced */
9253 replace_push(NUL);
9254 }
9255 }
9256
9257 /*
9258 * When 'expandtab' not set: Replace spaces by TABs where possible.
9259 */
9260 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9261 {
9262 char_u *ptr;
9263#ifdef FEAT_VREPLACE
9264 char_u *saved_line = NULL; /* init for GCC */
9265 pos_T pos;
9266#endif
9267 pos_T fpos;
9268 pos_T *cursor;
9269 colnr_T want_vcol, vcol;
9270 int change_col = -1;
9271 int save_list = curwin->w_p_list;
9272
9273 /*
9274 * Get the current line. For VREPLACE mode, don't make real changes
9275 * yet, just work on a copy of the line.
9276 */
9277#ifdef FEAT_VREPLACE
9278 if (State & VREPLACE_FLAG)
9279 {
9280 pos = curwin->w_cursor;
9281 cursor = &pos;
9282 saved_line = vim_strsave(ml_get_curline());
9283 if (saved_line == NULL)
9284 return FALSE;
9285 ptr = saved_line + pos.col;
9286 }
9287 else
9288#endif
9289 {
9290 ptr = ml_get_cursor();
9291 cursor = &curwin->w_cursor;
9292 }
9293
9294 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9295 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9296 curwin->w_p_list = FALSE;
9297
9298 /* Find first white before the cursor */
9299 fpos = curwin->w_cursor;
9300 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9301 {
9302 --fpos.col;
9303 --ptr;
9304 }
9305
9306 /* In Replace mode, don't change characters before the insert point. */
9307 if ((State & REPLACE_FLAG)
9308 && fpos.lnum == Insstart.lnum
9309 && fpos.col < Insstart.col)
9310 {
9311 ptr += Insstart.col - fpos.col;
9312 fpos.col = Insstart.col;
9313 }
9314
9315 /* compute virtual column numbers of first white and cursor */
9316 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9317 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9318
9319 /* Use as many TABs as possible. Beware of 'showbreak' and
9320 * 'linebreak' adding extra virtual columns. */
9321 while (vim_iswhite(*ptr))
9322 {
9323 i = lbr_chartabsize((char_u *)"\t", vcol);
9324 if (vcol + i > want_vcol)
9325 break;
9326 if (*ptr != TAB)
9327 {
9328 *ptr = TAB;
9329 if (change_col < 0)
9330 {
9331 change_col = fpos.col; /* Column of first change */
9332 /* May have to adjust Insstart */
9333 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9334 Insstart.col = fpos.col;
9335 }
9336 }
9337 ++fpos.col;
9338 ++ptr;
9339 vcol += i;
9340 }
9341
9342 if (change_col >= 0)
9343 {
9344 int repl_off = 0;
9345
9346 /* Skip over the spaces we need. */
9347 while (vcol < want_vcol && *ptr == ' ')
9348 {
9349 vcol += lbr_chartabsize(ptr, vcol);
9350 ++ptr;
9351 ++repl_off;
9352 }
9353 if (vcol > want_vcol)
9354 {
9355 /* Must have a char with 'showbreak' just before it. */
9356 --ptr;
9357 --repl_off;
9358 }
9359 fpos.col += repl_off;
9360
9361 /* Delete following spaces. */
9362 i = cursor->col - fpos.col;
9363 if (i > 0)
9364 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009365 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 /* correct replace stack. */
9367 if ((State & REPLACE_FLAG)
9368#ifdef FEAT_VREPLACE
9369 && !(State & VREPLACE_FLAG)
9370#endif
9371 )
9372 for (temp = i; --temp >= 0; )
9373 replace_join(repl_off);
9374 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009375#ifdef FEAT_NETBEANS_INTG
9376 if (usingNetbeans)
9377 {
9378 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9379 (long)(i + 1));
9380 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9381 (char_u *)"\t", 1);
9382 }
9383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 cursor->col -= i;
9385
9386#ifdef FEAT_VREPLACE
9387 /*
9388 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9389 * backspacing over the changed spacing and then inserting the new
9390 * spacing.
9391 */
9392 if (State & VREPLACE_FLAG)
9393 {
9394 /* Backspace from real cursor to change_col */
9395 backspace_until_column(change_col);
9396
9397 /* Insert each char in saved_line from changed_col to
9398 * ptr-cursor */
9399 ins_bytes_len(saved_line + change_col,
9400 cursor->col - change_col);
9401 }
9402#endif
9403 }
9404
9405#ifdef FEAT_VREPLACE
9406 if (State & VREPLACE_FLAG)
9407 vim_free(saved_line);
9408#endif
9409 curwin->w_p_list = save_list;
9410 }
9411
9412 return FALSE;
9413}
9414
9415/*
9416 * Handle CR or NL in insert mode.
9417 * Return TRUE when out of memory or can't undo.
9418 */
9419 static int
9420ins_eol(c)
9421 int c;
9422{
9423 int i;
9424
9425 if (echeck_abbr(c + ABBR_OFF))
9426 return FALSE;
9427 if (stop_arrow() == FAIL)
9428 return TRUE;
9429 undisplay_dollar();
9430
9431 /*
9432 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9433 * character under the cursor. Only push a NUL on the replace stack,
9434 * nothing to put back when the NL is deleted.
9435 */
9436 if ((State & REPLACE_FLAG)
9437#ifdef FEAT_VREPLACE
9438 && !(State & VREPLACE_FLAG)
9439#endif
9440 )
9441 replace_push(NUL);
9442
9443 /*
9444 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9445 * replacing the next line, so we push all of the characters left on the
9446 * line onto the replace stack. This is not done here though, it is done
9447 * in open_line().
9448 */
9449
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009450#ifdef FEAT_VIRTUALEDIT
9451 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9452 * CTRL-O). */
9453 if (virtual_active() && curwin->w_cursor.coladd > 0)
9454 coladvance(getviscol());
9455#endif
9456
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#ifdef FEAT_RIGHTLEFT
9458# ifdef FEAT_FKMAP
9459 if (p_altkeymap && p_fkmap)
9460 fkmap(NL);
9461# endif
9462 /* NL in reverse insert will always start in the end of
9463 * current line. */
9464 if (revins_on)
9465 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9466#endif
9467
9468 AppendToRedobuff(NL_STR);
9469 i = open_line(FORWARD,
9470#ifdef FEAT_COMMENTS
9471 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9472#endif
9473 0, old_indent);
9474 old_indent = 0;
9475#ifdef FEAT_CINDENT
9476 can_cindent = TRUE;
9477#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009478#ifdef FEAT_FOLDING
9479 /* When inserting a line the cursor line must never be in a closed fold. */
9480 foldOpenCursor();
9481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
9483 return (!i);
9484}
9485
9486#ifdef FEAT_DIGRAPHS
9487/*
9488 * Handle digraph in insert mode.
9489 * Returns character still to be inserted, or NUL when nothing remaining to be
9490 * done.
9491 */
9492 static int
9493ins_digraph()
9494{
9495 int c;
9496 int cc;
9497
9498 pc_status = PC_STATUS_UNSET;
9499 if (redrawing() && !char_avail())
9500 {
9501 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009502 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503
9504 edit_putchar('?', TRUE);
9505#ifdef FEAT_CMDL_INFO
9506 add_to_showcmd_c(Ctrl_K);
9507#endif
9508 }
9509
9510#ifdef USE_ON_FLY_SCROLL
9511 dont_scroll = TRUE; /* disallow scrolling here */
9512#endif
9513
9514 /* don't map the digraph chars. This also prevents the
9515 * mode message to be deleted when ESC is hit */
9516 ++no_mapping;
9517 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009518 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 --no_mapping;
9520 --allow_keys;
9521 if (IS_SPECIAL(c) || mod_mask) /* special key */
9522 {
9523#ifdef FEAT_CMDL_INFO
9524 clear_showcmd();
9525#endif
9526 insert_special(c, TRUE, FALSE);
9527 return NUL;
9528 }
9529 if (c != ESC)
9530 {
9531 if (redrawing() && !char_avail())
9532 {
9533 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009534 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535
9536 if (char2cells(c) == 1)
9537 {
9538 /* first remove the '?', otherwise it's restored when typing
9539 * an ESC next */
9540 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009541 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 edit_putchar(c, TRUE);
9543 }
9544#ifdef FEAT_CMDL_INFO
9545 add_to_showcmd_c(c);
9546#endif
9547 }
9548 ++no_mapping;
9549 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009550 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 --no_mapping;
9552 --allow_keys;
9553 if (cc != ESC)
9554 {
9555 AppendToRedobuff((char_u *)CTRL_V_STR);
9556 c = getdigraph(c, cc, TRUE);
9557#ifdef FEAT_CMDL_INFO
9558 clear_showcmd();
9559#endif
9560 return c;
9561 }
9562 }
9563 edit_unputchar();
9564#ifdef FEAT_CMDL_INFO
9565 clear_showcmd();
9566#endif
9567 return NUL;
9568}
9569#endif
9570
9571/*
9572 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9573 * Returns the char to be inserted, or NUL if none found.
9574 */
9575 static int
9576ins_copychar(lnum)
9577 linenr_T lnum;
9578{
9579 int c;
9580 int temp;
9581 char_u *ptr, *prev_ptr;
9582
9583 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9584 {
9585 vim_beep();
9586 return NUL;
9587 }
9588
9589 /* try to advance to the cursor column */
9590 temp = 0;
9591 ptr = ml_get(lnum);
9592 prev_ptr = ptr;
9593 validate_virtcol();
9594 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9595 {
9596 prev_ptr = ptr;
9597 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9598 }
9599 if ((colnr_T)temp > curwin->w_virtcol)
9600 ptr = prev_ptr;
9601
9602#ifdef FEAT_MBYTE
9603 c = (*mb_ptr2char)(ptr);
9604#else
9605 c = *ptr;
9606#endif
9607 if (c == NUL)
9608 vim_beep();
9609 return c;
9610}
9611
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009612/*
9613 * CTRL-Y or CTRL-E typed in Insert mode.
9614 */
9615 static int
9616ins_ctrl_ey(tc)
9617 int tc;
9618{
9619 int c = tc;
9620
9621#ifdef FEAT_INS_EXPAND
9622 if (ctrl_x_mode == CTRL_X_SCROLL)
9623 {
9624 if (c == Ctrl_Y)
9625 scrolldown_clamp();
9626 else
9627 scrollup_clamp();
9628 redraw_later(VALID);
9629 }
9630 else
9631#endif
9632 {
9633 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9634 if (c != NUL)
9635 {
9636 long tw_save;
9637
9638 /* The character must be taken literally, insert like it
9639 * was typed after a CTRL-V, and pretend 'textwidth'
9640 * wasn't set. Digits, 'o' and 'x' are special after a
9641 * CTRL-V, don't use it for these. */
9642 if (c < 256 && !isalnum(c))
9643 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9644 tw_save = curbuf->b_p_tw;
9645 curbuf->b_p_tw = -1;
9646 insert_special(c, TRUE, FALSE);
9647 curbuf->b_p_tw = tw_save;
9648#ifdef FEAT_RIGHTLEFT
9649 revins_chars++;
9650 revins_legal++;
9651#endif
9652 c = Ctrl_V; /* pretend CTRL-V is last character */
9653 auto_format(FALSE, TRUE);
9654 }
9655 }
9656 return c;
9657}
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659#ifdef FEAT_SMARTINDENT
9660/*
9661 * Try to do some very smart auto-indenting.
9662 * Used when inserting a "normal" character.
9663 */
9664 static void
9665ins_try_si(c)
9666 int c;
9667{
9668 pos_T *pos, old_pos;
9669 char_u *ptr;
9670 int i;
9671 int temp;
9672
9673 /*
9674 * do some very smart indenting when entering '{' or '}'
9675 */
9676 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9677 {
9678 /*
9679 * for '}' set indent equal to indent of line containing matching '{'
9680 */
9681 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9682 {
9683 old_pos = curwin->w_cursor;
9684 /*
9685 * If the matching '{' has a ')' immediately before it (ignoring
9686 * white-space), then line up with the start of the line
9687 * containing the matching '(' if there is one. This handles the
9688 * case where an "if (..\n..) {" statement continues over multiple
9689 * lines -- webb
9690 */
9691 ptr = ml_get(pos->lnum);
9692 i = pos->col;
9693 if (i > 0) /* skip blanks before '{' */
9694 while (--i > 0 && vim_iswhite(ptr[i]))
9695 ;
9696 curwin->w_cursor.lnum = pos->lnum;
9697 curwin->w_cursor.col = i;
9698 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9699 curwin->w_cursor = *pos;
9700 i = get_indent();
9701 curwin->w_cursor = old_pos;
9702#ifdef FEAT_VREPLACE
9703 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009704 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 else
9706#endif
9707 (void)set_indent(i, SIN_CHANGED);
9708 }
9709 else if (curwin->w_cursor.col > 0)
9710 {
9711 /*
9712 * when inserting '{' after "O" reduce indent, but not
9713 * more than indent of previous line
9714 */
9715 temp = TRUE;
9716 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9717 {
9718 old_pos = curwin->w_cursor;
9719 i = get_indent();
9720 while (curwin->w_cursor.lnum > 1)
9721 {
9722 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9723
9724 /* ignore empty lines and lines starting with '#'. */
9725 if (*ptr != '#' && *ptr != NUL)
9726 break;
9727 }
9728 if (get_indent() >= i)
9729 temp = FALSE;
9730 curwin->w_cursor = old_pos;
9731 }
9732 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009733 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 }
9735 }
9736
9737 /*
9738 * set indent of '#' always to 0
9739 */
9740 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9741 {
9742 /* remember current indent for next line */
9743 old_indent = get_indent();
9744 (void)set_indent(0, SIN_CHANGED);
9745 }
9746
9747 /* Adjust ai_col, the char at this position can be deleted. */
9748 if (ai_col > curwin->w_cursor.col)
9749 ai_col = curwin->w_cursor.col;
9750}
9751#endif
9752
9753/*
9754 * Get the value that w_virtcol would have when 'list' is off.
9755 * Unless 'cpo' contains the 'L' flag.
9756 */
9757 static colnr_T
9758get_nolist_virtcol()
9759{
9760 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9761 return getvcol_nolist(&curwin->w_cursor);
9762 validate_virtcol();
9763 return curwin->w_virtcol;
9764}