blob: dcf19b5bf6edc0b998980a9921621544a8f5cd2b [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 Moolenaar488c6512005-08-11 20:09:58 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^S^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 Moolenaar488c6512005-08-11 20:09:58 +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 Moolenaar8b6144b2006-02-08 09:20:24 +000072 char_u *cp_extra; /* extra menu text (allocated, can be NULL) */
73 char_u *cp_info; /* verbose info (can be NULL) */
74 char_u cp_kind; /* kind of match, single letter, or NUL */
75 char_u *cp_fname; /* file containing the match, allocated when
76 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
78 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079};
80
Bram Moolenaar572cb562005-08-05 21:35:02 +000081#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#define FREE_FNAME (2)
83
84/*
85 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000086 * "compl_first_match" points to the start of the list.
87 * "compl_curr_match" points to the currently selected entry.
88 * "compl_shown_match" is different from compl_curr_match during
89 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000091static compl_T *compl_first_match = NULL;
92static compl_T *compl_curr_match = NULL;
93static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaara6557602006-02-04 22:43:20 +000095/* When "compl_leader" is not NULL only matches that start with this string
96 * are used. */
97static char_u *compl_leader = NULL;
98
Bram Moolenaarc7453f52006-02-10 23:20:28 +000099static int compl_get_longest = FALSE; /* put longest common string
100 in compl_leader */
101
Bram Moolenaara6557602006-02-04 22:43:20 +0000102static int compl_used_match; /* Selected one of the matches. When
103 FALSE the match was edited or using
104 the longest common string. */
105
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000106/* When the first completion is done "compl_started" is set. When it's
107 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000108static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000109
Bram Moolenaar572cb562005-08-05 21:35:02 +0000110static int compl_matches = 0;
111static char_u *compl_pattern = NULL;
112static int compl_direction = FORWARD;
113static int compl_shows_dir = FORWARD;
114static int compl_pending = FALSE;
115static pos_T compl_startpos;
116static colnr_T compl_col = 0; /* column where the text starts
117 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000118static char_u *compl_orig_text = NULL; /* text as it was before
119 * completion started */
120static int compl_cont_mode = 0;
121static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000123static void ins_ctrl_x __ARGS((void));
124static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000125static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000126static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000127static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000129static void ins_compl_upd_pum __ARGS((void));
130static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000131static int pum_wanted __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000132static int pum_two_or_more __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000133static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000134static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static void ins_compl_free __ARGS((void));
136static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000137static int ins_compl_bs __ARGS((void));
138static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000139static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000140static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000141static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000143static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void ins_compl_delete __ARGS((void));
145static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000146static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000147static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000148static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000149static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000150static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static int ins_complete __ARGS((int c));
152static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
153#endif /* FEAT_INS_EXPAND */
154
155#define BACKSPACE_CHAR 1
156#define BACKSPACE_WORD 2
157#define BACKSPACE_WORD_NOT_SPACE 3
158#define BACKSPACE_LINE 4
159
Bram Moolenaar754b5602006-02-09 23:53:20 +0000160static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161static void ins_ctrl_v __ARGS((void));
162static void undisplay_dollar __ARGS((void));
163static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static void check_auto_format __ARGS((int));
166static void redo_literal __ARGS((int c));
167static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000168#ifdef FEAT_SYN_HL
169static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000170static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000171static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
174static int echeck_abbr __ARGS((int));
175static void replace_push_off __ARGS((int c));
176static int replace_pop __ARGS((void));
177static void replace_join __ARGS((int off));
178static void replace_pop_ins __ARGS((void));
179#ifdef FEAT_MBYTE
180static void mb_replace_pop_ins __ARGS((int cc));
181#endif
182static void replace_flush __ARGS((void));
183static void replace_do_bs __ARGS((void));
184#ifdef FEAT_CINDENT
185static int cindent_on __ARGS((void));
186#endif
187static void ins_reg __ARGS((void));
188static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000189static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000190static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#ifdef FEAT_RIGHTLEFT
192static void ins_ctrl_ __ARGS((void));
193#endif
194#ifdef FEAT_VISUAL
195static int ins_start_select __ARGS((int c));
196#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000197static void ins_insert __ARGS((int replaceState));
198static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199static void ins_shift __ARGS((int c, int lastc));
200static void ins_del __ARGS((void));
201static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
202#ifdef FEAT_MOUSE
203static void ins_mouse __ARGS((int c));
204static void ins_mousescroll __ARGS((int up));
205#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000206#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
207static void ins_tabline __ARGS((int c));
208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209static void ins_left __ARGS((void));
210static void ins_home __ARGS((int c));
211static void ins_end __ARGS((int c));
212static void ins_s_left __ARGS((void));
213static void ins_right __ARGS((void));
214static void ins_s_right __ARGS((void));
215static void ins_up __ARGS((int startcol));
216static void ins_pageup __ARGS((void));
217static void ins_down __ARGS((int startcol));
218static void ins_pagedown __ARGS((void));
219#ifdef FEAT_DND
220static void ins_drop __ARGS((void));
221#endif
222static int ins_tab __ARGS((void));
223static int ins_eol __ARGS((int c));
224#ifdef FEAT_DIGRAPHS
225static int ins_digraph __ARGS((void));
226#endif
227static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000228static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#ifdef FEAT_SMARTINDENT
230static void ins_try_si __ARGS((int c));
231#endif
232static colnr_T get_nolist_virtcol __ARGS((void));
233
234static colnr_T Insstart_textlen; /* length of line when insert started */
235static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
236
237static char_u *last_insert = NULL; /* the text of the previous insert,
238 K_SPECIAL and CSI are escaped */
239static int last_insert_skip; /* nr of chars in front of previous insert */
240static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000241static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242
243#ifdef FEAT_CINDENT
244static int can_cindent; /* may do cindenting on this line */
245#endif
246
247static int old_indent = 0; /* for ^^D command in insert mode */
248
249#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000250static int revins_on; /* reverse insert mode on */
251static int revins_chars; /* how much to skip after edit */
252static int revins_legal; /* was the last char 'legal'? */
253static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#endif
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256static int ins_need_undo; /* call u_save() before inserting a
257 char. Set when edit() is called.
258 after that arrow_used is used. */
259
260static int did_add_space = FALSE; /* auto_format() added an extra space
261 under the cursor */
262
263/*
264 * edit(): Start inserting text.
265 *
266 * "cmdchar" can be:
267 * 'i' normal insert command
268 * 'a' normal append command
269 * 'R' replace command
270 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
271 * but still only one <CR> is inserted. The <Esc> is not used for redo.
272 * 'g' "gI" command.
273 * 'V' "gR" command for Virtual Replace mode.
274 * 'v' "gr" command for single character Virtual Replace mode.
275 *
276 * This function is not called recursively. For CTRL-O commands, it returns
277 * and lets the caller handle the Normal-mode command.
278 *
279 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
280 */
281 int
282edit(cmdchar, startln, count)
283 int cmdchar;
284 int startln; /* if set, insert at start of line */
285 long count;
286{
287 int c = 0;
288 char_u *ptr;
289 int lastc;
290 colnr_T mincol;
291 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 int i;
293 int did_backspace = TRUE; /* previous char was backspace */
294#ifdef FEAT_CINDENT
295 int line_is_white = FALSE; /* line is empty before insert */
296#endif
297 linenr_T old_topline = 0; /* topline before insertion */
298#ifdef FEAT_DIFF
299 int old_topfill = -1;
300#endif
301 int inserted_space = FALSE; /* just inserted a space */
302 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000303 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000305 /* Remember whether editing was restarted after CTRL-O. */
306 did_restart_edit = restart_edit;
307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 /* sleep before redrawing, needed for "CTRL-O :" that results in an
309 * error message */
310 check_for_delay(TRUE);
311
312#ifdef HAVE_SANDBOX
313 /* Don't allow inserting in the sandbox. */
314 if (sandbox != 0)
315 {
316 EMSG(_(e_sandbox));
317 return FALSE;
318 }
319#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000320 /* Don't allow changes in the buffer while editing the cmdline. The
321 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000322 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000323 {
324 EMSG(_(e_secure));
325 return FALSE;
326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
328#ifdef FEAT_INS_EXPAND
329 ins_compl_clear(); /* clear stuff for CTRL-X mode */
330#endif
331
Bram Moolenaar843ee412004-06-30 16:16:41 +0000332#ifdef FEAT_AUTOCMD
333 /*
334 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
335 */
336 if (cmdchar != 'r' && cmdchar != 'v')
337 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000338# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000339 if (cmdchar == 'R')
340 ptr = (char_u *)"r";
341 else if (cmdchar == 'V')
342 ptr = (char_u *)"v";
343 else
344 ptr = (char_u *)"i";
345 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000346# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000347 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
348 }
349#endif
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#ifdef FEAT_MOUSE
352 /*
353 * When doing a paste with the middle mouse button, Insstart is set to
354 * where the paste started.
355 */
356 if (where_paste_started.lnum != 0)
357 Insstart = where_paste_started;
358 else
359#endif
360 {
361 Insstart = curwin->w_cursor;
362 if (startln)
363 Insstart.col = 0;
364 }
365 Insstart_textlen = linetabsize(ml_get_curline());
366 Insstart_blank_vcol = MAXCOL;
367 if (!did_ai)
368 ai_col = 0;
369
370 if (cmdchar != NUL && restart_edit == 0)
371 {
372 ResetRedobuff();
373 AppendNumberToRedobuff(count);
374#ifdef FEAT_VREPLACE
375 if (cmdchar == 'V' || cmdchar == 'v')
376 {
377 /* "gR" or "gr" command */
378 AppendCharToRedobuff('g');
379 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
380 }
381 else
382#endif
383 {
384 AppendCharToRedobuff(cmdchar);
385 if (cmdchar == 'g') /* "gI" command */
386 AppendCharToRedobuff('I');
387 else if (cmdchar == 'r') /* "r<CR>" command */
388 count = 1; /* insert only one <CR> */
389 }
390 }
391
392 if (cmdchar == 'R')
393 {
394#ifdef FEAT_FKMAP
395 if (p_fkmap && p_ri)
396 {
397 beep_flush();
398 EMSG(farsi_text_3); /* encoded in Farsi */
399 State = INSERT;
400 }
401 else
402#endif
403 State = REPLACE;
404 }
405#ifdef FEAT_VREPLACE
406 else if (cmdchar == 'V' || cmdchar == 'v')
407 {
408 State = VREPLACE;
409 replaceState = VREPLACE;
410 orig_line_count = curbuf->b_ml.ml_line_count;
411 vr_lines_changed = 1;
412 }
413#endif
414 else
415 State = INSERT;
416
417 stop_insert_mode = FALSE;
418
419 /*
420 * Need to recompute the cursor position, it might move when the cursor is
421 * on a TAB or special character.
422 */
423 curs_columns(TRUE);
424
425 /*
426 * Enable langmap or IME, indicated by 'iminsert'.
427 * Note that IME may enabled/disabled without us noticing here, thus the
428 * 'iminsert' value may not reflect what is actually used. It is updated
429 * when hitting <Esc>.
430 */
431 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
432 State |= LANGMAP;
433#ifdef USE_IM_CONTROL
434 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
435#endif
436
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#ifdef FEAT_MOUSE
438 setmouse();
439#endif
440#ifdef FEAT_CMDL_INFO
441 clear_showcmd();
442#endif
443#ifdef FEAT_RIGHTLEFT
444 /* there is no reverse replace mode */
445 revins_on = (State == INSERT && p_ri);
446 if (revins_on)
447 undisplay_dollar();
448 revins_chars = 0;
449 revins_legal = 0;
450 revins_scol = -1;
451#endif
452
453 /*
454 * Handle restarting Insert mode.
455 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
456 * restart_edit non-zero, and something in the stuff buffer.
457 */
458 if (restart_edit != 0 && stuff_empty())
459 {
460#ifdef FEAT_MOUSE
461 /*
462 * After a paste we consider text typed to be part of the insert for
463 * the pasted text. You can backspace over the pasted text too.
464 */
465 if (where_paste_started.lnum)
466 arrow_used = FALSE;
467 else
468#endif
469 arrow_used = TRUE;
470 restart_edit = 0;
471
472 /*
473 * If the cursor was after the end-of-line before the CTRL-O and it is
474 * now at the end-of-line, put it after the end-of-line (this is not
475 * correct in very rare cases).
476 * Also do this if curswant is greater than the current virtual
477 * column. Eg after "^O$" or "^O80|".
478 */
479 validate_virtcol();
480 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000481 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 || curwin->w_curswant > curwin->w_virtcol)
483 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
484 {
485 if (ptr[1] == NUL)
486 ++curwin->w_cursor.col;
487#ifdef FEAT_MBYTE
488 else if (has_mbyte)
489 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000490 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 if (ptr[i] == NUL)
492 curwin->w_cursor.col += i;
493 }
494#endif
495 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000496 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498 else
499 arrow_used = FALSE;
500
501 /* we are in insert mode now, don't need to start it anymore */
502 need_start_insertmode = FALSE;
503
504 /* Need to save the line for undo before inserting the first char. */
505 ins_need_undo = TRUE;
506
507#ifdef FEAT_MOUSE
508 where_paste_started.lnum = 0;
509#endif
510#ifdef FEAT_CINDENT
511 can_cindent = TRUE;
512#endif
513#ifdef FEAT_FOLDING
514 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
515 * restarting. */
516 if (!p_im && did_restart_edit == 0)
517 foldOpenCursor();
518#endif
519
520 /*
521 * If 'showmode' is set, show the current (insert/replace/..) mode.
522 * A warning message for changing a readonly file is given here, before
523 * actually changing anything. It's put after the mode, if any.
524 */
525 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000526 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 i = showmode();
528
529 if (!p_im && did_restart_edit == 0)
530 change_warning(i + 1);
531
532#ifdef CURSOR_SHAPE
533 ui_cursor_shape(); /* may show different cursor shape */
534#endif
535#ifdef FEAT_DIGRAPHS
536 do_digraph(-1); /* clear digraphs */
537#endif
538
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000539 /*
540 * Get the current length of the redo buffer, those characters have to be
541 * skipped if we want to get to the inserted characters.
542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 ptr = get_inserted();
544 if (ptr == NULL)
545 new_insert_skip = 0;
546 else
547 {
548 new_insert_skip = (int)STRLEN(ptr);
549 vim_free(ptr);
550 }
551
552 old_indent = 0;
553
554 /*
555 * Main loop in Insert mode: repeat until Insert mode is left.
556 */
557 for (;;)
558 {
559#ifdef FEAT_RIGHTLEFT
560 if (!revins_legal)
561 revins_scol = -1; /* reset on illegal motions */
562 else
563 revins_legal = 0;
564#endif
565 if (arrow_used) /* don't repeat insert when arrow key used */
566 count = 0;
567
568 if (stop_insert_mode)
569 {
570 /* ":stopinsert" used or 'insertmode' reset */
571 count = 0;
572 goto doESCkey;
573 }
574
575 /* set curwin->w_curswant for next K_DOWN or K_UP */
576 if (!arrow_used)
577 curwin->w_set_curswant = TRUE;
578
579 /* If there is no typeahead may check for timestamps (e.g., for when a
580 * menu invoked a shell command). */
581 if (stuff_empty())
582 {
583 did_check_timestamps = FALSE;
584 if (need_check_timestamps)
585 check_timestamps(FALSE);
586 }
587
588 /*
589 * When emsg() was called msg_scroll will have been set.
590 */
591 msg_scroll = FALSE;
592
593#ifdef FEAT_GUI
594 /* When 'mousefocus' is set a mouse movement may have taken us to
595 * another window. "need_mouse_correct" may then be set because of an
596 * autocommand. */
597 if (need_mouse_correct)
598 gui_mouse_correct();
599#endif
600
601#ifdef FEAT_FOLDING
602 /* Open fold at the cursor line, according to 'foldopen'. */
603 if (fdo_flags & FDO_INSERT)
604 foldOpenCursor();
605 /* Close folds where the cursor isn't, according to 'foldclose' */
606 if (!char_avail())
607 foldCheckClose();
608#endif
609
610 /*
611 * If we inserted a character at the last position of the last line in
612 * the window, scroll the window one line up. This avoids an extra
613 * redraw.
614 * This is detected when the cursor column is smaller after inserting
615 * something.
616 * Don't do this when the topline changed already, it has
617 * already been adjusted (by insertchar() calling open_line())).
618 */
619 if (curbuf->b_mod_set
620 && curwin->w_p_wrap
621 && !did_backspace
622 && curwin->w_topline == old_topline
623#ifdef FEAT_DIFF
624 && curwin->w_topfill == old_topfill
625#endif
626 )
627 {
628 mincol = curwin->w_wcol;
629 validate_cursor_col();
630
631 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
632 && curwin->w_wrow == W_WINROW(curwin)
633 + curwin->w_height - 1 - p_so
634 && (curwin->w_cursor.lnum != curwin->w_topline
635#ifdef FEAT_DIFF
636 || curwin->w_topfill > 0
637#endif
638 ))
639 {
640#ifdef FEAT_DIFF
641 if (curwin->w_topfill > 0)
642 --curwin->w_topfill;
643 else
644#endif
645#ifdef FEAT_FOLDING
646 if (hasFolding(curwin->w_topline, NULL, &old_topline))
647 set_topline(curwin, old_topline + 1);
648 else
649#endif
650 set_topline(curwin, curwin->w_topline + 1);
651 }
652 }
653
654 /* May need to adjust w_topline to show the cursor. */
655 update_topline();
656
657 did_backspace = FALSE;
658
659 validate_cursor(); /* may set must_redraw */
660
661 /*
662 * Redraw the display when no characters are waiting.
663 * Also shows mode, ruler and positions cursor.
664 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000665 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667#ifdef FEAT_SCROLLBIND
668 if (curwin->w_p_scb)
669 do_check_scrollbind(TRUE);
670#endif
671
672 update_curswant();
673 old_topline = curwin->w_topline;
674#ifdef FEAT_DIFF
675 old_topfill = curwin->w_topfill;
676#endif
677
678#ifdef USE_ON_FLY_SCROLL
679 dont_scroll = FALSE; /* allow scrolling here */
680#endif
681
682 /*
683 * Get a character for Insert mode.
684 */
685 lastc = c; /* remember previous char for CTRL-D */
686 c = safe_vgetc();
687
688#ifdef FEAT_RIGHTLEFT
689 if (p_hkmap && KeyTyped)
690 c = hkmap(c); /* Hebrew mode mapping */
691#endif
692#ifdef FEAT_FKMAP
693 if (p_fkmap && KeyTyped)
694 c = fkmap(c); /* Farsi mode mapping */
695#endif
696
697#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000698 /*
699 * Special handling of keys while the popup menu is visible or wanted
700 * and the cursor is still in the completed word.
701 */
702 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000703 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000704 /* BS: Delete one character from "compl_leader". */
705 if ((c == K_BS || c == Ctrl_H)
706 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000707 continue;
708
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000709 /* When no match was selected or it was edited. */
710 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000711 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000712 /* CTRL-L: Add one character from the current match to
713 * "compl_leader". */
714 if (c == Ctrl_L)
715 {
716 ins_compl_addfrommatch();
717 continue;
718 }
719
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000720 /* A printable, non-white character: Add to "compl_leader". */
721 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000722 {
723 ins_compl_addleader(c);
724 continue;
725 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000726
727 /* Pressing Enter selects the current match. */
728 if (c == CAR || c == K_KENTER || c == NL)
729 {
730 ins_compl_delete();
731 ins_compl_insert();
732 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000733 }
734 }
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
737 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000738 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000739 if (c != K_IGNORE && ins_compl_prep(c))
740 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741#endif
742
Bram Moolenaar488c6512005-08-11 20:09:58 +0000743 /* CTRL-\ CTRL-N goes to Normal mode,
744 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
745 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 if (c == Ctrl_BSL)
747 {
748 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000749 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 ++no_mapping;
751 ++allow_keys;
752 c = safe_vgetc();
753 --no_mapping;
754 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000755 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000757 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 vungetc(c);
759 c = Ctrl_BSL;
760 }
761 else if (c == Ctrl_G && p_im)
762 continue;
763 else
764 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000765 if (c == Ctrl_O)
766 {
767 ins_ctrl_o();
768 ins_at_eol = FALSE; /* cursor keeps its column */
769 nomove = TRUE;
770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 count = 0;
772 goto doESCkey;
773 }
774 }
775
776#ifdef FEAT_DIGRAPHS
777 c = do_digraph(c);
778#endif
779
780#ifdef FEAT_INS_EXPAND
781 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
782 goto docomplete;
783#endif
784 if (c == Ctrl_V || c == Ctrl_Q)
785 {
786 ins_ctrl_v();
787 c = Ctrl_V; /* pretend CTRL-V is last typed character */
788 continue;
789 }
790
791#ifdef FEAT_CINDENT
792 if (cindent_on()
793# ifdef FEAT_INS_EXPAND
794 && ctrl_x_mode == 0
795# endif
796 )
797 {
798 /* A key name preceded by a bang means this key is not to be
799 * inserted. Skip ahead to the re-indenting below.
800 * A key name preceded by a star means that indenting has to be
801 * done before inserting the key. */
802 line_is_white = inindent(0);
803 if (in_cinkeys(c, '!', line_is_white))
804 goto force_cindent;
805 if (can_cindent && in_cinkeys(c, '*', line_is_white)
806 && stop_arrow() == OK)
807 do_c_expr_indent();
808 }
809#endif
810
811#ifdef FEAT_RIGHTLEFT
812 if (curwin->w_p_rl)
813 switch (c)
814 {
815 case K_LEFT: c = K_RIGHT; break;
816 case K_S_LEFT: c = K_S_RIGHT; break;
817 case K_C_LEFT: c = K_C_RIGHT; break;
818 case K_RIGHT: c = K_LEFT; break;
819 case K_S_RIGHT: c = K_S_LEFT; break;
820 case K_C_RIGHT: c = K_C_LEFT; break;
821 }
822#endif
823
824#ifdef FEAT_VISUAL
825 /*
826 * If 'keymodel' contains "startsel", may start selection. If it
827 * does, a CTRL-O and c will be stuffed, we need to get these
828 * characters.
829 */
830 if (ins_start_select(c))
831 continue;
832#endif
833
834 /*
835 * The big switch to handle a character in insert mode.
836 */
837 switch (c)
838 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000839 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (echeck_abbr(ESC + ABBR_OFF))
841 break;
842 /*FALLTHROUGH*/
843
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#ifdef FEAT_CMDWIN
846 if (c == Ctrl_C && cmdwin_type != 0)
847 {
848 /* Close the cmdline window. */
849 cmdwin_result = K_IGNORE;
850 got_int = FALSE; /* don't stop executing autocommands et al. */
851 goto doESCkey;
852 }
853#endif
854
855#ifdef UNIX
856do_intr:
857#endif
858 /* when 'insertmode' set, and not halfway a mapping, don't leave
859 * Insert mode */
860 if (goto_im())
861 {
862 if (got_int)
863 {
864 (void)vgetc(); /* flush all buffers */
865 got_int = FALSE;
866 }
867 else
868 vim_beep();
869 break;
870 }
871doESCkey:
872 /*
873 * This is the ONLY return from edit()!
874 */
875 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
876 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000877 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 o_lnum = curwin->w_cursor.lnum;
879
Bram Moolenaar488c6512005-08-11 20:09:58 +0000880 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000881 {
882#ifdef FEAT_AUTOCMD
883 if (cmdchar != 'r' && cmdchar != 'v')
884 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
885 FALSE, curbuf);
886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 continue;
890
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000891 case Ctrl_Z: /* suspend when 'insertmode' set */
892 if (!p_im)
893 goto normalchar; /* insert CTRL-Z as normal char */
894 stuffReadbuff((char_u *)":st\r");
895 c = Ctrl_O;
896 /*FALLTHROUGH*/
897
898 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000899#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000900 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000901 goto docomplete;
902#endif
903 if (echeck_abbr(Ctrl_O + ABBR_OFF))
904 break;
905 ins_ctrl_o();
906 count = 0;
907 goto doESCkey;
908
Bram Moolenaar572cb562005-08-05 21:35:02 +0000909 case K_INS: /* toggle insert/replace mode */
910 case K_KINS:
911 ins_insert(replaceState);
912 break;
913
914 case K_SELECT: /* end of Select mode mapping - ignore */
915 break;
916
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000917#ifdef FEAT_SNIFF
918 case K_SNIFF: /* Sniff command received */
919 stuffcharReadbuff(K_SNIFF);
920 goto doESCkey;
921#endif
922
923 case K_HELP: /* Help key works like <ESC> <Help> */
924 case K_F1:
925 case K_XF1:
926 stuffcharReadbuff(K_HELP);
927 if (p_im)
928 need_start_insertmode = TRUE;
929 goto doESCkey;
930
931#ifdef FEAT_NETBEANS_INTG
932 case K_F21: /* NetBeans command */
933 ++no_mapping; /* don't map the next key hits */
934 i = safe_vgetc();
935 --no_mapping;
936 netbeans_keycommand(i);
937 break;
938#endif
939
940 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 case NUL:
942 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 /* For ^@ the trailing ESC will end the insert, unless there is an
944 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
946 && c != Ctrl_A && !p_im)
947 goto doESCkey; /* quit insert mode */
948 inserted_space = FALSE;
949 break;
950
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000951 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 ins_reg();
953 auto_format(FALSE, TRUE);
954 inserted_space = FALSE;
955 break;
956
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000957 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 ins_ctrl_g();
959 break;
960
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000961 case Ctrl_HAT: /* switch input mode and/or langmap */
962 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 break;
964
965#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 if (!p_ari)
968 goto normalchar;
969 ins_ctrl_();
970 break;
971#endif
972
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000973 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
975 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
976 goto docomplete;
977#endif
978 /* FALLTHROUGH */
979
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000980 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# ifdef FEAT_INS_EXPAND
982 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
983 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000984 if (has_compl_option(FALSE))
985 goto docomplete;
986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988# endif
989 ins_shift(c, lastc);
990 auto_format(FALSE, TRUE);
991 inserted_space = FALSE;
992 break;
993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 case K_KDEL:
996 ins_del();
997 auto_format(FALSE, TRUE);
998 break;
999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 case Ctrl_H:
1002 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1003 auto_format(FALSE, TRUE);
1004 break;
1005
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001006 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1008 auto_format(FALSE, TRUE);
1009 break;
1010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001012# ifdef FEAT_COMPL_FUNC
1013 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001015 goto docomplete;
1016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1018 auto_format(FALSE, TRUE);
1019 inserted_space = FALSE;
1020 break;
1021
1022#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 case K_LEFTMOUSE_NM:
1025 case K_LEFTDRAG:
1026 case K_LEFTRELEASE:
1027 case K_LEFTRELEASE_NM:
1028 case K_MIDDLEMOUSE:
1029 case K_MIDDLEDRAG:
1030 case K_MIDDLERELEASE:
1031 case K_RIGHTMOUSE:
1032 case K_RIGHTDRAG:
1033 case K_RIGHTRELEASE:
1034 case K_X1MOUSE:
1035 case K_X1DRAG:
1036 case K_X1RELEASE:
1037 case K_X2MOUSE:
1038 case K_X2DRAG:
1039 case K_X2RELEASE:
1040 ins_mouse(c);
1041 break;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 ins_mousescroll(FALSE);
1045 break;
1046
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 ins_mousescroll(TRUE);
1049 break;
1050#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001051#ifdef FEAT_GUI_TABLINE
1052 case K_TABLINE:
1053 case K_TABMENU:
1054 ins_tabline(c);
1055 break;
1056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 break;
1060
Bram Moolenaar754b5602006-02-09 23:53:20 +00001061#ifdef FEAT_AUTOCMD
1062 case K_CURSORHOLD: /* Didn't type something for a while. */
1063 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1064 did_cursorhold = TRUE;
1065 break;
1066#endif
1067
Bram Moolenaar4770d092006-01-12 23:22:24 +00001068#ifdef FEAT_GUI_W32
1069 /* On Win32 ignore <M-F4>, we get it when closing the window was
1070 * cancelled. */
1071 case K_F4:
1072 if (mod_mask != MOD_MASK_ALT)
1073 goto normalchar;
1074 break;
1075#endif
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#ifdef FEAT_GUI
1078 case K_VER_SCROLLBAR:
1079 ins_scroll();
1080 break;
1081
1082 case K_HOR_SCROLLBAR:
1083 ins_horscroll();
1084 break;
1085#endif
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 case K_S_HOME:
1090 case K_C_HOME:
1091 ins_home(c);
1092 break;
1093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 case K_S_END:
1097 case K_C_END:
1098 ins_end(c);
1099 break;
1100
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001102 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1103 ins_s_left();
1104 else
1105 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 case K_C_LEFT:
1110 ins_s_left();
1111 break;
1112
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001113 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001114 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1115 ins_s_right();
1116 else
1117 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 break;
1119
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 case K_C_RIGHT:
1122 ins_s_right();
1123 break;
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001126#ifdef FEAT_INS_EXPAND
1127 if (pum_visible())
1128 goto docomplete;
1129#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001130 if (mod_mask & MOD_MASK_SHIFT)
1131 ins_pageup();
1132 else
1133 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 case K_PAGEUP:
1138 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001139#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001140 if (pum_visible())
1141 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 ins_pageup();
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001147#ifdef FEAT_INS_EXPAND
1148 if (pum_visible())
1149 goto docomplete;
1150#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001151 if (mod_mask & MOD_MASK_SHIFT)
1152 ins_pagedown();
1153 else
1154 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 case K_PAGEDOWN:
1159 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001160#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001161 if (pum_visible())
1162 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 ins_pagedown();
1165 break;
1166
1167#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 ins_drop();
1170 break;
1171#endif
1172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 c = TAB;
1175 /* FALLTHROUGH */
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1179 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1180 goto docomplete;
1181#endif
1182 inserted_space = FALSE;
1183 if (ins_tab())
1184 goto normalchar; /* insert TAB as a normal char */
1185 auto_format(FALSE, TRUE);
1186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 c = CAR;
1190 /* FALLTHROUGH */
1191 case CAR:
1192 case NL:
1193#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1194 /* In a quickfix window a <CR> jumps to the error under the
1195 * cursor. */
1196 if (bt_quickfix(curbuf) && c == CAR)
1197 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001198 if (curwin->w_llist_ref == NULL) /* quickfix window */
1199 do_cmdline_cmd((char_u *)".cc");
1200 else /* location list window */
1201 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 break;
1203 }
1204#endif
1205#ifdef FEAT_CMDWIN
1206 if (cmdwin_type != 0)
1207 {
1208 /* Execute the command in the cmdline window. */
1209 cmdwin_result = CAR;
1210 goto doESCkey;
1211 }
1212#endif
1213 if (ins_eol(c) && !p_im)
1214 goto doESCkey; /* out of memory */
1215 auto_format(FALSE, FALSE);
1216 inserted_space = FALSE;
1217 break;
1218
1219#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221# ifdef FEAT_INS_EXPAND
1222 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1223 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 if (has_compl_option(TRUE))
1225 goto docomplete;
1226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 }
1228# endif
1229# ifdef FEAT_DIGRAPHS
1230 c = ins_digraph();
1231 if (c == NUL)
1232 break;
1233# endif
1234 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
1237#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001238 case Ctrl_X: /* Enter CTRL-X mode */
1239 ins_ctrl_x();
1240 break;
1241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001242 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (ctrl_x_mode != CTRL_X_TAGS)
1244 goto normalchar;
1245 goto docomplete;
1246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (ctrl_x_mode != CTRL_X_FILES)
1249 goto normalchar;
1250 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001251
1252 case 's': /* Spelling completion after ^X */
1253 case Ctrl_S:
1254 if (ctrl_x_mode != CTRL_X_SPELL)
1255 goto normalchar;
1256 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257#endif
1258
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001259 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#ifdef FEAT_INS_EXPAND
1261 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1262#endif
1263 {
1264 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1265 if (p_im)
1266 {
1267 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1268 break;
1269 goto doESCkey;
1270 }
1271 goto normalchar;
1272 }
1273#ifdef FEAT_INS_EXPAND
1274 /* FALLTHROUGH */
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 case Ctrl_N:
1278 /* if 'complete' is empty then plain ^P is no longer special,
1279 * but it is under other ^X modes */
1280 if (*curbuf->b_p_cpt == NUL
1281 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001282 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 goto normalchar;
1284
1285docomplete:
1286 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 break;
1289#endif /* FEAT_INS_EXPAND */
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case Ctrl_Y: /* copy from previous line or scroll down */
1292 case Ctrl_E: /* copy from next line or scroll up */
1293 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 break;
1295
1296 default:
1297#ifdef UNIX
1298 if (c == intr_char) /* special interrupt char */
1299 goto do_intr;
1300#endif
1301
1302 /*
1303 * Insert a nomal character.
1304 */
1305normalchar:
1306#ifdef FEAT_SMARTINDENT
1307 /* Try to perform smart-indenting. */
1308 ins_try_si(c);
1309#endif
1310
1311 if (c == ' ')
1312 {
1313 inserted_space = TRUE;
1314#ifdef FEAT_CINDENT
1315 if (inindent(0))
1316 can_cindent = FALSE;
1317#endif
1318 if (Insstart_blank_vcol == MAXCOL
1319 && curwin->w_cursor.lnum == Insstart.lnum)
1320 Insstart_blank_vcol = get_nolist_virtcol();
1321 }
1322
1323 if (vim_iswordc(c) || !echeck_abbr(
1324#ifdef FEAT_MBYTE
1325 /* Add ABBR_OFF for characters above 0x100, this is
1326 * what check_abbr() expects. */
1327 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1328#endif
1329 c))
1330 {
1331 insert_special(c, FALSE, FALSE);
1332#ifdef FEAT_RIGHTLEFT
1333 revins_legal++;
1334 revins_chars++;
1335#endif
1336 }
1337
1338 auto_format(FALSE, TRUE);
1339
1340#ifdef FEAT_FOLDING
1341 /* When inserting a character the cursor line must never be in a
1342 * closed fold. */
1343 foldOpenCursor();
1344#endif
1345 break;
1346 } /* end of switch (c) */
1347
1348 /* If the cursor was moved we didn't just insert a space */
1349 if (arrow_used)
1350 inserted_space = FALSE;
1351
1352#ifdef FEAT_CINDENT
1353 if (can_cindent && cindent_on()
1354# ifdef FEAT_INS_EXPAND
1355 && ctrl_x_mode == 0
1356# endif
1357 )
1358 {
1359force_cindent:
1360 /*
1361 * Indent now if a key was typed that is in 'cinkeys'.
1362 */
1363 if (in_cinkeys(c, ' ', line_is_white))
1364 {
1365 if (stop_arrow() == OK)
1366 /* re-indent the current line */
1367 do_c_expr_indent();
1368 }
1369 }
1370#endif /* FEAT_CINDENT */
1371
1372 } /* for (;;) */
1373 /* NOTREACHED */
1374}
1375
1376/*
1377 * Redraw for Insert mode.
1378 * This is postponed until getting the next character to make '$' in the 'cpo'
1379 * option work correctly.
1380 * Only redraw when there are no characters available. This speeds up
1381 * inserting sequences of characters (e.g., for CTRL-R).
1382 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001383/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001385ins_redraw(ready)
1386 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 if (!char_avail())
1389 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001390#ifdef FEAT_AUTOCMD
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001391 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001392 if (ready && has_cursormovedI()
1393 && !equalpos(last_cursormoved, curwin->w_cursor))
1394 {
1395 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1396 last_cursormoved = curwin->w_cursor;
1397 }
1398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (must_redraw)
1400 update_screen(0);
1401 else if (clear_cmdline || redraw_cmdline)
1402 showmode(); /* clear cmdline and show mode */
1403 showruler(FALSE);
1404 setcursor();
1405 emsg_on_display = FALSE; /* may remove error message now */
1406 }
1407}
1408
1409/*
1410 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1411 */
1412 static void
1413ins_ctrl_v()
1414{
1415 int c;
1416
1417 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001418 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 if (redrawing() && !char_avail())
1421 edit_putchar('^', TRUE);
1422 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1423
1424#ifdef FEAT_CMDL_INFO
1425 add_to_showcmd_c(Ctrl_V);
1426#endif
1427
1428 c = get_literal();
1429#ifdef FEAT_CMDL_INFO
1430 clear_showcmd();
1431#endif
1432 insert_special(c, FALSE, TRUE);
1433#ifdef FEAT_RIGHTLEFT
1434 revins_chars++;
1435 revins_legal++;
1436#endif
1437}
1438
1439/*
1440 * Put a character directly onto the screen. It's not stored in a buffer.
1441 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1442 */
1443static int pc_status;
1444#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1445#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1446#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1447#define PC_STATUS_SET 3 /* pc_bytes was filled */
1448#ifdef FEAT_MBYTE
1449static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1450#else
1451static char_u pc_bytes[2]; /* saved bytes */
1452#endif
1453static int pc_attr;
1454static int pc_row;
1455static int pc_col;
1456
1457 void
1458edit_putchar(c, highlight)
1459 int c;
1460 int highlight;
1461{
1462 int attr;
1463
1464 if (ScreenLines != NULL)
1465 {
1466 update_topline(); /* just in case w_topline isn't valid */
1467 validate_cursor();
1468 if (highlight)
1469 attr = hl_attr(HLF_8);
1470 else
1471 attr = 0;
1472 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1473 pc_col = W_WINCOL(curwin);
1474#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1475 pc_status = PC_STATUS_UNSET;
1476#endif
1477#ifdef FEAT_RIGHTLEFT
1478 if (curwin->w_p_rl)
1479 {
1480 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1481# ifdef FEAT_MBYTE
1482 if (has_mbyte)
1483 {
1484 int fix_col = mb_fix_col(pc_col, pc_row);
1485
1486 if (fix_col != pc_col)
1487 {
1488 screen_putchar(' ', pc_row, fix_col, attr);
1489 --curwin->w_wcol;
1490 pc_status = PC_STATUS_RIGHT;
1491 }
1492 }
1493# endif
1494 }
1495 else
1496#endif
1497 {
1498 pc_col += curwin->w_wcol;
1499#ifdef FEAT_MBYTE
1500 if (mb_lefthalve(pc_row, pc_col))
1501 pc_status = PC_STATUS_LEFT;
1502#endif
1503 }
1504
1505 /* save the character to be able to put it back */
1506#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1507 if (pc_status == PC_STATUS_UNSET)
1508#endif
1509 {
1510 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1511 pc_status = PC_STATUS_SET;
1512 }
1513 screen_putchar(c, pc_row, pc_col, attr);
1514 }
1515}
1516
1517/*
1518 * Undo the previous edit_putchar().
1519 */
1520 void
1521edit_unputchar()
1522{
1523 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1524 {
1525#if defined(FEAT_MBYTE)
1526 if (pc_status == PC_STATUS_RIGHT)
1527 ++curwin->w_wcol;
1528 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1529 redrawWinline(curwin->w_cursor.lnum, FALSE);
1530 else
1531#endif
1532 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1533 }
1534}
1535
1536/*
1537 * Called when p_dollar is set: display a '$' at the end of the changed text
1538 * Only works when cursor is in the line that changes.
1539 */
1540 void
1541display_dollar(col)
1542 colnr_T col;
1543{
1544 colnr_T save_col;
1545
1546 if (!redrawing())
1547 return;
1548
1549 cursor_off();
1550 save_col = curwin->w_cursor.col;
1551 curwin->w_cursor.col = col;
1552#ifdef FEAT_MBYTE
1553 if (has_mbyte)
1554 {
1555 char_u *p;
1556
1557 /* If on the last byte of a multi-byte move to the first byte. */
1558 p = ml_get_curline();
1559 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1560 }
1561#endif
1562 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1563 if (curwin->w_wcol < W_WIDTH(curwin))
1564 {
1565 edit_putchar('$', FALSE);
1566 dollar_vcol = curwin->w_virtcol;
1567 }
1568 curwin->w_cursor.col = save_col;
1569}
1570
1571/*
1572 * Call this function before moving the cursor from the normal insert position
1573 * in insert mode.
1574 */
1575 static void
1576undisplay_dollar()
1577{
1578 if (dollar_vcol)
1579 {
1580 dollar_vcol = 0;
1581 redrawWinline(curwin->w_cursor.lnum, FALSE);
1582 }
1583}
1584
1585/*
1586 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1587 * Keep the cursor on the same character.
1588 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1589 * type == INDENT_DEC decrease indent (for CTRL-D)
1590 * type == INDENT_SET set indent to "amount"
1591 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1592 */
1593 void
1594change_indent(type, amount, round, replaced)
1595 int type;
1596 int amount;
1597 int round;
1598 int replaced; /* replaced character, put on replace stack */
1599{
1600 int vcol;
1601 int last_vcol;
1602 int insstart_less; /* reduction for Insstart.col */
1603 int new_cursor_col;
1604 int i;
1605 char_u *ptr;
1606 int save_p_list;
1607 int start_col;
1608 colnr_T vc;
1609#ifdef FEAT_VREPLACE
1610 colnr_T orig_col = 0; /* init for GCC */
1611 char_u *new_line, *orig_line = NULL; /* init for GCC */
1612
1613 /* VREPLACE mode needs to know what the line was like before changing */
1614 if (State & VREPLACE_FLAG)
1615 {
1616 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1617 orig_col = curwin->w_cursor.col;
1618 }
1619#endif
1620
1621 /* for the following tricks we don't want list mode */
1622 save_p_list = curwin->w_p_list;
1623 curwin->w_p_list = FALSE;
1624 vc = getvcol_nolist(&curwin->w_cursor);
1625 vcol = vc;
1626
1627 /*
1628 * For Replace mode we need to fix the replace stack later, which is only
1629 * possible when the cursor is in the indent. Remember the number of
1630 * characters before the cursor if it's possible.
1631 */
1632 start_col = curwin->w_cursor.col;
1633
1634 /* determine offset from first non-blank */
1635 new_cursor_col = curwin->w_cursor.col;
1636 beginline(BL_WHITE);
1637 new_cursor_col -= curwin->w_cursor.col;
1638
1639 insstart_less = curwin->w_cursor.col;
1640
1641 /*
1642 * If the cursor is in the indent, compute how many screen columns the
1643 * cursor is to the left of the first non-blank.
1644 */
1645 if (new_cursor_col < 0)
1646 vcol = get_indent() - vcol;
1647
1648 if (new_cursor_col > 0) /* can't fix replace stack */
1649 start_col = -1;
1650
1651 /*
1652 * Set the new indent. The cursor will be put on the first non-blank.
1653 */
1654 if (type == INDENT_SET)
1655 (void)set_indent(amount, SIN_CHANGED);
1656 else
1657 {
1658#ifdef FEAT_VREPLACE
1659 int save_State = State;
1660
1661 /* Avoid being called recursively. */
1662 if (State & VREPLACE_FLAG)
1663 State = INSERT;
1664#endif
1665 shift_line(type == INDENT_DEC, round, 1);
1666#ifdef FEAT_VREPLACE
1667 State = save_State;
1668#endif
1669 }
1670 insstart_less -= curwin->w_cursor.col;
1671
1672 /*
1673 * Try to put cursor on same character.
1674 * If the cursor is at or after the first non-blank in the line,
1675 * compute the cursor column relative to the column of the first
1676 * non-blank character.
1677 * If we are not in insert mode, leave the cursor on the first non-blank.
1678 * If the cursor is before the first non-blank, position it relative
1679 * to the first non-blank, counted in screen columns.
1680 */
1681 if (new_cursor_col >= 0)
1682 {
1683 /*
1684 * When changing the indent while the cursor is touching it, reset
1685 * Insstart_col to 0.
1686 */
1687 if (new_cursor_col == 0)
1688 insstart_less = MAXCOL;
1689 new_cursor_col += curwin->w_cursor.col;
1690 }
1691 else if (!(State & INSERT))
1692 new_cursor_col = curwin->w_cursor.col;
1693 else
1694 {
1695 /*
1696 * Compute the screen column where the cursor should be.
1697 */
1698 vcol = get_indent() - vcol;
1699 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1700
1701 /*
1702 * Advance the cursor until we reach the right screen column.
1703 */
1704 vcol = last_vcol = 0;
1705 new_cursor_col = -1;
1706 ptr = ml_get_curline();
1707 while (vcol <= (int)curwin->w_virtcol)
1708 {
1709 last_vcol = vcol;
1710#ifdef FEAT_MBYTE
1711 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001712 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 else
1714#endif
1715 ++new_cursor_col;
1716 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1717 }
1718 vcol = last_vcol;
1719
1720 /*
1721 * May need to insert spaces to be able to position the cursor on
1722 * the right screen column.
1723 */
1724 if (vcol != (int)curwin->w_virtcol)
1725 {
1726 curwin->w_cursor.col = new_cursor_col;
1727 i = (int)curwin->w_virtcol - vcol;
1728 ptr = alloc(i + 1);
1729 if (ptr != NULL)
1730 {
1731 new_cursor_col += i;
1732 ptr[i] = NUL;
1733 while (--i >= 0)
1734 ptr[i] = ' ';
1735 ins_str(ptr);
1736 vim_free(ptr);
1737 }
1738 }
1739
1740 /*
1741 * When changing the indent while the cursor is in it, reset
1742 * Insstart_col to 0.
1743 */
1744 insstart_less = MAXCOL;
1745 }
1746
1747 curwin->w_p_list = save_p_list;
1748
1749 if (new_cursor_col <= 0)
1750 curwin->w_cursor.col = 0;
1751 else
1752 curwin->w_cursor.col = new_cursor_col;
1753 curwin->w_set_curswant = TRUE;
1754 changed_cline_bef_curs();
1755
1756 /*
1757 * May have to adjust the start of the insert.
1758 */
1759 if (State & INSERT)
1760 {
1761 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1762 {
1763 if ((int)Insstart.col <= insstart_less)
1764 Insstart.col = 0;
1765 else
1766 Insstart.col -= insstart_less;
1767 }
1768 if ((int)ai_col <= insstart_less)
1769 ai_col = 0;
1770 else
1771 ai_col -= insstart_less;
1772 }
1773
1774 /*
1775 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1776 * If the number of characters before the cursor decreased, need to pop a
1777 * few characters from the replace stack.
1778 * If the number of characters before the cursor increased, need to push a
1779 * few NULs onto the replace stack.
1780 */
1781 if (REPLACE_NORMAL(State) && start_col >= 0)
1782 {
1783 while (start_col > (int)curwin->w_cursor.col)
1784 {
1785 replace_join(0); /* remove a NUL from the replace stack */
1786 --start_col;
1787 }
1788 while (start_col < (int)curwin->w_cursor.col || replaced)
1789 {
1790 replace_push(NUL);
1791 if (replaced)
1792 {
1793 replace_push(replaced);
1794 replaced = NUL;
1795 }
1796 ++start_col;
1797 }
1798 }
1799
1800#ifdef FEAT_VREPLACE
1801 /*
1802 * For VREPLACE mode, we also have to fix the replace stack. In this case
1803 * it is always possible because we backspace over the whole line and then
1804 * put it back again the way we wanted it.
1805 */
1806 if (State & VREPLACE_FLAG)
1807 {
1808 /* If orig_line didn't allocate, just return. At least we did the job,
1809 * even if you can't backspace. */
1810 if (orig_line == NULL)
1811 return;
1812
1813 /* Save new line */
1814 new_line = vim_strsave(ml_get_curline());
1815 if (new_line == NULL)
1816 return;
1817
1818 /* We only put back the new line up to the cursor */
1819 new_line[curwin->w_cursor.col] = NUL;
1820
1821 /* Put back original line */
1822 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1823 curwin->w_cursor.col = orig_col;
1824
1825 /* Backspace from cursor to start of line */
1826 backspace_until_column(0);
1827
1828 /* Insert new stuff into line again */
1829 ins_bytes(new_line);
1830
1831 vim_free(new_line);
1832 }
1833#endif
1834}
1835
1836/*
1837 * Truncate the space at the end of a line. This is to be used only in an
1838 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1839 * modes.
1840 */
1841 void
1842truncate_spaces(line)
1843 char_u *line;
1844{
1845 int i;
1846
1847 /* find start of trailing white space */
1848 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1849 {
1850 if (State & REPLACE_FLAG)
1851 replace_join(0); /* remove a NUL from the replace stack */
1852 }
1853 line[i + 1] = NUL;
1854}
1855
1856#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1857 || defined(FEAT_COMMENTS) || defined(PROTO)
1858/*
1859 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1860 * modes correctly. May also be used when not in insert mode at all.
1861 */
1862 void
1863backspace_until_column(col)
1864 int col;
1865{
1866 while ((int)curwin->w_cursor.col > col)
1867 {
1868 curwin->w_cursor.col--;
1869 if (State & REPLACE_FLAG)
1870 replace_do_bs();
1871 else
1872 (void)del_char(FALSE);
1873 }
1874}
1875#endif
1876
1877#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1878/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001879 * CTRL-X pressed in Insert mode.
1880 */
1881 static void
1882ins_ctrl_x()
1883{
1884 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1885 * CTRL-V works like CTRL-N */
1886 if (ctrl_x_mode != CTRL_X_CMDLINE)
1887 {
1888 /* if the next ^X<> won't ADD nothing, then reset
1889 * compl_cont_status */
1890 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001891 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001892 else
1893 compl_cont_status = 0;
1894 /* We're not sure which CTRL-X mode it will be yet */
1895 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1896 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1897 edit_submode_pre = NULL;
1898 showmode();
1899 }
1900}
1901
1902/*
1903 * Return TRUE if the 'dict' or 'tsr' option can be used.
1904 */
1905 static int
1906has_compl_option(dict_opt)
1907 int dict_opt;
1908{
1909 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1910 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1911 {
1912 ctrl_x_mode = 0;
1913 edit_submode = NULL;
1914 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1915 : (char_u *)_("'thesaurus' option is empty"),
1916 hl_attr(HLF_E));
1917 if (emsg_silent == 0)
1918 {
1919 vim_beep();
1920 setcursor();
1921 out_flush();
1922 ui_delay(2000L, FALSE);
1923 }
1924 return FALSE;
1925 }
1926 return TRUE;
1927}
1928
1929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1931 * This depends on the current mode.
1932 */
1933 int
1934vim_is_ctrl_x_key(c)
1935 int c;
1936{
1937 /* Always allow ^R - let it's results then be checked */
1938 if (c == Ctrl_R)
1939 return TRUE;
1940
Bram Moolenaare3226be2005-12-18 22:10:00 +00001941 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001943 return TRUE;
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 switch (ctrl_x_mode)
1946 {
1947 case 0: /* Not in any CTRL-X mode */
1948 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1949 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001950 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1952 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1953 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001954 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1955 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 case CTRL_X_SCROLL:
1957 return (c == Ctrl_Y || c == Ctrl_E);
1958 case CTRL_X_WHOLE_LINE:
1959 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1960 case CTRL_X_FILES:
1961 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1962 case CTRL_X_DICTIONARY:
1963 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1964 case CTRL_X_THESAURUS:
1965 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1966 case CTRL_X_TAGS:
1967 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1968#ifdef FEAT_FIND_ID
1969 case CTRL_X_PATH_PATTERNS:
1970 return (c == Ctrl_P || c == Ctrl_N);
1971 case CTRL_X_PATH_DEFINES:
1972 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1973#endif
1974 case CTRL_X_CMDLINE:
1975 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1976 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001977#ifdef FEAT_COMPL_FUNC
1978 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001979 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001980 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001981 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001982#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001983 case CTRL_X_SPELL:
1984 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 EMSG(_(e_internal));
1987 return FALSE;
1988}
1989
1990/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001991 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 * case of the originally typed text is used, and the case of the completed
1993 * text is infered, ie this tries to work out what case you probably wanted
1994 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001995 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 */
1997 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001998ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 char_u *str;
2000 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002001 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 char_u *fname;
2003 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002004 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 int has_lower = FALSE;
2007 int was_letter = FALSE;
2008 int idx;
2009
2010 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2011 {
2012 /* Infer case of completed part -- webb */
2013 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002014 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002017 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002019 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
2021 has_lower = TRUE;
2022 if (isupper(IObuff[idx]))
2023 {
2024 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002025 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2027 break;
2028 }
2029 }
2030 }
2031
2032 /*
2033 * Rule 2: No lower case, 2nd consecutive letter converted to
2034 * upper case.
2035 */
2036 if (!has_lower)
2037 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002038 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002040 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 && islower(IObuff[idx]))
2042 {
2043 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002044 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2046 break;
2047 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002048 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050 }
2051
2052 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002053 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002055 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002057 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058}
2059
2060/*
2061 * Add a match to the list of matches.
2062 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002063 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002064 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002066 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002067ins_compl_add(str, len, icase, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 char_u *str;
2069 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002070 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002072 char_u *extra; /* extra text for popup menu or NULL */
2073 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002074 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002076 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002077 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
2079 ui_breakcheck();
2080 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002081 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (len < 0)
2083 len = (int)STRLEN(str);
2084
2085 /*
2086 * If the same match is already present, don't add it.
2087 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002088 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002090 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 do
2092 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002093 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002094 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002095 && match->cp_str[len] == NUL)
2096 return NOTDONE;
2097 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002098 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 }
2100
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002101 /* Remove any popup menu before changing the list of matches. */
2102 ins_compl_del_pum();
2103
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 /*
2105 * Allocate a new match structure.
2106 * Copy the values to the new match structure.
2107 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002108 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002110 return FAIL;
2111 match->cp_number = -1;
2112 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002113 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002114 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
2116 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002117 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002119 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002122 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2124 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002125 if (fname != NULL
2126 && compl_curr_match
2127 && compl_curr_match->cp_fname != NULL
2128 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002129 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002130 else if (fname != NULL)
2131 {
2132 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002133 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002136 match->cp_fname = NULL;
2137 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002138 if (extra != NULL)
2139 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 /*
2142 * Link the new match structure in the list of matches.
2143 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002144 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002145 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 else if (dir == FORWARD)
2147 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002148 match->cp_next = compl_curr_match->cp_next;
2149 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 else /* BACKWARD */
2152 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002153 match->cp_next = compl_curr_match;
2154 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002156 if (match->cp_next)
2157 match->cp_next->cp_prev = match;
2158 if (match->cp_prev)
2159 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002161 compl_first_match = match;
2162 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002164 /*
2165 * Find the longest common string if still doing that.
2166 */
2167 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2168 ins_compl_longest_match(match);
2169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 return OK;
2171}
2172
2173/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002174 * Return TRUE if "str[len]" matches with match->cp_str, considering
2175 * match->cp_icase.
2176 */
2177 static int
2178ins_compl_equal(match, str, len)
2179 compl_T *match;
2180 char_u *str;
2181 int len;
2182{
2183 if (match->cp_icase)
2184 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2185 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2186}
2187
2188/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002189 * Reduce the longest common string for match "match".
2190 */
2191 static void
2192ins_compl_longest_match(match)
2193 compl_T *match;
2194{
2195 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002196 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002197 int had_match;
2198
2199 if (compl_leader == NULL)
2200 /* First match, use it as a whole. */
2201 compl_leader = vim_strsave(match->cp_str);
2202 else
2203 {
2204 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002205 p = compl_leader;
2206 s = match->cp_str;
2207 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002208 {
2209#ifdef FEAT_MBYTE
2210 if (has_mbyte)
2211 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002212 c1 = mb_ptr2char(p);
2213 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002214 }
2215 else
2216#endif
2217 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002218 c1 = *p;
2219 c2 = *s;
2220 }
2221 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2222 : (c1 != c2))
2223 break;
2224#ifdef FEAT_MBYTE
2225 if (has_mbyte)
2226 {
2227 mb_ptr_adv(p);
2228 mb_ptr_adv(s);
2229 }
2230 else
2231#endif
2232 {
2233 ++p;
2234 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002235 }
2236 }
2237
2238 if (*p != NUL)
2239 {
2240 /* Leader was shortened, need to change the inserted text. */
2241 *p = NUL;
2242 had_match = (curwin->w_cursor.col > compl_col);
2243 ins_compl_delete();
2244 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2245 ins_redraw(FALSE);
2246
2247 /* When the match isn't there (to avoid matching itself) remove it
2248 * again after redrawing. */
2249 if (!had_match)
2250 ins_compl_delete();
2251 }
2252
2253 compl_used_match = FALSE;
2254 }
2255}
2256
2257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * Add an array of matches to the list of matches.
2259 * Frees matches[].
2260 */
2261 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002262ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 int num_matches;
2264 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002265 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 int i;
2268 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002269 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
Bram Moolenaar572cb562005-08-05 21:35:02 +00002271 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002272 if ((add_r = ins_compl_add(matches[i], -1, icase,
2273 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002275 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 FreeWild(num_matches, matches);
2277}
2278
2279/* Make the completion list cyclic.
2280 * Return the number of matches (excluding the original).
2281 */
2282 static int
2283ins_compl_make_cyclic()
2284{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002285 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int count = 0;
2287
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 /*
2291 * Find the end of the list.
2292 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002293 match = compl_first_match;
2294 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002295 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002297 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 ++count;
2299 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002300 match->cp_next = compl_first_match;
2301 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 return count;
2304}
2305
Bram Moolenaar9372a112005-12-06 19:59:18 +00002306/* "compl_match_array" points the currently displayed list of entries in the
2307 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002308static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002309static int compl_match_arraysize;
2310
2311/*
2312 * Update the screen and when there is any scrolling remove the popup menu.
2313 */
2314 static void
2315ins_compl_upd_pum()
2316{
2317 int h;
2318
2319 if (compl_match_array != NULL)
2320 {
2321 h = curwin->w_cline_height;
2322 update_screen(0);
2323 if (h != curwin->w_cline_height)
2324 ins_compl_del_pum();
2325 }
2326}
2327
2328/*
2329 * Remove any popup menu.
2330 */
2331 static void
2332ins_compl_del_pum()
2333{
2334 if (compl_match_array != NULL)
2335 {
2336 pum_undisplay();
2337 vim_free(compl_match_array);
2338 compl_match_array = NULL;
2339 }
2340}
2341
2342/*
2343 * Return TRUE if the popup menu should be displayed.
2344 */
2345 static int
2346pum_wanted()
2347{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002348 /* 'completeopt' must contain "menu" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002349 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002350 return FALSE;
2351
2352 /* The display looks bad on a B&W display. */
2353 if (t_colors < 8
2354#ifdef FEAT_GUI
2355 && !gui.in_use
2356#endif
2357 )
2358 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002359 return TRUE;
2360}
2361
2362/*
2363 * Return TRUE if there are two or more matches to be shown in the popup menu.
2364 */
2365 static int
2366pum_two_or_more()
2367{
2368 compl_T *compl;
2369 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002370
2371 /* Don't display the popup menu if there are no matches or there is only
2372 * one (ignoring the original text). */
2373 compl = compl_first_match;
2374 i = 0;
2375 do
2376 {
2377 if (compl == NULL
2378 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2379 break;
2380 compl = compl->cp_next;
2381 } while (compl != compl_first_match);
2382
2383 return (i >= 2);
2384}
2385
2386/*
2387 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002388 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002389 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002390 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002391ins_compl_show_pum()
2392{
2393 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002394 compl_T *shown_compl = NULL;
2395 int did_find_shown_match = FALSE;
2396 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002397 int i;
2398 int cur = -1;
2399 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002400 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002401
Bram Moolenaara6557602006-02-04 22:43:20 +00002402 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002403 return;
2404
2405 /* Update the screen before drawing the popup menu over it. */
2406 update_screen(0);
2407
2408 if (compl_match_array == NULL)
2409 {
2410 /* Need to build the popup menu list. */
2411 compl_match_arraysize = 0;
2412 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002413 if (compl_leader != NULL)
2414 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002415 do
2416 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002417 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2418 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002419 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002420 ++compl_match_arraysize;
2421 compl = compl->cp_next;
2422 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002423 if (compl_match_arraysize == 0)
2424 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002425 compl_match_array = (pumitem_T *)alloc_clear(
2426 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002427 * compl_match_arraysize));
2428 if (compl_match_array != NULL)
2429 {
2430 i = 0;
2431 compl = compl_first_match;
2432 do
2433 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002434 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2435 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002436 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002437 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002438 if (!shown_match_ok)
2439 {
2440 if (compl == compl_shown_match || did_find_shown_match)
2441 {
2442 /* This item is the shown match or this is the
2443 * first displayed item after the shown match. */
2444 compl_shown_match = compl;
2445 did_find_shown_match = TRUE;
2446 shown_match_ok = TRUE;
2447 }
2448 else
2449 /* Remember this displayed match for when the
2450 * shown match is just below it. */
2451 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002452 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002453 }
2454 compl_match_array[i].pum_text = compl->cp_str;
2455 if (compl->cp_extra != NULL)
2456 compl_match_array[i++].pum_extra = compl->cp_extra;
2457 else
2458 compl_match_array[i++].pum_extra = compl->cp_fname;
2459 }
2460
2461 if (compl == compl_shown_match)
2462 {
2463 did_find_shown_match = TRUE;
2464 if (!shown_match_ok && shown_compl != NULL)
2465 {
2466 /* The shown match isn't displayed, set it to the
2467 * previously displayed match. */
2468 compl_shown_match = shown_compl;
2469 shown_match_ok = TRUE;
2470 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002471 }
2472 compl = compl->cp_next;
2473 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002474
2475 if (!shown_match_ok) /* no displayed match at all */
2476 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002477 }
2478 }
2479 else
2480 {
2481 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002482 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002483 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002484 break;
2485 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002486 }
2487
2488 if (compl_match_array != NULL)
2489 {
2490 /* Compute the screen column of the start of the completed text.
2491 * Use the cursor to get all wrapping and other settings right. */
2492 col = curwin->w_cursor.col;
2493 curwin->w_cursor.col = compl_col;
2494 validate_cursor_col();
2495 pum_display(compl_match_array, compl_match_arraysize, cur,
2496 curwin->w_cline_row + W_WINROW(curwin),
2497 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002498 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002499 curwin->w_cursor.col = col;
2500 }
2501}
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#define DICT_FIRST (1) /* use just first element in "dict" */
2504#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506/*
2507 * Add any identifiers that match the given pattern to the list of
2508 * completions.
2509 */
2510 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002511ins_compl_dictionaries(dict, pat, flags, thesaurus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 char_u *dict;
2513 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002514 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int thesaurus;
2516{
2517 char_u *ptr;
2518 char_u *buf;
2519 FILE *fp;
2520 regmatch_T regmatch;
2521 int add_r;
2522 char_u **files;
2523 int count;
2524 int i;
2525 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002526 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 buf = alloc(LSIZE);
2529 /* If 'infercase' is set, don't use 'smartcase' here */
2530 save_p_scs = p_scs;
2531 if (curbuf->b_p_inf)
2532 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002533
2534 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2535 * to only match at the start of a line. Otherwise just match the
2536 * pattern. */
2537 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2538 {
2539 i = STRLEN(pat) + 8;
2540 ptr = alloc(i);
2541 if (ptr == NULL)
2542 return;
2543 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2544 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2545 vim_free(ptr);
2546 }
2547 else
2548 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2551 regmatch.rm_ic = ignorecase(pat);
2552 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002553 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 /* copy one dictionary file name into buf */
2556 if (flags == DICT_EXACT)
2557 {
2558 count = 1;
2559 files = &dict;
2560 }
2561 else
2562 {
2563 /* Expand wildcards in the dictionary name, but do not allow
2564 * backticks (for security, the 'dict' option may have been set in
2565 * a modeline). */
2566 copy_option_part(&dict, buf, LSIZE, ",");
2567 if (vim_strchr(buf, '`') != NULL
2568 || expand_wildcards(1, &buf, &count, &files,
2569 EW_FILE|EW_SILENT) != OK)
2570 count = 0;
2571 }
2572
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002573 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2576 if (flags != DICT_EXACT)
2577 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002578 vim_snprintf((char *)IObuff, IOSIZE,
2579 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2581 }
2582
2583 if (fp != NULL)
2584 {
2585 /*
2586 * Read dictionary file line by line.
2587 * Check each line for a match.
2588 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002589 while (!got_int && !compl_interrupted
2590 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 ptr = buf;
2593 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2594 {
2595 ptr = regmatch.startp[0];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002596 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2597 ptr = find_line_end(ptr);
2598 else
2599 ptr = find_word_end(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 add_r = ins_compl_add_infercase(regmatch.startp[0],
2601 (int)(ptr - regmatch.startp[0]),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002602 p_ic, files[i], dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 if (thesaurus)
2604 {
2605 char_u *wstart;
2606
2607 /*
2608 * Add the other matches on the line
2609 */
2610 while (!got_int)
2611 {
2612 /* Find start of the next word. Skip white
2613 * space and punctuation. */
2614 ptr = find_word_start(ptr);
2615 if (*ptr == NUL || *ptr == NL)
2616 break;
2617 wstart = ptr;
2618
2619 /* Find end of the word and add it. */
2620#ifdef FEAT_MBYTE
2621 if (has_mbyte)
2622 /* Japanese words may have characters in
2623 * different classes, only separate words
2624 * with single-byte non-word characters. */
2625 while (*ptr != NUL)
2626 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002627 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 if (l < 2 && !vim_iswordc(*ptr))
2630 break;
2631 ptr += l;
2632 }
2633 else
2634#endif
2635 ptr = find_word_end(ptr);
2636 add_r = ins_compl_add_infercase(wstart,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002637 (int)(ptr - wstart),
2638 p_ic, files[i], dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 }
2641 if (add_r == OK)
2642 /* if dir was BACKWARD then honor it just once */
2643 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002644 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 break;
2646 /* avoid expensive call to vim_regexec() when at end
2647 * of line */
2648 if (*ptr == '\n' || got_int)
2649 break;
2650 }
2651 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002652 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
2654 fclose(fp);
2655 }
2656 }
2657 if (flags != DICT_EXACT)
2658 FreeWild(count, files);
2659 if (flags)
2660 break;
2661 }
2662 p_scs = save_p_scs;
2663 vim_free(regmatch.regprog);
2664 vim_free(buf);
2665}
2666
2667/*
2668 * Find the start of the next word.
2669 * Returns a pointer to the first char of the word. Also stops at a NUL.
2670 */
2671 char_u *
2672find_word_start(ptr)
2673 char_u *ptr;
2674{
2675#ifdef FEAT_MBYTE
2676 if (has_mbyte)
2677 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002678 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else
2680#endif
2681 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2682 ++ptr;
2683 return ptr;
2684}
2685
2686/*
2687 * Find the end of the word. Assumes it starts inside a word.
2688 * Returns a pointer to just after the word.
2689 */
2690 char_u *
2691find_word_end(ptr)
2692 char_u *ptr;
2693{
2694#ifdef FEAT_MBYTE
2695 int start_class;
2696
2697 if (has_mbyte)
2698 {
2699 start_class = mb_get_class(ptr);
2700 if (start_class > 1)
2701 while (*ptr != NUL)
2702 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002703 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (mb_get_class(ptr) != start_class)
2705 break;
2706 }
2707 }
2708 else
2709#endif
2710 while (vim_iswordc(*ptr))
2711 ++ptr;
2712 return ptr;
2713}
2714
2715/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002716 * Find the end of the line, omitting CR and NL at the end.
2717 * Returns a pointer to just after the line.
2718 */
2719 static char_u *
2720find_line_end(ptr)
2721 char_u *ptr;
2722{
2723 char_u *s;
2724
2725 s = ptr + STRLEN(ptr);
2726 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2727 --s;
2728 return s;
2729}
2730
2731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 * Free the list of completions
2733 */
2734 static void
2735ins_compl_free()
2736{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002737 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002739 vim_free(compl_pattern);
2740 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002741 vim_free(compl_leader);
2742 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002744 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002746
2747 ins_compl_del_pum();
2748 pum_clear();
2749
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 do
2752 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002753 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002754 compl_curr_match = compl_curr_match->cp_next;
2755 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002757 if (match->cp_flags & FREE_FNAME)
2758 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002759 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002761 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2762 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763}
2764
2765 static void
2766ins_compl_clear()
2767{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002768 compl_cont_status = 0;
2769 compl_started = FALSE;
2770 compl_matches = 0;
2771 vim_free(compl_pattern);
2772 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002773 vim_free(compl_leader);
2774 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 edit_submode_extra = NULL;
2776}
2777
2778/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002779 * Return TRUE when Insert completion is active.
2780 */
2781 int
2782ins_compl_active()
2783{
2784 return compl_started;
2785}
2786
2787/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002788 * Delete one character before the cursor and show the subset of the matches
2789 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002790 * Returns TRUE if the work is done and another char to be got from the user.
2791 */
2792 static int
2793ins_compl_bs()
2794{
2795 char_u *line;
2796 char_u *p;
2797
2798 if (curwin->w_cursor.col <= compl_col + compl_length)
2799 {
2800 /* Deleted more than what was used to find matches, need to look for
2801 * matches all over again. */
2802 ins_compl_free();
2803 compl_started = FALSE;
2804 compl_matches = 0;
2805 }
2806
2807 line = ml_get_curline();
2808 p = line + curwin->w_cursor.col;
2809 mb_ptr_back(line, p);
2810
2811 vim_free(compl_leader);
2812 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2813 if (compl_leader != NULL)
2814 {
2815 ins_compl_del_pum();
2816 ins_compl_delete();
2817 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2818
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002819 if (compl_started)
2820 ins_compl_set_original_text(compl_leader);
2821 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002822 {
2823 /* Matches were cleared, need to search for them now. */
2824 if (ins_complete(Ctrl_N) == FAIL)
2825 compl_cont_status = 0;
2826 else
2827 {
2828 /* Remove the completed word again. */
2829 ins_compl_delete();
2830 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2831 }
2832 }
2833
2834 /* Show the popup menu with a different set of matches. */
2835 ins_compl_show_pum();
2836 compl_used_match = FALSE;
2837
2838 return TRUE;
2839 }
2840 return FALSE;
2841}
2842
2843/*
2844 * Append one character to the match leader. May reduce the number of
2845 * matches.
2846 */
2847 static void
2848ins_compl_addleader(c)
2849 int c;
2850{
2851#ifdef FEAT_MBYTE
2852 int cc;
2853
2854 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2855 {
2856 char_u buf[MB_MAXBYTES + 1];
2857
2858 (*mb_char2bytes)(c, buf);
2859 buf[cc] = NUL;
2860 ins_char_bytes(buf, cc);
2861 }
2862 else
2863#endif
2864 ins_char(c);
2865
2866 vim_free(compl_leader);
2867 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2868 curwin->w_cursor.col - compl_col);
2869 if (compl_leader != NULL)
2870 {
2871 /* Show the popup menu with a different set of matches. */
2872 ins_compl_del_pum();
2873 ins_compl_show_pum();
2874 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002875 ins_compl_set_original_text(compl_leader);
2876 }
2877}
2878
2879/*
2880 * Set the first match, the original text.
2881 */
2882 static void
2883ins_compl_set_original_text(str)
2884 char_u *str;
2885{
2886 char_u *p;
2887
2888 /* Replace the original text entry. */
2889 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
2890 {
2891 p = vim_strsave(str);
2892 if (p != NULL)
2893 {
2894 vim_free(compl_first_match->cp_str);
2895 compl_first_match->cp_str = p;
2896 }
Bram Moolenaara6557602006-02-04 22:43:20 +00002897 }
2898}
2899
2900/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002901 * Append one character to the match leader. May reduce the number of
2902 * matches.
2903 */
2904 static void
2905ins_compl_addfrommatch()
2906{
2907 char_u *p;
2908 int len = curwin->w_cursor.col - compl_col;
2909 int c;
2910
2911 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002912 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002913 return;
2914 p += len;
2915#ifdef FEAT_MBYTE
2916 c = mb_ptr2char(p);
2917#else
2918 c = *p;
2919#endif
2920 ins_compl_addleader(c);
2921}
2922
2923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002925 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002926 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929ins_compl_prep(c)
2930 int c;
2931{
2932 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 int temp;
2934 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002935 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
2937 /* Forget any previous 'special' messages if this is actually
2938 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2939 */
2940 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2941 edit_submode_extra = NULL;
2942
2943 /* Ignore end of Select mode mapping */
2944 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002947 /* Set "compl_get_longest" when finding the first matches. */
2948 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
2949 || (ctrl_x_mode == 0 && !compl_started))
2950 {
2951 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
2952 compl_used_match = TRUE;
2953 }
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2956 {
2957 /*
2958 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2959 * it will be yet. Now we decide.
2960 */
2961 switch (c)
2962 {
2963 case Ctrl_E:
2964 case Ctrl_Y:
2965 ctrl_x_mode = CTRL_X_SCROLL;
2966 if (!(State & REPLACE_FLAG))
2967 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2968 else
2969 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2970 edit_submode_pre = NULL;
2971 showmode();
2972 break;
2973 case Ctrl_L:
2974 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2975 break;
2976 case Ctrl_F:
2977 ctrl_x_mode = CTRL_X_FILES;
2978 break;
2979 case Ctrl_K:
2980 ctrl_x_mode = CTRL_X_DICTIONARY;
2981 break;
2982 case Ctrl_R:
2983 /* Simply allow ^R to happen without affecting ^X mode */
2984 break;
2985 case Ctrl_T:
2986 ctrl_x_mode = CTRL_X_THESAURUS;
2987 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002988#ifdef FEAT_COMPL_FUNC
2989 case Ctrl_U:
2990 ctrl_x_mode = CTRL_X_FUNCTION;
2991 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002992 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002993 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002994 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002995#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002996 case 's':
2997 case Ctrl_S:
2998 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002999#ifdef FEAT_SYN_HL
3000 spell_back_to_badword();
3001#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003002 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 case Ctrl_RSB:
3004 ctrl_x_mode = CTRL_X_TAGS;
3005 break;
3006#ifdef FEAT_FIND_ID
3007 case Ctrl_I:
3008 case K_S_TAB:
3009 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3010 break;
3011 case Ctrl_D:
3012 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3013 break;
3014#endif
3015 case Ctrl_V:
3016 case Ctrl_Q:
3017 ctrl_x_mode = CTRL_X_CMDLINE;
3018 break;
3019 case Ctrl_P:
3020 case Ctrl_N:
3021 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3022 * just started ^X mode, or there were enough ^X's to cancel
3023 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3024 * do normal expansion when interrupting a different mode (say
3025 * ^X^F^X^P or ^P^X^X^P, see below)
3026 * nothing changes if interrupting mode 0, (eg, the flag
3027 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003028 if (!(compl_cont_status & CONT_INTRPT))
3029 compl_cont_status |= CONT_LOCAL;
3030 else if (compl_cont_mode != 0)
3031 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 /* FALLTHROUGH */
3033 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003034 /* If we have typed at least 2 ^X's... for modes != 0, we set
3035 * compl_cont_status = 0 (eg, as if we had just started ^X
3036 * mode).
3037 * For mode 0, we set "compl_cont_mode" to an impossible
3038 * value, in both cases ^X^X can be used to restart the same
3039 * mode (avoiding ADDING mode).
3040 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3041 * 'complete' and local ^P expansions respectively.
3042 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3043 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (c == Ctrl_X)
3045 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003046 if (compl_cont_mode != 0)
3047 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003049 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 ctrl_x_mode = 0;
3052 edit_submode = NULL;
3053 showmode();
3054 break;
3055 }
3056 }
3057 else if (ctrl_x_mode != 0)
3058 {
3059 /* We're already in CTRL-X mode, do we stay in it? */
3060 if (!vim_is_ctrl_x_key(c))
3061 {
3062 if (ctrl_x_mode == CTRL_X_SCROLL)
3063 ctrl_x_mode = 0;
3064 else
3065 ctrl_x_mode = CTRL_X_FINISHED;
3066 edit_submode = NULL;
3067 }
3068 showmode();
3069 }
3070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003071 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
3073 /* Show error message from attempted keyword completion (probably
3074 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003075 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003077 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3078 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 || ctrl_x_mode == CTRL_X_FINISHED)
3080 {
3081 /* Get here when we have finished typing a sequence of ^N and
3082 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003083 * memory that was used, and make sure we can redo the insert. */
3084 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003086 char_u *p;
3087
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 /*
3089 * If any of the original typed text has been changed,
3090 * eg when ignorecase is set, we must add back-spaces to
3091 * the redo buffer. We add as few as necessary to delete
3092 * just the part of the original text that has changed.
3093 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003094 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003095 p = compl_orig_text;
3096 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003098 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 ++ptr;
3100 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003101 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003103 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105
3106#ifdef FEAT_CINDENT
3107 want_cindent = (can_cindent && cindent_on());
3108#endif
3109 /*
3110 * When completing whole lines: fix indent for 'cindent'.
3111 * Otherwise, break line if it's too long.
3112 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003113 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
3115#ifdef FEAT_CINDENT
3116 /* re-indent the current line */
3117 if (want_cindent)
3118 {
3119 do_c_expr_indent();
3120 want_cindent = FALSE; /* don't do it again */
3121 }
3122#endif
3123 }
3124 else
3125 {
3126 /* put the cursor on the last char, for 'tw' formatting */
3127 curwin->w_cursor.col--;
3128 if (stop_arrow() == OK)
3129 insertchar(NUL, 0, -1);
3130 curwin->w_cursor.col++;
3131 }
3132
3133 auto_format(FALSE, TRUE);
3134
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003135 /* if the popup menu is displayed hitting Enter means accepting
3136 * the selection without inserting anything. */
3137 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3138 retval = TRUE;
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003141 compl_started = FALSE;
3142 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 msg_clr_cmdline(); /* necessary for "noshowmode" */
3144 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (edit_submode != NULL)
3146 {
3147 edit_submode = NULL;
3148 showmode();
3149 }
3150
3151#ifdef FEAT_CINDENT
3152 /*
3153 * Indent now if a key was typed that is in 'cinkeys'.
3154 */
3155 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3156 do_c_expr_indent();
3157#endif
3158 }
3159 }
3160
3161 /* reset continue_* if we left expansion-mode, if we stay they'll be
3162 * (re)set properly in ins_complete() */
3163 if (!vim_is_ctrl_x_key(c))
3164 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003165 compl_cont_status = 0;
3166 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003168
3169 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170}
3171
3172/*
3173 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3174 * (depending on flag) starting from buf and looking for a non-scanned
3175 * buffer (other than curbuf). curbuf is special, if it is called with
3176 * buf=curbuf then it has to be the first call for a given flag/expansion.
3177 *
3178 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3179 */
3180 static buf_T *
3181ins_compl_next_buf(buf, flag)
3182 buf_T *buf;
3183 int flag;
3184{
3185#ifdef FEAT_WINDOWS
3186 static win_T *wp;
3187#endif
3188
3189 if (flag == 'w') /* just windows */
3190 {
3191#ifdef FEAT_WINDOWS
3192 if (buf == curbuf) /* first call for this flag/expansion */
3193 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003194 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 && wp->w_buffer->b_scanned)
3196 ;
3197 buf = wp->w_buffer;
3198#else
3199 buf = curbuf;
3200#endif
3201 }
3202 else
3203 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3204 * (unlisted buffers)
3205 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003206 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 && ((flag == 'U'
3208 ? buf->b_p_bl
3209 : (!buf->b_p_bl
3210 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003211 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 ;
3213 return buf;
3214}
3215
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003216#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003217static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003218
3219/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003220 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003221 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003222 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003223 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003224 static void
3225expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003226 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003227 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003228{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003229 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003230 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003231 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003232 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003233 char_u *funcname;
3234 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003235 int dir = compl_direction;
3236 char_u *x;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003237 int icase;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003238
Bram Moolenaare344bea2005-09-01 20:46:49 +00003239 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3240 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003241 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003242
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003243 /* Call 'completefunc' to obtain the list of matches. */
3244 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003245 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003246
Bram Moolenaare344bea2005-09-01 20:46:49 +00003247 pos = curwin->w_cursor;
3248 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3249 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003250 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003251 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003252
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003253 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003254 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003255 {
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003256 icase = p_ic;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003257 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3258 {
3259 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3260 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003261 if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase",
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003262 FALSE) != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003263 icase = get_dict_number(li->li_tv.vval.v_dict,
3264 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003265 }
3266 else
3267 {
3268 p = get_tv_string_chk(&li->li_tv);
3269 x = NULL;
3270 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003271 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003272 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003273 if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003274 /* if dir was BACKWARD then honor it just once */
3275 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003276 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003277 else if (did_emsg)
3278 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003279 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003280
3281 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003282}
3283#endif /* FEAT_COMPL_FUNC */
3284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003285/*
3286 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003287 * The search starts at position "ini" in curbuf and in the direction
3288 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 * When "compl_started" is FALSE start at that position, otherwise continue
3290 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003291 * This may return before finding all the matches.
3292 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 */
3294 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003295ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 static pos_T first_match_pos;
3299 static pos_T last_match_pos;
3300 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003301 static int found_all = FALSE; /* Found all matches of a
3302 certain type. */
3303 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaar572cb562005-08-05 21:35:02 +00003305 pos_T *pos;
3306 char_u **matches;
3307 int save_p_scs;
3308 int save_p_ws;
3309 int save_p_ic;
3310 int i;
3311 int num_matches;
3312 int len;
3313 int found_new_match;
3314 int type = ctrl_x_mode;
3315 char_u *ptr;
3316 char_u *dict = NULL;
3317 int dict_f = 0;
3318 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003320 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
3322 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3323 ins_buf->b_scanned = 0;
3324 found_all = FALSE;
3325 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003327 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 last_match_pos = first_match_pos = *ini;
3329 }
3330
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003331 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003332 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3334 for (;;)
3335 {
3336 found_new_match = FAIL;
3337
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003338 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 * or if found_all says this entry is done. For ^X^L only use the
3340 * entries from 'complete' that look in loaded buffers. */
3341 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003342 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344 found_all = FALSE;
3345 while (*e_cpt == ',' || *e_cpt == ' ')
3346 e_cpt++;
3347 if (*e_cpt == '.' && !curbuf->b_scanned)
3348 {
3349 ins_buf = curbuf;
3350 first_match_pos = *ini;
3351 /* So that ^N can match word immediately after cursor */
3352 if (ctrl_x_mode == 0)
3353 dec(&first_match_pos);
3354 last_match_pos = first_match_pos;
3355 type = 0;
3356 }
3357 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3358 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3359 {
3360 /* Scan a buffer, but not the current one. */
3361 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3362 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003363 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 first_match_pos.col = last_match_pos.col = 0;
3365 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3366 last_match_pos.lnum = 0;
3367 type = 0;
3368 }
3369 else /* unloaded buffer, scan like dictionary */
3370 {
3371 found_all = TRUE;
3372 if (ins_buf->b_fname == NULL)
3373 continue;
3374 type = CTRL_X_DICTIONARY;
3375 dict = ins_buf->b_fname;
3376 dict_f = DICT_EXACT;
3377 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003378 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 ins_buf->b_fname == NULL
3380 ? buf_spname(ins_buf)
3381 : ins_buf->b_sfname == NULL
3382 ? (char *)ins_buf->b_fname
3383 : (char *)ins_buf->b_sfname);
3384 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3385 }
3386 else if (*e_cpt == NUL)
3387 break;
3388 else
3389 {
3390 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3391 type = -1;
3392 else if (*e_cpt == 'k' || *e_cpt == 's')
3393 {
3394 if (*e_cpt == 'k')
3395 type = CTRL_X_DICTIONARY;
3396 else
3397 type = CTRL_X_THESAURUS;
3398 if (*++e_cpt != ',' && *e_cpt != NUL)
3399 {
3400 dict = e_cpt;
3401 dict_f = DICT_FIRST;
3402 }
3403 }
3404#ifdef FEAT_FIND_ID
3405 else if (*e_cpt == 'i')
3406 type = CTRL_X_PATH_PATTERNS;
3407 else if (*e_cpt == 'd')
3408 type = CTRL_X_PATH_DEFINES;
3409#endif
3410 else if (*e_cpt == ']' || *e_cpt == 't')
3411 {
3412 type = CTRL_X_TAGS;
3413 sprintf((char*)IObuff, _("Scanning tags."));
3414 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3415 }
3416 else
3417 type = -1;
3418
3419 /* in any case e_cpt is advanced to the next entry */
3420 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3421
3422 found_all = TRUE;
3423 if (type == -1)
3424 continue;
3425 }
3426 }
3427
3428 switch (type)
3429 {
3430 case -1:
3431 break;
3432#ifdef FEAT_FIND_ID
3433 case CTRL_X_PATH_PATTERNS:
3434 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003435 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003436 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003438 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3440 (linenr_T)1, (linenr_T)MAXLNUM);
3441 break;
3442#endif
3443
3444 case CTRL_X_DICTIONARY:
3445 case CTRL_X_THESAURUS:
3446 ins_compl_dictionaries(
3447 dict ? dict
3448 : (type == CTRL_X_THESAURUS
3449 ? (*curbuf->b_p_tsr == NUL
3450 ? p_tsr
3451 : curbuf->b_p_tsr)
3452 : (*curbuf->b_p_dict == NUL
3453 ? p_dict
3454 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003455 compl_pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3457 dict = NULL;
3458 break;
3459
3460 case CTRL_X_TAGS:
3461 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3462 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003463 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003466 * of matches is found when compl_pattern is empty */
3467 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3469 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3470 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3471 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003472 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474 p_ic = save_p_ic;
3475 break;
3476
3477 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003478 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3480 {
3481
3482 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003483 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003484 ins_compl_add_matches(num_matches, matches,
3485#ifdef CASE_INSENSITIVE_FILENAME
3486 TRUE
3487#else
3488 FALSE
3489#endif
3490 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492 break;
3493
3494 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003495 if (expand_cmdline(&compl_xp, compl_pattern,
3496 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003498 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003501#ifdef FEAT_COMPL_FUNC
3502 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003503 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003504 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003505 break;
3506#endif
3507
Bram Moolenaar488c6512005-08-11 20:09:58 +00003508 case CTRL_X_SPELL:
3509#ifdef FEAT_SYN_HL
3510 num_matches = expand_spelling(first_match_pos.lnum,
3511 first_match_pos.col, compl_pattern, &matches);
3512 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003513 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003514#endif
3515 break;
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 default: /* normal ^P/^N and ^X^L */
3518 /*
3519 * If 'infercase' is set, don't use 'smartcase' here
3520 */
3521 save_p_scs = p_scs;
3522 if (ins_buf->b_p_inf)
3523 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 /* buffers other than curbuf are scanned from the beginning or the
3526 * end but never from the middle, thus setting nowrapscan in this
3527 * buffers is a good idea, on the other hand, we always set
3528 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3529 save_p_ws = p_ws;
3530 if (ins_buf != curbuf)
3531 p_ws = FALSE;
3532 else if (*e_cpt == '.')
3533 p_ws = TRUE;
3534 for (;;)
3535 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003536 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003538 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3539 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003543 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003545 found_new_match = searchit(NULL, ins_buf, pos,
3546 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003548 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003549 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003551 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 first_match_pos = *pos;
3554 last_match_pos = *pos;
3555 }
3556 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003557 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 found_new_match = FAIL;
3559 if (found_new_match == FAIL)
3560 {
3561 if (ins_buf == curbuf)
3562 found_all = TRUE;
3563 break;
3564 }
3565
3566 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 && ini->lnum == pos->lnum
3569 && ini->col == pos->col)
3570 continue;
3571 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3572 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3573 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003574 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3577 continue;
3578 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3579 if (!p_paste)
3580 ptr = skipwhite(ptr);
3581 }
3582 len = (int)STRLEN(ptr);
3583 }
3584 else
3585 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 char_u *tmp_ptr = ptr;
3587
3588 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003590 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 /* Skip if already inside a word. */
3592 if (vim_iswordp(tmp_ptr))
3593 continue;
3594 /* Find start of next word. */
3595 tmp_ptr = find_word_start(tmp_ptr);
3596 }
3597 /* Find end of this word. */
3598 tmp_ptr = find_word_end(tmp_ptr);
3599 len = (int)(tmp_ptr - ptr);
3600
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003601 if ((compl_cont_status & CONT_ADDING)
3602 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3605 {
3606 /* Try next line, if any. the new word will be
3607 * "join" as if the normal command "J" was used.
3608 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003609 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 * works -- Acevedo */
3611 STRNCPY(IObuff, ptr, len);
3612 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3613 tmp_ptr = ptr = skipwhite(ptr);
3614 /* Find start of next word. */
3615 tmp_ptr = find_word_start(tmp_ptr);
3616 /* Find end of next word. */
3617 tmp_ptr = find_word_end(tmp_ptr);
3618 if (tmp_ptr > ptr)
3619 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003620 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003622 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 IObuff[len++] = ' ';
3624 /* IObuf =~ "\k.* ", thus len >= 2 */
3625 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003626 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 || (vim_strchr(p_cpo, CPO_JOINSP)
3628 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003629 && (IObuff[len - 2] == '?'
3630 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 IObuff[len++] = ' ';
3632 }
3633 /* copy as much as posible of the new word */
3634 if (tmp_ptr - ptr >= IOSIZE - len)
3635 tmp_ptr = ptr + IOSIZE - len - 1;
3636 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3637 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003638 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 IObuff[len] = NUL;
3641 ptr = IObuff;
3642 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003643 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 continue;
3645 }
3646 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003647 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003648 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003649 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 found_new_match = OK;
3652 break;
3653 }
3654 }
3655 p_scs = save_p_scs;
3656 p_ws = save_p_ws;
3657 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003658
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003659 /* check if compl_curr_match has changed, (e.g. other type of
3660 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003661 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 found_new_match = OK;
3663
3664 /* break the loop for specialized modes (use 'complete' just for the
3665 * generic ctrl_x_mode == 0) or when we've found a new match */
3666 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003667 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003668 {
3669 if (got_int)
3670 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003671 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003672 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003673 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003674
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003675 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3676 || compl_interrupted)
3677 break;
3678 compl_started = TRUE;
3679 }
3680 else
3681 {
3682 /* Mark a buffer scanned when it has been scanned completely */
3683 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3684 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003686 compl_started = FALSE;
3687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003689 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3692 && *e_cpt == NUL) /* Got to end of 'complete' */
3693 found_new_match = FAIL;
3694
3695 i = -1; /* total of matches, unknown */
3696 if (found_new_match == FAIL
3697 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3698 i = ins_compl_make_cyclic();
3699
3700 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003701 * just been made cyclic then we have to move compl_curr_match to the next
3702 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003703 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003704 if (compl_curr_match == NULL)
3705 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 return i;
3707}
3708
3709/* Delete the old text being completed. */
3710 static void
3711ins_compl_delete()
3712{
3713 int i;
3714
3715 /*
3716 * In insert mode: Delete the typed part.
3717 * In replace mode: Put the old characters back, if any.
3718 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003719 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 backspace_until_column(i);
3721 changed_cline_bef_curs();
3722}
3723
3724/* Insert the new text being completed. */
3725 static void
3726ins_compl_insert()
3727{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003728 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003729 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3730 compl_used_match = FALSE;
3731 else
3732 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733}
3734
3735/*
3736 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003737 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3738 * get more completions. If it is FALSE, then we just do nothing when there
3739 * are no more completions in a given direction. The latter case is used when
3740 * we are still in the middle of finding completions, to allow browsing
3741 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 * Return the total number of matches, or -1 if still unknown -- webb.
3743 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003744 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3745 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 *
3747 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003748 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3749 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 */
3751 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003752ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003754 int count; /* repeat completion this many times; should
3755 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003756 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 int num_matches = -1;
3759 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003760 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003761 compl_T *found_compl = NULL;
3762 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003764 if (compl_leader != NULL
3765 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003767 /* Set "compl_shown_match" to the actually shown match, it may differ
3768 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003769 while (!ins_compl_equal(compl_shown_match,
3770 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003771 && compl_shown_match->cp_next != NULL
3772 && compl_shown_match->cp_next != compl_first_match)
3773 compl_shown_match = compl_shown_match->cp_next;
3774 }
3775
3776 if (allow_get_expansion && insert_match
3777 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 /* Delete old text to be replaced */
3779 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003780
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003781 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003782
3783 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3784 * around. */
3785 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003787 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003789 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003790 found_end = (compl_first_match != NULL
3791 && (compl_shown_match->cp_next == compl_first_match
3792 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003793 }
3794 else if (compl_shows_dir == BACKWARD
3795 && compl_shown_match->cp_prev != NULL)
3796 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003797 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003798 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003799 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003802 {
3803 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003804 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003805 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003806
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003807 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003808 if (compl_pending && compl_direction == compl_shows_dir)
3809 compl_shown_match = compl_curr_match;
3810 found_end = FALSE;
3811 }
3812 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3813 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003814 && !ins_compl_equal(compl_shown_match,
3815 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003816 ++todo;
3817 else
3818 /* Remember a matching item. */
3819 found_compl = compl_shown_match;
3820
3821 /* Stop at the end of the list when we found a usable match. */
3822 if (found_end)
3823 {
3824 if (found_compl != NULL)
3825 {
3826 compl_shown_match = found_compl;
3827 break;
3828 }
3829 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003833 /* Insert the text of the new completion, or the compl_leader. */
3834 if (insert_match)
3835 {
3836 if (!compl_get_longest || compl_used_match)
3837 ins_compl_insert();
3838 else
3839 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3840 }
3841 else
3842 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 if (!allow_get_expansion)
3845 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003846 /* may undisplay the popup menu first */
3847 ins_compl_upd_pum();
3848
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003849 /* redraw to show the user what was inserted */
3850 update_screen(0);
3851
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003852 /* display the updated popup menu */
3853 ins_compl_show_pum();
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 /* Delete old text to be replaced, since we're still searching and
3856 * don't want to match ourselves! */
3857 ins_compl_delete();
3858 }
3859
3860 /*
3861 * Show the file name for the match (if any)
3862 * Truncate the file name to avoid a wait for return.
3863 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003864 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
3866 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003867 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (i <= 0)
3869 i = 0;
3870 else
3871 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003872 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 msg(IObuff);
3874 redraw_cmdline = FALSE; /* don't overwrite! */
3875 }
3876
3877 return num_matches;
3878}
3879
3880/*
3881 * Call this while finding completions, to check whether the user has hit a key
3882 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003883 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003885 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 */
3887 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003888ins_compl_check_keys(frequency)
3889 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890{
3891 static int count = 0;
3892
3893 int c;
3894
3895 /* Don't check when reading keys from a script. That would break the test
3896 * scripts */
3897 if (using_script())
3898 return;
3899
3900 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003901 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 return;
3903 count = 0;
3904
3905 ++no_mapping;
3906 c = vpeekc_any();
3907 --no_mapping;
3908 if (c != NUL)
3909 {
3910 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3911 {
3912 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003913 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003914 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3915 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 if (compl_pending && !got_int)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003921 (void)ins_compl_next(FALSE, 1, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003922}
3923
3924/*
3925 * Decide the direction of Insert mode complete from the key typed.
3926 * Returns BACKWARD or FORWARD.
3927 */
3928 static int
3929ins_compl_key2dir(c)
3930 int c;
3931{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003932 if (c == Ctrl_P || c == Ctrl_L
3933 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
3934 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003935 return BACKWARD;
3936 return FORWARD;
3937}
3938
3939/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003940 * Return TRUE for keys that are used for completion only when the popup menu
3941 * is visible.
3942 */
3943 static int
3944ins_compl_pum_key(c)
3945 int c;
3946{
3947 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003948 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3949 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003950}
3951
3952/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003953 * Decide the number of completions to move forward.
3954 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3955 */
3956 static int
3957ins_compl_key2count(c)
3958 int c;
3959{
3960 int h;
3961
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003962 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003963 {
3964 h = pum_get_height();
3965 if (h > 3)
3966 h -= 2; /* keep some context */
3967 return h;
3968 }
3969 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970}
3971
3972/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003973 * Return TRUE if completion with "c" should insert the match, FALSE if only
3974 * to change the currently selected completion.
3975 */
3976 static int
3977ins_compl_use_match(c)
3978 int c;
3979{
3980 switch (c)
3981 {
3982 case K_UP:
3983 case K_DOWN:
3984 case K_PAGEDOWN:
3985 case K_KPAGEDOWN:
3986 case K_S_DOWN:
3987 case K_PAGEUP:
3988 case K_KPAGEUP:
3989 case K_S_UP:
3990 return FALSE;
3991 }
3992 return TRUE;
3993}
3994
3995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 * Do Insert mode completion.
3997 * Called when character "c" was typed, which has a meaning for completion.
3998 * Returns OK if completion was done, FAIL if something failed (out of mem).
3999 */
4000 static int
4001ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004004 char_u *line;
4005 int startcol = 0; /* column where searched text starts */
4006 colnr_T curs_col; /* cursor column */
4007 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaare3226be2005-12-18 22:10:00 +00004009 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004010 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 /* First time we hit ^N or ^P (in a row, I mean) */
4013
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 did_ai = FALSE;
4015#ifdef FEAT_SMARTINDENT
4016 did_si = FALSE;
4017 can_si = FALSE;
4018 can_si_back = FALSE;
4019#endif
4020 if (stop_arrow() == FAIL)
4021 return FAIL;
4022
4023 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
4026 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004027 * "compl_startpos" to the cursor as a pattern to add a new word
4028 * instead of expand the one before the cursor, in word-wise if
4029 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 * is not in the same line as the cursor then fix it (the line has
4031 * been split because it was longer than 'tw'). if SOL is set then
4032 * skip the previous pattern, a word at the beginning of the line has
4033 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004034 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4035 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
4037 /*
4038 * it is a continued search
4039 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004040 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4042 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4043 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046 /* line (probably) wrapped, set compl_startpos to the
4047 * first non_blank in the line, if it is not a wordchar
4048 * include it to get a better pattern, but then we don't
4049 * want the "\\<" prefix, check it bellow */
4050 compl_col = (colnr_T)(skipwhite(line) - line);
4051 compl_startpos.col = compl_col;
4052 compl_startpos.lnum = curwin->w_cursor.lnum;
4053 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055 else
4056 {
4057 /* S_IPOS was set when we inserted a word that was at the
4058 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 * mode but first we need to redefine compl_startpos */
4060 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 compl_cont_status |= CONT_SOL;
4063 compl_startpos.col = (colnr_T)(skipwhite(
4064 line + compl_length
4065 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004067 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004070 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * have enough space? just being paranoic */
4072#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004073 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075 compl_cont_status &= ~CONT_SOL;
4076 compl_length = (IOSIZE - MIN_SPACE);
4077 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004079 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4080 if (compl_length < 1)
4081 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004084 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004086 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004089 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004091 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 compl_cont_status = 0;
4096 compl_cont_status |= CONT_N_ADDS;
4097 compl_startpos = curwin->w_cursor;
4098 startcol = (int)curs_col;
4099 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101
4102 /* Work out completion pattern and original text -- webb */
4103 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4104 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004105 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4107 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004108 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 compl_col += ++startcol;
4113 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116 compl_pattern = str_foldcase(line + compl_col,
4117 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004119 compl_pattern = vim_strnsave(line + compl_col,
4120 compl_length);
4121 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return FAIL;
4123 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
4126 char_u *prefix = (char_u *)"\\<";
4127
4128 /* we need 3 extra chars, 1 for the NUL and
4129 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4131 compl_length) + 3);
4132 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 if (!vim_iswordp(line + compl_col)
4135 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 && (
4137#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004138 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004140 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141#endif
4142 )))
4143 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 STRCPY((char *)compl_pattern, prefix);
4145 (void)quote_meta(compl_pattern + STRLEN(prefix),
4146 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004152 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#endif
4154 )
4155 {
4156 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4158 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004160 compl_col += curs_col;
4161 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 else
4164 {
4165#ifdef FEAT_MBYTE
4166 /* Search the point of change class of multibyte character
4167 * or not a word single byte character backward. */
4168 if (has_mbyte)
4169 {
4170 int base_class;
4171 int head_off;
4172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004173 startcol -= (*mb_head_off)(line, line + startcol);
4174 base_class = mb_get_class(line + startcol);
4175 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 head_off = (*mb_head_off)(line, line + startcol);
4178 if (base_class != mb_get_class(line + startcol
4179 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004181 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 }
4184 else
4185#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004186 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188 compl_col += ++startcol;
4189 compl_length = (int)curs_col - startcol;
4190 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
4192 /* Only match word with at least two chars -- webb
4193 * there's no need to call quote_meta,
4194 * alloc(7) is enough -- Acevedo
4195 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 compl_pattern = alloc(7);
4197 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 STRCPY((char *)compl_pattern, "\\<");
4200 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4201 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 else
4204 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4206 compl_length) + 3);
4207 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004209 STRCPY((char *)compl_pattern, "\\<");
4210 (void)quote_meta(compl_pattern + 2, line + compl_col,
4211 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213 }
4214 }
4215 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4216 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 compl_col = skipwhite(line) - line;
4218 compl_length = (int)curs_col - (int)compl_col;
4219 if (compl_length < 0) /* cursor in indent: empty pattern */
4220 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004222 compl_pattern = str_foldcase(line + compl_col, compl_length,
4223 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4226 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228 }
4229 else if (ctrl_x_mode == CTRL_X_FILES)
4230 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004231 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233 compl_col += ++startcol;
4234 compl_length = (int)curs_col - startcol;
4235 compl_pattern = addstar(line + compl_col, compl_length,
4236 EXPAND_FILES);
4237 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239 }
4240 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4241 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 compl_pattern = vim_strnsave(line, curs_col);
4243 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 set_cmd_context(&compl_xp, compl_pattern,
4246 (int)STRLEN(compl_pattern), curs_col);
4247 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4248 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4251 compl_col = startcol;
4252 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004254 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004255 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004256#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004257 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004258 * Call user defined function 'completefunc' with "a:findstart"
4259 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004260 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004261 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004262 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004263 char_u *funcname;
4264 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004265
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004266 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004267 * string */
4268 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4269 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4270 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271 {
4272 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4273 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004274 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004275 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004276
4277 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004278 args[1] = NULL;
4279 pos = curwin->w_cursor;
4280 col = call_func_retnr(funcname, 2, args, FALSE);
4281 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004282
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004283 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004284 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004285 compl_col = col;
4286 if ((colnr_T)compl_col > curs_col)
4287 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 /* Setup variables for completion. Need to obtain "line" again,
4290 * it may have become invalid. */
4291 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004292 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004293 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4294 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004295#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 return FAIL;
4297 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004298 else if (ctrl_x_mode == CTRL_X_SPELL)
4299 {
4300#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004301 if (spell_bad_len > 0)
4302 compl_col = curs_col - spell_bad_len;
4303 else
4304 compl_col = spell_word_start(startcol);
4305 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004306 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004307 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004308 compl_length = (int)curs_col - compl_col;
4309 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4310 if (compl_pattern == NULL)
4311#endif
4312 return FAIL;
4313 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314 else
4315 {
4316 EMSG2(_(e_intern2), "ins_complete()");
4317 return FAIL;
4318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322 edit_submode_pre = (char_u *)_(" Adding");
4323 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4324 {
4325 /* Insert a new line, keep indentation but ignore 'comments' */
4326#ifdef FEAT_COMMENTS
4327 char_u *old = curbuf->b_p_com;
4328
4329 curbuf->b_p_com = (char_u *)"";
4330#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 compl_startpos.lnum = curwin->w_cursor.lnum;
4332 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 ins_eol('\r');
4334#ifdef FEAT_COMMENTS
4335 curbuf->b_p_com = old;
4336#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 compl_length = 0;
4338 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 }
4341 else
4342 {
4343 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 if (compl_cont_status & CONT_LOCAL)
4348 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 else
4350 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4351
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004352 /* Always add completion for the original text. */
4353 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4355 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004356 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 vim_free(compl_pattern);
4359 compl_pattern = NULL;
4360 vim_free(compl_orig_text);
4361 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return FAIL;
4363 }
4364
4365 /* showmode might reset the internal line pointers, so it must
4366 * be called before line = ml_get(), or when this address is no
4367 * longer needed. -- Acevedo.
4368 */
4369 edit_submode_extra = (char_u *)_("-- Searching...");
4370 edit_submode_highl = HLF_COUNT;
4371 showmode();
4372 edit_submode_extra = NULL;
4373 out_flush();
4374 }
4375
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004376 compl_shown_match = compl_curr_match;
4377 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004380 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004382 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004384 /* may undisplay the popup menu */
4385 ins_compl_upd_pum();
4386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 if (n > 1) /* all matches have been found */
4388 compl_matches = n;
4389 compl_curr_match = compl_shown_match;
4390 compl_direction = compl_shows_dir;
4391 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 /* eat the ESC to avoid leaving insert mode */
4394 if (got_int && !global_busy)
4395 {
4396 (void)vgetc();
4397 got_int = FALSE;
4398 }
4399
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004400 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004401 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4404 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4406 edit_submode_highl = HLF_E;
4407 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4408 * because we couldn't expand anything at first place, but if we used
4409 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4410 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411 if ( compl_length > 1
4412 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 || (ctrl_x_mode != 0
4414 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4415 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418
Bram Moolenaar572cb562005-08-05 21:35:02 +00004419 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 if (edit_submode_extra == NULL)
4425 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004426 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
4428 edit_submode_extra = (char_u *)_("Back at original");
4429 edit_submode_highl = HLF_W;
4430 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
4433 edit_submode_extra = (char_u *)_("Word from other line");
4434 edit_submode_highl = HLF_COUNT;
4435 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004436 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 edit_submode_extra = (char_u *)_("The only match");
4439 edit_submode_highl = HLF_COUNT;
4440 }
4441 else
4442 {
4443 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004444 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004446 int number = 0;
4447 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
4451 /* search backwards for the first valid (!= -1) number.
4452 * This should normally succeed already at the first loop
4453 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004454 for (match = compl_curr_match->cp_prev; match != NULL
4455 && match != compl_first_match;
4456 match = match->cp_prev)
4457 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004459 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 break;
4461 }
4462 if (match != NULL)
4463 /* go up and assign all numbers which are not assigned
4464 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004465 for (match = match->cp_next;
4466 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004467 match = match->cp_next)
4468 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 else /* BACKWARD */
4471 {
4472 /* search forwards (upwards) for the first valid (!= -1)
4473 * number. This should normally succeed already at the
4474 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004475 for (match = compl_curr_match->cp_next; match != NULL
4476 && match != compl_first_match;
4477 match = match->cp_next)
4478 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004480 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 break;
4482 }
4483 if (match != NULL)
4484 /* go down and assign all numbers which are not
4485 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004486 for (match = match->cp_prev; match
4487 && match->cp_number == -1;
4488 match = match->cp_prev)
4489 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 }
4492
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004493 /* The match should always have a sequence number now, this is
4494 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004495 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
4497 /* Space for 10 text chars. + 2x10-digit no.s */
4498 static char_u match_ref[31];
4499
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004502 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004505 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004506 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 edit_submode_extra = match_ref;
4508 edit_submode_highl = HLF_R;
4509 if (dollar_vcol)
4510 curs_columns(FALSE);
4511 }
4512 }
4513 }
4514
4515 /* Show a message about what (completion) mode we're in. */
4516 showmode();
4517 if (edit_submode_extra != NULL)
4518 {
4519 if (!p_smd)
4520 msg_attr(edit_submode_extra,
4521 edit_submode_highl < HLF_COUNT
4522 ? hl_attr(edit_submode_highl) : 0);
4523 }
4524 else
4525 msg_clr_cmdline(); /* necessary for "noshowmode" */
4526
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004527 ins_compl_show_pum();
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 return OK;
4530}
4531
4532/*
4533 * Looks in the first "len" chars. of "src" for search-metachars.
4534 * If dest is not NULL the chars. are copied there quoting (with
4535 * a backslash) the metachars, and dest would be NUL terminated.
4536 * Returns the length (needed) of dest
4537 */
4538 static int
4539quote_meta(dest, src, len)
4540 char_u *dest;
4541 char_u *src;
4542 int len;
4543{
4544 int m;
4545
4546 for (m = len; --len >= 0; src++)
4547 {
4548 switch (*src)
4549 {
4550 case '.':
4551 case '*':
4552 case '[':
4553 if (ctrl_x_mode == CTRL_X_DICTIONARY
4554 || ctrl_x_mode == CTRL_X_THESAURUS)
4555 break;
4556 case '~':
4557 if (!p_magic) /* quote these only if magic is set */
4558 break;
4559 case '\\':
4560 if (ctrl_x_mode == CTRL_X_DICTIONARY
4561 || ctrl_x_mode == CTRL_X_THESAURUS)
4562 break;
4563 case '^': /* currently it's not needed. */
4564 case '$':
4565 m++;
4566 if (dest != NULL)
4567 *dest++ = '\\';
4568 break;
4569 }
4570 if (dest != NULL)
4571 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004572# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 /* Copy remaining bytes of a multibyte character. */
4574 if (has_mbyte)
4575 {
4576 int i, mb_len;
4577
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004578 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (mb_len > 0 && len >= mb_len)
4580 for (i = 0; i < mb_len; ++i)
4581 {
4582 --len;
4583 ++src;
4584 if (dest != NULL)
4585 *dest++ = *src;
4586 }
4587 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 if (dest != NULL)
4591 *dest = NUL;
4592
4593 return m;
4594}
4595#endif /* FEAT_INS_EXPAND */
4596
4597/*
4598 * Next character is interpreted literally.
4599 * A one, two or three digit decimal number is interpreted as its byte value.
4600 * If one or two digits are entered, the next character is given to vungetc().
4601 * For Unicode a character > 255 may be returned.
4602 */
4603 int
4604get_literal()
4605{
4606 int cc;
4607 int nc;
4608 int i;
4609 int hex = FALSE;
4610 int octal = FALSE;
4611#ifdef FEAT_MBYTE
4612 int unicode = 0;
4613#endif
4614
4615 if (got_int)
4616 return Ctrl_C;
4617
4618#ifdef FEAT_GUI
4619 /*
4620 * In GUI there is no point inserting the internal code for a special key.
4621 * It is more useful to insert the string "<KEY>" instead. This would
4622 * probably be useful in a text window too, but it would not be
4623 * vi-compatible (maybe there should be an option for it?) -- webb
4624 */
4625 if (gui.in_use)
4626 ++allow_keys;
4627#endif
4628#ifdef USE_ON_FLY_SCROLL
4629 dont_scroll = TRUE; /* disallow scrolling here */
4630#endif
4631 ++no_mapping; /* don't map the next key hits */
4632 cc = 0;
4633 i = 0;
4634 for (;;)
4635 {
4636 do
4637 nc = safe_vgetc();
4638 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4639 || nc == K_HOR_SCROLLBAR);
4640#ifdef FEAT_CMDL_INFO
4641 if (!(State & CMDLINE)
4642# ifdef FEAT_MBYTE
4643 && MB_BYTE2LEN_CHECK(nc) == 1
4644# endif
4645 )
4646 add_to_showcmd(nc);
4647#endif
4648 if (nc == 'x' || nc == 'X')
4649 hex = TRUE;
4650 else if (nc == 'o' || nc == 'O')
4651 octal = TRUE;
4652#ifdef FEAT_MBYTE
4653 else if (nc == 'u' || nc == 'U')
4654 unicode = nc;
4655#endif
4656 else
4657 {
4658 if (hex
4659#ifdef FEAT_MBYTE
4660 || unicode != 0
4661#endif
4662 )
4663 {
4664 if (!vim_isxdigit(nc))
4665 break;
4666 cc = cc * 16 + hex2nr(nc);
4667 }
4668 else if (octal)
4669 {
4670 if (nc < '0' || nc > '7')
4671 break;
4672 cc = cc * 8 + nc - '0';
4673 }
4674 else
4675 {
4676 if (!VIM_ISDIGIT(nc))
4677 break;
4678 cc = cc * 10 + nc - '0';
4679 }
4680
4681 ++i;
4682 }
4683
4684 if (cc > 255
4685#ifdef FEAT_MBYTE
4686 && unicode == 0
4687#endif
4688 )
4689 cc = 255; /* limit range to 0-255 */
4690 nc = 0;
4691
4692 if (hex) /* hex: up to two chars */
4693 {
4694 if (i >= 2)
4695 break;
4696 }
4697#ifdef FEAT_MBYTE
4698 else if (unicode) /* Unicode: up to four or eight chars */
4699 {
4700 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4701 break;
4702 }
4703#endif
4704 else if (i >= 3) /* decimal or octal: up to three chars */
4705 break;
4706 }
4707 if (i == 0) /* no number entered */
4708 {
4709 if (nc == K_ZERO) /* NUL is stored as NL */
4710 {
4711 cc = '\n';
4712 nc = 0;
4713 }
4714 else
4715 {
4716 cc = nc;
4717 nc = 0;
4718 }
4719 }
4720
4721 if (cc == 0) /* NUL is stored as NL */
4722 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004723#ifdef FEAT_MBYTE
4724 if (enc_dbcs && (cc & 0xff) == 0)
4725 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4726 second byte will cause trouble! */
4727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 --no_mapping;
4730#ifdef FEAT_GUI
4731 if (gui.in_use)
4732 --allow_keys;
4733#endif
4734 if (nc)
4735 vungetc(nc);
4736 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4737 return cc;
4738}
4739
4740/*
4741 * Insert character, taking care of special keys and mod_mask
4742 */
4743 static void
4744insert_special(c, allow_modmask, ctrlv)
4745 int c;
4746 int allow_modmask;
4747 int ctrlv; /* c was typed after CTRL-V */
4748{
4749 char_u *p;
4750 int len;
4751
4752 /*
4753 * Special function key, translate into "<Key>". Up to the last '>' is
4754 * inserted with ins_str(), so as not to replace characters in replace
4755 * mode.
4756 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4757 * unless 'allow_modmask' is TRUE.
4758 */
4759#ifdef MACOS
4760 /* Command-key never produces a normal key */
4761 if (mod_mask & MOD_MASK_CMD)
4762 allow_modmask = TRUE;
4763#endif
4764 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4765 {
4766 p = get_special_key_name(c, mod_mask);
4767 len = (int)STRLEN(p);
4768 c = p[len - 1];
4769 if (len > 2)
4770 {
4771 if (stop_arrow() == FAIL)
4772 return;
4773 p[len - 1] = NUL;
4774 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004775 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 ctrlv = FALSE;
4777 }
4778 }
4779 if (stop_arrow() == OK)
4780 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4781}
4782
4783/*
4784 * Special characters in this context are those that need processing other
4785 * than the simple insertion that can be performed here. This includes ESC
4786 * which terminates the insert, and CR/NL which need special processing to
4787 * open up a new line. This routine tries to optimize insertions performed by
4788 * the "redo", "undo" or "put" commands, so it needs to know when it should
4789 * stop and defer processing to the "normal" mechanism.
4790 * '0' and '^' are special, because they can be followed by CTRL-D.
4791 */
4792#ifdef EBCDIC
4793# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4794#else
4795# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4796#endif
4797
4798#ifdef FEAT_MBYTE
4799# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4800#else
4801# define WHITECHAR(cc) vim_iswhite(cc)
4802#endif
4803
4804 void
4805insertchar(c, flags, second_indent)
4806 int c; /* character to insert or NUL */
4807 int flags; /* INSCHAR_FORMAT, etc. */
4808 int second_indent; /* indent for second line if >= 0 */
4809{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 int textwidth;
4811#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4817 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
4819 /*
4820 * Try to break the line in two or more pieces when:
4821 * - Always do this if we have been called to do formatting only.
4822 * - Always do this when 'formatoptions' has the 'a' flag and the line
4823 * ends in white space.
4824 * - Otherwise:
4825 * - Don't do this if inserting a blank
4826 * - Don't do this if an existing character is being replaced, unless
4827 * we're in VREPLACE mode.
4828 * - Do this if the cursor is not on the line where insert started
4829 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4830 * before the insert.
4831 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4832 * before 'textwidth'
4833 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004834 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 && ((flags & INSCHAR_FORMAT)
4836 || (!vim_iswhite(c)
4837 && !((State & REPLACE_FLAG)
4838#ifdef FEAT_VREPLACE
4839 && !(State & VREPLACE_FLAG)
4840#endif
4841 && *ml_get_cursor() != NUL)
4842 && (curwin->w_cursor.lnum != Insstart.lnum
4843 || ((!has_format_option(FO_INS_LONG)
4844 || Insstart_textlen <= (colnr_T)textwidth)
4845 && (!fo_ins_blank
4846 || Insstart_blank_vcol <= (colnr_T)textwidth
4847 ))))))
4848 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004849 /* Format with 'formatexpr' when it's set. Use internal formatting
4850 * when 'formatexpr' isn't set or it returns non-zero. */
4851#if defined(FEAT_EVAL)
4852 if (*curbuf->b_p_fex == NUL
4853 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004855 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004857
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (c == NUL) /* only formatting was wanted */
4859 return;
4860
4861#ifdef FEAT_COMMENTS
4862 /* Check whether this character should end a comment. */
4863 if (did_ai && (int)c == end_comment_pending)
4864 {
4865 char_u *line;
4866 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4867 int middle_len, end_len;
4868 int i;
4869
4870 /*
4871 * Need to remove existing (middle) comment leader and insert end
4872 * comment leader. First, check what comment leader we can find.
4873 */
4874 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4875 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4876 {
4877 /* Skip middle-comment string */
4878 while (*p && p[-1] != ':') /* find end of middle flags */
4879 ++p;
4880 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4881 /* Don't count trailing white space for middle_len */
4882 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4883 --middle_len;
4884
4885 /* Find the end-comment string */
4886 while (*p && p[-1] != ':') /* find end of end flags */
4887 ++p;
4888 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4889
4890 /* Skip white space before the cursor */
4891 i = curwin->w_cursor.col;
4892 while (--i >= 0 && vim_iswhite(line[i]))
4893 ;
4894 i++;
4895
4896 /* Skip to before the middle leader */
4897 i -= middle_len;
4898
4899 /* Check some expected things before we go on */
4900 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4901 {
4902 /* Backspace over all the stuff we want to replace */
4903 backspace_until_column(i);
4904
4905 /*
4906 * Insert the end-comment string, except for the last
4907 * character, which will get inserted as normal later.
4908 */
4909 ins_bytes_len(lead_end, end_len - 1);
4910 }
4911 }
4912 }
4913 end_comment_pending = NUL;
4914#endif
4915
4916 did_ai = FALSE;
4917#ifdef FEAT_SMARTINDENT
4918 did_si = FALSE;
4919 can_si = FALSE;
4920 can_si_back = FALSE;
4921#endif
4922
4923 /*
4924 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4925 * This speeds up normal text input considerably.
4926 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4927 * need to re-indent at a ':', or any other character (but not what
4928 * 'paste' is set)..
4929 */
4930#ifdef USE_ON_FLY_SCROLL
4931 dont_scroll = FALSE; /* allow scrolling here */
4932#endif
4933
4934 if ( !ISSPECIAL(c)
4935#ifdef FEAT_MBYTE
4936 && (!has_mbyte || (*mb_char2len)(c) == 1)
4937#endif
4938 && vpeekc() != NUL
4939 && !(State & REPLACE_FLAG)
4940#ifdef FEAT_CINDENT
4941 && !cindent_on()
4942#endif
4943#ifdef FEAT_RIGHTLEFT
4944 && !p_ri
4945#endif
4946 )
4947 {
4948#define INPUT_BUFLEN 100
4949 char_u buf[INPUT_BUFLEN + 1];
4950 int i;
4951 colnr_T virtcol = 0;
4952
4953 buf[0] = c;
4954 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004955 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 virtcol = get_nolist_virtcol();
4957 /*
4958 * Stop the string when:
4959 * - no more chars available
4960 * - finding a special character (command key)
4961 * - buffer is full
4962 * - running into the 'textwidth' boundary
4963 * - need to check for abbreviation: A non-word char after a word-char
4964 */
4965 while ( (c = vpeekc()) != NUL
4966 && !ISSPECIAL(c)
4967#ifdef FEAT_MBYTE
4968 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4969#endif
4970 && i < INPUT_BUFLEN
4971 && (textwidth == 0
4972 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4973 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4974 {
4975#ifdef FEAT_RIGHTLEFT
4976 c = vgetc();
4977 if (p_hkmap && KeyTyped)
4978 c = hkmap(c); /* Hebrew mode mapping */
4979# ifdef FEAT_FKMAP
4980 if (p_fkmap && KeyTyped)
4981 c = fkmap(c); /* Farsi mode mapping */
4982# endif
4983 buf[i++] = c;
4984#else
4985 buf[i++] = vgetc();
4986#endif
4987 }
4988
4989#ifdef FEAT_DIGRAPHS
4990 do_digraph(-1); /* clear digraphs */
4991 do_digraph(buf[i-1]); /* may be the start of a digraph */
4992#endif
4993 buf[i] = NUL;
4994 ins_str(buf);
4995 if (flags & INSCHAR_CTRLV)
4996 {
4997 redo_literal(*buf);
4998 i = 1;
4999 }
5000 else
5001 i = 0;
5002 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005003 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005 else
5006 {
5007#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005008 int cc;
5009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5011 {
5012 char_u buf[MB_MAXBYTES + 1];
5013
5014 (*mb_char2bytes)(c, buf);
5015 buf[cc] = NUL;
5016 ins_char_bytes(buf, cc);
5017 AppendCharToRedobuff(c);
5018 }
5019 else
5020#endif
5021 {
5022 ins_char(c);
5023 if (flags & INSCHAR_CTRLV)
5024 redo_literal(c);
5025 else
5026 AppendCharToRedobuff(c);
5027 }
5028 }
5029}
5030
5031/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005032 * Format text at the current insert position.
5033 */
5034 static void
5035internal_format(textwidth, second_indent, flags, format_only)
5036 int textwidth;
5037 int second_indent;
5038 int flags;
5039 int format_only;
5040{
5041 int cc;
5042 int save_char = NUL;
5043 int haveto_redraw = FALSE;
5044 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5045#ifdef FEAT_MBYTE
5046 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5047#endif
5048 int fo_white_par = has_format_option(FO_WHITE_PAR);
5049 int first_line = TRUE;
5050#ifdef FEAT_COMMENTS
5051 colnr_T leader_len;
5052 int no_leader = FALSE;
5053 int do_comments = (flags & INSCHAR_DO_COM);
5054#endif
5055
5056 /*
5057 * When 'ai' is off we don't want a space under the cursor to be
5058 * deleted. Replace it with an 'x' temporarily.
5059 */
5060 if (!curbuf->b_p_ai)
5061 {
5062 cc = gchar_cursor();
5063 if (vim_iswhite(cc))
5064 {
5065 save_char = cc;
5066 pchar_cursor('x');
5067 }
5068 }
5069
5070 /*
5071 * Repeat breaking lines, until the current line is not too long.
5072 */
5073 while (!got_int)
5074 {
5075 int startcol; /* Cursor column at entry */
5076 int wantcol; /* column at textwidth border */
5077 int foundcol; /* column for start of spaces */
5078 int end_foundcol = 0; /* column for start of word */
5079 colnr_T len;
5080 colnr_T virtcol;
5081#ifdef FEAT_VREPLACE
5082 int orig_col = 0;
5083 char_u *saved_text = NULL;
5084#endif
5085 colnr_T col;
5086
5087 virtcol = get_nolist_virtcol();
5088 if (virtcol < (colnr_T)textwidth)
5089 break;
5090
5091#ifdef FEAT_COMMENTS
5092 if (no_leader)
5093 do_comments = FALSE;
5094 else if (!(flags & INSCHAR_FORMAT)
5095 && has_format_option(FO_WRAP_COMS))
5096 do_comments = TRUE;
5097
5098 /* Don't break until after the comment leader */
5099 if (do_comments)
5100 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5101 else
5102 leader_len = 0;
5103
5104 /* If the line doesn't start with a comment leader, then don't
5105 * start one in a following broken line. Avoids that a %word
5106 * moved to the start of the next line causes all following lines
5107 * to start with %. */
5108 if (leader_len == 0)
5109 no_leader = TRUE;
5110#endif
5111 if (!(flags & INSCHAR_FORMAT)
5112#ifdef FEAT_COMMENTS
5113 && leader_len == 0
5114#endif
5115 && !has_format_option(FO_WRAP))
5116
5117 {
5118 textwidth = 0;
5119 break;
5120 }
5121 if ((startcol = curwin->w_cursor.col) == 0)
5122 break;
5123
5124 /* find column of textwidth border */
5125 coladvance((colnr_T)textwidth);
5126 wantcol = curwin->w_cursor.col;
5127
5128 curwin->w_cursor.col = startcol - 1;
5129#ifdef FEAT_MBYTE
5130 /* Correct cursor for multi-byte character. */
5131 if (has_mbyte)
5132 mb_adjust_cursor();
5133#endif
5134 foundcol = 0;
5135
5136 /*
5137 * Find position to break at.
5138 * Stop at first entered white when 'formatoptions' has 'v'
5139 */
5140 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5141 || curwin->w_cursor.lnum != Insstart.lnum
5142 || curwin->w_cursor.col >= Insstart.col)
5143 {
5144 cc = gchar_cursor();
5145 if (WHITECHAR(cc))
5146 {
5147 /* remember position of blank just before text */
5148 end_foundcol = curwin->w_cursor.col;
5149
5150 /* find start of sequence of blanks */
5151 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5152 {
5153 dec_cursor();
5154 cc = gchar_cursor();
5155 }
5156 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5157 break; /* only spaces in front of text */
5158#ifdef FEAT_COMMENTS
5159 /* Don't break until after the comment leader */
5160 if (curwin->w_cursor.col < leader_len)
5161 break;
5162#endif
5163 if (has_format_option(FO_ONE_LETTER))
5164 {
5165 /* do not break after one-letter words */
5166 if (curwin->w_cursor.col == 0)
5167 break; /* one-letter word at begin */
5168
5169 col = curwin->w_cursor.col;
5170 dec_cursor();
5171 cc = gchar_cursor();
5172
5173 if (WHITECHAR(cc))
5174 continue; /* one-letter, continue */
5175 curwin->w_cursor.col = col;
5176 }
5177#ifdef FEAT_MBYTE
5178 if (has_mbyte)
5179 foundcol = curwin->w_cursor.col
5180 + (*mb_ptr2len)(ml_get_cursor());
5181 else
5182#endif
5183 foundcol = curwin->w_cursor.col + 1;
5184 if (curwin->w_cursor.col < (colnr_T)wantcol)
5185 break;
5186 }
5187#ifdef FEAT_MBYTE
5188 else if (cc >= 0x100 && fo_multibyte
5189 && curwin->w_cursor.col <= (colnr_T)wantcol)
5190 {
5191 /* Break after or before a multi-byte character. */
5192 foundcol = curwin->w_cursor.col;
5193 if (curwin->w_cursor.col < (colnr_T)wantcol)
5194 foundcol += (*mb_char2len)(cc);
5195 end_foundcol = foundcol;
5196 break;
5197 }
5198#endif
5199 if (curwin->w_cursor.col == 0)
5200 break;
5201 dec_cursor();
5202 }
5203
5204 if (foundcol == 0) /* no spaces, cannot break line */
5205 {
5206 curwin->w_cursor.col = startcol;
5207 break;
5208 }
5209
5210 /* Going to break the line, remove any "$" now. */
5211 undisplay_dollar();
5212
5213 /*
5214 * Offset between cursor position and line break is used by replace
5215 * stack functions. VREPLACE does not use this, and backspaces
5216 * over the text instead.
5217 */
5218#ifdef FEAT_VREPLACE
5219 if (State & VREPLACE_FLAG)
5220 orig_col = startcol; /* Will start backspacing from here */
5221 else
5222#endif
5223 replace_offset = startcol - end_foundcol - 1;
5224
5225 /*
5226 * adjust startcol for spaces that will be deleted and
5227 * characters that will remain on top line
5228 */
5229 curwin->w_cursor.col = foundcol;
5230 while (cc = gchar_cursor(), WHITECHAR(cc))
5231 inc_cursor();
5232 startcol -= curwin->w_cursor.col;
5233 if (startcol < 0)
5234 startcol = 0;
5235
5236#ifdef FEAT_VREPLACE
5237 if (State & VREPLACE_FLAG)
5238 {
5239 /*
5240 * In VREPLACE mode, we will backspace over the text to be
5241 * wrapped, so save a copy now to put on the next line.
5242 */
5243 saved_text = vim_strsave(ml_get_cursor());
5244 curwin->w_cursor.col = orig_col;
5245 if (saved_text == NULL)
5246 break; /* Can't do it, out of memory */
5247 saved_text[startcol] = NUL;
5248
5249 /* Backspace over characters that will move to the next line */
5250 if (!fo_white_par)
5251 backspace_until_column(foundcol);
5252 }
5253 else
5254#endif
5255 {
5256 /* put cursor after pos. to break line */
5257 if (!fo_white_par)
5258 curwin->w_cursor.col = foundcol;
5259 }
5260
5261 /*
5262 * Split the line just before the margin.
5263 * Only insert/delete lines, but don't really redraw the window.
5264 */
5265 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5266 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5267#ifdef FEAT_COMMENTS
5268 + (do_comments ? OPENLINE_DO_COM : 0)
5269#endif
5270 , old_indent);
5271 old_indent = 0;
5272
5273 replace_offset = 0;
5274 if (first_line)
5275 {
5276 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5277 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5278 if (second_indent >= 0)
5279 {
5280#ifdef FEAT_VREPLACE
5281 if (State & VREPLACE_FLAG)
5282 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5283 else
5284#endif
5285 (void)set_indent(second_indent, SIN_CHANGED);
5286 }
5287 first_line = FALSE;
5288 }
5289
5290#ifdef FEAT_VREPLACE
5291 if (State & VREPLACE_FLAG)
5292 {
5293 /*
5294 * In VREPLACE mode we have backspaced over the text to be
5295 * moved, now we re-insert it into the new line.
5296 */
5297 ins_bytes(saved_text);
5298 vim_free(saved_text);
5299 }
5300 else
5301#endif
5302 {
5303 /*
5304 * Check if cursor is not past the NUL off the line, cindent
5305 * may have added or removed indent.
5306 */
5307 curwin->w_cursor.col += startcol;
5308 len = (colnr_T)STRLEN(ml_get_curline());
5309 if (curwin->w_cursor.col > len)
5310 curwin->w_cursor.col = len;
5311 }
5312
5313 haveto_redraw = TRUE;
5314#ifdef FEAT_CINDENT
5315 can_cindent = TRUE;
5316#endif
5317 /* moved the cursor, don't autoindent or cindent now */
5318 did_ai = FALSE;
5319#ifdef FEAT_SMARTINDENT
5320 did_si = FALSE;
5321 can_si = FALSE;
5322 can_si_back = FALSE;
5323#endif
5324 line_breakcheck();
5325 }
5326
5327 if (save_char != NUL) /* put back space after cursor */
5328 pchar_cursor(save_char);
5329
5330 if (!format_only && haveto_redraw)
5331 {
5332 update_topline();
5333 redraw_curbuf_later(VALID);
5334 }
5335}
5336
5337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 * Called after inserting or deleting text: When 'formatoptions' includes the
5339 * 'a' flag format from the current line until the end of the paragraph.
5340 * Keep the cursor at the same position relative to the text.
5341 * The caller must have saved the cursor line for undo, following ones will be
5342 * saved here.
5343 */
5344 void
5345auto_format(trailblank, prev_line)
5346 int trailblank; /* when TRUE also format with trailing blank */
5347 int prev_line; /* may start in previous line */
5348{
5349 pos_T pos;
5350 colnr_T len;
5351 char_u *old;
5352 char_u *new, *pnew;
5353 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005354 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 if (!has_format_option(FO_AUTO))
5357 return;
5358
5359 pos = curwin->w_cursor;
5360 old = ml_get_curline();
5361
5362 /* may remove added space */
5363 check_auto_format(FALSE);
5364
5365 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5366 * user might insert normal text next. Also skip formatting when "1" is
5367 * in 'formatoptions' and there is a single character before the cursor.
5368 * Otherwise the line would be broken and when typing another non-white
5369 * next they are not joined back together. */
5370 wasatend = (pos.col == STRLEN(old));
5371 if (*old != NUL && !trailblank && wasatend)
5372 {
5373 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005374 cc = gchar_cursor();
5375 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5376 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005378 cc = gchar_cursor();
5379 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
5381 curwin->w_cursor = pos;
5382 return;
5383 }
5384 curwin->w_cursor = pos;
5385 }
5386
5387#ifdef FEAT_COMMENTS
5388 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5389 * comments. */
5390 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5391 && get_leader_len(old, NULL, FALSE) == 0)
5392 return;
5393#endif
5394
5395 /*
5396 * May start formatting in a previous line, so that after "x" a word is
5397 * moved to the previous line if it fits there now. Only when this is not
5398 * the start of a paragraph.
5399 */
5400 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5401 {
5402 --curwin->w_cursor.lnum;
5403 if (u_save_cursor() == FAIL)
5404 return;
5405 }
5406
5407 /*
5408 * Do the formatting and restore the cursor position. "saved_cursor" will
5409 * be adjusted for the text formatting.
5410 */
5411 saved_cursor = pos;
5412 format_lines((linenr_T)-1);
5413 curwin->w_cursor = saved_cursor;
5414 saved_cursor.lnum = 0;
5415
5416 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5417 {
5418 /* "cannot happen" */
5419 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5420 coladvance((colnr_T)MAXCOL);
5421 }
5422 else
5423 check_cursor_col();
5424
5425 /* Insert mode: If the cursor is now after the end of the line while it
5426 * previously wasn't, the line was broken. Because of the rule above we
5427 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5428 * formatted. */
5429 if (!wasatend && has_format_option(FO_WHITE_PAR))
5430 {
5431 new = ml_get_curline();
5432 len = STRLEN(new);
5433 if (curwin->w_cursor.col == len)
5434 {
5435 pnew = vim_strnsave(new, len + 2);
5436 pnew[len] = ' ';
5437 pnew[len + 1] = NUL;
5438 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5439 /* remove the space later */
5440 did_add_space = TRUE;
5441 }
5442 else
5443 /* may remove added space */
5444 check_auto_format(FALSE);
5445 }
5446
5447 check_cursor();
5448}
5449
5450/*
5451 * When an extra space was added to continue a paragraph for auto-formatting,
5452 * delete it now. The space must be under the cursor, just after the insert
5453 * position.
5454 */
5455 static void
5456check_auto_format(end_insert)
5457 int end_insert; /* TRUE when ending Insert mode */
5458{
5459 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005460 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462 if (did_add_space)
5463 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005464 cc = gchar_cursor();
5465 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 /* Somehow the space was removed already. */
5467 did_add_space = FALSE;
5468 else
5469 {
5470 if (!end_insert)
5471 {
5472 inc_cursor();
5473 c = gchar_cursor();
5474 dec_cursor();
5475 }
5476 if (c != NUL)
5477 {
5478 /* The space is no longer at the end of the line, delete it. */
5479 del_char(FALSE);
5480 did_add_space = FALSE;
5481 }
5482 }
5483 }
5484}
5485
5486/*
5487 * Find out textwidth to be used for formatting:
5488 * if 'textwidth' option is set, use it
5489 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5490 * if invalid value, use 0.
5491 * Set default to window width (maximum 79) for "gq" operator.
5492 */
5493 int
5494comp_textwidth(ff)
5495 int ff; /* force formatting (for "Q" command) */
5496{
5497 int textwidth;
5498
5499 textwidth = curbuf->b_p_tw;
5500 if (textwidth == 0 && curbuf->b_p_wm)
5501 {
5502 /* The width is the window width minus 'wrapmargin' minus all the
5503 * things that add to the margin. */
5504 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5505#ifdef FEAT_CMDWIN
5506 if (cmdwin_type != 0)
5507 textwidth -= 1;
5508#endif
5509#ifdef FEAT_FOLDING
5510 textwidth -= curwin->w_p_fdc;
5511#endif
5512#ifdef FEAT_SIGNS
5513 if (curwin->w_buffer->b_signlist != NULL
5514# ifdef FEAT_NETBEANS_INTG
5515 || usingNetbeans
5516# endif
5517 )
5518 textwidth -= 1;
5519#endif
5520 if (curwin->w_p_nu)
5521 textwidth -= 8;
5522 }
5523 if (textwidth < 0)
5524 textwidth = 0;
5525 if (ff && textwidth == 0)
5526 {
5527 textwidth = W_WIDTH(curwin) - 1;
5528 if (textwidth > 79)
5529 textwidth = 79;
5530 }
5531 return textwidth;
5532}
5533
5534/*
5535 * Put a character in the redo buffer, for when just after a CTRL-V.
5536 */
5537 static void
5538redo_literal(c)
5539 int c;
5540{
5541 char_u buf[10];
5542
5543 /* Only digits need special treatment. Translate them into a string of
5544 * three digits. */
5545 if (VIM_ISDIGIT(c))
5546 {
5547 sprintf((char *)buf, "%03d", c);
5548 AppendToRedobuff(buf);
5549 }
5550 else
5551 AppendCharToRedobuff(c);
5552}
5553
5554/*
5555 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005556 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 */
5558 static void
5559start_arrow(end_insert_pos)
5560 pos_T *end_insert_pos;
5561{
5562 if (!arrow_used) /* something has been inserted */
5563 {
5564 AppendToRedobuff(ESC_STR);
5565 stop_insert(end_insert_pos, FALSE);
5566 arrow_used = TRUE; /* this means we stopped the current insert */
5567 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005568#ifdef FEAT_SYN_HL
5569 check_spell_redraw();
5570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571}
5572
Bram Moolenaar217ad922005-03-20 22:37:15 +00005573#ifdef FEAT_SYN_HL
5574/*
5575 * If we skipped highlighting word at cursor, do it now.
5576 * It may be skipped again, thus reset spell_redraw_lnum first.
5577 */
5578 static void
5579check_spell_redraw()
5580{
5581 if (spell_redraw_lnum != 0)
5582 {
5583 linenr_T lnum = spell_redraw_lnum;
5584
5585 spell_redraw_lnum = 0;
5586 redrawWinline(lnum, FALSE);
5587 }
5588}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005589
5590/*
5591 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5592 * spelled word, if there is one.
5593 */
5594 static void
5595spell_back_to_badword()
5596{
5597 pos_T tpos = curwin->w_cursor;
5598
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005599 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005600 if (curwin->w_cursor.col != tpos.col)
5601 start_arrow(&tpos);
5602}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005603#endif
5604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605/*
5606 * stop_arrow() is called before a change is made in insert mode.
5607 * If an arrow key has been used, start a new insertion.
5608 * Returns FAIL if undo is impossible, shouldn't insert then.
5609 */
5610 int
5611stop_arrow()
5612{
5613 if (arrow_used)
5614 {
5615 if (u_save_cursor() == OK)
5616 {
5617 arrow_used = FALSE;
5618 ins_need_undo = FALSE;
5619 }
5620 Insstart = curwin->w_cursor; /* new insertion starts here */
5621 Insstart_textlen = linetabsize(ml_get_curline());
5622 ai_col = 0;
5623#ifdef FEAT_VREPLACE
5624 if (State & VREPLACE_FLAG)
5625 {
5626 orig_line_count = curbuf->b_ml.ml_line_count;
5627 vr_lines_changed = 1;
5628 }
5629#endif
5630 ResetRedobuff();
5631 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005632 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
5634 else if (ins_need_undo)
5635 {
5636 if (u_save_cursor() == OK)
5637 ins_need_undo = FALSE;
5638 }
5639
5640#ifdef FEAT_FOLDING
5641 /* Always open fold at the cursor line when inserting something. */
5642 foldOpenCursor();
5643#endif
5644
5645 return (arrow_used || ins_need_undo ? FAIL : OK);
5646}
5647
5648/*
5649 * do a few things to stop inserting
5650 */
5651 static void
5652stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005653 pos_T *end_insert_pos; /* where insert ended */
5654 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005656 int cc;
5657 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659 stop_redo_ins();
5660 replace_flush(); /* abandon replace stack */
5661
5662 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005663 * Save the inserted text for later redo with ^@ and CTRL-A.
5664 * Don't do it when "restart_edit" was set and nothing was inserted,
5665 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005667 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005668 if (did_restart_edit == 0 || (ptr != NULL
5669 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005670 {
5671 vim_free(last_insert);
5672 last_insert = ptr;
5673 last_insert_skip = new_insert_skip;
5674 }
5675 else
5676 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 if (!arrow_used)
5679 {
5680 /* Auto-format now. It may seem strange to do this when stopping an
5681 * insertion (or moving the cursor), but it's required when appending
5682 * a line and having it end in a space. But only do it when something
5683 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005684 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005686 pos_T tpos = curwin->w_cursor;
5687
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 /* When the cursor is at the end of the line after a space the
5689 * formatting will move it to the following word. Avoid that by
5690 * moving the cursor onto the space. */
5691 cc = 'x';
5692 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5693 {
5694 dec_cursor();
5695 cc = gchar_cursor();
5696 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005697 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699
5700 auto_format(TRUE, FALSE);
5701
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005702 if (vim_iswhite(cc))
5703 {
5704 if (gchar_cursor() != NUL)
5705 inc_cursor();
5706#ifdef FEAT_VIRTUALEDIT
5707 /* If the cursor is still at the same character, also keep
5708 * the "coladd". */
5709 if (gchar_cursor() == NUL
5710 && curwin->w_cursor.lnum == tpos.lnum
5711 && curwin->w_cursor.col == tpos.col)
5712 curwin->w_cursor.coladd = tpos.coladd;
5713#endif
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716
5717 /* If a space was inserted for auto-formatting, remove it now. */
5718 check_auto_format(TRUE);
5719
5720 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005721 * of the line, and put the cursor back.
5722 * Do this when ESC was used or moving the cursor up/down. */
5723 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5724 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005726 pos_T tpos = curwin->w_cursor;
5727
5728 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5730 --curwin->w_cursor.col;
5731 while (cc = gchar_cursor(), vim_iswhite(cc))
5732 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005733 if (curwin->w_cursor.lnum != tpos.lnum)
5734 curwin->w_cursor = tpos;
5735 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5737
5738#ifdef FEAT_VISUAL
5739 /* <C-S-Right> may have started Visual mode, adjust the position for
5740 * deleted characters. */
5741 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5742 {
5743 cc = STRLEN(ml_get_curline());
5744 if (VIsual.col > (colnr_T)cc)
5745 {
5746 VIsual.col = cc;
5747# ifdef FEAT_VIRTUALEDIT
5748 VIsual.coladd = 0;
5749# endif
5750 }
5751 }
5752#endif
5753 }
5754 }
5755 did_ai = FALSE;
5756#ifdef FEAT_SMARTINDENT
5757 did_si = FALSE;
5758 can_si = FALSE;
5759 can_si_back = FALSE;
5760#endif
5761
5762 /* set '[ and '] to the inserted text */
5763 curbuf->b_op_start = Insstart;
5764 curbuf->b_op_end = *end_insert_pos;
5765}
5766
5767/*
5768 * Set the last inserted text to a single character.
5769 * Used for the replace command.
5770 */
5771 void
5772set_last_insert(c)
5773 int c;
5774{
5775 char_u *s;
5776
5777 vim_free(last_insert);
5778#ifdef FEAT_MBYTE
5779 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5780#else
5781 last_insert = alloc(6);
5782#endif
5783 if (last_insert != NULL)
5784 {
5785 s = last_insert;
5786 /* Use the CTRL-V only when entering a special char */
5787 if (c < ' ' || c == DEL)
5788 *s++ = Ctrl_V;
5789 s = add_char2buf(c, s);
5790 *s++ = ESC;
5791 *s++ = NUL;
5792 last_insert_skip = 0;
5793 }
5794}
5795
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005796#if defined(EXITFREE) || defined(PROTO)
5797 void
5798free_last_insert()
5799{
5800 vim_free(last_insert);
5801 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005802 vim_free(compl_orig_text);
5803 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005804}
5805#endif
5806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807/*
5808 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5809 * and CSI. Handle multi-byte characters.
5810 * Returns a pointer to after the added bytes.
5811 */
5812 char_u *
5813add_char2buf(c, s)
5814 int c;
5815 char_u *s;
5816{
5817#ifdef FEAT_MBYTE
5818 char_u temp[MB_MAXBYTES];
5819 int i;
5820 int len;
5821
5822 len = (*mb_char2bytes)(c, temp);
5823 for (i = 0; i < len; ++i)
5824 {
5825 c = temp[i];
5826#endif
5827 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5828 if (c == K_SPECIAL)
5829 {
5830 *s++ = K_SPECIAL;
5831 *s++ = KS_SPECIAL;
5832 *s++ = KE_FILLER;
5833 }
5834#ifdef FEAT_GUI
5835 else if (c == CSI)
5836 {
5837 *s++ = CSI;
5838 *s++ = KS_EXTRA;
5839 *s++ = (int)KE_CSI;
5840 }
5841#endif
5842 else
5843 *s++ = c;
5844#ifdef FEAT_MBYTE
5845 }
5846#endif
5847 return s;
5848}
5849
5850/*
5851 * move cursor to start of line
5852 * if flags & BL_WHITE move to first non-white
5853 * if flags & BL_SOL move to first non-white if startofline is set,
5854 * otherwise keep "curswant" column
5855 * if flags & BL_FIX don't leave the cursor on a NUL.
5856 */
5857 void
5858beginline(flags)
5859 int flags;
5860{
5861 if ((flags & BL_SOL) && !p_sol)
5862 coladvance(curwin->w_curswant);
5863 else
5864 {
5865 curwin->w_cursor.col = 0;
5866#ifdef FEAT_VIRTUALEDIT
5867 curwin->w_cursor.coladd = 0;
5868#endif
5869
5870 if (flags & (BL_WHITE | BL_SOL))
5871 {
5872 char_u *ptr;
5873
5874 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5875 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5876 ++curwin->w_cursor.col;
5877 }
5878 curwin->w_set_curswant = TRUE;
5879 }
5880}
5881
5882/*
5883 * oneright oneleft cursor_down cursor_up
5884 *
5885 * Move one char {right,left,down,up}.
5886 * Doesn't move onto the NUL past the end of the line.
5887 * Return OK when successful, FAIL when we hit a line of file boundary.
5888 */
5889
5890 int
5891oneright()
5892{
5893 char_u *ptr;
5894#ifdef FEAT_MBYTE
5895 int l;
5896#endif
5897
5898#ifdef FEAT_VIRTUALEDIT
5899 if (virtual_active())
5900 {
5901 pos_T prevpos = curwin->w_cursor;
5902
5903 /* Adjust for multi-wide char (excluding TAB) */
5904 ptr = ml_get_cursor();
5905 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5906#ifdef FEAT_MBYTE
5907 (*mb_ptr2char)(ptr)
5908#else
5909 *ptr
5910#endif
5911 ))
5912 ? ptr2cells(ptr) : 1));
5913 curwin->w_set_curswant = TRUE;
5914 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5915 return (prevpos.col != curwin->w_cursor.col
5916 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5917 }
5918#endif
5919
5920 ptr = ml_get_cursor();
5921#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005922 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 {
5924 /* The character under the cursor is a multi-byte character, move
5925 * several bytes right, but don't end up on the NUL. */
5926 if (ptr[l] == NUL)
5927 return FAIL;
5928 curwin->w_cursor.col += l;
5929 }
5930 else
5931#endif
5932 {
5933 if (*ptr++ == NUL || *ptr == NUL)
5934 return FAIL;
5935 ++curwin->w_cursor.col;
5936 }
5937
5938 curwin->w_set_curswant = TRUE;
5939 return OK;
5940}
5941
5942 int
5943oneleft()
5944{
5945#ifdef FEAT_VIRTUALEDIT
5946 if (virtual_active())
5947 {
5948 int width;
5949 int v = getviscol();
5950
5951 if (v == 0)
5952 return FAIL;
5953
5954# ifdef FEAT_LINEBREAK
5955 /* We might get stuck on 'showbreak', skip over it. */
5956 width = 1;
5957 for (;;)
5958 {
5959 coladvance(v - width);
5960 /* getviscol() is slow, skip it when 'showbreak' is empty and
5961 * there are no multi-byte characters */
5962 if ((*p_sbr == NUL
5963# ifdef FEAT_MBYTE
5964 && !has_mbyte
5965# endif
5966 ) || getviscol() < v)
5967 break;
5968 ++width;
5969 }
5970# else
5971 coladvance(v - 1);
5972# endif
5973
5974 if (curwin->w_cursor.coladd == 1)
5975 {
5976 char_u *ptr;
5977
5978 /* Adjust for multi-wide char (not a TAB) */
5979 ptr = ml_get_cursor();
5980 if (*ptr != TAB && vim_isprintc(
5981# ifdef FEAT_MBYTE
5982 (*mb_ptr2char)(ptr)
5983# else
5984 *ptr
5985# endif
5986 ) && ptr2cells(ptr) > 1)
5987 curwin->w_cursor.coladd = 0;
5988 }
5989
5990 curwin->w_set_curswant = TRUE;
5991 return OK;
5992 }
5993#endif
5994
5995 if (curwin->w_cursor.col == 0)
5996 return FAIL;
5997
5998 curwin->w_set_curswant = TRUE;
5999 --curwin->w_cursor.col;
6000
6001#ifdef FEAT_MBYTE
6002 /* if the character on the left of the current cursor is a multi-byte
6003 * character, move to its first byte */
6004 if (has_mbyte)
6005 mb_adjust_cursor();
6006#endif
6007 return OK;
6008}
6009
6010 int
6011cursor_up(n, upd_topline)
6012 long n;
6013 int upd_topline; /* When TRUE: update topline */
6014{
6015 linenr_T lnum;
6016
6017 if (n > 0)
6018 {
6019 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006020 /* This fails if the cursor is already in the first line or the count
6021 * is larger than the line number and '-' is in 'cpoptions' */
6022 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 return FAIL;
6024 if (n >= lnum)
6025 lnum = 1;
6026 else
6027#ifdef FEAT_FOLDING
6028 if (hasAnyFolding(curwin))
6029 {
6030 /*
6031 * Count each sequence of folded lines as one logical line.
6032 */
6033 /* go to the the start of the current fold */
6034 (void)hasFolding(lnum, &lnum, NULL);
6035
6036 while (n--)
6037 {
6038 /* move up one line */
6039 --lnum;
6040 if (lnum <= 1)
6041 break;
6042 /* If we entered a fold, move to the beginning, unless in
6043 * Insert mode or when 'foldopen' contains "all": it will open
6044 * in a moment. */
6045 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6046 (void)hasFolding(lnum, &lnum, NULL);
6047 }
6048 if (lnum < 1)
6049 lnum = 1;
6050 }
6051 else
6052#endif
6053 lnum -= n;
6054 curwin->w_cursor.lnum = lnum;
6055 }
6056
6057 /* try to advance to the column we want to be at */
6058 coladvance(curwin->w_curswant);
6059
6060 if (upd_topline)
6061 update_topline(); /* make sure curwin->w_topline is valid */
6062
6063 return OK;
6064}
6065
6066/*
6067 * Cursor down a number of logical lines.
6068 */
6069 int
6070cursor_down(n, upd_topline)
6071 long n;
6072 int upd_topline; /* When TRUE: update topline */
6073{
6074 linenr_T lnum;
6075
6076 if (n > 0)
6077 {
6078 lnum = curwin->w_cursor.lnum;
6079#ifdef FEAT_FOLDING
6080 /* Move to last line of fold, will fail if it's the end-of-file. */
6081 (void)hasFolding(lnum, NULL, &lnum);
6082#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006083 /* This fails if the cursor is already in the last line or would move
6084 * beyound the last line and '-' is in 'cpoptions' */
6085 if (lnum >= curbuf->b_ml.ml_line_count
6086 || (lnum + n > curbuf->b_ml.ml_line_count
6087 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 return FAIL;
6089 if (lnum + n >= curbuf->b_ml.ml_line_count)
6090 lnum = curbuf->b_ml.ml_line_count;
6091 else
6092#ifdef FEAT_FOLDING
6093 if (hasAnyFolding(curwin))
6094 {
6095 linenr_T last;
6096
6097 /* count each sequence of folded lines as one logical line */
6098 while (n--)
6099 {
6100 if (hasFolding(lnum, NULL, &last))
6101 lnum = last + 1;
6102 else
6103 ++lnum;
6104 if (lnum >= curbuf->b_ml.ml_line_count)
6105 break;
6106 }
6107 if (lnum > curbuf->b_ml.ml_line_count)
6108 lnum = curbuf->b_ml.ml_line_count;
6109 }
6110 else
6111#endif
6112 lnum += n;
6113 curwin->w_cursor.lnum = lnum;
6114 }
6115
6116 /* try to advance to the column we want to be at */
6117 coladvance(curwin->w_curswant);
6118
6119 if (upd_topline)
6120 update_topline(); /* make sure curwin->w_topline is valid */
6121
6122 return OK;
6123}
6124
6125/*
6126 * Stuff the last inserted text in the read buffer.
6127 * Last_insert actually is a copy of the redo buffer, so we
6128 * first have to remove the command.
6129 */
6130 int
6131stuff_inserted(c, count, no_esc)
6132 int c; /* Command character to be inserted */
6133 long count; /* Repeat this many times */
6134 int no_esc; /* Don't add an ESC at the end */
6135{
6136 char_u *esc_ptr;
6137 char_u *ptr;
6138 char_u *last_ptr;
6139 char_u last = NUL;
6140
6141 ptr = get_last_insert();
6142 if (ptr == NULL)
6143 {
6144 EMSG(_(e_noinstext));
6145 return FAIL;
6146 }
6147
6148 /* may want to stuff the command character, to start Insert mode */
6149 if (c != NUL)
6150 stuffcharReadbuff(c);
6151 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6152 *esc_ptr = NUL; /* remove the ESC */
6153
6154 /* when the last char is either "0" or "^" it will be quoted if no ESC
6155 * comes after it OR if it will inserted more than once and "ptr"
6156 * starts with ^D. -- Acevedo
6157 */
6158 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6159 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6160 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6161 {
6162 last = *last_ptr;
6163 *last_ptr = NUL;
6164 }
6165
6166 do
6167 {
6168 stuffReadbuff(ptr);
6169 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6170 if (last)
6171 stuffReadbuff((char_u *)(last == '0'
6172 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6173 : IF_EB("\026^", CTRL_V_STR "^")));
6174 }
6175 while (--count > 0);
6176
6177 if (last)
6178 *last_ptr = last;
6179
6180 if (esc_ptr != NULL)
6181 *esc_ptr = ESC; /* put the ESC back */
6182
6183 /* may want to stuff a trailing ESC, to get out of Insert mode */
6184 if (!no_esc)
6185 stuffcharReadbuff(ESC);
6186
6187 return OK;
6188}
6189
6190 char_u *
6191get_last_insert()
6192{
6193 if (last_insert == NULL)
6194 return NULL;
6195 return last_insert + last_insert_skip;
6196}
6197
6198/*
6199 * Get last inserted string, and remove trailing <Esc>.
6200 * Returns pointer to allocated memory (must be freed) or NULL.
6201 */
6202 char_u *
6203get_last_insert_save()
6204{
6205 char_u *s;
6206 int len;
6207
6208 if (last_insert == NULL)
6209 return NULL;
6210 s = vim_strsave(last_insert + last_insert_skip);
6211 if (s != NULL)
6212 {
6213 len = (int)STRLEN(s);
6214 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6215 s[len - 1] = NUL;
6216 }
6217 return s;
6218}
6219
6220/*
6221 * Check the word in front of the cursor for an abbreviation.
6222 * Called when the non-id character "c" has been entered.
6223 * When an abbreviation is recognized it is removed from the text and
6224 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6225 */
6226 static int
6227echeck_abbr(c)
6228 int c;
6229{
6230 /* Don't check for abbreviation in paste mode, when disabled and just
6231 * after moving around with cursor keys. */
6232 if (p_paste || no_abbr || arrow_used)
6233 return FALSE;
6234
6235 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6236 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6237}
6238
6239/*
6240 * replace-stack functions
6241 *
6242 * When replacing characters, the replaced characters are remembered for each
6243 * new character. This is used to re-insert the old text when backspacing.
6244 *
6245 * There is a NUL headed list of characters for each character that is
6246 * currently in the file after the insertion point. When BS is used, one NUL
6247 * headed list is put back for the deleted character.
6248 *
6249 * For a newline, there are two NUL headed lists. One contains the characters
6250 * that the NL replaced. The extra one stores the characters after the cursor
6251 * that were deleted (always white space).
6252 *
6253 * Replace_offset is normally 0, in which case replace_push will add a new
6254 * character at the end of the stack. If replace_offset is not 0, that many
6255 * characters will be left on the stack above the newly inserted character.
6256 */
6257
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006258static char_u *replace_stack = NULL;
6259static long replace_stack_nr = 0; /* next entry in replace stack */
6260static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
6262 void
6263replace_push(c)
6264 int c; /* character that is replaced (NUL is none) */
6265{
6266 char_u *p;
6267
6268 if (replace_stack_nr < replace_offset) /* nothing to do */
6269 return;
6270 if (replace_stack_len <= replace_stack_nr)
6271 {
6272 replace_stack_len += 50;
6273 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6274 if (p == NULL) /* out of memory */
6275 {
6276 replace_stack_len -= 50;
6277 return;
6278 }
6279 if (replace_stack != NULL)
6280 {
6281 mch_memmove(p, replace_stack,
6282 (size_t)(replace_stack_nr * sizeof(char_u)));
6283 vim_free(replace_stack);
6284 }
6285 replace_stack = p;
6286 }
6287 p = replace_stack + replace_stack_nr - replace_offset;
6288 if (replace_offset)
6289 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6290 *p = c;
6291 ++replace_stack_nr;
6292}
6293
6294/*
6295 * call replace_push(c) with replace_offset set to the first NUL.
6296 */
6297 static void
6298replace_push_off(c)
6299 int c;
6300{
6301 char_u *p;
6302
6303 p = replace_stack + replace_stack_nr;
6304 for (replace_offset = 1; replace_offset < replace_stack_nr;
6305 ++replace_offset)
6306 if (*--p == NUL)
6307 break;
6308 replace_push(c);
6309 replace_offset = 0;
6310}
6311
6312/*
6313 * Pop one item from the replace stack.
6314 * return -1 if stack empty
6315 * return replaced character or NUL otherwise
6316 */
6317 static int
6318replace_pop()
6319{
6320 if (replace_stack_nr == 0)
6321 return -1;
6322 return (int)replace_stack[--replace_stack_nr];
6323}
6324
6325/*
6326 * Join the top two items on the replace stack. This removes to "off"'th NUL
6327 * encountered.
6328 */
6329 static void
6330replace_join(off)
6331 int off; /* offset for which NUL to remove */
6332{
6333 int i;
6334
6335 for (i = replace_stack_nr; --i >= 0; )
6336 if (replace_stack[i] == NUL && off-- <= 0)
6337 {
6338 --replace_stack_nr;
6339 mch_memmove(replace_stack + i, replace_stack + i + 1,
6340 (size_t)(replace_stack_nr - i));
6341 return;
6342 }
6343}
6344
6345/*
6346 * Pop bytes from the replace stack until a NUL is found, and insert them
6347 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6348 */
6349 static void
6350replace_pop_ins()
6351{
6352 int cc;
6353 int oldState = State;
6354
6355 State = NORMAL; /* don't want REPLACE here */
6356 while ((cc = replace_pop()) > 0)
6357 {
6358#ifdef FEAT_MBYTE
6359 mb_replace_pop_ins(cc);
6360#else
6361 ins_char(cc);
6362#endif
6363 dec_cursor();
6364 }
6365 State = oldState;
6366}
6367
6368#ifdef FEAT_MBYTE
6369/*
6370 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6371 * indicates a multi-byte char, pop the other bytes too.
6372 */
6373 static void
6374mb_replace_pop_ins(cc)
6375 int cc;
6376{
6377 int n;
6378 char_u buf[MB_MAXBYTES];
6379 int i;
6380 int c;
6381
6382 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6383 {
6384 buf[0] = cc;
6385 for (i = 1; i < n; ++i)
6386 buf[i] = replace_pop();
6387 ins_bytes_len(buf, n);
6388 }
6389 else
6390 ins_char(cc);
6391
6392 if (enc_utf8)
6393 /* Handle composing chars. */
6394 for (;;)
6395 {
6396 c = replace_pop();
6397 if (c == -1) /* stack empty */
6398 break;
6399 if ((n = MB_BYTE2LEN(c)) == 1)
6400 {
6401 /* Not a multi-byte char, put it back. */
6402 replace_push(c);
6403 break;
6404 }
6405 else
6406 {
6407 buf[0] = c;
6408 for (i = 1; i < n; ++i)
6409 buf[i] = replace_pop();
6410 if (utf_iscomposing(utf_ptr2char(buf)))
6411 ins_bytes_len(buf, n);
6412 else
6413 {
6414 /* Not a composing char, put it back. */
6415 for (i = n - 1; i >= 0; --i)
6416 replace_push(buf[i]);
6417 break;
6418 }
6419 }
6420 }
6421}
6422#endif
6423
6424/*
6425 * make the replace stack empty
6426 * (called when exiting replace mode)
6427 */
6428 static void
6429replace_flush()
6430{
6431 vim_free(replace_stack);
6432 replace_stack = NULL;
6433 replace_stack_len = 0;
6434 replace_stack_nr = 0;
6435}
6436
6437/*
6438 * Handle doing a BS for one character.
6439 * cc < 0: replace stack empty, just move cursor
6440 * cc == 0: character was inserted, delete it
6441 * cc > 0: character was replaced, put cc (first byte of original char) back
6442 * and check for more characters to be put back
6443 */
6444 static void
6445replace_do_bs()
6446{
6447 int cc;
6448#ifdef FEAT_VREPLACE
6449 int orig_len = 0;
6450 int ins_len;
6451 int orig_vcols = 0;
6452 colnr_T start_vcol;
6453 char_u *p;
6454 int i;
6455 int vcol;
6456#endif
6457
6458 cc = replace_pop();
6459 if (cc > 0)
6460 {
6461#ifdef FEAT_VREPLACE
6462 if (State & VREPLACE_FLAG)
6463 {
6464 /* Get the number of screen cells used by the character we are
6465 * going to delete. */
6466 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6467 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6468 }
6469#endif
6470#ifdef FEAT_MBYTE
6471 if (has_mbyte)
6472 {
6473 del_char(FALSE);
6474# ifdef FEAT_VREPLACE
6475 if (State & VREPLACE_FLAG)
6476 orig_len = STRLEN(ml_get_cursor());
6477# endif
6478 replace_push(cc);
6479 }
6480 else
6481#endif
6482 {
6483 pchar_cursor(cc);
6484#ifdef FEAT_VREPLACE
6485 if (State & VREPLACE_FLAG)
6486 orig_len = STRLEN(ml_get_cursor()) - 1;
6487#endif
6488 }
6489 replace_pop_ins();
6490
6491#ifdef FEAT_VREPLACE
6492 if (State & VREPLACE_FLAG)
6493 {
6494 /* Get the number of screen cells used by the inserted characters */
6495 p = ml_get_cursor();
6496 ins_len = STRLEN(p) - orig_len;
6497 vcol = start_vcol;
6498 for (i = 0; i < ins_len; ++i)
6499 {
6500 vcol += chartabsize(p + i, vcol);
6501#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006502 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503#endif
6504 }
6505 vcol -= start_vcol;
6506
6507 /* Delete spaces that were inserted after the cursor to keep the
6508 * text aligned. */
6509 curwin->w_cursor.col += ins_len;
6510 while (vcol > orig_vcols && gchar_cursor() == ' ')
6511 {
6512 del_char(FALSE);
6513 ++orig_vcols;
6514 }
6515 curwin->w_cursor.col -= ins_len;
6516 }
6517#endif
6518
6519 /* mark the buffer as changed and prepare for displaying */
6520 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6521 }
6522 else if (cc == 0)
6523 (void)del_char(FALSE);
6524}
6525
6526#ifdef FEAT_CINDENT
6527/*
6528 * Return TRUE if C-indenting is on.
6529 */
6530 static int
6531cindent_on()
6532{
6533 return (!p_paste && (curbuf->b_p_cin
6534# ifdef FEAT_EVAL
6535 || *curbuf->b_p_inde != NUL
6536# endif
6537 ));
6538}
6539#endif
6540
6541#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6542/*
6543 * Re-indent the current line, based on the current contents of it and the
6544 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6545 * confused what all the part that handles Control-T is doing that I'm not.
6546 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6547 */
6548
6549 void
6550fixthisline(get_the_indent)
6551 int (*get_the_indent) __ARGS((void));
6552{
6553 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6554 if (linewhite(curwin->w_cursor.lnum))
6555 did_ai = TRUE; /* delete the indent if the line stays empty */
6556}
6557
6558 void
6559fix_indent()
6560{
6561 if (p_paste)
6562 return;
6563# ifdef FEAT_LISP
6564 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6565 fixthisline(get_lisp_indent);
6566# endif
6567# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6568 else
6569# endif
6570# ifdef FEAT_CINDENT
6571 if (cindent_on())
6572 do_c_expr_indent();
6573# endif
6574}
6575
6576#endif
6577
6578#ifdef FEAT_CINDENT
6579/*
6580 * return TRUE if 'cinkeys' contains the key "keytyped",
6581 * when == '*': Only if key is preceded with '*' (indent before insert)
6582 * when == '!': Only if key is prededed with '!' (don't insert)
6583 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6584 *
6585 * "keytyped" can have a few special values:
6586 * KEY_OPEN_FORW
6587 * KEY_OPEN_BACK
6588 * KEY_COMPLETE just finished completion.
6589 *
6590 * If line_is_empty is TRUE accept keys with '0' before them.
6591 */
6592 int
6593in_cinkeys(keytyped, when, line_is_empty)
6594 int keytyped;
6595 int when;
6596 int line_is_empty;
6597{
6598 char_u *look;
6599 int try_match;
6600 int try_match_word;
6601 char_u *p;
6602 char_u *line;
6603 int icase;
6604 int i;
6605
6606#ifdef FEAT_EVAL
6607 if (*curbuf->b_p_inde != NUL)
6608 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6609 else
6610#endif
6611 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6612 while (*look)
6613 {
6614 /*
6615 * Find out if we want to try a match with this key, depending on
6616 * 'when' and a '*' or '!' before the key.
6617 */
6618 switch (when)
6619 {
6620 case '*': try_match = (*look == '*'); break;
6621 case '!': try_match = (*look == '!'); break;
6622 default: try_match = (*look != '*'); break;
6623 }
6624 if (*look == '*' || *look == '!')
6625 ++look;
6626
6627 /*
6628 * If there is a '0', only accept a match if the line is empty.
6629 * But may still match when typing last char of a word.
6630 */
6631 if (*look == '0')
6632 {
6633 try_match_word = try_match;
6634 if (!line_is_empty)
6635 try_match = FALSE;
6636 ++look;
6637 }
6638 else
6639 try_match_word = FALSE;
6640
6641 /*
6642 * does it look like a control character?
6643 */
6644 if (*look == '^'
6645#ifdef EBCDIC
6646 && (Ctrl_chr(look[1]) != 0)
6647#else
6648 && look[1] >= '?' && look[1] <= '_'
6649#endif
6650 )
6651 {
6652 if (try_match && keytyped == Ctrl_chr(look[1]))
6653 return TRUE;
6654 look += 2;
6655 }
6656 /*
6657 * 'o' means "o" command, open forward.
6658 * 'O' means "O" command, open backward.
6659 */
6660 else if (*look == 'o')
6661 {
6662 if (try_match && keytyped == KEY_OPEN_FORW)
6663 return TRUE;
6664 ++look;
6665 }
6666 else if (*look == 'O')
6667 {
6668 if (try_match && keytyped == KEY_OPEN_BACK)
6669 return TRUE;
6670 ++look;
6671 }
6672
6673 /*
6674 * 'e' means to check for "else" at start of line and just before the
6675 * cursor.
6676 */
6677 else if (*look == 'e')
6678 {
6679 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6680 {
6681 p = ml_get_curline();
6682 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6683 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6684 return TRUE;
6685 }
6686 ++look;
6687 }
6688
6689 /*
6690 * ':' only causes an indent if it is at the end of a label or case
6691 * statement, or when it was before typing the ':' (to fix
6692 * class::method for C++).
6693 */
6694 else if (*look == ':')
6695 {
6696 if (try_match && keytyped == ':')
6697 {
6698 p = ml_get_curline();
6699 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6700 return TRUE;
6701 if (curwin->w_cursor.col > 2
6702 && p[curwin->w_cursor.col - 1] == ':'
6703 && p[curwin->w_cursor.col - 2] == ':')
6704 {
6705 p[curwin->w_cursor.col - 1] = ' ';
6706 i = (cin_iscase(p) || cin_isscopedecl(p)
6707 || cin_islabel(30));
6708 p = ml_get_curline();
6709 p[curwin->w_cursor.col - 1] = ':';
6710 if (i)
6711 return TRUE;
6712 }
6713 }
6714 ++look;
6715 }
6716
6717
6718 /*
6719 * Is it a key in <>, maybe?
6720 */
6721 else if (*look == '<')
6722 {
6723 if (try_match)
6724 {
6725 /*
6726 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6727 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6728 * >, *, : and ! keys if they really really want to.
6729 */
6730 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6731 && keytyped == look[1])
6732 return TRUE;
6733
6734 if (keytyped == get_special_key_code(look + 1))
6735 return TRUE;
6736 }
6737 while (*look && *look != '>')
6738 look++;
6739 while (*look == '>')
6740 look++;
6741 }
6742
6743 /*
6744 * Is it a word: "=word"?
6745 */
6746 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6747 {
6748 ++look;
6749 if (*look == '~')
6750 {
6751 icase = TRUE;
6752 ++look;
6753 }
6754 else
6755 icase = FALSE;
6756 p = vim_strchr(look, ',');
6757 if (p == NULL)
6758 p = look + STRLEN(look);
6759 if ((try_match || try_match_word)
6760 && curwin->w_cursor.col >= (colnr_T)(p - look))
6761 {
6762 int match = FALSE;
6763
6764#ifdef FEAT_INS_EXPAND
6765 if (keytyped == KEY_COMPLETE)
6766 {
6767 char_u *s;
6768
6769 /* Just completed a word, check if it starts with "look".
6770 * search back for the start of a word. */
6771 line = ml_get_curline();
6772# ifdef FEAT_MBYTE
6773 if (has_mbyte)
6774 {
6775 char_u *n;
6776
6777 for (s = line + curwin->w_cursor.col; s > line; s = n)
6778 {
6779 n = mb_prevptr(line, s);
6780 if (!vim_iswordp(n))
6781 break;
6782 }
6783 }
6784 else
6785# endif
6786 for (s = line + curwin->w_cursor.col; s > line; --s)
6787 if (!vim_iswordc(s[-1]))
6788 break;
6789 if (s + (p - look) <= line + curwin->w_cursor.col
6790 && (icase
6791 ? MB_STRNICMP(s, look, p - look)
6792 : STRNCMP(s, look, p - look)) == 0)
6793 match = TRUE;
6794 }
6795 else
6796#endif
6797 /* TODO: multi-byte */
6798 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6799 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6800 {
6801 line = ml_get_cursor();
6802 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6803 || !vim_iswordc(line[-(p - look) - 1]))
6804 && (icase
6805 ? MB_STRNICMP(line - (p - look), look, p - look)
6806 : STRNCMP(line - (p - look), look, p - look))
6807 == 0)
6808 match = TRUE;
6809 }
6810 if (match && try_match_word && !try_match)
6811 {
6812 /* "0=word": Check if there are only blanks before the
6813 * word. */
6814 line = ml_get_curline();
6815 if ((int)(skipwhite(line) - line) !=
6816 (int)(curwin->w_cursor.col - (p - look)))
6817 match = FALSE;
6818 }
6819 if (match)
6820 return TRUE;
6821 }
6822 look = p;
6823 }
6824
6825 /*
6826 * ok, it's a boring generic character.
6827 */
6828 else
6829 {
6830 if (try_match && *look == keytyped)
6831 return TRUE;
6832 ++look;
6833 }
6834
6835 /*
6836 * Skip over ", ".
6837 */
6838 look = skip_to_option_part(look);
6839 }
6840 return FALSE;
6841}
6842#endif /* FEAT_CINDENT */
6843
6844#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6845/*
6846 * Map Hebrew keyboard when in hkmap mode.
6847 */
6848 int
6849hkmap(c)
6850 int c;
6851{
6852 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6853 {
6854 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6855 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6856 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6857 static char_u map[26] =
6858 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6859 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6860 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6861 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6862 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6863 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6864 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6865 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6866 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6867
6868 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6869 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6870 /* '-1'='sofit' */
6871 else if (c == 'x')
6872 return 'X';
6873 else if (c == 'q')
6874 return '\''; /* {geresh}={'} */
6875 else if (c == 246)
6876 return ' '; /* \"o --> ' ' for a german keyboard */
6877 else if (c == 228)
6878 return ' '; /* \"a --> ' ' -- / -- */
6879 else if (c == 252)
6880 return ' '; /* \"u --> ' ' -- / -- */
6881#ifdef EBCDIC
6882 else if (islower(c))
6883#else
6884 /* NOTE: islower() does not do the right thing for us on Linux so we
6885 * do this the same was as 5.7 and previous, so it works correctly on
6886 * all systems. Specifically, the e.g. Delete and Arrow keys are
6887 * munged and won't work if e.g. searching for Hebrew text.
6888 */
6889 else if (c >= 'a' && c <= 'z')
6890#endif
6891 return (int)(map[CharOrdLow(c)] + p_aleph);
6892 else
6893 return c;
6894 }
6895 else
6896 {
6897 switch (c)
6898 {
6899 case '`': return ';';
6900 case '/': return '.';
6901 case '\'': return ',';
6902 case 'q': return '/';
6903 case 'w': return '\'';
6904
6905 /* Hebrew letters - set offset from 'a' */
6906 case ',': c = '{'; break;
6907 case '.': c = 'v'; break;
6908 case ';': c = 't'; break;
6909 default: {
6910 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6911
6912#ifdef EBCDIC
6913 /* see note about islower() above */
6914 if (!islower(c))
6915#else
6916 if (c < 'a' || c > 'z')
6917#endif
6918 return c;
6919 c = str[CharOrdLow(c)];
6920 break;
6921 }
6922 }
6923
6924 return (int)(CharOrdLow(c) + p_aleph);
6925 }
6926}
6927#endif
6928
6929 static void
6930ins_reg()
6931{
6932 int need_redraw = FALSE;
6933 int regname;
6934 int literally = 0;
6935
6936 /*
6937 * If we are going to wait for a character, show a '"'.
6938 */
6939 pc_status = PC_STATUS_UNSET;
6940 if (redrawing() && !char_avail())
6941 {
6942 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006943 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
6945 edit_putchar('"', TRUE);
6946#ifdef FEAT_CMDL_INFO
6947 add_to_showcmd_c(Ctrl_R);
6948#endif
6949 }
6950
6951#ifdef USE_ON_FLY_SCROLL
6952 dont_scroll = TRUE; /* disallow scrolling here */
6953#endif
6954
6955 /*
6956 * Don't map the register name. This also prevents the mode message to be
6957 * deleted when ESC is hit.
6958 */
6959 ++no_mapping;
6960 regname = safe_vgetc();
6961#ifdef FEAT_LANGMAP
6962 LANGMAP_ADJUST(regname, TRUE);
6963#endif
6964 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6965 {
6966 /* Get a third key for literal register insertion */
6967 literally = regname;
6968#ifdef FEAT_CMDL_INFO
6969 add_to_showcmd_c(literally);
6970#endif
6971 regname = safe_vgetc();
6972#ifdef FEAT_LANGMAP
6973 LANGMAP_ADJUST(regname, TRUE);
6974#endif
6975 }
6976 --no_mapping;
6977
6978#ifdef FEAT_EVAL
6979 /*
6980 * Don't call u_sync() while getting the expression,
6981 * evaluating it or giving an error message for it!
6982 */
6983 ++no_u_sync;
6984 if (regname == '=')
6985 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006986# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006990# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 /* Restore the Input Method. */
6992 if (im_on)
6993 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006996 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6997 {
6998 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 else
7002 {
7003#endif
7004 if (literally == Ctrl_O || literally == Ctrl_P)
7005 {
7006 /* Append the command to the redo buffer. */
7007 AppendCharToRedobuff(Ctrl_R);
7008 AppendCharToRedobuff(literally);
7009 AppendCharToRedobuff(regname);
7010
7011 do_put(regname, BACKWARD, 1L,
7012 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7013 }
7014 else if (insert_reg(regname, literally) == FAIL)
7015 {
7016 vim_beep();
7017 need_redraw = TRUE; /* remove the '"' */
7018 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007019 else if (stop_insert_mode)
7020 /* When the '=' register was used and a function was invoked that
7021 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7022 * insert anything, need to remove the '"' */
7023 need_redraw = TRUE;
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025#ifdef FEAT_EVAL
7026 }
7027 --no_u_sync;
7028#endif
7029#ifdef FEAT_CMDL_INFO
7030 clear_showcmd();
7031#endif
7032
7033 /* If the inserted register is empty, we need to remove the '"' */
7034 if (need_redraw || stuff_empty())
7035 edit_unputchar();
7036}
7037
7038/*
7039 * CTRL-G commands in Insert mode.
7040 */
7041 static void
7042ins_ctrl_g()
7043{
7044 int c;
7045
7046#ifdef FEAT_INS_EXPAND
7047 /* Right after CTRL-X the cursor will be after the ruler. */
7048 setcursor();
7049#endif
7050
7051 /*
7052 * Don't map the second key. This also prevents the mode message to be
7053 * deleted when ESC is hit.
7054 */
7055 ++no_mapping;
7056 c = safe_vgetc();
7057 --no_mapping;
7058 switch (c)
7059 {
7060 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7061 case K_UP:
7062 case Ctrl_K:
7063 case 'k': ins_up(TRUE);
7064 break;
7065
7066 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7067 case K_DOWN:
7068 case Ctrl_J:
7069 case 'j': ins_down(TRUE);
7070 break;
7071
7072 /* CTRL-G u: start new undoable edit */
7073 case 'u': u_sync();
7074 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007075
7076 /* Need to reset Insstart, esp. because a BS that joins
7077 * aline to the previous one must save for undo. */
7078 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 break;
7080
7081 /* Unknown CTRL-G command, reserved for future expansion. */
7082 default: vim_beep();
7083 }
7084}
7085
7086/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007087 * CTRL-^ in Insert mode.
7088 */
7089 static void
7090ins_ctrl_hat()
7091{
7092 if (map_to_exists_mode((char_u *)"", LANGMAP))
7093 {
7094 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7095 if (State & LANGMAP)
7096 {
7097 curbuf->b_p_iminsert = B_IMODE_NONE;
7098 State &= ~LANGMAP;
7099 }
7100 else
7101 {
7102 curbuf->b_p_iminsert = B_IMODE_LMAP;
7103 State |= LANGMAP;
7104#ifdef USE_IM_CONTROL
7105 im_set_active(FALSE);
7106#endif
7107 }
7108 }
7109#ifdef USE_IM_CONTROL
7110 else
7111 {
7112 /* There are no ":lmap" mappings, toggle IM */
7113 if (im_get_status())
7114 {
7115 curbuf->b_p_iminsert = B_IMODE_NONE;
7116 im_set_active(FALSE);
7117 }
7118 else
7119 {
7120 curbuf->b_p_iminsert = B_IMODE_IM;
7121 State &= ~LANGMAP;
7122 im_set_active(TRUE);
7123 }
7124 }
7125#endif
7126 set_iminsert_global();
7127 showmode();
7128#ifdef FEAT_GUI
7129 /* may show different cursor shape or color */
7130 if (gui.in_use)
7131 gui_update_cursor(TRUE, FALSE);
7132#endif
7133#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7134 /* Show/unshow value of 'keymap' in status lines. */
7135 status_redraw_curbuf();
7136#endif
7137}
7138
7139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 * Handle ESC in insert mode.
7141 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7142 * insert.
7143 */
7144 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007145ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 long *count;
7147 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007148 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 int temp;
7151 static int disabled_redraw = FALSE;
7152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007153#ifdef FEAT_SYN_HL
7154 check_spell_redraw();
7155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156#if defined(FEAT_HANGULIN)
7157# if defined(ESC_CHG_TO_ENG_MODE)
7158 hangul_input_state_set(0);
7159# endif
7160 if (composing_hangul)
7161 {
7162 push_raw_key(composing_hangul_buffer, 2);
7163 composing_hangul = 0;
7164 }
7165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167 temp = curwin->w_cursor.col;
7168 if (disabled_redraw)
7169 {
7170 --RedrawingDisabled;
7171 disabled_redraw = FALSE;
7172 }
7173 if (!arrow_used)
7174 {
7175 /*
7176 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007177 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7178 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 */
7180 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007181 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
7183 /*
7184 * Repeating insert may take a long time. Check for
7185 * interrupt now and then.
7186 */
7187 if (*count > 0)
7188 {
7189 line_breakcheck();
7190 if (got_int)
7191 *count = 0;
7192 }
7193
7194 if (--*count > 0) /* repeat what was typed */
7195 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007196 /* Vi repeats the insert without replacing characters. */
7197 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7198 State &= ~REPLACE_FLAG;
7199
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 (void)start_redo_ins();
7201 if (cmdchar == 'r' || cmdchar == 'v')
7202 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7203 ++RedrawingDisabled;
7204 disabled_redraw = TRUE;
7205 return FALSE; /* repeat the insert */
7206 }
7207 stop_insert(&curwin->w_cursor, TRUE);
7208 undisplay_dollar();
7209 }
7210
7211 /* When an autoindent was removed, curswant stays after the
7212 * indent */
7213 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7214 curwin->w_set_curswant = TRUE;
7215
7216 /* Remember the last Insert position in the '^ mark. */
7217 if (!cmdmod.keepjumps)
7218 curbuf->b_last_insert = curwin->w_cursor;
7219
7220 /*
7221 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007222 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007224 if (!nomove
7225 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226#ifdef FEAT_VIRTUALEDIT
7227 || curwin->w_cursor.coladd > 0
7228#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007229 )
7230 && (restart_edit == NUL
7231 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007233 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007235 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236#ifdef FEAT_RIGHTLEFT
7237 && !revins_on
7238#endif
7239 )
7240 {
7241#ifdef FEAT_VIRTUALEDIT
7242 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7243 {
7244 oneleft();
7245 if (restart_edit != NUL)
7246 ++curwin->w_cursor.coladd;
7247 }
7248 else
7249#endif
7250 {
7251 --curwin->w_cursor.col;
7252#ifdef FEAT_MBYTE
7253 /* Correct cursor for multi-byte character. */
7254 if (has_mbyte)
7255 mb_adjust_cursor();
7256#endif
7257 }
7258 }
7259
7260#ifdef USE_IM_CONTROL
7261 /* Disable IM to allow typing English directly for Normal mode commands.
7262 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7263 * well). */
7264 if (!(State & LANGMAP))
7265 im_save_status(&curbuf->b_p_iminsert);
7266 im_set_active(FALSE);
7267#endif
7268
7269 State = NORMAL;
7270 /* need to position cursor again (e.g. when on a TAB ) */
7271 changed_cline_bef_curs();
7272
7273#ifdef FEAT_MOUSE
7274 setmouse();
7275#endif
7276#ifdef CURSOR_SHAPE
7277 ui_cursor_shape(); /* may show different cursor shape */
7278#endif
7279
7280 /*
7281 * When recording or for CTRL-O, need to display the new mode.
7282 * Otherwise remove the mode message.
7283 */
7284 if (Recording || restart_edit != NUL)
7285 showmode();
7286 else if (p_smd)
7287 MSG("");
7288
7289 return TRUE; /* exit Insert mode */
7290}
7291
7292#ifdef FEAT_RIGHTLEFT
7293/*
7294 * Toggle language: hkmap and revins_on.
7295 * Move to end of reverse inserted text.
7296 */
7297 static void
7298ins_ctrl_()
7299{
7300 if (revins_on && revins_chars && revins_scol >= 0)
7301 {
7302 while (gchar_cursor() != NUL && revins_chars--)
7303 ++curwin->w_cursor.col;
7304 }
7305 p_ri = !p_ri;
7306 revins_on = (State == INSERT && p_ri);
7307 if (revins_on)
7308 {
7309 revins_scol = curwin->w_cursor.col;
7310 revins_legal++;
7311 revins_chars = 0;
7312 undisplay_dollar();
7313 }
7314 else
7315 revins_scol = -1;
7316#ifdef FEAT_FKMAP
7317 if (p_altkeymap)
7318 {
7319 /*
7320 * to be consistent also for redo command, using '.'
7321 * set arrow_used to true and stop it - causing to redo
7322 * characters entered in one mode (normal/reverse insert).
7323 */
7324 arrow_used = TRUE;
7325 (void)stop_arrow();
7326 p_fkmap = curwin->w_p_rl ^ p_ri;
7327 if (p_fkmap && p_ri)
7328 State = INSERT;
7329 }
7330 else
7331#endif
7332 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7333 showmode();
7334}
7335#endif
7336
7337#ifdef FEAT_VISUAL
7338/*
7339 * If 'keymodel' contains "startsel", may start selection.
7340 * Returns TRUE when a CTRL-O and other keys stuffed.
7341 */
7342 static int
7343ins_start_select(c)
7344 int c;
7345{
7346 if (km_startsel)
7347 switch (c)
7348 {
7349 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 case K_PAGEUP:
7352 case K_KPAGEUP:
7353 case K_PAGEDOWN:
7354 case K_KPAGEDOWN:
7355# ifdef MACOS
7356 case K_LEFT:
7357 case K_RIGHT:
7358 case K_UP:
7359 case K_DOWN:
7360 case K_END:
7361 case K_HOME:
7362# endif
7363 if (!(mod_mask & MOD_MASK_SHIFT))
7364 break;
7365 /* FALLTHROUGH */
7366 case K_S_LEFT:
7367 case K_S_RIGHT:
7368 case K_S_UP:
7369 case K_S_DOWN:
7370 case K_S_END:
7371 case K_S_HOME:
7372 /* Start selection right away, the cursor can move with
7373 * CTRL-O when beyond the end of the line. */
7374 start_selection();
7375
7376 /* Execute the key in (insert) Select mode. */
7377 stuffcharReadbuff(Ctrl_O);
7378 if (mod_mask)
7379 {
7380 char_u buf[4];
7381
7382 buf[0] = K_SPECIAL;
7383 buf[1] = KS_MODIFIER;
7384 buf[2] = mod_mask;
7385 buf[3] = NUL;
7386 stuffReadbuff(buf);
7387 }
7388 stuffcharReadbuff(c);
7389 return TRUE;
7390 }
7391 return FALSE;
7392}
7393#endif
7394
7395/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007396 * <Insert> key in Insert mode: toggle insert/remplace mode.
7397 */
7398 static void
7399ins_insert(replaceState)
7400 int replaceState;
7401{
7402#ifdef FEAT_FKMAP
7403 if (p_fkmap && p_ri)
7404 {
7405 beep_flush();
7406 EMSG(farsi_text_3); /* encoded in Farsi */
7407 return;
7408 }
7409#endif
7410
7411#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007412# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007413 set_vim_var_string(VV_INSERTMODE,
7414 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007415# ifdef FEAT_VREPLACE
7416 replaceState == VREPLACE ? "v" :
7417# endif
7418 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007419# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007420 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7421#endif
7422 if (State & REPLACE_FLAG)
7423 State = INSERT | (State & LANGMAP);
7424 else
7425 State = replaceState | (State & LANGMAP);
7426 AppendCharToRedobuff(K_INS);
7427 showmode();
7428#ifdef CURSOR_SHAPE
7429 ui_cursor_shape(); /* may show different cursor shape */
7430#endif
7431}
7432
7433/*
7434 * Pressed CTRL-O in Insert mode.
7435 */
7436 static void
7437ins_ctrl_o()
7438{
7439#ifdef FEAT_VREPLACE
7440 if (State & VREPLACE_FLAG)
7441 restart_edit = 'V';
7442 else
7443#endif
7444 if (State & REPLACE_FLAG)
7445 restart_edit = 'R';
7446 else
7447 restart_edit = 'I';
7448#ifdef FEAT_VIRTUALEDIT
7449 if (virtual_active())
7450 ins_at_eol = FALSE; /* cursor always keeps its column */
7451 else
7452#endif
7453 ins_at_eol = (gchar_cursor() == NUL);
7454}
7455
7456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 * If the cursor is on an indent, ^T/^D insert/delete one
7458 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7459 * Always round the indent to 'shiftwith', this is compatible
7460 * with vi. But vi only supports ^T and ^D after an
7461 * autoindent, we support it everywhere.
7462 */
7463 static void
7464ins_shift(c, lastc)
7465 int c;
7466 int lastc;
7467{
7468 if (stop_arrow() == FAIL)
7469 return;
7470 AppendCharToRedobuff(c);
7471
7472 /*
7473 * 0^D and ^^D: remove all indent.
7474 */
7475 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7476 {
7477 --curwin->w_cursor.col;
7478 (void)del_char(FALSE); /* delete the '^' or '0' */
7479 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7480 if (State & REPLACE_FLAG)
7481 replace_pop_ins();
7482 if (lastc == '^')
7483 old_indent = get_indent(); /* remember curr. indent */
7484 change_indent(INDENT_SET, 0, TRUE, 0);
7485 }
7486 else
7487 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7488
7489 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7490 did_ai = FALSE;
7491#ifdef FEAT_SMARTINDENT
7492 did_si = FALSE;
7493 can_si = FALSE;
7494 can_si_back = FALSE;
7495#endif
7496#ifdef FEAT_CINDENT
7497 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7498#endif
7499}
7500
7501 static void
7502ins_del()
7503{
7504 int temp;
7505
7506 if (stop_arrow() == FAIL)
7507 return;
7508 if (gchar_cursor() == NUL) /* delete newline */
7509 {
7510 temp = curwin->w_cursor.col;
7511 if (!can_bs(BS_EOL) /* only if "eol" included */
7512 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7513 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7514 || do_join(FALSE) == FAIL)
7515 vim_beep();
7516 else
7517 curwin->w_cursor.col = temp;
7518 }
7519 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7520 vim_beep();
7521 did_ai = FALSE;
7522#ifdef FEAT_SMARTINDENT
7523 did_si = FALSE;
7524 can_si = FALSE;
7525 can_si_back = FALSE;
7526#endif
7527 AppendCharToRedobuff(K_DEL);
7528}
7529
7530/*
7531 * Handle Backspace, delete-word and delete-line in Insert mode.
7532 * Return TRUE when backspace was actually used.
7533 */
7534 static int
7535ins_bs(c, mode, inserted_space_p)
7536 int c;
7537 int mode;
7538 int *inserted_space_p;
7539{
7540 linenr_T lnum;
7541 int cc;
7542 int temp = 0; /* init for GCC */
7543 colnr_T mincol;
7544 int did_backspace = FALSE;
7545 int in_indent;
7546 int oldState;
7547#ifdef FEAT_MBYTE
7548 int p1, p2;
7549#endif
7550
7551 /*
7552 * can't delete anything in an empty file
7553 * can't backup past first character in buffer
7554 * can't backup past starting point unless 'backspace' > 1
7555 * can backup to a previous line if 'backspace' == 0
7556 */
7557 if ( bufempty()
7558 || (
7559#ifdef FEAT_RIGHTLEFT
7560 !revins_on &&
7561#endif
7562 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7563 || (!can_bs(BS_START)
7564 && (arrow_used
7565 || (curwin->w_cursor.lnum == Insstart.lnum
7566 && curwin->w_cursor.col <= Insstart.col)))
7567 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7568 && curwin->w_cursor.col <= ai_col)
7569 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7570 {
7571 vim_beep();
7572 return FALSE;
7573 }
7574
7575 if (stop_arrow() == FAIL)
7576 return FALSE;
7577 in_indent = inindent(0);
7578#ifdef FEAT_CINDENT
7579 if (in_indent)
7580 can_cindent = FALSE;
7581#endif
7582#ifdef FEAT_COMMENTS
7583 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7584#endif
7585#ifdef FEAT_RIGHTLEFT
7586 if (revins_on) /* put cursor after last inserted char */
7587 inc_cursor();
7588#endif
7589
7590#ifdef FEAT_VIRTUALEDIT
7591 /* Virtualedit:
7592 * BACKSPACE_CHAR eats a virtual space
7593 * BACKSPACE_WORD eats all coladd
7594 * BACKSPACE_LINE eats all coladd and keeps going
7595 */
7596 if (curwin->w_cursor.coladd > 0)
7597 {
7598 if (mode == BACKSPACE_CHAR)
7599 {
7600 --curwin->w_cursor.coladd;
7601 return TRUE;
7602 }
7603 if (mode == BACKSPACE_WORD)
7604 {
7605 curwin->w_cursor.coladd = 0;
7606 return TRUE;
7607 }
7608 curwin->w_cursor.coladd = 0;
7609 }
7610#endif
7611
7612 /*
7613 * delete newline!
7614 */
7615 if (curwin->w_cursor.col == 0)
7616 {
7617 lnum = Insstart.lnum;
7618 if (curwin->w_cursor.lnum == Insstart.lnum
7619#ifdef FEAT_RIGHTLEFT
7620 || revins_on
7621#endif
7622 )
7623 {
7624 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7625 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7626 return FALSE;
7627 --Insstart.lnum;
7628 Insstart.col = MAXCOL;
7629 }
7630 /*
7631 * In replace mode:
7632 * cc < 0: NL was inserted, delete it
7633 * cc >= 0: NL was replaced, put original characters back
7634 */
7635 cc = -1;
7636 if (State & REPLACE_FLAG)
7637 cc = replace_pop(); /* returns -1 if NL was inserted */
7638 /*
7639 * In replace mode, in the line we started replacing, we only move the
7640 * cursor.
7641 */
7642 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7643 {
7644 dec_cursor();
7645 }
7646 else
7647 {
7648#ifdef FEAT_VREPLACE
7649 if (!(State & VREPLACE_FLAG)
7650 || curwin->w_cursor.lnum > orig_line_count)
7651#endif
7652 {
7653 temp = gchar_cursor(); /* remember current char */
7654 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007655
7656 /* When "aw" is in 'formatoptions' we must delete the space at
7657 * the end of the line, otherwise the line will be broken
7658 * again when auto-formatting. */
7659 if (has_format_option(FO_AUTO)
7660 && has_format_option(FO_WHITE_PAR))
7661 {
7662 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7663 TRUE);
7664 int len;
7665
7666 len = STRLEN(ptr);
7667 if (len > 0 && ptr[len - 1] == ' ')
7668 ptr[len - 1] = NUL;
7669 }
7670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 (void)do_join(FALSE);
7672 if (temp == NUL && gchar_cursor() != NUL)
7673 inc_cursor();
7674 }
7675#ifdef FEAT_VREPLACE
7676 else
7677 dec_cursor();
7678#endif
7679
7680 /*
7681 * In REPLACE mode we have to put back the text that was replaced
7682 * by the NL. On the replace stack is first a NUL-terminated
7683 * sequence of characters that were deleted and then the
7684 * characters that NL replaced.
7685 */
7686 if (State & REPLACE_FLAG)
7687 {
7688 /*
7689 * Do the next ins_char() in NORMAL state, to
7690 * prevent ins_char() from replacing characters and
7691 * avoiding showmatch().
7692 */
7693 oldState = State;
7694 State = NORMAL;
7695 /*
7696 * restore characters (blanks) deleted after cursor
7697 */
7698 while (cc > 0)
7699 {
7700 temp = curwin->w_cursor.col;
7701#ifdef FEAT_MBYTE
7702 mb_replace_pop_ins(cc);
7703#else
7704 ins_char(cc);
7705#endif
7706 curwin->w_cursor.col = temp;
7707 cc = replace_pop();
7708 }
7709 /* restore the characters that NL replaced */
7710 replace_pop_ins();
7711 State = oldState;
7712 }
7713 }
7714 did_ai = FALSE;
7715 }
7716 else
7717 {
7718 /*
7719 * Delete character(s) before the cursor.
7720 */
7721#ifdef FEAT_RIGHTLEFT
7722 if (revins_on) /* put cursor on last inserted char */
7723 dec_cursor();
7724#endif
7725 mincol = 0;
7726 /* keep indent */
7727 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7728#ifdef FEAT_RIGHTLEFT
7729 && !revins_on
7730#endif
7731 )
7732 {
7733 temp = curwin->w_cursor.col;
7734 beginline(BL_WHITE);
7735 if (curwin->w_cursor.col < (colnr_T)temp)
7736 mincol = curwin->w_cursor.col;
7737 curwin->w_cursor.col = temp;
7738 }
7739
7740 /*
7741 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7742 */
7743 if ( mode == BACKSPACE_CHAR
7744 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007745 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 && (*(ml_get_cursor() - 1) == TAB
7747 || (*(ml_get_cursor() - 1) == ' '
7748 && (!*inserted_space_p
7749 || arrow_used))))))
7750 {
7751 int ts;
7752 colnr_T vcol;
7753 colnr_T want_vcol;
7754 int extra = 0;
7755
7756 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007757 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 ts = curbuf->b_p_sw;
7759 else
7760 ts = curbuf->b_p_sts;
7761 /* Compute the virtual column where we want to be. Since
7762 * 'showbreak' may get in the way, need to get the last column of
7763 * the previous character. */
7764 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7765 dec_cursor();
7766 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7767 inc_cursor();
7768 want_vcol = (want_vcol / ts) * ts;
7769
7770 /* delete characters until we are at or before want_vcol */
7771 while (vcol > want_vcol
7772 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7773 {
7774 dec_cursor();
7775 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7776 if (State & REPLACE_FLAG)
7777 {
7778 /* Don't delete characters before the insert point when in
7779 * Replace mode */
7780 if (curwin->w_cursor.lnum != Insstart.lnum
7781 || curwin->w_cursor.col >= Insstart.col)
7782 {
7783#if 0 /* what was this for? It causes problems when sw != ts. */
7784 if (State == REPLACE && (int)vcol < want_vcol)
7785 {
7786 (void)del_char(FALSE);
7787 extra = 2; /* don't pop too much */
7788 }
7789 else
7790#endif
7791 replace_do_bs();
7792 }
7793 }
7794 else
7795 (void)del_char(FALSE);
7796 }
7797
7798 /* insert extra spaces until we are at want_vcol */
7799 while (vcol < want_vcol)
7800 {
7801 /* Remember the first char we inserted */
7802 if (curwin->w_cursor.lnum == Insstart.lnum
7803 && curwin->w_cursor.col < Insstart.col)
7804 Insstart.col = curwin->w_cursor.col;
7805
7806#ifdef FEAT_VREPLACE
7807 if (State & VREPLACE_FLAG)
7808 ins_char(' ');
7809 else
7810#endif
7811 {
7812 ins_str((char_u *)" ");
7813 if ((State & REPLACE_FLAG) && extra <= 1)
7814 {
7815 if (extra)
7816 replace_push_off(NUL);
7817 else
7818 replace_push(NUL);
7819 }
7820 if (extra == 2)
7821 extra = 1;
7822 }
7823 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7824 }
7825 }
7826
7827 /*
7828 * Delete upto starting point, start of line or previous word.
7829 */
7830 else do
7831 {
7832#ifdef FEAT_RIGHTLEFT
7833 if (!revins_on) /* put cursor on char to be deleted */
7834#endif
7835 dec_cursor();
7836
7837 /* start of word? */
7838 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7839 {
7840 mode = BACKSPACE_WORD_NOT_SPACE;
7841 temp = vim_iswordc(gchar_cursor());
7842 }
7843 /* end of word? */
7844 else if (mode == BACKSPACE_WORD_NOT_SPACE
7845 && (vim_isspace(cc = gchar_cursor())
7846 || vim_iswordc(cc) != temp))
7847 {
7848#ifdef FEAT_RIGHTLEFT
7849 if (!revins_on)
7850#endif
7851 inc_cursor();
7852#ifdef FEAT_RIGHTLEFT
7853 else if (State & REPLACE_FLAG)
7854 dec_cursor();
7855#endif
7856 break;
7857 }
7858 if (State & REPLACE_FLAG)
7859 replace_do_bs();
7860 else
7861 {
7862#ifdef FEAT_MBYTE
7863 if (enc_utf8 && p_deco)
7864 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7865#endif
7866 (void)del_char(FALSE);
7867#ifdef FEAT_MBYTE
7868 /*
7869 * If p1 or p2 is non-zero, there are combining characters we
7870 * need to take account of. Don't back up before the base
7871 * character.
7872 */
7873 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7874 inc_cursor();
7875#endif
7876#ifdef FEAT_RIGHTLEFT
7877 if (revins_chars)
7878 {
7879 revins_chars--;
7880 revins_legal++;
7881 }
7882 if (revins_on && gchar_cursor() == NUL)
7883 break;
7884#endif
7885 }
7886 /* Just a single backspace?: */
7887 if (mode == BACKSPACE_CHAR)
7888 break;
7889 } while (
7890#ifdef FEAT_RIGHTLEFT
7891 revins_on ||
7892#endif
7893 (curwin->w_cursor.col > mincol
7894 && (curwin->w_cursor.lnum != Insstart.lnum
7895 || curwin->w_cursor.col != Insstart.col)));
7896 did_backspace = TRUE;
7897 }
7898#ifdef FEAT_SMARTINDENT
7899 did_si = FALSE;
7900 can_si = FALSE;
7901 can_si_back = FALSE;
7902#endif
7903 if (curwin->w_cursor.col <= 1)
7904 did_ai = FALSE;
7905 /*
7906 * It's a little strange to put backspaces into the redo
7907 * buffer, but it makes auto-indent a lot easier to deal
7908 * with.
7909 */
7910 AppendCharToRedobuff(c);
7911
7912 /* If deleted before the insertion point, adjust it */
7913 if (curwin->w_cursor.lnum == Insstart.lnum
7914 && curwin->w_cursor.col < Insstart.col)
7915 Insstart.col = curwin->w_cursor.col;
7916
7917 /* vi behaviour: the cursor moves backward but the character that
7918 * was there remains visible
7919 * Vim behaviour: the cursor moves backward and the character that
7920 * was there is erased from the screen.
7921 * We can emulate the vi behaviour by pretending there is a dollar
7922 * displayed even when there isn't.
7923 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7924 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7925 dollar_vcol = curwin->w_virtcol;
7926
7927 return did_backspace;
7928}
7929
7930#ifdef FEAT_MOUSE
7931 static void
7932ins_mouse(c)
7933 int c;
7934{
7935 pos_T tpos;
7936
7937# ifdef FEAT_GUI
7938 /* When GUI is active, also move/paste when 'mouse' is empty */
7939 if (!gui.in_use)
7940# endif
7941 if (!mouse_has(MOUSE_INSERT))
7942 return;
7943
7944 undisplay_dollar();
7945 tpos = curwin->w_cursor;
7946 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7947 {
7948 start_arrow(&tpos);
7949# ifdef FEAT_CINDENT
7950 can_cindent = TRUE;
7951# endif
7952 }
7953
7954#ifdef FEAT_WINDOWS
7955 /* redraw status lines (in case another window became active) */
7956 redraw_statuslines();
7957#endif
7958}
7959
7960 static void
7961ins_mousescroll(up)
7962 int up;
7963{
7964 pos_T tpos;
7965# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7966 win_T *old_curwin;
7967# endif
7968
7969 tpos = curwin->w_cursor;
7970
7971# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7972 old_curwin = curwin;
7973
7974 /* Currently the mouse coordinates are only known in the GUI. */
7975 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7976 {
7977 int row, col;
7978
7979 row = mouse_row;
7980 col = mouse_col;
7981
7982 /* find the window at the pointer coordinates */
7983 curwin = mouse_find_win(&row, &col);
7984 curbuf = curwin->w_buffer;
7985 }
7986 if (curwin == old_curwin)
7987# endif
7988 undisplay_dollar();
7989
7990 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7991 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7992 else
7993 scroll_redraw(up, 3L);
7994
7995# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7996 curwin->w_redr_status = TRUE;
7997
7998 curwin = old_curwin;
7999 curbuf = curwin->w_buffer;
8000# endif
8001
8002 if (!equalpos(curwin->w_cursor, tpos))
8003 {
8004 start_arrow(&tpos);
8005# ifdef FEAT_CINDENT
8006 can_cindent = TRUE;
8007# endif
8008 }
8009}
8010#endif
8011
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008012#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8013 void
8014ins_tabline(c)
8015 int c;
8016{
8017 /* We will be leaving the current window, unless closing another tab. */
8018 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8019 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8020 {
8021 undisplay_dollar();
8022 start_arrow(&curwin->w_cursor);
8023# ifdef FEAT_CINDENT
8024 can_cindent = TRUE;
8025# endif
8026 }
8027
8028 if (c == K_TABLINE)
8029 goto_tabpage(current_tab);
8030 else
8031 handle_tabmenu();
8032
8033}
8034#endif
8035
8036#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 void
8038ins_scroll()
8039{
8040 pos_T tpos;
8041
8042 undisplay_dollar();
8043 tpos = curwin->w_cursor;
8044 if (gui_do_scroll())
8045 {
8046 start_arrow(&tpos);
8047# ifdef FEAT_CINDENT
8048 can_cindent = TRUE;
8049# endif
8050 }
8051}
8052
8053 void
8054ins_horscroll()
8055{
8056 pos_T tpos;
8057
8058 undisplay_dollar();
8059 tpos = curwin->w_cursor;
8060 if (gui_do_horiz_scroll())
8061 {
8062 start_arrow(&tpos);
8063# ifdef FEAT_CINDENT
8064 can_cindent = TRUE;
8065# endif
8066 }
8067}
8068#endif
8069
8070 static void
8071ins_left()
8072{
8073 pos_T tpos;
8074
8075#ifdef FEAT_FOLDING
8076 if ((fdo_flags & FDO_HOR) && KeyTyped)
8077 foldOpenCursor();
8078#endif
8079 undisplay_dollar();
8080 tpos = curwin->w_cursor;
8081 if (oneleft() == OK)
8082 {
8083 start_arrow(&tpos);
8084#ifdef FEAT_RIGHTLEFT
8085 /* If exit reversed string, position is fixed */
8086 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8087 revins_legal++;
8088 revins_chars++;
8089#endif
8090 }
8091
8092 /*
8093 * if 'whichwrap' set for cursor in insert mode may go to
8094 * previous line
8095 */
8096 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8097 {
8098 start_arrow(&tpos);
8099 --(curwin->w_cursor.lnum);
8100 coladvance((colnr_T)MAXCOL);
8101 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8102 }
8103 else
8104 vim_beep();
8105}
8106
8107 static void
8108ins_home(c)
8109 int c;
8110{
8111 pos_T tpos;
8112
8113#ifdef FEAT_FOLDING
8114 if ((fdo_flags & FDO_HOR) && KeyTyped)
8115 foldOpenCursor();
8116#endif
8117 undisplay_dollar();
8118 tpos = curwin->w_cursor;
8119 if (c == K_C_HOME)
8120 curwin->w_cursor.lnum = 1;
8121 curwin->w_cursor.col = 0;
8122#ifdef FEAT_VIRTUALEDIT
8123 curwin->w_cursor.coladd = 0;
8124#endif
8125 curwin->w_curswant = 0;
8126 start_arrow(&tpos);
8127}
8128
8129 static void
8130ins_end(c)
8131 int c;
8132{
8133 pos_T tpos;
8134
8135#ifdef FEAT_FOLDING
8136 if ((fdo_flags & FDO_HOR) && KeyTyped)
8137 foldOpenCursor();
8138#endif
8139 undisplay_dollar();
8140 tpos = curwin->w_cursor;
8141 if (c == K_C_END)
8142 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8143 coladvance((colnr_T)MAXCOL);
8144 curwin->w_curswant = MAXCOL;
8145
8146 start_arrow(&tpos);
8147}
8148
8149 static void
8150ins_s_left()
8151{
8152#ifdef FEAT_FOLDING
8153 if ((fdo_flags & FDO_HOR) && KeyTyped)
8154 foldOpenCursor();
8155#endif
8156 undisplay_dollar();
8157 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8158 {
8159 start_arrow(&curwin->w_cursor);
8160 (void)bck_word(1L, FALSE, FALSE);
8161 curwin->w_set_curswant = TRUE;
8162 }
8163 else
8164 vim_beep();
8165}
8166
8167 static void
8168ins_right()
8169{
8170#ifdef FEAT_FOLDING
8171 if ((fdo_flags & FDO_HOR) && KeyTyped)
8172 foldOpenCursor();
8173#endif
8174 undisplay_dollar();
8175 if (gchar_cursor() != NUL || virtual_active()
8176 )
8177 {
8178 start_arrow(&curwin->w_cursor);
8179 curwin->w_set_curswant = TRUE;
8180#ifdef FEAT_VIRTUALEDIT
8181 if (virtual_active())
8182 oneright();
8183 else
8184#endif
8185 {
8186#ifdef FEAT_MBYTE
8187 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008188 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 else
8190#endif
8191 ++curwin->w_cursor.col;
8192 }
8193
8194#ifdef FEAT_RIGHTLEFT
8195 revins_legal++;
8196 if (revins_chars)
8197 revins_chars--;
8198#endif
8199 }
8200 /* if 'whichwrap' set for cursor in insert mode, may move the
8201 * cursor to the next line */
8202 else if (vim_strchr(p_ww, ']') != NULL
8203 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8204 {
8205 start_arrow(&curwin->w_cursor);
8206 curwin->w_set_curswant = TRUE;
8207 ++curwin->w_cursor.lnum;
8208 curwin->w_cursor.col = 0;
8209 }
8210 else
8211 vim_beep();
8212}
8213
8214 static void
8215ins_s_right()
8216{
8217#ifdef FEAT_FOLDING
8218 if ((fdo_flags & FDO_HOR) && KeyTyped)
8219 foldOpenCursor();
8220#endif
8221 undisplay_dollar();
8222 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8223 || gchar_cursor() != NUL)
8224 {
8225 start_arrow(&curwin->w_cursor);
8226 (void)fwd_word(1L, FALSE, 0);
8227 curwin->w_set_curswant = TRUE;
8228 }
8229 else
8230 vim_beep();
8231}
8232
8233 static void
8234ins_up(startcol)
8235 int startcol; /* when TRUE move to Insstart.col */
8236{
8237 pos_T tpos;
8238 linenr_T old_topline = curwin->w_topline;
8239#ifdef FEAT_DIFF
8240 int old_topfill = curwin->w_topfill;
8241#endif
8242
8243 undisplay_dollar();
8244 tpos = curwin->w_cursor;
8245 if (cursor_up(1L, TRUE) == OK)
8246 {
8247 if (startcol)
8248 coladvance(getvcol_nolist(&Insstart));
8249 if (old_topline != curwin->w_topline
8250#ifdef FEAT_DIFF
8251 || old_topfill != curwin->w_topfill
8252#endif
8253 )
8254 redraw_later(VALID);
8255 start_arrow(&tpos);
8256#ifdef FEAT_CINDENT
8257 can_cindent = TRUE;
8258#endif
8259 }
8260 else
8261 vim_beep();
8262}
8263
8264 static void
8265ins_pageup()
8266{
8267 pos_T tpos;
8268
8269 undisplay_dollar();
8270 tpos = curwin->w_cursor;
8271 if (onepage(BACKWARD, 1L) == OK)
8272 {
8273 start_arrow(&tpos);
8274#ifdef FEAT_CINDENT
8275 can_cindent = TRUE;
8276#endif
8277 }
8278 else
8279 vim_beep();
8280}
8281
8282 static void
8283ins_down(startcol)
8284 int startcol; /* when TRUE move to Insstart.col */
8285{
8286 pos_T tpos;
8287 linenr_T old_topline = curwin->w_topline;
8288#ifdef FEAT_DIFF
8289 int old_topfill = curwin->w_topfill;
8290#endif
8291
8292 undisplay_dollar();
8293 tpos = curwin->w_cursor;
8294 if (cursor_down(1L, TRUE) == OK)
8295 {
8296 if (startcol)
8297 coladvance(getvcol_nolist(&Insstart));
8298 if (old_topline != curwin->w_topline
8299#ifdef FEAT_DIFF
8300 || old_topfill != curwin->w_topfill
8301#endif
8302 )
8303 redraw_later(VALID);
8304 start_arrow(&tpos);
8305#ifdef FEAT_CINDENT
8306 can_cindent = TRUE;
8307#endif
8308 }
8309 else
8310 vim_beep();
8311}
8312
8313 static void
8314ins_pagedown()
8315{
8316 pos_T tpos;
8317
8318 undisplay_dollar();
8319 tpos = curwin->w_cursor;
8320 if (onepage(FORWARD, 1L) == OK)
8321 {
8322 start_arrow(&tpos);
8323#ifdef FEAT_CINDENT
8324 can_cindent = TRUE;
8325#endif
8326 }
8327 else
8328 vim_beep();
8329}
8330
8331#ifdef FEAT_DND
8332 static void
8333ins_drop()
8334{
8335 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8336}
8337#endif
8338
8339/*
8340 * Handle TAB in Insert or Replace mode.
8341 * Return TRUE when the TAB needs to be inserted like a normal character.
8342 */
8343 static int
8344ins_tab()
8345{
8346 int ind;
8347 int i;
8348 int temp;
8349
8350 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8351 Insstart_blank_vcol = get_nolist_virtcol();
8352 if (echeck_abbr(TAB + ABBR_OFF))
8353 return FALSE;
8354
8355 ind = inindent(0);
8356#ifdef FEAT_CINDENT
8357 if (ind)
8358 can_cindent = FALSE;
8359#endif
8360
8361 /*
8362 * When nothing special, insert TAB like a normal character
8363 */
8364 if (!curbuf->b_p_et
8365 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8366 && curbuf->b_p_sts == 0)
8367 return TRUE;
8368
8369 if (stop_arrow() == FAIL)
8370 return TRUE;
8371
8372 did_ai = FALSE;
8373#ifdef FEAT_SMARTINDENT
8374 did_si = FALSE;
8375 can_si = FALSE;
8376 can_si_back = FALSE;
8377#endif
8378 AppendToRedobuff((char_u *)"\t");
8379
8380 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8381 temp = (int)curbuf->b_p_sw;
8382 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8383 temp = (int)curbuf->b_p_sts;
8384 else /* otherwise use 'tabstop' */
8385 temp = (int)curbuf->b_p_ts;
8386 temp -= get_nolist_virtcol() % temp;
8387
8388 /*
8389 * Insert the first space with ins_char(). It will delete one char in
8390 * replace mode. Insert the rest with ins_str(); it will not delete any
8391 * chars. For VREPLACE mode, we use ins_char() for all characters.
8392 */
8393 ins_char(' ');
8394 while (--temp > 0)
8395 {
8396#ifdef FEAT_VREPLACE
8397 if (State & VREPLACE_FLAG)
8398 ins_char(' ');
8399 else
8400#endif
8401 {
8402 ins_str((char_u *)" ");
8403 if (State & REPLACE_FLAG) /* no char replaced */
8404 replace_push(NUL);
8405 }
8406 }
8407
8408 /*
8409 * When 'expandtab' not set: Replace spaces by TABs where possible.
8410 */
8411 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8412 {
8413 char_u *ptr;
8414#ifdef FEAT_VREPLACE
8415 char_u *saved_line = NULL; /* init for GCC */
8416 pos_T pos;
8417#endif
8418 pos_T fpos;
8419 pos_T *cursor;
8420 colnr_T want_vcol, vcol;
8421 int change_col = -1;
8422 int save_list = curwin->w_p_list;
8423
8424 /*
8425 * Get the current line. For VREPLACE mode, don't make real changes
8426 * yet, just work on a copy of the line.
8427 */
8428#ifdef FEAT_VREPLACE
8429 if (State & VREPLACE_FLAG)
8430 {
8431 pos = curwin->w_cursor;
8432 cursor = &pos;
8433 saved_line = vim_strsave(ml_get_curline());
8434 if (saved_line == NULL)
8435 return FALSE;
8436 ptr = saved_line + pos.col;
8437 }
8438 else
8439#endif
8440 {
8441 ptr = ml_get_cursor();
8442 cursor = &curwin->w_cursor;
8443 }
8444
8445 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8446 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8447 curwin->w_p_list = FALSE;
8448
8449 /* Find first white before the cursor */
8450 fpos = curwin->w_cursor;
8451 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8452 {
8453 --fpos.col;
8454 --ptr;
8455 }
8456
8457 /* In Replace mode, don't change characters before the insert point. */
8458 if ((State & REPLACE_FLAG)
8459 && fpos.lnum == Insstart.lnum
8460 && fpos.col < Insstart.col)
8461 {
8462 ptr += Insstart.col - fpos.col;
8463 fpos.col = Insstart.col;
8464 }
8465
8466 /* compute virtual column numbers of first white and cursor */
8467 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8468 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8469
8470 /* Use as many TABs as possible. Beware of 'showbreak' and
8471 * 'linebreak' adding extra virtual columns. */
8472 while (vim_iswhite(*ptr))
8473 {
8474 i = lbr_chartabsize((char_u *)"\t", vcol);
8475 if (vcol + i > want_vcol)
8476 break;
8477 if (*ptr != TAB)
8478 {
8479 *ptr = TAB;
8480 if (change_col < 0)
8481 {
8482 change_col = fpos.col; /* Column of first change */
8483 /* May have to adjust Insstart */
8484 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8485 Insstart.col = fpos.col;
8486 }
8487 }
8488 ++fpos.col;
8489 ++ptr;
8490 vcol += i;
8491 }
8492
8493 if (change_col >= 0)
8494 {
8495 int repl_off = 0;
8496
8497 /* Skip over the spaces we need. */
8498 while (vcol < want_vcol && *ptr == ' ')
8499 {
8500 vcol += lbr_chartabsize(ptr, vcol);
8501 ++ptr;
8502 ++repl_off;
8503 }
8504 if (vcol > want_vcol)
8505 {
8506 /* Must have a char with 'showbreak' just before it. */
8507 --ptr;
8508 --repl_off;
8509 }
8510 fpos.col += repl_off;
8511
8512 /* Delete following spaces. */
8513 i = cursor->col - fpos.col;
8514 if (i > 0)
8515 {
8516 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8517 /* correct replace stack. */
8518 if ((State & REPLACE_FLAG)
8519#ifdef FEAT_VREPLACE
8520 && !(State & VREPLACE_FLAG)
8521#endif
8522 )
8523 for (temp = i; --temp >= 0; )
8524 replace_join(repl_off);
8525 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008526#ifdef FEAT_NETBEANS_INTG
8527 if (usingNetbeans)
8528 {
8529 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8530 (long)(i + 1));
8531 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8532 (char_u *)"\t", 1);
8533 }
8534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 cursor->col -= i;
8536
8537#ifdef FEAT_VREPLACE
8538 /*
8539 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8540 * backspacing over the changed spacing and then inserting the new
8541 * spacing.
8542 */
8543 if (State & VREPLACE_FLAG)
8544 {
8545 /* Backspace from real cursor to change_col */
8546 backspace_until_column(change_col);
8547
8548 /* Insert each char in saved_line from changed_col to
8549 * ptr-cursor */
8550 ins_bytes_len(saved_line + change_col,
8551 cursor->col - change_col);
8552 }
8553#endif
8554 }
8555
8556#ifdef FEAT_VREPLACE
8557 if (State & VREPLACE_FLAG)
8558 vim_free(saved_line);
8559#endif
8560 curwin->w_p_list = save_list;
8561 }
8562
8563 return FALSE;
8564}
8565
8566/*
8567 * Handle CR or NL in insert mode.
8568 * Return TRUE when out of memory or can't undo.
8569 */
8570 static int
8571ins_eol(c)
8572 int c;
8573{
8574 int i;
8575
8576 if (echeck_abbr(c + ABBR_OFF))
8577 return FALSE;
8578 if (stop_arrow() == FAIL)
8579 return TRUE;
8580 undisplay_dollar();
8581
8582 /*
8583 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8584 * character under the cursor. Only push a NUL on the replace stack,
8585 * nothing to put back when the NL is deleted.
8586 */
8587 if ((State & REPLACE_FLAG)
8588#ifdef FEAT_VREPLACE
8589 && !(State & VREPLACE_FLAG)
8590#endif
8591 )
8592 replace_push(NUL);
8593
8594 /*
8595 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8596 * replacing the next line, so we push all of the characters left on the
8597 * line onto the replace stack. This is not done here though, it is done
8598 * in open_line().
8599 */
8600
8601#ifdef FEAT_RIGHTLEFT
8602# ifdef FEAT_FKMAP
8603 if (p_altkeymap && p_fkmap)
8604 fkmap(NL);
8605# endif
8606 /* NL in reverse insert will always start in the end of
8607 * current line. */
8608 if (revins_on)
8609 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8610#endif
8611
8612 AppendToRedobuff(NL_STR);
8613 i = open_line(FORWARD,
8614#ifdef FEAT_COMMENTS
8615 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8616#endif
8617 0, old_indent);
8618 old_indent = 0;
8619#ifdef FEAT_CINDENT
8620 can_cindent = TRUE;
8621#endif
8622
8623 return (!i);
8624}
8625
8626#ifdef FEAT_DIGRAPHS
8627/*
8628 * Handle digraph in insert mode.
8629 * Returns character still to be inserted, or NUL when nothing remaining to be
8630 * done.
8631 */
8632 static int
8633ins_digraph()
8634{
8635 int c;
8636 int cc;
8637
8638 pc_status = PC_STATUS_UNSET;
8639 if (redrawing() && !char_avail())
8640 {
8641 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008642 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
8644 edit_putchar('?', TRUE);
8645#ifdef FEAT_CMDL_INFO
8646 add_to_showcmd_c(Ctrl_K);
8647#endif
8648 }
8649
8650#ifdef USE_ON_FLY_SCROLL
8651 dont_scroll = TRUE; /* disallow scrolling here */
8652#endif
8653
8654 /* don't map the digraph chars. This also prevents the
8655 * mode message to be deleted when ESC is hit */
8656 ++no_mapping;
8657 ++allow_keys;
8658 c = safe_vgetc();
8659 --no_mapping;
8660 --allow_keys;
8661 if (IS_SPECIAL(c) || mod_mask) /* special key */
8662 {
8663#ifdef FEAT_CMDL_INFO
8664 clear_showcmd();
8665#endif
8666 insert_special(c, TRUE, FALSE);
8667 return NUL;
8668 }
8669 if (c != ESC)
8670 {
8671 if (redrawing() && !char_avail())
8672 {
8673 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008674 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
8676 if (char2cells(c) == 1)
8677 {
8678 /* first remove the '?', otherwise it's restored when typing
8679 * an ESC next */
8680 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008681 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 edit_putchar(c, TRUE);
8683 }
8684#ifdef FEAT_CMDL_INFO
8685 add_to_showcmd_c(c);
8686#endif
8687 }
8688 ++no_mapping;
8689 ++allow_keys;
8690 cc = safe_vgetc();
8691 --no_mapping;
8692 --allow_keys;
8693 if (cc != ESC)
8694 {
8695 AppendToRedobuff((char_u *)CTRL_V_STR);
8696 c = getdigraph(c, cc, TRUE);
8697#ifdef FEAT_CMDL_INFO
8698 clear_showcmd();
8699#endif
8700 return c;
8701 }
8702 }
8703 edit_unputchar();
8704#ifdef FEAT_CMDL_INFO
8705 clear_showcmd();
8706#endif
8707 return NUL;
8708}
8709#endif
8710
8711/*
8712 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8713 * Returns the char to be inserted, or NUL if none found.
8714 */
8715 static int
8716ins_copychar(lnum)
8717 linenr_T lnum;
8718{
8719 int c;
8720 int temp;
8721 char_u *ptr, *prev_ptr;
8722
8723 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8724 {
8725 vim_beep();
8726 return NUL;
8727 }
8728
8729 /* try to advance to the cursor column */
8730 temp = 0;
8731 ptr = ml_get(lnum);
8732 prev_ptr = ptr;
8733 validate_virtcol();
8734 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8735 {
8736 prev_ptr = ptr;
8737 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8738 }
8739 if ((colnr_T)temp > curwin->w_virtcol)
8740 ptr = prev_ptr;
8741
8742#ifdef FEAT_MBYTE
8743 c = (*mb_ptr2char)(ptr);
8744#else
8745 c = *ptr;
8746#endif
8747 if (c == NUL)
8748 vim_beep();
8749 return c;
8750}
8751
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008752/*
8753 * CTRL-Y or CTRL-E typed in Insert mode.
8754 */
8755 static int
8756ins_ctrl_ey(tc)
8757 int tc;
8758{
8759 int c = tc;
8760
8761#ifdef FEAT_INS_EXPAND
8762 if (ctrl_x_mode == CTRL_X_SCROLL)
8763 {
8764 if (c == Ctrl_Y)
8765 scrolldown_clamp();
8766 else
8767 scrollup_clamp();
8768 redraw_later(VALID);
8769 }
8770 else
8771#endif
8772 {
8773 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8774 if (c != NUL)
8775 {
8776 long tw_save;
8777
8778 /* The character must be taken literally, insert like it
8779 * was typed after a CTRL-V, and pretend 'textwidth'
8780 * wasn't set. Digits, 'o' and 'x' are special after a
8781 * CTRL-V, don't use it for these. */
8782 if (c < 256 && !isalnum(c))
8783 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8784 tw_save = curbuf->b_p_tw;
8785 curbuf->b_p_tw = -1;
8786 insert_special(c, TRUE, FALSE);
8787 curbuf->b_p_tw = tw_save;
8788#ifdef FEAT_RIGHTLEFT
8789 revins_chars++;
8790 revins_legal++;
8791#endif
8792 c = Ctrl_V; /* pretend CTRL-V is last character */
8793 auto_format(FALSE, TRUE);
8794 }
8795 }
8796 return c;
8797}
8798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799#ifdef FEAT_SMARTINDENT
8800/*
8801 * Try to do some very smart auto-indenting.
8802 * Used when inserting a "normal" character.
8803 */
8804 static void
8805ins_try_si(c)
8806 int c;
8807{
8808 pos_T *pos, old_pos;
8809 char_u *ptr;
8810 int i;
8811 int temp;
8812
8813 /*
8814 * do some very smart indenting when entering '{' or '}'
8815 */
8816 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8817 {
8818 /*
8819 * for '}' set indent equal to indent of line containing matching '{'
8820 */
8821 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8822 {
8823 old_pos = curwin->w_cursor;
8824 /*
8825 * If the matching '{' has a ')' immediately before it (ignoring
8826 * white-space), then line up with the start of the line
8827 * containing the matching '(' if there is one. This handles the
8828 * case where an "if (..\n..) {" statement continues over multiple
8829 * lines -- webb
8830 */
8831 ptr = ml_get(pos->lnum);
8832 i = pos->col;
8833 if (i > 0) /* skip blanks before '{' */
8834 while (--i > 0 && vim_iswhite(ptr[i]))
8835 ;
8836 curwin->w_cursor.lnum = pos->lnum;
8837 curwin->w_cursor.col = i;
8838 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8839 curwin->w_cursor = *pos;
8840 i = get_indent();
8841 curwin->w_cursor = old_pos;
8842#ifdef FEAT_VREPLACE
8843 if (State & VREPLACE_FLAG)
8844 change_indent(INDENT_SET, i, FALSE, NUL);
8845 else
8846#endif
8847 (void)set_indent(i, SIN_CHANGED);
8848 }
8849 else if (curwin->w_cursor.col > 0)
8850 {
8851 /*
8852 * when inserting '{' after "O" reduce indent, but not
8853 * more than indent of previous line
8854 */
8855 temp = TRUE;
8856 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8857 {
8858 old_pos = curwin->w_cursor;
8859 i = get_indent();
8860 while (curwin->w_cursor.lnum > 1)
8861 {
8862 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8863
8864 /* ignore empty lines and lines starting with '#'. */
8865 if (*ptr != '#' && *ptr != NUL)
8866 break;
8867 }
8868 if (get_indent() >= i)
8869 temp = FALSE;
8870 curwin->w_cursor = old_pos;
8871 }
8872 if (temp)
8873 shift_line(TRUE, FALSE, 1);
8874 }
8875 }
8876
8877 /*
8878 * set indent of '#' always to 0
8879 */
8880 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8881 {
8882 /* remember current indent for next line */
8883 old_indent = get_indent();
8884 (void)set_indent(0, SIN_CHANGED);
8885 }
8886
8887 /* Adjust ai_col, the char at this position can be deleted. */
8888 if (ai_col > curwin->w_cursor.col)
8889 ai_col = curwin->w_cursor.col;
8890}
8891#endif
8892
8893/*
8894 * Get the value that w_virtcol would have when 'list' is off.
8895 * Unless 'cpo' contains the 'L' flag.
8896 */
8897 static colnr_T
8898get_nolist_virtcol()
8899{
8900 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8901 return getvcol_nolist(&curwin->w_cursor);
8902 validate_virtcol();
8903 return curwin->w_virtcol;
8904}