blob: 9b3a528b0fe5f81f65c466f562981666a0df0218 [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 Moolenaar4a85b412006-04-23 22:40:29 +0000132static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int dup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000133static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000134static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000135static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000137static void ins_compl_upd_pum __ARGS((void));
138static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000139static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000140static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000142static 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 +0000143static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void ins_compl_free __ARGS((void));
145static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000146static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000147static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000148static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000149static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000150static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000151static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000152static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000154#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
155static void ins_compl_add_list __ARGS((list_T *list));
156#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void ins_compl_delete __ARGS((void));
159static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000160static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000161static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000163static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000164static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static int ins_complete __ARGS((int c));
166static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
167#endif /* FEAT_INS_EXPAND */
168
169#define BACKSPACE_CHAR 1
170#define BACKSPACE_WORD 2
171#define BACKSPACE_WORD_NOT_SPACE 3
172#define BACKSPACE_LINE 4
173
Bram Moolenaar754b5602006-02-09 23:53:20 +0000174static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void ins_ctrl_v __ARGS((void));
176static void undisplay_dollar __ARGS((void));
177static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static void check_auto_format __ARGS((int));
180static void redo_literal __ARGS((int c));
181static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000182#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000183static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000184static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000185static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
188static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000189#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static int replace_pop __ARGS((void));
193static void replace_join __ARGS((int off));
194static void replace_pop_ins __ARGS((void));
195#ifdef FEAT_MBYTE
196static void mb_replace_pop_ins __ARGS((int cc));
197#endif
198static void replace_flush __ARGS((void));
199static void replace_do_bs __ARGS((void));
200#ifdef FEAT_CINDENT
201static int cindent_on __ARGS((void));
202#endif
203static void ins_reg __ARGS((void));
204static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000205static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000206static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_RIGHTLEFT
208static void ins_ctrl_ __ARGS((void));
209#endif
210#ifdef FEAT_VISUAL
211static int ins_start_select __ARGS((int c));
212#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000213static void ins_insert __ARGS((int replaceState));
214static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ins_shift __ARGS((int c, int lastc));
216static void ins_del __ARGS((void));
217static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
218#ifdef FEAT_MOUSE
219static void ins_mouse __ARGS((int c));
220static void ins_mousescroll __ARGS((int up));
221#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000222#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
223static void ins_tabline __ARGS((int c));
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225static void ins_left __ARGS((void));
226static void ins_home __ARGS((int c));
227static void ins_end __ARGS((int c));
228static void ins_s_left __ARGS((void));
229static void ins_right __ARGS((void));
230static void ins_s_right __ARGS((void));
231static void ins_up __ARGS((int startcol));
232static void ins_pageup __ARGS((void));
233static void ins_down __ARGS((int startcol));
234static void ins_pagedown __ARGS((void));
235#ifdef FEAT_DND
236static void ins_drop __ARGS((void));
237#endif
238static int ins_tab __ARGS((void));
239static int ins_eol __ARGS((int c));
240#ifdef FEAT_DIGRAPHS
241static int ins_digraph __ARGS((void));
242#endif
243static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000244static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_SMARTINDENT
246static void ins_try_si __ARGS((int c));
247#endif
248static colnr_T get_nolist_virtcol __ARGS((void));
249
250static colnr_T Insstart_textlen; /* length of line when insert started */
251static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
252
253static char_u *last_insert = NULL; /* the text of the previous insert,
254 K_SPECIAL and CSI are escaped */
255static int last_insert_skip; /* nr of chars in front of previous insert */
256static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000257static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259#ifdef FEAT_CINDENT
260static int can_cindent; /* may do cindenting on this line */
261#endif
262
263static int old_indent = 0; /* for ^^D command in insert mode */
264
265#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000266static int revins_on; /* reverse insert mode on */
267static int revins_chars; /* how much to skip after edit */
268static int revins_legal; /* was the last char 'legal'? */
269static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272static int ins_need_undo; /* call u_save() before inserting a
273 char. Set when edit() is called.
274 after that arrow_used is used. */
275
276static int did_add_space = FALSE; /* auto_format() added an extra space
277 under the cursor */
278
279/*
280 * edit(): Start inserting text.
281 *
282 * "cmdchar" can be:
283 * 'i' normal insert command
284 * 'a' normal append command
285 * 'R' replace command
286 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
287 * but still only one <CR> is inserted. The <Esc> is not used for redo.
288 * 'g' "gI" command.
289 * 'V' "gR" command for Virtual Replace mode.
290 * 'v' "gr" command for single character Virtual Replace mode.
291 *
292 * This function is not called recursively. For CTRL-O commands, it returns
293 * and lets the caller handle the Normal-mode command.
294 *
295 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
296 */
297 int
298edit(cmdchar, startln, count)
299 int cmdchar;
300 int startln; /* if set, insert at start of line */
301 long count;
302{
303 int c = 0;
304 char_u *ptr;
305 int lastc;
306 colnr_T mincol;
307 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 int i;
309 int did_backspace = TRUE; /* previous char was backspace */
310#ifdef FEAT_CINDENT
311 int line_is_white = FALSE; /* line is empty before insert */
312#endif
313 linenr_T old_topline = 0; /* topline before insertion */
314#ifdef FEAT_DIFF
315 int old_topfill = -1;
316#endif
317 int inserted_space = FALSE; /* just inserted a space */
318 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000319 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000321 /* Remember whether editing was restarted after CTRL-O. */
322 did_restart_edit = restart_edit;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 /* sleep before redrawing, needed for "CTRL-O :" that results in an
325 * error message */
326 check_for_delay(TRUE);
327
328#ifdef HAVE_SANDBOX
329 /* Don't allow inserting in the sandbox. */
330 if (sandbox != 0)
331 {
332 EMSG(_(e_sandbox));
333 return FALSE;
334 }
335#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000336 /* Don't allow changes in the buffer while editing the cmdline. The
337 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000338 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000339 {
340 EMSG(_(e_secure));
341 return FALSE;
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 /* Don't allow recursive insert mode when busy with completion. */
346 if (compl_started || pum_visible())
347 {
348 EMSG(_(e_secure));
349 return FALSE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 ins_compl_clear(); /* clear stuff for CTRL-X mode */
352#endif
353
Bram Moolenaar843ee412004-06-30 16:16:41 +0000354#ifdef FEAT_AUTOCMD
355 /*
356 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
357 */
358 if (cmdchar != 'r' && cmdchar != 'v')
359 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000360# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361 if (cmdchar == 'R')
362 ptr = (char_u *)"r";
363 else if (cmdchar == 'V')
364 ptr = (char_u *)"v";
365 else
366 ptr = (char_u *)"i";
367 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000368# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
370 }
371#endif
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifdef FEAT_MOUSE
374 /*
375 * When doing a paste with the middle mouse button, Insstart is set to
376 * where the paste started.
377 */
378 if (where_paste_started.lnum != 0)
379 Insstart = where_paste_started;
380 else
381#endif
382 {
383 Insstart = curwin->w_cursor;
384 if (startln)
385 Insstart.col = 0;
386 }
387 Insstart_textlen = linetabsize(ml_get_curline());
388 Insstart_blank_vcol = MAXCOL;
389 if (!did_ai)
390 ai_col = 0;
391
392 if (cmdchar != NUL && restart_edit == 0)
393 {
394 ResetRedobuff();
395 AppendNumberToRedobuff(count);
396#ifdef FEAT_VREPLACE
397 if (cmdchar == 'V' || cmdchar == 'v')
398 {
399 /* "gR" or "gr" command */
400 AppendCharToRedobuff('g');
401 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
402 }
403 else
404#endif
405 {
406 AppendCharToRedobuff(cmdchar);
407 if (cmdchar == 'g') /* "gI" command */
408 AppendCharToRedobuff('I');
409 else if (cmdchar == 'r') /* "r<CR>" command */
410 count = 1; /* insert only one <CR> */
411 }
412 }
413
414 if (cmdchar == 'R')
415 {
416#ifdef FEAT_FKMAP
417 if (p_fkmap && p_ri)
418 {
419 beep_flush();
420 EMSG(farsi_text_3); /* encoded in Farsi */
421 State = INSERT;
422 }
423 else
424#endif
425 State = REPLACE;
426 }
427#ifdef FEAT_VREPLACE
428 else if (cmdchar == 'V' || cmdchar == 'v')
429 {
430 State = VREPLACE;
431 replaceState = VREPLACE;
432 orig_line_count = curbuf->b_ml.ml_line_count;
433 vr_lines_changed = 1;
434 }
435#endif
436 else
437 State = INSERT;
438
439 stop_insert_mode = FALSE;
440
441 /*
442 * Need to recompute the cursor position, it might move when the cursor is
443 * on a TAB or special character.
444 */
445 curs_columns(TRUE);
446
447 /*
448 * Enable langmap or IME, indicated by 'iminsert'.
449 * Note that IME may enabled/disabled without us noticing here, thus the
450 * 'iminsert' value may not reflect what is actually used. It is updated
451 * when hitting <Esc>.
452 */
453 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
454 State |= LANGMAP;
455#ifdef USE_IM_CONTROL
456 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_MOUSE
460 setmouse();
461#endif
462#ifdef FEAT_CMDL_INFO
463 clear_showcmd();
464#endif
465#ifdef FEAT_RIGHTLEFT
466 /* there is no reverse replace mode */
467 revins_on = (State == INSERT && p_ri);
468 if (revins_on)
469 undisplay_dollar();
470 revins_chars = 0;
471 revins_legal = 0;
472 revins_scol = -1;
473#endif
474
475 /*
476 * Handle restarting Insert mode.
477 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
478 * restart_edit non-zero, and something in the stuff buffer.
479 */
480 if (restart_edit != 0 && stuff_empty())
481 {
482#ifdef FEAT_MOUSE
483 /*
484 * After a paste we consider text typed to be part of the insert for
485 * the pasted text. You can backspace over the pasted text too.
486 */
487 if (where_paste_started.lnum)
488 arrow_used = FALSE;
489 else
490#endif
491 arrow_used = TRUE;
492 restart_edit = 0;
493
494 /*
495 * If the cursor was after the end-of-line before the CTRL-O and it is
496 * now at the end-of-line, put it after the end-of-line (this is not
497 * correct in very rare cases).
498 * Also do this if curswant is greater than the current virtual
499 * column. Eg after "^O$" or "^O80|".
500 */
501 validate_virtcol();
502 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000503 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 || curwin->w_curswant > curwin->w_virtcol)
505 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
506 {
507 if (ptr[1] == NUL)
508 ++curwin->w_cursor.col;
509#ifdef FEAT_MBYTE
510 else if (has_mbyte)
511 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000512 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (ptr[i] == NUL)
514 curwin->w_cursor.col += i;
515 }
516#endif
517 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000518 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 else
521 arrow_used = FALSE;
522
523 /* we are in insert mode now, don't need to start it anymore */
524 need_start_insertmode = FALSE;
525
526 /* Need to save the line for undo before inserting the first char. */
527 ins_need_undo = TRUE;
528
529#ifdef FEAT_MOUSE
530 where_paste_started.lnum = 0;
531#endif
532#ifdef FEAT_CINDENT
533 can_cindent = TRUE;
534#endif
535#ifdef FEAT_FOLDING
536 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
537 * restarting. */
538 if (!p_im && did_restart_edit == 0)
539 foldOpenCursor();
540#endif
541
542 /*
543 * If 'showmode' is set, show the current (insert/replace/..) mode.
544 * A warning message for changing a readonly file is given here, before
545 * actually changing anything. It's put after the mode, if any.
546 */
547 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000548 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 i = showmode();
550
551 if (!p_im && did_restart_edit == 0)
552 change_warning(i + 1);
553
554#ifdef CURSOR_SHAPE
555 ui_cursor_shape(); /* may show different cursor shape */
556#endif
557#ifdef FEAT_DIGRAPHS
558 do_digraph(-1); /* clear digraphs */
559#endif
560
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000561 /*
562 * Get the current length of the redo buffer, those characters have to be
563 * skipped if we want to get to the inserted characters.
564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 ptr = get_inserted();
566 if (ptr == NULL)
567 new_insert_skip = 0;
568 else
569 {
570 new_insert_skip = (int)STRLEN(ptr);
571 vim_free(ptr);
572 }
573
574 old_indent = 0;
575
576 /*
577 * Main loop in Insert mode: repeat until Insert mode is left.
578 */
579 for (;;)
580 {
581#ifdef FEAT_RIGHTLEFT
582 if (!revins_legal)
583 revins_scol = -1; /* reset on illegal motions */
584 else
585 revins_legal = 0;
586#endif
587 if (arrow_used) /* don't repeat insert when arrow key used */
588 count = 0;
589
590 if (stop_insert_mode)
591 {
592 /* ":stopinsert" used or 'insertmode' reset */
593 count = 0;
594 goto doESCkey;
595 }
596
597 /* set curwin->w_curswant for next K_DOWN or K_UP */
598 if (!arrow_used)
599 curwin->w_set_curswant = TRUE;
600
601 /* If there is no typeahead may check for timestamps (e.g., for when a
602 * menu invoked a shell command). */
603 if (stuff_empty())
604 {
605 did_check_timestamps = FALSE;
606 if (need_check_timestamps)
607 check_timestamps(FALSE);
608 }
609
610 /*
611 * When emsg() was called msg_scroll will have been set.
612 */
613 msg_scroll = FALSE;
614
615#ifdef FEAT_GUI
616 /* When 'mousefocus' is set a mouse movement may have taken us to
617 * another window. "need_mouse_correct" may then be set because of an
618 * autocommand. */
619 if (need_mouse_correct)
620 gui_mouse_correct();
621#endif
622
623#ifdef FEAT_FOLDING
624 /* Open fold at the cursor line, according to 'foldopen'. */
625 if (fdo_flags & FDO_INSERT)
626 foldOpenCursor();
627 /* Close folds where the cursor isn't, according to 'foldclose' */
628 if (!char_avail())
629 foldCheckClose();
630#endif
631
632 /*
633 * If we inserted a character at the last position of the last line in
634 * the window, scroll the window one line up. This avoids an extra
635 * redraw.
636 * This is detected when the cursor column is smaller after inserting
637 * something.
638 * Don't do this when the topline changed already, it has
639 * already been adjusted (by insertchar() calling open_line())).
640 */
641 if (curbuf->b_mod_set
642 && curwin->w_p_wrap
643 && !did_backspace
644 && curwin->w_topline == old_topline
645#ifdef FEAT_DIFF
646 && curwin->w_topfill == old_topfill
647#endif
648 )
649 {
650 mincol = curwin->w_wcol;
651 validate_cursor_col();
652
653 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
654 && curwin->w_wrow == W_WINROW(curwin)
655 + curwin->w_height - 1 - p_so
656 && (curwin->w_cursor.lnum != curwin->w_topline
657#ifdef FEAT_DIFF
658 || curwin->w_topfill > 0
659#endif
660 ))
661 {
662#ifdef FEAT_DIFF
663 if (curwin->w_topfill > 0)
664 --curwin->w_topfill;
665 else
666#endif
667#ifdef FEAT_FOLDING
668 if (hasFolding(curwin->w_topline, NULL, &old_topline))
669 set_topline(curwin, old_topline + 1);
670 else
671#endif
672 set_topline(curwin, curwin->w_topline + 1);
673 }
674 }
675
676 /* May need to adjust w_topline to show the cursor. */
677 update_topline();
678
679 did_backspace = FALSE;
680
681 validate_cursor(); /* may set must_redraw */
682
683 /*
684 * Redraw the display when no characters are waiting.
685 * Also shows mode, ruler and positions cursor.
686 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000687 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689#ifdef FEAT_SCROLLBIND
690 if (curwin->w_p_scb)
691 do_check_scrollbind(TRUE);
692#endif
693
694 update_curswant();
695 old_topline = curwin->w_topline;
696#ifdef FEAT_DIFF
697 old_topfill = curwin->w_topfill;
698#endif
699
700#ifdef USE_ON_FLY_SCROLL
701 dont_scroll = FALSE; /* allow scrolling here */
702#endif
703
704 /*
705 * Get a character for Insert mode.
706 */
707 lastc = c; /* remember previous char for CTRL-D */
708 c = safe_vgetc();
709
710#ifdef FEAT_RIGHTLEFT
711 if (p_hkmap && KeyTyped)
712 c = hkmap(c); /* Hebrew mode mapping */
713#endif
714#ifdef FEAT_FKMAP
715 if (p_fkmap && KeyTyped)
716 c = fkmap(c); /* Farsi mode mapping */
717#endif
718
719#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000720 /*
721 * Special handling of keys while the popup menu is visible or wanted
722 * and the cursor is still in the completed word.
723 */
724 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000725 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000726 /* BS: Delete one character from "compl_leader". */
727 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000728 && curwin->w_cursor.col > compl_col
729 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000730 continue;
731
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000732 /* When no match was selected or it was edited. */
733 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000734 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000736 * "compl_leader". Except when at the original match and
737 * there is nothing to add, CTRL-L works like CTRL-P then. */
738 if (c == Ctrl_L
739 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
740 || STRLEN(compl_shown_match->cp_str)
741 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000742 {
743 ins_compl_addfrommatch();
744 continue;
745 }
746
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000747 /* A printable, non-white character: Add to "compl_leader". */
748 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000749 {
750 ins_compl_addleader(c);
751 continue;
752 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000753
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000754 /* Pressing CTRL-Y selects the current match. Shen
755 * compl_enter_selects is set the Enter key does the same. */
756 if (c == Ctrl_Y || (compl_enter_selects
757 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000758 {
759 ins_compl_delete();
760 ins_compl_insert();
761 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000762 }
763 }
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
766 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000767 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000768 if (c != K_IGNORE && ins_compl_prep(c))
769 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771
Bram Moolenaar488c6512005-08-11 20:09:58 +0000772 /* CTRL-\ CTRL-N goes to Normal mode,
773 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
774 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (c == Ctrl_BSL)
776 {
777 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000778 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 ++no_mapping;
780 ++allow_keys;
781 c = safe_vgetc();
782 --no_mapping;
783 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000784 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000786 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 vungetc(c);
788 c = Ctrl_BSL;
789 }
790 else if (c == Ctrl_G && p_im)
791 continue;
792 else
793 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000794 if (c == Ctrl_O)
795 {
796 ins_ctrl_o();
797 ins_at_eol = FALSE; /* cursor keeps its column */
798 nomove = TRUE;
799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 count = 0;
801 goto doESCkey;
802 }
803 }
804
805#ifdef FEAT_DIGRAPHS
806 c = do_digraph(c);
807#endif
808
809#ifdef FEAT_INS_EXPAND
810 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
811 goto docomplete;
812#endif
813 if (c == Ctrl_V || c == Ctrl_Q)
814 {
815 ins_ctrl_v();
816 c = Ctrl_V; /* pretend CTRL-V is last typed character */
817 continue;
818 }
819
820#ifdef FEAT_CINDENT
821 if (cindent_on()
822# ifdef FEAT_INS_EXPAND
823 && ctrl_x_mode == 0
824# endif
825 )
826 {
827 /* A key name preceded by a bang means this key is not to be
828 * inserted. Skip ahead to the re-indenting below.
829 * A key name preceded by a star means that indenting has to be
830 * done before inserting the key. */
831 line_is_white = inindent(0);
832 if (in_cinkeys(c, '!', line_is_white))
833 goto force_cindent;
834 if (can_cindent && in_cinkeys(c, '*', line_is_white)
835 && stop_arrow() == OK)
836 do_c_expr_indent();
837 }
838#endif
839
840#ifdef FEAT_RIGHTLEFT
841 if (curwin->w_p_rl)
842 switch (c)
843 {
844 case K_LEFT: c = K_RIGHT; break;
845 case K_S_LEFT: c = K_S_RIGHT; break;
846 case K_C_LEFT: c = K_C_RIGHT; break;
847 case K_RIGHT: c = K_LEFT; break;
848 case K_S_RIGHT: c = K_S_LEFT; break;
849 case K_C_RIGHT: c = K_C_LEFT; break;
850 }
851#endif
852
853#ifdef FEAT_VISUAL
854 /*
855 * If 'keymodel' contains "startsel", may start selection. If it
856 * does, a CTRL-O and c will be stuffed, we need to get these
857 * characters.
858 */
859 if (ins_start_select(c))
860 continue;
861#endif
862
863 /*
864 * The big switch to handle a character in insert mode.
865 */
866 switch (c)
867 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000868 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (echeck_abbr(ESC + ABBR_OFF))
870 break;
871 /*FALLTHROUGH*/
872
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000873 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874#ifdef FEAT_CMDWIN
875 if (c == Ctrl_C && cmdwin_type != 0)
876 {
877 /* Close the cmdline window. */
878 cmdwin_result = K_IGNORE;
879 got_int = FALSE; /* don't stop executing autocommands et al. */
880 goto doESCkey;
881 }
882#endif
883
884#ifdef UNIX
885do_intr:
886#endif
887 /* when 'insertmode' set, and not halfway a mapping, don't leave
888 * Insert mode */
889 if (goto_im())
890 {
891 if (got_int)
892 {
893 (void)vgetc(); /* flush all buffers */
894 got_int = FALSE;
895 }
896 else
897 vim_beep();
898 break;
899 }
900doESCkey:
901 /*
902 * This is the ONLY return from edit()!
903 */
904 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
905 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000906 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 o_lnum = curwin->w_cursor.lnum;
908
Bram Moolenaar488c6512005-08-11 20:09:58 +0000909 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000910 {
911#ifdef FEAT_AUTOCMD
912 if (cmdchar != 'r' && cmdchar != 'v')
913 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
914 FALSE, curbuf);
915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 continue;
919
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000920 case Ctrl_Z: /* suspend when 'insertmode' set */
921 if (!p_im)
922 goto normalchar; /* insert CTRL-Z as normal char */
923 stuffReadbuff((char_u *)":st\r");
924 c = Ctrl_O;
925 /*FALLTHROUGH*/
926
927 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000928#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000929 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000930 goto docomplete;
931#endif
932 if (echeck_abbr(Ctrl_O + ABBR_OFF))
933 break;
934 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000935
936#ifdef FEAT_VIRTUALEDIT
937 /* don't move the cursor left when 'virtualedit' has "onemore". */
938 if (ve_flags & VE_ONEMORE)
939 {
940 ins_at_eol = FALSE;
941 nomove = TRUE;
942 }
943#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000944 count = 0;
945 goto doESCkey;
946
Bram Moolenaar572cb562005-08-05 21:35:02 +0000947 case K_INS: /* toggle insert/replace mode */
948 case K_KINS:
949 ins_insert(replaceState);
950 break;
951
952 case K_SELECT: /* end of Select mode mapping - ignore */
953 break;
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955#ifdef FEAT_SNIFF
956 case K_SNIFF: /* Sniff command received */
957 stuffcharReadbuff(K_SNIFF);
958 goto doESCkey;
959#endif
960
961 case K_HELP: /* Help key works like <ESC> <Help> */
962 case K_F1:
963 case K_XF1:
964 stuffcharReadbuff(K_HELP);
965 if (p_im)
966 need_start_insertmode = TRUE;
967 goto doESCkey;
968
969#ifdef FEAT_NETBEANS_INTG
970 case K_F21: /* NetBeans command */
971 ++no_mapping; /* don't map the next key hits */
972 i = safe_vgetc();
973 --no_mapping;
974 netbeans_keycommand(i);
975 break;
976#endif
977
978 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 case NUL:
980 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000981 /* For ^@ the trailing ESC will end the insert, unless there is an
982 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
984 && c != Ctrl_A && !p_im)
985 goto doESCkey; /* quit insert mode */
986 inserted_space = FALSE;
987 break;
988
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 ins_reg();
991 auto_format(FALSE, TRUE);
992 inserted_space = FALSE;
993 break;
994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 ins_ctrl_g();
997 break;
998
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000999 case Ctrl_HAT: /* switch input mode and/or langmap */
1000 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 break;
1002
1003#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (!p_ari)
1006 goto normalchar;
1007 ins_ctrl_();
1008 break;
1009#endif
1010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1013 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1014 goto docomplete;
1015#endif
1016 /* FALLTHROUGH */
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019# ifdef FEAT_INS_EXPAND
1020 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1021 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 if (has_compl_option(FALSE))
1023 goto docomplete;
1024 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026# endif
1027 ins_shift(c, lastc);
1028 auto_format(FALSE, TRUE);
1029 inserted_space = FALSE;
1030 break;
1031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001032 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 case K_KDEL:
1034 ins_del();
1035 auto_format(FALSE, TRUE);
1036 break;
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 case Ctrl_H:
1040 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1041 auto_format(FALSE, TRUE);
1042 break;
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1046 auto_format(FALSE, TRUE);
1047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001050# ifdef FEAT_COMPL_FUNC
1051 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001052 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001053 goto docomplete;
1054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1056 auto_format(FALSE, TRUE);
1057 inserted_space = FALSE;
1058 break;
1059
1060#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case K_LEFTMOUSE_NM:
1063 case K_LEFTDRAG:
1064 case K_LEFTRELEASE:
1065 case K_LEFTRELEASE_NM:
1066 case K_MIDDLEMOUSE:
1067 case K_MIDDLEDRAG:
1068 case K_MIDDLERELEASE:
1069 case K_RIGHTMOUSE:
1070 case K_RIGHTDRAG:
1071 case K_RIGHTRELEASE:
1072 case K_X1MOUSE:
1073 case K_X1DRAG:
1074 case K_X1RELEASE:
1075 case K_X2MOUSE:
1076 case K_X2DRAG:
1077 case K_X2RELEASE:
1078 ins_mouse(c);
1079 break;
1080
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 ins_mousescroll(FALSE);
1083 break;
1084
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 ins_mousescroll(TRUE);
1087 break;
1088#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001089#ifdef FEAT_GUI_TABLINE
1090 case K_TABLINE:
1091 case K_TABMENU:
1092 ins_tabline(c);
1093 break;
1094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001096 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 break;
1098
Bram Moolenaar754b5602006-02-09 23:53:20 +00001099#ifdef FEAT_AUTOCMD
1100 case K_CURSORHOLD: /* Didn't type something for a while. */
1101 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1102 did_cursorhold = TRUE;
1103 break;
1104#endif
1105
Bram Moolenaar4770d092006-01-12 23:22:24 +00001106#ifdef FEAT_GUI_W32
1107 /* On Win32 ignore <M-F4>, we get it when closing the window was
1108 * cancelled. */
1109 case K_F4:
1110 if (mod_mask != MOD_MASK_ALT)
1111 goto normalchar;
1112 break;
1113#endif
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#ifdef FEAT_GUI
1116 case K_VER_SCROLLBAR:
1117 ins_scroll();
1118 break;
1119
1120 case K_HOR_SCROLLBAR:
1121 ins_horscroll();
1122 break;
1123#endif
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 case K_S_HOME:
1128 case K_C_HOME:
1129 ins_home(c);
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 case K_S_END:
1135 case K_C_END:
1136 ins_end(c);
1137 break;
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001140 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1141 ins_s_left();
1142 else
1143 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 case K_C_LEFT:
1148 ins_s_left();
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001152 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1153 ins_s_right();
1154 else
1155 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 case K_C_RIGHT:
1160 ins_s_right();
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001164#ifdef FEAT_INS_EXPAND
1165 if (pum_visible())
1166 goto docomplete;
1167#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001168 if (mod_mask & MOD_MASK_SHIFT)
1169 ins_pageup();
1170 else
1171 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_PAGEUP:
1176 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001177#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001178 if (pum_visible())
1179 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 ins_pageup();
1182 break;
1183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001184 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001185#ifdef FEAT_INS_EXPAND
1186 if (pum_visible())
1187 goto docomplete;
1188#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001189 if (mod_mask & MOD_MASK_SHIFT)
1190 ins_pagedown();
1191 else
1192 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 break;
1194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 case K_PAGEDOWN:
1197 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001198#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001199 if (pum_visible())
1200 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 ins_pagedown();
1203 break;
1204
1205#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001206 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 ins_drop();
1208 break;
1209#endif
1210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 c = TAB;
1213 /* FALLTHROUGH */
1214
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001215 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1217 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1218 goto docomplete;
1219#endif
1220 inserted_space = FALSE;
1221 if (ins_tab())
1222 goto normalchar; /* insert TAB as a normal char */
1223 auto_format(FALSE, TRUE);
1224 break;
1225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001226 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 c = CAR;
1228 /* FALLTHROUGH */
1229 case CAR:
1230 case NL:
1231#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1232 /* In a quickfix window a <CR> jumps to the error under the
1233 * cursor. */
1234 if (bt_quickfix(curbuf) && c == CAR)
1235 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001236 if (curwin->w_llist_ref == NULL) /* quickfix window */
1237 do_cmdline_cmd((char_u *)".cc");
1238 else /* location list window */
1239 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 break;
1241 }
1242#endif
1243#ifdef FEAT_CMDWIN
1244 if (cmdwin_type != 0)
1245 {
1246 /* Execute the command in the cmdline window. */
1247 cmdwin_result = CAR;
1248 goto doESCkey;
1249 }
1250#endif
1251 if (ins_eol(c) && !p_im)
1252 goto doESCkey; /* out of memory */
1253 auto_format(FALSE, FALSE);
1254 inserted_space = FALSE;
1255 break;
1256
1257#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259# ifdef FEAT_INS_EXPAND
1260 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1261 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 if (has_compl_option(TRUE))
1263 goto docomplete;
1264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266# endif
1267# ifdef FEAT_DIGRAPHS
1268 c = ins_digraph();
1269 if (c == NUL)
1270 break;
1271# endif
1272 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001276 case Ctrl_X: /* Enter CTRL-X mode */
1277 ins_ctrl_x();
1278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (ctrl_x_mode != CTRL_X_TAGS)
1282 goto normalchar;
1283 goto docomplete;
1284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (ctrl_x_mode != CTRL_X_FILES)
1287 goto normalchar;
1288 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001289
1290 case 's': /* Spelling completion after ^X */
1291 case Ctrl_S:
1292 if (ctrl_x_mode != CTRL_X_SPELL)
1293 goto normalchar;
1294 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295#endif
1296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298#ifdef FEAT_INS_EXPAND
1299 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1300#endif
1301 {
1302 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1303 if (p_im)
1304 {
1305 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1306 break;
1307 goto doESCkey;
1308 }
1309 goto normalchar;
1310 }
1311#ifdef FEAT_INS_EXPAND
1312 /* FALLTHROUGH */
1313
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001314 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 case Ctrl_N:
1316 /* if 'complete' is empty then plain ^P is no longer special,
1317 * but it is under other ^X modes */
1318 if (*curbuf->b_p_cpt == NUL
1319 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 goto normalchar;
1322
1323docomplete:
1324 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001325 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 break;
1327#endif /* FEAT_INS_EXPAND */
1328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001329 case Ctrl_Y: /* copy from previous line or scroll down */
1330 case Ctrl_E: /* copy from next line or scroll up */
1331 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333
1334 default:
1335#ifdef UNIX
1336 if (c == intr_char) /* special interrupt char */
1337 goto do_intr;
1338#endif
1339
1340 /*
1341 * Insert a nomal character.
1342 */
1343normalchar:
1344#ifdef FEAT_SMARTINDENT
1345 /* Try to perform smart-indenting. */
1346 ins_try_si(c);
1347#endif
1348
1349 if (c == ' ')
1350 {
1351 inserted_space = TRUE;
1352#ifdef FEAT_CINDENT
1353 if (inindent(0))
1354 can_cindent = FALSE;
1355#endif
1356 if (Insstart_blank_vcol == MAXCOL
1357 && curwin->w_cursor.lnum == Insstart.lnum)
1358 Insstart_blank_vcol = get_nolist_virtcol();
1359 }
1360
1361 if (vim_iswordc(c) || !echeck_abbr(
1362#ifdef FEAT_MBYTE
1363 /* Add ABBR_OFF for characters above 0x100, this is
1364 * what check_abbr() expects. */
1365 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1366#endif
1367 c))
1368 {
1369 insert_special(c, FALSE, FALSE);
1370#ifdef FEAT_RIGHTLEFT
1371 revins_legal++;
1372 revins_chars++;
1373#endif
1374 }
1375
1376 auto_format(FALSE, TRUE);
1377
1378#ifdef FEAT_FOLDING
1379 /* When inserting a character the cursor line must never be in a
1380 * closed fold. */
1381 foldOpenCursor();
1382#endif
1383 break;
1384 } /* end of switch (c) */
1385
1386 /* If the cursor was moved we didn't just insert a space */
1387 if (arrow_used)
1388 inserted_space = FALSE;
1389
1390#ifdef FEAT_CINDENT
1391 if (can_cindent && cindent_on()
1392# ifdef FEAT_INS_EXPAND
1393 && ctrl_x_mode == 0
1394# endif
1395 )
1396 {
1397force_cindent:
1398 /*
1399 * Indent now if a key was typed that is in 'cinkeys'.
1400 */
1401 if (in_cinkeys(c, ' ', line_is_white))
1402 {
1403 if (stop_arrow() == OK)
1404 /* re-indent the current line */
1405 do_c_expr_indent();
1406 }
1407 }
1408#endif /* FEAT_CINDENT */
1409
1410 } /* for (;;) */
1411 /* NOTREACHED */
1412}
1413
1414/*
1415 * Redraw for Insert mode.
1416 * This is postponed until getting the next character to make '$' in the 'cpo'
1417 * option work correctly.
1418 * Only redraw when there are no characters available. This speeds up
1419 * inserting sequences of characters (e.g., for CTRL-R).
1420 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001421/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001423ins_redraw(ready)
1424 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 if (!char_avail())
1427 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001428#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001429 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1430 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001431 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001432 && !equalpos(last_cursormoved, curwin->w_cursor)
1433# ifdef FEAT_INS_EXPAND
1434 && !pum_visible()
1435# endif
1436 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001437 {
1438 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1439 last_cursormoved = curwin->w_cursor;
1440 }
1441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (must_redraw)
1443 update_screen(0);
1444 else if (clear_cmdline || redraw_cmdline)
1445 showmode(); /* clear cmdline and show mode */
1446 showruler(FALSE);
1447 setcursor();
1448 emsg_on_display = FALSE; /* may remove error message now */
1449 }
1450}
1451
1452/*
1453 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1454 */
1455 static void
1456ins_ctrl_v()
1457{
1458 int c;
1459
1460 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001461 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463 if (redrawing() && !char_avail())
1464 edit_putchar('^', TRUE);
1465 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1466
1467#ifdef FEAT_CMDL_INFO
1468 add_to_showcmd_c(Ctrl_V);
1469#endif
1470
1471 c = get_literal();
1472#ifdef FEAT_CMDL_INFO
1473 clear_showcmd();
1474#endif
1475 insert_special(c, FALSE, TRUE);
1476#ifdef FEAT_RIGHTLEFT
1477 revins_chars++;
1478 revins_legal++;
1479#endif
1480}
1481
1482/*
1483 * Put a character directly onto the screen. It's not stored in a buffer.
1484 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1485 */
1486static int pc_status;
1487#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1488#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1489#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1490#define PC_STATUS_SET 3 /* pc_bytes was filled */
1491#ifdef FEAT_MBYTE
1492static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1493#else
1494static char_u pc_bytes[2]; /* saved bytes */
1495#endif
1496static int pc_attr;
1497static int pc_row;
1498static int pc_col;
1499
1500 void
1501edit_putchar(c, highlight)
1502 int c;
1503 int highlight;
1504{
1505 int attr;
1506
1507 if (ScreenLines != NULL)
1508 {
1509 update_topline(); /* just in case w_topline isn't valid */
1510 validate_cursor();
1511 if (highlight)
1512 attr = hl_attr(HLF_8);
1513 else
1514 attr = 0;
1515 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1516 pc_col = W_WINCOL(curwin);
1517#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1518 pc_status = PC_STATUS_UNSET;
1519#endif
1520#ifdef FEAT_RIGHTLEFT
1521 if (curwin->w_p_rl)
1522 {
1523 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1524# ifdef FEAT_MBYTE
1525 if (has_mbyte)
1526 {
1527 int fix_col = mb_fix_col(pc_col, pc_row);
1528
1529 if (fix_col != pc_col)
1530 {
1531 screen_putchar(' ', pc_row, fix_col, attr);
1532 --curwin->w_wcol;
1533 pc_status = PC_STATUS_RIGHT;
1534 }
1535 }
1536# endif
1537 }
1538 else
1539#endif
1540 {
1541 pc_col += curwin->w_wcol;
1542#ifdef FEAT_MBYTE
1543 if (mb_lefthalve(pc_row, pc_col))
1544 pc_status = PC_STATUS_LEFT;
1545#endif
1546 }
1547
1548 /* save the character to be able to put it back */
1549#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1550 if (pc_status == PC_STATUS_UNSET)
1551#endif
1552 {
1553 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1554 pc_status = PC_STATUS_SET;
1555 }
1556 screen_putchar(c, pc_row, pc_col, attr);
1557 }
1558}
1559
1560/*
1561 * Undo the previous edit_putchar().
1562 */
1563 void
1564edit_unputchar()
1565{
1566 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1567 {
1568#if defined(FEAT_MBYTE)
1569 if (pc_status == PC_STATUS_RIGHT)
1570 ++curwin->w_wcol;
1571 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1572 redrawWinline(curwin->w_cursor.lnum, FALSE);
1573 else
1574#endif
1575 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1576 }
1577}
1578
1579/*
1580 * Called when p_dollar is set: display a '$' at the end of the changed text
1581 * Only works when cursor is in the line that changes.
1582 */
1583 void
1584display_dollar(col)
1585 colnr_T col;
1586{
1587 colnr_T save_col;
1588
1589 if (!redrawing())
1590 return;
1591
1592 cursor_off();
1593 save_col = curwin->w_cursor.col;
1594 curwin->w_cursor.col = col;
1595#ifdef FEAT_MBYTE
1596 if (has_mbyte)
1597 {
1598 char_u *p;
1599
1600 /* If on the last byte of a multi-byte move to the first byte. */
1601 p = ml_get_curline();
1602 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1603 }
1604#endif
1605 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1606 if (curwin->w_wcol < W_WIDTH(curwin))
1607 {
1608 edit_putchar('$', FALSE);
1609 dollar_vcol = curwin->w_virtcol;
1610 }
1611 curwin->w_cursor.col = save_col;
1612}
1613
1614/*
1615 * Call this function before moving the cursor from the normal insert position
1616 * in insert mode.
1617 */
1618 static void
1619undisplay_dollar()
1620{
1621 if (dollar_vcol)
1622 {
1623 dollar_vcol = 0;
1624 redrawWinline(curwin->w_cursor.lnum, FALSE);
1625 }
1626}
1627
1628/*
1629 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1630 * Keep the cursor on the same character.
1631 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1632 * type == INDENT_DEC decrease indent (for CTRL-D)
1633 * type == INDENT_SET set indent to "amount"
1634 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1635 */
1636 void
1637change_indent(type, amount, round, replaced)
1638 int type;
1639 int amount;
1640 int round;
1641 int replaced; /* replaced character, put on replace stack */
1642{
1643 int vcol;
1644 int last_vcol;
1645 int insstart_less; /* reduction for Insstart.col */
1646 int new_cursor_col;
1647 int i;
1648 char_u *ptr;
1649 int save_p_list;
1650 int start_col;
1651 colnr_T vc;
1652#ifdef FEAT_VREPLACE
1653 colnr_T orig_col = 0; /* init for GCC */
1654 char_u *new_line, *orig_line = NULL; /* init for GCC */
1655
1656 /* VREPLACE mode needs to know what the line was like before changing */
1657 if (State & VREPLACE_FLAG)
1658 {
1659 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1660 orig_col = curwin->w_cursor.col;
1661 }
1662#endif
1663
1664 /* for the following tricks we don't want list mode */
1665 save_p_list = curwin->w_p_list;
1666 curwin->w_p_list = FALSE;
1667 vc = getvcol_nolist(&curwin->w_cursor);
1668 vcol = vc;
1669
1670 /*
1671 * For Replace mode we need to fix the replace stack later, which is only
1672 * possible when the cursor is in the indent. Remember the number of
1673 * characters before the cursor if it's possible.
1674 */
1675 start_col = curwin->w_cursor.col;
1676
1677 /* determine offset from first non-blank */
1678 new_cursor_col = curwin->w_cursor.col;
1679 beginline(BL_WHITE);
1680 new_cursor_col -= curwin->w_cursor.col;
1681
1682 insstart_less = curwin->w_cursor.col;
1683
1684 /*
1685 * If the cursor is in the indent, compute how many screen columns the
1686 * cursor is to the left of the first non-blank.
1687 */
1688 if (new_cursor_col < 0)
1689 vcol = get_indent() - vcol;
1690
1691 if (new_cursor_col > 0) /* can't fix replace stack */
1692 start_col = -1;
1693
1694 /*
1695 * Set the new indent. The cursor will be put on the first non-blank.
1696 */
1697 if (type == INDENT_SET)
1698 (void)set_indent(amount, SIN_CHANGED);
1699 else
1700 {
1701#ifdef FEAT_VREPLACE
1702 int save_State = State;
1703
1704 /* Avoid being called recursively. */
1705 if (State & VREPLACE_FLAG)
1706 State = INSERT;
1707#endif
1708 shift_line(type == INDENT_DEC, round, 1);
1709#ifdef FEAT_VREPLACE
1710 State = save_State;
1711#endif
1712 }
1713 insstart_less -= curwin->w_cursor.col;
1714
1715 /*
1716 * Try to put cursor on same character.
1717 * If the cursor is at or after the first non-blank in the line,
1718 * compute the cursor column relative to the column of the first
1719 * non-blank character.
1720 * If we are not in insert mode, leave the cursor on the first non-blank.
1721 * If the cursor is before the first non-blank, position it relative
1722 * to the first non-blank, counted in screen columns.
1723 */
1724 if (new_cursor_col >= 0)
1725 {
1726 /*
1727 * When changing the indent while the cursor is touching it, reset
1728 * Insstart_col to 0.
1729 */
1730 if (new_cursor_col == 0)
1731 insstart_less = MAXCOL;
1732 new_cursor_col += curwin->w_cursor.col;
1733 }
1734 else if (!(State & INSERT))
1735 new_cursor_col = curwin->w_cursor.col;
1736 else
1737 {
1738 /*
1739 * Compute the screen column where the cursor should be.
1740 */
1741 vcol = get_indent() - vcol;
1742 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1743
1744 /*
1745 * Advance the cursor until we reach the right screen column.
1746 */
1747 vcol = last_vcol = 0;
1748 new_cursor_col = -1;
1749 ptr = ml_get_curline();
1750 while (vcol <= (int)curwin->w_virtcol)
1751 {
1752 last_vcol = vcol;
1753#ifdef FEAT_MBYTE
1754 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001755 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 else
1757#endif
1758 ++new_cursor_col;
1759 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1760 }
1761 vcol = last_vcol;
1762
1763 /*
1764 * May need to insert spaces to be able to position the cursor on
1765 * the right screen column.
1766 */
1767 if (vcol != (int)curwin->w_virtcol)
1768 {
1769 curwin->w_cursor.col = new_cursor_col;
1770 i = (int)curwin->w_virtcol - vcol;
1771 ptr = alloc(i + 1);
1772 if (ptr != NULL)
1773 {
1774 new_cursor_col += i;
1775 ptr[i] = NUL;
1776 while (--i >= 0)
1777 ptr[i] = ' ';
1778 ins_str(ptr);
1779 vim_free(ptr);
1780 }
1781 }
1782
1783 /*
1784 * When changing the indent while the cursor is in it, reset
1785 * Insstart_col to 0.
1786 */
1787 insstart_less = MAXCOL;
1788 }
1789
1790 curwin->w_p_list = save_p_list;
1791
1792 if (new_cursor_col <= 0)
1793 curwin->w_cursor.col = 0;
1794 else
1795 curwin->w_cursor.col = new_cursor_col;
1796 curwin->w_set_curswant = TRUE;
1797 changed_cline_bef_curs();
1798
1799 /*
1800 * May have to adjust the start of the insert.
1801 */
1802 if (State & INSERT)
1803 {
1804 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1805 {
1806 if ((int)Insstart.col <= insstart_less)
1807 Insstart.col = 0;
1808 else
1809 Insstart.col -= insstart_less;
1810 }
1811 if ((int)ai_col <= insstart_less)
1812 ai_col = 0;
1813 else
1814 ai_col -= insstart_less;
1815 }
1816
1817 /*
1818 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1819 * If the number of characters before the cursor decreased, need to pop a
1820 * few characters from the replace stack.
1821 * If the number of characters before the cursor increased, need to push a
1822 * few NULs onto the replace stack.
1823 */
1824 if (REPLACE_NORMAL(State) && start_col >= 0)
1825 {
1826 while (start_col > (int)curwin->w_cursor.col)
1827 {
1828 replace_join(0); /* remove a NUL from the replace stack */
1829 --start_col;
1830 }
1831 while (start_col < (int)curwin->w_cursor.col || replaced)
1832 {
1833 replace_push(NUL);
1834 if (replaced)
1835 {
1836 replace_push(replaced);
1837 replaced = NUL;
1838 }
1839 ++start_col;
1840 }
1841 }
1842
1843#ifdef FEAT_VREPLACE
1844 /*
1845 * For VREPLACE mode, we also have to fix the replace stack. In this case
1846 * it is always possible because we backspace over the whole line and then
1847 * put it back again the way we wanted it.
1848 */
1849 if (State & VREPLACE_FLAG)
1850 {
1851 /* If orig_line didn't allocate, just return. At least we did the job,
1852 * even if you can't backspace. */
1853 if (orig_line == NULL)
1854 return;
1855
1856 /* Save new line */
1857 new_line = vim_strsave(ml_get_curline());
1858 if (new_line == NULL)
1859 return;
1860
1861 /* We only put back the new line up to the cursor */
1862 new_line[curwin->w_cursor.col] = NUL;
1863
1864 /* Put back original line */
1865 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1866 curwin->w_cursor.col = orig_col;
1867
1868 /* Backspace from cursor to start of line */
1869 backspace_until_column(0);
1870
1871 /* Insert new stuff into line again */
1872 ins_bytes(new_line);
1873
1874 vim_free(new_line);
1875 }
1876#endif
1877}
1878
1879/*
1880 * Truncate the space at the end of a line. This is to be used only in an
1881 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1882 * modes.
1883 */
1884 void
1885truncate_spaces(line)
1886 char_u *line;
1887{
1888 int i;
1889
1890 /* find start of trailing white space */
1891 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1892 {
1893 if (State & REPLACE_FLAG)
1894 replace_join(0); /* remove a NUL from the replace stack */
1895 }
1896 line[i + 1] = NUL;
1897}
1898
1899#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1900 || defined(FEAT_COMMENTS) || defined(PROTO)
1901/*
1902 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1903 * modes correctly. May also be used when not in insert mode at all.
1904 */
1905 void
1906backspace_until_column(col)
1907 int col;
1908{
1909 while ((int)curwin->w_cursor.col > col)
1910 {
1911 curwin->w_cursor.col--;
1912 if (State & REPLACE_FLAG)
1913 replace_do_bs();
1914 else
1915 (void)del_char(FALSE);
1916 }
1917}
1918#endif
1919
1920#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1921/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001922 * CTRL-X pressed in Insert mode.
1923 */
1924 static void
1925ins_ctrl_x()
1926{
1927 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1928 * CTRL-V works like CTRL-N */
1929 if (ctrl_x_mode != CTRL_X_CMDLINE)
1930 {
1931 /* if the next ^X<> won't ADD nothing, then reset
1932 * compl_cont_status */
1933 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001934 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001935 else
1936 compl_cont_status = 0;
1937 /* We're not sure which CTRL-X mode it will be yet */
1938 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1939 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1940 edit_submode_pre = NULL;
1941 showmode();
1942 }
1943}
1944
1945/*
1946 * Return TRUE if the 'dict' or 'tsr' option can be used.
1947 */
1948 static int
1949has_compl_option(dict_opt)
1950 int dict_opt;
1951{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001952 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001953# ifdef FEAT_SPELL
1954 && !curwin->w_p_spell
1955# endif
1956 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001957 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1958 {
1959 ctrl_x_mode = 0;
1960 edit_submode = NULL;
1961 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1962 : (char_u *)_("'thesaurus' option is empty"),
1963 hl_attr(HLF_E));
1964 if (emsg_silent == 0)
1965 {
1966 vim_beep();
1967 setcursor();
1968 out_flush();
1969 ui_delay(2000L, FALSE);
1970 }
1971 return FALSE;
1972 }
1973 return TRUE;
1974}
1975
1976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1978 * This depends on the current mode.
1979 */
1980 int
1981vim_is_ctrl_x_key(c)
1982 int c;
1983{
1984 /* Always allow ^R - let it's results then be checked */
1985 if (c == Ctrl_R)
1986 return TRUE;
1987
Bram Moolenaare3226be2005-12-18 22:10:00 +00001988 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001989 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001990 return TRUE;
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 switch (ctrl_x_mode)
1993 {
1994 case 0: /* Not in any CTRL-X mode */
1995 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1996 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001997 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1999 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2000 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002001 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2002 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 case CTRL_X_SCROLL:
2004 return (c == Ctrl_Y || c == Ctrl_E);
2005 case CTRL_X_WHOLE_LINE:
2006 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2007 case CTRL_X_FILES:
2008 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2009 case CTRL_X_DICTIONARY:
2010 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2011 case CTRL_X_THESAURUS:
2012 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2013 case CTRL_X_TAGS:
2014 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2015#ifdef FEAT_FIND_ID
2016 case CTRL_X_PATH_PATTERNS:
2017 return (c == Ctrl_P || c == Ctrl_N);
2018 case CTRL_X_PATH_DEFINES:
2019 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2020#endif
2021 case CTRL_X_CMDLINE:
2022 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2023 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002024#ifdef FEAT_COMPL_FUNC
2025 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002026 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002027 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002028 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002029#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002030 case CTRL_X_SPELL:
2031 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033 EMSG(_(e_internal));
2034 return FALSE;
2035}
2036
2037/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002038 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 * case of the originally typed text is used, and the case of the completed
2040 * text is infered, ie this tries to work out what case you probably wanted
2041 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002042 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 */
2044 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002045ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 char_u *str;
2047 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002048 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 char_u *fname;
2050 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002051 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
2053 int has_lower = FALSE;
2054 int was_letter = FALSE;
2055 int idx;
2056
2057 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2058 {
2059 /* Infer case of completed part -- webb */
2060 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002061 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002064 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002066 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 has_lower = TRUE;
2069 if (isupper(IObuff[idx]))
2070 {
2071 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002072 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2074 break;
2075 }
2076 }
2077 }
2078
2079 /*
2080 * Rule 2: No lower case, 2nd consecutive letter converted to
2081 * upper case.
2082 */
2083 if (!has_lower)
2084 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002085 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002087 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 && islower(IObuff[idx]))
2089 {
2090 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002091 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2093 break;
2094 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002095 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 }
2098
2099 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002100 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002102 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2103 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002105 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106}
2107
2108/*
2109 * Add a match to the list of matches.
2110 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002111 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002112 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002114 static int
2115ins_compl_add(str, len, icase, fname, cptext, cdir, flags, dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 char_u *str;
2117 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002118 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002120 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002121 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002122 int flags;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002123 int dup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002125 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002126 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 ui_breakcheck();
2129 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002130 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (len < 0)
2132 len = (int)STRLEN(str);
2133
2134 /*
2135 * If the same match is already present, don't add it.
2136 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002137 if (compl_first_match != NULL && !dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002139 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 do
2141 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002142 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002143 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002144 && match->cp_str[len] == NUL)
2145 return NOTDONE;
2146 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002147 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
2149
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002150 /* Remove any popup menu before changing the list of matches. */
2151 ins_compl_del_pum();
2152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 /*
2154 * Allocate a new match structure.
2155 * Copy the values to the new match structure.
2156 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002157 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002159 return FAIL;
2160 match->cp_number = -1;
2161 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002162 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002163 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002166 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002168 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002171 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2173 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002174 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002175 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002176 && compl_curr_match->cp_fname != NULL
2177 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002178 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002179 else if (fname != NULL)
2180 {
2181 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002182 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002185 match->cp_fname = NULL;
2186 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002187
2188 if (cptext != NULL)
2189 {
2190 int i;
2191
2192 for (i = 0; i < CPT_COUNT; ++i)
2193 if (cptext[i] != NULL && *cptext[i] != NUL)
2194 match->cp_text[i] = vim_strsave(cptext[i]);
2195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 /*
2198 * Link the new match structure in the list of matches.
2199 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002200 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002201 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else if (dir == FORWARD)
2203 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002204 match->cp_next = compl_curr_match->cp_next;
2205 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 else /* BACKWARD */
2208 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002209 match->cp_next = compl_curr_match;
2210 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002212 if (match->cp_next)
2213 match->cp_next->cp_prev = match;
2214 if (match->cp_prev)
2215 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002217 compl_first_match = match;
2218 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002220 /*
2221 * Find the longest common string if still doing that.
2222 */
2223 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2224 ins_compl_longest_match(match);
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 return OK;
2227}
2228
2229/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002230 * Return TRUE if "str[len]" matches with match->cp_str, considering
2231 * match->cp_icase.
2232 */
2233 static int
2234ins_compl_equal(match, str, len)
2235 compl_T *match;
2236 char_u *str;
2237 int len;
2238{
2239 if (match->cp_icase)
2240 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2241 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2242}
2243
2244/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002245 * Reduce the longest common string for match "match".
2246 */
2247 static void
2248ins_compl_longest_match(match)
2249 compl_T *match;
2250{
2251 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002252 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002253 int had_match;
2254
2255 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002256 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002257 /* First match, use it as a whole. */
2258 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002259 if (compl_leader != NULL)
2260 {
2261 had_match = (curwin->w_cursor.col > compl_col);
2262 ins_compl_delete();
2263 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2264 ins_redraw(FALSE);
2265
2266 /* When the match isn't there (to avoid matching itself) remove it
2267 * again after redrawing. */
2268 if (!had_match)
2269 ins_compl_delete();
2270 compl_used_match = FALSE;
2271 }
2272 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002273 else
2274 {
2275 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002276 p = compl_leader;
2277 s = match->cp_str;
2278 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002279 {
2280#ifdef FEAT_MBYTE
2281 if (has_mbyte)
2282 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002283 c1 = mb_ptr2char(p);
2284 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002285 }
2286 else
2287#endif
2288 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002289 c1 = *p;
2290 c2 = *s;
2291 }
2292 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2293 : (c1 != c2))
2294 break;
2295#ifdef FEAT_MBYTE
2296 if (has_mbyte)
2297 {
2298 mb_ptr_adv(p);
2299 mb_ptr_adv(s);
2300 }
2301 else
2302#endif
2303 {
2304 ++p;
2305 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002306 }
2307 }
2308
2309 if (*p != NUL)
2310 {
2311 /* Leader was shortened, need to change the inserted text. */
2312 *p = NUL;
2313 had_match = (curwin->w_cursor.col > compl_col);
2314 ins_compl_delete();
2315 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2316 ins_redraw(FALSE);
2317
2318 /* When the match isn't there (to avoid matching itself) remove it
2319 * again after redrawing. */
2320 if (!had_match)
2321 ins_compl_delete();
2322 }
2323
2324 compl_used_match = FALSE;
2325 }
2326}
2327
2328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 * Add an array of matches to the list of matches.
2330 * Frees matches[].
2331 */
2332 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002333ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int num_matches;
2335 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002336 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
2338 int i;
2339 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002340 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
Bram Moolenaar572cb562005-08-05 21:35:02 +00002342 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002343 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002344 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002346 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 FreeWild(num_matches, matches);
2348}
2349
2350/* Make the completion list cyclic.
2351 * Return the number of matches (excluding the original).
2352 */
2353 static int
2354ins_compl_make_cyclic()
2355{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002356 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int count = 0;
2358
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002359 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 /*
2362 * Find the end of the list.
2363 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002364 match = compl_first_match;
2365 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002366 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002368 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 ++count;
2370 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002371 match->cp_next = compl_first_match;
2372 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374 return count;
2375}
2376
Bram Moolenaara94bc432006-03-10 21:42:59 +00002377/*
2378 * Start completion for the complete() function.
2379 * "startcol" is where the matched text starts (1 is first column).
2380 * "list" is the list of matches.
2381 */
2382 void
2383set_completion(startcol, list)
2384 int startcol;
2385 list_T *list;
2386{
2387 /* If already doing completions stop it. */
2388 if (ctrl_x_mode != 0)
2389 ins_compl_prep(' ');
2390 ins_compl_clear();
2391
2392 if (stop_arrow() == FAIL)
2393 return;
2394
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002395 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002396 startcol = curwin->w_cursor.col;
2397 compl_col = startcol;
2398 compl_length = curwin->w_cursor.col - startcol;
2399 /* compl_pattern doesn't need to be set */
2400 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2401 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002402 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002403 return;
2404
2405 /* Handle like dictionary completion. */
2406 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2407
2408 ins_compl_add_list(list);
2409 compl_matches = ins_compl_make_cyclic();
2410 compl_started = TRUE;
2411 compl_used_match = TRUE;
2412
2413 compl_curr_match = compl_first_match;
2414 ins_complete(Ctrl_N);
2415 out_flush();
2416}
2417
2418
Bram Moolenaar9372a112005-12-06 19:59:18 +00002419/* "compl_match_array" points the currently displayed list of entries in the
2420 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002421static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002422static int compl_match_arraysize;
2423
2424/*
2425 * Update the screen and when there is any scrolling remove the popup menu.
2426 */
2427 static void
2428ins_compl_upd_pum()
2429{
2430 int h;
2431
2432 if (compl_match_array != NULL)
2433 {
2434 h = curwin->w_cline_height;
2435 update_screen(0);
2436 if (h != curwin->w_cline_height)
2437 ins_compl_del_pum();
2438 }
2439}
2440
2441/*
2442 * Remove any popup menu.
2443 */
2444 static void
2445ins_compl_del_pum()
2446{
2447 if (compl_match_array != NULL)
2448 {
2449 pum_undisplay();
2450 vim_free(compl_match_array);
2451 compl_match_array = NULL;
2452 }
2453}
2454
2455/*
2456 * Return TRUE if the popup menu should be displayed.
2457 */
2458 static int
2459pum_wanted()
2460{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002461 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002462 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002463 return FALSE;
2464
2465 /* The display looks bad on a B&W display. */
2466 if (t_colors < 8
2467#ifdef FEAT_GUI
2468 && !gui.in_use
2469#endif
2470 )
2471 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002472 return TRUE;
2473}
2474
2475/*
2476 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002477 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002478 */
2479 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002480pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002481{
2482 compl_T *compl;
2483 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002484
2485 /* Don't display the popup menu if there are no matches or there is only
2486 * one (ignoring the original text). */
2487 compl = compl_first_match;
2488 i = 0;
2489 do
2490 {
2491 if (compl == NULL
2492 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2493 break;
2494 compl = compl->cp_next;
2495 } while (compl != compl_first_match);
2496
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002497 if (strstr((char *)p_cot, "menuone") != NULL)
2498 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002499 return (i >= 2);
2500}
2501
2502/*
2503 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002504 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002505 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002506 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002507ins_compl_show_pum()
2508{
2509 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002510 compl_T *shown_compl = NULL;
2511 int did_find_shown_match = FALSE;
2512 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002513 int i;
2514 int cur = -1;
2515 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002516 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002517
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002518 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002519 return;
2520
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002521#if defined(FEAT_EVAL)
2522 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2523 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2524#endif
2525
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002526 /* Update the screen before drawing the popup menu over it. */
2527 update_screen(0);
2528
2529 if (compl_match_array == NULL)
2530 {
2531 /* Need to build the popup menu list. */
2532 compl_match_arraysize = 0;
2533 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002534 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002535 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002536 do
2537 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002538 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2539 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002541 ++compl_match_arraysize;
2542 compl = compl->cp_next;
2543 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002544 if (compl_match_arraysize == 0)
2545 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002546 compl_match_array = (pumitem_T *)alloc_clear(
2547 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002548 * compl_match_arraysize));
2549 if (compl_match_array != NULL)
2550 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002551 /* If the current match is the original text don't find the first
2552 * match after it, don't highlight anything. */
2553 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2554 shown_match_ok = TRUE;
2555
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002556 i = 0;
2557 compl = compl_first_match;
2558 do
2559 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002560 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2561 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002562 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002563 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002564 if (!shown_match_ok)
2565 {
2566 if (compl == compl_shown_match || did_find_shown_match)
2567 {
2568 /* This item is the shown match or this is the
2569 * first displayed item after the shown match. */
2570 compl_shown_match = compl;
2571 did_find_shown_match = TRUE;
2572 shown_match_ok = TRUE;
2573 }
2574 else
2575 /* Remember this displayed match for when the
2576 * shown match is just below it. */
2577 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002578 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002579 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002580
2581 if (compl->cp_text[CPT_ABBR] != NULL)
2582 compl_match_array[i].pum_text =
2583 compl->cp_text[CPT_ABBR];
2584 else
2585 compl_match_array[i].pum_text = compl->cp_str;
2586 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2587 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2588 if (compl->cp_text[CPT_MENU] != NULL)
2589 compl_match_array[i++].pum_extra =
2590 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591 else
2592 compl_match_array[i++].pum_extra = compl->cp_fname;
2593 }
2594
2595 if (compl == compl_shown_match)
2596 {
2597 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002598
2599 /* When the original text is the shown match don't set
2600 * compl_shown_match. */
2601 if (compl->cp_flags & ORIGINAL_TEXT)
2602 shown_match_ok = TRUE;
2603
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002604 if (!shown_match_ok && shown_compl != NULL)
2605 {
2606 /* The shown match isn't displayed, set it to the
2607 * previously displayed match. */
2608 compl_shown_match = shown_compl;
2609 shown_match_ok = TRUE;
2610 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002611 }
2612 compl = compl->cp_next;
2613 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002614
2615 if (!shown_match_ok) /* no displayed match at all */
2616 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002617 }
2618 }
2619 else
2620 {
2621 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002622 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002623 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2624 || compl_match_array[i].pum_text
2625 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002626 {
2627 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002628 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002629 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002630 }
2631
2632 if (compl_match_array != NULL)
2633 {
2634 /* Compute the screen column of the start of the completed text.
2635 * Use the cursor to get all wrapping and other settings right. */
2636 col = curwin->w_cursor.col;
2637 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002638 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002639 curwin->w_cursor.col = col;
2640 }
2641}
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#define DICT_FIRST (1) /* use just first element in "dict" */
2644#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002647 * Add any identifiers that match the given pattern in the list of dictionary
2648 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 */
2650 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002651ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2652 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002654 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002655 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002657 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 char_u *ptr;
2659 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 char_u **files;
2662 int count;
2663 int i;
2664 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002665 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
Bram Moolenaar0b238792006-03-02 22:49:12 +00002667 if (*dict == NUL)
2668 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002669#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002670 /* When 'dictionary' is empty and spell checking is enabled use
2671 * "spell". */
2672 if (!thesaurus && curwin->w_p_spell)
2673 dict = (char_u *)"spell";
2674 else
2675#endif
2676 return;
2677 }
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002680 if (buf == NULL)
2681 return;
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 /* If 'infercase' is set, don't use 'smartcase' here */
2684 save_p_scs = p_scs;
2685 if (curbuf->b_p_inf)
2686 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002687
2688 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2689 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002690 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002691 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2692 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002693 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2694
2695 if (pat_esc == NULL)
2696 return ;
2697 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002698 ptr = alloc(i);
2699 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002700 {
2701 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002702 return;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002703 }
2704 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2705 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2706 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002707 vim_free(ptr);
2708 }
2709 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002710 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002711 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002712 if (regmatch.regprog == NULL)
2713 goto theend;
2714 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2717 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002718 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 /* copy one dictionary file name into buf */
2721 if (flags == DICT_EXACT)
2722 {
2723 count = 1;
2724 files = &dict;
2725 }
2726 else
2727 {
2728 /* Expand wildcards in the dictionary name, but do not allow
2729 * backticks (for security, the 'dict' option may have been set in
2730 * a modeline). */
2731 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002732# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002733 if (!thesaurus && STRCMP(buf, "spell") == 0)
2734 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002735 else
2736# endif
2737 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 || expand_wildcards(1, &buf, &count, &files,
2739 EW_FILE|EW_SILENT) != OK)
2740 count = 0;
2741 }
2742
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002743# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002744 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002746 /* Complete from active spelling. Skip "\<" in the pattern, we
2747 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002748 if (pat[0] == '\\' && pat[1] == '<')
2749 ptr = pat + 2;
2750 else
2751 ptr = pat;
2752 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002754 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002755# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002756 {
2757 ins_compl_files(count, files, thesaurus, flags,
2758 &regmatch, buf, &dir);
2759 if (flags != DICT_EXACT)
2760 FreeWild(count, files);
2761 }
2762 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 break;
2764 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002765
2766theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 p_scs = save_p_scs;
2768 vim_free(regmatch.regprog);
2769 vim_free(buf);
2770}
2771
Bram Moolenaar0b238792006-03-02 22:49:12 +00002772 static void
2773ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2774 int count;
2775 char_u **files;
2776 int thesaurus;
2777 int flags;
2778 regmatch_T *regmatch;
2779 char_u *buf;
2780 int *dir;
2781{
2782 char_u *ptr;
2783 int i;
2784 FILE *fp;
2785 int add_r;
2786
2787 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2788 {
2789 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2790 if (flags != DICT_EXACT)
2791 {
2792 vim_snprintf((char *)IObuff, IOSIZE,
2793 _("Scanning dictionary: %s"), (char *)files[i]);
2794 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2795 }
2796
2797 if (fp != NULL)
2798 {
2799 /*
2800 * Read dictionary file line by line.
2801 * Check each line for a match.
2802 */
2803 while (!got_int && !compl_interrupted
2804 && !vim_fgets(buf, LSIZE, fp))
2805 {
2806 ptr = buf;
2807 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2808 {
2809 ptr = regmatch->startp[0];
2810 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2811 ptr = find_line_end(ptr);
2812 else
2813 ptr = find_word_end(ptr);
2814 add_r = ins_compl_add_infercase(regmatch->startp[0],
2815 (int)(ptr - regmatch->startp[0]),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002816 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002817 if (thesaurus)
2818 {
2819 char_u *wstart;
2820
2821 /*
2822 * Add the other matches on the line
2823 */
2824 while (!got_int)
2825 {
2826 /* Find start of the next word. Skip white
2827 * space and punctuation. */
2828 ptr = find_word_start(ptr);
2829 if (*ptr == NUL || *ptr == NL)
2830 break;
2831 wstart = ptr;
2832
2833 /* Find end of the word and add it. */
2834#ifdef FEAT_MBYTE
2835 if (has_mbyte)
2836 /* Japanese words may have characters in
2837 * different classes, only separate words
2838 * with single-byte non-word characters. */
2839 while (*ptr != NUL)
2840 {
2841 int l = (*mb_ptr2len)(ptr);
2842
2843 if (l < 2 && !vim_iswordc(*ptr))
2844 break;
2845 ptr += l;
2846 }
2847 else
2848#endif
2849 ptr = find_word_end(ptr);
2850 add_r = ins_compl_add_infercase(wstart,
2851 (int)(ptr - wstart),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002852 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002853 }
2854 }
2855 if (add_r == OK)
2856 /* if dir was BACKWARD then honor it just once */
2857 *dir = FORWARD;
2858 else if (add_r == FAIL)
2859 break;
2860 /* avoid expensive call to vim_regexec() when at end
2861 * of line */
2862 if (*ptr == '\n' || got_int)
2863 break;
2864 }
2865 line_breakcheck();
2866 ins_compl_check_keys(50);
2867 }
2868 fclose(fp);
2869 }
2870 }
2871}
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873/*
2874 * Find the start of the next word.
2875 * Returns a pointer to the first char of the word. Also stops at a NUL.
2876 */
2877 char_u *
2878find_word_start(ptr)
2879 char_u *ptr;
2880{
2881#ifdef FEAT_MBYTE
2882 if (has_mbyte)
2883 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002884 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else
2886#endif
2887 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2888 ++ptr;
2889 return ptr;
2890}
2891
2892/*
2893 * Find the end of the word. Assumes it starts inside a word.
2894 * Returns a pointer to just after the word.
2895 */
2896 char_u *
2897find_word_end(ptr)
2898 char_u *ptr;
2899{
2900#ifdef FEAT_MBYTE
2901 int start_class;
2902
2903 if (has_mbyte)
2904 {
2905 start_class = mb_get_class(ptr);
2906 if (start_class > 1)
2907 while (*ptr != NUL)
2908 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002909 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (mb_get_class(ptr) != start_class)
2911 break;
2912 }
2913 }
2914 else
2915#endif
2916 while (vim_iswordc(*ptr))
2917 ++ptr;
2918 return ptr;
2919}
2920
2921/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002922 * Find the end of the line, omitting CR and NL at the end.
2923 * Returns a pointer to just after the line.
2924 */
2925 static char_u *
2926find_line_end(ptr)
2927 char_u *ptr;
2928{
2929 char_u *s;
2930
2931 s = ptr + STRLEN(ptr);
2932 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2933 --s;
2934 return s;
2935}
2936
2937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 * Free the list of completions
2939 */
2940 static void
2941ins_compl_free()
2942{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002943 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002944 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002946 vim_free(compl_pattern);
2947 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002948 vim_free(compl_leader);
2949 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002951 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002953
2954 ins_compl_del_pum();
2955 pum_clear();
2956
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002957 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 do
2959 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002960 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002961 compl_curr_match = compl_curr_match->cp_next;
2962 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002964 if (match->cp_flags & FREE_FNAME)
2965 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002966 for (i = 0; i < CPT_COUNT; ++i)
2967 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002969 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2970 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
2973 static void
2974ins_compl_clear()
2975{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002976 compl_cont_status = 0;
2977 compl_started = FALSE;
2978 compl_matches = 0;
2979 vim_free(compl_pattern);
2980 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002981 vim_free(compl_leader);
2982 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002984 vim_free(compl_orig_text);
2985 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002986 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988
2989/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002990 * Return TRUE when Insert completion is active.
2991 */
2992 int
2993ins_compl_active()
2994{
2995 return compl_started;
2996}
2997
2998/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002999 * Delete one character before the cursor and show the subset of the matches
3000 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003001 * Returns the character to be used, NUL if the work is done and another char
3002 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003003 */
3004 static int
3005ins_compl_bs()
3006{
3007 char_u *line;
3008 char_u *p;
3009
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003010 line = ml_get_curline();
3011 p = line + curwin->w_cursor.col;
3012 mb_ptr_back(line, p);
3013
3014 /* Stop completion when the whole word was deleted. */
3015 if ((int)(p - line) - (int)compl_col <= 0)
3016 return K_BS;
3017
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003018 /* For redo we need to repeat this backspace. */
3019 AppendCharToRedobuff(K_BS);
3020
3021 /* Deleted more than what was used to find matches or didn't finish
3022 * finding all matches: need to look for matches all over again. */
3023 if (curwin->w_cursor.col <= compl_col + compl_length
3024 || compl_was_interrupted)
3025 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003026
Bram Moolenaara6557602006-02-04 22:43:20 +00003027 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003028 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003029 if (compl_leader != NULL)
3030 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003031 ins_compl_new_leader();
3032 return NUL;
3033 }
3034 return K_BS;
3035}
Bram Moolenaara6557602006-02-04 22:43:20 +00003036
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003037/*
3038 * Called after changing "compl_leader".
3039 * Show the popup menu with a different set of matches.
3040 * May also search for matches again if the previous search was interrupted.
3041 */
3042 static void
3043ins_compl_new_leader()
3044{
3045 ins_compl_del_pum();
3046 ins_compl_delete();
3047 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3048 compl_used_match = FALSE;
3049 compl_enter_selects = FALSE;
3050
3051 if (compl_started)
3052 ins_compl_set_original_text(compl_leader);
3053 else
3054 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003055#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003056 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003057#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003058 /*
3059 * Matches were cleared, need to search for them now. First display
3060 * the changed text before the cursor. Set "compl_restarting" to
3061 * avoid that the first match is inserted.
3062 */
3063 update_screen(0);
3064#ifdef FEAT_GUI
3065 if (gui.in_use)
3066 {
3067 /* Show the cursor after the match, not after the redrawn text. */
3068 setcursor();
3069 out_flush();
3070 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003071 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003072#endif
3073 compl_restarting = TRUE;
3074 if (ins_complete(Ctrl_N) == FAIL)
3075 compl_cont_status = 0;
3076 compl_restarting = FALSE;
3077 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003078
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003079 if (!compl_used_match)
3080 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003081 /* Go to the original text, since none of the matches is inserted. */
3082 if (compl_first_match->cp_prev != NULL
3083 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3084 compl_shown_match = compl_first_match->cp_prev;
3085 else
3086 compl_shown_match = compl_first_match;
3087 compl_curr_match = compl_shown_match;
3088 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003089 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003090
3091 /* Show the popup menu with a different set of matches. */
3092 ins_compl_show_pum();
Bram Moolenaara6557602006-02-04 22:43:20 +00003093}
3094
3095/*
3096 * Append one character to the match leader. May reduce the number of
3097 * matches.
3098 */
3099 static void
3100ins_compl_addleader(c)
3101 int c;
3102{
3103#ifdef FEAT_MBYTE
3104 int cc;
3105
3106 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3107 {
3108 char_u buf[MB_MAXBYTES + 1];
3109
3110 (*mb_char2bytes)(c, buf);
3111 buf[cc] = NUL;
3112 ins_char_bytes(buf, cc);
3113 }
3114 else
3115#endif
3116 ins_char(c);
3117
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003118 /* For redo we need to count this character so that the number of
3119 * backspaces is correct. */
3120 AppendCharToRedobuff(c);
3121
3122 /* If we didn't complete finding matches we must search again. */
3123 if (compl_was_interrupted)
3124 ins_compl_restart();
3125
Bram Moolenaara6557602006-02-04 22:43:20 +00003126 vim_free(compl_leader);
3127 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3128 curwin->w_cursor.col - compl_col);
3129 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003130 ins_compl_new_leader();
3131}
3132
3133/*
3134 * Setup for finding completions again without leaving CTRL-X mode. Used when
3135 * BS or a key was typed while still searching for matches.
3136 */
3137 static void
3138ins_compl_restart()
3139{
3140 ins_compl_free();
3141 compl_started = FALSE;
3142 compl_matches = 0;
3143 compl_cont_status = 0;
3144 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003145}
3146
3147/*
3148 * Set the first match, the original text.
3149 */
3150 static void
3151ins_compl_set_original_text(str)
3152 char_u *str;
3153{
3154 char_u *p;
3155
3156 /* Replace the original text entry. */
3157 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3158 {
3159 p = vim_strsave(str);
3160 if (p != NULL)
3161 {
3162 vim_free(compl_first_match->cp_str);
3163 compl_first_match->cp_str = p;
3164 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003165 }
3166}
3167
3168/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003169 * Append one character to the match leader. May reduce the number of
3170 * matches.
3171 */
3172 static void
3173ins_compl_addfrommatch()
3174{
3175 char_u *p;
3176 int len = curwin->w_cursor.col - compl_col;
3177 int c;
3178
3179 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003180 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003181 return;
3182 p += len;
3183#ifdef FEAT_MBYTE
3184 c = mb_ptr2char(p);
3185#else
3186 c = *p;
3187#endif
3188 ins_compl_addleader(c);
3189}
3190
3191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003193 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003194 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003196 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197ins_compl_prep(c)
3198 int c;
3199{
3200 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 int temp;
3202 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003203 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 /* Forget any previous 'special' messages if this is actually
3206 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3207 */
3208 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3209 edit_submode_extra = NULL;
3210
3211 /* Ignore end of Select mode mapping */
3212 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003213 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003215 /* Set "compl_get_longest" when finding the first matches. */
3216 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3217 || (ctrl_x_mode == 0 && !compl_started))
3218 {
3219 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3220 compl_used_match = TRUE;
3221 }
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3224 {
3225 /*
3226 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3227 * it will be yet. Now we decide.
3228 */
3229 switch (c)
3230 {
3231 case Ctrl_E:
3232 case Ctrl_Y:
3233 ctrl_x_mode = CTRL_X_SCROLL;
3234 if (!(State & REPLACE_FLAG))
3235 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3236 else
3237 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3238 edit_submode_pre = NULL;
3239 showmode();
3240 break;
3241 case Ctrl_L:
3242 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3243 break;
3244 case Ctrl_F:
3245 ctrl_x_mode = CTRL_X_FILES;
3246 break;
3247 case Ctrl_K:
3248 ctrl_x_mode = CTRL_X_DICTIONARY;
3249 break;
3250 case Ctrl_R:
3251 /* Simply allow ^R to happen without affecting ^X mode */
3252 break;
3253 case Ctrl_T:
3254 ctrl_x_mode = CTRL_X_THESAURUS;
3255 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003256#ifdef FEAT_COMPL_FUNC
3257 case Ctrl_U:
3258 ctrl_x_mode = CTRL_X_FUNCTION;
3259 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003260 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003261 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003262 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003263#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003264 case 's':
3265 case Ctrl_S:
3266 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003267#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003268 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003269 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003270 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003271#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 case Ctrl_RSB:
3274 ctrl_x_mode = CTRL_X_TAGS;
3275 break;
3276#ifdef FEAT_FIND_ID
3277 case Ctrl_I:
3278 case K_S_TAB:
3279 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3280 break;
3281 case Ctrl_D:
3282 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3283 break;
3284#endif
3285 case Ctrl_V:
3286 case Ctrl_Q:
3287 ctrl_x_mode = CTRL_X_CMDLINE;
3288 break;
3289 case Ctrl_P:
3290 case Ctrl_N:
3291 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3292 * just started ^X mode, or there were enough ^X's to cancel
3293 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3294 * do normal expansion when interrupting a different mode (say
3295 * ^X^F^X^P or ^P^X^X^P, see below)
3296 * nothing changes if interrupting mode 0, (eg, the flag
3297 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003298 if (!(compl_cont_status & CONT_INTRPT))
3299 compl_cont_status |= CONT_LOCAL;
3300 else if (compl_cont_mode != 0)
3301 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 /* FALLTHROUGH */
3303 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003304 /* If we have typed at least 2 ^X's... for modes != 0, we set
3305 * compl_cont_status = 0 (eg, as if we had just started ^X
3306 * mode).
3307 * For mode 0, we set "compl_cont_mode" to an impossible
3308 * value, in both cases ^X^X can be used to restart the same
3309 * mode (avoiding ADDING mode).
3310 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3311 * 'complete' and local ^P expansions respectively.
3312 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3313 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (c == Ctrl_X)
3315 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003316 if (compl_cont_mode != 0)
3317 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003319 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321 ctrl_x_mode = 0;
3322 edit_submode = NULL;
3323 showmode();
3324 break;
3325 }
3326 }
3327 else if (ctrl_x_mode != 0)
3328 {
3329 /* We're already in CTRL-X mode, do we stay in it? */
3330 if (!vim_is_ctrl_x_key(c))
3331 {
3332 if (ctrl_x_mode == CTRL_X_SCROLL)
3333 ctrl_x_mode = 0;
3334 else
3335 ctrl_x_mode = CTRL_X_FINISHED;
3336 edit_submode = NULL;
3337 }
3338 showmode();
3339 }
3340
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003341 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 /* Show error message from attempted keyword completion (probably
3344 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003345 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003347 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3348 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 || ctrl_x_mode == CTRL_X_FINISHED)
3350 {
3351 /* Get here when we have finished typing a sequence of ^N and
3352 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003353 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003354 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003356 char_u *p;
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003359 * If any of the original typed text has been changed, eg when
3360 * ignorecase is set, we must add back-spaces to the redo
3361 * buffer. We add as few as necessary to delete just the part
3362 * of the original text that has changed.
3363 * When using the longest match, edited the match or used
3364 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003366 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3367 ptr = compl_curr_match->cp_str;
3368 else if (compl_leader != NULL)
3369 ptr = compl_leader;
3370 else
3371 ptr = compl_orig_text;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003372 p = compl_orig_text;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003373 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp]; ++temp)
3374 ;
3375#ifdef FEAT_MBYTE
3376 if (temp > 0)
3377 temp -= (*mb_head_off)(compl_orig_text, p + temp);
3378#endif
3379 for (p += temp; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 AppendCharToRedobuff(K_BS);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003381 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383
3384#ifdef FEAT_CINDENT
3385 want_cindent = (can_cindent && cindent_on());
3386#endif
3387 /*
3388 * When completing whole lines: fix indent for 'cindent'.
3389 * Otherwise, break line if it's too long.
3390 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003391 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393#ifdef FEAT_CINDENT
3394 /* re-indent the current line */
3395 if (want_cindent)
3396 {
3397 do_c_expr_indent();
3398 want_cindent = FALSE; /* don't do it again */
3399 }
3400#endif
3401 }
3402 else
3403 {
3404 /* put the cursor on the last char, for 'tw' formatting */
3405 curwin->w_cursor.col--;
3406 if (stop_arrow() == OK)
3407 insertchar(NUL, 0, -1);
3408 curwin->w_cursor.col++;
3409 }
3410
3411 auto_format(FALSE, TRUE);
3412
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003413 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003414 * the selection without inserting anything. When
3415 * compl_enter_selects is set the Enter key does the same. */
3416 if ((c == Ctrl_Y || (compl_enter_selects
3417 && (c == CAR || c == K_KENTER || c == NL)))
3418 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003419 retval = TRUE;
3420
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003421 /* CTRL-E means completion is Ended, go back to the typed text. */
3422 if (c == Ctrl_E)
3423 {
3424 ins_compl_delete();
3425 if (compl_leader != NULL)
3426 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3427 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003428 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003429 + curwin->w_cursor.col - compl_col);
3430 retval = TRUE;
3431 }
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003434 compl_started = FALSE;
3435 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 msg_clr_cmdline(); /* necessary for "noshowmode" */
3437 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003438 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (edit_submode != NULL)
3440 {
3441 edit_submode = NULL;
3442 showmode();
3443 }
3444
3445#ifdef FEAT_CINDENT
3446 /*
3447 * Indent now if a key was typed that is in 'cinkeys'.
3448 */
3449 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3450 do_c_expr_indent();
3451#endif
3452 }
3453 }
3454
3455 /* reset continue_* if we left expansion-mode, if we stay they'll be
3456 * (re)set properly in ins_complete() */
3457 if (!vim_is_ctrl_x_key(c))
3458 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003459 compl_cont_status = 0;
3460 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003462
3463 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464}
3465
3466/*
3467 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3468 * (depending on flag) starting from buf and looking for a non-scanned
3469 * buffer (other than curbuf). curbuf is special, if it is called with
3470 * buf=curbuf then it has to be the first call for a given flag/expansion.
3471 *
3472 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3473 */
3474 static buf_T *
3475ins_compl_next_buf(buf, flag)
3476 buf_T *buf;
3477 int flag;
3478{
3479#ifdef FEAT_WINDOWS
3480 static win_T *wp;
3481#endif
3482
3483 if (flag == 'w') /* just windows */
3484 {
3485#ifdef FEAT_WINDOWS
3486 if (buf == curbuf) /* first call for this flag/expansion */
3487 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003488 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 && wp->w_buffer->b_scanned)
3490 ;
3491 buf = wp->w_buffer;
3492#else
3493 buf = curbuf;
3494#endif
3495 }
3496 else
3497 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3498 * (unlisted buffers)
3499 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003500 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 && ((flag == 'U'
3502 ? buf->b_p_bl
3503 : (!buf->b_p_bl
3504 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003505 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 ;
3507 return buf;
3508}
3509
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003510#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003511static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003512
3513/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003514 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003515 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003516 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003517 static void
3518expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003519 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003520 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003521{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003522 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003523 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003524 char_u *funcname;
3525 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003526
Bram Moolenaare344bea2005-09-01 20:46:49 +00003527 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3528 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003529 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003530
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003531 /* Call 'completefunc' to obtain the list of matches. */
3532 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003533 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003534
Bram Moolenaare344bea2005-09-01 20:46:49 +00003535 pos = curwin->w_cursor;
3536 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3537 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003538 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003539 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003540
Bram Moolenaara94bc432006-03-10 21:42:59 +00003541 ins_compl_add_list(matchlist);
3542 list_unref(matchlist);
3543}
3544#endif /* FEAT_COMPL_FUNC */
3545
Bram Moolenaar39f05632006-03-19 22:15:26 +00003546#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003547/*
3548 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003549 */
3550 static void
3551ins_compl_add_list(list)
3552 list_T *list;
3553{
3554 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003555 int dir = compl_direction;
3556
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003557 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003558 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003559 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003560 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3561 /* if dir was BACKWARD then honor it just once */
3562 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003563 else if (did_emsg)
3564 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003565 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003566}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003567
3568/*
3569 * Add a match to the list of matches from a typeval_T.
3570 * If the given string is already in the list of completions, then return
3571 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3572 * maybe because alloc() returns NULL, then FAIL is returned.
3573 */
3574 int
3575ins_compl_add_tv(tv, dir)
3576 typval_T *tv;
3577 int dir;
3578{
3579 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003580 int icase = FALSE;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003581 int dup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003582 char_u *(cptext[CPT_COUNT]);
3583
3584 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3585 {
3586 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3587 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3588 (char_u *)"abbr", FALSE);
3589 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3590 (char_u *)"menu", FALSE);
3591 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3592 (char_u *)"kind", FALSE);
3593 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3594 (char_u *)"info", FALSE);
3595 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3596 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003597 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
3598 dup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003599 }
3600 else
3601 {
3602 word = get_tv_string_chk(tv);
3603 vim_memset(cptext, 0, sizeof(cptext));
3604 }
3605 if (word == NULL || *word == NUL)
3606 return FAIL;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003607 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, dup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003608}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003609#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003610
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003611/*
3612 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003613 * The search starts at position "ini" in curbuf and in the direction
3614 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615 * When "compl_started" is FALSE start at that position, otherwise continue
3616 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617 * This may return before finding all the matches.
3618 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
3620 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003621ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
3624 static pos_T first_match_pos;
3625 static pos_T last_match_pos;
3626 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003627 static int found_all = FALSE; /* Found all matches of a
3628 certain type. */
3629 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaar572cb562005-08-05 21:35:02 +00003631 pos_T *pos;
3632 char_u **matches;
3633 int save_p_scs;
3634 int save_p_ws;
3635 int save_p_ic;
3636 int i;
3637 int num_matches;
3638 int len;
3639 int found_new_match;
3640 int type = ctrl_x_mode;
3641 char_u *ptr;
3642 char_u *dict = NULL;
3643 int dict_f = 0;
3644 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003646 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3649 ins_buf->b_scanned = 0;
3650 found_all = FALSE;
3651 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003652 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003653 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 last_match_pos = first_match_pos = *ini;
3655 }
3656
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003658 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3660 for (;;)
3661 {
3662 found_new_match = FAIL;
3663
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * or if found_all says this entry is done. For ^X^L only use the
3666 * entries from 'complete' that look in loaded buffers. */
3667 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003668 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 found_all = FALSE;
3671 while (*e_cpt == ',' || *e_cpt == ' ')
3672 e_cpt++;
3673 if (*e_cpt == '.' && !curbuf->b_scanned)
3674 {
3675 ins_buf = curbuf;
3676 first_match_pos = *ini;
3677 /* So that ^N can match word immediately after cursor */
3678 if (ctrl_x_mode == 0)
3679 dec(&first_match_pos);
3680 last_match_pos = first_match_pos;
3681 type = 0;
3682 }
3683 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3684 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3685 {
3686 /* Scan a buffer, but not the current one. */
3687 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3688 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003689 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 first_match_pos.col = last_match_pos.col = 0;
3691 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3692 last_match_pos.lnum = 0;
3693 type = 0;
3694 }
3695 else /* unloaded buffer, scan like dictionary */
3696 {
3697 found_all = TRUE;
3698 if (ins_buf->b_fname == NULL)
3699 continue;
3700 type = CTRL_X_DICTIONARY;
3701 dict = ins_buf->b_fname;
3702 dict_f = DICT_EXACT;
3703 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003704 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 ins_buf->b_fname == NULL
3706 ? buf_spname(ins_buf)
3707 : ins_buf->b_sfname == NULL
3708 ? (char *)ins_buf->b_fname
3709 : (char *)ins_buf->b_sfname);
3710 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3711 }
3712 else if (*e_cpt == NUL)
3713 break;
3714 else
3715 {
3716 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3717 type = -1;
3718 else if (*e_cpt == 'k' || *e_cpt == 's')
3719 {
3720 if (*e_cpt == 'k')
3721 type = CTRL_X_DICTIONARY;
3722 else
3723 type = CTRL_X_THESAURUS;
3724 if (*++e_cpt != ',' && *e_cpt != NUL)
3725 {
3726 dict = e_cpt;
3727 dict_f = DICT_FIRST;
3728 }
3729 }
3730#ifdef FEAT_FIND_ID
3731 else if (*e_cpt == 'i')
3732 type = CTRL_X_PATH_PATTERNS;
3733 else if (*e_cpt == 'd')
3734 type = CTRL_X_PATH_DEFINES;
3735#endif
3736 else if (*e_cpt == ']' || *e_cpt == 't')
3737 {
3738 type = CTRL_X_TAGS;
3739 sprintf((char*)IObuff, _("Scanning tags."));
3740 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3741 }
3742 else
3743 type = -1;
3744
3745 /* in any case e_cpt is advanced to the next entry */
3746 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3747
3748 found_all = TRUE;
3749 if (type == -1)
3750 continue;
3751 }
3752 }
3753
3754 switch (type)
3755 {
3756 case -1:
3757 break;
3758#ifdef FEAT_FIND_ID
3759 case CTRL_X_PATH_PATTERNS:
3760 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003761 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003764 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3766 (linenr_T)1, (linenr_T)MAXLNUM);
3767 break;
3768#endif
3769
3770 case CTRL_X_DICTIONARY:
3771 case CTRL_X_THESAURUS:
3772 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003773 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 : (type == CTRL_X_THESAURUS
3775 ? (*curbuf->b_p_tsr == NUL
3776 ? p_tsr
3777 : curbuf->b_p_tsr)
3778 : (*curbuf->b_p_dict == NUL
3779 ? p_dict
3780 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003781 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003782 dict != NULL ? dict_f
3783 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 dict = NULL;
3785 break;
3786
3787 case CTRL_X_TAGS:
3788 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3789 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003790 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003793 * of matches is found when compl_pattern is empty */
3794 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3796 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3797 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3798 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00003799 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801 p_ic = save_p_ic;
3802 break;
3803
3804 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003805 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3807 {
3808
3809 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003810 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003811 ins_compl_add_matches(num_matches, matches,
3812#ifdef CASE_INSENSITIVE_FILENAME
3813 TRUE
3814#else
3815 FALSE
3816#endif
3817 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 break;
3820
3821 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003822 if (expand_cmdline(&compl_xp, compl_pattern,
3823 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003825 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 break;
3827
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003828#ifdef FEAT_COMPL_FUNC
3829 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003830 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003831 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003832 break;
3833#endif
3834
Bram Moolenaar488c6512005-08-11 20:09:58 +00003835 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003836#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003837 num_matches = expand_spelling(first_match_pos.lnum,
3838 first_match_pos.col, compl_pattern, &matches);
3839 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003840 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003841#endif
3842 break;
3843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 default: /* normal ^P/^N and ^X^L */
3845 /*
3846 * If 'infercase' is set, don't use 'smartcase' here
3847 */
3848 save_p_scs = p_scs;
3849 if (ins_buf->b_p_inf)
3850 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 /* buffers other than curbuf are scanned from the beginning or the
3853 * end but never from the middle, thus setting nowrapscan in this
3854 * buffers is a good idea, on the other hand, we always set
3855 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3856 save_p_ws = p_ws;
3857 if (ins_buf != curbuf)
3858 p_ws = FALSE;
3859 else if (*e_cpt == '.')
3860 p_ws = TRUE;
3861 for (;;)
3862 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003863 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003865 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3866 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003870 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003872 found_new_match = searchit(NULL, ins_buf, pos,
3873 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003875 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003876 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003878 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003879 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 first_match_pos = *pos;
3881 last_match_pos = *pos;
3882 }
3883 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003884 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 found_new_match = FAIL;
3886 if (found_new_match == FAIL)
3887 {
3888 if (ins_buf == curbuf)
3889 found_all = TRUE;
3890 break;
3891 }
3892
3893 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003894 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 && ini->lnum == pos->lnum
3896 && ini->col == pos->col)
3897 continue;
3898 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3899 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3900 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
3903 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3904 continue;
3905 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3906 if (!p_paste)
3907 ptr = skipwhite(ptr);
3908 }
3909 len = (int)STRLEN(ptr);
3910 }
3911 else
3912 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003913 char_u *tmp_ptr = ptr;
3914
3915 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003917 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 /* Skip if already inside a word. */
3919 if (vim_iswordp(tmp_ptr))
3920 continue;
3921 /* Find start of next word. */
3922 tmp_ptr = find_word_start(tmp_ptr);
3923 }
3924 /* Find end of this word. */
3925 tmp_ptr = find_word_end(tmp_ptr);
3926 len = (int)(tmp_ptr - ptr);
3927
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 if ((compl_cont_status & CONT_ADDING)
3929 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
3931 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3932 {
3933 /* Try next line, if any. the new word will be
3934 * "join" as if the normal command "J" was used.
3935 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003936 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 * works -- Acevedo */
3938 STRNCPY(IObuff, ptr, len);
3939 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3940 tmp_ptr = ptr = skipwhite(ptr);
3941 /* Find start of next word. */
3942 tmp_ptr = find_word_start(tmp_ptr);
3943 /* Find end of next word. */
3944 tmp_ptr = find_word_end(tmp_ptr);
3945 if (tmp_ptr > ptr)
3946 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003947 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003949 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 IObuff[len++] = ' ';
3951 /* IObuf =~ "\k.* ", thus len >= 2 */
3952 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003953 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 || (vim_strchr(p_cpo, CPO_JOINSP)
3955 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003956 && (IObuff[len - 2] == '?'
3957 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 IObuff[len++] = ' ';
3959 }
3960 /* copy as much as posible of the new word */
3961 if (tmp_ptr - ptr >= IOSIZE - len)
3962 tmp_ptr = ptr + IOSIZE - len - 1;
3963 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3964 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003965 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 IObuff[len] = NUL;
3968 ptr = IObuff;
3969 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003970 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 continue;
3972 }
3973 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00003974 if (ins_compl_add_infercase(ptr, len, FALSE,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003975 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003976 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
3978 found_new_match = OK;
3979 break;
3980 }
3981 }
3982 p_scs = save_p_scs;
3983 p_ws = save_p_ws;
3984 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003985
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 /* check if compl_curr_match has changed, (e.g. other type of
3987 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003988 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 found_new_match = OK;
3990
3991 /* break the loop for specialized modes (use 'complete' just for the
3992 * generic ctrl_x_mode == 0) or when we've found a new match */
3993 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003994 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003995 {
3996 if (got_int)
3997 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003998 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003999 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004000 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004001
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004002 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4003 || compl_interrupted)
4004 break;
4005 compl_started = TRUE;
4006 }
4007 else
4008 {
4009 /* Mark a buffer scanned when it has been scanned completely */
4010 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4011 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004013 compl_started = FALSE;
4014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4019 && *e_cpt == NUL) /* Got to end of 'complete' */
4020 found_new_match = FAIL;
4021
4022 i = -1; /* total of matches, unknown */
4023 if (found_new_match == FAIL
4024 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4025 i = ins_compl_make_cyclic();
4026
4027 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004028 * just been made cyclic then we have to move compl_curr_match to the next
4029 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004030 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4031 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004032 if (compl_curr_match == NULL)
4033 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return i;
4035}
4036
4037/* Delete the old text being completed. */
4038 static void
4039ins_compl_delete()
4040{
4041 int i;
4042
4043 /*
4044 * In insert mode: Delete the typed part.
4045 * In replace mode: Put the old characters back, if any.
4046 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 backspace_until_column(i);
4049 changed_cline_bef_curs();
4050}
4051
4052/* Insert the new text being completed. */
4053 static void
4054ins_compl_insert()
4055{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004056 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004057 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4058 compl_used_match = FALSE;
4059 else
4060 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061}
4062
4063/*
4064 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004065 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4066 * get more completions. If it is FALSE, then we just do nothing when there
4067 * are no more completions in a given direction. The latter case is used when
4068 * we are still in the middle of finding completions, to allow browsing
4069 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 * Return the total number of matches, or -1 if still unknown -- webb.
4071 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4073 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 *
4075 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004076 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4077 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 */
4079 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004080ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004082 int count; /* repeat completion this many times; should
4083 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004084 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 int num_matches = -1;
4087 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004088 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004089 compl_T *found_compl = NULL;
4090 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004091 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004093 if (compl_leader != NULL
4094 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004096 /* Set "compl_shown_match" to the actually shown match, it may differ
4097 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004098 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004099 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004100 && compl_shown_match->cp_next != NULL
4101 && compl_shown_match->cp_next != compl_first_match)
4102 compl_shown_match = compl_shown_match->cp_next;
4103 }
4104
4105 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004106 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 /* Delete old text to be replaced */
4108 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004109
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004110 /* When finding the longest common text we stick at the original text,
4111 * don't let CTRL-N or CTRL-P move to the first match. */
4112 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4113
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004114 /* When restarting the search don't insert the first match either. */
4115 if (compl_restarting)
4116 {
4117 advance = FALSE;
4118 compl_restarting = FALSE;
4119 }
4120
Bram Moolenaare3226be2005-12-18 22:10:00 +00004121 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4122 * around. */
4123 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004125 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004127 if (compl_pending != 0)
4128 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004129 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004130 found_end = (compl_first_match != NULL
4131 && (compl_shown_match->cp_next == compl_first_match
4132 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004133 }
4134 else if (compl_shows_dir == BACKWARD
4135 && compl_shown_match->cp_prev != NULL)
4136 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004137 if (compl_pending != 0)
4138 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004139 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004140 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004141 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004144 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004145 if (advance)
4146 {
4147 if (compl_shows_dir == BACKWARD)
4148 --compl_pending;
4149 else
4150 ++compl_pending;
4151 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004152 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004153 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00004154
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004155 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004156 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004157 if (compl_pending != 0 && compl_direction == compl_shows_dir
4158 && advance)
Bram Moolenaara6557602006-02-04 22:43:20 +00004159 compl_shown_match = compl_curr_match;
4160 found_end = FALSE;
4161 }
4162 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4163 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004164 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004165 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004166 ++todo;
4167 else
4168 /* Remember a matching item. */
4169 found_compl = compl_shown_match;
4170
4171 /* Stop at the end of the list when we found a usable match. */
4172 if (found_end)
4173 {
4174 if (found_compl != NULL)
4175 {
4176 compl_shown_match = found_compl;
4177 break;
4178 }
4179 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004183 /* Insert the text of the new completion, or the compl_leader. */
4184 if (insert_match)
4185 {
4186 if (!compl_get_longest || compl_used_match)
4187 ins_compl_insert();
4188 else
4189 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4190 }
4191 else
4192 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
4194 if (!allow_get_expansion)
4195 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004196 /* may undisplay the popup menu first */
4197 ins_compl_upd_pum();
4198
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004199 /* redraw to show the user what was inserted */
4200 update_screen(0);
4201
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004202 /* display the updated popup menu */
4203 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004204#ifdef FEAT_GUI
4205 if (gui.in_use)
4206 {
4207 /* Show the cursor after the match, not after the redrawn text. */
4208 setcursor();
4209 out_flush();
4210 gui_update_cursor(FALSE, FALSE);
4211 }
4212#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 /* Delete old text to be replaced, since we're still searching and
4215 * don't want to match ourselves! */
4216 ins_compl_delete();
4217 }
4218
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004219 /* Enter will select a match when the match wasn't inserted and the popup
4220 * menu is visislbe. */
4221 compl_enter_selects = !insert_match && compl_match_array != NULL;
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 /*
4224 * Show the file name for the match (if any)
4225 * Truncate the file name to avoid a wait for return.
4226 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004227 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004230 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (i <= 0)
4232 i = 0;
4233 else
4234 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004235 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 msg(IObuff);
4237 redraw_cmdline = FALSE; /* don't overwrite! */
4238 }
4239
4240 return num_matches;
4241}
4242
4243/*
4244 * Call this while finding completions, to check whether the user has hit a key
4245 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004246 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004248 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 */
4250 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004251ins_compl_check_keys(frequency)
4252 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 static int count = 0;
4255
4256 int c;
4257
4258 /* Don't check when reading keys from a script. That would break the test
4259 * scripts */
4260 if (using_script())
4261 return;
4262
4263 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004264 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 return;
4266 count = 0;
4267
4268 ++no_mapping;
4269 c = vpeekc_any();
4270 --no_mapping;
4271 if (c != NUL)
4272 {
4273 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4274 {
4275 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004276 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004277 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4278 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004281 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004283 if (compl_pending != 0 && !got_int)
4284 (void)ins_compl_next(FALSE, compl_pending > 0
4285 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004286}
4287
4288/*
4289 * Decide the direction of Insert mode complete from the key typed.
4290 * Returns BACKWARD or FORWARD.
4291 */
4292 static int
4293ins_compl_key2dir(c)
4294 int c;
4295{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004296 if (c == Ctrl_P || c == Ctrl_L
4297 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4298 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004299 return BACKWARD;
4300 return FORWARD;
4301}
4302
4303/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004304 * Return TRUE for keys that are used for completion only when the popup menu
4305 * is visible.
4306 */
4307 static int
4308ins_compl_pum_key(c)
4309 int c;
4310{
4311 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004312 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4313 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004314}
4315
4316/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004317 * Decide the number of completions to move forward.
4318 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4319 */
4320 static int
4321ins_compl_key2count(c)
4322 int c;
4323{
4324 int h;
4325
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004326 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004327 {
4328 h = pum_get_height();
4329 if (h > 3)
4330 h -= 2; /* keep some context */
4331 return h;
4332 }
4333 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334}
4335
4336/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004337 * Return TRUE if completion with "c" should insert the match, FALSE if only
4338 * to change the currently selected completion.
4339 */
4340 static int
4341ins_compl_use_match(c)
4342 int c;
4343{
4344 switch (c)
4345 {
4346 case K_UP:
4347 case K_DOWN:
4348 case K_PAGEDOWN:
4349 case K_KPAGEDOWN:
4350 case K_S_DOWN:
4351 case K_PAGEUP:
4352 case K_KPAGEUP:
4353 case K_S_UP:
4354 return FALSE;
4355 }
4356 return TRUE;
4357}
4358
4359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 * Do Insert mode completion.
4361 * Called when character "c" was typed, which has a meaning for completion.
4362 * Returns OK if completion was done, FAIL if something failed (out of mem).
4363 */
4364 static int
4365ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004366 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 char_u *line;
4369 int startcol = 0; /* column where searched text starts */
4370 colnr_T curs_col; /* cursor column */
4371 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
Bram Moolenaare3226be2005-12-18 22:10:00 +00004373 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
4376 /* First time we hit ^N or ^P (in a row, I mean) */
4377
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 did_ai = FALSE;
4379#ifdef FEAT_SMARTINDENT
4380 did_si = FALSE;
4381 can_si = FALSE;
4382 can_si_back = FALSE;
4383#endif
4384 if (stop_arrow() == FAIL)
4385 return FAIL;
4386
4387 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004389 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 * "compl_startpos" to the cursor as a pattern to add a new word
4393 * instead of expand the one before the cursor, in word-wise if
4394 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 * is not in the same line as the cursor then fix it (the line has
4396 * been split because it was longer than 'tw'). if SOL is set then
4397 * skip the previous pattern, a word at the beginning of the line has
4398 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004399 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4400 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402 /*
4403 * it is a continued search
4404 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4407 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4408 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411 /* line (probably) wrapped, set compl_startpos to the
4412 * first non_blank in the line, if it is not a wordchar
4413 * include it to get a better pattern, but then we don't
4414 * want the "\\<" prefix, check it bellow */
4415 compl_col = (colnr_T)(skipwhite(line) - line);
4416 compl_startpos.col = compl_col;
4417 compl_startpos.lnum = curwin->w_cursor.lnum;
4418 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 else
4421 {
4422 /* S_IPOS was set when we inserted a word that was at the
4423 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 * mode but first we need to redefine compl_startpos */
4425 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 compl_cont_status |= CONT_SOL;
4428 compl_startpos.col = (colnr_T)(skipwhite(
4429 line + compl_length
4430 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004435 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 * have enough space? just being paranoic */
4437#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440 compl_cont_status &= ~CONT_SOL;
4441 compl_length = (IOSIZE - MIN_SPACE);
4442 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4445 if (compl_length < 1)
4446 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 compl_cont_status = 0;
4461 compl_cont_status |= CONT_N_ADDS;
4462 compl_startpos = curwin->w_cursor;
4463 startcol = (int)curs_col;
4464 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466
4467 /* Work out completion pattern and original text -- webb */
4468 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4469 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4472 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004473 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004477 compl_col += ++startcol;
4478 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004481 compl_pattern = str_foldcase(line + compl_col,
4482 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004484 compl_pattern = vim_strnsave(line + compl_col,
4485 compl_length);
4486 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return FAIL;
4488 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 char_u *prefix = (char_u *)"\\<";
4492
4493 /* we need 3 extra chars, 1 for the NUL and
4494 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004495 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4496 compl_length) + 3);
4497 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004499 if (!vim_iswordp(line + compl_col)
4500 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 && (
4502#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#endif
4507 )))
4508 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004509 STRCPY((char *)compl_pattern, prefix);
4510 (void)quote_meta(compl_pattern + STRLEN(prefix),
4511 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004515 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004517 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#endif
4519 )
4520 {
4521 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4523 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004525 compl_col += curs_col;
4526 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 else
4529 {
4530#ifdef FEAT_MBYTE
4531 /* Search the point of change class of multibyte character
4532 * or not a word single byte character backward. */
4533 if (has_mbyte)
4534 {
4535 int base_class;
4536 int head_off;
4537
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 startcol -= (*mb_head_off)(line, line + startcol);
4539 base_class = mb_get_class(line + startcol);
4540 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542 head_off = (*mb_head_off)(line, line + startcol);
4543 if (base_class != mb_get_class(line + startcol
4544 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 }
4549 else
4550#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553 compl_col += ++startcol;
4554 compl_length = (int)curs_col - startcol;
4555 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 /* Only match word with at least two chars -- webb
4558 * there's no need to call quote_meta,
4559 * alloc(7) is enough -- Acevedo
4560 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004561 compl_pattern = alloc(7);
4562 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 STRCPY((char *)compl_pattern, "\\<");
4565 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4566 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568 else
4569 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4571 compl_length) + 3);
4572 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 STRCPY((char *)compl_pattern, "\\<");
4575 (void)quote_meta(compl_pattern + 2, line + compl_col,
4576 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 }
4579 }
4580 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4581 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004582 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 compl_length = (int)curs_col - (int)compl_col;
4584 if (compl_length < 0) /* cursor in indent: empty pattern */
4585 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587 compl_pattern = str_foldcase(line + compl_col, compl_length,
4588 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4591 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 return FAIL;
4593 }
4594 else if (ctrl_x_mode == CTRL_X_FILES)
4595 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598 compl_col += ++startcol;
4599 compl_length = (int)curs_col - startcol;
4600 compl_pattern = addstar(line + compl_col, compl_length,
4601 EXPAND_FILES);
4602 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 return FAIL;
4604 }
4605 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4606 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607 compl_pattern = vim_strnsave(line, curs_col);
4608 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 set_cmd_context(&compl_xp, compl_pattern,
4611 (int)STRLEN(compl_pattern), curs_col);
4612 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4613 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004615 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4616 compl_col = startcol;
4617 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004619 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004620 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004621#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004622 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004623 * Call user defined function 'completefunc' with "a:findstart"
4624 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004625 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004626 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004627 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004628 char_u *funcname;
4629 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004630
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004631 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004632 * string */
4633 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4634 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4635 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004636 {
4637 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4638 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004639 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004640 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004641
4642 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004643 args[1] = NULL;
4644 pos = curwin->w_cursor;
4645 col = call_func_retnr(funcname, 2, args, FALSE);
4646 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004647
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004648 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004649 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004650 compl_col = col;
4651 if ((colnr_T)compl_col > curs_col)
4652 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004653
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 /* Setup variables for completion. Need to obtain "line" again,
4655 * it may have become invalid. */
4656 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004657 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4659 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004660#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 return FAIL;
4662 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004663 else if (ctrl_x_mode == CTRL_X_SPELL)
4664 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004665#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004666 if (spell_bad_len > 0)
4667 compl_col = curs_col - spell_bad_len;
4668 else
4669 compl_col = spell_word_start(startcol);
4670 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004671 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004672 spell_expand_check_cap(compl_col);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004673 /* Need to obtain "line" again, it may have become invalid. */
4674 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004675 compl_length = (int)curs_col - compl_col;
4676 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4677 if (compl_pattern == NULL)
4678#endif
4679 return FAIL;
4680 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004681 else
4682 {
4683 EMSG2(_(e_intern2), "ins_complete()");
4684 return FAIL;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 edit_submode_pre = (char_u *)_(" Adding");
4690 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4691 {
4692 /* Insert a new line, keep indentation but ignore 'comments' */
4693#ifdef FEAT_COMMENTS
4694 char_u *old = curbuf->b_p_com;
4695
4696 curbuf->b_p_com = (char_u *)"";
4697#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004698 compl_startpos.lnum = curwin->w_cursor.lnum;
4699 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 ins_eol('\r');
4701#ifdef FEAT_COMMENTS
4702 curbuf->b_p_com = old;
4703#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004704 compl_length = 0;
4705 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 }
4708 else
4709 {
4710 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004711 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 if (compl_cont_status & CONT_LOCAL)
4715 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 else
4717 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4718
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004719 /* Always add completion for the original text. */
4720 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004721 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4722 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004723 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 vim_free(compl_pattern);
4726 compl_pattern = NULL;
4727 vim_free(compl_orig_text);
4728 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FAIL;
4730 }
4731
4732 /* showmode might reset the internal line pointers, so it must
4733 * be called before line = ml_get(), or when this address is no
4734 * longer needed. -- Acevedo.
4735 */
4736 edit_submode_extra = (char_u *)_("-- Searching...");
4737 edit_submode_highl = HLF_COUNT;
4738 showmode();
4739 edit_submode_extra = NULL;
4740 out_flush();
4741 }
4742
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004743 compl_shown_match = compl_curr_match;
4744 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004747 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004749 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004751 /* may undisplay the popup menu */
4752 ins_compl_upd_pum();
4753
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 if (n > 1) /* all matches have been found */
4755 compl_matches = n;
4756 compl_curr_match = compl_shown_match;
4757 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaard68071d2006-05-02 22:08:30 +00004759 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4760 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (got_int && !global_busy)
4762 {
4763 (void)vgetc();
4764 got_int = FALSE;
4765 }
4766
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004768 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004770 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4771 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4773 edit_submode_highl = HLF_E;
4774 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4775 * because we couldn't expand anything at first place, but if we used
4776 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4777 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004778 if ( compl_length > 1
4779 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 || (ctrl_x_mode != 0
4781 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4782 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004783 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785
Bram Moolenaar572cb562005-08-05 21:35:02 +00004786 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004789 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791 if (edit_submode_extra == NULL)
4792 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004793 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
4795 edit_submode_extra = (char_u *)_("Back at original");
4796 edit_submode_highl = HLF_W;
4797 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
4800 edit_submode_extra = (char_u *)_("Word from other line");
4801 edit_submode_highl = HLF_COUNT;
4802 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004803 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 edit_submode_extra = (char_u *)_("The only match");
4806 edit_submode_highl = HLF_COUNT;
4807 }
4808 else
4809 {
4810 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004811 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004813 int number = 0;
4814 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004816 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 /* search backwards for the first valid (!= -1) number.
4819 * This should normally succeed already at the first loop
4820 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004821 for (match = compl_curr_match->cp_prev; match != NULL
4822 && match != compl_first_match;
4823 match = match->cp_prev)
4824 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004826 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 break;
4828 }
4829 if (match != NULL)
4830 /* go up and assign all numbers which are not assigned
4831 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004832 for (match = match->cp_next;
4833 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004834 match = match->cp_next)
4835 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 else /* BACKWARD */
4838 {
4839 /* search forwards (upwards) for the first valid (!= -1)
4840 * number. This should normally succeed already at the
4841 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004842 for (match = compl_curr_match->cp_next; match != NULL
4843 && match != compl_first_match;
4844 match = match->cp_next)
4845 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004847 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 break;
4849 }
4850 if (match != NULL)
4851 /* go down and assign all numbers which are not
4852 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004853 for (match = match->cp_prev; match
4854 && match->cp_number == -1;
4855 match = match->cp_prev)
4856 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 }
4859
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004860 /* The match should always have a sequence number now, this is
4861 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004862 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 /* Space for 10 text chars. + 2x10-digit no.s */
4865 static char_u match_ref[31];
4866
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004867 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004869 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004872 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004873 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 edit_submode_extra = match_ref;
4875 edit_submode_highl = HLF_R;
4876 if (dollar_vcol)
4877 curs_columns(FALSE);
4878 }
4879 }
4880 }
4881
4882 /* Show a message about what (completion) mode we're in. */
4883 showmode();
4884 if (edit_submode_extra != NULL)
4885 {
4886 if (!p_smd)
4887 msg_attr(edit_submode_extra,
4888 edit_submode_highl < HLF_COUNT
4889 ? hl_attr(edit_submode_highl) : 0);
4890 }
4891 else
4892 msg_clr_cmdline(); /* necessary for "noshowmode" */
4893
Bram Moolenaard68071d2006-05-02 22:08:30 +00004894 /* Show the popup menu, unless we got interrupted. */
4895 if (!compl_interrupted)
4896 {
4897 /* RedrawingDisabled may be set when invoked through complete(). */
4898 n = RedrawingDisabled;
4899 RedrawingDisabled = 0;
4900 ins_compl_show_pum();
4901 setcursor();
4902 RedrawingDisabled = n;
4903 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004904 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004905 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return OK;
4908}
4909
4910/*
4911 * Looks in the first "len" chars. of "src" for search-metachars.
4912 * If dest is not NULL the chars. are copied there quoting (with
4913 * a backslash) the metachars, and dest would be NUL terminated.
4914 * Returns the length (needed) of dest
4915 */
4916 static int
4917quote_meta(dest, src, len)
4918 char_u *dest;
4919 char_u *src;
4920 int len;
4921{
4922 int m;
4923
4924 for (m = len; --len >= 0; src++)
4925 {
4926 switch (*src)
4927 {
4928 case '.':
4929 case '*':
4930 case '[':
4931 if (ctrl_x_mode == CTRL_X_DICTIONARY
4932 || ctrl_x_mode == CTRL_X_THESAURUS)
4933 break;
4934 case '~':
4935 if (!p_magic) /* quote these only if magic is set */
4936 break;
4937 case '\\':
4938 if (ctrl_x_mode == CTRL_X_DICTIONARY
4939 || ctrl_x_mode == CTRL_X_THESAURUS)
4940 break;
4941 case '^': /* currently it's not needed. */
4942 case '$':
4943 m++;
4944 if (dest != NULL)
4945 *dest++ = '\\';
4946 break;
4947 }
4948 if (dest != NULL)
4949 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004950# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 /* Copy remaining bytes of a multibyte character. */
4952 if (has_mbyte)
4953 {
4954 int i, mb_len;
4955
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004956 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 if (mb_len > 0 && len >= mb_len)
4958 for (i = 0; i < mb_len; ++i)
4959 {
4960 --len;
4961 ++src;
4962 if (dest != NULL)
4963 *dest++ = *src;
4964 }
4965 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 if (dest != NULL)
4969 *dest = NUL;
4970
4971 return m;
4972}
4973#endif /* FEAT_INS_EXPAND */
4974
4975/*
4976 * Next character is interpreted literally.
4977 * A one, two or three digit decimal number is interpreted as its byte value.
4978 * If one or two digits are entered, the next character is given to vungetc().
4979 * For Unicode a character > 255 may be returned.
4980 */
4981 int
4982get_literal()
4983{
4984 int cc;
4985 int nc;
4986 int i;
4987 int hex = FALSE;
4988 int octal = FALSE;
4989#ifdef FEAT_MBYTE
4990 int unicode = 0;
4991#endif
4992
4993 if (got_int)
4994 return Ctrl_C;
4995
4996#ifdef FEAT_GUI
4997 /*
4998 * In GUI there is no point inserting the internal code for a special key.
4999 * It is more useful to insert the string "<KEY>" instead. This would
5000 * probably be useful in a text window too, but it would not be
5001 * vi-compatible (maybe there should be an option for it?) -- webb
5002 */
5003 if (gui.in_use)
5004 ++allow_keys;
5005#endif
5006#ifdef USE_ON_FLY_SCROLL
5007 dont_scroll = TRUE; /* disallow scrolling here */
5008#endif
5009 ++no_mapping; /* don't map the next key hits */
5010 cc = 0;
5011 i = 0;
5012 for (;;)
5013 {
5014 do
5015 nc = safe_vgetc();
5016 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5017 || nc == K_HOR_SCROLLBAR);
5018#ifdef FEAT_CMDL_INFO
5019 if (!(State & CMDLINE)
5020# ifdef FEAT_MBYTE
5021 && MB_BYTE2LEN_CHECK(nc) == 1
5022# endif
5023 )
5024 add_to_showcmd(nc);
5025#endif
5026 if (nc == 'x' || nc == 'X')
5027 hex = TRUE;
5028 else if (nc == 'o' || nc == 'O')
5029 octal = TRUE;
5030#ifdef FEAT_MBYTE
5031 else if (nc == 'u' || nc == 'U')
5032 unicode = nc;
5033#endif
5034 else
5035 {
5036 if (hex
5037#ifdef FEAT_MBYTE
5038 || unicode != 0
5039#endif
5040 )
5041 {
5042 if (!vim_isxdigit(nc))
5043 break;
5044 cc = cc * 16 + hex2nr(nc);
5045 }
5046 else if (octal)
5047 {
5048 if (nc < '0' || nc > '7')
5049 break;
5050 cc = cc * 8 + nc - '0';
5051 }
5052 else
5053 {
5054 if (!VIM_ISDIGIT(nc))
5055 break;
5056 cc = cc * 10 + nc - '0';
5057 }
5058
5059 ++i;
5060 }
5061
5062 if (cc > 255
5063#ifdef FEAT_MBYTE
5064 && unicode == 0
5065#endif
5066 )
5067 cc = 255; /* limit range to 0-255 */
5068 nc = 0;
5069
5070 if (hex) /* hex: up to two chars */
5071 {
5072 if (i >= 2)
5073 break;
5074 }
5075#ifdef FEAT_MBYTE
5076 else if (unicode) /* Unicode: up to four or eight chars */
5077 {
5078 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5079 break;
5080 }
5081#endif
5082 else if (i >= 3) /* decimal or octal: up to three chars */
5083 break;
5084 }
5085 if (i == 0) /* no number entered */
5086 {
5087 if (nc == K_ZERO) /* NUL is stored as NL */
5088 {
5089 cc = '\n';
5090 nc = 0;
5091 }
5092 else
5093 {
5094 cc = nc;
5095 nc = 0;
5096 }
5097 }
5098
5099 if (cc == 0) /* NUL is stored as NL */
5100 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005101#ifdef FEAT_MBYTE
5102 if (enc_dbcs && (cc & 0xff) == 0)
5103 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5104 second byte will cause trouble! */
5105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107 --no_mapping;
5108#ifdef FEAT_GUI
5109 if (gui.in_use)
5110 --allow_keys;
5111#endif
5112 if (nc)
5113 vungetc(nc);
5114 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5115 return cc;
5116}
5117
5118/*
5119 * Insert character, taking care of special keys and mod_mask
5120 */
5121 static void
5122insert_special(c, allow_modmask, ctrlv)
5123 int c;
5124 int allow_modmask;
5125 int ctrlv; /* c was typed after CTRL-V */
5126{
5127 char_u *p;
5128 int len;
5129
5130 /*
5131 * Special function key, translate into "<Key>". Up to the last '>' is
5132 * inserted with ins_str(), so as not to replace characters in replace
5133 * mode.
5134 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5135 * unless 'allow_modmask' is TRUE.
5136 */
5137#ifdef MACOS
5138 /* Command-key never produces a normal key */
5139 if (mod_mask & MOD_MASK_CMD)
5140 allow_modmask = TRUE;
5141#endif
5142 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5143 {
5144 p = get_special_key_name(c, mod_mask);
5145 len = (int)STRLEN(p);
5146 c = p[len - 1];
5147 if (len > 2)
5148 {
5149 if (stop_arrow() == FAIL)
5150 return;
5151 p[len - 1] = NUL;
5152 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005153 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 ctrlv = FALSE;
5155 }
5156 }
5157 if (stop_arrow() == OK)
5158 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5159}
5160
5161/*
5162 * Special characters in this context are those that need processing other
5163 * than the simple insertion that can be performed here. This includes ESC
5164 * which terminates the insert, and CR/NL which need special processing to
5165 * open up a new line. This routine tries to optimize insertions performed by
5166 * the "redo", "undo" or "put" commands, so it needs to know when it should
5167 * stop and defer processing to the "normal" mechanism.
5168 * '0' and '^' are special, because they can be followed by CTRL-D.
5169 */
5170#ifdef EBCDIC
5171# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5172#else
5173# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5174#endif
5175
5176#ifdef FEAT_MBYTE
5177# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5178#else
5179# define WHITECHAR(cc) vim_iswhite(cc)
5180#endif
5181
5182 void
5183insertchar(c, flags, second_indent)
5184 int c; /* character to insert or NUL */
5185 int flags; /* INSCHAR_FORMAT, etc. */
5186 int second_indent; /* indent for second line if >= 0 */
5187{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 int textwidth;
5189#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5195 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
5197 /*
5198 * Try to break the line in two or more pieces when:
5199 * - Always do this if we have been called to do formatting only.
5200 * - Always do this when 'formatoptions' has the 'a' flag and the line
5201 * ends in white space.
5202 * - Otherwise:
5203 * - Don't do this if inserting a blank
5204 * - Don't do this if an existing character is being replaced, unless
5205 * we're in VREPLACE mode.
5206 * - Do this if the cursor is not on the line where insert started
5207 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5208 * before the insert.
5209 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5210 * before 'textwidth'
5211 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005212 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 && ((flags & INSCHAR_FORMAT)
5214 || (!vim_iswhite(c)
5215 && !((State & REPLACE_FLAG)
5216#ifdef FEAT_VREPLACE
5217 && !(State & VREPLACE_FLAG)
5218#endif
5219 && *ml_get_cursor() != NUL)
5220 && (curwin->w_cursor.lnum != Insstart.lnum
5221 || ((!has_format_option(FO_INS_LONG)
5222 || Insstart_textlen <= (colnr_T)textwidth)
5223 && (!fo_ins_blank
5224 || Insstart_blank_vcol <= (colnr_T)textwidth
5225 ))))))
5226 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005227 /* Format with 'formatexpr' when it's set. Use internal formatting
5228 * when 'formatexpr' isn't set or it returns non-zero. */
5229#if defined(FEAT_EVAL)
5230 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005231 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005233 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (c == NUL) /* only formatting was wanted */
5237 return;
5238
5239#ifdef FEAT_COMMENTS
5240 /* Check whether this character should end a comment. */
5241 if (did_ai && (int)c == end_comment_pending)
5242 {
5243 char_u *line;
5244 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5245 int middle_len, end_len;
5246 int i;
5247
5248 /*
5249 * Need to remove existing (middle) comment leader and insert end
5250 * comment leader. First, check what comment leader we can find.
5251 */
5252 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5253 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5254 {
5255 /* Skip middle-comment string */
5256 while (*p && p[-1] != ':') /* find end of middle flags */
5257 ++p;
5258 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5259 /* Don't count trailing white space for middle_len */
5260 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5261 --middle_len;
5262
5263 /* Find the end-comment string */
5264 while (*p && p[-1] != ':') /* find end of end flags */
5265 ++p;
5266 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5267
5268 /* Skip white space before the cursor */
5269 i = curwin->w_cursor.col;
5270 while (--i >= 0 && vim_iswhite(line[i]))
5271 ;
5272 i++;
5273
5274 /* Skip to before the middle leader */
5275 i -= middle_len;
5276
5277 /* Check some expected things before we go on */
5278 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5279 {
5280 /* Backspace over all the stuff we want to replace */
5281 backspace_until_column(i);
5282
5283 /*
5284 * Insert the end-comment string, except for the last
5285 * character, which will get inserted as normal later.
5286 */
5287 ins_bytes_len(lead_end, end_len - 1);
5288 }
5289 }
5290 }
5291 end_comment_pending = NUL;
5292#endif
5293
5294 did_ai = FALSE;
5295#ifdef FEAT_SMARTINDENT
5296 did_si = FALSE;
5297 can_si = FALSE;
5298 can_si_back = FALSE;
5299#endif
5300
5301 /*
5302 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5303 * This speeds up normal text input considerably.
5304 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5305 * need to re-indent at a ':', or any other character (but not what
5306 * 'paste' is set)..
5307 */
5308#ifdef USE_ON_FLY_SCROLL
5309 dont_scroll = FALSE; /* allow scrolling here */
5310#endif
5311
5312 if ( !ISSPECIAL(c)
5313#ifdef FEAT_MBYTE
5314 && (!has_mbyte || (*mb_char2len)(c) == 1)
5315#endif
5316 && vpeekc() != NUL
5317 && !(State & REPLACE_FLAG)
5318#ifdef FEAT_CINDENT
5319 && !cindent_on()
5320#endif
5321#ifdef FEAT_RIGHTLEFT
5322 && !p_ri
5323#endif
5324 )
5325 {
5326#define INPUT_BUFLEN 100
5327 char_u buf[INPUT_BUFLEN + 1];
5328 int i;
5329 colnr_T virtcol = 0;
5330
5331 buf[0] = c;
5332 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005333 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 virtcol = get_nolist_virtcol();
5335 /*
5336 * Stop the string when:
5337 * - no more chars available
5338 * - finding a special character (command key)
5339 * - buffer is full
5340 * - running into the 'textwidth' boundary
5341 * - need to check for abbreviation: A non-word char after a word-char
5342 */
5343 while ( (c = vpeekc()) != NUL
5344 && !ISSPECIAL(c)
5345#ifdef FEAT_MBYTE
5346 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5347#endif
5348 && i < INPUT_BUFLEN
5349 && (textwidth == 0
5350 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5351 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5352 {
5353#ifdef FEAT_RIGHTLEFT
5354 c = vgetc();
5355 if (p_hkmap && KeyTyped)
5356 c = hkmap(c); /* Hebrew mode mapping */
5357# ifdef FEAT_FKMAP
5358 if (p_fkmap && KeyTyped)
5359 c = fkmap(c); /* Farsi mode mapping */
5360# endif
5361 buf[i++] = c;
5362#else
5363 buf[i++] = vgetc();
5364#endif
5365 }
5366
5367#ifdef FEAT_DIGRAPHS
5368 do_digraph(-1); /* clear digraphs */
5369 do_digraph(buf[i-1]); /* may be the start of a digraph */
5370#endif
5371 buf[i] = NUL;
5372 ins_str(buf);
5373 if (flags & INSCHAR_CTRLV)
5374 {
5375 redo_literal(*buf);
5376 i = 1;
5377 }
5378 else
5379 i = 0;
5380 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005381 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 else
5384 {
5385#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005386 int cc;
5387
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5389 {
5390 char_u buf[MB_MAXBYTES + 1];
5391
5392 (*mb_char2bytes)(c, buf);
5393 buf[cc] = NUL;
5394 ins_char_bytes(buf, cc);
5395 AppendCharToRedobuff(c);
5396 }
5397 else
5398#endif
5399 {
5400 ins_char(c);
5401 if (flags & INSCHAR_CTRLV)
5402 redo_literal(c);
5403 else
5404 AppendCharToRedobuff(c);
5405 }
5406 }
5407}
5408
5409/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005410 * Format text at the current insert position.
5411 */
5412 static void
5413internal_format(textwidth, second_indent, flags, format_only)
5414 int textwidth;
5415 int second_indent;
5416 int flags;
5417 int format_only;
5418{
5419 int cc;
5420 int save_char = NUL;
5421 int haveto_redraw = FALSE;
5422 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5423#ifdef FEAT_MBYTE
5424 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5425#endif
5426 int fo_white_par = has_format_option(FO_WHITE_PAR);
5427 int first_line = TRUE;
5428#ifdef FEAT_COMMENTS
5429 colnr_T leader_len;
5430 int no_leader = FALSE;
5431 int do_comments = (flags & INSCHAR_DO_COM);
5432#endif
5433
5434 /*
5435 * When 'ai' is off we don't want a space under the cursor to be
5436 * deleted. Replace it with an 'x' temporarily.
5437 */
5438 if (!curbuf->b_p_ai)
5439 {
5440 cc = gchar_cursor();
5441 if (vim_iswhite(cc))
5442 {
5443 save_char = cc;
5444 pchar_cursor('x');
5445 }
5446 }
5447
5448 /*
5449 * Repeat breaking lines, until the current line is not too long.
5450 */
5451 while (!got_int)
5452 {
5453 int startcol; /* Cursor column at entry */
5454 int wantcol; /* column at textwidth border */
5455 int foundcol; /* column for start of spaces */
5456 int end_foundcol = 0; /* column for start of word */
5457 colnr_T len;
5458 colnr_T virtcol;
5459#ifdef FEAT_VREPLACE
5460 int orig_col = 0;
5461 char_u *saved_text = NULL;
5462#endif
5463 colnr_T col;
5464
5465 virtcol = get_nolist_virtcol();
5466 if (virtcol < (colnr_T)textwidth)
5467 break;
5468
5469#ifdef FEAT_COMMENTS
5470 if (no_leader)
5471 do_comments = FALSE;
5472 else if (!(flags & INSCHAR_FORMAT)
5473 && has_format_option(FO_WRAP_COMS))
5474 do_comments = TRUE;
5475
5476 /* Don't break until after the comment leader */
5477 if (do_comments)
5478 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5479 else
5480 leader_len = 0;
5481
5482 /* If the line doesn't start with a comment leader, then don't
5483 * start one in a following broken line. Avoids that a %word
5484 * moved to the start of the next line causes all following lines
5485 * to start with %. */
5486 if (leader_len == 0)
5487 no_leader = TRUE;
5488#endif
5489 if (!(flags & INSCHAR_FORMAT)
5490#ifdef FEAT_COMMENTS
5491 && leader_len == 0
5492#endif
5493 && !has_format_option(FO_WRAP))
5494
5495 {
5496 textwidth = 0;
5497 break;
5498 }
5499 if ((startcol = curwin->w_cursor.col) == 0)
5500 break;
5501
5502 /* find column of textwidth border */
5503 coladvance((colnr_T)textwidth);
5504 wantcol = curwin->w_cursor.col;
5505
5506 curwin->w_cursor.col = startcol - 1;
5507#ifdef FEAT_MBYTE
5508 /* Correct cursor for multi-byte character. */
5509 if (has_mbyte)
5510 mb_adjust_cursor();
5511#endif
5512 foundcol = 0;
5513
5514 /*
5515 * Find position to break at.
5516 * Stop at first entered white when 'formatoptions' has 'v'
5517 */
5518 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5519 || curwin->w_cursor.lnum != Insstart.lnum
5520 || curwin->w_cursor.col >= Insstart.col)
5521 {
5522 cc = gchar_cursor();
5523 if (WHITECHAR(cc))
5524 {
5525 /* remember position of blank just before text */
5526 end_foundcol = curwin->w_cursor.col;
5527
5528 /* find start of sequence of blanks */
5529 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5530 {
5531 dec_cursor();
5532 cc = gchar_cursor();
5533 }
5534 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5535 break; /* only spaces in front of text */
5536#ifdef FEAT_COMMENTS
5537 /* Don't break until after the comment leader */
5538 if (curwin->w_cursor.col < leader_len)
5539 break;
5540#endif
5541 if (has_format_option(FO_ONE_LETTER))
5542 {
5543 /* do not break after one-letter words */
5544 if (curwin->w_cursor.col == 0)
5545 break; /* one-letter word at begin */
5546
5547 col = curwin->w_cursor.col;
5548 dec_cursor();
5549 cc = gchar_cursor();
5550
5551 if (WHITECHAR(cc))
5552 continue; /* one-letter, continue */
5553 curwin->w_cursor.col = col;
5554 }
5555#ifdef FEAT_MBYTE
5556 if (has_mbyte)
5557 foundcol = curwin->w_cursor.col
5558 + (*mb_ptr2len)(ml_get_cursor());
5559 else
5560#endif
5561 foundcol = curwin->w_cursor.col + 1;
5562 if (curwin->w_cursor.col < (colnr_T)wantcol)
5563 break;
5564 }
5565#ifdef FEAT_MBYTE
5566 else if (cc >= 0x100 && fo_multibyte
5567 && curwin->w_cursor.col <= (colnr_T)wantcol)
5568 {
5569 /* Break after or before a multi-byte character. */
5570 foundcol = curwin->w_cursor.col;
5571 if (curwin->w_cursor.col < (colnr_T)wantcol)
5572 foundcol += (*mb_char2len)(cc);
5573 end_foundcol = foundcol;
5574 break;
5575 }
5576#endif
5577 if (curwin->w_cursor.col == 0)
5578 break;
5579 dec_cursor();
5580 }
5581
5582 if (foundcol == 0) /* no spaces, cannot break line */
5583 {
5584 curwin->w_cursor.col = startcol;
5585 break;
5586 }
5587
5588 /* Going to break the line, remove any "$" now. */
5589 undisplay_dollar();
5590
5591 /*
5592 * Offset between cursor position and line break is used by replace
5593 * stack functions. VREPLACE does not use this, and backspaces
5594 * over the text instead.
5595 */
5596#ifdef FEAT_VREPLACE
5597 if (State & VREPLACE_FLAG)
5598 orig_col = startcol; /* Will start backspacing from here */
5599 else
5600#endif
5601 replace_offset = startcol - end_foundcol - 1;
5602
5603 /*
5604 * adjust startcol for spaces that will be deleted and
5605 * characters that will remain on top line
5606 */
5607 curwin->w_cursor.col = foundcol;
5608 while (cc = gchar_cursor(), WHITECHAR(cc))
5609 inc_cursor();
5610 startcol -= curwin->w_cursor.col;
5611 if (startcol < 0)
5612 startcol = 0;
5613
5614#ifdef FEAT_VREPLACE
5615 if (State & VREPLACE_FLAG)
5616 {
5617 /*
5618 * In VREPLACE mode, we will backspace over the text to be
5619 * wrapped, so save a copy now to put on the next line.
5620 */
5621 saved_text = vim_strsave(ml_get_cursor());
5622 curwin->w_cursor.col = orig_col;
5623 if (saved_text == NULL)
5624 break; /* Can't do it, out of memory */
5625 saved_text[startcol] = NUL;
5626
5627 /* Backspace over characters that will move to the next line */
5628 if (!fo_white_par)
5629 backspace_until_column(foundcol);
5630 }
5631 else
5632#endif
5633 {
5634 /* put cursor after pos. to break line */
5635 if (!fo_white_par)
5636 curwin->w_cursor.col = foundcol;
5637 }
5638
5639 /*
5640 * Split the line just before the margin.
5641 * Only insert/delete lines, but don't really redraw the window.
5642 */
5643 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5644 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5645#ifdef FEAT_COMMENTS
5646 + (do_comments ? OPENLINE_DO_COM : 0)
5647#endif
5648 , old_indent);
5649 old_indent = 0;
5650
5651 replace_offset = 0;
5652 if (first_line)
5653 {
5654 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5655 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5656 if (second_indent >= 0)
5657 {
5658#ifdef FEAT_VREPLACE
5659 if (State & VREPLACE_FLAG)
5660 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5661 else
5662#endif
5663 (void)set_indent(second_indent, SIN_CHANGED);
5664 }
5665 first_line = FALSE;
5666 }
5667
5668#ifdef FEAT_VREPLACE
5669 if (State & VREPLACE_FLAG)
5670 {
5671 /*
5672 * In VREPLACE mode we have backspaced over the text to be
5673 * moved, now we re-insert it into the new line.
5674 */
5675 ins_bytes(saved_text);
5676 vim_free(saved_text);
5677 }
5678 else
5679#endif
5680 {
5681 /*
5682 * Check if cursor is not past the NUL off the line, cindent
5683 * may have added or removed indent.
5684 */
5685 curwin->w_cursor.col += startcol;
5686 len = (colnr_T)STRLEN(ml_get_curline());
5687 if (curwin->w_cursor.col > len)
5688 curwin->w_cursor.col = len;
5689 }
5690
5691 haveto_redraw = TRUE;
5692#ifdef FEAT_CINDENT
5693 can_cindent = TRUE;
5694#endif
5695 /* moved the cursor, don't autoindent or cindent now */
5696 did_ai = FALSE;
5697#ifdef FEAT_SMARTINDENT
5698 did_si = FALSE;
5699 can_si = FALSE;
5700 can_si_back = FALSE;
5701#endif
5702 line_breakcheck();
5703 }
5704
5705 if (save_char != NUL) /* put back space after cursor */
5706 pchar_cursor(save_char);
5707
5708 if (!format_only && haveto_redraw)
5709 {
5710 update_topline();
5711 redraw_curbuf_later(VALID);
5712 }
5713}
5714
5715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 * Called after inserting or deleting text: When 'formatoptions' includes the
5717 * 'a' flag format from the current line until the end of the paragraph.
5718 * Keep the cursor at the same position relative to the text.
5719 * The caller must have saved the cursor line for undo, following ones will be
5720 * saved here.
5721 */
5722 void
5723auto_format(trailblank, prev_line)
5724 int trailblank; /* when TRUE also format with trailing blank */
5725 int prev_line; /* may start in previous line */
5726{
5727 pos_T pos;
5728 colnr_T len;
5729 char_u *old;
5730 char_u *new, *pnew;
5731 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005732 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 if (!has_format_option(FO_AUTO))
5735 return;
5736
5737 pos = curwin->w_cursor;
5738 old = ml_get_curline();
5739
5740 /* may remove added space */
5741 check_auto_format(FALSE);
5742
5743 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5744 * user might insert normal text next. Also skip formatting when "1" is
5745 * in 'formatoptions' and there is a single character before the cursor.
5746 * Otherwise the line would be broken and when typing another non-white
5747 * next they are not joined back together. */
5748 wasatend = (pos.col == STRLEN(old));
5749 if (*old != NUL && !trailblank && wasatend)
5750 {
5751 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005752 cc = gchar_cursor();
5753 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5754 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005756 cc = gchar_cursor();
5757 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
5759 curwin->w_cursor = pos;
5760 return;
5761 }
5762 curwin->w_cursor = pos;
5763 }
5764
5765#ifdef FEAT_COMMENTS
5766 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5767 * comments. */
5768 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5769 && get_leader_len(old, NULL, FALSE) == 0)
5770 return;
5771#endif
5772
5773 /*
5774 * May start formatting in a previous line, so that after "x" a word is
5775 * moved to the previous line if it fits there now. Only when this is not
5776 * the start of a paragraph.
5777 */
5778 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5779 {
5780 --curwin->w_cursor.lnum;
5781 if (u_save_cursor() == FAIL)
5782 return;
5783 }
5784
5785 /*
5786 * Do the formatting and restore the cursor position. "saved_cursor" will
5787 * be adjusted for the text formatting.
5788 */
5789 saved_cursor = pos;
5790 format_lines((linenr_T)-1);
5791 curwin->w_cursor = saved_cursor;
5792 saved_cursor.lnum = 0;
5793
5794 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5795 {
5796 /* "cannot happen" */
5797 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5798 coladvance((colnr_T)MAXCOL);
5799 }
5800 else
5801 check_cursor_col();
5802
5803 /* Insert mode: If the cursor is now after the end of the line while it
5804 * previously wasn't, the line was broken. Because of the rule above we
5805 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5806 * formatted. */
5807 if (!wasatend && has_format_option(FO_WHITE_PAR))
5808 {
5809 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005810 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 if (curwin->w_cursor.col == len)
5812 {
5813 pnew = vim_strnsave(new, len + 2);
5814 pnew[len] = ' ';
5815 pnew[len + 1] = NUL;
5816 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5817 /* remove the space later */
5818 did_add_space = TRUE;
5819 }
5820 else
5821 /* may remove added space */
5822 check_auto_format(FALSE);
5823 }
5824
5825 check_cursor();
5826}
5827
5828/*
5829 * When an extra space was added to continue a paragraph for auto-formatting,
5830 * delete it now. The space must be under the cursor, just after the insert
5831 * position.
5832 */
5833 static void
5834check_auto_format(end_insert)
5835 int end_insert; /* TRUE when ending Insert mode */
5836{
5837 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005838 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
5840 if (did_add_space)
5841 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005842 cc = gchar_cursor();
5843 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 /* Somehow the space was removed already. */
5845 did_add_space = FALSE;
5846 else
5847 {
5848 if (!end_insert)
5849 {
5850 inc_cursor();
5851 c = gchar_cursor();
5852 dec_cursor();
5853 }
5854 if (c != NUL)
5855 {
5856 /* The space is no longer at the end of the line, delete it. */
5857 del_char(FALSE);
5858 did_add_space = FALSE;
5859 }
5860 }
5861 }
5862}
5863
5864/*
5865 * Find out textwidth to be used for formatting:
5866 * if 'textwidth' option is set, use it
5867 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5868 * if invalid value, use 0.
5869 * Set default to window width (maximum 79) for "gq" operator.
5870 */
5871 int
5872comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005873 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 int textwidth;
5876
5877 textwidth = curbuf->b_p_tw;
5878 if (textwidth == 0 && curbuf->b_p_wm)
5879 {
5880 /* The width is the window width minus 'wrapmargin' minus all the
5881 * things that add to the margin. */
5882 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5883#ifdef FEAT_CMDWIN
5884 if (cmdwin_type != 0)
5885 textwidth -= 1;
5886#endif
5887#ifdef FEAT_FOLDING
5888 textwidth -= curwin->w_p_fdc;
5889#endif
5890#ifdef FEAT_SIGNS
5891 if (curwin->w_buffer->b_signlist != NULL
5892# ifdef FEAT_NETBEANS_INTG
5893 || usingNetbeans
5894# endif
5895 )
5896 textwidth -= 1;
5897#endif
5898 if (curwin->w_p_nu)
5899 textwidth -= 8;
5900 }
5901 if (textwidth < 0)
5902 textwidth = 0;
5903 if (ff && textwidth == 0)
5904 {
5905 textwidth = W_WIDTH(curwin) - 1;
5906 if (textwidth > 79)
5907 textwidth = 79;
5908 }
5909 return textwidth;
5910}
5911
5912/*
5913 * Put a character in the redo buffer, for when just after a CTRL-V.
5914 */
5915 static void
5916redo_literal(c)
5917 int c;
5918{
5919 char_u buf[10];
5920
5921 /* Only digits need special treatment. Translate them into a string of
5922 * three digits. */
5923 if (VIM_ISDIGIT(c))
5924 {
5925 sprintf((char *)buf, "%03d", c);
5926 AppendToRedobuff(buf);
5927 }
5928 else
5929 AppendCharToRedobuff(c);
5930}
5931
5932/*
5933 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005934 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 */
5936 static void
5937start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005938 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 if (!arrow_used) /* something has been inserted */
5941 {
5942 AppendToRedobuff(ESC_STR);
5943 stop_insert(end_insert_pos, FALSE);
5944 arrow_used = TRUE; /* this means we stopped the current insert */
5945 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005946#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005947 check_spell_redraw();
5948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949}
5950
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005951#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005952/*
5953 * If we skipped highlighting word at cursor, do it now.
5954 * It may be skipped again, thus reset spell_redraw_lnum first.
5955 */
5956 static void
5957check_spell_redraw()
5958{
5959 if (spell_redraw_lnum != 0)
5960 {
5961 linenr_T lnum = spell_redraw_lnum;
5962
5963 spell_redraw_lnum = 0;
5964 redrawWinline(lnum, FALSE);
5965 }
5966}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005967
5968/*
5969 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5970 * spelled word, if there is one.
5971 */
5972 static void
5973spell_back_to_badword()
5974{
5975 pos_T tpos = curwin->w_cursor;
5976
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005977 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005978 if (curwin->w_cursor.col != tpos.col)
5979 start_arrow(&tpos);
5980}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005981#endif
5982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983/*
5984 * stop_arrow() is called before a change is made in insert mode.
5985 * If an arrow key has been used, start a new insertion.
5986 * Returns FAIL if undo is impossible, shouldn't insert then.
5987 */
5988 int
5989stop_arrow()
5990{
5991 if (arrow_used)
5992 {
5993 if (u_save_cursor() == OK)
5994 {
5995 arrow_used = FALSE;
5996 ins_need_undo = FALSE;
5997 }
5998 Insstart = curwin->w_cursor; /* new insertion starts here */
5999 Insstart_textlen = linetabsize(ml_get_curline());
6000 ai_col = 0;
6001#ifdef FEAT_VREPLACE
6002 if (State & VREPLACE_FLAG)
6003 {
6004 orig_line_count = curbuf->b_ml.ml_line_count;
6005 vr_lines_changed = 1;
6006 }
6007#endif
6008 ResetRedobuff();
6009 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006010 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 }
6012 else if (ins_need_undo)
6013 {
6014 if (u_save_cursor() == OK)
6015 ins_need_undo = FALSE;
6016 }
6017
6018#ifdef FEAT_FOLDING
6019 /* Always open fold at the cursor line when inserting something. */
6020 foldOpenCursor();
6021#endif
6022
6023 return (arrow_used || ins_need_undo ? FAIL : OK);
6024}
6025
6026/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006027 * Do a few things to stop inserting.
6028 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6029 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 */
6031 static void
6032stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006033 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006034 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006036 int cc;
6037 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039 stop_redo_ins();
6040 replace_flush(); /* abandon replace stack */
6041
6042 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006043 * Save the inserted text for later redo with ^@ and CTRL-A.
6044 * Don't do it when "restart_edit" was set and nothing was inserted,
6045 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006047 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006048 if (did_restart_edit == 0 || (ptr != NULL
6049 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006050 {
6051 vim_free(last_insert);
6052 last_insert = ptr;
6053 last_insert_skip = new_insert_skip;
6054 }
6055 else
6056 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006058 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
6060 /* Auto-format now. It may seem strange to do this when stopping an
6061 * insertion (or moving the cursor), but it's required when appending
6062 * a line and having it end in a space. But only do it when something
6063 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006064 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006066 pos_T tpos = curwin->w_cursor;
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 /* When the cursor is at the end of the line after a space the
6069 * formatting will move it to the following word. Avoid that by
6070 * moving the cursor onto the space. */
6071 cc = 'x';
6072 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6073 {
6074 dec_cursor();
6075 cc = gchar_cursor();
6076 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006077 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 }
6079
6080 auto_format(TRUE, FALSE);
6081
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006082 if (vim_iswhite(cc))
6083 {
6084 if (gchar_cursor() != NUL)
6085 inc_cursor();
6086#ifdef FEAT_VIRTUALEDIT
6087 /* If the cursor is still at the same character, also keep
6088 * the "coladd". */
6089 if (gchar_cursor() == NUL
6090 && curwin->w_cursor.lnum == tpos.lnum
6091 && curwin->w_cursor.col == tpos.col)
6092 curwin->w_cursor.coladd = tpos.coladd;
6093#endif
6094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 }
6096
6097 /* If a space was inserted for auto-formatting, remove it now. */
6098 check_auto_format(TRUE);
6099
6100 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006101 * of the line, and put the cursor back.
6102 * Do this when ESC was used or moving the cursor up/down. */
6103 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006104 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006106 pos_T tpos = curwin->w_cursor;
6107
6108 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006109 for (;;)
6110 {
6111 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6112 --curwin->w_cursor.col;
6113 cc = gchar_cursor();
6114 if (!vim_iswhite(cc))
6115 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006117 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006118 if (curwin->w_cursor.lnum != tpos.lnum)
6119 curwin->w_cursor = tpos;
6120 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6122
6123#ifdef FEAT_VISUAL
6124 /* <C-S-Right> may have started Visual mode, adjust the position for
6125 * deleted characters. */
6126 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6127 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006128 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (VIsual.col > (colnr_T)cc)
6130 {
6131 VIsual.col = cc;
6132# ifdef FEAT_VIRTUALEDIT
6133 VIsual.coladd = 0;
6134# endif
6135 }
6136 }
6137#endif
6138 }
6139 }
6140 did_ai = FALSE;
6141#ifdef FEAT_SMARTINDENT
6142 did_si = FALSE;
6143 can_si = FALSE;
6144 can_si_back = FALSE;
6145#endif
6146
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006147 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6148 * now in a different buffer. */
6149 if (end_insert_pos != NULL)
6150 {
6151 curbuf->b_op_start = Insstart;
6152 curbuf->b_op_end = *end_insert_pos;
6153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154}
6155
6156/*
6157 * Set the last inserted text to a single character.
6158 * Used for the replace command.
6159 */
6160 void
6161set_last_insert(c)
6162 int c;
6163{
6164 char_u *s;
6165
6166 vim_free(last_insert);
6167#ifdef FEAT_MBYTE
6168 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6169#else
6170 last_insert = alloc(6);
6171#endif
6172 if (last_insert != NULL)
6173 {
6174 s = last_insert;
6175 /* Use the CTRL-V only when entering a special char */
6176 if (c < ' ' || c == DEL)
6177 *s++ = Ctrl_V;
6178 s = add_char2buf(c, s);
6179 *s++ = ESC;
6180 *s++ = NUL;
6181 last_insert_skip = 0;
6182 }
6183}
6184
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006185#if defined(EXITFREE) || defined(PROTO)
6186 void
6187free_last_insert()
6188{
6189 vim_free(last_insert);
6190 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006191 vim_free(compl_orig_text);
6192 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006193}
6194#endif
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196/*
6197 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6198 * and CSI. Handle multi-byte characters.
6199 * Returns a pointer to after the added bytes.
6200 */
6201 char_u *
6202add_char2buf(c, s)
6203 int c;
6204 char_u *s;
6205{
6206#ifdef FEAT_MBYTE
6207 char_u temp[MB_MAXBYTES];
6208 int i;
6209 int len;
6210
6211 len = (*mb_char2bytes)(c, temp);
6212 for (i = 0; i < len; ++i)
6213 {
6214 c = temp[i];
6215#endif
6216 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6217 if (c == K_SPECIAL)
6218 {
6219 *s++ = K_SPECIAL;
6220 *s++ = KS_SPECIAL;
6221 *s++ = KE_FILLER;
6222 }
6223#ifdef FEAT_GUI
6224 else if (c == CSI)
6225 {
6226 *s++ = CSI;
6227 *s++ = KS_EXTRA;
6228 *s++ = (int)KE_CSI;
6229 }
6230#endif
6231 else
6232 *s++ = c;
6233#ifdef FEAT_MBYTE
6234 }
6235#endif
6236 return s;
6237}
6238
6239/*
6240 * move cursor to start of line
6241 * if flags & BL_WHITE move to first non-white
6242 * if flags & BL_SOL move to first non-white if startofline is set,
6243 * otherwise keep "curswant" column
6244 * if flags & BL_FIX don't leave the cursor on a NUL.
6245 */
6246 void
6247beginline(flags)
6248 int flags;
6249{
6250 if ((flags & BL_SOL) && !p_sol)
6251 coladvance(curwin->w_curswant);
6252 else
6253 {
6254 curwin->w_cursor.col = 0;
6255#ifdef FEAT_VIRTUALEDIT
6256 curwin->w_cursor.coladd = 0;
6257#endif
6258
6259 if (flags & (BL_WHITE | BL_SOL))
6260 {
6261 char_u *ptr;
6262
6263 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6264 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6265 ++curwin->w_cursor.col;
6266 }
6267 curwin->w_set_curswant = TRUE;
6268 }
6269}
6270
6271/*
6272 * oneright oneleft cursor_down cursor_up
6273 *
6274 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006275 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 * Return OK when successful, FAIL when we hit a line of file boundary.
6277 */
6278
6279 int
6280oneright()
6281{
6282 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285#ifdef FEAT_VIRTUALEDIT
6286 if (virtual_active())
6287 {
6288 pos_T prevpos = curwin->w_cursor;
6289
6290 /* Adjust for multi-wide char (excluding TAB) */
6291 ptr = ml_get_cursor();
6292 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006293# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006295# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 ))
6299 ? ptr2cells(ptr) : 1));
6300 curwin->w_set_curswant = TRUE;
6301 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6302 return (prevpos.col != curwin->w_cursor.col
6303 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6304 }
6305#endif
6306
6307 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006308 if (*ptr == NUL)
6309 return FAIL; /* already at the very end */
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006312 if (has_mbyte)
6313 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 else
6315#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006316 l = 1;
6317
6318 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6319 * contains "onemore". */
6320 if (ptr[l] == NUL
6321#ifdef FEAT_VIRTUALEDIT
6322 && (ve_flags & VE_ONEMORE) == 0
6323#endif
6324 )
6325 return FAIL;
6326 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
6328 curwin->w_set_curswant = TRUE;
6329 return OK;
6330}
6331
6332 int
6333oneleft()
6334{
6335#ifdef FEAT_VIRTUALEDIT
6336 if (virtual_active())
6337 {
6338 int width;
6339 int v = getviscol();
6340
6341 if (v == 0)
6342 return FAIL;
6343
6344# ifdef FEAT_LINEBREAK
6345 /* We might get stuck on 'showbreak', skip over it. */
6346 width = 1;
6347 for (;;)
6348 {
6349 coladvance(v - width);
6350 /* getviscol() is slow, skip it when 'showbreak' is empty and
6351 * there are no multi-byte characters */
6352 if ((*p_sbr == NUL
6353# ifdef FEAT_MBYTE
6354 && !has_mbyte
6355# endif
6356 ) || getviscol() < v)
6357 break;
6358 ++width;
6359 }
6360# else
6361 coladvance(v - 1);
6362# endif
6363
6364 if (curwin->w_cursor.coladd == 1)
6365 {
6366 char_u *ptr;
6367
6368 /* Adjust for multi-wide char (not a TAB) */
6369 ptr = ml_get_cursor();
6370 if (*ptr != TAB && vim_isprintc(
6371# ifdef FEAT_MBYTE
6372 (*mb_ptr2char)(ptr)
6373# else
6374 *ptr
6375# endif
6376 ) && ptr2cells(ptr) > 1)
6377 curwin->w_cursor.coladd = 0;
6378 }
6379
6380 curwin->w_set_curswant = TRUE;
6381 return OK;
6382 }
6383#endif
6384
6385 if (curwin->w_cursor.col == 0)
6386 return FAIL;
6387
6388 curwin->w_set_curswant = TRUE;
6389 --curwin->w_cursor.col;
6390
6391#ifdef FEAT_MBYTE
6392 /* if the character on the left of the current cursor is a multi-byte
6393 * character, move to its first byte */
6394 if (has_mbyte)
6395 mb_adjust_cursor();
6396#endif
6397 return OK;
6398}
6399
6400 int
6401cursor_up(n, upd_topline)
6402 long n;
6403 int upd_topline; /* When TRUE: update topline */
6404{
6405 linenr_T lnum;
6406
6407 if (n > 0)
6408 {
6409 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006410 /* This fails if the cursor is already in the first line or the count
6411 * is larger than the line number and '-' is in 'cpoptions' */
6412 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 return FAIL;
6414 if (n >= lnum)
6415 lnum = 1;
6416 else
6417#ifdef FEAT_FOLDING
6418 if (hasAnyFolding(curwin))
6419 {
6420 /*
6421 * Count each sequence of folded lines as one logical line.
6422 */
6423 /* go to the the start of the current fold */
6424 (void)hasFolding(lnum, &lnum, NULL);
6425
6426 while (n--)
6427 {
6428 /* move up one line */
6429 --lnum;
6430 if (lnum <= 1)
6431 break;
6432 /* If we entered a fold, move to the beginning, unless in
6433 * Insert mode or when 'foldopen' contains "all": it will open
6434 * in a moment. */
6435 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6436 (void)hasFolding(lnum, &lnum, NULL);
6437 }
6438 if (lnum < 1)
6439 lnum = 1;
6440 }
6441 else
6442#endif
6443 lnum -= n;
6444 curwin->w_cursor.lnum = lnum;
6445 }
6446
6447 /* try to advance to the column we want to be at */
6448 coladvance(curwin->w_curswant);
6449
6450 if (upd_topline)
6451 update_topline(); /* make sure curwin->w_topline is valid */
6452
6453 return OK;
6454}
6455
6456/*
6457 * Cursor down a number of logical lines.
6458 */
6459 int
6460cursor_down(n, upd_topline)
6461 long n;
6462 int upd_topline; /* When TRUE: update topline */
6463{
6464 linenr_T lnum;
6465
6466 if (n > 0)
6467 {
6468 lnum = curwin->w_cursor.lnum;
6469#ifdef FEAT_FOLDING
6470 /* Move to last line of fold, will fail if it's the end-of-file. */
6471 (void)hasFolding(lnum, NULL, &lnum);
6472#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006473 /* This fails if the cursor is already in the last line or would move
6474 * beyound the last line and '-' is in 'cpoptions' */
6475 if (lnum >= curbuf->b_ml.ml_line_count
6476 || (lnum + n > curbuf->b_ml.ml_line_count
6477 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 return FAIL;
6479 if (lnum + n >= curbuf->b_ml.ml_line_count)
6480 lnum = curbuf->b_ml.ml_line_count;
6481 else
6482#ifdef FEAT_FOLDING
6483 if (hasAnyFolding(curwin))
6484 {
6485 linenr_T last;
6486
6487 /* count each sequence of folded lines as one logical line */
6488 while (n--)
6489 {
6490 if (hasFolding(lnum, NULL, &last))
6491 lnum = last + 1;
6492 else
6493 ++lnum;
6494 if (lnum >= curbuf->b_ml.ml_line_count)
6495 break;
6496 }
6497 if (lnum > curbuf->b_ml.ml_line_count)
6498 lnum = curbuf->b_ml.ml_line_count;
6499 }
6500 else
6501#endif
6502 lnum += n;
6503 curwin->w_cursor.lnum = lnum;
6504 }
6505
6506 /* try to advance to the column we want to be at */
6507 coladvance(curwin->w_curswant);
6508
6509 if (upd_topline)
6510 update_topline(); /* make sure curwin->w_topline is valid */
6511
6512 return OK;
6513}
6514
6515/*
6516 * Stuff the last inserted text in the read buffer.
6517 * Last_insert actually is a copy of the redo buffer, so we
6518 * first have to remove the command.
6519 */
6520 int
6521stuff_inserted(c, count, no_esc)
6522 int c; /* Command character to be inserted */
6523 long count; /* Repeat this many times */
6524 int no_esc; /* Don't add an ESC at the end */
6525{
6526 char_u *esc_ptr;
6527 char_u *ptr;
6528 char_u *last_ptr;
6529 char_u last = NUL;
6530
6531 ptr = get_last_insert();
6532 if (ptr == NULL)
6533 {
6534 EMSG(_(e_noinstext));
6535 return FAIL;
6536 }
6537
6538 /* may want to stuff the command character, to start Insert mode */
6539 if (c != NUL)
6540 stuffcharReadbuff(c);
6541 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6542 *esc_ptr = NUL; /* remove the ESC */
6543
6544 /* when the last char is either "0" or "^" it will be quoted if no ESC
6545 * comes after it OR if it will inserted more than once and "ptr"
6546 * starts with ^D. -- Acevedo
6547 */
6548 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6549 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6550 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6551 {
6552 last = *last_ptr;
6553 *last_ptr = NUL;
6554 }
6555
6556 do
6557 {
6558 stuffReadbuff(ptr);
6559 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6560 if (last)
6561 stuffReadbuff((char_u *)(last == '0'
6562 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6563 : IF_EB("\026^", CTRL_V_STR "^")));
6564 }
6565 while (--count > 0);
6566
6567 if (last)
6568 *last_ptr = last;
6569
6570 if (esc_ptr != NULL)
6571 *esc_ptr = ESC; /* put the ESC back */
6572
6573 /* may want to stuff a trailing ESC, to get out of Insert mode */
6574 if (!no_esc)
6575 stuffcharReadbuff(ESC);
6576
6577 return OK;
6578}
6579
6580 char_u *
6581get_last_insert()
6582{
6583 if (last_insert == NULL)
6584 return NULL;
6585 return last_insert + last_insert_skip;
6586}
6587
6588/*
6589 * Get last inserted string, and remove trailing <Esc>.
6590 * Returns pointer to allocated memory (must be freed) or NULL.
6591 */
6592 char_u *
6593get_last_insert_save()
6594{
6595 char_u *s;
6596 int len;
6597
6598 if (last_insert == NULL)
6599 return NULL;
6600 s = vim_strsave(last_insert + last_insert_skip);
6601 if (s != NULL)
6602 {
6603 len = (int)STRLEN(s);
6604 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6605 s[len - 1] = NUL;
6606 }
6607 return s;
6608}
6609
6610/*
6611 * Check the word in front of the cursor for an abbreviation.
6612 * Called when the non-id character "c" has been entered.
6613 * When an abbreviation is recognized it is removed from the text and
6614 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6615 */
6616 static int
6617echeck_abbr(c)
6618 int c;
6619{
6620 /* Don't check for abbreviation in paste mode, when disabled and just
6621 * after moving around with cursor keys. */
6622 if (p_paste || no_abbr || arrow_used)
6623 return FALSE;
6624
6625 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6626 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6627}
6628
6629/*
6630 * replace-stack functions
6631 *
6632 * When replacing characters, the replaced characters are remembered for each
6633 * new character. This is used to re-insert the old text when backspacing.
6634 *
6635 * There is a NUL headed list of characters for each character that is
6636 * currently in the file after the insertion point. When BS is used, one NUL
6637 * headed list is put back for the deleted character.
6638 *
6639 * For a newline, there are two NUL headed lists. One contains the characters
6640 * that the NL replaced. The extra one stores the characters after the cursor
6641 * that were deleted (always white space).
6642 *
6643 * Replace_offset is normally 0, in which case replace_push will add a new
6644 * character at the end of the stack. If replace_offset is not 0, that many
6645 * characters will be left on the stack above the newly inserted character.
6646 */
6647
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006648static char_u *replace_stack = NULL;
6649static long replace_stack_nr = 0; /* next entry in replace stack */
6650static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 void
6653replace_push(c)
6654 int c; /* character that is replaced (NUL is none) */
6655{
6656 char_u *p;
6657
6658 if (replace_stack_nr < replace_offset) /* nothing to do */
6659 return;
6660 if (replace_stack_len <= replace_stack_nr)
6661 {
6662 replace_stack_len += 50;
6663 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6664 if (p == NULL) /* out of memory */
6665 {
6666 replace_stack_len -= 50;
6667 return;
6668 }
6669 if (replace_stack != NULL)
6670 {
6671 mch_memmove(p, replace_stack,
6672 (size_t)(replace_stack_nr * sizeof(char_u)));
6673 vim_free(replace_stack);
6674 }
6675 replace_stack = p;
6676 }
6677 p = replace_stack + replace_stack_nr - replace_offset;
6678 if (replace_offset)
6679 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6680 *p = c;
6681 ++replace_stack_nr;
6682}
6683
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006684#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
6686 * call replace_push(c) with replace_offset set to the first NUL.
6687 */
6688 static void
6689replace_push_off(c)
6690 int c;
6691{
6692 char_u *p;
6693
6694 p = replace_stack + replace_stack_nr;
6695 for (replace_offset = 1; replace_offset < replace_stack_nr;
6696 ++replace_offset)
6697 if (*--p == NUL)
6698 break;
6699 replace_push(c);
6700 replace_offset = 0;
6701}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704/*
6705 * Pop one item from the replace stack.
6706 * return -1 if stack empty
6707 * return replaced character or NUL otherwise
6708 */
6709 static int
6710replace_pop()
6711{
6712 if (replace_stack_nr == 0)
6713 return -1;
6714 return (int)replace_stack[--replace_stack_nr];
6715}
6716
6717/*
6718 * Join the top two items on the replace stack. This removes to "off"'th NUL
6719 * encountered.
6720 */
6721 static void
6722replace_join(off)
6723 int off; /* offset for which NUL to remove */
6724{
6725 int i;
6726
6727 for (i = replace_stack_nr; --i >= 0; )
6728 if (replace_stack[i] == NUL && off-- <= 0)
6729 {
6730 --replace_stack_nr;
6731 mch_memmove(replace_stack + i, replace_stack + i + 1,
6732 (size_t)(replace_stack_nr - i));
6733 return;
6734 }
6735}
6736
6737/*
6738 * Pop bytes from the replace stack until a NUL is found, and insert them
6739 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6740 */
6741 static void
6742replace_pop_ins()
6743{
6744 int cc;
6745 int oldState = State;
6746
6747 State = NORMAL; /* don't want REPLACE here */
6748 while ((cc = replace_pop()) > 0)
6749 {
6750#ifdef FEAT_MBYTE
6751 mb_replace_pop_ins(cc);
6752#else
6753 ins_char(cc);
6754#endif
6755 dec_cursor();
6756 }
6757 State = oldState;
6758}
6759
6760#ifdef FEAT_MBYTE
6761/*
6762 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6763 * indicates a multi-byte char, pop the other bytes too.
6764 */
6765 static void
6766mb_replace_pop_ins(cc)
6767 int cc;
6768{
6769 int n;
6770 char_u buf[MB_MAXBYTES];
6771 int i;
6772 int c;
6773
6774 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6775 {
6776 buf[0] = cc;
6777 for (i = 1; i < n; ++i)
6778 buf[i] = replace_pop();
6779 ins_bytes_len(buf, n);
6780 }
6781 else
6782 ins_char(cc);
6783
6784 if (enc_utf8)
6785 /* Handle composing chars. */
6786 for (;;)
6787 {
6788 c = replace_pop();
6789 if (c == -1) /* stack empty */
6790 break;
6791 if ((n = MB_BYTE2LEN(c)) == 1)
6792 {
6793 /* Not a multi-byte char, put it back. */
6794 replace_push(c);
6795 break;
6796 }
6797 else
6798 {
6799 buf[0] = c;
6800 for (i = 1; i < n; ++i)
6801 buf[i] = replace_pop();
6802 if (utf_iscomposing(utf_ptr2char(buf)))
6803 ins_bytes_len(buf, n);
6804 else
6805 {
6806 /* Not a composing char, put it back. */
6807 for (i = n - 1; i >= 0; --i)
6808 replace_push(buf[i]);
6809 break;
6810 }
6811 }
6812 }
6813}
6814#endif
6815
6816/*
6817 * make the replace stack empty
6818 * (called when exiting replace mode)
6819 */
6820 static void
6821replace_flush()
6822{
6823 vim_free(replace_stack);
6824 replace_stack = NULL;
6825 replace_stack_len = 0;
6826 replace_stack_nr = 0;
6827}
6828
6829/*
6830 * Handle doing a BS for one character.
6831 * cc < 0: replace stack empty, just move cursor
6832 * cc == 0: character was inserted, delete it
6833 * cc > 0: character was replaced, put cc (first byte of original char) back
6834 * and check for more characters to be put back
6835 */
6836 static void
6837replace_do_bs()
6838{
6839 int cc;
6840#ifdef FEAT_VREPLACE
6841 int orig_len = 0;
6842 int ins_len;
6843 int orig_vcols = 0;
6844 colnr_T start_vcol;
6845 char_u *p;
6846 int i;
6847 int vcol;
6848#endif
6849
6850 cc = replace_pop();
6851 if (cc > 0)
6852 {
6853#ifdef FEAT_VREPLACE
6854 if (State & VREPLACE_FLAG)
6855 {
6856 /* Get the number of screen cells used by the character we are
6857 * going to delete. */
6858 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6859 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6860 }
6861#endif
6862#ifdef FEAT_MBYTE
6863 if (has_mbyte)
6864 {
6865 del_char(FALSE);
6866# ifdef FEAT_VREPLACE
6867 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006868 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869# endif
6870 replace_push(cc);
6871 }
6872 else
6873#endif
6874 {
6875 pchar_cursor(cc);
6876#ifdef FEAT_VREPLACE
6877 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006878 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#endif
6880 }
6881 replace_pop_ins();
6882
6883#ifdef FEAT_VREPLACE
6884 if (State & VREPLACE_FLAG)
6885 {
6886 /* Get the number of screen cells used by the inserted characters */
6887 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006888 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 vcol = start_vcol;
6890 for (i = 0; i < ins_len; ++i)
6891 {
6892 vcol += chartabsize(p + i, vcol);
6893#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006894 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895#endif
6896 }
6897 vcol -= start_vcol;
6898
6899 /* Delete spaces that were inserted after the cursor to keep the
6900 * text aligned. */
6901 curwin->w_cursor.col += ins_len;
6902 while (vcol > orig_vcols && gchar_cursor() == ' ')
6903 {
6904 del_char(FALSE);
6905 ++orig_vcols;
6906 }
6907 curwin->w_cursor.col -= ins_len;
6908 }
6909#endif
6910
6911 /* mark the buffer as changed and prepare for displaying */
6912 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6913 }
6914 else if (cc == 0)
6915 (void)del_char(FALSE);
6916}
6917
6918#ifdef FEAT_CINDENT
6919/*
6920 * Return TRUE if C-indenting is on.
6921 */
6922 static int
6923cindent_on()
6924{
6925 return (!p_paste && (curbuf->b_p_cin
6926# ifdef FEAT_EVAL
6927 || *curbuf->b_p_inde != NUL
6928# endif
6929 ));
6930}
6931#endif
6932
6933#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6934/*
6935 * Re-indent the current line, based on the current contents of it and the
6936 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6937 * confused what all the part that handles Control-T is doing that I'm not.
6938 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6939 */
6940
6941 void
6942fixthisline(get_the_indent)
6943 int (*get_the_indent) __ARGS((void));
6944{
6945 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6946 if (linewhite(curwin->w_cursor.lnum))
6947 did_ai = TRUE; /* delete the indent if the line stays empty */
6948}
6949
6950 void
6951fix_indent()
6952{
6953 if (p_paste)
6954 return;
6955# ifdef FEAT_LISP
6956 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6957 fixthisline(get_lisp_indent);
6958# endif
6959# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6960 else
6961# endif
6962# ifdef FEAT_CINDENT
6963 if (cindent_on())
6964 do_c_expr_indent();
6965# endif
6966}
6967
6968#endif
6969
6970#ifdef FEAT_CINDENT
6971/*
6972 * return TRUE if 'cinkeys' contains the key "keytyped",
6973 * when == '*': Only if key is preceded with '*' (indent before insert)
6974 * when == '!': Only if key is prededed with '!' (don't insert)
6975 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6976 *
6977 * "keytyped" can have a few special values:
6978 * KEY_OPEN_FORW
6979 * KEY_OPEN_BACK
6980 * KEY_COMPLETE just finished completion.
6981 *
6982 * If line_is_empty is TRUE accept keys with '0' before them.
6983 */
6984 int
6985in_cinkeys(keytyped, when, line_is_empty)
6986 int keytyped;
6987 int when;
6988 int line_is_empty;
6989{
6990 char_u *look;
6991 int try_match;
6992 int try_match_word;
6993 char_u *p;
6994 char_u *line;
6995 int icase;
6996 int i;
6997
6998#ifdef FEAT_EVAL
6999 if (*curbuf->b_p_inde != NUL)
7000 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7001 else
7002#endif
7003 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7004 while (*look)
7005 {
7006 /*
7007 * Find out if we want to try a match with this key, depending on
7008 * 'when' and a '*' or '!' before the key.
7009 */
7010 switch (when)
7011 {
7012 case '*': try_match = (*look == '*'); break;
7013 case '!': try_match = (*look == '!'); break;
7014 default: try_match = (*look != '*'); break;
7015 }
7016 if (*look == '*' || *look == '!')
7017 ++look;
7018
7019 /*
7020 * If there is a '0', only accept a match if the line is empty.
7021 * But may still match when typing last char of a word.
7022 */
7023 if (*look == '0')
7024 {
7025 try_match_word = try_match;
7026 if (!line_is_empty)
7027 try_match = FALSE;
7028 ++look;
7029 }
7030 else
7031 try_match_word = FALSE;
7032
7033 /*
7034 * does it look like a control character?
7035 */
7036 if (*look == '^'
7037#ifdef EBCDIC
7038 && (Ctrl_chr(look[1]) != 0)
7039#else
7040 && look[1] >= '?' && look[1] <= '_'
7041#endif
7042 )
7043 {
7044 if (try_match && keytyped == Ctrl_chr(look[1]))
7045 return TRUE;
7046 look += 2;
7047 }
7048 /*
7049 * 'o' means "o" command, open forward.
7050 * 'O' means "O" command, open backward.
7051 */
7052 else if (*look == 'o')
7053 {
7054 if (try_match && keytyped == KEY_OPEN_FORW)
7055 return TRUE;
7056 ++look;
7057 }
7058 else if (*look == 'O')
7059 {
7060 if (try_match && keytyped == KEY_OPEN_BACK)
7061 return TRUE;
7062 ++look;
7063 }
7064
7065 /*
7066 * 'e' means to check for "else" at start of line and just before the
7067 * cursor.
7068 */
7069 else if (*look == 'e')
7070 {
7071 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7072 {
7073 p = ml_get_curline();
7074 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7075 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7076 return TRUE;
7077 }
7078 ++look;
7079 }
7080
7081 /*
7082 * ':' only causes an indent if it is at the end of a label or case
7083 * statement, or when it was before typing the ':' (to fix
7084 * class::method for C++).
7085 */
7086 else if (*look == ':')
7087 {
7088 if (try_match && keytyped == ':')
7089 {
7090 p = ml_get_curline();
7091 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7092 return TRUE;
7093 if (curwin->w_cursor.col > 2
7094 && p[curwin->w_cursor.col - 1] == ':'
7095 && p[curwin->w_cursor.col - 2] == ':')
7096 {
7097 p[curwin->w_cursor.col - 1] = ' ';
7098 i = (cin_iscase(p) || cin_isscopedecl(p)
7099 || cin_islabel(30));
7100 p = ml_get_curline();
7101 p[curwin->w_cursor.col - 1] = ':';
7102 if (i)
7103 return TRUE;
7104 }
7105 }
7106 ++look;
7107 }
7108
7109
7110 /*
7111 * Is it a key in <>, maybe?
7112 */
7113 else if (*look == '<')
7114 {
7115 if (try_match)
7116 {
7117 /*
7118 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7119 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7120 * >, *, : and ! keys if they really really want to.
7121 */
7122 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7123 && keytyped == look[1])
7124 return TRUE;
7125
7126 if (keytyped == get_special_key_code(look + 1))
7127 return TRUE;
7128 }
7129 while (*look && *look != '>')
7130 look++;
7131 while (*look == '>')
7132 look++;
7133 }
7134
7135 /*
7136 * Is it a word: "=word"?
7137 */
7138 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7139 {
7140 ++look;
7141 if (*look == '~')
7142 {
7143 icase = TRUE;
7144 ++look;
7145 }
7146 else
7147 icase = FALSE;
7148 p = vim_strchr(look, ',');
7149 if (p == NULL)
7150 p = look + STRLEN(look);
7151 if ((try_match || try_match_word)
7152 && curwin->w_cursor.col >= (colnr_T)(p - look))
7153 {
7154 int match = FALSE;
7155
7156#ifdef FEAT_INS_EXPAND
7157 if (keytyped == KEY_COMPLETE)
7158 {
7159 char_u *s;
7160
7161 /* Just completed a word, check if it starts with "look".
7162 * search back for the start of a word. */
7163 line = ml_get_curline();
7164# ifdef FEAT_MBYTE
7165 if (has_mbyte)
7166 {
7167 char_u *n;
7168
7169 for (s = line + curwin->w_cursor.col; s > line; s = n)
7170 {
7171 n = mb_prevptr(line, s);
7172 if (!vim_iswordp(n))
7173 break;
7174 }
7175 }
7176 else
7177# endif
7178 for (s = line + curwin->w_cursor.col; s > line; --s)
7179 if (!vim_iswordc(s[-1]))
7180 break;
7181 if (s + (p - look) <= line + curwin->w_cursor.col
7182 && (icase
7183 ? MB_STRNICMP(s, look, p - look)
7184 : STRNCMP(s, look, p - look)) == 0)
7185 match = TRUE;
7186 }
7187 else
7188#endif
7189 /* TODO: multi-byte */
7190 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7191 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7192 {
7193 line = ml_get_cursor();
7194 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7195 || !vim_iswordc(line[-(p - look) - 1]))
7196 && (icase
7197 ? MB_STRNICMP(line - (p - look), look, p - look)
7198 : STRNCMP(line - (p - look), look, p - look))
7199 == 0)
7200 match = TRUE;
7201 }
7202 if (match && try_match_word && !try_match)
7203 {
7204 /* "0=word": Check if there are only blanks before the
7205 * word. */
7206 line = ml_get_curline();
7207 if ((int)(skipwhite(line) - line) !=
7208 (int)(curwin->w_cursor.col - (p - look)))
7209 match = FALSE;
7210 }
7211 if (match)
7212 return TRUE;
7213 }
7214 look = p;
7215 }
7216
7217 /*
7218 * ok, it's a boring generic character.
7219 */
7220 else
7221 {
7222 if (try_match && *look == keytyped)
7223 return TRUE;
7224 ++look;
7225 }
7226
7227 /*
7228 * Skip over ", ".
7229 */
7230 look = skip_to_option_part(look);
7231 }
7232 return FALSE;
7233}
7234#endif /* FEAT_CINDENT */
7235
7236#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7237/*
7238 * Map Hebrew keyboard when in hkmap mode.
7239 */
7240 int
7241hkmap(c)
7242 int c;
7243{
7244 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7245 {
7246 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7247 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7248 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7249 static char_u map[26] =
7250 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7251 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7252 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7253 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7254 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7255 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7256 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7257 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7258 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7259
7260 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7261 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7262 /* '-1'='sofit' */
7263 else if (c == 'x')
7264 return 'X';
7265 else if (c == 'q')
7266 return '\''; /* {geresh}={'} */
7267 else if (c == 246)
7268 return ' '; /* \"o --> ' ' for a german keyboard */
7269 else if (c == 228)
7270 return ' '; /* \"a --> ' ' -- / -- */
7271 else if (c == 252)
7272 return ' '; /* \"u --> ' ' -- / -- */
7273#ifdef EBCDIC
7274 else if (islower(c))
7275#else
7276 /* NOTE: islower() does not do the right thing for us on Linux so we
7277 * do this the same was as 5.7 and previous, so it works correctly on
7278 * all systems. Specifically, the e.g. Delete and Arrow keys are
7279 * munged and won't work if e.g. searching for Hebrew text.
7280 */
7281 else if (c >= 'a' && c <= 'z')
7282#endif
7283 return (int)(map[CharOrdLow(c)] + p_aleph);
7284 else
7285 return c;
7286 }
7287 else
7288 {
7289 switch (c)
7290 {
7291 case '`': return ';';
7292 case '/': return '.';
7293 case '\'': return ',';
7294 case 'q': return '/';
7295 case 'w': return '\'';
7296
7297 /* Hebrew letters - set offset from 'a' */
7298 case ',': c = '{'; break;
7299 case '.': c = 'v'; break;
7300 case ';': c = 't'; break;
7301 default: {
7302 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7303
7304#ifdef EBCDIC
7305 /* see note about islower() above */
7306 if (!islower(c))
7307#else
7308 if (c < 'a' || c > 'z')
7309#endif
7310 return c;
7311 c = str[CharOrdLow(c)];
7312 break;
7313 }
7314 }
7315
7316 return (int)(CharOrdLow(c) + p_aleph);
7317 }
7318}
7319#endif
7320
7321 static void
7322ins_reg()
7323{
7324 int need_redraw = FALSE;
7325 int regname;
7326 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007327#ifdef FEAT_VISUAL
7328 int vis_active = VIsual_active;
7329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330
7331 /*
7332 * If we are going to wait for a character, show a '"'.
7333 */
7334 pc_status = PC_STATUS_UNSET;
7335 if (redrawing() && !char_avail())
7336 {
7337 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007338 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
7340 edit_putchar('"', TRUE);
7341#ifdef FEAT_CMDL_INFO
7342 add_to_showcmd_c(Ctrl_R);
7343#endif
7344 }
7345
7346#ifdef USE_ON_FLY_SCROLL
7347 dont_scroll = TRUE; /* disallow scrolling here */
7348#endif
7349
7350 /*
7351 * Don't map the register name. This also prevents the mode message to be
7352 * deleted when ESC is hit.
7353 */
7354 ++no_mapping;
7355 regname = safe_vgetc();
7356#ifdef FEAT_LANGMAP
7357 LANGMAP_ADJUST(regname, TRUE);
7358#endif
7359 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7360 {
7361 /* Get a third key for literal register insertion */
7362 literally = regname;
7363#ifdef FEAT_CMDL_INFO
7364 add_to_showcmd_c(literally);
7365#endif
7366 regname = safe_vgetc();
7367#ifdef FEAT_LANGMAP
7368 LANGMAP_ADJUST(regname, TRUE);
7369#endif
7370 }
7371 --no_mapping;
7372
7373#ifdef FEAT_EVAL
7374 /*
7375 * Don't call u_sync() while getting the expression,
7376 * evaluating it or giving an error message for it!
7377 */
7378 ++no_u_sync;
7379 if (regname == '=')
7380 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007381# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007385# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 /* Restore the Input Method. */
7387 if (im_on)
7388 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007391 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7392 {
7393 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 else
7397 {
7398#endif
7399 if (literally == Ctrl_O || literally == Ctrl_P)
7400 {
7401 /* Append the command to the redo buffer. */
7402 AppendCharToRedobuff(Ctrl_R);
7403 AppendCharToRedobuff(literally);
7404 AppendCharToRedobuff(regname);
7405
7406 do_put(regname, BACKWARD, 1L,
7407 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7408 }
7409 else if (insert_reg(regname, literally) == FAIL)
7410 {
7411 vim_beep();
7412 need_redraw = TRUE; /* remove the '"' */
7413 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007414 else if (stop_insert_mode)
7415 /* When the '=' register was used and a function was invoked that
7416 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7417 * insert anything, need to remove the '"' */
7418 need_redraw = TRUE;
7419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420#ifdef FEAT_EVAL
7421 }
7422 --no_u_sync;
7423#endif
7424#ifdef FEAT_CMDL_INFO
7425 clear_showcmd();
7426#endif
7427
7428 /* If the inserted register is empty, we need to remove the '"' */
7429 if (need_redraw || stuff_empty())
7430 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007431
7432#ifdef FEAT_VISUAL
7433 /* Disallow starting Visual mode here, would get a weird mode. */
7434 if (!vis_active && VIsual_active)
7435 end_visual_mode();
7436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437}
7438
7439/*
7440 * CTRL-G commands in Insert mode.
7441 */
7442 static void
7443ins_ctrl_g()
7444{
7445 int c;
7446
7447#ifdef FEAT_INS_EXPAND
7448 /* Right after CTRL-X the cursor will be after the ruler. */
7449 setcursor();
7450#endif
7451
7452 /*
7453 * Don't map the second key. This also prevents the mode message to be
7454 * deleted when ESC is hit.
7455 */
7456 ++no_mapping;
7457 c = safe_vgetc();
7458 --no_mapping;
7459 switch (c)
7460 {
7461 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7462 case K_UP:
7463 case Ctrl_K:
7464 case 'k': ins_up(TRUE);
7465 break;
7466
7467 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7468 case K_DOWN:
7469 case Ctrl_J:
7470 case 'j': ins_down(TRUE);
7471 break;
7472
7473 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007474 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007476
7477 /* Need to reset Insstart, esp. because a BS that joins
7478 * aline to the previous one must save for undo. */
7479 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 break;
7481
7482 /* Unknown CTRL-G command, reserved for future expansion. */
7483 default: vim_beep();
7484 }
7485}
7486
7487/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007488 * CTRL-^ in Insert mode.
7489 */
7490 static void
7491ins_ctrl_hat()
7492{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007493 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007494 {
7495 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7496 if (State & LANGMAP)
7497 {
7498 curbuf->b_p_iminsert = B_IMODE_NONE;
7499 State &= ~LANGMAP;
7500 }
7501 else
7502 {
7503 curbuf->b_p_iminsert = B_IMODE_LMAP;
7504 State |= LANGMAP;
7505#ifdef USE_IM_CONTROL
7506 im_set_active(FALSE);
7507#endif
7508 }
7509 }
7510#ifdef USE_IM_CONTROL
7511 else
7512 {
7513 /* There are no ":lmap" mappings, toggle IM */
7514 if (im_get_status())
7515 {
7516 curbuf->b_p_iminsert = B_IMODE_NONE;
7517 im_set_active(FALSE);
7518 }
7519 else
7520 {
7521 curbuf->b_p_iminsert = B_IMODE_IM;
7522 State &= ~LANGMAP;
7523 im_set_active(TRUE);
7524 }
7525 }
7526#endif
7527 set_iminsert_global();
7528 showmode();
7529#ifdef FEAT_GUI
7530 /* may show different cursor shape or color */
7531 if (gui.in_use)
7532 gui_update_cursor(TRUE, FALSE);
7533#endif
7534#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7535 /* Show/unshow value of 'keymap' in status lines. */
7536 status_redraw_curbuf();
7537#endif
7538}
7539
7540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 * Handle ESC in insert mode.
7542 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7543 * insert.
7544 */
7545 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007546ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 long *count;
7548 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007549 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550{
7551 int temp;
7552 static int disabled_redraw = FALSE;
7553
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007554#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007555 check_spell_redraw();
7556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557#if defined(FEAT_HANGULIN)
7558# if defined(ESC_CHG_TO_ENG_MODE)
7559 hangul_input_state_set(0);
7560# endif
7561 if (composing_hangul)
7562 {
7563 push_raw_key(composing_hangul_buffer, 2);
7564 composing_hangul = 0;
7565 }
7566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
7568 temp = curwin->w_cursor.col;
7569 if (disabled_redraw)
7570 {
7571 --RedrawingDisabled;
7572 disabled_redraw = FALSE;
7573 }
7574 if (!arrow_used)
7575 {
7576 /*
7577 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007578 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7579 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 */
7581 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007582 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
7584 /*
7585 * Repeating insert may take a long time. Check for
7586 * interrupt now and then.
7587 */
7588 if (*count > 0)
7589 {
7590 line_breakcheck();
7591 if (got_int)
7592 *count = 0;
7593 }
7594
7595 if (--*count > 0) /* repeat what was typed */
7596 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007597 /* Vi repeats the insert without replacing characters. */
7598 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7599 State &= ~REPLACE_FLAG;
7600
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 (void)start_redo_ins();
7602 if (cmdchar == 'r' || cmdchar == 'v')
7603 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7604 ++RedrawingDisabled;
7605 disabled_redraw = TRUE;
7606 return FALSE; /* repeat the insert */
7607 }
7608 stop_insert(&curwin->w_cursor, TRUE);
7609 undisplay_dollar();
7610 }
7611
7612 /* When an autoindent was removed, curswant stays after the
7613 * indent */
7614 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7615 curwin->w_set_curswant = TRUE;
7616
7617 /* Remember the last Insert position in the '^ mark. */
7618 if (!cmdmod.keepjumps)
7619 curbuf->b_last_insert = curwin->w_cursor;
7620
7621 /*
7622 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007623 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007625 if (!nomove
7626 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627#ifdef FEAT_VIRTUALEDIT
7628 || curwin->w_cursor.coladd > 0
7629#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007630 )
7631 && (restart_edit == NUL
7632 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007634 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007636 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637#ifdef FEAT_RIGHTLEFT
7638 && !revins_on
7639#endif
7640 )
7641 {
7642#ifdef FEAT_VIRTUALEDIT
7643 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7644 {
7645 oneleft();
7646 if (restart_edit != NUL)
7647 ++curwin->w_cursor.coladd;
7648 }
7649 else
7650#endif
7651 {
7652 --curwin->w_cursor.col;
7653#ifdef FEAT_MBYTE
7654 /* Correct cursor for multi-byte character. */
7655 if (has_mbyte)
7656 mb_adjust_cursor();
7657#endif
7658 }
7659 }
7660
7661#ifdef USE_IM_CONTROL
7662 /* Disable IM to allow typing English directly for Normal mode commands.
7663 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7664 * well). */
7665 if (!(State & LANGMAP))
7666 im_save_status(&curbuf->b_p_iminsert);
7667 im_set_active(FALSE);
7668#endif
7669
7670 State = NORMAL;
7671 /* need to position cursor again (e.g. when on a TAB ) */
7672 changed_cline_bef_curs();
7673
7674#ifdef FEAT_MOUSE
7675 setmouse();
7676#endif
7677#ifdef CURSOR_SHAPE
7678 ui_cursor_shape(); /* may show different cursor shape */
7679#endif
7680
7681 /*
7682 * When recording or for CTRL-O, need to display the new mode.
7683 * Otherwise remove the mode message.
7684 */
7685 if (Recording || restart_edit != NUL)
7686 showmode();
7687 else if (p_smd)
7688 MSG("");
7689
7690 return TRUE; /* exit Insert mode */
7691}
7692
7693#ifdef FEAT_RIGHTLEFT
7694/*
7695 * Toggle language: hkmap and revins_on.
7696 * Move to end of reverse inserted text.
7697 */
7698 static void
7699ins_ctrl_()
7700{
7701 if (revins_on && revins_chars && revins_scol >= 0)
7702 {
7703 while (gchar_cursor() != NUL && revins_chars--)
7704 ++curwin->w_cursor.col;
7705 }
7706 p_ri = !p_ri;
7707 revins_on = (State == INSERT && p_ri);
7708 if (revins_on)
7709 {
7710 revins_scol = curwin->w_cursor.col;
7711 revins_legal++;
7712 revins_chars = 0;
7713 undisplay_dollar();
7714 }
7715 else
7716 revins_scol = -1;
7717#ifdef FEAT_FKMAP
7718 if (p_altkeymap)
7719 {
7720 /*
7721 * to be consistent also for redo command, using '.'
7722 * set arrow_used to true and stop it - causing to redo
7723 * characters entered in one mode (normal/reverse insert).
7724 */
7725 arrow_used = TRUE;
7726 (void)stop_arrow();
7727 p_fkmap = curwin->w_p_rl ^ p_ri;
7728 if (p_fkmap && p_ri)
7729 State = INSERT;
7730 }
7731 else
7732#endif
7733 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7734 showmode();
7735}
7736#endif
7737
7738#ifdef FEAT_VISUAL
7739/*
7740 * If 'keymodel' contains "startsel", may start selection.
7741 * Returns TRUE when a CTRL-O and other keys stuffed.
7742 */
7743 static int
7744ins_start_select(c)
7745 int c;
7746{
7747 if (km_startsel)
7748 switch (c)
7749 {
7750 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 case K_PAGEUP:
7753 case K_KPAGEUP:
7754 case K_PAGEDOWN:
7755 case K_KPAGEDOWN:
7756# ifdef MACOS
7757 case K_LEFT:
7758 case K_RIGHT:
7759 case K_UP:
7760 case K_DOWN:
7761 case K_END:
7762 case K_HOME:
7763# endif
7764 if (!(mod_mask & MOD_MASK_SHIFT))
7765 break;
7766 /* FALLTHROUGH */
7767 case K_S_LEFT:
7768 case K_S_RIGHT:
7769 case K_S_UP:
7770 case K_S_DOWN:
7771 case K_S_END:
7772 case K_S_HOME:
7773 /* Start selection right away, the cursor can move with
7774 * CTRL-O when beyond the end of the line. */
7775 start_selection();
7776
7777 /* Execute the key in (insert) Select mode. */
7778 stuffcharReadbuff(Ctrl_O);
7779 if (mod_mask)
7780 {
7781 char_u buf[4];
7782
7783 buf[0] = K_SPECIAL;
7784 buf[1] = KS_MODIFIER;
7785 buf[2] = mod_mask;
7786 buf[3] = NUL;
7787 stuffReadbuff(buf);
7788 }
7789 stuffcharReadbuff(c);
7790 return TRUE;
7791 }
7792 return FALSE;
7793}
7794#endif
7795
7796/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007797 * <Insert> key in Insert mode: toggle insert/remplace mode.
7798 */
7799 static void
7800ins_insert(replaceState)
7801 int replaceState;
7802{
7803#ifdef FEAT_FKMAP
7804 if (p_fkmap && p_ri)
7805 {
7806 beep_flush();
7807 EMSG(farsi_text_3); /* encoded in Farsi */
7808 return;
7809 }
7810#endif
7811
7812#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007813# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007814 set_vim_var_string(VV_INSERTMODE,
7815 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007816# ifdef FEAT_VREPLACE
7817 replaceState == VREPLACE ? "v" :
7818# endif
7819 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007820# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007821 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7822#endif
7823 if (State & REPLACE_FLAG)
7824 State = INSERT | (State & LANGMAP);
7825 else
7826 State = replaceState | (State & LANGMAP);
7827 AppendCharToRedobuff(K_INS);
7828 showmode();
7829#ifdef CURSOR_SHAPE
7830 ui_cursor_shape(); /* may show different cursor shape */
7831#endif
7832}
7833
7834/*
7835 * Pressed CTRL-O in Insert mode.
7836 */
7837 static void
7838ins_ctrl_o()
7839{
7840#ifdef FEAT_VREPLACE
7841 if (State & VREPLACE_FLAG)
7842 restart_edit = 'V';
7843 else
7844#endif
7845 if (State & REPLACE_FLAG)
7846 restart_edit = 'R';
7847 else
7848 restart_edit = 'I';
7849#ifdef FEAT_VIRTUALEDIT
7850 if (virtual_active())
7851 ins_at_eol = FALSE; /* cursor always keeps its column */
7852 else
7853#endif
7854 ins_at_eol = (gchar_cursor() == NUL);
7855}
7856
7857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 * If the cursor is on an indent, ^T/^D insert/delete one
7859 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7860 * Always round the indent to 'shiftwith', this is compatible
7861 * with vi. But vi only supports ^T and ^D after an
7862 * autoindent, we support it everywhere.
7863 */
7864 static void
7865ins_shift(c, lastc)
7866 int c;
7867 int lastc;
7868{
7869 if (stop_arrow() == FAIL)
7870 return;
7871 AppendCharToRedobuff(c);
7872
7873 /*
7874 * 0^D and ^^D: remove all indent.
7875 */
7876 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7877 {
7878 --curwin->w_cursor.col;
7879 (void)del_char(FALSE); /* delete the '^' or '0' */
7880 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7881 if (State & REPLACE_FLAG)
7882 replace_pop_ins();
7883 if (lastc == '^')
7884 old_indent = get_indent(); /* remember curr. indent */
7885 change_indent(INDENT_SET, 0, TRUE, 0);
7886 }
7887 else
7888 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7889
7890 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7891 did_ai = FALSE;
7892#ifdef FEAT_SMARTINDENT
7893 did_si = FALSE;
7894 can_si = FALSE;
7895 can_si_back = FALSE;
7896#endif
7897#ifdef FEAT_CINDENT
7898 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7899#endif
7900}
7901
7902 static void
7903ins_del()
7904{
7905 int temp;
7906
7907 if (stop_arrow() == FAIL)
7908 return;
7909 if (gchar_cursor() == NUL) /* delete newline */
7910 {
7911 temp = curwin->w_cursor.col;
7912 if (!can_bs(BS_EOL) /* only if "eol" included */
7913 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7914 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7915 || do_join(FALSE) == FAIL)
7916 vim_beep();
7917 else
7918 curwin->w_cursor.col = temp;
7919 }
7920 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7921 vim_beep();
7922 did_ai = FALSE;
7923#ifdef FEAT_SMARTINDENT
7924 did_si = FALSE;
7925 can_si = FALSE;
7926 can_si_back = FALSE;
7927#endif
7928 AppendCharToRedobuff(K_DEL);
7929}
7930
7931/*
7932 * Handle Backspace, delete-word and delete-line in Insert mode.
7933 * Return TRUE when backspace was actually used.
7934 */
7935 static int
7936ins_bs(c, mode, inserted_space_p)
7937 int c;
7938 int mode;
7939 int *inserted_space_p;
7940{
7941 linenr_T lnum;
7942 int cc;
7943 int temp = 0; /* init for GCC */
7944 colnr_T mincol;
7945 int did_backspace = FALSE;
7946 int in_indent;
7947 int oldState;
7948#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007949 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950#endif
7951
7952 /*
7953 * can't delete anything in an empty file
7954 * can't backup past first character in buffer
7955 * can't backup past starting point unless 'backspace' > 1
7956 * can backup to a previous line if 'backspace' == 0
7957 */
7958 if ( bufempty()
7959 || (
7960#ifdef FEAT_RIGHTLEFT
7961 !revins_on &&
7962#endif
7963 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7964 || (!can_bs(BS_START)
7965 && (arrow_used
7966 || (curwin->w_cursor.lnum == Insstart.lnum
7967 && curwin->w_cursor.col <= Insstart.col)))
7968 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7969 && curwin->w_cursor.col <= ai_col)
7970 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7971 {
7972 vim_beep();
7973 return FALSE;
7974 }
7975
7976 if (stop_arrow() == FAIL)
7977 return FALSE;
7978 in_indent = inindent(0);
7979#ifdef FEAT_CINDENT
7980 if (in_indent)
7981 can_cindent = FALSE;
7982#endif
7983#ifdef FEAT_COMMENTS
7984 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7985#endif
7986#ifdef FEAT_RIGHTLEFT
7987 if (revins_on) /* put cursor after last inserted char */
7988 inc_cursor();
7989#endif
7990
7991#ifdef FEAT_VIRTUALEDIT
7992 /* Virtualedit:
7993 * BACKSPACE_CHAR eats a virtual space
7994 * BACKSPACE_WORD eats all coladd
7995 * BACKSPACE_LINE eats all coladd and keeps going
7996 */
7997 if (curwin->w_cursor.coladd > 0)
7998 {
7999 if (mode == BACKSPACE_CHAR)
8000 {
8001 --curwin->w_cursor.coladd;
8002 return TRUE;
8003 }
8004 if (mode == BACKSPACE_WORD)
8005 {
8006 curwin->w_cursor.coladd = 0;
8007 return TRUE;
8008 }
8009 curwin->w_cursor.coladd = 0;
8010 }
8011#endif
8012
8013 /*
8014 * delete newline!
8015 */
8016 if (curwin->w_cursor.col == 0)
8017 {
8018 lnum = Insstart.lnum;
8019 if (curwin->w_cursor.lnum == Insstart.lnum
8020#ifdef FEAT_RIGHTLEFT
8021 || revins_on
8022#endif
8023 )
8024 {
8025 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8026 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8027 return FALSE;
8028 --Insstart.lnum;
8029 Insstart.col = MAXCOL;
8030 }
8031 /*
8032 * In replace mode:
8033 * cc < 0: NL was inserted, delete it
8034 * cc >= 0: NL was replaced, put original characters back
8035 */
8036 cc = -1;
8037 if (State & REPLACE_FLAG)
8038 cc = replace_pop(); /* returns -1 if NL was inserted */
8039 /*
8040 * In replace mode, in the line we started replacing, we only move the
8041 * cursor.
8042 */
8043 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8044 {
8045 dec_cursor();
8046 }
8047 else
8048 {
8049#ifdef FEAT_VREPLACE
8050 if (!(State & VREPLACE_FLAG)
8051 || curwin->w_cursor.lnum > orig_line_count)
8052#endif
8053 {
8054 temp = gchar_cursor(); /* remember current char */
8055 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008056
8057 /* When "aw" is in 'formatoptions' we must delete the space at
8058 * the end of the line, otherwise the line will be broken
8059 * again when auto-formatting. */
8060 if (has_format_option(FO_AUTO)
8061 && has_format_option(FO_WHITE_PAR))
8062 {
8063 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8064 TRUE);
8065 int len;
8066
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008067 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008068 if (len > 0 && ptr[len - 1] == ' ')
8069 ptr[len - 1] = NUL;
8070 }
8071
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 (void)do_join(FALSE);
8073 if (temp == NUL && gchar_cursor() != NUL)
8074 inc_cursor();
8075 }
8076#ifdef FEAT_VREPLACE
8077 else
8078 dec_cursor();
8079#endif
8080
8081 /*
8082 * In REPLACE mode we have to put back the text that was replaced
8083 * by the NL. On the replace stack is first a NUL-terminated
8084 * sequence of characters that were deleted and then the
8085 * characters that NL replaced.
8086 */
8087 if (State & REPLACE_FLAG)
8088 {
8089 /*
8090 * Do the next ins_char() in NORMAL state, to
8091 * prevent ins_char() from replacing characters and
8092 * avoiding showmatch().
8093 */
8094 oldState = State;
8095 State = NORMAL;
8096 /*
8097 * restore characters (blanks) deleted after cursor
8098 */
8099 while (cc > 0)
8100 {
8101 temp = curwin->w_cursor.col;
8102#ifdef FEAT_MBYTE
8103 mb_replace_pop_ins(cc);
8104#else
8105 ins_char(cc);
8106#endif
8107 curwin->w_cursor.col = temp;
8108 cc = replace_pop();
8109 }
8110 /* restore the characters that NL replaced */
8111 replace_pop_ins();
8112 State = oldState;
8113 }
8114 }
8115 did_ai = FALSE;
8116 }
8117 else
8118 {
8119 /*
8120 * Delete character(s) before the cursor.
8121 */
8122#ifdef FEAT_RIGHTLEFT
8123 if (revins_on) /* put cursor on last inserted char */
8124 dec_cursor();
8125#endif
8126 mincol = 0;
8127 /* keep indent */
8128 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8129#ifdef FEAT_RIGHTLEFT
8130 && !revins_on
8131#endif
8132 )
8133 {
8134 temp = curwin->w_cursor.col;
8135 beginline(BL_WHITE);
8136 if (curwin->w_cursor.col < (colnr_T)temp)
8137 mincol = curwin->w_cursor.col;
8138 curwin->w_cursor.col = temp;
8139 }
8140
8141 /*
8142 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8143 */
8144 if ( mode == BACKSPACE_CHAR
8145 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008146 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 && (*(ml_get_cursor() - 1) == TAB
8148 || (*(ml_get_cursor() - 1) == ' '
8149 && (!*inserted_space_p
8150 || arrow_used))))))
8151 {
8152 int ts;
8153 colnr_T vcol;
8154 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008155#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
8159 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008160 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 ts = curbuf->b_p_sw;
8162 else
8163 ts = curbuf->b_p_sts;
8164 /* Compute the virtual column where we want to be. Since
8165 * 'showbreak' may get in the way, need to get the last column of
8166 * the previous character. */
8167 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8168 dec_cursor();
8169 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8170 inc_cursor();
8171 want_vcol = (want_vcol / ts) * ts;
8172
8173 /* delete characters until we are at or before want_vcol */
8174 while (vcol > want_vcol
8175 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8176 {
8177 dec_cursor();
8178 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8179 if (State & REPLACE_FLAG)
8180 {
8181 /* Don't delete characters before the insert point when in
8182 * Replace mode */
8183 if (curwin->w_cursor.lnum != Insstart.lnum
8184 || curwin->w_cursor.col >= Insstart.col)
8185 {
8186#if 0 /* what was this for? It causes problems when sw != ts. */
8187 if (State == REPLACE && (int)vcol < want_vcol)
8188 {
8189 (void)del_char(FALSE);
8190 extra = 2; /* don't pop too much */
8191 }
8192 else
8193#endif
8194 replace_do_bs();
8195 }
8196 }
8197 else
8198 (void)del_char(FALSE);
8199 }
8200
8201 /* insert extra spaces until we are at want_vcol */
8202 while (vcol < want_vcol)
8203 {
8204 /* Remember the first char we inserted */
8205 if (curwin->w_cursor.lnum == Insstart.lnum
8206 && curwin->w_cursor.col < Insstart.col)
8207 Insstart.col = curwin->w_cursor.col;
8208
8209#ifdef FEAT_VREPLACE
8210 if (State & VREPLACE_FLAG)
8211 ins_char(' ');
8212 else
8213#endif
8214 {
8215 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008216 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008218#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 if (extra)
8220 replace_push_off(NUL);
8221 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 replace_push(NUL);
8224 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008225#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 if (extra == 2)
8227 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 }
8230 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8231 }
8232 }
8233
8234 /*
8235 * Delete upto starting point, start of line or previous word.
8236 */
8237 else do
8238 {
8239#ifdef FEAT_RIGHTLEFT
8240 if (!revins_on) /* put cursor on char to be deleted */
8241#endif
8242 dec_cursor();
8243
8244 /* start of word? */
8245 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8246 {
8247 mode = BACKSPACE_WORD_NOT_SPACE;
8248 temp = vim_iswordc(gchar_cursor());
8249 }
8250 /* end of word? */
8251 else if (mode == BACKSPACE_WORD_NOT_SPACE
8252 && (vim_isspace(cc = gchar_cursor())
8253 || vim_iswordc(cc) != temp))
8254 {
8255#ifdef FEAT_RIGHTLEFT
8256 if (!revins_on)
8257#endif
8258 inc_cursor();
8259#ifdef FEAT_RIGHTLEFT
8260 else if (State & REPLACE_FLAG)
8261 dec_cursor();
8262#endif
8263 break;
8264 }
8265 if (State & REPLACE_FLAG)
8266 replace_do_bs();
8267 else
8268 {
8269#ifdef FEAT_MBYTE
8270 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008271 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272#endif
8273 (void)del_char(FALSE);
8274#ifdef FEAT_MBYTE
8275 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008276 * If there are combining characters and 'delcombine' is set
8277 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 * character.
8279 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008280 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 inc_cursor();
8282#endif
8283#ifdef FEAT_RIGHTLEFT
8284 if (revins_chars)
8285 {
8286 revins_chars--;
8287 revins_legal++;
8288 }
8289 if (revins_on && gchar_cursor() == NUL)
8290 break;
8291#endif
8292 }
8293 /* Just a single backspace?: */
8294 if (mode == BACKSPACE_CHAR)
8295 break;
8296 } while (
8297#ifdef FEAT_RIGHTLEFT
8298 revins_on ||
8299#endif
8300 (curwin->w_cursor.col > mincol
8301 && (curwin->w_cursor.lnum != Insstart.lnum
8302 || curwin->w_cursor.col != Insstart.col)));
8303 did_backspace = TRUE;
8304 }
8305#ifdef FEAT_SMARTINDENT
8306 did_si = FALSE;
8307 can_si = FALSE;
8308 can_si_back = FALSE;
8309#endif
8310 if (curwin->w_cursor.col <= 1)
8311 did_ai = FALSE;
8312 /*
8313 * It's a little strange to put backspaces into the redo
8314 * buffer, but it makes auto-indent a lot easier to deal
8315 * with.
8316 */
8317 AppendCharToRedobuff(c);
8318
8319 /* If deleted before the insertion point, adjust it */
8320 if (curwin->w_cursor.lnum == Insstart.lnum
8321 && curwin->w_cursor.col < Insstart.col)
8322 Insstart.col = curwin->w_cursor.col;
8323
8324 /* vi behaviour: the cursor moves backward but the character that
8325 * was there remains visible
8326 * Vim behaviour: the cursor moves backward and the character that
8327 * was there is erased from the screen.
8328 * We can emulate the vi behaviour by pretending there is a dollar
8329 * displayed even when there isn't.
8330 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8331 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8332 dollar_vcol = curwin->w_virtcol;
8333
8334 return did_backspace;
8335}
8336
8337#ifdef FEAT_MOUSE
8338 static void
8339ins_mouse(c)
8340 int c;
8341{
8342 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008343 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345# ifdef FEAT_GUI
8346 /* When GUI is active, also move/paste when 'mouse' is empty */
8347 if (!gui.in_use)
8348# endif
8349 if (!mouse_has(MOUSE_INSERT))
8350 return;
8351
8352 undisplay_dollar();
8353 tpos = curwin->w_cursor;
8354 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8355 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008356#ifdef FEAT_WINDOWS
8357 win_T *new_curwin = curwin;
8358
8359 if (curwin != old_curwin && win_valid(old_curwin))
8360 {
8361 /* Mouse took us to another window. We need to go back to the
8362 * previous one to stop insert there properly. */
8363 curwin = old_curwin;
8364 curbuf = curwin->w_buffer;
8365 }
8366#endif
8367 start_arrow(curwin == old_curwin ? &tpos : NULL);
8368#ifdef FEAT_WINDOWS
8369 if (curwin != new_curwin && win_valid(new_curwin))
8370 {
8371 curwin = new_curwin;
8372 curbuf = curwin->w_buffer;
8373 }
8374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375# ifdef FEAT_CINDENT
8376 can_cindent = TRUE;
8377# endif
8378 }
8379
8380#ifdef FEAT_WINDOWS
8381 /* redraw status lines (in case another window became active) */
8382 redraw_statuslines();
8383#endif
8384}
8385
8386 static void
8387ins_mousescroll(up)
8388 int up;
8389{
8390 pos_T tpos;
8391# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8392 win_T *old_curwin;
8393# endif
8394
8395 tpos = curwin->w_cursor;
8396
8397# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8398 old_curwin = curwin;
8399
8400 /* Currently the mouse coordinates are only known in the GUI. */
8401 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8402 {
8403 int row, col;
8404
8405 row = mouse_row;
8406 col = mouse_col;
8407
8408 /* find the window at the pointer coordinates */
8409 curwin = mouse_find_win(&row, &col);
8410 curbuf = curwin->w_buffer;
8411 }
8412 if (curwin == old_curwin)
8413# endif
8414 undisplay_dollar();
8415
8416 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8417 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8418 else
8419 scroll_redraw(up, 3L);
8420
8421# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8422 curwin->w_redr_status = TRUE;
8423
8424 curwin = old_curwin;
8425 curbuf = curwin->w_buffer;
8426# endif
8427
8428 if (!equalpos(curwin->w_cursor, tpos))
8429 {
8430 start_arrow(&tpos);
8431# ifdef FEAT_CINDENT
8432 can_cindent = TRUE;
8433# endif
8434 }
8435}
8436#endif
8437
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008438#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008439 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008440ins_tabline(c)
8441 int c;
8442{
8443 /* We will be leaving the current window, unless closing another tab. */
8444 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8445 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8446 {
8447 undisplay_dollar();
8448 start_arrow(&curwin->w_cursor);
8449# ifdef FEAT_CINDENT
8450 can_cindent = TRUE;
8451# endif
8452 }
8453
8454 if (c == K_TABLINE)
8455 goto_tabpage(current_tab);
8456 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008457 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008458 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008459 redraw_statuslines(); /* will redraw the tabline when needed */
8460 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008461}
8462#endif
8463
8464#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 void
8466ins_scroll()
8467{
8468 pos_T tpos;
8469
8470 undisplay_dollar();
8471 tpos = curwin->w_cursor;
8472 if (gui_do_scroll())
8473 {
8474 start_arrow(&tpos);
8475# ifdef FEAT_CINDENT
8476 can_cindent = TRUE;
8477# endif
8478 }
8479}
8480
8481 void
8482ins_horscroll()
8483{
8484 pos_T tpos;
8485
8486 undisplay_dollar();
8487 tpos = curwin->w_cursor;
8488 if (gui_do_horiz_scroll())
8489 {
8490 start_arrow(&tpos);
8491# ifdef FEAT_CINDENT
8492 can_cindent = TRUE;
8493# endif
8494 }
8495}
8496#endif
8497
8498 static void
8499ins_left()
8500{
8501 pos_T tpos;
8502
8503#ifdef FEAT_FOLDING
8504 if ((fdo_flags & FDO_HOR) && KeyTyped)
8505 foldOpenCursor();
8506#endif
8507 undisplay_dollar();
8508 tpos = curwin->w_cursor;
8509 if (oneleft() == OK)
8510 {
8511 start_arrow(&tpos);
8512#ifdef FEAT_RIGHTLEFT
8513 /* If exit reversed string, position is fixed */
8514 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8515 revins_legal++;
8516 revins_chars++;
8517#endif
8518 }
8519
8520 /*
8521 * if 'whichwrap' set for cursor in insert mode may go to
8522 * previous line
8523 */
8524 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8525 {
8526 start_arrow(&tpos);
8527 --(curwin->w_cursor.lnum);
8528 coladvance((colnr_T)MAXCOL);
8529 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8530 }
8531 else
8532 vim_beep();
8533}
8534
8535 static void
8536ins_home(c)
8537 int c;
8538{
8539 pos_T tpos;
8540
8541#ifdef FEAT_FOLDING
8542 if ((fdo_flags & FDO_HOR) && KeyTyped)
8543 foldOpenCursor();
8544#endif
8545 undisplay_dollar();
8546 tpos = curwin->w_cursor;
8547 if (c == K_C_HOME)
8548 curwin->w_cursor.lnum = 1;
8549 curwin->w_cursor.col = 0;
8550#ifdef FEAT_VIRTUALEDIT
8551 curwin->w_cursor.coladd = 0;
8552#endif
8553 curwin->w_curswant = 0;
8554 start_arrow(&tpos);
8555}
8556
8557 static void
8558ins_end(c)
8559 int c;
8560{
8561 pos_T tpos;
8562
8563#ifdef FEAT_FOLDING
8564 if ((fdo_flags & FDO_HOR) && KeyTyped)
8565 foldOpenCursor();
8566#endif
8567 undisplay_dollar();
8568 tpos = curwin->w_cursor;
8569 if (c == K_C_END)
8570 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8571 coladvance((colnr_T)MAXCOL);
8572 curwin->w_curswant = MAXCOL;
8573
8574 start_arrow(&tpos);
8575}
8576
8577 static void
8578ins_s_left()
8579{
8580#ifdef FEAT_FOLDING
8581 if ((fdo_flags & FDO_HOR) && KeyTyped)
8582 foldOpenCursor();
8583#endif
8584 undisplay_dollar();
8585 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8586 {
8587 start_arrow(&curwin->w_cursor);
8588 (void)bck_word(1L, FALSE, FALSE);
8589 curwin->w_set_curswant = TRUE;
8590 }
8591 else
8592 vim_beep();
8593}
8594
8595 static void
8596ins_right()
8597{
8598#ifdef FEAT_FOLDING
8599 if ((fdo_flags & FDO_HOR) && KeyTyped)
8600 foldOpenCursor();
8601#endif
8602 undisplay_dollar();
8603 if (gchar_cursor() != NUL || virtual_active()
8604 )
8605 {
8606 start_arrow(&curwin->w_cursor);
8607 curwin->w_set_curswant = TRUE;
8608#ifdef FEAT_VIRTUALEDIT
8609 if (virtual_active())
8610 oneright();
8611 else
8612#endif
8613 {
8614#ifdef FEAT_MBYTE
8615 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008616 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 else
8618#endif
8619 ++curwin->w_cursor.col;
8620 }
8621
8622#ifdef FEAT_RIGHTLEFT
8623 revins_legal++;
8624 if (revins_chars)
8625 revins_chars--;
8626#endif
8627 }
8628 /* if 'whichwrap' set for cursor in insert mode, may move the
8629 * cursor to the next line */
8630 else if (vim_strchr(p_ww, ']') != NULL
8631 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8632 {
8633 start_arrow(&curwin->w_cursor);
8634 curwin->w_set_curswant = TRUE;
8635 ++curwin->w_cursor.lnum;
8636 curwin->w_cursor.col = 0;
8637 }
8638 else
8639 vim_beep();
8640}
8641
8642 static void
8643ins_s_right()
8644{
8645#ifdef FEAT_FOLDING
8646 if ((fdo_flags & FDO_HOR) && KeyTyped)
8647 foldOpenCursor();
8648#endif
8649 undisplay_dollar();
8650 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8651 || gchar_cursor() != NUL)
8652 {
8653 start_arrow(&curwin->w_cursor);
8654 (void)fwd_word(1L, FALSE, 0);
8655 curwin->w_set_curswant = TRUE;
8656 }
8657 else
8658 vim_beep();
8659}
8660
8661 static void
8662ins_up(startcol)
8663 int startcol; /* when TRUE move to Insstart.col */
8664{
8665 pos_T tpos;
8666 linenr_T old_topline = curwin->w_topline;
8667#ifdef FEAT_DIFF
8668 int old_topfill = curwin->w_topfill;
8669#endif
8670
8671 undisplay_dollar();
8672 tpos = curwin->w_cursor;
8673 if (cursor_up(1L, TRUE) == OK)
8674 {
8675 if (startcol)
8676 coladvance(getvcol_nolist(&Insstart));
8677 if (old_topline != curwin->w_topline
8678#ifdef FEAT_DIFF
8679 || old_topfill != curwin->w_topfill
8680#endif
8681 )
8682 redraw_later(VALID);
8683 start_arrow(&tpos);
8684#ifdef FEAT_CINDENT
8685 can_cindent = TRUE;
8686#endif
8687 }
8688 else
8689 vim_beep();
8690}
8691
8692 static void
8693ins_pageup()
8694{
8695 pos_T tpos;
8696
8697 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008698
8699#ifdef FEAT_WINDOWS
8700 if (mod_mask & MOD_MASK_CTRL)
8701 {
8702 /* <C-PageUp>: tab page back */
8703 goto_tabpage(-1);
8704 return;
8705 }
8706#endif
8707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 tpos = curwin->w_cursor;
8709 if (onepage(BACKWARD, 1L) == OK)
8710 {
8711 start_arrow(&tpos);
8712#ifdef FEAT_CINDENT
8713 can_cindent = TRUE;
8714#endif
8715 }
8716 else
8717 vim_beep();
8718}
8719
8720 static void
8721ins_down(startcol)
8722 int startcol; /* when TRUE move to Insstart.col */
8723{
8724 pos_T tpos;
8725 linenr_T old_topline = curwin->w_topline;
8726#ifdef FEAT_DIFF
8727 int old_topfill = curwin->w_topfill;
8728#endif
8729
8730 undisplay_dollar();
8731 tpos = curwin->w_cursor;
8732 if (cursor_down(1L, TRUE) == OK)
8733 {
8734 if (startcol)
8735 coladvance(getvcol_nolist(&Insstart));
8736 if (old_topline != curwin->w_topline
8737#ifdef FEAT_DIFF
8738 || old_topfill != curwin->w_topfill
8739#endif
8740 )
8741 redraw_later(VALID);
8742 start_arrow(&tpos);
8743#ifdef FEAT_CINDENT
8744 can_cindent = TRUE;
8745#endif
8746 }
8747 else
8748 vim_beep();
8749}
8750
8751 static void
8752ins_pagedown()
8753{
8754 pos_T tpos;
8755
8756 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008757
8758#ifdef FEAT_WINDOWS
8759 if (mod_mask & MOD_MASK_CTRL)
8760 {
8761 /* <C-PageDown>: tab page forward */
8762 goto_tabpage(0);
8763 return;
8764 }
8765#endif
8766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 tpos = curwin->w_cursor;
8768 if (onepage(FORWARD, 1L) == OK)
8769 {
8770 start_arrow(&tpos);
8771#ifdef FEAT_CINDENT
8772 can_cindent = TRUE;
8773#endif
8774 }
8775 else
8776 vim_beep();
8777}
8778
8779#ifdef FEAT_DND
8780 static void
8781ins_drop()
8782{
8783 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8784}
8785#endif
8786
8787/*
8788 * Handle TAB in Insert or Replace mode.
8789 * Return TRUE when the TAB needs to be inserted like a normal character.
8790 */
8791 static int
8792ins_tab()
8793{
8794 int ind;
8795 int i;
8796 int temp;
8797
8798 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8799 Insstart_blank_vcol = get_nolist_virtcol();
8800 if (echeck_abbr(TAB + ABBR_OFF))
8801 return FALSE;
8802
8803 ind = inindent(0);
8804#ifdef FEAT_CINDENT
8805 if (ind)
8806 can_cindent = FALSE;
8807#endif
8808
8809 /*
8810 * When nothing special, insert TAB like a normal character
8811 */
8812 if (!curbuf->b_p_et
8813 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8814 && curbuf->b_p_sts == 0)
8815 return TRUE;
8816
8817 if (stop_arrow() == FAIL)
8818 return TRUE;
8819
8820 did_ai = FALSE;
8821#ifdef FEAT_SMARTINDENT
8822 did_si = FALSE;
8823 can_si = FALSE;
8824 can_si_back = FALSE;
8825#endif
8826 AppendToRedobuff((char_u *)"\t");
8827
8828 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8829 temp = (int)curbuf->b_p_sw;
8830 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8831 temp = (int)curbuf->b_p_sts;
8832 else /* otherwise use 'tabstop' */
8833 temp = (int)curbuf->b_p_ts;
8834 temp -= get_nolist_virtcol() % temp;
8835
8836 /*
8837 * Insert the first space with ins_char(). It will delete one char in
8838 * replace mode. Insert the rest with ins_str(); it will not delete any
8839 * chars. For VREPLACE mode, we use ins_char() for all characters.
8840 */
8841 ins_char(' ');
8842 while (--temp > 0)
8843 {
8844#ifdef FEAT_VREPLACE
8845 if (State & VREPLACE_FLAG)
8846 ins_char(' ');
8847 else
8848#endif
8849 {
8850 ins_str((char_u *)" ");
8851 if (State & REPLACE_FLAG) /* no char replaced */
8852 replace_push(NUL);
8853 }
8854 }
8855
8856 /*
8857 * When 'expandtab' not set: Replace spaces by TABs where possible.
8858 */
8859 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8860 {
8861 char_u *ptr;
8862#ifdef FEAT_VREPLACE
8863 char_u *saved_line = NULL; /* init for GCC */
8864 pos_T pos;
8865#endif
8866 pos_T fpos;
8867 pos_T *cursor;
8868 colnr_T want_vcol, vcol;
8869 int change_col = -1;
8870 int save_list = curwin->w_p_list;
8871
8872 /*
8873 * Get the current line. For VREPLACE mode, don't make real changes
8874 * yet, just work on a copy of the line.
8875 */
8876#ifdef FEAT_VREPLACE
8877 if (State & VREPLACE_FLAG)
8878 {
8879 pos = curwin->w_cursor;
8880 cursor = &pos;
8881 saved_line = vim_strsave(ml_get_curline());
8882 if (saved_line == NULL)
8883 return FALSE;
8884 ptr = saved_line + pos.col;
8885 }
8886 else
8887#endif
8888 {
8889 ptr = ml_get_cursor();
8890 cursor = &curwin->w_cursor;
8891 }
8892
8893 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8894 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8895 curwin->w_p_list = FALSE;
8896
8897 /* Find first white before the cursor */
8898 fpos = curwin->w_cursor;
8899 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8900 {
8901 --fpos.col;
8902 --ptr;
8903 }
8904
8905 /* In Replace mode, don't change characters before the insert point. */
8906 if ((State & REPLACE_FLAG)
8907 && fpos.lnum == Insstart.lnum
8908 && fpos.col < Insstart.col)
8909 {
8910 ptr += Insstart.col - fpos.col;
8911 fpos.col = Insstart.col;
8912 }
8913
8914 /* compute virtual column numbers of first white and cursor */
8915 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8916 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8917
8918 /* Use as many TABs as possible. Beware of 'showbreak' and
8919 * 'linebreak' adding extra virtual columns. */
8920 while (vim_iswhite(*ptr))
8921 {
8922 i = lbr_chartabsize((char_u *)"\t", vcol);
8923 if (vcol + i > want_vcol)
8924 break;
8925 if (*ptr != TAB)
8926 {
8927 *ptr = TAB;
8928 if (change_col < 0)
8929 {
8930 change_col = fpos.col; /* Column of first change */
8931 /* May have to adjust Insstart */
8932 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8933 Insstart.col = fpos.col;
8934 }
8935 }
8936 ++fpos.col;
8937 ++ptr;
8938 vcol += i;
8939 }
8940
8941 if (change_col >= 0)
8942 {
8943 int repl_off = 0;
8944
8945 /* Skip over the spaces we need. */
8946 while (vcol < want_vcol && *ptr == ' ')
8947 {
8948 vcol += lbr_chartabsize(ptr, vcol);
8949 ++ptr;
8950 ++repl_off;
8951 }
8952 if (vcol > want_vcol)
8953 {
8954 /* Must have a char with 'showbreak' just before it. */
8955 --ptr;
8956 --repl_off;
8957 }
8958 fpos.col += repl_off;
8959
8960 /* Delete following spaces. */
8961 i = cursor->col - fpos.col;
8962 if (i > 0)
8963 {
8964 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8965 /* correct replace stack. */
8966 if ((State & REPLACE_FLAG)
8967#ifdef FEAT_VREPLACE
8968 && !(State & VREPLACE_FLAG)
8969#endif
8970 )
8971 for (temp = i; --temp >= 0; )
8972 replace_join(repl_off);
8973 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008974#ifdef FEAT_NETBEANS_INTG
8975 if (usingNetbeans)
8976 {
8977 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8978 (long)(i + 1));
8979 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8980 (char_u *)"\t", 1);
8981 }
8982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 cursor->col -= i;
8984
8985#ifdef FEAT_VREPLACE
8986 /*
8987 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8988 * backspacing over the changed spacing and then inserting the new
8989 * spacing.
8990 */
8991 if (State & VREPLACE_FLAG)
8992 {
8993 /* Backspace from real cursor to change_col */
8994 backspace_until_column(change_col);
8995
8996 /* Insert each char in saved_line from changed_col to
8997 * ptr-cursor */
8998 ins_bytes_len(saved_line + change_col,
8999 cursor->col - change_col);
9000 }
9001#endif
9002 }
9003
9004#ifdef FEAT_VREPLACE
9005 if (State & VREPLACE_FLAG)
9006 vim_free(saved_line);
9007#endif
9008 curwin->w_p_list = save_list;
9009 }
9010
9011 return FALSE;
9012}
9013
9014/*
9015 * Handle CR or NL in insert mode.
9016 * Return TRUE when out of memory or can't undo.
9017 */
9018 static int
9019ins_eol(c)
9020 int c;
9021{
9022 int i;
9023
9024 if (echeck_abbr(c + ABBR_OFF))
9025 return FALSE;
9026 if (stop_arrow() == FAIL)
9027 return TRUE;
9028 undisplay_dollar();
9029
9030 /*
9031 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9032 * character under the cursor. Only push a NUL on the replace stack,
9033 * nothing to put back when the NL is deleted.
9034 */
9035 if ((State & REPLACE_FLAG)
9036#ifdef FEAT_VREPLACE
9037 && !(State & VREPLACE_FLAG)
9038#endif
9039 )
9040 replace_push(NUL);
9041
9042 /*
9043 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9044 * replacing the next line, so we push all of the characters left on the
9045 * line onto the replace stack. This is not done here though, it is done
9046 * in open_line().
9047 */
9048
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009049#ifdef FEAT_VIRTUALEDIT
9050 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9051 * CTRL-O). */
9052 if (virtual_active() && curwin->w_cursor.coladd > 0)
9053 coladvance(getviscol());
9054#endif
9055
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056#ifdef FEAT_RIGHTLEFT
9057# ifdef FEAT_FKMAP
9058 if (p_altkeymap && p_fkmap)
9059 fkmap(NL);
9060# endif
9061 /* NL in reverse insert will always start in the end of
9062 * current line. */
9063 if (revins_on)
9064 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9065#endif
9066
9067 AppendToRedobuff(NL_STR);
9068 i = open_line(FORWARD,
9069#ifdef FEAT_COMMENTS
9070 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9071#endif
9072 0, old_indent);
9073 old_indent = 0;
9074#ifdef FEAT_CINDENT
9075 can_cindent = TRUE;
9076#endif
9077
9078 return (!i);
9079}
9080
9081#ifdef FEAT_DIGRAPHS
9082/*
9083 * Handle digraph in insert mode.
9084 * Returns character still to be inserted, or NUL when nothing remaining to be
9085 * done.
9086 */
9087 static int
9088ins_digraph()
9089{
9090 int c;
9091 int cc;
9092
9093 pc_status = PC_STATUS_UNSET;
9094 if (redrawing() && !char_avail())
9095 {
9096 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009097 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098
9099 edit_putchar('?', TRUE);
9100#ifdef FEAT_CMDL_INFO
9101 add_to_showcmd_c(Ctrl_K);
9102#endif
9103 }
9104
9105#ifdef USE_ON_FLY_SCROLL
9106 dont_scroll = TRUE; /* disallow scrolling here */
9107#endif
9108
9109 /* don't map the digraph chars. This also prevents the
9110 * mode message to be deleted when ESC is hit */
9111 ++no_mapping;
9112 ++allow_keys;
9113 c = safe_vgetc();
9114 --no_mapping;
9115 --allow_keys;
9116 if (IS_SPECIAL(c) || mod_mask) /* special key */
9117 {
9118#ifdef FEAT_CMDL_INFO
9119 clear_showcmd();
9120#endif
9121 insert_special(c, TRUE, FALSE);
9122 return NUL;
9123 }
9124 if (c != ESC)
9125 {
9126 if (redrawing() && !char_avail())
9127 {
9128 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009129 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130
9131 if (char2cells(c) == 1)
9132 {
9133 /* first remove the '?', otherwise it's restored when typing
9134 * an ESC next */
9135 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009136 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 edit_putchar(c, TRUE);
9138 }
9139#ifdef FEAT_CMDL_INFO
9140 add_to_showcmd_c(c);
9141#endif
9142 }
9143 ++no_mapping;
9144 ++allow_keys;
9145 cc = safe_vgetc();
9146 --no_mapping;
9147 --allow_keys;
9148 if (cc != ESC)
9149 {
9150 AppendToRedobuff((char_u *)CTRL_V_STR);
9151 c = getdigraph(c, cc, TRUE);
9152#ifdef FEAT_CMDL_INFO
9153 clear_showcmd();
9154#endif
9155 return c;
9156 }
9157 }
9158 edit_unputchar();
9159#ifdef FEAT_CMDL_INFO
9160 clear_showcmd();
9161#endif
9162 return NUL;
9163}
9164#endif
9165
9166/*
9167 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9168 * Returns the char to be inserted, or NUL if none found.
9169 */
9170 static int
9171ins_copychar(lnum)
9172 linenr_T lnum;
9173{
9174 int c;
9175 int temp;
9176 char_u *ptr, *prev_ptr;
9177
9178 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9179 {
9180 vim_beep();
9181 return NUL;
9182 }
9183
9184 /* try to advance to the cursor column */
9185 temp = 0;
9186 ptr = ml_get(lnum);
9187 prev_ptr = ptr;
9188 validate_virtcol();
9189 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9190 {
9191 prev_ptr = ptr;
9192 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9193 }
9194 if ((colnr_T)temp > curwin->w_virtcol)
9195 ptr = prev_ptr;
9196
9197#ifdef FEAT_MBYTE
9198 c = (*mb_ptr2char)(ptr);
9199#else
9200 c = *ptr;
9201#endif
9202 if (c == NUL)
9203 vim_beep();
9204 return c;
9205}
9206
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009207/*
9208 * CTRL-Y or CTRL-E typed in Insert mode.
9209 */
9210 static int
9211ins_ctrl_ey(tc)
9212 int tc;
9213{
9214 int c = tc;
9215
9216#ifdef FEAT_INS_EXPAND
9217 if (ctrl_x_mode == CTRL_X_SCROLL)
9218 {
9219 if (c == Ctrl_Y)
9220 scrolldown_clamp();
9221 else
9222 scrollup_clamp();
9223 redraw_later(VALID);
9224 }
9225 else
9226#endif
9227 {
9228 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9229 if (c != NUL)
9230 {
9231 long tw_save;
9232
9233 /* The character must be taken literally, insert like it
9234 * was typed after a CTRL-V, and pretend 'textwidth'
9235 * wasn't set. Digits, 'o' and 'x' are special after a
9236 * CTRL-V, don't use it for these. */
9237 if (c < 256 && !isalnum(c))
9238 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9239 tw_save = curbuf->b_p_tw;
9240 curbuf->b_p_tw = -1;
9241 insert_special(c, TRUE, FALSE);
9242 curbuf->b_p_tw = tw_save;
9243#ifdef FEAT_RIGHTLEFT
9244 revins_chars++;
9245 revins_legal++;
9246#endif
9247 c = Ctrl_V; /* pretend CTRL-V is last character */
9248 auto_format(FALSE, TRUE);
9249 }
9250 }
9251 return c;
9252}
9253
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254#ifdef FEAT_SMARTINDENT
9255/*
9256 * Try to do some very smart auto-indenting.
9257 * Used when inserting a "normal" character.
9258 */
9259 static void
9260ins_try_si(c)
9261 int c;
9262{
9263 pos_T *pos, old_pos;
9264 char_u *ptr;
9265 int i;
9266 int temp;
9267
9268 /*
9269 * do some very smart indenting when entering '{' or '}'
9270 */
9271 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9272 {
9273 /*
9274 * for '}' set indent equal to indent of line containing matching '{'
9275 */
9276 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9277 {
9278 old_pos = curwin->w_cursor;
9279 /*
9280 * If the matching '{' has a ')' immediately before it (ignoring
9281 * white-space), then line up with the start of the line
9282 * containing the matching '(' if there is one. This handles the
9283 * case where an "if (..\n..) {" statement continues over multiple
9284 * lines -- webb
9285 */
9286 ptr = ml_get(pos->lnum);
9287 i = pos->col;
9288 if (i > 0) /* skip blanks before '{' */
9289 while (--i > 0 && vim_iswhite(ptr[i]))
9290 ;
9291 curwin->w_cursor.lnum = pos->lnum;
9292 curwin->w_cursor.col = i;
9293 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9294 curwin->w_cursor = *pos;
9295 i = get_indent();
9296 curwin->w_cursor = old_pos;
9297#ifdef FEAT_VREPLACE
9298 if (State & VREPLACE_FLAG)
9299 change_indent(INDENT_SET, i, FALSE, NUL);
9300 else
9301#endif
9302 (void)set_indent(i, SIN_CHANGED);
9303 }
9304 else if (curwin->w_cursor.col > 0)
9305 {
9306 /*
9307 * when inserting '{' after "O" reduce indent, but not
9308 * more than indent of previous line
9309 */
9310 temp = TRUE;
9311 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9312 {
9313 old_pos = curwin->w_cursor;
9314 i = get_indent();
9315 while (curwin->w_cursor.lnum > 1)
9316 {
9317 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9318
9319 /* ignore empty lines and lines starting with '#'. */
9320 if (*ptr != '#' && *ptr != NUL)
9321 break;
9322 }
9323 if (get_indent() >= i)
9324 temp = FALSE;
9325 curwin->w_cursor = old_pos;
9326 }
9327 if (temp)
9328 shift_line(TRUE, FALSE, 1);
9329 }
9330 }
9331
9332 /*
9333 * set indent of '#' always to 0
9334 */
9335 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9336 {
9337 /* remember current indent for next line */
9338 old_indent = get_indent();
9339 (void)set_indent(0, SIN_CHANGED);
9340 }
9341
9342 /* Adjust ai_col, the char at this position can be deleted. */
9343 if (ai_col > curwin->w_cursor.col)
9344 ai_col = curwin->w_cursor.col;
9345}
9346#endif
9347
9348/*
9349 * Get the value that w_virtcol would have when 'list' is off.
9350 * Unless 'cpo' contains the 'L' flag.
9351 */
9352 static colnr_T
9353get_nolist_virtcol()
9354{
9355 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9356 return getvcol_nolist(&curwin->w_cursor);
9357 validate_virtcol();
9358 return curwin->w_virtcol;
9359}