blob: 25a10be2a96f89c29f4a5a04ab2992aa71a66abd [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 Moolenaar8b6144b2006-02-08 09:20:24 +00003256 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3257 {
3258 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3259 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003260 if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase",
3261 FALSE) == NULL)
3262 icase = p_ic;
3263 else
3264 icase = get_dict_number(li->li_tv.vval.v_dict,
3265 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003266 }
3267 else
3268 {
3269 p = get_tv_string_chk(&li->li_tv);
3270 x = NULL;
3271 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003272 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003273 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003274 if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003275 /* if dir was BACKWARD then honor it just once */
3276 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003277 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003278 else if (did_emsg)
3279 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003280 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003281
3282 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003283}
3284#endif /* FEAT_COMPL_FUNC */
3285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003286/*
3287 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003288 * The search starts at position "ini" in curbuf and in the direction
3289 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003290 * When "compl_started" is FALSE start at that position, otherwise continue
3291 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003292 * This may return before finding all the matches.
3293 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 */
3295 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003296ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 static pos_T first_match_pos;
3300 static pos_T last_match_pos;
3301 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003302 static int found_all = FALSE; /* Found all matches of a
3303 certain type. */
3304 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar572cb562005-08-05 21:35:02 +00003306 pos_T *pos;
3307 char_u **matches;
3308 int save_p_scs;
3309 int save_p_ws;
3310 int save_p_ic;
3311 int i;
3312 int num_matches;
3313 int len;
3314 int found_new_match;
3315 int type = ctrl_x_mode;
3316 char_u *ptr;
3317 char_u *dict = NULL;
3318 int dict_f = 0;
3319 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003321 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
3323 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3324 ins_buf->b_scanned = 0;
3325 found_all = FALSE;
3326 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003327 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003328 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 last_match_pos = first_match_pos = *ini;
3330 }
3331
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003332 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003333 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3335 for (;;)
3336 {
3337 found_new_match = FAIL;
3338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003339 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 * or if found_all says this entry is done. For ^X^L only use the
3341 * entries from 'complete' that look in loaded buffers. */
3342 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003343 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
3345 found_all = FALSE;
3346 while (*e_cpt == ',' || *e_cpt == ' ')
3347 e_cpt++;
3348 if (*e_cpt == '.' && !curbuf->b_scanned)
3349 {
3350 ins_buf = curbuf;
3351 first_match_pos = *ini;
3352 /* So that ^N can match word immediately after cursor */
3353 if (ctrl_x_mode == 0)
3354 dec(&first_match_pos);
3355 last_match_pos = first_match_pos;
3356 type = 0;
3357 }
3358 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3359 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3360 {
3361 /* Scan a buffer, but not the current one. */
3362 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3363 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003364 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 first_match_pos.col = last_match_pos.col = 0;
3366 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3367 last_match_pos.lnum = 0;
3368 type = 0;
3369 }
3370 else /* unloaded buffer, scan like dictionary */
3371 {
3372 found_all = TRUE;
3373 if (ins_buf->b_fname == NULL)
3374 continue;
3375 type = CTRL_X_DICTIONARY;
3376 dict = ins_buf->b_fname;
3377 dict_f = DICT_EXACT;
3378 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003379 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 ins_buf->b_fname == NULL
3381 ? buf_spname(ins_buf)
3382 : ins_buf->b_sfname == NULL
3383 ? (char *)ins_buf->b_fname
3384 : (char *)ins_buf->b_sfname);
3385 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3386 }
3387 else if (*e_cpt == NUL)
3388 break;
3389 else
3390 {
3391 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3392 type = -1;
3393 else if (*e_cpt == 'k' || *e_cpt == 's')
3394 {
3395 if (*e_cpt == 'k')
3396 type = CTRL_X_DICTIONARY;
3397 else
3398 type = CTRL_X_THESAURUS;
3399 if (*++e_cpt != ',' && *e_cpt != NUL)
3400 {
3401 dict = e_cpt;
3402 dict_f = DICT_FIRST;
3403 }
3404 }
3405#ifdef FEAT_FIND_ID
3406 else if (*e_cpt == 'i')
3407 type = CTRL_X_PATH_PATTERNS;
3408 else if (*e_cpt == 'd')
3409 type = CTRL_X_PATH_DEFINES;
3410#endif
3411 else if (*e_cpt == ']' || *e_cpt == 't')
3412 {
3413 type = CTRL_X_TAGS;
3414 sprintf((char*)IObuff, _("Scanning tags."));
3415 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3416 }
3417 else
3418 type = -1;
3419
3420 /* in any case e_cpt is advanced to the next entry */
3421 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3422
3423 found_all = TRUE;
3424 if (type == -1)
3425 continue;
3426 }
3427 }
3428
3429 switch (type)
3430 {
3431 case -1:
3432 break;
3433#ifdef FEAT_FIND_ID
3434 case CTRL_X_PATH_PATTERNS:
3435 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003436 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003437 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003439 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3441 (linenr_T)1, (linenr_T)MAXLNUM);
3442 break;
3443#endif
3444
3445 case CTRL_X_DICTIONARY:
3446 case CTRL_X_THESAURUS:
3447 ins_compl_dictionaries(
3448 dict ? dict
3449 : (type == CTRL_X_THESAURUS
3450 ? (*curbuf->b_p_tsr == NUL
3451 ? p_tsr
3452 : curbuf->b_p_tsr)
3453 : (*curbuf->b_p_dict == NUL
3454 ? p_dict
3455 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003456 compl_pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3458 dict = NULL;
3459 break;
3460
3461 case CTRL_X_TAGS:
3462 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3463 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003464 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
3466 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003467 * of matches is found when compl_pattern is empty */
3468 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3470 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3471 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3472 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003473 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 p_ic = save_p_ic;
3476 break;
3477
3478 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003479 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3481 {
3482
3483 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003484 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003485 ins_compl_add_matches(num_matches, matches,
3486#ifdef CASE_INSENSITIVE_FILENAME
3487 TRUE
3488#else
3489 FALSE
3490#endif
3491 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493 break;
3494
3495 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003496 if (expand_cmdline(&compl_xp, compl_pattern,
3497 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003499 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 break;
3501
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003502#ifdef FEAT_COMPL_FUNC
3503 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003504 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003505 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003506 break;
3507#endif
3508
Bram Moolenaar488c6512005-08-11 20:09:58 +00003509 case CTRL_X_SPELL:
3510#ifdef FEAT_SYN_HL
3511 num_matches = expand_spelling(first_match_pos.lnum,
3512 first_match_pos.col, compl_pattern, &matches);
3513 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003514 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003515#endif
3516 break;
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 default: /* normal ^P/^N and ^X^L */
3519 /*
3520 * If 'infercase' is set, don't use 'smartcase' here
3521 */
3522 save_p_scs = p_scs;
3523 if (ins_buf->b_p_inf)
3524 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 /* buffers other than curbuf are scanned from the beginning or the
3527 * end but never from the middle, thus setting nowrapscan in this
3528 * buffers is a good idea, on the other hand, we always set
3529 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3530 save_p_ws = p_ws;
3531 if (ins_buf != curbuf)
3532 p_ws = FALSE;
3533 else if (*e_cpt == '.')
3534 p_ws = TRUE;
3535 for (;;)
3536 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003537 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003539 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3540 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003542 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003544 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003546 found_new_match = searchit(NULL, ins_buf, pos,
3547 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003549 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003552 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 first_match_pos = *pos;
3555 last_match_pos = *pos;
3556 }
3557 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003558 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 found_new_match = FAIL;
3560 if (found_new_match == FAIL)
3561 {
3562 if (ins_buf == curbuf)
3563 found_all = TRUE;
3564 break;
3565 }
3566
3567 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003568 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 && ini->lnum == pos->lnum
3570 && ini->col == pos->col)
3571 continue;
3572 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3573 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3574 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003575 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
3577 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3578 continue;
3579 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3580 if (!p_paste)
3581 ptr = skipwhite(ptr);
3582 }
3583 len = (int)STRLEN(ptr);
3584 }
3585 else
3586 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003587 char_u *tmp_ptr = ptr;
3588
3589 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 /* Skip if already inside a word. */
3593 if (vim_iswordp(tmp_ptr))
3594 continue;
3595 /* Find start of next word. */
3596 tmp_ptr = find_word_start(tmp_ptr);
3597 }
3598 /* Find end of this word. */
3599 tmp_ptr = find_word_end(tmp_ptr);
3600 len = (int)(tmp_ptr - ptr);
3601
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 if ((compl_cont_status & CONT_ADDING)
3603 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3606 {
3607 /* Try next line, if any. the new word will be
3608 * "join" as if the normal command "J" was used.
3609 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003610 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 * works -- Acevedo */
3612 STRNCPY(IObuff, ptr, len);
3613 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3614 tmp_ptr = ptr = skipwhite(ptr);
3615 /* Find start of next word. */
3616 tmp_ptr = find_word_start(tmp_ptr);
3617 /* Find end of next word. */
3618 tmp_ptr = find_word_end(tmp_ptr);
3619 if (tmp_ptr > ptr)
3620 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003621 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003623 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 IObuff[len++] = ' ';
3625 /* IObuf =~ "\k.* ", thus len >= 2 */
3626 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003627 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 || (vim_strchr(p_cpo, CPO_JOINSP)
3629 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003630 && (IObuff[len - 2] == '?'
3631 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 IObuff[len++] = ' ';
3633 }
3634 /* copy as much as posible of the new word */
3635 if (tmp_ptr - ptr >= IOSIZE - len)
3636 tmp_ptr = ptr + IOSIZE - len - 1;
3637 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3638 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003639 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641 IObuff[len] = NUL;
3642 ptr = IObuff;
3643 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003644 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 continue;
3646 }
3647 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003648 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003649 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003650 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 found_new_match = OK;
3653 break;
3654 }
3655 }
3656 p_scs = save_p_scs;
3657 p_ws = save_p_ws;
3658 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003659
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003660 /* check if compl_curr_match has changed, (e.g. other type of
3661 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003662 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 found_new_match = OK;
3664
3665 /* break the loop for specialized modes (use 'complete' just for the
3666 * generic ctrl_x_mode == 0) or when we've found a new match */
3667 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003668 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003669 {
3670 if (got_int)
3671 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003672 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003673 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003674 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003675
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003676 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3677 || compl_interrupted)
3678 break;
3679 compl_started = TRUE;
3680 }
3681 else
3682 {
3683 /* Mark a buffer scanned when it has been scanned completely */
3684 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3685 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003687 compl_started = FALSE;
3688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003690 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
3692 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3693 && *e_cpt == NUL) /* Got to end of 'complete' */
3694 found_new_match = FAIL;
3695
3696 i = -1; /* total of matches, unknown */
3697 if (found_new_match == FAIL
3698 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3699 i = ins_compl_make_cyclic();
3700
3701 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 * just been made cyclic then we have to move compl_curr_match to the next
3703 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003704 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003705 if (compl_curr_match == NULL)
3706 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 return i;
3708}
3709
3710/* Delete the old text being completed. */
3711 static void
3712ins_compl_delete()
3713{
3714 int i;
3715
3716 /*
3717 * In insert mode: Delete the typed part.
3718 * In replace mode: Put the old characters back, if any.
3719 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003720 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 backspace_until_column(i);
3722 changed_cline_bef_curs();
3723}
3724
3725/* Insert the new text being completed. */
3726 static void
3727ins_compl_insert()
3728{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003729 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003730 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3731 compl_used_match = FALSE;
3732 else
3733 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734}
3735
3736/*
3737 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003738 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3739 * get more completions. If it is FALSE, then we just do nothing when there
3740 * are no more completions in a given direction. The latter case is used when
3741 * we are still in the middle of finding completions, to allow browsing
3742 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 * Return the total number of matches, or -1 if still unknown -- webb.
3744 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003745 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3746 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 *
3748 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003749 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3750 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 */
3752 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003753ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003755 int count; /* repeat completion this many times; should
3756 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003757 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 int num_matches = -1;
3760 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003761 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003762 compl_T *found_compl = NULL;
3763 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003765 if (compl_leader != NULL
3766 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003768 /* Set "compl_shown_match" to the actually shown match, it may differ
3769 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003770 while (!ins_compl_equal(compl_shown_match,
3771 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003772 && compl_shown_match->cp_next != NULL
3773 && compl_shown_match->cp_next != compl_first_match)
3774 compl_shown_match = compl_shown_match->cp_next;
3775 }
3776
3777 if (allow_get_expansion && insert_match
3778 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 /* Delete old text to be replaced */
3780 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003781
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003782 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003783
3784 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3785 * around. */
3786 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003788 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003790 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003791 found_end = (compl_first_match != NULL
3792 && (compl_shown_match->cp_next == compl_first_match
3793 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003794 }
3795 else if (compl_shows_dir == BACKWARD
3796 && compl_shown_match->cp_prev != NULL)
3797 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003798 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003799 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003800 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003803 {
3804 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003805 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003806 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003807
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003809 if (compl_pending && compl_direction == compl_shows_dir)
3810 compl_shown_match = compl_curr_match;
3811 found_end = FALSE;
3812 }
3813 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3814 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003815 && !ins_compl_equal(compl_shown_match,
3816 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003817 ++todo;
3818 else
3819 /* Remember a matching item. */
3820 found_compl = compl_shown_match;
3821
3822 /* Stop at the end of the list when we found a usable match. */
3823 if (found_end)
3824 {
3825 if (found_compl != NULL)
3826 {
3827 compl_shown_match = found_compl;
3828 break;
3829 }
3830 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003834 /* Insert the text of the new completion, or the compl_leader. */
3835 if (insert_match)
3836 {
3837 if (!compl_get_longest || compl_used_match)
3838 ins_compl_insert();
3839 else
3840 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3841 }
3842 else
3843 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 if (!allow_get_expansion)
3846 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003847 /* may undisplay the popup menu first */
3848 ins_compl_upd_pum();
3849
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003850 /* redraw to show the user what was inserted */
3851 update_screen(0);
3852
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003853 /* display the updated popup menu */
3854 ins_compl_show_pum();
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 /* Delete old text to be replaced, since we're still searching and
3857 * don't want to match ourselves! */
3858 ins_compl_delete();
3859 }
3860
3861 /*
3862 * Show the file name for the match (if any)
3863 * Truncate the file name to avoid a wait for return.
3864 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003865 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003868 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (i <= 0)
3870 i = 0;
3871 else
3872 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003873 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 msg(IObuff);
3875 redraw_cmdline = FALSE; /* don't overwrite! */
3876 }
3877
3878 return num_matches;
3879}
3880
3881/*
3882 * Call this while finding completions, to check whether the user has hit a key
3883 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003884 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003886 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 */
3888 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003889ins_compl_check_keys(frequency)
3890 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
3892 static int count = 0;
3893
3894 int c;
3895
3896 /* Don't check when reading keys from a script. That would break the test
3897 * scripts */
3898 if (using_script())
3899 return;
3900
3901 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003902 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 return;
3904 count = 0;
3905
3906 ++no_mapping;
3907 c = vpeekc_any();
3908 --no_mapping;
3909 if (c != NUL)
3910 {
3911 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3912 {
3913 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003914 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003915 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3916 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 if (compl_pending && !got_int)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003922 (void)ins_compl_next(FALSE, 1, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003923}
3924
3925/*
3926 * Decide the direction of Insert mode complete from the key typed.
3927 * Returns BACKWARD or FORWARD.
3928 */
3929 static int
3930ins_compl_key2dir(c)
3931 int c;
3932{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003933 if (c == Ctrl_P || c == Ctrl_L
3934 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
3935 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003936 return BACKWARD;
3937 return FORWARD;
3938}
3939
3940/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003941 * Return TRUE for keys that are used for completion only when the popup menu
3942 * is visible.
3943 */
3944 static int
3945ins_compl_pum_key(c)
3946 int c;
3947{
3948 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003949 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3950 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951}
3952
3953/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003954 * Decide the number of completions to move forward.
3955 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3956 */
3957 static int
3958ins_compl_key2count(c)
3959 int c;
3960{
3961 int h;
3962
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003963 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003964 {
3965 h = pum_get_height();
3966 if (h > 3)
3967 h -= 2; /* keep some context */
3968 return h;
3969 }
3970 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971}
3972
3973/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003974 * Return TRUE if completion with "c" should insert the match, FALSE if only
3975 * to change the currently selected completion.
3976 */
3977 static int
3978ins_compl_use_match(c)
3979 int c;
3980{
3981 switch (c)
3982 {
3983 case K_UP:
3984 case K_DOWN:
3985 case K_PAGEDOWN:
3986 case K_KPAGEDOWN:
3987 case K_S_DOWN:
3988 case K_PAGEUP:
3989 case K_KPAGEUP:
3990 case K_S_UP:
3991 return FALSE;
3992 }
3993 return TRUE;
3994}
3995
3996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 * Do Insert mode completion.
3998 * Called when character "c" was typed, which has a meaning for completion.
3999 * Returns OK if completion was done, FAIL if something failed (out of mem).
4000 */
4001 static int
4002ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004003 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004005 char_u *line;
4006 int startcol = 0; /* column where searched text starts */
4007 colnr_T curs_col; /* cursor column */
4008 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaare3226be2005-12-18 22:10:00 +00004010 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004011 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
4013 /* First time we hit ^N or ^P (in a row, I mean) */
4014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 did_ai = FALSE;
4016#ifdef FEAT_SMARTINDENT
4017 did_si = FALSE;
4018 can_si = FALSE;
4019 can_si_back = FALSE;
4020#endif
4021 if (stop_arrow() == FAIL)
4022 return FAIL;
4023
4024 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004025 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004028 * "compl_startpos" to the cursor as a pattern to add a new word
4029 * instead of expand the one before the cursor, in word-wise if
4030 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 * is not in the same line as the cursor then fix it (the line has
4032 * been split because it was longer than 'tw'). if SOL is set then
4033 * skip the previous pattern, a word at the beginning of the line has
4034 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004035 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4036 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038 /*
4039 * it is a continued search
4040 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4043 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4044 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004045 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 /* line (probably) wrapped, set compl_startpos to the
4048 * first non_blank in the line, if it is not a wordchar
4049 * include it to get a better pattern, but then we don't
4050 * want the "\\<" prefix, check it bellow */
4051 compl_col = (colnr_T)(skipwhite(line) - line);
4052 compl_startpos.col = compl_col;
4053 compl_startpos.lnum = curwin->w_cursor.lnum;
4054 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
4056 else
4057 {
4058 /* S_IPOS was set when we inserted a word that was at the
4059 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004060 * mode but first we need to redefine compl_startpos */
4061 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004063 compl_cont_status |= CONT_SOL;
4064 compl_startpos.col = (colnr_T)(skipwhite(
4065 line + compl_length
4066 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004068 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004071 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 * have enough space? just being paranoic */
4073#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004076 compl_cont_status &= ~CONT_SOL;
4077 compl_length = (IOSIZE - MIN_SPACE);
4078 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004080 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4081 if (compl_length < 1)
4082 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004090 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004092 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004094 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096 compl_cont_status = 0;
4097 compl_cont_status |= CONT_N_ADDS;
4098 compl_startpos = curwin->w_cursor;
4099 startcol = (int)curs_col;
4100 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102
4103 /* Work out completion pattern and original text -- webb */
4104 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4105 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004106 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4108 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004109 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004111 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004113 compl_col += ++startcol;
4114 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117 compl_pattern = str_foldcase(line + compl_col,
4118 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 compl_pattern = vim_strnsave(line + compl_col,
4121 compl_length);
4122 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004125 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
4127 char_u *prefix = (char_u *)"\\<";
4128
4129 /* we need 3 extra chars, 1 for the NUL and
4130 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4132 compl_length) + 3);
4133 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 if (!vim_iswordp(line + compl_col)
4136 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 && (
4138#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142#endif
4143 )))
4144 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 STRCPY((char *)compl_pattern, prefix);
4146 (void)quote_meta(compl_pattern + STRLEN(prefix),
4147 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004149 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004153 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#endif
4155 )
4156 {
4157 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4159 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161 compl_col += curs_col;
4162 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 else
4165 {
4166#ifdef FEAT_MBYTE
4167 /* Search the point of change class of multibyte character
4168 * or not a word single byte character backward. */
4169 if (has_mbyte)
4170 {
4171 int base_class;
4172 int head_off;
4173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 startcol -= (*mb_head_off)(line, line + startcol);
4175 base_class = mb_get_class(line + startcol);
4176 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004178 head_off = (*mb_head_off)(line, line + startcol);
4179 if (base_class != mb_get_class(line + startcol
4180 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 }
4185 else
4186#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004187 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189 compl_col += ++startcol;
4190 compl_length = (int)curs_col - startcol;
4191 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
4193 /* Only match word with at least two chars -- webb
4194 * there's no need to call quote_meta,
4195 * alloc(7) is enough -- Acevedo
4196 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004197 compl_pattern = alloc(7);
4198 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200 STRCPY((char *)compl_pattern, "\\<");
4201 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4202 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 else
4205 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4207 compl_length) + 3);
4208 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 STRCPY((char *)compl_pattern, "\\<");
4211 (void)quote_meta(compl_pattern + 2, line + compl_col,
4212 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 }
4215 }
4216 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4217 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218 compl_col = skipwhite(line) - line;
4219 compl_length = (int)curs_col - (int)compl_col;
4220 if (compl_length < 0) /* cursor in indent: empty pattern */
4221 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 compl_pattern = str_foldcase(line + compl_col, compl_length,
4224 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004226 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4227 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229 }
4230 else if (ctrl_x_mode == CTRL_X_FILES)
4231 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 compl_col += ++startcol;
4235 compl_length = (int)curs_col - startcol;
4236 compl_pattern = addstar(line + compl_col, compl_length,
4237 EXPAND_FILES);
4238 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return FAIL;
4240 }
4241 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4242 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004243 compl_pattern = vim_strnsave(line, curs_col);
4244 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 set_cmd_context(&compl_xp, compl_pattern,
4247 (int)STRLEN(compl_pattern), curs_col);
4248 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4249 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4252 compl_col = startcol;
4253 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004255 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004256 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004257#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004258 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004259 * Call user defined function 'completefunc' with "a:findstart"
4260 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004261 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004262 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004263 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004264 char_u *funcname;
4265 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004266
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004267 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004268 * string */
4269 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4270 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4271 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004272 {
4273 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4274 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004275 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004276 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004277
4278 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004279 args[1] = NULL;
4280 pos = curwin->w_cursor;
4281 col = call_func_retnr(funcname, 2, args, FALSE);
4282 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004283
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004284 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004285 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004286 compl_col = col;
4287 if ((colnr_T)compl_col > curs_col)
4288 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004290 /* Setup variables for completion. Need to obtain "line" again,
4291 * it may have become invalid. */
4292 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004293 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4295 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004296#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 return FAIL;
4298 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004299 else if (ctrl_x_mode == CTRL_X_SPELL)
4300 {
4301#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004302 if (spell_bad_len > 0)
4303 compl_col = curs_col - spell_bad_len;
4304 else
4305 compl_col = spell_word_start(startcol);
4306 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004307 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004308 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004309 compl_length = (int)curs_col - compl_col;
4310 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4311 if (compl_pattern == NULL)
4312#endif
4313 return FAIL;
4314 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 else
4316 {
4317 EMSG2(_(e_intern2), "ins_complete()");
4318 return FAIL;
4319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
4323 edit_submode_pre = (char_u *)_(" Adding");
4324 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4325 {
4326 /* Insert a new line, keep indentation but ignore 'comments' */
4327#ifdef FEAT_COMMENTS
4328 char_u *old = curbuf->b_p_com;
4329
4330 curbuf->b_p_com = (char_u *)"";
4331#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332 compl_startpos.lnum = curwin->w_cursor.lnum;
4333 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 ins_eol('\r');
4335#ifdef FEAT_COMMENTS
4336 curbuf->b_p_com = old;
4337#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 compl_length = 0;
4339 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 }
4342 else
4343 {
4344 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004345 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348 if (compl_cont_status & CONT_LOCAL)
4349 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 else
4351 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4352
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004353 /* Always add completion for the original text. */
4354 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004355 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4356 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004357 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359 vim_free(compl_pattern);
4360 compl_pattern = NULL;
4361 vim_free(compl_orig_text);
4362 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 return FAIL;
4364 }
4365
4366 /* showmode might reset the internal line pointers, so it must
4367 * be called before line = ml_get(), or when this address is no
4368 * longer needed. -- Acevedo.
4369 */
4370 edit_submode_extra = (char_u *)_("-- Searching...");
4371 edit_submode_highl = HLF_COUNT;
4372 showmode();
4373 edit_submode_extra = NULL;
4374 out_flush();
4375 }
4376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004377 compl_shown_match = compl_curr_match;
4378 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004381 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004383 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004385 /* may undisplay the popup menu */
4386 ins_compl_upd_pum();
4387
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 if (n > 1) /* all matches have been found */
4389 compl_matches = n;
4390 compl_curr_match = compl_shown_match;
4391 compl_direction = compl_shows_dir;
4392 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 /* eat the ESC to avoid leaving insert mode */
4395 if (got_int && !global_busy)
4396 {
4397 (void)vgetc();
4398 got_int = FALSE;
4399 }
4400
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004402 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004404 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4405 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4407 edit_submode_highl = HLF_E;
4408 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4409 * because we couldn't expand anything at first place, but if we used
4410 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4411 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 if ( compl_length > 1
4413 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 || (ctrl_x_mode != 0
4415 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4416 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419
Bram Moolenaar572cb562005-08-05 21:35:02 +00004420 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 if (edit_submode_extra == NULL)
4426 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004427 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
4429 edit_submode_extra = (char_u *)_("Back at original");
4430 edit_submode_highl = HLF_W;
4431 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
4434 edit_submode_extra = (char_u *)_("Word from other line");
4435 edit_submode_highl = HLF_COUNT;
4436 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004437 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 edit_submode_extra = (char_u *)_("The only match");
4440 edit_submode_highl = HLF_COUNT;
4441 }
4442 else
4443 {
4444 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004445 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004447 int number = 0;
4448 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 /* search backwards for the first valid (!= -1) number.
4453 * This should normally succeed already at the first loop
4454 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004455 for (match = compl_curr_match->cp_prev; match != NULL
4456 && match != compl_first_match;
4457 match = match->cp_prev)
4458 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004460 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 break;
4462 }
4463 if (match != NULL)
4464 /* go up and assign all numbers which are not assigned
4465 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004466 for (match = match->cp_next;
4467 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004468 match = match->cp_next)
4469 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 else /* BACKWARD */
4472 {
4473 /* search forwards (upwards) for the first valid (!= -1)
4474 * number. This should normally succeed already at the
4475 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004476 for (match = compl_curr_match->cp_next; match != NULL
4477 && match != compl_first_match;
4478 match = match->cp_next)
4479 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004481 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 break;
4483 }
4484 if (match != NULL)
4485 /* go down and assign all numbers which are not
4486 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004487 for (match = match->cp_prev; match
4488 && match->cp_number == -1;
4489 match = match->cp_prev)
4490 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 }
4493
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004494 /* The match should always have a sequence number now, this is
4495 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004496 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
4498 /* Space for 10 text chars. + 2x10-digit no.s */
4499 static char_u match_ref[31];
4500
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004503 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004506 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004507 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 edit_submode_extra = match_ref;
4509 edit_submode_highl = HLF_R;
4510 if (dollar_vcol)
4511 curs_columns(FALSE);
4512 }
4513 }
4514 }
4515
4516 /* Show a message about what (completion) mode we're in. */
4517 showmode();
4518 if (edit_submode_extra != NULL)
4519 {
4520 if (!p_smd)
4521 msg_attr(edit_submode_extra,
4522 edit_submode_highl < HLF_COUNT
4523 ? hl_attr(edit_submode_highl) : 0);
4524 }
4525 else
4526 msg_clr_cmdline(); /* necessary for "noshowmode" */
4527
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004528 ins_compl_show_pum();
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 return OK;
4531}
4532
4533/*
4534 * Looks in the first "len" chars. of "src" for search-metachars.
4535 * If dest is not NULL the chars. are copied there quoting (with
4536 * a backslash) the metachars, and dest would be NUL terminated.
4537 * Returns the length (needed) of dest
4538 */
4539 static int
4540quote_meta(dest, src, len)
4541 char_u *dest;
4542 char_u *src;
4543 int len;
4544{
4545 int m;
4546
4547 for (m = len; --len >= 0; src++)
4548 {
4549 switch (*src)
4550 {
4551 case '.':
4552 case '*':
4553 case '[':
4554 if (ctrl_x_mode == CTRL_X_DICTIONARY
4555 || ctrl_x_mode == CTRL_X_THESAURUS)
4556 break;
4557 case '~':
4558 if (!p_magic) /* quote these only if magic is set */
4559 break;
4560 case '\\':
4561 if (ctrl_x_mode == CTRL_X_DICTIONARY
4562 || ctrl_x_mode == CTRL_X_THESAURUS)
4563 break;
4564 case '^': /* currently it's not needed. */
4565 case '$':
4566 m++;
4567 if (dest != NULL)
4568 *dest++ = '\\';
4569 break;
4570 }
4571 if (dest != NULL)
4572 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004573# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 /* Copy remaining bytes of a multibyte character. */
4575 if (has_mbyte)
4576 {
4577 int i, mb_len;
4578
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004579 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (mb_len > 0 && len >= mb_len)
4581 for (i = 0; i < mb_len; ++i)
4582 {
4583 --len;
4584 ++src;
4585 if (dest != NULL)
4586 *dest++ = *src;
4587 }
4588 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 if (dest != NULL)
4592 *dest = NUL;
4593
4594 return m;
4595}
4596#endif /* FEAT_INS_EXPAND */
4597
4598/*
4599 * Next character is interpreted literally.
4600 * A one, two or three digit decimal number is interpreted as its byte value.
4601 * If one or two digits are entered, the next character is given to vungetc().
4602 * For Unicode a character > 255 may be returned.
4603 */
4604 int
4605get_literal()
4606{
4607 int cc;
4608 int nc;
4609 int i;
4610 int hex = FALSE;
4611 int octal = FALSE;
4612#ifdef FEAT_MBYTE
4613 int unicode = 0;
4614#endif
4615
4616 if (got_int)
4617 return Ctrl_C;
4618
4619#ifdef FEAT_GUI
4620 /*
4621 * In GUI there is no point inserting the internal code for a special key.
4622 * It is more useful to insert the string "<KEY>" instead. This would
4623 * probably be useful in a text window too, but it would not be
4624 * vi-compatible (maybe there should be an option for it?) -- webb
4625 */
4626 if (gui.in_use)
4627 ++allow_keys;
4628#endif
4629#ifdef USE_ON_FLY_SCROLL
4630 dont_scroll = TRUE; /* disallow scrolling here */
4631#endif
4632 ++no_mapping; /* don't map the next key hits */
4633 cc = 0;
4634 i = 0;
4635 for (;;)
4636 {
4637 do
4638 nc = safe_vgetc();
4639 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4640 || nc == K_HOR_SCROLLBAR);
4641#ifdef FEAT_CMDL_INFO
4642 if (!(State & CMDLINE)
4643# ifdef FEAT_MBYTE
4644 && MB_BYTE2LEN_CHECK(nc) == 1
4645# endif
4646 )
4647 add_to_showcmd(nc);
4648#endif
4649 if (nc == 'x' || nc == 'X')
4650 hex = TRUE;
4651 else if (nc == 'o' || nc == 'O')
4652 octal = TRUE;
4653#ifdef FEAT_MBYTE
4654 else if (nc == 'u' || nc == 'U')
4655 unicode = nc;
4656#endif
4657 else
4658 {
4659 if (hex
4660#ifdef FEAT_MBYTE
4661 || unicode != 0
4662#endif
4663 )
4664 {
4665 if (!vim_isxdigit(nc))
4666 break;
4667 cc = cc * 16 + hex2nr(nc);
4668 }
4669 else if (octal)
4670 {
4671 if (nc < '0' || nc > '7')
4672 break;
4673 cc = cc * 8 + nc - '0';
4674 }
4675 else
4676 {
4677 if (!VIM_ISDIGIT(nc))
4678 break;
4679 cc = cc * 10 + nc - '0';
4680 }
4681
4682 ++i;
4683 }
4684
4685 if (cc > 255
4686#ifdef FEAT_MBYTE
4687 && unicode == 0
4688#endif
4689 )
4690 cc = 255; /* limit range to 0-255 */
4691 nc = 0;
4692
4693 if (hex) /* hex: up to two chars */
4694 {
4695 if (i >= 2)
4696 break;
4697 }
4698#ifdef FEAT_MBYTE
4699 else if (unicode) /* Unicode: up to four or eight chars */
4700 {
4701 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4702 break;
4703 }
4704#endif
4705 else if (i >= 3) /* decimal or octal: up to three chars */
4706 break;
4707 }
4708 if (i == 0) /* no number entered */
4709 {
4710 if (nc == K_ZERO) /* NUL is stored as NL */
4711 {
4712 cc = '\n';
4713 nc = 0;
4714 }
4715 else
4716 {
4717 cc = nc;
4718 nc = 0;
4719 }
4720 }
4721
4722 if (cc == 0) /* NUL is stored as NL */
4723 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004724#ifdef FEAT_MBYTE
4725 if (enc_dbcs && (cc & 0xff) == 0)
4726 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4727 second byte will cause trouble! */
4728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 --no_mapping;
4731#ifdef FEAT_GUI
4732 if (gui.in_use)
4733 --allow_keys;
4734#endif
4735 if (nc)
4736 vungetc(nc);
4737 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4738 return cc;
4739}
4740
4741/*
4742 * Insert character, taking care of special keys and mod_mask
4743 */
4744 static void
4745insert_special(c, allow_modmask, ctrlv)
4746 int c;
4747 int allow_modmask;
4748 int ctrlv; /* c was typed after CTRL-V */
4749{
4750 char_u *p;
4751 int len;
4752
4753 /*
4754 * Special function key, translate into "<Key>". Up to the last '>' is
4755 * inserted with ins_str(), so as not to replace characters in replace
4756 * mode.
4757 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4758 * unless 'allow_modmask' is TRUE.
4759 */
4760#ifdef MACOS
4761 /* Command-key never produces a normal key */
4762 if (mod_mask & MOD_MASK_CMD)
4763 allow_modmask = TRUE;
4764#endif
4765 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4766 {
4767 p = get_special_key_name(c, mod_mask);
4768 len = (int)STRLEN(p);
4769 c = p[len - 1];
4770 if (len > 2)
4771 {
4772 if (stop_arrow() == FAIL)
4773 return;
4774 p[len - 1] = NUL;
4775 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004776 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 ctrlv = FALSE;
4778 }
4779 }
4780 if (stop_arrow() == OK)
4781 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4782}
4783
4784/*
4785 * Special characters in this context are those that need processing other
4786 * than the simple insertion that can be performed here. This includes ESC
4787 * which terminates the insert, and CR/NL which need special processing to
4788 * open up a new line. This routine tries to optimize insertions performed by
4789 * the "redo", "undo" or "put" commands, so it needs to know when it should
4790 * stop and defer processing to the "normal" mechanism.
4791 * '0' and '^' are special, because they can be followed by CTRL-D.
4792 */
4793#ifdef EBCDIC
4794# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4795#else
4796# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4797#endif
4798
4799#ifdef FEAT_MBYTE
4800# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4801#else
4802# define WHITECHAR(cc) vim_iswhite(cc)
4803#endif
4804
4805 void
4806insertchar(c, flags, second_indent)
4807 int c; /* character to insert or NUL */
4808 int flags; /* INSCHAR_FORMAT, etc. */
4809 int second_indent; /* indent for second line if >= 0 */
4810{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int textwidth;
4812#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
4817 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4818 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 /*
4821 * Try to break the line in two or more pieces when:
4822 * - Always do this if we have been called to do formatting only.
4823 * - Always do this when 'formatoptions' has the 'a' flag and the line
4824 * ends in white space.
4825 * - Otherwise:
4826 * - Don't do this if inserting a blank
4827 * - Don't do this if an existing character is being replaced, unless
4828 * we're in VREPLACE mode.
4829 * - Do this if the cursor is not on the line where insert started
4830 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4831 * before the insert.
4832 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4833 * before 'textwidth'
4834 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004835 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 && ((flags & INSCHAR_FORMAT)
4837 || (!vim_iswhite(c)
4838 && !((State & REPLACE_FLAG)
4839#ifdef FEAT_VREPLACE
4840 && !(State & VREPLACE_FLAG)
4841#endif
4842 && *ml_get_cursor() != NUL)
4843 && (curwin->w_cursor.lnum != Insstart.lnum
4844 || ((!has_format_option(FO_INS_LONG)
4845 || Insstart_textlen <= (colnr_T)textwidth)
4846 && (!fo_ins_blank
4847 || Insstart_blank_vcol <= (colnr_T)textwidth
4848 ))))))
4849 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004850 /* Format with 'formatexpr' when it's set. Use internal formatting
4851 * when 'formatexpr' isn't set or it returns non-zero. */
4852#if defined(FEAT_EVAL)
4853 if (*curbuf->b_p_fex == NUL
4854 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004856 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (c == NUL) /* only formatting was wanted */
4860 return;
4861
4862#ifdef FEAT_COMMENTS
4863 /* Check whether this character should end a comment. */
4864 if (did_ai && (int)c == end_comment_pending)
4865 {
4866 char_u *line;
4867 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4868 int middle_len, end_len;
4869 int i;
4870
4871 /*
4872 * Need to remove existing (middle) comment leader and insert end
4873 * comment leader. First, check what comment leader we can find.
4874 */
4875 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4876 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4877 {
4878 /* Skip middle-comment string */
4879 while (*p && p[-1] != ':') /* find end of middle flags */
4880 ++p;
4881 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4882 /* Don't count trailing white space for middle_len */
4883 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4884 --middle_len;
4885
4886 /* Find the end-comment string */
4887 while (*p && p[-1] != ':') /* find end of end flags */
4888 ++p;
4889 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4890
4891 /* Skip white space before the cursor */
4892 i = curwin->w_cursor.col;
4893 while (--i >= 0 && vim_iswhite(line[i]))
4894 ;
4895 i++;
4896
4897 /* Skip to before the middle leader */
4898 i -= middle_len;
4899
4900 /* Check some expected things before we go on */
4901 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4902 {
4903 /* Backspace over all the stuff we want to replace */
4904 backspace_until_column(i);
4905
4906 /*
4907 * Insert the end-comment string, except for the last
4908 * character, which will get inserted as normal later.
4909 */
4910 ins_bytes_len(lead_end, end_len - 1);
4911 }
4912 }
4913 }
4914 end_comment_pending = NUL;
4915#endif
4916
4917 did_ai = FALSE;
4918#ifdef FEAT_SMARTINDENT
4919 did_si = FALSE;
4920 can_si = FALSE;
4921 can_si_back = FALSE;
4922#endif
4923
4924 /*
4925 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4926 * This speeds up normal text input considerably.
4927 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4928 * need to re-indent at a ':', or any other character (but not what
4929 * 'paste' is set)..
4930 */
4931#ifdef USE_ON_FLY_SCROLL
4932 dont_scroll = FALSE; /* allow scrolling here */
4933#endif
4934
4935 if ( !ISSPECIAL(c)
4936#ifdef FEAT_MBYTE
4937 && (!has_mbyte || (*mb_char2len)(c) == 1)
4938#endif
4939 && vpeekc() != NUL
4940 && !(State & REPLACE_FLAG)
4941#ifdef FEAT_CINDENT
4942 && !cindent_on()
4943#endif
4944#ifdef FEAT_RIGHTLEFT
4945 && !p_ri
4946#endif
4947 )
4948 {
4949#define INPUT_BUFLEN 100
4950 char_u buf[INPUT_BUFLEN + 1];
4951 int i;
4952 colnr_T virtcol = 0;
4953
4954 buf[0] = c;
4955 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004956 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 virtcol = get_nolist_virtcol();
4958 /*
4959 * Stop the string when:
4960 * - no more chars available
4961 * - finding a special character (command key)
4962 * - buffer is full
4963 * - running into the 'textwidth' boundary
4964 * - need to check for abbreviation: A non-word char after a word-char
4965 */
4966 while ( (c = vpeekc()) != NUL
4967 && !ISSPECIAL(c)
4968#ifdef FEAT_MBYTE
4969 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4970#endif
4971 && i < INPUT_BUFLEN
4972 && (textwidth == 0
4973 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4974 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4975 {
4976#ifdef FEAT_RIGHTLEFT
4977 c = vgetc();
4978 if (p_hkmap && KeyTyped)
4979 c = hkmap(c); /* Hebrew mode mapping */
4980# ifdef FEAT_FKMAP
4981 if (p_fkmap && KeyTyped)
4982 c = fkmap(c); /* Farsi mode mapping */
4983# endif
4984 buf[i++] = c;
4985#else
4986 buf[i++] = vgetc();
4987#endif
4988 }
4989
4990#ifdef FEAT_DIGRAPHS
4991 do_digraph(-1); /* clear digraphs */
4992 do_digraph(buf[i-1]); /* may be the start of a digraph */
4993#endif
4994 buf[i] = NUL;
4995 ins_str(buf);
4996 if (flags & INSCHAR_CTRLV)
4997 {
4998 redo_literal(*buf);
4999 i = 1;
5000 }
5001 else
5002 i = 0;
5003 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005004 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 else
5007 {
5008#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005009 int cc;
5010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5012 {
5013 char_u buf[MB_MAXBYTES + 1];
5014
5015 (*mb_char2bytes)(c, buf);
5016 buf[cc] = NUL;
5017 ins_char_bytes(buf, cc);
5018 AppendCharToRedobuff(c);
5019 }
5020 else
5021#endif
5022 {
5023 ins_char(c);
5024 if (flags & INSCHAR_CTRLV)
5025 redo_literal(c);
5026 else
5027 AppendCharToRedobuff(c);
5028 }
5029 }
5030}
5031
5032/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005033 * Format text at the current insert position.
5034 */
5035 static void
5036internal_format(textwidth, second_indent, flags, format_only)
5037 int textwidth;
5038 int second_indent;
5039 int flags;
5040 int format_only;
5041{
5042 int cc;
5043 int save_char = NUL;
5044 int haveto_redraw = FALSE;
5045 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5046#ifdef FEAT_MBYTE
5047 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5048#endif
5049 int fo_white_par = has_format_option(FO_WHITE_PAR);
5050 int first_line = TRUE;
5051#ifdef FEAT_COMMENTS
5052 colnr_T leader_len;
5053 int no_leader = FALSE;
5054 int do_comments = (flags & INSCHAR_DO_COM);
5055#endif
5056
5057 /*
5058 * When 'ai' is off we don't want a space under the cursor to be
5059 * deleted. Replace it with an 'x' temporarily.
5060 */
5061 if (!curbuf->b_p_ai)
5062 {
5063 cc = gchar_cursor();
5064 if (vim_iswhite(cc))
5065 {
5066 save_char = cc;
5067 pchar_cursor('x');
5068 }
5069 }
5070
5071 /*
5072 * Repeat breaking lines, until the current line is not too long.
5073 */
5074 while (!got_int)
5075 {
5076 int startcol; /* Cursor column at entry */
5077 int wantcol; /* column at textwidth border */
5078 int foundcol; /* column for start of spaces */
5079 int end_foundcol = 0; /* column for start of word */
5080 colnr_T len;
5081 colnr_T virtcol;
5082#ifdef FEAT_VREPLACE
5083 int orig_col = 0;
5084 char_u *saved_text = NULL;
5085#endif
5086 colnr_T col;
5087
5088 virtcol = get_nolist_virtcol();
5089 if (virtcol < (colnr_T)textwidth)
5090 break;
5091
5092#ifdef FEAT_COMMENTS
5093 if (no_leader)
5094 do_comments = FALSE;
5095 else if (!(flags & INSCHAR_FORMAT)
5096 && has_format_option(FO_WRAP_COMS))
5097 do_comments = TRUE;
5098
5099 /* Don't break until after the comment leader */
5100 if (do_comments)
5101 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5102 else
5103 leader_len = 0;
5104
5105 /* If the line doesn't start with a comment leader, then don't
5106 * start one in a following broken line. Avoids that a %word
5107 * moved to the start of the next line causes all following lines
5108 * to start with %. */
5109 if (leader_len == 0)
5110 no_leader = TRUE;
5111#endif
5112 if (!(flags & INSCHAR_FORMAT)
5113#ifdef FEAT_COMMENTS
5114 && leader_len == 0
5115#endif
5116 && !has_format_option(FO_WRAP))
5117
5118 {
5119 textwidth = 0;
5120 break;
5121 }
5122 if ((startcol = curwin->w_cursor.col) == 0)
5123 break;
5124
5125 /* find column of textwidth border */
5126 coladvance((colnr_T)textwidth);
5127 wantcol = curwin->w_cursor.col;
5128
5129 curwin->w_cursor.col = startcol - 1;
5130#ifdef FEAT_MBYTE
5131 /* Correct cursor for multi-byte character. */
5132 if (has_mbyte)
5133 mb_adjust_cursor();
5134#endif
5135 foundcol = 0;
5136
5137 /*
5138 * Find position to break at.
5139 * Stop at first entered white when 'formatoptions' has 'v'
5140 */
5141 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5142 || curwin->w_cursor.lnum != Insstart.lnum
5143 || curwin->w_cursor.col >= Insstart.col)
5144 {
5145 cc = gchar_cursor();
5146 if (WHITECHAR(cc))
5147 {
5148 /* remember position of blank just before text */
5149 end_foundcol = curwin->w_cursor.col;
5150
5151 /* find start of sequence of blanks */
5152 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5153 {
5154 dec_cursor();
5155 cc = gchar_cursor();
5156 }
5157 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5158 break; /* only spaces in front of text */
5159#ifdef FEAT_COMMENTS
5160 /* Don't break until after the comment leader */
5161 if (curwin->w_cursor.col < leader_len)
5162 break;
5163#endif
5164 if (has_format_option(FO_ONE_LETTER))
5165 {
5166 /* do not break after one-letter words */
5167 if (curwin->w_cursor.col == 0)
5168 break; /* one-letter word at begin */
5169
5170 col = curwin->w_cursor.col;
5171 dec_cursor();
5172 cc = gchar_cursor();
5173
5174 if (WHITECHAR(cc))
5175 continue; /* one-letter, continue */
5176 curwin->w_cursor.col = col;
5177 }
5178#ifdef FEAT_MBYTE
5179 if (has_mbyte)
5180 foundcol = curwin->w_cursor.col
5181 + (*mb_ptr2len)(ml_get_cursor());
5182 else
5183#endif
5184 foundcol = curwin->w_cursor.col + 1;
5185 if (curwin->w_cursor.col < (colnr_T)wantcol)
5186 break;
5187 }
5188#ifdef FEAT_MBYTE
5189 else if (cc >= 0x100 && fo_multibyte
5190 && curwin->w_cursor.col <= (colnr_T)wantcol)
5191 {
5192 /* Break after or before a multi-byte character. */
5193 foundcol = curwin->w_cursor.col;
5194 if (curwin->w_cursor.col < (colnr_T)wantcol)
5195 foundcol += (*mb_char2len)(cc);
5196 end_foundcol = foundcol;
5197 break;
5198 }
5199#endif
5200 if (curwin->w_cursor.col == 0)
5201 break;
5202 dec_cursor();
5203 }
5204
5205 if (foundcol == 0) /* no spaces, cannot break line */
5206 {
5207 curwin->w_cursor.col = startcol;
5208 break;
5209 }
5210
5211 /* Going to break the line, remove any "$" now. */
5212 undisplay_dollar();
5213
5214 /*
5215 * Offset between cursor position and line break is used by replace
5216 * stack functions. VREPLACE does not use this, and backspaces
5217 * over the text instead.
5218 */
5219#ifdef FEAT_VREPLACE
5220 if (State & VREPLACE_FLAG)
5221 orig_col = startcol; /* Will start backspacing from here */
5222 else
5223#endif
5224 replace_offset = startcol - end_foundcol - 1;
5225
5226 /*
5227 * adjust startcol for spaces that will be deleted and
5228 * characters that will remain on top line
5229 */
5230 curwin->w_cursor.col = foundcol;
5231 while (cc = gchar_cursor(), WHITECHAR(cc))
5232 inc_cursor();
5233 startcol -= curwin->w_cursor.col;
5234 if (startcol < 0)
5235 startcol = 0;
5236
5237#ifdef FEAT_VREPLACE
5238 if (State & VREPLACE_FLAG)
5239 {
5240 /*
5241 * In VREPLACE mode, we will backspace over the text to be
5242 * wrapped, so save a copy now to put on the next line.
5243 */
5244 saved_text = vim_strsave(ml_get_cursor());
5245 curwin->w_cursor.col = orig_col;
5246 if (saved_text == NULL)
5247 break; /* Can't do it, out of memory */
5248 saved_text[startcol] = NUL;
5249
5250 /* Backspace over characters that will move to the next line */
5251 if (!fo_white_par)
5252 backspace_until_column(foundcol);
5253 }
5254 else
5255#endif
5256 {
5257 /* put cursor after pos. to break line */
5258 if (!fo_white_par)
5259 curwin->w_cursor.col = foundcol;
5260 }
5261
5262 /*
5263 * Split the line just before the margin.
5264 * Only insert/delete lines, but don't really redraw the window.
5265 */
5266 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5267 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5268#ifdef FEAT_COMMENTS
5269 + (do_comments ? OPENLINE_DO_COM : 0)
5270#endif
5271 , old_indent);
5272 old_indent = 0;
5273
5274 replace_offset = 0;
5275 if (first_line)
5276 {
5277 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5278 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5279 if (second_indent >= 0)
5280 {
5281#ifdef FEAT_VREPLACE
5282 if (State & VREPLACE_FLAG)
5283 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5284 else
5285#endif
5286 (void)set_indent(second_indent, SIN_CHANGED);
5287 }
5288 first_line = FALSE;
5289 }
5290
5291#ifdef FEAT_VREPLACE
5292 if (State & VREPLACE_FLAG)
5293 {
5294 /*
5295 * In VREPLACE mode we have backspaced over the text to be
5296 * moved, now we re-insert it into the new line.
5297 */
5298 ins_bytes(saved_text);
5299 vim_free(saved_text);
5300 }
5301 else
5302#endif
5303 {
5304 /*
5305 * Check if cursor is not past the NUL off the line, cindent
5306 * may have added or removed indent.
5307 */
5308 curwin->w_cursor.col += startcol;
5309 len = (colnr_T)STRLEN(ml_get_curline());
5310 if (curwin->w_cursor.col > len)
5311 curwin->w_cursor.col = len;
5312 }
5313
5314 haveto_redraw = TRUE;
5315#ifdef FEAT_CINDENT
5316 can_cindent = TRUE;
5317#endif
5318 /* moved the cursor, don't autoindent or cindent now */
5319 did_ai = FALSE;
5320#ifdef FEAT_SMARTINDENT
5321 did_si = FALSE;
5322 can_si = FALSE;
5323 can_si_back = FALSE;
5324#endif
5325 line_breakcheck();
5326 }
5327
5328 if (save_char != NUL) /* put back space after cursor */
5329 pchar_cursor(save_char);
5330
5331 if (!format_only && haveto_redraw)
5332 {
5333 update_topline();
5334 redraw_curbuf_later(VALID);
5335 }
5336}
5337
5338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 * Called after inserting or deleting text: When 'formatoptions' includes the
5340 * 'a' flag format from the current line until the end of the paragraph.
5341 * Keep the cursor at the same position relative to the text.
5342 * The caller must have saved the cursor line for undo, following ones will be
5343 * saved here.
5344 */
5345 void
5346auto_format(trailblank, prev_line)
5347 int trailblank; /* when TRUE also format with trailing blank */
5348 int prev_line; /* may start in previous line */
5349{
5350 pos_T pos;
5351 colnr_T len;
5352 char_u *old;
5353 char_u *new, *pnew;
5354 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005355 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 if (!has_format_option(FO_AUTO))
5358 return;
5359
5360 pos = curwin->w_cursor;
5361 old = ml_get_curline();
5362
5363 /* may remove added space */
5364 check_auto_format(FALSE);
5365
5366 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5367 * user might insert normal text next. Also skip formatting when "1" is
5368 * in 'formatoptions' and there is a single character before the cursor.
5369 * Otherwise the line would be broken and when typing another non-white
5370 * next they are not joined back together. */
5371 wasatend = (pos.col == STRLEN(old));
5372 if (*old != NUL && !trailblank && wasatend)
5373 {
5374 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005375 cc = gchar_cursor();
5376 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5377 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005379 cc = gchar_cursor();
5380 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 curwin->w_cursor = pos;
5383 return;
5384 }
5385 curwin->w_cursor = pos;
5386 }
5387
5388#ifdef FEAT_COMMENTS
5389 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5390 * comments. */
5391 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5392 && get_leader_len(old, NULL, FALSE) == 0)
5393 return;
5394#endif
5395
5396 /*
5397 * May start formatting in a previous line, so that after "x" a word is
5398 * moved to the previous line if it fits there now. Only when this is not
5399 * the start of a paragraph.
5400 */
5401 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5402 {
5403 --curwin->w_cursor.lnum;
5404 if (u_save_cursor() == FAIL)
5405 return;
5406 }
5407
5408 /*
5409 * Do the formatting and restore the cursor position. "saved_cursor" will
5410 * be adjusted for the text formatting.
5411 */
5412 saved_cursor = pos;
5413 format_lines((linenr_T)-1);
5414 curwin->w_cursor = saved_cursor;
5415 saved_cursor.lnum = 0;
5416
5417 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5418 {
5419 /* "cannot happen" */
5420 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5421 coladvance((colnr_T)MAXCOL);
5422 }
5423 else
5424 check_cursor_col();
5425
5426 /* Insert mode: If the cursor is now after the end of the line while it
5427 * previously wasn't, the line was broken. Because of the rule above we
5428 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5429 * formatted. */
5430 if (!wasatend && has_format_option(FO_WHITE_PAR))
5431 {
5432 new = ml_get_curline();
5433 len = STRLEN(new);
5434 if (curwin->w_cursor.col == len)
5435 {
5436 pnew = vim_strnsave(new, len + 2);
5437 pnew[len] = ' ';
5438 pnew[len + 1] = NUL;
5439 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5440 /* remove the space later */
5441 did_add_space = TRUE;
5442 }
5443 else
5444 /* may remove added space */
5445 check_auto_format(FALSE);
5446 }
5447
5448 check_cursor();
5449}
5450
5451/*
5452 * When an extra space was added to continue a paragraph for auto-formatting,
5453 * delete it now. The space must be under the cursor, just after the insert
5454 * position.
5455 */
5456 static void
5457check_auto_format(end_insert)
5458 int end_insert; /* TRUE when ending Insert mode */
5459{
5460 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005461 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 if (did_add_space)
5464 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005465 cc = gchar_cursor();
5466 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 /* Somehow the space was removed already. */
5468 did_add_space = FALSE;
5469 else
5470 {
5471 if (!end_insert)
5472 {
5473 inc_cursor();
5474 c = gchar_cursor();
5475 dec_cursor();
5476 }
5477 if (c != NUL)
5478 {
5479 /* The space is no longer at the end of the line, delete it. */
5480 del_char(FALSE);
5481 did_add_space = FALSE;
5482 }
5483 }
5484 }
5485}
5486
5487/*
5488 * Find out textwidth to be used for formatting:
5489 * if 'textwidth' option is set, use it
5490 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5491 * if invalid value, use 0.
5492 * Set default to window width (maximum 79) for "gq" operator.
5493 */
5494 int
5495comp_textwidth(ff)
5496 int ff; /* force formatting (for "Q" command) */
5497{
5498 int textwidth;
5499
5500 textwidth = curbuf->b_p_tw;
5501 if (textwidth == 0 && curbuf->b_p_wm)
5502 {
5503 /* The width is the window width minus 'wrapmargin' minus all the
5504 * things that add to the margin. */
5505 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5506#ifdef FEAT_CMDWIN
5507 if (cmdwin_type != 0)
5508 textwidth -= 1;
5509#endif
5510#ifdef FEAT_FOLDING
5511 textwidth -= curwin->w_p_fdc;
5512#endif
5513#ifdef FEAT_SIGNS
5514 if (curwin->w_buffer->b_signlist != NULL
5515# ifdef FEAT_NETBEANS_INTG
5516 || usingNetbeans
5517# endif
5518 )
5519 textwidth -= 1;
5520#endif
5521 if (curwin->w_p_nu)
5522 textwidth -= 8;
5523 }
5524 if (textwidth < 0)
5525 textwidth = 0;
5526 if (ff && textwidth == 0)
5527 {
5528 textwidth = W_WIDTH(curwin) - 1;
5529 if (textwidth > 79)
5530 textwidth = 79;
5531 }
5532 return textwidth;
5533}
5534
5535/*
5536 * Put a character in the redo buffer, for when just after a CTRL-V.
5537 */
5538 static void
5539redo_literal(c)
5540 int c;
5541{
5542 char_u buf[10];
5543
5544 /* Only digits need special treatment. Translate them into a string of
5545 * three digits. */
5546 if (VIM_ISDIGIT(c))
5547 {
5548 sprintf((char *)buf, "%03d", c);
5549 AppendToRedobuff(buf);
5550 }
5551 else
5552 AppendCharToRedobuff(c);
5553}
5554
5555/*
5556 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005557 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
5559 static void
5560start_arrow(end_insert_pos)
5561 pos_T *end_insert_pos;
5562{
5563 if (!arrow_used) /* something has been inserted */
5564 {
5565 AppendToRedobuff(ESC_STR);
5566 stop_insert(end_insert_pos, FALSE);
5567 arrow_used = TRUE; /* this means we stopped the current insert */
5568 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005569#ifdef FEAT_SYN_HL
5570 check_spell_redraw();
5571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572}
5573
Bram Moolenaar217ad922005-03-20 22:37:15 +00005574#ifdef FEAT_SYN_HL
5575/*
5576 * If we skipped highlighting word at cursor, do it now.
5577 * It may be skipped again, thus reset spell_redraw_lnum first.
5578 */
5579 static void
5580check_spell_redraw()
5581{
5582 if (spell_redraw_lnum != 0)
5583 {
5584 linenr_T lnum = spell_redraw_lnum;
5585
5586 spell_redraw_lnum = 0;
5587 redrawWinline(lnum, FALSE);
5588 }
5589}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005590
5591/*
5592 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5593 * spelled word, if there is one.
5594 */
5595 static void
5596spell_back_to_badword()
5597{
5598 pos_T tpos = curwin->w_cursor;
5599
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005600 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005601 if (curwin->w_cursor.col != tpos.col)
5602 start_arrow(&tpos);
5603}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005604#endif
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606/*
5607 * stop_arrow() is called before a change is made in insert mode.
5608 * If an arrow key has been used, start a new insertion.
5609 * Returns FAIL if undo is impossible, shouldn't insert then.
5610 */
5611 int
5612stop_arrow()
5613{
5614 if (arrow_used)
5615 {
5616 if (u_save_cursor() == OK)
5617 {
5618 arrow_used = FALSE;
5619 ins_need_undo = FALSE;
5620 }
5621 Insstart = curwin->w_cursor; /* new insertion starts here */
5622 Insstart_textlen = linetabsize(ml_get_curline());
5623 ai_col = 0;
5624#ifdef FEAT_VREPLACE
5625 if (State & VREPLACE_FLAG)
5626 {
5627 orig_line_count = curbuf->b_ml.ml_line_count;
5628 vr_lines_changed = 1;
5629 }
5630#endif
5631 ResetRedobuff();
5632 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005633 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635 else if (ins_need_undo)
5636 {
5637 if (u_save_cursor() == OK)
5638 ins_need_undo = FALSE;
5639 }
5640
5641#ifdef FEAT_FOLDING
5642 /* Always open fold at the cursor line when inserting something. */
5643 foldOpenCursor();
5644#endif
5645
5646 return (arrow_used || ins_need_undo ? FAIL : OK);
5647}
5648
5649/*
5650 * do a few things to stop inserting
5651 */
5652 static void
5653stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005654 pos_T *end_insert_pos; /* where insert ended */
5655 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005657 int cc;
5658 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 stop_redo_ins();
5661 replace_flush(); /* abandon replace stack */
5662
5663 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005664 * Save the inserted text for later redo with ^@ and CTRL-A.
5665 * Don't do it when "restart_edit" was set and nothing was inserted,
5666 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005668 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005669 if (did_restart_edit == 0 || (ptr != NULL
5670 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005671 {
5672 vim_free(last_insert);
5673 last_insert = ptr;
5674 last_insert_skip = new_insert_skip;
5675 }
5676 else
5677 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
5679 if (!arrow_used)
5680 {
5681 /* Auto-format now. It may seem strange to do this when stopping an
5682 * insertion (or moving the cursor), but it's required when appending
5683 * a line and having it end in a space. But only do it when something
5684 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005685 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005687 pos_T tpos = curwin->w_cursor;
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 /* When the cursor is at the end of the line after a space the
5690 * formatting will move it to the following word. Avoid that by
5691 * moving the cursor onto the space. */
5692 cc = 'x';
5693 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5694 {
5695 dec_cursor();
5696 cc = gchar_cursor();
5697 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005698 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700
5701 auto_format(TRUE, FALSE);
5702
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005703 if (vim_iswhite(cc))
5704 {
5705 if (gchar_cursor() != NUL)
5706 inc_cursor();
5707#ifdef FEAT_VIRTUALEDIT
5708 /* If the cursor is still at the same character, also keep
5709 * the "coladd". */
5710 if (gchar_cursor() == NUL
5711 && curwin->w_cursor.lnum == tpos.lnum
5712 && curwin->w_cursor.col == tpos.col)
5713 curwin->w_cursor.coladd = tpos.coladd;
5714#endif
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717
5718 /* If a space was inserted for auto-formatting, remove it now. */
5719 check_auto_format(TRUE);
5720
5721 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005722 * of the line, and put the cursor back.
5723 * Do this when ESC was used or moving the cursor up/down. */
5724 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5725 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005727 pos_T tpos = curwin->w_cursor;
5728
5729 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5731 --curwin->w_cursor.col;
5732 while (cc = gchar_cursor(), vim_iswhite(cc))
5733 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005734 if (curwin->w_cursor.lnum != tpos.lnum)
5735 curwin->w_cursor = tpos;
5736 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5738
5739#ifdef FEAT_VISUAL
5740 /* <C-S-Right> may have started Visual mode, adjust the position for
5741 * deleted characters. */
5742 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5743 {
5744 cc = STRLEN(ml_get_curline());
5745 if (VIsual.col > (colnr_T)cc)
5746 {
5747 VIsual.col = cc;
5748# ifdef FEAT_VIRTUALEDIT
5749 VIsual.coladd = 0;
5750# endif
5751 }
5752 }
5753#endif
5754 }
5755 }
5756 did_ai = FALSE;
5757#ifdef FEAT_SMARTINDENT
5758 did_si = FALSE;
5759 can_si = FALSE;
5760 can_si_back = FALSE;
5761#endif
5762
5763 /* set '[ and '] to the inserted text */
5764 curbuf->b_op_start = Insstart;
5765 curbuf->b_op_end = *end_insert_pos;
5766}
5767
5768/*
5769 * Set the last inserted text to a single character.
5770 * Used for the replace command.
5771 */
5772 void
5773set_last_insert(c)
5774 int c;
5775{
5776 char_u *s;
5777
5778 vim_free(last_insert);
5779#ifdef FEAT_MBYTE
5780 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5781#else
5782 last_insert = alloc(6);
5783#endif
5784 if (last_insert != NULL)
5785 {
5786 s = last_insert;
5787 /* Use the CTRL-V only when entering a special char */
5788 if (c < ' ' || c == DEL)
5789 *s++ = Ctrl_V;
5790 s = add_char2buf(c, s);
5791 *s++ = ESC;
5792 *s++ = NUL;
5793 last_insert_skip = 0;
5794 }
5795}
5796
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005797#if defined(EXITFREE) || defined(PROTO)
5798 void
5799free_last_insert()
5800{
5801 vim_free(last_insert);
5802 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005803 vim_free(compl_orig_text);
5804 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005805}
5806#endif
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808/*
5809 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5810 * and CSI. Handle multi-byte characters.
5811 * Returns a pointer to after the added bytes.
5812 */
5813 char_u *
5814add_char2buf(c, s)
5815 int c;
5816 char_u *s;
5817{
5818#ifdef FEAT_MBYTE
5819 char_u temp[MB_MAXBYTES];
5820 int i;
5821 int len;
5822
5823 len = (*mb_char2bytes)(c, temp);
5824 for (i = 0; i < len; ++i)
5825 {
5826 c = temp[i];
5827#endif
5828 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5829 if (c == K_SPECIAL)
5830 {
5831 *s++ = K_SPECIAL;
5832 *s++ = KS_SPECIAL;
5833 *s++ = KE_FILLER;
5834 }
5835#ifdef FEAT_GUI
5836 else if (c == CSI)
5837 {
5838 *s++ = CSI;
5839 *s++ = KS_EXTRA;
5840 *s++ = (int)KE_CSI;
5841 }
5842#endif
5843 else
5844 *s++ = c;
5845#ifdef FEAT_MBYTE
5846 }
5847#endif
5848 return s;
5849}
5850
5851/*
5852 * move cursor to start of line
5853 * if flags & BL_WHITE move to first non-white
5854 * if flags & BL_SOL move to first non-white if startofline is set,
5855 * otherwise keep "curswant" column
5856 * if flags & BL_FIX don't leave the cursor on a NUL.
5857 */
5858 void
5859beginline(flags)
5860 int flags;
5861{
5862 if ((flags & BL_SOL) && !p_sol)
5863 coladvance(curwin->w_curswant);
5864 else
5865 {
5866 curwin->w_cursor.col = 0;
5867#ifdef FEAT_VIRTUALEDIT
5868 curwin->w_cursor.coladd = 0;
5869#endif
5870
5871 if (flags & (BL_WHITE | BL_SOL))
5872 {
5873 char_u *ptr;
5874
5875 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5876 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5877 ++curwin->w_cursor.col;
5878 }
5879 curwin->w_set_curswant = TRUE;
5880 }
5881}
5882
5883/*
5884 * oneright oneleft cursor_down cursor_up
5885 *
5886 * Move one char {right,left,down,up}.
5887 * Doesn't move onto the NUL past the end of the line.
5888 * Return OK when successful, FAIL when we hit a line of file boundary.
5889 */
5890
5891 int
5892oneright()
5893{
5894 char_u *ptr;
5895#ifdef FEAT_MBYTE
5896 int l;
5897#endif
5898
5899#ifdef FEAT_VIRTUALEDIT
5900 if (virtual_active())
5901 {
5902 pos_T prevpos = curwin->w_cursor;
5903
5904 /* Adjust for multi-wide char (excluding TAB) */
5905 ptr = ml_get_cursor();
5906 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5907#ifdef FEAT_MBYTE
5908 (*mb_ptr2char)(ptr)
5909#else
5910 *ptr
5911#endif
5912 ))
5913 ? ptr2cells(ptr) : 1));
5914 curwin->w_set_curswant = TRUE;
5915 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5916 return (prevpos.col != curwin->w_cursor.col
5917 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5918 }
5919#endif
5920
5921 ptr = ml_get_cursor();
5922#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005923 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
5925 /* The character under the cursor is a multi-byte character, move
5926 * several bytes right, but don't end up on the NUL. */
5927 if (ptr[l] == NUL)
5928 return FAIL;
5929 curwin->w_cursor.col += l;
5930 }
5931 else
5932#endif
5933 {
5934 if (*ptr++ == NUL || *ptr == NUL)
5935 return FAIL;
5936 ++curwin->w_cursor.col;
5937 }
5938
5939 curwin->w_set_curswant = TRUE;
5940 return OK;
5941}
5942
5943 int
5944oneleft()
5945{
5946#ifdef FEAT_VIRTUALEDIT
5947 if (virtual_active())
5948 {
5949 int width;
5950 int v = getviscol();
5951
5952 if (v == 0)
5953 return FAIL;
5954
5955# ifdef FEAT_LINEBREAK
5956 /* We might get stuck on 'showbreak', skip over it. */
5957 width = 1;
5958 for (;;)
5959 {
5960 coladvance(v - width);
5961 /* getviscol() is slow, skip it when 'showbreak' is empty and
5962 * there are no multi-byte characters */
5963 if ((*p_sbr == NUL
5964# ifdef FEAT_MBYTE
5965 && !has_mbyte
5966# endif
5967 ) || getviscol() < v)
5968 break;
5969 ++width;
5970 }
5971# else
5972 coladvance(v - 1);
5973# endif
5974
5975 if (curwin->w_cursor.coladd == 1)
5976 {
5977 char_u *ptr;
5978
5979 /* Adjust for multi-wide char (not a TAB) */
5980 ptr = ml_get_cursor();
5981 if (*ptr != TAB && vim_isprintc(
5982# ifdef FEAT_MBYTE
5983 (*mb_ptr2char)(ptr)
5984# else
5985 *ptr
5986# endif
5987 ) && ptr2cells(ptr) > 1)
5988 curwin->w_cursor.coladd = 0;
5989 }
5990
5991 curwin->w_set_curswant = TRUE;
5992 return OK;
5993 }
5994#endif
5995
5996 if (curwin->w_cursor.col == 0)
5997 return FAIL;
5998
5999 curwin->w_set_curswant = TRUE;
6000 --curwin->w_cursor.col;
6001
6002#ifdef FEAT_MBYTE
6003 /* if the character on the left of the current cursor is a multi-byte
6004 * character, move to its first byte */
6005 if (has_mbyte)
6006 mb_adjust_cursor();
6007#endif
6008 return OK;
6009}
6010
6011 int
6012cursor_up(n, upd_topline)
6013 long n;
6014 int upd_topline; /* When TRUE: update topline */
6015{
6016 linenr_T lnum;
6017
6018 if (n > 0)
6019 {
6020 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006021 /* This fails if the cursor is already in the first line or the count
6022 * is larger than the line number and '-' is in 'cpoptions' */
6023 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 return FAIL;
6025 if (n >= lnum)
6026 lnum = 1;
6027 else
6028#ifdef FEAT_FOLDING
6029 if (hasAnyFolding(curwin))
6030 {
6031 /*
6032 * Count each sequence of folded lines as one logical line.
6033 */
6034 /* go to the the start of the current fold */
6035 (void)hasFolding(lnum, &lnum, NULL);
6036
6037 while (n--)
6038 {
6039 /* move up one line */
6040 --lnum;
6041 if (lnum <= 1)
6042 break;
6043 /* If we entered a fold, move to the beginning, unless in
6044 * Insert mode or when 'foldopen' contains "all": it will open
6045 * in a moment. */
6046 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6047 (void)hasFolding(lnum, &lnum, NULL);
6048 }
6049 if (lnum < 1)
6050 lnum = 1;
6051 }
6052 else
6053#endif
6054 lnum -= n;
6055 curwin->w_cursor.lnum = lnum;
6056 }
6057
6058 /* try to advance to the column we want to be at */
6059 coladvance(curwin->w_curswant);
6060
6061 if (upd_topline)
6062 update_topline(); /* make sure curwin->w_topline is valid */
6063
6064 return OK;
6065}
6066
6067/*
6068 * Cursor down a number of logical lines.
6069 */
6070 int
6071cursor_down(n, upd_topline)
6072 long n;
6073 int upd_topline; /* When TRUE: update topline */
6074{
6075 linenr_T lnum;
6076
6077 if (n > 0)
6078 {
6079 lnum = curwin->w_cursor.lnum;
6080#ifdef FEAT_FOLDING
6081 /* Move to last line of fold, will fail if it's the end-of-file. */
6082 (void)hasFolding(lnum, NULL, &lnum);
6083#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006084 /* This fails if the cursor is already in the last line or would move
6085 * beyound the last line and '-' is in 'cpoptions' */
6086 if (lnum >= curbuf->b_ml.ml_line_count
6087 || (lnum + n > curbuf->b_ml.ml_line_count
6088 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 return FAIL;
6090 if (lnum + n >= curbuf->b_ml.ml_line_count)
6091 lnum = curbuf->b_ml.ml_line_count;
6092 else
6093#ifdef FEAT_FOLDING
6094 if (hasAnyFolding(curwin))
6095 {
6096 linenr_T last;
6097
6098 /* count each sequence of folded lines as one logical line */
6099 while (n--)
6100 {
6101 if (hasFolding(lnum, NULL, &last))
6102 lnum = last + 1;
6103 else
6104 ++lnum;
6105 if (lnum >= curbuf->b_ml.ml_line_count)
6106 break;
6107 }
6108 if (lnum > curbuf->b_ml.ml_line_count)
6109 lnum = curbuf->b_ml.ml_line_count;
6110 }
6111 else
6112#endif
6113 lnum += n;
6114 curwin->w_cursor.lnum = lnum;
6115 }
6116
6117 /* try to advance to the column we want to be at */
6118 coladvance(curwin->w_curswant);
6119
6120 if (upd_topline)
6121 update_topline(); /* make sure curwin->w_topline is valid */
6122
6123 return OK;
6124}
6125
6126/*
6127 * Stuff the last inserted text in the read buffer.
6128 * Last_insert actually is a copy of the redo buffer, so we
6129 * first have to remove the command.
6130 */
6131 int
6132stuff_inserted(c, count, no_esc)
6133 int c; /* Command character to be inserted */
6134 long count; /* Repeat this many times */
6135 int no_esc; /* Don't add an ESC at the end */
6136{
6137 char_u *esc_ptr;
6138 char_u *ptr;
6139 char_u *last_ptr;
6140 char_u last = NUL;
6141
6142 ptr = get_last_insert();
6143 if (ptr == NULL)
6144 {
6145 EMSG(_(e_noinstext));
6146 return FAIL;
6147 }
6148
6149 /* may want to stuff the command character, to start Insert mode */
6150 if (c != NUL)
6151 stuffcharReadbuff(c);
6152 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6153 *esc_ptr = NUL; /* remove the ESC */
6154
6155 /* when the last char is either "0" or "^" it will be quoted if no ESC
6156 * comes after it OR if it will inserted more than once and "ptr"
6157 * starts with ^D. -- Acevedo
6158 */
6159 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6160 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6161 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6162 {
6163 last = *last_ptr;
6164 *last_ptr = NUL;
6165 }
6166
6167 do
6168 {
6169 stuffReadbuff(ptr);
6170 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6171 if (last)
6172 stuffReadbuff((char_u *)(last == '0'
6173 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6174 : IF_EB("\026^", CTRL_V_STR "^")));
6175 }
6176 while (--count > 0);
6177
6178 if (last)
6179 *last_ptr = last;
6180
6181 if (esc_ptr != NULL)
6182 *esc_ptr = ESC; /* put the ESC back */
6183
6184 /* may want to stuff a trailing ESC, to get out of Insert mode */
6185 if (!no_esc)
6186 stuffcharReadbuff(ESC);
6187
6188 return OK;
6189}
6190
6191 char_u *
6192get_last_insert()
6193{
6194 if (last_insert == NULL)
6195 return NULL;
6196 return last_insert + last_insert_skip;
6197}
6198
6199/*
6200 * Get last inserted string, and remove trailing <Esc>.
6201 * Returns pointer to allocated memory (must be freed) or NULL.
6202 */
6203 char_u *
6204get_last_insert_save()
6205{
6206 char_u *s;
6207 int len;
6208
6209 if (last_insert == NULL)
6210 return NULL;
6211 s = vim_strsave(last_insert + last_insert_skip);
6212 if (s != NULL)
6213 {
6214 len = (int)STRLEN(s);
6215 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6216 s[len - 1] = NUL;
6217 }
6218 return s;
6219}
6220
6221/*
6222 * Check the word in front of the cursor for an abbreviation.
6223 * Called when the non-id character "c" has been entered.
6224 * When an abbreviation is recognized it is removed from the text and
6225 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6226 */
6227 static int
6228echeck_abbr(c)
6229 int c;
6230{
6231 /* Don't check for abbreviation in paste mode, when disabled and just
6232 * after moving around with cursor keys. */
6233 if (p_paste || no_abbr || arrow_used)
6234 return FALSE;
6235
6236 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6237 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6238}
6239
6240/*
6241 * replace-stack functions
6242 *
6243 * When replacing characters, the replaced characters are remembered for each
6244 * new character. This is used to re-insert the old text when backspacing.
6245 *
6246 * There is a NUL headed list of characters for each character that is
6247 * currently in the file after the insertion point. When BS is used, one NUL
6248 * headed list is put back for the deleted character.
6249 *
6250 * For a newline, there are two NUL headed lists. One contains the characters
6251 * that the NL replaced. The extra one stores the characters after the cursor
6252 * that were deleted (always white space).
6253 *
6254 * Replace_offset is normally 0, in which case replace_push will add a new
6255 * character at the end of the stack. If replace_offset is not 0, that many
6256 * characters will be left on the stack above the newly inserted character.
6257 */
6258
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006259static char_u *replace_stack = NULL;
6260static long replace_stack_nr = 0; /* next entry in replace stack */
6261static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263 void
6264replace_push(c)
6265 int c; /* character that is replaced (NUL is none) */
6266{
6267 char_u *p;
6268
6269 if (replace_stack_nr < replace_offset) /* nothing to do */
6270 return;
6271 if (replace_stack_len <= replace_stack_nr)
6272 {
6273 replace_stack_len += 50;
6274 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6275 if (p == NULL) /* out of memory */
6276 {
6277 replace_stack_len -= 50;
6278 return;
6279 }
6280 if (replace_stack != NULL)
6281 {
6282 mch_memmove(p, replace_stack,
6283 (size_t)(replace_stack_nr * sizeof(char_u)));
6284 vim_free(replace_stack);
6285 }
6286 replace_stack = p;
6287 }
6288 p = replace_stack + replace_stack_nr - replace_offset;
6289 if (replace_offset)
6290 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6291 *p = c;
6292 ++replace_stack_nr;
6293}
6294
6295/*
6296 * call replace_push(c) with replace_offset set to the first NUL.
6297 */
6298 static void
6299replace_push_off(c)
6300 int c;
6301{
6302 char_u *p;
6303
6304 p = replace_stack + replace_stack_nr;
6305 for (replace_offset = 1; replace_offset < replace_stack_nr;
6306 ++replace_offset)
6307 if (*--p == NUL)
6308 break;
6309 replace_push(c);
6310 replace_offset = 0;
6311}
6312
6313/*
6314 * Pop one item from the replace stack.
6315 * return -1 if stack empty
6316 * return replaced character or NUL otherwise
6317 */
6318 static int
6319replace_pop()
6320{
6321 if (replace_stack_nr == 0)
6322 return -1;
6323 return (int)replace_stack[--replace_stack_nr];
6324}
6325
6326/*
6327 * Join the top two items on the replace stack. This removes to "off"'th NUL
6328 * encountered.
6329 */
6330 static void
6331replace_join(off)
6332 int off; /* offset for which NUL to remove */
6333{
6334 int i;
6335
6336 for (i = replace_stack_nr; --i >= 0; )
6337 if (replace_stack[i] == NUL && off-- <= 0)
6338 {
6339 --replace_stack_nr;
6340 mch_memmove(replace_stack + i, replace_stack + i + 1,
6341 (size_t)(replace_stack_nr - i));
6342 return;
6343 }
6344}
6345
6346/*
6347 * Pop bytes from the replace stack until a NUL is found, and insert them
6348 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6349 */
6350 static void
6351replace_pop_ins()
6352{
6353 int cc;
6354 int oldState = State;
6355
6356 State = NORMAL; /* don't want REPLACE here */
6357 while ((cc = replace_pop()) > 0)
6358 {
6359#ifdef FEAT_MBYTE
6360 mb_replace_pop_ins(cc);
6361#else
6362 ins_char(cc);
6363#endif
6364 dec_cursor();
6365 }
6366 State = oldState;
6367}
6368
6369#ifdef FEAT_MBYTE
6370/*
6371 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6372 * indicates a multi-byte char, pop the other bytes too.
6373 */
6374 static void
6375mb_replace_pop_ins(cc)
6376 int cc;
6377{
6378 int n;
6379 char_u buf[MB_MAXBYTES];
6380 int i;
6381 int c;
6382
6383 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6384 {
6385 buf[0] = cc;
6386 for (i = 1; i < n; ++i)
6387 buf[i] = replace_pop();
6388 ins_bytes_len(buf, n);
6389 }
6390 else
6391 ins_char(cc);
6392
6393 if (enc_utf8)
6394 /* Handle composing chars. */
6395 for (;;)
6396 {
6397 c = replace_pop();
6398 if (c == -1) /* stack empty */
6399 break;
6400 if ((n = MB_BYTE2LEN(c)) == 1)
6401 {
6402 /* Not a multi-byte char, put it back. */
6403 replace_push(c);
6404 break;
6405 }
6406 else
6407 {
6408 buf[0] = c;
6409 for (i = 1; i < n; ++i)
6410 buf[i] = replace_pop();
6411 if (utf_iscomposing(utf_ptr2char(buf)))
6412 ins_bytes_len(buf, n);
6413 else
6414 {
6415 /* Not a composing char, put it back. */
6416 for (i = n - 1; i >= 0; --i)
6417 replace_push(buf[i]);
6418 break;
6419 }
6420 }
6421 }
6422}
6423#endif
6424
6425/*
6426 * make the replace stack empty
6427 * (called when exiting replace mode)
6428 */
6429 static void
6430replace_flush()
6431{
6432 vim_free(replace_stack);
6433 replace_stack = NULL;
6434 replace_stack_len = 0;
6435 replace_stack_nr = 0;
6436}
6437
6438/*
6439 * Handle doing a BS for one character.
6440 * cc < 0: replace stack empty, just move cursor
6441 * cc == 0: character was inserted, delete it
6442 * cc > 0: character was replaced, put cc (first byte of original char) back
6443 * and check for more characters to be put back
6444 */
6445 static void
6446replace_do_bs()
6447{
6448 int cc;
6449#ifdef FEAT_VREPLACE
6450 int orig_len = 0;
6451 int ins_len;
6452 int orig_vcols = 0;
6453 colnr_T start_vcol;
6454 char_u *p;
6455 int i;
6456 int vcol;
6457#endif
6458
6459 cc = replace_pop();
6460 if (cc > 0)
6461 {
6462#ifdef FEAT_VREPLACE
6463 if (State & VREPLACE_FLAG)
6464 {
6465 /* Get the number of screen cells used by the character we are
6466 * going to delete. */
6467 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6468 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6469 }
6470#endif
6471#ifdef FEAT_MBYTE
6472 if (has_mbyte)
6473 {
6474 del_char(FALSE);
6475# ifdef FEAT_VREPLACE
6476 if (State & VREPLACE_FLAG)
6477 orig_len = STRLEN(ml_get_cursor());
6478# endif
6479 replace_push(cc);
6480 }
6481 else
6482#endif
6483 {
6484 pchar_cursor(cc);
6485#ifdef FEAT_VREPLACE
6486 if (State & VREPLACE_FLAG)
6487 orig_len = STRLEN(ml_get_cursor()) - 1;
6488#endif
6489 }
6490 replace_pop_ins();
6491
6492#ifdef FEAT_VREPLACE
6493 if (State & VREPLACE_FLAG)
6494 {
6495 /* Get the number of screen cells used by the inserted characters */
6496 p = ml_get_cursor();
6497 ins_len = STRLEN(p) - orig_len;
6498 vcol = start_vcol;
6499 for (i = 0; i < ins_len; ++i)
6500 {
6501 vcol += chartabsize(p + i, vcol);
6502#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006503 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504#endif
6505 }
6506 vcol -= start_vcol;
6507
6508 /* Delete spaces that were inserted after the cursor to keep the
6509 * text aligned. */
6510 curwin->w_cursor.col += ins_len;
6511 while (vcol > orig_vcols && gchar_cursor() == ' ')
6512 {
6513 del_char(FALSE);
6514 ++orig_vcols;
6515 }
6516 curwin->w_cursor.col -= ins_len;
6517 }
6518#endif
6519
6520 /* mark the buffer as changed and prepare for displaying */
6521 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6522 }
6523 else if (cc == 0)
6524 (void)del_char(FALSE);
6525}
6526
6527#ifdef FEAT_CINDENT
6528/*
6529 * Return TRUE if C-indenting is on.
6530 */
6531 static int
6532cindent_on()
6533{
6534 return (!p_paste && (curbuf->b_p_cin
6535# ifdef FEAT_EVAL
6536 || *curbuf->b_p_inde != NUL
6537# endif
6538 ));
6539}
6540#endif
6541
6542#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6543/*
6544 * Re-indent the current line, based on the current contents of it and the
6545 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6546 * confused what all the part that handles Control-T is doing that I'm not.
6547 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6548 */
6549
6550 void
6551fixthisline(get_the_indent)
6552 int (*get_the_indent) __ARGS((void));
6553{
6554 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6555 if (linewhite(curwin->w_cursor.lnum))
6556 did_ai = TRUE; /* delete the indent if the line stays empty */
6557}
6558
6559 void
6560fix_indent()
6561{
6562 if (p_paste)
6563 return;
6564# ifdef FEAT_LISP
6565 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6566 fixthisline(get_lisp_indent);
6567# endif
6568# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6569 else
6570# endif
6571# ifdef FEAT_CINDENT
6572 if (cindent_on())
6573 do_c_expr_indent();
6574# endif
6575}
6576
6577#endif
6578
6579#ifdef FEAT_CINDENT
6580/*
6581 * return TRUE if 'cinkeys' contains the key "keytyped",
6582 * when == '*': Only if key is preceded with '*' (indent before insert)
6583 * when == '!': Only if key is prededed with '!' (don't insert)
6584 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6585 *
6586 * "keytyped" can have a few special values:
6587 * KEY_OPEN_FORW
6588 * KEY_OPEN_BACK
6589 * KEY_COMPLETE just finished completion.
6590 *
6591 * If line_is_empty is TRUE accept keys with '0' before them.
6592 */
6593 int
6594in_cinkeys(keytyped, when, line_is_empty)
6595 int keytyped;
6596 int when;
6597 int line_is_empty;
6598{
6599 char_u *look;
6600 int try_match;
6601 int try_match_word;
6602 char_u *p;
6603 char_u *line;
6604 int icase;
6605 int i;
6606
6607#ifdef FEAT_EVAL
6608 if (*curbuf->b_p_inde != NUL)
6609 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6610 else
6611#endif
6612 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6613 while (*look)
6614 {
6615 /*
6616 * Find out if we want to try a match with this key, depending on
6617 * 'when' and a '*' or '!' before the key.
6618 */
6619 switch (when)
6620 {
6621 case '*': try_match = (*look == '*'); break;
6622 case '!': try_match = (*look == '!'); break;
6623 default: try_match = (*look != '*'); break;
6624 }
6625 if (*look == '*' || *look == '!')
6626 ++look;
6627
6628 /*
6629 * If there is a '0', only accept a match if the line is empty.
6630 * But may still match when typing last char of a word.
6631 */
6632 if (*look == '0')
6633 {
6634 try_match_word = try_match;
6635 if (!line_is_empty)
6636 try_match = FALSE;
6637 ++look;
6638 }
6639 else
6640 try_match_word = FALSE;
6641
6642 /*
6643 * does it look like a control character?
6644 */
6645 if (*look == '^'
6646#ifdef EBCDIC
6647 && (Ctrl_chr(look[1]) != 0)
6648#else
6649 && look[1] >= '?' && look[1] <= '_'
6650#endif
6651 )
6652 {
6653 if (try_match && keytyped == Ctrl_chr(look[1]))
6654 return TRUE;
6655 look += 2;
6656 }
6657 /*
6658 * 'o' means "o" command, open forward.
6659 * 'O' means "O" command, open backward.
6660 */
6661 else if (*look == 'o')
6662 {
6663 if (try_match && keytyped == KEY_OPEN_FORW)
6664 return TRUE;
6665 ++look;
6666 }
6667 else if (*look == 'O')
6668 {
6669 if (try_match && keytyped == KEY_OPEN_BACK)
6670 return TRUE;
6671 ++look;
6672 }
6673
6674 /*
6675 * 'e' means to check for "else" at start of line and just before the
6676 * cursor.
6677 */
6678 else if (*look == 'e')
6679 {
6680 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6681 {
6682 p = ml_get_curline();
6683 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6684 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6685 return TRUE;
6686 }
6687 ++look;
6688 }
6689
6690 /*
6691 * ':' only causes an indent if it is at the end of a label or case
6692 * statement, or when it was before typing the ':' (to fix
6693 * class::method for C++).
6694 */
6695 else if (*look == ':')
6696 {
6697 if (try_match && keytyped == ':')
6698 {
6699 p = ml_get_curline();
6700 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6701 return TRUE;
6702 if (curwin->w_cursor.col > 2
6703 && p[curwin->w_cursor.col - 1] == ':'
6704 && p[curwin->w_cursor.col - 2] == ':')
6705 {
6706 p[curwin->w_cursor.col - 1] = ' ';
6707 i = (cin_iscase(p) || cin_isscopedecl(p)
6708 || cin_islabel(30));
6709 p = ml_get_curline();
6710 p[curwin->w_cursor.col - 1] = ':';
6711 if (i)
6712 return TRUE;
6713 }
6714 }
6715 ++look;
6716 }
6717
6718
6719 /*
6720 * Is it a key in <>, maybe?
6721 */
6722 else if (*look == '<')
6723 {
6724 if (try_match)
6725 {
6726 /*
6727 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6728 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6729 * >, *, : and ! keys if they really really want to.
6730 */
6731 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6732 && keytyped == look[1])
6733 return TRUE;
6734
6735 if (keytyped == get_special_key_code(look + 1))
6736 return TRUE;
6737 }
6738 while (*look && *look != '>')
6739 look++;
6740 while (*look == '>')
6741 look++;
6742 }
6743
6744 /*
6745 * Is it a word: "=word"?
6746 */
6747 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6748 {
6749 ++look;
6750 if (*look == '~')
6751 {
6752 icase = TRUE;
6753 ++look;
6754 }
6755 else
6756 icase = FALSE;
6757 p = vim_strchr(look, ',');
6758 if (p == NULL)
6759 p = look + STRLEN(look);
6760 if ((try_match || try_match_word)
6761 && curwin->w_cursor.col >= (colnr_T)(p - look))
6762 {
6763 int match = FALSE;
6764
6765#ifdef FEAT_INS_EXPAND
6766 if (keytyped == KEY_COMPLETE)
6767 {
6768 char_u *s;
6769
6770 /* Just completed a word, check if it starts with "look".
6771 * search back for the start of a word. */
6772 line = ml_get_curline();
6773# ifdef FEAT_MBYTE
6774 if (has_mbyte)
6775 {
6776 char_u *n;
6777
6778 for (s = line + curwin->w_cursor.col; s > line; s = n)
6779 {
6780 n = mb_prevptr(line, s);
6781 if (!vim_iswordp(n))
6782 break;
6783 }
6784 }
6785 else
6786# endif
6787 for (s = line + curwin->w_cursor.col; s > line; --s)
6788 if (!vim_iswordc(s[-1]))
6789 break;
6790 if (s + (p - look) <= line + curwin->w_cursor.col
6791 && (icase
6792 ? MB_STRNICMP(s, look, p - look)
6793 : STRNCMP(s, look, p - look)) == 0)
6794 match = TRUE;
6795 }
6796 else
6797#endif
6798 /* TODO: multi-byte */
6799 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6800 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6801 {
6802 line = ml_get_cursor();
6803 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6804 || !vim_iswordc(line[-(p - look) - 1]))
6805 && (icase
6806 ? MB_STRNICMP(line - (p - look), look, p - look)
6807 : STRNCMP(line - (p - look), look, p - look))
6808 == 0)
6809 match = TRUE;
6810 }
6811 if (match && try_match_word && !try_match)
6812 {
6813 /* "0=word": Check if there are only blanks before the
6814 * word. */
6815 line = ml_get_curline();
6816 if ((int)(skipwhite(line) - line) !=
6817 (int)(curwin->w_cursor.col - (p - look)))
6818 match = FALSE;
6819 }
6820 if (match)
6821 return TRUE;
6822 }
6823 look = p;
6824 }
6825
6826 /*
6827 * ok, it's a boring generic character.
6828 */
6829 else
6830 {
6831 if (try_match && *look == keytyped)
6832 return TRUE;
6833 ++look;
6834 }
6835
6836 /*
6837 * Skip over ", ".
6838 */
6839 look = skip_to_option_part(look);
6840 }
6841 return FALSE;
6842}
6843#endif /* FEAT_CINDENT */
6844
6845#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6846/*
6847 * Map Hebrew keyboard when in hkmap mode.
6848 */
6849 int
6850hkmap(c)
6851 int c;
6852{
6853 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6854 {
6855 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6856 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6857 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6858 static char_u map[26] =
6859 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6860 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6861 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6862 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6863 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6864 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6865 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6866 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6867 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6868
6869 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6870 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6871 /* '-1'='sofit' */
6872 else if (c == 'x')
6873 return 'X';
6874 else if (c == 'q')
6875 return '\''; /* {geresh}={'} */
6876 else if (c == 246)
6877 return ' '; /* \"o --> ' ' for a german keyboard */
6878 else if (c == 228)
6879 return ' '; /* \"a --> ' ' -- / -- */
6880 else if (c == 252)
6881 return ' '; /* \"u --> ' ' -- / -- */
6882#ifdef EBCDIC
6883 else if (islower(c))
6884#else
6885 /* NOTE: islower() does not do the right thing for us on Linux so we
6886 * do this the same was as 5.7 and previous, so it works correctly on
6887 * all systems. Specifically, the e.g. Delete and Arrow keys are
6888 * munged and won't work if e.g. searching for Hebrew text.
6889 */
6890 else if (c >= 'a' && c <= 'z')
6891#endif
6892 return (int)(map[CharOrdLow(c)] + p_aleph);
6893 else
6894 return c;
6895 }
6896 else
6897 {
6898 switch (c)
6899 {
6900 case '`': return ';';
6901 case '/': return '.';
6902 case '\'': return ',';
6903 case 'q': return '/';
6904 case 'w': return '\'';
6905
6906 /* Hebrew letters - set offset from 'a' */
6907 case ',': c = '{'; break;
6908 case '.': c = 'v'; break;
6909 case ';': c = 't'; break;
6910 default: {
6911 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6912
6913#ifdef EBCDIC
6914 /* see note about islower() above */
6915 if (!islower(c))
6916#else
6917 if (c < 'a' || c > 'z')
6918#endif
6919 return c;
6920 c = str[CharOrdLow(c)];
6921 break;
6922 }
6923 }
6924
6925 return (int)(CharOrdLow(c) + p_aleph);
6926 }
6927}
6928#endif
6929
6930 static void
6931ins_reg()
6932{
6933 int need_redraw = FALSE;
6934 int regname;
6935 int literally = 0;
6936
6937 /*
6938 * If we are going to wait for a character, show a '"'.
6939 */
6940 pc_status = PC_STATUS_UNSET;
6941 if (redrawing() && !char_avail())
6942 {
6943 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006944 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 edit_putchar('"', TRUE);
6947#ifdef FEAT_CMDL_INFO
6948 add_to_showcmd_c(Ctrl_R);
6949#endif
6950 }
6951
6952#ifdef USE_ON_FLY_SCROLL
6953 dont_scroll = TRUE; /* disallow scrolling here */
6954#endif
6955
6956 /*
6957 * Don't map the register name. This also prevents the mode message to be
6958 * deleted when ESC is hit.
6959 */
6960 ++no_mapping;
6961 regname = safe_vgetc();
6962#ifdef FEAT_LANGMAP
6963 LANGMAP_ADJUST(regname, TRUE);
6964#endif
6965 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6966 {
6967 /* Get a third key for literal register insertion */
6968 literally = regname;
6969#ifdef FEAT_CMDL_INFO
6970 add_to_showcmd_c(literally);
6971#endif
6972 regname = safe_vgetc();
6973#ifdef FEAT_LANGMAP
6974 LANGMAP_ADJUST(regname, TRUE);
6975#endif
6976 }
6977 --no_mapping;
6978
6979#ifdef FEAT_EVAL
6980 /*
6981 * Don't call u_sync() while getting the expression,
6982 * evaluating it or giving an error message for it!
6983 */
6984 ++no_u_sync;
6985 if (regname == '=')
6986 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006987# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006991# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 /* Restore the Input Method. */
6993 if (im_on)
6994 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006997 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6998 {
6999 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 else
7003 {
7004#endif
7005 if (literally == Ctrl_O || literally == Ctrl_P)
7006 {
7007 /* Append the command to the redo buffer. */
7008 AppendCharToRedobuff(Ctrl_R);
7009 AppendCharToRedobuff(literally);
7010 AppendCharToRedobuff(regname);
7011
7012 do_put(regname, BACKWARD, 1L,
7013 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7014 }
7015 else if (insert_reg(regname, literally) == FAIL)
7016 {
7017 vim_beep();
7018 need_redraw = TRUE; /* remove the '"' */
7019 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007020 else if (stop_insert_mode)
7021 /* When the '=' register was used and a function was invoked that
7022 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7023 * insert anything, need to remove the '"' */
7024 need_redraw = TRUE;
7025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026#ifdef FEAT_EVAL
7027 }
7028 --no_u_sync;
7029#endif
7030#ifdef FEAT_CMDL_INFO
7031 clear_showcmd();
7032#endif
7033
7034 /* If the inserted register is empty, we need to remove the '"' */
7035 if (need_redraw || stuff_empty())
7036 edit_unputchar();
7037}
7038
7039/*
7040 * CTRL-G commands in Insert mode.
7041 */
7042 static void
7043ins_ctrl_g()
7044{
7045 int c;
7046
7047#ifdef FEAT_INS_EXPAND
7048 /* Right after CTRL-X the cursor will be after the ruler. */
7049 setcursor();
7050#endif
7051
7052 /*
7053 * Don't map the second key. This also prevents the mode message to be
7054 * deleted when ESC is hit.
7055 */
7056 ++no_mapping;
7057 c = safe_vgetc();
7058 --no_mapping;
7059 switch (c)
7060 {
7061 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7062 case K_UP:
7063 case Ctrl_K:
7064 case 'k': ins_up(TRUE);
7065 break;
7066
7067 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7068 case K_DOWN:
7069 case Ctrl_J:
7070 case 'j': ins_down(TRUE);
7071 break;
7072
7073 /* CTRL-G u: start new undoable edit */
7074 case 'u': u_sync();
7075 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007076
7077 /* Need to reset Insstart, esp. because a BS that joins
7078 * aline to the previous one must save for undo. */
7079 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 break;
7081
7082 /* Unknown CTRL-G command, reserved for future expansion. */
7083 default: vim_beep();
7084 }
7085}
7086
7087/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007088 * CTRL-^ in Insert mode.
7089 */
7090 static void
7091ins_ctrl_hat()
7092{
7093 if (map_to_exists_mode((char_u *)"", LANGMAP))
7094 {
7095 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7096 if (State & LANGMAP)
7097 {
7098 curbuf->b_p_iminsert = B_IMODE_NONE;
7099 State &= ~LANGMAP;
7100 }
7101 else
7102 {
7103 curbuf->b_p_iminsert = B_IMODE_LMAP;
7104 State |= LANGMAP;
7105#ifdef USE_IM_CONTROL
7106 im_set_active(FALSE);
7107#endif
7108 }
7109 }
7110#ifdef USE_IM_CONTROL
7111 else
7112 {
7113 /* There are no ":lmap" mappings, toggle IM */
7114 if (im_get_status())
7115 {
7116 curbuf->b_p_iminsert = B_IMODE_NONE;
7117 im_set_active(FALSE);
7118 }
7119 else
7120 {
7121 curbuf->b_p_iminsert = B_IMODE_IM;
7122 State &= ~LANGMAP;
7123 im_set_active(TRUE);
7124 }
7125 }
7126#endif
7127 set_iminsert_global();
7128 showmode();
7129#ifdef FEAT_GUI
7130 /* may show different cursor shape or color */
7131 if (gui.in_use)
7132 gui_update_cursor(TRUE, FALSE);
7133#endif
7134#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7135 /* Show/unshow value of 'keymap' in status lines. */
7136 status_redraw_curbuf();
7137#endif
7138}
7139
7140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 * Handle ESC in insert mode.
7142 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7143 * insert.
7144 */
7145 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007146ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 long *count;
7148 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007149 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 int temp;
7152 static int disabled_redraw = FALSE;
7153
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007154#ifdef FEAT_SYN_HL
7155 check_spell_redraw();
7156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157#if defined(FEAT_HANGULIN)
7158# if defined(ESC_CHG_TO_ENG_MODE)
7159 hangul_input_state_set(0);
7160# endif
7161 if (composing_hangul)
7162 {
7163 push_raw_key(composing_hangul_buffer, 2);
7164 composing_hangul = 0;
7165 }
7166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168 temp = curwin->w_cursor.col;
7169 if (disabled_redraw)
7170 {
7171 --RedrawingDisabled;
7172 disabled_redraw = FALSE;
7173 }
7174 if (!arrow_used)
7175 {
7176 /*
7177 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007178 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7179 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 */
7181 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007182 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
7184 /*
7185 * Repeating insert may take a long time. Check for
7186 * interrupt now and then.
7187 */
7188 if (*count > 0)
7189 {
7190 line_breakcheck();
7191 if (got_int)
7192 *count = 0;
7193 }
7194
7195 if (--*count > 0) /* repeat what was typed */
7196 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007197 /* Vi repeats the insert without replacing characters. */
7198 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7199 State &= ~REPLACE_FLAG;
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 (void)start_redo_ins();
7202 if (cmdchar == 'r' || cmdchar == 'v')
7203 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7204 ++RedrawingDisabled;
7205 disabled_redraw = TRUE;
7206 return FALSE; /* repeat the insert */
7207 }
7208 stop_insert(&curwin->w_cursor, TRUE);
7209 undisplay_dollar();
7210 }
7211
7212 /* When an autoindent was removed, curswant stays after the
7213 * indent */
7214 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7215 curwin->w_set_curswant = TRUE;
7216
7217 /* Remember the last Insert position in the '^ mark. */
7218 if (!cmdmod.keepjumps)
7219 curbuf->b_last_insert = curwin->w_cursor;
7220
7221 /*
7222 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007223 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007225 if (!nomove
7226 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227#ifdef FEAT_VIRTUALEDIT
7228 || curwin->w_cursor.coladd > 0
7229#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007230 )
7231 && (restart_edit == NUL
7232 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007234 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007236 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237#ifdef FEAT_RIGHTLEFT
7238 && !revins_on
7239#endif
7240 )
7241 {
7242#ifdef FEAT_VIRTUALEDIT
7243 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7244 {
7245 oneleft();
7246 if (restart_edit != NUL)
7247 ++curwin->w_cursor.coladd;
7248 }
7249 else
7250#endif
7251 {
7252 --curwin->w_cursor.col;
7253#ifdef FEAT_MBYTE
7254 /* Correct cursor for multi-byte character. */
7255 if (has_mbyte)
7256 mb_adjust_cursor();
7257#endif
7258 }
7259 }
7260
7261#ifdef USE_IM_CONTROL
7262 /* Disable IM to allow typing English directly for Normal mode commands.
7263 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7264 * well). */
7265 if (!(State & LANGMAP))
7266 im_save_status(&curbuf->b_p_iminsert);
7267 im_set_active(FALSE);
7268#endif
7269
7270 State = NORMAL;
7271 /* need to position cursor again (e.g. when on a TAB ) */
7272 changed_cline_bef_curs();
7273
7274#ifdef FEAT_MOUSE
7275 setmouse();
7276#endif
7277#ifdef CURSOR_SHAPE
7278 ui_cursor_shape(); /* may show different cursor shape */
7279#endif
7280
7281 /*
7282 * When recording or for CTRL-O, need to display the new mode.
7283 * Otherwise remove the mode message.
7284 */
7285 if (Recording || restart_edit != NUL)
7286 showmode();
7287 else if (p_smd)
7288 MSG("");
7289
7290 return TRUE; /* exit Insert mode */
7291}
7292
7293#ifdef FEAT_RIGHTLEFT
7294/*
7295 * Toggle language: hkmap and revins_on.
7296 * Move to end of reverse inserted text.
7297 */
7298 static void
7299ins_ctrl_()
7300{
7301 if (revins_on && revins_chars && revins_scol >= 0)
7302 {
7303 while (gchar_cursor() != NUL && revins_chars--)
7304 ++curwin->w_cursor.col;
7305 }
7306 p_ri = !p_ri;
7307 revins_on = (State == INSERT && p_ri);
7308 if (revins_on)
7309 {
7310 revins_scol = curwin->w_cursor.col;
7311 revins_legal++;
7312 revins_chars = 0;
7313 undisplay_dollar();
7314 }
7315 else
7316 revins_scol = -1;
7317#ifdef FEAT_FKMAP
7318 if (p_altkeymap)
7319 {
7320 /*
7321 * to be consistent also for redo command, using '.'
7322 * set arrow_used to true and stop it - causing to redo
7323 * characters entered in one mode (normal/reverse insert).
7324 */
7325 arrow_used = TRUE;
7326 (void)stop_arrow();
7327 p_fkmap = curwin->w_p_rl ^ p_ri;
7328 if (p_fkmap && p_ri)
7329 State = INSERT;
7330 }
7331 else
7332#endif
7333 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7334 showmode();
7335}
7336#endif
7337
7338#ifdef FEAT_VISUAL
7339/*
7340 * If 'keymodel' contains "startsel", may start selection.
7341 * Returns TRUE when a CTRL-O and other keys stuffed.
7342 */
7343 static int
7344ins_start_select(c)
7345 int c;
7346{
7347 if (km_startsel)
7348 switch (c)
7349 {
7350 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 case K_PAGEUP:
7353 case K_KPAGEUP:
7354 case K_PAGEDOWN:
7355 case K_KPAGEDOWN:
7356# ifdef MACOS
7357 case K_LEFT:
7358 case K_RIGHT:
7359 case K_UP:
7360 case K_DOWN:
7361 case K_END:
7362 case K_HOME:
7363# endif
7364 if (!(mod_mask & MOD_MASK_SHIFT))
7365 break;
7366 /* FALLTHROUGH */
7367 case K_S_LEFT:
7368 case K_S_RIGHT:
7369 case K_S_UP:
7370 case K_S_DOWN:
7371 case K_S_END:
7372 case K_S_HOME:
7373 /* Start selection right away, the cursor can move with
7374 * CTRL-O when beyond the end of the line. */
7375 start_selection();
7376
7377 /* Execute the key in (insert) Select mode. */
7378 stuffcharReadbuff(Ctrl_O);
7379 if (mod_mask)
7380 {
7381 char_u buf[4];
7382
7383 buf[0] = K_SPECIAL;
7384 buf[1] = KS_MODIFIER;
7385 buf[2] = mod_mask;
7386 buf[3] = NUL;
7387 stuffReadbuff(buf);
7388 }
7389 stuffcharReadbuff(c);
7390 return TRUE;
7391 }
7392 return FALSE;
7393}
7394#endif
7395
7396/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007397 * <Insert> key in Insert mode: toggle insert/remplace mode.
7398 */
7399 static void
7400ins_insert(replaceState)
7401 int replaceState;
7402{
7403#ifdef FEAT_FKMAP
7404 if (p_fkmap && p_ri)
7405 {
7406 beep_flush();
7407 EMSG(farsi_text_3); /* encoded in Farsi */
7408 return;
7409 }
7410#endif
7411
7412#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007413# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007414 set_vim_var_string(VV_INSERTMODE,
7415 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416# ifdef FEAT_VREPLACE
7417 replaceState == VREPLACE ? "v" :
7418# endif
7419 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007420# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007421 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7422#endif
7423 if (State & REPLACE_FLAG)
7424 State = INSERT | (State & LANGMAP);
7425 else
7426 State = replaceState | (State & LANGMAP);
7427 AppendCharToRedobuff(K_INS);
7428 showmode();
7429#ifdef CURSOR_SHAPE
7430 ui_cursor_shape(); /* may show different cursor shape */
7431#endif
7432}
7433
7434/*
7435 * Pressed CTRL-O in Insert mode.
7436 */
7437 static void
7438ins_ctrl_o()
7439{
7440#ifdef FEAT_VREPLACE
7441 if (State & VREPLACE_FLAG)
7442 restart_edit = 'V';
7443 else
7444#endif
7445 if (State & REPLACE_FLAG)
7446 restart_edit = 'R';
7447 else
7448 restart_edit = 'I';
7449#ifdef FEAT_VIRTUALEDIT
7450 if (virtual_active())
7451 ins_at_eol = FALSE; /* cursor always keeps its column */
7452 else
7453#endif
7454 ins_at_eol = (gchar_cursor() == NUL);
7455}
7456
7457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 * If the cursor is on an indent, ^T/^D insert/delete one
7459 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7460 * Always round the indent to 'shiftwith', this is compatible
7461 * with vi. But vi only supports ^T and ^D after an
7462 * autoindent, we support it everywhere.
7463 */
7464 static void
7465ins_shift(c, lastc)
7466 int c;
7467 int lastc;
7468{
7469 if (stop_arrow() == FAIL)
7470 return;
7471 AppendCharToRedobuff(c);
7472
7473 /*
7474 * 0^D and ^^D: remove all indent.
7475 */
7476 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7477 {
7478 --curwin->w_cursor.col;
7479 (void)del_char(FALSE); /* delete the '^' or '0' */
7480 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7481 if (State & REPLACE_FLAG)
7482 replace_pop_ins();
7483 if (lastc == '^')
7484 old_indent = get_indent(); /* remember curr. indent */
7485 change_indent(INDENT_SET, 0, TRUE, 0);
7486 }
7487 else
7488 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7489
7490 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7491 did_ai = FALSE;
7492#ifdef FEAT_SMARTINDENT
7493 did_si = FALSE;
7494 can_si = FALSE;
7495 can_si_back = FALSE;
7496#endif
7497#ifdef FEAT_CINDENT
7498 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7499#endif
7500}
7501
7502 static void
7503ins_del()
7504{
7505 int temp;
7506
7507 if (stop_arrow() == FAIL)
7508 return;
7509 if (gchar_cursor() == NUL) /* delete newline */
7510 {
7511 temp = curwin->w_cursor.col;
7512 if (!can_bs(BS_EOL) /* only if "eol" included */
7513 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7514 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7515 || do_join(FALSE) == FAIL)
7516 vim_beep();
7517 else
7518 curwin->w_cursor.col = temp;
7519 }
7520 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7521 vim_beep();
7522 did_ai = FALSE;
7523#ifdef FEAT_SMARTINDENT
7524 did_si = FALSE;
7525 can_si = FALSE;
7526 can_si_back = FALSE;
7527#endif
7528 AppendCharToRedobuff(K_DEL);
7529}
7530
7531/*
7532 * Handle Backspace, delete-word and delete-line in Insert mode.
7533 * Return TRUE when backspace was actually used.
7534 */
7535 static int
7536ins_bs(c, mode, inserted_space_p)
7537 int c;
7538 int mode;
7539 int *inserted_space_p;
7540{
7541 linenr_T lnum;
7542 int cc;
7543 int temp = 0; /* init for GCC */
7544 colnr_T mincol;
7545 int did_backspace = FALSE;
7546 int in_indent;
7547 int oldState;
7548#ifdef FEAT_MBYTE
7549 int p1, p2;
7550#endif
7551
7552 /*
7553 * can't delete anything in an empty file
7554 * can't backup past first character in buffer
7555 * can't backup past starting point unless 'backspace' > 1
7556 * can backup to a previous line if 'backspace' == 0
7557 */
7558 if ( bufempty()
7559 || (
7560#ifdef FEAT_RIGHTLEFT
7561 !revins_on &&
7562#endif
7563 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7564 || (!can_bs(BS_START)
7565 && (arrow_used
7566 || (curwin->w_cursor.lnum == Insstart.lnum
7567 && curwin->w_cursor.col <= Insstart.col)))
7568 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7569 && curwin->w_cursor.col <= ai_col)
7570 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7571 {
7572 vim_beep();
7573 return FALSE;
7574 }
7575
7576 if (stop_arrow() == FAIL)
7577 return FALSE;
7578 in_indent = inindent(0);
7579#ifdef FEAT_CINDENT
7580 if (in_indent)
7581 can_cindent = FALSE;
7582#endif
7583#ifdef FEAT_COMMENTS
7584 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7585#endif
7586#ifdef FEAT_RIGHTLEFT
7587 if (revins_on) /* put cursor after last inserted char */
7588 inc_cursor();
7589#endif
7590
7591#ifdef FEAT_VIRTUALEDIT
7592 /* Virtualedit:
7593 * BACKSPACE_CHAR eats a virtual space
7594 * BACKSPACE_WORD eats all coladd
7595 * BACKSPACE_LINE eats all coladd and keeps going
7596 */
7597 if (curwin->w_cursor.coladd > 0)
7598 {
7599 if (mode == BACKSPACE_CHAR)
7600 {
7601 --curwin->w_cursor.coladd;
7602 return TRUE;
7603 }
7604 if (mode == BACKSPACE_WORD)
7605 {
7606 curwin->w_cursor.coladd = 0;
7607 return TRUE;
7608 }
7609 curwin->w_cursor.coladd = 0;
7610 }
7611#endif
7612
7613 /*
7614 * delete newline!
7615 */
7616 if (curwin->w_cursor.col == 0)
7617 {
7618 lnum = Insstart.lnum;
7619 if (curwin->w_cursor.lnum == Insstart.lnum
7620#ifdef FEAT_RIGHTLEFT
7621 || revins_on
7622#endif
7623 )
7624 {
7625 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7626 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7627 return FALSE;
7628 --Insstart.lnum;
7629 Insstart.col = MAXCOL;
7630 }
7631 /*
7632 * In replace mode:
7633 * cc < 0: NL was inserted, delete it
7634 * cc >= 0: NL was replaced, put original characters back
7635 */
7636 cc = -1;
7637 if (State & REPLACE_FLAG)
7638 cc = replace_pop(); /* returns -1 if NL was inserted */
7639 /*
7640 * In replace mode, in the line we started replacing, we only move the
7641 * cursor.
7642 */
7643 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7644 {
7645 dec_cursor();
7646 }
7647 else
7648 {
7649#ifdef FEAT_VREPLACE
7650 if (!(State & VREPLACE_FLAG)
7651 || curwin->w_cursor.lnum > orig_line_count)
7652#endif
7653 {
7654 temp = gchar_cursor(); /* remember current char */
7655 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007656
7657 /* When "aw" is in 'formatoptions' we must delete the space at
7658 * the end of the line, otherwise the line will be broken
7659 * again when auto-formatting. */
7660 if (has_format_option(FO_AUTO)
7661 && has_format_option(FO_WHITE_PAR))
7662 {
7663 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7664 TRUE);
7665 int len;
7666
7667 len = STRLEN(ptr);
7668 if (len > 0 && ptr[len - 1] == ' ')
7669 ptr[len - 1] = NUL;
7670 }
7671
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 (void)do_join(FALSE);
7673 if (temp == NUL && gchar_cursor() != NUL)
7674 inc_cursor();
7675 }
7676#ifdef FEAT_VREPLACE
7677 else
7678 dec_cursor();
7679#endif
7680
7681 /*
7682 * In REPLACE mode we have to put back the text that was replaced
7683 * by the NL. On the replace stack is first a NUL-terminated
7684 * sequence of characters that were deleted and then the
7685 * characters that NL replaced.
7686 */
7687 if (State & REPLACE_FLAG)
7688 {
7689 /*
7690 * Do the next ins_char() in NORMAL state, to
7691 * prevent ins_char() from replacing characters and
7692 * avoiding showmatch().
7693 */
7694 oldState = State;
7695 State = NORMAL;
7696 /*
7697 * restore characters (blanks) deleted after cursor
7698 */
7699 while (cc > 0)
7700 {
7701 temp = curwin->w_cursor.col;
7702#ifdef FEAT_MBYTE
7703 mb_replace_pop_ins(cc);
7704#else
7705 ins_char(cc);
7706#endif
7707 curwin->w_cursor.col = temp;
7708 cc = replace_pop();
7709 }
7710 /* restore the characters that NL replaced */
7711 replace_pop_ins();
7712 State = oldState;
7713 }
7714 }
7715 did_ai = FALSE;
7716 }
7717 else
7718 {
7719 /*
7720 * Delete character(s) before the cursor.
7721 */
7722#ifdef FEAT_RIGHTLEFT
7723 if (revins_on) /* put cursor on last inserted char */
7724 dec_cursor();
7725#endif
7726 mincol = 0;
7727 /* keep indent */
7728 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7729#ifdef FEAT_RIGHTLEFT
7730 && !revins_on
7731#endif
7732 )
7733 {
7734 temp = curwin->w_cursor.col;
7735 beginline(BL_WHITE);
7736 if (curwin->w_cursor.col < (colnr_T)temp)
7737 mincol = curwin->w_cursor.col;
7738 curwin->w_cursor.col = temp;
7739 }
7740
7741 /*
7742 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7743 */
7744 if ( mode == BACKSPACE_CHAR
7745 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007746 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 && (*(ml_get_cursor() - 1) == TAB
7748 || (*(ml_get_cursor() - 1) == ' '
7749 && (!*inserted_space_p
7750 || arrow_used))))))
7751 {
7752 int ts;
7753 colnr_T vcol;
7754 colnr_T want_vcol;
7755 int extra = 0;
7756
7757 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007758 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 ts = curbuf->b_p_sw;
7760 else
7761 ts = curbuf->b_p_sts;
7762 /* Compute the virtual column where we want to be. Since
7763 * 'showbreak' may get in the way, need to get the last column of
7764 * the previous character. */
7765 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7766 dec_cursor();
7767 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7768 inc_cursor();
7769 want_vcol = (want_vcol / ts) * ts;
7770
7771 /* delete characters until we are at or before want_vcol */
7772 while (vcol > want_vcol
7773 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7774 {
7775 dec_cursor();
7776 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7777 if (State & REPLACE_FLAG)
7778 {
7779 /* Don't delete characters before the insert point when in
7780 * Replace mode */
7781 if (curwin->w_cursor.lnum != Insstart.lnum
7782 || curwin->w_cursor.col >= Insstart.col)
7783 {
7784#if 0 /* what was this for? It causes problems when sw != ts. */
7785 if (State == REPLACE && (int)vcol < want_vcol)
7786 {
7787 (void)del_char(FALSE);
7788 extra = 2; /* don't pop too much */
7789 }
7790 else
7791#endif
7792 replace_do_bs();
7793 }
7794 }
7795 else
7796 (void)del_char(FALSE);
7797 }
7798
7799 /* insert extra spaces until we are at want_vcol */
7800 while (vcol < want_vcol)
7801 {
7802 /* Remember the first char we inserted */
7803 if (curwin->w_cursor.lnum == Insstart.lnum
7804 && curwin->w_cursor.col < Insstart.col)
7805 Insstart.col = curwin->w_cursor.col;
7806
7807#ifdef FEAT_VREPLACE
7808 if (State & VREPLACE_FLAG)
7809 ins_char(' ');
7810 else
7811#endif
7812 {
7813 ins_str((char_u *)" ");
7814 if ((State & REPLACE_FLAG) && extra <= 1)
7815 {
7816 if (extra)
7817 replace_push_off(NUL);
7818 else
7819 replace_push(NUL);
7820 }
7821 if (extra == 2)
7822 extra = 1;
7823 }
7824 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7825 }
7826 }
7827
7828 /*
7829 * Delete upto starting point, start of line or previous word.
7830 */
7831 else do
7832 {
7833#ifdef FEAT_RIGHTLEFT
7834 if (!revins_on) /* put cursor on char to be deleted */
7835#endif
7836 dec_cursor();
7837
7838 /* start of word? */
7839 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7840 {
7841 mode = BACKSPACE_WORD_NOT_SPACE;
7842 temp = vim_iswordc(gchar_cursor());
7843 }
7844 /* end of word? */
7845 else if (mode == BACKSPACE_WORD_NOT_SPACE
7846 && (vim_isspace(cc = gchar_cursor())
7847 || vim_iswordc(cc) != temp))
7848 {
7849#ifdef FEAT_RIGHTLEFT
7850 if (!revins_on)
7851#endif
7852 inc_cursor();
7853#ifdef FEAT_RIGHTLEFT
7854 else if (State & REPLACE_FLAG)
7855 dec_cursor();
7856#endif
7857 break;
7858 }
7859 if (State & REPLACE_FLAG)
7860 replace_do_bs();
7861 else
7862 {
7863#ifdef FEAT_MBYTE
7864 if (enc_utf8 && p_deco)
7865 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7866#endif
7867 (void)del_char(FALSE);
7868#ifdef FEAT_MBYTE
7869 /*
7870 * If p1 or p2 is non-zero, there are combining characters we
7871 * need to take account of. Don't back up before the base
7872 * character.
7873 */
7874 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7875 inc_cursor();
7876#endif
7877#ifdef FEAT_RIGHTLEFT
7878 if (revins_chars)
7879 {
7880 revins_chars--;
7881 revins_legal++;
7882 }
7883 if (revins_on && gchar_cursor() == NUL)
7884 break;
7885#endif
7886 }
7887 /* Just a single backspace?: */
7888 if (mode == BACKSPACE_CHAR)
7889 break;
7890 } while (
7891#ifdef FEAT_RIGHTLEFT
7892 revins_on ||
7893#endif
7894 (curwin->w_cursor.col > mincol
7895 && (curwin->w_cursor.lnum != Insstart.lnum
7896 || curwin->w_cursor.col != Insstart.col)));
7897 did_backspace = TRUE;
7898 }
7899#ifdef FEAT_SMARTINDENT
7900 did_si = FALSE;
7901 can_si = FALSE;
7902 can_si_back = FALSE;
7903#endif
7904 if (curwin->w_cursor.col <= 1)
7905 did_ai = FALSE;
7906 /*
7907 * It's a little strange to put backspaces into the redo
7908 * buffer, but it makes auto-indent a lot easier to deal
7909 * with.
7910 */
7911 AppendCharToRedobuff(c);
7912
7913 /* If deleted before the insertion point, adjust it */
7914 if (curwin->w_cursor.lnum == Insstart.lnum
7915 && curwin->w_cursor.col < Insstart.col)
7916 Insstart.col = curwin->w_cursor.col;
7917
7918 /* vi behaviour: the cursor moves backward but the character that
7919 * was there remains visible
7920 * Vim behaviour: the cursor moves backward and the character that
7921 * was there is erased from the screen.
7922 * We can emulate the vi behaviour by pretending there is a dollar
7923 * displayed even when there isn't.
7924 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7925 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7926 dollar_vcol = curwin->w_virtcol;
7927
7928 return did_backspace;
7929}
7930
7931#ifdef FEAT_MOUSE
7932 static void
7933ins_mouse(c)
7934 int c;
7935{
7936 pos_T tpos;
7937
7938# ifdef FEAT_GUI
7939 /* When GUI is active, also move/paste when 'mouse' is empty */
7940 if (!gui.in_use)
7941# endif
7942 if (!mouse_has(MOUSE_INSERT))
7943 return;
7944
7945 undisplay_dollar();
7946 tpos = curwin->w_cursor;
7947 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7948 {
7949 start_arrow(&tpos);
7950# ifdef FEAT_CINDENT
7951 can_cindent = TRUE;
7952# endif
7953 }
7954
7955#ifdef FEAT_WINDOWS
7956 /* redraw status lines (in case another window became active) */
7957 redraw_statuslines();
7958#endif
7959}
7960
7961 static void
7962ins_mousescroll(up)
7963 int up;
7964{
7965 pos_T tpos;
7966# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7967 win_T *old_curwin;
7968# endif
7969
7970 tpos = curwin->w_cursor;
7971
7972# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7973 old_curwin = curwin;
7974
7975 /* Currently the mouse coordinates are only known in the GUI. */
7976 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7977 {
7978 int row, col;
7979
7980 row = mouse_row;
7981 col = mouse_col;
7982
7983 /* find the window at the pointer coordinates */
7984 curwin = mouse_find_win(&row, &col);
7985 curbuf = curwin->w_buffer;
7986 }
7987 if (curwin == old_curwin)
7988# endif
7989 undisplay_dollar();
7990
7991 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7992 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7993 else
7994 scroll_redraw(up, 3L);
7995
7996# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7997 curwin->w_redr_status = TRUE;
7998
7999 curwin = old_curwin;
8000 curbuf = curwin->w_buffer;
8001# endif
8002
8003 if (!equalpos(curwin->w_cursor, tpos))
8004 {
8005 start_arrow(&tpos);
8006# ifdef FEAT_CINDENT
8007 can_cindent = TRUE;
8008# endif
8009 }
8010}
8011#endif
8012
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008013#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8014 void
8015ins_tabline(c)
8016 int c;
8017{
8018 /* We will be leaving the current window, unless closing another tab. */
8019 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8020 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8021 {
8022 undisplay_dollar();
8023 start_arrow(&curwin->w_cursor);
8024# ifdef FEAT_CINDENT
8025 can_cindent = TRUE;
8026# endif
8027 }
8028
8029 if (c == K_TABLINE)
8030 goto_tabpage(current_tab);
8031 else
8032 handle_tabmenu();
8033
8034}
8035#endif
8036
8037#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 void
8039ins_scroll()
8040{
8041 pos_T tpos;
8042
8043 undisplay_dollar();
8044 tpos = curwin->w_cursor;
8045 if (gui_do_scroll())
8046 {
8047 start_arrow(&tpos);
8048# ifdef FEAT_CINDENT
8049 can_cindent = TRUE;
8050# endif
8051 }
8052}
8053
8054 void
8055ins_horscroll()
8056{
8057 pos_T tpos;
8058
8059 undisplay_dollar();
8060 tpos = curwin->w_cursor;
8061 if (gui_do_horiz_scroll())
8062 {
8063 start_arrow(&tpos);
8064# ifdef FEAT_CINDENT
8065 can_cindent = TRUE;
8066# endif
8067 }
8068}
8069#endif
8070
8071 static void
8072ins_left()
8073{
8074 pos_T tpos;
8075
8076#ifdef FEAT_FOLDING
8077 if ((fdo_flags & FDO_HOR) && KeyTyped)
8078 foldOpenCursor();
8079#endif
8080 undisplay_dollar();
8081 tpos = curwin->w_cursor;
8082 if (oneleft() == OK)
8083 {
8084 start_arrow(&tpos);
8085#ifdef FEAT_RIGHTLEFT
8086 /* If exit reversed string, position is fixed */
8087 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8088 revins_legal++;
8089 revins_chars++;
8090#endif
8091 }
8092
8093 /*
8094 * if 'whichwrap' set for cursor in insert mode may go to
8095 * previous line
8096 */
8097 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8098 {
8099 start_arrow(&tpos);
8100 --(curwin->w_cursor.lnum);
8101 coladvance((colnr_T)MAXCOL);
8102 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8103 }
8104 else
8105 vim_beep();
8106}
8107
8108 static void
8109ins_home(c)
8110 int c;
8111{
8112 pos_T tpos;
8113
8114#ifdef FEAT_FOLDING
8115 if ((fdo_flags & FDO_HOR) && KeyTyped)
8116 foldOpenCursor();
8117#endif
8118 undisplay_dollar();
8119 tpos = curwin->w_cursor;
8120 if (c == K_C_HOME)
8121 curwin->w_cursor.lnum = 1;
8122 curwin->w_cursor.col = 0;
8123#ifdef FEAT_VIRTUALEDIT
8124 curwin->w_cursor.coladd = 0;
8125#endif
8126 curwin->w_curswant = 0;
8127 start_arrow(&tpos);
8128}
8129
8130 static void
8131ins_end(c)
8132 int c;
8133{
8134 pos_T tpos;
8135
8136#ifdef FEAT_FOLDING
8137 if ((fdo_flags & FDO_HOR) && KeyTyped)
8138 foldOpenCursor();
8139#endif
8140 undisplay_dollar();
8141 tpos = curwin->w_cursor;
8142 if (c == K_C_END)
8143 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8144 coladvance((colnr_T)MAXCOL);
8145 curwin->w_curswant = MAXCOL;
8146
8147 start_arrow(&tpos);
8148}
8149
8150 static void
8151ins_s_left()
8152{
8153#ifdef FEAT_FOLDING
8154 if ((fdo_flags & FDO_HOR) && KeyTyped)
8155 foldOpenCursor();
8156#endif
8157 undisplay_dollar();
8158 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8159 {
8160 start_arrow(&curwin->w_cursor);
8161 (void)bck_word(1L, FALSE, FALSE);
8162 curwin->w_set_curswant = TRUE;
8163 }
8164 else
8165 vim_beep();
8166}
8167
8168 static void
8169ins_right()
8170{
8171#ifdef FEAT_FOLDING
8172 if ((fdo_flags & FDO_HOR) && KeyTyped)
8173 foldOpenCursor();
8174#endif
8175 undisplay_dollar();
8176 if (gchar_cursor() != NUL || virtual_active()
8177 )
8178 {
8179 start_arrow(&curwin->w_cursor);
8180 curwin->w_set_curswant = TRUE;
8181#ifdef FEAT_VIRTUALEDIT
8182 if (virtual_active())
8183 oneright();
8184 else
8185#endif
8186 {
8187#ifdef FEAT_MBYTE
8188 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008189 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 else
8191#endif
8192 ++curwin->w_cursor.col;
8193 }
8194
8195#ifdef FEAT_RIGHTLEFT
8196 revins_legal++;
8197 if (revins_chars)
8198 revins_chars--;
8199#endif
8200 }
8201 /* if 'whichwrap' set for cursor in insert mode, may move the
8202 * cursor to the next line */
8203 else if (vim_strchr(p_ww, ']') != NULL
8204 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8205 {
8206 start_arrow(&curwin->w_cursor);
8207 curwin->w_set_curswant = TRUE;
8208 ++curwin->w_cursor.lnum;
8209 curwin->w_cursor.col = 0;
8210 }
8211 else
8212 vim_beep();
8213}
8214
8215 static void
8216ins_s_right()
8217{
8218#ifdef FEAT_FOLDING
8219 if ((fdo_flags & FDO_HOR) && KeyTyped)
8220 foldOpenCursor();
8221#endif
8222 undisplay_dollar();
8223 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8224 || gchar_cursor() != NUL)
8225 {
8226 start_arrow(&curwin->w_cursor);
8227 (void)fwd_word(1L, FALSE, 0);
8228 curwin->w_set_curswant = TRUE;
8229 }
8230 else
8231 vim_beep();
8232}
8233
8234 static void
8235ins_up(startcol)
8236 int startcol; /* when TRUE move to Insstart.col */
8237{
8238 pos_T tpos;
8239 linenr_T old_topline = curwin->w_topline;
8240#ifdef FEAT_DIFF
8241 int old_topfill = curwin->w_topfill;
8242#endif
8243
8244 undisplay_dollar();
8245 tpos = curwin->w_cursor;
8246 if (cursor_up(1L, TRUE) == OK)
8247 {
8248 if (startcol)
8249 coladvance(getvcol_nolist(&Insstart));
8250 if (old_topline != curwin->w_topline
8251#ifdef FEAT_DIFF
8252 || old_topfill != curwin->w_topfill
8253#endif
8254 )
8255 redraw_later(VALID);
8256 start_arrow(&tpos);
8257#ifdef FEAT_CINDENT
8258 can_cindent = TRUE;
8259#endif
8260 }
8261 else
8262 vim_beep();
8263}
8264
8265 static void
8266ins_pageup()
8267{
8268 pos_T tpos;
8269
8270 undisplay_dollar();
8271 tpos = curwin->w_cursor;
8272 if (onepage(BACKWARD, 1L) == OK)
8273 {
8274 start_arrow(&tpos);
8275#ifdef FEAT_CINDENT
8276 can_cindent = TRUE;
8277#endif
8278 }
8279 else
8280 vim_beep();
8281}
8282
8283 static void
8284ins_down(startcol)
8285 int startcol; /* when TRUE move to Insstart.col */
8286{
8287 pos_T tpos;
8288 linenr_T old_topline = curwin->w_topline;
8289#ifdef FEAT_DIFF
8290 int old_topfill = curwin->w_topfill;
8291#endif
8292
8293 undisplay_dollar();
8294 tpos = curwin->w_cursor;
8295 if (cursor_down(1L, TRUE) == OK)
8296 {
8297 if (startcol)
8298 coladvance(getvcol_nolist(&Insstart));
8299 if (old_topline != curwin->w_topline
8300#ifdef FEAT_DIFF
8301 || old_topfill != curwin->w_topfill
8302#endif
8303 )
8304 redraw_later(VALID);
8305 start_arrow(&tpos);
8306#ifdef FEAT_CINDENT
8307 can_cindent = TRUE;
8308#endif
8309 }
8310 else
8311 vim_beep();
8312}
8313
8314 static void
8315ins_pagedown()
8316{
8317 pos_T tpos;
8318
8319 undisplay_dollar();
8320 tpos = curwin->w_cursor;
8321 if (onepage(FORWARD, 1L) == OK)
8322 {
8323 start_arrow(&tpos);
8324#ifdef FEAT_CINDENT
8325 can_cindent = TRUE;
8326#endif
8327 }
8328 else
8329 vim_beep();
8330}
8331
8332#ifdef FEAT_DND
8333 static void
8334ins_drop()
8335{
8336 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8337}
8338#endif
8339
8340/*
8341 * Handle TAB in Insert or Replace mode.
8342 * Return TRUE when the TAB needs to be inserted like a normal character.
8343 */
8344 static int
8345ins_tab()
8346{
8347 int ind;
8348 int i;
8349 int temp;
8350
8351 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8352 Insstart_blank_vcol = get_nolist_virtcol();
8353 if (echeck_abbr(TAB + ABBR_OFF))
8354 return FALSE;
8355
8356 ind = inindent(0);
8357#ifdef FEAT_CINDENT
8358 if (ind)
8359 can_cindent = FALSE;
8360#endif
8361
8362 /*
8363 * When nothing special, insert TAB like a normal character
8364 */
8365 if (!curbuf->b_p_et
8366 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8367 && curbuf->b_p_sts == 0)
8368 return TRUE;
8369
8370 if (stop_arrow() == FAIL)
8371 return TRUE;
8372
8373 did_ai = FALSE;
8374#ifdef FEAT_SMARTINDENT
8375 did_si = FALSE;
8376 can_si = FALSE;
8377 can_si_back = FALSE;
8378#endif
8379 AppendToRedobuff((char_u *)"\t");
8380
8381 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8382 temp = (int)curbuf->b_p_sw;
8383 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8384 temp = (int)curbuf->b_p_sts;
8385 else /* otherwise use 'tabstop' */
8386 temp = (int)curbuf->b_p_ts;
8387 temp -= get_nolist_virtcol() % temp;
8388
8389 /*
8390 * Insert the first space with ins_char(). It will delete one char in
8391 * replace mode. Insert the rest with ins_str(); it will not delete any
8392 * chars. For VREPLACE mode, we use ins_char() for all characters.
8393 */
8394 ins_char(' ');
8395 while (--temp > 0)
8396 {
8397#ifdef FEAT_VREPLACE
8398 if (State & VREPLACE_FLAG)
8399 ins_char(' ');
8400 else
8401#endif
8402 {
8403 ins_str((char_u *)" ");
8404 if (State & REPLACE_FLAG) /* no char replaced */
8405 replace_push(NUL);
8406 }
8407 }
8408
8409 /*
8410 * When 'expandtab' not set: Replace spaces by TABs where possible.
8411 */
8412 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8413 {
8414 char_u *ptr;
8415#ifdef FEAT_VREPLACE
8416 char_u *saved_line = NULL; /* init for GCC */
8417 pos_T pos;
8418#endif
8419 pos_T fpos;
8420 pos_T *cursor;
8421 colnr_T want_vcol, vcol;
8422 int change_col = -1;
8423 int save_list = curwin->w_p_list;
8424
8425 /*
8426 * Get the current line. For VREPLACE mode, don't make real changes
8427 * yet, just work on a copy of the line.
8428 */
8429#ifdef FEAT_VREPLACE
8430 if (State & VREPLACE_FLAG)
8431 {
8432 pos = curwin->w_cursor;
8433 cursor = &pos;
8434 saved_line = vim_strsave(ml_get_curline());
8435 if (saved_line == NULL)
8436 return FALSE;
8437 ptr = saved_line + pos.col;
8438 }
8439 else
8440#endif
8441 {
8442 ptr = ml_get_cursor();
8443 cursor = &curwin->w_cursor;
8444 }
8445
8446 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8447 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8448 curwin->w_p_list = FALSE;
8449
8450 /* Find first white before the cursor */
8451 fpos = curwin->w_cursor;
8452 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8453 {
8454 --fpos.col;
8455 --ptr;
8456 }
8457
8458 /* In Replace mode, don't change characters before the insert point. */
8459 if ((State & REPLACE_FLAG)
8460 && fpos.lnum == Insstart.lnum
8461 && fpos.col < Insstart.col)
8462 {
8463 ptr += Insstart.col - fpos.col;
8464 fpos.col = Insstart.col;
8465 }
8466
8467 /* compute virtual column numbers of first white and cursor */
8468 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8469 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8470
8471 /* Use as many TABs as possible. Beware of 'showbreak' and
8472 * 'linebreak' adding extra virtual columns. */
8473 while (vim_iswhite(*ptr))
8474 {
8475 i = lbr_chartabsize((char_u *)"\t", vcol);
8476 if (vcol + i > want_vcol)
8477 break;
8478 if (*ptr != TAB)
8479 {
8480 *ptr = TAB;
8481 if (change_col < 0)
8482 {
8483 change_col = fpos.col; /* Column of first change */
8484 /* May have to adjust Insstart */
8485 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8486 Insstart.col = fpos.col;
8487 }
8488 }
8489 ++fpos.col;
8490 ++ptr;
8491 vcol += i;
8492 }
8493
8494 if (change_col >= 0)
8495 {
8496 int repl_off = 0;
8497
8498 /* Skip over the spaces we need. */
8499 while (vcol < want_vcol && *ptr == ' ')
8500 {
8501 vcol += lbr_chartabsize(ptr, vcol);
8502 ++ptr;
8503 ++repl_off;
8504 }
8505 if (vcol > want_vcol)
8506 {
8507 /* Must have a char with 'showbreak' just before it. */
8508 --ptr;
8509 --repl_off;
8510 }
8511 fpos.col += repl_off;
8512
8513 /* Delete following spaces. */
8514 i = cursor->col - fpos.col;
8515 if (i > 0)
8516 {
8517 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8518 /* correct replace stack. */
8519 if ((State & REPLACE_FLAG)
8520#ifdef FEAT_VREPLACE
8521 && !(State & VREPLACE_FLAG)
8522#endif
8523 )
8524 for (temp = i; --temp >= 0; )
8525 replace_join(repl_off);
8526 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008527#ifdef FEAT_NETBEANS_INTG
8528 if (usingNetbeans)
8529 {
8530 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8531 (long)(i + 1));
8532 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8533 (char_u *)"\t", 1);
8534 }
8535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 cursor->col -= i;
8537
8538#ifdef FEAT_VREPLACE
8539 /*
8540 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8541 * backspacing over the changed spacing and then inserting the new
8542 * spacing.
8543 */
8544 if (State & VREPLACE_FLAG)
8545 {
8546 /* Backspace from real cursor to change_col */
8547 backspace_until_column(change_col);
8548
8549 /* Insert each char in saved_line from changed_col to
8550 * ptr-cursor */
8551 ins_bytes_len(saved_line + change_col,
8552 cursor->col - change_col);
8553 }
8554#endif
8555 }
8556
8557#ifdef FEAT_VREPLACE
8558 if (State & VREPLACE_FLAG)
8559 vim_free(saved_line);
8560#endif
8561 curwin->w_p_list = save_list;
8562 }
8563
8564 return FALSE;
8565}
8566
8567/*
8568 * Handle CR or NL in insert mode.
8569 * Return TRUE when out of memory or can't undo.
8570 */
8571 static int
8572ins_eol(c)
8573 int c;
8574{
8575 int i;
8576
8577 if (echeck_abbr(c + ABBR_OFF))
8578 return FALSE;
8579 if (stop_arrow() == FAIL)
8580 return TRUE;
8581 undisplay_dollar();
8582
8583 /*
8584 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8585 * character under the cursor. Only push a NUL on the replace stack,
8586 * nothing to put back when the NL is deleted.
8587 */
8588 if ((State & REPLACE_FLAG)
8589#ifdef FEAT_VREPLACE
8590 && !(State & VREPLACE_FLAG)
8591#endif
8592 )
8593 replace_push(NUL);
8594
8595 /*
8596 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8597 * replacing the next line, so we push all of the characters left on the
8598 * line onto the replace stack. This is not done here though, it is done
8599 * in open_line().
8600 */
8601
8602#ifdef FEAT_RIGHTLEFT
8603# ifdef FEAT_FKMAP
8604 if (p_altkeymap && p_fkmap)
8605 fkmap(NL);
8606# endif
8607 /* NL in reverse insert will always start in the end of
8608 * current line. */
8609 if (revins_on)
8610 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8611#endif
8612
8613 AppendToRedobuff(NL_STR);
8614 i = open_line(FORWARD,
8615#ifdef FEAT_COMMENTS
8616 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8617#endif
8618 0, old_indent);
8619 old_indent = 0;
8620#ifdef FEAT_CINDENT
8621 can_cindent = TRUE;
8622#endif
8623
8624 return (!i);
8625}
8626
8627#ifdef FEAT_DIGRAPHS
8628/*
8629 * Handle digraph in insert mode.
8630 * Returns character still to be inserted, or NUL when nothing remaining to be
8631 * done.
8632 */
8633 static int
8634ins_digraph()
8635{
8636 int c;
8637 int cc;
8638
8639 pc_status = PC_STATUS_UNSET;
8640 if (redrawing() && !char_avail())
8641 {
8642 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008643 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
8645 edit_putchar('?', TRUE);
8646#ifdef FEAT_CMDL_INFO
8647 add_to_showcmd_c(Ctrl_K);
8648#endif
8649 }
8650
8651#ifdef USE_ON_FLY_SCROLL
8652 dont_scroll = TRUE; /* disallow scrolling here */
8653#endif
8654
8655 /* don't map the digraph chars. This also prevents the
8656 * mode message to be deleted when ESC is hit */
8657 ++no_mapping;
8658 ++allow_keys;
8659 c = safe_vgetc();
8660 --no_mapping;
8661 --allow_keys;
8662 if (IS_SPECIAL(c) || mod_mask) /* special key */
8663 {
8664#ifdef FEAT_CMDL_INFO
8665 clear_showcmd();
8666#endif
8667 insert_special(c, TRUE, FALSE);
8668 return NUL;
8669 }
8670 if (c != ESC)
8671 {
8672 if (redrawing() && !char_avail())
8673 {
8674 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008675 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676
8677 if (char2cells(c) == 1)
8678 {
8679 /* first remove the '?', otherwise it's restored when typing
8680 * an ESC next */
8681 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008682 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 edit_putchar(c, TRUE);
8684 }
8685#ifdef FEAT_CMDL_INFO
8686 add_to_showcmd_c(c);
8687#endif
8688 }
8689 ++no_mapping;
8690 ++allow_keys;
8691 cc = safe_vgetc();
8692 --no_mapping;
8693 --allow_keys;
8694 if (cc != ESC)
8695 {
8696 AppendToRedobuff((char_u *)CTRL_V_STR);
8697 c = getdigraph(c, cc, TRUE);
8698#ifdef FEAT_CMDL_INFO
8699 clear_showcmd();
8700#endif
8701 return c;
8702 }
8703 }
8704 edit_unputchar();
8705#ifdef FEAT_CMDL_INFO
8706 clear_showcmd();
8707#endif
8708 return NUL;
8709}
8710#endif
8711
8712/*
8713 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8714 * Returns the char to be inserted, or NUL if none found.
8715 */
8716 static int
8717ins_copychar(lnum)
8718 linenr_T lnum;
8719{
8720 int c;
8721 int temp;
8722 char_u *ptr, *prev_ptr;
8723
8724 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8725 {
8726 vim_beep();
8727 return NUL;
8728 }
8729
8730 /* try to advance to the cursor column */
8731 temp = 0;
8732 ptr = ml_get(lnum);
8733 prev_ptr = ptr;
8734 validate_virtcol();
8735 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8736 {
8737 prev_ptr = ptr;
8738 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8739 }
8740 if ((colnr_T)temp > curwin->w_virtcol)
8741 ptr = prev_ptr;
8742
8743#ifdef FEAT_MBYTE
8744 c = (*mb_ptr2char)(ptr);
8745#else
8746 c = *ptr;
8747#endif
8748 if (c == NUL)
8749 vim_beep();
8750 return c;
8751}
8752
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008753/*
8754 * CTRL-Y or CTRL-E typed in Insert mode.
8755 */
8756 static int
8757ins_ctrl_ey(tc)
8758 int tc;
8759{
8760 int c = tc;
8761
8762#ifdef FEAT_INS_EXPAND
8763 if (ctrl_x_mode == CTRL_X_SCROLL)
8764 {
8765 if (c == Ctrl_Y)
8766 scrolldown_clamp();
8767 else
8768 scrollup_clamp();
8769 redraw_later(VALID);
8770 }
8771 else
8772#endif
8773 {
8774 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8775 if (c != NUL)
8776 {
8777 long tw_save;
8778
8779 /* The character must be taken literally, insert like it
8780 * was typed after a CTRL-V, and pretend 'textwidth'
8781 * wasn't set. Digits, 'o' and 'x' are special after a
8782 * CTRL-V, don't use it for these. */
8783 if (c < 256 && !isalnum(c))
8784 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8785 tw_save = curbuf->b_p_tw;
8786 curbuf->b_p_tw = -1;
8787 insert_special(c, TRUE, FALSE);
8788 curbuf->b_p_tw = tw_save;
8789#ifdef FEAT_RIGHTLEFT
8790 revins_chars++;
8791 revins_legal++;
8792#endif
8793 c = Ctrl_V; /* pretend CTRL-V is last character */
8794 auto_format(FALSE, TRUE);
8795 }
8796 }
8797 return c;
8798}
8799
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800#ifdef FEAT_SMARTINDENT
8801/*
8802 * Try to do some very smart auto-indenting.
8803 * Used when inserting a "normal" character.
8804 */
8805 static void
8806ins_try_si(c)
8807 int c;
8808{
8809 pos_T *pos, old_pos;
8810 char_u *ptr;
8811 int i;
8812 int temp;
8813
8814 /*
8815 * do some very smart indenting when entering '{' or '}'
8816 */
8817 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8818 {
8819 /*
8820 * for '}' set indent equal to indent of line containing matching '{'
8821 */
8822 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8823 {
8824 old_pos = curwin->w_cursor;
8825 /*
8826 * If the matching '{' has a ')' immediately before it (ignoring
8827 * white-space), then line up with the start of the line
8828 * containing the matching '(' if there is one. This handles the
8829 * case where an "if (..\n..) {" statement continues over multiple
8830 * lines -- webb
8831 */
8832 ptr = ml_get(pos->lnum);
8833 i = pos->col;
8834 if (i > 0) /* skip blanks before '{' */
8835 while (--i > 0 && vim_iswhite(ptr[i]))
8836 ;
8837 curwin->w_cursor.lnum = pos->lnum;
8838 curwin->w_cursor.col = i;
8839 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8840 curwin->w_cursor = *pos;
8841 i = get_indent();
8842 curwin->w_cursor = old_pos;
8843#ifdef FEAT_VREPLACE
8844 if (State & VREPLACE_FLAG)
8845 change_indent(INDENT_SET, i, FALSE, NUL);
8846 else
8847#endif
8848 (void)set_indent(i, SIN_CHANGED);
8849 }
8850 else if (curwin->w_cursor.col > 0)
8851 {
8852 /*
8853 * when inserting '{' after "O" reduce indent, but not
8854 * more than indent of previous line
8855 */
8856 temp = TRUE;
8857 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8858 {
8859 old_pos = curwin->w_cursor;
8860 i = get_indent();
8861 while (curwin->w_cursor.lnum > 1)
8862 {
8863 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8864
8865 /* ignore empty lines and lines starting with '#'. */
8866 if (*ptr != '#' && *ptr != NUL)
8867 break;
8868 }
8869 if (get_indent() >= i)
8870 temp = FALSE;
8871 curwin->w_cursor = old_pos;
8872 }
8873 if (temp)
8874 shift_line(TRUE, FALSE, 1);
8875 }
8876 }
8877
8878 /*
8879 * set indent of '#' always to 0
8880 */
8881 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8882 {
8883 /* remember current indent for next line */
8884 old_indent = get_indent();
8885 (void)set_indent(0, SIN_CHANGED);
8886 }
8887
8888 /* Adjust ai_col, the char at this position can be deleted. */
8889 if (ai_col > curwin->w_cursor.col)
8890 ai_col = curwin->w_cursor.col;
8891}
8892#endif
8893
8894/*
8895 * Get the value that w_virtcol would have when 'list' is off.
8896 * Unless 'cpo' contains the 'L' flag.
8897 */
8898 static colnr_T
8899get_nolist_virtcol()
8900{
8901 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8902 return getvcol_nolist(&curwin->w_cursor);
8903 validate_virtcol();
8904 return curwin->w_virtcol;
8905}