blob: f528b9649bc7e1fafb8fad8ad808207be99db3ce [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
60static char_u e_hitend[] = N_("Hit end of paragraph");
61
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 Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000132static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000133static 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 +0000134static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000135static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000136static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000138static void ins_compl_upd_pum __ARGS((void));
139static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000140static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000141static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000142static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000143static 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 +0000144static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static void ins_compl_free __ARGS((void));
146static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000147static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000148static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000149static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000150static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000151static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000152static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000153static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000155#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
156static void ins_compl_add_list __ARGS((list_T *list));
157#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000158static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static void ins_compl_delete __ARGS((void));
160static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000161static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000162static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000164static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000165static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static int ins_complete __ARGS((int c));
167static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
168#endif /* FEAT_INS_EXPAND */
169
170#define BACKSPACE_CHAR 1
171#define BACKSPACE_WORD 2
172#define BACKSPACE_WORD_NOT_SPACE 3
173#define BACKSPACE_LINE 4
174
Bram Moolenaar754b5602006-02-09 23:53:20 +0000175static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176static void ins_ctrl_v __ARGS((void));
177static void undisplay_dollar __ARGS((void));
178static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000179static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static void check_auto_format __ARGS((int));
181static void redo_literal __ARGS((int c));
182static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000183#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000184static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000185static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000186static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
189static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000190#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static int replace_pop __ARGS((void));
194static void replace_join __ARGS((int off));
195static void replace_pop_ins __ARGS((void));
196#ifdef FEAT_MBYTE
197static void mb_replace_pop_ins __ARGS((int cc));
198#endif
199static void replace_flush __ARGS((void));
200static void replace_do_bs __ARGS((void));
201#ifdef FEAT_CINDENT
202static int cindent_on __ARGS((void));
203#endif
204static void ins_reg __ARGS((void));
205static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000206static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000207static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#ifdef FEAT_RIGHTLEFT
209static void ins_ctrl_ __ARGS((void));
210#endif
211#ifdef FEAT_VISUAL
212static int ins_start_select __ARGS((int c));
213#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000214static void ins_insert __ARGS((int replaceState));
215static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216static void ins_shift __ARGS((int c, int lastc));
217static void ins_del __ARGS((void));
218static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
219#ifdef FEAT_MOUSE
220static void ins_mouse __ARGS((int c));
221static void ins_mousescroll __ARGS((int up));
222#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000223#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
224static void ins_tabline __ARGS((int c));
225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226static void ins_left __ARGS((void));
227static void ins_home __ARGS((int c));
228static void ins_end __ARGS((int c));
229static void ins_s_left __ARGS((void));
230static void ins_right __ARGS((void));
231static void ins_s_right __ARGS((void));
232static void ins_up __ARGS((int startcol));
233static void ins_pageup __ARGS((void));
234static void ins_down __ARGS((int startcol));
235static void ins_pagedown __ARGS((void));
236#ifdef FEAT_DND
237static void ins_drop __ARGS((void));
238#endif
239static int ins_tab __ARGS((void));
240static int ins_eol __ARGS((int c));
241#ifdef FEAT_DIGRAPHS
242static int ins_digraph __ARGS((void));
243#endif
244static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000245static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifdef FEAT_SMARTINDENT
247static void ins_try_si __ARGS((int c));
248#endif
249static colnr_T get_nolist_virtcol __ARGS((void));
250
251static colnr_T Insstart_textlen; /* length of line when insert started */
252static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
253
254static char_u *last_insert = NULL; /* the text of the previous insert,
255 K_SPECIAL and CSI are escaped */
256static int last_insert_skip; /* nr of chars in front of previous insert */
257static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000258static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260#ifdef FEAT_CINDENT
261static int can_cindent; /* may do cindenting on this line */
262#endif
263
264static int old_indent = 0; /* for ^^D command in insert mode */
265
266#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000267static int revins_on; /* reverse insert mode on */
268static int revins_chars; /* how much to skip after edit */
269static int revins_legal; /* was the last char 'legal'? */
270static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271#endif
272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273static int ins_need_undo; /* call u_save() before inserting a
274 char. Set when edit() is called.
275 after that arrow_used is used. */
276
277static int did_add_space = FALSE; /* auto_format() added an extra space
278 under the cursor */
279
280/*
281 * edit(): Start inserting text.
282 *
283 * "cmdchar" can be:
284 * 'i' normal insert command
285 * 'a' normal append command
286 * 'R' replace command
287 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
288 * but still only one <CR> is inserted. The <Esc> is not used for redo.
289 * 'g' "gI" command.
290 * 'V' "gR" command for Virtual Replace mode.
291 * 'v' "gr" command for single character Virtual Replace mode.
292 *
293 * This function is not called recursively. For CTRL-O commands, it returns
294 * and lets the caller handle the Normal-mode command.
295 *
296 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
297 */
298 int
299edit(cmdchar, startln, count)
300 int cmdchar;
301 int startln; /* if set, insert at start of line */
302 long count;
303{
304 int c = 0;
305 char_u *ptr;
306 int lastc;
307 colnr_T mincol;
308 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 int i;
310 int did_backspace = TRUE; /* previous char was backspace */
311#ifdef FEAT_CINDENT
312 int line_is_white = FALSE; /* line is empty before insert */
313#endif
314 linenr_T old_topline = 0; /* topline before insertion */
315#ifdef FEAT_DIFF
316 int old_topfill = -1;
317#endif
318 int inserted_space = FALSE; /* just inserted a space */
319 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000320 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000322 /* Remember whether editing was restarted after CTRL-O. */
323 did_restart_edit = restart_edit;
324
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 /* sleep before redrawing, needed for "CTRL-O :" that results in an
326 * error message */
327 check_for_delay(TRUE);
328
329#ifdef HAVE_SANDBOX
330 /* Don't allow inserting in the sandbox. */
331 if (sandbox != 0)
332 {
333 EMSG(_(e_sandbox));
334 return FALSE;
335 }
336#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000337 /* Don't allow changes in the buffer while editing the cmdline. The
338 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000339 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000340 {
341 EMSG(_(e_secure));
342 return FALSE;
343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
345#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000346 /* Don't allow recursive insert mode when busy with completion. */
347 if (compl_started || pum_visible())
348 {
349 EMSG(_(e_secure));
350 return FALSE;
351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 ins_compl_clear(); /* clear stuff for CTRL-X mode */
353#endif
354
Bram Moolenaar843ee412004-06-30 16:16:41 +0000355#ifdef FEAT_AUTOCMD
356 /*
357 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
358 */
359 if (cmdchar != 'r' && cmdchar != 'v')
360 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000361# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000362 if (cmdchar == 'R')
363 ptr = (char_u *)"r";
364 else if (cmdchar == 'V')
365 ptr = (char_u *)"v";
366 else
367 ptr = (char_u *)"i";
368 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000369# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000370 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
371 }
372#endif
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#ifdef FEAT_MOUSE
375 /*
376 * When doing a paste with the middle mouse button, Insstart is set to
377 * where the paste started.
378 */
379 if (where_paste_started.lnum != 0)
380 Insstart = where_paste_started;
381 else
382#endif
383 {
384 Insstart = curwin->w_cursor;
385 if (startln)
386 Insstart.col = 0;
387 }
388 Insstart_textlen = linetabsize(ml_get_curline());
389 Insstart_blank_vcol = MAXCOL;
390 if (!did_ai)
391 ai_col = 0;
392
393 if (cmdchar != NUL && restart_edit == 0)
394 {
395 ResetRedobuff();
396 AppendNumberToRedobuff(count);
397#ifdef FEAT_VREPLACE
398 if (cmdchar == 'V' || cmdchar == 'v')
399 {
400 /* "gR" or "gr" command */
401 AppendCharToRedobuff('g');
402 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
403 }
404 else
405#endif
406 {
407 AppendCharToRedobuff(cmdchar);
408 if (cmdchar == 'g') /* "gI" command */
409 AppendCharToRedobuff('I');
410 else if (cmdchar == 'r') /* "r<CR>" command */
411 count = 1; /* insert only one <CR> */
412 }
413 }
414
415 if (cmdchar == 'R')
416 {
417#ifdef FEAT_FKMAP
418 if (p_fkmap && p_ri)
419 {
420 beep_flush();
421 EMSG(farsi_text_3); /* encoded in Farsi */
422 State = INSERT;
423 }
424 else
425#endif
426 State = REPLACE;
427 }
428#ifdef FEAT_VREPLACE
429 else if (cmdchar == 'V' || cmdchar == 'v')
430 {
431 State = VREPLACE;
432 replaceState = VREPLACE;
433 orig_line_count = curbuf->b_ml.ml_line_count;
434 vr_lines_changed = 1;
435 }
436#endif
437 else
438 State = INSERT;
439
440 stop_insert_mode = FALSE;
441
442 /*
443 * Need to recompute the cursor position, it might move when the cursor is
444 * on a TAB or special character.
445 */
446 curs_columns(TRUE);
447
448 /*
449 * Enable langmap or IME, indicated by 'iminsert'.
450 * Note that IME may enabled/disabled without us noticing here, thus the
451 * 'iminsert' value may not reflect what is actually used. It is updated
452 * when hitting <Esc>.
453 */
454 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
455 State |= LANGMAP;
456#ifdef USE_IM_CONTROL
457 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
458#endif
459
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460#ifdef FEAT_MOUSE
461 setmouse();
462#endif
463#ifdef FEAT_CMDL_INFO
464 clear_showcmd();
465#endif
466#ifdef FEAT_RIGHTLEFT
467 /* there is no reverse replace mode */
468 revins_on = (State == INSERT && p_ri);
469 if (revins_on)
470 undisplay_dollar();
471 revins_chars = 0;
472 revins_legal = 0;
473 revins_scol = -1;
474#endif
475
476 /*
477 * Handle restarting Insert mode.
478 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
479 * restart_edit non-zero, and something in the stuff buffer.
480 */
481 if (restart_edit != 0 && stuff_empty())
482 {
483#ifdef FEAT_MOUSE
484 /*
485 * After a paste we consider text typed to be part of the insert for
486 * the pasted text. You can backspace over the pasted text too.
487 */
488 if (where_paste_started.lnum)
489 arrow_used = FALSE;
490 else
491#endif
492 arrow_used = TRUE;
493 restart_edit = 0;
494
495 /*
496 * If the cursor was after the end-of-line before the CTRL-O and it is
497 * now at the end-of-line, put it after the end-of-line (this is not
498 * correct in very rare cases).
499 * Also do this if curswant is greater than the current virtual
500 * column. Eg after "^O$" or "^O80|".
501 */
502 validate_virtcol();
503 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000504 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 || curwin->w_curswant > curwin->w_virtcol)
506 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
507 {
508 if (ptr[1] == NUL)
509 ++curwin->w_cursor.col;
510#ifdef FEAT_MBYTE
511 else if (has_mbyte)
512 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000513 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr[i] == NUL)
515 curwin->w_cursor.col += i;
516 }
517#endif
518 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000519 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521 else
522 arrow_used = FALSE;
523
524 /* we are in insert mode now, don't need to start it anymore */
525 need_start_insertmode = FALSE;
526
527 /* Need to save the line for undo before inserting the first char. */
528 ins_need_undo = TRUE;
529
530#ifdef FEAT_MOUSE
531 where_paste_started.lnum = 0;
532#endif
533#ifdef FEAT_CINDENT
534 can_cindent = TRUE;
535#endif
536#ifdef FEAT_FOLDING
537 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
538 * restarting. */
539 if (!p_im && did_restart_edit == 0)
540 foldOpenCursor();
541#endif
542
543 /*
544 * If 'showmode' is set, show the current (insert/replace/..) mode.
545 * A warning message for changing a readonly file is given here, before
546 * actually changing anything. It's put after the mode, if any.
547 */
548 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000549 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 i = showmode();
551
552 if (!p_im && did_restart_edit == 0)
553 change_warning(i + 1);
554
555#ifdef CURSOR_SHAPE
556 ui_cursor_shape(); /* may show different cursor shape */
557#endif
558#ifdef FEAT_DIGRAPHS
559 do_digraph(-1); /* clear digraphs */
560#endif
561
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000562 /*
563 * Get the current length of the redo buffer, those characters have to be
564 * skipped if we want to get to the inserted characters.
565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 ptr = get_inserted();
567 if (ptr == NULL)
568 new_insert_skip = 0;
569 else
570 {
571 new_insert_skip = (int)STRLEN(ptr);
572 vim_free(ptr);
573 }
574
575 old_indent = 0;
576
577 /*
578 * Main loop in Insert mode: repeat until Insert mode is left.
579 */
580 for (;;)
581 {
582#ifdef FEAT_RIGHTLEFT
583 if (!revins_legal)
584 revins_scol = -1; /* reset on illegal motions */
585 else
586 revins_legal = 0;
587#endif
588 if (arrow_used) /* don't repeat insert when arrow key used */
589 count = 0;
590
591 if (stop_insert_mode)
592 {
593 /* ":stopinsert" used or 'insertmode' reset */
594 count = 0;
595 goto doESCkey;
596 }
597
598 /* set curwin->w_curswant for next K_DOWN or K_UP */
599 if (!arrow_used)
600 curwin->w_set_curswant = TRUE;
601
602 /* If there is no typeahead may check for timestamps (e.g., for when a
603 * menu invoked a shell command). */
604 if (stuff_empty())
605 {
606 did_check_timestamps = FALSE;
607 if (need_check_timestamps)
608 check_timestamps(FALSE);
609 }
610
611 /*
612 * When emsg() was called msg_scroll will have been set.
613 */
614 msg_scroll = FALSE;
615
616#ifdef FEAT_GUI
617 /* When 'mousefocus' is set a mouse movement may have taken us to
618 * another window. "need_mouse_correct" may then be set because of an
619 * autocommand. */
620 if (need_mouse_correct)
621 gui_mouse_correct();
622#endif
623
624#ifdef FEAT_FOLDING
625 /* Open fold at the cursor line, according to 'foldopen'. */
626 if (fdo_flags & FDO_INSERT)
627 foldOpenCursor();
628 /* Close folds where the cursor isn't, according to 'foldclose' */
629 if (!char_avail())
630 foldCheckClose();
631#endif
632
633 /*
634 * If we inserted a character at the last position of the last line in
635 * the window, scroll the window one line up. This avoids an extra
636 * redraw.
637 * This is detected when the cursor column is smaller after inserting
638 * something.
639 * Don't do this when the topline changed already, it has
640 * already been adjusted (by insertchar() calling open_line())).
641 */
642 if (curbuf->b_mod_set
643 && curwin->w_p_wrap
644 && !did_backspace
645 && curwin->w_topline == old_topline
646#ifdef FEAT_DIFF
647 && curwin->w_topfill == old_topfill
648#endif
649 )
650 {
651 mincol = curwin->w_wcol;
652 validate_cursor_col();
653
654 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
655 && curwin->w_wrow == W_WINROW(curwin)
656 + curwin->w_height - 1 - p_so
657 && (curwin->w_cursor.lnum != curwin->w_topline
658#ifdef FEAT_DIFF
659 || curwin->w_topfill > 0
660#endif
661 ))
662 {
663#ifdef FEAT_DIFF
664 if (curwin->w_topfill > 0)
665 --curwin->w_topfill;
666 else
667#endif
668#ifdef FEAT_FOLDING
669 if (hasFolding(curwin->w_topline, NULL, &old_topline))
670 set_topline(curwin, old_topline + 1);
671 else
672#endif
673 set_topline(curwin, curwin->w_topline + 1);
674 }
675 }
676
677 /* May need to adjust w_topline to show the cursor. */
678 update_topline();
679
680 did_backspace = FALSE;
681
682 validate_cursor(); /* may set must_redraw */
683
684 /*
685 * Redraw the display when no characters are waiting.
686 * Also shows mode, ruler and positions cursor.
687 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000688 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690#ifdef FEAT_SCROLLBIND
691 if (curwin->w_p_scb)
692 do_check_scrollbind(TRUE);
693#endif
694
695 update_curswant();
696 old_topline = curwin->w_topline;
697#ifdef FEAT_DIFF
698 old_topfill = curwin->w_topfill;
699#endif
700
701#ifdef USE_ON_FLY_SCROLL
702 dont_scroll = FALSE; /* allow scrolling here */
703#endif
704
705 /*
706 * Get a character for Insert mode.
707 */
708 lastc = c; /* remember previous char for CTRL-D */
709 c = safe_vgetc();
710
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000711#ifdef FEAT_AUTOCMD
712 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
713 did_cursorhold = TRUE;
714#endif
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_RIGHTLEFT
717 if (p_hkmap && KeyTyped)
718 c = hkmap(c); /* Hebrew mode mapping */
719#endif
720#ifdef FEAT_FKMAP
721 if (p_fkmap && KeyTyped)
722 c = fkmap(c); /* Farsi mode mapping */
723#endif
724
725#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000726 /*
727 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000728 * and the cursor is still in the completed word. Only when there is
729 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000730 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000731 if (compl_started
732 && pum_wanted()
733 && curwin->w_cursor.col >= compl_col
734 && (compl_shown_match == NULL
735 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000736 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000737 /* BS: Delete one character from "compl_leader". */
738 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000739 && curwin->w_cursor.col > compl_col
740 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000741 continue;
742
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000743 /* When no match was selected or it was edited. */
744 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000745 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000746 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000747 * "compl_leader". Except when at the original match and
748 * there is nothing to add, CTRL-L works like CTRL-P then. */
749 if (c == Ctrl_L
750 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
751 || STRLEN(compl_shown_match->cp_str)
752 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000753 {
754 ins_compl_addfrommatch();
755 continue;
756 }
757
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000758 /* A non-white character that fits in with the current
759 * completion: Add to "compl_leader". */
760 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000761 {
762 ins_compl_addleader(c);
763 continue;
764 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000765
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000766 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000767 * compl_enter_selects is set the Enter key does the same. */
768 if (c == Ctrl_Y || (compl_enter_selects
769 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000770 {
771 ins_compl_delete();
772 ins_compl_insert();
773 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000774 }
775 }
776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
778 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000779 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000780 if (c != K_IGNORE && ins_compl_prep(c))
781 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783
Bram Moolenaar488c6512005-08-11 20:09:58 +0000784 /* CTRL-\ CTRL-N goes to Normal mode,
785 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
786 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (c == Ctrl_BSL)
788 {
789 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000790 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 ++no_mapping;
792 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000793 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 --no_mapping;
795 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000796 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000798 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 vungetc(c);
800 c = Ctrl_BSL;
801 }
802 else if (c == Ctrl_G && p_im)
803 continue;
804 else
805 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000806 if (c == Ctrl_O)
807 {
808 ins_ctrl_o();
809 ins_at_eol = FALSE; /* cursor keeps its column */
810 nomove = TRUE;
811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 count = 0;
813 goto doESCkey;
814 }
815 }
816
817#ifdef FEAT_DIGRAPHS
818 c = do_digraph(c);
819#endif
820
821#ifdef FEAT_INS_EXPAND
822 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
823 goto docomplete;
824#endif
825 if (c == Ctrl_V || c == Ctrl_Q)
826 {
827 ins_ctrl_v();
828 c = Ctrl_V; /* pretend CTRL-V is last typed character */
829 continue;
830 }
831
832#ifdef FEAT_CINDENT
833 if (cindent_on()
834# ifdef FEAT_INS_EXPAND
835 && ctrl_x_mode == 0
836# endif
837 )
838 {
839 /* A key name preceded by a bang means this key is not to be
840 * inserted. Skip ahead to the re-indenting below.
841 * A key name preceded by a star means that indenting has to be
842 * done before inserting the key. */
843 line_is_white = inindent(0);
844 if (in_cinkeys(c, '!', line_is_white))
845 goto force_cindent;
846 if (can_cindent && in_cinkeys(c, '*', line_is_white)
847 && stop_arrow() == OK)
848 do_c_expr_indent();
849 }
850#endif
851
852#ifdef FEAT_RIGHTLEFT
853 if (curwin->w_p_rl)
854 switch (c)
855 {
856 case K_LEFT: c = K_RIGHT; break;
857 case K_S_LEFT: c = K_S_RIGHT; break;
858 case K_C_LEFT: c = K_C_RIGHT; break;
859 case K_RIGHT: c = K_LEFT; break;
860 case K_S_RIGHT: c = K_S_LEFT; break;
861 case K_C_RIGHT: c = K_C_LEFT; break;
862 }
863#endif
864
865#ifdef FEAT_VISUAL
866 /*
867 * If 'keymodel' contains "startsel", may start selection. If it
868 * does, a CTRL-O and c will be stuffed, we need to get these
869 * characters.
870 */
871 if (ins_start_select(c))
872 continue;
873#endif
874
875 /*
876 * The big switch to handle a character in insert mode.
877 */
878 switch (c)
879 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000880 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (echeck_abbr(ESC + ABBR_OFF))
882 break;
883 /*FALLTHROUGH*/
884
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_CMDWIN
887 if (c == Ctrl_C && cmdwin_type != 0)
888 {
889 /* Close the cmdline window. */
890 cmdwin_result = K_IGNORE;
891 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000892 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 goto doESCkey;
894 }
895#endif
896
897#ifdef UNIX
898do_intr:
899#endif
900 /* when 'insertmode' set, and not halfway a mapping, don't leave
901 * Insert mode */
902 if (goto_im())
903 {
904 if (got_int)
905 {
906 (void)vgetc(); /* flush all buffers */
907 got_int = FALSE;
908 }
909 else
910 vim_beep();
911 break;
912 }
913doESCkey:
914 /*
915 * This is the ONLY return from edit()!
916 */
917 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
918 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000919 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 o_lnum = curwin->w_cursor.lnum;
921
Bram Moolenaar488c6512005-08-11 20:09:58 +0000922 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000923 {
924#ifdef FEAT_AUTOCMD
925 if (cmdchar != 'r' && cmdchar != 'v')
926 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
927 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000928 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 continue;
933
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000934 case Ctrl_Z: /* suspend when 'insertmode' set */
935 if (!p_im)
936 goto normalchar; /* insert CTRL-Z as normal char */
937 stuffReadbuff((char_u *)":st\r");
938 c = Ctrl_O;
939 /*FALLTHROUGH*/
940
941 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000942#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000943 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000944 goto docomplete;
945#endif
946 if (echeck_abbr(Ctrl_O + ABBR_OFF))
947 break;
948 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000949
950#ifdef FEAT_VIRTUALEDIT
951 /* don't move the cursor left when 'virtualedit' has "onemore". */
952 if (ve_flags & VE_ONEMORE)
953 {
954 ins_at_eol = FALSE;
955 nomove = TRUE;
956 }
957#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000958 count = 0;
959 goto doESCkey;
960
Bram Moolenaar572cb562005-08-05 21:35:02 +0000961 case K_INS: /* toggle insert/replace mode */
962 case K_KINS:
963 ins_insert(replaceState);
964 break;
965
966 case K_SELECT: /* end of Select mode mapping - ignore */
967 break;
968
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000969#ifdef FEAT_SNIFF
970 case K_SNIFF: /* Sniff command received */
971 stuffcharReadbuff(K_SNIFF);
972 goto doESCkey;
973#endif
974
975 case K_HELP: /* Help key works like <ESC> <Help> */
976 case K_F1:
977 case K_XF1:
978 stuffcharReadbuff(K_HELP);
979 if (p_im)
980 need_start_insertmode = TRUE;
981 goto doESCkey;
982
983#ifdef FEAT_NETBEANS_INTG
984 case K_F21: /* NetBeans command */
985 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000986 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000987 --no_mapping;
988 netbeans_keycommand(i);
989 break;
990#endif
991
992 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 case NUL:
994 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 /* For ^@ the trailing ESC will end the insert, unless there is an
996 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
998 && c != Ctrl_A && !p_im)
999 goto doESCkey; /* quit insert mode */
1000 inserted_space = FALSE;
1001 break;
1002
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001003 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 ins_reg();
1005 auto_format(FALSE, TRUE);
1006 inserted_space = FALSE;
1007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 ins_ctrl_g();
1011 break;
1012
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001013 case Ctrl_HAT: /* switch input mode and/or langmap */
1014 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 break;
1016
1017#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (!p_ari)
1020 goto normalchar;
1021 ins_ctrl_();
1022 break;
1023#endif
1024
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001025 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1027 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1028 goto docomplete;
1029#endif
1030 /* FALLTHROUGH */
1031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001032 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033# ifdef FEAT_INS_EXPAND
1034 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1035 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001036 if (has_compl_option(FALSE))
1037 goto docomplete;
1038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
1040# endif
1041 ins_shift(c, lastc);
1042 auto_format(FALSE, TRUE);
1043 inserted_space = FALSE;
1044 break;
1045
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001046 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 case K_KDEL:
1048 ins_del();
1049 auto_format(FALSE, TRUE);
1050 break;
1051
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001052 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 case Ctrl_H:
1054 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1055 auto_format(FALSE, TRUE);
1056 break;
1057
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1060 auto_format(FALSE, TRUE);
1061 break;
1062
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001064# ifdef FEAT_COMPL_FUNC
1065 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001067 goto docomplete;
1068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1070 auto_format(FALSE, TRUE);
1071 inserted_space = FALSE;
1072 break;
1073
1074#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 case K_LEFTMOUSE_NM:
1077 case K_LEFTDRAG:
1078 case K_LEFTRELEASE:
1079 case K_LEFTRELEASE_NM:
1080 case K_MIDDLEMOUSE:
1081 case K_MIDDLEDRAG:
1082 case K_MIDDLERELEASE:
1083 case K_RIGHTMOUSE:
1084 case K_RIGHTDRAG:
1085 case K_RIGHTRELEASE:
1086 case K_X1MOUSE:
1087 case K_X1DRAG:
1088 case K_X1RELEASE:
1089 case K_X2MOUSE:
1090 case K_X2DRAG:
1091 case K_X2RELEASE:
1092 ins_mouse(c);
1093 break;
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 ins_mousescroll(FALSE);
1097 break;
1098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 ins_mousescroll(TRUE);
1101 break;
1102#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001103#ifdef FEAT_GUI_TABLINE
1104 case K_TABLINE:
1105 case K_TABMENU:
1106 ins_tabline(c);
1107 break;
1108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 break;
1112
Bram Moolenaar754b5602006-02-09 23:53:20 +00001113#ifdef FEAT_AUTOCMD
1114 case K_CURSORHOLD: /* Didn't type something for a while. */
1115 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1116 did_cursorhold = TRUE;
1117 break;
1118#endif
1119
Bram Moolenaar4770d092006-01-12 23:22:24 +00001120#ifdef FEAT_GUI_W32
1121 /* On Win32 ignore <M-F4>, we get it when closing the window was
1122 * cancelled. */
1123 case K_F4:
1124 if (mod_mask != MOD_MASK_ALT)
1125 goto normalchar;
1126 break;
1127#endif
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#ifdef FEAT_GUI
1130 case K_VER_SCROLLBAR:
1131 ins_scroll();
1132 break;
1133
1134 case K_HOR_SCROLLBAR:
1135 ins_horscroll();
1136 break;
1137#endif
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 case K_S_HOME:
1142 case K_C_HOME:
1143 ins_home(c);
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 case K_S_END:
1149 case K_C_END:
1150 ins_end(c);
1151 break;
1152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001154 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1155 ins_s_left();
1156 else
1157 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 case K_C_LEFT:
1162 ins_s_left();
1163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001166 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1167 ins_s_right();
1168 else
1169 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 break;
1171
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_C_RIGHT:
1174 ins_s_right();
1175 break;
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001178#ifdef FEAT_INS_EXPAND
1179 if (pum_visible())
1180 goto docomplete;
1181#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001182 if (mod_mask & MOD_MASK_SHIFT)
1183 ins_pageup();
1184 else
1185 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 case K_PAGEUP:
1190 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001191#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001192 if (pum_visible())
1193 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 ins_pageup();
1196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001199#ifdef FEAT_INS_EXPAND
1200 if (pum_visible())
1201 goto docomplete;
1202#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001203 if (mod_mask & MOD_MASK_SHIFT)
1204 ins_pagedown();
1205 else
1206 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 break;
1208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_PAGEDOWN:
1211 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001212#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001213 if (pum_visible())
1214 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 ins_pagedown();
1217 break;
1218
1219#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 ins_drop();
1222 break;
1223#endif
1224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 c = TAB;
1227 /* FALLTHROUGH */
1228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1231 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1232 goto docomplete;
1233#endif
1234 inserted_space = FALSE;
1235 if (ins_tab())
1236 goto normalchar; /* insert TAB as a normal char */
1237 auto_format(FALSE, TRUE);
1238 break;
1239
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001240 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 c = CAR;
1242 /* FALLTHROUGH */
1243 case CAR:
1244 case NL:
1245#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1246 /* In a quickfix window a <CR> jumps to the error under the
1247 * cursor. */
1248 if (bt_quickfix(curbuf) && c == CAR)
1249 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001250 if (curwin->w_llist_ref == NULL) /* quickfix window */
1251 do_cmdline_cmd((char_u *)".cc");
1252 else /* location list window */
1253 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 break;
1255 }
1256#endif
1257#ifdef FEAT_CMDWIN
1258 if (cmdwin_type != 0)
1259 {
1260 /* Execute the command in the cmdline window. */
1261 cmdwin_result = CAR;
1262 goto doESCkey;
1263 }
1264#endif
1265 if (ins_eol(c) && !p_im)
1266 goto doESCkey; /* out of memory */
1267 auto_format(FALSE, FALSE);
1268 inserted_space = FALSE;
1269 break;
1270
1271#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001272 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273# ifdef FEAT_INS_EXPAND
1274 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1275 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 if (has_compl_option(TRUE))
1277 goto docomplete;
1278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280# endif
1281# ifdef FEAT_DIGRAPHS
1282 c = ins_digraph();
1283 if (c == NUL)
1284 break;
1285# endif
1286 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001290 case Ctrl_X: /* Enter CTRL-X mode */
1291 ins_ctrl_x();
1292 break;
1293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001294 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (ctrl_x_mode != CTRL_X_TAGS)
1296 goto normalchar;
1297 goto docomplete;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (ctrl_x_mode != CTRL_X_FILES)
1301 goto normalchar;
1302 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001303
1304 case 's': /* Spelling completion after ^X */
1305 case Ctrl_S:
1306 if (ctrl_x_mode != CTRL_X_SPELL)
1307 goto normalchar;
1308 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#endif
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312#ifdef FEAT_INS_EXPAND
1313 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1314#endif
1315 {
1316 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1317 if (p_im)
1318 {
1319 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1320 break;
1321 goto doESCkey;
1322 }
1323 goto normalchar;
1324 }
1325#ifdef FEAT_INS_EXPAND
1326 /* FALLTHROUGH */
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 case Ctrl_N:
1330 /* if 'complete' is empty then plain ^P is no longer special,
1331 * but it is under other ^X modes */
1332 if (*curbuf->b_p_cpt == NUL
1333 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 goto normalchar;
1336
1337docomplete:
1338 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 break;
1341#endif /* FEAT_INS_EXPAND */
1342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case Ctrl_Y: /* copy from previous line or scroll down */
1344 case Ctrl_E: /* copy from next line or scroll up */
1345 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 break;
1347
1348 default:
1349#ifdef UNIX
1350 if (c == intr_char) /* special interrupt char */
1351 goto do_intr;
1352#endif
1353
1354 /*
1355 * Insert a nomal character.
1356 */
1357normalchar:
1358#ifdef FEAT_SMARTINDENT
1359 /* Try to perform smart-indenting. */
1360 ins_try_si(c);
1361#endif
1362
1363 if (c == ' ')
1364 {
1365 inserted_space = TRUE;
1366#ifdef FEAT_CINDENT
1367 if (inindent(0))
1368 can_cindent = FALSE;
1369#endif
1370 if (Insstart_blank_vcol == MAXCOL
1371 && curwin->w_cursor.lnum == Insstart.lnum)
1372 Insstart_blank_vcol = get_nolist_virtcol();
1373 }
1374
1375 if (vim_iswordc(c) || !echeck_abbr(
1376#ifdef FEAT_MBYTE
1377 /* Add ABBR_OFF for characters above 0x100, this is
1378 * what check_abbr() expects. */
1379 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1380#endif
1381 c))
1382 {
1383 insert_special(c, FALSE, FALSE);
1384#ifdef FEAT_RIGHTLEFT
1385 revins_legal++;
1386 revins_chars++;
1387#endif
1388 }
1389
1390 auto_format(FALSE, TRUE);
1391
1392#ifdef FEAT_FOLDING
1393 /* When inserting a character the cursor line must never be in a
1394 * closed fold. */
1395 foldOpenCursor();
1396#endif
1397 break;
1398 } /* end of switch (c) */
1399
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001400#ifdef FEAT_AUTOCMD
1401 /* If typed something may trigger CursorHoldI again. */
1402 if (c != K_CURSORHOLD)
1403 did_cursorhold = FALSE;
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /* If the cursor was moved we didn't just insert a space */
1407 if (arrow_used)
1408 inserted_space = FALSE;
1409
1410#ifdef FEAT_CINDENT
1411 if (can_cindent && cindent_on()
1412# ifdef FEAT_INS_EXPAND
1413 && ctrl_x_mode == 0
1414# endif
1415 )
1416 {
1417force_cindent:
1418 /*
1419 * Indent now if a key was typed that is in 'cinkeys'.
1420 */
1421 if (in_cinkeys(c, ' ', line_is_white))
1422 {
1423 if (stop_arrow() == OK)
1424 /* re-indent the current line */
1425 do_c_expr_indent();
1426 }
1427 }
1428#endif /* FEAT_CINDENT */
1429
1430 } /* for (;;) */
1431 /* NOTREACHED */
1432}
1433
1434/*
1435 * Redraw for Insert mode.
1436 * This is postponed until getting the next character to make '$' in the 'cpo'
1437 * option work correctly.
1438 * Only redraw when there are no characters available. This speeds up
1439 * inserting sequences of characters (e.g., for CTRL-R).
1440 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001441/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001443ins_redraw(ready)
1444 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 if (!char_avail())
1447 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001448#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001449 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1450 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001451 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001452 && !equalpos(last_cursormoved, curwin->w_cursor)
1453# ifdef FEAT_INS_EXPAND
1454 && !pum_visible()
1455# endif
1456 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001457 {
1458 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1459 last_cursormoved = curwin->w_cursor;
1460 }
1461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (must_redraw)
1463 update_screen(0);
1464 else if (clear_cmdline || redraw_cmdline)
1465 showmode(); /* clear cmdline and show mode */
1466 showruler(FALSE);
1467 setcursor();
1468 emsg_on_display = FALSE; /* may remove error message now */
1469 }
1470}
1471
1472/*
1473 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1474 */
1475 static void
1476ins_ctrl_v()
1477{
1478 int c;
1479
1480 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001481 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 if (redrawing() && !char_avail())
1484 edit_putchar('^', TRUE);
1485 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1486
1487#ifdef FEAT_CMDL_INFO
1488 add_to_showcmd_c(Ctrl_V);
1489#endif
1490
1491 c = get_literal();
1492#ifdef FEAT_CMDL_INFO
1493 clear_showcmd();
1494#endif
1495 insert_special(c, FALSE, TRUE);
1496#ifdef FEAT_RIGHTLEFT
1497 revins_chars++;
1498 revins_legal++;
1499#endif
1500}
1501
1502/*
1503 * Put a character directly onto the screen. It's not stored in a buffer.
1504 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1505 */
1506static int pc_status;
1507#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1508#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1509#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1510#define PC_STATUS_SET 3 /* pc_bytes was filled */
1511#ifdef FEAT_MBYTE
1512static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1513#else
1514static char_u pc_bytes[2]; /* saved bytes */
1515#endif
1516static int pc_attr;
1517static int pc_row;
1518static int pc_col;
1519
1520 void
1521edit_putchar(c, highlight)
1522 int c;
1523 int highlight;
1524{
1525 int attr;
1526
1527 if (ScreenLines != NULL)
1528 {
1529 update_topline(); /* just in case w_topline isn't valid */
1530 validate_cursor();
1531 if (highlight)
1532 attr = hl_attr(HLF_8);
1533 else
1534 attr = 0;
1535 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1536 pc_col = W_WINCOL(curwin);
1537#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1538 pc_status = PC_STATUS_UNSET;
1539#endif
1540#ifdef FEAT_RIGHTLEFT
1541 if (curwin->w_p_rl)
1542 {
1543 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1544# ifdef FEAT_MBYTE
1545 if (has_mbyte)
1546 {
1547 int fix_col = mb_fix_col(pc_col, pc_row);
1548
1549 if (fix_col != pc_col)
1550 {
1551 screen_putchar(' ', pc_row, fix_col, attr);
1552 --curwin->w_wcol;
1553 pc_status = PC_STATUS_RIGHT;
1554 }
1555 }
1556# endif
1557 }
1558 else
1559#endif
1560 {
1561 pc_col += curwin->w_wcol;
1562#ifdef FEAT_MBYTE
1563 if (mb_lefthalve(pc_row, pc_col))
1564 pc_status = PC_STATUS_LEFT;
1565#endif
1566 }
1567
1568 /* save the character to be able to put it back */
1569#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1570 if (pc_status == PC_STATUS_UNSET)
1571#endif
1572 {
1573 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1574 pc_status = PC_STATUS_SET;
1575 }
1576 screen_putchar(c, pc_row, pc_col, attr);
1577 }
1578}
1579
1580/*
1581 * Undo the previous edit_putchar().
1582 */
1583 void
1584edit_unputchar()
1585{
1586 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1587 {
1588#if defined(FEAT_MBYTE)
1589 if (pc_status == PC_STATUS_RIGHT)
1590 ++curwin->w_wcol;
1591 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1592 redrawWinline(curwin->w_cursor.lnum, FALSE);
1593 else
1594#endif
1595 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1596 }
1597}
1598
1599/*
1600 * Called when p_dollar is set: display a '$' at the end of the changed text
1601 * Only works when cursor is in the line that changes.
1602 */
1603 void
1604display_dollar(col)
1605 colnr_T col;
1606{
1607 colnr_T save_col;
1608
1609 if (!redrawing())
1610 return;
1611
1612 cursor_off();
1613 save_col = curwin->w_cursor.col;
1614 curwin->w_cursor.col = col;
1615#ifdef FEAT_MBYTE
1616 if (has_mbyte)
1617 {
1618 char_u *p;
1619
1620 /* If on the last byte of a multi-byte move to the first byte. */
1621 p = ml_get_curline();
1622 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1623 }
1624#endif
1625 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1626 if (curwin->w_wcol < W_WIDTH(curwin))
1627 {
1628 edit_putchar('$', FALSE);
1629 dollar_vcol = curwin->w_virtcol;
1630 }
1631 curwin->w_cursor.col = save_col;
1632}
1633
1634/*
1635 * Call this function before moving the cursor from the normal insert position
1636 * in insert mode.
1637 */
1638 static void
1639undisplay_dollar()
1640{
1641 if (dollar_vcol)
1642 {
1643 dollar_vcol = 0;
1644 redrawWinline(curwin->w_cursor.lnum, FALSE);
1645 }
1646}
1647
1648/*
1649 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1650 * Keep the cursor on the same character.
1651 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1652 * type == INDENT_DEC decrease indent (for CTRL-D)
1653 * type == INDENT_SET set indent to "amount"
1654 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1655 */
1656 void
1657change_indent(type, amount, round, replaced)
1658 int type;
1659 int amount;
1660 int round;
1661 int replaced; /* replaced character, put on replace stack */
1662{
1663 int vcol;
1664 int last_vcol;
1665 int insstart_less; /* reduction for Insstart.col */
1666 int new_cursor_col;
1667 int i;
1668 char_u *ptr;
1669 int save_p_list;
1670 int start_col;
1671 colnr_T vc;
1672#ifdef FEAT_VREPLACE
1673 colnr_T orig_col = 0; /* init for GCC */
1674 char_u *new_line, *orig_line = NULL; /* init for GCC */
1675
1676 /* VREPLACE mode needs to know what the line was like before changing */
1677 if (State & VREPLACE_FLAG)
1678 {
1679 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1680 orig_col = curwin->w_cursor.col;
1681 }
1682#endif
1683
1684 /* for the following tricks we don't want list mode */
1685 save_p_list = curwin->w_p_list;
1686 curwin->w_p_list = FALSE;
1687 vc = getvcol_nolist(&curwin->w_cursor);
1688 vcol = vc;
1689
1690 /*
1691 * For Replace mode we need to fix the replace stack later, which is only
1692 * possible when the cursor is in the indent. Remember the number of
1693 * characters before the cursor if it's possible.
1694 */
1695 start_col = curwin->w_cursor.col;
1696
1697 /* determine offset from first non-blank */
1698 new_cursor_col = curwin->w_cursor.col;
1699 beginline(BL_WHITE);
1700 new_cursor_col -= curwin->w_cursor.col;
1701
1702 insstart_less = curwin->w_cursor.col;
1703
1704 /*
1705 * If the cursor is in the indent, compute how many screen columns the
1706 * cursor is to the left of the first non-blank.
1707 */
1708 if (new_cursor_col < 0)
1709 vcol = get_indent() - vcol;
1710
1711 if (new_cursor_col > 0) /* can't fix replace stack */
1712 start_col = -1;
1713
1714 /*
1715 * Set the new indent. The cursor will be put on the first non-blank.
1716 */
1717 if (type == INDENT_SET)
1718 (void)set_indent(amount, SIN_CHANGED);
1719 else
1720 {
1721#ifdef FEAT_VREPLACE
1722 int save_State = State;
1723
1724 /* Avoid being called recursively. */
1725 if (State & VREPLACE_FLAG)
1726 State = INSERT;
1727#endif
1728 shift_line(type == INDENT_DEC, round, 1);
1729#ifdef FEAT_VREPLACE
1730 State = save_State;
1731#endif
1732 }
1733 insstart_less -= curwin->w_cursor.col;
1734
1735 /*
1736 * Try to put cursor on same character.
1737 * If the cursor is at or after the first non-blank in the line,
1738 * compute the cursor column relative to the column of the first
1739 * non-blank character.
1740 * If we are not in insert mode, leave the cursor on the first non-blank.
1741 * If the cursor is before the first non-blank, position it relative
1742 * to the first non-blank, counted in screen columns.
1743 */
1744 if (new_cursor_col >= 0)
1745 {
1746 /*
1747 * When changing the indent while the cursor is touching it, reset
1748 * Insstart_col to 0.
1749 */
1750 if (new_cursor_col == 0)
1751 insstart_less = MAXCOL;
1752 new_cursor_col += curwin->w_cursor.col;
1753 }
1754 else if (!(State & INSERT))
1755 new_cursor_col = curwin->w_cursor.col;
1756 else
1757 {
1758 /*
1759 * Compute the screen column where the cursor should be.
1760 */
1761 vcol = get_indent() - vcol;
1762 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1763
1764 /*
1765 * Advance the cursor until we reach the right screen column.
1766 */
1767 vcol = last_vcol = 0;
1768 new_cursor_col = -1;
1769 ptr = ml_get_curline();
1770 while (vcol <= (int)curwin->w_virtcol)
1771 {
1772 last_vcol = vcol;
1773#ifdef FEAT_MBYTE
1774 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001775 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 else
1777#endif
1778 ++new_cursor_col;
1779 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1780 }
1781 vcol = last_vcol;
1782
1783 /*
1784 * May need to insert spaces to be able to position the cursor on
1785 * the right screen column.
1786 */
1787 if (vcol != (int)curwin->w_virtcol)
1788 {
1789 curwin->w_cursor.col = new_cursor_col;
1790 i = (int)curwin->w_virtcol - vcol;
1791 ptr = alloc(i + 1);
1792 if (ptr != NULL)
1793 {
1794 new_cursor_col += i;
1795 ptr[i] = NUL;
1796 while (--i >= 0)
1797 ptr[i] = ' ';
1798 ins_str(ptr);
1799 vim_free(ptr);
1800 }
1801 }
1802
1803 /*
1804 * When changing the indent while the cursor is in it, reset
1805 * Insstart_col to 0.
1806 */
1807 insstart_less = MAXCOL;
1808 }
1809
1810 curwin->w_p_list = save_p_list;
1811
1812 if (new_cursor_col <= 0)
1813 curwin->w_cursor.col = 0;
1814 else
1815 curwin->w_cursor.col = new_cursor_col;
1816 curwin->w_set_curswant = TRUE;
1817 changed_cline_bef_curs();
1818
1819 /*
1820 * May have to adjust the start of the insert.
1821 */
1822 if (State & INSERT)
1823 {
1824 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1825 {
1826 if ((int)Insstart.col <= insstart_less)
1827 Insstart.col = 0;
1828 else
1829 Insstart.col -= insstart_less;
1830 }
1831 if ((int)ai_col <= insstart_less)
1832 ai_col = 0;
1833 else
1834 ai_col -= insstart_less;
1835 }
1836
1837 /*
1838 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1839 * If the number of characters before the cursor decreased, need to pop a
1840 * few characters from the replace stack.
1841 * If the number of characters before the cursor increased, need to push a
1842 * few NULs onto the replace stack.
1843 */
1844 if (REPLACE_NORMAL(State) && start_col >= 0)
1845 {
1846 while (start_col > (int)curwin->w_cursor.col)
1847 {
1848 replace_join(0); /* remove a NUL from the replace stack */
1849 --start_col;
1850 }
1851 while (start_col < (int)curwin->w_cursor.col || replaced)
1852 {
1853 replace_push(NUL);
1854 if (replaced)
1855 {
1856 replace_push(replaced);
1857 replaced = NUL;
1858 }
1859 ++start_col;
1860 }
1861 }
1862
1863#ifdef FEAT_VREPLACE
1864 /*
1865 * For VREPLACE mode, we also have to fix the replace stack. In this case
1866 * it is always possible because we backspace over the whole line and then
1867 * put it back again the way we wanted it.
1868 */
1869 if (State & VREPLACE_FLAG)
1870 {
1871 /* If orig_line didn't allocate, just return. At least we did the job,
1872 * even if you can't backspace. */
1873 if (orig_line == NULL)
1874 return;
1875
1876 /* Save new line */
1877 new_line = vim_strsave(ml_get_curline());
1878 if (new_line == NULL)
1879 return;
1880
1881 /* We only put back the new line up to the cursor */
1882 new_line[curwin->w_cursor.col] = NUL;
1883
1884 /* Put back original line */
1885 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1886 curwin->w_cursor.col = orig_col;
1887
1888 /* Backspace from cursor to start of line */
1889 backspace_until_column(0);
1890
1891 /* Insert new stuff into line again */
1892 ins_bytes(new_line);
1893
1894 vim_free(new_line);
1895 }
1896#endif
1897}
1898
1899/*
1900 * Truncate the space at the end of a line. This is to be used only in an
1901 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1902 * modes.
1903 */
1904 void
1905truncate_spaces(line)
1906 char_u *line;
1907{
1908 int i;
1909
1910 /* find start of trailing white space */
1911 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1912 {
1913 if (State & REPLACE_FLAG)
1914 replace_join(0); /* remove a NUL from the replace stack */
1915 }
1916 line[i + 1] = NUL;
1917}
1918
1919#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1920 || defined(FEAT_COMMENTS) || defined(PROTO)
1921/*
1922 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1923 * modes correctly. May also be used when not in insert mode at all.
1924 */
1925 void
1926backspace_until_column(col)
1927 int col;
1928{
1929 while ((int)curwin->w_cursor.col > col)
1930 {
1931 curwin->w_cursor.col--;
1932 if (State & REPLACE_FLAG)
1933 replace_do_bs();
1934 else
1935 (void)del_char(FALSE);
1936 }
1937}
1938#endif
1939
1940#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1941/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001942 * CTRL-X pressed in Insert mode.
1943 */
1944 static void
1945ins_ctrl_x()
1946{
1947 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1948 * CTRL-V works like CTRL-N */
1949 if (ctrl_x_mode != CTRL_X_CMDLINE)
1950 {
1951 /* if the next ^X<> won't ADD nothing, then reset
1952 * compl_cont_status */
1953 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001954 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001955 else
1956 compl_cont_status = 0;
1957 /* We're not sure which CTRL-X mode it will be yet */
1958 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1959 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1960 edit_submode_pre = NULL;
1961 showmode();
1962 }
1963}
1964
1965/*
1966 * Return TRUE if the 'dict' or 'tsr' option can be used.
1967 */
1968 static int
1969has_compl_option(dict_opt)
1970 int dict_opt;
1971{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001972 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001973# ifdef FEAT_SPELL
1974 && !curwin->w_p_spell
1975# endif
1976 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001977 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1978 {
1979 ctrl_x_mode = 0;
1980 edit_submode = NULL;
1981 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1982 : (char_u *)_("'thesaurus' option is empty"),
1983 hl_attr(HLF_E));
1984 if (emsg_silent == 0)
1985 {
1986 vim_beep();
1987 setcursor();
1988 out_flush();
1989 ui_delay(2000L, FALSE);
1990 }
1991 return FALSE;
1992 }
1993 return TRUE;
1994}
1995
1996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1998 * This depends on the current mode.
1999 */
2000 int
2001vim_is_ctrl_x_key(c)
2002 int c;
2003{
2004 /* Always allow ^R - let it's results then be checked */
2005 if (c == Ctrl_R)
2006 return TRUE;
2007
Bram Moolenaare3226be2005-12-18 22:10:00 +00002008 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002010 return TRUE;
2011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 switch (ctrl_x_mode)
2013 {
2014 case 0: /* Not in any CTRL-X mode */
2015 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2016 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002017 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2019 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2020 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002021 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2022 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 case CTRL_X_SCROLL:
2024 return (c == Ctrl_Y || c == Ctrl_E);
2025 case CTRL_X_WHOLE_LINE:
2026 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2027 case CTRL_X_FILES:
2028 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2029 case CTRL_X_DICTIONARY:
2030 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2031 case CTRL_X_THESAURUS:
2032 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2033 case CTRL_X_TAGS:
2034 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2035#ifdef FEAT_FIND_ID
2036 case CTRL_X_PATH_PATTERNS:
2037 return (c == Ctrl_P || c == Ctrl_N);
2038 case CTRL_X_PATH_DEFINES:
2039 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2040#endif
2041 case CTRL_X_CMDLINE:
2042 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2043 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002044#ifdef FEAT_COMPL_FUNC
2045 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002046 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002047 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002048 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002049#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002050 case CTRL_X_SPELL:
2051 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 EMSG(_(e_internal));
2054 return FALSE;
2055}
2056
2057/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002058 * Return TRUE when character "c" is part of the item currently being
2059 * completed. Used to decide whether to abandon complete mode when the menu
2060 * is visible.
2061 */
2062 static int
2063ins_compl_accept_char(c)
2064 int c;
2065{
2066 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2067 /* When expanding an identifier only accept identifier chars. */
2068 return vim_isIDc(c);
2069
2070 switch (ctrl_x_mode)
2071 {
2072 case CTRL_X_FILES:
2073 /* When expanding file name only accept file name chars. But not
2074 * path separators, so that "proto/<Tab>" expands files in
2075 * "proto", not "proto/" as a whole */
2076 return vim_isfilec(c) && !vim_ispathsep(c);
2077
2078 case CTRL_X_CMDLINE:
2079 case CTRL_X_OMNI:
2080 /* Command line and Omni completion can work with just about any
2081 * printable character, but do stop at white space. */
2082 return vim_isprintc(c) && !vim_iswhite(c);
2083
2084 case CTRL_X_WHOLE_LINE:
2085 /* For while line completion a space can be part of the line. */
2086 return vim_isprintc(c);
2087 }
2088 return vim_iswordc(c);
2089}
2090
2091/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002092 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002094 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 * the rest of the word to be in -- webb
2096 */
2097 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002098ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 char_u *str;
2100 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002101 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 char_u *fname;
2103 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002104 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002106 char_u *p;
2107 int i, c;
2108 int actual_len; /* Take multi-byte characters */
2109 int actual_compl_length; /* into account. */
2110 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 int has_lower = FALSE;
2112 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002114 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002116 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaara2993e12007-08-12 14:38:46 +00002118 /* Find actual length of completion. */
2119#ifdef FEAT_MBYTE
2120 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002122 p = str;
2123 actual_len = 0;
2124 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002126 mb_ptr_adv(p);
2127 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002130 else
2131#endif
2132 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
Bram Moolenaara2993e12007-08-12 14:38:46 +00002134 /* Find actual length of original text. */
2135#ifdef FEAT_MBYTE
2136 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002138 p = compl_orig_text;
2139 actual_compl_length = 0;
2140 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002142 mb_ptr_adv(p);
2143 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002146 else
2147#endif
2148 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
Bram Moolenaara2993e12007-08-12 14:38:46 +00002150 /* Allocate wide character array for the completion and fill it. */
2151 wca = (int *)alloc(actual_len * sizeof(int));
2152 if (wca != NULL)
2153 {
2154 p = str;
2155 for (i = 0; i < actual_len; ++i)
2156#ifdef FEAT_MBYTE
2157 if (has_mbyte)
2158 wca[i] = mb_ptr2char_adv(&p);
2159 else
2160#endif
2161 wca[i] = *(p++);
2162
2163 /* Rule 1: Were any chars converted to lower? */
2164 p = compl_orig_text;
2165 for (i = 0; i < actual_compl_length; ++i)
2166 {
2167#ifdef FEAT_MBYTE
2168 if (has_mbyte)
2169 c = mb_ptr2char_adv(&p);
2170 else
2171#endif
2172 c = *(p++);
2173 if (MB_ISLOWER(c))
2174 {
2175 has_lower = TRUE;
2176 if (MB_ISUPPER(wca[i]))
2177 {
2178 /* Rule 1 is satisfied. */
2179 for (i = actual_compl_length; i < actual_len; ++i)
2180 wca[i] = MB_TOLOWER(wca[i]);
2181 break;
2182 }
2183 }
2184 }
2185
2186 /*
2187 * Rule 2: No lower case, 2nd consecutive letter converted to
2188 * upper case.
2189 */
2190 if (!has_lower)
2191 {
2192 p = compl_orig_text;
2193 for (i = 0; i < actual_compl_length; ++i)
2194 {
2195#ifdef FEAT_MBYTE
2196 if (has_mbyte)
2197 c = mb_ptr2char_adv(&p);
2198 else
2199#endif
2200 c = *(p++);
2201 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2202 {
2203 /* Rule 2 is satisfied. */
2204 for (i = actual_compl_length; i < actual_len; ++i)
2205 wca[i] = MB_TOUPPER(wca[i]);
2206 break;
2207 }
2208 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2209 }
2210 }
2211
2212 /* Copy the original case of the part we typed. */
2213 p = compl_orig_text;
2214 for (i = 0; i < actual_compl_length; ++i)
2215 {
2216#ifdef FEAT_MBYTE
2217 if (has_mbyte)
2218 c = mb_ptr2char_adv(&p);
2219 else
2220#endif
2221 c = *(p++);
2222 if (MB_ISLOWER(c))
2223 wca[i] = MB_TOLOWER(wca[i]);
2224 else if (MB_ISUPPER(c))
2225 wca[i] = MB_TOUPPER(wca[i]);
2226 }
2227
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002228 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002229 * Generate encoding specific output from wide character array.
2230 * Multi-byte characters can occupy up to five bytes more than
2231 * ASCII characters, and we also need one byte for NUL, so stay
2232 * six bytes away from the edge of IObuff.
2233 */
2234 p = IObuff;
2235 i = 0;
2236 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2237#ifdef FEAT_MBYTE
2238 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002239 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002240 else
2241#endif
2242 *(p++) = wca[i++];
2243 *p = NUL;
2244
2245 vim_free(wca);
2246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002248 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2249 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002251 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252}
2253
2254/*
2255 * Add a match to the list of matches.
2256 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002257 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002258 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002260 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002261ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 char_u *str;
2263 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002264 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002266 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002267 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002268 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002269 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002271 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002272 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274 ui_breakcheck();
2275 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (len < 0)
2278 len = (int)STRLEN(str);
2279
2280 /*
2281 * If the same match is already present, don't add it.
2282 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002283 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002285 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 do
2287 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002288 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002289 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002290 && match->cp_str[len] == NUL)
2291 return NOTDONE;
2292 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002293 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002296 /* Remove any popup menu before changing the list of matches. */
2297 ins_compl_del_pum();
2298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 /*
2300 * Allocate a new match structure.
2301 * Copy the values to the new match structure.
2302 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002303 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002305 return FAIL;
2306 match->cp_number = -1;
2307 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002308 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002309 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002314 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002317 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2319 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002320 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002321 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002322 && compl_curr_match->cp_fname != NULL
2323 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002324 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002325 else if (fname != NULL)
2326 {
2327 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002328 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002331 match->cp_fname = NULL;
2332 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002333
2334 if (cptext != NULL)
2335 {
2336 int i;
2337
2338 for (i = 0; i < CPT_COUNT; ++i)
2339 if (cptext[i] != NULL && *cptext[i] != NUL)
2340 match->cp_text[i] = vim_strsave(cptext[i]);
2341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343 /*
2344 * Link the new match structure in the list of matches.
2345 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002346 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002347 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 else if (dir == FORWARD)
2349 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002350 match->cp_next = compl_curr_match->cp_next;
2351 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 else /* BACKWARD */
2354 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002355 match->cp_next = compl_curr_match;
2356 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002358 if (match->cp_next)
2359 match->cp_next->cp_prev = match;
2360 if (match->cp_prev)
2361 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002363 compl_first_match = match;
2364 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002366 /*
2367 * Find the longest common string if still doing that.
2368 */
2369 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2370 ins_compl_longest_match(match);
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 return OK;
2373}
2374
2375/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002376 * Return TRUE if "str[len]" matches with match->cp_str, considering
2377 * match->cp_icase.
2378 */
2379 static int
2380ins_compl_equal(match, str, len)
2381 compl_T *match;
2382 char_u *str;
2383 int len;
2384{
2385 if (match->cp_icase)
2386 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2387 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2388}
2389
2390/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002391 * Reduce the longest common string for match "match".
2392 */
2393 static void
2394ins_compl_longest_match(match)
2395 compl_T *match;
2396{
2397 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002398 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002399 int had_match;
2400
2401 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002402 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002403 /* First match, use it as a whole. */
2404 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002405 if (compl_leader != NULL)
2406 {
2407 had_match = (curwin->w_cursor.col > compl_col);
2408 ins_compl_delete();
2409 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2410 ins_redraw(FALSE);
2411
2412 /* When the match isn't there (to avoid matching itself) remove it
2413 * again after redrawing. */
2414 if (!had_match)
2415 ins_compl_delete();
2416 compl_used_match = FALSE;
2417 }
2418 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002419 else
2420 {
2421 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002422 p = compl_leader;
2423 s = match->cp_str;
2424 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002425 {
2426#ifdef FEAT_MBYTE
2427 if (has_mbyte)
2428 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002429 c1 = mb_ptr2char(p);
2430 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002431 }
2432 else
2433#endif
2434 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002435 c1 = *p;
2436 c2 = *s;
2437 }
2438 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2439 : (c1 != c2))
2440 break;
2441#ifdef FEAT_MBYTE
2442 if (has_mbyte)
2443 {
2444 mb_ptr_adv(p);
2445 mb_ptr_adv(s);
2446 }
2447 else
2448#endif
2449 {
2450 ++p;
2451 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002452 }
2453 }
2454
2455 if (*p != NUL)
2456 {
2457 /* Leader was shortened, need to change the inserted text. */
2458 *p = NUL;
2459 had_match = (curwin->w_cursor.col > compl_col);
2460 ins_compl_delete();
2461 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2462 ins_redraw(FALSE);
2463
2464 /* When the match isn't there (to avoid matching itself) remove it
2465 * again after redrawing. */
2466 if (!had_match)
2467 ins_compl_delete();
2468 }
2469
2470 compl_used_match = FALSE;
2471 }
2472}
2473
2474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 * Add an array of matches to the list of matches.
2476 * Frees matches[].
2477 */
2478 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002479ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 int num_matches;
2481 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002482 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 int i;
2485 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002486 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar572cb562005-08-05 21:35:02 +00002488 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002489 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002490 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002492 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 FreeWild(num_matches, matches);
2494}
2495
2496/* Make the completion list cyclic.
2497 * Return the number of matches (excluding the original).
2498 */
2499 static int
2500ins_compl_make_cyclic()
2501{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002502 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 int count = 0;
2504
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002505 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 /*
2508 * Find the end of the list.
2509 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002510 match = compl_first_match;
2511 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002512 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002514 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 ++count;
2516 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002517 match->cp_next = compl_first_match;
2518 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520 return count;
2521}
2522
Bram Moolenaara94bc432006-03-10 21:42:59 +00002523/*
2524 * Start completion for the complete() function.
2525 * "startcol" is where the matched text starts (1 is first column).
2526 * "list" is the list of matches.
2527 */
2528 void
2529set_completion(startcol, list)
2530 int startcol;
2531 list_T *list;
2532{
2533 /* If already doing completions stop it. */
2534 if (ctrl_x_mode != 0)
2535 ins_compl_prep(' ');
2536 ins_compl_clear();
2537
2538 if (stop_arrow() == FAIL)
2539 return;
2540
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002541 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002542 startcol = curwin->w_cursor.col;
2543 compl_col = startcol;
2544 compl_length = curwin->w_cursor.col - startcol;
2545 /* compl_pattern doesn't need to be set */
2546 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2547 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002548 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002549 return;
2550
2551 /* Handle like dictionary completion. */
2552 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2553
2554 ins_compl_add_list(list);
2555 compl_matches = ins_compl_make_cyclic();
2556 compl_started = TRUE;
2557 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002558 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002559
2560 compl_curr_match = compl_first_match;
2561 ins_complete(Ctrl_N);
2562 out_flush();
2563}
2564
2565
Bram Moolenaar9372a112005-12-06 19:59:18 +00002566/* "compl_match_array" points the currently displayed list of entries in the
2567 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002568static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002569static int compl_match_arraysize;
2570
2571/*
2572 * Update the screen and when there is any scrolling remove the popup menu.
2573 */
2574 static void
2575ins_compl_upd_pum()
2576{
2577 int h;
2578
2579 if (compl_match_array != NULL)
2580 {
2581 h = curwin->w_cline_height;
2582 update_screen(0);
2583 if (h != curwin->w_cline_height)
2584 ins_compl_del_pum();
2585 }
2586}
2587
2588/*
2589 * Remove any popup menu.
2590 */
2591 static void
2592ins_compl_del_pum()
2593{
2594 if (compl_match_array != NULL)
2595 {
2596 pum_undisplay();
2597 vim_free(compl_match_array);
2598 compl_match_array = NULL;
2599 }
2600}
2601
2602/*
2603 * Return TRUE if the popup menu should be displayed.
2604 */
2605 static int
2606pum_wanted()
2607{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002608 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002609 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002610 return FALSE;
2611
2612 /* The display looks bad on a B&W display. */
2613 if (t_colors < 8
2614#ifdef FEAT_GUI
2615 && !gui.in_use
2616#endif
2617 )
2618 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002619 return TRUE;
2620}
2621
2622/*
2623 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002624 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002625 */
2626 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002627pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002628{
2629 compl_T *compl;
2630 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002631
2632 /* Don't display the popup menu if there are no matches or there is only
2633 * one (ignoring the original text). */
2634 compl = compl_first_match;
2635 i = 0;
2636 do
2637 {
2638 if (compl == NULL
2639 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2640 break;
2641 compl = compl->cp_next;
2642 } while (compl != compl_first_match);
2643
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002644 if (strstr((char *)p_cot, "menuone") != NULL)
2645 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002646 return (i >= 2);
2647}
2648
2649/*
2650 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002651 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002652 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002653 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002654ins_compl_show_pum()
2655{
2656 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002657 compl_T *shown_compl = NULL;
2658 int did_find_shown_match = FALSE;
2659 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002660 int i;
2661 int cur = -1;
2662 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002663 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002664
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002665 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002666 return;
2667
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002668#if defined(FEAT_EVAL)
2669 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2670 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2671#endif
2672
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002673 /* Update the screen before drawing the popup menu over it. */
2674 update_screen(0);
2675
2676 if (compl_match_array == NULL)
2677 {
2678 /* Need to build the popup menu list. */
2679 compl_match_arraysize = 0;
2680 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002681 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002682 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002683 do
2684 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002685 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2686 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002687 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002688 ++compl_match_arraysize;
2689 compl = compl->cp_next;
2690 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002691 if (compl_match_arraysize == 0)
2692 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002693 compl_match_array = (pumitem_T *)alloc_clear(
2694 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002695 * compl_match_arraysize));
2696 if (compl_match_array != NULL)
2697 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002698 /* If the current match is the original text don't find the first
2699 * match after it, don't highlight anything. */
2700 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2701 shown_match_ok = TRUE;
2702
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002703 i = 0;
2704 compl = compl_first_match;
2705 do
2706 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002707 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2708 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002709 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002710 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002711 if (!shown_match_ok)
2712 {
2713 if (compl == compl_shown_match || did_find_shown_match)
2714 {
2715 /* This item is the shown match or this is the
2716 * first displayed item after the shown match. */
2717 compl_shown_match = compl;
2718 did_find_shown_match = TRUE;
2719 shown_match_ok = TRUE;
2720 }
2721 else
2722 /* Remember this displayed match for when the
2723 * shown match is just below it. */
2724 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002725 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002726 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002727
2728 if (compl->cp_text[CPT_ABBR] != NULL)
2729 compl_match_array[i].pum_text =
2730 compl->cp_text[CPT_ABBR];
2731 else
2732 compl_match_array[i].pum_text = compl->cp_str;
2733 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2734 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2735 if (compl->cp_text[CPT_MENU] != NULL)
2736 compl_match_array[i++].pum_extra =
2737 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002738 else
2739 compl_match_array[i++].pum_extra = compl->cp_fname;
2740 }
2741
2742 if (compl == compl_shown_match)
2743 {
2744 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002745
2746 /* When the original text is the shown match don't set
2747 * compl_shown_match. */
2748 if (compl->cp_flags & ORIGINAL_TEXT)
2749 shown_match_ok = TRUE;
2750
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002751 if (!shown_match_ok && shown_compl != NULL)
2752 {
2753 /* The shown match isn't displayed, set it to the
2754 * previously displayed match. */
2755 compl_shown_match = shown_compl;
2756 shown_match_ok = TRUE;
2757 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002758 }
2759 compl = compl->cp_next;
2760 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002761
2762 if (!shown_match_ok) /* no displayed match at all */
2763 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002764 }
2765 }
2766 else
2767 {
2768 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002769 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002770 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2771 || compl_match_array[i].pum_text
2772 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002773 {
2774 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002775 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002776 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002777 }
2778
2779 if (compl_match_array != NULL)
2780 {
2781 /* Compute the screen column of the start of the completed text.
2782 * Use the cursor to get all wrapping and other settings right. */
2783 col = curwin->w_cursor.col;
2784 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002785 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002786 curwin->w_cursor.col = col;
2787 }
2788}
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#define DICT_FIRST (1) /* use just first element in "dict" */
2791#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002792
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002794 * Add any identifiers that match the given pattern in the list of dictionary
2795 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
2797 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002798ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2799 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002801 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002802 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002804 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 char_u *ptr;
2806 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 char_u **files;
2809 int count;
2810 int i;
2811 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002812 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
Bram Moolenaar0b238792006-03-02 22:49:12 +00002814 if (*dict == NUL)
2815 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002816#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002817 /* When 'dictionary' is empty and spell checking is enabled use
2818 * "spell". */
2819 if (!thesaurus && curwin->w_p_spell)
2820 dict = (char_u *)"spell";
2821 else
2822#endif
2823 return;
2824 }
2825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002827 if (buf == NULL)
2828 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002829 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 /* If 'infercase' is set, don't use 'smartcase' here */
2832 save_p_scs = p_scs;
2833 if (curbuf->b_p_inf)
2834 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002835
2836 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2837 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002838 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002839 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2840 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002841 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2842
2843 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002844 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002845 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002846 ptr = alloc(i);
2847 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002848 {
2849 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002850 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002851 }
2852 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2853 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2854 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002855 vim_free(ptr);
2856 }
2857 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002858 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002859 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002860 if (regmatch.regprog == NULL)
2861 goto theend;
2862 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2865 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002866 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
2868 /* copy one dictionary file name into buf */
2869 if (flags == DICT_EXACT)
2870 {
2871 count = 1;
2872 files = &dict;
2873 }
2874 else
2875 {
2876 /* Expand wildcards in the dictionary name, but do not allow
2877 * backticks (for security, the 'dict' option may have been set in
2878 * a modeline). */
2879 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002880# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002881 if (!thesaurus && STRCMP(buf, "spell") == 0)
2882 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002883 else
2884# endif
2885 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 || expand_wildcards(1, &buf, &count, &files,
2887 EW_FILE|EW_SILENT) != OK)
2888 count = 0;
2889 }
2890
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002891# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002892 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002894 /* Complete from active spelling. Skip "\<" in the pattern, we
2895 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002896 if (pat[0] == '\\' && pat[1] == '<')
2897 ptr = pat + 2;
2898 else
2899 ptr = pat;
2900 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002902 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002903# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002904 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002905 {
2906 ins_compl_files(count, files, thesaurus, flags,
2907 &regmatch, buf, &dir);
2908 if (flags != DICT_EXACT)
2909 FreeWild(count, files);
2910 }
2911 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 break;
2913 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002914
2915theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 p_scs = save_p_scs;
2917 vim_free(regmatch.regprog);
2918 vim_free(buf);
2919}
2920
Bram Moolenaar0b238792006-03-02 22:49:12 +00002921 static void
2922ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2923 int count;
2924 char_u **files;
2925 int thesaurus;
2926 int flags;
2927 regmatch_T *regmatch;
2928 char_u *buf;
2929 int *dir;
2930{
2931 char_u *ptr;
2932 int i;
2933 FILE *fp;
2934 int add_r;
2935
2936 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2937 {
2938 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2939 if (flags != DICT_EXACT)
2940 {
2941 vim_snprintf((char *)IObuff, IOSIZE,
2942 _("Scanning dictionary: %s"), (char *)files[i]);
2943 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2944 }
2945
2946 if (fp != NULL)
2947 {
2948 /*
2949 * Read dictionary file line by line.
2950 * Check each line for a match.
2951 */
2952 while (!got_int && !compl_interrupted
2953 && !vim_fgets(buf, LSIZE, fp))
2954 {
2955 ptr = buf;
2956 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2957 {
2958 ptr = regmatch->startp[0];
2959 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2960 ptr = find_line_end(ptr);
2961 else
2962 ptr = find_word_end(ptr);
2963 add_r = ins_compl_add_infercase(regmatch->startp[0],
2964 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002965 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002966 if (thesaurus)
2967 {
2968 char_u *wstart;
2969
2970 /*
2971 * Add the other matches on the line
2972 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002973 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00002974 while (!got_int)
2975 {
2976 /* Find start of the next word. Skip white
2977 * space and punctuation. */
2978 ptr = find_word_start(ptr);
2979 if (*ptr == NUL || *ptr == NL)
2980 break;
2981 wstart = ptr;
2982
Bram Moolenaara2993e12007-08-12 14:38:46 +00002983 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002984#ifdef FEAT_MBYTE
2985 if (has_mbyte)
2986 /* Japanese words may have characters in
2987 * different classes, only separate words
2988 * with single-byte non-word characters. */
2989 while (*ptr != NUL)
2990 {
2991 int l = (*mb_ptr2len)(ptr);
2992
2993 if (l < 2 && !vim_iswordc(*ptr))
2994 break;
2995 ptr += l;
2996 }
2997 else
2998#endif
2999 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003000
3001 /* Add the word. Skip the regexp match. */
3002 if (wstart != regmatch->startp[0])
3003 add_r = ins_compl_add_infercase(wstart,
3004 (int)(ptr - wstart),
3005 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003006 }
3007 }
3008 if (add_r == OK)
3009 /* if dir was BACKWARD then honor it just once */
3010 *dir = FORWARD;
3011 else if (add_r == FAIL)
3012 break;
3013 /* avoid expensive call to vim_regexec() when at end
3014 * of line */
3015 if (*ptr == '\n' || got_int)
3016 break;
3017 }
3018 line_breakcheck();
3019 ins_compl_check_keys(50);
3020 }
3021 fclose(fp);
3022 }
3023 }
3024}
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026/*
3027 * Find the start of the next word.
3028 * Returns a pointer to the first char of the word. Also stops at a NUL.
3029 */
3030 char_u *
3031find_word_start(ptr)
3032 char_u *ptr;
3033{
3034#ifdef FEAT_MBYTE
3035 if (has_mbyte)
3036 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003037 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else
3039#endif
3040 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3041 ++ptr;
3042 return ptr;
3043}
3044
3045/*
3046 * Find the end of the word. Assumes it starts inside a word.
3047 * Returns a pointer to just after the word.
3048 */
3049 char_u *
3050find_word_end(ptr)
3051 char_u *ptr;
3052{
3053#ifdef FEAT_MBYTE
3054 int start_class;
3055
3056 if (has_mbyte)
3057 {
3058 start_class = mb_get_class(ptr);
3059 if (start_class > 1)
3060 while (*ptr != NUL)
3061 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003062 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 if (mb_get_class(ptr) != start_class)
3064 break;
3065 }
3066 }
3067 else
3068#endif
3069 while (vim_iswordc(*ptr))
3070 ++ptr;
3071 return ptr;
3072}
3073
3074/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003075 * Find the end of the line, omitting CR and NL at the end.
3076 * Returns a pointer to just after the line.
3077 */
3078 static char_u *
3079find_line_end(ptr)
3080 char_u *ptr;
3081{
3082 char_u *s;
3083
3084 s = ptr + STRLEN(ptr);
3085 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3086 --s;
3087 return s;
3088}
3089
3090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * Free the list of completions
3092 */
3093 static void
3094ins_compl_free()
3095{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003096 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003097 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003099 vim_free(compl_pattern);
3100 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003101 vim_free(compl_leader);
3102 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003104 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003106
3107 ins_compl_del_pum();
3108 pum_clear();
3109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003110 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 do
3112 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003113 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003114 compl_curr_match = compl_curr_match->cp_next;
3115 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003117 if (match->cp_flags & FREE_FNAME)
3118 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003119 for (i = 0; i < CPT_COUNT; ++i)
3120 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003122 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3123 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124}
3125
3126 static void
3127ins_compl_clear()
3128{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003129 compl_cont_status = 0;
3130 compl_started = FALSE;
3131 compl_matches = 0;
3132 vim_free(compl_pattern);
3133 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003134 vim_free(compl_leader);
3135 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003137 vim_free(compl_orig_text);
3138 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003139 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141
3142/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003143 * Return TRUE when Insert completion is active.
3144 */
3145 int
3146ins_compl_active()
3147{
3148 return compl_started;
3149}
3150
3151/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003152 * Delete one character before the cursor and show the subset of the matches
3153 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003154 * Returns the character to be used, NUL if the work is done and another char
3155 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003156 */
3157 static int
3158ins_compl_bs()
3159{
3160 char_u *line;
3161 char_u *p;
3162
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003163 line = ml_get_curline();
3164 p = line + curwin->w_cursor.col;
3165 mb_ptr_back(line, p);
3166
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003167 /* Stop completion when the whole word was deleted. For Omni completion
3168 * allow the word to be deleted, we won't match everything. */
3169 if ((int)(p - line) - (int)compl_col < 0
3170 || ((int)(p - line) - (int)compl_col == 0
3171 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003172 return K_BS;
3173
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003174 /* Deleted more than what was used to find matches or didn't finish
3175 * finding all matches: need to look for matches all over again. */
3176 if (curwin->w_cursor.col <= compl_col + compl_length
3177 || compl_was_interrupted)
3178 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003179
Bram Moolenaara6557602006-02-04 22:43:20 +00003180 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003181 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003182 if (compl_leader != NULL)
3183 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003184 ins_compl_new_leader();
3185 return NUL;
3186 }
3187 return K_BS;
3188}
Bram Moolenaara6557602006-02-04 22:43:20 +00003189
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003190/*
3191 * Called after changing "compl_leader".
3192 * Show the popup menu with a different set of matches.
3193 * May also search for matches again if the previous search was interrupted.
3194 */
3195 static void
3196ins_compl_new_leader()
3197{
3198 ins_compl_del_pum();
3199 ins_compl_delete();
3200 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3201 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003202
3203 if (compl_started)
3204 ins_compl_set_original_text(compl_leader);
3205 else
3206 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003207#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003208 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003209#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003210 /*
3211 * Matches were cleared, need to search for them now. First display
3212 * the changed text before the cursor. Set "compl_restarting" to
3213 * avoid that the first match is inserted.
3214 */
3215 update_screen(0);
3216#ifdef FEAT_GUI
3217 if (gui.in_use)
3218 {
3219 /* Show the cursor after the match, not after the redrawn text. */
3220 setcursor();
3221 out_flush();
3222 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003223 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003224#endif
3225 compl_restarting = TRUE;
3226 if (ins_complete(Ctrl_N) == FAIL)
3227 compl_cont_status = 0;
3228 compl_restarting = FALSE;
3229 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003230
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003231#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003232 if (!compl_used_match)
3233 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003234 /* Go to the original text, since none of the matches is inserted. */
3235 if (compl_first_match->cp_prev != NULL
3236 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3237 compl_shown_match = compl_first_match->cp_prev;
3238 else
3239 compl_shown_match = compl_first_match;
3240 compl_curr_match = compl_shown_match;
3241 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003242 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003243#endif
3244 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003245
3246 /* Show the popup menu with a different set of matches. */
3247 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003248
3249 /* Don't let Enter select the original text when there is no popup menu. */
3250 if (compl_match_array == NULL)
3251 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003252}
3253
3254/*
3255 * Append one character to the match leader. May reduce the number of
3256 * matches.
3257 */
3258 static void
3259ins_compl_addleader(c)
3260 int c;
3261{
3262#ifdef FEAT_MBYTE
3263 int cc;
3264
3265 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3266 {
3267 char_u buf[MB_MAXBYTES + 1];
3268
3269 (*mb_char2bytes)(c, buf);
3270 buf[cc] = NUL;
3271 ins_char_bytes(buf, cc);
3272 }
3273 else
3274#endif
3275 ins_char(c);
3276
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003277 /* If we didn't complete finding matches we must search again. */
3278 if (compl_was_interrupted)
3279 ins_compl_restart();
3280
Bram Moolenaara6557602006-02-04 22:43:20 +00003281 vim_free(compl_leader);
3282 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3283 curwin->w_cursor.col - compl_col);
3284 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003285 ins_compl_new_leader();
3286}
3287
3288/*
3289 * Setup for finding completions again without leaving CTRL-X mode. Used when
3290 * BS or a key was typed while still searching for matches.
3291 */
3292 static void
3293ins_compl_restart()
3294{
3295 ins_compl_free();
3296 compl_started = FALSE;
3297 compl_matches = 0;
3298 compl_cont_status = 0;
3299 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003300}
3301
3302/*
3303 * Set the first match, the original text.
3304 */
3305 static void
3306ins_compl_set_original_text(str)
3307 char_u *str;
3308{
3309 char_u *p;
3310
3311 /* Replace the original text entry. */
3312 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3313 {
3314 p = vim_strsave(str);
3315 if (p != NULL)
3316 {
3317 vim_free(compl_first_match->cp_str);
3318 compl_first_match->cp_str = p;
3319 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003320 }
3321}
3322
3323/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003324 * Append one character to the match leader. May reduce the number of
3325 * matches.
3326 */
3327 static void
3328ins_compl_addfrommatch()
3329{
3330 char_u *p;
3331 int len = curwin->w_cursor.col - compl_col;
3332 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003333 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003334
3335 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003336 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003337 {
3338 /* When still at the original match use the first entry that matches
3339 * the leader. */
3340 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3341 {
3342 p = NULL;
3343 for (cp = compl_shown_match->cp_next; cp != NULL
3344 && cp != compl_first_match; cp = cp->cp_next)
3345 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003346 if (compl_leader == NULL
3347 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003348 (int)STRLEN(compl_leader)))
3349 {
3350 p = cp->cp_str;
3351 break;
3352 }
3353 }
3354 if (p == NULL || (int)STRLEN(p) <= len)
3355 return;
3356 }
3357 else
3358 return;
3359 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003360 p += len;
3361#ifdef FEAT_MBYTE
3362 c = mb_ptr2char(p);
3363#else
3364 c = *p;
3365#endif
3366 ins_compl_addleader(c);
3367}
3368
3369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003371 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003372 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003374 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375ins_compl_prep(c)
3376 int c;
3377{
3378 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003380 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 /* Forget any previous 'special' messages if this is actually
3383 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3384 */
3385 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3386 edit_submode_extra = NULL;
3387
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003388 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3389 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003390 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003392 /* Set "compl_get_longest" when finding the first matches. */
3393 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3394 || (ctrl_x_mode == 0 && !compl_started))
3395 {
3396 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3397 compl_used_match = TRUE;
3398 }
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3401 {
3402 /*
3403 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3404 * it will be yet. Now we decide.
3405 */
3406 switch (c)
3407 {
3408 case Ctrl_E:
3409 case Ctrl_Y:
3410 ctrl_x_mode = CTRL_X_SCROLL;
3411 if (!(State & REPLACE_FLAG))
3412 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3413 else
3414 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3415 edit_submode_pre = NULL;
3416 showmode();
3417 break;
3418 case Ctrl_L:
3419 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3420 break;
3421 case Ctrl_F:
3422 ctrl_x_mode = CTRL_X_FILES;
3423 break;
3424 case Ctrl_K:
3425 ctrl_x_mode = CTRL_X_DICTIONARY;
3426 break;
3427 case Ctrl_R:
3428 /* Simply allow ^R to happen without affecting ^X mode */
3429 break;
3430 case Ctrl_T:
3431 ctrl_x_mode = CTRL_X_THESAURUS;
3432 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003433#ifdef FEAT_COMPL_FUNC
3434 case Ctrl_U:
3435 ctrl_x_mode = CTRL_X_FUNCTION;
3436 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003437 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003438 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003439 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003440#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003441 case 's':
3442 case Ctrl_S:
3443 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003444#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003445 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003446 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003447 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003448#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003449 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 case Ctrl_RSB:
3451 ctrl_x_mode = CTRL_X_TAGS;
3452 break;
3453#ifdef FEAT_FIND_ID
3454 case Ctrl_I:
3455 case K_S_TAB:
3456 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3457 break;
3458 case Ctrl_D:
3459 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3460 break;
3461#endif
3462 case Ctrl_V:
3463 case Ctrl_Q:
3464 ctrl_x_mode = CTRL_X_CMDLINE;
3465 break;
3466 case Ctrl_P:
3467 case Ctrl_N:
3468 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3469 * just started ^X mode, or there were enough ^X's to cancel
3470 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3471 * do normal expansion when interrupting a different mode (say
3472 * ^X^F^X^P or ^P^X^X^P, see below)
3473 * nothing changes if interrupting mode 0, (eg, the flag
3474 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003475 if (!(compl_cont_status & CONT_INTRPT))
3476 compl_cont_status |= CONT_LOCAL;
3477 else if (compl_cont_mode != 0)
3478 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 /* FALLTHROUGH */
3480 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003481 /* If we have typed at least 2 ^X's... for modes != 0, we set
3482 * compl_cont_status = 0 (eg, as if we had just started ^X
3483 * mode).
3484 * For mode 0, we set "compl_cont_mode" to an impossible
3485 * value, in both cases ^X^X can be used to restart the same
3486 * mode (avoiding ADDING mode).
3487 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3488 * 'complete' and local ^P expansions respectively.
3489 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3490 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (c == Ctrl_X)
3492 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 if (compl_cont_mode != 0)
3494 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003496 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
3498 ctrl_x_mode = 0;
3499 edit_submode = NULL;
3500 showmode();
3501 break;
3502 }
3503 }
3504 else if (ctrl_x_mode != 0)
3505 {
3506 /* We're already in CTRL-X mode, do we stay in it? */
3507 if (!vim_is_ctrl_x_key(c))
3508 {
3509 if (ctrl_x_mode == CTRL_X_SCROLL)
3510 ctrl_x_mode = 0;
3511 else
3512 ctrl_x_mode = CTRL_X_FINISHED;
3513 edit_submode = NULL;
3514 }
3515 showmode();
3516 }
3517
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003518 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 /* Show error message from attempted keyword completion (probably
3521 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003522 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003524 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3525 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 || ctrl_x_mode == CTRL_X_FINISHED)
3527 {
3528 /* Get here when we have finished typing a sequence of ^N and
3529 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003530 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003531 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003533 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003534 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003537 * If any of the original typed text has been changed, eg when
3538 * ignorecase is set, we must add back-spaces to the redo
3539 * buffer. We add as few as necessary to delete just the part
3540 * of the original text that has changed.
3541 * When using the longest match, edited the match or used
3542 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003544 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3545 ptr = compl_curr_match->cp_str;
3546 else if (compl_leader != NULL)
3547 ptr = compl_leader;
3548 else
3549 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003550 if (compl_orig_text != NULL)
3551 {
3552 p = compl_orig_text;
3553 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3554 ++temp)
3555 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003556#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003557 if (temp > 0)
3558 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003559#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003560 for (p += temp; *p != NUL; mb_ptr_adv(p))
3561 AppendCharToRedobuff(K_BS);
3562 }
3563 if (ptr != NULL)
3564 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566
3567#ifdef FEAT_CINDENT
3568 want_cindent = (can_cindent && cindent_on());
3569#endif
3570 /*
3571 * When completing whole lines: fix indent for 'cindent'.
3572 * Otherwise, break line if it's too long.
3573 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003574 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576#ifdef FEAT_CINDENT
3577 /* re-indent the current line */
3578 if (want_cindent)
3579 {
3580 do_c_expr_indent();
3581 want_cindent = FALSE; /* don't do it again */
3582 }
3583#endif
3584 }
3585 else
3586 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003587 int prev_col = curwin->w_cursor.col;
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003590 if (prev_col > 0)
3591 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (stop_arrow() == OK)
3593 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003594 if (prev_col > 0
3595 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3596 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
3599 auto_format(FALSE, TRUE);
3600
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003601 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003602 * the selection without inserting anything. When
3603 * compl_enter_selects is set the Enter key does the same. */
3604 if ((c == Ctrl_Y || (compl_enter_selects
3605 && (c == CAR || c == K_KENTER || c == NL)))
3606 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003607 retval = TRUE;
3608
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003609 /* CTRL-E means completion is Ended, go back to the typed text. */
3610 if (c == Ctrl_E)
3611 {
3612 ins_compl_delete();
3613 if (compl_leader != NULL)
3614 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3615 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003616 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003617 + curwin->w_cursor.col - compl_col);
3618 retval = TRUE;
3619 }
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003622 compl_started = FALSE;
3623 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 msg_clr_cmdline(); /* necessary for "noshowmode" */
3625 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003626 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 if (edit_submode != NULL)
3628 {
3629 edit_submode = NULL;
3630 showmode();
3631 }
3632
3633#ifdef FEAT_CINDENT
3634 /*
3635 * Indent now if a key was typed that is in 'cinkeys'.
3636 */
3637 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3638 do_c_expr_indent();
3639#endif
3640 }
3641 }
3642
3643 /* reset continue_* if we left expansion-mode, if we stay they'll be
3644 * (re)set properly in ins_complete() */
3645 if (!vim_is_ctrl_x_key(c))
3646 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003647 compl_cont_status = 0;
3648 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003650
3651 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652}
3653
3654/*
3655 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3656 * (depending on flag) starting from buf and looking for a non-scanned
3657 * buffer (other than curbuf). curbuf is special, if it is called with
3658 * buf=curbuf then it has to be the first call for a given flag/expansion.
3659 *
3660 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3661 */
3662 static buf_T *
3663ins_compl_next_buf(buf, flag)
3664 buf_T *buf;
3665 int flag;
3666{
3667#ifdef FEAT_WINDOWS
3668 static win_T *wp;
3669#endif
3670
3671 if (flag == 'w') /* just windows */
3672 {
3673#ifdef FEAT_WINDOWS
3674 if (buf == curbuf) /* first call for this flag/expansion */
3675 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003676 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 && wp->w_buffer->b_scanned)
3678 ;
3679 buf = wp->w_buffer;
3680#else
3681 buf = curbuf;
3682#endif
3683 }
3684 else
3685 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3686 * (unlisted buffers)
3687 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003688 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 && ((flag == 'U'
3690 ? buf->b_p_bl
3691 : (!buf->b_p_bl
3692 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003693 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 ;
3695 return buf;
3696}
3697
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003698#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003699static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003700
3701/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003702 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003703 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003704 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003705 static void
3706expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003707 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003708 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003709{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003710 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003711 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003712 char_u *funcname;
3713 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003714
Bram Moolenaare344bea2005-09-01 20:46:49 +00003715 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3716 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003717 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003718
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003719 /* Call 'completefunc' to obtain the list of matches. */
3720 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003721 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003722
Bram Moolenaare344bea2005-09-01 20:46:49 +00003723 pos = curwin->w_cursor;
3724 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3725 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003726 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003727 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003728
Bram Moolenaara94bc432006-03-10 21:42:59 +00003729 ins_compl_add_list(matchlist);
3730 list_unref(matchlist);
3731}
3732#endif /* FEAT_COMPL_FUNC */
3733
Bram Moolenaar39f05632006-03-19 22:15:26 +00003734#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003735/*
3736 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003737 */
3738 static void
3739ins_compl_add_list(list)
3740 list_T *list;
3741{
3742 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003743 int dir = compl_direction;
3744
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003745 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003746 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003747 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003748 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3749 /* if dir was BACKWARD then honor it just once */
3750 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003751 else if (did_emsg)
3752 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003753 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003754}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003755
3756/*
3757 * Add a match to the list of matches from a typeval_T.
3758 * If the given string is already in the list of completions, then return
3759 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3760 * maybe because alloc() returns NULL, then FAIL is returned.
3761 */
3762 int
3763ins_compl_add_tv(tv, dir)
3764 typval_T *tv;
3765 int dir;
3766{
3767 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003768 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003769 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003770 char_u *(cptext[CPT_COUNT]);
3771
3772 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3773 {
3774 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3775 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3776 (char_u *)"abbr", FALSE);
3777 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3778 (char_u *)"menu", FALSE);
3779 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3780 (char_u *)"kind", FALSE);
3781 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3782 (char_u *)"info", FALSE);
3783 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3784 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003785 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003786 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003787 }
3788 else
3789 {
3790 word = get_tv_string_chk(tv);
3791 vim_memset(cptext, 0, sizeof(cptext));
3792 }
3793 if (word == NULL || *word == NUL)
3794 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003795 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003796}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003797#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003798
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003799/*
3800 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003801 * The search starts at position "ini" in curbuf and in the direction
3802 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003803 * When "compl_started" is FALSE start at that position, otherwise continue
3804 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003805 * This may return before finding all the matches.
3806 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 */
3808 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003809ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 static pos_T first_match_pos;
3813 static pos_T last_match_pos;
3814 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 static int found_all = FALSE; /* Found all matches of a
3816 certain type. */
3817 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
Bram Moolenaar572cb562005-08-05 21:35:02 +00003819 pos_T *pos;
3820 char_u **matches;
3821 int save_p_scs;
3822 int save_p_ws;
3823 int save_p_ic;
3824 int i;
3825 int num_matches;
3826 int len;
3827 int found_new_match;
3828 int type = ctrl_x_mode;
3829 char_u *ptr;
3830 char_u *dict = NULL;
3831 int dict_f = 0;
3832 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 {
3836 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3837 ins_buf->b_scanned = 0;
3838 found_all = FALSE;
3839 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003840 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003841 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 last_match_pos = first_match_pos = *ini;
3843 }
3844
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003846 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3848 for (;;)
3849 {
3850 found_new_match = FAIL;
3851
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 * or if found_all says this entry is done. For ^X^L only use the
3854 * entries from 'complete' that look in loaded buffers. */
3855 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
3858 found_all = FALSE;
3859 while (*e_cpt == ',' || *e_cpt == ' ')
3860 e_cpt++;
3861 if (*e_cpt == '.' && !curbuf->b_scanned)
3862 {
3863 ins_buf = curbuf;
3864 first_match_pos = *ini;
3865 /* So that ^N can match word immediately after cursor */
3866 if (ctrl_x_mode == 0)
3867 dec(&first_match_pos);
3868 last_match_pos = first_match_pos;
3869 type = 0;
3870 }
3871 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3872 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3873 {
3874 /* Scan a buffer, but not the current one. */
3875 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3876 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003877 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 first_match_pos.col = last_match_pos.col = 0;
3879 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3880 last_match_pos.lnum = 0;
3881 type = 0;
3882 }
3883 else /* unloaded buffer, scan like dictionary */
3884 {
3885 found_all = TRUE;
3886 if (ins_buf->b_fname == NULL)
3887 continue;
3888 type = CTRL_X_DICTIONARY;
3889 dict = ins_buf->b_fname;
3890 dict_f = DICT_EXACT;
3891 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003892 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 ins_buf->b_fname == NULL
3894 ? buf_spname(ins_buf)
3895 : ins_buf->b_sfname == NULL
3896 ? (char *)ins_buf->b_fname
3897 : (char *)ins_buf->b_sfname);
3898 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3899 }
3900 else if (*e_cpt == NUL)
3901 break;
3902 else
3903 {
3904 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3905 type = -1;
3906 else if (*e_cpt == 'k' || *e_cpt == 's')
3907 {
3908 if (*e_cpt == 'k')
3909 type = CTRL_X_DICTIONARY;
3910 else
3911 type = CTRL_X_THESAURUS;
3912 if (*++e_cpt != ',' && *e_cpt != NUL)
3913 {
3914 dict = e_cpt;
3915 dict_f = DICT_FIRST;
3916 }
3917 }
3918#ifdef FEAT_FIND_ID
3919 else if (*e_cpt == 'i')
3920 type = CTRL_X_PATH_PATTERNS;
3921 else if (*e_cpt == 'd')
3922 type = CTRL_X_PATH_DEFINES;
3923#endif
3924 else if (*e_cpt == ']' || *e_cpt == 't')
3925 {
3926 type = CTRL_X_TAGS;
3927 sprintf((char*)IObuff, _("Scanning tags."));
3928 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3929 }
3930 else
3931 type = -1;
3932
3933 /* in any case e_cpt is advanced to the next entry */
3934 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3935
3936 found_all = TRUE;
3937 if (type == -1)
3938 continue;
3939 }
3940 }
3941
3942 switch (type)
3943 {
3944 case -1:
3945 break;
3946#ifdef FEAT_FIND_ID
3947 case CTRL_X_PATH_PATTERNS:
3948 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003949 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003952 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3954 (linenr_T)1, (linenr_T)MAXLNUM);
3955 break;
3956#endif
3957
3958 case CTRL_X_DICTIONARY:
3959 case CTRL_X_THESAURUS:
3960 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003961 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 : (type == CTRL_X_THESAURUS
3963 ? (*curbuf->b_p_tsr == NUL
3964 ? p_tsr
3965 : curbuf->b_p_tsr)
3966 : (*curbuf->b_p_dict == NUL
3967 ? p_dict
3968 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003969 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003970 dict != NULL ? dict_f
3971 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 dict = NULL;
3973 break;
3974
3975 case CTRL_X_TAGS:
3976 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3977 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003978 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 * of matches is found when compl_pattern is empty */
3982 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3984 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3985 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3986 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003987 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 p_ic = save_p_ic;
3990 break;
3991
3992 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003993 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3995 {
3996
3997 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003998 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003999 ins_compl_add_matches(num_matches, matches,
4000#ifdef CASE_INSENSITIVE_FILENAME
4001 TRUE
4002#else
4003 FALSE
4004#endif
4005 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 break;
4008
4009 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004010 if (expand_cmdline(&compl_xp, compl_pattern,
4011 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004013 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 break;
4015
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004016#ifdef FEAT_COMPL_FUNC
4017 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004018 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004019 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004020 break;
4021#endif
4022
Bram Moolenaar488c6512005-08-11 20:09:58 +00004023 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004024#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004025 num_matches = expand_spelling(first_match_pos.lnum,
4026 first_match_pos.col, compl_pattern, &matches);
4027 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004028 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004029#endif
4030 break;
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 default: /* normal ^P/^N and ^X^L */
4033 /*
4034 * If 'infercase' is set, don't use 'smartcase' here
4035 */
4036 save_p_scs = p_scs;
4037 if (ins_buf->b_p_inf)
4038 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 /* buffers other than curbuf are scanned from the beginning or the
4041 * end but never from the middle, thus setting nowrapscan in this
4042 * buffers is a good idea, on the other hand, we always set
4043 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4044 save_p_ws = p_ws;
4045 if (ins_buf != curbuf)
4046 p_ws = FALSE;
4047 else if (*e_cpt == '.')
4048 p_ws = TRUE;
4049 for (;;)
4050 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004051 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004053 ++msg_silent; /* Don't want messages for wrapscan. */
4054
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004055 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4056 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004060 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004062 found_new_match = searchit(NULL, ins_buf, pos,
4063 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004065 RE_LAST, (linenr_T)0);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004066 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004067 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004069 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 first_match_pos = *pos;
4072 last_match_pos = *pos;
4073 }
4074 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004075 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 found_new_match = FAIL;
4077 if (found_new_match == FAIL)
4078 {
4079 if (ins_buf == curbuf)
4080 found_all = TRUE;
4081 break;
4082 }
4083
4084 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 && ini->lnum == pos->lnum
4087 && ini->col == pos->col)
4088 continue;
4089 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4090 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004092 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
4094 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4095 continue;
4096 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4097 if (!p_paste)
4098 ptr = skipwhite(ptr);
4099 }
4100 len = (int)STRLEN(ptr);
4101 }
4102 else
4103 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104 char_u *tmp_ptr = ptr;
4105
4106 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004108 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 /* Skip if already inside a word. */
4110 if (vim_iswordp(tmp_ptr))
4111 continue;
4112 /* Find start of next word. */
4113 tmp_ptr = find_word_start(tmp_ptr);
4114 }
4115 /* Find end of this word. */
4116 tmp_ptr = find_word_end(tmp_ptr);
4117 len = (int)(tmp_ptr - ptr);
4118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004119 if ((compl_cont_status & CONT_ADDING)
4120 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4123 {
4124 /* Try next line, if any. the new word will be
4125 * "join" as if the normal command "J" was used.
4126 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * works -- Acevedo */
4129 STRNCPY(IObuff, ptr, len);
4130 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4131 tmp_ptr = ptr = skipwhite(ptr);
4132 /* Find start of next word. */
4133 tmp_ptr = find_word_start(tmp_ptr);
4134 /* Find end of next word. */
4135 tmp_ptr = find_word_end(tmp_ptr);
4136 if (tmp_ptr > ptr)
4137 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004138 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004140 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 IObuff[len++] = ' ';
4142 /* IObuf =~ "\k.* ", thus len >= 2 */
4143 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004144 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 || (vim_strchr(p_cpo, CPO_JOINSP)
4146 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004147 && (IObuff[len - 2] == '?'
4148 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 IObuff[len++] = ' ';
4150 }
4151 /* copy as much as posible of the new word */
4152 if (tmp_ptr - ptr >= IOSIZE - len)
4153 tmp_ptr = ptr + IOSIZE - len - 1;
4154 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4155 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004156 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
4158 IObuff[len] = NUL;
4159 ptr = IObuff;
4160 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 continue;
4163 }
4164 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004165 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004166 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004167 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
4169 found_new_match = OK;
4170 break;
4171 }
4172 }
4173 p_scs = save_p_scs;
4174 p_ws = save_p_ws;
4175 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 /* check if compl_curr_match has changed, (e.g. other type of
4178 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004179 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 found_new_match = OK;
4181
4182 /* break the loop for specialized modes (use 'complete' just for the
4183 * generic ctrl_x_mode == 0) or when we've found a new match */
4184 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004186 {
4187 if (got_int)
4188 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004189 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004190 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004191 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004192
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004193 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4194 || compl_interrupted)
4195 break;
4196 compl_started = TRUE;
4197 }
4198 else
4199 {
4200 /* Mark a buffer scanned when it has been scanned completely */
4201 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4202 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004204 compl_started = FALSE;
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004207 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4210 && *e_cpt == NUL) /* Got to end of 'complete' */
4211 found_new_match = FAIL;
4212
4213 i = -1; /* total of matches, unknown */
4214 if (found_new_match == FAIL
4215 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4216 i = ins_compl_make_cyclic();
4217
4218 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219 * just been made cyclic then we have to move compl_curr_match to the next
4220 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004221 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4222 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 if (compl_curr_match == NULL)
4224 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return i;
4226}
4227
4228/* Delete the old text being completed. */
4229 static void
4230ins_compl_delete()
4231{
4232 int i;
4233
4234 /*
4235 * In insert mode: Delete the typed part.
4236 * In replace mode: Put the old characters back, if any.
4237 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 backspace_until_column(i);
4240 changed_cline_bef_curs();
4241}
4242
4243/* Insert the new text being completed. */
4244 static void
4245ins_compl_insert()
4246{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004247 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004248 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4249 compl_used_match = FALSE;
4250 else
4251 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252}
4253
4254/*
4255 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004256 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4257 * get more completions. If it is FALSE, then we just do nothing when there
4258 * are no more completions in a given direction. The latter case is used when
4259 * we are still in the middle of finding completions, to allow browsing
4260 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 * Return the total number of matches, or -1 if still unknown -- webb.
4262 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004263 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4264 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 *
4266 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004267 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4268 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 */
4270 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004271ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004273 int count; /* repeat completion this many times; should
4274 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004275 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
4277 int num_matches = -1;
4278 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004279 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004280 compl_T *found_compl = NULL;
4281 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004282 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004284 if (compl_leader != NULL
4285 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004287 /* Set "compl_shown_match" to the actually shown match, it may differ
4288 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004289 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004290 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004291 && compl_shown_match->cp_next != NULL
4292 && compl_shown_match->cp_next != compl_first_match)
4293 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004294
4295 /* If we didn't find it searching forward, and compl_shows_dir is
4296 * backward, find the last match. */
4297 if (compl_shows_dir == BACKWARD
4298 && !ins_compl_equal(compl_shown_match,
4299 compl_leader, (int)STRLEN(compl_leader))
4300 && (compl_shown_match->cp_next == NULL
4301 || compl_shown_match->cp_next == compl_first_match))
4302 {
4303 while (!ins_compl_equal(compl_shown_match,
4304 compl_leader, (int)STRLEN(compl_leader))
4305 && compl_shown_match->cp_prev != NULL
4306 && compl_shown_match->cp_prev != compl_first_match)
4307 compl_shown_match = compl_shown_match->cp_prev;
4308 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004309 }
4310
4311 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004312 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 /* Delete old text to be replaced */
4314 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004315
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004316 /* When finding the longest common text we stick at the original text,
4317 * don't let CTRL-N or CTRL-P move to the first match. */
4318 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4319
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004320 /* When restarting the search don't insert the first match either. */
4321 if (compl_restarting)
4322 {
4323 advance = FALSE;
4324 compl_restarting = FALSE;
4325 }
4326
Bram Moolenaare3226be2005-12-18 22:10:00 +00004327 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4328 * around. */
4329 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004331 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004333 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004334 found_end = (compl_first_match != NULL
4335 && (compl_shown_match->cp_next == compl_first_match
4336 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004337 }
4338 else if (compl_shows_dir == BACKWARD
4339 && compl_shown_match->cp_prev != NULL)
4340 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004341 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004342 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004343 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004346 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004347 if (!allow_get_expansion)
4348 {
4349 if (advance)
4350 {
4351 if (compl_shows_dir == BACKWARD)
4352 compl_pending -= todo + 1;
4353 else
4354 compl_pending += todo + 1;
4355 }
4356 return -1;
4357 }
4358
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004359 if (advance)
4360 {
4361 if (compl_shows_dir == BACKWARD)
4362 --compl_pending;
4363 else
4364 ++compl_pending;
4365 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004366
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004367 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004368 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004369
4370 /* handle any pending completions */
4371 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004372 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004373 {
4374 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4375 {
4376 compl_shown_match = compl_shown_match->cp_next;
4377 --compl_pending;
4378 }
4379 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4380 {
4381 compl_shown_match = compl_shown_match->cp_prev;
4382 ++compl_pending;
4383 }
4384 else
4385 break;
4386 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004387 found_end = FALSE;
4388 }
4389 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4390 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004391 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004392 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004393 ++todo;
4394 else
4395 /* Remember a matching item. */
4396 found_compl = compl_shown_match;
4397
4398 /* Stop at the end of the list when we found a usable match. */
4399 if (found_end)
4400 {
4401 if (found_compl != NULL)
4402 {
4403 compl_shown_match = found_compl;
4404 break;
4405 }
4406 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004410 /* Insert the text of the new completion, or the compl_leader. */
4411 if (insert_match)
4412 {
4413 if (!compl_get_longest || compl_used_match)
4414 ins_compl_insert();
4415 else
4416 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4417 }
4418 else
4419 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 if (!allow_get_expansion)
4422 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004423 /* may undisplay the popup menu first */
4424 ins_compl_upd_pum();
4425
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004426 /* redraw to show the user what was inserted */
4427 update_screen(0);
4428
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004429 /* display the updated popup menu */
4430 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004431#ifdef FEAT_GUI
4432 if (gui.in_use)
4433 {
4434 /* Show the cursor after the match, not after the redrawn text. */
4435 setcursor();
4436 out_flush();
4437 gui_update_cursor(FALSE, FALSE);
4438 }
4439#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004440
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 /* Delete old text to be replaced, since we're still searching and
4442 * don't want to match ourselves! */
4443 ins_compl_delete();
4444 }
4445
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004446 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004447 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004448 compl_enter_selects = !insert_match && compl_match_array != NULL;
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 /*
4451 * Show the file name for the match (if any)
4452 * Truncate the file name to avoid a wait for return.
4453 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004454 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
4456 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004457 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 if (i <= 0)
4459 i = 0;
4460 else
4461 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004462 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 msg(IObuff);
4464 redraw_cmdline = FALSE; /* don't overwrite! */
4465 }
4466
4467 return num_matches;
4468}
4469
4470/*
4471 * Call this while finding completions, to check whether the user has hit a key
4472 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004473 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004475 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 */
4477 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004478ins_compl_check_keys(frequency)
4479 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480{
4481 static int count = 0;
4482
4483 int c;
4484
4485 /* Don't check when reading keys from a script. That would break the test
4486 * scripts */
4487 if (using_script())
4488 return;
4489
4490 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004491 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 return;
4493 count = 0;
4494
Bram Moolenaara260a972006-06-23 19:36:29 +00004495 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4496 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (c != NUL)
4499 {
4500 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4501 {
4502 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004503 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004504 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4505 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004507 else
4508 {
4509 /* Need to get the character to have KeyTyped set. We'll put it
4510 * back with vungetc() below. */
4511 c = safe_vgetc();
4512
4513 /* Don't interrupt completion when the character wasn't typed,
4514 * e.g., when doing @q to replay keys. */
4515 if (c != Ctrl_R && KeyTyped)
4516 compl_interrupted = TRUE;
4517
4518 vungetc(c);
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004521 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004522 {
4523 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4524
4525 compl_pending = 0;
4526 (void)ins_compl_next(FALSE, todo, TRUE);
4527 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004528}
4529
4530/*
4531 * Decide the direction of Insert mode complete from the key typed.
4532 * Returns BACKWARD or FORWARD.
4533 */
4534 static int
4535ins_compl_key2dir(c)
4536 int c;
4537{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004538 if (c == Ctrl_P || c == Ctrl_L
4539 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4540 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004541 return BACKWARD;
4542 return FORWARD;
4543}
4544
4545/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004546 * Return TRUE for keys that are used for completion only when the popup menu
4547 * is visible.
4548 */
4549 static int
4550ins_compl_pum_key(c)
4551 int c;
4552{
4553 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004554 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4555 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004556}
4557
4558/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004559 * Decide the number of completions to move forward.
4560 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4561 */
4562 static int
4563ins_compl_key2count(c)
4564 int c;
4565{
4566 int h;
4567
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004568 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004569 {
4570 h = pum_get_height();
4571 if (h > 3)
4572 h -= 2; /* keep some context */
4573 return h;
4574 }
4575 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576}
4577
4578/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004579 * Return TRUE if completion with "c" should insert the match, FALSE if only
4580 * to change the currently selected completion.
4581 */
4582 static int
4583ins_compl_use_match(c)
4584 int c;
4585{
4586 switch (c)
4587 {
4588 case K_UP:
4589 case K_DOWN:
4590 case K_PAGEDOWN:
4591 case K_KPAGEDOWN:
4592 case K_S_DOWN:
4593 case K_PAGEUP:
4594 case K_KPAGEUP:
4595 case K_S_UP:
4596 return FALSE;
4597 }
4598 return TRUE;
4599}
4600
4601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 * Do Insert mode completion.
4603 * Called when character "c" was typed, which has a meaning for completion.
4604 * Returns OK if completion was done, FAIL if something failed (out of mem).
4605 */
4606 static int
4607ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 char_u *line;
4611 int startcol = 0; /* column where searched text starts */
4612 colnr_T curs_col; /* cursor column */
4613 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
Bram Moolenaare3226be2005-12-18 22:10:00 +00004615 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
4618 /* First time we hit ^N or ^P (in a row, I mean) */
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 did_ai = FALSE;
4621#ifdef FEAT_SMARTINDENT
4622 did_si = FALSE;
4623 can_si = FALSE;
4624 can_si_back = FALSE;
4625#endif
4626 if (stop_arrow() == FAIL)
4627 return FAIL;
4628
4629 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004631 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004633 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634 * "compl_startpos" to the cursor as a pattern to add a new word
4635 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004636 * "compl_startpos" is not in the same line as the cursor then fix it
4637 * (the line has been split because it was longer than 'tw'). if SOL
4638 * is set then skip the previous pattern, a word at the beginning of
4639 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004640 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4641 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
4643 /*
4644 * it is a continued search
4645 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004646 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4648 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4649 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 /* line (probably) wrapped, set compl_startpos to the
4653 * first non_blank in the line, if it is not a wordchar
4654 * include it to get a better pattern, but then we don't
4655 * want the "\\<" prefix, check it bellow */
4656 compl_col = (colnr_T)(skipwhite(line) - line);
4657 compl_startpos.col = compl_col;
4658 compl_startpos.lnum = curwin->w_cursor.lnum;
4659 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
4661 else
4662 {
4663 /* S_IPOS was set when we inserted a word that was at the
4664 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 * mode but first we need to redefine compl_startpos */
4666 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 compl_cont_status |= CONT_SOL;
4669 compl_startpos.col = (colnr_T)(skipwhite(
4670 line + compl_length
4671 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004675 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004676 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 * have enough space? just being paranoic */
4678#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004681 compl_cont_status &= ~CONT_SOL;
4682 compl_length = (IOSIZE - MIN_SPACE);
4683 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004685 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4686 if (compl_length < 1)
4687 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
4689 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004695 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004697 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004699 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004701 compl_cont_status = 0;
4702 compl_cont_status |= CONT_N_ADDS;
4703 compl_startpos = curwin->w_cursor;
4704 startcol = (int)curs_col;
4705 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707
4708 /* Work out completion pattern and original text -- webb */
4709 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4710 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004711 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004716 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 compl_col += ++startcol;
4719 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004722 compl_pattern = str_foldcase(line + compl_col,
4723 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 compl_pattern = vim_strnsave(line + compl_col,
4726 compl_length);
4727 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return FAIL;
4729 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004730 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732 char_u *prefix = (char_u *)"\\<";
4733
4734 /* we need 3 extra chars, 1 for the NUL and
4735 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4737 compl_length) + 3);
4738 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004740 if (!vim_iswordp(line + compl_col)
4741 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 && (
4743#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004744 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004746 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747#endif
4748 )))
4749 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004750 STRCPY((char *)compl_pattern, prefix);
4751 (void)quote_meta(compl_pattern + STRLEN(prefix),
4752 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004758 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
4760 )
4761 {
4762 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004763 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4764 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 compl_col += curs_col;
4767 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769 else
4770 {
4771#ifdef FEAT_MBYTE
4772 /* Search the point of change class of multibyte character
4773 * or not a word single byte character backward. */
4774 if (has_mbyte)
4775 {
4776 int base_class;
4777 int head_off;
4778
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004779 startcol -= (*mb_head_off)(line, line + startcol);
4780 base_class = mb_get_class(line + startcol);
4781 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004783 head_off = (*mb_head_off)(line, line + startcol);
4784 if (base_class != mb_get_class(line + startcol
4785 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 }
4790 else
4791#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004794 compl_col += ++startcol;
4795 compl_length = (int)curs_col - startcol;
4796 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 /* Only match word with at least two chars -- webb
4799 * there's no need to call quote_meta,
4800 * alloc(7) is enough -- Acevedo
4801 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004802 compl_pattern = alloc(7);
4803 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004805 STRCPY((char *)compl_pattern, "\\<");
4806 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4807 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 else
4810 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4812 compl_length) + 3);
4813 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 STRCPY((char *)compl_pattern, "\\<");
4816 (void)quote_meta(compl_pattern + 2, line + compl_col,
4817 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 }
4820 }
4821 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4822 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004823 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004824 compl_length = (int)curs_col - (int)compl_col;
4825 if (compl_length < 0) /* cursor in indent: empty pattern */
4826 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004828 compl_pattern = str_foldcase(line + compl_col, compl_length,
4829 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4832 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return FAIL;
4834 }
4835 else if (ctrl_x_mode == CTRL_X_FILES)
4836 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004837 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004839 compl_col += ++startcol;
4840 compl_length = (int)curs_col - startcol;
4841 compl_pattern = addstar(line + compl_col, compl_length,
4842 EXPAND_FILES);
4843 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 return FAIL;
4845 }
4846 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4847 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004848 compl_pattern = vim_strnsave(line, curs_col);
4849 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004851 set_cmd_context(&compl_xp, compl_pattern,
4852 (int)STRLEN(compl_pattern), curs_col);
4853 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4854 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004855 /* No completion possible, use an empty pattern to get a
4856 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004857 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004858 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004859 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4860 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004862 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004863 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004864#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004865 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004866 * Call user defined function 'completefunc' with "a:findstart"
4867 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004868 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004869 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004870 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004871 char_u *funcname;
4872 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004873
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004874 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004875 * string */
4876 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4877 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4878 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004879 {
4880 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4881 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004882 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004883 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004884
4885 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004886 args[1] = NULL;
4887 pos = curwin->w_cursor;
4888 col = call_func_retnr(funcname, 2, args, FALSE);
4889 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004890
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004891 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004892 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004893 compl_col = col;
4894 if ((colnr_T)compl_col > curs_col)
4895 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004896
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004897 /* Setup variables for completion. Need to obtain "line" again,
4898 * it may have become invalid. */
4899 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004900 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004901 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4902 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004903#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 return FAIL;
4905 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004906 else if (ctrl_x_mode == CTRL_X_SPELL)
4907 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004908#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004909 if (spell_bad_len > 0)
4910 compl_col = curs_col - spell_bad_len;
4911 else
4912 compl_col = spell_word_start(startcol);
4913 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004914 {
4915 compl_length = 0;
4916 compl_col = curs_col;
4917 }
4918 else
4919 {
4920 spell_expand_check_cap(compl_col);
4921 compl_length = (int)curs_col - compl_col;
4922 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004923 /* Need to obtain "line" again, it may have become invalid. */
4924 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004925 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4926 if (compl_pattern == NULL)
4927#endif
4928 return FAIL;
4929 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004930 else
4931 {
4932 EMSG2(_(e_intern2), "ins_complete()");
4933 return FAIL;
4934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004936 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 edit_submode_pre = (char_u *)_(" Adding");
4939 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4940 {
4941 /* Insert a new line, keep indentation but ignore 'comments' */
4942#ifdef FEAT_COMMENTS
4943 char_u *old = curbuf->b_p_com;
4944
4945 curbuf->b_p_com = (char_u *)"";
4946#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004947 compl_startpos.lnum = curwin->w_cursor.lnum;
4948 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 ins_eol('\r');
4950#ifdef FEAT_COMMENTS
4951 curbuf->b_p_com = old;
4952#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004953 compl_length = 0;
4954 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 }
4957 else
4958 {
4959 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004960 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004963 if (compl_cont_status & CONT_LOCAL)
4964 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 else
4966 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4967
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004968 /* Always add completion for the original text. */
4969 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004970 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4971 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004972 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004974 vim_free(compl_pattern);
4975 compl_pattern = NULL;
4976 vim_free(compl_orig_text);
4977 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return FAIL;
4979 }
4980
4981 /* showmode might reset the internal line pointers, so it must
4982 * be called before line = ml_get(), or when this address is no
4983 * longer needed. -- Acevedo.
4984 */
4985 edit_submode_extra = (char_u *)_("-- Searching...");
4986 edit_submode_highl = HLF_COUNT;
4987 showmode();
4988 edit_submode_extra = NULL;
4989 out_flush();
4990 }
4991
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004992 compl_shown_match = compl_curr_match;
4993 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004996 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004998 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005000 /* may undisplay the popup menu */
5001 ins_compl_upd_pum();
5002
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005003 if (n > 1) /* all matches have been found */
5004 compl_matches = n;
5005 compl_curr_match = compl_shown_match;
5006 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
Bram Moolenaard68071d2006-05-02 22:08:30 +00005008 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5009 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (got_int && !global_busy)
5011 {
5012 (void)vgetc();
5013 got_int = FALSE;
5014 }
5015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005016 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005017 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005019 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5020 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5022 edit_submode_highl = HLF_E;
5023 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5024 * because we couldn't expand anything at first place, but if we used
5025 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5026 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005027 if ( compl_length > 1
5028 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 || (ctrl_x_mode != 0
5030 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5031 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005032 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034
Bram Moolenaar572cb562005-08-05 21:35:02 +00005035 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005036 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
5040 if (edit_submode_extra == NULL)
5041 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005042 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 edit_submode_extra = (char_u *)_("Back at original");
5045 edit_submode_highl = HLF_W;
5046 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
5049 edit_submode_extra = (char_u *)_("Word from other line");
5050 edit_submode_highl = HLF_COUNT;
5051 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005052 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 edit_submode_extra = (char_u *)_("The only match");
5055 edit_submode_highl = HLF_COUNT;
5056 }
5057 else
5058 {
5059 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005060 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005062 int number = 0;
5063 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005065 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 /* search backwards for the first valid (!= -1) number.
5068 * This should normally succeed already at the first loop
5069 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005070 for (match = compl_curr_match->cp_prev; match != NULL
5071 && match != compl_first_match;
5072 match = match->cp_prev)
5073 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005075 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077 }
5078 if (match != NULL)
5079 /* go up and assign all numbers which are not assigned
5080 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005081 for (match = match->cp_next;
5082 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005083 match = match->cp_next)
5084 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 else /* BACKWARD */
5087 {
5088 /* search forwards (upwards) for the first valid (!= -1)
5089 * number. This should normally succeed already at the
5090 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005091 for (match = compl_curr_match->cp_next; match != NULL
5092 && match != compl_first_match;
5093 match = match->cp_next)
5094 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005096 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098 }
5099 if (match != NULL)
5100 /* go down and assign all numbers which are not
5101 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005102 for (match = match->cp_prev; match
5103 && match->cp_number == -1;
5104 match = match->cp_prev)
5105 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107 }
5108
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005109 /* The match should always have a sequence number now, this is
5110 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005111 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005113 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5114 * Translations may need more than twice that. */
5115 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005118 vim_snprintf((char *)match_ref, sizeof(match_ref),
5119 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005120 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005122 vim_snprintf((char *)match_ref, sizeof(match_ref),
5123 _("match %d"),
5124 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 edit_submode_extra = match_ref;
5126 edit_submode_highl = HLF_R;
5127 if (dollar_vcol)
5128 curs_columns(FALSE);
5129 }
5130 }
5131 }
5132
5133 /* Show a message about what (completion) mode we're in. */
5134 showmode();
5135 if (edit_submode_extra != NULL)
5136 {
5137 if (!p_smd)
5138 msg_attr(edit_submode_extra,
5139 edit_submode_highl < HLF_COUNT
5140 ? hl_attr(edit_submode_highl) : 0);
5141 }
5142 else
5143 msg_clr_cmdline(); /* necessary for "noshowmode" */
5144
Bram Moolenaard68071d2006-05-02 22:08:30 +00005145 /* Show the popup menu, unless we got interrupted. */
5146 if (!compl_interrupted)
5147 {
5148 /* RedrawingDisabled may be set when invoked through complete(). */
5149 n = RedrawingDisabled;
5150 RedrawingDisabled = 0;
5151 ins_compl_show_pum();
5152 setcursor();
5153 RedrawingDisabled = n;
5154 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005155 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005156 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005157
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 return OK;
5159}
5160
5161/*
5162 * Looks in the first "len" chars. of "src" for search-metachars.
5163 * If dest is not NULL the chars. are copied there quoting (with
5164 * a backslash) the metachars, and dest would be NUL terminated.
5165 * Returns the length (needed) of dest
5166 */
5167 static int
5168quote_meta(dest, src, len)
5169 char_u *dest;
5170 char_u *src;
5171 int len;
5172{
5173 int m;
5174
5175 for (m = len; --len >= 0; src++)
5176 {
5177 switch (*src)
5178 {
5179 case '.':
5180 case '*':
5181 case '[':
5182 if (ctrl_x_mode == CTRL_X_DICTIONARY
5183 || ctrl_x_mode == CTRL_X_THESAURUS)
5184 break;
5185 case '~':
5186 if (!p_magic) /* quote these only if magic is set */
5187 break;
5188 case '\\':
5189 if (ctrl_x_mode == CTRL_X_DICTIONARY
5190 || ctrl_x_mode == CTRL_X_THESAURUS)
5191 break;
5192 case '^': /* currently it's not needed. */
5193 case '$':
5194 m++;
5195 if (dest != NULL)
5196 *dest++ = '\\';
5197 break;
5198 }
5199 if (dest != NULL)
5200 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005201# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 /* Copy remaining bytes of a multibyte character. */
5203 if (has_mbyte)
5204 {
5205 int i, mb_len;
5206
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005207 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (mb_len > 0 && len >= mb_len)
5209 for (i = 0; i < mb_len; ++i)
5210 {
5211 --len;
5212 ++src;
5213 if (dest != NULL)
5214 *dest++ = *src;
5215 }
5216 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 if (dest != NULL)
5220 *dest = NUL;
5221
5222 return m;
5223}
5224#endif /* FEAT_INS_EXPAND */
5225
5226/*
5227 * Next character is interpreted literally.
5228 * A one, two or three digit decimal number is interpreted as its byte value.
5229 * If one or two digits are entered, the next character is given to vungetc().
5230 * For Unicode a character > 255 may be returned.
5231 */
5232 int
5233get_literal()
5234{
5235 int cc;
5236 int nc;
5237 int i;
5238 int hex = FALSE;
5239 int octal = FALSE;
5240#ifdef FEAT_MBYTE
5241 int unicode = 0;
5242#endif
5243
5244 if (got_int)
5245 return Ctrl_C;
5246
5247#ifdef FEAT_GUI
5248 /*
5249 * In GUI there is no point inserting the internal code for a special key.
5250 * It is more useful to insert the string "<KEY>" instead. This would
5251 * probably be useful in a text window too, but it would not be
5252 * vi-compatible (maybe there should be an option for it?) -- webb
5253 */
5254 if (gui.in_use)
5255 ++allow_keys;
5256#endif
5257#ifdef USE_ON_FLY_SCROLL
5258 dont_scroll = TRUE; /* disallow scrolling here */
5259#endif
5260 ++no_mapping; /* don't map the next key hits */
5261 cc = 0;
5262 i = 0;
5263 for (;;)
5264 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005265 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#ifdef FEAT_CMDL_INFO
5267 if (!(State & CMDLINE)
5268# ifdef FEAT_MBYTE
5269 && MB_BYTE2LEN_CHECK(nc) == 1
5270# endif
5271 )
5272 add_to_showcmd(nc);
5273#endif
5274 if (nc == 'x' || nc == 'X')
5275 hex = TRUE;
5276 else if (nc == 'o' || nc == 'O')
5277 octal = TRUE;
5278#ifdef FEAT_MBYTE
5279 else if (nc == 'u' || nc == 'U')
5280 unicode = nc;
5281#endif
5282 else
5283 {
5284 if (hex
5285#ifdef FEAT_MBYTE
5286 || unicode != 0
5287#endif
5288 )
5289 {
5290 if (!vim_isxdigit(nc))
5291 break;
5292 cc = cc * 16 + hex2nr(nc);
5293 }
5294 else if (octal)
5295 {
5296 if (nc < '0' || nc > '7')
5297 break;
5298 cc = cc * 8 + nc - '0';
5299 }
5300 else
5301 {
5302 if (!VIM_ISDIGIT(nc))
5303 break;
5304 cc = cc * 10 + nc - '0';
5305 }
5306
5307 ++i;
5308 }
5309
5310 if (cc > 255
5311#ifdef FEAT_MBYTE
5312 && unicode == 0
5313#endif
5314 )
5315 cc = 255; /* limit range to 0-255 */
5316 nc = 0;
5317
5318 if (hex) /* hex: up to two chars */
5319 {
5320 if (i >= 2)
5321 break;
5322 }
5323#ifdef FEAT_MBYTE
5324 else if (unicode) /* Unicode: up to four or eight chars */
5325 {
5326 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5327 break;
5328 }
5329#endif
5330 else if (i >= 3) /* decimal or octal: up to three chars */
5331 break;
5332 }
5333 if (i == 0) /* no number entered */
5334 {
5335 if (nc == K_ZERO) /* NUL is stored as NL */
5336 {
5337 cc = '\n';
5338 nc = 0;
5339 }
5340 else
5341 {
5342 cc = nc;
5343 nc = 0;
5344 }
5345 }
5346
5347 if (cc == 0) /* NUL is stored as NL */
5348 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005349#ifdef FEAT_MBYTE
5350 if (enc_dbcs && (cc & 0xff) == 0)
5351 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5352 second byte will cause trouble! */
5353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 --no_mapping;
5356#ifdef FEAT_GUI
5357 if (gui.in_use)
5358 --allow_keys;
5359#endif
5360 if (nc)
5361 vungetc(nc);
5362 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5363 return cc;
5364}
5365
5366/*
5367 * Insert character, taking care of special keys and mod_mask
5368 */
5369 static void
5370insert_special(c, allow_modmask, ctrlv)
5371 int c;
5372 int allow_modmask;
5373 int ctrlv; /* c was typed after CTRL-V */
5374{
5375 char_u *p;
5376 int len;
5377
5378 /*
5379 * Special function key, translate into "<Key>". Up to the last '>' is
5380 * inserted with ins_str(), so as not to replace characters in replace
5381 * mode.
5382 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5383 * unless 'allow_modmask' is TRUE.
5384 */
5385#ifdef MACOS
5386 /* Command-key never produces a normal key */
5387 if (mod_mask & MOD_MASK_CMD)
5388 allow_modmask = TRUE;
5389#endif
5390 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5391 {
5392 p = get_special_key_name(c, mod_mask);
5393 len = (int)STRLEN(p);
5394 c = p[len - 1];
5395 if (len > 2)
5396 {
5397 if (stop_arrow() == FAIL)
5398 return;
5399 p[len - 1] = NUL;
5400 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005401 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 ctrlv = FALSE;
5403 }
5404 }
5405 if (stop_arrow() == OK)
5406 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5407}
5408
5409/*
5410 * Special characters in this context are those that need processing other
5411 * than the simple insertion that can be performed here. This includes ESC
5412 * which terminates the insert, and CR/NL which need special processing to
5413 * open up a new line. This routine tries to optimize insertions performed by
5414 * the "redo", "undo" or "put" commands, so it needs to know when it should
5415 * stop and defer processing to the "normal" mechanism.
5416 * '0' and '^' are special, because they can be followed by CTRL-D.
5417 */
5418#ifdef EBCDIC
5419# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5420#else
5421# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5422#endif
5423
5424#ifdef FEAT_MBYTE
5425# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5426#else
5427# define WHITECHAR(cc) vim_iswhite(cc)
5428#endif
5429
5430 void
5431insertchar(c, flags, second_indent)
5432 int c; /* character to insert or NUL */
5433 int flags; /* INSCHAR_FORMAT, etc. */
5434 int second_indent; /* indent for second line if >= 0 */
5435{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 int textwidth;
5437#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5443 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445 /*
5446 * Try to break the line in two or more pieces when:
5447 * - Always do this if we have been called to do formatting only.
5448 * - Always do this when 'formatoptions' has the 'a' flag and the line
5449 * ends in white space.
5450 * - Otherwise:
5451 * - Don't do this if inserting a blank
5452 * - Don't do this if an existing character is being replaced, unless
5453 * we're in VREPLACE mode.
5454 * - Do this if the cursor is not on the line where insert started
5455 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5456 * before the insert.
5457 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5458 * before 'textwidth'
5459 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005460 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 && ((flags & INSCHAR_FORMAT)
5462 || (!vim_iswhite(c)
5463 && !((State & REPLACE_FLAG)
5464#ifdef FEAT_VREPLACE
5465 && !(State & VREPLACE_FLAG)
5466#endif
5467 && *ml_get_cursor() != NUL)
5468 && (curwin->w_cursor.lnum != Insstart.lnum
5469 || ((!has_format_option(FO_INS_LONG)
5470 || Insstart_textlen <= (colnr_T)textwidth)
5471 && (!fo_ins_blank
5472 || Insstart_blank_vcol <= (colnr_T)textwidth
5473 ))))))
5474 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005475 /* Format with 'formatexpr' when it's set. Use internal formatting
5476 * when 'formatexpr' isn't set or it returns non-zero. */
5477#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005478 int do_internal = TRUE;
5479
5480 if (*curbuf->b_p_fex != NUL)
5481 {
5482 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5483 /* It may be required to save for undo again, e.g. when setline()
5484 * was called. */
5485 ins_need_undo = TRUE;
5486 }
5487 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005489 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005491
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 if (c == NUL) /* only formatting was wanted */
5493 return;
5494
5495#ifdef FEAT_COMMENTS
5496 /* Check whether this character should end a comment. */
5497 if (did_ai && (int)c == end_comment_pending)
5498 {
5499 char_u *line;
5500 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5501 int middle_len, end_len;
5502 int i;
5503
5504 /*
5505 * Need to remove existing (middle) comment leader and insert end
5506 * comment leader. First, check what comment leader we can find.
5507 */
5508 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5509 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5510 {
5511 /* Skip middle-comment string */
5512 while (*p && p[-1] != ':') /* find end of middle flags */
5513 ++p;
5514 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5515 /* Don't count trailing white space for middle_len */
5516 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5517 --middle_len;
5518
5519 /* Find the end-comment string */
5520 while (*p && p[-1] != ':') /* find end of end flags */
5521 ++p;
5522 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5523
5524 /* Skip white space before the cursor */
5525 i = curwin->w_cursor.col;
5526 while (--i >= 0 && vim_iswhite(line[i]))
5527 ;
5528 i++;
5529
5530 /* Skip to before the middle leader */
5531 i -= middle_len;
5532
5533 /* Check some expected things before we go on */
5534 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5535 {
5536 /* Backspace over all the stuff we want to replace */
5537 backspace_until_column(i);
5538
5539 /*
5540 * Insert the end-comment string, except for the last
5541 * character, which will get inserted as normal later.
5542 */
5543 ins_bytes_len(lead_end, end_len - 1);
5544 }
5545 }
5546 }
5547 end_comment_pending = NUL;
5548#endif
5549
5550 did_ai = FALSE;
5551#ifdef FEAT_SMARTINDENT
5552 did_si = FALSE;
5553 can_si = FALSE;
5554 can_si_back = FALSE;
5555#endif
5556
5557 /*
5558 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5559 * This speeds up normal text input considerably.
5560 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5561 * need to re-indent at a ':', or any other character (but not what
5562 * 'paste' is set)..
5563 */
5564#ifdef USE_ON_FLY_SCROLL
5565 dont_scroll = FALSE; /* allow scrolling here */
5566#endif
5567
5568 if ( !ISSPECIAL(c)
5569#ifdef FEAT_MBYTE
5570 && (!has_mbyte || (*mb_char2len)(c) == 1)
5571#endif
5572 && vpeekc() != NUL
5573 && !(State & REPLACE_FLAG)
5574#ifdef FEAT_CINDENT
5575 && !cindent_on()
5576#endif
5577#ifdef FEAT_RIGHTLEFT
5578 && !p_ri
5579#endif
5580 )
5581 {
5582#define INPUT_BUFLEN 100
5583 char_u buf[INPUT_BUFLEN + 1];
5584 int i;
5585 colnr_T virtcol = 0;
5586
5587 buf[0] = c;
5588 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005589 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 virtcol = get_nolist_virtcol();
5591 /*
5592 * Stop the string when:
5593 * - no more chars available
5594 * - finding a special character (command key)
5595 * - buffer is full
5596 * - running into the 'textwidth' boundary
5597 * - need to check for abbreviation: A non-word char after a word-char
5598 */
5599 while ( (c = vpeekc()) != NUL
5600 && !ISSPECIAL(c)
5601#ifdef FEAT_MBYTE
5602 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5603#endif
5604 && i < INPUT_BUFLEN
5605 && (textwidth == 0
5606 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5607 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5608 {
5609#ifdef FEAT_RIGHTLEFT
5610 c = vgetc();
5611 if (p_hkmap && KeyTyped)
5612 c = hkmap(c); /* Hebrew mode mapping */
5613# ifdef FEAT_FKMAP
5614 if (p_fkmap && KeyTyped)
5615 c = fkmap(c); /* Farsi mode mapping */
5616# endif
5617 buf[i++] = c;
5618#else
5619 buf[i++] = vgetc();
5620#endif
5621 }
5622
5623#ifdef FEAT_DIGRAPHS
5624 do_digraph(-1); /* clear digraphs */
5625 do_digraph(buf[i-1]); /* may be the start of a digraph */
5626#endif
5627 buf[i] = NUL;
5628 ins_str(buf);
5629 if (flags & INSCHAR_CTRLV)
5630 {
5631 redo_literal(*buf);
5632 i = 1;
5633 }
5634 else
5635 i = 0;
5636 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005637 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639 else
5640 {
5641#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005642 int cc;
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5645 {
5646 char_u buf[MB_MAXBYTES + 1];
5647
5648 (*mb_char2bytes)(c, buf);
5649 buf[cc] = NUL;
5650 ins_char_bytes(buf, cc);
5651 AppendCharToRedobuff(c);
5652 }
5653 else
5654#endif
5655 {
5656 ins_char(c);
5657 if (flags & INSCHAR_CTRLV)
5658 redo_literal(c);
5659 else
5660 AppendCharToRedobuff(c);
5661 }
5662 }
5663}
5664
5665/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005666 * Format text at the current insert position.
5667 */
5668 static void
5669internal_format(textwidth, second_indent, flags, format_only)
5670 int textwidth;
5671 int second_indent;
5672 int flags;
5673 int format_only;
5674{
5675 int cc;
5676 int save_char = NUL;
5677 int haveto_redraw = FALSE;
5678 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5679#ifdef FEAT_MBYTE
5680 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5681#endif
5682 int fo_white_par = has_format_option(FO_WHITE_PAR);
5683 int first_line = TRUE;
5684#ifdef FEAT_COMMENTS
5685 colnr_T leader_len;
5686 int no_leader = FALSE;
5687 int do_comments = (flags & INSCHAR_DO_COM);
5688#endif
5689
5690 /*
5691 * When 'ai' is off we don't want a space under the cursor to be
5692 * deleted. Replace it with an 'x' temporarily.
5693 */
5694 if (!curbuf->b_p_ai)
5695 {
5696 cc = gchar_cursor();
5697 if (vim_iswhite(cc))
5698 {
5699 save_char = cc;
5700 pchar_cursor('x');
5701 }
5702 }
5703
5704 /*
5705 * Repeat breaking lines, until the current line is not too long.
5706 */
5707 while (!got_int)
5708 {
5709 int startcol; /* Cursor column at entry */
5710 int wantcol; /* column at textwidth border */
5711 int foundcol; /* column for start of spaces */
5712 int end_foundcol = 0; /* column for start of word */
5713 colnr_T len;
5714 colnr_T virtcol;
5715#ifdef FEAT_VREPLACE
5716 int orig_col = 0;
5717 char_u *saved_text = NULL;
5718#endif
5719 colnr_T col;
5720
5721 virtcol = get_nolist_virtcol();
5722 if (virtcol < (colnr_T)textwidth)
5723 break;
5724
5725#ifdef FEAT_COMMENTS
5726 if (no_leader)
5727 do_comments = FALSE;
5728 else if (!(flags & INSCHAR_FORMAT)
5729 && has_format_option(FO_WRAP_COMS))
5730 do_comments = TRUE;
5731
5732 /* Don't break until after the comment leader */
5733 if (do_comments)
5734 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5735 else
5736 leader_len = 0;
5737
5738 /* If the line doesn't start with a comment leader, then don't
5739 * start one in a following broken line. Avoids that a %word
5740 * moved to the start of the next line causes all following lines
5741 * to start with %. */
5742 if (leader_len == 0)
5743 no_leader = TRUE;
5744#endif
5745 if (!(flags & INSCHAR_FORMAT)
5746#ifdef FEAT_COMMENTS
5747 && leader_len == 0
5748#endif
5749 && !has_format_option(FO_WRAP))
5750
5751 {
5752 textwidth = 0;
5753 break;
5754 }
5755 if ((startcol = curwin->w_cursor.col) == 0)
5756 break;
5757
5758 /* find column of textwidth border */
5759 coladvance((colnr_T)textwidth);
5760 wantcol = curwin->w_cursor.col;
5761
5762 curwin->w_cursor.col = startcol - 1;
5763#ifdef FEAT_MBYTE
5764 /* Correct cursor for multi-byte character. */
5765 if (has_mbyte)
5766 mb_adjust_cursor();
5767#endif
5768 foundcol = 0;
5769
5770 /*
5771 * Find position to break at.
5772 * Stop at first entered white when 'formatoptions' has 'v'
5773 */
5774 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5775 || curwin->w_cursor.lnum != Insstart.lnum
5776 || curwin->w_cursor.col >= Insstart.col)
5777 {
5778 cc = gchar_cursor();
5779 if (WHITECHAR(cc))
5780 {
5781 /* remember position of blank just before text */
5782 end_foundcol = curwin->w_cursor.col;
5783
5784 /* find start of sequence of blanks */
5785 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5786 {
5787 dec_cursor();
5788 cc = gchar_cursor();
5789 }
5790 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5791 break; /* only spaces in front of text */
5792#ifdef FEAT_COMMENTS
5793 /* Don't break until after the comment leader */
5794 if (curwin->w_cursor.col < leader_len)
5795 break;
5796#endif
5797 if (has_format_option(FO_ONE_LETTER))
5798 {
5799 /* do not break after one-letter words */
5800 if (curwin->w_cursor.col == 0)
5801 break; /* one-letter word at begin */
5802
5803 col = curwin->w_cursor.col;
5804 dec_cursor();
5805 cc = gchar_cursor();
5806
5807 if (WHITECHAR(cc))
5808 continue; /* one-letter, continue */
5809 curwin->w_cursor.col = col;
5810 }
5811#ifdef FEAT_MBYTE
5812 if (has_mbyte)
5813 foundcol = curwin->w_cursor.col
5814 + (*mb_ptr2len)(ml_get_cursor());
5815 else
5816#endif
5817 foundcol = curwin->w_cursor.col + 1;
5818 if (curwin->w_cursor.col < (colnr_T)wantcol)
5819 break;
5820 }
5821#ifdef FEAT_MBYTE
5822 else if (cc >= 0x100 && fo_multibyte
5823 && curwin->w_cursor.col <= (colnr_T)wantcol)
5824 {
5825 /* Break after or before a multi-byte character. */
5826 foundcol = curwin->w_cursor.col;
5827 if (curwin->w_cursor.col < (colnr_T)wantcol)
5828 foundcol += (*mb_char2len)(cc);
5829 end_foundcol = foundcol;
5830 break;
5831 }
5832#endif
5833 if (curwin->w_cursor.col == 0)
5834 break;
5835 dec_cursor();
5836 }
5837
5838 if (foundcol == 0) /* no spaces, cannot break line */
5839 {
5840 curwin->w_cursor.col = startcol;
5841 break;
5842 }
5843
5844 /* Going to break the line, remove any "$" now. */
5845 undisplay_dollar();
5846
5847 /*
5848 * Offset between cursor position and line break is used by replace
5849 * stack functions. VREPLACE does not use this, and backspaces
5850 * over the text instead.
5851 */
5852#ifdef FEAT_VREPLACE
5853 if (State & VREPLACE_FLAG)
5854 orig_col = startcol; /* Will start backspacing from here */
5855 else
5856#endif
5857 replace_offset = startcol - end_foundcol - 1;
5858
5859 /*
5860 * adjust startcol for spaces that will be deleted and
5861 * characters that will remain on top line
5862 */
5863 curwin->w_cursor.col = foundcol;
5864 while (cc = gchar_cursor(), WHITECHAR(cc))
5865 inc_cursor();
5866 startcol -= curwin->w_cursor.col;
5867 if (startcol < 0)
5868 startcol = 0;
5869
5870#ifdef FEAT_VREPLACE
5871 if (State & VREPLACE_FLAG)
5872 {
5873 /*
5874 * In VREPLACE mode, we will backspace over the text to be
5875 * wrapped, so save a copy now to put on the next line.
5876 */
5877 saved_text = vim_strsave(ml_get_cursor());
5878 curwin->w_cursor.col = orig_col;
5879 if (saved_text == NULL)
5880 break; /* Can't do it, out of memory */
5881 saved_text[startcol] = NUL;
5882
5883 /* Backspace over characters that will move to the next line */
5884 if (!fo_white_par)
5885 backspace_until_column(foundcol);
5886 }
5887 else
5888#endif
5889 {
5890 /* put cursor after pos. to break line */
5891 if (!fo_white_par)
5892 curwin->w_cursor.col = foundcol;
5893 }
5894
5895 /*
5896 * Split the line just before the margin.
5897 * Only insert/delete lines, but don't really redraw the window.
5898 */
5899 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5900 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5901#ifdef FEAT_COMMENTS
5902 + (do_comments ? OPENLINE_DO_COM : 0)
5903#endif
5904 , old_indent);
5905 old_indent = 0;
5906
5907 replace_offset = 0;
5908 if (first_line)
5909 {
5910 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5911 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5912 if (second_indent >= 0)
5913 {
5914#ifdef FEAT_VREPLACE
5915 if (State & VREPLACE_FLAG)
5916 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5917 else
5918#endif
5919 (void)set_indent(second_indent, SIN_CHANGED);
5920 }
5921 first_line = FALSE;
5922 }
5923
5924#ifdef FEAT_VREPLACE
5925 if (State & VREPLACE_FLAG)
5926 {
5927 /*
5928 * In VREPLACE mode we have backspaced over the text to be
5929 * moved, now we re-insert it into the new line.
5930 */
5931 ins_bytes(saved_text);
5932 vim_free(saved_text);
5933 }
5934 else
5935#endif
5936 {
5937 /*
5938 * Check if cursor is not past the NUL off the line, cindent
5939 * may have added or removed indent.
5940 */
5941 curwin->w_cursor.col += startcol;
5942 len = (colnr_T)STRLEN(ml_get_curline());
5943 if (curwin->w_cursor.col > len)
5944 curwin->w_cursor.col = len;
5945 }
5946
5947 haveto_redraw = TRUE;
5948#ifdef FEAT_CINDENT
5949 can_cindent = TRUE;
5950#endif
5951 /* moved the cursor, don't autoindent or cindent now */
5952 did_ai = FALSE;
5953#ifdef FEAT_SMARTINDENT
5954 did_si = FALSE;
5955 can_si = FALSE;
5956 can_si_back = FALSE;
5957#endif
5958 line_breakcheck();
5959 }
5960
5961 if (save_char != NUL) /* put back space after cursor */
5962 pchar_cursor(save_char);
5963
5964 if (!format_only && haveto_redraw)
5965 {
5966 update_topline();
5967 redraw_curbuf_later(VALID);
5968 }
5969}
5970
5971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 * Called after inserting or deleting text: When 'formatoptions' includes the
5973 * 'a' flag format from the current line until the end of the paragraph.
5974 * Keep the cursor at the same position relative to the text.
5975 * The caller must have saved the cursor line for undo, following ones will be
5976 * saved here.
5977 */
5978 void
5979auto_format(trailblank, prev_line)
5980 int trailblank; /* when TRUE also format with trailing blank */
5981 int prev_line; /* may start in previous line */
5982{
5983 pos_T pos;
5984 colnr_T len;
5985 char_u *old;
5986 char_u *new, *pnew;
5987 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005988 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
5990 if (!has_format_option(FO_AUTO))
5991 return;
5992
5993 pos = curwin->w_cursor;
5994 old = ml_get_curline();
5995
5996 /* may remove added space */
5997 check_auto_format(FALSE);
5998
5999 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6000 * user might insert normal text next. Also skip formatting when "1" is
6001 * in 'formatoptions' and there is a single character before the cursor.
6002 * Otherwise the line would be broken and when typing another non-white
6003 * next they are not joined back together. */
6004 wasatend = (pos.col == STRLEN(old));
6005 if (*old != NUL && !trailblank && wasatend)
6006 {
6007 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006008 cc = gchar_cursor();
6009 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6010 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006012 cc = gchar_cursor();
6013 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {
6015 curwin->w_cursor = pos;
6016 return;
6017 }
6018 curwin->w_cursor = pos;
6019 }
6020
6021#ifdef FEAT_COMMENTS
6022 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6023 * comments. */
6024 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6025 && get_leader_len(old, NULL, FALSE) == 0)
6026 return;
6027#endif
6028
6029 /*
6030 * May start formatting in a previous line, so that after "x" a word is
6031 * moved to the previous line if it fits there now. Only when this is not
6032 * the start of a paragraph.
6033 */
6034 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6035 {
6036 --curwin->w_cursor.lnum;
6037 if (u_save_cursor() == FAIL)
6038 return;
6039 }
6040
6041 /*
6042 * Do the formatting and restore the cursor position. "saved_cursor" will
6043 * be adjusted for the text formatting.
6044 */
6045 saved_cursor = pos;
6046 format_lines((linenr_T)-1);
6047 curwin->w_cursor = saved_cursor;
6048 saved_cursor.lnum = 0;
6049
6050 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6051 {
6052 /* "cannot happen" */
6053 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6054 coladvance((colnr_T)MAXCOL);
6055 }
6056 else
6057 check_cursor_col();
6058
6059 /* Insert mode: If the cursor is now after the end of the line while it
6060 * previously wasn't, the line was broken. Because of the rule above we
6061 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6062 * formatted. */
6063 if (!wasatend && has_format_option(FO_WHITE_PAR))
6064 {
6065 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006066 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (curwin->w_cursor.col == len)
6068 {
6069 pnew = vim_strnsave(new, len + 2);
6070 pnew[len] = ' ';
6071 pnew[len + 1] = NUL;
6072 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6073 /* remove the space later */
6074 did_add_space = TRUE;
6075 }
6076 else
6077 /* may remove added space */
6078 check_auto_format(FALSE);
6079 }
6080
6081 check_cursor();
6082}
6083
6084/*
6085 * When an extra space was added to continue a paragraph for auto-formatting,
6086 * delete it now. The space must be under the cursor, just after the insert
6087 * position.
6088 */
6089 static void
6090check_auto_format(end_insert)
6091 int end_insert; /* TRUE when ending Insert mode */
6092{
6093 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006094 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
6096 if (did_add_space)
6097 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006098 cc = gchar_cursor();
6099 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 /* Somehow the space was removed already. */
6101 did_add_space = FALSE;
6102 else
6103 {
6104 if (!end_insert)
6105 {
6106 inc_cursor();
6107 c = gchar_cursor();
6108 dec_cursor();
6109 }
6110 if (c != NUL)
6111 {
6112 /* The space is no longer at the end of the line, delete it. */
6113 del_char(FALSE);
6114 did_add_space = FALSE;
6115 }
6116 }
6117 }
6118}
6119
6120/*
6121 * Find out textwidth to be used for formatting:
6122 * if 'textwidth' option is set, use it
6123 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6124 * if invalid value, use 0.
6125 * Set default to window width (maximum 79) for "gq" operator.
6126 */
6127 int
6128comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006129 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 int textwidth;
6132
6133 textwidth = curbuf->b_p_tw;
6134 if (textwidth == 0 && curbuf->b_p_wm)
6135 {
6136 /* The width is the window width minus 'wrapmargin' minus all the
6137 * things that add to the margin. */
6138 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6139#ifdef FEAT_CMDWIN
6140 if (cmdwin_type != 0)
6141 textwidth -= 1;
6142#endif
6143#ifdef FEAT_FOLDING
6144 textwidth -= curwin->w_p_fdc;
6145#endif
6146#ifdef FEAT_SIGNS
6147 if (curwin->w_buffer->b_signlist != NULL
6148# ifdef FEAT_NETBEANS_INTG
6149 || usingNetbeans
6150# endif
6151 )
6152 textwidth -= 1;
6153#endif
6154 if (curwin->w_p_nu)
6155 textwidth -= 8;
6156 }
6157 if (textwidth < 0)
6158 textwidth = 0;
6159 if (ff && textwidth == 0)
6160 {
6161 textwidth = W_WIDTH(curwin) - 1;
6162 if (textwidth > 79)
6163 textwidth = 79;
6164 }
6165 return textwidth;
6166}
6167
6168/*
6169 * Put a character in the redo buffer, for when just after a CTRL-V.
6170 */
6171 static void
6172redo_literal(c)
6173 int c;
6174{
6175 char_u buf[10];
6176
6177 /* Only digits need special treatment. Translate them into a string of
6178 * three digits. */
6179 if (VIM_ISDIGIT(c))
6180 {
6181 sprintf((char *)buf, "%03d", c);
6182 AppendToRedobuff(buf);
6183 }
6184 else
6185 AppendCharToRedobuff(c);
6186}
6187
6188/*
6189 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006190 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 */
6192 static void
6193start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006194 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 if (!arrow_used) /* something has been inserted */
6197 {
6198 AppendToRedobuff(ESC_STR);
6199 stop_insert(end_insert_pos, FALSE);
6200 arrow_used = TRUE; /* this means we stopped the current insert */
6201 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006202#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006203 check_spell_redraw();
6204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205}
6206
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006207#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006208/*
6209 * If we skipped highlighting word at cursor, do it now.
6210 * It may be skipped again, thus reset spell_redraw_lnum first.
6211 */
6212 static void
6213check_spell_redraw()
6214{
6215 if (spell_redraw_lnum != 0)
6216 {
6217 linenr_T lnum = spell_redraw_lnum;
6218
6219 spell_redraw_lnum = 0;
6220 redrawWinline(lnum, FALSE);
6221 }
6222}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006223
6224/*
6225 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6226 * spelled word, if there is one.
6227 */
6228 static void
6229spell_back_to_badword()
6230{
6231 pos_T tpos = curwin->w_cursor;
6232
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006233 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006234 if (curwin->w_cursor.col != tpos.col)
6235 start_arrow(&tpos);
6236}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006237#endif
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239/*
6240 * stop_arrow() is called before a change is made in insert mode.
6241 * If an arrow key has been used, start a new insertion.
6242 * Returns FAIL if undo is impossible, shouldn't insert then.
6243 */
6244 int
6245stop_arrow()
6246{
6247 if (arrow_used)
6248 {
6249 if (u_save_cursor() == OK)
6250 {
6251 arrow_used = FALSE;
6252 ins_need_undo = FALSE;
6253 }
6254 Insstart = curwin->w_cursor; /* new insertion starts here */
6255 Insstart_textlen = linetabsize(ml_get_curline());
6256 ai_col = 0;
6257#ifdef FEAT_VREPLACE
6258 if (State & VREPLACE_FLAG)
6259 {
6260 orig_line_count = curbuf->b_ml.ml_line_count;
6261 vr_lines_changed = 1;
6262 }
6263#endif
6264 ResetRedobuff();
6265 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006266 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
6268 else if (ins_need_undo)
6269 {
6270 if (u_save_cursor() == OK)
6271 ins_need_undo = FALSE;
6272 }
6273
6274#ifdef FEAT_FOLDING
6275 /* Always open fold at the cursor line when inserting something. */
6276 foldOpenCursor();
6277#endif
6278
6279 return (arrow_used || ins_need_undo ? FAIL : OK);
6280}
6281
6282/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006283 * Do a few things to stop inserting.
6284 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6285 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 */
6287 static void
6288stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006289 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006290 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006292 int cc;
6293 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295 stop_redo_ins();
6296 replace_flush(); /* abandon replace stack */
6297
6298 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006299 * Save the inserted text for later redo with ^@ and CTRL-A.
6300 * Don't do it when "restart_edit" was set and nothing was inserted,
6301 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006303 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006304 if (did_restart_edit == 0 || (ptr != NULL
6305 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006306 {
6307 vim_free(last_insert);
6308 last_insert = ptr;
6309 last_insert_skip = new_insert_skip;
6310 }
6311 else
6312 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006314 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
6316 /* Auto-format now. It may seem strange to do this when stopping an
6317 * insertion (or moving the cursor), but it's required when appending
6318 * a line and having it end in a space. But only do it when something
6319 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006320 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006322 pos_T tpos = curwin->w_cursor;
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 /* When the cursor is at the end of the line after a space the
6325 * formatting will move it to the following word. Avoid that by
6326 * moving the cursor onto the space. */
6327 cc = 'x';
6328 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6329 {
6330 dec_cursor();
6331 cc = gchar_cursor();
6332 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006333 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 }
6335
6336 auto_format(TRUE, FALSE);
6337
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006338 if (vim_iswhite(cc))
6339 {
6340 if (gchar_cursor() != NUL)
6341 inc_cursor();
6342#ifdef FEAT_VIRTUALEDIT
6343 /* If the cursor is still at the same character, also keep
6344 * the "coladd". */
6345 if (gchar_cursor() == NUL
6346 && curwin->w_cursor.lnum == tpos.lnum
6347 && curwin->w_cursor.col == tpos.col)
6348 curwin->w_cursor.coladd = tpos.coladd;
6349#endif
6350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 }
6352
6353 /* If a space was inserted for auto-formatting, remove it now. */
6354 check_auto_format(TRUE);
6355
6356 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006357 * of the line, and put the cursor back.
6358 * Do this when ESC was used or moving the cursor up/down. */
6359 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006360 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006362 pos_T tpos = curwin->w_cursor;
6363
6364 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006365 for (;;)
6366 {
6367 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6368 --curwin->w_cursor.col;
6369 cc = gchar_cursor();
6370 if (!vim_iswhite(cc))
6371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006373 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006374 if (curwin->w_cursor.lnum != tpos.lnum)
6375 curwin->w_cursor = tpos;
6376 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6378
6379#ifdef FEAT_VISUAL
6380 /* <C-S-Right> may have started Visual mode, adjust the position for
6381 * deleted characters. */
6382 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6383 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006384 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (VIsual.col > (colnr_T)cc)
6386 {
6387 VIsual.col = cc;
6388# ifdef FEAT_VIRTUALEDIT
6389 VIsual.coladd = 0;
6390# endif
6391 }
6392 }
6393#endif
6394 }
6395 }
6396 did_ai = FALSE;
6397#ifdef FEAT_SMARTINDENT
6398 did_si = FALSE;
6399 can_si = FALSE;
6400 can_si_back = FALSE;
6401#endif
6402
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006403 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6404 * now in a different buffer. */
6405 if (end_insert_pos != NULL)
6406 {
6407 curbuf->b_op_start = Insstart;
6408 curbuf->b_op_end = *end_insert_pos;
6409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
6412/*
6413 * Set the last inserted text to a single character.
6414 * Used for the replace command.
6415 */
6416 void
6417set_last_insert(c)
6418 int c;
6419{
6420 char_u *s;
6421
6422 vim_free(last_insert);
6423#ifdef FEAT_MBYTE
6424 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6425#else
6426 last_insert = alloc(6);
6427#endif
6428 if (last_insert != NULL)
6429 {
6430 s = last_insert;
6431 /* Use the CTRL-V only when entering a special char */
6432 if (c < ' ' || c == DEL)
6433 *s++ = Ctrl_V;
6434 s = add_char2buf(c, s);
6435 *s++ = ESC;
6436 *s++ = NUL;
6437 last_insert_skip = 0;
6438 }
6439}
6440
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006441#if defined(EXITFREE) || defined(PROTO)
6442 void
6443free_last_insert()
6444{
6445 vim_free(last_insert);
6446 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006447# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006448 vim_free(compl_orig_text);
6449 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006450# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006451}
6452#endif
6453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454/*
6455 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6456 * and CSI. Handle multi-byte characters.
6457 * Returns a pointer to after the added bytes.
6458 */
6459 char_u *
6460add_char2buf(c, s)
6461 int c;
6462 char_u *s;
6463{
6464#ifdef FEAT_MBYTE
6465 char_u temp[MB_MAXBYTES];
6466 int i;
6467 int len;
6468
6469 len = (*mb_char2bytes)(c, temp);
6470 for (i = 0; i < len; ++i)
6471 {
6472 c = temp[i];
6473#endif
6474 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6475 if (c == K_SPECIAL)
6476 {
6477 *s++ = K_SPECIAL;
6478 *s++ = KS_SPECIAL;
6479 *s++ = KE_FILLER;
6480 }
6481#ifdef FEAT_GUI
6482 else if (c == CSI)
6483 {
6484 *s++ = CSI;
6485 *s++ = KS_EXTRA;
6486 *s++ = (int)KE_CSI;
6487 }
6488#endif
6489 else
6490 *s++ = c;
6491#ifdef FEAT_MBYTE
6492 }
6493#endif
6494 return s;
6495}
6496
6497/*
6498 * move cursor to start of line
6499 * if flags & BL_WHITE move to first non-white
6500 * if flags & BL_SOL move to first non-white if startofline is set,
6501 * otherwise keep "curswant" column
6502 * if flags & BL_FIX don't leave the cursor on a NUL.
6503 */
6504 void
6505beginline(flags)
6506 int flags;
6507{
6508 if ((flags & BL_SOL) && !p_sol)
6509 coladvance(curwin->w_curswant);
6510 else
6511 {
6512 curwin->w_cursor.col = 0;
6513#ifdef FEAT_VIRTUALEDIT
6514 curwin->w_cursor.coladd = 0;
6515#endif
6516
6517 if (flags & (BL_WHITE | BL_SOL))
6518 {
6519 char_u *ptr;
6520
6521 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6522 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6523 ++curwin->w_cursor.col;
6524 }
6525 curwin->w_set_curswant = TRUE;
6526 }
6527}
6528
6529/*
6530 * oneright oneleft cursor_down cursor_up
6531 *
6532 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006533 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 * Return OK when successful, FAIL when we hit a line of file boundary.
6535 */
6536
6537 int
6538oneright()
6539{
6540 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
6543#ifdef FEAT_VIRTUALEDIT
6544 if (virtual_active())
6545 {
6546 pos_T prevpos = curwin->w_cursor;
6547
6548 /* Adjust for multi-wide char (excluding TAB) */
6549 ptr = ml_get_cursor();
6550 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006551# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006553# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 ))
6557 ? ptr2cells(ptr) : 1));
6558 curwin->w_set_curswant = TRUE;
6559 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6560 return (prevpos.col != curwin->w_cursor.col
6561 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6562 }
6563#endif
6564
6565 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006566 if (*ptr == NUL)
6567 return FAIL; /* already at the very end */
6568
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006570 if (has_mbyte)
6571 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 else
6573#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006574 l = 1;
6575
6576 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6577 * contains "onemore". */
6578 if (ptr[l] == NUL
6579#ifdef FEAT_VIRTUALEDIT
6580 && (ve_flags & VE_ONEMORE) == 0
6581#endif
6582 )
6583 return FAIL;
6584 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586 curwin->w_set_curswant = TRUE;
6587 return OK;
6588}
6589
6590 int
6591oneleft()
6592{
6593#ifdef FEAT_VIRTUALEDIT
6594 if (virtual_active())
6595 {
6596 int width;
6597 int v = getviscol();
6598
6599 if (v == 0)
6600 return FAIL;
6601
6602# ifdef FEAT_LINEBREAK
6603 /* We might get stuck on 'showbreak', skip over it. */
6604 width = 1;
6605 for (;;)
6606 {
6607 coladvance(v - width);
6608 /* getviscol() is slow, skip it when 'showbreak' is empty and
6609 * there are no multi-byte characters */
6610 if ((*p_sbr == NUL
6611# ifdef FEAT_MBYTE
6612 && !has_mbyte
6613# endif
6614 ) || getviscol() < v)
6615 break;
6616 ++width;
6617 }
6618# else
6619 coladvance(v - 1);
6620# endif
6621
6622 if (curwin->w_cursor.coladd == 1)
6623 {
6624 char_u *ptr;
6625
6626 /* Adjust for multi-wide char (not a TAB) */
6627 ptr = ml_get_cursor();
6628 if (*ptr != TAB && vim_isprintc(
6629# ifdef FEAT_MBYTE
6630 (*mb_ptr2char)(ptr)
6631# else
6632 *ptr
6633# endif
6634 ) && ptr2cells(ptr) > 1)
6635 curwin->w_cursor.coladd = 0;
6636 }
6637
6638 curwin->w_set_curswant = TRUE;
6639 return OK;
6640 }
6641#endif
6642
6643 if (curwin->w_cursor.col == 0)
6644 return FAIL;
6645
6646 curwin->w_set_curswant = TRUE;
6647 --curwin->w_cursor.col;
6648
6649#ifdef FEAT_MBYTE
6650 /* if the character on the left of the current cursor is a multi-byte
6651 * character, move to its first byte */
6652 if (has_mbyte)
6653 mb_adjust_cursor();
6654#endif
6655 return OK;
6656}
6657
6658 int
6659cursor_up(n, upd_topline)
6660 long n;
6661 int upd_topline; /* When TRUE: update topline */
6662{
6663 linenr_T lnum;
6664
6665 if (n > 0)
6666 {
6667 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006668 /* This fails if the cursor is already in the first line or the count
6669 * is larger than the line number and '-' is in 'cpoptions' */
6670 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 return FAIL;
6672 if (n >= lnum)
6673 lnum = 1;
6674 else
6675#ifdef FEAT_FOLDING
6676 if (hasAnyFolding(curwin))
6677 {
6678 /*
6679 * Count each sequence of folded lines as one logical line.
6680 */
6681 /* go to the the start of the current fold */
6682 (void)hasFolding(lnum, &lnum, NULL);
6683
6684 while (n--)
6685 {
6686 /* move up one line */
6687 --lnum;
6688 if (lnum <= 1)
6689 break;
6690 /* If we entered a fold, move to the beginning, unless in
6691 * Insert mode or when 'foldopen' contains "all": it will open
6692 * in a moment. */
6693 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6694 (void)hasFolding(lnum, &lnum, NULL);
6695 }
6696 if (lnum < 1)
6697 lnum = 1;
6698 }
6699 else
6700#endif
6701 lnum -= n;
6702 curwin->w_cursor.lnum = lnum;
6703 }
6704
6705 /* try to advance to the column we want to be at */
6706 coladvance(curwin->w_curswant);
6707
6708 if (upd_topline)
6709 update_topline(); /* make sure curwin->w_topline is valid */
6710
6711 return OK;
6712}
6713
6714/*
6715 * Cursor down a number of logical lines.
6716 */
6717 int
6718cursor_down(n, upd_topline)
6719 long n;
6720 int upd_topline; /* When TRUE: update topline */
6721{
6722 linenr_T lnum;
6723
6724 if (n > 0)
6725 {
6726 lnum = curwin->w_cursor.lnum;
6727#ifdef FEAT_FOLDING
6728 /* Move to last line of fold, will fail if it's the end-of-file. */
6729 (void)hasFolding(lnum, NULL, &lnum);
6730#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006731 /* This fails if the cursor is already in the last line or would move
6732 * beyound the last line and '-' is in 'cpoptions' */
6733 if (lnum >= curbuf->b_ml.ml_line_count
6734 || (lnum + n > curbuf->b_ml.ml_line_count
6735 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 return FAIL;
6737 if (lnum + n >= curbuf->b_ml.ml_line_count)
6738 lnum = curbuf->b_ml.ml_line_count;
6739 else
6740#ifdef FEAT_FOLDING
6741 if (hasAnyFolding(curwin))
6742 {
6743 linenr_T last;
6744
6745 /* count each sequence of folded lines as one logical line */
6746 while (n--)
6747 {
6748 if (hasFolding(lnum, NULL, &last))
6749 lnum = last + 1;
6750 else
6751 ++lnum;
6752 if (lnum >= curbuf->b_ml.ml_line_count)
6753 break;
6754 }
6755 if (lnum > curbuf->b_ml.ml_line_count)
6756 lnum = curbuf->b_ml.ml_line_count;
6757 }
6758 else
6759#endif
6760 lnum += n;
6761 curwin->w_cursor.lnum = lnum;
6762 }
6763
6764 /* try to advance to the column we want to be at */
6765 coladvance(curwin->w_curswant);
6766
6767 if (upd_topline)
6768 update_topline(); /* make sure curwin->w_topline is valid */
6769
6770 return OK;
6771}
6772
6773/*
6774 * Stuff the last inserted text in the read buffer.
6775 * Last_insert actually is a copy of the redo buffer, so we
6776 * first have to remove the command.
6777 */
6778 int
6779stuff_inserted(c, count, no_esc)
6780 int c; /* Command character to be inserted */
6781 long count; /* Repeat this many times */
6782 int no_esc; /* Don't add an ESC at the end */
6783{
6784 char_u *esc_ptr;
6785 char_u *ptr;
6786 char_u *last_ptr;
6787 char_u last = NUL;
6788
6789 ptr = get_last_insert();
6790 if (ptr == NULL)
6791 {
6792 EMSG(_(e_noinstext));
6793 return FAIL;
6794 }
6795
6796 /* may want to stuff the command character, to start Insert mode */
6797 if (c != NUL)
6798 stuffcharReadbuff(c);
6799 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6800 *esc_ptr = NUL; /* remove the ESC */
6801
6802 /* when the last char is either "0" or "^" it will be quoted if no ESC
6803 * comes after it OR if it will inserted more than once and "ptr"
6804 * starts with ^D. -- Acevedo
6805 */
6806 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6807 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6808 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6809 {
6810 last = *last_ptr;
6811 *last_ptr = NUL;
6812 }
6813
6814 do
6815 {
6816 stuffReadbuff(ptr);
6817 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6818 if (last)
6819 stuffReadbuff((char_u *)(last == '0'
6820 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6821 : IF_EB("\026^", CTRL_V_STR "^")));
6822 }
6823 while (--count > 0);
6824
6825 if (last)
6826 *last_ptr = last;
6827
6828 if (esc_ptr != NULL)
6829 *esc_ptr = ESC; /* put the ESC back */
6830
6831 /* may want to stuff a trailing ESC, to get out of Insert mode */
6832 if (!no_esc)
6833 stuffcharReadbuff(ESC);
6834
6835 return OK;
6836}
6837
6838 char_u *
6839get_last_insert()
6840{
6841 if (last_insert == NULL)
6842 return NULL;
6843 return last_insert + last_insert_skip;
6844}
6845
6846/*
6847 * Get last inserted string, and remove trailing <Esc>.
6848 * Returns pointer to allocated memory (must be freed) or NULL.
6849 */
6850 char_u *
6851get_last_insert_save()
6852{
6853 char_u *s;
6854 int len;
6855
6856 if (last_insert == NULL)
6857 return NULL;
6858 s = vim_strsave(last_insert + last_insert_skip);
6859 if (s != NULL)
6860 {
6861 len = (int)STRLEN(s);
6862 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6863 s[len - 1] = NUL;
6864 }
6865 return s;
6866}
6867
6868/*
6869 * Check the word in front of the cursor for an abbreviation.
6870 * Called when the non-id character "c" has been entered.
6871 * When an abbreviation is recognized it is removed from the text and
6872 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6873 */
6874 static int
6875echeck_abbr(c)
6876 int c;
6877{
6878 /* Don't check for abbreviation in paste mode, when disabled and just
6879 * after moving around with cursor keys. */
6880 if (p_paste || no_abbr || arrow_used)
6881 return FALSE;
6882
6883 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6884 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6885}
6886
6887/*
6888 * replace-stack functions
6889 *
6890 * When replacing characters, the replaced characters are remembered for each
6891 * new character. This is used to re-insert the old text when backspacing.
6892 *
6893 * There is a NUL headed list of characters for each character that is
6894 * currently in the file after the insertion point. When BS is used, one NUL
6895 * headed list is put back for the deleted character.
6896 *
6897 * For a newline, there are two NUL headed lists. One contains the characters
6898 * that the NL replaced. The extra one stores the characters after the cursor
6899 * that were deleted (always white space).
6900 *
6901 * Replace_offset is normally 0, in which case replace_push will add a new
6902 * character at the end of the stack. If replace_offset is not 0, that many
6903 * characters will be left on the stack above the newly inserted character.
6904 */
6905
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006906static char_u *replace_stack = NULL;
6907static long replace_stack_nr = 0; /* next entry in replace stack */
6908static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
6910 void
6911replace_push(c)
6912 int c; /* character that is replaced (NUL is none) */
6913{
6914 char_u *p;
6915
6916 if (replace_stack_nr < replace_offset) /* nothing to do */
6917 return;
6918 if (replace_stack_len <= replace_stack_nr)
6919 {
6920 replace_stack_len += 50;
6921 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6922 if (p == NULL) /* out of memory */
6923 {
6924 replace_stack_len -= 50;
6925 return;
6926 }
6927 if (replace_stack != NULL)
6928 {
6929 mch_memmove(p, replace_stack,
6930 (size_t)(replace_stack_nr * sizeof(char_u)));
6931 vim_free(replace_stack);
6932 }
6933 replace_stack = p;
6934 }
6935 p = replace_stack + replace_stack_nr - replace_offset;
6936 if (replace_offset)
6937 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6938 *p = c;
6939 ++replace_stack_nr;
6940}
6941
Bram Moolenaar2c994e82008-01-02 16:49:36 +00006942#if defined(FEAT_MBYTE) || defined(PROTO)
6943/*
6944 * Push a character onto the replace stack. Handles a multi-byte character in
6945 * reverse byte order, so that the first byte is popped off first.
6946 * Return the number of bytes done (includes composing characters).
6947 */
6948 int
6949replace_push_mb(p)
6950 char_u *p;
6951{
6952 int l = (*mb_ptr2len)(p);
6953 int j;
6954
6955 for (j = l - 1; j >= 0; --j)
6956 replace_push(p[j]);
6957 return l;
6958}
6959#endif
6960
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006961#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962/*
6963 * call replace_push(c) with replace_offset set to the first NUL.
6964 */
6965 static void
6966replace_push_off(c)
6967 int c;
6968{
6969 char_u *p;
6970
6971 p = replace_stack + replace_stack_nr;
6972 for (replace_offset = 1; replace_offset < replace_stack_nr;
6973 ++replace_offset)
6974 if (*--p == NUL)
6975 break;
6976 replace_push(c);
6977 replace_offset = 0;
6978}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981/*
6982 * Pop one item from the replace stack.
6983 * return -1 if stack empty
6984 * return replaced character or NUL otherwise
6985 */
6986 static int
6987replace_pop()
6988{
6989 if (replace_stack_nr == 0)
6990 return -1;
6991 return (int)replace_stack[--replace_stack_nr];
6992}
6993
6994/*
6995 * Join the top two items on the replace stack. This removes to "off"'th NUL
6996 * encountered.
6997 */
6998 static void
6999replace_join(off)
7000 int off; /* offset for which NUL to remove */
7001{
7002 int i;
7003
7004 for (i = replace_stack_nr; --i >= 0; )
7005 if (replace_stack[i] == NUL && off-- <= 0)
7006 {
7007 --replace_stack_nr;
7008 mch_memmove(replace_stack + i, replace_stack + i + 1,
7009 (size_t)(replace_stack_nr - i));
7010 return;
7011 }
7012}
7013
7014/*
7015 * Pop bytes from the replace stack until a NUL is found, and insert them
7016 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7017 */
7018 static void
7019replace_pop_ins()
7020{
7021 int cc;
7022 int oldState = State;
7023
7024 State = NORMAL; /* don't want REPLACE here */
7025 while ((cc = replace_pop()) > 0)
7026 {
7027#ifdef FEAT_MBYTE
7028 mb_replace_pop_ins(cc);
7029#else
7030 ins_char(cc);
7031#endif
7032 dec_cursor();
7033 }
7034 State = oldState;
7035}
7036
7037#ifdef FEAT_MBYTE
7038/*
7039 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7040 * indicates a multi-byte char, pop the other bytes too.
7041 */
7042 static void
7043mb_replace_pop_ins(cc)
7044 int cc;
7045{
7046 int n;
7047 char_u buf[MB_MAXBYTES];
7048 int i;
7049 int c;
7050
7051 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7052 {
7053 buf[0] = cc;
7054 for (i = 1; i < n; ++i)
7055 buf[i] = replace_pop();
7056 ins_bytes_len(buf, n);
7057 }
7058 else
7059 ins_char(cc);
7060
7061 if (enc_utf8)
7062 /* Handle composing chars. */
7063 for (;;)
7064 {
7065 c = replace_pop();
7066 if (c == -1) /* stack empty */
7067 break;
7068 if ((n = MB_BYTE2LEN(c)) == 1)
7069 {
7070 /* Not a multi-byte char, put it back. */
7071 replace_push(c);
7072 break;
7073 }
7074 else
7075 {
7076 buf[0] = c;
7077 for (i = 1; i < n; ++i)
7078 buf[i] = replace_pop();
7079 if (utf_iscomposing(utf_ptr2char(buf)))
7080 ins_bytes_len(buf, n);
7081 else
7082 {
7083 /* Not a composing char, put it back. */
7084 for (i = n - 1; i >= 0; --i)
7085 replace_push(buf[i]);
7086 break;
7087 }
7088 }
7089 }
7090}
7091#endif
7092
7093/*
7094 * make the replace stack empty
7095 * (called when exiting replace mode)
7096 */
7097 static void
7098replace_flush()
7099{
7100 vim_free(replace_stack);
7101 replace_stack = NULL;
7102 replace_stack_len = 0;
7103 replace_stack_nr = 0;
7104}
7105
7106/*
7107 * Handle doing a BS for one character.
7108 * cc < 0: replace stack empty, just move cursor
7109 * cc == 0: character was inserted, delete it
7110 * cc > 0: character was replaced, put cc (first byte of original char) back
7111 * and check for more characters to be put back
7112 */
7113 static void
7114replace_do_bs()
7115{
7116 int cc;
7117#ifdef FEAT_VREPLACE
7118 int orig_len = 0;
7119 int ins_len;
7120 int orig_vcols = 0;
7121 colnr_T start_vcol;
7122 char_u *p;
7123 int i;
7124 int vcol;
7125#endif
7126
7127 cc = replace_pop();
7128 if (cc > 0)
7129 {
7130#ifdef FEAT_VREPLACE
7131 if (State & VREPLACE_FLAG)
7132 {
7133 /* Get the number of screen cells used by the character we are
7134 * going to delete. */
7135 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7136 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7137 }
7138#endif
7139#ifdef FEAT_MBYTE
7140 if (has_mbyte)
7141 {
7142 del_char(FALSE);
7143# ifdef FEAT_VREPLACE
7144 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007145 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146# endif
7147 replace_push(cc);
7148 }
7149 else
7150#endif
7151 {
7152 pchar_cursor(cc);
7153#ifdef FEAT_VREPLACE
7154 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007155 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156#endif
7157 }
7158 replace_pop_ins();
7159
7160#ifdef FEAT_VREPLACE
7161 if (State & VREPLACE_FLAG)
7162 {
7163 /* Get the number of screen cells used by the inserted characters */
7164 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007165 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 vcol = start_vcol;
7167 for (i = 0; i < ins_len; ++i)
7168 {
7169 vcol += chartabsize(p + i, vcol);
7170#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007171 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172#endif
7173 }
7174 vcol -= start_vcol;
7175
7176 /* Delete spaces that were inserted after the cursor to keep the
7177 * text aligned. */
7178 curwin->w_cursor.col += ins_len;
7179 while (vcol > orig_vcols && gchar_cursor() == ' ')
7180 {
7181 del_char(FALSE);
7182 ++orig_vcols;
7183 }
7184 curwin->w_cursor.col -= ins_len;
7185 }
7186#endif
7187
7188 /* mark the buffer as changed and prepare for displaying */
7189 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7190 }
7191 else if (cc == 0)
7192 (void)del_char(FALSE);
7193}
7194
7195#ifdef FEAT_CINDENT
7196/*
7197 * Return TRUE if C-indenting is on.
7198 */
7199 static int
7200cindent_on()
7201{
7202 return (!p_paste && (curbuf->b_p_cin
7203# ifdef FEAT_EVAL
7204 || *curbuf->b_p_inde != NUL
7205# endif
7206 ));
7207}
7208#endif
7209
7210#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7211/*
7212 * Re-indent the current line, based on the current contents of it and the
7213 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7214 * confused what all the part that handles Control-T is doing that I'm not.
7215 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7216 */
7217
7218 void
7219fixthisline(get_the_indent)
7220 int (*get_the_indent) __ARGS((void));
7221{
7222 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7223 if (linewhite(curwin->w_cursor.lnum))
7224 did_ai = TRUE; /* delete the indent if the line stays empty */
7225}
7226
7227 void
7228fix_indent()
7229{
7230 if (p_paste)
7231 return;
7232# ifdef FEAT_LISP
7233 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7234 fixthisline(get_lisp_indent);
7235# endif
7236# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7237 else
7238# endif
7239# ifdef FEAT_CINDENT
7240 if (cindent_on())
7241 do_c_expr_indent();
7242# endif
7243}
7244
7245#endif
7246
7247#ifdef FEAT_CINDENT
7248/*
7249 * return TRUE if 'cinkeys' contains the key "keytyped",
7250 * when == '*': Only if key is preceded with '*' (indent before insert)
7251 * when == '!': Only if key is prededed with '!' (don't insert)
7252 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7253 *
7254 * "keytyped" can have a few special values:
7255 * KEY_OPEN_FORW
7256 * KEY_OPEN_BACK
7257 * KEY_COMPLETE just finished completion.
7258 *
7259 * If line_is_empty is TRUE accept keys with '0' before them.
7260 */
7261 int
7262in_cinkeys(keytyped, when, line_is_empty)
7263 int keytyped;
7264 int when;
7265 int line_is_empty;
7266{
7267 char_u *look;
7268 int try_match;
7269 int try_match_word;
7270 char_u *p;
7271 char_u *line;
7272 int icase;
7273 int i;
7274
7275#ifdef FEAT_EVAL
7276 if (*curbuf->b_p_inde != NUL)
7277 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7278 else
7279#endif
7280 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7281 while (*look)
7282 {
7283 /*
7284 * Find out if we want to try a match with this key, depending on
7285 * 'when' and a '*' or '!' before the key.
7286 */
7287 switch (when)
7288 {
7289 case '*': try_match = (*look == '*'); break;
7290 case '!': try_match = (*look == '!'); break;
7291 default: try_match = (*look != '*'); break;
7292 }
7293 if (*look == '*' || *look == '!')
7294 ++look;
7295
7296 /*
7297 * If there is a '0', only accept a match if the line is empty.
7298 * But may still match when typing last char of a word.
7299 */
7300 if (*look == '0')
7301 {
7302 try_match_word = try_match;
7303 if (!line_is_empty)
7304 try_match = FALSE;
7305 ++look;
7306 }
7307 else
7308 try_match_word = FALSE;
7309
7310 /*
7311 * does it look like a control character?
7312 */
7313 if (*look == '^'
7314#ifdef EBCDIC
7315 && (Ctrl_chr(look[1]) != 0)
7316#else
7317 && look[1] >= '?' && look[1] <= '_'
7318#endif
7319 )
7320 {
7321 if (try_match && keytyped == Ctrl_chr(look[1]))
7322 return TRUE;
7323 look += 2;
7324 }
7325 /*
7326 * 'o' means "o" command, open forward.
7327 * 'O' means "O" command, open backward.
7328 */
7329 else if (*look == 'o')
7330 {
7331 if (try_match && keytyped == KEY_OPEN_FORW)
7332 return TRUE;
7333 ++look;
7334 }
7335 else if (*look == 'O')
7336 {
7337 if (try_match && keytyped == KEY_OPEN_BACK)
7338 return TRUE;
7339 ++look;
7340 }
7341
7342 /*
7343 * 'e' means to check for "else" at start of line and just before the
7344 * cursor.
7345 */
7346 else if (*look == 'e')
7347 {
7348 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7349 {
7350 p = ml_get_curline();
7351 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7352 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7353 return TRUE;
7354 }
7355 ++look;
7356 }
7357
7358 /*
7359 * ':' only causes an indent if it is at the end of a label or case
7360 * statement, or when it was before typing the ':' (to fix
7361 * class::method for C++).
7362 */
7363 else if (*look == ':')
7364 {
7365 if (try_match && keytyped == ':')
7366 {
7367 p = ml_get_curline();
7368 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7369 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007370 /* Need to get the line again after cin_islabel(). */
7371 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 if (curwin->w_cursor.col > 2
7373 && p[curwin->w_cursor.col - 1] == ':'
7374 && p[curwin->w_cursor.col - 2] == ':')
7375 {
7376 p[curwin->w_cursor.col - 1] = ' ';
7377 i = (cin_iscase(p) || cin_isscopedecl(p)
7378 || cin_islabel(30));
7379 p = ml_get_curline();
7380 p[curwin->w_cursor.col - 1] = ':';
7381 if (i)
7382 return TRUE;
7383 }
7384 }
7385 ++look;
7386 }
7387
7388
7389 /*
7390 * Is it a key in <>, maybe?
7391 */
7392 else if (*look == '<')
7393 {
7394 if (try_match)
7395 {
7396 /*
7397 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7398 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7399 * >, *, : and ! keys if they really really want to.
7400 */
7401 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7402 && keytyped == look[1])
7403 return TRUE;
7404
7405 if (keytyped == get_special_key_code(look + 1))
7406 return TRUE;
7407 }
7408 while (*look && *look != '>')
7409 look++;
7410 while (*look == '>')
7411 look++;
7412 }
7413
7414 /*
7415 * Is it a word: "=word"?
7416 */
7417 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7418 {
7419 ++look;
7420 if (*look == '~')
7421 {
7422 icase = TRUE;
7423 ++look;
7424 }
7425 else
7426 icase = FALSE;
7427 p = vim_strchr(look, ',');
7428 if (p == NULL)
7429 p = look + STRLEN(look);
7430 if ((try_match || try_match_word)
7431 && curwin->w_cursor.col >= (colnr_T)(p - look))
7432 {
7433 int match = FALSE;
7434
7435#ifdef FEAT_INS_EXPAND
7436 if (keytyped == KEY_COMPLETE)
7437 {
7438 char_u *s;
7439
7440 /* Just completed a word, check if it starts with "look".
7441 * search back for the start of a word. */
7442 line = ml_get_curline();
7443# ifdef FEAT_MBYTE
7444 if (has_mbyte)
7445 {
7446 char_u *n;
7447
7448 for (s = line + curwin->w_cursor.col; s > line; s = n)
7449 {
7450 n = mb_prevptr(line, s);
7451 if (!vim_iswordp(n))
7452 break;
7453 }
7454 }
7455 else
7456# endif
7457 for (s = line + curwin->w_cursor.col; s > line; --s)
7458 if (!vim_iswordc(s[-1]))
7459 break;
7460 if (s + (p - look) <= line + curwin->w_cursor.col
7461 && (icase
7462 ? MB_STRNICMP(s, look, p - look)
7463 : STRNCMP(s, look, p - look)) == 0)
7464 match = TRUE;
7465 }
7466 else
7467#endif
7468 /* TODO: multi-byte */
7469 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7470 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7471 {
7472 line = ml_get_cursor();
7473 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7474 || !vim_iswordc(line[-(p - look) - 1]))
7475 && (icase
7476 ? MB_STRNICMP(line - (p - look), look, p - look)
7477 : STRNCMP(line - (p - look), look, p - look))
7478 == 0)
7479 match = TRUE;
7480 }
7481 if (match && try_match_word && !try_match)
7482 {
7483 /* "0=word": Check if there are only blanks before the
7484 * word. */
7485 line = ml_get_curline();
7486 if ((int)(skipwhite(line) - line) !=
7487 (int)(curwin->w_cursor.col - (p - look)))
7488 match = FALSE;
7489 }
7490 if (match)
7491 return TRUE;
7492 }
7493 look = p;
7494 }
7495
7496 /*
7497 * ok, it's a boring generic character.
7498 */
7499 else
7500 {
7501 if (try_match && *look == keytyped)
7502 return TRUE;
7503 ++look;
7504 }
7505
7506 /*
7507 * Skip over ", ".
7508 */
7509 look = skip_to_option_part(look);
7510 }
7511 return FALSE;
7512}
7513#endif /* FEAT_CINDENT */
7514
7515#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7516/*
7517 * Map Hebrew keyboard when in hkmap mode.
7518 */
7519 int
7520hkmap(c)
7521 int c;
7522{
7523 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7524 {
7525 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7526 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7527 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7528 static char_u map[26] =
7529 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7530 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7531 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7532 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7533 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7534 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7535 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7536 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7537 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7538
7539 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7540 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7541 /* '-1'='sofit' */
7542 else if (c == 'x')
7543 return 'X';
7544 else if (c == 'q')
7545 return '\''; /* {geresh}={'} */
7546 else if (c == 246)
7547 return ' '; /* \"o --> ' ' for a german keyboard */
7548 else if (c == 228)
7549 return ' '; /* \"a --> ' ' -- / -- */
7550 else if (c == 252)
7551 return ' '; /* \"u --> ' ' -- / -- */
7552#ifdef EBCDIC
7553 else if (islower(c))
7554#else
7555 /* NOTE: islower() does not do the right thing for us on Linux so we
7556 * do this the same was as 5.7 and previous, so it works correctly on
7557 * all systems. Specifically, the e.g. Delete and Arrow keys are
7558 * munged and won't work if e.g. searching for Hebrew text.
7559 */
7560 else if (c >= 'a' && c <= 'z')
7561#endif
7562 return (int)(map[CharOrdLow(c)] + p_aleph);
7563 else
7564 return c;
7565 }
7566 else
7567 {
7568 switch (c)
7569 {
7570 case '`': return ';';
7571 case '/': return '.';
7572 case '\'': return ',';
7573 case 'q': return '/';
7574 case 'w': return '\'';
7575
7576 /* Hebrew letters - set offset from 'a' */
7577 case ',': c = '{'; break;
7578 case '.': c = 'v'; break;
7579 case ';': c = 't'; break;
7580 default: {
7581 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7582
7583#ifdef EBCDIC
7584 /* see note about islower() above */
7585 if (!islower(c))
7586#else
7587 if (c < 'a' || c > 'z')
7588#endif
7589 return c;
7590 c = str[CharOrdLow(c)];
7591 break;
7592 }
7593 }
7594
7595 return (int)(CharOrdLow(c) + p_aleph);
7596 }
7597}
7598#endif
7599
7600 static void
7601ins_reg()
7602{
7603 int need_redraw = FALSE;
7604 int regname;
7605 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007606#ifdef FEAT_VISUAL
7607 int vis_active = VIsual_active;
7608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609
7610 /*
7611 * If we are going to wait for a character, show a '"'.
7612 */
7613 pc_status = PC_STATUS_UNSET;
7614 if (redrawing() && !char_avail())
7615 {
7616 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007617 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618
7619 edit_putchar('"', TRUE);
7620#ifdef FEAT_CMDL_INFO
7621 add_to_showcmd_c(Ctrl_R);
7622#endif
7623 }
7624
7625#ifdef USE_ON_FLY_SCROLL
7626 dont_scroll = TRUE; /* disallow scrolling here */
7627#endif
7628
7629 /*
7630 * Don't map the register name. This also prevents the mode message to be
7631 * deleted when ESC is hit.
7632 */
7633 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007634 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635#ifdef FEAT_LANGMAP
7636 LANGMAP_ADJUST(regname, TRUE);
7637#endif
7638 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7639 {
7640 /* Get a third key for literal register insertion */
7641 literally = regname;
7642#ifdef FEAT_CMDL_INFO
7643 add_to_showcmd_c(literally);
7644#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007645 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646#ifdef FEAT_LANGMAP
7647 LANGMAP_ADJUST(regname, TRUE);
7648#endif
7649 }
7650 --no_mapping;
7651
7652#ifdef FEAT_EVAL
7653 /*
7654 * Don't call u_sync() while getting the expression,
7655 * evaluating it or giving an error message for it!
7656 */
7657 ++no_u_sync;
7658 if (regname == '=')
7659 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007660# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007664# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 /* Restore the Input Method. */
7666 if (im_on)
7667 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007670 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7671 {
7672 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 else
7676 {
7677#endif
7678 if (literally == Ctrl_O || literally == Ctrl_P)
7679 {
7680 /* Append the command to the redo buffer. */
7681 AppendCharToRedobuff(Ctrl_R);
7682 AppendCharToRedobuff(literally);
7683 AppendCharToRedobuff(regname);
7684
7685 do_put(regname, BACKWARD, 1L,
7686 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7687 }
7688 else if (insert_reg(regname, literally) == FAIL)
7689 {
7690 vim_beep();
7691 need_redraw = TRUE; /* remove the '"' */
7692 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007693 else if (stop_insert_mode)
7694 /* When the '=' register was used and a function was invoked that
7695 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7696 * insert anything, need to remove the '"' */
7697 need_redraw = TRUE;
7698
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699#ifdef FEAT_EVAL
7700 }
7701 --no_u_sync;
7702#endif
7703#ifdef FEAT_CMDL_INFO
7704 clear_showcmd();
7705#endif
7706
7707 /* If the inserted register is empty, we need to remove the '"' */
7708 if (need_redraw || stuff_empty())
7709 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007710
7711#ifdef FEAT_VISUAL
7712 /* Disallow starting Visual mode here, would get a weird mode. */
7713 if (!vis_active && VIsual_active)
7714 end_visual_mode();
7715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717
7718/*
7719 * CTRL-G commands in Insert mode.
7720 */
7721 static void
7722ins_ctrl_g()
7723{
7724 int c;
7725
7726#ifdef FEAT_INS_EXPAND
7727 /* Right after CTRL-X the cursor will be after the ruler. */
7728 setcursor();
7729#endif
7730
7731 /*
7732 * Don't map the second key. This also prevents the mode message to be
7733 * deleted when ESC is hit.
7734 */
7735 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007736 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 --no_mapping;
7738 switch (c)
7739 {
7740 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7741 case K_UP:
7742 case Ctrl_K:
7743 case 'k': ins_up(TRUE);
7744 break;
7745
7746 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7747 case K_DOWN:
7748 case Ctrl_J:
7749 case 'j': ins_down(TRUE);
7750 break;
7751
7752 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007753 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007755
7756 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007757 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007758 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 break;
7760
7761 /* Unknown CTRL-G command, reserved for future expansion. */
7762 default: vim_beep();
7763 }
7764}
7765
7766/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007767 * CTRL-^ in Insert mode.
7768 */
7769 static void
7770ins_ctrl_hat()
7771{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007772 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007773 {
7774 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7775 if (State & LANGMAP)
7776 {
7777 curbuf->b_p_iminsert = B_IMODE_NONE;
7778 State &= ~LANGMAP;
7779 }
7780 else
7781 {
7782 curbuf->b_p_iminsert = B_IMODE_LMAP;
7783 State |= LANGMAP;
7784#ifdef USE_IM_CONTROL
7785 im_set_active(FALSE);
7786#endif
7787 }
7788 }
7789#ifdef USE_IM_CONTROL
7790 else
7791 {
7792 /* There are no ":lmap" mappings, toggle IM */
7793 if (im_get_status())
7794 {
7795 curbuf->b_p_iminsert = B_IMODE_NONE;
7796 im_set_active(FALSE);
7797 }
7798 else
7799 {
7800 curbuf->b_p_iminsert = B_IMODE_IM;
7801 State &= ~LANGMAP;
7802 im_set_active(TRUE);
7803 }
7804 }
7805#endif
7806 set_iminsert_global();
7807 showmode();
7808#ifdef FEAT_GUI
7809 /* may show different cursor shape or color */
7810 if (gui.in_use)
7811 gui_update_cursor(TRUE, FALSE);
7812#endif
7813#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7814 /* Show/unshow value of 'keymap' in status lines. */
7815 status_redraw_curbuf();
7816#endif
7817}
7818
7819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 * Handle ESC in insert mode.
7821 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7822 * insert.
7823 */
7824 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007825ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 long *count;
7827 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007828 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830 int temp;
7831 static int disabled_redraw = FALSE;
7832
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007833#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007834 check_spell_redraw();
7835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836#if defined(FEAT_HANGULIN)
7837# if defined(ESC_CHG_TO_ENG_MODE)
7838 hangul_input_state_set(0);
7839# endif
7840 if (composing_hangul)
7841 {
7842 push_raw_key(composing_hangul_buffer, 2);
7843 composing_hangul = 0;
7844 }
7845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846
7847 temp = curwin->w_cursor.col;
7848 if (disabled_redraw)
7849 {
7850 --RedrawingDisabled;
7851 disabled_redraw = FALSE;
7852 }
7853 if (!arrow_used)
7854 {
7855 /*
7856 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007857 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7858 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 */
7860 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007861 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862
7863 /*
7864 * Repeating insert may take a long time. Check for
7865 * interrupt now and then.
7866 */
7867 if (*count > 0)
7868 {
7869 line_breakcheck();
7870 if (got_int)
7871 *count = 0;
7872 }
7873
7874 if (--*count > 0) /* repeat what was typed */
7875 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007876 /* Vi repeats the insert without replacing characters. */
7877 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7878 State &= ~REPLACE_FLAG;
7879
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 (void)start_redo_ins();
7881 if (cmdchar == 'r' || cmdchar == 'v')
7882 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7883 ++RedrawingDisabled;
7884 disabled_redraw = TRUE;
7885 return FALSE; /* repeat the insert */
7886 }
7887 stop_insert(&curwin->w_cursor, TRUE);
7888 undisplay_dollar();
7889 }
7890
7891 /* When an autoindent was removed, curswant stays after the
7892 * indent */
7893 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7894 curwin->w_set_curswant = TRUE;
7895
7896 /* Remember the last Insert position in the '^ mark. */
7897 if (!cmdmod.keepjumps)
7898 curbuf->b_last_insert = curwin->w_cursor;
7899
7900 /*
7901 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007902 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007904 if (!nomove
7905 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906#ifdef FEAT_VIRTUALEDIT
7907 || curwin->w_cursor.coladd > 0
7908#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007909 )
7910 && (restart_edit == NUL
7911 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007913 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007915 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916#ifdef FEAT_RIGHTLEFT
7917 && !revins_on
7918#endif
7919 )
7920 {
7921#ifdef FEAT_VIRTUALEDIT
7922 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7923 {
7924 oneleft();
7925 if (restart_edit != NUL)
7926 ++curwin->w_cursor.coladd;
7927 }
7928 else
7929#endif
7930 {
7931 --curwin->w_cursor.col;
7932#ifdef FEAT_MBYTE
7933 /* Correct cursor for multi-byte character. */
7934 if (has_mbyte)
7935 mb_adjust_cursor();
7936#endif
7937 }
7938 }
7939
7940#ifdef USE_IM_CONTROL
7941 /* Disable IM to allow typing English directly for Normal mode commands.
7942 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7943 * well). */
7944 if (!(State & LANGMAP))
7945 im_save_status(&curbuf->b_p_iminsert);
7946 im_set_active(FALSE);
7947#endif
7948
7949 State = NORMAL;
7950 /* need to position cursor again (e.g. when on a TAB ) */
7951 changed_cline_bef_curs();
7952
7953#ifdef FEAT_MOUSE
7954 setmouse();
7955#endif
7956#ifdef CURSOR_SHAPE
7957 ui_cursor_shape(); /* may show different cursor shape */
7958#endif
7959
7960 /*
7961 * When recording or for CTRL-O, need to display the new mode.
7962 * Otherwise remove the mode message.
7963 */
7964 if (Recording || restart_edit != NUL)
7965 showmode();
7966 else if (p_smd)
7967 MSG("");
7968
7969 return TRUE; /* exit Insert mode */
7970}
7971
7972#ifdef FEAT_RIGHTLEFT
7973/*
7974 * Toggle language: hkmap and revins_on.
7975 * Move to end of reverse inserted text.
7976 */
7977 static void
7978ins_ctrl_()
7979{
7980 if (revins_on && revins_chars && revins_scol >= 0)
7981 {
7982 while (gchar_cursor() != NUL && revins_chars--)
7983 ++curwin->w_cursor.col;
7984 }
7985 p_ri = !p_ri;
7986 revins_on = (State == INSERT && p_ri);
7987 if (revins_on)
7988 {
7989 revins_scol = curwin->w_cursor.col;
7990 revins_legal++;
7991 revins_chars = 0;
7992 undisplay_dollar();
7993 }
7994 else
7995 revins_scol = -1;
7996#ifdef FEAT_FKMAP
7997 if (p_altkeymap)
7998 {
7999 /*
8000 * to be consistent also for redo command, using '.'
8001 * set arrow_used to true and stop it - causing to redo
8002 * characters entered in one mode (normal/reverse insert).
8003 */
8004 arrow_used = TRUE;
8005 (void)stop_arrow();
8006 p_fkmap = curwin->w_p_rl ^ p_ri;
8007 if (p_fkmap && p_ri)
8008 State = INSERT;
8009 }
8010 else
8011#endif
8012 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8013 showmode();
8014}
8015#endif
8016
8017#ifdef FEAT_VISUAL
8018/*
8019 * If 'keymodel' contains "startsel", may start selection.
8020 * Returns TRUE when a CTRL-O and other keys stuffed.
8021 */
8022 static int
8023ins_start_select(c)
8024 int c;
8025{
8026 if (km_startsel)
8027 switch (c)
8028 {
8029 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 case K_PAGEUP:
8032 case K_KPAGEUP:
8033 case K_PAGEDOWN:
8034 case K_KPAGEDOWN:
8035# ifdef MACOS
8036 case K_LEFT:
8037 case K_RIGHT:
8038 case K_UP:
8039 case K_DOWN:
8040 case K_END:
8041 case K_HOME:
8042# endif
8043 if (!(mod_mask & MOD_MASK_SHIFT))
8044 break;
8045 /* FALLTHROUGH */
8046 case K_S_LEFT:
8047 case K_S_RIGHT:
8048 case K_S_UP:
8049 case K_S_DOWN:
8050 case K_S_END:
8051 case K_S_HOME:
8052 /* Start selection right away, the cursor can move with
8053 * CTRL-O when beyond the end of the line. */
8054 start_selection();
8055
8056 /* Execute the key in (insert) Select mode. */
8057 stuffcharReadbuff(Ctrl_O);
8058 if (mod_mask)
8059 {
8060 char_u buf[4];
8061
8062 buf[0] = K_SPECIAL;
8063 buf[1] = KS_MODIFIER;
8064 buf[2] = mod_mask;
8065 buf[3] = NUL;
8066 stuffReadbuff(buf);
8067 }
8068 stuffcharReadbuff(c);
8069 return TRUE;
8070 }
8071 return FALSE;
8072}
8073#endif
8074
8075/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008076 * <Insert> key in Insert mode: toggle insert/remplace mode.
8077 */
8078 static void
8079ins_insert(replaceState)
8080 int replaceState;
8081{
8082#ifdef FEAT_FKMAP
8083 if (p_fkmap && p_ri)
8084 {
8085 beep_flush();
8086 EMSG(farsi_text_3); /* encoded in Farsi */
8087 return;
8088 }
8089#endif
8090
8091#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008092# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008093 set_vim_var_string(VV_INSERTMODE,
8094 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008095# ifdef FEAT_VREPLACE
8096 replaceState == VREPLACE ? "v" :
8097# endif
8098 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008099# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008100 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8101#endif
8102 if (State & REPLACE_FLAG)
8103 State = INSERT | (State & LANGMAP);
8104 else
8105 State = replaceState | (State & LANGMAP);
8106 AppendCharToRedobuff(K_INS);
8107 showmode();
8108#ifdef CURSOR_SHAPE
8109 ui_cursor_shape(); /* may show different cursor shape */
8110#endif
8111}
8112
8113/*
8114 * Pressed CTRL-O in Insert mode.
8115 */
8116 static void
8117ins_ctrl_o()
8118{
8119#ifdef FEAT_VREPLACE
8120 if (State & VREPLACE_FLAG)
8121 restart_edit = 'V';
8122 else
8123#endif
8124 if (State & REPLACE_FLAG)
8125 restart_edit = 'R';
8126 else
8127 restart_edit = 'I';
8128#ifdef FEAT_VIRTUALEDIT
8129 if (virtual_active())
8130 ins_at_eol = FALSE; /* cursor always keeps its column */
8131 else
8132#endif
8133 ins_at_eol = (gchar_cursor() == NUL);
8134}
8135
8136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 * If the cursor is on an indent, ^T/^D insert/delete one
8138 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
8139 * Always round the indent to 'shiftwith', this is compatible
8140 * with vi. But vi only supports ^T and ^D after an
8141 * autoindent, we support it everywhere.
8142 */
8143 static void
8144ins_shift(c, lastc)
8145 int c;
8146 int lastc;
8147{
8148 if (stop_arrow() == FAIL)
8149 return;
8150 AppendCharToRedobuff(c);
8151
8152 /*
8153 * 0^D and ^^D: remove all indent.
8154 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008155 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8156 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {
8158 --curwin->w_cursor.col;
8159 (void)del_char(FALSE); /* delete the '^' or '0' */
8160 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8161 if (State & REPLACE_FLAG)
8162 replace_pop_ins();
8163 if (lastc == '^')
8164 old_indent = get_indent(); /* remember curr. indent */
8165 change_indent(INDENT_SET, 0, TRUE, 0);
8166 }
8167 else
8168 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
8169
8170 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8171 did_ai = FALSE;
8172#ifdef FEAT_SMARTINDENT
8173 did_si = FALSE;
8174 can_si = FALSE;
8175 can_si_back = FALSE;
8176#endif
8177#ifdef FEAT_CINDENT
8178 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8179#endif
8180}
8181
8182 static void
8183ins_del()
8184{
8185 int temp;
8186
8187 if (stop_arrow() == FAIL)
8188 return;
8189 if (gchar_cursor() == NUL) /* delete newline */
8190 {
8191 temp = curwin->w_cursor.col;
8192 if (!can_bs(BS_EOL) /* only if "eol" included */
8193 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8194 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8195 || do_join(FALSE) == FAIL)
8196 vim_beep();
8197 else
8198 curwin->w_cursor.col = temp;
8199 }
8200 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8201 vim_beep();
8202 did_ai = FALSE;
8203#ifdef FEAT_SMARTINDENT
8204 did_si = FALSE;
8205 can_si = FALSE;
8206 can_si_back = FALSE;
8207#endif
8208 AppendCharToRedobuff(K_DEL);
8209}
8210
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008211static void ins_bs_one __ARGS((colnr_T *vcolp));
8212
8213/*
8214 * Delete one character for ins_bs().
8215 */
8216 static void
8217ins_bs_one(vcolp)
8218 colnr_T *vcolp;
8219{
8220 dec_cursor();
8221 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8222 if (State & REPLACE_FLAG)
8223 {
8224 /* Don't delete characters before the insert point when in
8225 * Replace mode */
8226 if (curwin->w_cursor.lnum != Insstart.lnum
8227 || curwin->w_cursor.col >= Insstart.col)
8228 replace_do_bs();
8229 }
8230 else
8231 (void)del_char(FALSE);
8232}
8233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234/*
8235 * Handle Backspace, delete-word and delete-line in Insert mode.
8236 * Return TRUE when backspace was actually used.
8237 */
8238 static int
8239ins_bs(c, mode, inserted_space_p)
8240 int c;
8241 int mode;
8242 int *inserted_space_p;
8243{
8244 linenr_T lnum;
8245 int cc;
8246 int temp = 0; /* init for GCC */
8247 colnr_T mincol;
8248 int did_backspace = FALSE;
8249 int in_indent;
8250 int oldState;
8251#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008252 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#endif
8254
8255 /*
8256 * can't delete anything in an empty file
8257 * can't backup past first character in buffer
8258 * can't backup past starting point unless 'backspace' > 1
8259 * can backup to a previous line if 'backspace' == 0
8260 */
8261 if ( bufempty()
8262 || (
8263#ifdef FEAT_RIGHTLEFT
8264 !revins_on &&
8265#endif
8266 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8267 || (!can_bs(BS_START)
8268 && (arrow_used
8269 || (curwin->w_cursor.lnum == Insstart.lnum
8270 && curwin->w_cursor.col <= Insstart.col)))
8271 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8272 && curwin->w_cursor.col <= ai_col)
8273 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8274 {
8275 vim_beep();
8276 return FALSE;
8277 }
8278
8279 if (stop_arrow() == FAIL)
8280 return FALSE;
8281 in_indent = inindent(0);
8282#ifdef FEAT_CINDENT
8283 if (in_indent)
8284 can_cindent = FALSE;
8285#endif
8286#ifdef FEAT_COMMENTS
8287 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8288#endif
8289#ifdef FEAT_RIGHTLEFT
8290 if (revins_on) /* put cursor after last inserted char */
8291 inc_cursor();
8292#endif
8293
8294#ifdef FEAT_VIRTUALEDIT
8295 /* Virtualedit:
8296 * BACKSPACE_CHAR eats a virtual space
8297 * BACKSPACE_WORD eats all coladd
8298 * BACKSPACE_LINE eats all coladd and keeps going
8299 */
8300 if (curwin->w_cursor.coladd > 0)
8301 {
8302 if (mode == BACKSPACE_CHAR)
8303 {
8304 --curwin->w_cursor.coladd;
8305 return TRUE;
8306 }
8307 if (mode == BACKSPACE_WORD)
8308 {
8309 curwin->w_cursor.coladd = 0;
8310 return TRUE;
8311 }
8312 curwin->w_cursor.coladd = 0;
8313 }
8314#endif
8315
8316 /*
8317 * delete newline!
8318 */
8319 if (curwin->w_cursor.col == 0)
8320 {
8321 lnum = Insstart.lnum;
8322 if (curwin->w_cursor.lnum == Insstart.lnum
8323#ifdef FEAT_RIGHTLEFT
8324 || revins_on
8325#endif
8326 )
8327 {
8328 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8329 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8330 return FALSE;
8331 --Insstart.lnum;
8332 Insstart.col = MAXCOL;
8333 }
8334 /*
8335 * In replace mode:
8336 * cc < 0: NL was inserted, delete it
8337 * cc >= 0: NL was replaced, put original characters back
8338 */
8339 cc = -1;
8340 if (State & REPLACE_FLAG)
8341 cc = replace_pop(); /* returns -1 if NL was inserted */
8342 /*
8343 * In replace mode, in the line we started replacing, we only move the
8344 * cursor.
8345 */
8346 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8347 {
8348 dec_cursor();
8349 }
8350 else
8351 {
8352#ifdef FEAT_VREPLACE
8353 if (!(State & VREPLACE_FLAG)
8354 || curwin->w_cursor.lnum > orig_line_count)
8355#endif
8356 {
8357 temp = gchar_cursor(); /* remember current char */
8358 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008359
8360 /* When "aw" is in 'formatoptions' we must delete the space at
8361 * the end of the line, otherwise the line will be broken
8362 * again when auto-formatting. */
8363 if (has_format_option(FO_AUTO)
8364 && has_format_option(FO_WHITE_PAR))
8365 {
8366 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8367 TRUE);
8368 int len;
8369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008370 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008371 if (len > 0 && ptr[len - 1] == ' ')
8372 ptr[len - 1] = NUL;
8373 }
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 (void)do_join(FALSE);
8376 if (temp == NUL && gchar_cursor() != NUL)
8377 inc_cursor();
8378 }
8379#ifdef FEAT_VREPLACE
8380 else
8381 dec_cursor();
8382#endif
8383
8384 /*
8385 * In REPLACE mode we have to put back the text that was replaced
8386 * by the NL. On the replace stack is first a NUL-terminated
8387 * sequence of characters that were deleted and then the
8388 * characters that NL replaced.
8389 */
8390 if (State & REPLACE_FLAG)
8391 {
8392 /*
8393 * Do the next ins_char() in NORMAL state, to
8394 * prevent ins_char() from replacing characters and
8395 * avoiding showmatch().
8396 */
8397 oldState = State;
8398 State = NORMAL;
8399 /*
8400 * restore characters (blanks) deleted after cursor
8401 */
8402 while (cc > 0)
8403 {
8404 temp = curwin->w_cursor.col;
8405#ifdef FEAT_MBYTE
8406 mb_replace_pop_ins(cc);
8407#else
8408 ins_char(cc);
8409#endif
8410 curwin->w_cursor.col = temp;
8411 cc = replace_pop();
8412 }
8413 /* restore the characters that NL replaced */
8414 replace_pop_ins();
8415 State = oldState;
8416 }
8417 }
8418 did_ai = FALSE;
8419 }
8420 else
8421 {
8422 /*
8423 * Delete character(s) before the cursor.
8424 */
8425#ifdef FEAT_RIGHTLEFT
8426 if (revins_on) /* put cursor on last inserted char */
8427 dec_cursor();
8428#endif
8429 mincol = 0;
8430 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008431 if (mode == BACKSPACE_LINE
8432 && (curbuf->b_p_ai
8433#ifdef FEAT_CINDENT
8434 || cindent_on()
8435#endif
8436 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437#ifdef FEAT_RIGHTLEFT
8438 && !revins_on
8439#endif
8440 )
8441 {
8442 temp = curwin->w_cursor.col;
8443 beginline(BL_WHITE);
8444 if (curwin->w_cursor.col < (colnr_T)temp)
8445 mincol = curwin->w_cursor.col;
8446 curwin->w_cursor.col = temp;
8447 }
8448
8449 /*
8450 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8451 */
8452 if ( mode == BACKSPACE_CHAR
8453 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008454 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 && (*(ml_get_cursor() - 1) == TAB
8456 || (*(ml_get_cursor() - 1) == ' '
8457 && (!*inserted_space_p
8458 || arrow_used))))))
8459 {
8460 int ts;
8461 colnr_T vcol;
8462 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008463 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464
8465 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008466 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 ts = curbuf->b_p_sw;
8468 else
8469 ts = curbuf->b_p_sts;
8470 /* Compute the virtual column where we want to be. Since
8471 * 'showbreak' may get in the way, need to get the last column of
8472 * the previous character. */
8473 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008474 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 dec_cursor();
8476 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8477 inc_cursor();
8478 want_vcol = (want_vcol / ts) * ts;
8479
8480 /* delete characters until we are at or before want_vcol */
8481 while (vcol > want_vcol
8482 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008483 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484
8485 /* insert extra spaces until we are at want_vcol */
8486 while (vcol < want_vcol)
8487 {
8488 /* Remember the first char we inserted */
8489 if (curwin->w_cursor.lnum == Insstart.lnum
8490 && curwin->w_cursor.col < Insstart.col)
8491 Insstart.col = curwin->w_cursor.col;
8492
8493#ifdef FEAT_VREPLACE
8494 if (State & VREPLACE_FLAG)
8495 ins_char(' ');
8496 else
8497#endif
8498 {
8499 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008500 if ((State & REPLACE_FLAG))
8501 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 }
8503 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8504 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008505
8506 /* If we are now back where we started delete one character. Can
8507 * happen when using 'sts' and 'linebreak'. */
8508 if (vcol >= start_vcol)
8509 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 }
8511
8512 /*
8513 * Delete upto starting point, start of line or previous word.
8514 */
8515 else do
8516 {
8517#ifdef FEAT_RIGHTLEFT
8518 if (!revins_on) /* put cursor on char to be deleted */
8519#endif
8520 dec_cursor();
8521
8522 /* start of word? */
8523 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8524 {
8525 mode = BACKSPACE_WORD_NOT_SPACE;
8526 temp = vim_iswordc(gchar_cursor());
8527 }
8528 /* end of word? */
8529 else if (mode == BACKSPACE_WORD_NOT_SPACE
8530 && (vim_isspace(cc = gchar_cursor())
8531 || vim_iswordc(cc) != temp))
8532 {
8533#ifdef FEAT_RIGHTLEFT
8534 if (!revins_on)
8535#endif
8536 inc_cursor();
8537#ifdef FEAT_RIGHTLEFT
8538 else if (State & REPLACE_FLAG)
8539 dec_cursor();
8540#endif
8541 break;
8542 }
8543 if (State & REPLACE_FLAG)
8544 replace_do_bs();
8545 else
8546 {
8547#ifdef FEAT_MBYTE
8548 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008549 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550#endif
8551 (void)del_char(FALSE);
8552#ifdef FEAT_MBYTE
8553 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008554 * If there are combining characters and 'delcombine' is set
8555 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 * character.
8557 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008558 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 inc_cursor();
8560#endif
8561#ifdef FEAT_RIGHTLEFT
8562 if (revins_chars)
8563 {
8564 revins_chars--;
8565 revins_legal++;
8566 }
8567 if (revins_on && gchar_cursor() == NUL)
8568 break;
8569#endif
8570 }
8571 /* Just a single backspace?: */
8572 if (mode == BACKSPACE_CHAR)
8573 break;
8574 } while (
8575#ifdef FEAT_RIGHTLEFT
8576 revins_on ||
8577#endif
8578 (curwin->w_cursor.col > mincol
8579 && (curwin->w_cursor.lnum != Insstart.lnum
8580 || curwin->w_cursor.col != Insstart.col)));
8581 did_backspace = TRUE;
8582 }
8583#ifdef FEAT_SMARTINDENT
8584 did_si = FALSE;
8585 can_si = FALSE;
8586 can_si_back = FALSE;
8587#endif
8588 if (curwin->w_cursor.col <= 1)
8589 did_ai = FALSE;
8590 /*
8591 * It's a little strange to put backspaces into the redo
8592 * buffer, but it makes auto-indent a lot easier to deal
8593 * with.
8594 */
8595 AppendCharToRedobuff(c);
8596
8597 /* If deleted before the insertion point, adjust it */
8598 if (curwin->w_cursor.lnum == Insstart.lnum
8599 && curwin->w_cursor.col < Insstart.col)
8600 Insstart.col = curwin->w_cursor.col;
8601
8602 /* vi behaviour: the cursor moves backward but the character that
8603 * was there remains visible
8604 * Vim behaviour: the cursor moves backward and the character that
8605 * was there is erased from the screen.
8606 * We can emulate the vi behaviour by pretending there is a dollar
8607 * displayed even when there isn't.
8608 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8609 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8610 dollar_vcol = curwin->w_virtcol;
8611
8612 return did_backspace;
8613}
8614
8615#ifdef FEAT_MOUSE
8616 static void
8617ins_mouse(c)
8618 int c;
8619{
8620 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008621 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622
8623# ifdef FEAT_GUI
8624 /* When GUI is active, also move/paste when 'mouse' is empty */
8625 if (!gui.in_use)
8626# endif
8627 if (!mouse_has(MOUSE_INSERT))
8628 return;
8629
8630 undisplay_dollar();
8631 tpos = curwin->w_cursor;
8632 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8633 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008634#ifdef FEAT_WINDOWS
8635 win_T *new_curwin = curwin;
8636
8637 if (curwin != old_curwin && win_valid(old_curwin))
8638 {
8639 /* Mouse took us to another window. We need to go back to the
8640 * previous one to stop insert there properly. */
8641 curwin = old_curwin;
8642 curbuf = curwin->w_buffer;
8643 }
8644#endif
8645 start_arrow(curwin == old_curwin ? &tpos : NULL);
8646#ifdef FEAT_WINDOWS
8647 if (curwin != new_curwin && win_valid(new_curwin))
8648 {
8649 curwin = new_curwin;
8650 curbuf = curwin->w_buffer;
8651 }
8652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653# ifdef FEAT_CINDENT
8654 can_cindent = TRUE;
8655# endif
8656 }
8657
8658#ifdef FEAT_WINDOWS
8659 /* redraw status lines (in case another window became active) */
8660 redraw_statuslines();
8661#endif
8662}
8663
8664 static void
8665ins_mousescroll(up)
8666 int up;
8667{
8668 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008669# if defined(FEAT_WINDOWS)
8670 win_T *old_curwin = curwin;
8671# endif
8672# ifdef FEAT_INS_EXPAND
8673 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674# endif
8675
8676 tpos = curwin->w_cursor;
8677
8678# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 /* Currently the mouse coordinates are only known in the GUI. */
8680 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8681 {
8682 int row, col;
8683
8684 row = mouse_row;
8685 col = mouse_col;
8686
8687 /* find the window at the pointer coordinates */
8688 curwin = mouse_find_win(&row, &col);
8689 curbuf = curwin->w_buffer;
8690 }
8691 if (curwin == old_curwin)
8692# endif
8693 undisplay_dollar();
8694
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008695# ifdef FEAT_INS_EXPAND
8696 /* Don't scroll the window in which completion is being done. */
8697 if (!pum_visible()
8698# if defined(FEAT_WINDOWS)
8699 || curwin != old_curwin
8700# endif
8701 )
8702# endif
8703 {
8704 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8705 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8706 else
8707 scroll_redraw(up, 3L);
8708# ifdef FEAT_INS_EXPAND
8709 did_scroll = TRUE;
8710# endif
8711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8714 curwin->w_redr_status = TRUE;
8715
8716 curwin = old_curwin;
8717 curbuf = curwin->w_buffer;
8718# endif
8719
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008720# ifdef FEAT_INS_EXPAND
8721 /* The popup menu may overlay the window, need to redraw it.
8722 * TODO: Would be more efficient to only redraw the windows that are
8723 * overlapped by the popup menu. */
8724 if (pum_visible() && did_scroll)
8725 {
8726 redraw_all_later(NOT_VALID);
8727 ins_compl_show_pum();
8728 }
8729# endif
8730
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 if (!equalpos(curwin->w_cursor, tpos))
8732 {
8733 start_arrow(&tpos);
8734# ifdef FEAT_CINDENT
8735 can_cindent = TRUE;
8736# endif
8737 }
8738}
8739#endif
8740
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008741#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008742 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008743ins_tabline(c)
8744 int c;
8745{
8746 /* We will be leaving the current window, unless closing another tab. */
8747 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8748 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8749 {
8750 undisplay_dollar();
8751 start_arrow(&curwin->w_cursor);
8752# ifdef FEAT_CINDENT
8753 can_cindent = TRUE;
8754# endif
8755 }
8756
8757 if (c == K_TABLINE)
8758 goto_tabpage(current_tab);
8759 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008760 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008761 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008762 redraw_statuslines(); /* will redraw the tabline when needed */
8763 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008764}
8765#endif
8766
8767#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 void
8769ins_scroll()
8770{
8771 pos_T tpos;
8772
8773 undisplay_dollar();
8774 tpos = curwin->w_cursor;
8775 if (gui_do_scroll())
8776 {
8777 start_arrow(&tpos);
8778# ifdef FEAT_CINDENT
8779 can_cindent = TRUE;
8780# endif
8781 }
8782}
8783
8784 void
8785ins_horscroll()
8786{
8787 pos_T tpos;
8788
8789 undisplay_dollar();
8790 tpos = curwin->w_cursor;
8791 if (gui_do_horiz_scroll())
8792 {
8793 start_arrow(&tpos);
8794# ifdef FEAT_CINDENT
8795 can_cindent = TRUE;
8796# endif
8797 }
8798}
8799#endif
8800
8801 static void
8802ins_left()
8803{
8804 pos_T tpos;
8805
8806#ifdef FEAT_FOLDING
8807 if ((fdo_flags & FDO_HOR) && KeyTyped)
8808 foldOpenCursor();
8809#endif
8810 undisplay_dollar();
8811 tpos = curwin->w_cursor;
8812 if (oneleft() == OK)
8813 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008814#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8815 /* Only call start_arrow() when not busy with preediting, it will
8816 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8817 if (!im_is_preediting())
8818#endif
8819 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820#ifdef FEAT_RIGHTLEFT
8821 /* If exit reversed string, position is fixed */
8822 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8823 revins_legal++;
8824 revins_chars++;
8825#endif
8826 }
8827
8828 /*
8829 * if 'whichwrap' set for cursor in insert mode may go to
8830 * previous line
8831 */
8832 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8833 {
8834 start_arrow(&tpos);
8835 --(curwin->w_cursor.lnum);
8836 coladvance((colnr_T)MAXCOL);
8837 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8838 }
8839 else
8840 vim_beep();
8841}
8842
8843 static void
8844ins_home(c)
8845 int c;
8846{
8847 pos_T tpos;
8848
8849#ifdef FEAT_FOLDING
8850 if ((fdo_flags & FDO_HOR) && KeyTyped)
8851 foldOpenCursor();
8852#endif
8853 undisplay_dollar();
8854 tpos = curwin->w_cursor;
8855 if (c == K_C_HOME)
8856 curwin->w_cursor.lnum = 1;
8857 curwin->w_cursor.col = 0;
8858#ifdef FEAT_VIRTUALEDIT
8859 curwin->w_cursor.coladd = 0;
8860#endif
8861 curwin->w_curswant = 0;
8862 start_arrow(&tpos);
8863}
8864
8865 static void
8866ins_end(c)
8867 int c;
8868{
8869 pos_T tpos;
8870
8871#ifdef FEAT_FOLDING
8872 if ((fdo_flags & FDO_HOR) && KeyTyped)
8873 foldOpenCursor();
8874#endif
8875 undisplay_dollar();
8876 tpos = curwin->w_cursor;
8877 if (c == K_C_END)
8878 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8879 coladvance((colnr_T)MAXCOL);
8880 curwin->w_curswant = MAXCOL;
8881
8882 start_arrow(&tpos);
8883}
8884
8885 static void
8886ins_s_left()
8887{
8888#ifdef FEAT_FOLDING
8889 if ((fdo_flags & FDO_HOR) && KeyTyped)
8890 foldOpenCursor();
8891#endif
8892 undisplay_dollar();
8893 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8894 {
8895 start_arrow(&curwin->w_cursor);
8896 (void)bck_word(1L, FALSE, FALSE);
8897 curwin->w_set_curswant = TRUE;
8898 }
8899 else
8900 vim_beep();
8901}
8902
8903 static void
8904ins_right()
8905{
8906#ifdef FEAT_FOLDING
8907 if ((fdo_flags & FDO_HOR) && KeyTyped)
8908 foldOpenCursor();
8909#endif
8910 undisplay_dollar();
8911 if (gchar_cursor() != NUL || virtual_active()
8912 )
8913 {
8914 start_arrow(&curwin->w_cursor);
8915 curwin->w_set_curswant = TRUE;
8916#ifdef FEAT_VIRTUALEDIT
8917 if (virtual_active())
8918 oneright();
8919 else
8920#endif
8921 {
8922#ifdef FEAT_MBYTE
8923 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008924 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 else
8926#endif
8927 ++curwin->w_cursor.col;
8928 }
8929
8930#ifdef FEAT_RIGHTLEFT
8931 revins_legal++;
8932 if (revins_chars)
8933 revins_chars--;
8934#endif
8935 }
8936 /* if 'whichwrap' set for cursor in insert mode, may move the
8937 * cursor to the next line */
8938 else if (vim_strchr(p_ww, ']') != NULL
8939 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8940 {
8941 start_arrow(&curwin->w_cursor);
8942 curwin->w_set_curswant = TRUE;
8943 ++curwin->w_cursor.lnum;
8944 curwin->w_cursor.col = 0;
8945 }
8946 else
8947 vim_beep();
8948}
8949
8950 static void
8951ins_s_right()
8952{
8953#ifdef FEAT_FOLDING
8954 if ((fdo_flags & FDO_HOR) && KeyTyped)
8955 foldOpenCursor();
8956#endif
8957 undisplay_dollar();
8958 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8959 || gchar_cursor() != NUL)
8960 {
8961 start_arrow(&curwin->w_cursor);
8962 (void)fwd_word(1L, FALSE, 0);
8963 curwin->w_set_curswant = TRUE;
8964 }
8965 else
8966 vim_beep();
8967}
8968
8969 static void
8970ins_up(startcol)
8971 int startcol; /* when TRUE move to Insstart.col */
8972{
8973 pos_T tpos;
8974 linenr_T old_topline = curwin->w_topline;
8975#ifdef FEAT_DIFF
8976 int old_topfill = curwin->w_topfill;
8977#endif
8978
8979 undisplay_dollar();
8980 tpos = curwin->w_cursor;
8981 if (cursor_up(1L, TRUE) == OK)
8982 {
8983 if (startcol)
8984 coladvance(getvcol_nolist(&Insstart));
8985 if (old_topline != curwin->w_topline
8986#ifdef FEAT_DIFF
8987 || old_topfill != curwin->w_topfill
8988#endif
8989 )
8990 redraw_later(VALID);
8991 start_arrow(&tpos);
8992#ifdef FEAT_CINDENT
8993 can_cindent = TRUE;
8994#endif
8995 }
8996 else
8997 vim_beep();
8998}
8999
9000 static void
9001ins_pageup()
9002{
9003 pos_T tpos;
9004
9005 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009006
9007#ifdef FEAT_WINDOWS
9008 if (mod_mask & MOD_MASK_CTRL)
9009 {
9010 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009011 if (first_tabpage->tp_next != NULL)
9012 {
9013 start_arrow(&curwin->w_cursor);
9014 goto_tabpage(-1);
9015 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009016 return;
9017 }
9018#endif
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 tpos = curwin->w_cursor;
9021 if (onepage(BACKWARD, 1L) == OK)
9022 {
9023 start_arrow(&tpos);
9024#ifdef FEAT_CINDENT
9025 can_cindent = TRUE;
9026#endif
9027 }
9028 else
9029 vim_beep();
9030}
9031
9032 static void
9033ins_down(startcol)
9034 int startcol; /* when TRUE move to Insstart.col */
9035{
9036 pos_T tpos;
9037 linenr_T old_topline = curwin->w_topline;
9038#ifdef FEAT_DIFF
9039 int old_topfill = curwin->w_topfill;
9040#endif
9041
9042 undisplay_dollar();
9043 tpos = curwin->w_cursor;
9044 if (cursor_down(1L, TRUE) == OK)
9045 {
9046 if (startcol)
9047 coladvance(getvcol_nolist(&Insstart));
9048 if (old_topline != curwin->w_topline
9049#ifdef FEAT_DIFF
9050 || old_topfill != curwin->w_topfill
9051#endif
9052 )
9053 redraw_later(VALID);
9054 start_arrow(&tpos);
9055#ifdef FEAT_CINDENT
9056 can_cindent = TRUE;
9057#endif
9058 }
9059 else
9060 vim_beep();
9061}
9062
9063 static void
9064ins_pagedown()
9065{
9066 pos_T tpos;
9067
9068 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009069
9070#ifdef FEAT_WINDOWS
9071 if (mod_mask & MOD_MASK_CTRL)
9072 {
9073 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009074 if (first_tabpage->tp_next != NULL)
9075 {
9076 start_arrow(&curwin->w_cursor);
9077 goto_tabpage(0);
9078 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009079 return;
9080 }
9081#endif
9082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 tpos = curwin->w_cursor;
9084 if (onepage(FORWARD, 1L) == OK)
9085 {
9086 start_arrow(&tpos);
9087#ifdef FEAT_CINDENT
9088 can_cindent = TRUE;
9089#endif
9090 }
9091 else
9092 vim_beep();
9093}
9094
9095#ifdef FEAT_DND
9096 static void
9097ins_drop()
9098{
9099 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9100}
9101#endif
9102
9103/*
9104 * Handle TAB in Insert or Replace mode.
9105 * Return TRUE when the TAB needs to be inserted like a normal character.
9106 */
9107 static int
9108ins_tab()
9109{
9110 int ind;
9111 int i;
9112 int temp;
9113
9114 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9115 Insstart_blank_vcol = get_nolist_virtcol();
9116 if (echeck_abbr(TAB + ABBR_OFF))
9117 return FALSE;
9118
9119 ind = inindent(0);
9120#ifdef FEAT_CINDENT
9121 if (ind)
9122 can_cindent = FALSE;
9123#endif
9124
9125 /*
9126 * When nothing special, insert TAB like a normal character
9127 */
9128 if (!curbuf->b_p_et
9129 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9130 && curbuf->b_p_sts == 0)
9131 return TRUE;
9132
9133 if (stop_arrow() == FAIL)
9134 return TRUE;
9135
9136 did_ai = FALSE;
9137#ifdef FEAT_SMARTINDENT
9138 did_si = FALSE;
9139 can_si = FALSE;
9140 can_si_back = FALSE;
9141#endif
9142 AppendToRedobuff((char_u *)"\t");
9143
9144 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9145 temp = (int)curbuf->b_p_sw;
9146 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9147 temp = (int)curbuf->b_p_sts;
9148 else /* otherwise use 'tabstop' */
9149 temp = (int)curbuf->b_p_ts;
9150 temp -= get_nolist_virtcol() % temp;
9151
9152 /*
9153 * Insert the first space with ins_char(). It will delete one char in
9154 * replace mode. Insert the rest with ins_str(); it will not delete any
9155 * chars. For VREPLACE mode, we use ins_char() for all characters.
9156 */
9157 ins_char(' ');
9158 while (--temp > 0)
9159 {
9160#ifdef FEAT_VREPLACE
9161 if (State & VREPLACE_FLAG)
9162 ins_char(' ');
9163 else
9164#endif
9165 {
9166 ins_str((char_u *)" ");
9167 if (State & REPLACE_FLAG) /* no char replaced */
9168 replace_push(NUL);
9169 }
9170 }
9171
9172 /*
9173 * When 'expandtab' not set: Replace spaces by TABs where possible.
9174 */
9175 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9176 {
9177 char_u *ptr;
9178#ifdef FEAT_VREPLACE
9179 char_u *saved_line = NULL; /* init for GCC */
9180 pos_T pos;
9181#endif
9182 pos_T fpos;
9183 pos_T *cursor;
9184 colnr_T want_vcol, vcol;
9185 int change_col = -1;
9186 int save_list = curwin->w_p_list;
9187
9188 /*
9189 * Get the current line. For VREPLACE mode, don't make real changes
9190 * yet, just work on a copy of the line.
9191 */
9192#ifdef FEAT_VREPLACE
9193 if (State & VREPLACE_FLAG)
9194 {
9195 pos = curwin->w_cursor;
9196 cursor = &pos;
9197 saved_line = vim_strsave(ml_get_curline());
9198 if (saved_line == NULL)
9199 return FALSE;
9200 ptr = saved_line + pos.col;
9201 }
9202 else
9203#endif
9204 {
9205 ptr = ml_get_cursor();
9206 cursor = &curwin->w_cursor;
9207 }
9208
9209 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9210 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9211 curwin->w_p_list = FALSE;
9212
9213 /* Find first white before the cursor */
9214 fpos = curwin->w_cursor;
9215 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9216 {
9217 --fpos.col;
9218 --ptr;
9219 }
9220
9221 /* In Replace mode, don't change characters before the insert point. */
9222 if ((State & REPLACE_FLAG)
9223 && fpos.lnum == Insstart.lnum
9224 && fpos.col < Insstart.col)
9225 {
9226 ptr += Insstart.col - fpos.col;
9227 fpos.col = Insstart.col;
9228 }
9229
9230 /* compute virtual column numbers of first white and cursor */
9231 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9232 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9233
9234 /* Use as many TABs as possible. Beware of 'showbreak' and
9235 * 'linebreak' adding extra virtual columns. */
9236 while (vim_iswhite(*ptr))
9237 {
9238 i = lbr_chartabsize((char_u *)"\t", vcol);
9239 if (vcol + i > want_vcol)
9240 break;
9241 if (*ptr != TAB)
9242 {
9243 *ptr = TAB;
9244 if (change_col < 0)
9245 {
9246 change_col = fpos.col; /* Column of first change */
9247 /* May have to adjust Insstart */
9248 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9249 Insstart.col = fpos.col;
9250 }
9251 }
9252 ++fpos.col;
9253 ++ptr;
9254 vcol += i;
9255 }
9256
9257 if (change_col >= 0)
9258 {
9259 int repl_off = 0;
9260
9261 /* Skip over the spaces we need. */
9262 while (vcol < want_vcol && *ptr == ' ')
9263 {
9264 vcol += lbr_chartabsize(ptr, vcol);
9265 ++ptr;
9266 ++repl_off;
9267 }
9268 if (vcol > want_vcol)
9269 {
9270 /* Must have a char with 'showbreak' just before it. */
9271 --ptr;
9272 --repl_off;
9273 }
9274 fpos.col += repl_off;
9275
9276 /* Delete following spaces. */
9277 i = cursor->col - fpos.col;
9278 if (i > 0)
9279 {
9280 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9281 /* correct replace stack. */
9282 if ((State & REPLACE_FLAG)
9283#ifdef FEAT_VREPLACE
9284 && !(State & VREPLACE_FLAG)
9285#endif
9286 )
9287 for (temp = i; --temp >= 0; )
9288 replace_join(repl_off);
9289 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009290#ifdef FEAT_NETBEANS_INTG
9291 if (usingNetbeans)
9292 {
9293 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9294 (long)(i + 1));
9295 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9296 (char_u *)"\t", 1);
9297 }
9298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 cursor->col -= i;
9300
9301#ifdef FEAT_VREPLACE
9302 /*
9303 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9304 * backspacing over the changed spacing and then inserting the new
9305 * spacing.
9306 */
9307 if (State & VREPLACE_FLAG)
9308 {
9309 /* Backspace from real cursor to change_col */
9310 backspace_until_column(change_col);
9311
9312 /* Insert each char in saved_line from changed_col to
9313 * ptr-cursor */
9314 ins_bytes_len(saved_line + change_col,
9315 cursor->col - change_col);
9316 }
9317#endif
9318 }
9319
9320#ifdef FEAT_VREPLACE
9321 if (State & VREPLACE_FLAG)
9322 vim_free(saved_line);
9323#endif
9324 curwin->w_p_list = save_list;
9325 }
9326
9327 return FALSE;
9328}
9329
9330/*
9331 * Handle CR or NL in insert mode.
9332 * Return TRUE when out of memory or can't undo.
9333 */
9334 static int
9335ins_eol(c)
9336 int c;
9337{
9338 int i;
9339
9340 if (echeck_abbr(c + ABBR_OFF))
9341 return FALSE;
9342 if (stop_arrow() == FAIL)
9343 return TRUE;
9344 undisplay_dollar();
9345
9346 /*
9347 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9348 * character under the cursor. Only push a NUL on the replace stack,
9349 * nothing to put back when the NL is deleted.
9350 */
9351 if ((State & REPLACE_FLAG)
9352#ifdef FEAT_VREPLACE
9353 && !(State & VREPLACE_FLAG)
9354#endif
9355 )
9356 replace_push(NUL);
9357
9358 /*
9359 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9360 * replacing the next line, so we push all of the characters left on the
9361 * line onto the replace stack. This is not done here though, it is done
9362 * in open_line().
9363 */
9364
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009365#ifdef FEAT_VIRTUALEDIT
9366 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9367 * CTRL-O). */
9368 if (virtual_active() && curwin->w_cursor.coladd > 0)
9369 coladvance(getviscol());
9370#endif
9371
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372#ifdef FEAT_RIGHTLEFT
9373# ifdef FEAT_FKMAP
9374 if (p_altkeymap && p_fkmap)
9375 fkmap(NL);
9376# endif
9377 /* NL in reverse insert will always start in the end of
9378 * current line. */
9379 if (revins_on)
9380 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9381#endif
9382
9383 AppendToRedobuff(NL_STR);
9384 i = open_line(FORWARD,
9385#ifdef FEAT_COMMENTS
9386 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9387#endif
9388 0, old_indent);
9389 old_indent = 0;
9390#ifdef FEAT_CINDENT
9391 can_cindent = TRUE;
9392#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009393#ifdef FEAT_FOLDING
9394 /* When inserting a line the cursor line must never be in a closed fold. */
9395 foldOpenCursor();
9396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
9398 return (!i);
9399}
9400
9401#ifdef FEAT_DIGRAPHS
9402/*
9403 * Handle digraph in insert mode.
9404 * Returns character still to be inserted, or NUL when nothing remaining to be
9405 * done.
9406 */
9407 static int
9408ins_digraph()
9409{
9410 int c;
9411 int cc;
9412
9413 pc_status = PC_STATUS_UNSET;
9414 if (redrawing() && !char_avail())
9415 {
9416 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009417 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418
9419 edit_putchar('?', TRUE);
9420#ifdef FEAT_CMDL_INFO
9421 add_to_showcmd_c(Ctrl_K);
9422#endif
9423 }
9424
9425#ifdef USE_ON_FLY_SCROLL
9426 dont_scroll = TRUE; /* disallow scrolling here */
9427#endif
9428
9429 /* don't map the digraph chars. This also prevents the
9430 * mode message to be deleted when ESC is hit */
9431 ++no_mapping;
9432 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009433 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 --no_mapping;
9435 --allow_keys;
9436 if (IS_SPECIAL(c) || mod_mask) /* special key */
9437 {
9438#ifdef FEAT_CMDL_INFO
9439 clear_showcmd();
9440#endif
9441 insert_special(c, TRUE, FALSE);
9442 return NUL;
9443 }
9444 if (c != ESC)
9445 {
9446 if (redrawing() && !char_avail())
9447 {
9448 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009449 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450
9451 if (char2cells(c) == 1)
9452 {
9453 /* first remove the '?', otherwise it's restored when typing
9454 * an ESC next */
9455 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009456 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 edit_putchar(c, TRUE);
9458 }
9459#ifdef FEAT_CMDL_INFO
9460 add_to_showcmd_c(c);
9461#endif
9462 }
9463 ++no_mapping;
9464 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009465 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 --no_mapping;
9467 --allow_keys;
9468 if (cc != ESC)
9469 {
9470 AppendToRedobuff((char_u *)CTRL_V_STR);
9471 c = getdigraph(c, cc, TRUE);
9472#ifdef FEAT_CMDL_INFO
9473 clear_showcmd();
9474#endif
9475 return c;
9476 }
9477 }
9478 edit_unputchar();
9479#ifdef FEAT_CMDL_INFO
9480 clear_showcmd();
9481#endif
9482 return NUL;
9483}
9484#endif
9485
9486/*
9487 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9488 * Returns the char to be inserted, or NUL if none found.
9489 */
9490 static int
9491ins_copychar(lnum)
9492 linenr_T lnum;
9493{
9494 int c;
9495 int temp;
9496 char_u *ptr, *prev_ptr;
9497
9498 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9499 {
9500 vim_beep();
9501 return NUL;
9502 }
9503
9504 /* try to advance to the cursor column */
9505 temp = 0;
9506 ptr = ml_get(lnum);
9507 prev_ptr = ptr;
9508 validate_virtcol();
9509 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9510 {
9511 prev_ptr = ptr;
9512 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9513 }
9514 if ((colnr_T)temp > curwin->w_virtcol)
9515 ptr = prev_ptr;
9516
9517#ifdef FEAT_MBYTE
9518 c = (*mb_ptr2char)(ptr);
9519#else
9520 c = *ptr;
9521#endif
9522 if (c == NUL)
9523 vim_beep();
9524 return c;
9525}
9526
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009527/*
9528 * CTRL-Y or CTRL-E typed in Insert mode.
9529 */
9530 static int
9531ins_ctrl_ey(tc)
9532 int tc;
9533{
9534 int c = tc;
9535
9536#ifdef FEAT_INS_EXPAND
9537 if (ctrl_x_mode == CTRL_X_SCROLL)
9538 {
9539 if (c == Ctrl_Y)
9540 scrolldown_clamp();
9541 else
9542 scrollup_clamp();
9543 redraw_later(VALID);
9544 }
9545 else
9546#endif
9547 {
9548 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9549 if (c != NUL)
9550 {
9551 long tw_save;
9552
9553 /* The character must be taken literally, insert like it
9554 * was typed after a CTRL-V, and pretend 'textwidth'
9555 * wasn't set. Digits, 'o' and 'x' are special after a
9556 * CTRL-V, don't use it for these. */
9557 if (c < 256 && !isalnum(c))
9558 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9559 tw_save = curbuf->b_p_tw;
9560 curbuf->b_p_tw = -1;
9561 insert_special(c, TRUE, FALSE);
9562 curbuf->b_p_tw = tw_save;
9563#ifdef FEAT_RIGHTLEFT
9564 revins_chars++;
9565 revins_legal++;
9566#endif
9567 c = Ctrl_V; /* pretend CTRL-V is last character */
9568 auto_format(FALSE, TRUE);
9569 }
9570 }
9571 return c;
9572}
9573
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574#ifdef FEAT_SMARTINDENT
9575/*
9576 * Try to do some very smart auto-indenting.
9577 * Used when inserting a "normal" character.
9578 */
9579 static void
9580ins_try_si(c)
9581 int c;
9582{
9583 pos_T *pos, old_pos;
9584 char_u *ptr;
9585 int i;
9586 int temp;
9587
9588 /*
9589 * do some very smart indenting when entering '{' or '}'
9590 */
9591 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9592 {
9593 /*
9594 * for '}' set indent equal to indent of line containing matching '{'
9595 */
9596 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9597 {
9598 old_pos = curwin->w_cursor;
9599 /*
9600 * If the matching '{' has a ')' immediately before it (ignoring
9601 * white-space), then line up with the start of the line
9602 * containing the matching '(' if there is one. This handles the
9603 * case where an "if (..\n..) {" statement continues over multiple
9604 * lines -- webb
9605 */
9606 ptr = ml_get(pos->lnum);
9607 i = pos->col;
9608 if (i > 0) /* skip blanks before '{' */
9609 while (--i > 0 && vim_iswhite(ptr[i]))
9610 ;
9611 curwin->w_cursor.lnum = pos->lnum;
9612 curwin->w_cursor.col = i;
9613 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9614 curwin->w_cursor = *pos;
9615 i = get_indent();
9616 curwin->w_cursor = old_pos;
9617#ifdef FEAT_VREPLACE
9618 if (State & VREPLACE_FLAG)
9619 change_indent(INDENT_SET, i, FALSE, NUL);
9620 else
9621#endif
9622 (void)set_indent(i, SIN_CHANGED);
9623 }
9624 else if (curwin->w_cursor.col > 0)
9625 {
9626 /*
9627 * when inserting '{' after "O" reduce indent, but not
9628 * more than indent of previous line
9629 */
9630 temp = TRUE;
9631 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9632 {
9633 old_pos = curwin->w_cursor;
9634 i = get_indent();
9635 while (curwin->w_cursor.lnum > 1)
9636 {
9637 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9638
9639 /* ignore empty lines and lines starting with '#'. */
9640 if (*ptr != '#' && *ptr != NUL)
9641 break;
9642 }
9643 if (get_indent() >= i)
9644 temp = FALSE;
9645 curwin->w_cursor = old_pos;
9646 }
9647 if (temp)
9648 shift_line(TRUE, FALSE, 1);
9649 }
9650 }
9651
9652 /*
9653 * set indent of '#' always to 0
9654 */
9655 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9656 {
9657 /* remember current indent for next line */
9658 old_indent = get_indent();
9659 (void)set_indent(0, SIN_CHANGED);
9660 }
9661
9662 /* Adjust ai_col, the char at this position can be deleted. */
9663 if (ai_col > curwin->w_cursor.col)
9664 ai_col = curwin->w_cursor.col;
9665}
9666#endif
9667
9668/*
9669 * Get the value that w_virtcol would have when 'list' is off.
9670 * Unless 'cpo' contains the 'L' flag.
9671 */
9672 static colnr_T
9673get_nolist_virtcol()
9674{
9675 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9676 return getvcol_nolist(&curwin->w_cursor);
9677 validate_virtcol();
9678 return curwin->w_virtcol;
9679}